PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/Akismet/class.akismet.plugin.php

https://github.com/Emaratilicious/Addons
PHP | 102 lines | 71 code | 19 blank | 12 comment | 8 complexity | 851c903c16ada26d194310111c965a7b MD5 | raw file
  1. <?php if (!defined('APPLICATION')) exit();
  2. /**
  3. * @copyright Copyright 2008, 2009 Vanilla Forums Inc.
  4. * @license Proprietary
  5. */
  6. // Define the plugin:
  7. $PluginInfo['Akismet'] = array(
  8. 'Name' => 'Akismet',
  9. 'Description' => 'Akismet spam protection integration for Vanilla.',
  10. 'Version' => '1.0b',
  11. 'RequiredApplications' => array('Vanilla' => '2.0.18a1'),
  12. 'SettingsUrl' => '/settings/akismet',
  13. 'SettingsPermission' => 'Garden.Settings.Manage',
  14. 'Author' => 'Todd Burry',
  15. 'AuthorEmail' => 'todd@vanillaforums.com',
  16. 'AuthorUrl' => 'http://www.vanillaforums.org/profile/todd'
  17. );
  18. class AkismetPlugin extends Gdn_Plugin {
  19. /// PROPERTIES ///
  20. /// METHODS ///
  21. public function CheckAkismet($RecordType, $Data) {
  22. $Key = C('Plugins.Akismet.Key');
  23. $UserID = $this->UserID();
  24. if (!$Key || !$UserID)
  25. return FALSE;
  26. static $Akismet;
  27. if (!$Akismet) $Akismet = new Akismet(Gdn::Request()->Url('/', TRUE), $Key);
  28. $Akismet->setCommentAuthor($Data['Username']);
  29. $Akismet->setCommentAuthorEmail($Data['Email']);
  30. $Body = ConcatSep("\n\n", GetValue('Name', $Data), GetValue('Body', $Data), GetValue('Story', $Data));
  31. $Akismet->setCommentContent($Body);
  32. $Akismet->setUserIP($Data['IPAddress']);
  33. $Result = $Akismet->isCommentSpam();
  34. return $Result;
  35. }
  36. public function Structure() {
  37. // Get a user for operations.
  38. $UserID = Gdn::SQL()->GetWhere('User', array('Name' => 'Akismet', 'Admin' => 2))->Value('UserID');
  39. if (!$UserID) {
  40. $UserID = Gdn::SQL()->Insert('User', array(
  41. 'Name' => 'Akismet',
  42. 'Password' => RandomString('20'),
  43. 'HashMethod' => 'Random',
  44. 'Email' => 'akismet@domain.com',
  45. 'DateInserted' => Gdn_Format::ToDateTime(),
  46. 'Admin' => '2'
  47. ));
  48. }
  49. SaveToConfig('Plugins.Akismet.UserID', $UserID);
  50. }
  51. public function UserID() {
  52. return C('Plugins.Akismet.UserID', NULL);
  53. }
  54. /// EVENT HANDLERS ///
  55. public function Base_CheckSpam_Handler($Sender, $Args) {
  56. if ($Args['IsSpam'])
  57. return; // don't double check
  58. $RecordType = $Args['RecordType'];
  59. $Data =& $Args['Data'];
  60. switch ($RecordType) {
  61. case 'User':
  62. // $Data['Name'] = '';
  63. // $Data['Body'] = GetValue('DiscoveryText', $Data);
  64. // $Result = $this->CheckAkismet($RecordType, $Data);
  65. break;
  66. case 'Comment':
  67. case 'Discussion':
  68. case 'Activity':
  69. $Result = $this->CheckAkismet($RecordType, $Data);
  70. if ($Result)
  71. $Data['Log_InsertUserID'] = $this->UserID();
  72. break;
  73. }
  74. $Sender->EventArguments['IsSpam'] = $Result;
  75. }
  76. public function SettingsController_Akismet_Create($Sender, $Args) {
  77. $Sender->SetData('Title', T('Akismet Settings'));
  78. $Cf = new ConfigurationModule($Sender);
  79. $Cf->Initialize(array('Plugins.Akismet.Key' => array('Description' => 'Enter the key you obtained from <a href="http://akismet.com">akismet.com</a>')));
  80. $Sender->AddSideMenu('dashboard/settings/plugins');
  81. $Cf->RenderAll();
  82. }
  83. }