AnonymousFavePlugin.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * A plugin to allow anonymous users to favorite notices
  18. *
  19. * If you want to keep certain users from having anonymous faving for their
  20. * notices initialize the plugin with the restricted array, e.g.:
  21. *
  22. * addPlugin(
  23. * 'AnonymousFave',
  24. * ['restricted' => ['spock', 'kirk', 'bones']]
  25. * );
  26. *
  27. * @category Plugin
  28. * @package GNUsocial
  29. * @author Zach Copley <zach@status.net>
  30. * @copyright 2010 StatusNet, Inc.
  31. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  32. */
  33. defined('GNUSOCIAL') || die();
  34. include("../plugins/AnonymousFave/classes/Fave_tally.php");
  35. define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1.0');
  36. /**
  37. * Anonymous Fave plugin to allow anonymous (not logged in) users
  38. * to favorite notices
  39. *
  40. * @category Plugin
  41. * @package GNUsocial
  42. * @author Zach Copley <zach@status.net>
  43. * @copyright 2010 StatusNet, Inc.
  44. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  45. */
  46. class AnonymousFavePlugin extends Plugin
  47. {
  48. // Array of users who should not have anon faving. The default is
  49. // that anonymous faving is allowed for all users.
  50. public $restricted = array();
  51. public function onArgsInitialize()
  52. {
  53. // We always want a session because we're tracking anon users
  54. common_ensure_session();
  55. }
  56. /**
  57. * Hook for ensuring our tables are created
  58. *
  59. * Ensures the fave_tally table is there and has the right columns
  60. *
  61. * @return boolean hook return
  62. */
  63. public function onCheckSchema()
  64. {
  65. $schema = Schema::get();
  66. // For storing total number of times a notice has been faved
  67. $schema->ensureTable('fave_tally', Fave_tally::schemaDef());
  68. return true;
  69. }
  70. public function onEndShowHTML($action)
  71. {
  72. if (!common_logged_in()) {
  73. // Set a place to return to when submitting forms
  74. common_set_returnto($action->selfUrl());
  75. }
  76. }
  77. public function onEndShowScripts($action)
  78. {
  79. // Setup ajax calls for favoriting. Usually this is only done when
  80. // a user is logged in.
  81. $action->inlineScript('SN.U.NoticeFavor();');
  82. }
  83. public function onStartInitializeRouter($m)
  84. {
  85. $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
  86. $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
  87. return true;
  88. }
  89. public function onStartShowNoticeOptionItems(NoticeListItem $item): bool
  90. {
  91. if (!common_logged_in()) {
  92. $item->out->elementStart('div', 'notice-options');
  93. if (Event::handle('StartShowFaveForm', [$item])) {
  94. Event::handle('EndShowFaveForm', [$item]);
  95. }
  96. $item->out->elementEnd('div');
  97. }
  98. return true;
  99. }
  100. public function onStartShowFaveForm($item)
  101. {
  102. if (!common_logged_in() && $this->hasAnonFaving($item)) {
  103. $profile = AnonymousFavePlugin::getAnonProfile();
  104. if ($profile instanceof Profile) {
  105. if (Fave::existsForProfile($item->notice, $profile)) {
  106. $disfavor = new AnonDisFavorForm($item->out, $item->notice);
  107. $disfavor->show();
  108. } else {
  109. $favor = new AnonFavorForm($item->out, $item->notice);
  110. $favor->show();
  111. }
  112. }
  113. }
  114. return true;
  115. }
  116. public function onEndFavorNoticeForm($form, $notice)
  117. {
  118. $this->showTally($form->out, $notice);
  119. }
  120. public function onEndDisFavorNoticeForm($form, $notice)
  121. {
  122. $this->showTally($form->out, $notice);
  123. }
  124. private function showTally($out, Notice $notice): void
  125. {
  126. $tally = Fave_tally::ensureTally($notice->id);
  127. if (!empty($tally)) {
  128. $out->elementStart(
  129. 'div',
  130. array(
  131. 'id' => 'notice-' . $notice->id . '-tally',
  132. 'class' => 'notice-tally'
  133. )
  134. );
  135. $out->elementStart('span', array('class' => 'fave-tally-title'));
  136. // TRANS: Label for tally for number of times a notice was favored.
  137. $out->raw(sprintf(_m("Favored")));
  138. $out->elementEnd('span');
  139. $out->elementStart('span', array('class' => 'fave-tally'));
  140. $out->raw($tally->count);
  141. $out->elementEnd('span');
  142. $out->elementEnd('div');
  143. }
  144. }
  145. public function onEndFavorNotice($profile, $notice)
  146. {
  147. $tally = Fave_tally::increment($notice->id);
  148. }
  149. public function onEndDisfavorNotice($profile, $notice)
  150. {
  151. $tally = Fave_tally::decrement($notice->id);
  152. }
  153. /**
  154. * Remove tally when the notice is deleted
  155. *
  156. * @param Notice $notice Notice being deleted
  157. * @return bool hook value
  158. */
  159. public function onNoticeDeleteRelated(Notice $notice): bool
  160. {
  161. $ft = Fave_tally::getKV('notice_id', $notice->id);
  162. if (!empty($ft)) {
  163. $ft->delete();
  164. }
  165. return true;
  166. }
  167. private static function createAnonProfile(): Profile
  168. {
  169. // Get the anon user's IP, and turn it into a nickname
  170. list($proxy, $ip) = common_client_ip();
  171. // IP + time + random number should help to avoid collisions
  172. $baseNickname = $ip . '-' . time() . '-' . common_random_hexstr(5);
  173. $profile = new Profile();
  174. $profile->nickname = $baseNickname;
  175. $id = $profile->insert();
  176. if (!$id) {
  177. // TRANS: Server exception.
  178. throw new ServerException(_m("Could not create anonymous user session."));
  179. }
  180. // Stick the Profile ID into the nickname
  181. $orig = clone($profile);
  182. $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
  183. $result = $profile->update($orig);
  184. if (!$result) {
  185. // TRANS: Server exception.
  186. throw new ServerException(_m("Could not create anonymous user session."));
  187. }
  188. common_log(
  189. LOG_INFO,
  190. "AnonymousFavePlugin - created profile for anonymous user from IP: "
  191. . $ip
  192. . ', nickname = '
  193. . $profile->nickname
  194. );
  195. return $profile;
  196. }
  197. public static function getAnonProfile()
  198. {
  199. $token = $_SESSION['anon_token'];
  200. $anon = base64_decode($token);
  201. $profile = null;
  202. if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
  203. $parts = explode('-', $anon);
  204. $id = $parts[1];
  205. // Do Profile lookup by ID instead of nickname for safety/performance
  206. $profile = Profile::getKV('id', $id);
  207. } else {
  208. $profile = AnonymousFavePlugin::createAnonProfile();
  209. // Obfuscate so it's hard to figure out the Profile ID
  210. $_SESSION['anon_token'] = base64_encode($profile->nickname);
  211. }
  212. return $profile;
  213. }
  214. /**
  215. * Determine whether a given NoticeListItem should have the
  216. * anonymous fave/disfave form
  217. *
  218. * @param NoticeListItem $item
  219. *
  220. * @return boolean false if the profile associated with the notice is
  221. * in the list of restricted profiles, otherwise
  222. * return true
  223. */
  224. private function hasAnonFaving($item): bool
  225. {
  226. $profile = Profile::getKV('id', $item->notice->profile_id);
  227. if (in_array($profile->nickname, $this->restricted)) {
  228. return false;
  229. }
  230. return true;
  231. }
  232. /**
  233. * Provide plugin version information.
  234. *
  235. * This data is used when showing the version page.
  236. *
  237. * @param array &$versions array of version data arrays; see EVENTS.txt
  238. *
  239. * @return boolean hook value
  240. */
  241. public function onPluginVersion(array &$versions): bool
  242. {
  243. $url = GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/AnonymousFave';
  244. $versions[] = array('name' => 'AnonymousFave',
  245. 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
  246. 'author' => 'Zach Copley',
  247. 'homepage' => $url,
  248. 'rawdescription' =>
  249. // TRANS: Plugin description.
  250. _m('Allow anonymous users to favorite notices.'));
  251. return true;
  252. }
  253. }