]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmon.php
Merge branch 'apinamespace' into 0.9.x
[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     const REL_SALMON = 'salmon';
32     const REL_MENTIONED = 'mentioned';
33
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";
37
38     /**
39      * Sign and post the given Atom entry as a Salmon message.
40      *
41      * @fixme pass through the actor for signing?
42      *
43      * @param string $endpoint_uri
44      * @param string $xml
45      * @return boolean success
46      */
47     public function post($endpoint_uri, $xml, $actor)
48     {
49         if (empty($endpoint_uri)) {
50             return false;
51         }
52
53         try {
54             $xml = $this->createMagicEnv($xml, $actor);
55         } catch (Exception $e) {
56             common_log(LOG_ERR, "Salmon unable to sign: " . $e->getMessage());
57             return false;
58         }
59
60         $headers = array('Content-Type: application/magic-envelope+xml');
61
62         try {
63             $client = new HTTPClient();
64             $client->setBody($xml);
65             $response = $client->post($endpoint_uri, $headers);
66         } catch (HTTP_Request2_Exception $e) {
67             common_log(LOG_ERR, "Salmon post to $endpoint_uri failed: " . $e->getMessage());
68             return false;
69         }
70         if ($response->getStatus() != 200) {
71             common_log(LOG_ERR, "Salmon at $endpoint_uri returned status " .
72                 $response->getStatus() . ': ' . $response->getBody());
73             return false;
74         }
75         return true;
76     }
77
78     public function createMagicEnv($text, $actor)
79     {
80         $magic_env = new MagicEnvelope();
81
82         $user = User::staticGet('id', $actor->id);
83         if ($user->id) {
84             // Use local key
85             $magickey = Magicsig::staticGet('user_id', $user->id);
86             if (!$magickey) {
87                 // No keypair yet, let's generate one.
88                 $magickey = new Magicsig();
89                 $magickey->generate($user->id);
90             }
91         } else {
92             // @todo i18n FIXME: added i18n and use sprintf when using parameters.
93             throw new Exception("Salmon invalid actor for signing.");
94         }
95
96         try {
97             $env = $magic_env->signMessage($text, 'application/atom+xml', $magickey->toString());
98         } catch (Exception $e) {
99             return $text;
100         }
101         return $magic_env->toXML($env);
102     }
103
104
105     public function verifyMagicEnv($text)
106     {
107         $magic_env = new MagicEnvelope();
108
109         $env = $magic_env->parse($text);
110
111         return $magic_env->verify($env);
112     }
113 }