-
Notifications
You must be signed in to change notification settings - Fork 13
/
AnimatedCaptcha.php
161 lines (134 loc) · 3.98 KB
/
AnimatedCaptcha.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
<?php
/**
* @author Michael Kliewe
* @copyright 2011 Michael Kliewe
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpgangsta.de/
*/
class AnimatedCaptcha
{
protected $_options = array(
'millisecondsBetweenFrames' => 100,
'pluginName' => 'MovingRectangle',
'pluginOptions' => array()
);
/**
* @var string
*/
protected $_tempFilePath;
/**
* @var array
*/
protected $_frames;
public function __construct(array $options = array())
{
if (!class_exists( 'GIFEncoder') && !include('library/GIFEncoder3.0.php')) {
throw new Exception('GIFEncoder class not found');
}
if (is_array($options)) {
$this->setOptions($options);
}
}
/**
* All given options in the array are given to their setters
*
* @param array $options
* @return ImageLabeler
*/
public function setOptions(array $options)
{
foreach ($options as $key => $value) {
$normalized = ucfirst($key);
$method = 'set' . $normalized;
if (method_exists($this, $method)) {
$this->$method($value);
}
}
return $this;
}
public function setMillisecondsBetweenFrames($ms)
{
$this->_options['millisecondsBetweenFrames'] = $ms;
return $this;
}
public function setPluginName($pluginName)
{
$this->_options['pluginName'] = $pluginName;
return $this;
}
public function setPluginOptions($pluginOptions)
{
$this->_options['pluginOptions'] = $pluginOptions;
return $this;
}
public function render()
{
$this->_createTempFilePath();
$this->_getFramesFromPlugin();
$this->_writeFramesToTargetFile();
return $this;
}
/**
* Send header and output the image
*
* @return ImageLabeler
*/
public function outputRenderedImage()
{
header('Cache-Control: private, no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s',time()-60).' GMT');
header('Pragma: no-cache');
header('Content-Length: '.filesize($this->_tempFilePath));
header('Content-Type: image/gif');
readfile($this->_tempFilePath);
return $this;
}
/**
* Get the image as a string
*
* @return string
*/
public function getRenderedFileContent()
{
return file_get_contents($this->_tempFilePath);
}
/**
* Get the path to the file after render() has been called
*
* @return string
*/
public function getRenderedFilePath()
{
return $this->_tempFilePath;
}
// ====== private and protected methods =============
protected function _getFramesFromPlugin()
{
require __DIR__ . '/plugins/'.$this->_options['pluginName'].'.php';
$pluginClassName = 'AnimatedCaptcha_Plugin_'.$this->_options['pluginName'];
$pluginClass = new $pluginClassName($this->_options['pluginOptions']);
/** @var $pluginClass AnimatedCaptcha_Plugin */
$this->_frames = $pluginClass->getFrames();
}
/**
* Get a random file path in the system temp path
*/
protected function _createTempFilePath()
{
$tempFilePath = tempnam('', '') . '.gif';
$this->_tempFilePath = $tempFilePath;
}
/**
* write the frames to a file depending on the output format
*/
protected function _writeFramesToTargetFile()
{
$delays = array();
for ($i=0; $i<count($this->_frames); $i++) {
$delays[] = $this->_options['millisecondsBetweenFrames'];
}
$gif = new GIFEncoder($this->_frames, $delays, 0, 2, 0, 0, 0, 0, "bin" );
file_put_contents($this->_tempFilePath, $gif->GetAnimation());
}
}