Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Text_CAPTCHA_Driver_Word - Text_CAPTCHA driver word CAPTCHAs
4
 *
5
 * Class to create a textual Turing test
6
 *
7
 * @author  Tobias Schlitt <schlitt@php.net>
8
 * @author  Christian Wenz <wenz@php.net>
9
 * @license BSD License
10
 */
11
 
12
/**
13
 *
14
 * Require Numbers_Words class for generating the text.
15
 *
16
 */
17
require_once 'Text/CAPTCHA.php';
18
require_once 'Numbers/Words.php';
19
 
20
class Text_CAPTCHA_Driver_Word extends Text_CAPTCHA
21
{
22
 
23
    /**
24
     * Phrase length
25
     *
26
     * This variable holds the length of the Word
27
     *
28
     * @access private
29
     */
30
    var $_length;
31
 
32
    /**
33
     * Numbers_Words mode
34
     *
35
     * This variable holds the mode for Numbers_Words
36
     *
37
     * @access private
38
     */
39
    var $_mode;
40
 
41
    /**
42
     * Locale
43
     *
44
     * This variable holds the locale for Numbers_Words
45
     *
46
     * @access private
47
     */
48
    var $_locale;
49
 
50
    /**
51
     * init function
52
     *
53
     * Initializes the new Text_CAPTCHA_Driver_Word object
54
     *
55
     * @param array $options CAPTCHA options with these keys:
56
     *               phrase  The "secret word" of the CAPTCHA
57
     *               length  The number of characters in the phrase
58
     *               locale  The locale for Numbers_Words
59
     *               mode    The mode for Numbers_Words
60
     *
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
}