Advertisement
Guest User

Untitled

a guest
May 9th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. <?php
  2. class AniDB {
  3.     const API_VERSION    = 3;
  4.     const CLIENT_NAME    = ""; # Your client name goes here
  5.     const CLIENT_VERSION = 1;  # Your client version goes here
  6.    
  7.     public $lastcmd = 0;
  8.     public $key     = FALSE;
  9.     public $debug   = TRUE;
  10.    
  11.     public $hostname;
  12.     public $port;
  13.     public $s;
  14.    
  15.    
  16.     public $bad = array(
  17.         500, 501, 502, 503, 504, 505, 506, 509,
  18.         519, 555, 598, 600, 601, 602, 666);
  19.    
  20.     public function __construct($hostname = "udp://api.anidb.net", $port = 9000) {
  21.         $this->hostname = $hostname;
  22.         $this->port     = $port;
  23.        
  24.         $this->s = stream_socket_client(
  25.             $hostname.":".$port,
  26.             $errno, $error,
  27.             30, # Timeout
  28.             STREAM_CLIENT_CONNECT,
  29.             stream_context_create(array(
  30.                 "socket" => array("bindto" => "0:37153"))));
  31.        
  32.         if($this->s === FALSE) {
  33.             echo $error;
  34.             throw new Exception("Failed to connect ($errno : $error.)");
  35.         }
  36.     }
  37.     public function __destroy() {
  38.         if(!feof($this->s)) {
  39.             $self->logout();
  40.             fclose($this->s);
  41.         }
  42.     }
  43.    
  44.     public function query($cmd, $args = array(), $tries = 0) {
  45.         # See if we've tried too many times
  46.         if($tries >= 3) {
  47.             throw new Exception("UDP API seems to be unreachable, I don't know what to do.");
  48.         }
  49.        
  50.         $cmd = strtoupper($cmd);
  51.        
  52.         # Make sure we have an array
  53.         if(!is_array($args)) {
  54.             $args = array();
  55.         }
  56.        
  57.         # Add session key to list of arguments if present
  58.         if($this->key) {
  59.             $args = array_merge($args, array("s" => $this->key));
  60.         }
  61.        
  62.         # Build query if we've got any arguments
  63.         $q = $cmd;
  64.         if(!empty($args)) {
  65.             $q .= " ".join(
  66.                 array_map(
  67.                     create_function('$x, $y', 'return "$x=$y";'),
  68.                     array_keys($args),
  69.                     array_values($args)),
  70.                 "&");
  71.         }
  72.        
  73.         # Do we need to sleep?
  74.         if(microtime(TRUE) - $this->lastcmd < 2) {
  75.             sleep(ceil(microtime(TRUE) - $this->lastcmd));
  76.         }
  77.        
  78.         # Send query
  79.         $this->debug(">", $q);
  80.         fwrite($this->s, $q);
  81.         $this->lastcmd = microtime(TRUE);
  82.        
  83.         # Get reply
  84.         $reply = trim(fread($this->s, 1500));
  85.        
  86.         # Is response empty? Try again
  87.         if(empty($reply)) {
  88.             return $this->query($cmd, $args, $tries++);
  89.         }
  90.        
  91.         # Treat reply
  92.         $r = array();
  93.         list($r["num"], $r["msg"]) = explode(" ", $reply, 2);
  94.        
  95.         # Did we get a bad return code?
  96.         if(in_array($r["num"], $this->bad)) {
  97.             throw new Exception($r["num"]." ".$r["msg"]);
  98.         }
  99.        
  100.         # Check if we've got data
  101.         if(strpos($r["msg"], "\n") !== FALSE) {
  102.             list($r["msg"], $r["data"]) = explode("\n", $r["msg"], 2);
  103.             $this->debug("<", $r["num"]." ".$r["msg"], $r["data"]);
  104.            
  105.             # Split data at each "|"
  106.             if(strpos($r["data"], "|") === FALSE) {
  107.                 $r["data"] = array($r["data"]);
  108.             } else {
  109.                 $r["data"] = explode("|", $r["data"]);
  110.             }
  111.         } else {
  112.             $this->debug("<", $r["num"]." ".$r["msg"]);
  113.         }
  114.         return $r;
  115.     }
  116.    
  117.     public function debug($type, $msg, $data = FALSE) {
  118.         $msg = preg_replace('/pass=([^&]+)/', 'pass=xxxxxx', $msg);
  119.         if($this->debug) {
  120.             if(empty($data)) {
  121.                 printf("[%s] [%s] %s\n",
  122.                     date("Y-m-d H:i:s"),
  123.                     $type, $msg);
  124.             } else {
  125.                 printf("[%s] [%s] %s\n".str_repeat(" ", 26)."%s\n",
  126.                     date("Y-m-d H:i:s"),
  127.                     $type, $msg, $data);
  128.             }
  129.         }
  130.     }
  131.    
  132.     public function login($user, $pass) {
  133.         $this->user = $user;
  134.         $this->pass = $pass;
  135.        
  136.         $r = $this->query("AUTH", array(
  137.             "user"      => $user,
  138.             "pass"      => $pass,
  139.             "protover"  => self::API_VERSION,
  140.             "client"    => self::CLIENT_NAME,
  141.             "clientver" => self::CLIENT_VERSION));
  142.        
  143.         if(preg_match('/^20[01]$/', $r["num"])) {
  144.             $this->key = substr(
  145.                 $r["msg"],
  146.                 0,
  147.                 strpos($r["msg"], " "));
  148.             return TRUE;
  149.         } else {
  150.             return FALSE;
  151.         }
  152.     }
  153.     public function logout() {
  154.         $this->query("LOGOUT");
  155.     }
  156.     public function ping() {
  157.         $ping = microtime(TRUE);
  158.         $r = $this->query("PING");
  159.         $pong = microtime(TRUE);
  160.        
  161.         $time = round(($pong - $ping) * 1000);
  162.         $this->debug("!", "Responded in $time ms");
  163.         return $time;
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement