config = Management::get_config('Antigate'); if (!function_exists('getimagesize')) { die('Not installed PHP extension: GD'); } $this->http = new Http(); } /** * Отправить каптчу на распознание * * @access public * @param string * @param string * @param string * @param string * @param string * @param string * @param string * @return mixed */ public function recognition($file, $is_phrase = 0, $is_regsense = 0, $is_numeric = 0, $min_len = 0, $max_len = 0, $is_russian = 0) { if (!$socket = $this->http->open($this->config['host'])) { return FALSE; } if (is_file($file) OR strpos($file, '://') !== FALSE) { if (!$file_data = @file_get_contents($file) OR !$file_info = @getimagesize($file) OR !in_array($file_info['mime'], $this->mime_allowed)) { return FALSE; } $mime = $file_info['mime']; $file_name = basename($file); } else { if (substr($file, 1, 3) == 'PNG') { $mime = 'image/png'; $ext = 'png'; } else if (substr($file, 0, 3) == 'GIF') { $mime = 'image/gif'; $ext = 'gif'; } else if (substr($file, 6, 4) == 'JFIF') { $mime = 'image/jpeg'; $ext = 'jpeg'; } else { return FALSE; } $file_data = $file; $file_name = 'capcha.'.$ext; } $boundary = '-----------------------------'.substr(md5(rand(0, 32000)), 0, 10); $body = '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="method"'."\r\n\r\n".'post'."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="key"'."\r\n\r\n".$this->config['apikey']."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="phrase"'."\r\n\r\n".$is_phrase."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="regsense"'."\r\n\r\n".$is_regsense."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="numeric"'."\r\n\r\n".$is_numeric."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="min_len"'."\r\n\r\n".$min_len."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="max_len"'."\r\n\r\n".$max_len."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="is_russian"'."\r\n\r\n".$is_russian."\r\n"; $body .= '--'.$boundary."\r\n"; $body .= 'Content-Disposition: form-data; name="file"; filename="'.$file_name.'"'."\r\n"; $body .= 'Content-Type: '.$mime."\r\n\r\n"; $body .= $file_data."\r\n"; $body .= '--'.$boundary.'--'."\r\n"; $headers = array ( 'Host' => $this->config['host'], 'User-Agent' => 'HSDN antigate client/1', 'Content-Type' => 'multipart/form-data; boundary='.$boundary, 'Content-Length' => strlen($body), 'Connection' => 'close' ); $request = $this->http->generate_request('POST', '/in.php', $headers, $body); $this->http->write($socket, $request); $content = $this->http->get_content($socket); $this->http->close($socket); if ($this->http->validate_content($content, 200)) { if (strpos($content['body'], 'ERROR') !== FALSE OR strpos($content['body'], '') !== FALSE) { return FALSE; } list(, $captcha_id) = explode('|', $content['body']); $waittime = 0; sleep($this->config['read_timeout']); while(TRUE) { $captcha_info = $this->action('get', array('id' => $captcha_id)); if ($captcha_info === FALSE OR strpos($captcha_info, 'ERROR') !== FALSE) { return FALSE; } if ($captcha_info == 'CAPCHA_NOT_READY') { $waittime += $this->config['read_timeout']; if ($waittime > $this->config['recognition_timeout']) { return FALSE; } sleep($this->config['read_timeout']); } else { list($status, $code) = explode('|', $captcha_info); if (trim($status) == 'OK') { return trim($code); } } } } return FALSE; } /** * Отправить запрос action к сервису (получение баланса, статуса каптчи и т.д.) * * @access public * @param string * @param array * @return mixed */ public function action($value, $params = array()) { if (!$socket = $this->http->open($this->config['host'])) { return FALSE; } $params['key'] = $this->config['apikey']; $params['action'] = $value; $headers = array ( 'Host' => $this->config['host'], 'User-Agent' => 'HSDN antigate client/1', 'Connection' => 'close' ); $request = $this->http->generate_request('GET', '/res.php?'.http_build_query($params), $headers); $this->http->write($socket, $request); $content = $this->http->get_content($socket); $this->http->close($socket); if ($this->http->validate_content($content, 200)) { return $content['body']; } return FALSE; } } /* End of file */