ping.php 5.3 KB
Newer Older
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21 22

function ping_broadcast_notice($notice) {
23 24 25
    if ($notice->is_local != Notice::LOCAL_PUBLIC && $notice->is_local != Notice::LOCAL_NONPUBLIC) {
        return true;
    }
26

27
    // Array of servers, URL => type
28
    $notify = common_config('ping', 'notify');
29 30 31 32 33 34 35 36
    try {
        $profile = $notice->getProfile();
    } catch (Exception $e) {
        // @todo: distinguish the 'broken notice/profile' case from more general
        //        transitory errors.
        common_log(LOG_ERR, "Exception getting notice profile: " . $e->getMessage());
        return true;
    }
37
    $tags = ping_notice_tags($notice);
38

39 40 41 42 43 44 45 46 47 48 49 50 51
    foreach ($notify as $notify_url => $type) {
        switch ($type) {
         case 'xmlrpc':
         case 'extended':
            $req = xmlrpc_encode_request('weblogUpdates.ping',
                                         array($profile->nickname, # site name
                                               common_local_url('showstream',
                                                                array('nickname' => $profile->nickname)),
                                               common_local_url('shownotice',
                                                                array('notice' => $notice->id)),
                                               common_local_url('userrss',
                                                                array('nickname' => $profile->nickname)),
                                               $tags));
52

53
            $request = HTTPClient::start();
54 55 56 57 58 59 60 61 62
            $request->setConfig('connect_timeout', common_config('ping', 'timeout'));
            $request->setConfig('timeout', common_config('ping', 'timeout'));
            try {
                $httpResponse = $request->post($notify_url, array('Content-Type: text/xml'), $req);
            } catch (Exception $e) {
                common_log(LOG_ERR,
                           "Exception pinging $notify_url: " . $e->getMessage());
                continue;
            }
63

64
            if (!$httpResponse || mb_strlen($httpResponse->getBody()) == 0) {
65 66 67 68 69
                common_log(LOG_WARNING,
                           "XML-RPC empty results for ping ($notify_url, $notice->id) ");
                continue;
            }

70
            $response = xmlrpc_decode($httpResponse->getBody());
71

72
            if (is_array($response) && xmlrpc_is_fault($response)) {
73 74 75 76 77 78 79 80
                common_log(LOG_WARNING,
                           "XML-RPC error for ping ($notify_url, $notice->id) ".
                           "$response[faultString] ($response[faultCode])");
            } else {
                common_log(LOG_INFO,
                           "Ping success for $notify_url $notice->id");
            }
            break;
81 82
         case 'get':
         case 'post':
83 84 85 86 87 88 89 90 91 92
            $args = array('name' => $profile->nickname,
                          'url' => common_local_url('showstream',
                                                    array('nickname' => $profile->nickname)),
                          'changesURL' => common_local_url('userrss',
                                                           array('nickname' => $profile->nickname)));

            $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();

            if ($type === 'get') {
                $result = $fetcher->get($notify_url . '?' . http_build_query($args),
93
                                        array('User-Agent: ' . HTTPClient::userAgent()));
94 95 96
            } else {
                $result = $fetcher->post($notify_url,
                                         http_build_query($args),
97
                                         array('User-Agent: ' . HTTPClient::userAgent()));
98 99 100 101 102 103 104 105 106 107 108
            }
            if ($result->status != '200') {
                common_log(LOG_WARNING,
                           "Ping error for '$notify_url' ($notice->id): ".
                           "$result->body");
            } else {
                common_log(LOG_INFO,
                           "Ping success for '$notify_url' ($notice->id): ".
                           "'$result->body'");
            }
            break;
109 110
         default:
            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 119 120 121 122 123 124 125 126 127 128 129
    $tag = new Notice_tag();
    $tag->notice_id = $notice->id;
    $tags = array();
    if ($tag->find()) {
        while ($tag->fetch()) {
            $tags[] = $tag->tag;
        }
        $tag->free();
        unset($tag);
        return implode('|', $tags);
    }
    return NULL;
130
}