Source of file MollomField.php
Size: 7,128 Bytes - Last Modified: 2021-12-23T10:33:14+00:00
/var/www/docs.ssmods.com/process/src/code/formfields/MollomField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 | <?php /** * Mollom Form Field. * * The {@link FormField} which is inserted into your form fields via the * spam protector class. * * @package mollom */ class MollomField extends FormField { /** * @var array */ private $fieldMapping = array(); /** * @var string */ private $captchaType = 'image'; /** * @config * * @see setAlwaysShowCaptcha */ private static $always_show_captcha = false; /** * @config * * @see setForceCheckOnMembers */ private static $force_check_on_members = false; /** * @var string */ protected $template = 'MollomField'; /** * @return string */ public function getCaptchaAudioFile() { return MOLLOM_DIR . '/mollom-captcha-player.swf'; } /** * Returns the captcha url if we need to get it from the user * * @return string */ public function getCaptcha() { if($this->getShowCaptcha()) { Requirements::css(MOLLOM_DIR . '/css/mollom.css'); $result = $this->getMollom()->createCaptcha(array( 'type' => $this->getCaptchaType() )); if(is_array($result)) { Session::set('mollom_session_id', $result['id']); return $result['url']; } } return null; } /** * Set the captcha type. Mollom supports "image" and "audio" types. * * @return void */ public function setCaptchaType($Type) { if (in_array($Type, array('image', 'audio'))) { $this->captchaType = $Type; } } /** * Returns the captcha type that has been set. Default is "image". * * @return string */ public function getCaptchaType() { return $this->captchaType; } /** * Determines if the current user is exempt from spam detection * * @return boolean True if the user is exempt from spam detection */ protected function exemptUser() { // Never show captcha for admins if (Permission::check('ADMIN')) { return true; } // Allow logged in members to bypass captcha if allowed if (Member::currentUser() && !Config::inst()->get('MollomField', 'force_check_on_members')) { return true; } return false; } /** * Return if we should show the captcha to the user. * * @return boolean */ public function getShowCaptcha() { // If this user is eligible to bypass spam detection, don't show them the recaptcha if($this->exemptUser()) return false; // Show captcha if always requested if (Config::inst()->get('MollomField', 'always_show_captcha')) return true; // If a captcha is requested then we need to redisplay it to the user if (Session::get('mollom_captcha_requested')) return true; // If there are no field mappings, then the captcha is mandatory return empty($this->fieldMapping); } /** * Returns the field label if showing captcha - used by templates. * * @return string Title if field is showing the captcha */ public function Title() { if ($this->getShowCaptcha()) { return parent::Title(); } } /** * @return MollomSpamProtector */ public function getMollom() { if(!$this->_mollom) { $this->_mollom = Injector::inst()->create('MollomSpamProtector'); $this->_mollom ->publicKey = Config::inst()->get('Mollom', 'public_key'); $this->_mollom ->privateKey = Config::inst()->get('Mollom', 'private_key'); if(Config::inst()->get('Mollom', 'dev')) { $this->_mollom->server = 'dev.mollom.com'; } } return $this->_mollom; } /** * @return array */ public function getSpamMappedData() { if(empty($this->fieldMapping)) return null; $result = array(); $data = $this->form->getData(); foreach($this->fieldMapping as $fieldName => $mappedName) { $result[$mappedName] = (isset($data[$fieldName])) ? $data[$fieldName] : null; } return $result; } /** * Validate the captcha information * * @param Validator $validator * * @return boolean */ public function validate($validator) { // Bypass spam detection for eligible users if($this->exemptUser()) { $this->clearMollomSession(); return true; } $session_id = Session::get("mollom_session_id"); $mapped = $this->getSpamMappedData(); $data = array(); // prepare submission foreach(array('authorName', 'authorUrl', 'authorMail', 'authorIp', 'authorId') as $k) { if(isset($mapped[$k])) { $data[$k] = $mapped[$k]; } } if($session_id) { // session ID exists so has been checked by captcha $data['id'] = $session_id; $data['solution'] = $this->Value(); $result = $this->getMollom()->checkCaptcha($data); if(is_array($result)) { if($result['solved']) { $this->clearMollomSession(); return true; } else { $this->requestMollom(); $validator->validationError( $this->name, _t( 'MollomCaptchaField.CAPTCHAREQUESTED', "Please answer the captcha question", "Mollom Captcha provides words in an image, and expects a user to type them in a textfield" ), "warning" ); return false; } } } else { $contentMap = array( 'id' => 'id', 'title' => 'postTitle', 'body' => 'postBody' ); foreach($contentMap as $k => $v) { if(isset($mapped[$k])) { $data[$v] = $mapped[$k]; } } $result = $this->getMollom()->checkContent($data); // Mollom can do much more useful things. // @todo handle profanityScore, qualityScore, sentimentScore, reason if(is_array($result)) { switch($result['spamClassification']) { case 'ham': $this->clearMollomSession(); return true; case 'unsure': $this->requestMollom(); // we're unsure so request the captcha. $validator->validationError( $this->name, _t( 'MollomCaptchaField.CAPTCHAREQUESTED', "Please answer the captcha question", "Mollom Captcha provides words in an image, and expects a user to type them in a textfield" ), "warning" ); return false; case 'spam': $this->clearMollomSession(); $this->requestMollom(); $validator->validationError( $this->name, _t( 'MollomCaptchaField.SPAM', "Your submission has been rejected because it was treated as spam.", "Mollom Captcha provides words in an image, and expects a user to type them in a textfield" ), "error" ); return false; break; } } } return true; } /** * @return void */ private function requestMollom() { Session::set('mollom_captcha_requested', true); } /** * Helper to quickly clear all the mollom session settings. For example * after a successful post. * * @return void */ private function clearMollomSession() { Session::clear('mollom_session_id'); Session::clear('mollom_captcha_requested'); } /** * Set the fields to map spam protection too * * @param array $fieldMapping array of Field Names, where the indexes of the array are * the field names of the form and the values are the standard spamprotection * fields used by the protector */ public function setFieldMapping($fieldMapping) { $this->fieldMapping = $fieldMapping; } } |