Last active
December 15, 2015 10:49
-
-
Save vincent4vx/5248869 to your computer and use it in GitHub Desktop.
Une API pour RPG paradize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* API pour RPG paradize | |
* @author v4vx | |
*/ | |
class RpgApi{ | |
/** | |
* l'id RPG | |
* @var int | |
*/ | |
private $id; | |
/** | |
* Initialise l'api RPG paradise | |
* @param int $rpg_id l'id de la page RPG | |
*/ | |
public function __construct($rpg_id){ | |
$this->id=$rpg_id; | |
} | |
/** | |
* Capture le script du captcha de la page de vote | |
* /!\ Cette opération est très lourde, veuillez utiliser un système de cache, ou limiter | |
* le nombre d'affichage du captcha /!\ | |
* @return mixed retourne le script à afficher ou FALSE en cas d'erreur (404, ou autres) | |
*/ | |
public function getCaptcha(){ | |
$page_data = file_get_contents('http://www.rpg-paradize.com/?page=vote&vote='.$this->id); | |
if(!$page_data){ | |
trigger_error('RpgApi->getCaptcha() : une erreur est survenue lors de la récupération de la page. FALSE retourné !', E_USER_NOTICE); | |
return false; | |
} | |
$matches = array(); | |
$start_tag = '<script src=\'http://api.adscaptcha.com/Get.aspx'; | |
$end_tag = '\' type=\'text/javascript\'></script>'; | |
if(!preg_match('#'.$start_tag.'(\?.+)'.$end_tag.'#', $page_data, $matches)){ | |
trigger_error('RpgApi->getCaptcha() : La page a été récupéré avec succès, mais son contenue est invalide. L\'id RPG est peut-être invalide... FALSE retourné.', E_USER_NOTICE); | |
return false; | |
} | |
return $start_tag.$matches[1].$end_tag; | |
} | |
/** | |
* Envoit le vote à RPG paradize et test sont autenticité | |
* @param mixed $get Mettre TRUE si l'on passe par $_GET, ou mettre le tableau d'entrés | |
* si vous utiliser un système d'input particulier. Laissez FALSE (par defaut) sinon. | |
* @return boolean TRUE si le captacha est bon, FALSE sinon | |
*/ | |
public function submitVote($get = false){ | |
if(!$get) | |
$in = $_POST; | |
elseif($get===true) | |
$in = $_GET; | |
elseif(is_array($get)) | |
$in = $get; | |
else{ | |
trigger_error('RpgApi->submitVote() : Argument passé à la méthode invalide, vous avez donné un '.gettype($get).' au lieu d\'un booléen (true/false) ou un tableau !', E_USER_WARNING); | |
return false; | |
} | |
if(empty($in['adscaptcha_response_field']) || empty($in['adscaptcha_challenge_field'])) | |
return false; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, 'http://www.rpg-paradize.com/?page=vote2'); | |
curl_setopt($curl, CURLOPT_POST, TRUE); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, array( | |
'adscaptcha_response_field'=>$in['adscaptcha_response_field'], | |
'adscaptcha_challenge_field'=>$in['adscaptcha_challenge_field'], | |
'submitvote'=>$this->id | |
)); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
$data = curl_exec($curl); | |
curl_close($curl); | |
if($data===false){ | |
trigger_error('RpgApi->submitVote() : Une erreur est survenue lors du test du captcha...', E_USER_NOTICE); | |
return false; | |
} | |
if(strpos($data, '<span style="font-size:20px;color:red;">Captcha incorrect</span>')!==false) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment