Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AniDB {
- const API_VERSION = 3;
- const CLIENT_NAME = ""; # Your client name goes here
- const CLIENT_VERSION = 1; # Your client version goes here
- public $lastcmd = 0;
- public $key = FALSE;
- public $debug = TRUE;
- public $hostname;
- public $port;
- public $s;
- public $bad = array(
- 500, 501, 502, 503, 504, 505, 506, 509,
- 519, 555, 598, 600, 601, 602, 666);
- public function __construct($hostname = "udp://api.anidb.net", $port = 9000) {
- $this->hostname = $hostname;
- $this->port = $port;
- $this->s = stream_socket_client(
- $hostname.":".$port,
- $errno, $error,
- 30, # Timeout
- STREAM_CLIENT_CONNECT,
- stream_context_create(array(
- "socket" => array("bindto" => "0:37153"))));
- if($this->s === FALSE) {
- echo $error;
- throw new Exception("Failed to connect ($errno : $error.)");
- }
- }
- public function __destroy() {
- if(!feof($this->s)) {
- $self->logout();
- fclose($this->s);
- }
- }
- public function query($cmd, $args = array(), $tries = 0) {
- # See if we've tried too many times
- if($tries >= 3) {
- throw new Exception("UDP API seems to be unreachable, I don't know what to do.");
- }
- $cmd = strtoupper($cmd);
- # Make sure we have an array
- if(!is_array($args)) {
- $args = array();
- }
- # Add session key to list of arguments if present
- if($this->key) {
- $args = array_merge($args, array("s" => $this->key));
- }
- # Build query if we've got any arguments
- $q = $cmd;
- if(!empty($args)) {
- $q .= " ".join(
- array_map(
- create_function('$x, $y', 'return "$x=$y";'),
- array_keys($args),
- array_values($args)),
- "&");
- }
- # Do we need to sleep?
- if(microtime(TRUE) - $this->lastcmd < 2) {
- sleep(ceil(microtime(TRUE) - $this->lastcmd));
- }
- # Send query
- $this->debug(">", $q);
- fwrite($this->s, $q);
- $this->lastcmd = microtime(TRUE);
- # Get reply
- $reply = trim(fread($this->s, 1500));
- # Is response empty? Try again
- if(empty($reply)) {
- return $this->query($cmd, $args, $tries++);
- }
- # Treat reply
- $r = array();
- list($r["num"], $r["msg"]) = explode(" ", $reply, 2);
- # Did we get a bad return code?
- if(in_array($r["num"], $this->bad)) {
- throw new Exception($r["num"]." ".$r["msg"]);
- }
- # Check if we've got data
- if(strpos($r["msg"], "\n") !== FALSE) {
- list($r["msg"], $r["data"]) = explode("\n", $r["msg"], 2);
- $this->debug("<", $r["num"]." ".$r["msg"], $r["data"]);
- # Split data at each "|"
- if(strpos($r["data"], "|") === FALSE) {
- $r["data"] = array($r["data"]);
- } else {
- $r["data"] = explode("|", $r["data"]);
- }
- } else {
- $this->debug("<", $r["num"]." ".$r["msg"]);
- }
- return $r;
- }
- public function debug($type, $msg, $data = FALSE) {
- $msg = preg_replace('/pass=([^&]+)/', 'pass=xxxxxx', $msg);
- if($this->debug) {
- if(empty($data)) {
- printf("[%s] [%s] %s\n",
- date("Y-m-d H:i:s"),
- $type, $msg);
- } else {
- printf("[%s] [%s] %s\n".str_repeat(" ", 26)."%s\n",
- date("Y-m-d H:i:s"),
- $type, $msg, $data);
- }
- }
- }
- public function login($user, $pass) {
- $this->user = $user;
- $this->pass = $pass;
- $r = $this->query("AUTH", array(
- "user" => $user,
- "pass" => $pass,
- "protover" => self::API_VERSION,
- "client" => self::CLIENT_NAME,
- "clientver" => self::CLIENT_VERSION));
- if(preg_match('/^20[01]$/', $r["num"])) {
- $this->key = substr(
- $r["msg"],
- 0,
- strpos($r["msg"], " "));
- return TRUE;
- } else {
- return FALSE;
- }
- }
- public function logout() {
- $this->query("LOGOUT");
- }
- public function ping() {
- $ping = microtime(TRUE);
- $r = $this->query("PING");
- $pong = microtime(TRUE);
- $time = round(($pong - $ping) * 1000);
- $this->debug("!", "Responded in $time ms");
- return $time;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement