Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Jv {
- private int $apiVersion = 4;
- private string $subdomain = "api", $partnerKey = "550c04bf5cb2b", $hmacSecret = "d84e9e5f191ea4ffc39c22d11c77dd6c", $coniunctio = "";
- /**
- * Constructeur
- *
- * @param string $username Nom d'utilisateur
- * @param string $password Mot de passe
- */
- public function __construct(string $username, string $password) {
- $this->curl = curl_init();
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($this->curl, CURLOPT_USERAGENT, "JeuxVideo-Android/267");
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- $this->login($username, $password);
- }
- /**
- * Destructeur
- */
- public function __destruct() {
- $this->logout();
- curl_close($this->curl);
- }
- /**
- * Lance un appel vers un path de l'API JV
- *
- * @param string $path Path
- * @param string $method Méthode HTTP
- * @param array $post Données POST
- * @param bool $enableHeaders Retourne ou non les headers HTTP (et désactive en conséquence l'encodage en JSON)
- */
- private function call(string $path, string $method = "GET", array $post = [], bool $enableHeaders = false) {
- $date = date("c");
- $signature = hash_hmac("sha256", "{$this->partnerKey}\n$date\n$method\n{$this->subdomain}.jeuxvideo.com\n/v{$this->apiVersion}/$path\n", $this->hmacSecret);
- $header = "PartnerKey={$this->partnerKey}, Signature=$signature, Timestamp=$date";
- curl_setopt($this->curl, CURLOPT_URL, "https://{$this->subdomain}.jeuxvideo.com/v{$this->apiVersion}/$path");
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, ["Jvc-Authorization: $header", "Content-Type: application/json"]);
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
- if ($method != "GET") {
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($post));
- } else {
- curl_setopt($this->curl, CURLOPT_POST, false);
- }
- if (!empty($this->coniunctio)) {
- curl_setopt($this->curl, CURLOPT_COOKIE, "coniunctio={$this->coniunctio}");
- }
- curl_setopt($this->curl, CURLOPT_HEADER, $enableHeaders);
- return !$enableHeaders ? json_decode(curl_exec($this->curl)) : curl_exec($this->curl);
- }
- /**
- * Se connecte à un compte JV
- *
- * @param string $username Nom d'utilisateur
- * @param string $password Mot de passe
- *
- * @return bool Résultat
- */
- private function login(string $username, string $password) : bool {
- $result = $this->call("accounts/login", "POST", ["alias" => $username, "password" => $password], true);
- if (!strstr($result, "coniunctio=")) {
- trigger_error("Impossible de récupérer le cookie d'authentification.", E_USER_ERROR);
- }
- $this->coniunctio = explode("coniunctio=", explode(";", $result)[0])[1];
- return true;
- }
- /**
- * Se déconnecte d'un compte JV (détruit en conséquence le coniunctio)
- *
- * @return bool Résultat
- */
- private function logout() : bool {
- if (empty($this->coniunctio)) {
- return false;
- }
- $this->call("accounts/logout", "POST");
- return true;
- }
- /**
- * Charge les données d'un profil JV
- *
- * @param int $accountId ID du compte
- *
- * @return stdClass Résultat
- */
- public function loadProfile(int $accountId) : stdClass {
- return $this->call("accounts/$accountId/profile");
- }
- /**
- * Charge les contributions d'un profil JV
- *
- * @param int $accountId ID du compte
- *
- * @return stdClass Résultat
- */
- public function loadProfilePage(int $accountId) : stdClass {
- return $this->call("accounts/$accountId/page");
- }
- /**
- * Charge les "contenus" d'un profil JV
- *
- * @param int $accountId ID du compte
- * @param int $page Page
- * @param int $perPage Nombre d'entrées par page
- *
- * @return stdClass Résultat
- */
- public function loadProfileContents(int $accountId, $page = 1, $perPage = 10) : stdClass {
- return $this->call("accounts/$accountId/page/contents");
- }
- /**
- * Charge les reviews d'un profil JV
- *
- * @param int $accountId ID du compte
- * @param int $page Page
- * @param int $perPage Nombre d'entrées par page
- *
- * @return stdClass Résultat
- */
- public function loadProfileReviews(int $accountId, $page = 1, $perPage = 10) : stdClass {
- return $this->call("accounts/$accountId/page/reviews");
- }
- /**
- * Modifie la description du compte
- *
- * @param string $description Description
- *
- * @return bool Résultat
- */
- public function changeDescription(string $description = "") : bool {
- $result = $this->call("accounts/me/profile/description", "PUT", ["description" => $description]);
- if (isset($result->message)) {
- trigger_error($result->message, E_USER_ERROR);
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment