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