123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class ActivitySink
- {
- protected $svcDocUrl = null;
- protected $username = null;
- protected $password = null;
- protected $collections = array();
- function __construct($svcDocUrl, $username, $password)
- {
- $this->svcDocUrl = $svcDocUrl;
- $this->username = $username;
- $this->password = $password;
- $this->_parseSvcDoc();
- }
- private function _parseSvcDoc()
- {
- $client = new HTTPClient();
- $response = $client->get($this->svcDocUrl);
- if ($response->getStatus() != 200) {
- throw new Exception("Can't get {$this->svcDocUrl}; response status " . $response->getStatus());
- }
- $xml = $response->getBody();
- $dom = new DOMDocument();
-
- $dom->preserveWhiteSpace = false;
-
- $old = error_reporting();
- error_reporting($old & ~E_WARNING);
- $ok = $dom->loadXML($xml);
- error_reporting($old);
- $path = new DOMXPath($dom);
- $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
- $path->registerNamespace('app', 'http://www.w3.org/2007/app');
- $path->registerNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
- $collections = $path->query('//app:collection');
- for ($i = 0; $i < $collections->length; $i++) {
- $collection = $collections->item($i);
- $url = $collection->getAttribute('href');
- $takesEntries = false;
- $accepts = $path->query('app:accept', $collection);
- for ($j = 0; $j < $accepts->length; $j++) {
- $accept = $accepts->item($j);
- $acceptValue = $accept->nodeValue;
- if (preg_match('#application/atom\+xml(;\s*type=entry)?#', $acceptValue)) {
- $takesEntries = true;
- break;
- }
- }
- if (!$takesEntries) {
- continue;
- }
- $verbs = $path->query('activity:verb', $collection);
- if ($verbs->length == 0) {
- $this->_addCollection(ActivityVerb::POST, $url);
- } else {
- for ($k = 0; $k < $verbs->length; $k++) {
- $verb = $verbs->item($k);
- $this->_addCollection($verb->nodeValue, $url);
- }
- }
- }
- }
- private function _addCollection($verb, $url)
- {
- if (array_key_exists($verb, $this->collections)) {
- $this->collections[$verb][] = $url;
- } else {
- $this->collections[$verb] = array($url);
- }
- return;
- }
- function postActivity($activity)
- {
- if (!array_key_exists($activity->verb, $this->collections)) {
- throw new Exception("No collection for verb {$activity->verb}");
- } else {
- if (count($this->collections[$activity->verb]) > 1) {
- common_log(LOG_NOTICE, "More than one collection for verb {$activity->verb}");
- }
- $this->postToCollection($this->collections[$activity->verb][0], $activity);
- }
- }
- function postToCollection($url, $activity)
- {
- $client = new HTTPClient($url);
- $client->setMethod('POST');
- $client->setAuth($this->username, $this->password);
- $client->setHeader('Content-Type', 'application/atom+xml;type=entry');
- $client->setBody($activity->asString(true, true, true));
- $response = $client->send();
- $status = $response->getStatus();
- $reason = $response->getReasonPhrase();
- if ($status >= 200 && $status < 300) {
- return true;
- } else if ($status >= 400 && $status < 500) {
-
-
- throw new ClientException(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
- } else if ($status >= 500 && $status < 600) {
-
-
- throw new ServerException(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
- } else {
-
-
-
- throw new Exception(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
- }
- }
- }
|