Likes.php 12 KB

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