antispam
[ class tree: antispam ] [ index: antispam ] [ all elements ]

Source for file antispam.inc.php

Documentation is available at antispam.inc.php

  1. <?php
  2.     /**
  3.      * @author            Matthias Reuter ($LastChangedBy: matthias $)
  4.      * @version            $LastChangedDate: 2009-08-26 19:19:41 +0200 (Mi, 26 Aug 2009) $
  5.      * @package            antispam
  6.      * @copyright        2007-2010 IPBWI development team
  7.      * @link            http://ipbwi.com/examples/attachment.php
  8.      * @since            2.0
  9.      * @license            http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License
  10.      */
  11.     class ipbwi_antispam extends ipbwi {
  12.         private $ipbwi            null;
  13.         private $captchaClass    null;
  14.         private $captcha        null;
  15.         private $uniqueId        null;
  16.         private $recaptchaPrivateKey    ipbwi_RECAPTCHA_PRIVATE_KEY;
  17.         private $recaptchaPublicKey        ipbwi_RECAPTCHA_PUBLIC_KEY;
  18.         /**
  19.          * @desc            Loads and checks different vars when class is initiating
  20.          * @param    object    $ipbwi The ipbwi class object
  21.          * @author            Matthias Reuter
  22.          * @since            2.0
  23.          * @ignore
  24.          */
  25.         public function __construct($ipbwi){
  26.             // loads common classes
  27.             $this->ipbwi $ipbwi;
  28.  
  29.             // loads recaptcha lib
  30.             require_once(ipbwi_ROOT_PATH.'lib/third_party/recaptchalib.inc.php');
  31.  
  32.             // loads IP.Board Captcha Class
  33.             require_once(ipbwi_BOARD_PATH.'ips_kernel/classCaptcha.php');
  34.             $this->captcha    new classCaptcha($this->ipbwi->ips_wrapper->registry,$this->ipbwi->ips_wrapper->settings['bot_antispam_type']);
  35.  
  36.             // if the captcha mode is auto, and the board uses recaptcha we have
  37.             // to use the public and private key that is set in the board
  38.             if(ipbwi_CAPTCHA_MODE == 'auto'){
  39.                 if($this->ipbwi->ips_wrapper->settings['bot_antispam_type'== 'recaptcha'){
  40.                     $this->recaptchaPrivateKey    $this->ipbwi->ips_wrapper->settings['recaptcha_private_key'];
  41.                     $this->recaptchaPublicKey    $this->ipbwi->ips_wrapper->settings['recaptcha_public_key'];
  42.                 }
  43.             }
  44.         }
  45.         /**
  46.          * @desc            Returns the Captcha HTML Code
  47.          * @param    string    $ajaxUrl The URL that should be called to renew the captcha Image (only for GD Based captchas)
  48.          * @return    string    captcha html code
  49.          * @author            Jan Ecker
  50.          * @since            2.0
  51.          */
  52.         public function getHTML($ajaxUrl false){
  53.             if(isset($ajaxUrl)){
  54.                 $ajaxUrl urlencode($ajaxUrl);
  55.             }
  56.             switch(ipbwi_CAPTCHA_MODE){
  57.             case 'gd':
  58.                 return $this->getGdHtml($ajaxUrl);
  59.                 break;
  60.             case 'recaptcha':
  61.                 return $this->getRecaptchaHtml();
  62.                 break;
  63.             case 'auto':
  64.                 if($this->ipbwi->ips_wrapper->settings['bot_antispam_type'== 'recaptcha'){
  65.                     return $this->getRecaptchaHtml();
  66.                 }else{
  67.                     return $this->getGdHtml($ajaxUrl);
  68.                 }
  69.                 break;
  70.             default:
  71.                 die('Wrong config Value, please check ipbwi_CAPTCHA_MODE');
  72.             }
  73.         }
  74.         /**
  75.          * @desc            Checks weather the entered string was correct
  76.          * @return    bool    True if the entered string was correct, otherwise false
  77.          * @author            Jan Ecker
  78.          * @since            2.0
  79.          */
  80.         public function validate(){
  81.             $result    false;
  82.             // Checks which captcha mode was used and than validate it
  83.             if(isset($_POST['ipbwiCaptchaString']&& isset($_POST['ipbwiCaptchaUniqueId'])){
  84.                 $result $this->validateGd($_POST['ipbwiCaptchaUniqueId']$_POST['ipbwiCaptchaString']);
  85.             }elseif(isset($_POST['recaptcha_response_field'])){
  86.                 $result $this->validateRecaptcha($_POST['recaptcha_response_field']$_POST['recaptcha_challenge_field']);
  87.             }
  88.             return $result;
  89.         }
  90.         /**
  91.          * @desc            Generates a new GD Based Captcha
  92.          * @return    string    The unique id of the new GD based captcha
  93.          * @author            Jan Ecker
  94.          * @since            2.0
  95.          */
  96.         public function renewGdImage(){
  97.             // Clean up database
  98.             $this->clearGdDatabase();
  99.             $captchaString    $this->createRandomString();
  100.             $uniqueId        $this->createUniqueId();
  101.             $this->uniqueId    $uniqueId;
  102.             // Save the new captcha data in Database
  103.             $this->ipbwi->ips_wrapper->DB->insert('captcha'array('captcha_unique_id' => $uniqueId'captcha_string' => $captchaString'captcha_ipaddress' => $_SERVER['REMOTE_ADDR']'captcha_date' => time()));
  104.             return $uniqueId;
  105.         }
  106.         /**
  107.          * @desc            Checks if the entered string in the recaptcha was correct
  108.          * @param    string    $responseField The entered captcha string
  109.          * @param    string    $challengeField The unique id of the captcha image
  110.          * @return    bool    True if the entered string was correct, otherwise false
  111.          * @author            Jan Ecker
  112.          * @since            2.0
  113.          */
  114.         private function validateRecaptcha($responseField$challengeField){
  115.             // Send the entered string to the recaptcha server and listen if it was correct
  116.             $resp recaptcha_check_answer($this->recaptchaPrivateKey$_SERVER['REMOTE_ADDR']$challengeField$responseField);
  117.             if($resp->is_valid){
  118.                 return true;
  119.             }else{
  120.                 return false;
  121.             }
  122.         }
  123.         /**
  124.          * @desc            Checks if the entered string in the GD Based captcha was correct
  125.          * @param    string    $uniqueId The unique id of the GD based captcha
  126.          * @param    string    $userCaptchaString The entered captcha string
  127.          * @return    bool    True if the entered String was correct, otherwise false
  128.          * @author            Jan Ecker
  129.          * @since            2.0
  130.          */
  131.         private function validateGd($uniqueId$userCaptchaString){
  132.             // Get the correct Captcha String from the database, using the unique id
  133.             $this->ipbwi->ips_wrapper->DB->query('SELECT captcha_string FROM '.$this->ipbwi->board['sql_tbl_prefix'].'captcha WHERE captcha_unique_id="'.addslashes($uniqueId).'"');
  134.             $row $this->ipbwi->ips_wrapper->DB->fetch();
  135.             if(isset($row&& !empty($row)){
  136.                 // Is not case sesetive! Maybe change later?
  137.                 if(strtoupper($row['captcha_string']== strtoupper($userCaptchaString)){
  138.                     // Captcha was correct! Clear seassion and return true
  139.                     $this->clearGdDatabase();
  140.                     return true;
  141.                 }else{
  142.                     // Captcha was not correct! Report the error
  143.                     $this->ipbwi->addSystemMessage('Error',$this->ipbwi->getLibLang('badKey'),'Located in file '.__FILE__.' at class '.__CLASS__.' in function '.'Located in file <strong>'.__FILE__.'</strong> at class <strong>'.__CLASS__.'</strong> in function <strong>'.__FUNCTION__.'</strong> on line #<strong>'.__LINE__.'</strong>');
  144.                     return false;
  145.                 }
  146.             }else{
  147.                 return false;
  148.             }
  149.         }
  150.         /**
  151.          * @desc            Get the HTML code from the recaptcha server and returns it
  152.          * @return    string    The HTML code to display the recaptcha
  153.          * @author            Jan Ecker
  154.          * @since            2.0
  155.          */
  156.         private function getRecaptchaHtml(){
  157.             $error null;
  158.             // Get the HTML data from the recaptcha server
  159.             return recaptcha_get_html($this->recaptchaPublicKey$error);
  160.         }
  161.         /**
  162.          * @desc            Creates the HTML code for the GD Based captchas and returns it
  163.          * @param    string    $ajaxUrl If given it will additionally return ajax code to refresh the GD based Captcha
  164.          * @return    string    The HTML code to display the GD based captcha
  165.          * @author            Jan Ecker
  166.          * @author            Matthias Reuter
  167.          * @since            2.0
  168.          */
  169.         private function getGdHtml($ajaxUrl false){
  170.             // Create a new image and get the unique id
  171.             $uniqueId    $this->renewGdImage();
  172.  
  173.             // ajax support for on demand reloading of anti_spam-Image
  174.             $ajaxCode    false;
  175.             $ajaxLink    false;
  176.             if(isset($ajaxUrl&& !empty($ajaxUrl)){
  177.                 $ajaxCode =
  178. <<<AJAXCODE
  179.     <script type="text/javascript">
  180.         var keycode_id = '{$uniqueId}';
  181.  
  182.         function get_new_hash(){
  183.             var url = unescape('{$ajaxUrl}');
  184.             var xmlHttp = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  185.             if(xmlHttp){
  186.                 xmlHttp.open('GET', url, true);
  187.                 xmlHttp.onreadystatechange = function (){
  188.                     if(xmlHttp.readyState == 4){
  189.                         keycode_id = xmlHttp.responseText;
  190.                         document.getElementById("anti_spam_image").src = unescape('{$this->ipbwi->getBoardVar('url')}index.php%3Fact%3Dcaptcha%26do%3DshowImage%26captcha_unique_id%3D') + keycode_id;
  191.                         document.getElementById("anti_spam_session_id").value = keycode_id;
  192.                     }
  193.                 }
  194.                 xmlHttp.send(null);
  195.             }
  196.         }
  197.     </script>
  198. AJAXCODE;
  199.                 $ajaxLink ' onclick="get_new_hash();" style="cursor:pointer;" title="Click here to refresh Spam-Image"';
  200.             }
  201.             $html     =    '<p>'.$ajaxCode.'<img'.$ajaxLink.' src="'.$this->ipbwi->getBoardVar('url').'index.php?act=captcha&amp;do=showImage&amp;captcha_unique_id='.$uniqueId.'" alt="Code Bit" id="anti_spam_image" /></p>';
  202.             $html    .=    '<p><input type="hidden" name="ipbwiCaptchaUniqueId" value="'.$this->uniqueId.'" id="anti_spam_session_id" /></p>';
  203.             $html    .=    '<p><strong>Insert Code</strong></p>';
  204.             $html    .=    '<p><input type="text" name="ipbwiCaptchaString" value="" id="keycode" /></p>';
  205.             return $html;
  206.         }
  207.         /**
  208.          * @desc            Clears the database for the GD Based captchas
  209.          * @param    string    $ajaxUrl If given it will additionally return ajax code to refresh the GD based Captcha
  210.          * @return    bool    Always returns true
  211.          * @author            Jan Ecker
  212.          * @since            2.0
  213.          */
  214.         private function clearGdDatabase(){
  215.             $time  time(60*3600;
  216.             // Delete all old entries and the seassion of the actual user
  217.             $this->ipbwi->ips_wrapper->DB->query('DELETE FROM '.$this->ipbwi->board['sql_tbl_prefix'].'captcha WHERE captcha_date < "'.$time.'" OR captcha_ipaddress = "'.$_SERVER['REMOTE_ADDR'].'"');
  218.             return true;
  219.         }
  220.         /**
  221.          * @desc            Generates and returns an unique id
  222.          * @return    string    The generated unique id
  223.          * @author            Jan Ecker
  224.          * @since            2.0
  225.          */
  226.         private function createUniqueId(){
  227.             srand(microtime()*1000000);
  228.             return md5(uniqid(rand()true));
  229.         }
  230.         /**
  231.          * @desc            Generates a random string and returns it
  232.          * @param    int        $length The length of the generated random string
  233.          * @return    string    The generated string
  234.          * @author            Jan Ecker
  235.          * @since            2.0
  236.          */
  237.         private function createRandomString($length 6){
  238.             srand(microtime()*1000000);
  239.             $captchaString    '';
  240.             // Generate an array of all letters and numbers
  241.             $chars    array();
  242.             $smallLetter    'a';
  243.             $bigLetter        'A';
  244.             for($i 0;$i <= 25;$i++){
  245.                 $chars[]    $smallLetter++;
  246.                 $chars[]    $bigLetter++;
  247.             }
  248.             for($i 0;$i <= 9;$i++){
  249.                 $chars[]    $i;
  250.             }
  251.             // Generate a string of the given length with randomly taken letters and numbers
  252.             for($i 0;$i $length;$i++){
  253.                 $index    rand(0count($chars)-1);
  254.                 $captchaString .= $chars[$index];
  255.             }
  256.             return $captchaString;
  257.         }
  258.     }
  259. ?>

Documentation generated on Sat, 23 Oct 2010 23:35:44 +0200 by phpDocumentor 1.4.3