Source for file Server.php
Documentation is available at Server.php
* Object-oriented CDDB Server components
* Alpha-quality CDDB server capable of serving CDDB requests over a variety of
* CDDB protocol options (HTTP only for now, more coming soon) and using a
* variety of data sources.
* @author Keith Palmer <Keith@UglySlug.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* Need the constants from this file...
require_once 'Net/CDDB.php';
* A hook that runs when the server receives the request
* The function signature for request hooks should look like this:
* function your_request_hook(&$arr_a_list_of_cddb_commands_passed_in_the_request)
* // return true; (if you want the CDDB server to not run any other request hooks)
* // return false; (if you want the CDDB server to continue to run the other request hooks)
define('NET_CDDB_SERVER_HOOK_REQUEST', 'request');
* A hook that runs whenever the server responds to a request
* The function signature for response hooks should look like this:
* funtion your_response_hook($cmd, &$int_response_code, &$str_response_message, &$str_any_data_returned_with_request, &$bln_whether_or_not_to_add_a_period_as_a_termination_character)
* // return true; (if you want the CDDB server to not run any other response hooks)
* // return false; (if you want the CDDB server to continue to run the other response hooks)
define('NET_CDDB_SERVER_HOOK_RESPONSE', 'response');
* A hook that runs for each command the CDDB server recieves
* The function signature for response hooks should look like this:
* function your_command_hook(&$str_the_cddb_command)
* // return true; (if you want the CDDB server to not run any other command hooks)
* // return false; (if you want the CDDB server to continue to run the other command hooks)
define('NET_CDDB_SERVER_HOOK_COMMAND', 'command');
* CDDB Server (ALPHA QUALITY, *USE AT YOUR OWN RISK!*)
* Use at your own risk, it doesn't work very well yet!
* - CDDB protocol level 5
* - Multiple data sources: tell the server to read from a local filesystem, a remote HTTP CDDB server, a remote CDDBP server, a database (coming soon) etc.
* - Supports about half of the CDDB commands
* - Write and register your own 'hooks' which run when the CDDB server responds to commands
* - Extremelly lightly tested (use at your own risk!)
* - Command: 'motd' reports and incorrect last modified date
* - Lack of any error reporting...
* - Lack of any login/hello command support
* - Only supports protocol level 5
* - Only provides HTTP request support (no CDDBP or SMTP... yet)
* You can write your own custom 'hooks' that get run a certain stages of the
* CDDB request/response process and register them with the ->registerHook()
* method. You can register multiple hooks at each stage of the process, and
* they will get run in the order you register them in.
* Hooks should receive passed parameters by reference if they intend to modify
* commands/requests/responses. Hooks should return either a four element array
* which will be treated as the response (as below) or void if the server should
* continue to process the response.
* $my_custom_response = array(
* NET_CDDB_RESPONSE_OK, // This could actually be *any* valid response code
* 'status message to be returned with the response code',
* 'any data the response returns (a cddb record, a list of matching records, a list of mirror sites, etc.)',
* true, // true if a '.' should be appended to the response as a terminating character, false otherwise
* @todo Probably have the Net_CDDB_Request_* classes have three methods: getHello(), getProto(), and getCmd() or something
* Net_CDDB_Request_* object instance to use for listening for requests
* Net_CDDB_Protocol_* object instance to use for data access
* An array of function names to call as request hooks
* An array of function names to call as command hooks
* An arra of function names to call as response hooks
* @see Net_CDDB_Server::__construct()
$this->__construct($request_dsn, $protocol_dsn, $options);
* @param string $request_dsn
* @param string $protocol_dsn
function __construct($request_dsn, $protocol_dsn, $options = array ())
* Create a Net_CDDB_Request object instance
* @return Net_CDDB_Request Returns an instance of a subclass of Net_CDDB_Request (i.e: Net_CDDB_Request_HTTP instance)
$class = 'Net_CDDB_Request_' . $file;
* Require the file the protocol class is stored in
include_once 'Net/CDDB/Request/' . $file . '.php';
return new $class($dsn, $options);
return PEAR ::raiseError ('Could not find request file for: ' . $file);
* @todo Listen forever support (daemon mode)
'cddb lscat' => '_cddbLscat',
'cddb hello' => '_cddbHello',
'cddb query' => '_cddbQuery',
'cddb read' => '_cddbRead',
// Read data from request listener
foreach ($commands as $cmd)
// Parse command from command parameters
$cmd = 'cddb ' . next($tmp);
$message = 'Empty command input.';
$this->_request->respond ($status, $message);
} else if (isset ($impl_cmds[$cmd]) and method_exists($this, $impl_cmds[$cmd])) {
// Call the appropriate command handler, and then respond
if ($this->{$impl_cmds[$cmd]}($cmd, $query, $status, $message, $data, $term)) {
$this->_request->respond ($status, $message, $data, $term);
// Respond indicating the command could not be found
$message = 'Unrecognized command.';
$this->_request->respond ($status, $message); // Unrecognized command...?
// listen forever, not implemented yet
* Register a hook function
* See the example file or the class description for more details. Custom
* hooks can be used to hook into CDDB server requests and implement custom
* @param function $function
* Execute the command hooks
if ($arr = $hook($cmd) and is_array($arr) and count($arr) == 4 ) {
// We still need to run the response hooks though...
$this->_request->respond ($arr[0 ], $arr[1 ], $arr[2 ], $arr[3 ]);
* Execute the response hooks
if ($arr = $hook($cmd, $status, $message, $data, $term) and is_array($arr) and count($arr) == 4 ) {
$this->_request->respond ($arr[0 ], $arr[1 ], $arr[2 ], $arr[3 ]);
* Execute the request hooks
if ($arr = $hook($arr_cmds) and is_array($arr) and count($arr) == 4 ) {
$this->_request->respond ($arr[0 ], $arr[1 ], $arr[2 ], $arr[3 ]);
* Dummy placeholder for the 'cddb proto' command
* Dummy placeholder for 'cddb hello' command
* Respond to the 'cddb lscat' CDDB command (list categories)
function _cddbLscat($cmd, $query, &$status, &$message, &$data, &$term)
$message = $headers[$status];
$message = 'Internal server error.';
* Respond to the 'cddb read ...' CDDB command
function _cddbRead($cmd, $query, &$status, &$message, &$data, &$term)
$message = $headers[$status];
$message = 'Internal server error.';
* Respond to the 'cddb query ...' CDDB command
function _cddbQuery($cmd, $query, &$status, &$message, &$data, &$term)
$message = 'Internal server error.';
* Respond to the CDDB 'motd' command
* @todo Find out the motd modified time somehow...?
function _motd($cmd, $query, &$status, &$message, &$data, &$term)
$message = $headers[$status];
$message = 'Internal server error.';
* Respond to the CDDB 'ver' command
function _ver($cmd, $query, &$status, &$message, &$data, &$term)
* If the protocol we're using to get the data is a remote protocol
* (i.e.: We're reading this data from a remote CDDB server via
* HTTP/CDDBP instead of using a local copy of the CDDB db) then we'll
* modify the version information to reflect the tunnelling of the
* response through the Net_CDDB/PHP server.
$messsage = 'Internal server error.';
function _stat ($cmd, $query, &$status, &$message, &$data, &$term)
// Correct the interface stat header
$explode = explode("\n", $resp_body);
foreach ($explode as $num => $line) {
$explode[$num] = ' interface: ' . $this->_request->face ();
$resp_body = implode("\n", $explode);
$message = 'OK, status information follows (until terminating `.\')';
$data = trim($resp_body);
$message = 'Internal server error.';
Documentation generated on Sun, 25 Feb 2007 14:00:20 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|