| | 1 | <?php |
|---|
| | 2 | /* Reminder: always indent with 4 spaces (no tabs). */ |
|---|
| | 3 | // +---------------------------------------------------------------------------+ |
|---|
| | 4 | // | Copyright (c) 2006, Demian Turner | |
|---|
| | 5 | // | All rights reserved. | |
|---|
| | 6 | // | | |
|---|
| | 7 | // | Redistribution and use in source and binary forms, with or without | |
|---|
| | 8 | // | modification, are permitted provided that the following conditions | |
|---|
| | 9 | // | are met: | |
|---|
| | 10 | // | | |
|---|
| | 11 | // | o Redistributions of source code must retain the above copyright | |
|---|
| | 12 | // | notice, this list of conditions and the following disclaimer. | |
|---|
| | 13 | // | o Redistributions in binary form must reproduce the above copyright | |
|---|
| | 14 | // | notice, this list of conditions and the following disclaimer in the | |
|---|
| | 15 | // | documentation and/or other materials provided with the distribution. | |
|---|
| | 16 | // | o The names of the authors may not be used to endorse or promote | |
|---|
| | 17 | // | products derived from this software without specific prior written | |
|---|
| | 18 | // | permission. | |
|---|
| | 19 | // | | |
|---|
| | 20 | // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|---|
| | 21 | // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|---|
| | 22 | // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
|---|
| | 23 | // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|---|
| | 24 | // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|---|
| | 25 | // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|---|
| | 26 | // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|---|
| | 27 | // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|---|
| | 28 | // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|---|
| | 29 | // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|---|
| | 30 | // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
|---|
| | 31 | // | | |
|---|
| | 32 | // +---------------------------------------------------------------------------+ |
|---|
| | 33 | // | Seagull 0.6 | |
|---|
| | 34 | // +---------------------------------------------------------------------------+ |
|---|
| | 35 | // | Captcha.php | |
|---|
| | 36 | // +---------------------------------------------------------------------------+ |
|---|
| | 37 | // | Author: Steven Stremciuc <steve@freeslacker.net> | |
|---|
| | 38 | // +---------------------------------------------------------------------------+ |
|---|
| | 39 | |
|---|
| | 40 | /** |
|---|
| | 41 | * Provides methods for generating/validating a captcha. |
|---|
| | 42 | */ |
|---|
| | 43 | class SGL_Captcha |
|---|
| | 44 | { |
|---|
| | 45 | /** |
|---|
| | 46 | * Returns random ascii art code for use as captcha. |
|---|
| | 47 | * |
|---|
| | 48 | * @access public |
|---|
| | 49 | * @static |
|---|
| | 50 | * @return string $captcha formatted ascii art captcha |
|---|
| | 51 | */ |
|---|
| | 52 | function generateCaptcha() |
|---|
| | 53 | { |
|---|
| | 54 | $aNumbers = array( |
|---|
| | 55 | array(' ### ',' # ',' ##### ',' ##### ',' # ',' ####### ',' ##### ',' ####### ',' ##### ',' ##### '), |
|---|
| | 56 | array(' # # ',' ## ',' # # ',' # # ',' # # ',' # ',' # # ',' # # ',' # # ',' # # '), |
|---|
| | 57 | array(' # # ',' # # ',' # ',' # ',' # # ',' # ',' # ',' # ',' # # ',' # # '), |
|---|
| | 58 | array(' # # ',' # ',' ##### ',' ##### ',' # # ',' ###### ',' ###### ',' # ',' ##### ',' ###### '), |
|---|
| | 59 | array(' # # ',' # ',' # ',' # ',' ####### ',' # ',' # # ',' # ',' # # ',' # '), |
|---|
| | 60 | array(' # # ',' # ',' # ',' # # ',' # ',' # # ',' # # ',' # ',' # # ',' # # '), |
|---|
| | 61 | array(' ### ',' ##### ',' ####### ',' ##### ',' # ',' ##### ',' ##### ',' # ',' ##### ',' ##### '), |
|---|
| | 62 | ); |
|---|
| | 63 | |
|---|
| | 64 | // randomly pick 4 numbers |
|---|
| | 65 | $aCode = array_rand($aNumbers[0], 4); |
|---|
| | 66 | |
|---|
| | 67 | // get code |
|---|
| | 68 | $code = ''; |
|---|
| | 69 | foreach ($aCode as $digit) { |
|---|
| | 70 | $code .= $digit; |
|---|
| | 71 | } |
|---|
| | 72 | |
|---|
| | 73 | // set code in users session |
|---|
| | 74 | SGL_Session::set('captcha',$code); |
|---|
| | 75 | |
|---|
| | 76 | // turn code into ascii art numbers |
|---|
| | 77 | $captcha = ''; |
|---|
| | 78 | for ($i = 0;$i < 7;$i++) { |
|---|
| | 79 | foreach ($aCode as $digit) { |
|---|
| | 80 | $captcha .= $aNumbers[$i][$digit]; |
|---|
| | 81 | } |
|---|
| | 82 | $captcha .="\n"; |
|---|
| | 83 | } |
|---|
| | 84 | |
|---|
| | 85 | return $captcha; |
|---|
| | 86 | } |
|---|
| | 87 | |
|---|
| | 88 | /** |
|---|
| | 89 | * Validates input against captcha code in users session. |
|---|
| | 90 | * |
|---|
| | 91 | * @access public |
|---|
| | 92 | * @param string $captcha |
|---|
| | 93 | * @static |
|---|
| | 94 | * @return bool |
|---|
| | 95 | */ |
|---|
| | 96 | function validateCaptcha($captcha = '') |
|---|
| | 97 | { |
|---|
| | 98 | if (!empty($captcha) && SGL_Session::get('captcha') == $captcha) { |
|---|
| | 99 | return true; |
|---|
| | 100 | } |
|---|
| | 101 | return false; |
|---|
| | 102 | } |
|---|
| | 103 | } |
|---|
| | 104 | ?> |