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