Exemplo n.º 1
0
 public function spamAction()
 {
     // Get navigation
     $this->view->navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('core_admin_banning', array(), 'core_admin_banning_general');
     // Get form
     $this->view->form = $form = new Core_Form_Admin_Settings_Spam();
     // Get db
     $db = Engine_Db_Table::getDefaultAdapter();
     // Populate some settings
     $settings = Engine_Api::_()->getApi('settings', 'core');
     $config = (array) $settings->core_spam;
     // Load all IPs
     $bannedIpsTable = Engine_Api::_()->getDbtable('BannedIps', 'core');
     $bannedIps = array();
     foreach ($bannedIpsTable->getAddresses() as $bannedIp) {
         if (is_array($bannedIp)) {
             $bannedIps[] = join(' - ', $bannedIp);
         } else {
             if (is_string($bannedIp)) {
                 $bannedIps[] = $bannedIp;
             }
         }
     }
     $config['bannedips'] = join("\n", $bannedIps);
     // Load all emails
     $bannedEmailsTable = Engine_Api::_()->getDbtable('BannedEmails', 'core');
     $bannedEmails = $bannedEmailsTable->getEmails();
     $config['bannedemails'] = join("\n", $bannedEmails);
     // Load all usernames
     $bannedUsernamesTable = Engine_Api::_()->getDbtable('BannedUsernames', 'core');
     $bannedUsernames = $bannedUsernamesTable->getUsernames();
     $config['bannedusernames'] = join("\n", $bannedUsernames);
     // Load all words
     $bannedWordsTable = Engine_Api::_()->getDbtable('BannedWords', 'core');
     $bannedWords = $bannedWordsTable->getWords();
     $config['bannedwords'] = join("\n", $bannedWords);
     // Populate
     if (_ENGINE_ADMIN_NEUTER) {
         $config['recaptchapublic'] = '**********';
         $config['recaptchaprivate'] = '**********';
     }
     $form->populate($config);
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $db = Engine_Api::_()->getDbtable('settings', 'core')->getAdapter();
     $db->beginTransaction();
     $values = $form->getValues();
     // Build banned IPs
     $bannedIpsNew = preg_split('/\\s*[,\\n]+\\s*/', $values['bannedips']);
     foreach ($bannedIpsNew as &$bannedIpNew) {
         if (false !== strpos($bannedIpNew, '-')) {
             $bannedIpNew = preg_split('/\\s*-\\s*/', $bannedIpNew, 2);
         } else {
             if (false != strpos($bannedIpNew, '*')) {
                 $tmp = $bannedIpNew;
                 if (false != strpos($tmp, ':')) {
                     $bannedIpNew = array(str_replace('*', '0', $tmp), str_replace('*', 'ffff', $tmp));
                 } else {
                     $bannedIpNew = array(str_replace('*', '0', $tmp), str_replace('*', '255', $tmp));
                 }
             }
         }
     }
     // Check if they are banning their own address
     if ($bannedIpsTable->isAddressBanned(Engine_IP::getRealRemoteAddress(), $bannedIpsTable->normalizeAddressArray($bannedIpsNew))) {
         return $form->addError('One of the IP addresses or IP address ranges you entered contains your own IP address.');
     }
     if (!empty($values['recaptchapublic']) && !empty($values['recaptchaprivate'])) {
         $recaptcha = new Zend_Service_ReCaptcha($values['recaptchapublic'], $values['recaptchaprivate']);
         try {
             $resp = $recaptcha->verify('test', 'test');
             //        if( false === stripos($resp, 'error') ) {
             //          return $form->addError('ReCaptcha Key Invalid: ' . $resp);
             //        }
             if (in_array($err = $resp->getErrorCode(), array('invalid-site-private-key', 'invalid-site-public-key'))) {
                 return $form->addError('ReCaptcha Error: ' . $err);
             }
             // Validate public key
             $httpClient = new Zend_Http_Client();
             $httpClient->setUri('http://www.google.com/recaptcha/api/challenge');
             $httpClient->setParameterGet('k', $values['recaptchapublic']);
             $resp = $httpClient->request('GET');
             if (false !== stripos($resp->getBody(), 'Input error')) {
                 return $form->addError('ReCaptcha Error: ' . str_replace(array("document.write('", "\\n');"), array('', ''), $resp->getBody()));
             }
         } catch (Exception $e) {
             return $form->addError('ReCaptcha Key Invalid: ' . $e->getMessage());
         }
         $values['recaptchaenabled'] = true;
     } else {
         $values['recaptchaenabled'] = false;
     }
     try {
         if (!empty($bannedIpNew)) {
             // Save Banned IPs
             $bannedIpsTable->setAddresses($bannedIpsNew);
             unset($values['bannedips']);
         }
         // Save Banned Emails
         $bannedEmailsNew = preg_split('/\\s*[,\\n]+\\s*/', $values['bannedemails']);
         $bannedEmailsTable->setEmails($bannedEmailsNew);
         unset($values['bannedemails']);
         // Save Banned Usernames
         $bannedUsernamesNew = preg_split('/\\s*[,\\n]+\\s*/', $values['bannedusernames']);
         $bannedUsernamesTable->setUsernames($bannedUsernamesNew);
         unset($values['bannedusernames']);
         // Save Banned Words
         $bannedWordsNew = preg_split('/\\s*[,\\n]+\\s*/', $values['bannedwords']);
         $bannedWordsTable->setWords($bannedWordsNew);
         unset($values['bannedwords']);
         // Save other settings
         $settings->core_spam = $values;
         $db->commit();
         $form->addNotice('Your changes have been saved.');
     } catch (Exception $e) {
         $db->rollback();
         throw $e;
     }
 }
Exemplo n.º 2
0
 public function spamAction()
 {
     $this->view->form = $form = new Core_Form_Admin_Settings_Spam();
     $settings = Engine_Api::_()->getApi('settings', 'core');
     // Save
     if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
         $db = Engine_Api::_()->getDbtable('settings', 'core')->getAdapter();
         $db->beginTransaction();
         try {
             $settings->core_spam = $this->view->form->getValues();
             $db->commit();
             $form->addNotice('Your changes have been saved.');
         } catch (Exception $e) {
             $db->rollback();
             throw $e;
         }
     }
     if ($settings->core_spam) {
         $this->view->form->populate($settings->core_spam);
     }
 }