Advertisement
iisisrael

SI CAPTCHA return as string

Feb 6th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 56.52 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: SI CAPTCHA Anti-Spam
  4. Plugin URI: http://www.642weather.com/weather/scripts-wordpress-captcha.php
  5. Description: Adds CAPTCHA anti-spam methods to WordPress forms for comments, registration, lost password, login, or all. This prevents spam from automated bots. WP, WPMU, and BuddyPress compatible. <a href="plugins.php?page=si-captcha-for-wordpress/si-captcha.php">Settings</a> | <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=KXJWLPPWZG83S">Donate</a>
  6. Version: 2.7.7.3
  7. Author: Mike Challis
  8. Author URI: http://www.642weather.com/weather/scripts.php
  9. */
  10.  
  11. $si_captcha_version = '2.7.7.3';
  12.  
  13. /*  Copyright (C) 2008-2013 Mike Challis  (http://www.642weather.com/weather/contact_us.php)
  14.  
  15.     This program is free software; you can redistribute it and/or modify
  16.     it under the terms of the GNU General Public License as published by
  17.     the Free Software Foundation; either version 2 of the License, or
  18.     (at your option) any later version.
  19.  
  20.     This program is distributed in the hope that it will be useful,
  21.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.     GNU General Public License for more details.
  24.  
  25.     You should have received a copy of the GNU General Public License
  26.     along with this program; if not, write to the Free Software
  27.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  28. */
  29.  
  30. // settings get deleted when plugin is deleted from admin plugins page
  31. // this must be outside the class or it does not work
  32. function si_captcha_unset_options() {
  33.  
  34.    if (basename(dirname(__FILE__)) != "mu-plugins")
  35.       delete_option('si_captcha');
  36. }
  37.  
  38. if (!class_exists('siCaptcha')) {
  39.  
  40.  class siCaptcha {
  41.     var $si_captcha_add_script;
  42.     var $si_captcha_version;
  43.  
  44. function si_captcha_add_tabs() {
  45.    global $wpmu, $wp_version;
  46.  
  47.    // for WP 3.0+ ONLY!
  48.    if( $wpmu == 1 && version_compare($wp_version,'3','>=') && is_multisite() && is_super_admin() ) { // wp 3.0 +
  49.      add_submenu_page('ms-admin.php', __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
  50.      add_options_page( __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
  51.    }
  52.    else if ($wpmu == 1 && function_exists('is_site_admin') && is_site_admin()) {
  53.         add_submenu_page('wpmu-admin.php', __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
  54.         add_options_page( __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
  55.    }
  56.    else if ($wpmu != 1) {
  57.         add_submenu_page('plugins.php', __('SI Captcha Options', 'si-captcha'), __('SI Captcha Options', 'si-captcha'), 'manage_options', __FILE__,array(&$this,'si_captcha_options_page'));
  58.    }
  59. }
  60.  
  61. function si_captcha_get_options() {
  62.   global $wpmu, $si_captcha_opt, $si_captcha_option_defaults;
  63.  
  64.   $default_position = ( function_exists('bp_loaded') ) ? 'label-required-input' : 'input-label-required';
  65.  
  66.   $si_captcha_option_defaults = array(
  67.          'si_captcha_donated' => 'false',
  68.          'si_captcha_perm' => 'true',
  69.          'si_captcha_perm_level' => 'read',
  70.          'si_captcha_comment' => 'true',
  71.          'si_captcha_comment_label_position' => $default_position,
  72.          'si_captcha_login' => 'false',
  73.          'si_captcha_register' => 'true',
  74.          'si_captcha_lostpwd'  => 'true',
  75.          'si_captcha_rearrange' => 'true',
  76.          'si_captcha_enable_session' => 'false',
  77.          'si_captcha_captcha_small' => 'false',
  78.          'si_captcha_honeypot_enable' => 'false',
  79.          'si_captcha_aria_required' => 'false',
  80.          'si_captcha_external_style' => 'false',
  81.          'si_captcha_captcha_div_style' =>   'display:block;',
  82.          'si_captcha_captcha_div_style_sm' => 'width:175px; height:45px; padding-top:10px;',
  83.          'si_captcha_captcha_div_style_m'  => 'width:250px; height:60px; padding-top:10px;',
  84.          'si_captcha_captcha_image_style' => 'border-style:none; margin:0; padding-right:5px; float:left;',
  85.          'si_captcha_refresh_image_style' => 'border-style:none; margin:0; vertical-align:bottom;',
  86.          'si_captcha_captcha_input_div_style'  => 'display:block; padding-top:15px; padding-bottom:5px;',
  87.          'si_captcha_comment_label_style' => 'margin:0;',
  88.          'si_captcha_comment_field_style' => 'width:65px;',
  89.          'si_captcha_label_captcha' =>    '',
  90.          'si_captcha_error_spambot' =>    '',
  91.          'si_captcha_error_incorrect' =>    '',
  92.          'si_captcha_error_empty' =>    '',
  93.          'si_captcha_error_token' =>    '',
  94.          'si_captcha_error_unreadable' =>    '',
  95.          'si_captcha_error_cookie' =>    '',
  96.          'si_captcha_error_error' =>    '',
  97.          'si_captcha_required_indicator' => ' *',
  98.          'si_captcha_tooltip_captcha' =>  '',
  99.          'si_captcha_tooltip_refresh' =>  '',
  100.   );
  101.  
  102.   // upgrade path from old version
  103.   if ($wpmu != 1 && !get_option('si_captcha') && get_option('si_captcha_comment')) {
  104.     // just now updating, migrate settings
  105.     $si_captcha_option_defaults = $this->si_captcha_migrate($si_captcha_option_defaults);
  106.   }
  107.  
  108.   // install the option defaults
  109.   if ($wpmu == 1) {
  110.         if( !get_site_option('si_captcha') ) {
  111.           add_site_option('si_captcha', $si_captcha_option_defaults, '', 'yes');
  112.         }
  113.   }else{
  114.         add_option('si_captcha', $si_captcha_option_defaults, '', 'yes');
  115.   }
  116.  
  117.   // get the options from the database
  118.   if ($wpmu == 1)
  119.    $si_captcha_opt = get_site_option('si_captcha'); // get the options from the database
  120.   else
  121.    $si_captcha_opt = get_option('si_captcha');
  122.  
  123.   // array merge incase this version has added new options
  124.   $si_captcha_opt = array_merge($si_captcha_option_defaults, $si_captcha_opt);
  125.  
  126.   // strip slashes on get options array
  127.   foreach($si_captcha_opt as $key => $val) {
  128.            $si_captcha_opt[$key] = $this->si_stripslashes($val);
  129.   }
  130.  
  131.   if ($si_captcha_opt['si_captcha_captcha_image_style'] == '') {
  132.      // if default styles are missing, reset styles
  133.      $style_resets_arr = array('si_captcha_comment_label_style','si_captcha_comment_field_style','si_captcha_captcha_div_style','si_captcha_captcha_div_style_sm','si_captcha_captcha_div_style_m','si_captcha_captcha_input_div_style','si_captcha_captcha_image_style','si_captcha_refresh_image_style');
  134.      foreach($style_resets_arr as $style_reset) {
  135.            $si_captcha_opt[$style_reset] = $si_captcha_option_defaults[$style_reset];
  136.      }
  137.   }
  138.  
  139.     if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
  140.       $si_captcha_opt['si_captcha_login'] = 'false'; // disable captcha on xmlrpc connections
  141.  
  142. } // end function si_captcha_get_options
  143.  
  144. function si_captcha_migrate($si_captcha_option_defaults) {
  145.   // read the options from the prior version
  146.    $new_options = array ();
  147.    foreach($si_captcha_option_defaults as $key => $val) {
  148.       $new_options[$key] = get_option( "$key" );
  149.       // now delete the options from the prior version
  150.       delete_option("$key");
  151.    }
  152.    // now the old settings will carry over to the new version
  153.    return $new_options;
  154. } // end function si_captcha_migrate
  155.  
  156. function si_captcha_options_page() {
  157.   global $wpmu, $si_captcha_dir, $si_captcha_url, $si_captcha_url_ns, $si_captcha_dir_ns, $si_captcha_opt, $si_captcha_option_defaults, $si_captcha_version;
  158.  
  159.   $si_captcha_admin_path = str_replace('/captcha','',$si_captcha_dir);
  160.   if ($wpmu == 1)
  161.      $si_captcha_admin_path = 'si-captcha-for-wordpress';
  162.   require_once($si_captcha_admin_path . '/si-captcha-admin.php');
  163.  
  164. }// end function si_captcha_options_page
  165.  
  166. function si_captcha_perm_dropdown($select_name, $checked_value='') {
  167.         // choices: Display text => permission_level
  168.         $choices = array (
  169.                  __('All registered users', 'si-captcha') => 'read',
  170.                  __('Edit posts', 'si-captcha') => 'edit_posts',
  171.                  __('Publish Posts', 'si-captcha') => 'publish_posts',
  172.                  __('Moderate Comments', 'si-captcha') => 'moderate_comments',
  173.                  __('Administer site', 'si-captcha') => 'level_10'
  174.                  );
  175.         // print the <select> and loop through <options>
  176.         echo '<select name="' . esc_attr($select_name) . '" id="' . esc_attr($select_name) . '">' . "\n";
  177.         foreach ($choices as $text => $capability) :
  178.                 if ($capability == $checked_value) $checked = ' selected="selected" ';
  179.                 echo "\t". '<option value="' . esc_attr($capability) . '"' . $checked . '>'.esc_html($text)."</option>\n";
  180.                 $checked = '';
  181.         endforeach;
  182.         echo "\t</select>\n";
  183.  } // end function si_captcha_perm_dropdown
  184.  
  185. function si_captcha_check_requires() {
  186.   global $si_captcha_dir, $si_captcha_add_script;
  187.  
  188.   $ok = 'ok';
  189.   // Test for some required things, print error message if not OK.
  190.   if ( !extension_loaded('gd') || !function_exists('gd_info') ) {
  191.        echo '<p style="color:maroon">'.__('ERROR: si-captcha.php plugin says GD image support not detected in PHP!', 'si-captcha').'</p>';
  192.        echo '<p>'.__('Contact your web host and ask them why GD image support is not enabled for PHP.', 'si-captcha').'</p>';
  193.       $ok = 'no';
  194.   }
  195.   if ( !function_exists('imagepng') ) {
  196.        echo '<p style="color:maroon">'.__('ERROR: si-captcha.php plugin says imagepng function not detected in PHP!', 'si-captcha').'</p>';
  197.        echo '<p>'.__('Contact your web host and ask them why imagepng function is not enabled for PHP.', 'si-captcha').'</p>';
  198.       $ok = 'no';
  199.   }
  200.   if ( !@strtolower(ini_get('safe_mode')) == 'on' && !file_exists("$si_captcha_dir/securimage.php") ) {
  201.        echo '<p style="color:maroon">'.__('ERROR: si-captcha.php plugin says captcha_library not found.', 'si-captcha').'</p>';
  202.        $ok = 'no';
  203.   }
  204.   if ($ok == 'no')  return false;
  205.   $si_captcha_add_script = true;
  206.   return true;
  207. } // end function si_captcha_check_requires
  208.  
  209. // this function adds the captcha to the comment form
  210. function si_captcha_comment_form() {
  211.     global $si_captcha_url, $si_captcha_opt;
  212.  
  213.     // skip the captcha if user is logged in and the settings allow
  214.     if (is_user_logged_in() && $si_captcha_opt['si_captcha_perm'] == 'true') {
  215.        // skip the CAPTCHA display if the minimum capability is met
  216.        if ( current_user_can( $si_captcha_opt['si_captcha_perm_level'] ) ) {
  217.                // skip capthca
  218.                return true;
  219.        }
  220.     }
  221.  
  222. // the captcha html
  223. echo '
  224. <div id="captchaImgDiv">
  225. ';
  226.  
  227. // Test for some required things, print error message right here if not OK.
  228. if ($this->si_captcha_check_requires()) {
  229.  
  230.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  231.  
  232. // the captcha html - comment form 2.0
  233. echo '
  234. <div ';
  235. echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
  236. echo '>';
  237. $this->si_captcha_captcha_html('si_image_com','com');
  238. echo '</div>
  239. <div id="captchaInputDiv">';
  240.  
  241. $label_string = ' <label id="captcha_code_label" for="captcha_code">';
  242. $label_string .= ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  243. $label_string .= '</label>';
  244. $required_string = '<span class="required">'.$si_captcha_opt['si_captcha_required_indicator']."</span>\n";
  245. $input_string = '<input id="captcha_code" name="captcha_code" type="text" value="" tabindex="4" '.$si_aria_required.' />
  246. ';
  247.  
  248.  if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-input' || $si_captcha_opt['si_captcha_comment_label_position'] == 'left'  ) { // buddypress (label-required-input)(label left)
  249.       echo $label_string . $required_string . $input_string; // BP
  250.  } else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-linebreak-input' ||  $si_captcha_opt['si_captcha_comment_label_position'] == 'top' ) {
  251.       echo $label_string . $required_string .'<br />'. $input_string; // regular WP - twenty ten
  252.  } else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-input-required' ||  $si_captcha_opt['si_captcha_comment_label_position'] == 'right' ) {
  253.       echo $label_string . $input_string . $required_string; // suffusion
  254.  } else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'input-label-required' ) {
  255.       echo $input_string . $label_string . $required_string; // regular WP
  256.  } else {
  257.       echo $input_string . $label_string . $required_string;  // regular WP
  258.  }
  259.  
  260. echo ' </div>
  261. </div>
  262. ';
  263.  
  264. // rearrange submit button display order
  265. if ($si_captcha_opt['si_captcha_rearrange'] == 'true') {
  266.      print  <<<EOT
  267.       <script type='text/javascript'>
  268.           var sUrlInput = document.getElementById("comment");
  269.                   var oParent = sUrlInput.parentNode;
  270.           var sSubstitue = document.getElementById("captchaImgDiv");
  271.                   oParent.appendChild(sSubstitue, sUrlInput);
  272.       </script>
  273.             <noscript>
  274.           <style type='text/css'>#submit {display:none;}</style><br />
  275. EOT;
  276.   echo '           <input name="submit" type="submit" id="submit-alt" tabindex="6" value="'.__('Submit Comment', 'si-captcha').'" />
  277.          </noscript>
  278.  ';
  279.  
  280. }
  281. }else{
  282.  echo '</div>';
  283. }
  284.     return true;
  285. } // end function si_captcha_comment_form  2.0
  286.  
  287.  
  288. // this function adds the captcha to the comment form WP3
  289. function si_captcha_comment_form_wp3() {
  290.     global $si_captcha_url, $si_captcha_opt;
  291.  
  292.     // skip the captcha if user is logged in and the settings allow
  293.     if (is_user_logged_in() && $si_captcha_opt['si_captcha_perm'] == 'true') {
  294.        // skip the CAPTCHA display if the minimum capability is met
  295.        if ( current_user_can( $si_captcha_opt['si_captcha_perm_level'] ) ) {
  296.                // skip capthca
  297.                return true;
  298.        }
  299.     }
  300.  
  301. // the captch html
  302. // Test for some required things, print error message right here if not OK.
  303. if ($this->si_captcha_check_requires()) {
  304.  
  305.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  306.  
  307. // the captcha html - comment form 3.0+
  308. if (is_user_logged_in()) {
  309.       echo '<br />';
  310. }
  311.  
  312. echo '
  313. <div ';
  314. echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
  315. echo '>';
  316. $this->si_captcha_captcha_html('si_image_com','com');
  317. echo '</div>
  318. <br />
  319. ';
  320. echo '<p>';
  321.  
  322. $label_string = '<label id="captcha_code_label" for="captcha_code" >';
  323. $label_string .= ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  324. $label_string .= '</label>';
  325. $required_string = '<span class="required">'.$si_captcha_opt['si_captcha_required_indicator']."</span>\n";
  326. $input_string = '<input id="captcha_code" name="captcha_code" type="text" size="6" ' . $si_aria_required . ' />
  327. ';
  328.  
  329.  if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-input' || $si_captcha_opt['si_captcha_comment_label_position'] == 'left'  ) { // buddypress (label-required-input)(label left)
  330.       echo $label_string . $required_string . $input_string; // BP
  331.  } else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-required-linebreak-input' ||  $si_captcha_opt['si_captcha_comment_label_position'] == 'top' ) {
  332.       echo $label_string . $required_string .'<br />'. $input_string; // regular WP - twenty ten
  333.  } else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'label-input-required' ||  $si_captcha_opt['si_captcha_comment_label_position'] == 'right' ) {
  334.       echo $label_string . $input_string . $required_string; // suffusion
  335.  } else if ($si_captcha_opt['si_captcha_comment_label_position'] == 'input-label-required' ) {
  336.       echo $input_string . $label_string . $required_string; // regular WP
  337.  } else {
  338.       echo $input_string . $label_string . $required_string;  // regular WP
  339.  }
  340. echo '</p>';
  341.  
  342.  
  343. }
  344.  
  345.     // prevent double captcha fields
  346.     remove_action('comment_form', array(&$this, 'si_captcha_comment_form'), 1);
  347.  
  348.     return true;
  349. } // end function si_captcha_comment_form_wp3
  350.  
  351. // this function adds the captcha to the login form
  352. function si_captcha_login_form() {
  353.    global $si_captcha_url, $si_captcha_opt;
  354.  
  355.    if ($si_captcha_opt['si_captcha_login'] != 'true') {
  356.         return true; // captcha setting is disabled for login
  357.    }
  358.  
  359. // Test for some required things, print error message right here if not OK.
  360. if ($this->si_captcha_check_requires()) {
  361.  
  362.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  363.  
  364. // the captcha html - login form
  365. echo '
  366. <br />
  367. <div ';
  368. echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
  369. echo '>';
  370. $this->si_captcha_captcha_html('si_image_log','log');
  371. echo '</div>
  372. <p>
  373. <label>';
  374.   echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  375.   echo '<br />
  376. <input id="captcha_code" name="captcha_code" class="input" type="text" value="" size="12" tabindex="30" '.$si_aria_required.' style="font-size: 24px; width: 97%; padding: 3px; margin-top: 2px; margin-right: 6px; margin-bottom: 16px; border: 1px solid #e5e5e5; background: #fbfbfb;" /></label>
  377. </p>
  378. <br />
  379. ';
  380. }
  381.  
  382.   return true;
  383.  
  384. } //  end function si_captcha_login_form
  385.  
  386.  
  387. // this function adds the captcha to the login bar form of all buddypress versions
  388. function si_captcha_bp_login_form() {
  389.    global $si_captcha_url, $si_captcha_opt;
  390.  
  391.    if ($si_captcha_opt['si_captcha_login'] != 'true') {
  392.         return true; // captcha setting is disabled for login
  393.    }
  394.  
  395. // Test for some required things, print error message right here if not OK.
  396. if ($this->si_captcha_check_requires()) {
  397.  
  398.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  399.  
  400. // the captcha html - buddypress login form
  401. echo '
  402. <div style="width:440px; height:45px">';
  403. $this->si_captcha_captcha_html('si_image_log','log');
  404. echo '<input id="captcha_code" name="captcha_code" class="input" type="text" value="" '.$si_aria_required.' />
  405.         <label for="captcha_code">';
  406.   echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  407.   echo '</label>
  408. </div>
  409. </div>
  410. ';
  411. }
  412.  
  413.   return true;
  414.  
  415. } //  end function si_captcha_bp_login_form
  416.  
  417. // this function adds the captcha to the login sidebar form of all buddypress versions
  418. function si_captcha_bp_login_sidebar_form() {
  419.    global $si_captcha_url, $si_captcha_opt;
  420.  
  421.    if ($si_captcha_opt['si_captcha_login'] != 'true') {
  422.         return true; // captcha setting is disabled for login
  423.    }
  424.  
  425. // Test for some required things, print error message right here if not OK.
  426. if ($this->si_captcha_check_requires()) {
  427.  
  428.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  429.  
  430. // the captcha html - buddypress sidebar login form
  431. echo '
  432. <div class="captchaSizeDivSmall">
  433. ';
  434.   $this->si_captcha_captcha_html('si_image_side_login','log');
  435. echo '
  436. </div>
  437.  
  438.    <label for="captcha_code_side_login">';
  439.   echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  440.   echo '</label>
  441. <input style="width=145px;" id="captcha_code_side_login" name="captcha_code" class="input" type="text" value="" '.$si_aria_required.' />
  442. <br />
  443. <br />
  444. ';
  445. }
  446.  
  447.   return true;
  448.  
  449. } //  end function si_captcha_bp_login_sidebar_form
  450.  
  451.  
  452. // this function adds the captcha to the login form any time wp_login_form is called
  453. function si_captcha_inline_login_form() {
  454.    global $si_captcha_url, $si_captcha_opt;
  455.  
  456.    if ($si_captcha_opt['si_captcha_login'] != 'true') {
  457.         return true; // captcha setting is disabled for login
  458.    }
  459.  
  460. // Test for some required things, print error message right here if not OK.
  461. if ($this->si_captcha_check_requires()) {
  462.  
  463.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  464.  
  465. // the captcha html - buddypress sidebar login form
  466. $si_html = '
  467. <div class="captchaSizeDivSmall">
  468. ';
  469.   $si_html .= $this->si_captcha_captcha_html('si_image_side_login','log', true);
  470. $si_html .= '
  471. </div>
  472.  
  473.    <label for="captcha_code_side_login">';
  474.   $si_html .= ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  475.   $si_html .= '</label>
  476. <input style="width=145px;" id="captcha_code_side_login" name="captcha_code" class="input" type="text" value="" '.$si_aria_required.' />
  477. <br />
  478. <br />
  479. ';
  480. }
  481.  
  482.   return $si_html;
  483.  
  484. } //  end function si_captcha_inline_login_form
  485.  
  486.  
  487. // this function adds the captcha to the register form
  488. function si_captcha_register_form() {
  489.    global $si_captcha_url, $si_captcha_opt;
  490.  
  491.    if ($si_captcha_opt['si_captcha_register'] != 'true') {
  492.         return true; // captcha setting is disabled for registration
  493.    }
  494.  
  495. // Test for some required things, print error message right here if not OK.
  496. if ($this->si_captcha_check_requires()) {
  497.  
  498.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  499.  
  500. // the captcha html - register form
  501. echo '
  502. <br />
  503. <div ';
  504. echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
  505. echo '>';
  506. $this->si_captcha_captcha_html('si_image_reg','reg');
  507. echo '</div>
  508. <p>
  509. <label>';
  510.   echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  511.   echo '<br />
  512. <input id="captcha_code" name="captcha_code" class="input" type="text" value="" tabindex="30" '.$si_aria_required.' style="font-size: 24px; width: 97%; padding: 3px; margin-top: 2px; margin-right: 6px; margin-bottom: 16px; border: 1px solid #e5e5e5; background: #fbfbfb;" /></label>
  513. </p>
  514.  
  515. ';
  516. }
  517.  
  518.   return true;
  519. } // end function si_captcha_register_form
  520.  
  521. // for wpmu and buddypress before 1.1
  522. function si_captcha_wpmu_signup_form( $errors ) {
  523.    global $si_captcha_url, $si_captcha_opt;
  524.  
  525.    if ($si_captcha_opt['si_captcha_register'] != 'true') {
  526.         return true; // captcha setting is disabled for registration
  527.    }
  528.    $error = $errors->get_error_message('captcha');
  529.  
  530.    if( isset($error) && $error != '') {
  531.      echo '<p class="error">' . $error . '</p>';
  532.    }
  533. // Test for some required things, print error message right here if not OK.
  534. if ($this->si_captcha_check_requires()) {
  535.  
  536.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  537.  
  538. // the captcha html - wpmu register form
  539. echo '
  540. <div ';
  541. echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
  542. echo '>';
  543. $this->si_captcha_captcha_html('si_image_reg','reg');
  544. echo '</div>
  545.  
  546. <label for="captcha_code">';
  547.   echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  548.   echo '</label>
  549. <input id="captcha_code" name="captcha_code" type="text" value="" '.$si_aria_required.' />
  550.  
  551. ';
  552. }
  553. } // end function si_captcha_wpmu_signup_form
  554.  
  555. // for buddypress 1.1+ only
  556. // hooks into register.php do_action( 'bp_before_registration_submit_buttons' )
  557. // and bp-core-signup.php add_action( 'bp_' . $fieldname . '_errors', ...
  558. function si_captcha_bp_signup_form() {
  559.    global $si_captcha_url, $si_captcha_opt;
  560.  
  561.    if ($si_captcha_opt['si_captcha_register'] != 'true') {
  562.         return true; // captcha setting is disabled for registration
  563.    }
  564.  
  565. // Test for some required things, print error message right here if not OK.
  566. if ($this->si_captcha_check_requires()) {
  567.  
  568.   $si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == 'true') ? ' aria-required="true" ' : '';
  569.  
  570. // the captcha html - buddypress 1.1 register form
  571. echo '
  572. <div class="register-section" style="clear:left; margin-top:-10px;">
  573. <div ';
  574. echo ($si_captcha_opt['si_captcha_captcha_small'] == 'true') ? 'class="captchaSizeDivSmall"' : 'class="captchaSizeDivLarge"';
  575. echo '>';
  576. $this->si_captcha_captcha_html('si_image_reg','reg');
  577. echo '</div>
  578. <label for="captcha_code">';
  579.   do_action( 'bp_captcha_code_errors' );
  580.   echo ($si_captcha_opt['si_captcha_label_captcha'] != '') ? $si_captcha_opt['si_captcha_label_captcha'] : __('CAPTCHA Code', 'si-captcha');
  581.   echo '</label>
  582. <input style="width:145px;" id="captcha_code" name="captcha_code" type="text" value="" '.$si_aria_required.' />
  583. </div>
  584.  
  585. ';
  586. }
  587. } // end function si_captcha_wpmu_signup_form
  588.  
  589.  
  590. // this function checks the captcha posted with registration on BuddyPress 1.1+
  591. // hooks into bp-core-signup.php do_action( 'bp_signup_validate' );
  592. function si_captcha_bp_signup_validate() {
  593.    global $bp, $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
  594.  
  595.    $validate_result = $this->si_captcha_validate_code('reg', 'unlink');
  596.    if($validate_result != 'valid') {
  597.        $bp->signup->errors['captcha_code'] = $validate_result;
  598.        return;
  599.    }
  600.    return;
  601. } // end function si_captcha_bp_signup_validate
  602.  
  603. // this function checks the captcha posted with registration on wpmu and buddypress before 1.1
  604. function si_captcha_wpmu_signup_post($errors) {
  605.    global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
  606.  
  607.  if ($_POST['stage'] == 'validate-user-signup') {
  608.    $validate_result = $this->si_captcha_validate_code('reg', 'unlink');
  609.    if($validate_result != 'valid') {
  610.        $error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
  611.        $errors['errors']->add('captcha', "<strong>$error</strong>: $validate_result");
  612.        return $errors;
  613.    }
  614.  }
  615.  return($errors);
  616.  
  617. } // end function si_captcha_wpmu_signup_post
  618.  
  619. // this function checks the captcha posted with registration
  620. function si_captcha_register_post($errors) {
  621.    global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
  622.  
  623.    $validate_result = $this->si_captcha_validate_code('reg', 'unlink');
  624.    if($validate_result != 'valid') {
  625.        $error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
  626.        $errors->add('captcha_error', "<strong>$error</strong>: $validate_result");
  627.        return $errors;
  628.    }
  629.    return($errors);
  630. } // end function si_captcha_register_post
  631.  
  632. function si_captcha_lostpassword_post() {
  633.    global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
  634.  
  635.    $validate_result = $this->si_captcha_validate_code('reg', 'unlink');
  636.    if($validate_result != 'valid') {
  637.        $error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
  638.        wp_die( "<strong>$error</strong>: $validate_result" );
  639.    }
  640.    return;
  641.  
  642. } // function si_captcha_lostpassword_post
  643.  
  644. // this function checks the captcha posted with the comment
  645. function si_captcha_comment_post($comment) {
  646.     global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
  647.  
  648.     // added for compatibility with WP Wall plugin
  649.     // this does NOT add CAPTCHA to WP Wall plugin,
  650.     // it just prevents the "Empty CAPTCHA" when submitting a WP Wall comment
  651.     if ( function_exists('WPWall_Widget') && isset($_POST['wpwall_comment']) ) {
  652.         // skip capthca
  653.         return $comment;
  654.     }
  655.  
  656.     // skip the captcha if user is logged in and the settings allow
  657.     if (is_user_logged_in() && $si_captcha_opt['si_captcha_perm'] == 'true') {
  658.        // skip the CAPTCHA display if the minimum capability is met
  659.        if ( current_user_can( $si_captcha_opt['si_captcha_perm_level'] ) ) {
  660.            // skip capthca
  661.            return $comment;
  662.         }
  663.     }
  664.  
  665.     // skip captcha for comment replies from admin menu
  666.     if ( isset($_POST['action']) && $_POST['action'] == 'replyto-comment' &&
  667.     ( check_ajax_referer( 'replyto-comment', '_ajax_nonce', false ) || check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment', false )) ) {
  668.           // skip capthca
  669.           return $comment;
  670.     }
  671.  
  672.     // Skip captcha for trackback or pingback
  673.     if ( $comment['comment_type'] != '' && $comment['comment_type'] != 'comment' ) {
  674.                // skip capthca
  675.                return $comment;
  676.     }
  677.  
  678.    $validate_result = $this->si_captcha_validate_code('com', 'unlink');
  679.    if($validate_result != 'valid') {
  680.        $error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
  681.        wp_die( "<strong>$error</strong>: $validate_result" );
  682.    }
  683.    return($comment);
  684.  
  685. } // end function si_captcha_comment_post
  686.  
  687. function si_wp_authenticate_username_password($user, $username, $password) {
  688.         global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt, $wp_version;
  689.  
  690.         if ( is_a($user, 'WP_User') ) { return $user; }
  691.  
  692.         if ( empty($username) || empty($password) || isset($_POST['captcha_code']) && empty($_POST['captcha_code'])) {
  693.             $error = new WP_Error();
  694.  
  695.             if ( empty($username) )
  696.                 $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));
  697.  
  698.             if ( empty($password) )
  699.                 $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
  700.  
  701.             if (isset($_POST['captcha_code']) && empty($_POST['captcha_code'])) {
  702.                 remove_filter('authenticate', 'wp_authenticate_username_password', 20, 3);
  703.                 $print_error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
  704.                 $empty_captcha = ($si_captcha_opt['si_captcha_error_empty'] != '') ? $si_captcha_opt['si_captcha_error_empty'] : __('Empty CAPTCHA', 'si-captcha');
  705.                 $error->add('empty_captcha', "<strong>$print_error</strong>: $empty_captcha");
  706.             }
  707.             return $error;
  708.         }
  709.  
  710.    // begin si captcha check
  711.     $validate_result = $this->si_captcha_validate_code('log', 'unlink');
  712.     if($validate_result != 'valid') {
  713.        remove_filter('authenticate', 'wp_authenticate_username_password', 20, 3);
  714.        $print_error = ($si_captcha_opt['si_captcha_error_error'] != '') ? $si_captcha_opt['si_captcha_error_error'] : __('ERROR', 'si-captcha');
  715.        return new WP_Error('captcha_error', "<strong>$print_error</strong>: $validate_result");
  716.  
  717.     }
  718.    // end si captcha check
  719.  
  720.         $userdata = get_user_by('login', $username);
  721.  
  722.         if ( !$userdata ) {
  723.             return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
  724.         }
  725.  
  726.    // for WP 3.0+ ONLY!
  727.    if( version_compare($wp_version,'3','>=') ) { // wp 3.0 +
  728.      if ( is_multisite() ) {
  729.         // Is user marked as spam?
  730.         if ( 1 == $userdata->spam)
  731.             return new WP_Error('invalid_username', __('<strong>ERROR</strong>: Your account has been marked as a spammer.'));
  732.  
  733.         // Is a user's blog marked as spam?
  734.         if ( !is_super_admin( $userdata->ID ) && isset($userdata->primary_blog) ) {
  735.             $details = get_blog_details( $userdata->primary_blog );
  736.             if ( is_object( $details ) && $details->spam == 1 )
  737.                 return new WP_Error('blog_suspended', __('Site Suspended.'));
  738.         }
  739.     }
  740.    }
  741.         $userdata = apply_filters('wp_authenticate_user', $userdata, $password);
  742.         if ( is_wp_error($userdata) ) {
  743.             return $userdata;
  744.         }
  745.  
  746.         if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) ) {
  747.             return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
  748.         }
  749.  
  750.         $user =  new WP_User($userdata->ID);
  751.         return $user;
  752. } // end function si_wp_authenticate_username_password
  753.  
  754.  
  755. // check the honeypot trap for spam bots
  756. // hidden empty field honyepot trap for spam bots
  757. function si_captcha_check_honeypot($form_id = 'com') {
  758.       global $si_captcha_opt;
  759.  
  760.       if ($si_captcha_opt['si_captcha_honeypot_enable'] == 'false')
  761.            return 'ok';
  762.  
  763.     // validate hidden honeypot field
  764.     if( isset($_POST["email_$form_id"]) && trim($_POST["email_$form_id"]) != '')
  765.          return 'failed honeypot';
  766.  
  767.       return 'ok';
  768.  
  769. }  //  end function si_captcha_check_honeypot
  770.  
  771. // check if the posted capcha code was valid
  772. function si_captcha_validate_code($form_id = 'com', $unlink = 'unlink') {
  773.        global $si_captcha_dir, $si_captcha_dir_ns, $si_captcha_opt;
  774.  
  775.   if (isset($_REQUEST['action']) &&  $_REQUEST['action'] == 'lostpassword' && $form_id == 'log')
  776.         return 'valid';  // fixes lostpassword page because add_filter('login_errors' is also being called before
  777.  
  778.   if (isset($_POST['captcha_code']) && empty($_POST['captcha_code']))
  779.         return ($si_captcha_opt['si_captcha_error_empty'] != '') ? $si_captcha_opt['si_captcha_error_empty'] : __('Empty CAPTCHA', 'si-captcha');
  780.  
  781.   if($si_captcha_opt['si_captcha_enable_session'] != 'true') {
  782.    //captcha without sessions
  783.       if (empty($_POST['captcha_code']) || $_POST['captcha_code'] == '') {
  784.          return ($si_captcha_opt['si_captcha_error_empty'] != '') ? $si_captcha_opt['si_captcha_error_empty'] : __('Empty CAPTCHA', 'si-captcha');
  785.       }else if (!isset($_POST["si_code_$form_id"]) || empty($_POST["si_code_$form_id"])) {
  786.           return ($si_captcha_opt['si_captcha_error_token'] != '') ? $si_captcha_opt['si_captcha_error_token'] : __('Missing CAPTCHA token', 'si-captcha');
  787.       }else{
  788.          $prefix = 'xxxxxx';
  789.          if ( isset($_POST["si_code_$form_id"]) && is_string($_POST["si_code_$form_id"]) && preg_match('/^[a-zA-Z0-9]{15,17}$/',$_POST["si_code_$form_id"]) ){
  790.            $prefix = $_POST["si_code_$form_id"];
  791.          }
  792.          if ( is_readable( $si_captcha_dir_ns . $prefix . '.php' ) ) {
  793.             include( $si_captcha_dir_ns . $prefix . '.php' );
  794.             if ( 0 == strcasecmp( trim(strip_tags($_POST['captcha_code'])), $captcha_word ) ) {
  795.               // captcha was matched
  796.              if($unlink == 'unlink')
  797.                 @unlink ($si_captcha_dir_ns . $prefix . '.php');
  798.                    // empty field honyepot trap for spam bots
  799.                    $hp_check = $this->si_captcha_check_honeypot("$form_id");
  800.                    if($hp_check != 'ok')
  801.                       return ($si_captcha_opt['si_captcha_error_spambot'] != '') ? $si_captcha_opt['si_captcha_error_spambot'] : __('Possible spam bot', 'si-captcha');
  802.               return 'valid';
  803.             } else {
  804.                return ($si_captcha_opt['si_captcha_error_incorrect'] != '') ? $si_captcha_opt['si_captcha_error_incorrect'] : __('Wrong CAPTCHA', 'si-captcha');
  805.             }
  806.          } else {
  807.            return ($si_captcha_opt['si_captcha_error_unreadable'] != '') ? $si_captcha_opt['si_captcha_error_unreadable'] : __('Unreadable CAPTCHA token file', 'si-captcha');
  808.            //$this->si_captcha_token_error();
  809.         }
  810.       }
  811.  
  812.   }else{
  813.    //captcha with PHP sessions
  814.    if (!isset($_SESSION["securimage_code_si_$form_id"]) || empty($_SESSION["securimage_code_si_$form_id"])) {
  815.           return ($si_captcha_opt['si_captcha_error_cookie'] != '') ? $si_captcha_opt['si_captcha_error_cookie'] : __('Unreadable CAPTCHA cookie', 'si-captcha');
  816.  
  817.    }else{
  818.  
  819.       $captcha_code = trim(strip_tags($_POST['captcha_code']));
  820.  
  821.       require_once "$si_captcha_dir/securimage.php";
  822.       $img = new Securimage_si();
  823.       $img->form_id = $form_id; // makes compatible with multi-forms on same page
  824.       $valid = $img->check("$captcha_code");
  825.       // Check, that the right CAPTCHA password has been entered, display an error message otherwise.
  826.       if($valid == true) {
  827.            // empty field honyepot trap for spam bots
  828.            $hp_check= $this->si_captcha_check_honeypot("$form_id");
  829.            if($hp_check != 'ok')
  830.                 return ($si_captcha_opt['si_captcha_error_spambot'] != '') ? $si_captcha_opt['si_captcha_error_spambot'] : __('Possible spam bot', 'si-captcha');
  831.           // ok can continue
  832.           return 'valid';
  833.       } else {
  834.           return ($si_captcha_opt['si_captcha_error_incorrect'] != '') ? $si_captcha_opt['si_captcha_error_incorrect'] : __('Wrong CAPTCHA', 'si-captcha');
  835.       }
  836.    }
  837.   }
  838.  
  839. } // end function si_captcha_validate_code
  840.  
  841. // displays the CAPTCHA in the forms
  842. function si_captcha_captcha_html($label = 'si_image', $form_id = 'com', $no_echo = false) {
  843.   global $si_captcha_url, $si_captcha_dir, $si_captcha_url_ns, $si_captcha_dir_ns, $si_captcha_opt;
  844.  
  845.   $capt_disable_sess = 0;
  846.    if ($si_captcha_opt['si_captcha_enable_session'] != 'true')
  847.      $capt_disable_sess = 1;
  848.  
  849.   // url for no session captcha image
  850.   $securimage_show_url = $si_captcha_url .'/securimage_show.php?';
  851.   $securimage_size = 'width="175" height="60"';
  852.   if($si_captcha_opt['si_captcha_captcha_small'] == 'true' || $label == 'si_image_side_login' ) {
  853.     $securimage_show_url .= 'si_sm_captcha=1&amp;';
  854.     $securimage_size = 'width="132" height="45"';
  855.   }
  856.  
  857.   $parseUrl = parse_url($si_captcha_url);
  858.   $securimage_url = $parseUrl['path'];
  859.  
  860.   $securimage_show_url .= 'si_form_id=' .$form_id;
  861.  
  862.   if($capt_disable_sess) {
  863.      // clean out old captcha no session temp files
  864.     $this->si_captcha_clean_temp_dir($si_captcha_dir_ns, 30);
  865.     // pick new prefix token
  866.     $prefix_length = 16;
  867.     $prefix_characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
  868.     $prefix = '';
  869.     $prefix_count = strlen($prefix_characters);
  870.     while ($prefix_length--) {
  871.         $prefix .= $prefix_characters[mt_rand(0, $prefix_count-1)];
  872.     }
  873.     $securimage_show_rf_url = $securimage_show_url . '&amp;prefix=';
  874.     $securimage_show_url .= '&amp;prefix='.$prefix;
  875.   }
  876.  
  877.   $si_html = '';
  878.  
  879.    if($si_captcha_opt['si_captcha_honeypot_enable'] == 'true' ) {
  880.       // hidden empty honeypot field
  881.       $si_html .= '
  882.        <div style="display:none;">
  883.          <label for="email_'.$form_id.'"><small>'.__('Leave this field empty', 'si-captcha').'</small></label>
  884.          <input type="text" name="email_'.$form_id.'" id="email_'.$form_id.'" value="" />
  885.        </div>
  886. ';
  887.   }
  888.  
  889.   $si_html .= '<img id="'.$label.'" class="si-captcha" src="'.$securimage_show_url.'" '.$securimage_size.' alt="';
  890.   $si_html .= ($si_captcha_opt['si_captcha_tooltip_captcha'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_captcha'] ) : esc_attr(__('CAPTCHA Image', 'si-captcha'));
  891.   $si_html .= '" title="';
  892.   $si_html .= ($si_captcha_opt['si_captcha_tooltip_captcha'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_captcha'] ) : esc_attr(__('CAPTCHA Image', 'si-captcha'));
  893.   $si_html .= '" />'."\n";
  894.   if($capt_disable_sess) {
  895.         $si_html .= '    <input id="si_code_'.$form_id.'" name="si_code_'.$form_id.'" type="hidden"  value="'.$prefix.'" />'."\n";
  896.   }
  897.  
  898.   $si_html .= '    <div id="si_refresh_'.$form_id.'">'."\n";
  899.   $si_html .= '<a href="#" rel="nofollow" title="';
  900.   $si_html .= ($si_captcha_opt['si_captcha_tooltip_refresh'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_refresh'] ) : esc_attr(__('Refresh Image', 'si-captcha'));
  901.   if($capt_disable_sess) {
  902.     $si_html .= '" onclick="si_captcha_refresh(\''.$label.'\',\''.$form_id.'\',\''.$securimage_url.'\',\''.$securimage_show_rf_url.'\'); return false;">'."\n";
  903.   }else{
  904.     $si_html .= '" onclick="document.getElementById(\''.$label.'\').src = \''.$securimage_show_url.'&amp;sid=\''.' + Math.random(); return false;">'."\n";
  905.   }
  906.   $si_html .= '      <img class="captchaImgRefresh" src="'.$si_captcha_url.'/images/refresh.png" width="22" height="20" alt="';
  907.   $si_html .= ($si_captcha_opt['si_captcha_tooltip_refresh'] != '') ? esc_attr( $si_captcha_opt['si_captcha_tooltip_refresh'] ) : esc_attr(__('Refresh Image', 'si-captcha'));
  908.   $si_html .= '" onclick="this.blur();" /></a>
  909.  </div>
  910.  ';
  911.  
  912.   if ( $no_echo ) return $si_html;
  913.   echo $si_html;
  914.  
  915. } // end function si_captcha_captcha_html
  916.  
  917. function si_captcha_plugin_action_links( $links, $file ) {
  918.     //Static so we don't call plugin_basename on every plugin row.
  919.     static $this_plugin;
  920.     if ( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__);
  921.  
  922.     if ( $file == $this_plugin ){
  923.          $settings_link = '<a href="plugins.php?page=si-captcha-for-wordpress/si-captcha.php">' . __('Settings', 'si-captcha') . '</a>';
  924.          array_unshift( $links, $settings_link );
  925.     }
  926.     return $links;
  927. } // end function si_captcha_plugin_action_links
  928.  
  929. function si_captcha_init() {
  930.    global $wpmu;
  931.  
  932.   if (function_exists('load_plugin_textdomain')) {
  933.      if ($wpmu == 1) {
  934.           load_plugin_textdomain('si-captcha', false, dirname(plugin_basename(__FILE__)).'/si-captcha-for-wordpress/languages' );
  935.      } else {
  936.           load_plugin_textdomain('si-captcha', false, dirname(plugin_basename(__FILE__)).'/languages' );
  937.      }
  938.   }
  939.  
  940. } // end function si_captcha_init
  941.  
  942. function si_captcha_start_session() {
  943.  
  944.    // a PHP session cookie is set so that the captcha can be remembered and function
  945.   // this has to be set before any header output
  946.    //echo "before starting session si captcha";
  947.   if( !isset( $_SESSION ) ) { // play nice with other plugins
  948.    if ( !defined('XMLRPC_REQUEST') ) { // buddypress fix
  949.       //set the $_SESSION cookie into HTTPOnly mode for better security
  950.       if (version_compare(PHP_VERSION, '5.2.0') >= 0)  // supported on PHP version 5.2.0  and higher
  951.         @ini_set("session.cookie_httponly", 1);
  952.       session_cache_limiter ('private, must-revalidate');
  953.       session_start();
  954.       //echo "session started si captcha";
  955.    }
  956.   }
  957.  
  958. } // function si_captcha_start_session
  959.  
  960. // needed for making temp directories for attachments and captcha session files
  961. function si_captcha_init_temp_dir($dir) {
  962.     $dir = trailingslashit( $dir );
  963.     // make the temp directory
  964.     wp_mkdir_p( $dir );
  965.     //@chmod( $dir, 0733 );
  966.     $htaccess_file = $dir . '.htaccess';
  967.     if ( !file_exists( $htaccess_file ) ) {
  968.        if ( $handle = @fopen( $htaccess_file, 'w' ) ) {
  969.            fwrite( $handle, "Deny from all\n" );
  970.            fclose( $handle );
  971.        }
  972.     }
  973.     $php_file = $dir . 'index.php';
  974.     if ( !file_exists( $php_file ) ) {
  975.         if ( $handle = @fopen( $php_file, 'w' ) ) {
  976.            fwrite( $handle, '<?php //do not delete ?>' );
  977.            fclose( $handle );
  978.         }
  979.     }
  980. } // end function si_captcha_init_temp_dir
  981.  
  982. // needed for emptying temp directories for attachments and captcha session files
  983. function si_captcha_clean_temp_dir($dir, $minutes = 60) {
  984.     // deletes all files over xx minutes old in a temp directory
  985.     if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! is_writable( $dir ) )
  986.         return false;
  987.  
  988.     $count = 0;
  989.     if ( $handle = @opendir( $dir ) ) {
  990.         while ( false !== ( $file = readdir( $handle ) ) ) {
  991.             if ( $file == '.' || $file == '..' || $file == '.htaccess' || $file == 'index.php')
  992.                 continue;
  993.  
  994.             $stat = @stat( $dir . $file );
  995.             if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) {
  996.                 @unlink( $dir . $file );
  997.                 $count += 1;
  998.             }
  999.         }
  1000.         closedir( $handle );
  1001.     }
  1002.     return $count;
  1003. }
  1004.  
  1005. // functions for form vars
  1006. function si_stripslashes($string) {
  1007.         //if (get_magic_quotes_gpc()) {
  1008.         // wordpress always needs stripslashes
  1009.                 return stripslashes($string);
  1010.         //} else {
  1011.                 //return $string;
  1012.        // }
  1013. } // end function si_stripslashes
  1014.  
  1015. function si_captcha_convert_css($string,$css) {
  1016.     global $si_captcha_option_defaults;
  1017.     if( preg_match("/^style=\"(.*)\"$/i", $string, $matches) ){
  1018.       return $matches[1];
  1019.     }
  1020.     if( preg_match("/^class=\"(.*)\"$/i", $string) ){
  1021.       return (isset($si_captcha_option_defaults[$css])) ? $si_captcha_option_defaults[$css] : $string;
  1022.     }
  1023.     return $string;
  1024.  
  1025. } // end function si_captcha_convert_css
  1026.  
  1027. function si_captcha_admin_head() {
  1028.  // only load this header stuff on the admin settings page
  1029. if(isset($_GET['page']) && is_string($_GET['page']) && preg_match('/si-captcha.php$/',$_GET['page']) ) {
  1030. ?>
  1031. <!-- begin SI CAPTCHA Anti-Spam - admin settings page header code -->
  1032. <style type="text/css">
  1033. div.si-star-holder { position: relative; height:19px; width:100px; font-size:19px;}
  1034. div.si-star {height: 100%; position:absolute; top:0px; left:0px; background-color: transparent; letter-spacing:1ex; border:none;}
  1035. .si-star1 {width:20%;} .si-star2 {width:40%;} .si-star3 {width:60%;} .si-star4 {width:80%;} .si-star5 {width:100%;}
  1036. .si-star.si-star-rating {background-color: #fc0;}
  1037. .si-star img{display:block; position:absolute; right:0px; border:none; text-decoration:none;}
  1038. div.si-star img {width:19px; height:19px; border-left:1px solid #fff; border-right:1px solid #fff;}
  1039. .si-notice{background-color:#ffffe0;border-color:#e6db55;border-width:1px;border-style:solid;padding:5px;margin:5px 5px 20px;-moz-border-radius:3px;-khtml-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;}
  1040. .fscf_left {clear:left; float:left;}
  1041. .fscf_img {margin:0 10px 10px 0;}
  1042. .fscf_tip {text-align:left; display:none;color:#006B00;padding:5px;}
  1043. </style>
  1044. <!-- end SI CAPTCHA Anti-Spam - admin settings page header code -->
  1045. <?php
  1046.   } // end if(isset($_GET['page'])
  1047.  
  1048. }
  1049.  
  1050. function si_captcha_login_head(){
  1051.   global $si_captcha_opt;
  1052.   echo '<script type="text/javascript" src="'.plugins_url('si-captcha-for-wordpress/captcha/si_captcha.js?ver='.time()).'"></script>'."\n";
  1053.  
  1054.  // only load this css on the blog pages where login/register could be
  1055. if( $si_captcha_opt['si_captcha_external_style'] == 'true' )
  1056.   return;
  1057.  
  1058. ?>
  1059. <!-- begin SI CAPTCHA Anti-Spam - login/register form style -->
  1060. <style type="text/css">
  1061. div#captchaImgDiv { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_div_style'],'si_captcha_captcha_div_style'); ?> }
  1062. .captchaSizeDivSmall { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_div_style_sm'],'si_captcha_captcha_div_style_sm'); ?> }
  1063. .captchaSizeDivLarge { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_div_style_m'],'si_captcha_captcha_div_style_m'); ?> }
  1064. img#si_image_com,#si_image_reg,#si_image_log,#si_image_side_login { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_image_style'],'si_captcha_captcha_image_style'); ?> }
  1065. .captchaImgRefresh { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_refresh_image_style'],'si_captcha_refresh_image_style'); ?> }
  1066. div#captchaInputDiv { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_input_div_style'],'si_captcha_captcha_input_div_style'); ?> }
  1067. label#captcha_code_label { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_comment_label_style'],'si_captcha_comment_label_style'); ?> }
  1068. input#captcha_code { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_comment_field_style'],'si_captcha_comment_field_style'); ?> }
  1069. </style>
  1070. <!-- end SI CAPTCHA Anti-Spam - login/register form style -->
  1071. <?php
  1072. }
  1073.  
  1074. function si_captcha_add_css(){
  1075.    global $si_captcha_opt, $si_captcha_add_script;
  1076.  
  1077.    if (!$si_captcha_add_script)
  1078.       return;
  1079.  
  1080.   // only load this css on the blog pages where the captcha could be
  1081.   wp_enqueue_script('jquery');
  1082. if( $si_captcha_opt['si_captcha_external_style'] != 'true' ) {
  1083. ?>
  1084. <script type="text/javascript">
  1085. //<![CDATA[
  1086. var si_captcha_styles = "\
  1087. <!-- begin SI CAPTCHA Anti-Spam - comment form style -->\
  1088. <style type='text/css'>\
  1089. div#captchaImgDiv { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_div_style'],'si_captcha_captcha_div_style'); ?> }\
  1090. .captchaSizeDivSmall { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_div_style_sm'],'si_captcha_captcha_div_style_sm'); ?> }\
  1091. .captchaSizeDivLarge { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_div_style_m'],'si_captcha_captcha_div_style_m'); ?> }\
  1092. img#si_image_com,#si_image_reg,#si_image_log,#si_image_side_login { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_image_style'],'si_captcha_captcha_image_style'); ?> }\
  1093. .captchaImgRefresh { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_refresh_image_style'],'si_captcha_refresh_image_style'); ?> }\
  1094. div#captchaInputDiv { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_captcha_input_div_style'],'si_captcha_captcha_input_div_style'); ?> }\
  1095. label#captcha_code_label { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_comment_label_style'],'si_captcha_comment_label_style'); ?> }\
  1096. input#captcha_code { <?php echo $this->si_captcha_convert_css($si_captcha_opt['si_captcha_comment_field_style'],'si_captcha_comment_field_style'); ?> }\
  1097. </style>\
  1098. <!-- end SI CAPTCHA Anti-Spam - comment form style -->\
  1099. ";
  1100. jQuery(document).ready(function($) {
  1101. $('head').append(si_captcha_styles);
  1102. });
  1103. //]]>
  1104. </script>
  1105. <?php
  1106.   }
  1107. }
  1108.  
  1109. function si_captcha_add_script(){
  1110.    global $si_captcha_opt, $si_captcha_url, $si_captcha_add_script;
  1111.  
  1112.    if (!$si_captcha_add_script)
  1113.       return;
  1114.  
  1115.    // only load this javascript on the blog pages where captcha could be
  1116.    wp_register_script('si_captcha', $si_captcha_url.'/si_captcha.js', array(), '1.0', true);
  1117.    wp_print_scripts('si_captcha');
  1118. }
  1119.  
  1120.  
  1121. function get_captcha_url_si() {
  1122.    global $wpmu, $current_blog;
  1123.   // The captcha URL cannot be on a different domain as the site rewrites to or the cookie won't work
  1124.   // also the path has to be correct or the image won't load.
  1125.   // WP_PLUGIN_URL was not getting the job done! this code should fix it.
  1126.  
  1127.   //http://media.example.com/wordpress   WordPress address get_option( 'siteurl' )
  1128.   //http://tada.example.com              Blog address      get_option( 'home' )
  1129.  
  1130.   //http://example.com/wordpress  WordPress address get_option( 'siteurl' )
  1131.   //http://example.com/           Blog address      get_option( 'home' )
  1132.  
  1133.   $site_uri = parse_url(get_option('home'));
  1134.   $home_uri = parse_url(get_option('siteurl'));
  1135.  
  1136.   $si_dir = '/si-captcha-for-wordpress/captcha';
  1137.  
  1138.   $url  = WP_PLUGIN_URL . $si_dir;
  1139.  
  1140.   if ($site_uri['host'] == $home_uri['host']) {
  1141.       $url = WP_PLUGIN_URL . $si_dir;
  1142.       if ($wpmu == 1)
  1143.            $url = get_option('siteurl') . '/' . MUPLUGINDIR . $si_dir;
  1144.   } else {
  1145.       $url = get_option( 'home' ) . '/' . PLUGINDIR . $si_dir;
  1146.       if ($wpmu == 1)
  1147.           $url = get_option( 'home' ) . '/' . MUPLUGINDIR . $si_dir;
  1148.   }
  1149.   // "MU domain mapping" plugin compatabilty
  1150.   if ( isset( $current_blog ) && !empty( $current_blog->domain ) && !empty( $current_blog->path ) )
  1151.     $url = (is_ssl() ? 'https://' : 'http://') . $current_blog->domain . $current_blog->path . ($wpmu == 1 ? MUPLUGINDIR : PLUGINDIR) . $si_dir;
  1152.  
  1153.   // set the type of request (SSL or not)
  1154.   if ( is_ssl() ) {
  1155.       $url = preg_replace('|http://|', 'https://', $url);
  1156.   }
  1157.  
  1158.   return $url;
  1159. }
  1160.  
  1161. } // end of class
  1162. } // end of if class
  1163.  
  1164. // backwards compatibility
  1165.  
  1166. // Pre-2.8 compatibility
  1167. if ( ! function_exists( 'esc_html' ) ) {
  1168.     function esc_html( $text ) {
  1169.         return wp_specialchars( $text );
  1170.     }
  1171. }
  1172.  
  1173. // Pre-2.8 compatibility
  1174. if ( ! function_exists( 'esc_attr' ) ) {
  1175.     function esc_attr( $text ) {
  1176.         return attribute_escape( $text );
  1177.     }
  1178. }
  1179.  
  1180. if (class_exists("siCaptcha")) {
  1181.  $si_image_captcha = new siCaptcha();
  1182. }
  1183.  
  1184. if (isset($si_image_captcha)) {
  1185. global $wp_version, $si_captcha_opt;
  1186.  
  1187. // WordPress MU detection
  1188. //    0  Regular WordPress installation
  1189. //    1  WordPress MU Forced Activated
  1190. //    2  WordPress MU Optional Activation
  1191.  
  1192. $wpmu = 0;
  1193.  
  1194. if (basename(dirname(__FILE__)) == "mu-plugins") // forced activated
  1195.    $wpmu = 1;
  1196. else if (basename(dirname(__FILE__)) == "si-captcha-for-wordpress" && function_exists('is_site_admin')) // optionally activated
  1197.    $wpmu = 2;
  1198.  
  1199.   $si_captcha_dir = WP_PLUGIN_DIR . '/si-captcha-for-wordpress/captcha';
  1200.   if ($wpmu == 1) {
  1201.      if ( defined( 'MUPLUGINDIR' ) )
  1202.          $si_captcha_dir = MUPLUGINDIR . '/si-captcha-for-wordpress/captcha';
  1203.      else
  1204.          $si_captcha_dir = WP_CONTENT_DIR . '/mu-plugins/si-captcha-for-wordpress/captcha';
  1205.   }
  1206.  
  1207.   $si_captcha_url  = $si_image_captcha->get_captcha_url_si();
  1208.  
  1209.   // only used for the no-session captcha setting
  1210.   $si_captcha_url_ns = $si_captcha_url  . '/cache/';
  1211.   $si_captcha_dir_ns = $si_captcha_dir . '/cache/';
  1212.   $si_image_captcha->si_captcha_init_temp_dir($si_captcha_dir_ns);
  1213.  
  1214.   //Actions
  1215.   add_action('init', array(&$si_image_captcha, 'si_captcha_init'));
  1216.  
  1217.   // get the options now
  1218.   $si_image_captcha->si_captcha_get_options();
  1219.  
  1220.   if ( isset($si_captcha_opt['si_captcha_enable_session']) && $si_captcha_opt['si_captcha_enable_session'] != 'true') {
  1221.      // add javascript (conditionally to footer)
  1222.      // http://scribu.net/wordpress/optimal-script-loading.html
  1223.      add_action( 'wp_footer', array(&$si_image_captcha,'si_captcha_add_script'));
  1224.   }  else {
  1225.      // start the PHP session
  1226.      // buddypress had session error on member and groups pages, so start session here instead of init
  1227.      add_action('plugins_loaded', array(&$si_image_captcha, 'si_captcha_start_session'));
  1228.   }
  1229.  
  1230.   // si captcha admin options
  1231.   add_action('admin_menu', array(&$si_image_captcha,'si_captcha_add_tabs'),1);
  1232.   add_action('admin_head', array(&$si_image_captcha,'si_captcha_admin_head'),1);
  1233.  
  1234.   if( $si_captcha_opt['si_captcha_external_style'] != 'true' && !is_admin() ) {
  1235.      // css is added to footer, then jquery appends it to head
  1236.      add_action( 'wp_footer', array(&$si_image_captcha,'si_captcha_add_css'));
  1237.   }
  1238.  
  1239.   // adds "Settings" link to the plugin action page
  1240.   add_filter( 'plugin_action_links', array(&$si_image_captcha,'si_captcha_plugin_action_links'),10,2);
  1241.  
  1242.   if ($si_captcha_opt['si_captcha_comment'] == 'true') {
  1243.      // for WP 3.0+
  1244.      if( version_compare($wp_version,'3','>=') && !function_exists('sfc_commentform_submit')  ) { // wp 3.0 +
  1245.         // SFC Comment plugin was removing the captcha when logged into facebook
  1246.         add_action( 'comment_form_after_fields', array(&$si_image_captcha, 'si_captcha_comment_form_wp3'), 1);
  1247.         add_action( 'comment_form_logged_in_after', array(&$si_image_captcha, 'si_captcha_comment_form_wp3'), 1);
  1248.      }
  1249.      // for WP before WP 3.0
  1250.      add_action('comment_form', array(&$si_image_captcha, 'si_captcha_comment_form'), 1);
  1251.      add_filter('preprocess_comment', array(&$si_image_captcha, 'si_captcha_comment_post'), 1);
  1252.   }
  1253.  
  1254.   if ($si_captcha_opt['si_captcha_register'] == 'true') {
  1255.     add_action('login_head', array( &$si_image_captcha, 'si_captcha_login_head') );
  1256.     add_action('register_form', array(&$si_image_captcha, 'si_captcha_register_form'), 10);
  1257.     add_filter('registration_errors', array(&$si_image_captcha, 'si_captcha_register_post'), 10);
  1258.   }
  1259.  
  1260.   if ($wpmu && $si_captcha_opt['si_captcha_register'] == 'true') {
  1261.         // for buddypress 1.1 only
  1262.     add_action('bp_before_registration_submit_buttons', array( &$si_image_captcha, 'si_captcha_bp_signup_form' ));
  1263.         // for buddypress 1.1 only
  1264.     add_action('bp_signup_validate', array( &$si_image_captcha, 'si_captcha_bp_signup_validate' ));
  1265.         // for wpmu and (buddypress versions before 1.1)
  1266.     add_action('signup_extra_fields', array( &$si_image_captcha, 'si_captcha_wpmu_signup_form' ));
  1267.         // for wpmu and (buddypress versions before 1.1)
  1268.     add_filter('wpmu_validate_user_signup', array( &$si_image_captcha, 'si_captcha_wpmu_signup_post'));
  1269.   }
  1270.  
  1271.   if ($si_captcha_opt['si_captcha_login'] == 'true') {
  1272.     add_action('login_form', array( &$si_image_captcha, 'si_captcha_login_form' ) );
  1273.     add_filter('login_form_middle', array( &$si_image_captcha, 'si_captcha_inline_login_form' ) );
  1274.     add_action('login_head', array( &$si_image_captcha, 'si_captcha_login_head' ) );
  1275.     add_action('bp_login_bar_logged_out', array( &$si_image_captcha, 'si_captcha_bp_login_form' ) );
  1276.     add_action('bp_sidebar_login_form', array( &$si_image_captcha, 'si_captcha_bp_login_sidebar_form' ) );
  1277.     add_filter('authenticate', array( &$si_image_captcha, 'si_wp_authenticate_username_password'), 9, 3);
  1278.   }
  1279.  
  1280.   if ($si_captcha_opt['si_captcha_lostpwd'] == 'true') {
  1281.     add_action('lostpassword_form', array( &$si_image_captcha, 'si_captcha_register_form'), 10);
  1282.     add_action('lostpassword_post', array( &$si_image_captcha, 'si_captcha_lostpassword_post'), 10);
  1283.   }
  1284.  
  1285.  // add_action('bbp_template_notices', array( &$si_image_captcha, 'si_captcha_register_form'), 10);
  1286.  
  1287.   // options deleted when this plugin is deleted in WP 2.7+
  1288.   if ( function_exists('register_uninstall_hook') )
  1289.      register_uninstall_hook(__FILE__, 'si_captcha_unset_options');
  1290. }
  1291.  
  1292. // end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement