]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/ping.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / lib / ping.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 function ping_broadcast_notice($notice) {
23     if ($notice->is_local != Notice::LOCAL_PUBLIC && $notice->is_local != Notice::LOCAL_NONPUBLIC) {
24         return true;
25     }
26
27     // Array of servers, URL => type
28     $notify = common_config('ping', 'notify');
29     try {
30         $profile = $notice->getProfile();
31     } catch (Exception $e) {
32         // @todo: distinguish the 'broken notice/profile' case from more general
33         //        transitory errors.
34         common_log(LOG_ERR, "Exception getting notice profile: " . $e->getMessage());
35         return true;
36     }
37     $tags = ping_notice_tags($notice);
38
39     foreach ($notify as $notify_url => $type) {
40         switch ($type) {
41          case 'xmlrpc':
42          case 'extended':
43             $req = xmlrpc_encode_request('weblogUpdates.ping',
44                                          array($profile->nickname, # site name
45                                                common_local_url('showstream',
46                                                                 array('nickname' => $profile->nickname)),
47                                                common_local_url('shownotice',
48                                                                 array('notice' => $notice->id)),
49                                                common_local_url('userrss',
50                                                                 array('nickname' => $profile->nickname)),
51                                                $tags));
52
53             $request = HTTPClient::start();
54             $request->setConfig('connect_timeout', common_config('ping', 'timeout'));
55             $request->setConfig('timeout', common_config('ping', 'timeout'));
56             try {
57                 $httpResponse = $request->post($notify_url, array('Content-Type: text/xml'), $req);
58             } catch (Exception $e) {
59                 common_log(LOG_ERR,
60                            "Exception pinging $notify_url: " . $e->getMessage());
61                 continue;
62             }
63
64             if (!$httpResponse || mb_strlen($httpResponse->getBody()) == 0) {
65                 common_log(LOG_WARNING,
66                            "XML-RPC empty results for ping ($notify_url, $notice->id) ");
67                 continue;
68             }
69
70             $response = xmlrpc_decode($httpResponse->getBody());
71
72             if (is_array($response) && xmlrpc_is_fault($response)) {
73                 common_log(LOG_WARNING,
74                            "XML-RPC error for ping ($notify_url, $notice->id) ".
75                            "$response[faultString] ($response[faultCode])");
76             } else {
77                 common_log(LOG_INFO,
78                            "Ping success for $notify_url $notice->id");
79             }
80             break;
81          case 'get':
82          case 'post':
83             $args = array('name' => $profile->nickname,
84                           'url' => common_local_url('showstream',
85                                                     array('nickname' => $profile->nickname)),
86                           'changesURL' => common_local_url('userrss',
87                                                            array('nickname' => $profile->nickname)));
88
89             $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
90
91             if ($type === 'get') {
92                 $result = $fetcher->get($notify_url . '?' . http_build_query($args),
93                                         array('User-Agent: ' . HTTPClient::userAgent()));
94             } else {
95                 $result = $fetcher->post($notify_url,
96                                          http_build_query($args),
97                                          array('User-Agent: ' . HTTPClient::userAgent()));
98             }
99             if ($result->status != '200') {
100                 common_log(LOG_WARNING,
101                            "Ping error for '$notify_url' ($notice->id): ".
102                            "$result->body");
103             } else {
104                 common_log(LOG_INFO,
105                            "Ping success for '$notify_url' ($notice->id): ".
106                            "'$result->body'");
107             }
108             break;
109          default:
110             common_log(LOG_WARNING, 'Unknown notify type for ' . $notify_url . ': ' . $type);
111         }
112     }
113
114     return true;
115 }
116
117 function ping_notice_tags($notice) {
118     $tag = new Notice_tag();
119     $tag->notice_id = $notice->id;
120     $tags = array();
121     if ($tag->find()) {
122         while ($tag->fetch()) {
123             $tags[] = $tag->tag;
124         }
125         $tag->free();
126         unset($tag);
127         return implode('|', $tags);
128     }
129     return NULL;
130 }