train.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2012, StatusNet, Inc.
  5. *
  6. * Train a notice as spam
  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 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. * Train a notice as spam
  37. *
  38. * @category Spam
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 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 TrainAction extends Action
  46. {
  47. protected $notice = null;
  48. protected $filter = null;
  49. protected $category = null;
  50. /**
  51. * For initializing members of the class.
  52. *
  53. * @param array $argarray misc. arguments
  54. *
  55. * @return boolean true
  56. */
  57. function prepare($argarray)
  58. {
  59. parent::prepare($argarray);
  60. // User must be logged in.
  61. $user = common_current_user();
  62. if (empty($user)) {
  63. throw new ClientException(_("You must be logged in to train spam."), 403);
  64. }
  65. // User must have the right to review spam
  66. if (!$user->hasRight(ActivitySpamPlugin::TRAINSPAM)) {
  67. throw new ClientException(_('You cannot review spam on this site.'), 403);
  68. }
  69. $id = $this->trimmed('notice');
  70. $this->notice = Notice::getKV('id', $id);
  71. if (empty($this->notice)) {
  72. throw new ClientException(_("No such notice."));
  73. }
  74. $this->checkSessionToken();
  75. $filter = null;
  76. Event::handle('GetSpamFilter', array(&$filter));
  77. if (empty($filter)) {
  78. throw new ServerException(_("No spam filter configured."));
  79. }
  80. $this->filter = $filter;
  81. $this->category = $this->trimmed('category');
  82. if ($this->category !== SpamFilter::SPAM &&
  83. $this->category !== SpamFilter::HAM)
  84. {
  85. throw new ClientException(_("No such category."));
  86. }
  87. return true;
  88. }
  89. /**
  90. * Handler method
  91. *
  92. * @param array $argarray is ignored since it's now passed in in prepare()
  93. *
  94. * @return void
  95. */
  96. function handle($argarray=null)
  97. {
  98. // Train
  99. $this->filter->trainOnError($this->notice, $this->category);
  100. // Re-test
  101. $result = $this->filter->test($this->notice);
  102. // Update or insert
  103. $score = Spam_score::save($this->notice, $result);
  104. // Show new toggle form
  105. if ($this->category === SpamFilter::SPAM) {
  106. $form = new TrainHamForm($this, $this->notice);
  107. } else {
  108. $form = new TrainSpamForm($this, $this->notice);
  109. }
  110. if ($this->boolean('ajax')) {
  111. $this->startHTML('text/xml;charset=utf-8');
  112. $this->elementStart('head');
  113. // TRANS: Page title for page on which we train the spam filter for ham or spam
  114. $this->element('title', null, _('Train spam filter'));
  115. $this->elementEnd('head');
  116. $this->elementStart('body');
  117. $form->show();
  118. $this->elementEnd('body');
  119. $this->endHTML();
  120. } else {
  121. common_redirect(common_local_url('spam'), 303);
  122. }
  123. }
  124. }