Likes.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * This file contains liking posts and displaying the list of who liked a post.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2013 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * The main handler. Verifies permissions (whether the user can see the content in question)
  18. * before either liking/unliking or spitting out the list of likers.
  19. * Accessed from index.php?action=likes
  20. */
  21. function Likes()
  22. {
  23. global $context, $smcFunc;
  24. // Zerothly, they did indicate some kind of content to like, right?
  25. $like_type = isset($_GET['ltype']) ? $_GET['ltype'] : '';
  26. preg_match('~^([a-z0-9\-\_]{1,6})~i', $like_type, $matches);
  27. $like_type = isset($matches[1]) ? $matches[1] : '';
  28. $like_content = isset($_GET['like']) ? (int) $_GET['like'] : 0;
  29. if ($like_type == '' || $like_content <= 0)
  30. fatal_lang_error(isset($_GET['view']) ? 'cannot_view_likes' : 'cannot_like_content', false);
  31. // First we need to verify if the user can see the type of content or not. This is set up to be extensible,
  32. // so we'll check for the one type we do know about, and if it's not that, we'll defer to any hooks.
  33. if ($like_type == 'msg')
  34. {
  35. // So we're doing something off a like. We need to verify that it exists, and that the current user can see it.
  36. // Fortunately for messages, this is quite easy to do - and we'll get the topic id while we're at it, because
  37. // we need this later for other things.
  38. $request = $smcFunc['db_query']('', '
  39. SELECT m.id_topic
  40. FROM {db_prefix}messages AS m
  41. INNER JOIN {db_prefix}boards AS b ON (m.id_board = b.id_board)
  42. WHERE {query_see_board}
  43. AND m.id_msg = {int:msg}',
  44. array(
  45. 'msg' => $like_content,
  46. )
  47. );
  48. if ($smcFunc['db_num_rows']($request) == 1)
  49. list ($id_topic) = $smcFunc['db_fetch_row']($request);
  50. $smcFunc['db_free_result']($request);
  51. if (empty($id_topic))
  52. fatal_lang_error(isset($_GET['view']) ? 'cannot_view_likes' : 'cannot_like_content', false);
  53. // So we know what topic it's in and more importantly we know the user can see it.
  54. // If we're not viewing, we need some info set up.
  55. if (!isset($_GET['view']))
  56. {
  57. $context['flush_cache'] = 'likes_topic_' . $id_topic . '_' . $context['user']['id'];
  58. $context['redirect_from_like'] = 'topic=' . $id_topic . '.msg' . $like_content . '#msg' . $like_content;
  59. add_integration_function('integrate_issue_like', 'msg_issue_like', '', false);
  60. }
  61. }
  62. else
  63. {
  64. // Modders: This will give you whatever the user offers up in terms of liking, e.g. $like_type=msg, $like_content=1
  65. // When you hook this, check $like_type first. If it is not something your mod worries about, return false.
  66. // Otherwise, determine (however you need to) that the user can see the relevant liked content (and it exists).
  67. // If the user cannot see it, return false. If the user can see it and can like it, you MUST return your $like_type back.
  68. // See also issueLike() for further notes.
  69. $can_like = call_integration_hook('integrate_valid_likes', array($like_type, $like_content));
  70. $found = false;
  71. if (!empty($can_like))
  72. {
  73. $can_like = (array) $can_like;
  74. foreach ($can_like as $result)
  75. {
  76. if ($result !== false)
  77. {
  78. $like_type = $result;
  79. $found = true;
  80. break;
  81. }
  82. }
  83. }
  84. if (!$found)
  85. fatal_lang_error(isset($_GET['view']) ? 'cannot_view_likes' : 'cannot_like_content', false);
  86. }
  87. // So at this point, whatever type of like the user supplied and the item of content in question,
  88. // we know it exists, now we need to figure out what we're doing with that.
  89. if (isset($_GET['view']))
  90. viewLikes($like_type, $like_content);
  91. else
  92. {
  93. // Only registered users may actually like content.
  94. is_not_guest();
  95. issueLike($like_type, $like_content);
  96. }
  97. }
  98. function issueLike($like_type, $like_content)
  99. {
  100. global $context, $smcFunc;
  101. // Do we already like this?
  102. $request = $smcFunc['db_query']('', '
  103. SELECT content_id, content_type, id_member
  104. FROM {db_prefix}user_likes
  105. WHERE content_id = {int:like_content}
  106. AND content_type = {string:like_type}
  107. AND id_member = {int:id_member}',
  108. array(
  109. 'like_content' => $like_content,
  110. 'like_type' => $like_type,
  111. 'id_member' => $context['user']['id'],
  112. )
  113. );
  114. $already_liked = $smcFunc['db_num_rows']($request) != 0;
  115. $smcFunc['db_free_result']($request);
  116. if ($already_liked)
  117. {
  118. $smcFunc['db_query']('', '
  119. DELETE FROM {db_prefix}user_likes
  120. WHERE content_id = {int:like_content}
  121. AND content_type = {string:like_type}
  122. AND id_member = {int:id_member}',
  123. array(
  124. 'like_content' => $like_content,
  125. 'like_type' => $like_type,
  126. 'id_member' => $context['user']['id'],
  127. )
  128. );
  129. }
  130. else
  131. {
  132. $smcFunc['db_insert']('insert',
  133. '{db_prefix}user_likes',
  134. array('content_id' => 'int', 'content_type' => 'string-6', 'id_member' => 'int', 'like_time' => 'int'),
  135. array($like_content, $like_type, $context['user']['id'], time()),
  136. array('content_id', 'content_type', 'id_member')
  137. );
  138. }
  139. // Now, how many people like this content now? We *could* just +1 / -1 the relevant container but that has proven to become unstable.
  140. $request = $smcFunc['db_query']('', '
  141. SELECT COUNT(id_member)
  142. FROM {db_prefix}user_likes
  143. WHERE content_id = {int:like_content}
  144. AND content_type = {string:like_type}',
  145. array(
  146. 'like_content' => $like_content,
  147. 'like_type' => $like_type,
  148. )
  149. );
  150. list ($num_likes) = $smcFunc['db_fetch_row']($request);
  151. $smcFunc['db_free_result']($request);
  152. // Sometimes there might be other things that need updating after we do this like.
  153. call_integration_hook('integrate_issue_like', array($like_type, $like_content, $num_likes));
  154. // Now some clean up. This is provided here for any like handlers that want to do any cache flushing.
  155. // This way a like handler doesn't need to explicitly declare anything in integrate_issue_like, but do so
  156. // in integrate_valid_likes where it absolutely has to exist.
  157. if (!empty($context['flush_cache']))
  158. cache_put_data($context['flush_cache'], null);
  159. if (!empty($context['redirect_from_like']))
  160. redirectexit($context['redirect_from_like']);
  161. else
  162. redirectexit(); // Because we have to go *somewhere*.
  163. }
  164. /**
  165. * Callback attached to integrate_issue_like.
  166. * Partly it indicates how it's supposed to work and partly it deals with updating the count of likes
  167. * attached to this message now.
  168. */
  169. function msg_issue_like($like_type, $like_content, $num_likes)
  170. {
  171. global $smcFunc;
  172. if ($like_type !== 'msg')
  173. return;
  174. $smcFunc['db_query']('', '
  175. UPDATE {db_prefix}messages
  176. SET likes = {int:num_likes}
  177. WHERE id_msg = {int:id_msg}',
  178. array(
  179. 'id_msg' => $like_content,
  180. 'num_likes' => $num_likes,
  181. )
  182. );
  183. // Note that we could just as easily have cleared the cache here, or set up the redirection address
  184. // but if your liked content doesn't need to do anything other than have the record in smf_user_likes,
  185. // there's no point in creating another function unnecessarily.
  186. }
  187. /**
  188. * This is for viewing the people who liked a thing.
  189. * Accessed from index.php?action=likes;view and should generally load in a popup.
  190. * We use a template for this in case themers want to style it.
  191. */
  192. function viewLikes($like_type, $like_content)
  193. {
  194. global $smcFunc, $txt, $context, $memberContext;
  195. // Firstly, load what we need. We already know we can see this, so that's something.
  196. $context['likers'] = array();
  197. $request = $smcFunc['db_query']('', '
  198. SELECT id_member, like_time
  199. FROM {db_prefix}user_likes
  200. WHERE content_id = {int:like_content}
  201. AND content_type = {string:like_type}
  202. ORDER BY like_time DESC',
  203. array(
  204. 'like_content' => $like_content,
  205. 'like_type' => $like_type,
  206. )
  207. );
  208. while ($row = $smcFunc['db_fetch_assoc']($request))
  209. $context['likers'][$row['id_member']] = array('timestamp' => $row['like_time']);
  210. // Now to get member data, including avatars and so on.
  211. $members = array_keys($context['likers']);
  212. $loaded = loadMemberData($members);
  213. if (count($loaded) != count($members))
  214. {
  215. $members = array_diff($members, $loaded);
  216. foreach ($members as $not_loaded)
  217. unset ($context['likers'][$not_loaded]);
  218. }
  219. foreach ($context['likers'] as $liker => $dummy)
  220. {
  221. $loaded = loadMemberContext($liker);
  222. if (!$loaded)
  223. {
  224. unset ($context['likers'][$liker]);
  225. continue;
  226. }
  227. $context['likers'][$liker]['profile'] = &$memberContext[$liker];
  228. $context['likers'][$liker]['time'] = timeformat($dummy['timestamp']);
  229. }
  230. $count = count($context['likers']);
  231. $title_base = isset($txt['likes_' . $count]) ? 'likes_' . $count : 'likes_n';
  232. $context['page_title'] = strip_tags(sprintf($txt[$title_base], '', comma_format($count)));
  233. // Lastly, setting up for display
  234. loadTemplate('Likes');
  235. loadLanguage('Help'); // for the close window button
  236. $context['template_layers'] = array();
  237. $context['sub_template'] = 'popup';
  238. }
  239. /**
  240. * What's this? I dunno, what are you talking about? Never seen this before, nope. No sir.
  241. */
  242. function BookOfUnknown()
  243. {
  244. global $context, $scripturl;
  245. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  246. <html xmlns="http://www.w3.org/1999/xhtml"', $context['right_to_left'] ? ' dir="rtl"' : '', '>
  247. <head>
  248. <title>The Book of Unknown, ', @$_GET['verse'] == '2:18' ? '2:18' : '4:16', '</title>
  249. <style type="text/css">
  250. em
  251. {
  252. font-size: 1.3em;
  253. line-height: 0;
  254. }
  255. </style>
  256. </head>
  257. <body style="background-color: #444455; color: white; font-style: italic; font-family: serif;">
  258. <div style="margin-top: 12%; font-size: 1.1em; line-height: 1.4; text-align: center;">';
  259. if (!isset($_GET['verse']) || ($_GET['verse'] != '2:18' && $_GET['verse'] != '22:1-2'))
  260. $_GET['verse'] = '4:16';
  261. if ($_GET['verse'] == '2:18')
  262. echo '
  263. Woe, it was that his name wasn\'t <em>known</em>, that he came in mystery, and was recognized by none.&nbsp;And it became to be in those days <em>something</em>.&nbsp; Something not yet <em id="unknown" name="[Unknown]">unknown</em> to mankind.&nbsp; And thus what was to be known the <em>secret project</em> began into its existence.&nbsp; Henceforth the opposition was only <em>weary</em> and <em>fearful</em>, for now their match was at arms against them.';
  264. elseif ($_GET['verse'] == '4:16')
  265. echo '
  266. And it came to pass that the <em>unbelievers</em> dwindled in number and saw rise of many <em>proselytizers</em>, and the opposition found fear in the face of the <em>x</em> and the <em>j</em> while those who stood with the <em>something</em> grew stronger and came together.&nbsp; Still, this was only the <em>beginning</em>, and what lay in the future was <em id="unknown" name="[Unknown]">unknown</em> to all, even those on the right side.';
  267. elseif ($_GET['verse'] == '22:1-2')
  268. echo '
  269. <p>Now <em>behold</em>, that which was once the secret project was <em id="unknown" name="[Unknown]">unknown</em> no longer.&nbsp; Alas, it needed more than <em>only one</em>, but yet even thought otherwise.&nbsp; It became that the opposition <em>rumored</em> and lied, but still to no avail.&nbsp; Their match, though not <em>perfect</em>, had them outdone.</p>
  270. <p style="margin: 2ex 1ex 0 1ex; font-size: 1.05em; line-height: 1.5; text-align: center;">Let it continue.&nbsp; <em>The end</em>.</p>';
  271. echo '
  272. </div>
  273. <div style="margin-top: 2ex; font-size: 2em; text-align: right;">';
  274. if ($_GET['verse'] == '2:18')
  275. echo '
  276. from <span style="font-family: Georgia, serif;"><strong><a href="', $scripturl, '?action=about:unknown;verse=4:16" style="color: white; text-decoration: none; cursor: text;">The Book of Unknown</a></strong>, 2:18</span>';
  277. elseif ($_GET['verse'] == '4:16')
  278. echo '
  279. from <span style="font-family: Georgia, serif;"><strong><a href="', $scripturl, '?action=about:unknown;verse=22:1-2" style="color: white; text-decoration: none; cursor: text;">The Book of Unknown</a></strong>, 4:16</span>';
  280. elseif ($_GET['verse'] == '22:1-2')
  281. echo '
  282. from <span style="font-family: Georgia, serif;"><strong>The Book of Unknown</strong>, 22:1-2</span>';
  283. echo '
  284. </div>
  285. </body>
  286. </html>';
  287. obExit(false);
  288. }
  289. ?>