phpMyFAQ /phpmyfaq/inc/Captcha.php

Language PHP Lines 566
MD5 Hash c4275f43f444cb98ce10b8a712ad36b8
Repository https://github.com/cyrke/phpMyFAQ.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
/**
 * The phpMyFAQ Captcha class
 *
 * PHP Version 5.3
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * @category  phpMyFAQ
 * @package   PMF_Captcha
 * @author    Thomas Zeithaml <seo@annatom.de>
 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
 * @author    Matteo Scaramuccia <matteo@scaramuccia.com>
 * @copyright 2006-2012 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2006-02-04
 */

if (!defined('IS_VALID_PHPMYFAQ')) {
    exit();
}

/**
 * PMF_Captcha
 *
 * @category  phpMyFAQ
 * @package   PMF_Captcha
 * @author    Thomas Zeithaml <seo@annatom.de>
 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
 * @author    Matteo Scaramuccia <matteo@scaramuccia.com>
 * @copyright 2006-2012 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2006-02-04
 */
class PMF_Captcha
{
    /**
     * @var PMF_Configuration
     */
    private $_config = null;

    /**
     * The phpMyFAQ session id
     *
     * @var string
     */
    private $sids;

    /**
     * Array of fonts
     *
     * @var array
     */
    private $fonts = array();

    /**
     * The captcha code
     *
     * @var string
     */
    private $code = '';

    /**
     * Array of characters
     *
     * @var array
     */
    private $letters = array(
        '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
        'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
        'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    );

    /**
     * Length of the captcha code
     *
     * @var integer
     */
    public $caplength = 6;

    /**
     * Width of the image
     *
     * @var integer
     */
    private $width = 165;

    /**
     * Height of the image
     *
     * @var integer
     */
    private $height = 40;

    /**
     * JPEG quality in percents
     *
     * @var integer
     */
    private $quality = 60;

    /**
     * Random background color RGB components
     *
     * @var array
     */
    private $_backgroundColor;

    /**
     * Generated image
     *
     * @var resource
     */
    private $img;

    /**
     * The user agent string
     *
     * @var string
     */
    private $userAgent;

    /**
     * Timestamp
     *
     * @var integer
     */
    private $timestamp;

    /**
     * Constructor
     *
     * @param PMF_Configuration $config
     *
     * @return PMF_Captcha
     */
    public function __construct(PMF_Configuration $config)
    {
        $this->_config   = $config;
        $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
        $this->ip        = $_SERVER['REMOTE_ADDR'];
        $this->fonts     = $this->getFonts();
        $this->timestamp = $_SERVER['REQUEST_TIME'];
    }

    //
    // public functions
    //
    
    /**
     * Setter for session id
     *
     * @param integer $sid session id
     *
     * @return void
     */
    public function setSessionId($sid)
    {
        $this->sids = $sid;
    }
    
    /**
     * Setter for the captcha code length
     *
     * @param integer $caplength Length of captch code
     *
     * @return void
     */
    public function setCodeLength($length = 6)
    {
        $this->caplength = $length;
    }
    
    /**
     * Gives the HTML output code for the Captcha
     *
     * @param   string $action The action parameter
     * @return  string
     */
    public function printCaptcha($action)
    {
        $output = sprintf(
            '<img id="captchaImage" src="%s?%saction=%s&amp;gen=img&amp;ck=%s" height="%d" width="%d" border="0" alt="%s" title="%s" />',
            $_SERVER['SCRIPT_NAME'],
            $this->sids,
            $action,
            $_SERVER['REQUEST_TIME'],
            $this->height,
            $this->width,
            'Chuck Norris has counted to infinity. Twice.',
            'click to refresh');
        return $output;
    }

    /**
     * Draw the Captcha
     *
     * @return  void
     */
    public function showCaptchaImg()
    {
        $this->createBackground();
        $this->drawlines();
        $this->generateCaptchaCode($this->caplength);
        $this->drawText();
        if (function_exists('imagepng')) {
            header('Content-Type: image/png');
            imagepng($this->img);
        } elseif (function_exists('imagejpeg')) {
            header('Content-Type: image/jpeg');
            imagejpeg($this->img, '', ( int )$this->quality);
        } elseif (function_exists('imagegif')) {
            header('Content-Type: image/gif');
            imagegif($this->img);
        }
        imagedestroy($this->img);
    }

    /**
     * Gets the Captcha from the DB
     *
     * @return  string
     */
    public function getCaptchaCode()
    {
        $query  = sprintf('SELECT id FROM %sfaqcaptcha', PMF_Db::getTablePrefix());
        $result = $this->_config->getDb()->query($query);
        while ($row = $this->_config->fetchArray($result)) {
            $this->code = $row['id'];
        }

        return $this->code;
    }

    /**
     * Validate the Captcha
     *
     * @param  string $captchaCode Captcha code
     * @return boolean
     */
    public function validateCaptchaCode($captchaCode)
    {
        // Sanity check
        if (0 == PMF_String::strlen($captchaCode)) {
            return false;
        }

        $captchaCode = PMF_String::strtoupper($captchaCode);
        // Help the user: treat "0" (ASCII 48) like "O" (ASCII 79)
        //                if "0" is not in the realm of captcha code letters
        if (!in_array("0", $this->letters)) {
            $captchaCode = str_replace("0", "O", $captchaCode);
        }
        // Sanity check
        for ($i = 0; $i < PMF_String::strlen( $captchaCode ); $i++) {
            if (!in_array($captchaCode[$i], $this->letters)) {
                return false;
            }
        }
        // Search for this Captcha in the db
        $query = sprintf("
            SELECT
                id
            FROM
                %sfaqcaptcha
            WHERE
                id = '%s'",
            PMF_Db::getTablePrefix(),
            $this->_config->getDb()->escape($captchaCode));

        if ($result = $this->_config->getDb()->query($query)) {
            $num = $this->_config->getDb()->numRows($result);
            if ($num > 0) {
                $this->code = $captchaCode;
                $this->removeCaptcha($captchaCode);
                return true;
            }
        }

        return false;
    }
    
    /**
     * This function checks the provided captcha code
     * if the captcha code spam protection has been activated from the general PMF configuration.
     *
     * @param  string $code Captcha Code
     * @return bool
     */
    public function checkCaptchaCode($code)
    {
        if ($this->_config->get('spam.enableCaptchaCode')) {
            return $this->validateCaptchaCode($code);
        } else {
            return true;
        }
    }
    

    //
    // private functions
    //

    /**
     * Draw random lines
     *
     * @return resource
     */
    private function drawlines()
    {
        $color1   = rand(150, 185);
        $color2   = rand(185, 225);
        $nextline = 4;
        $w1       = 0;
        $w2       = 0;

        for ($x = 0; $x < $this->width; $x += (int)$nextline) {
            if ($x < $this->width) {
                imageline($this->img, $x + $w1, 0, $x + $w2, $this->height - 1, rand($color1, $color2));
            }
            if ($x < $this->height) {
                imageline($this->img, 0, $x - $w2, $this->width - 1, $x - $w1, rand($color1, $color2));
            }
            if (function_exists('imagettftext') && (count($this->fonts) > 0)) {
                $nextline += rand(-5, 7);
                if ($nextline < 1) {
                    $nextline = 2;
                }
            }
            else {
                $nextline += rand(1, 7);
            }
            $w1 += rand(-4, 4);
            $w2 += rand(-4, 4);
        }

        return $this->img;
    }

    /**
     * Draw the Text
     *
     * @return resource
     */
    private function drawText()
    {
        $len = PMF_String::strlen($this->code);
        $w1  = 15;
        $w2  = $this->width / ($len + 1);

        for ($p = 0; $p < $len; $p++) {
            $letter = $this->code[$p];
            if (count($this->fonts) > 0) {
                $font = $this->fonts[rand(0, count($this->fonts) - 1)];
            }
            $size = rand(20, $this->height / 2.2);
            $rotation = rand(-23, 23);
            $y   = rand($size + 3, $this->height-5);
            // $w1 += rand(- $this->width / 90, $this->width / 40 );
            $x   = $w1 + $w2*$p;
            $c1 = array(); // fore char color
            $c2 = array(); // back char color
            do {
                $c1['r'] = mt_rand(30, 199);
            } while ($c1['r'] == $this->_backgroundColor['r']);
            do {
                $c1['g'] = mt_rand(30, 199);
            } while ($c1['g'] == $this->_backgroundColor['g']);
            do {
                $c1['b'] = mt_rand(30, 199);
            } while ($c1['b'] == $this->_backgroundColor['b']);
            $c1 = imagecolorallocate($this->img, $c1['r'], $c1['g'], $c1['b']);
            do {
                $c2['r'] = ($c1['r'] < 100 ? $c1['r'] * 2 : mt_rand(30, 199));
            } while (($c2['r'] == $this->_backgroundColor['r']) && ($c2['r'] == $c1['r']));
            do {
                $c2['g'] = ($c1['g'] < 100 ? $c1['g'] * 2 : mt_rand(30, 199));
            } while (($c2['g'] == $this->_backgroundColor['g']) && ($c2['g'] == $c1['g']));
            do {
                $c2['b'] = ($c1['b'] < 100 ? $c1['b'] * 2 : mt_rand(30, 199));
            } while (($c2['b'] == $this->_backgroundColor['b']) && ($c2['b'] == $c1['b']));
            $c2 = imagecolorallocate($this->img, $c2['r'], $c2['g'], $c2['b']);
            // Add the letter
            if (function_exists('imagettftext') && (count($this->fonts) > 0)) {
                imagettftext($this->img, $size, $rotation, $x + 2, $y,     $c2, $font, $letter);
                imagettftext($this->img, $size, $rotation, $x + 1, $y + 1, $c2, $font, $letter);
                imagettftext($this->img, $size, $rotation, $x,     $y-2,   $c1, $font, $letter);
            } else {
                $size = 5;
                $c3 = imagecolorallocate($this->img, 0, 0, 255);
                $x = 20;
                $y = 12;
                $s = 30;
                imagestring($this->img, $size, $x + 1 + ($s * $p), $y+1, $letter, $c3);
                imagestring($this->img, $size, $x + ($s * $p),     $y,   $letter, $c1);
            }
        }

        return $this->img;
    }

    /**
     * Create the background
     *
     * @return resource
     */
    private function createBackground()
    {
        $this->img                   = imagecreate($this->width, $this->height);
        $this->_backgroundColor['r'] = rand(220, 255);
        $this->_backgroundColor['g'] = rand(220, 255);
        $this->_backgroundColor['b'] = rand(220, 255);
        
        $colorallocate = imagecolorallocate(
            $this->img,
            $this->_backgroundColor['r'],
            $this->_backgroundColor['g'],
            $this->_backgroundColor['b']
        );
                                            
        imagefilledrectangle($this->img, 0, 0, $this->width, $this->height, $colorallocate);

        return $this->img;
    }

    /**
     * Generate a Captcha Code
     *
     * @param   integer $caplength Length of captch code
     * @return  string
     */
    private function generateCaptchaCode($caplength)
    {
        // Start garbage collector for removing old (==unresolved) captcha codes
        // Note that we would like to avoid performing any garbaging of old records
        // because these data could be used as a database for collecting ip addresses,
        // eventually organizing them in subnetwork addresses, in order to use
        // them as an input for PMF IP banning.
        // This because we always perform these 3 checks on the public forms
        // in which captcha code feature is attached:
        //   1. Check against IP/Network address
        //   2. Check against banned words
        //   3. Check against the captcha code
        // so you could ban those "users" at the address level (1.).
        // If you want to look over your current data you could use this SQL query below:
        //   SELECT DISTINCT ip, useragent, COUNT(ip) AS times
        //   FROM faqcaptcha
        //   GROUP BY ip
        //   ORDER BY times DESC
        // to find out *bots and human attempts
        $this->garbageCollector();

        // Create the captcha code
        for ($i = 1; $i <= $caplength; $i++) {
            $j = floor(rand(0,34));
            $this->code .= $this->letters[$j];
        }
        if (!$this->saveCaptcha()) {
            return $this->generateCaptchaCode($caplength);
        }

        return $this->code;
    }

    /**
    * Save the Captcha
    *
    * @return   boolean
    */
    private function saveCaptcha()
    {
        $select = sprintf("
           SELECT 
               id 
           FROM 
               %sfaqcaptcha 
           WHERE 
               id = '%s'",
           PMF_Db::getTablePrefix(),
           $this->code
        );
        
        $result = $this->_config->getDb()->query($select);
        
        if ($result) {
            $num = $this->_config->getDb()->numRows($result);
            if ($num > 0) {
                return false;
            } else {
                $insert = sprintf("
                    INSERT INTO 
                        %sfaqcaptcha 
                    (id, useragent, language, ip, captcha_time) 
                        VALUES 
                    ('%s', '%s', '%s', '%s', %d)", 
                    PMF_Db::getTablePrefix(),
                    $this->code, 
                    $this->userAgent, 
                    $this->_config->getLanguage()->getLanguage(),
                    $this->ip, 
                    $this->timestamp);
                    
                $this->_config->getDb()->query($insert);
                return true;
            }
        }

        return false;
    }

    /**
     * Remove the Captcha
     *
     * @param  string $captchaCode Captch code
     * @return void
     */
    private function removeCaptcha($captchaCode = null)
    {
        if ($captchaCode == null) {
            $captchaCode = $this->code;
        }
        $query = sprintf("DELETE FROM %sfaqcaptcha WHERE id = '%s'", PMF_Db::getTablePrefix(), $captchaCode);
        $this->_config->getDb()->query($query);
    }

    /**
     * Delete old captcha records.
     *
     * During normal use the <b>faqcaptcha</b> table would be empty, on average:
     * each record is created when a captcha image is showed to the user
     * and deleted upon a successful matching, so, on average, a record
     * in this table is probably related to a spam attack.
     *
     * @param  int $time The time (sec) to define a captcha code old and ready 
     *                   to be deleted (default: 1 week)
     * @return void
     */
    private function garbageCollector($time = 604800)
    {
        $delete = sprintf("
            DELETE FROM 
                %sfaqcaptcha 
            WHERE 
                captcha_time < %d", 
            PMF_Db::getTablePrefix(),
            $_SERVER['REQUEST_TIME'] - $time);
            
        $this->_config->getDb()->query($delete);
    }

    /**
     * Get Fonts
     *
     * @return array
     */
    private function getFonts()
    {
        return glob(__DIR__ . '/fonts/*.ttf');
    }
}
Back to Top