ActivitySpamPlugin.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. const PLUGIN_VERSION = '2.0.0';
  48. public $server = null;
  49. public $hideSpam = false;
  50. const REVIEWSPAM = 'ActivitySpamPlugin::REVIEWSPAM';
  51. const TRAINSPAM = 'ActivitySpamPlugin::TRAINSPAM';
  52. /**
  53. * Initializer
  54. *
  55. * @return boolean hook value; true means continue processing, false means stop.
  56. */
  57. function initialize()
  58. {
  59. $this->filter = new SpamFilter(common_config('activityspam', 'server'),
  60. common_config('activityspam', 'consumerkey'),
  61. common_config('activityspam', 'secret'));
  62. $this->hideSpam = common_config('activityspam', 'hidespam');
  63. // Let DB_DataObject find Spam_score
  64. common_config_set('db', 'class_location',
  65. common_config('db', 'class_location') .':'.dirname(__FILE__));
  66. return true;
  67. }
  68. /**
  69. * Database schema setup
  70. *
  71. * @see Schema
  72. * @see ColumnDef
  73. *
  74. * @return boolean hook value; true means continue processing, false means stop.
  75. */
  76. function onCheckSchema()
  77. {
  78. $schema = Schema::get();
  79. $schema->ensureTable('spam_score', Spam_score::schemaDef());
  80. Spam_score::upgrade();
  81. return true;
  82. }
  83. /**
  84. * When a notice is saved, check its spam score
  85. *
  86. * @param Notice $notice Notice that was just saved
  87. *
  88. * @return boolean hook value; true means continue processing, false means stop.
  89. */
  90. function onEndNoticeSave($notice)
  91. {
  92. try {
  93. $result = $this->filter->test($notice);
  94. $score = Spam_score::saveNew($notice, $result);
  95. $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
  96. } catch (Exception $e) {
  97. // Log but continue
  98. $this->log(LOG_ERR, $e->getMessage());
  99. }
  100. return true;
  101. }
  102. function onNoticeDeleteRelated($notice) {
  103. $score = Spam_score::getKV('notice_id', $notice->id);
  104. if (!empty($score)) {
  105. $score->delete();
  106. }
  107. return true;
  108. }
  109. function onUserRightsCheck($profile, $right, &$result) {
  110. switch ($right) {
  111. case self::REVIEWSPAM:
  112. case self::TRAINSPAM:
  113. $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
  114. return false;
  115. default:
  116. return true;
  117. }
  118. }
  119. function onGetSpamFilter(&$filter) {
  120. $filter = $this->filter;
  121. return false;
  122. }
  123. function onEndShowNoticeOptionItems($nli)
  124. {
  125. $profile = Profile::current();
  126. if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
  127. $notice = $nli->getNotice();
  128. $out = $nli->getOut();
  129. if (!empty($notice)) {
  130. $score = Spam_score::getKV('notice_id', $notice->id);
  131. if (empty($score)) {
  132. // If it's empty, we can train it.
  133. $form = new TrainSpamForm($out, $notice);
  134. $form->show();
  135. } else if ($score->is_spam) {
  136. $form = new TrainHamForm($out, $notice);
  137. $form->show();
  138. } else if (!$score->is_spam) {
  139. $form = new TrainSpamForm($out, $notice);
  140. $form->show();
  141. }
  142. }
  143. }
  144. return true;
  145. }
  146. /**
  147. * Map URLs to actions
  148. *
  149. * @param URLMapper $m path-to-action mapper
  150. *
  151. * @return boolean hook value; true means continue processing, false means stop.
  152. */
  153. public function onRouterInitialized(URLMapper $m)
  154. {
  155. $m->connect('main/train/spam',
  156. ['action' => 'train',
  157. 'category' => 'spam']);
  158. $m->connect('main/train/ham',
  159. ['action' => 'train',
  160. 'category' => 'ham']);
  161. $m->connect('main/spam',
  162. ['action' => 'spam']);
  163. return true;
  164. }
  165. function onEndShowStyles($action)
  166. {
  167. $action->element('style', null,
  168. '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
  169. '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
  170. return true;
  171. }
  172. function onEndPublicGroupNav($nav)
  173. {
  174. $user = common_current_user();
  175. if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
  176. $nav->out->menuItem(common_local_url('spam'),
  177. _m('MENU','Spam'),
  178. // TRANS: Menu item title in search group navigation panel.
  179. _('Notices marked as spam'),
  180. $nav->actionName == 'spam',
  181. 'nav_timeline_spam');
  182. }
  183. return true;
  184. }
  185. public function onPluginVersion(array &$versions): bool
  186. {
  187. $versions[] = array('name' => 'ActivitySpam',
  188. 'version' => self::PLUGIN_VERSION,
  189. 'author' => 'Evan Prodromou',
  190. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/ActivitySpam',
  191. 'description' =>
  192. _m('Test notices against the Activity Spam service.'));
  193. return true;
  194. }
  195. function onEndNoticeInScope($notice, $profile, &$bResult)
  196. {
  197. if ($this->hideSpam) {
  198. if ($bResult) {
  199. $score = Spam_score::getKV('notice_id', $notice->id);
  200. if (!empty($score) && $score->is_spam) {
  201. if (empty($profile) ||
  202. ($profile->id !== $notice->profile_id &&
  203. !$profile->hasRight(self::REVIEWSPAM))) {
  204. $bResult = false;
  205. }
  206. }
  207. }
  208. }
  209. return true;
  210. }
  211. /**
  212. * Pre-cache our spam scores if needed.
  213. */
  214. function onEndNoticeListPrefill(array &$notices, array &$profiles, array $notice_ids, Profile $scoped=null) {
  215. if ($this->hideSpam) {
  216. Spam_score::multiGet('notice_id', $notice_ids);
  217. }
  218. return true;
  219. }
  220. }