123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class RSSCloudRequestNotifyAction extends Action
- {
-
- function prepare(array $args = array())
- {
- parent::prepare($args);
- $this->ip = $_SERVER['REMOTE_ADDR'];
- $this->port = $this->arg('port');
- $this->path = $this->arg('path');
- if ($this->path[0] != '/') {
- $this->path = '/' . $this->path;
- }
- $this->protocol = $this->arg('protocol');
- $this->procedure = $this->arg('notifyProcedure');
- $this->domain = $this->arg('domain');
- $this->feeds = $this->getFeeds();
- return true;
- }
-
- function handle()
- {
- parent::handle();
- if ($_SERVER['REQUEST_METHOD'] != 'POST') {
-
- $this->showResult(false, _m('Request must be POST.'));
- return;
- }
- $missing = array();
- if (empty($this->port)) {
- $missing[] = 'port';
- }
- if (empty($this->path)) {
- $missing[] = 'path';
- }
- if (empty($this->protocol)) {
- $missing[] = 'protocol';
- } else if (strtolower($this->protocol) != 'http-post') {
-
- $msg = _m('Only HTTP POST notifications are supported at this time.');
- $this->showResult(false, $msg);
- return;
- }
- if (!isset($this->procedure)) {
- $missing[] = 'notifyProcedure';
- }
- if (!empty($missing)) {
-
- $separator = _m('SEPARATOR',', ');
-
-
- $msg = sprintf(_m('The following parameters were missing from the request body: %s.'),implode($separator, $missing));
- $this->showResult(false, $msg);
- return;
- }
- if (empty($this->feeds)) {
-
- $msg = _m('You must provide at least one valid profile feed URL ' .
- '(url1, url2, url3 ... urlN).');
- $this->showResult(false, $msg);
- return;
- }
-
-
-
- foreach ($this->feeds as $feed) {
- if (!$this->validateFeed($feed)) {
- $nh = $this->getNotifyUrl();
- common_log(LOG_WARNING,
- "RSSCloud plugin - $nh tried to subscribe to invalid feed: $feed");
-
- $msg = _m('Feed subscription failed: Not a valid feed.');
- $this->showResult(false, $msg);
- return;
- }
- if (!$this->testNotificationHandler($feed)) {
-
- $msg = _m('Feed subscription failed: ' .
- 'Notification handler does not respond correctly.');
- $this->showResult(false, $msg);
- return;
- }
- }
- foreach ($this->feeds as $feed) {
- $this->saveSubscription($feed);
- }
-
-
-
-
- $msg = _m('Thanks for the subscription. ' .
- 'When the feed(s) update(s), you will be notified.');
- $this->showResult(true, $msg);
- }
-
- function validateFeed($feed)
- {
- $user = $this->userFromFeed($feed);
- if (empty($user)) {
- return false;
- }
- return true;
- }
-
- function getFeeds()
- {
- $feeds = array();
- while (list($key, $feed) = each($this->args)) {
- if (preg_match('/^url\d*$/', $key)) {
- $feeds[] = $feed;
- }
- }
- return $feeds;
- }
-
- function testNotificationHandler($feed)
- {
- $notifyUrl = $this->getNotifyUrl();
- $notifier = new RSSCloudNotifier();
- if (isset($this->domain)) {
-
- common_log(LOG_INFO,
- 'RSSCloud plugin - Testing notification handler with challenge: ' .
- $notifyUrl);
- return $notifier->challenge($notifyUrl, $feed);
- } else {
- common_log(LOG_INFO, 'RSSCloud plugin - Testing notification handler: ' .
- $notifyUrl);
- return $notifier->postUpdate($notifyUrl, $feed);
- }
- }
-
- function getNotifyUrl()
- {
- if (isset($this->domain)) {
- return 'http://' . $this->domain . ':' . $this->port . $this->path;
- } else {
- return 'http://' . $this->ip . ':' . $this->port . $this->path;
- }
- }
-
- function userFromFeed($feed)
- {
-
-
- $path = common_path('api/statuses/user_timeline/');
- $valid = '%^' . $path . '(?<id>.*)\.rss$%';
- if (preg_match($valid, $feed, $matches)) {
- $user = User::getKV('id', $matches['id']);
- if (!empty($user)) {
- return $user;
- }
- }
- return false;
- }
-
- function saveSubscription($feed)
- {
- $user = $this->userFromFeed($feed);
- $notifyUrl = $this->getNotifyUrl();
- $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
- if ($sub) {
- common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl refreshed subscription" .
- " to user $user->nickname (id: $user->id).");
- } else {
- $sub = new RSSCloudSubscription();
- $sub->subscribed = $user->id;
- $sub->url = $notifyUrl;
- $sub->created = common_sql_now();
- if (!$sub->insert()) {
- common_log_db_error($sub, 'INSERT', __FILE__);
- return false;
- }
- common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl subscribed" .
- " to user $user->nickname (id: $user->id)");
- }
- return true;
- }
-
- function showResult($success, $msg)
- {
- $this->startXML();
- $this->elementStart('notifyResult',
- array('success' => ($success) ? 'true' : 'false',
- 'msg' => $msg));
- $this->endXML();
- }
- }
|