3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A sample module to show best practices for StatusNet plugins
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author James Walker <james@status.net>
25 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27 * @link http://status.net/
31 const REL_SALMON = 'salmon';
32 const REL_MENTIONED = 'mentioned';
34 // XXX: these are deprecated
35 const NS_REPLIES = "http://salmon-protocol.org/ns/salmon-replies";
36 const NS_MENTIONS = "http://salmon-protocol.org/ns/salmon-mention";
39 * Sign and post the given Atom entry as a Salmon message.
41 * Side effects: may generate a keypair on-demand for the given user,
42 * which can be very slow on some systems (like those without php5-gmp).
44 * @param string $endpoint_uri
45 * @param string $xml string representation of payload
46 * @param User $user local user profile whose keys we sign with
47 * @return boolean success
49 public static function post($endpoint_uri, $xml, User $user)
51 if (empty($endpoint_uri)) {
52 common_debug('No endpoint URI for Salmon post to '.$user->getUri());
57 $magic_env = MagicEnvelope::signAsUser($xml, $user);
58 $envxml = $magic_env->toXML();
59 } catch (Exception $e) {
60 common_log(LOG_ERR, "Salmon unable to sign: " . $e->getMessage());
64 $headers = array('Content-Type: application/magic-envelope+xml');
67 $client = new HTTPClient();
68 $client->setBody($envxml);
69 $response = $client->post($endpoint_uri, $headers);
70 } catch (HTTP_Request2_Exception $e) {
71 common_log(LOG_ERR, "Salmon post to $endpoint_uri failed: " . $e->getMessage());
75 // Diaspora wants a slightly different formatting on the POST (other Content-type, so body needs "xml=")
76 if ($response->getStatus() === 422) {
77 common_debug(sprintf('Salmon (from profile %d) endpoint %s returned status %s. Diaspora? Will try again! Body: %s',
78 $user->id, $endpoint_uri, $response->getStatus(), $response->getBody()));
79 $headers = array('Content-Type: application/x-www-form-urlencoded');
80 $client->setBody('xml=' . Magicsig::base64_url_encode($envxml));
81 $response = $client->post($endpoint_uri, $headers);
84 // 200 OK is the best response
85 // 202 Accepted is what we get from Diaspora for example
86 if (!in_array($response->getStatus(), array(200, 202))) {
87 common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s',
88 $user->id, $endpoint_uri, $response->getStatus(), $response->getBody()));