Likes.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 2014 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. checkSession('get');
  96. issueLike($like_type, $like_content);
  97. }
  98. }
  99. /**
  100. * @param string $like_type The type of content being liked
  101. * @param integer $like_content The ID of the content being liked
  102. */
  103. function issueLike($like_type, $like_content)
  104. {
  105. global $context, $smcFunc;
  106. // Do we already like this?
  107. $request = $smcFunc['db_query']('', '
  108. SELECT content_id, content_type, id_member
  109. FROM {db_prefix}user_likes
  110. WHERE content_id = {int:like_content}
  111. AND content_type = {string:like_type}
  112. AND id_member = {int:id_member}',
  113. array(
  114. 'like_content' => $like_content,
  115. 'like_type' => $like_type,
  116. 'id_member' => $context['user']['id'],
  117. )
  118. );
  119. $already_liked = $smcFunc['db_num_rows']($request) != 0;
  120. $smcFunc['db_free_result']($request);
  121. if ($already_liked)
  122. {
  123. $smcFunc['db_query']('', '
  124. DELETE FROM {db_prefix}user_likes
  125. WHERE content_id = {int:like_content}
  126. AND content_type = {string:like_type}
  127. AND id_member = {int:id_member}',
  128. array(
  129. 'like_content' => $like_content,
  130. 'like_type' => $like_type,
  131. 'id_member' => $context['user']['id'],
  132. )
  133. );
  134. }
  135. else
  136. {
  137. // Insert the like.
  138. $smcFunc['db_insert']('insert',
  139. '{db_prefix}user_likes',
  140. array('content_id' => 'int', 'content_type' => 'string-6', 'id_member' => 'int', 'like_time' => 'int'),
  141. array($like_content, $like_type, $context['user']['id'], time()),
  142. array('content_id', 'content_type', 'id_member')
  143. );
  144. // Add a background task to process sending alerts.
  145. $smcFunc['db_insert']('insert',
  146. '{db_prefix}background_tasks',
  147. array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
  148. array('$sourcedir/tasks/Likes-Notify.php', 'Likes_Notify_Background', serialize(array(
  149. 'content_id' => $like_content,
  150. 'content_type' => $like_type,
  151. 'sender_id' => $context['user']['id'],
  152. 'sender_name' => $context['user']['name'],
  153. 'time' => time(),
  154. )), 0),
  155. array('id_task')
  156. );
  157. }
  158. // Now, how many people like this content now? We *could* just +1 / -1 the relevant container but that has proven to become unstable.
  159. $request = $smcFunc['db_query']('', '
  160. SELECT COUNT(id_member)
  161. FROM {db_prefix}user_likes
  162. WHERE content_id = {int:like_content}
  163. AND content_type = {string:like_type}',
  164. array(
  165. 'like_content' => $like_content,
  166. 'like_type' => $like_type,
  167. )
  168. );
  169. list ($num_likes) = $smcFunc['db_fetch_row']($request);
  170. $smcFunc['db_free_result']($request);
  171. // Sometimes there might be other things that need updating after we do this like.
  172. call_integration_hook('integrate_issue_like', array($like_type, $like_content, $num_likes));
  173. // Now some clean up. This is provided here for any like handlers that want to do any cache flushing.
  174. // This way a like handler doesn't need to explicitly declare anything in integrate_issue_like, but do so
  175. // in integrate_valid_likes where it absolutely has to exist.
  176. if (!empty($context['flush_cache']))
  177. cache_put_data($context['flush_cache'], null);
  178. if (!empty($context['redirect_from_like']))
  179. redirectexit($context['redirect_from_like']);
  180. else
  181. redirectexit(); // Because we have to go *somewhere*.
  182. }
  183. /**
  184. * Callback attached to integrate_issue_like.
  185. * Partly it indicates how it's supposed to work and partly it deals with updating the count of likes
  186. * attached to this message now.
  187. * @param string $like_type The type of content being liked - should always be 'msg'
  188. * @param int $like_content The ID of the post being liked
  189. * @param int $num_likes The number of likes this message has received
  190. */
  191. function msg_issue_like($like_type, $like_content, $num_likes)
  192. {
  193. global $smcFunc;
  194. if ($like_type !== 'msg')
  195. return;
  196. $smcFunc['db_query']('', '
  197. UPDATE {db_prefix}messages
  198. SET likes = {int:num_likes}
  199. WHERE id_msg = {int:id_msg}',
  200. array(
  201. 'id_msg' => $like_content,
  202. 'num_likes' => $num_likes,
  203. )
  204. );
  205. // Note that we could just as easily have cleared the cache here, or set up the redirection address
  206. // but if your liked content doesn't need to do anything other than have the record in smf_user_likes,
  207. // there's no point in creating another function unnecessarily.
  208. }
  209. /**
  210. * This is for viewing the people who liked a thing.
  211. * Accessed from index.php?action=likes;view and should generally load in a popup.
  212. * We use a template for this in case themers want to style it.
  213. * @param string $like_type The type of content being liked
  214. * @param integer $like_content The ID of the content being liked
  215. */
  216. function viewLikes($like_type, $like_content)
  217. {
  218. global $smcFunc, $txt, $context, $memberContext;
  219. // Firstly, load what we need. We already know we can see this, so that's something.
  220. $context['likers'] = array();
  221. $request = $smcFunc['db_query']('', '
  222. SELECT id_member, like_time
  223. FROM {db_prefix}user_likes
  224. WHERE content_id = {int:like_content}
  225. AND content_type = {string:like_type}
  226. ORDER BY like_time DESC',
  227. array(
  228. 'like_content' => $like_content,
  229. 'like_type' => $like_type,
  230. )
  231. );
  232. while ($row = $smcFunc['db_fetch_assoc']($request))
  233. $context['likers'][$row['id_member']] = array('timestamp' => $row['like_time']);
  234. // Now to get member data, including avatars and so on.
  235. $members = array_keys($context['likers']);
  236. $loaded = loadMemberData($members);
  237. if (count($loaded) != count($members))
  238. {
  239. $members = array_diff($members, $loaded);
  240. foreach ($members as $not_loaded)
  241. unset ($context['likers'][$not_loaded]);
  242. }
  243. foreach ($context['likers'] as $liker => $dummy)
  244. {
  245. $loaded = loadMemberContext($liker);
  246. if (!$loaded)
  247. {
  248. unset ($context['likers'][$liker]);
  249. continue;
  250. }
  251. $context['likers'][$liker]['profile'] = &$memberContext[$liker];
  252. $context['likers'][$liker]['time'] = timeformat($dummy['timestamp']);
  253. }
  254. $count = count($context['likers']);
  255. $title_base = isset($txt['likes_' . $count]) ? 'likes_' . $count : 'likes_n';
  256. $context['page_title'] = strip_tags(sprintf($txt[$title_base], '', comma_format($count)));
  257. // Lastly, setting up for display
  258. loadTemplate('Likes');
  259. loadLanguage('Help'); // for the close window button
  260. $context['template_layers'] = array();
  261. $context['sub_template'] = 'popup';
  262. }
  263. /**
  264. * What's this? I dunno, what are you talking about? Never seen this before, nope. No sir.
  265. */
  266. function BookOfUnknown()
  267. {
  268. global $context, $scripturl;
  269. echo '<!DOCTYPE html>
  270. <html', $context['right_to_left'] ? ' dir="rtl"' : '', '>
  271. <head>
  272. <title>The Book of Unknown, ', @$_GET['verse'] == '2:18' ? '2:18' : '4:16', '</title>
  273. <style type="text/css">
  274. em
  275. {
  276. font-size: 1.3em;
  277. line-height: 0;
  278. }
  279. </style>
  280. </head>
  281. <body style="background-color: #444455; color: white; font-style: italic; font-family: serif;">
  282. <div style="margin-top: 12%; font-size: 1.1em; line-height: 1.4; text-align: center;">';
  283. if (!isset($_GET['verse']) || ($_GET['verse'] != '2:18' && $_GET['verse'] != '22:1-2'))
  284. $_GET['verse'] = '4:16';
  285. if ($_GET['verse'] == '2:18')
  286. echo '
  287. 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.';
  288. elseif ($_GET['verse'] == '4:16')
  289. echo '
  290. 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.';
  291. elseif ($_GET['verse'] == '22:1-2')
  292. echo '
  293. <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>
  294. <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>';
  295. echo '
  296. </div>
  297. <div style="margin-top: 2ex; font-size: 2em; text-align: right;">';
  298. if ($_GET['verse'] == '2:18')
  299. echo '
  300. 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>';
  301. elseif ($_GET['verse'] == '4:16')
  302. echo '
  303. 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>';
  304. elseif ($_GET['verse'] == '22:1-2')
  305. echo '
  306. from <span style="font-family: Georgia, serif;"><strong>The Book of Unknown</strong>, 22:1-2</span>';
  307. echo '
  308. </div>
  309. </body>
  310. </html>';
  311. obExit(false);
  312. }
  313. ?>