]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/salmon.php
Stronger typing and function access control in OStatus
[quix0rs-gnu-social.git] / plugins / OStatus / actions / salmon.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     var $xml      = null;
30     var $activity = null;
31     var $target   = null;
32
33     protected function prepare(array $args=array())
34     {
35         StatusNet::setApi(true); // Send smaller error pages
36
37         parent::prepare($args);
38
39         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
40             // TRANS: Client error. POST is a HTTP command. It should not be translated.
41             $this->clientError(_m('This method requires a POST.'));
42         }
43
44         if (empty($_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         $xml = file_get_contents('php://input');
50
51         // Check the signature
52         $salmon = new Salmon;
53         if (!$salmon->verifyMagicEnv($xml)) {
54             common_log(LOG_DEBUG, "Salmon signature verification failed.");
55             // TRANS: Client error.
56             $this->clientError(_m('Salmon signature verification failed.'));
57         } else {
58             $magic_env = new MagicEnvelope();
59             $env = $magic_env->parse($xml);
60             $xml = $magic_env->unfold($env);
61         }
62
63         $dom = DOMDocument::loadXML($xml);
64         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
65             $dom->documentElement->localName != 'entry') {
66             common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
67             // TRANS: Client error.
68             $this->clientError(_m('Salmon post must be an Atom entry.'));
69         }
70
71         $this->activity = new Activity($dom->documentElement);
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_log(LOG_DEBUG, "Got a " . $this->activity->verb);
84         if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
85             Event::handle('StartHandleSalmon', array($this->activity))) {
86             switch ($this->activity->verb)
87             {
88             case ActivityVerb::POST:
89                 $this->handlePost();
90                 break;
91             case ActivityVerb::SHARE:
92                 $this->handleShare();
93                 break;
94             case ActivityVerb::FAVORITE:
95                 $this->handleFavorite();
96                 break;
97             case ActivityVerb::UNFAVORITE:
98                 $this->handleUnfavorite();
99                 break;
100             case ActivityVerb::FOLLOW:
101             case ActivityVerb::FRIEND:
102                 $this->handleFollow();
103                 break;
104             case ActivityVerb::UNFOLLOW:
105                 $this->handleUnfollow();
106                 break;
107             case ActivityVerb::JOIN:
108                 $this->handleJoin();
109                 break;
110             case ActivityVerb::LEAVE:
111                 $this->handleLeave();
112                 break;
113             case ActivityVerb::TAG:
114                 $this->handleTag();
115                 break;
116             case ActivityVerb::UNTAG:
117                 $this->handleUntag();
118                 break;
119             case ActivityVerb::UPDATE_PROFILE:
120                 $this->handleUpdateProfile();
121                 break;
122             default:
123                 // TRANS: Client exception.
124                 throw new ClientException(_m('Unrecognized activity type.'));
125             }
126             Event::handle('EndHandleSalmon', array($this->activity));
127             Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
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) {
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 }