AnonymousFavePlugin.php 8.4 KB

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