Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// {{{ Class Text_CAPTCHA_Driver_Numeral
3
// +----------------------------------------------------------------------+
4
// | PHP version 5                                                       |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1998-2006 David Coallier                               |
7
// | All rights reserved.                                                 |
8
// +----------------------------------------------------------------------+
9
// |                                                                      |
10
// | Redistribution and use in source and binary forms, with or without   |
11
// | modification, are permitted provided that the following conditions   |
12
// | are met:                                                             |
13
// |                                                                      |
14
// | Redistributions of source code must retain the above copyright       |
15
// | notice, this list of conditions and the following disclaimer.        |
16
// |                                                                      |
17
// | Redistributions in binary form must reproduce the above copyright    |
18
// | notice, this list of conditions and the following disclaimer in the  |
19
// | documentation and/or other materials provided with the distribution. |
20
// |                                                                      |
21
// | Neither the name of David Coallier nor the names of his contributors |
22
// | may be used to endorse                                               |
23
// | or promote products derived from this software without specific prior|
24
// | written permission.                                                  |
25
// |                                                                      |
26
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
27
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
28
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
29
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
30
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
31
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
32
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
33
// |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
34
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
35
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
36
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
37
// | POSSIBILITY OF SUCH DAMAGE.                                          |
38
// +----------------------------------------------------------------------+
39
// | Author: David Coallier <davidc@agoraproduction.com>                  |
40
// +----------------------------------------------------------------------+
41
//
42
require_once 'Text/CAPTCHA.php';
43
/**
44
 * Class used for numeral captchas
45
 *
46
 * This class is intended to be used to generate
47
 * numeral captchas as such as:
48
 * Example:
49
 *  Give me the answer to "54 + 2" to prove that you are human.
50
 *
51
 * @category Text
52
 * @package  Text_CAPTCHA
53
 * @author   David Coallier <davidc@agoraproduction.com>
54
 * @author   Christian Wenz <wenz@php.net>
55
 */
56
class Text_CAPTCHA_Driver_Numeral extends Text_CAPTCHA
57
{
58
    // {{{ Variables
59
    /**
60
     * Minimum range value
61
     *
62
     * This variable holds the minimum range value
63
     * default set to "1"
64
     *
65
     * @access private
66
     * @var    integer $_minValue The minimum range value
67
     */
68
    var $_minValue = 1;
69
 
70
    /**
71
     * Maximum range value
72
     *
73
     * This variable holds the maximum range value
74
     * default set to "50"
75
     *
76
     * @access private
77
     * @var    integer $_maxValue The maximum value of the number range
78
     */
79
    var $_maxValue = 50;
80
 
81
    /**
82
     * Operators
83
     *
84
     * The valid operators to use
85
     * in the numeral captcha. We could
86
     * use / and * but not yet.
87
     *
88
     * @access private
89
     * @var    array $_operators The operations for the captcha
90
     */
91
    var $_operators = array('-', '+');
92
 
93
    /**
94
     * Operator to use
95
     *
96
     * This variable is basically the operation
97
     * that we're going to be using in the
98
     * numeral captcha we are about to generate.
99
     *
100
     * @access private
101
     * @var    string $_operator The operation's operator
102
     */
103
    var $_operator = '';
104
 
105
    /**
106
     * Mathematical Operation
107
     *
108
     * This is the mathematical operation
109
     * that we are displaying to the user.
110
     *
111
     * @access private
112
     * @var    string $_operation The math operation
113
     */
114
    var $_operation = '';
115
 
116
    /**
117
     * First number of the operation
118
     *
119
     * This variable holds the first number
120
     * of the numeral operation we are about
121
     * to generate.
122
     *
123
     * @access private
124
     * @var    integer $_firstNumber The first number of the operation
125
     */
126
    var $_firstNumber = '';
127
 
128
    /**
129
     * Second Number of the operation
130
     *
131
     * This variable holds the value of the
132
     * second variable of the operation we are
133
     * about to generate for the captcha.
134
     *
135
     * @access private
136
     * @var    integer $_secondNumber The second number of the operation
137
     */
138
    var $_secondNumber = '';
139
    // }}}
140
    // {{{ Constructor
141
    function init($options = array())
142
    {
143
        if (isset($options['minValue'])) {
144
            $this->_minValue = (int)$options['minValue'];
145
        }
146
        if (isset($options['maxValue'])) {
147
            $this->_maxValue = (int)$options['maxValue'];
148
        }
149
 
150
        $this->_createCAPTCHA();
151
    }
152
    // }}}
153
    // {{{ private function _createCAPTCHA
154
    /**
155
     * Create the CAPTCHA (the numeral expression)
156
     *
157
     * This function determines a random numeral expression
158
     * and set the associated class properties
159
     *
160
     * @access private
161
     * @return void
162
     */
163
    function _createCAPTCHA()
164
    {
165
        $this->_generateFirstNumber();
166
        $this->_generateSecondNumber();
167
        $this->_generateOperator();
168
        $this->_generateOperation();
169
    }
170
    // }}}
171
    // {{{ private function _setRangeMinimum
172
    /**
173
     * Set Range Minimum value
174
     *
175
     * This function give the developer the ability
176
     * to set the range minimum value so the operations
177
     * can be bigger, smaller, etc.
178
     *
179
     * @param  integer $minValue The minimum value
180
     *
181
     * @access private
182
     * @return void
183
     */
184
    function _setRangeMinimum($minValue = 1)
185
    {
186
        $this->minValue = (int)$minValue;
187
    }
188
    // }}}
189
    // {{{ private function _generateFirstNumber
190
    /**
191
     * Sets the first number
192
     *
193
     * This function sets the first number
194
     * of the operation by calling the _generateNumber
195
     * function that generates a random number.
196
     *
197
     * @access private
198
     * @return void
199
     * @see    $this->_firstNumber, $this->_generateNumber
200
     */
201
    function _generateFirstNumber()
202
    {
203
        $this->_setFirstNumber($this->_generateNumber());
204
    }
205
    // }}}
206
    // {{{ private function generateSecondNumber
207
    /**
208
     * Sets second number
209
     *
210
     * This function sets the second number of the
211
     * operation by calling _generateNumber()
212
     *
213
     * @access private
214
     * @return void
215
     * @see    $this->_secondNumber, $this->_generateNumber()
216
     */
217
    function _generateSecondNumber()
218
    {
219
        $this->_setSecondNumber($this->_generateNumber());
220
    }
221
    // }}}
222
    // {{{ private function generateOperator
223
    /**
224
     * Sets the operation operator
225
     *
226
     * This function sets the operation operator by
227
     * getting the array value of an array_rand() of
228
     * the $this->_operators() array.
229
     *
230
     * @access private
231
     * @return void
232
     * @see    $this->_operators, $this->_operator
233
     */
234
    function _generateOperator()
235
    {
236
        $this->_operator = $this->_operators[array_rand($this->_operators)];
237
    }
238
    // }}}
239
    // {{{ private function setAnswer
240
    /**
241
     * Sets the answer value
242
     *
243
     * This function will accept the parameters which is
244
     * basically the result of the function we have done
245
     * and it will set $this->answer with it.
246
     *
247
     * @param  integer $phraseValue The answer value
248
     *
249
     * @access private
250
     * @return void
251
     * @see    $this->_phrase
252
     */
253
    function _setPhrase($phraseValue)
254
    {
255
        $this->_phrase = $phraseValue;
256
    }
257
    // }}}
258
    // {{{ private function setFirstNumber
259
    /**
260
     * Set First number
261
     *
262
     * This function sets the first number
263
     * to the value passed to the function
264
     *
265
     * @param  integer $value The first number value.
266
     *
267
     * @access private
268
     * @return void
269
     */
270
    function _setFirstNumber($value)
271
    {
272
        $this->_firstNumber = (int)$value;
273
    }
274
    // }}}
275
    // {{{ private function setSecondNumber
276
    /**
277
     * Sets the second number
278
     *
279
     * This function sets the second number
280
     * with the value passed to it.
281
     *
282
     * @param  integer $value The second number new value.
283
     *
284
     * @access private
285
     * @return void
286
     */
287
    function _setSecondNumber($value)
288
    {
289
        $this->_secondNumber = (int)$value;
290
    }
291
    // }}}
292
    // {{{ private function setOperation
293
    /**
294
     * Set operation
295
     *
296
     * This variable sets the operation variable
297
     * by taking the firstNumber, secondNumber and operator
298
     *
299
     * @access private
300
     * @return void
301
     * @see    $this->_operation
302
     */
303
    function _setOperation()
304
    {
305
        $this->_operation = $this->_getFirstNumber() . ' ' .
306
                            $this->_operator . ' ' .
307
                            $this->_getSecondNumber();
308
    }
309
    // }}}
310
    // {{{ private function _generateNumber
311
    /**
312
     * Generate a number
313
     *
314
     * This function takes the parameters that are in
315
     * the $this->_maxValue and $this->_minValue and get
316
     * the random number from them using mt_rand()
317
     *
318
     * @access private
319
     * @return integer Random value between _minValue and _maxValue
320
     */
321
    function _generateNumber()
322
    {
323
        return mt_rand($this->_minValue, $this->_maxValue);
324
    }
325
    // }}}
326
    // {{{ private function _doAdd
327
    /**
328
     * Adds values
329
     *
330
     * This function will add the firstNumber and the
331
     * secondNumber value and then call setAnswer to
332
     * set the answer value.
333
     *
334
     * @access private
335
     * @return void
336
     * @see    $this->_firstNumber, $this->_secondNumber, $this->_setAnswer()
337
     */
338
    function _doAdd()
339
    {
340
        $phrase = $this->_getFirstNumber() + $this->_getSecondNumber();
341
        $this->_setPhrase($phrase);
342
    }
343
    // }}}
344
    // {{{ private function _doSubstract
345
    /**
346
     * Does a substract on the values
347
     *
348
     * This function executes a substraction on the firstNumber
349
     * and the secondNumber to then call $this->setAnswer to set
350
     * the answer value.
351
     *
352
     * If the firstnumber value is smaller than the secondnumber value
353
     * then we regenerate the first number and regenerate the operation.
354
     *
355
     * @access private
356
     * @return void
357
     * @see    $this->_firstNumber, $this->_secondNumber, $this->_setAnswer()
358
     */
359
    function _doSubstract()
360
    {
361
        $first  = $this->_getFirstNumber();
362
        $second = $this->_getSecondNumber();
363
 
364
        /**
365
         * Check if firstNumber is smaller than secondNumber
366
         */
367
        if ($first < $second) {
368
            $this->_setFirstNumber($second);
369
            $this->_setSecondNumber($first);
370
            $this->_setOperation();
371
        }
372
 
373
        $phrase = $this->_getFirstNumber() - $this->_getSecondNumber();
374
        $this->_setPhrase($phrase);
375
    }
376
    // }}}
377
    // {{{ private function _generateOperation
378
    /**
379
     * Generate the operation
380
     *
381
     * This function will call the _setOperation() function
382
     * to set the operation string that will be called
383
     * to display the operation, and call the function necessary
384
     * depending on which operation is set by this->operator.
385
     *
386
     * @access private
387
     * @return void
388
     * @see    $this->_setOperation(), $this->_operator
389
     */
390
    function _generateOperation()
391
    {
392
        $this->_setOperation();
393
 
394
        switch ($this->_operator) {
395
        case '+':
396
            $this->_doAdd();
397
            break;
398
        case '-':
399
            $this->_doSubstract();
400
            break;
401
        default:
402
            $this->_doAdd();
403
            break;
404
        }
405
    }
406
    // }}}
407
    // {{{ public function _getFirstNumber
408
    /**
409
     * Get the first number
410
     *
411
     * This function will get the first number
412
     * value from $this->_firstNumber
413
     *
414
     * @access public
415
     * @return integer $this->_firstNumber The firstNumber
416
     */
417
    function _getFirstNumber()
418
    {
419
        return $this->_firstNumber;
420
    }
421
    // }}}
422
    // {{{ public function _getSecondNumber
423
    /**
424
     * Get the second number value
425
     *
426
     * This function will return the second number value
427
     *
428
     * @access public
429
     * @return integer $this->_secondNumber The second number
430
     */
431
    function _getSecondNumber()
432
    {
433
        return $this->_secondNumber;
434
    }
435
    // }}}
436
    // {{{ public function getCAPTCHA
437
    /**
438
     * Get operation
439
     *
440
     * This function will get the operation
441
     * string from $this->_operation
442
     *
443
     * @access public
444
     * @return string The operation String
445
     */
446
    function getCAPTCHA()
447
    {
448
        return $this->_operation;
449
    }
450
}
451
// }}}