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