]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/ping.php
Merge branch '1.0.x' into shortcontrol10x
[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
24         if ($notice->is_local != Notice::LOCAL_PUBLIC && $notice->is_local != Notice::LOCAL_NONPUBLIC) {
25                 return true;
26         }
27
28         # Array of servers, URL => type
29         $notify = common_config('ping', 'notify');
30         $profile = $notice->getProfile();
31         $tags = ping_notice_tags($notice);
32
33         foreach ($notify as $notify_url => $type) {
34                 switch ($type) {
35                  case 'xmlrpc':
36                  case 'extended':
37                         $req = xmlrpc_encode_request('weblogUpdates.ping',
38                                                                                  array($profile->nickname, # site name
39                                                                                            common_local_url('showstream',
40                                                                                                                                 array('nickname' => $profile->nickname)),
41                                                                                            common_local_url('shownotice',
42                                                                                                                                 array('notice' => $notice->id)),
43                                                                                            common_local_url('userrss',
44                                                                                                                                 array('nickname' => $profile->nickname)),
45                                                                                            $tags));
46
47             $request = HTTPClient::start();
48             $request->setConfig('connect_timeout', common_config('ping', 'timeout'));
49             $request->setConfig('timeout', common_config('ping', 'timeout'));
50             try {
51                 $httpResponse = $request->post($notify_url, array('Content-Type: text/xml'), $req);
52             } catch (Exception $e) {
53                 common_log(LOG_ERR,
54                            "Exception pinging $notify_url: " . $e->getMessage());
55                 continue;
56             }
57
58             if (!$httpResponse || mb_strlen($httpResponse->getBody()) == 0) {
59                 common_log(LOG_WARNING,
60                            "XML-RPC empty results for ping ($notify_url, $notice->id) ");
61                 continue;
62             }
63
64             $response = xmlrpc_decode($httpResponse->getBody());
65
66             if (is_array($response) && xmlrpc_is_fault($response)) {
67                 common_log(LOG_WARNING,
68                            "XML-RPC error for ping ($notify_url, $notice->id) ".
69                            "$response[faultString] ($response[faultCode])");
70             } else {
71                 common_log(LOG_INFO,
72                            "Ping success for $notify_url $notice->id");
73             }
74             break;
75
76                  case 'get':
77                  case 'post':
78             $args = array('name' => $profile->nickname,
79                           'url' => common_local_url('showstream',
80                                                     array('nickname' => $profile->nickname)),
81                           'changesURL' => common_local_url('userrss',
82                                                            array('nickname' => $profile->nickname)));
83
84             $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
85
86             if ($type === 'get') {
87                 $result = $fetcher->get($notify_url . '?' . http_build_query($args),
88                                         array('User-Agent: StatusNet/'.STATUSNET_VERSION));
89             } else {
90                 $result = $fetcher->post($notify_url,
91                                          http_build_query($args),
92                                          array('User-Agent: StatusNet/'.STATUSNET_VERSION));
93             }
94             if ($result->status != '200') {
95                 common_log(LOG_WARNING,
96                            "Ping error for '$notify_url' ($notice->id): ".
97                            "$result->body");
98             } else {
99                 common_log(LOG_INFO,
100                            "Ping success for '$notify_url' ($notice->id): ".
101                            "'$result->body'");
102             }
103             break;
104
105                  default:
106                         common_log(LOG_WARNING, 'Unknown notify type for ' . $notify_url . ': ' . $type);
107         }
108         }
109
110     return true;
111 }
112
113 function ping_notice_tags($notice) {
114         $tag = new Notice_tag();
115         $tag->notice_id = $notice->id;
116         $tags = array();
117         if ($tag->find()) {
118                 while ($tag->fetch()) {
119                         $tags[] = $tag->tag;
120                 }
121                 $tag->free();
122                 unset($tag);
123                 return implode('|', $tags);
124         }
125         return NULL;
126 }