]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmon.php
Merge remote branch 'statusnet/testing' into testing
[quix0rs-gnu-social.git] / plugins / OStatus / lib / salmon.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A sample module to show best practices for StatusNet plugins
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @package   StatusNet
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/
28  */
29 class Salmon
30 {
31     /**
32      * Sign and post the given Atom entry as a Salmon message.
33      *
34      * @fixme pass through the actor for signing?
35      *
36      * @param string $endpoint_uri
37      * @param string $xml
38      * @return boolean success
39      */
40     public function post($endpoint_uri, $xml)
41     {
42         if (empty($endpoint_uri)) {
43             return false;
44         }
45
46         if (!common_config('ostatus', 'skip_signatures')) {
47             $xml = $this->createMagicEnv($xml);
48         }
49
50         $headers = array('Content-Type: application/atom+xml');
51
52         try {
53             $client = new HTTPClient();
54             $client->setBody($xml);
55             $response = $client->post($endpoint_uri, $headers);
56         } catch (HTTP_Request2_Exception $e) {
57             common_log(LOG_ERR, "Salmon post to $endpoint_uri failed: " . $e->getMessage());
58             return false;
59         }
60         if ($response->getStatus() != 200) {
61             common_log(LOG_ERR, "Salmon at $endpoint_uri returned status " .
62                 $response->getStatus() . ': ' . $response->getBody());
63             return false;
64         }
65         return true;
66     }
67
68     public function createMagicEnv($text)
69     {
70         $magic_env = new MagicEnvelope();
71
72         // TODO: Should probably be getting the signer uri as an argument?
73         $signer_uri = $magic_env->getAuthor($text);
74
75         try {
76             $env = $magic_env->signMessage($text, 'application/atom+xml', $signer_uri);
77         } catch (Exception $e) {
78             common_log(LOG_ERR, "Salmon signing failed: ". $e->getMessage());
79             return $text;
80         }
81         return $magic_env->unfold($env);
82     }
83
84
85     public function verifyMagicEnv($dom)
86     {
87         $magic_env = new MagicEnvelope();
88         
89         $env = $magic_env->fromDom($dom);
90
91         return $magic_env->verify($env);
92     }
93 }