]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudNotifier.php
eb3198b5a43e489ae2a2f538a4afe33c68f740e7
[quix0rs-gnu-social.git] / plugins / RSSCloud / RSSCloudNotifier.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Class to ping an rssCloud endpoint when a feed has been updated
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18    *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 class RSSCloudNotifier {
35
36     function challenge($endpoint, $feed)
37     {
38         $code   = common_confirmation_code(128);
39         $params = array('url' => $feed, 'challenge' => $code);
40         $url    = $endpoint . '?' . http_build_query($params);
41
42         try {
43             $client = new HTTPClient();
44             $response = $client->get($url);
45         } catch (HTTP_Request2_Exception $e) {
46             common_log(LOG_INFO, 'RSSCloud plugin - failure testing notify handler ' .
47                        $endpoint . ' - '  . $e->getMessage());
48             return false;
49         }
50
51         // Check response is betweet 200 and 299 and body contains challenge data
52
53         $status = $response->getStatus();
54         $body   = $response->getBody();
55
56         if ($status >= 200 && $status < 300) {
57
58             if (strpos($body, $code) !== false) {
59                 common_log(LOG_INFO, 'RSSCloud plugin - ' .
60                            "success testing notify handler:  $endpoint");
61                 return true;
62             } else {
63                 common_log(LOG_INFO, 'RSSCloud plugin - ' .
64                           'challenge/repsonse failed for notify handler ' .
65                            $endpoint);
66                 common_debug('body = ' . var_export($body, true));
67                 return false;
68             }
69         } else {
70             common_log(LOG_INFO, 'RSSCloud plugin - ' .
71                        "failure testing notify handler:  $endpoint " .
72                        ' - got HTTP ' . $status);
73             common_debug('body = ' . var_export($body, true));
74             return false;
75         }
76     }
77
78     function postUpdate($endpoint, $feed) {
79
80         $headers  = array();
81         $postdata = array('url' => $feed);
82
83         try {
84             $client = new HTTPClient();
85             $response = $client->post($endpoint, $headers, $postdata);
86         } catch (HTTP_Request2_Exception $e) {
87             common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
88                        $endpoint . ' that feed ' . $feed .
89                        ' has changed: ' . $e->getMessage());
90             return false;
91         }
92
93         $status = $response->getStatus();
94
95         if ($status >= 200 && $status < 300) {
96             common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
97                        $endpoint . ' that feed ' . $feed . ' has changed.');
98             return true;
99         } else {
100             common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
101                        $endpoint . ' that feed ' . $feed .
102                        ' has changed: got HTTP ' . $status);
103             common_debug('body = ' . var_export($response->getBody(), true));
104             return false;
105         }
106     }
107
108 }
109