-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.php
333 lines (232 loc) · 7.09 KB
/
app.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
<?php
// Initialize Session
//$_E107['no_buffer'] = true;
$_E107['no_online'] = true;
$_E107['no_forceuserupdate'] = true;
$_E107['no_menus'] = true;
$_E107['no_maintenance'] = true;
require_once("../../class2.php");
class visualcaptch_front
{
private $session;
function __construct()
{
if(!defined('e107_INIT'))
{
return null;
}
while (@ob_end_clean()); // clean out any chars or space
if(!empty($_GET['namespace']))
{
$this->session = e107::getSession('visualcaptcha_' . $_GET['namespace']) ;
}
else
{
$this->session = e107::getSession('visualcaptcha');
}
// header('Access-Control-Allow-Origin: *');
if(strpos(e_REQUEST_URI,'/start/'))
{
$this->sendJSON();
}
if(strpos(e_REQUEST_URI,'/image/'))
{
$this->sendImage();
}
if(strpos(e_REQUEST_URI,'/audio'))
{
$this->sendAudio();
}
if(strpos(e_REQUEST_URI,'/test') && e_DEBUG === true)
{
$this->sendTest();
}
exit;
}
private function sendTest()
{
$captcha = new \visualCaptcha\Captcha($this->session, __DIR__.'/languages/English'); //todo replace with e_LANGUAGE
$captcha->generate(5);
//print_a($captcha);
$opts = $captcha->getImageOptions();
$data = $captcha->getImageOptionAtIndex(3);
print_a($opts);
echo "Index at 3: ".print_a($data,true);
}
function sendJSON()
{
$this->session->clear();
$vpref = e107::pref('visualcaptcha');
$captcha = new \visualCaptcha\Captcha($this->session, __DIR__.'/languages/English'); //todo replace with e_LANGUAGE
$amt = vartrue($vpref['imgCount'],5);
$captcha->generate($amt);
header( 'Content-Type: application/json' );
$text = json_encode($captcha->getFrontendData());
header( 'Content-Length: ' . strlen($text) );
echo $text;
// print_r($this->session);
}
function sendImage()
{
$captcha = new \visualCaptcha\Captcha($this->session, __DIR__); // e107_plugins/visualcaptcha/images
list($index,$tmp) = explode("?", str_replace(e_SELF, '', e_REQUEST_URL));
$index = str_replace('/image/','',$index);
// $index = intval($index);
$data = $captcha->getImageOptionAtIndex($index);
$image = __DIR__.DIRECTORY_SEPARATOR."images".DIRECTORY_SEPARATOR.$data['path'];
if(empty($data['path']))
{
echo "Not found";
return;
}
header( 'Pragma: public' );
// header( 'Expires: 0' );
header( 'Cache-Control: no-cache, no-store, must-revalidate' , true);
header( 'Cache-Control: private', true );
header( 'Content-Type: image/png' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . filesize($image) );
readfile( $image );
}
function sendAudio()
{
$captcha = new \visualCaptcha\Captcha($this->session, __DIR__.'/languages/English'); //todo calculate current e107 language.
$data = $captcha->getAudioOption();
$sep = DIRECTORY_SEPARATOR;
$file = $data['path'];
$audioFile = __DIR__.$sep."languages".$sep.e_LANGUAGE.$sep."audios".$sep.$file;
$mime = $this->getMimeType($audioFile);
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: no-cache, no-store, must-revalidate' );
header( 'Cache-Control: private', false );
header( 'Content-Type: '.$mime );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . filesize($audioFile) );
readfile($audioFile);
}
private function getMimeType($filePath)
{
if(function_exists('mime_content_type'))
{
return mime_content_type($filePath);
}
else
{
// Some PHP 5.3 builds don't have mime_content_type because it's deprecated
if(function_exists('finfo_open'))
{// Use finfo (right way)
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if($mimetype = finfo_file($finfo, $filePath))
{
finfo_close($finfo);
return $mimetype;
}
}
elseif(function_exists('pathinfo'))
{// Use pathinfo
if($pathinfo = pathinfo($filePath))
{
$imagetypes = array('gif', 'jpg', 'png');
if(in_array($pathinfo['extension'], $imagetypes) && getimagesize($filePath))
{
$size = getimagesize($filePath);
return $size['mime'];
}
}
}
// Just figure out from a set of possibilities, if we didn't figure it out before
$fileProperties = explode('.', $filePath);
$extension = end($fileProperties);
switch($extension)
{
case 'png':
return 'image/png';
case 'gif':
return 'image/gif';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'mp3':
return 'audio/mpeg3';
case 'ogg':
return 'audio/ogg';
default:
return 'application/octet-stream';
}
}
}
}
/**
* todo This entire class can be removed and class2.php remain - if/when the audio functions at line 323.
*/
new visualcaptch_front();
/*
if(!defined('e107_INIT'))
{
session_cache_limiter(false);
if(session_id() == '')
{
session_start();
}
}
@include(__DIR__ . '/vendor/autoload.php');
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
while (@ob_end_clean()); //XXX clean out any chars or space - this fixes many issues.
// Setup CORS
$app->response['Access-Control-Allow-Origin'] = '*';
// Inject Session object into app
if($namespace = $app->request->params('namespace'))
{
$app->session = defined("e107_INIT") ? e107::getSession('visualcaptcha_' . $namespace) : new \visualCaptcha\Session('visualcaptcha_' . $namespace);
// $app->session = e107::getSession('visualcaptcha_' . $namespace); //
}
else
{
$app->session = defined("e107_INIT") ? e107::getSession('visualcaptcha') : new \visualCaptcha\Session();
// $app->session = e107::getSession('visualcaptcha'); //;
}
// Populates captcha data into session object
// -----------------------------------------------------------------------------
// @param howmany is required, the number of images to generate
$app->get('/start/:howmany', function ($howMany) use ($app)
{
$captcha = new \visualCaptcha\Captcha($app->session, __DIR__.'/languages/English'); //todo calculate current e107 language.
$captcha->generate($howMany);
$app->response['Content-Type'] = 'application/json';
echo json_encode($captcha->getFrontendData());
});
// Streams captcha images from disk
// -----------------------------------------------------------------------------
// @param index is required, the index of the image you wish to get
$app->get('/image/:index', function ($index) use ($app)
{
$captcha = new \visualCaptcha\Captcha($app->session, __DIR__); // e107_plugins/visualcaptcha/images
if(defined("e107_INIT"))
{
list($index,$tmp) = explode("?", $index); // Fix for class2.php inclusion
}
$index = intval($index);
if(!$captcha->streamImage(
$app->response,
$index,
$app->request->params('retina')
))
{
$app->pass();
}
});
// Streams captcha audio from disk
// -----------------------------------------------------------------------------
// @param type is optional and defaults to 'mp3', but can also be 'ogg'
$app->get('/audio(/:type)', function ($type = 'mp3') use ($app) //todo this is failing
{
$captcha = new \visualCaptcha\Captcha($app->session, __DIR__.'/languages/English'); //todo replace 'English' with e_LANGUAGE once working
if(!$captcha->streamAudio($app->response, $type))
{
$app->pass();
}
});
$app->run();
*/