From bc0d0be07283bb4669e17528e347cd3870656136 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 6 Mar 2006 16:07:19 +0000 Subject: [PATCH] - ws --- lib/BigGet.php | 384 ++++++++++++++++++++--------------------- lib/FeedAggregator.php | 332 +++++++++++++++++------------------ lib/XmlRpcClient.php | 122 ++++++------- 3 files changed, 419 insertions(+), 419 deletions(-) diff --git a/lib/BigGet.php b/lib/BigGet.php index e61ef7e..0189261 100644 --- a/lib/BigGet.php +++ b/lib/BigGet.php @@ -13,197 +13,197 @@ class BigGet extends HttpRequestPool /** * File split size */ - const SIZE = 1048576; - - /** - * Parallel Request count - */ - const RMAX = 5; - - /** - * Whether to output debug messages - * - * @var bool - */ - public $dbg = false; - - /** - * URL - * - * @var string - */ - private $url; - - /** - * Temp file prefix - * - * @var string - */ - private $tmp; - - /** - * Size of requested resource - * - * @var int - */ - private $size; - - /** - * Whether the requests have been sent - * - * @var bool - */ - private $sent = false; - - /** - * Request counter - * - * @var int - */ - private $count = 0; - - /** - * Static constructor - * - * @param string $url - * @param string $tmp - * @return BigGet - * @throws Exception - */ - public static function url($url, $tmp = '/tmp') - { - $head = new HttpRequest($url, HttpRequest::METH_HEAD); - $headers = $head->send()->getHeaders(); - - if (200 != $head->getResponseCode()) { - throw new HttpException("Did not receive '200 Ok' from HEAD $url"); - } - if (!isset($headers['Accept-Ranges'])) { - throw new HttpException("Did not receive an Accept-Ranges header from HEAD $url"); - } - if (!isset($headers['Content-Length'])) { - throw new HttpException("Did not receive a Content-Length header from HEAD $url"); - } - - $bigget = new BigGet; - $bigget->url = $url; - $bigget->tmp = tempnam($tmp, 'BigGet.'); - $bigget->size = $headers['Content-Length']; - return $bigget; - } - - /** - * Save the resource to a file - * - * @param string $file - * @return bool - * @throws Exception - */ - public function saveTo($file) - { - $this->sent or $this->send(); - - if ($w = fopen($this->tmp, 'wb')) { - - $this->dbg && print "\nCopying temp files to $file ...\n"; - - foreach (glob($this->tmp .".????") as $tmp) { - - $this->dbg && print "\t$tmp\n"; - - if ($r = fopen($tmp, 'rb')) { - stream_copy_to_stream($r, $w); - fclose($r); - } - unlink($tmp); - } - fclose($w); - rename($this->tmp, $file); - - $this->dbg && print "\nDone.\n"; - - return true; - } - return false; - } - - /** - * Overrides HttpRequestPool::send() - * - * @return void - * @throws Exception - */ - public function send() - { - $this->sent = true; - - // use max RMAX simultanous requests with a req size of SIZE - while ($this->count < self::RMAX && -1 != $offset = $this->getRangeOffset()) { - $this->attachNew($offset); - } - - while ($this->socketPerform()) { - if (!$this->socketSelect()) { - throw new HttpSocketException; - } - } - } - - /** - * Overrides HttpRequestPool::socketPerform() - * - * @return bool - */ - protected function socketPerform() - { - try { - $rs = parent::socketPerform(); - } catch (HttpRequestPoolException $x) { - foreach ($x->exceptionStack as $e) { - echo $e->getMessage(), "\n"; - } - } - - foreach ($this->getFinishedRequests() as $r) { - $this->detach($r); - - if (206 != $rc = $r->getResponseCode()) { - throw new HttpException("Unexpected response code: $rc"); - } - - file_put_contents(sprintf("%s.%04d", $this->tmp, $r->id), $r->getResponseBody()); - - if (-1 != $offset = $this->getRangeOffset()) { - $this->attachNew($offset); - } - } - - return $rs; - } - - private function attachNew($offset) - { - $stop = min($this->count * self::SIZE + self::SIZE, $this->size) - 1; - - $this->dbg && print "Attaching new request to get range: $offset-$stop\n"; - - $req = new BigGetRequest( - $this->url, - HttpRequest::METH_GET, - array( - 'headers' => array( - 'Range' => "bytes=$offset-$stop" - ) - ) - ); - $this->attach($req); - $req->id = $this->count++; - } - - private function getRangeOffset() - { - return ($this->size >= $start = $this->count * self::SIZE) ? $start : -1; - } + const SIZE = 1048576; + + /** + * Parallel Request count + */ + const RMAX = 5; + + /** + * Whether to output debug messages + * + * @var bool + */ + public $dbg = false; + + /** + * URL + * + * @var string + */ + private $url; + + /** + * Temp file prefix + * + * @var string + */ + private $tmp; + + /** + * Size of requested resource + * + * @var int + */ + private $size; + + /** + * Whether the requests have been sent + * + * @var bool + */ + private $sent = false; + + /** + * Request counter + * + * @var int + */ + private $count = 0; + + /** + * Static constructor + * + * @param string $url + * @param string $tmp + * @return BigGet + * @throws Exception + */ + public static function url($url, $tmp = '/tmp') + { + $head = new HttpRequest($url, HttpRequest::METH_HEAD); + $headers = $head->send()->getHeaders(); + + if (200 != $head->getResponseCode()) { + throw new HttpException("Did not receive '200 Ok' from HEAD $url"); + } + if (!isset($headers['Accept-Ranges'])) { + throw new HttpException("Did not receive an Accept-Ranges header from HEAD $url"); + } + if (!isset($headers['Content-Length'])) { + throw new HttpException("Did not receive a Content-Length header from HEAD $url"); + } + + $bigget = new BigGet; + $bigget->url = $url; + $bigget->tmp = tempnam($tmp, 'BigGet.'); + $bigget->size = $headers['Content-Length']; + return $bigget; + } + + /** + * Save the resource to a file + * + * @param string $file + * @return bool + * @throws Exception + */ + public function saveTo($file) + { + $this->sent or $this->send(); + + if ($w = fopen($this->tmp, 'wb')) { + + $this->dbg && print "\nCopying temp files to $file ...\n"; + + foreach (glob($this->tmp .".????") as $tmp) { + + $this->dbg && print "\t$tmp\n"; + + if ($r = fopen($tmp, 'rb')) { + stream_copy_to_stream($r, $w); + fclose($r); + } + unlink($tmp); + } + fclose($w); + rename($this->tmp, $file); + + $this->dbg && print "\nDone.\n"; + + return true; + } + return false; + } + + /** + * Overrides HttpRequestPool::send() + * + * @return void + * @throws Exception + */ + public function send() + { + $this->sent = true; + + // use max RMAX simultanous requests with a req size of SIZE + while ($this->count < self::RMAX && -1 != $offset = $this->getRangeOffset()) { + $this->attachNew($offset); + } + + while ($this->socketPerform()) { + if (!$this->socketSelect()) { + throw new HttpSocketException; + } + } + } + + /** + * Overrides HttpRequestPool::socketPerform() + * + * @return bool + */ + protected function socketPerform() + { + try { + $rs = parent::socketPerform(); + } catch (HttpRequestPoolException $x) { + foreach ($x->exceptionStack as $e) { + echo $e->getMessage(), "\n"; + } + } + + foreach ($this->getFinishedRequests() as $r) { + $this->detach($r); + + if (206 != $rc = $r->getResponseCode()) { + throw new HttpException("Unexpected response code: $rc"); + } + + file_put_contents(sprintf("%s.%04d", $this->tmp, $r->id), $r->getResponseBody()); + + if (-1 != $offset = $this->getRangeOffset()) { + $this->attachNew($offset); + } + } + + return $rs; + } + + private function attachNew($offset) + { + $stop = min($this->count * self::SIZE + self::SIZE, $this->size) - 1; + + $this->dbg && print "Attaching new request to get range: $offset-$stop\n"; + + $req = new BigGetRequest( + $this->url, + HttpRequest::METH_GET, + array( + 'headers' => array( + 'Range' => "bytes=$offset-$stop" + ) + ) + ); + $this->attach($req); + $req->id = $this->count++; + } + + private function getRangeOffset() + { + return ($this->size >= $start = $this->count * self::SIZE) ? $start : -1; + } } @@ -213,7 +213,7 @@ class BigGet extends HttpRequestPool */ class BigGetRequest extends HttpRequest { - public $id; + public $id; } ?> diff --git a/lib/FeedAggregator.php b/lib/FeedAggregator.php index de2ae0b..689e788 100644 --- a/lib/FeedAggregator.php +++ b/lib/FeedAggregator.php @@ -16,172 +16,172 @@ class FeedAggregator * * @var string */ - public $directory; - - /** - * Feeds - * - * @var array - */ - protected $feeds = array(); - - /** - * Constructor - * - * @param string $directory - */ - public function __construct($directory = 'feeds') - { - $this->setDirectory($directory); - } - - /** - * Set cache directory - * - * @param string $directory - */ - public function setDirectory($directory) - { - $this->directory = $directory; - foreach (glob($this->directory .'/*.xml') as $feed) { - $this->feeds[basename($feed, '.xml')] = filemtime($feed); - } - } - - /** - * Strips all special chars - * - * @param string $url - * @return string - */ - public function url2name($url) - { - return preg_replace('/[^\w\.-]+/', '_', $url); - } - - /** - * Checks if $url is a known feed - * - * @param string $url - * @return bool - */ - public function hasFeed($url) - { - return isset($this->feeds[$this->url2name($url)]); - } - - /** - * Add an URL as feed - * - * @param string $url - * @return void - * @throws Exception - */ - public function addFeed($url) - { - $r = $this->setupRequest($url); - $r->send(); - $this->handleResponse($r); - } - - /** - * Add several URLs as feeds - * - * @param array $urls - * @return void - * @throws Exception - */ - public function addFeeds(array $urls) - { - $pool = new HttpRequestPool; - foreach ($urls as $url) { - $pool->attach($r = $this->setupRequest($url)); - } - $pool->send(); - - foreach ($pool as $request) { - $this->handleResponse($request); - } - } - - /** - * Load a feed (from cache) - * - * @param string $url - * @return string - * @throws Exception - */ - public function getFeed($url) - { - $this->addFeed($url); - return $this->loadFeed($this->url2name($url)); - } - - /** - * Load several feeds (from cache) - * - * @param array $urls - * @return array - * @throws Exception - */ - public function getFeeds(array $urls) - { - $feeds = array(); - $this->addFeeds($urls); - foreach ($urls as $url) { - $feeds[] = $this->loadFeed($this->url2name($url)); - } - return $feeds; - } - - protected function saveFeed($file, $contents) - { - if (file_put_contents($this->directory .'/'. $file .'.xml', $contents)) { - $this->feeds[$file] = time(); - } else { - throw new Exception("Could not save feed contents to $file.xml"); - } - } - - protected function loadFeed($file) - { - if (isset($this->feeds[$file])) { - if ($data = file_get_contents($this->directory .'/'. $file .'.xml')) { - return $data; - } else { - throw new Exception("Could not load feed contents from $file.xml"); - } - } else { - throw new Exception("Unknown feed/file $file.xml"); - } - } - - protected function setupRequest($url, $escape = true) - { - $r = new HttpRequest($url); - $r->setOptions(array('redirect' => true)); - - $file = $escape ? $this->url2name($url) : $url; - - if (isset($this->feeds[$file])) { - $r->setOptions(array('lastmodified' => $this->feeds[$file])); - } - - return $r; - } - - protected function handleResponse(HttpRequest $r) - { - if ($r->getResponseCode() != 304) { - if ($r->getResponseCode() != 200) { - throw new Exception("Unexpected response code ". $r->getResponseCode()); - } - if (!strlen($body = $r->getResponseBody())) { - throw new Exception("Received empty feed from ". $r->getUrl()); - } - $this->saveFeed($this->url2name($r->getUrl()), $body); - } - } + public $directory; + + /** + * Feeds + * + * @var array + */ + protected $feeds = array(); + + /** + * Constructor + * + * @param string $directory + */ + public function __construct($directory = 'feeds') + { + $this->setDirectory($directory); + } + + /** + * Set cache directory + * + * @param string $directory + */ + public function setDirectory($directory) + { + $this->directory = $directory; + foreach (glob($this->directory .'/*.xml') as $feed) { + $this->feeds[basename($feed, '.xml')] = filemtime($feed); + } + } + + /** + * Strips all special chars + * + * @param string $url + * @return string + */ + public function url2name($url) + { + return preg_replace('/[^\w\.-]+/', '_', $url); + } + + /** + * Checks if $url is a known feed + * + * @param string $url + * @return bool + */ + public function hasFeed($url) + { + return isset($this->feeds[$this->url2name($url)]); + } + + /** + * Add an URL as feed + * + * @param string $url + * @return void + * @throws Exception + */ + public function addFeed($url) + { + $r = $this->setupRequest($url); + $r->send(); + $this->handleResponse($r); + } + + /** + * Add several URLs as feeds + * + * @param array $urls + * @return void + * @throws Exception + */ + public function addFeeds(array $urls) + { + $pool = new HttpRequestPool; + foreach ($urls as $url) { + $pool->attach($r = $this->setupRequest($url)); + } + $pool->send(); + + foreach ($pool as $request) { + $this->handleResponse($request); + } + } + + /** + * Load a feed (from cache) + * + * @param string $url + * @return string + * @throws Exception + */ + public function getFeed($url) + { + $this->addFeed($url); + return $this->loadFeed($this->url2name($url)); + } + + /** + * Load several feeds (from cache) + * + * @param array $urls + * @return array + * @throws Exception + */ + public function getFeeds(array $urls) + { + $feeds = array(); + $this->addFeeds($urls); + foreach ($urls as $url) { + $feeds[] = $this->loadFeed($this->url2name($url)); + } + return $feeds; + } + + protected function saveFeed($file, $contents) + { + if (file_put_contents($this->directory .'/'. $file .'.xml', $contents)) { + $this->feeds[$file] = time(); + } else { + throw new Exception("Could not save feed contents to $file.xml"); + } + } + + protected function loadFeed($file) + { + if (isset($this->feeds[$file])) { + if ($data = file_get_contents($this->directory .'/'. $file .'.xml')) { + return $data; + } else { + throw new Exception("Could not load feed contents from $file.xml"); + } + } else { + throw new Exception("Unknown feed/file $file.xml"); + } + } + + protected function setupRequest($url, $escape = true) + { + $r = new HttpRequest($url); + $r->setOptions(array('redirect' => true)); + + $file = $escape ? $this->url2name($url) : $url; + + if (isset($this->feeds[$file])) { + $r->setOptions(array('lastmodified' => $this->feeds[$file])); + } + + return $r; + } + + protected function handleResponse(HttpRequest $r) + { + if ($r->getResponseCode() != 304) { + if ($r->getResponseCode() != 200) { + throw new Exception("Unexpected response code ". $r->getResponseCode()); + } + if (!strlen($body = $r->getResponseBody())) { + throw new Exception("Received empty feed from ". $r->getUrl()); + } + $this->saveFeed($this->url2name($r->getUrl()), $body); + } + } } ?> diff --git a/lib/XmlRpcClient.php b/lib/XmlRpcClient.php index f36347d..886198b 100644 --- a/lib/XmlRpcClient.php +++ b/lib/XmlRpcClient.php @@ -11,7 +11,7 @@ * listdomain(array('domain' => 'example.com')); + * print_r($rpc->listdomain(array('domain' => 'example.com'))); * } catch (Exception $ex) { * echo $ex; * } @@ -30,69 +30,69 @@ class XmlRpcClient * * @var string */ - public $namespace; - - /** - * HttpRequest instance - * - * @var HttpRequest - */ - protected $request; + public $namespace; + + /** + * HttpRequest instance + * + * @var HttpRequest + */ + protected $request; - /** - * Constructor - * - * @param string $url RPC endpoint - * @param string $namespace RPC namespace - */ - public function __construct($url, $namespace = '') - { - $this->namespace = $namespace; - $this->request = new HttpRequest($url, HTTP_METH_POST); - $this->request->setContentType('text/xml'); - } + /** + * Constructor + * + * @param string $url RPC endpoint + * @param string $namespace RPC namespace + */ + public function __construct($url, $namespace = '') + { + $this->namespace = $namespace; + $this->request = new HttpRequest($url, HTTP_METH_POST); + $this->request->setContentType('text/xml'); + } - /** - * Proxy to HttpRequest::setOptions() - * - * @param array $options - * @return unknown - */ - public function setOptions(array $options = null) - { - return $this->request->setOptions($options); - } - - /** - * Get associated HttpRequest instance - * - * @return HttpRequest - */ - public function getRequest() - { - return $this->request; - } + /** + * Proxy to HttpRequest::setOptions() + * + * @param array $options + * @return unknown + */ + public function setOptions(array $options = null) + { + return $this->request->setOptions($options); + } + + /** + * Get associated HttpRequest instance + * + * @return HttpRequest + */ + public function getRequest() + { + return $this->request; + } - /** - * RPC method proxy - * - * @param string $method RPC method name - * @param array $params RPC method arguments - * @return mixed decoded RPC response - * @throws Exception - */ - public function __call($method, array $params) - { - if ($this->namespace) { - $method = $this->namespace .'.'. $method; - } - $this->request->setRawPostData(xmlrpc_encode_request($method, $params)); - $response = $this->request->send(); - if ($response->getResponseCode() != 200) { - throw new Exception($response->getBody(), $response->getResponseCode()); - } - return xmlrpc_decode($response->getBody(), 'utf-8'); - } + /** + * RPC method proxy + * + * @param string $method RPC method name + * @param array $params RPC method arguments + * @return mixed decoded RPC response + * @throws Exception + */ + public function __call($method, array $params) + { + if ($this->namespace) { + $method = $this->namespace .'.'. $method; + } + $this->request->setRawPostData(xmlrpc_encode_request($method, $params)); + $response = $this->request->send(); + if ($response->getResponseCode() != 200) { + throw new Exception($response->getBody(), $response->getResponseCode()); + } + return xmlrpc_decode($response->getBody(), 'utf-8'); + } } ?> -- 2.30.2