home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / CAPTCHA / Driver / Word.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  2.9 KB  |  121 lines

  1. <?php
  2. /**
  3.  * Text_CAPTCHA_Driver_Word - Text_CAPTCHA driver word CAPTCHAs
  4.  *
  5.  * Class to create a textual Turing test 
  6.  * 
  7.  * 
  8.  * @license PHP License, version 3.0
  9.  * @author Tobias Schlitt <schlitt@php.net>
  10.  * @author Christian Wenz <wenz@php.net>
  11.  */
  12.  
  13. /**
  14.  *
  15.  * Require Numbers_Words class for generating the text.
  16.  *
  17.  */
  18. require_once 'Text/CAPTCHA.php';
  19. require_once 'Numbers/Words.php';
  20.  
  21. class Text_CAPTCHA_Driver_Word extends Text_CAPTCHA
  22. {
  23.  
  24.     /**
  25.      * Phrase length
  26.      * 
  27.      * This variable holds the length of the Word
  28.      * 
  29.      * @access private
  30.      */
  31.     var $_length;
  32.  
  33.     /**
  34.      * Numbers_Words mode
  35.      * 
  36.      * This variable holds the mode for Numbers_Words
  37.      * 
  38.      * @access private
  39.      */
  40.     var $_mode;
  41.  
  42.     /**
  43.      * Locale
  44.      * 
  45.      * This variable holds the locale for Numbers_Words
  46.      * 
  47.      * @access private
  48.      */
  49.     var $_locale;
  50.  
  51.     /**
  52.      * init function
  53.      *
  54.      * Initializes the new Text_CAPTCHA_Driver_Word object
  55.      *
  56.      * @param   array   $options    CAPTCHA options with these keys:
  57.      *                  phrase     The "secret word" of the CAPTCHA
  58.      *                  length     The number of characters in the phrase 
  59.      *                  locale     The locale for Numbers_Words
  60.      *                  mode       The mode for Numbers_Words
  61.      * @access public
  62.      */
  63.     function init($options = array()) 
  64.     {
  65.         if (isset($options['length']) && is_int($options['length'])) {
  66.             $this->_length = $options['length'];
  67.         } else {
  68.             $this->_length = 4;
  69.         }
  70.         if (isset($options['phrase']) && !empty($options['phrase'])) {
  71.             $this->_phrase = (string)(int)$options['phrase'];
  72.         } else {
  73.             $this->_createPhrase();
  74.         }
  75.         if (isset($options['mode']) && !empty($options['mode'])) {
  76.             $this->_mode = $options['mode'];
  77.         } else {
  78.             $this->_mode = 'single';
  79.         }
  80.         if (isset($options['locale']) && !empty($options['locale'])) {
  81.             $this->_locale = $options['locale'];
  82.         } else {
  83.             $this->_locale = 'en_US';
  84.         }
  85.     }
  86.  
  87.     /**
  88.      * Create random CAPTCHA phrase, "Word edition" (numbers only)
  89.      *
  90.      * This method creates a random phrase
  91.      *
  92.      * @access  private
  93.      */
  94.     function _createPhrase()
  95.     {
  96.         $this->_phrase = (string)Text_Password::create($this->_length, 'unpronounceable', 'numeric');
  97.     }
  98.  
  99.     /**
  100.      * Return CAPTCHA as a string
  101.      *
  102.      * This method returns the CAPTCHA as string
  103.      *
  104.      * @access  public
  105.      * @return  text        string
  106.      */
  107.     function getCAPTCHA()
  108.     {
  109.         $res = ''; 
  110.         if ($this->_mode == 'single') {
  111.             for ($i = 0; $i < strlen($this->_phrase); $i++) {
  112.                 $res .= ' '.Numbers_Words::toWords($this->_phrase{$i}, $this->_locale);
  113.             }
  114.         } else {
  115.             $res = Numbers_Words::toWords($this->_phrase, $this->_locale);
  116.         }
  117.         return $res;
  118.     }
  119. }
  120. ?>
  121.