PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/src/Antispam/Service.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 158 lines | 115 code | 14 blank | 29 comment | 20 complexity | 01f65cf40ec7c0c30c470c8ac25fd38e MD5 | raw file
  1. <?php
  2. namespace Concrete\Core\Antispam;
  3. use Config;
  4. use Core;
  5. use Group;
  6. use Loader;
  7. use Log;
  8. use Page;
  9. use User;
  10. use UserInfo;
  11. class Service
  12. {
  13. /**
  14. * @var bool|mixed
  15. */
  16. protected $controller = false;
  17. public function __construct()
  18. {
  19. $library = Library::getActive();
  20. if (is_object($library)) {
  21. $this->controller = $library->getController();
  22. }
  23. }
  24. /**
  25. * @return Group|null
  26. */
  27. public function getWhitelistGroup()
  28. {
  29. return Group::getByID(Config::get('concrete.spam.whitelist_group'));
  30. }
  31. /**
  32. * Report some content with the poster's information to the AntiSpam service
  33. * @param string $content
  34. * @param UserInfo $ui
  35. * @param string $ip
  36. * @param string $ua
  37. * @param array $additionalArgs
  38. */
  39. public function report($content, $author, $email, $ip, $ua, $additionalArgs = array())
  40. {
  41. $args['content'] = $content;
  42. $args['author'] = $author;
  43. $args['author_email'] = $email;
  44. $args['ip_address'] = $ip;
  45. $args['user_agent'] = $ua;
  46. foreach ($additionalArgs as $key => $value) {
  47. $args[$key] = $value;
  48. }
  49. if (method_exists($this->controller, 'report')) {
  50. $this->controller->report($args);
  51. }
  52. $u = new User();
  53. \Log::info(t('Content %s (author %s, %s) flagged as spam by user %s',
  54. $content,
  55. $author,
  56. $email,
  57. $u->getUserName()
  58. ));
  59. }
  60. /**
  61. * @param string $content
  62. * @param string $type
  63. * @param array $additionalArgs
  64. * @param bool $user
  65. * @return bool
  66. * @throws \Exception
  67. */
  68. public function check($content, $type, $additionalArgs = array(), $user = false)
  69. {
  70. if ($this->controller) {
  71. if (!$user) {
  72. $user = new User;
  73. }
  74. $wlg = $this->getWhitelistGroup();
  75. if ($wlg instanceof Group && $user->inGroup($wlg)) {
  76. // Never spam if user is in the whitelist
  77. return true;
  78. }
  79. /** @var \Concrete\Core\Permission\IPService $iph */
  80. $iph = Core::make('helper/validation/ip');
  81. $ip = $iph->getRequestIP();
  82. $args['ip_address'] = ($ip === false)?(''):($ip->getIp($ip::FORMAT_IP_STRING));
  83. $args['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  84. $args['content'] = $content;
  85. foreach ($additionalArgs as $key => $value) {
  86. $args[$key] = $value;
  87. }
  88. if (isset($args['user']) && is_object($args['user'])) {
  89. $u = $args['user'];
  90. } else {
  91. $u = new User();
  92. }
  93. if (!isset($args['email']) && $u->isRegistered()) {
  94. $ui = UserInfo::getByID($u->getUserID());
  95. $args['email'] = $ui->getUserEmail();
  96. }
  97. $r = $this->controller->check($args);
  98. if ($r) {
  99. return true;
  100. } else {
  101. $logText = '';
  102. $c = Page::getCurrentPage();
  103. if (is_object($c)) {
  104. $logText .= t('URL: %s', Loader::helper('navigation')->getLinkToCollection($c, true));
  105. $logText .= "\n";
  106. }
  107. if ($u->isRegistered()) {
  108. $logText .= t('User: %s (ID %s)', $u->getUserName(), $u->getUserID());
  109. $logText .= "\n";
  110. }
  111. $logText .= t('Type: %s', Loader::helper('text')->unhandle($type));
  112. $logText .= "\n";
  113. foreach ($args as $key => $value) {
  114. $logText .= Loader::helper('text')->unhandle($key) . ': ' . $value . "\n";
  115. }
  116. if (Config::get('concrete.log.spam')) {
  117. Log::addEntry($logText, t('spam'));
  118. }
  119. if (Config::get('concrete.spam.notify_email') != '') {
  120. $mh = Loader::helper('mail');
  121. $mh->to(Config::get('concrete.spam.notify_email'));
  122. $mh->addParameter('content', $logText);
  123. $mh->addParameter('siteName', tc('SiteName', Config::get('concrete.site')));
  124. $mh->load('spam_detected');
  125. $mh->sendMail();
  126. }
  127. return false;
  128. }
  129. } else {
  130. return true; // return true if it passes the test
  131. }
  132. }
  133. /**
  134. * @param $nm
  135. * @param $args
  136. * @return mixed
  137. */
  138. public function __call($nm, $args)
  139. {
  140. if (method_exists($this->controller, $nm)) {
  141. return call_user_func_array(array($this->controller, $nm), $args);
  142. }
  143. }
  144. }