ActivitySpamPlugin.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011,2012, StatusNet, Inc.
  5. *
  6. * ActivitySpam Plugin
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Spam
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011,2012 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Check new notices with activity spam service.
  37. *
  38. * @category Spam
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011,2012 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class ActivitySpamPlugin extends Plugin
  46. {
  47. public $server = null;
  48. public $hideSpam = false;
  49. const REVIEWSPAM = 'ActivitySpamPlugin::REVIEWSPAM';
  50. const TRAINSPAM = 'ActivitySpamPlugin::TRAINSPAM';
  51. /**
  52. * Initializer
  53. *
  54. * @return boolean hook value; true means continue processing, false means stop.
  55. */
  56. function initialize()
  57. {
  58. $this->filter = new SpamFilter(common_config('activityspam', 'server'),
  59. common_config('activityspam', 'consumerkey'),
  60. common_config('activityspam', 'secret'));
  61. $this->hideSpam = common_config('activityspam', 'hidespam');
  62. // Let DB_DataObject find Spam_score
  63. common_config_set('db', 'class_location',
  64. common_config('db', 'class_location') .':'.dirname(__FILE__));
  65. return true;
  66. }
  67. /**
  68. * Database schema setup
  69. *
  70. * @see Schema
  71. * @see ColumnDef
  72. *
  73. * @return boolean hook value; true means continue processing, false means stop.
  74. */
  75. function onCheckSchema()
  76. {
  77. $schema = Schema::get();
  78. $schema->ensureTable('spam_score', Spam_score::schemaDef());
  79. Spam_score::upgrade();
  80. return true;
  81. }
  82. /**
  83. * When a notice is saved, check its spam score
  84. *
  85. * @param Notice $notice Notice that was just saved
  86. *
  87. * @return boolean hook value; true means continue processing, false means stop.
  88. */
  89. function onEndNoticeSave($notice)
  90. {
  91. try {
  92. $result = $this->filter->test($notice);
  93. $score = Spam_score::saveNew($notice, $result);
  94. $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
  95. } catch (Exception $e) {
  96. // Log but continue
  97. $this->log(LOG_ERR, $e->getMessage());
  98. }
  99. return true;
  100. }
  101. function onNoticeDeleteRelated($notice) {
  102. $score = Spam_score::getKV('notice_id', $notice->id);
  103. if (!empty($score)) {
  104. $score->delete();
  105. }
  106. return true;
  107. }
  108. function onUserRightsCheck($profile, $right, &$result) {
  109. switch ($right) {
  110. case self::REVIEWSPAM:
  111. case self::TRAINSPAM:
  112. $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
  113. return false;
  114. default:
  115. return true;
  116. }
  117. }
  118. function onGetSpamFilter(&$filter) {
  119. $filter = $this->filter;
  120. return false;
  121. }
  122. function onEndShowNoticeOptionItems($nli)
  123. {
  124. $profile = Profile::current();
  125. if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
  126. $notice = $nli->getNotice();
  127. $out = $nli->getOut();
  128. if (!empty($notice)) {
  129. $score = Spam_score::getKV('notice_id', $notice->id);
  130. if (empty($score)) {
  131. // If it's empty, we can train it.
  132. $form = new TrainSpamForm($out, $notice);
  133. $form->show();
  134. } else if ($score->is_spam) {
  135. $form = new TrainHamForm($out, $notice);
  136. $form->show();
  137. } else if (!$score->is_spam) {
  138. $form = new TrainSpamForm($out, $notice);
  139. $form->show();
  140. }
  141. }
  142. }
  143. return true;
  144. }
  145. /**
  146. * Map URLs to actions
  147. *
  148. * @param URLMapper $m path-to-action mapper
  149. *
  150. * @return boolean hook value; true means continue processing, false means stop.
  151. */
  152. public function onRouterInitialized(URLMapper $m)
  153. {
  154. $m->connect('main/train/spam',
  155. array('action' => 'train', 'category' => 'spam'));
  156. $m->connect('main/train/ham',
  157. array('action' => 'train', 'category' => 'ham'));
  158. $m->connect('main/spam',
  159. array('action' => 'spam'));
  160. return true;
  161. }
  162. function onEndShowStyles($action)
  163. {
  164. $action->element('style', null,
  165. '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
  166. '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
  167. return true;
  168. }
  169. function onEndPublicGroupNav($nav)
  170. {
  171. $user = common_current_user();
  172. if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
  173. $nav->out->menuItem(common_local_url('spam'),
  174. _m('MENU','Spam'),
  175. // TRANS: Menu item title in search group navigation panel.
  176. _('Notices marked as spam'),
  177. $nav->actionName == 'spam',
  178. 'nav_timeline_spam');
  179. }
  180. return true;
  181. }
  182. function onPluginVersion(&$versions)
  183. {
  184. $versions[] = array('name' => 'ActivitySpam',
  185. 'version' => GNUSOCIAL_VERSION,
  186. 'author' => 'Evan Prodromou',
  187. 'homepage' => 'http://status.net/wiki/Plugin:ActivitySpam',
  188. 'description' =>
  189. _m('Test notices against the Activity Spam service.'));
  190. return true;
  191. }
  192. function onEndNoticeInScope($notice, $profile, &$bResult)
  193. {
  194. if ($this->hideSpam) {
  195. if ($bResult) {
  196. $score = Spam_score::getKV('notice_id', $notice->id);
  197. if (!empty($score) && $score->is_spam) {
  198. if (empty($profile) ||
  199. ($profile->id !== $notice->profile_id &&
  200. !$profile->hasRight(self::REVIEWSPAM))) {
  201. $bResult = false;
  202. }
  203. }
  204. }
  205. }
  206. return true;
  207. }
  208. /**
  209. * Pre-cache our spam scores if needed.
  210. */
  211. function onEndNoticeListPrefill(array &$notices, array &$profiles, array $notice_ids, Profile $scoped=null) {
  212. if ($this->hideSpam) {
  213. Spam_score::multiGet('notice_id', $notice_ids);
  214. }
  215. return true;
  216. }
  217. }