AnonymousFavePlugin.php 8.8 KB

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