public function setUp()
 {
     parent::setUp();
     Injector::nest();
     Injector::inst()->unregisterAllObjects();
     // Mock service
     Config::nest();
     Config::inst()->update('Injector', 'AkismetService', 'AkismetTest_Service');
     Config::inst()->update('AkismetSpamProtector', 'api_key', 'dummykey');
     AkismetSpamProtector::set_api_key(null);
     // Reset options to reasonable default
     Config::inst()->remove('AkismetSpamProtector', 'save_spam');
     Config::inst()->remove('AkismetSpamProtector', 'require_confirmation');
     Config::inst()->remove('AkismetSpamProtector', 'bypass_members');
     Config::inst()->update('AkismetSpamProtector', 'bypass_permission', 'ADMIN');
 }
 public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
 {
     // Skip if database isn't ready
     if (!DB::isActive() || !DB::getConn()->hasField('SiteConfig', 'AkismetKey')) {
         return;
     }
     // Skip if SiteConfig doesn't have this extension
     if (!SiteConfig::has_extension('AkismetConfig')) {
         return;
     }
     // Check if key exists
     $akismetKey = SiteConfig::current_site_config()->AkismetKey;
     if ($akismetKey) {
         AkismetSpamProtector::set_api_key($akismetKey);
     }
 }
 /**
  * This function first gets values from mapped fields and then check these values against
  * Mollom web service and then notify callback object with the spam checking result. 
  * @return 	boolean		- true when Mollom confirms that the submission is ham (not spam)
  *						- false when Mollom confirms that the submission is spam 
  * 						- false when Mollom say 'unsure'. 
  *						  In this case, 'mollom_captcha_requested' session is set to true 
  *       				  so that Field() knows it's time to display captcha 			
  */
 public function validate($validator)
 {
     // Check that, if necessary, the user has given permission to check for spam
     $requireConfirmation = Config::inst()->get('AkismetSpamProtector', 'require_confirmation');
     if ($requireConfirmation && !$this->Value()) {
         $validator->validationError($this->name, _t('AkismetField.NOTIFICATIONREQUIRED', 'You must give consent to submit this content to spam detection'), "error");
         return false;
     }
     // Check bypass permission
     $permission = Config::inst()->get('AkismetSpamProtector', 'bypass_permission');
     if ($permission && Permission::check($permission)) {
         return true;
     }
     // if the user has logged and there's no force check on member
     $bypassMember = Config::inst()->get('AkismetSpamProtector', 'bypass_members');
     if ($bypassMember && Member::currentUser()) {
         return true;
     }
     // Map input fields to spam fields
     $mappedData = $this->getSpamMappedData();
     $content = isset($mappedData['body']) ? $mappedData['body'] : null;
     $author = isset($mappedData['authorName']) ? $mappedData['authorName'] : null;
     $email = isset($mappedData['authorMail']) ? $mappedData['authorMail'] : null;
     $url = isset($mappedData['authorUrl']) ? $mappedData['authorUrl'] : null;
     // Check result
     $isSpam = AkismetSpamProtector::api()->isSpam($content, $author, $email, $url);
     if (!$isSpam) {
         return true;
     }
     // Mark as spam
     $validator->validationError($this->name, _t('AkismetField.SPAM', "Your submission has been rejected because it was treated as spam."), "error");
     return false;
 }
 /**
  * Determine if this field is spam or not
  *
  * @return boolean
  */
 public function getIsSpam()
 {
     // Prevent multiple API calls
     if ($this->isSpam !== null) {
         return $this->isSpam;
     }
     // Check bypass permission
     $permission = Config::inst()->get('AkismetSpamProtector', 'bypass_permission');
     if ($permission && Permission::check($permission)) {
         return false;
     }
     // if the user has logged and there's no force check on member
     $bypassMember = Config::inst()->get('AkismetSpamProtector', 'bypass_members');
     if ($bypassMember && Member::currentUser()) {
         return false;
     }
     // Map input fields to spam fields
     $mappedData = $this->getSpamMappedData();
     $content = isset($mappedData['body']) ? $mappedData['body'] : null;
     $author = isset($mappedData['authorName']) ? $mappedData['authorName'] : null;
     $email = isset($mappedData['authorMail']) ? $mappedData['authorMail'] : null;
     $url = isset($mappedData['authorUrl']) ? $mappedData['authorUrl'] : null;
     // Check result
     $api = AkismetSpamProtector::api();
     $this->isSpam = $api && $api->isSpam($content, $author, $email, $url);
     return $this->isSpam;
 }
 /**
  * Set the API key
  * 
  * @param string $key
  */
 public static function set_api_key($key)
 {
     self::$_api_key = $key;
 }