Likes.php 12 KB

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