diff --git a/Client.php b/Client.php new file mode 100644 index 0000000..e4fd254 --- /dev/null +++ b/Client.php @@ -0,0 +1,199 @@ + Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + * + * This file is part of php-plex. + * + * php-plex is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * php-plex is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +/** + * Represents a Plex client on the network. + * + * @category php-plex + * @package Plex_Client + * @author Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + */ +class Plex_Client extends Plex_MachineAbstract +{ + /** + * Plex client host as returned by Plex_Server::getClients(). + * @var string + */ + private $host; + + /** + * Plex client machine identifier as returned by Plex_Server::getClients(). + * @var string + */ + private $machineIdentifier; + + /** + * Version of Plex the client is running as returned by + * Plex_Server::getClients(). + * @var string + */ + private $version; + + /** + * The default port on which a Plex client listens. + */ + const DEFAULT_PORT = 3000; + + /** + * Sets up our Plex client using the minimum amount of data required to + * interact. + * + * @param string $name The name of the Plex client. + * @param string $address The IP address of the Plex client. + * @param integer $port The port on which the Plex client is listening. + * + * @uses Plex_MachineAbstract::$name + * @uses Plex_MachineAbstract::$address + * @uses Plex_MachineAbstract::$port + * @uses Plex_Client::DEFAULT_PORT + * + * @return void + */ + public function __construct($name, $address, $port) + { + $this->name = $name; + $this->address = $address; + $this->port = $port ? $port : self::DEFAULT_PORT; + } + + /** + * Returns the Plex client's name. + * + * @uses Plex_MachineAbstract::$name + * + * @return string The name of the Plex client. + */ + public function getName() + { + return $this->name; + } + + /** + * Returns the Plex client's IP address. + * + * @uses Plex_MachineAbstract::$address + * + * @return string The IP address of the Plex client. + */ + public function getAddress() + { + return $this->address; + } + + /** + * Returns the port on which the Plex client listens. + * + * @uses Plex_MachineAbstract::$port + * + * @return integer The port on which the Plex client listens. + */ + public function getPort() + { + return $this->port; + } + + /** + * Returns the hostname of the Plex client. + * + * @uses Plex_Client::$host + * + * @return string The hostname of the Plex client. + */ + public function getHost() + { + return $this->host; + } + + /** + * Sets the hostname of the Plex client. + * + * @param string $host The hostname of the Plex client. + * + * @uses Plex_client::$host + * + * @return void + */ + public function setHost($host) + { + $this->host = $host; + } + + /** + * Returns the mac address of the Plex client. + * + * @uses Plex_Client::$machineIdentifier + * + * @return string The mac address of the Plex client. + */ + public function getMachineIdentifier() + { + return $this->machineIdentifier; + } + + /** + * Sets the mac address of the Plex client. + * + * @param string $machineIdentifier The macc address of the Plex client. + * + * @uses Plex_Client::$machineIdentifier + * + * @return void + */ + public function setMachineIdentifier($machineIdentifier) + { + $this->machineIdentifier = $machineIdentifier; + } + + /** + * Returns the version of the Plex software the Plex client is running. + * + * @uses Plex_Client::$version + * + * @return string The version of the Plex software the Plex client is + * running. + */ + public function getVersion() + { + return $this->version; + } + + /** + * Sets the version of the Plex software the Plex client is running. + * + * @param string $version The version of the Plex software teh Plex client + * is running. + * + * @uses Plex_Client::$version + * + * @return void + */ + public function setVersion($version) + { + $this->version = $version; + } +} diff --git a/Machine/MachineAbstract.php b/Machine/MachineAbstract.php new file mode 100644 index 0000000..a123d7c --- /dev/null +++ b/Machine/MachineAbstract.php @@ -0,0 +1,126 @@ + Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + * + * This file is part of php-plex. + * + * php-plex is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * php-plex is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +/** + * Abstract base class containing methods and members for all Plex machines on + * the network. This is used to define both Plex clients and servers. + * + * @category php-plex + * @package Plex_Machine + * @author Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + */ +abstract class Plex_MachineAbstract implements Plex_MachineInterface +{ + /** + * The name of the Plex machine on the network. + * @var string + */ + protected $name; + + /** + * The IP address of the Plex machine on the network. + * @var string + */ + protected $address; + + /** + * The port on which the Plex machine is listening. Typically 32400 for + * servers and 3000 for clients. + * @var integer + */ + protected $port; + + /** + * Returns the base URL, which will be standard for all requests made to the + * Plex machine. + * + * @uses Plex_MachineAbstract::$address + * @uses Plex_MachineAbstract::$port + * + * @return string The base URL, which will be standard for all requests made + * to the Plex machine. + */ + protected function getBaseUrl() + { + return sprintf( + 'http://%s:%s', + $this->address, + $this->port + ); + } + + /** + * Typically the useful data returned by a Plex machine will containted in + * XML attributes. This allows a set of XML nodes to be passed and all the + * attribues extracted and returned as an associated array. + * + * @param SimpleXMLElement $xmlNodes An XML node to have its attributes + * converted to a useful PHP array. + * + * @todo Make this recursive. + * + * @return array An associated array of XML attributes. + */ + protected function xmlAttributesToArray($xmlNodes) + { + $array = array(); + $i= 0;; + foreach($xmlNodes as $xmlNode) { + foreach($xmlNode->attributes() as $key => $value) { + // For abstraction, everything is casted to string. It is the + // responsibility of the calling method to handle typing. + $array[$i][$key] = (string) $value[0]; + } + $i++; + } + return $array; + } + + /** + * Utilizes php-curl to send a request to the passed URL and returns an XML + * document reprentation of the returned content. + * + * @param string $url The URL to which the request is to be made. + * + * @return SimpleXMLElement An XML document from a Plex machine. + */ + protected function makeCall($url) + { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, FALSE); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + + $response = curl_exec($ch); + + curl_close($ch); + + return simplexml_load_string($response); + } +} diff --git a/Machine/MachineInterface.php b/Machine/MachineInterface.php new file mode 100644 index 0000000..1d54832 --- /dev/null +++ b/Machine/MachineInterface.php @@ -0,0 +1,58 @@ + Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + * + * This file is part of php-plex. + * + * php-plex is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * php-plex is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +/** + * Interface that defines the structure of Plex machines. + * + * @category php-plex + * @package Plex_Machine + * @author Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + */ +interface Plex_MachineInterface +{ + /** + * Returns the name of the Plex machine. + * + * @return string The name of the Plex machine. + */ + public function getName(); + + /** + * Returns the IP address of the Plex machine. + * + * @return string The IP address of the Plex machine. + */ + public function getAddress(); + + /** + * Returns the port on which the Plex machine listens. + * + * @return integer The port on which the Plex machine listens. + */ + public function getPort(); +} diff --git a/Plex.php b/Plex.php new file mode 100644 index 0000000..cf23ef6 --- /dev/null +++ b/Plex.php @@ -0,0 +1,152 @@ + Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + * + * This file is part of php-plex. + * + * php-plex is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * php-plex is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +$phpPlexDir = dirname(__FILE__); + +require_once(sprintf('%s/Machine/MachineInterface.php', $phpPlexDir)); +require_once(sprintf('%s/Machine/MachineAbstract.php', $phpPlexDir)); +require_once(sprintf('%s/Server.php', $phpPlexDir)); +require_once(sprintf('%s/Client.php', $phpPlexDir)); + +/** + * Bootstrap class for using php-plex to interact with the Plex HTTP API. + * + * @category php-plex + * @package Plex + * @author Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + */ +class Plex +{ + /** + * A list of Plex server machines on the network. This is defined by the + * instantiating software. + * @var Plex_Server[] + */ + private static $servers = array(); + + /** + * A list of the Plex client machines on the network This is found upon + * registring of Plex server. The first registered Plex server will go out + * and get the list of available clients and register them accordingly. + * @var Plex_Client[] + */ + private static $clients = array(); + + /** + * Allows an instantiating software to define a list of Plex servers on the + * network. In addition, the first server listed will be used to find the + * list of available clients and will register them accordingly. + * + * @param array $servers An associative array of Plex server machines on the + * network defined thusly: + * + * array ( + * 'server-1-name' => array( + * 'address' => '192.168.1.5', + * 'port' => 32400 + * ), + * 'server-2-name' => array( + * 'address' => '192.168.1.10', + * 'port' => 32400 + * ) + * ) + * + * @uses Plex::$servers + * @uses Plex::registerClients() + * @uses Plex::getServer() + * @uses Plex_Server::getClient() + * + * @return void + */ + public function registerServers(array $servers) + { + // Register each server. + foreach ($servers as $name => $server) { + $port = isset($server['port']) ? $server['port'] : NULL; + self::$servers[$name] = new Plex_Server( + $name, + $server['address'], + $port + ); + } + + // We are going to use the first server in the list to get a list of the + // availalble clients and register those automatically. + $serverName = reset(array_keys(self::$servers)); + $this->registerClients( + $this->getServer($serverName)->getClients() + ); + } + + /** + * Registers each found client with the bootstrap, so they can be found and + * used by the instantiating software. + * + * @param Plex_Client[] $clients An associative array of Plex client machines on the + * network. + * + * @uses Plex::$clients + * + * @return void + */ + private function registerClients(array $clients) + { + self::$clients = $clients; + } + + /** + * Returns the requested server by the unique name under which it was registered. + * + * @param string $serverName The unique name of the requested server. + * + * @uses Plex::$servers + * + * @return Plex_Server The requested Plex server machine. + */ + public function getServer($serverName) + { + return self::$servers[$serverName]; + } + + /** + * Returns the requested client by the unique name under which it was registered. + * + * @param string $clientName The unique name of the requested client. + * + * @uses Plex::$clients + * + * @return Plex_Client The requested Plex client machine. + */ + public function getClient($clientName) + { + return self::$clients[$clientName]; + } +} diff --git a/README.md b/README.md index e8a631b..f7a2be3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ php-plex -======== \ No newline at end of file +======== + +A simple PHP library for interacting with the Plex (http://plexapp.com) HTTP API. + +Requirements +============ + +php-curl +simpleXML + +Examples +======== + + $servers = array( + 'shepherd' => array( + 'host' => '192.168.11.9' + ) + ); + + + $plex = new Plex(); + $plex->registerServers($servers); + + $server = $plex->getServer('shepherd'); + $client = $plex->getClient('zoe'); + + diff --git a/Server.php b/Server.php new file mode 100644 index 0000000..6776c4a --- /dev/null +++ b/Server.php @@ -0,0 +1,148 @@ + Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + * + * This file is part of php-plex. + * + * php-plex is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * php-plex is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +/** + * Represents a Plex server on the network. + * + * @category php-plex + * @package Plex_Server + * @author Nick Bartkowiak + * @copyright (c) 2012 Nick Bartkowiak + * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3) + * @version 0.0.1 + */ +class Plex_Server extends Plex_MachineAbstract +{ + /** + * The default port on which a Plex server listens. + */ + const DEFAULT_PORT = 32400; + + /** + * The Plex HTTP API endpoint for client listing. + */ + const ENDPOINT_CLIENT = 'clients'; + + /** + * Sets up our Plex server using the minimum amount of data required to + * interact. + * + * @param string $name The name of the Plex server. + * @param string $address The IP address of the Plex server. + * @param integer $port The port on which the Plex server is listening. + * + * @uses Plex_MachineAbstract::$name + * @uses Plex_MachineAbstract::$address + * @uses Plex_MachineAbstract::$port + * @uses Plex_Server::DEFAULT_PORT + * + * @return void + */ + public function __construct($name, $address, $port) + { + $this->name = $name; + $this->address = $address; + $this->port = $port ? $port : self::DEFAULT_PORT; + } + + /** + * Returns all the availalble clients to which the Plex server has access + * inedxed by the Plex client name. + * + * @uses Plex_MachineAbstract::getBaseUrl() + * @uses Plex_MachineAbstract::makeCall() + * @uses Plex_MachineAbstract::xmlAttributesToArray() + * @uses Plex_Server::ENDPOINT_CLIENT + * @uses Plex_Client::setHost() + * @uses Plex_Client::setMachineIdentifier() + * @uses Plex_Client::setVersion() + * + * @return Plex_Client[] An array of Plex clients indexed by the Plex client + * name. + */ + public function getClients() + { + $url = sprintf( + '%s/%s', + $this->getBaseUrl(), + self::ENDPOINT_CLIENT + ); + + $clients = array(); + + $xml = $this->makeCall($url); + $attributes = $this->xmlAttributesToArray($xml->Server); + + foreach ($attributes as $attribute) { + $client = new Plex_Client( + $attribute['name'], + $attribute['address'], + (int) $attribute['port'] + ); + $client->setHost($attribute['host']); + $client->setMachineIdentifier($attribute['machineIdentifier']); + $client->setVersion($attribute['version']); + $clients[$attribute['name']] = $client; + } + + return $clients; + } + + /** + * Returns the Plex server's name. + * + * @uses Plex_MachineAbstract::$name + * + * @return string The name of the Plex server. + */ + public function getName() + { + return $this->name; + } + + /** + * Returns the Plex server's IP address. + * + * @uses Plex_MachineAbstract::$address + * + * @return string The IP address of the Plex server. + */ + public function getAddress() + { + return $this->address; + } + + /** + * Returns the port on which the Plex server listens. + * + * @uses Plex_MachineAbstract::$port + * + * @return integer The port on which the Plex client server. + */ + public function getPort() + { + return $this->port; + } +} diff --git a/Server/Library.php b/Server/Library.php new file mode 100644 index 0000000..211c734 --- /dev/null +++ b/Server/Library.php @@ -0,0 +1,57 @@ +setUrl( + sprintf( + '%s%s', + $this->getBaseUrl(), + self::LIBRARY_DIRECTORY + ) + ); + + } + + public function getSections() + { + $url = sprintf( + '%s/%s', + $this->getUrl(), + self::SECTION_DIRECTORY + ); + return $this->makeCall($url); + } + + public function getSectionByKey($key) + { + $url = sprintf( + '%s%s/%s', + $this->getUrl(), + self::SECTION_DIRECTORY, + $key + ); + + return $this->makeCall($url); + } + + private function setUrl($url) + { + $this->url = $url; + } + + public function getUrl() + { + return $this->url; + } +}