Skip to content

Commit

Permalink
dodano obsługę kilku serwisów captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
luzik committed Feb 25, 2016
1 parent 2d156e8 commit 28c53c6
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 9 deletions.
34 changes: 25 additions & 9 deletions PlayMobile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class PlayMobile
const securityPage = 'https://bramka.play.pl/composer/j_security_check';
const samlLog = 'https://bramka.play.pl/composer/samlLog?action=sso';

public static $captchaRequired = FALSE;

public static $dbcEnabled = TRUE;
private static $captchaRequired = FALSE;
public static $captchaService = NULL; // Serwis captcha deathbycaptcha.com|rucaptcha.com|2captcha.com|pixodrom.com|captcha24.com|socialink.ru|anti-captcha.com
public static $captchaApiKey = NULL; // APIKey to captcha service
public static $dbcUser = NULL; // Login DeathByCaptcha
public static $dbcPass = NULL; // Hasło DeathByCaptcha

Expand Down Expand Up @@ -152,7 +152,7 @@ public static function sendSms($login, $password, $recipent, $message)

// Ustawiamy captchę
if(PlayMobile::$captchaRequired) {
if (PlayMobile::$dbcEnabled and PlayMobile::$dbcUser) {
if (PlayMobile::$captchaService and (PlayMobile::$dbcUser or PlayMobile::$captchaApiKey)) {
$formParams['inputCaptcha'] = PlayMobile::fixCaptcha(PlayMobile::$captchaRequired);
}
else {
Expand Down Expand Up @@ -191,16 +191,32 @@ public static function getFormRandomId()
*/
public static function fixCaptcha($captchaUrl)
{
require_once 'vendor/deathbycaptcha.php';
$client = new DeathByCaptcha_SocketClient(PlayMobile::$dbcUser, PlayMobile::$dbcPass);
$captchaFile = dirname(__FILE__) . '/captcha/' . uniqid('captcha') . '.png';

file_put_contents( $captchaFile, PlayMobile::curl($captchaUrl));

if ($captcha = $client->decode($captcha_filename, DeathByCaptcha_Client::DEFAULT_TIMEOUT))
if (PlayMobile::$captchaService == 'deathbycaptcha.com') {
require_once 'vendor/deathbycaptcha.php';
$client = new DeathByCaptcha_SocketClient(PlayMobile::$dbcUser, PlayMobile::$dbcPass);
$captcha = $client->decode($captchaFile, DeathByCaptcha_Client::DEFAULT_TIMEOUT);
$captcha = $captcha['text'];
}
else {
require_once 'vendor/Captcha.php';
$client = new Captcha();
$client->domain = PlayMobile::$captchaService;
$client->setApiKey(PlayMobile::$captchaApiKey);
if ($client->run($captchaFile)){
$captcha = $client->result();
}
else {
$captcha = NULL;
}
}
if ($captcha)
{
@unlink($captchaFile);
return $captcha['text'];
print "Captcha: $captcha\n";
return $captcha;
}
else
{
Expand Down
130 changes: 130 additions & 0 deletions vendor/Captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

/**
* Captcha Resolvers. Based on code from https://github.com/jumper423/yii2-captcha
*
* @author luzik
*/

class Captcha
{
public $domain = "rucaptcha.com";
public $pathTmp = 'captcha';
private $apiKey;
public $isVerbose = true;
public $requestTimeout = 5;
public $maxTimeout = 120;
public $isPhrase = 0;
public $isRegSense = 0;
public $isNumeric = 0;
public $minLen = 0;
public $maxLen = 0;
public $language = 0;
private $error = null;
private $result = null;

private $errors = [
'ERROR_NO_SLOT_AVAILABLE' => 'The current bid is higher than the maximum bid set for your account.',
'ERROR_ZERO_CAPTCHA_FILESIZE' => 'CAPTCHA size is less than 100 bites',
'ERROR_TOO_BIG_CAPTCHA_FILESIZE' => 'CAPTCHA size is more than 100 Kbites',
'ERROR_ZERO_BALANCE' => 'You don’t have money on your account',
'ERROR_IP_NOT_ALLOWED' => 'The request has sent from the IP that is not on the list of your IPs. Check the list of your IPs in the system.',
'ERROR_CAPTCHA_UNSOLVABLE' => 'Captcha could not solve three different employee. Funds for this captcha not',
'ERROR_BAD_DUPLICATES' => 'ERROR_BAD_DUPLICATES',
'ERROR_NO_SUCH_METHOD' => 'ERROR_NO_SUCH_METHOD',
'ERROR_IMAGE_TYPE_NOT_SUPPORTED' => 'The server cannot recognize the CAPTCHA file type',
'ERROR_KEY_DOES_NOT_EXIST' => 'The "key" do not exist',
'ERROR_WRONG_USER_KEY' => 'Wrong "key" parameter format, it should contain 32 symbols',
'ERROR_WRONG_ID_FORMAT' => 'ERROR_WRONG_ID_FORMAT',
'ERROR_WRONG_FILE_EXTENSION' => 'The CAPTCHA has a wrong extension. Possible extensions are: jpg,jpeg,gif,png',
];

public function setApiKey($apiKey)
{
if (is_callable($apiKey)){
$this->apiKey = $apiKey();
} else {
$this->apiKey = $apiKey;
}
}

public function run($filename)
{
$this->result = null;
$this->error = null;
try {
$postData = [
'method' => 'post',
'key' => $this->apiKey,
'file' => (version_compare(PHP_VERSION, '5.5.0') >= 0) ? new \CURLFile($filename): '@' . $filename,
'phrase' => $this->isPhrase,
'regsense' => $this->isRegSense,
'numeric' => $this->isNumeric,
'min_len' => $this->minLen,
'max_len' => $this->maxLen,
'language' => $this->language,
'soft_id' => 882,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://{$this->domain}/in.php");
if (version_compare(PHP_VERSION, '5.5.0') >= 0 && version_compare(PHP_VERSION, '7.0') < 0) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception("CURL returned error: " . curl_error($ch));
}
curl_close($ch);
$this->setError($result);
list(, $captcha_id) = explode("|", $result);
$waitTime = 0;
sleep($this->requestTimeout);
while (true) {
$result = file_get_contents("http://{$this->domain}/res.php?key={$this->apiKey}&action=get&id={$captcha_id}");
$this->setError($result);
if ($result == "CAPCHA_NOT_READY") {
$waitTime += $this->requestTimeout;
if ($waitTime > $this->maxTimeout) {
break;
}
sleep($this->requestTimeout);
} else {
$ex = explode('|', $result);
if (trim($ex[0]) == 'OK') {
$this->result = trim($ex[1]);
return true;
}
}
}
throw new Exception('Timeout');
} catch (Exception $e) {
$this->error = $e->getMessage();
return false;
}
}

public function result()
{
return $this->result;
}

public function error()
{
return $this->error;
}

private function setError($error)
{
if (strpos($error, 'ERROR') !== false) {
if (array_key_exists($error, $this->errors)) {
throw new Exception($this->errors[$error]);
} else {
throw new Exception($error);
}
}
}
}

0 comments on commit 28c53c6

Please sign in to comment.