diff options
author | Paul Arthur <paul.arthur@flowerysong.com> | 2013-01-27 16:03:58 -0500 |
---|---|---|
committer | Paul Arthur <paul.arthur@flowerysong.com> | 2013-03-28 16:56:21 -0400 |
commit | 46e325284e716b8f2fec3cc8ae18c3b0d886100d (patch) | |
tree | 4a7f7f109c892b411b9a92830a9332eca827f041 /modules | |
parent | 0ecaf693698b1ead5dc9167f7166770a54e39f51 (diff) | |
download | ampache-46e325284e716b8f2fec3cc8ae18c3b0d886100d.tar.gz ampache-46e325284e716b8f2fec3cc8ae18c3b0d886100d.tar.bz2 ampache-46e325284e716b8f2fec3cc8ae18c3b0d886100d.zip |
Clean up AmpacheApi module
Remove trailing whitespace.
Centralise debugging by using a function.
Add an optional debug callback for passing debug messages up the chain.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/ampacheapi/AmpacheApi.lib.php | 371 |
1 files changed, 191 insertions, 180 deletions
diff --git a/modules/ampacheapi/AmpacheApi.lib.php b/modules/ampacheapi/AmpacheApi.lib.php index 97fe86d5..c2db3697 100644 --- a/modules/ampacheapi/AmpacheApi.lib.php +++ b/modules/ampacheapi/AmpacheApi.lib.php @@ -1,4 +1,4 @@ -<?php +<?php /* vim:set softtabstop=4 shiftwidth=4 expandtab: */ /** * @@ -20,226 +20,246 @@ * */ -class AmpacheApi { +class AmpacheApi { // General Settings - private $server; - private $username; - private $password; - private $api_secure; + private $server; + private $username; + private $password; + private $api_secure; // Handshake variables - private $handshake; + private $handshake; private $handshake_time; // Used to figure out how stale our data is // Response variables - private $api_session; + private $api_session; // Constructed variables - private $api_url; - private $api_state='UNCONFIGURED'; - private $api_auth; + private $api_url; + private $api_state = 'UNCONFIGURED'; + private $api_auth; // XML Parser variables private $XML_currentTag; - private $XML_subTag; + private $XML_subTag; private $XML_parser; - private $XML_results; - private $XML_position=0; - protected $XML_grabtags = array(); - protected $XML_skiptags = array('root'); + private $XML_results; + private $XML_position = 0; + protected $XML_grabtags = array(); + protected $XML_skiptags = array('root'); protected $XML_parenttags = array('artist','album','song','tag','video','playlist','result', 'auth','version','update','add','clean','songs', 'artists','albums','tags','videos','api','playlists','catalogs'); // Library static version information - protected $LIB_version = '350001'; - private $API_version = ''; + protected static $LIB_version = '350001'; + private static $API_version = ''; - private $DEBUG=false; + private $_debug_callback = null; + private $_debug_output = false; /** * Constructor - * This takes an array of input, if enough information is provided then it will + * This takes an array of input, if enough information is provided then it will * attempt to connect to the API right away, otherwise it will simply return an * object that can be later configured and then connected */ - public function __construct($config=array()) { + public function __construct($config=array()) { // See if we are setting debug first - if ($config['debug']) { - $this->debug($config['debug']); - } + if (isset($config['debug'])) { + $this->_debug_output = $config['debug']; + } + + if (isset($config['debug_callback'])) { + $this->_debug_callback = $config['debug_callback']; + } // If we got something, then configure! - if (is_array($config) AND count($config)) { - $this->configure($config); - } + if (is_array($config) AND count($config)) { + $this->configure($config); + } // If we've been READY'd then go ahead and attempt to connect - if ($this->state() == 'READY') { + if ($this->state() == 'READY') { $this->connect(); - } + } } // constructor /** + * _debug + * + * Make debugging all nice and pretty. + */ + private function _debug($source, $message, $level = 5) { + if ($this->_debug_output) { + echo "$source :: $message\n"; + } + + if (!is_null($this->_debug_callback)) { + call_user_func($this->_debug_callback, 'AmpacheApi', "$source :: $message", $level); + } + } + + /** * connect * This attempts to connect to the ampache instance, for now we assume the newer version */ - public function connect() { + public function connect() { + + $this->_debug('CONNECT', "Using $this->username / $this->password"); - if ($this->debug) { echo "CONNECT:: Using $this->username / $this->password\n"; } + // Set up the handshake + $results = array(); + $timestamp = time(); + $key = hash('sha256',$this->password); + $passphrase = hash('sha256',$timestamp . $key); - // Setup the handshake - $results = array(); - $timestamp = time(); - $key = hash('sha256',$this->password); - $passphrase = hash('sha256',$timestamp . $key); + $options = array('timestamp'=>$timestamp,'auth'=>$passphrase,'version'=>$this->LIB_version,'user'=>$this->username); - $options = array('timestamp'=>$timestamp,'auth'=>$passphrase,'version'=>$this->LIB_version,'user'=>$this->username); + $response = $this->send_command('handshake',$options); - $response = $this->send_command('handshake',$options); + $this->parse_response($response); - $this->parse_response($response); - // We want the first response - $data = $this->get_response(); - foreach ($data as $value) { - $results = array_merge($results,$value); - } - - if (!$results['auth']) { - $this->set_state('error'); - return false; - } - $this->api_auth = $results['auth']; - $this->set_state('connected'); + $data = $this->get_response(); + foreach ($data as $value) { + $results = array_merge($results,$value); + } + + if (!$results['auth']) { + $this->set_state('error'); + return false; + } + $this->api_auth = $results['auth']; + $this->set_state('connected'); // Define when we pulled this, it is not wine, it does // not get better with age - $this->handshake_time = time(); - $this->handshake = $results; + $this->handshake_time = time(); + $this->handshake = $results; } // connect /** * configure * This function takes an array of elements and configures the AmpaceApi object - * it doesn't really do much more, it is it's own function so we can call it - * from the constructor or directly, if we so desire. + * it doesn't really do much more, it is it's own function so we can call it + * from the constructor or directly, if we so desire. */ - public function configure($config=array()) { + public function configure($config=array()) { - if ($this->debug) { echo "CONFIGURE :: Checking Passed config options\n"; } + $this->_debug('CONFIGURE', 'Checking passed config options'); if (!is_array($config)) { - trigger_error('AmpacheApi::configure received a non-array value'); - return false; - } + trigger_error('AmpacheApi::configure received a non-array value'); + return false; + } if (isset($config['username'])) { - $this->username = htmlentities($config['username'],ENT_QUOTES,'UTF-8'); - } - if (isset($config['password'])) { - $this->password = htmlentities($config['password'],ENT_QUOTES,'UTF-8'); - } - if (isset($config['server'])) { + $this->username = htmlentities($config['username'],ENT_QUOTES,'UTF-8'); + } + if (isset($config['password'])) { + $this->password = htmlentities($config['password'],ENT_QUOTES,'UTF-8'); + } + if (isset($config['server'])) { // Replace any http:// in the URL with '' - $config['server'] = str_replace('http://','',$config['server']); - $this->server = htmlentities($config['server'],ENT_QUOTES,'UTF-8'); - } - if (isset($config['api_secure'])) { + $config['server'] = str_replace('http://','',$config['server']); + $this->server = htmlentities($config['server'],ENT_QUOTES,'UTF-8'); + } + if (isset($config['api_secure'])) { // This should be a boolean response - $this->api_secure = $config['api_secure'] ? true : false; - } + $this->api_secure = $config['api_secure'] ? true : false; + } // Once we've loaded the config variables we can build some of the final values - $this->api_url = ($this->api_secure ? 'https://' : 'http://') . $this->server . '/server/xml.server.php'; + $this->api_url = ($this->api_secure ? 'https://' : 'http://') . $this->server . '/server/xml.server.php'; // See if we have enough to authenticate, if so change the state - if ($this->username AND $this->password AND $this->server) { - $this->set_state('ready'); - } + if ($this->username AND $this->password AND $this->server) { + $this->set_state('ready'); + } - return true; + return true; } // configure /** * set_state * This sets the current state of the API, it is used mostly internally but - * the state can be accessed externally so it could be used to check and see + * the state can be accessed externally so it could be used to check and see * where the API is at, at this moment */ - public function set_state($state) { + public function set_state($state) { // Very simple for now, maybe we'll do something more with this later - $this->api_state = strtoupper($state); + $this->api_state = strtoupper($state); } // set_state /** * state - * This returns the state of the API + * This returns the state of the API */ - public function state() { + public function state() { - return $this->api_state; + return $this->api_state; } // state /** * info - * Returns the information gathered by the handshake - * not raw so we can formated it if we wanted? + * Returns the information gathered by the handshake + * not raw so we can formated it if we wanted? */ - public function info() { + public function info() { - if ($this->state() != 'CONNECTED') { - throw new Exception('AmpacheApi::info API in non-ready state, unable to return info'); - } + if ($this->state() != 'CONNECTED') { + throw new Exception('AmpacheApi::info API in non-ready state, unable to return info'); + } - return $this->handshake; + return $this->handshake; } // info /** * send_command * This sends an API command, with options to the currently connected - * host, and returns a nice clean keyed array + * host, and returns a nice clean keyed array */ - public function send_command($command,$options=array()) { - - if ($this->debug) { echo "SEND COMMAND:: $command"; print_r($options,1); echo "\n"; } + public function send_command($command,$options=array()) { + + $this->_debug('SEND COMMAND', $command . ' ' . json_encode($options)); - if ($this->state() != 'READY' AND $this->state() != 'CONNECTED') { + if ($this->state() != 'READY' AND $this->state() != 'CONNECTED') { throw new Exception('AmpacheApi::send_command API in non-ready state, unable to send'); - } - if (!trim($command)) { - throw new Exception('AmpacheApi::send_command no command specified'); - } - if (!$this->validate_command($command)) { - throw new Exception('AmpacheApi::send_command Invalid/Unknown command ' . $command . ' issued'); - } - - $url = $this->api_url . '?action=' . urlencode($command); - - foreach ($options as $key=>$value) { - if (!trim($key)) { + } + if (!trim($command)) { + throw new Exception('AmpacheApi::send_command no command specified'); + } + if (!$this->validate_command($command)) { + throw new Exception('AmpacheApi::send_command Invalid/Unknown command ' . $command . ' issued'); + } + + $url = $this->api_url . '?action=' . urlencode($command); + + foreach ($options as $key=>$value) { + if (!trim($key)) { // Non fatal don't need to except it - trigger_error('AmpacheApi::send_command unable to append empty variable to command'); - continue; - } - $url .= '&' . urlencode($key) . '=' . urlencode($value); - } + trigger_error('AmpacheApi::send_command unable to append empty variable to command'); + continue; + } + $url .= '&' . urlencode($key) . '=' . urlencode($value); + } // IF Auth is set then we append it so you don't have to think about it, also do username - if ($this->api_auth) { - $url .= '&auth=' . urlencode($this->api_auth) . '&username=' . urlencode($this->username); - } + if ($this->api_auth) { + $url .= '&auth=' . urlencode($this->api_auth) . '&username=' . urlencode($this->username); + } - $data = file_get_contents($url); - return $data; + $data = file_get_contents($url); + return $data; } // send_command @@ -247,11 +267,11 @@ class AmpacheApi { * validate_command * This takes the specified command, and checks it against the known * commands for the current version of Ampache. If no version is known yet - * This it will return FALSE for everything except ping and handshake. + * This it will return FALSE for everything except ping and handshake. */ - public function validate_command($command) { + public function validate_command($command) { - return true; + return true; } // validate_command @@ -261,20 +281,20 @@ class AmpacheApi { * it does that it will clean up anything that was there before, so I hope * you've saved! */ - public function parse_response($response) { + public function parse_response($response) { // Reset the results - $this->XML_results = array(); - $this->XML_position = 0; - - $this->XML_create_parser(); + $this->XML_results = array(); + $this->XML_position = 0; + + $this->XML_create_parser(); - if (!xml_parse($this->XML_parser,$response)) { - throw new Exception('AmpacheApi::parse_response was unable to parse XML document'); - } + if (!xml_parse($this->XML_parser,$response)) { + throw new Exception('AmpacheApi::parse_response was unable to parse XML document'); + } - xml_parser_free($this->XML_parser); - return true; + xml_parser_free($this->XML_parser); + return true; } // parse_response @@ -282,21 +302,12 @@ class AmpacheApi { * get_response * This returns the raw response from the last parsed response */ - public function get_response() { + public function get_response() { - return $this->XML_results; + return $this->XML_results; } // get_response - /** - * debug - * set debug to true? - */ - private function debug($value) { - - $this->debug = intval($value); - - } // debug /////////////////////////// XML PARSER FUNCTIONS ///////////////////////////// @@ -304,13 +315,13 @@ class AmpacheApi { * XML_create_parser * This creates the xml parser and sets the options */ - public function XML_create_parser() { + public function XML_create_parser() { - $this->XML_parser = xml_parser_create(); - xml_parser_set_option($this->XML_parser,XML_OPTION_CASE_FOLDING,false); - xml_set_object($this->XML_parser,$this); - xml_set_element_handler($this->XML_parser,'XML_start_element','XML_end_element'); - xml_set_character_data_handler($this->XML_parser,'XML_cdata'); + $this->XML_parser = xml_parser_create(); + xml_parser_set_option($this->XML_parser,XML_OPTION_CASE_FOLDING,false); + xml_set_object($this->XML_parser,$this); + xml_set_element_handler($this->XML_parser,'XML_start_element','XML_end_element'); + xml_set_character_data_handler($this->XML_parser,'XML_cdata'); } // XML_create_parser @@ -318,54 +329,54 @@ class AmpacheApi { * XML_cdata * This is called for the content of the XML tag */ - public function XML_cdata($parser,$cdata) { + public function XML_cdata($parser,$cdata) { - $cdata = trim($cdata); + $cdata = trim($cdata); - if (!$this->XML_currentTag || !$cdata) { return false; } + if (!$this->XML_currentTag || !$cdata) { return false; } - if ($this->XML_subTag) { - $this->XML_results[$this->XML_position][$this->XML_currentTag][$this->XML_subTag] = $cdata; - } - else { - $this->XML_results[$this->XML_position][$this->XML_currentTag] = $cdata; - } + if ($this->XML_subTag) { + $this->XML_results[$this->XML_position][$this->XML_currentTag][$this->XML_subTag] = $cdata; + } + else { + $this->XML_results[$this->XML_position][$this->XML_currentTag] = $cdata; + } } // XML_cdata - public function XML_start_element($parser,$tag,$attributes) { + public function XML_start_element($parser,$tag,$attributes) { // Skip it! - if (in_array($tag,$this->XML_skiptags)) { return false; } - - if (!in_array($tag,$this->XML_parenttags) OR $this->XML_currentTag) { - $this->XML_subTag = $tag; - } - else { - $this->XML_currentTag = $tag; - } - - if (count($attributes)) { - if (!$this->XML_subTag) { - $this->XML_results[$this->XML_position][$this->XML_currentTag]['self'] = $attributes; + if (in_array($tag,$this->XML_skiptags)) { return false; } + + if (!in_array($tag,$this->XML_parenttags) OR $this->XML_currentTag) { + $this->XML_subTag = $tag; + } + else { + $this->XML_currentTag = $tag; + } + + if (count($attributes)) { + if (!$this->XML_subTag) { + $this->XML_results[$this->XML_position][$this->XML_currentTag]['self'] = $attributes; } - else { - $this->XML_results[$this->XML_position][$this->XML_currentTag][$this->XML_subTag]['self'] = $attributes; + else { + $this->XML_results[$this->XML_position][$this->XML_currentTag][$this->XML_subTag]['self'] = $attributes; } - } + } } // start_element - public function XML_end_element($parser,$tag) { + public function XML_end_element($parser,$tag) { - if ($tag != $this->XML_currentTag) { - $this->XML_subTag = false; - } - else { - $this->XML_currentTag = false; - $this->XML_position++; - } + if ($tag != $this->XML_currentTag) { + $this->XML_subTag = false; + } + else { + $this->XML_currentTag = false; + $this->XML_position++; + } } // end_element |