]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
Don't ensureProfile before we verify signature
[quix0rs-gnu-social.git] / plugins / OStatus / lib / salmonaction.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @package OStatusPlugin
22  * @author James Walker <james@status.net>
23  */
24
25 if (!defined('GNUSOCIAL')) { exit(1); }
26
27 class SalmonAction extends Action
28 {
29     protected $needPost = true;
30
31     var $xml      = null;
32     var $activity = null;
33     var $target   = null;
34
35     protected function prepare(array $args=array())
36     {
37         StatusNet::setApi(true); // Send smaller error pages
38
39         parent::prepare($args);
40
41         if (!isset($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/magic-envelope+xml') {
42             // TRANS: Client error. Do not translate "application/magic-envelope+xml".
43             $this->clientError(_m('Salmon requires "application/magic-envelope+xml".'));
44         }
45
46         try {
47             $envxml = file_get_contents('php://input');
48             $magic_env = new MagicEnvelope($envxml);   // parse incoming XML as a MagicEnvelope
49
50             $entry = $magic_env->getPayload();  // Not cryptographically verified yet!
51             $this->activity = new Activity($entry->documentElement);
52             $profile = Profile::fromUri($this->activity->actor->id);
53             assert($profile instanceof Profile);
54         } catch (Exception $e) {
55             common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
56             $this->clientError($e->getMessage());
57         }
58
59         // Cryptographic verification test
60         if (!$magic_env->verify($profile)) {
61             common_log(LOG_DEBUG, "Salmon signature verification failed.");
62             // TRANS: Client error.
63             $this->clientError(_m('Salmon signature verification failed.'));
64         }
65
66         return true;
67     }
68
69     /**
70      * Check the posted activity type and break out to appropriate processing.
71      */
72
73     protected function handle()
74     {
75         parent::handle();
76
77         common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
78         try {
79             if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
80                     Event::handle('StartHandleSalmon', array($this->activity))) {
81                 switch ($this->activity->verb) {
82                 case ActivityVerb::POST:
83                     $this->handlePost();
84                     break;
85                 case ActivityVerb::SHARE:
86                     $this->handleShare();
87                     break;
88                 case ActivityVerb::FAVORITE:
89                     $this->handleFavorite();
90                     break;
91                 case ActivityVerb::UNFAVORITE:
92                     $this->handleUnfavorite();
93                     break;
94                 case ActivityVerb::FOLLOW:
95                 case ActivityVerb::FRIEND:
96                     $this->handleFollow();
97                     break;
98                 case ActivityVerb::UNFOLLOW:
99                     $this->handleUnfollow();
100                     break;
101                 case ActivityVerb::JOIN:
102                     $this->handleJoin();
103                     break;
104                 case ActivityVerb::LEAVE:
105                     $this->handleLeave();
106                     break;
107                 case ActivityVerb::TAG:
108                     $this->handleTag();
109                     break;
110                 case ActivityVerb::UNTAG:
111                     $this->handleUntag();
112                     break;
113                 case ActivityVerb::UPDATE_PROFILE:
114                     $this->handleUpdateProfile();
115                     break;
116                 default:
117                     // TRANS: Client exception.
118                     throw new ClientException(_m('Unrecognized activity type.'));
119                 }
120                 Event::handle('EndHandleSalmon', array($this->activity));
121                 Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
122             }
123         } catch (AlreadyFulfilledException $e) {
124             // The action's results are already fulfilled. Maybe it was a
125             // duplicate? Maybe someone's database is out of sync?
126             // Let's just accept it and move on.
127             common_log(LOG_INFO, 'Salmon slap carried an event which had already been fulfilled.');
128         }
129     }
130
131     function handlePost()
132     {
133         // TRANS: Client exception.
134         throw new ClientException(_m('This target does not understand posts.'));
135     }
136
137     function handleFollow()
138     {
139         // TRANS: Client exception.
140         throw new ClientException(_m('This target does not understand follows.'));
141     }
142
143     function handleUnfollow()
144     {
145         // TRANS: Client exception.
146         throw new ClientException(_m('This target does not understand unfollows.'));
147     }
148
149     function handleFavorite()
150     {
151         // TRANS: Client exception.
152         throw new ClientException(_m('This target does not understand favorites.'));
153     }
154
155     function handleUnfavorite()
156     {
157         // TRANS: Client exception.
158         throw new ClientException(_m('This target does not understand unfavorites.'));
159     }
160
161     function handleShare()
162     {
163         // TRANS: Client exception.
164         throw new ClientException(_m('This target does not understand share events.'));
165     }
166
167     function handleJoin()
168     {
169         // TRANS: Client exception.
170         throw new ClientException(_m('This target does not understand joins.'));
171     }
172
173     function handleLeave()
174     {
175         // TRANS: Client exception.
176         throw new ClientException(_m('This target does not understand leave events.'));
177     }
178
179     function handleTag()
180     {
181         // TRANS: Client exception.
182         throw new ClientException(_m('This target does not understand list events.'));
183     }
184
185     function handleUntag()
186     {
187         // TRANS: Client exception.
188         throw new ClientException(_m('This target does not understand unlist events.'));
189     }
190
191     /**
192      * Remote user sent us an update to their profile.
193      * If we already know them, accept the updates.
194      */
195     function handleUpdateProfile()
196     {
197         $oprofile = Ostatus_profile::getActorProfile($this->activity);
198         if ($oprofile instanceof Ostatus_profile) {
199             common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
200             $oprofile->updateFromActivityObject($this->activity->actor);
201         } else {
202             common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
203         }
204     }
205
206     /**
207      * @return Ostatus_profile
208      */
209     function ensureProfile()
210     {
211         $actor = $this->activity->actor;
212         if (empty($actor->id)) {
213             common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
214             common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
215             // TRANS: Exception.
216             throw new Exception(_m('Received a salmon slap from unidentified actor.'));
217         }
218
219         return Ostatus_profile::ensureActivityObjectProfile($actor);
220     }
221
222     function saveNotice()
223     {
224         $oprofile = $this->ensureProfile();
225         return $oprofile->processPost($this->activity, 'salmon');
226     }
227 }