PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/src/Antispam/Service.php

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