[pLog-CVS] plog_plugins/ploogle-xmlrpc IXR_Library.lib.php,NONE,1.1 xmlrpc.php,NONE,1.1
Brought to you by:
jondaley
From: Oscar R. <phu...@us...> - 2004-07-17 16:37:21
|
Update of /cvsroot/plog/plog_plugins/ploogle-xmlrpc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22821/ploogle-xmlrpc Added Files: IXR_Library.lib.php xmlrpc.php Log Message: xmlrpc interfaces to ploogle. Currently supported methods: pLoogle.rateSite pLoogle.search pLoogle.getSiteInfo pLoogle.getCategories --- NEW FILE: xmlrpc.php --- <?php include_once('IXR_Library.lib.php'); include_once("mainfile.php" ); /** * how many seconds between votes for the same site. Default is 3 minutes (180 seconds) * Change it if needed :) */ define( "DEFAULT_NUM_SECONDS_BETWEEN_VOTES", 180 ); /** * different error codes that can happen */ define( "ERROR_CODE_INVALID_LINK_ID", -100 ); define( "ERROR_MESSAGE_INVALID_LINK_ID", "Invalid link identifier" ); define( "ERROR_CODE_INVALID_RATING", -200 ); define( "ERROR_MESSAGE_INVALID_RATING", "Rating must be between 0 and 10" ); define( "ERROR_CODE_ERROR_ADDING_VOTE", -300 ); define( "ERROR_MESSAGE_ERROR_ADDING_VOTE", "There was an error storing the vote in the database!" ); define( "ERROR_CODE_SEARCH_NO_RESULTS", -400 ); define( "ERROR_MESSAGE_SEARCH_NO_RESULTS", "No matches" ); define( "ERROR_CODE_INVALID_QUERY", -500 ); define( "ERROR_MESSAGE_INVALID_QUERY", "Invalid query" ); define( "ERROR_CODE_MUST_WAIT_TIME", -600 ); define( "ERROR_MESSAGE_MUST_WAIT_TIME", "Sorry, you must wait at least ".DEFAULT_NUM_SECONDS_BETWEEN_VOTES." seconds to send another vote" ); define( "ERROR_CODE_NO_CATEGORIES_FOUND", -700 ); define( "ERROR_MESSAGE_NO_CATEGORIES_FOUND", "Sorry, no categories were found in the database" ); /** * all the different ways of sorting searches */ define( "ORDER_RESULTS_TITLE_ASC", 1 ); define( "ORDER_RESULTS_TITLE_DESC", 2 ); define( "ORDER_RESUTS_DATE_ASC", 3 ); define( "ORDER_RESUTS_DATE_DESC", 4 ); define( "ORDER_RESULTS_HITS_ASC", 5 ); define( "ORDER_RESULTS_HITS_DESC", 6 ); define( "ORDER_RESULTS_RATING_ASC", 7 ); define( "ORDER_RESULTS_RATING_DESC", 8 ); // needed for the code taken from phpnuke $module_name = "Web_Links"; /** * This function receives the ploogle.rateSite method and deals with it. * The parameters that are needed are the siteId and the rating, a value * between 0 and 10 * * This remote procedure needs 2 parameters: * - the link identifier: any integer higher than 0 * - the rating: any integer between 0 and 10 */ function rateSite( $args ) { // global values created by phpnuke somewhere in mainfile.php global $prefix, $db, $module_name; // get the values from the parameters $linkId = $args[0]; $rating = $args[1]; // check if the link id is correct if( $linkId < 0 ) return( new IXR_Error( ERROR_CODE_INVALID_LINK_ID, ERROR_MESSAGE_INVALID_LINK_ID )); // and if the rating is correct and between 0 and 10 if( $rating < 0 || $rating > 10 ) return( new IXR_Error( ERROR_CODE_INVALID_RATING, ERROR_MESSAGE_INVALID_RATING )); // now rate the link, for real :) include("modules/$module_name/l_config.php"); $ratinglid = $linkId; $ratinglid = intval(trim($ratinglid)); $ratinguser = "anonymous"; $results3 = $db->sql_query("SELECT title FROM ".$prefix."_links_links WHERE lid=$ratinglid"); // if the link id is invalid, then throw an error if( sql_num_rows($results3) == 0 ) { return( new IXR_Error( ERROR_CODE_INVALID_LINK_ID, ERROR_MESSAGE_INVALID_LINK_ID )); } while(list($title)=$db->sql_fetchrow($results3)) $ttitle = $title; // check if this site has already been votes less than DEFAULT_NUM_SECONDS_BETWEEN_VOTES ago /* Check if OUTSIDE user is trying to vote more than once per day. */ $result=$db->sql_query("select * FROM ".$prefix."_links_votedata WHERE ratinglid=$ratinglid AND ratinguser='outside' AND unix_timestamp(now())-unix_timestamp(ratingtimestamp) < ".DEFAULT_NUM_SECONDS_BETWEEN_VOTES.";"); $outsidevotecount = $db->sql_numrows($result); if ($outsidevotecount >= 1) { return( new IXR_Error( ERROR_CODE_MUST_WAIT_TIME, ERROR_MESSAGE_MUST_WAIT_TIME )); } /* All is well. Add to Line Item Rate to DB. */ $ratinglid = intval($ratinglid); $rating = intval($rating); $ratinguser = "outside"; $result = $db->sql_query("INSERT into ".$prefix."_links_votedata values (NULL,'$ratinglid', '$ratinguser', '$rating', '$ip', '$ratingcomments', now())"); if( !$result ) { return( new IXR_Error( ERROR_CODE_INVALID_LINK_ID, ERROR_MESSAGE_INVALID_LINK_ID )); } /* All is well. Calculate Score & Add to Summary (for quick retrieval & sorting) to DB. */ /* NOTE: If weight is modified, ALL links need to be refreshed with new weight. */ /* Running a SQL statement with your modded calc for ALL links will accomplish this. */ $voteresult = $db->sql_query("select rating, ratinguser, ratingcomments FROM ".$prefix."_links_votedata WHERE ratinglid = $ratinglid"); $totalvotesDB = $db->sql_numrows($voteresult); // // KLUDGE: stupid php nuke checks whether we're including a file from // index.php or not... I think it's stupid, but hey, this is how easily // we can fool this protection :P // $_SERVER["PHP_SELF"] = "index.php"; include ("modules/$module_name/voteinclude.php"); $lid = intval($lid); $db->sql_query("UPDATE ".$prefix."_links_links SET linkratingsummary=$finalrating,totalvotes=$totalvotesDB,totalcomments=$truecomments WHERE lid = $ratinglid"); // tell the client that everything went fine and return the current rating if needed $resultArray = Array( "message" => "OK", "current_rating" => $finalrating ); return( $resultArray ); } /** * generates the correct sql code, depending on how we would like the results sorted. * Adapted from modules/Web_Links/index.php * * ORDER_RESULTS_TITLE_ASC (1) * ORDER_RESULTS_TITLE_DESC (2) * ORDER_RESUTS_DATE_ASC (3) * ORDER_RESUTS_DATE_DESC (4) * ORDER_RESULTS_HITS_ASC (5) * ORDER_RESULTS_HITS_DESC (6) * ORDER_RESULTS_RATING_ASC (7) * ORDER_RESULTS_RATING_DESC (8) * */ function convertorderbyin($orderby) { if ($orderby == ORDER_RESULTS_TITLE_ASC ) $orderby = "title ASC"; if ($orderby == ORDER_RESUTS_DATE_ASC ) $orderby = "date ASC"; if ($orderby == ORDER_RESULTS_HITS_ASC ) $orderby = "hits ASC"; if ($orderby == ORDER_RESULTS_RATING_ASC ) $orderby = "linkratingsummary ASC"; if ($orderby == ORDER_RESULTS_TITLE_DESC ) $orderby = "title DESC"; if ($orderby == ORDER_RESUTS_DATE_DESC ) $orderby = "date DESC"; if ($orderby == ORDER_RESULTS_HITS_DESC ) $orderby = "hits DESC"; if ($orderby == ORDER_RESULTS_RATING_DESC ) $orderby = "linkratingsummary DESC"; return $orderby; } /** * returns the results of a search in ploogle * * This remote method takes the following parameters * - a query * - min * - orderby * - show */ function search( $args ) { global $prefix, $db, $module_name; // get the parameters we are going to need $query = $args[0]; $orderby = $args[1]; $max = $args[2]; // // KLUDGE: as above, we need to fool phpnuke into believing that we really // came from a script index.php // $_SERVER["PHP_SELF"] = "index.php"; include("modules/$module_name/l_config.php"); $orderby = convertorderbyin($orderby); $query = addslashes($query); $sqlQuery = "select lid, cid, sid, title, url, description, date, hits, linkratingsummary, totalvotes, totalcomments from ".$prefix."_links_links where title LIKE '%$query%' OR description LIKE '%$query%' ORDER BY $orderby"; if( $max > 0 ) $sqlQuery .= "LIMIT 0, $max"; $result = $db->sql_query( $sqlQuery ); $fullcountresult=$db->sql_query("select lid, title, description, date, hits, linkratingsummary, totalvotes, totalcomments from ".$prefix."_links_links where title LIKE '%$query%' OR description LIKE '%$query%'"); $totalselectedlinks = $db->sql_numrows($fullcountresult); $lid = intval(trim($lid)); $hits = intval(trim($hits)); $totalvotes = intval(trim($totalvotes)); $totalcomments = intval(trim($totalcomments)); $nrows = $db->sql_numrows($result); $x=0; // quit if things don't look good! if( $query == "" ) { return( new IXR_Error( ERROR_CODE_INVALID_QUERY, ERROR_MESSAGE_INVALID_QUERY )); } // array used to hold all the results $searchResults = Array(); while(list($lid, $cid, $sid, $title, $url, $description, $time, $hits, $linkratingsummary, $totalvotes, $totalcomments) = $db->sql_fetchrow($result)) { // information about the current result $link = Array(); $sid = intval(trim($sid)); $lid = intval(trim($lid)); $hits = intval(trim($hits)); $link["description"] = stripslashes($description); $link["title"] = stripslashes($title); $link["date_added"] = $time; $link["url"] = $url; $link["hits"] = $hits; $link["total_votes"] = intval(trim($totalvotes)); $link["average_rating"] = number_format($linkratingsummary, $mainvotedecimal); $link["category_id"] = intval(trim($cid)); $link["total_comments"] = intval(trim($totalcomments)); $link["link_id"] = $lid; // fetch the category $result3 = $db->sql_query("select cid,title,parentid from ".$prefix."_links_categories where cid=$cid"); list($cid3,$title3,$parentid3) = $db->sql_fetchrow($result3); $x++; $link["category"] = $title3; array_push( $searchResults, $link ); } return $searchResults; } /** * returns information about a site * * The only parameter needed is the side id (link id) */ function getSiteInfo( $params ) { global $db, $prefix; // fetch the parameter needed $linkId = $params[0]; // try to find the info, or throw an error if the link id does not exist $query = "SELECT * FROM {$prefix}_links_links WHERE lid=$linkId"; $result = $db->sql_query( $query ); if( $db->sql_numrows($result) == 0 ) { return( new IXR_Error( ERROR_CODE_INVALID_LINK_ID, ERROR_MESSAGE_INVALID_LINK_ID )); } // if everything correct, continue $return = Array(); $row = $db->sql_fetchrow($result); $return["link_id"] = $row["lid"]; $return["title"] = $row["title"]; $return["url"] = $row["url"]; $return["description"] = $row["description"]; $return["date_added"] = $row["date"]; $return["name"] = $row["name"]; $return["email"] = $row["email"]; $return["hits"] = $row["hits"]; $return["average_rating"] = $row["linkratingsummary"]; $return["total_votes"] = $row["totalvotes"]; $return["category_id"] = $row["cid"]; // fetch the category $cid = $row["cid"]; $result3 = $db->sql_query("select cid,title,parentid from ".$prefix."_links_categories where cid=$cid"); list($cid3,$title3,$parentid3) = $db->sql_fetchrow($result3); $return["category"] = $title3; // return the information we just collected return $return; } /** * returns a list with the categories available in ploogle * * no parameters are needed! */ function getCategories( $params ) { global $db, $prefix; $query = "SELECT * FROM {$prefix}_links_categories"; $result = $db->sql_query( $query ); // throw an error if no categories were found if( $db->sql_numrows($result) == 0 ) { return( new IXR_Error( ERROR_CODE_NO_CATEGORIES_FOUND, ERROR_MESSAGE_NO_CATEGORIES_FOUND )); } $categories = Array(); while( $row = $db->sql_fetchrow($result)) { $category = Array(); $category["category_id"] = $row["cid"]; $category["description"] = $row["cdescription"]; $category["title"] = $row["title"]; $category["parent_id"] = $row["parentid"]; array_push( $categories, $category ); } return $categories; } /* Create the server and map the XML-RPC method names to the relevant functions */ $server = new IXR_Server(array( "pLoogle.rateSite" => "rateSite", "pLoogle.search" => "search", "pLoogle.getSiteInfo" => "getSiteInfo", "pLoogle.getCategories" => "getCategories" )); ?> --- NEW FILE: IXR_Library.lib.php --- <?php /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Value { var $data; var $type; function IXR_Value ($data, $type = false) { $this->data = $data; if (!$type) { $type = $this->calculateType(); } $this->type = $type; if ($type == 'struct') { /* Turn all the values in the array in to new IXR_Value objects */ foreach ($this->data as $key => $value) { $this->data[$key] = new IXR_Value($value); } } if ($type == 'array') { for ($i = 0, $j = count($this->data); $i < $j; $i++) { $this->data[$i] = new IXR_Value($this->data[$i]); } } } function calculateType() { if ($this->data === true || $this->data === false) { return 'boolean'; } if (is_integer($this->data)) { return 'int'; } if (is_double($this->data)) { return 'double'; } // Deal with IXR object types base64 and date if (is_object($this->data) && is_a($this->data, 'IXR_Date')) { return 'date'; } if (is_object($this->data) && is_a($this->data, 'IXR_Base64')) { return 'base64'; } // If it is a normal PHP object convert it in to a struct if (is_object($this->data)) { $this->data = get_object_vars($this->data); return 'struct'; } if (!is_array($this->data)) { return 'string'; } /* We have an array - is it an array or a struct ? */ if ($this->isStruct($this->data)) { return 'struct'; } else { return 'array'; } } function getXml() { /* Return XML for this value */ switch ($this->type) { case 'boolean': return '<boolean>'.(($this->data) ? '1' : '0').'</boolean>'; break; case 'int': return '<int>'.$this->data.'</int>'; break; case 'double': return '<double>'.$this->data.'</double>'; break; case 'string': $val = $this->data; // -mhe encoding modified $val = str_replace('&', '&', $val); $val = str_replace("'", ''', $val); $val = str_replace('"', '"', $val); $val = str_replace('<', '<', $val); $val = str_replace('>', '>', $val); return '<string>'.$val.'</string>'; // return '<string>'.htmlentities($this->data).'</string>'; break; case 'array': $return = '<array><data>'."\n"; foreach ($this->data as $item) { $return .= ' <value>'.$item->getXml()."</value>\n"; } $return .= '</data></array>'; return $return; break; case 'struct': $return = '<struct>'."\n"; foreach ($this->data as $name => $value) { $return .= " <member><name>$name</name><value>"; $return .= $value->getXml()."</value></member>\n"; } $return .= '</struct>'; return $return; break; case 'date': case 'base64': return $this->data->getXml(); break; } return false; } function isStruct($array) { /* Nasty function to check if an array is a struct or not */ $expected = 0; foreach ($array as $key => $value) { if ((string)$key != (string)$expected) { return true; } $expected++; } return false; } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Message { var $message; var $messageType; // methodCall / methodResponse / fault var $faultCode; var $faultString; var $methodName; var $params; // Current variable stacks var $_arraystructs = array(); // The stack used to keep track of the current array/struct var $_arraystructstypes = array(); // Stack keeping track of if things are structs or array var $_currentStructName = array(); // A stack as well var $_param; var $_value; var $_currentTag; var $_currentTagContents; // The XML parser var $_parser; function IXR_Message ($message) { $this->message = $message; } function parse() { // first remove the XML declaration $this->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message); if (trim($this->message) == '') { return false; } $this->_parser = xml_parser_create(); // Set XML parser to take the case of tags in to account xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false); // Set XML parser callback functions xml_set_object($this->_parser, $this); xml_set_element_handler($this->_parser, 'tag_open', 'tag_close'); xml_set_character_data_handler($this->_parser, 'cdata'); if (!xml_parse($this->_parser, $this->message)) { /* die(sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($this->_parser)), xml_get_current_line_number($this->_parser))); */ return false; } xml_parser_free($this->_parser); // Grab the error messages, if any if ($this->messageType == 'fault') { $this->faultCode = $this->params[0]['faultCode']; $this->faultString = $this->params[0]['faultString']; } return true; } function tag_open($parser, $tag, $attr) { $this->currentTag = $tag; switch($tag) { case 'methodCall': case 'methodResponse': case 'fault': $this->messageType = $tag; break; /* Deal with stacks of arrays and structs */ case 'data': // data is to all intents and puposes more interesting than array $this->_arraystructstypes[] = 'array'; $this->_arraystructs[] = array(); break; case 'struct': $this->_arraystructstypes[] = 'struct'; $this->_arraystructs[] = array(); break; } } function cdata($parser, $cdata) { $this->_currentTagContents .= $cdata; } function tag_close($parser, $tag) { $valueFlag = false; switch($tag) { case 'int': case 'i4': $value = (int)trim($this->_currentTagContents); $this->_currentTagContents = ''; $valueFlag = true; break; case 'double': $value = (double)trim($this->_currentTagContents); $this->_currentTagContents = ''; $valueFlag = true; break; case 'string': $value = (string)$this->_currentTagContents; $this->_currentTagContents = ''; $valueFlag = true; break; case 'dateTime.iso8601': $value = new IXR_Date(trim($this->_currentTagContents)); // $value = $iso->getTimestamp(); $this->_currentTagContents = ''; $valueFlag = true; break; case 'value': // "If no type is indicated, the type is string." if (trim($this->_currentTagContents) != '') { $value = (string)$this->_currentTagContents; $this->_currentTagContents = ''; $valueFlag = true; } break; case 'boolean': $value = (boolean)trim($this->_currentTagContents); $this->_currentTagContents = ''; $valueFlag = true; break; case 'base64': $value = base64_decode($this->_currentTagContents); $this->_currentTagContents = ''; $valueFlag = true; break; /* Deal with stacks of arrays and structs */ case 'data': case 'struct': $value = array_pop($this->_arraystructs); array_pop($this->_arraystructstypes); $valueFlag = true; break; case 'member': array_pop($this->_currentStructName); break; case 'name': $this->_currentStructName[] = trim($this->_currentTagContents); $this->_currentTagContents = ''; break; case 'methodName': $this->methodName = trim($this->_currentTagContents); $this->_currentTagContents = ''; break; } if ($valueFlag) { if (!is_array($value)) { $value = trim($value); } if (count($this->_arraystructs) > 0) { // Add value to struct or array if ($this->_arraystructstypes[count($this->_arraystructstypes)-1] == 'struct') { // Add to struct $this->_arraystructs[count($this->_arraystructs)-1][$this->_currentStructName[count($this->_currentStructName)-1]] = $value; } else { // Add to array $this->_arraystructs[count($this->_arraystructs)-1][] = $value; } } else { // Just add as a paramater $this->params[] = $value; } } } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Server { var $data; var $callbacks = array(); var $message; var $capabilities; var $defencoding = "ISO-8859-1"; // -mhe added defencoding function IXR_Server($callbacks = false, $data = false) { $this->setCapabilities(); if ($callbacks) { $this->callbacks = $callbacks; } $this->setCallbacks(); $this->serve($data); } function serve($data = false) { if (!$data) { global $HTTP_RAW_POST_DATA; if (!$HTTP_RAW_POST_DATA) { die('XML-RPC server accepts POST requests only.'); } $data = $HTTP_RAW_POST_DATA; } $this->message = new IXR_Message($data); if (!$this->message->parse()) { $this->error(-32700, 'parse error. not well formed'); } if ($this->message->messageType != 'methodCall') { $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall'); } $result = $this->call($this->message->methodName, $this->message->params); // Is the result an error? if (is_a($result, 'IXR_Error')) { $this->error($result); } // Encode the result $r = new IXR_Value($result); $resultxml = $r->getXml(); // Create the XML $xml = <<<EOD <methodResponse> <params> <param> <value> $resultxml </value> </param> </params> </methodResponse> EOD; // Send it $this->output($xml); } function call($methodname, $args) { if (!$this->hasMethod($methodname)) { return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.'); } $method = $this->callbacks[$methodname]; // Perform the callback and send the response if (count($args) == 1) { // If only one paramater just send that instead of the whole array $args = $args[0]; } // Are we dealing with a function or a method? if (substr($method, 0, 5) == 'this:') { // It's a class method - check it exists $method = substr($method, 5); if (!method_exists($this, $method)) { return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.'); } // Call the method $result = $this->$method($args); } else { // It's a function - does it exist? if (!function_exists($method)) { return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.'); } // Call the function $result = $method($args); } return $result; } function error($error, $message = false) { // Accepts either an error object or an error code and message if ($message && !is_object($error)) { $error = new IXR_Error($error, $message); } $this->output($error->getXml()); } function output($xml) { // -mhe added defencoding for xml header $xml = '<?xml version="1.0" encoding="' . $this->defencoding . '"?>'."\n".$xml; $length = strlen($xml); header('Connection: close'); header('Content-Length: '.$length); header('Content-Type: text/xml'); header('Date: '.date('r')); echo $xml; exit; } function hasMethod($method) { return in_array($method, array_keys($this->callbacks)); } function setCapabilities() { // Initialises capabilities array $this->capabilities = array( 'xmlrpc' => array( 'specUrl' => 'http://www.xmlrpc.com/spec', 'specVersion' => 1 ), 'faults_interop' => array( 'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php', 'specVersion' => 20010516 ), 'system.multicall' => array( 'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208', 'specVersion' => 1 ), ); } function getCapabilities($args) { return $this->capabilities; } function setCallbacks() { $this->callbacks['system.getCapabilities'] = 'this:getCapabilities'; $this->callbacks['system.listMethods'] = 'this:listMethods'; $this->callbacks['system.multicall'] = 'this:multiCall'; } function listMethods($args) { // Returns a list of methods - uses array_reverse to ensure user defined // methods are listed before server defined methods return array_reverse(array_keys($this->callbacks)); } function multiCall($methodcalls) { // See http://www.xmlrpc.com/discuss/msgReader$1208 $return = array(); foreach ($methodcalls as $call) { $method = $call['methodName']; $params = $call['params']; if ($method == 'system.multicall') { $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden'); } else { $result = $this->call($method, $params); } if (is_a($result, 'IXR_Error')) { $return[] = array( 'faultCode' => $result->code, 'faultString' => $result->message ); } else { $return[] = array($result); } } return $return; } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Request { var $method; var $args; var $xml; function IXR_Request($method, $args) { $this->method = $method; $this->args = $args; $this->xml = <<<EOD <?xml version="1.0"?> <methodCall> <methodName>{$this->method}</methodName> <params> EOD; foreach ($this->args as $arg) { $this->xml .= '<param><value>'; $v = new IXR_Value($arg); $this->xml .= $v->getXml(); $this->xml .= "</value></param>\n"; } $this->xml .= '</params></methodCall>'; } function getLength() { return strlen($this->xml); } function getXml() { return $this->xml; } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Client { var $server; var $port; var $path; var $useragent; var $response; var $message = false; var $debug = false; // Storage place for an error message var $error = false; function IXR_Client($server, $path = false, $port = 80) { if (!$path) { // Assume we have been given a URL instead $bits = parse_url($server); $this->server = $bits['host']; $this->port = isset($bits['port']) ? $bits['port'] : 80; $this->path = isset($bits['path']) ? $bits['path'] : '/'; // Make absolutely sure we have a path if (!$this->path) { $this->path = '/'; } } else { $this->server = $server; $this->path = $path; $this->port = $port; } $this->useragent = 'The Incutio XML-RPC PHP Library'; } function query() { $args = func_get_args(); $method = array_shift($args); $request = new IXR_Request($method, $args); $length = $request->getLength(); $xml = $request->getXml(); $r = "\r\n"; $request = "POST {$this->path} HTTP/1.0$r"; $request .= "Host: {$this->server}$r"; $request .= "Content-Type: text/xml$r"; $request .= "User-Agent: {$this->useragent}$r"; $request .= "Content-length: {$length}$r$r"; $request .= $xml; // Now send the request if ($this->debug) { echo '<pre>'.htmlentities($request)."\n</pre>\n\n"; } $fp = fsockopen($this->server, $this->port); if (!$fp) { $this->error = new IXR_Error(-32300, 'transport error - could not open socket'); return false; } fputs($fp, $request); $contents = ''; $gotFirstLine = false; $gettingHeaders = true; while (!feof($fp)) { $line = fgets($fp, 4096); if (!$gotFirstLine) { // Check line for '200' if (strstr($line, '200') === false) { $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200'); return false; } $gotFirstLine = true; } if (trim($line) == '') { $gettingHeaders = false; } if (!$gettingHeaders) { $contents .= trim($line)."\n"; } } if ($this->debug) { echo '<pre>'.htmlentities($contents)."\n</pre>\n\n"; } // Now parse what we've got back $this->message = new IXR_Message($contents); if (!$this->message->parse()) { // XML error $this->error = new IXR_Error(-32700, 'parse error. not well formed'); return false; } // Is the message a fault? if ($this->message->messageType == 'fault') { $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString); return false; } // Message must be OK return true; } function getResponse() { // methodResponses can only have one param - return that return $this->message->params[0]; } function isError() { return (is_object($this->error)); } function getErrorCode() { return $this->error->code; } function getErrorMessage() { return $this->error->message; } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Error { var $code; var $message; function IXR_Error($code, $message) { $this->code = $code; $this->message = $message; } function getXml() { $xml = <<<EOD <methodResponse> <fault> <value> <struct> <member> <name>faultCode</name> <value><int>{$this->code}</int></value> </member> <member> <name>faultString</name> <value><string>{$this->message}</string></value> </member> </struct> </value> </fault> </methodResponse> EOD; return $xml; } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Date { var $year; var $month; var $day; var $hour; var $minute; var $second; function IXR_Date($time) { // $time can be a PHP timestamp or an ISO one if (is_int($time) == $time) { $this->parseTimestamp($time); } else { $this->parseIso($time); } } function parseTimestamp($timestamp) { // -mhe corrected bug with 'Y''Y''Y' to 'Y''m''d' $this->year = date('Y', $timestamp); $this->month = date('m', $timestamp); $this->day = date('d', $timestamp); $this->hour = date('H', $timestamp); $this->minute = date('i', $timestamp); $this->second = date('s', $timestamp); } function parseIso($iso) { $this->year = substr($iso, 0, 4); $this->month = substr($iso, 4, 2); $this->day = substr($iso, 6, 2); $this->hour = substr($iso, 9, 2); $this->minute = substr($iso, 12, 2); $this->second = substr($iso, 15, 2); } function getIso() { return $this->year.$this->month.$this->day.'T'.$this->hour.':'.$this->minute.':'.$this->second; } function getXml() { return '<dateTime.iso8601>'.$this->getIso().'</dateTime.iso8601>'; } function getTimestamp() { return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year); } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_Base64 { var $data; function IXR_Base64($data) { $this->data = $data; } function getXml() { return '<base64>'.base64_encode($this->data).'</base64>'; } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_IntrospectionServer extends IXR_Server { var $signatures; var $help; function IXR_IntrospectionServer() { $this->setCallbacks(); $this->setCapabilities(); $this->capabilities['introspection'] = array( 'specUrl' => 'http://xmlrpc.usefulinc.com/doc/reserved.html', 'specVersion' => 1 ); $this->addCallback( 'system.methodSignature', 'this:methodSignature', array('array', 'string'), 'Returns an array describing the return type and required parameters of a method' ); $this->addCallback( 'system.getCapabilities', 'this:getCapabilities', array('struct'), 'Returns a struct describing the XML-RPC specifications supported by this server' ); $this->addCallback( 'system.listMethods', 'this:listMethods', array('array'), 'Returns an array of available methods on this server' ); $this->addCallback( 'system.methodHelp', 'this:methodHelp', array('string', 'string'), 'Returns a documentation string for the specified method' ); } function addCallback($method, $callback, $args, $help) { $this->callbacks[$method] = $callback; $this->signatures[$method] = $args; $this->help[$method] = $help; } function call($methodname, $args) { // Make sure it's in an array if ($args && !is_array($args)) { $args = array($args); } // Over-rides default call method, adds signature check if (!$this->hasMethod($methodname)) { return new IXR_Error(-32601, 'server error. requested method "'.$this->message->methodName.'" not specified.'); } $method = $this->callbacks[$methodname]; $signature = $this->signatures[$methodname]; $returnType = array_shift($signature); // Check the number of arguments if (count($args) != count($signature)) { // print 'Num of args: '.count($args).' Num in signature: '.count($signature); return new IXR_Error(-32602, 'server error. wrong number of method parameters'); } // Check the argument types $ok = true; $argsbackup = $args; for ($i = 0, $j = count($args); $i < $j; $i++) { $arg = array_shift($args); $type = array_shift($signature); switch ($type) { case 'int': case 'i4': if (is_array($arg) || !is_int($arg)) { $ok = false; } break; case 'base64': case 'string': if (!is_string($arg)) { $ok = false; } break; case 'boolean': if ($arg !== false && $arg !== true) { $ok = false; } break; case 'float': case 'double': if (!is_float($arg)) { $ok = false; } break; case 'date': case 'dateTime.iso8601': if (!is_a($arg, 'IXR_Date')) { $ok = false; } break; } if (!$ok) { return new IXR_Error(-32602, 'server error. invalid method parameters'); } } // It passed the test - run the "real" method call return parent::call($methodname, $argsbackup); } function methodSignature($method) { if (!$this->hasMethod($method)) { return new IXR_Error(-32601, 'server error. requested method "'.$method.'" not specified.'); } // We should be returning an array of types $types = $this->signatures[$method]; $return = array(); foreach ($types as $type) { switch ($type) { case 'string': $return[] = 'string'; break; case 'int': case 'i4': $return[] = 42; break; case 'double': $return[] = 3.1415; break; case 'dateTime.iso8601': $return[] = new IXR_Date(time()); break; case 'boolean': $return[] = true; break; case 'base64': $return[] = new IXR_Base64('base64'); break; case 'array': $return[] = array('array'); break; case 'struct': $return[] = array('struct' => 'struct'); break; } } return $return; } function methodHelp($method) { return $this->help[$method]; } } /** * IXR - The Inutio XML-RPC Library - (c) Incutio Ltd 2002 * Beta 1.5 - Simon Willison, September 4th 2002 * Site: http://scripts.incutio.com/xmlrpc/ * Manual: http://scripts.incutio.com/xmlrpc/manual.php * Forum: http://forums.incutio.com/viewforum.php?forum=ixr * Made available under the Artistic License: http://www.opensource.org/licenses/artistic-license.php * * Modified by Oscar 05-06-2003 to make it inherit from the Object object */ class IXR_ClientMulticall extends IXR_Client { var $calls = array(); function IXR_ClientMulticall($server, $path = false, $port = 80) { parent::IXR_Client($server, $path, $port); $this->useragent = 'The Incutio XML-RPC PHP Library (multicall client)'; } function addCall() { $args = func_get_args(); $methodName = array_shift($args); $struct = array( 'methodName' => $methodName, 'params' => $args ); $this->calls[] = $struct; } function query() { // Prepare multicall, then call the parent::query() method return parent::query('system.multicall', $this->calls); } } ?> |