Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Require Figlet class for rendering the text.
4
 */
5
require_once 'Text/CAPTCHA.php';
6
require_once 'Text/Figlet.php';
7
 
8
/**
9
 * Text_CAPTCHA_Driver_Figlet - Text_CAPTCHA driver Figlet based CAPTCHAs
10
 *
11
 * @author  Aaron Wormus <wormus@php.net>
12
 * @author  Christian Wenz <wenz@php.net>
13
 * @license BSD License
14
 * @todo define an obfuscation algorithm
15
 */
16
 
17
class Text_CAPTCHA_Driver_Figlet extends Text_CAPTCHA
18
{
19
    /**
20
     * Text_Figlet object
21
     *
22
     * @access private
23
     * @var resource
24
     */
25
    var $_fig;
26
 
27
    /**
28
     * Width of CAPTCHA
29
     *
30
     * @access private
31
     * @var int
32
     */
33
    var $_width;
34
 
35
    /**
36
     * Figlet output string
37
     *
38
     * @access private
39
     * @var string
40
     */
41
    var $_output_string;
42
 
43
     /**
44
     * Figlet font options
45
     *
46
     * @access private
47
     * @var array
48
     */
49
    var $_fonts = array();
50
 
51
    /**
52
     * Figlet font
53
     *
54
     * @access private
55
     * @var string
56
     */
57
    var $_font;
58
 
59
    /**
60
     * Figlet font
61
     *
62
     * @access private
63
     * @var array
64
     */
65
    var $_style = array();
66
 
67
    /**
68
     * Output Format
69
     *
70
     * @access private
71
     * @var string
72
     */
73
    var $_output;
74
 
75
    /**
76
     * Last error
77
     *
78
     * @access protected
79
     * @var PEAR_Error
80
     */
81
    var $_error = null;
82
 
83
    /**
84
     * init function
85
     *
86
     * Initializes the new Text_CAPTCHA_Driver_Figlet object and creates a GD image
87
     *
88
     * @param array $options CAPTCHA options
89
     * @access public
90
     * @return mixed true upon success, PEAR error otherwise
91
     */
92
    function init($options = array())
93
    {
94
        if (is_array($options)) {
95
            if (!empty($options['output'])) {
96
                $this->_output = $options['output'];
97
            } else {
98
                $this->_output = 'html';
99
            }
100
 
101
            if (isset($options['width']) && is_int($options['width'])) {
102
                $this->_width = $options['width'];
103
            } else {
104
                $this->_width = 200;
105
            }
106
 
107
            if (!empty($options['length'])) {
108
                $this->_length = $options['length'];
109
            } else {
110
                $this->_length = 6;
111
            }
112
 
113
            if (!isset($options['phrase']) || empty($options['phrase'])) {
114
                $phraseoptions = (isset($options['phraseOptions']) && is_array($options['phraseOptions'])) ? $options['phraseOptions'] : array();
115
                $this->_createPhrase($phraseoptions);
116
            } else {
117
                $this->_phrase = $options['phrase'];
118
            }
119
        }
120
 
121
        if (!isset($options['options']) || empty($options['options']) || !is_array($options['options'])) {
122
            die;
123
        } else {
124
            if (isset($options['options']['style']) && !empty($options['options']['style']) && is_array($options['options']['style'])) {
125
                $this->_style = $options['options']['style'];
126
            }
127
 
128
            if (!isset($this->_style['padding']) || empty($this->_style['padding'])) {
129
                $this->_style['padding'] = '5px';
130
            }
131
 
132
            if (isset($options['options']['font_file']) && !empty($options['options']['font_file'])) {
133
                if (is_array($options['options']['font_file'])) {
134
                    $this->_font = $options['options']['font_file'][array_rand($options['options']['font_file'])];
135
                } else {
136
                    $this->_font = $options['options']['font_file'];
137
                }
138
            }
139
        }
140
    }
141
 
142
    /**
143
     * Create random CAPTCHA phrase
144
     * This method creates a random phrase
145
     *
146
     * @param array $options Optionally supply advanced options for the phase creation;
147
     *                       used for the initialization of Text_Password
148
     *
149
     * @access private
150
     * @return void
151
     */
152
    function _createPhrase($options = array())
153
    {
154
        if (!is_array($options) || count($options) === 0) {
155
            $this->_phrase = Text_Password::create($this->_length);
156
        } else {
157
            if (count($options) === 1) {
158
                $this->_phrase = Text_Password::create($this->_length, $options[0]);
159
            } else {
160
                $this->_phrase = Text_Password::create($this->_length, $options[0], $options[1]);
161
            }
162
        }
163
    }
164
 
165
    /**
166
     * Create CAPTCHA image
167
     *
168
     * This method creates a CAPTCHA image
169
     *
170
     * @access private
171
     * @return void PEAR_Error on error
172
     */
173
    function _createCAPTCHA()
174
    {
175
        $this->_fig = new Text_Figlet();
176
 
177
        if (PEAR::isError($this->_fig->LoadFont($this->_font))) {
178
            $this->_error = PEAR::raiseError('Error loading Text_Figlet font');
179
            return $this->_error;
180
        }
181
 
182
        $this->_output_string = $this->_fig->LineEcho($this->_phrase);
183
    }
184
 
185
    /**
186
     * Return CAPTCHA in the specified format
187
     *
188
     * This method returns the CAPTCHA depending on the output format
189
     *
190
     * @access public
191
     * @return mixed Formatted captcha or PEAR error
192
     */
193
    function getCAPTCHA()
194
    {
195
        $retval = $this->_createCAPTCHA();
196
        if (PEAR::isError($retval)) {
197
            return PEAR::raiseError($retval->getMessage());
198
        }
199
 
200
        switch ($this->_output) {
201
            case 'text':
202
                return $this->_output_string;
203
                break;
204
            case 'html':
205
                return $this->getCAPTCHAAsHTML();
206
                break;
207
            case 'javascript':
208
                return $this->getCAPTCHAAsJavascript();
209
                break;
210
        }
211
    }
212
 
213
    /**
214
     * Return CAPTCHA as HTML
215
     *
216
     * This method returns the CAPTCHA as HTML
217
     *
218
     * @access public
219
     * @return mixed HTML Figlet image or PEAR error
220
     */
221
    function getCAPTCHAAsHTML()
222
    {
223
        $retval = $this->_createCAPTCHA();
224
        if (PEAR::isError($retval)) {
225
            return PEAR::raiseError($retval->getMessage());
226
        }
227
 
228
        $charwidth = strpos($this->_output_string, "\n");
229
        $data = str_replace("\n", '<br />', $this->_output_string);
230
 
231
        $textsize = ($this->_width / $charwidth) * 1.4;
232
 
233
        $css_output = "";
234
        foreach ($this->_style as $key => $value) {
235
            $css_output .= "$key: $value;";
236
        }
237
 
238
        $htmloutput = '<div style="font-family: courier;
239
          font-size: '.$textsize.'px;
240
          width:'.$this->_width.'px;
241
          text-align:center;">';
242
        $htmloutput .= '<div style="'.$css_output.'margin:0px;">
243
          <pre style="padding: 0px; margin: 0px;">'. $data. '</pre></div></div>';
244
 
245
        return $htmloutput;
246
    }
247
 
248
    /**
249
     * Return CAPTCHA as JavaScript version of HTML
250
     *
251
     * This method returns the CAPTCHA as a JavaScript string
252
     * I'm not exactly sure what the point of doing this would be.
253
     *
254
     * @access public
255
     * @return mixed JavaScript string or PEAR error
256
     */
257
    function getCAPTCHAAsJavascript()
258
    {
259
        $data = $this->getCAPTCHAAsHTML();
260
        if (PEAR::isError($data)) {
261
            return PEAR::raiseError($data->getMessage());
262
        }
263
 
264
        $obfus_data = rawurlencode($data);
265
 
266
        $javascript = "<script type=\"text/javascript\">
267
          document.write(unescape(\"${obfus_data}\"));
268
          </script>";
269
 
270
        return $javascript;
271
    }
272
}