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