cvs: pear /Services_Trackback generate_package_xml.php package.xml package2.xml /Services_Trackback/Services Trackback.php /Services_Trackback/test
Services_Trackback_Test.php simple_test.php
| From: | Tobias Schlitt | Date: | Fri, 11 Mar 2005 14:58:57 +0000 |
| Subject: | cvs: pear /Services_Trackback generate_package_xml.php package.xml package2.xml /Services_Trackback/Services Trackback.php /Services_Trackback/test Services_Trackback_Test.php simple_test.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-30196@lists.php.net to get a copy of this message | ||
toby Fri Mar 11 09:58:57 2005 EDT
Added files:
/pear/Services_Trackback generate_package_xml.php package.xml
package2.xml
/pear/Services_Trackback/Services Trackback.php
/pear/Services_Trackback/test Services_Trackback_Test.php
simple_test.php
Log:
* Initial checkin.
http://cvs.php.net/co.php/pear/Services_Trackback/generate_package_xml.php?r=1.1&p=1 Index: pear/Services_Trackback/generate_package_xml.php +++ pear/Services_Trackback/generate_package_xml.php #!/usr/bin/php <?php $make = 1; require_once('PEAR/PackageFileManager.php'); $pkg = new PEAR_PackageFileManager; // directory that PEAR CVS is located in $cvsdir = '/cvs/pear/'; $packagedir = $cvsdir . 'Services_Trackback/'; // Filemanager settings $category = 'Services'; $package = 'Services_Trackback'; $version = '0.4.0'; $state = 'alpha'; $summary = 'Trackback - A generic class for sending and receiving trackbacks.'; $description = <<<EOT A generic class for sending and receiving trackbacks. EOT; $notes = <<<EOT * Initial release on PEARWeb. EOT; $e = $pkg->setOptions( array('simpleoutput' => true, 'baseinstalldir' => '', 'summary' => $summary, 'description' => $description, 'version' => $version, 'packagedirectory' => $packagedir, 'pathtopackagefile' => $packagedir, 'state' => $state, 'filelistgenerator' => 'file', 'notes' => $notes, 'package' => $package, 'dir_roles' => array( 'test' => 'test'), 'ignore' => array('package.xml', 'package2.xml', 'CHANGES.txt', 'doc*', 'generate_package_xml.php', '*.tgz', 'local_test.php'), )); if (PEAR::isError($e)) { echo $e->getMessage(); exit; } $e = $pkg->addMaintainer('toby', 'lead', 'Tobias Schlitt', 'toby@php.net'); if (PEAR::isError($e)) { echo $e->getMessage(); exit; } $e = $pkg->addDependency('HTTP_Request', '1.2.4', 'ge', 'pkg'); if (PEAR::isError($e)) { echo $e->getMessage(); exit; } // hack until they get their shit in line with docroot role $pkg->addRole('tpl', 'php'); $pkg->addRole('png', 'php'); $pkg->addRole('gif', 'php'); $pkg->addRole('jpg', 'php'); $pkg->addRole('css', 'php'); $pkg->addRole('js', 'php'); $pkg->addRole('ini', 'php'); $pkg->addRole('inc', 'php'); $pkg->addRole('afm', 'php'); $pkg->addRole('pkg', 'doc'); $pkg->addRole('cls', 'doc'); $pkg->addRole('proc', 'doc'); $pkg->addRole('sh', 'doc'); if (isset($make)) { $e = $pkg->writePackageFile(); } else { $e = $pkg->debugPackageFile(); } if (PEAR::isError($e)) { echo $e->getMessage(); } if (!isset($make)) { echo '<a href="' . $_SERVER['PHP_SELF'] . '?make=1">Make this file</a>'; } ?> http://cvs.php.net/co.php/pear/Services_Trackback/package.xml?r=1.1&p=1 Index: pear/Services_Trackback/package.xml +++ pear/Services_Trackback/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0" packagerversion="1.4.0a4"> <name>Services_Trackback</name> <summary>Trackback - A generic class for sending and receiving trackbacks.</summary> <description>A generic class for sending and receiving trackbacks. </description> <maintainers> <maintainer> <user>toby</user> <name>Tobias Schlitt</name> <email>toby@php.net</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.4.0</version> <date>2005-03-11</date> <license>PHP License</license> <state>alpha</state> <notes>* Initial release on PEARWeb. </notes> <deps> <dep type="pkg" rel="ge" version="1.2.4" optional="no">HTTP_Request</dep> </deps> <filelist> <dir name="/" baseinstalldir=""> <dir name="Services"> <file role="php" name="Trackback.php"/> </dir> <!-- /Services --> <dir name="test"> <file role="test" name="Services_Trackback_Test.php"/> <file role="test" name="simple_test.php"/> </dir> <!-- /test --> </dir> <!-- / --> </filelist> </release> <changelog> <release> <version>0.2.0</version> <date>2005-02-25</date> <license>PHP License</license> <state>devel</state> <notes>* Removed overloading. * Refactored. * Added autodiscver(). * Added PEAR 1.4 conform package2.xml (package.xml version 2.0). </notes> </release> <release> <version>0.3.0</version> <date>2005-02-26</date> <license>PHP License</license> <state>devel</state> <notes>* Refactored. * Added send(). * Added dependency to HTTP_Request for the send() method. </notes> </release> <release> <version>0.4.0</version> <date>2005-03-11</date> <license>PHP License</license> <state>alpha</state> <notes>* Initial release on PEARWeb. </notes> </release> </changelog> </package> http://cvs.php.net/co.php/pear/Services_Trackback/package2.xml?r=1.1&p=1 Index: pear/Services_Trackback/package2.xml +++ pear/Services_Trackback/package2.xml <?xml version="1.0"?> <package packagerversion="1.4.0a4" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Services_Trackback</name> <channel>pear.php.net</channel> <summary>Trackback - A generic class for sending and receiving trackbacks.</summary> <description>A generic class for sending and receiving trackbacks. </description> <lead> <name>Tobias Schlitt</name> <user>toby</user> <email>toby@php.net</email> <active>yes</active> </lead> <date>2005-03-11</date> <time>15:53:32</time> <version> <release>0.4.0</release> <api>0.4.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://www.php.net/license">PHP License</license> <notes>* Initial release on PEARWeb. </notes> <contents> <dir name="/"> <dir name="Services"> <file baseinstalldir="" name="Trackback.php" role="php"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> </dir> <!-- /Services --> <dir name="test"> <file baseinstalldir="" name="Services_Trackback_Test.php" role="test"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> <file baseinstalldir="" name="simple_test.php" role="test"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> </dir> <!-- /test --> </dir> <!-- / --> </contents> <dependencies> <required> <php> <min>4.0.0</min> <max>6.0.0</max> </php> <pearinstaller> <min>1.4.0a1</min> </pearinstaller> <package> <name>HTTP_Request</name> <channel>pear.php.net</channel> <min>1.2.4</min> </package> </required> </dependencies> <phprelease /> <changelog> <release> <version> <release>0.2.0</release> <api>0.2.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2005-02-25</date> <license uri="http://www.php.net/license">PHP License</license> <notes>* Removed overloading. * Refactored. * Added autodiscver(). * Added PEAR 1.4 conform package2.xml (package.xml version 2.0). </notes> </release> <release> <version> <release>0.3.0</release> <api>0.3.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2005-02-26</date> <license uri="http://www.php.net/license">PHP License</license> <notes>* Refactored. * Added send(). * Added dependency to HTTP_Request for the send() method. </notes> </release> <release> <version> <release>0.4.0</release> <api>0.4.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2005-03-11</date> <license uri="http://www.php.net/license">PHP License</license> <notes>* Initial release on PEARWeb. </notes> </release> </changelog> </package> http://cvs.php.net/co.php/pear/Services_Trackback/Services/Trackback.php?r=1.1&p=1 Index: pear/Services_Trackback/Services/Trackback.php +++ pear/Services_Trackback/Services/Trackback.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Services_Trackback. * * This is the main file of the Services_Trackback package. This file has to be * included for usage of Services_Trackback. * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Webservices * @package Trackback * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Trackback.php,v 1.1 2005/03/11 14:58:57 toby Exp $ * @link http://pear.php.net/package/Services_Trackback * @since File available since Release 0.0.1 */ // {{{ require_once /** * Load PEAR error handling */ require_once 'PEAR.php'; /** * PEAR::HTTP_Request is required only when needed. * @see Services_Trackback::send(). */ // require_once 'HTTP/Request.php'; // }}} // {{{ Constants /** * This constant is used with the @see Services_Trackback::autodiscover() method. * Using this constant you supress the URL check described in the trackback specs. */ define('SERVICES_TRACKBACK_STRICTNESS_LOW', 1); /** * This constant is used with the @see Services_Trackback::autodiscover() method. * Using this constant you use a not so strict URL check than described in the * trackback specs. Only the domain name is checked. */ define('SERVICES_TRACKBACK_STRICTNESS_MIDDLE', 2); /** * This constant is used with the @see Services_Trackback::autodiscover() method. * Using this constant activate the URL check described in the trackback specs. */ define('SERVICES_TRACKBACK_STRICTNESS_HIGH', 3); // }}} /** * Trackback * A generic class to send/receive trackbacks. * * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @category Webservices * @package Trackback * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @version Release: @package_version@ * @link http://pear.php.net/package/Services_Trackback * @since 0.0.1 * @access public */ class Services_Trackback { // {{{ var $_data /** * The necessary trackback data. * * @var array * @access private */ var $_data = array( 'id' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'url' => '', 'trackback_url' => '', ); // }}} // {{{ var $_options /** * Options to influence Services_Trackback. * * @see Services_Trackback::create() * @since 0.4.0 * @var array * @access private */ var $_options = array( 'strictness' => SERVICES_TRACKBACK_STRICTNESS_LOW, 'timeout' => 30, // seconds 'allowRedirects' => true, 'maxRedirects' => 2, 'fetchlines' => 30, 'useragent' => 'PEAR::Services_Trackback v@package_version@' ); // }}} // {{{ Services_Trackback() /** * Constructor * Creates a new Trackback object. Private because of factory use. * * @since 0.1 * @access private * @return void */ function Services_Trackback ( ) { } // }}} // {{{ create() /** * Factory * This static method is used to create a trackback object. (Services_Trackback::create($data)) * The factory requires a data array as described below for creation. The 'id' key is * obligatory for this method. Every other data is not quite necessary for the creation, but * might be necessary for calling other methods afterwards. See the specific methods for * further info on which data is required. * * @since 0.2 * @static * @access public * @param array $data Data for the trackback, which is obligatory: * 'id' The ID of the trackback target. * Data which is optional (for construction, not for specific methods): * 'title' string Title of the trackback sending/receiving blog entry. * 'excerpt' string Abstract of the trackback sending/receiving blog entry. * 'blog_name' string Name of the trackback sending/receiving weblog. * 'url' string URL of the trackback sending/receiving blog entry. * 'trackback_url' string URL to send trackbacks to. * @param array $options Options to set for this trackback. Valid options: * 'strictness': int The default strictness to use in @see Services_Trackback::autodiscover(). * 'timeout': int The default timeout for network operations in seconds. * 'allowRedirects': bool Wether to follow HTTP redirects or not. * 'maxRedirects': int Maximum number of redirects. * 'fetchsize': int The max file size to fetch over the network. * 'useragent': string The user agent to use for HTTP requests. * * @return object(Services_Trackback) The newly created Trackback. */ function &create($data, $options = null) { $options = (isset($options)) ? $options : array(); $trackback = new Services_Trackback(); $res = $trackback->_fromArray($data); if (PEAR::isError($res)) { return $res; } $res = $trackback->setOptions($options); if (PEAR::isError($res)) { return $res; } return $trackback; } // }}} // {{{ setOptions() /** * setOptions * Set options for the trackback. * * @since 0.4.0 * @access public * @see Services_Trackback::create() * @see Services_Trackback::getOptions() * @param array $options Pairs of 'option' => 'value' as described at @see Services_Trackback::create(). * @return mixed Bool true on success, otherwise PEAR::Error(). */ function setOptions($options) { foreach($options as $option => $value) { if (!isset($this->_options[$option])) { return PEAR::raiseError('Desired option "'.$option.'" not found.'); } switch ($option) { case 'strictness': if (!is_int($value) || ($value < 1) || ($value > 3)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'timeout': if (!is_int($value) || ($value < 0)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'allowRedirects': if (!is_bool($value)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'maxRedirects': if (!is_int($value) || ($value < 0)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'fetchsize': if (!is_int($value) || ($value < 1)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; } $this->_options[$option] = $value; } return true; } // }}} // {{{ getOptions() /** * getOptions * Get the currently set option set. * * @since 0.4.0 * @access public * @see Services_Trackback::setOptions() * @see Services_Trackback::create() * @return array The currently active options. */ function getOptions() { return $this->_options; } // }}} // {{{ autodiscover() /** * autodiscover * Checks a given URL for trackback autodiscovery code. This is another factory method. * * @since 0.2.0 * @access public * @return mixed Services_Trackback object on success, otherwise PEAR::Error. */ function autodiscover() { $necessaryData = array('url'); $res = $this->_checkData($necessaryData); if (PEAR::isError($res)) { return $res; } $url = $this->_data['url']; /* Sample autodiscovery code <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://pear.php.net/package/Net_FTP" dc:identifier="http://pear.php.net/package/Net_FTP" dc:title="Net_FTP" trackback:ping="http://pear.php.net/trackback/trackback.php?id=Net_FTP" /> </rdf:RDF> */ // Receive file contents $content = $this->_getContent($url); if (PEAR::isError($content)) { return $content; } // Get trackback identifier if (!preg_match('@dc:identifier\s*=\s*["\'](http:[^"\']+)"@i', $content, $matches)) { return PEAR::raiseError('No trackback RDF found in "'.$url.'".'); } $identifier = trim($matches[1]); // Get trackback URI if (!preg_match('@trackback:ping\s*=\s*["\'](http:[^"\']+)"@i', $content, $matches)) { return PEAR::raiseError('No trackback URI found in "'.$url.'".'); } $trackbackUrl = trim($matches[1]); // Check if the URL to trackback matches the identifier from the autodiscovery code $res = $this->_checkURLs($url, $identifier, $strictness); if (PEAR::isError($res)) { return $res; } $this->_data['trackback_url'] = $trackbackUrl; return true; } // }}} // {{{ send() /** * send * This method sends a trackback to the trackback_url saved in it. The * data array of the trackback object can be completed by submitting the * necessary data through the $data parameter of this method. * The following data has to be set to call this method: * 'title' Title of the weblog entry sending the trackback. * 'url' URL of the weblog entry sending the trackback. * 'excerpt' Excerpt of the weblog entry sending the trackback. * 'blog_name' Name of the weblog sending the trackback. * 'trackback_url' URL to send the trackback to. * Services_Trackback::send() requires HTTP_Request. * * @since 0.3.0 * @access public * @param string $data Additional data to complete the trackback. * @return mixed True on success, otherwise PEAR::Error. */ function send($data = null) { // Load HTTP_Request @require_once 'HTTP/Request.php'; if (!class_exists('HTTP_Request')) { return PEAR::raiseError('Unable to load PEAR::HTTP_Request'); } // Consistancy check if (!isset($data)) $data = array(); $this->_data = array_merge($this->_data, $data); $necessaryData = array('title', 'url', 'excerpt', 'blog_name', 'trackback_url'); $res = $this->_checkData($necessaryData); if (PEAR::isError($res)) return $res; // Get URL $url = str_replace('&', '&', $this->_data['trackback_url']); // Create new HTTP_Request $req = new HTTP_Request($url, array( 'timeout' => $this->_options['timeout'], 'allowRedirects' => $this->_options['allowRedirects'], 'maxRedirects' => $this->_options['maxRedirects'], )); $req->setMethod(HTTP_REQUEST_METHOD_POST); // Add HTTP headers $req->addHeader("User-Agent", $this->options['useragent']); // Adding data to send $req->addPostData('url', $this->_data['url']); $req->addPostData('title', $this->_data['title']); $req->addPostData('blog_name', $this->_data['blog_name']); $req->addPostData('excerpt', strip_tags($this->_data['excerpt'])); // Send POST request $res = $req->sendRequest(); if (PEAR::isError($res)) { return $res; } // Check return code if ($req->getResponseCode() != 200) { return PEAR::raiseError('Host returned Error '.$req->getRequestCode().'.'); } return $this->_interpretTrackbackResponse($req->getResponseBody()); } // }}} // {{{ getAutodiscoveryCode() /** * getAutodiscoverCode * Returns the RDF Code for a given website to let weblogs autodiscover * the possibility of tracking it back. * The following data has to be set to call this method: * 'id' * 'title' * 'url' * 'trackback_url' * @since 0.1.0 * @access public * @param bool $comments Whether to include HTML comments around the RDF using <!-- --> * @return string RDF code */ function getAutodiscoveryCode($comments = true) { $necessaryData = array('title', 'url', 'trackback_url'); $res = $this->_checkData($necessaryData); if (PEAR::isError($res)) { return $res; } $data = $this->_getEncodedData($necessaryData); $res = <<<EOD <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="%s" dc:identifier="%s" dc:title="%s" trackback:ping="%s" /> </rdf:RDF> EOD; $res = sprintf($res, $data['url'], $data['url'], $data['title'], $data['trackback_url']); if ($comments) { return "<!--\n".$res."\n-->\n"; } else { return $res."\n"; } } // }}} // {{{ receive() /** * receive * Receives a trackback. The following data has to be set in the data array to fulfill this * operation: * 'id' * * @since 0.1.0 * @access public * @return object Services_Trackback */ function receive($data = null) { if (!isset($data)) { $data = $_POST; } $necessaryPostData = array('title', 'excerpt', 'url', 'blog_name'); $res = $this->_checkData(array('id')); if (PEAR::isError($res)) { return $res; } $res = $this->_checkData($necessaryPostData, $data); if (PEAR::isError($res)) { return PEAR::raiseError('POST data incomplete: '.$res->getMessage()); } $this->_data = array_merge($this->_data, $this->_getDecodedData($necessaryPostData, $data)); return true; } // }}} // {{{ getResponseSuccess() /** * getResponseSuccess * Returns an XML response for a successful trackback. * * @since 0.1.0 * @access public * @static * @return string The XML code */ function getResponseSuccess () { return <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>0</error> </response> EOD; } // }}} // {{{ getResponseError() /** * getResponseError * Returns an XML response for a trackback error. * * @since 0.1.0 * @access public * @param int $code The error code * @param string $message The error message * @return void */ function getResponseError($message, $code) { $data = Services_Trackback::_getEncodedData(array('code', 'message'), array('code' => $code, 'message' => $message)); $res = <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>%s</error> <message>%s</message> </response> EOD; return sprintf($res, $data['code'], $data['message']); } // }}} // {{{ get() /** * get * Get data from the trackback. Returns the value of a given * key or PEAR::Error. * * @since 0.2.0 * @access public * @param string $key The key to fetch a value for. * @return mixed A string value or a PEAR::Error on failure. */ function get($key) { return (isset($this->_data[$key])) ? $this->_data[$key] : PEAR::raiseError('Key '.$key.' not found.'); } // }}} // {{{ set() /** * set * Set data of the trackback. Saves the value of a given * key , returning true on success, PEAR::Error on faulure. * * @since 0.2.0 * @access public * @param string $key The key to set a value for. * @param string $val The value for the key. * @return mixed Boolean true on success or a PEAR::Error on failure. */ function set($key, $val) { $this->_data[$key] = $val; return true; } // }}} // {{{ _fromArray() /** * Create a Trackback from a $data array. * * @since 0.2.0 * @access private * @param array $data The data array (@see Services_Trackback::create()). * @return mixed True on success, otherwise PEAR::Error. */ function _fromArray($data) { $res = $this->_checkData(array('id'), $data); if (PEAR::isError($res)) { return $res; } $this->_data = $data; return true; } // }}} // {{{ _getContent() /** * _getContent * Receive the content from a specific URL. * * @since 0.4.0 * @access private * @param string $url The URL to download data from. * @return string The content. */ function _getContent($url) { $handle = fopen($url, 'r'); $tmpTrack = new Services_Trackback(); stream_set_timeout($handle, $tmpTrack->_options['timeout']); if (!is_resource($handle)) { return PEAR::raiseError('Could not open URL "'.$url.'"'); } $content = ''; for ($i = 0; ($i < $tmpTrack->_options['fetchlines']) && !feof($handle);$i++) { $content .= fgets($handle); } return $content; } // }}} // {{{ _getEncodedData() /** * _getEncodedData * Receives a number of data from the internal data store, encoded for XML usage. * * @since 0.1.0 * @access private * @param array $keys Data keys to receive * @param array $data Optionally the data to check (default is the object data). * @return void */ function _getEncodedData($keys, $data = null) { if (!isset($data)) { $data =& $this->_data; } foreach ($keys as $key) { $res[$key] = htmlentities($data[$key]); } return $res; } // }}} // {{{ _getDecodedData() /** * _getDecodedData * Receives a number of data from the internal data store. * * @since 0.1.0 * @access private * @param array $keys Data keys to receive * @param array $data Optionally the data to check (default is the object data). * @return void */ function _getDecodedData($keys, $data = null) { if (!isset($data)) { $data =& $this->_data; } foreach ($keys as $key) { $res[$key] = $data[$key]; } return $res; } // }}} // {{{ _checkData() /** * _checkData * Checks a given array of keys for the validity of their data. * * @since 0.1.0 * @access private * @param array $keys Data keys to check. * @param array $data Optionally the data to check (default is the object data). * @return void */ function _checkData($keys, $data = null) { if (!isset($data)) { $data =& $this->_data; } foreach ($keys as $key) { if (empty($data[$key])) { return PEAR::raiseError('Invalid data. Key "'.$key.'" missing.'); } } return true; } // }}} // {{{ _checkURLs() /** * _checkURLs * This little method checks if 2 URLs (the URL to trackback against the trackback * identifier found in the autodiscovery code) are equal. * * @see Services_Trackback::autodiscover() * @since 0.2.0 * @access private * @param string $url1 The first URL. * @param string $url2 The second URL. * @param constant $strictness How strict to check URLs. Use one of SERVICES_TRACKBACK_STRICTNESS_* constants. * @return mixed True on success, otherwise PEAR::Error. */ function _checkURLs($url1, $url2, $strictness) { switch ($strictness) { case SERVICES_TRACKBACK_STRICTNESS_HIGH: if ($url1 !== $url2) { return PEAR::raiseError('URLs mismatch. "'.$url.'" !== "'.$identifier.'".'); } break; case SERVICES_TRACKBACK_STRICTNESS_MIDDLE: $domainRegex = "@http://([^/]+).*@"; $res = preg_match($domainRegex, $url1, $matches); if (!$res) return PEAR::raiseError('Invalid URL1, no domain part found ("'.$url1.'").'); $domain1 = $matches[1]; $res = preg_match($domainRegex, $url2, $matches); if (!$res) return PEAR::raiseError('Invalid URL1, no domain part found ("'.$url1.'").'); $domain2 = $matches[1]; if ($domain1 !== $domain2) return PEAR::raiseError('URLs missmatch. "'.$domain1.'" !== "'.$domain2.'".'); break; case SERVICES_TRACKBACK_STRICTNESS_LOW: default: // No checks, when strictness is low. break; } return true; } // }}} // {{{ _interpretTrackbackResponse() /** * Interpret the returned XML code, when sending a trackback. * * @see Services_Trackback::send() * @since 0.3.0 * @access private * @return void Mixed true on success, otherwise PEAR::Error. */ function _interpretTrackbackResponse($response) { if (!preg_match('@<error>([0-9]+)</error>@', $response, $matches)) { return PEAR::raiseError('Invalid trackback response, error code not found.'); } $errorCode = $matches[1]; // Error code 0 means no error. if ($errorCode == 0) { return true; } if (!preg_match('@<message>([^<]+)</message>@', $response, $matches)) { return PEAR::raiseError('Error code '.$errorCode.', no message received.'); } else { return PEAR::raiseError('Error code '.$errorCode.', message "'.$matches[1].'" received.'); } } // }}} /** * Overloading * * @since 0.1.0 * @access public * @deprecated */ /* // Removed since 0.2 */ } ?> http://cvs.php.net/co.php/pear/Services_Trackback/test/Services_Trackback_Test.php?r=1.1&p=1 Index: pear/Services_Trackback/test/Services_Trackback_Test.php +++ pear/Services_Trackback/test/Services_Trackback_Test.php <?php // Includepath for local CVS development set_include_path('/cvs/pear/Services_Trackback'.PATH_SEPARATOR.get_include_path()); // {{{ require_once require_once 'Services/Trackback.php'; require_once 'PHPUnit.php'; // }}} class Webservices_Trackback_TestCase extends PHPUnit_TestCase { // {{{ Webservices_Trackback_TestCase() // constructor of the test suite function Webservices_Trackback_TestCase($name) { $this->PHPUnit_TestCase($name); } // }}} // {{{ setup() function setUp() { } // }}} // {{{ tearDown() function tearDown() { } // }}} // {{{ Test create() function test_create() { $data = array( 'title' => 'Test title.', 'id' => 'Test' ); $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $fakeTrack->_data = $data; $this->assertTrue(Services_Trackback::create($data, $options) == $fakeTrack); } // }}} // {{{ Test setOptions() function test_setOptions_success() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $realTrack = new Services_Trackback; $realTrack->setOptions($options); $this->assertTrue($realTrack == $fakeTrack); } function test_setOptions_failure_1() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => 'Test', 'maxRedirects' => 0, 'fetchlines' => 100, ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $realTrack = new Services_Trackback; $res = $realTrack->setOptions($options); $this->assertTrue(PEAR::isError($res)); } function test_setOptions_failure_2() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, 'foobarbaz' => 'No real option' ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $realTrack = new Services_Trackback; $res = $realTrack->setOptions($options); $this->assertTrue(PEAR::isError($res)); } // }}} // {{{ Test getOptions() function test_getOptions_success() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, ); $track = new Services_Trackback; $track->_options = $options; $this->assertTrue($track->getOptions() == $options); } // }}} // {{{ Test autodiscover() function test_autodiscover_success() { $data = array( 'id' => 'Test', 'url' => 'http://pear.php.net/package/net_ftp' ); $track1 = Services_Trackback::create($data); $track1->autodiscover(); $data['trackback_url'] = 'http://pear.php.net/trackback/trackback.php?id=Net_FTP'; $track2 = Services_Trackback::create($data); $this->assertTrue($track1 == $track2); } function test_autodiscover_failure() { $data = array( 'id' => 'Test', 'url' => 'http://pear.php.net/' ); $track1 = Services_Trackback::create($data); $res = $track1->autodiscover(); $this->assertTrue(PEAR::isError($res)); } // }}} // {{{Test send() function test_send() { $track = Services_Trackback::create(array('id' => 'Test', )); } // }}} // {{{Test getAutodiscoveryCode() function test_getAutodiscoveryCode_nocomments() { $xml = <<<EOD <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://www.example.com/trackback_test?a=1&b=2" dc:identifier="http://www.example.com/trackback_test?a=1&b=2" dc:title="Some strange test data: @<>&?" trackback:ping="http://www.example.com/trackback/test" /> </rdf:RDF> EOD; $data = array( 'title' => 'Some strange test data: @<>&?', 'url' => 'http://www.example.com/trackback_test?a=1&b=2', 'trackback_url' => 'http://www.example.com/trackback/test', 'id' => 1 ); $track = Services_Trackback::create($data); $this->assertTrue($track->getAutodiscoveryCode(false) == $xml); } function test_getAutodiscoveryCode_comments() { $xml = <<<EOD <!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://www.example.com/trackback_test?a=1&b=2" dc:identifier="http://www.example.com/trackback_test?a=1&b=2" dc:title="Some strange test data: @<>&?" trackback:ping="http://www.example.com/trackback/test" /> </rdf:RDF> --> EOD; $data = array( 'title' => 'Some strange test data: @<>&?', 'url' => 'http://www.example.com/trackback_test?a=1&b=2', 'trackback_url' => 'http://www.example.com/trackback/test', 'id' => 1 ); $track = Services_Trackback::create($data); $this->assertTrue($track->getAutodiscoveryCode() == $xml); } // }}} // {{{ Test receive() function test_receive() { $postData = array( 'title' => 'Little test entry...', 'excerpt' => 'This is the story of foo, bar and baz.', 'url' => 'http://www.example.com/blog/entries/18-foo-bar-baz.htm', 'blog_name' => 'Little test blog.' ); $data = $postData; $data['id'] = 1; $recTrack = Services_Trackback::create(array('id' => 1)); $recTrack->receive($data); $this->assertTrue($recTrack == Services_Trackback::create($data)); } // }}} // {{{ Test getResponseSuccess() function test_getResponseSuccess() { $xml = <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>0</error> </response> EOD; $this->assertTrue(Services_Trackback::getResponseSuccess() == $xml); } // }}} // {{{ Test getResponseError() function test_getResponseError() { $xml = <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>-2</error> <message>Me & you</message> </response> EOD; $this->assertTrue(Services_Trackback::getResponseError('Me & you', -2) == $xml); } // }}} // {{{ Test _fromArray() function test_fromArray() { $data = array( 'title' => 'Test title.', 'id' => 'Test' ); $fakeTrack = new Services_Trackback; $fakeTrack->_data = $data; $realTrack = new Services_Trackback; $realTrack->_fromArray($data); $this->assertTrue($realTrack == $fakeTrack); } // }}} // {{{ Test _getContent() function test_getContent() { $url = 'http://www.example.com'; $res = <<<EOD <HTML> <HEAD> <TITLE>Example Web Page</TITLE> </HEAD> <body> <p>You have reached this web page by typing "example.com", "example.net", or "example.org" into your web browser.</p> <p>These domain names are reserved for use in documentation and are not available for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 2606</a>, Section 3.</p> </BODY> </HTML> EOD; $this->assertTrue(trim(Services_Trackback::_getContent($url)) == trim($res)); } // }}} // {{{ Test _getEncodedData() function test_getEncodedData() { $in = array( 'foo' => 'bar & baz', 'bar' => 'foo << baz', 'baz' => 'foo && bar' ); $out = array( 'foo' => 'bar & baz', 'bar' => 'foo << baz', 'baz' => 'foo && bar' ); $this->assertTrue(Services_Trackback::_getEncodedData(array('foo', 'bar', 'baz'), $in) == $out); } // }}} // {{{ Test _getDecodedData() function test_getDecodedData() { $in = array( 'foo' => 'bar & baz', 'bar' => 'foo << baz', 'baz' => 'foo && bar' ); $out = array( 'foo' => 'bar & baz', 'baz' => 'foo && bar' ); $this->assertTrue(Services_Trackback::_getDecodedData(array('foo', 'baz'), $in) == $out); } // }}} // {{{ Test _checkData function test_checkData_true() { $keys = array('id', 'test'); $data = array('id' => 1, 'test' => 'x', 'test2' => 0); $this->assertTrue(Services_Trackback::_checkData($keys, $data)); } function test_checkData_false() { $keys = array('id', 'test'); $data = array('id' => 1, 'test2' => 0); $this->assertTrue(PEAR::isError(Services_Trackback::_checkData($keys, $data))); } // }}} // {{{ Test _checkURLs() function test_checkURLs_true_1() { $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.net/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_true_2() { $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_true_3() { $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackback/index.php"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_false_1() { // No real test, should always return true $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "https://test.net/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_false_2() { $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.net/trackback/index.php"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } function test_checkURLs_false_3() { $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackback/index.htm"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } function test_checkURLs_invalid_1() { // No real test, should always return true $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "https://test.net/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_invalid_2() { $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE; $url1 = "http:///trackback/index.php"; $url2 = "http://www.example.net/trackback/index.php"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } function test_checkURLs_invalid_3() { // No real test, URLs are not invalid, but unequal $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackback/index.htm"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } // // }}} // {{{ Test _interpretTrackbackResponse() function test_interpretTrackbackResponse_success() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> <error>0</error> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue($res); } function test_interpretTrackbackResponse_failure() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> <error>-1</error> <message>No more trackbacks from this host</message> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue(PEAR::isError($res)); } function test_interpretTrackbackResponse_invalid_1() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> <error></error> <message>No more trackbacks from this host</message> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue(PEAR::isError($res)); } function test_interpretTrackbackResponse_invalid_2() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue(PEAR::isError($res)); } // }}} } $suite = new PHPUnit_TestSuite("Webservices_Trackback_TestCase"); $result = PHPUnit::run($suite); echo $result->toString(); ?> http://cvs.php.net/co.php/pear/Services_Trackback/test/simple_test.php?r=1.1&p=1 Index: pear/Services_Trackback/test/simple_test.php +++ pear/Services_Trackback/test/simple_test.php <?php set_include_path('.:/cvs/pear/Services_Trackback:/usr/php4/share/pear'); require_once 'Services/Trackback.php'; $trackback = new Services_Trackback(array('id' => 1)); echo "\n\n--------------- Success response ---------------\n\n"; echo $trackback->getResponseSuccess(); echo "\n\n--------------- Error response ---------------\n\n"; echo $trackback->getResponseError(1, "Test error"); $trackback = Services_Trackback::create(array('id' => 1, 'title' => 'Test title', 'url' => 'http://www.example.com/', 'trackback_url' => 'http://www.example.com/trackback/1')); echo "\n\n--------------- Newly created trackback object ---------------\n\n"; var_dump($trackback); echo "\n\n--------------- Autodiscovery code (with comments) ---------------\n\n"; var_dump($trackback->getAutoDiscoveryCode()); echo "\n\n--------------- Autodiscovery code (without comments) ---------------\n\n"; var_dump($trackback->getAutoDiscoveryCode(false)); $data = array( 'title' => 'Test title', 'excerpt' => 'A test blog entry', 'url' => 'http://www.example.com/', 'blog_name' => 'Example weblog' ); $trackback = Services_Trackback::create(array('id' => 1)); var_dump($trackback->receive($data)); echo "\n\n--------------- Received trackback object ---------------\n\n"; var_dump($trackback); echo "\n\n--------------- Autodiscovered trackback object ---------------\n\n"; $trackback = Services_Trackback::create(array('id' => 'Test', 'url' => 'http://pear.php.net/package/Net_FTP')); var_dump($trackback->autodiscover()); var_dump($trackback); echo "\n\n--------------- Sending trackback ---------------\n\n"; $trackback->set('title', 'Testing Services_Trackback'); $trackback->set('url', 'http://www.example.com'); $trackback->set('excerpt', 'Test test tes...'); $trackback->set('blog_name', 'Tobias Schlitt testing Services_Trackback'); var_dump($trackback); var_dump($trackback->send()); ?>
http://cvs.php.net/co.php/pear/Services_Trackback/generate_package_xml.php?r=1.1&p=1 Index: pear/Services_Trackback/generate_package_xml.php +++ pear/Services_Trackback/generate_package_xml.php #!/usr/bin/php <?php $make = 1; require_once('PEAR/PackageFileManager.php'); $pkg = new PEAR_PackageFileManager; // directory that PEAR CVS is located in $cvsdir = '/cvs/pear/'; $packagedir = $cvsdir . 'Services_Trackback/'; // Filemanager settings $category = 'Services'; $package = 'Services_Trackback'; $version = '0.4.0'; $state = 'alpha'; $summary = 'Trackback - A generic class for sending and receiving trackbacks.'; $description = <<<EOT A generic class for sending and receiving trackbacks. EOT; $notes = <<<EOT * Initial release on PEARWeb. EOT; $e = $pkg->setOptions( array('simpleoutput' => true, 'baseinstalldir' => '', 'summary' => $summary, 'description' => $description, 'version' => $version, 'packagedirectory' => $packagedir, 'pathtopackagefile' => $packagedir, 'state' => $state, 'filelistgenerator' => 'file', 'notes' => $notes, 'package' => $package, 'dir_roles' => array( 'test' => 'test'), 'ignore' => array('package.xml', 'package2.xml', 'CHANGES.txt', 'doc*', 'generate_package_xml.php', '*.tgz', 'local_test.php'), )); if (PEAR::isError($e)) { echo $e->getMessage(); exit; } $e = $pkg->addMaintainer('toby', 'lead', 'Tobias Schlitt', 'toby@php.net'); if (PEAR::isError($e)) { echo $e->getMessage(); exit; } $e = $pkg->addDependency('HTTP_Request', '1.2.4', 'ge', 'pkg'); if (PEAR::isError($e)) { echo $e->getMessage(); exit; } // hack until they get their shit in line with docroot role $pkg->addRole('tpl', 'php'); $pkg->addRole('png', 'php'); $pkg->addRole('gif', 'php'); $pkg->addRole('jpg', 'php'); $pkg->addRole('css', 'php'); $pkg->addRole('js', 'php'); $pkg->addRole('ini', 'php'); $pkg->addRole('inc', 'php'); $pkg->addRole('afm', 'php'); $pkg->addRole('pkg', 'doc'); $pkg->addRole('cls', 'doc'); $pkg->addRole('proc', 'doc'); $pkg->addRole('sh', 'doc'); if (isset($make)) { $e = $pkg->writePackageFile(); } else { $e = $pkg->debugPackageFile(); } if (PEAR::isError($e)) { echo $e->getMessage(); } if (!isset($make)) { echo '<a href="' . $_SERVER['PHP_SELF'] . '?make=1">Make this file</a>'; } ?> http://cvs.php.net/co.php/pear/Services_Trackback/package.xml?r=1.1&p=1 Index: pear/Services_Trackback/package.xml +++ pear/Services_Trackback/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0" packagerversion="1.4.0a4"> <name>Services_Trackback</name> <summary>Trackback - A generic class for sending and receiving trackbacks.</summary> <description>A generic class for sending and receiving trackbacks. </description> <maintainers> <maintainer> <user>toby</user> <name>Tobias Schlitt</name> <email>toby@php.net</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.4.0</version> <date>2005-03-11</date> <license>PHP License</license> <state>alpha</state> <notes>* Initial release on PEARWeb. </notes> <deps> <dep type="pkg" rel="ge" version="1.2.4" optional="no">HTTP_Request</dep> </deps> <filelist> <dir name="/" baseinstalldir=""> <dir name="Services"> <file role="php" name="Trackback.php"/> </dir> <!-- /Services --> <dir name="test"> <file role="test" name="Services_Trackback_Test.php"/> <file role="test" name="simple_test.php"/> </dir> <!-- /test --> </dir> <!-- / --> </filelist> </release> <changelog> <release> <version>0.2.0</version> <date>2005-02-25</date> <license>PHP License</license> <state>devel</state> <notes>* Removed overloading. * Refactored. * Added autodiscver(). * Added PEAR 1.4 conform package2.xml (package.xml version 2.0). </notes> </release> <release> <version>0.3.0</version> <date>2005-02-26</date> <license>PHP License</license> <state>devel</state> <notes>* Refactored. * Added send(). * Added dependency to HTTP_Request for the send() method. </notes> </release> <release> <version>0.4.0</version> <date>2005-03-11</date> <license>PHP License</license> <state>alpha</state> <notes>* Initial release on PEARWeb. </notes> </release> </changelog> </package> http://cvs.php.net/co.php/pear/Services_Trackback/package2.xml?r=1.1&p=1 Index: pear/Services_Trackback/package2.xml +++ pear/Services_Trackback/package2.xml <?xml version="1.0"?> <package packagerversion="1.4.0a4" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Services_Trackback</name> <channel>pear.php.net</channel> <summary>Trackback - A generic class for sending and receiving trackbacks.</summary> <description>A generic class for sending and receiving trackbacks. </description> <lead> <name>Tobias Schlitt</name> <user>toby</user> <email>toby@php.net</email> <active>yes</active> </lead> <date>2005-03-11</date> <time>15:53:32</time> <version> <release>0.4.0</release> <api>0.4.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <license uri="http://www.php.net/license">PHP License</license> <notes>* Initial release on PEARWeb. </notes> <contents> <dir name="/"> <dir name="Services"> <file baseinstalldir="" name="Trackback.php" role="php"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> </dir> <!-- /Services --> <dir name="test"> <file baseinstalldir="" name="Services_Trackback_Test.php" role="test"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> <file baseinstalldir="" name="simple_test.php" role="test"> <tasks:replace from="@package_version@" to="version" type="package-info" /> </file> </dir> <!-- /test --> </dir> <!-- / --> </contents> <dependencies> <required> <php> <min>4.0.0</min> <max>6.0.0</max> </php> <pearinstaller> <min>1.4.0a1</min> </pearinstaller> <package> <name>HTTP_Request</name> <channel>pear.php.net</channel> <min>1.2.4</min> </package> </required> </dependencies> <phprelease /> <changelog> <release> <version> <release>0.2.0</release> <api>0.2.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2005-02-25</date> <license uri="http://www.php.net/license">PHP License</license> <notes>* Removed overloading. * Refactored. * Added autodiscver(). * Added PEAR 1.4 conform package2.xml (package.xml version 2.0). </notes> </release> <release> <version> <release>0.3.0</release> <api>0.3.0</api> </version> <stability> <release>devel</release> <api>devel</api> </stability> <date>2005-02-26</date> <license uri="http://www.php.net/license">PHP License</license> <notes>* Refactored. * Added send(). * Added dependency to HTTP_Request for the send() method. </notes> </release> <release> <version> <release>0.4.0</release> <api>0.4.0</api> </version> <stability> <release>alpha</release> <api>alpha</api> </stability> <date>2005-03-11</date> <license uri="http://www.php.net/license">PHP License</license> <notes>* Initial release on PEARWeb. </notes> </release> </changelog> </package> http://cvs.php.net/co.php/pear/Services_Trackback/Services/Trackback.php?r=1.1&p=1 Index: pear/Services_Trackback/Services/Trackback.php +++ pear/Services_Trackback/Services/Trackback.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Services_Trackback. * * This is the main file of the Services_Trackback package. This file has to be * included for usage of Services_Trackback. * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Webservices * @package Trackback * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Trackback.php,v 1.1 2005/03/11 14:58:57 toby Exp $ * @link http://pear.php.net/package/Services_Trackback * @since File available since Release 0.0.1 */ // {{{ require_once /** * Load PEAR error handling */ require_once 'PEAR.php'; /** * PEAR::HTTP_Request is required only when needed. * @see Services_Trackback::send(). */ // require_once 'HTTP/Request.php'; // }}} // {{{ Constants /** * This constant is used with the @see Services_Trackback::autodiscover() method. * Using this constant you supress the URL check described in the trackback specs. */ define('SERVICES_TRACKBACK_STRICTNESS_LOW', 1); /** * This constant is used with the @see Services_Trackback::autodiscover() method. * Using this constant you use a not so strict URL check than described in the * trackback specs. Only the domain name is checked. */ define('SERVICES_TRACKBACK_STRICTNESS_MIDDLE', 2); /** * This constant is used with the @see Services_Trackback::autodiscover() method. * Using this constant activate the URL check described in the trackback specs. */ define('SERVICES_TRACKBACK_STRICTNESS_HIGH', 3); // }}} /** * Trackback * A generic class to send/receive trackbacks. * * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @category Webservices * @package Trackback * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @version Release: @package_version@ * @link http://pear.php.net/package/Services_Trackback * @since 0.0.1 * @access public */ class Services_Trackback { // {{{ var $_data /** * The necessary trackback data. * * @var array * @access private */ var $_data = array( 'id' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'url' => '', 'trackback_url' => '', ); // }}} // {{{ var $_options /** * Options to influence Services_Trackback. * * @see Services_Trackback::create() * @since 0.4.0 * @var array * @access private */ var $_options = array( 'strictness' => SERVICES_TRACKBACK_STRICTNESS_LOW, 'timeout' => 30, // seconds 'allowRedirects' => true, 'maxRedirects' => 2, 'fetchlines' => 30, 'useragent' => 'PEAR::Services_Trackback v@package_version@' ); // }}} // {{{ Services_Trackback() /** * Constructor * Creates a new Trackback object. Private because of factory use. * * @since 0.1 * @access private * @return void */ function Services_Trackback ( ) { } // }}} // {{{ create() /** * Factory * This static method is used to create a trackback object. (Services_Trackback::create($data)) * The factory requires a data array as described below for creation. The 'id' key is * obligatory for this method. Every other data is not quite necessary for the creation, but * might be necessary for calling other methods afterwards. See the specific methods for * further info on which data is required. * * @since 0.2 * @static * @access public * @param array $data Data for the trackback, which is obligatory: * 'id' The ID of the trackback target. * Data which is optional (for construction, not for specific methods): * 'title' string Title of the trackback sending/receiving blog entry. * 'excerpt' string Abstract of the trackback sending/receiving blog entry. * 'blog_name' string Name of the trackback sending/receiving weblog. * 'url' string URL of the trackback sending/receiving blog entry. * 'trackback_url' string URL to send trackbacks to. * @param array $options Options to set for this trackback. Valid options: * 'strictness': int The default strictness to use in @see Services_Trackback::autodiscover(). * 'timeout': int The default timeout for network operations in seconds. * 'allowRedirects': bool Wether to follow HTTP redirects or not. * 'maxRedirects': int Maximum number of redirects. * 'fetchsize': int The max file size to fetch over the network. * 'useragent': string The user agent to use for HTTP requests. * * @return object(Services_Trackback) The newly created Trackback. */ function &create($data, $options = null) { $options = (isset($options)) ? $options : array(); $trackback = new Services_Trackback(); $res = $trackback->_fromArray($data); if (PEAR::isError($res)) { return $res; } $res = $trackback->setOptions($options); if (PEAR::isError($res)) { return $res; } return $trackback; } // }}} // {{{ setOptions() /** * setOptions * Set options for the trackback. * * @since 0.4.0 * @access public * @see Services_Trackback::create() * @see Services_Trackback::getOptions() * @param array $options Pairs of 'option' => 'value' as described at @see Services_Trackback::create(). * @return mixed Bool true on success, otherwise PEAR::Error(). */ function setOptions($options) { foreach($options as $option => $value) { if (!isset($this->_options[$option])) { return PEAR::raiseError('Desired option "'.$option.'" not found.'); } switch ($option) { case 'strictness': if (!is_int($value) || ($value < 1) || ($value > 3)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'timeout': if (!is_int($value) || ($value < 0)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'allowRedirects': if (!is_bool($value)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'maxRedirects': if (!is_int($value) || ($value < 0)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; case 'fetchsize': if (!is_int($value) || ($value < 1)) { return PEAR::raiseError('Invalid value for option "'.$option.'".'); } break; } $this->_options[$option] = $value; } return true; } // }}} // {{{ getOptions() /** * getOptions * Get the currently set option set. * * @since 0.4.0 * @access public * @see Services_Trackback::setOptions() * @see Services_Trackback::create() * @return array The currently active options. */ function getOptions() { return $this->_options; } // }}} // {{{ autodiscover() /** * autodiscover * Checks a given URL for trackback autodiscovery code. This is another factory method. * * @since 0.2.0 * @access public * @return mixed Services_Trackback object on success, otherwise PEAR::Error. */ function autodiscover() { $necessaryData = array('url'); $res = $this->_checkData($necessaryData); if (PEAR::isError($res)) { return $res; } $url = $this->_data['url']; /* Sample autodiscovery code <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://pear.php.net/package/Net_FTP" dc:identifier="http://pear.php.net/package/Net_FTP" dc:title="Net_FTP" trackback:ping="http://pear.php.net/trackback/trackback.php?id=Net_FTP" /> </rdf:RDF> */ // Receive file contents $content = $this->_getContent($url); if (PEAR::isError($content)) { return $content; } // Get trackback identifier if (!preg_match('@dc:identifier\s*=\s*["\'](http:[^"\']+)"@i', $content, $matches)) { return PEAR::raiseError('No trackback RDF found in "'.$url.'".'); } $identifier = trim($matches[1]); // Get trackback URI if (!preg_match('@trackback:ping\s*=\s*["\'](http:[^"\']+)"@i', $content, $matches)) { return PEAR::raiseError('No trackback URI found in "'.$url.'".'); } $trackbackUrl = trim($matches[1]); // Check if the URL to trackback matches the identifier from the autodiscovery code $res = $this->_checkURLs($url, $identifier, $strictness); if (PEAR::isError($res)) { return $res; } $this->_data['trackback_url'] = $trackbackUrl; return true; } // }}} // {{{ send() /** * send * This method sends a trackback to the trackback_url saved in it. The * data array of the trackback object can be completed by submitting the * necessary data through the $data parameter of this method. * The following data has to be set to call this method: * 'title' Title of the weblog entry sending the trackback. * 'url' URL of the weblog entry sending the trackback. * 'excerpt' Excerpt of the weblog entry sending the trackback. * 'blog_name' Name of the weblog sending the trackback. * 'trackback_url' URL to send the trackback to. * Services_Trackback::send() requires HTTP_Request. * * @since 0.3.0 * @access public * @param string $data Additional data to complete the trackback. * @return mixed True on success, otherwise PEAR::Error. */ function send($data = null) { // Load HTTP_Request @require_once 'HTTP/Request.php'; if (!class_exists('HTTP_Request')) { return PEAR::raiseError('Unable to load PEAR::HTTP_Request'); } // Consistancy check if (!isset($data)) $data = array(); $this->_data = array_merge($this->_data, $data); $necessaryData = array('title', 'url', 'excerpt', 'blog_name', 'trackback_url'); $res = $this->_checkData($necessaryData); if (PEAR::isError($res)) return $res; // Get URL $url = str_replace('&', '&', $this->_data['trackback_url']); // Create new HTTP_Request $req = new HTTP_Request($url, array( 'timeout' => $this->_options['timeout'], 'allowRedirects' => $this->_options['allowRedirects'], 'maxRedirects' => $this->_options['maxRedirects'], )); $req->setMethod(HTTP_REQUEST_METHOD_POST); // Add HTTP headers $req->addHeader("User-Agent", $this->options['useragent']); // Adding data to send $req->addPostData('url', $this->_data['url']); $req->addPostData('title', $this->_data['title']); $req->addPostData('blog_name', $this->_data['blog_name']); $req->addPostData('excerpt', strip_tags($this->_data['excerpt'])); // Send POST request $res = $req->sendRequest(); if (PEAR::isError($res)) { return $res; } // Check return code if ($req->getResponseCode() != 200) { return PEAR::raiseError('Host returned Error '.$req->getRequestCode().'.'); } return $this->_interpretTrackbackResponse($req->getResponseBody()); } // }}} // {{{ getAutodiscoveryCode() /** * getAutodiscoverCode * Returns the RDF Code for a given website to let weblogs autodiscover * the possibility of tracking it back. * The following data has to be set to call this method: * 'id' * 'title' * 'url' * 'trackback_url' * @since 0.1.0 * @access public * @param bool $comments Whether to include HTML comments around the RDF using <!-- --> * @return string RDF code */ function getAutodiscoveryCode($comments = true) { $necessaryData = array('title', 'url', 'trackback_url'); $res = $this->_checkData($necessaryData); if (PEAR::isError($res)) { return $res; } $data = $this->_getEncodedData($necessaryData); $res = <<<EOD <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="%s" dc:identifier="%s" dc:title="%s" trackback:ping="%s" /> </rdf:RDF> EOD; $res = sprintf($res, $data['url'], $data['url'], $data['title'], $data['trackback_url']); if ($comments) { return "<!--\n".$res."\n-->\n"; } else { return $res."\n"; } } // }}} // {{{ receive() /** * receive * Receives a trackback. The following data has to be set in the data array to fulfill this * operation: * 'id' * * @since 0.1.0 * @access public * @return object Services_Trackback */ function receive($data = null) { if (!isset($data)) { $data = $_POST; } $necessaryPostData = array('title', 'excerpt', 'url', 'blog_name'); $res = $this->_checkData(array('id')); if (PEAR::isError($res)) { return $res; } $res = $this->_checkData($necessaryPostData, $data); if (PEAR::isError($res)) { return PEAR::raiseError('POST data incomplete: '.$res->getMessage()); } $this->_data = array_merge($this->_data, $this->_getDecodedData($necessaryPostData, $data)); return true; } // }}} // {{{ getResponseSuccess() /** * getResponseSuccess * Returns an XML response for a successful trackback. * * @since 0.1.0 * @access public * @static * @return string The XML code */ function getResponseSuccess () { return <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>0</error> </response> EOD; } // }}} // {{{ getResponseError() /** * getResponseError * Returns an XML response for a trackback error. * * @since 0.1.0 * @access public * @param int $code The error code * @param string $message The error message * @return void */ function getResponseError($message, $code) { $data = Services_Trackback::_getEncodedData(array('code', 'message'), array('code' => $code, 'message' => $message)); $res = <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>%s</error> <message>%s</message> </response> EOD; return sprintf($res, $data['code'], $data['message']); } // }}} // {{{ get() /** * get * Get data from the trackback. Returns the value of a given * key or PEAR::Error. * * @since 0.2.0 * @access public * @param string $key The key to fetch a value for. * @return mixed A string value or a PEAR::Error on failure. */ function get($key) { return (isset($this->_data[$key])) ? $this->_data[$key] : PEAR::raiseError('Key '.$key.' not found.'); } // }}} // {{{ set() /** * set * Set data of the trackback. Saves the value of a given * key , returning true on success, PEAR::Error on faulure. * * @since 0.2.0 * @access public * @param string $key The key to set a value for. * @param string $val The value for the key. * @return mixed Boolean true on success or a PEAR::Error on failure. */ function set($key, $val) { $this->_data[$key] = $val; return true; } // }}} // {{{ _fromArray() /** * Create a Trackback from a $data array. * * @since 0.2.0 * @access private * @param array $data The data array (@see Services_Trackback::create()). * @return mixed True on success, otherwise PEAR::Error. */ function _fromArray($data) { $res = $this->_checkData(array('id'), $data); if (PEAR::isError($res)) { return $res; } $this->_data = $data; return true; } // }}} // {{{ _getContent() /** * _getContent * Receive the content from a specific URL. * * @since 0.4.0 * @access private * @param string $url The URL to download data from. * @return string The content. */ function _getContent($url) { $handle = fopen($url, 'r'); $tmpTrack = new Services_Trackback(); stream_set_timeout($handle, $tmpTrack->_options['timeout']); if (!is_resource($handle)) { return PEAR::raiseError('Could not open URL "'.$url.'"'); } $content = ''; for ($i = 0; ($i < $tmpTrack->_options['fetchlines']) && !feof($handle);$i++) { $content .= fgets($handle); } return $content; } // }}} // {{{ _getEncodedData() /** * _getEncodedData * Receives a number of data from the internal data store, encoded for XML usage. * * @since 0.1.0 * @access private * @param array $keys Data keys to receive * @param array $data Optionally the data to check (default is the object data). * @return void */ function _getEncodedData($keys, $data = null) { if (!isset($data)) { $data =& $this->_data; } foreach ($keys as $key) { $res[$key] = htmlentities($data[$key]); } return $res; } // }}} // {{{ _getDecodedData() /** * _getDecodedData * Receives a number of data from the internal data store. * * @since 0.1.0 * @access private * @param array $keys Data keys to receive * @param array $data Optionally the data to check (default is the object data). * @return void */ function _getDecodedData($keys, $data = null) { if (!isset($data)) { $data =& $this->_data; } foreach ($keys as $key) { $res[$key] = $data[$key]; } return $res; } // }}} // {{{ _checkData() /** * _checkData * Checks a given array of keys for the validity of their data. * * @since 0.1.0 * @access private * @param array $keys Data keys to check. * @param array $data Optionally the data to check (default is the object data). * @return void */ function _checkData($keys, $data = null) { if (!isset($data)) { $data =& $this->_data; } foreach ($keys as $key) { if (empty($data[$key])) { return PEAR::raiseError('Invalid data. Key "'.$key.'" missing.'); } } return true; } // }}} // {{{ _checkURLs() /** * _checkURLs * This little method checks if 2 URLs (the URL to trackback against the trackback * identifier found in the autodiscovery code) are equal. * * @see Services_Trackback::autodiscover() * @since 0.2.0 * @access private * @param string $url1 The first URL. * @param string $url2 The second URL. * @param constant $strictness How strict to check URLs. Use one of SERVICES_TRACKBACK_STRICTNESS_* constants. * @return mixed True on success, otherwise PEAR::Error. */ function _checkURLs($url1, $url2, $strictness) { switch ($strictness) { case SERVICES_TRACKBACK_STRICTNESS_HIGH: if ($url1 !== $url2) { return PEAR::raiseError('URLs mismatch. "'.$url.'" !== "'.$identifier.'".'); } break; case SERVICES_TRACKBACK_STRICTNESS_MIDDLE: $domainRegex = "@http://([^/]+).*@"; $res = preg_match($domainRegex, $url1, $matches); if (!$res) return PEAR::raiseError('Invalid URL1, no domain part found ("'.$url1.'").'); $domain1 = $matches[1]; $res = preg_match($domainRegex, $url2, $matches); if (!$res) return PEAR::raiseError('Invalid URL1, no domain part found ("'.$url1.'").'); $domain2 = $matches[1]; if ($domain1 !== $domain2) return PEAR::raiseError('URLs missmatch. "'.$domain1.'" !== "'.$domain2.'".'); break; case SERVICES_TRACKBACK_STRICTNESS_LOW: default: // No checks, when strictness is low. break; } return true; } // }}} // {{{ _interpretTrackbackResponse() /** * Interpret the returned XML code, when sending a trackback. * * @see Services_Trackback::send() * @since 0.3.0 * @access private * @return void Mixed true on success, otherwise PEAR::Error. */ function _interpretTrackbackResponse($response) { if (!preg_match('@<error>([0-9]+)</error>@', $response, $matches)) { return PEAR::raiseError('Invalid trackback response, error code not found.'); } $errorCode = $matches[1]; // Error code 0 means no error. if ($errorCode == 0) { return true; } if (!preg_match('@<message>([^<]+)</message>@', $response, $matches)) { return PEAR::raiseError('Error code '.$errorCode.', no message received.'); } else { return PEAR::raiseError('Error code '.$errorCode.', message "'.$matches[1].'" received.'); } } // }}} /** * Overloading * * @since 0.1.0 * @access public * @deprecated */ /* // Removed since 0.2 */ } ?> http://cvs.php.net/co.php/pear/Services_Trackback/test/Services_Trackback_Test.php?r=1.1&p=1 Index: pear/Services_Trackback/test/Services_Trackback_Test.php +++ pear/Services_Trackback/test/Services_Trackback_Test.php <?php // Includepath for local CVS development set_include_path('/cvs/pear/Services_Trackback'.PATH_SEPARATOR.get_include_path()); // {{{ require_once require_once 'Services/Trackback.php'; require_once 'PHPUnit.php'; // }}} class Webservices_Trackback_TestCase extends PHPUnit_TestCase { // {{{ Webservices_Trackback_TestCase() // constructor of the test suite function Webservices_Trackback_TestCase($name) { $this->PHPUnit_TestCase($name); } // }}} // {{{ setup() function setUp() { } // }}} // {{{ tearDown() function tearDown() { } // }}} // {{{ Test create() function test_create() { $data = array( 'title' => 'Test title.', 'id' => 'Test' ); $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $fakeTrack->_data = $data; $this->assertTrue(Services_Trackback::create($data, $options) == $fakeTrack); } // }}} // {{{ Test setOptions() function test_setOptions_success() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $realTrack = new Services_Trackback; $realTrack->setOptions($options); $this->assertTrue($realTrack == $fakeTrack); } function test_setOptions_failure_1() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => 'Test', 'maxRedirects' => 0, 'fetchlines' => 100, ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $realTrack = new Services_Trackback; $res = $realTrack->setOptions($options); $this->assertTrue(PEAR::isError($res)); } function test_setOptions_failure_2() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, 'foobarbaz' => 'No real option' ); $fakeTrack = new Services_Trackback; $fakeTrack->_options = $options; $realTrack = new Services_Trackback; $res = $realTrack->setOptions($options); $this->assertTrue(PEAR::isError($res)); } // }}} // {{{ Test getOptions() function test_getOptions_success() { $options = array( 'useragent' => 'Mozilla 10.0', 'strictness' => SERVICES_TRACKBACK_STRICTNESS_HIGH, 'timeout' => 10, 'allowRedirects' => false, 'maxRedirects' => 0, 'fetchlines' => 100, ); $track = new Services_Trackback; $track->_options = $options; $this->assertTrue($track->getOptions() == $options); } // }}} // {{{ Test autodiscover() function test_autodiscover_success() { $data = array( 'id' => 'Test', 'url' => 'http://pear.php.net/package/net_ftp' ); $track1 = Services_Trackback::create($data); $track1->autodiscover(); $data['trackback_url'] = 'http://pear.php.net/trackback/trackback.php?id=Net_FTP'; $track2 = Services_Trackback::create($data); $this->assertTrue($track1 == $track2); } function test_autodiscover_failure() { $data = array( 'id' => 'Test', 'url' => 'http://pear.php.net/' ); $track1 = Services_Trackback::create($data); $res = $track1->autodiscover(); $this->assertTrue(PEAR::isError($res)); } // }}} // {{{Test send() function test_send() { $track = Services_Trackback::create(array('id' => 'Test', )); } // }}} // {{{Test getAutodiscoveryCode() function test_getAutodiscoveryCode_nocomments() { $xml = <<<EOD <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://www.example.com/trackback_test?a=1&b=2" dc:identifier="http://www.example.com/trackback_test?a=1&b=2" dc:title="Some strange test data: @<>&?" trackback:ping="http://www.example.com/trackback/test" /> </rdf:RDF> EOD; $data = array( 'title' => 'Some strange test data: @<>&?', 'url' => 'http://www.example.com/trackback_test?a=1&b=2', 'trackback_url' => 'http://www.example.com/trackback/test', 'id' => 1 ); $track = Services_Trackback::create($data); $this->assertTrue($track->getAutodiscoveryCode(false) == $xml); } function test_getAutodiscoveryCode_comments() { $xml = <<<EOD <!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://www.example.com/trackback_test?a=1&b=2" dc:identifier="http://www.example.com/trackback_test?a=1&b=2" dc:title="Some strange test data: @<>&?" trackback:ping="http://www.example.com/trackback/test" /> </rdf:RDF> --> EOD; $data = array( 'title' => 'Some strange test data: @<>&?', 'url' => 'http://www.example.com/trackback_test?a=1&b=2', 'trackback_url' => 'http://www.example.com/trackback/test', 'id' => 1 ); $track = Services_Trackback::create($data); $this->assertTrue($track->getAutodiscoveryCode() == $xml); } // }}} // {{{ Test receive() function test_receive() { $postData = array( 'title' => 'Little test entry...', 'excerpt' => 'This is the story of foo, bar and baz.', 'url' => 'http://www.example.com/blog/entries/18-foo-bar-baz.htm', 'blog_name' => 'Little test blog.' ); $data = $postData; $data['id'] = 1; $recTrack = Services_Trackback::create(array('id' => 1)); $recTrack->receive($data); $this->assertTrue($recTrack == Services_Trackback::create($data)); } // }}} // {{{ Test getResponseSuccess() function test_getResponseSuccess() { $xml = <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>0</error> </response> EOD; $this->assertTrue(Services_Trackback::getResponseSuccess() == $xml); } // }}} // {{{ Test getResponseError() function test_getResponseError() { $xml = <<<EOD <?xml version="1.0" encoding="iso-8859-1"?> <response> <error>-2</error> <message>Me & you</message> </response> EOD; $this->assertTrue(Services_Trackback::getResponseError('Me & you', -2) == $xml); } // }}} // {{{ Test _fromArray() function test_fromArray() { $data = array( 'title' => 'Test title.', 'id' => 'Test' ); $fakeTrack = new Services_Trackback; $fakeTrack->_data = $data; $realTrack = new Services_Trackback; $realTrack->_fromArray($data); $this->assertTrue($realTrack == $fakeTrack); } // }}} // {{{ Test _getContent() function test_getContent() { $url = 'http://www.example.com'; $res = <<<EOD <HTML> <HEAD> <TITLE>Example Web Page</TITLE> </HEAD> <body> <p>You have reached this web page by typing "example.com", "example.net", or "example.org" into your web browser.</p> <p>These domain names are reserved for use in documentation and are not available for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 2606</a>, Section 3.</p> </BODY> </HTML> EOD; $this->assertTrue(trim(Services_Trackback::_getContent($url)) == trim($res)); } // }}} // {{{ Test _getEncodedData() function test_getEncodedData() { $in = array( 'foo' => 'bar & baz', 'bar' => 'foo << baz', 'baz' => 'foo && bar' ); $out = array( 'foo' => 'bar & baz', 'bar' => 'foo << baz', 'baz' => 'foo && bar' ); $this->assertTrue(Services_Trackback::_getEncodedData(array('foo', 'bar', 'baz'), $in) == $out); } // }}} // {{{ Test _getDecodedData() function test_getDecodedData() { $in = array( 'foo' => 'bar & baz', 'bar' => 'foo << baz', 'baz' => 'foo && bar' ); $out = array( 'foo' => 'bar & baz', 'baz' => 'foo && bar' ); $this->assertTrue(Services_Trackback::_getDecodedData(array('foo', 'baz'), $in) == $out); } // }}} // {{{ Test _checkData function test_checkData_true() { $keys = array('id', 'test'); $data = array('id' => 1, 'test' => 'x', 'test2' => 0); $this->assertTrue(Services_Trackback::_checkData($keys, $data)); } function test_checkData_false() { $keys = array('id', 'test'); $data = array('id' => 1, 'test2' => 0); $this->assertTrue(PEAR::isError(Services_Trackback::_checkData($keys, $data))); } // }}} // {{{ Test _checkURLs() function test_checkURLs_true_1() { $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.net/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_true_2() { $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_true_3() { $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackback/index.php"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_false_1() { // No real test, should always return true $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "https://test.net/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_false_2() { $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.net/trackback/index.php"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } function test_checkURLs_false_3() { $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackback/index.htm"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } function test_checkURLs_invalid_1() { // No real test, should always return true $strictness = SERVICES_TRACKBACK_STRICTNESS_LOW; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "https://test.net/trackbike/index.htm"; $this->assertTrue(Services_Trackback::_checkURLs($url1, $url2, $strictness)); } function test_checkURLs_invalid_2() { $strictness = SERVICES_TRACKBACK_STRICTNESS_MIDDLE; $url1 = "http:///trackback/index.php"; $url2 = "http://www.example.net/trackback/index.php"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } function test_checkURLs_invalid_3() { // No real test, URLs are not invalid, but unequal $strictness = SERVICES_TRACKBACK_STRICTNESS_HIGH; $url1 = "http://www.example.com/trackback/index.php"; $url2 = "http://www.example.com/trackback/index.htm"; $this->assertTrue(PEAR::isError(Services_Trackback::_checkURLs($url1, $url2, $strictness))); } // // }}} // {{{ Test _interpretTrackbackResponse() function test_interpretTrackbackResponse_success() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> <error>0</error> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue($res); } function test_interpretTrackbackResponse_failure() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> <error>-1</error> <message>No more trackbacks from this host</message> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue(PEAR::isError($res)); } function test_interpretTrackbackResponse_invalid_1() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> <error></error> <message>No more trackbacks from this host</message> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue(PEAR::isError($res)); } function test_interpretTrackbackResponse_invalid_2() { $xml = <<<EOD <?xml version='1.0' encoding='iso-8859-1'?> <response> </response> EOD; $res = Services_Trackback::_interpretTrackbackResponse($xml); $this->assertTrue(PEAR::isError($res)); } // }}} } $suite = new PHPUnit_TestSuite("Webservices_Trackback_TestCase"); $result = PHPUnit::run($suite); echo $result->toString(); ?> http://cvs.php.net/co.php/pear/Services_Trackback/test/simple_test.php?r=1.1&p=1 Index: pear/Services_Trackback/test/simple_test.php +++ pear/Services_Trackback/test/simple_test.php <?php set_include_path('.:/cvs/pear/Services_Trackback:/usr/php4/share/pear'); require_once 'Services/Trackback.php'; $trackback = new Services_Trackback(array('id' => 1)); echo "\n\n--------------- Success response ---------------\n\n"; echo $trackback->getResponseSuccess(); echo "\n\n--------------- Error response ---------------\n\n"; echo $trackback->getResponseError(1, "Test error"); $trackback = Services_Trackback::create(array('id' => 1, 'title' => 'Test title', 'url' => 'http://www.example.com/', 'trackback_url' => 'http://www.example.com/trackback/1')); echo "\n\n--------------- Newly created trackback object ---------------\n\n"; var_dump($trackback); echo "\n\n--------------- Autodiscovery code (with comments) ---------------\n\n"; var_dump($trackback->getAutoDiscoveryCode()); echo "\n\n--------------- Autodiscovery code (without comments) ---------------\n\n"; var_dump($trackback->getAutoDiscoveryCode(false)); $data = array( 'title' => 'Test title', 'excerpt' => 'A test blog entry', 'url' => 'http://www.example.com/', 'blog_name' => 'Example weblog' ); $trackback = Services_Trackback::create(array('id' => 1)); var_dump($trackback->receive($data)); echo "\n\n--------------- Received trackback object ---------------\n\n"; var_dump($trackback); echo "\n\n--------------- Autodiscovered trackback object ---------------\n\n"; $trackback = Services_Trackback::create(array('id' => 'Test', 'url' => 'http://pear.php.net/package/Net_FTP')); var_dump($trackback->autodiscover()); var_dump($trackback); echo "\n\n--------------- Sending trackback ---------------\n\n"; $trackback->set('title', 'Testing Services_Trackback'); $trackback->set('url', 'http://www.example.com'); $trackback->set('excerpt', 'Test test tes...'); $trackback->set('blog_name', 'Tobias Schlitt testing Services_Trackback'); var_dump($trackback); var_dump($trackback->send()); ?>