-
Notifications
You must be signed in to change notification settings - Fork 190
/
securimage.php
2977 lines (2537 loc) · 99.7 KB
/
securimage.php
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//error_reporting(E_ALL); ini_set('display_errors', 1); // uncomment this line for debugging
/**
* Project: Securimage: A PHP class dealing with CAPTCHA images, audio, and validation
* File: securimage.php
*
* Copyright (c) 2018, Drew Phillips
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Any modifications to the library should be indicated clearly in the source code
* to inform users that the changes are not a part of the original software.
*
* @link https://github.com/dapphp/securimage Securimage Homepage
* @copyright 2018 Drew Phillips
* @author Drew Phillips <[email protected]>
* @version 4.0.2 (March 2018)
* @package Securimage
*
* See the file CHANGES for changelog
*
*/
/**
* Securimage CAPTCHA Class.
*
* A class for creating and validating secure CAPTCHA images and audio.
*
* The class contains many options regarding appearance, security, storage of
* captcha data and image/audio generation options.
*
* @package Securimage
* @subpackage classes
* @author Drew Phillips <[email protected]>
*
*/
class Securimage
{
// All of the public variables below are securimage options
// They can be passed as an array to the Securimage constructor, set below,
// or set from securimage_show.php and securimage_play.php
/**
* Constant for rendering captcha as a JPEG image
* @var int
*/
const SI_IMAGE_JPEG = 1;
/**
* Constant for rendering captcha as a PNG image (default)
* @var int
*/
const SI_IMAGE_PNG = 2;
/**
* Constant for rendering captcha as a GIF image
* @var int
*/
const SI_IMAGE_GIF = 3;
/**
* Constant for generating a normal alphanumeric captcha based on the
* character set
*
* @see Securimage::$charset charset property
* @var int
*/
const SI_CAPTCHA_STRING = 0;
/**
* Constant for generating a captcha consisting of a simple math problem
*
* @var int
*/
const SI_CAPTCHA_MATHEMATIC = 1;
/**
* Constant for generating a word based captcha using 2 words from a list
*
* @var int
*/
const SI_CAPTCHA_WORDS = 2;
/**
* MySQL option identifier for database storage option
*
* @var string
*/
const SI_DRIVER_MYSQL = 'mysql';
/**
* PostgreSQL option identifier for database storage option
*
* @var string
*/
const SI_DRIVER_PGSQL = 'pgsql';
/**
* SQLite option identifier for database storage option
*
* @var string
*/
const SI_DRIVER_SQLITE3 = 'sqlite';
/**
* getCaptchaHtml() display constant for HTML Captcha Image
*
* @var integer
*/
const HTML_IMG = 1;
/**
* getCaptchaHtml() display constant for HTML5 Audio code
*
* @var integer
*/
const HTML_AUDIO = 2;
/**
* getCaptchaHtml() display constant for Captcha Input text box
*
* @var integer
*/
const HTML_INPUT = 4;
/**
* getCaptchaHtml() display constant for Captcha Text HTML label
*
* @var integer
*/
const HTML_INPUT_LABEL = 8;
/**
* getCaptchaHtml() display constant for HTML Refresh button
*
* @var integer
*/
const HTML_ICON_REFRESH = 16;
/**
* getCaptchaHtml() display constant for all HTML elements (default)
*
* @var integer
*/
const HTML_ALL = 0xffffffff;
/*%*********************************************************************%*/
// Properties
/**
* The width of the captcha image
* @var int
*/
public $image_width = 215;
/**
* The height of the captcha image
* @var int
*/
public $image_height = 80;
/**
* Font size is calculated by image height and this ratio. Leave blank for
* default ratio of 0.4.
*
* Valid range: 0.1 - 0.99.
*
* Depending on image_width, values > 0.6 are probably too large and
* values < 0.3 are too small.
*
* @var float
*/
public $font_ratio;
/**
* The type of the image, default = png
*
* @see Securimage::SI_IMAGE_PNG SI_IMAGE_PNG
* @see Securimage::SI_IMAGE_JPEG SI_IMAGE_JPEG
* @see Securimage::SI_IMAGE_GIF SI_IMAGE_GIF
* @var int
*/
public $image_type = self::SI_IMAGE_PNG;
/**
* The background color of the captcha
* @var Securimage_Color|string
*/
public $image_bg_color = '#ffffff';
/**
* The color of the captcha text
* @var Securimage_Color|string
*/
public $text_color = '#707070';
/**
* The color of the lines over the captcha
* @var Securimage_Color|string
*/
public $line_color = '#707070';
/**
* The color of the noise that is drawn
* @var Securimage_Color|string
*/
public $noise_color = '#707070';
/**
* Deprecated. How transparent to make the text.
*
* 0 = completely opaque, 100 = invisible
*
* @var int
* @deprecated 4.0.0
*/
public $text_transparency_percentage = 0;
/**
* Deprecated. Whether or not to draw the text transparently.
*
* true = use transparency, false = no transparency
*
* @var bool
* @deprecated 4.0.0
*/
public $use_transparent_text = false;
/**
* The length of the captcha code
* @var int
*/
public $code_length = 6;
/**
* Display random spaces in the captcha text on the image
*
* @var bool true to insert random spacing between groups of letters
*/
public $use_random_spaces = false;
/**
* Draw each character at an angle with random starting angle and increase/decrease per character
* @var bool true to use random angles, false to draw each character normally
*/
public $use_text_angles = false;
/**
* Instead of centering text vertically in the image, the baseline of each character is
* randomized in such a way that the next character is drawn slightly higher or lower than
* the previous in a step-like fashion.
*
* @var bool true to use random baselines, false to center text in image
*/
public $use_random_baseline = false;
/**
* Draw a bounding box around some characters at random. 20% of the time, random boxes
* may be drawn around 0 or more characters on the image.
*
* @var bool true to randomly draw boxes around letters, false not to
*/
public $use_random_boxes = false;
/**
* Whether the captcha should be case sensitive or not.
*
* Not recommended, use only for maximum protection.
*
* @var bool
*/
public $case_sensitive = false;
/**
* The character set to use for generating the captcha code
* @var string
*/
public $charset = 'abcdefghijkmnopqrstuvwxzyABCDEFGHJKLMNPQRSTUVWXZY0123456789';
/**
* How long in seconds a captcha remains valid, after this time it will be
* considered incorrect.
*
* @var int
*/
public $expiry_time = 900;
/**
* true to use the wordlist file, false to generate random captcha codes
* @var bool
*/
public $use_wordlist = false;
/**
* The level of distortion.
*
* 0.75 = normal, 1.0 = very high distortion
*
* @var double
*/
public $perturbation = 0.85;
/**
* How many lines to draw over the captcha code to increase security
* @var int
*/
public $num_lines = 5;
/**
* The level of noise (random dots) to place on the image, 0-10
* @var int
*/
public $noise_level = 2;
/**
* The signature text to draw on the bottom corner of the image
* @var string
*/
public $image_signature = '';
/**
* The color of the signature text
* @var Securimage_Color|string
*/
public $signature_color = '#707070';
/**
* The path to the ttf font file to use for the signature text.
* Defaults to $ttf_file (AHGBold.ttf)
*
* @see Securimage::$ttf_file
* @var string
*/
public $signature_font;
/**
* The type of captcha to create.
*
* Either alphanumeric based on *charset*, a simple math problem, or an
* image consisting of 2 words from the word list.
*
* @see Securimage::SI_CAPTCHA_STRING SI_CAPTCHA_STRING
* @see Securimage::SI_CAPTCHA_MATHEMATIC SI_CAPTCHA_MATHEMATIC
* @see Securimage::SI_CAPTCHA_WORDS SI_CAPTCHA_WORDS
* @see Securimage::$charset charset property
* @see Securimage::$wordlist_file wordlist_file property
* @var int
*/
public $captcha_type = self::SI_CAPTCHA_STRING; // or self::SI_CAPTCHA_MATHEMATIC, or self::SI_CAPTCHA_WORDS;
/**
* The TTF font file to use to draw the captcha code.
*
* Leave blank for default font AHGBold.ttf
*
* @var string
*/
public $ttf_file;
/**
* The path to the wordlist file to use.
*
* Leave blank for default words/words.txt
*
* @var string
*/
public $wordlist_file;
/**
* Character encoding of the wordlist file.
* Requires PHP Multibyte String (mbstring) support.
* Allows word list to contain characters other than US-ASCII (requires compatible TTF font).
*
* @var string The character encoding (e.g. UTF-8, UTF-7, EUC-JP, GB2312)
* @see http://php.net/manual/en/mbstring.supported-encodings.php
* @since 3.6.3
*/
public $wordlist_file_encoding = null;
/**
* The directory to scan for background images, if set a random background
* will be chosen from this folder
*
* @var string
*/
public $background_directory;
/**
* The path to the audio files to be used for audio captchas.
*
* Can also be set in securimage_play.php
*
* Example:
*
* $img->audio_path = '/home/yoursite/public_html/securimage/audio/en/';
*
* @var string
*/
public $audio_path;
/**
* The path to the lame (mp3 encoder) binary on your system
* Static so that Securimage::getCaptchaHtml() has access to this value.
*
* @since 3.6
* @var string
*/
public static $lame_binary_path = '/usr/bin/lame';
/**
* The path to the directory containing audio files that will be selected
* randomly and mixed with the captcha audio.
*
* @var string
*/
public $audio_noise_path;
/**
* Whether or not to mix background noise files into captcha audio
*
* Mixing random background audio with noise can help improve security of
* audio captcha.
*
* Default: securimage/audio/noise
*
* @since 3.0.3
* @see Securimage::$audio_noise_path audio_noise_path property
* @var bool true = mix, false = no
*/
public $audio_use_noise;
/**
* The method and threshold (or gain factor) used to normalize the mixing
* with background noise.
*
* See http://www.voegler.eu/pub/audio/ for more information.
*
* Default: 0.6
*
* Valid:
* >= 1
* Normalize by multiplying by the threshold (boost - positive gain).
* A value of 1 in effect means no normalization (and results in clipping).
*
* <= -1
* Normalize by dividing by the the absolute value of threshold (attenuate - negative gain).
* A factor of 2 (-2) is about 6dB reduction in volume.
*
* [0, 1) (open inverval - not including 1)
* The threshold above which amplitudes are comressed logarithmically.
* e.g. 0.6 to leave amplitudes up to 60% "as is" and compressabove.
*
* (-1, 0) (open inverval - not including -1 and 0)
* The threshold above which amplitudes are comressed linearly.
* e.g. -0.6 to leave amplitudes up to 60% "as is" and compress above.
*
* @since 3.0.4
* @var float
*/
public $audio_mix_normalization = 0.8;
/**
* Whether or not to degrade audio by introducing random noise.
*
* Current research shows this may not increase the security of audible
* captchas.
*
* Default: true
*
* @since 3.0.3
* @var bool
*/
public $degrade_audio;
/**
* Minimum delay to insert between captcha audio letters in milliseconds
*
* @since 3.0.3
* @var float
*/
public $audio_gap_min = 0;
/**
* Maximum delay to insert between captcha audio letters in milliseconds
*
* @since 3.0.3
* @var float
*/
public $audio_gap_max = 3000;
/**
* The file path for logging errors from audio (default __DIR__)
*
* @var string|null
*/
public $log_path = null;
/**
* The name of the log file for logging audio errors
*
* @var string|null (defualt si_error.log)
*/
public $log_file = null;
/**
* Captcha ID if using static captcha
* @var string Unique captcha id
*/
protected static $_captchaId = null;
/**
* The GD image resource of the captcha image
*
* @var resource
*/
protected $im;
/**
* A temporary GD image resource of the captcha image for distortion
*
* @var resource
*/
protected $tmpimg;
/**
* The background image GD resource
* @var string
*/
protected $bgimg;
/**
* Scale factor for magnification of distorted captcha image
*
* @var int
*/
protected $iscale = 2;
/**
* Absolute path to securimage directory.
*
* This is calculated at runtime
*
* @var string
*/
public $securimage_path = null;
/**
* The captcha challenge value.
*
* Either the case-sensitive/insensitive word captcha, or the solution to
* the math captcha.
*
* @var string|bool Captcha challenge value
*/
protected $code;
/**
* The display value of the captcha to draw on the image
*
* Either the word captcha or the math equation to present to the user
*
* @var string Captcha display value to draw on the image
*/
protected $code_display;
/**
* Alternate text to draw as the captcha image text
*
* A value that can be passed to the constructor that can be used to
* generate a captcha image with a given value.
*
* This value does not get stored in the session or database and is only
* used when calling Securimage::show().
*
* If a display_value was passed to the constructor and the captcha image
* is generated, the display_value will be used as the string to draw on
* the captcha image.
*
* Used only if captcha codes are generated and managed by a 3rd party
* app/library
*
* @var string Captcha code value to display on the image
*/
public $display_value;
/**
* Captcha code supplied by user [set from Securimage::check()]
*
* @var string
*/
protected $captcha_code;
/**
* Time (in seconds) that the captcha was solved in (correctly or incorrectly).
*
* This is from the time of code creation, to when validation was attempted.
*
* @var int
*/
protected $_timeToSolve = 0;
/**
* Flag that can be specified telling securimage not to call exit after
* generating a captcha image or audio file
*
* @var bool If true, script will not terminate; if false script will terminate (default)
*/
protected $no_exit;
/**
* Flag indicating whether or not a PHP session should be started and used
*
* @var bool If true, no session will be started; if false, session will be started and used to store data (default)
*/
protected $no_session;
/**
* List of storage adapters for persisting captcha data
*
* @var array Array of one or more storage adapters
*/
protected $storage_adapters = array();
/**
* Flag indicating whether or not HTTP headers will be sent when outputting
* captcha image/audio
*
* @var bool If true (default) headers will be sent, if false, no headers are sent
*/
protected $send_headers;
/**
* The GD color for the background color
*
* @var int
*/
protected $gdbgcolor;
/**
* The GD color for the text color
*
* @var int
*/
protected $gdtextcolor;
/**
* The GD color for the line color
*
* @var int
*/
protected $gdlinecolor;
/**
* The GD color for the signature text color
*
* @var int
*/
protected $gdsignaturecolor;
/**
* Create a new securimage object, pass options to set in the constructor.
*
* The object can then be used to display a captcha, play an audible captcha, or validate a submission.
*
* @param array $options Options to initialize the class. May be any class property.
*
* $options = array(
* 'text_color' => new Securimage_Color('#013020'),
* 'code_length' => 5,
* 'num_lines' => 5,
* 'noise_level' => 3,
* 'font_file' => Securimage::getPath() . '/custom.ttf'
* );
*
* $img = new Securimage($options);
*
*/
public function __construct($options = array())
{
$this->securimage_path = dirname(__FILE__);
if (!is_array($options)) {
trigger_error(
'$options passed to Securimage::__construct() must be an array. ' .
gettype($options) . ' given',
E_USER_WARNING
);
$options = array();
}
if (function_exists('mb_internal_encoding')) {
mb_internal_encoding('UTF-8');
}
// check for and load settings from custom config file
$config_file = null;
if (file_exists(dirname(__FILE__) . '/config.inc.php')) {
$config_file = dirname(__FILE__) . '/config.inc.php';
}
if (isset($options['config_file']) && file_exists($options['config_file'])) {
$config_file = $options['config_file'];
}
if ($config_file) {
$settings = include $config_file;
if (is_array($settings)) {
$options = array_merge($settings, $options);
}
}
if (!class_exists(\Securimage\CaptchaObject::class)) {
// not using Composer autoloader
require_once __DIR__ . '/CaptchaObject.php';
require_once __DIR__ . '/StorageAdapter/AdapterInterface.php';
}
if (is_array($options) && sizeof($options) > 0) {
foreach($options as $prop => $val) {
if ($prop == 'captchaId') {
Securimage::$_captchaId = $val;
} else if ($prop == 'storage_adapters') {
if (!is_array($val)) {
$val = array($val);
}
foreach($val as $adapter) {
$this->addStorageAdapter($adapter);
}
} else {
$this->$prop = $val;
}
}
}
$this->image_bg_color = $this->initColor($this->image_bg_color, '#ffffff');
$this->text_color = $this->initColor($this->text_color, '#616161');
$this->line_color = $this->initColor($this->line_color, '#616161');
$this->noise_color = $this->initColor($this->noise_color, '#616161');
$this->signature_color = $this->initColor($this->signature_color, '#616161');
if (is_null($this->ttf_file)) {
$this->ttf_file = $this->securimage_path . '/AHGBold.ttf';
}
$this->signature_font = $this->ttf_file;
if (is_null($this->wordlist_file)) {
$this->wordlist_file = $this->securimage_path . '/words/words.txt';
}
if (is_null($this->audio_path)) {
$this->audio_path = $this->securimage_path . '/audio/en/';
}
if (is_null($this->audio_noise_path)) {
$this->audio_noise_path = $this->securimage_path . '/audio/noise/';
}
if (is_null($this->audio_use_noise)) {
$this->audio_use_noise = true;
}
if (is_null($this->degrade_audio)) {
$this->degrade_audio = true;
}
if (is_null($this->code_length) || (int)$this->code_length < 1) {
$this->code_length = 6;
}
if (is_null($this->perturbation) || !is_numeric($this->perturbation)) {
$this->perturbation = 0.75;
}
if (is_null($this->no_exit)) {
$this->no_exit = false;
}
if (is_null($this->no_session)) {
$this->no_session = false;
}
if (is_null($this->send_headers)) {
$this->send_headers = true;
}
// Set up storage adapters if none defined
// PHP Session storage adapter
if (sizeof($this->storage_adapters) < 1 && $this->no_session != true) {
if (!class_exists('Securimage\StorageAdapter\Session')) {
require_once __DIR__ . '/StorageAdapter/Session.php';
}
$sessionOpts = array(
'session_name' => (isset($options['session_name']) ?
$options['session_name'] :
null),
);
$defaultAdapter = new \Securimage\StorageAdapter\Session($sessionOpts);
$this->addStorageAdapter($defaultAdapter);
}
// PDO database storage adapter
if (isset($options['use_database']) && $options['use_database']) {
if (!isset($options['database_driver']) ||
!in_array($options['database_driver'], array(
self::SI_DRIVER_MYSQL,
self::SI_DRIVER_PGSQL,
self::SI_DRIVER_SQLITE3)
)
) {
throw new \Exception(
"Invalid database driver supplied to Securimage. '" .
htmlspecialchars($options['database_driver']) . "' is not a valid driver"
);
}
if (!class_exists('Securimage\StorageAdapter\PDO')) {
require_once __DIR__ . '/StorageAdapter/PDO.php';
}
$dbOpts = array(
'database_driver' => $options['database_driver'],
'database_table' => $options['database_table'],
'skip_table_check' => @$options['skip_table_check'],
'expiry_time' => $this->expiry_time,
);
if ($options['database_driver'] == self::SI_DRIVER_SQLITE3) {
$dbOpts['database_file'] = (isset($options['database_file']) ?
$options['database_file'] :
$this->securimage_path . '/database/securimage.sq3');
} else {
$dbOpts['database_host'] = @$options['database_host'];
$dbOpts['database_port'] = !empty($options['database_port']) ? $options['database_port'] : null;
$dbOpts['database_name'] = @$options['database_name'];
$dbOpts['database_user'] = @$options['database_user'];
$dbOpts['database_pass'] = @$options['database_pass'];
}
$dbAdapter = new \Securimage\StorageAdapter\PDO($dbOpts);
$this->addStorageAdapter($dbAdapter);
}
if (isset($options['use_mysqli']) && $options['use_mysqli']) {
if (!class_exists('Securimage\StorageAdapter\Mysqli')) {
require_once __DIR__ . '/StorageAdapter/Mysqli.php';
}
$mysqliOpts = array(
'database_host' => $options['database_host'],
'database_port' => !empty($options['database_port']) ? $options['database_port'] : null,
'database_name' => $options['database_name'],
'database_user' => $options['database_user'],
'database_pass' => $options['database_pass'],
'expiry_time' => $this->expiry_time,
'database_table' => $options['database_table'],
'skip_table_check' => @$options['skip_table_check'],
'mysqli_conn' => @$options['mysqli_conn'],
);
$mysqliAdapter = new \Securimage\StorageAdapter\Mysqli($mysqliOpts);
$this->addStorageAdapter($mysqliAdapter);
}
if (isset($options['use_memcached']) && $options['use_memcached']) {
$mcopts = array(
'memcached_servers' => $options['memcached_servers'],
'persistent' => @$options['memcached_persistent'],
'expiration' => $this->expiry_time,
);
if (!class_exists('Securimage\StorageAdapter\Memcached')) {
require_once __DIR__ . '/StorageAdapter/Memcached.php';
}
$mcAdapter = new \Securimage\StorageAdapter\Memcached($mcopts);
$this->addStorageAdapter($mcAdapter);
}
if (isset($options['use_redis']) && $options['use_redis']) {
$redisOpts = array(
'redis_server' => $options['redis_server'],
'expiration' => $this->expiry_time,
'redis_dbindex' => @$options['redis_dbindex'],
);
if (!class_exists('Securimage\StorageAdapter\Redis')) {
require_once __DIR__ . '/StorageAdapter/Redis.php';
}
$redisAdapter = new \Securimage\StorageAdapter\Redis($redisOpts);
$this->addStorageAdapter($redisAdapter);
}
if (Securimage::$_captchaId) {
// populated by securimage_show.php and securimage_play.php
$this->getCode();
}
}
/**
* Return the absolute path to the Securimage directory.
*
* @return string The path to the securimage base directory
*/
public static function getPath()
{
return dirname(__FILE__);
}
public static function generateCaptchaId()
{
$data = sprintf('%s:%d-%s', $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT'], microtime(true));
$hash = sha1(uniqid($data, true));
return $hash;
}
/**
* Add a storage adapter for persisting captcha data
*
* @param Securimage\StorageAdapter\AdapterInterface $adapter The storage adapter to add
* @return bool true if adapter added, false otherwise
*/
public function addStorageAdapter($adapter)
{
if (! ($adapter instanceof Securimage\StorageAdapter\AdapterInterface) ) {
trigger_error(
"Adapter supplied to addStorageAdapter is not an instance " .
"of Securimage AdapterInterface, cannot add",
E_USER_WARNING
);
return false;
}
$this->storage_adapters[] = $adapter;
return true;
}
/**
* Generates a new challenge and serves a captcha image.
*
* Appropriate headers will be sent to the browser unless the *send_headers* option is false.
*
* @param string $background_image The absolute or relative path to the background image to use as the background of the captcha image.
*
* $img = new Securimage();
* $img->code_length = 6;
* $img->num_lines = 5;
* $img->noise_level = 5;
*
* $img->show(); // sends the image and appropriate headers to browser
* exit;
*/
public function show($background_image = '')
{
set_error_handler(array(&$this, 'errorHandler'));
if(!empty($background_image) && is_readable($background_image)) {
$this->bgimg = $background_image;
}
$this->doImage();
}
/**