]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
dcd7f00fd169bf238a480a85f08dfa67d65de5df
[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     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         $envxml = file_get_contents('php://input');
50         $magic_env = new MagicEnvelope($envxml);   // parse incoming XML as a MagicEnvelope
51         if (!$magic_env->verify()) {
52             common_log(LOG_DEBUG, "Salmon signature verification failed.");
53             // TRANS: Client error.
54             $this->clientError(_m('Salmon signature verification failed.'));
55         }
56
57         $xml = $magic_env->unfold();    // return the enveloped XML (the actual data)
58         $dom = DOMDocument::loadXML($xml);
59         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
60             $dom->documentElement->localName != 'entry') {
61             common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
62             // TRANS: Client error.
63             $this->clientError(_m('Salmon post must be an Atom entry.'));
64         }
65
66         $this->activity = new Activity($dom->documentElement);
67         return true;
68     }
69
70     /**
71      * Check the posted activity type and break out to appropriate processing.
72      */
73
74     protected function handle()
75     {
76         parent::handle();
77
78         common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
79         if (Event::handle('StartHandleSalmonTarget', array($this->activity, $this->target)) &&
80             Event::handle('StartHandleSalmon', array($this->activity))) {
81             switch ($this->activity->verb)
82             {
83             case ActivityVerb::POST:
84                 $this->handlePost();
85                 break;
86             case ActivityVerb::SHARE:
87                 $this->handleShare();
88                 break;
89             case ActivityVerb::FAVORITE:
90                 $this->handleFavorite();
91                 break;
92             case ActivityVerb::UNFAVORITE:
93                 $this->handleUnfavorite();
94                 break;
95             case ActivityVerb::FOLLOW:
96             case ActivityVerb::FRIEND:
97                 $this->handleFollow();
98                 break;
99             case ActivityVerb::UNFOLLOW:
100                 $this->handleUnfollow();
101                 break;
102             case ActivityVerb::JOIN:
103                 $this->handleJoin();
104                 break;
105             case ActivityVerb::LEAVE:
106                 $this->handleLeave();
107                 break;
108             case ActivityVerb::TAG:
109                 $this->handleTag();
110                 break;
111             case ActivityVerb::UNTAG:
112                 $this->handleUntag();
113                 break;
114             case ActivityVerb::UPDATE_PROFILE:
115                 $this->handleUpdateProfile();
116                 break;
117             default:
118                 // TRANS: Client exception.
119                 throw new ClientException(_m('Unrecognized activity type.'));
120             }
121             Event::handle('EndHandleSalmon', array($this->activity));
122             Event::handle('EndHandleSalmonTarget', array($this->activity, $this->target));
123         }
124     }
125
126     function handlePost()
127     {
128         // TRANS: Client exception.
129         throw new ClientException(_m('This target does not understand posts.'));
130     }
131
132     function handleFollow()
133     {
134         // TRANS: Client exception.
135         throw new ClientException(_m('This target does not understand follows.'));
136     }
137
138     function handleUnfollow()
139     {
140         // TRANS: Client exception.
141         throw new ClientException(_m('This target does not understand unfollows.'));
142     }
143
144     function handleFavorite()
145     {
146         // TRANS: Client exception.
147         throw new ClientException(_m('This target does not understand favorites.'));
148     }
149
150     function handleUnfavorite()
151     {
152         // TRANS: Client exception.
153         throw new ClientException(_m('This target does not understand unfavorites.'));
154     }
155
156     function handleShare()
157     {
158         // TRANS: Client exception.
159         throw new ClientException(_m('This target does not understand share events.'));
160     }
161
162     function handleJoin()
163     {
164         // TRANS: Client exception.
165         throw new ClientException(_m('This target does not understand joins.'));
166     }
167
168     function handleLeave()
169     {
170         // TRANS: Client exception.
171         throw new ClientException(_m('This target does not understand leave events.'));
172     }
173
174     function handleTag()
175     {
176         // TRANS: Client exception.
177         throw new ClientException(_m('This target does not understand list events.'));
178     }
179
180     function handleUntag()
181     {
182         // TRANS: Client exception.
183         throw new ClientException(_m('This target does not understand unlist events.'));
184     }
185
186     /**
187      * Remote user sent us an update to their profile.
188      * If we already know them, accept the updates.
189      */
190     function handleUpdateProfile()
191     {
192         $oprofile = Ostatus_profile::getActorProfile($this->activity);
193         if ($oprofile instanceof Ostatus_profile) {
194             common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
195             $oprofile->updateFromActivityObject($this->activity->actor);
196         } else {
197             common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
198         }
199     }
200
201     /**
202      * @return Ostatus_profile
203      */
204     function ensureProfile()
205     {
206         $actor = $this->activity->actor;
207         if (empty($actor->id)) {
208             common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
209             common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
210             // TRANS: Exception.
211             throw new Exception(_m('Received a salmon slap from unidentified actor.'));
212         }
213
214         return Ostatus_profile::ensureActivityObjectProfile($actor);
215     }
216
217     function saveNotice()
218     {
219         $oprofile = $this->ensureProfile();
220         return $oprofile->processPost($this->activity, 'salmon');
221     }
222 }