]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
bb3a6180d82edbd4451ba7f6b48147c6c7b3d2c4
[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     protected $oprofile = null; // Ostatus_profile of the actor
32     protected $actor    = null; // Profile object of the actor
33
34     var $xml      = null;
35     var $activity = null;
36     var $target   = null;
37
38     protected function prepare(array $args=array())
39     {
40         StatusNet::setApi(true); // Send smaller error pages
41
42         parent::prepare($args);
43
44         if (!isset($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/magic-envelope+xml') {
45             // TRANS: Client error. Do not translate "application/magic-envelope+xml".
46             $this->clientError(_m('Salmon requires "application/magic-envelope+xml".'));
47         }
48
49         try {
50             $envxml = file_get_contents('php://input');
51             $magic_env = new MagicEnvelope($envxml);   // parse incoming XML as a MagicEnvelope
52
53             $entry = $magic_env->getPayload();  // Not cryptographically verified yet!
54             $this->activity = new Activity($entry->documentElement);
55             $profile = Profile::fromUri($this->activity->actor->id);
56             assert($profile instanceof Profile);
57         } catch (Exception $e) {
58             common_debug('Salmon envelope parsing failed with: '.$e->getMessage());
59             $this->clientError($e->getMessage());
60         }
61
62         // Cryptographic verification test
63         if (!$magic_env->verify($profile)) {
64             common_debug("Salmon signature verification failed.");
65             // TRANS: Client error.
66             $this->clientError(_m('Salmon signature verification failed.'));
67         }
68
69         $this->oprofile = $this->ensureProfile();
70         $this->actor    = $this->oprofile->localProfile();
71
72         return true;
73     }
74
75     /**
76      * Check the posted activity type and break out to appropriate processing.
77      */
78
79     protected function handle()
80     {
81         parent::handle();
82
83         common_debug("Got a " . $this->activity->verb);
84         try {
85             if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
86                     Event::handle('StartHandleSalmon', array($this->activity))) {
87                 switch ($this->activity->verb) {
88                 case ActivityVerb::POST:
89                     $this->handlePost();
90                     break;
91                 case ActivityVerb::SHARE:
92                     $this->handleShare();
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 handleShare()
150     {
151         // TRANS: Client exception.
152         throw new ClientException(_m('This target does not understand share events.'));
153     }
154
155     function handleJoin()
156     {
157         // TRANS: Client exception.
158         throw new ClientException(_m('This target does not understand joins.'));
159     }
160
161     function handleLeave()
162     {
163         // TRANS: Client exception.
164         throw new ClientException(_m('This target does not understand leave events.'));
165     }
166
167     function handleTag()
168     {
169         // TRANS: Client exception.
170         throw new ClientException(_m('This target does not understand list events.'));
171     }
172
173     function handleUntag()
174     {
175         // TRANS: Client exception.
176         throw new ClientException(_m('This target does not understand unlist events.'));
177     }
178
179     /**
180      * Remote user sent us an update to their profile.
181      * If we already know them, accept the updates.
182      */
183     function handleUpdateProfile()
184     {
185         $oprofile = Ostatus_profile::getActorProfile($this->activity);
186         if ($oprofile instanceof Ostatus_profile) {
187             common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
188             $oprofile->updateFromActivityObject($this->activity->actor);
189         } else {
190             common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
191         }
192     }
193
194     /**
195      * @return Ostatus_profile
196      */
197     function ensureProfile()
198     {
199         $actor = $this->activity->actor;
200         if (empty($actor->id)) {
201             common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
202             common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
203             // TRANS: Exception.
204             throw new Exception(_m('Received a salmon slap from unidentified actor.'));
205         }
206
207         // ensureActivityObjectProfile throws exception on failure
208         return Ostatus_profile::ensureActivityObjectProfile($actor);
209     }
210
211     function saveNotice()
212     {
213         $oprofile = $this->ensureProfile();
214         return $oprofile->processPost($this->activity, 'salmon');
215     }
216 }