rsscloudrequestnotify.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * Action to let RSSCloud aggregators request update notification when
  4. * user profile feeds change.
  5. *
  6. * PHP version 5
  7. *
  8. * @category Plugin
  9. * @package StatusNet
  10. * @author Zach Copley <zach@status.net>
  11. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  12. * @link http://status.net/
  13. *
  14. * StatusNet - the distributed open-source microblogging tool
  15. * Copyright (C) 2009, StatusNet, Inc.
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Action class to handle RSSCloud notification (subscription) requests
  35. *
  36. * @category Plugin
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class RSSCloudRequestNotifyAction extends Action
  43. {
  44. /**
  45. * Initialization.
  46. *
  47. * @param array $args Web and URL arguments
  48. *
  49. * @return boolean false if user doesn't exist
  50. */
  51. function prepare($args)
  52. {
  53. parent::prepare($args);
  54. $this->ip = $_SERVER['REMOTE_ADDR'];
  55. $this->port = $this->arg('port');
  56. $this->path = $this->arg('path');
  57. if ($this->path[0] != '/') {
  58. $this->path = '/' . $this->path;
  59. }
  60. $this->protocol = $this->arg('protocol');
  61. $this->procedure = $this->arg('notifyProcedure');
  62. $this->domain = $this->arg('domain');
  63. $this->feeds = $this->getFeeds();
  64. return true;
  65. }
  66. /**
  67. * Handle the request
  68. *
  69. * Checks for all the required parameters for a subscription,
  70. * validates that the feed being subscribed to is real, and then
  71. * saves the subsctiption.
  72. *
  73. * @param array $args $_REQUEST data (unused)
  74. *
  75. * @return void
  76. */
  77. function handle($args)
  78. {
  79. parent::handle($args);
  80. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  81. // TRANS: Form validation error displayed when POST is not used.
  82. $this->showResult(false, _m('Request must be POST.'));
  83. return;
  84. }
  85. $missing = array();
  86. if (empty($this->port)) {
  87. $missing[] = 'port';
  88. }
  89. if (empty($this->path)) {
  90. $missing[] = 'path';
  91. }
  92. if (empty($this->protocol)) {
  93. $missing[] = 'protocol';
  94. } else if (strtolower($this->protocol) != 'http-post') {
  95. // TRANS: Form validation error displayed when HTTP POST is not used.
  96. $msg = _m('Only HTTP POST notifications are supported at this time.');
  97. $this->showResult(false, $msg);
  98. return;
  99. }
  100. if (!isset($this->procedure)) {
  101. $missing[] = 'notifyProcedure';
  102. }
  103. if (!empty($missing)) {
  104. // TRANS: List separator.
  105. $separator = _m('SEPARATOR',', ');
  106. // TRANS: Form validation error displayed when a request body is missing expected parameters.
  107. // TRANS: %s is a list of parameters separated by a list separator (default: ", ").
  108. $msg = sprintf(_m('The following parameters were missing from the request body: %s.'),implode($separator, $missing));
  109. $this->showResult(false, $msg);
  110. return;
  111. }
  112. if (empty($this->feeds)) {
  113. // TRANS: Form validation error displayed when not providing any valid profile feed URLs.
  114. $msg = _m('You must provide at least one valid profile feed URL ' .
  115. '(url1, url2, url3 ... urlN).');
  116. $this->showResult(false, $msg);
  117. return;
  118. }
  119. // We have to validate everything before saving anything.
  120. // We only return one success or failure no matter how
  121. // many feeds the subscriber is trying to subscribe to
  122. foreach ($this->feeds as $feed) {
  123. if (!$this->validateFeed($feed)) {
  124. $nh = $this->getNotifyUrl();
  125. common_log(LOG_WARNING,
  126. "RSSCloud plugin - $nh tried to subscribe to invalid feed: $feed");
  127. // TRANS: Form validation error displayed when not providing a valid feed URL.
  128. $msg = _m('Feed subscription failed: Not a valid feed.');
  129. $this->showResult(false, $msg);
  130. return;
  131. }
  132. if (!$this->testNotificationHandler($feed)) {
  133. // TRANS: Form validation error displayed when feed subscription failed.
  134. $msg = _m('Feed subscription failed: ' .
  135. 'Notification handler does not respond correctly.');
  136. $this->showResult(false, $msg);
  137. return;
  138. }
  139. }
  140. foreach ($this->feeds as $feed) {
  141. $this->saveSubscription($feed);
  142. }
  143. // XXX: What to do about deleting stale subscriptions?
  144. // 25 hours seems harsh. WordPress doesn't ever remove
  145. // subscriptions.
  146. // TRANS: Success message after subscribing to one or more feeds.
  147. $msg = _m('Thanks for the subscription. ' .
  148. 'When the feed(s) update(s), you will be notified.');
  149. $this->showResult(true, $msg);
  150. }
  151. /**
  152. * Validate that the requested feed is one we serve
  153. * up via RSSCloud.
  154. *
  155. * @param string $feed the feed in question
  156. *
  157. * @return void
  158. */
  159. function validateFeed($feed)
  160. {
  161. $user = $this->userFromFeed($feed);
  162. if (empty($user)) {
  163. return false;
  164. }
  165. return true;
  166. }
  167. /**
  168. * Pull all of the urls (url1, url2, url3...urlN) that
  169. * the subscriber wants to subscribe to.
  170. *
  171. * @return array $feeds the list of feeds
  172. */
  173. function getFeeds()
  174. {
  175. $feeds = array();
  176. while (list($key, $feed) = each($this->args)) {
  177. if (preg_match('/^url\d*$/', $key)) {
  178. $feeds[] = $feed;
  179. }
  180. }
  181. return $feeds;
  182. }
  183. /**
  184. * Test that a notification handler is there and is reponding
  185. * correctly. This is called before adding a subscription.
  186. *
  187. * @param string $feed the feed to verify
  188. *
  189. * @return boolean success result
  190. */
  191. function testNotificationHandler($feed)
  192. {
  193. $notifyUrl = $this->getNotifyUrl();
  194. $notifier = new RSSCloudNotifier();
  195. if (isset($this->domain)) {
  196. // 'domain' param set, so we have to use GET and send a challenge
  197. common_log(LOG_INFO,
  198. 'RSSCloud plugin - Testing notification handler with challenge: ' .
  199. $notifyUrl);
  200. return $notifier->challenge($notifyUrl, $feed);
  201. } else {
  202. common_log(LOG_INFO, 'RSSCloud plugin - Testing notification handler: ' .
  203. $notifyUrl);
  204. return $notifier->postUpdate($notifyUrl, $feed);
  205. }
  206. }
  207. /**
  208. * Build the URL for the notification handler based on the
  209. * parameters passed in with the subscription request.
  210. *
  211. * @return string notification handler url
  212. */
  213. function getNotifyUrl()
  214. {
  215. if (isset($this->domain)) {
  216. return 'http://' . $this->domain . ':' . $this->port . $this->path;
  217. } else {
  218. return 'http://' . $this->ip . ':' . $this->port . $this->path;
  219. }
  220. }
  221. /**
  222. * Uses the nickname part of the subscribed feed URL to figure out
  223. * whethere there's really a user with such a feed. Used to
  224. * validate feeds before adding a subscription.
  225. *
  226. * @param string $feed the feed in question
  227. *
  228. * @return boolean success
  229. */
  230. function userFromFeed($feed)
  231. {
  232. // We only do canonical RSS2 profile feeds (specified by ID), e.g.:
  233. // http://www.example.com/api/statuses/user_timeline/2.rss
  234. $path = common_path('api/statuses/user_timeline/');
  235. $valid = '%^' . $path . '(?<id>.*)\.rss$%';
  236. if (preg_match($valid, $feed, $matches)) {
  237. $user = User::getKV('id', $matches['id']);
  238. if (!empty($user)) {
  239. return $user;
  240. }
  241. }
  242. return false;
  243. }
  244. /**
  245. * Save an RSSCloud subscription
  246. *
  247. * @param string $feed a valid profile feed
  248. *
  249. * @return boolean success result
  250. */
  251. function saveSubscription($feed)
  252. {
  253. $user = $this->userFromFeed($feed);
  254. $notifyUrl = $this->getNotifyUrl();
  255. $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
  256. if ($sub) {
  257. common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl refreshed subscription" .
  258. " to user $user->nickname (id: $user->id).");
  259. } else {
  260. $sub = new RSSCloudSubscription();
  261. $sub->subscribed = $user->id;
  262. $sub->url = $notifyUrl;
  263. $sub->created = common_sql_now();
  264. if (!$sub->insert()) {
  265. common_log_db_error($sub, 'INSERT', __FILE__);
  266. return false;
  267. }
  268. common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl subscribed" .
  269. " to user $user->nickname (id: $user->id)");
  270. }
  271. return true;
  272. }
  273. /**
  274. * Show an XML message indicating the subscription
  275. * was successful or failed.
  276. *
  277. * @param boolean $success whether it was good or bad
  278. * @param string $msg the message to output
  279. *
  280. * @return boolean success result
  281. */
  282. function showResult($success, $msg)
  283. {
  284. $this->startXML();
  285. $this->elementStart('notifyResult',
  286. array('success' => ($success) ? 'true' : 'false',
  287. 'msg' => $msg));
  288. $this->endXML();
  289. }
  290. }