activitysink.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * A remote, atompub-receiving service
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category AtomPub
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * A remote service that supports AtomPub
  37. *
  38. * @category AtomPub
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class ActivitySink
  46. {
  47. protected $svcDocUrl = null;
  48. protected $username = null;
  49. protected $password = null;
  50. protected $collections = array();
  51. function __construct($svcDocUrl, $username, $password)
  52. {
  53. $this->svcDocUrl = $svcDocUrl;
  54. $this->username = $username;
  55. $this->password = $password;
  56. $this->_parseSvcDoc();
  57. }
  58. private function _parseSvcDoc()
  59. {
  60. $client = new HTTPClient();
  61. $response = $client->get($this->svcDocUrl);
  62. if ($response->getStatus() != 200) {
  63. throw new Exception("Can't get {$this->svcDocUrl}; response status " . $response->getStatus());
  64. }
  65. $xml = $response->getBody();
  66. $dom = new DOMDocument();
  67. // We don't want to bother with white spaces
  68. $dom->preserveWhiteSpace = false;
  69. // Don't spew XML warnings to output
  70. $old = error_reporting();
  71. error_reporting($old & ~E_WARNING);
  72. $ok = $dom->loadXML($xml);
  73. error_reporting($old);
  74. $path = new DOMXPath($dom);
  75. $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  76. $path->registerNamespace('app', 'http://www.w3.org/2007/app');
  77. $path->registerNamespace('activity', 'http://activitystrea.ms/spec/1.0/');
  78. $collections = $path->query('//app:collection');
  79. for ($i = 0; $i < $collections->length; $i++) {
  80. $collection = $collections->item($i);
  81. $url = $collection->getAttribute('href');
  82. $takesEntries = false;
  83. $accepts = $path->query('app:accept', $collection);
  84. for ($j = 0; $j < $accepts->length; $j++) {
  85. $accept = $accepts->item($j);
  86. $acceptValue = $accept->nodeValue;
  87. if (preg_match('#application/atom\+xml(;\s*type=entry)?#', $acceptValue)) {
  88. $takesEntries = true;
  89. break;
  90. }
  91. }
  92. if (!$takesEntries) {
  93. continue;
  94. }
  95. $verbs = $path->query('activity:verb', $collection);
  96. if ($verbs->length == 0) {
  97. $this->_addCollection(ActivityVerb::POST, $url);
  98. } else {
  99. for ($k = 0; $k < $verbs->length; $k++) {
  100. $verb = $verbs->item($k);
  101. $this->_addCollection($verb->nodeValue, $url);
  102. }
  103. }
  104. }
  105. }
  106. private function _addCollection($verb, $url)
  107. {
  108. if (array_key_exists($verb, $this->collections)) {
  109. $this->collections[$verb][] = $url;
  110. } else {
  111. $this->collections[$verb] = array($url);
  112. }
  113. return;
  114. }
  115. function postActivity($activity)
  116. {
  117. if (!array_key_exists($activity->verb, $this->collections)) {
  118. throw new Exception("No collection for verb {$activity->verb}");
  119. } else {
  120. if (count($this->collections[$activity->verb]) > 1) {
  121. common_log(LOG_NOTICE, "More than one collection for verb {$activity->verb}");
  122. }
  123. $this->postToCollection($this->collections[$activity->verb][0], $activity);
  124. }
  125. }
  126. function postToCollection($url, $activity)
  127. {
  128. $client = new HTTPClient($url);
  129. $client->setMethod('POST');
  130. $client->setAuth($this->username, $this->password);
  131. $client->setHeader('Content-Type', 'application/atom+xml;type=entry');
  132. $client->setBody($activity->asString(true, true, true));
  133. $response = $client->send();
  134. $status = $response->getStatus();
  135. $reason = $response->getReasonPhrase();
  136. if ($status >= 200 && $status < 300) {
  137. return true;
  138. } else if ($status >= 400 && $status < 500) {
  139. // TRANS: Client exception thrown when post to collection fails with a 400 status.
  140. // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason.
  141. throw new ClientException(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
  142. } else if ($status >= 500 && $status < 600) {
  143. // TRANS: Server exception thrown when post to collection fails with a 500 status.
  144. // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason.
  145. throw new ServerException(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
  146. } else {
  147. // That's unexpected.
  148. // TRANS: Exception thrown when post to collection fails with a status that is not handled.
  149. // TRANS: %1$s is a URL, %2$s is the status, %s$s is the fail reason.
  150. throw new Exception(sprintf(_m('URLSTATUSREASON','%1$s %2$s %3$s'), $url, $status, $reason));
  151. }
  152. }
  153. }