-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeathbycaptcha.php
More file actions
921 lines (818 loc) · 26.5 KB
/
deathbycaptcha.php
File metadata and controls
921 lines (818 loc) · 26.5 KB
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
<?php
/**
* @package DBCAPI
*/
/**
* Base class for DBC related exceptions.
*
* @package DBCAPI
* @subpackage PHP
*/
abstract class DeathByCaptcha_Exception extends Exception
{}
/**
* Exception to throw on environment or runtime related failures.
*
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_RuntimeException extends DeathByCaptcha_Exception
{}
/**
* Exception to throw on network or disk IO failures.
*
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_IOException extends DeathByCaptcha_Exception
{}
/**
* Generic exception to throw on API client errors.
*
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_ClientException extends DeathByCaptcha_Exception
{}
/**
* Exception to throw on rejected login attemts due to invalid DBC credentials, low balance, or when account being banned.
*
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_AccessDeniedException extends DeathByCaptcha_ClientException
{}
/**
* Exception to throw on invalid CAPTCHA image payload: on empty images, on images too big, on non-image payloads.
*
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_InvalidCaptchaException extends DeathByCaptcha_ClientException
{}
/**
* Generic exception to throw on API server errors.
*
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_ServerException extends DeathByCaptcha_Exception
{}
/**
* Exception to throw when service is overloaded.
*
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_ServiceOverloadException extends DeathByCaptcha_ServerException
{}
/**
* Base Death by Captcha API client
*
* @property-read array|null $user User's details
* @property-read float|null $balance User's balance (in US cents)
*
* @package DBCAPI
* @subpackage PHP
*/
abstract class DeathByCaptcha_Client
{
const API_VERSION = 'DBC/PHP v4.5';
const DEFAULT_TIMEOUT = 60;
const DEFAULT_TOKEN_TIMEOUT = 120;
const POLLS_INTERVAL = array(1, 1, 2, 3, 2, 2, 3, 2, 2);
const DFLT_POLL_INTERVAL = 3;
/**
* DBC account credentials
*
* @var array
*/
protected $_userpwd = array();
/**
* Verbosity flag.
* When it's set to true, the client will produce debug output on every API call.
*
* @var bool
*/
public $is_verbose = false;
/**
* Parses URL query encoded responses
*
* @param string $s
* @return array
*/
static public function parse_plain_response($s)
{
parse_str($s, $a);
return $a;
}
/**
* Parses JSON encoded response
*
* @param string $s
* @return array
*/
static public function parse_json_response($s)
{
return json_decode(rtrim($s), true);
}
/**
* Checks if CAPTCHA is valid (not empty)
*
* @param string $img Raw CAPTCHA image
* @throws DeathByCaptcha_InvalidCaptchaException On invalid CAPTCHA images
*/
protected function _is_valid_captcha($img)
{
if (0 == strlen($img)) {
throw new DeathByCaptcha_InvalidCaptchaException(
'CAPTCHA image file is empty'
);
} else {
return true;
}
}
protected function _load_captcha($captcha)
{
if (is_resource($captcha)) {
$img = '';
rewind($captcha);
while ($s = fread($captcha, 8192)) {
$img .= $s;
}
return $img;
} else if (is_array($captcha)) {
return implode('', array_map('chr', $captcha));
} else if ('base64:' == substr($captcha, 0, 7)) {
return base64_decode(substr($captcha, 7));
} else {
return file_get_contents($captcha);
}
}
/**
* Closes opened connection (if any), as gracefully as possible
*
* @return DeathByCaptcha_Client
*/
abstract public function close();
/**
* Returns user details
*
* @return array|null
*/
abstract public function get_user();
/**
* Returns user's balance (in US cents)
*
* @uses DeathByCaptcha_Client::get_user()
* @return float|null
*/
public function get_balance()
{
return ($user = $this->get_user()) ? $user['balance'] : null;
}
/**
* Returns CAPTCHA details
*
* @param int $cid CAPTCHA ID
* @return array|null
*/
abstract public function get_captcha($cid);
/**
* Returns CAPTCHA text
*
* @uses DeathByCaptcha_Client::get_captcha()
* @param int $cid CAPTCHA ID
* @return string|null
*/
public function get_text($cid)
{
return ($captcha = $this->get_captcha($cid)) ? $captcha['text'] : null;
}
/**
* Reports an incorrectly solved CAPTCHA
*
* @param int $cid CAPTCHA ID
* @return bool
*/
abstract public function report($cid);
/**
* Uploads a CAPTCHA
*
* @param string|array|resource $captcha CAPTCHA image file name, vector of bytes, or file handle
* @return array|null Uploaded CAPTCHA details on success
* @throws DeathByCaptcha_InvalidCaptchaException On invalid CAPTCHA file
*/
abstract public function upload($captcha);
/**
* Tries to solve CAPTCHA by uploading it and polling for its status/text
* with arbitrary timeout. See {@link DeathByCaptcha_Client::upload()} for
* $captcha param details.
*
* @uses DeathByCaptcha_Client::upload()
* @uses DeathByCaptcha_Client::get_captcha()
* @param int $timeout Optional solving timeout (in seconds)
* @return array|null CAPTCHA details hash on success
*/
public function decode($captcha=null, $extra=[], $timeout=null)
{
if (!$extra || !is_array($extra)){
$extra = [];
}
if (is_null($timeout)){
if (is_null($captcha)){
$timeout = self::DEFAULT_TOKEN_TIMEOUT;
}
else {
$timeout = self::DEFAULT_TIMEOUT;
}
}
$deadline = time() + (0 < $timeout ? $timeout : self::DEFAULT_TIMEOUT);
if ($c = $this->upload(
$captcha,
$extra=$extra)
) {
$intvl_idx = 0; // POLLS_INTERVAL index
while ($deadline > time() && $c && !$c['text']) {
list($intvl, $intvl_idx) = $this->_get_poll_interval($intvl_idx);
sleep($intvl);
$c = $this->get_captcha($c['captcha']);
}
if ($c && $c['text'] && $c['is_correct']) {
return $c;
}
}
return null;
}
/**
* @param string $username DBC account username
* @param string $password DBC account password
* @throws DeathByCaptcha_RuntimeException On missing/empty DBC account credentials
* @throws DeathByCaptcha_RuntimeException When required extensions/functions not found
*/
public function __construct($username, $password)
{
foreach (array('username', 'password') as $k) {
if (!$$k) {
throw new DeathByCaptcha_RuntimeException(
"Account {$k} is missing or empty"
);
}
}
$this->_userpwd = array($username, $password);
}
/**
* @ignore
*/
public function __destruct()
{
$this->close();
}
/**
* @ignore
*/
public function __get($key)
{
switch ($key) {
case 'user':
return $this->get_user();
case 'balance':
return $this->get_balance();
}
}
/**
* @param int $idx index of POLLS_INTERVAL to be accessed
* @return array with interval and index
*/
protected function _get_poll_interval($idx)
{
if (count(self::POLLS_INTERVAL) > $idx) {
$intvl = self::POLLS_INTERVAL[$idx];
}
else {
$intvl = self::DFLT_POLL_INTERVAL;
}
$idx++;
return array($intvl, $idx);
}
}
/**
* Death by Captcha HTTP API Client
*
* @see DeathByCaptcha_Client
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_HttpClient extends DeathByCaptcha_Client
{
const BASE_URL = 'https://api.dbcapi.me/api';
protected $_conn = null;
protected $_response_type = '';
protected $_response_parser = null;
/**
* Sets up CURL connection
*/
protected function _connect()
{
if (!is_resource($this->_conn)) {
if ($this->is_verbose) {
fputs(STDERR, time() . " CONN\n");
}
if (!($this->_conn = curl_init())) {
throw new DeathByCaptcha_RuntimeException(
'Failed initializing a CURL connection'
);
}
curl_setopt_array($this->_conn, array(
CURLOPT_TIMEOUT => self::DEFAULT_TIMEOUT,
CURLOPT_CONNECTTIMEOUT => (int)(self::DEFAULT_TIMEOUT / 4),
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => false,
CURLOPT_HTTPHEADER => array(
'Accept: ' . $this->_response_type,
'Expect: ',
'User-Agent: ' . self::API_VERSION
)
));
if ((version_compare(PHP_VERSION, '5.5') == 0)) {
curl_setopt($this->_conn, CURLOPT_SAFE_UPLOAD, true);
}
}
return $this;
}
/**
* Makes an API call
*
* @param string $cmd API command
* @param array $payload API call payload, essentially HTTP POST fields
* @return array|null API response hash table on success
* @throws DeathByCaptcha_IOException On network related errors
* @throws DeathByCaptcha_AccessDeniedException On failed login attempt
* @throws DeathByCaptcha_InvalidCaptchaException On invalid CAPTCHAs rejected by the service
* @throws DeathByCaptcha_ServerException On API server errors
*/
protected function _call($cmd, $payload=null)
{
if (null !== $payload) {
if ($this->_userpwd[0] == "authtoken"){
if ($this->is_verbose) {
fputs(STDERR, time() . ' _call get authtoken into payload ' . "\n");
}
$payload = array_merge($payload, array(
'authtoken' => $this->_userpwd[1],
));
} else {
$payload = array_merge($payload, array(
'username' => $this->_userpwd[0],
'password' => $this->_userpwd[1],
));
}
}
$this->_connect();
$opts = array(CURLOPT_URL => self::BASE_URL . '/' . trim($cmd, '/'),
CURLOPT_REFERER => '');
if (null !== $payload) {
$opts[CURLOPT_POST] = true;
$opts[CURLOPT_POSTFIELDS] = array_key_exists('captchafile', $payload)
? $payload
: http_build_query($payload);
} else {
$opts[CURLOPT_HTTPGET] = true;
}
curl_setopt_array($this->_conn, $opts);
if ($this->is_verbose) {
fputs(STDERR, time() . " SEND-: {$cmd} " . serialize($payload) . "\n");
}
$response = curl_exec($this->_conn);
if (0 < ($err = curl_errno($this->_conn))) {
throw new DeathByCaptcha_IOException(
"API connection failed: [{$err}] " . curl_error($this->_conn)
);
}
if ($this->is_verbose) {
fputs(STDERR, time() . " RECV: {$response}\n");
}
$status_code = curl_getinfo($this->_conn, CURLINFO_HTTP_CODE);
if (403 == $status_code) {
throw new DeathByCaptcha_AccessDeniedException(
'Access denied, check your credentials and/or balance'
);
} else if (400 == $status_code || 413 == $status_code) {
throw new DeathByCaptcha_InvalidCaptchaException(
"CAPTCHA was rejected by the service, check if it's a valid image"
);
} else if (503 == $status_code) {
throw new DeathByCaptcha_ServiceOverloadException(
"CAPTCHA was rejected due to service overload, try again later"
);
} else if (!($response = call_user_func($this->_response_parser, $response))) {
throw new DeathByCaptcha_ServerException(
'Invalid API response'
);
} else {
return $response;
}
}
/**
* @see DeathByCaptcha_Client::__construct()
*/
public function __construct($username, $password)
{
if (!extension_loaded('curl')) {
throw new DeathByCaptcha_RuntimeException(
'CURL extension not found'
);
}
if (function_exists('json_decode')) {
$this->_response_type = 'application/json';
$this->_response_parser = array($this, 'parse_json_response');
} else {
$this->_response_type = 'text/plain';
$this->_response_parser = array($this, 'parse_plain_response');
}
parent::__construct($username, $password);
}
/**
* @see DeathByCaptcha_Client::close()
*/
public function close()
{
if (is_resource($this->_conn)) {
if ($this->is_verbose) {
fputs(STDERR, time() . " CLOSE\n");
}
curl_close($this->_conn);
$this->_conn = null;
}
return $this;
}
/**
* @see DeathByCaptcha_Client::get_user()
*/
public function get_user()
{
$user = $this->_call('user', array());
return (0 < ($id = (int)@$user['user']))
? array('user' => $id,
'balance' => (float)@$user['balance'],
'is_banned' => (bool)@$user['is_banned'])
: null;
}
/**
* @see DeathByCaptcha_Client::upload()
* @throws DeathByCaptcha_RuntimeException When failed to save CAPTCHA image to a temporary file
*/
public function upload($captcha=null, $extra=[])
{
if(null !== $captcha){
$img = $this->_load_captcha($captcha);
if($extra['banner']){
$banner = $this->_load_captcha($extra['banner']);
if ($this->_is_valid_captcha($banner)) {
$tmp_bn = tempnam(null, 'banner');
file_put_contents($tmp_bn, $banner);
$extra['banner'] = '@'.$tmp_bn;
}else{
$extra['banner'] = '';
}
}
if ($this->_is_valid_captcha($img)) {
$tmp_fn = tempnam(null, 'captcha');
file_put_contents($tmp_fn, $img);
try {
$captchafile = null;
if (version_compare(PHP_VERSION, '5.5') <= 0) {
$captchafile = '@'. $tmp_fn;
} else {
$captchafile = new CURLFile($tmp_fn);
}
$captcha = $this->_call('captcha', array_merge(
['captchafile' => $captchafile],
$extra
));
} catch (Exception $e) {
@unlink($tmp_fn);
throw $e;
}
@unlink($tmp_fn);
}
}else{
$captcha = $this->_call('captcha', $extra);
}
if(null !== $captcha){
if (0 < ($cid = (int)@$captcha['captcha'])) {
return array(
'captcha' => $cid,
'text' => (!empty($captcha['text']) ? $captcha['text'] : null),
'is_correct' => (bool)@$captcha['is_correct'],
);
}
}
return null;
}
/**
* @see DeathByCaptcha_Client::get_captcha()
*/
public function get_captcha($cid)
{
$captcha = $this->_call('captcha/' . (int)$cid);
return (0 < ($cid = (int)@$captcha['captcha']))
? array('captcha' => $cid,
'text' => (!empty($captcha['text']) ? $captcha['text'] : null),
'is_correct' => (bool)$captcha['is_correct'])
: null;
}
/**
* @see DeathByCaptcha_Client::report()
*/
public function report($cid)
{
$captcha = $this->_call('captcha/' . (int)$cid . '/report', array());
return !(bool)@$captcha['is_correct'];
}
}
/**
* Death by Captcha socket API Client
*
* @see DeathByCaptcha_Client
* @package DBCAPI
* @subpackage PHP
*/
class DeathByCaptcha_SocketClient extends DeathByCaptcha_Client
{
const HOST = 'api.dbcapi.me';
const FIRST_PORT = 8123;
const LAST_PORT = 8130;
const TERMINATOR = "\r\n";
protected $_sock = null;
/**
* Opens a socket connection to the API server
*
* @throws DeathByCaptcha_IOException When API connection fails
* @throws DeathByCaptcha_RuntimeException When socket operations fail
*/
protected function _connect()
{
if (null === $this->_sock) {
if ($this->is_verbose) {
fputs(STDERR, time() . " CONN\n");
}
$errno = 0;
$error = '';
$port = rand(self::FIRST_PORT, self::LAST_PORT);
$sock = null;
if (!($sock = @fsockopen(self::HOST, $port, $errno, $error, self::DEFAULT_TIMEOUT))) {
throw new DeathByCaptcha_IOException(
'Failed connecting to ' . self::HOST . ":{$port}: fsockopen(): [{$errno}] {$error}"
);
} else if (!@stream_set_timeout($sock, self::DEFAULT_TIMEOUT / 4)) {
fclose($sock);
throw new DeathByCaptcha_IOException(
'Failed setting socket timeout'
);
} else {
$this->_sock = $sock;
}
}
return $this;
}
/**
* Socket send()/recv() wrapper
*
* @param string $buf Raw API request to send
* @return string Raw API response on success
* @throws DeathByCaptcha_IOException On network failures
*/
protected function _sendrecv($buf)
{
if ($this->is_verbose) {
fputs(STDERR, time() . ' SEND: ' . strlen($buf) . ' ' . rtrim($buf) . "\n");
}
$buf .= self::TERMINATOR;
$response = '';
while (true) {
if ($buf) {
if (!($n = fwrite($this->_sock, $buf))) {
throw new DeathByCaptcha_IOException(
'Connection lost while sending API request'
);
} else {
$buf = substr($buf, $n);
}
}
if (!$buf) {
if (!($s = fread($this->_sock, 4096))) {
throw new DeathByCaptcha_IOException(
'Connection lost while receiving API response'
);
} else {
$response .= $s;
if (self::TERMINATOR == substr($s, strlen($s) - 2)) {
$response = rtrim($response, self::TERMINATOR);
if ($this->is_verbose) {
fputs(STDERR, time() . ' RECV: ' . strlen($response) . " {$response}\n");
}
return $response;
}
}
}
}
throw new DeathByCaptcha_IOException('API request timed out');
}
/**
* Makes an API call
*
* @param string $cmd API command to call
* @param array $payload API request payload
* @return array|null API response hash map on success
* @throws DeathByCaptcha_IOException On network errors
* @throws DeathByCaptcha_AccessDeniedException On failed login attempt
* @throws DeathByCaptcha_InvalidCaptchaException On invalid CAPTCHAs rejected by the service
* @throws DeathByCaptcha_ServerException On API server errors
*/
protected function _call($cmd, $payload=null)
{
if (null === $payload) {
$payload = array();
}
$payload = array_merge($payload, array(
'cmd' => $cmd,
'version' => self::API_VERSION,
));
$payload = json_encode($payload);
$response = null;
for ($attempt = 2; 0 < $attempt && null === $response; $attempt--) {
if (null === $this->_sock && 'login' != $cmd) {
if ($this->_userpwd[0] == "authtoken"){
if ($this->is_verbose) {
fputs(STDERR, time() . ' _call using authtoken ' . "\n");
}
$this->_call('login', array(
'authtoken' => $this->_userpwd[1],
));
} else {
$this->_call('login', array(
'username' => $this->_userpwd[0],
'password' => $this->_userpwd[1],
));
}
}
$this->_connect();
try {
$response = $this->_sendrecv($payload);
} catch (DeathByCaptcha_Exception $e) {
$this->close();
}
}
try {
if (null === $response) {
throw new DeathByCaptcha_IOException(
'API connection lost or timed out'
);
} else if (!($response = $this->parse_json_response($response))) {
throw new DeathByCaptcha_ServerException(
'Invalid API response'
);
}
if (!empty($response['error'])) {
switch ($response['error']) {
case 'not-logged-in':
throw new DeathByCaptcha_AccessDeniedException(
'Access denied, check your credentials'
);
case 'banned':
throw new DeathByCaptcha_AccessDeniedException(
'Access denied, account suspended'
);
case 'insufficient-funds':
throw new DeathByCaptcha_AccessDeniedException(
'Access denied, balance is too low'
);
case 'invalid-captcha':
throw new DeathByCaptcha_InvalidCaptchaException(
"CAPTCHA was rejected by the service, check if it's a valid image"
);
case 'service-overload':
throw new DeathByCaptcha_ServiceOverloadException(
'CAPTCHA was rejected due to service overload, try again later'
);
default:
throw new DeathByCaptcha_ServerException(
'API server error occured: ' . $response['error']
);
}
} else {
return $response;
}
} catch (Exception $e) {
$this->close();
throw $e;
}
}
/**
* @see DeathByCaptcha_Client::__construct()
*/
public function __construct($username, $password)
{
// PHP for Windows lacks EAGAIN errno constant
if (!defined('SOCKET_EAGAIN')) {
define('SOCKET_EAGAIN', 11);
}
foreach (array('json', ) as $k) {
if (!extension_loaded($k)) {
throw new DeathByCaptcha_RuntimeException(
"Required {$k} extension not found, check your PHP configuration"
);
}
}
foreach (array('json_encode', 'json_decode', 'base64_encode') as $k) {
if (!function_exists($k)) {
throw new DeathByCaptcha_RuntimeException(
"Required {$k}() function not found, check your PHP configuration"
);
}
}
parent::__construct($username, $password);
}
/**
* @see DeathByCaptcha_Client::close()
*/
public function close()
{
if (null !== $this->_sock) {
if ($this->is_verbose) {
fputs(STDERR, time() . " CLOSE\n");
}
fclose($this->_sock);
$this->_sock = null;
}
return $this;
}
/**
* @see DeathByCaptcha_Client::get_user()
*/
public function get_user()
{
$user = $this->_call('user');
return (0 < ($id = (int)@$user['user']))
? array('user' => $id,
'balance' => (float)@$user['balance'],
'is_banned' => (bool)@$user['is_banned'])
: null;
}
/**
* @see DeathByCaptcha_Client::get_user()
*/
public function upload($captcha=null, $extra=[])
{
if(null!==$captcha){
$img = $this->_load_captcha($captcha);
if ($this->_is_valid_captcha($img)) {
if ($extra['banner']){
$extra['banner'] = $this->_load_captcha($extra['banner']);
if ($this->_is_valid_captcha($extra['banner'])){
$extra['banner'] = base64_encode($extra['banner']);
}
}
$captcha = $this->_call('upload', array_merge(
['captcha' => base64_encode($img)],
$extra
));
}
}elseif(sizeof($extra)>0){
$captcha = $this->_call('upload', $extra);
}
if (null!== $captcha){
if (0 < ($cid = (int)@$captcha['captcha'])) {
return array(
'captcha' => $cid,
'text' => (!empty($captcha['text']) ? $captcha['text'] : null),
'is_correct' => (bool)@$captcha['is_correct'],
);
}
}
return null;
}
/**
* @see DeathByCaptcha_Client::get_captcha()
*/
public function get_captcha($cid)
{
$captcha = $this->_call('captcha', array('captcha' => (int)$cid));
return (0 < ($cid = (int)@$captcha['captcha']))
? array('captcha' => $cid,
'text' => (!empty($captcha['text']) ? $captcha['text'] : null),
'is_correct' => (bool)$captcha['is_correct'])
: null;
}
/**
* @see DeathByCaptcha_Client::report()
*/
public function report($cid)
{
$captcha = $this->_call('report', array('captcha' => (int)$cid));
return !@$captcha['is_correct'];
}
}