login = $login; if($password) $this->pass = $password; if($host) $this->host = $host; if($port) $this->port = $port; } public function __destruct() { $this->stream_send(2); fclose( $this->handle ); } public function login() { if(!$this->stream_open()) { $this->debug = 'failed opening stream'; return -3; } // send login packet if(!$this->stream_send(1,$this->login)) { $this->debug = 'failed sending login'; return -3; } // receive hash if($this->stream_get()) { // check whether we got back a hash if($this->data['cmd'] != 3 || $this->data['size'] != $this->rand_size) { $this->debug = 'failed getting hash'; return -3; } } else { return -3; } $hash = NULL; $hash .= $this->data['bin']; $hash .= md5( $this->pass ); $hash .= $this->login; $hash = hash( 'sha256', $hash, TRUE ); // send hash packet if(!$this->stream_send(4,$hash)) { $this->debug = 'failed sending hash'; return -3; } // receive OK if($this->stream_get()) { if($this->data['cmd'] == 7) { // set success flag $this->logined = 1; return 0; } else { $this->debug = 'bad OK: '.$this->data['cmd']; return -3; } } else { $this->debug = 'failed getting OK'; return -3; } } // $pic is picture data, $timeout is 0,1,2,3,4 - default,long default, 30s,60s,90s public function send($pic, $timeout=0) { if(!$this->logined) return -2; $this->pic_timeout = $timeout; if($this->stream_send(12, $this->pic_pack($pic))) { return -3; } if($this->stream_get()) { switch( $this->data['cmd']) { case 14: $this->pic_unpack( $this->data['bin'] ); $this->answer = $this->pic_data['bin']; return 0; case 11: // picture timed out return -7; case 10: // balance depleted return -6; case 9: // server's busy return -5; case 8: // server's error return -1; default: // unknown error return -200; } } else { return -3; } } public function bad() { if(!$this->logined) return -2; $data = $this->pic_pack('',$this->pic_data['major_id'],$this->pic_data['minor_id']); if($this->stream_send(13,$data)) { return 0; } else { return -3; } } public function balance() { if(!$this->logined) return -2; if(!$this->stream_send(10)) return -3; if($this->stream_get()) { if($this->data['cmd'] = 10) { $this->balance = $this->data['bin']; return 0; } else { return -200; } } return -3; } protected function stream_open() { $errnum = 0; $errstr = ''; if(( $this->handle = fsockopen( $this->host, $this->port, $errnum, $errstr )) === FALSE ) { $this->debug = 'We have an fsockopen() error: ' . $errstr . ' (' . $errnum . ')'; return false; } else { return true; } } protected function stream_send($command, $data='') { $sz = $data?strlen($data):0; $packet = pack( 'CCV', $this->proto_version, $command, $sz ); return fwrite( $this->handle, $packet.$data, $this->packet_size + $sz); } protected function stream_get() { $this->data = null; if( ($bin = stream_get_contents( $this->handle, $this->packet_size )) === FALSE ) { return false; } $this->data = unpack('Cver/Ccmd/Vsize', $bin ); if( $this->data['size'] > 0) { if(($bin = stream_get_contents( $this->handle, $this->data['size'] )) === FALSE ) { return false; } else { $this->data['bin'] = $bin; } } return true; } protected function pic_pack($pic,$major_id=0, $minor_id=0 ) { return pack( 'VVVVV', $this->pic_timeout, $this->pic_type, strlen($pic), $major_id, $minor_id ) . $pic; } protected function pic_unpack($bin) { $arr = unpack( 'Vtimeout/Vtype/Vsize/Vmajor_id/Vminor_id', $bin ); $this->pic_data = $arr; $this->pic_data['bin'] = substr( $bin, $this->pic_packet_size ); } } /* Example $d = new Decaptcher(); $pic_path = './captcha.jpg'; if($d->send(file_get_contents($pic_path)) < 0) { die(); // error happened } // your answer is here $answer = $d->answer; //do something with it // ... //if it didn't work $d->bad(); //you can try again if($d->send(file_get_contents($pic_path)) < 0) { die(); // error happened } // your answer is here $answer = $d->answer; //do something with it // ... //if it didn't work $d->bad(); // Check your API balance if($d->balance() < 0) { die(); // error happened } echo $d->balance; // When you are done unset($d); */ ?>