summaryrefslogtreecommitdiff
path: root/lib/ping.php
blob: 2cbec2f212484b1f49f1b3991997c95bf61c99fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/*
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2009, Control Yourself, Inc.
 *
 * 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/>.
 */

if (!defined('LACONICA')) { exit(1); }

function ping_broadcast_notice($notice) {

	if (!$notice->is_local) {
		return true;
	}

	# Array of servers, URL => type
	$notify = common_config('ping', 'notify');
	$profile = $notice->getProfile();
	$tags = ping_notice_tags($notice);

	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));

            $context = stream_context_create(array('http' => array('method' => "POST",
                                                                   'header' =>
                                                                   "Content-Type: text/xml\r\n".
                                                                   "User-Agent: Laconica/".LACONICA_VERSION."\r\n",
                                                                   'content' => $request)));
            $file = file_get_contents($notify_url, false, $context);
            $response = xmlrpc_decode($file);
            if (xmlrpc_is_fault($response)) {
                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;

		 case 'get':
		 case 'post':
            $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),
                                        array('User-Agent: Laconica/'.LACONICA_VERSION));
            } else {
                $result = $fetcher->post($notify_url,
                                         http_build_query($args),
                                         array('User-Agent: Laconica/'.LACONICA_VERSION));
            }
            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;

		 default:
			common_log(LOG_WARNING, 'Unknown notify type for ' . $notify_url . ': ' . $type);
        }
	}

    return true;
}

function ping_notice_tags($notice) {
	$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;
}