]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmon.php
Added doc comments on Salmon magicsig-related stuff to help in figuring out what...
[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      * Side effects: may generate a keypair on-demand for the given user,
42      * which can be very slow on some systems.
43      *
44      * @param string $endpoint_uri
45      * @param string $xml string representation of payload
46      * @param Profile $actor local user profile whose keys to sign with
47      * @return boolean success
48      */
49     public function post($endpoint_uri, $xml, $actor)
50     {
51         if (empty($endpoint_uri)) {
52             return false;
53         }
54
55         try {
56             $xml = $this->createMagicEnv($xml, $actor);
57         } catch (Exception $e) {
58             common_log(LOG_ERR, "Salmon unable to sign: " . $e->getMessage());
59             return false;
60         }
61
62         $headers = array('Content-Type: application/magic-envelope+xml');
63
64         try {
65             $client = new HTTPClient();
66             $client->setBody($xml);
67             $response = $client->post($endpoint_uri, $headers);
68         } catch (HTTP_Request2_Exception $e) {
69             common_log(LOG_ERR, "Salmon post to $endpoint_uri failed: " . $e->getMessage());
70             return false;
71         }
72         if ($response->getStatus() != 200) {
73             common_log(LOG_ERR, "Salmon at $endpoint_uri returned status " .
74                 $response->getStatus() . ': ' . $response->getBody());
75             return false;
76         }
77         return true;
78     }
79
80     /**
81      * Encode the given string as a signed MagicEnvelope XML document,
82      * using the keypair for the given local user profile.
83      *
84      * Side effects: will create and store a keypair on-demand if one
85      * hasn't already been generated for this user. This can be very slow
86      * on some systems.
87      *
88      * @param string $text XML fragment to sign, assumed to be Atom
89      * @param Profile $actor Profile of a local user to use as signer
90      * @return string XML string representation of magic envelope
91      *
92      * @throws Exception on bad profile input or key generation problems
93      * @fixme if signing fails, this seems to return the original text without warning. Is there a reason for this?
94      */
95     public function createMagicEnv($text, $actor)
96     {
97         $magic_env = new MagicEnvelope();
98
99         $user = User::staticGet('id', $actor->id);
100         if ($user->id) {
101             // Use local key
102             $magickey = Magicsig::staticGet('user_id', $user->id);
103             if (!$magickey) {
104                 // No keypair yet, let's generate one.
105                 $magickey = new Magicsig();
106                 $magickey->generate($user->id);
107             }
108         } else {
109             // TRANS: Exception.
110             throw new Exception(_m('Salmon invalid actor for signing.'));
111         }
112
113         try {
114             $env = $magic_env->signMessage($text, 'application/atom+xml', $magickey->toString());
115         } catch (Exception $e) {
116             return $text;
117         }
118         return $magic_env->toXML($env);
119     }
120
121     /**
122      * Check if the given magic envelope is well-formed and correctly signed.
123      * Needs to have network access to fetch public keys over the web.
124      *
125      * Side effects: exceptions and caching updates may occur during network
126      * fetches.
127      *
128      * @param string $text XML fragment of magic envelope
129      * @return boolean
130      *
131      * @throws Exception on bad profile input or key generation problems
132      * @fixme could hit fatal errors or spew output on invalid XML
133      */
134     public function verifyMagicEnv($text)
135     {
136         $magic_env = new MagicEnvelope();
137
138         $env = $magic_env->parse($text);
139
140         return $magic_env->verify($env);
141     }
142 }