]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
tweak RSSCloud plugin to use new schema layout (but still the old event); the timesta...
[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
34     function prepare($args)
35     {
36         StatusNet::setApi(true); // Send smaller error pages
37
38         parent::prepare($args);
39
40         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
41             // TRANS: Client error. POST is a HTTP command. It should not be translated.
42             $this->clientError(_m('This method requires a POST.'));
43         }
44
45         if (empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/magic-envelope+xml') {
46             // TRANS: Client error. Do not translate "application/magic-envelope+xml"
47             $this->clientError(_m('Salmon requires "application/magic-envelope+xml".'));
48         }
49
50         $xml = file_get_contents('php://input');
51
52         // Check the signature
53         $salmon = new Salmon;
54         if (!$salmon->verifyMagicEnv($xml)) {
55             common_log(LOG_DEBUG, "Salmon signature verification failed.");
56             // TRANS: Client error.
57             $this->clientError(_m('Salmon signature verification failed.'));
58         } else {
59             $magic_env = new MagicEnvelope();
60             $env = $magic_env->parse($xml);
61             $xml = $magic_env->unfold($env);
62         }
63
64         $dom = DOMDocument::loadXML($xml);
65         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
66             $dom->documentElement->localName != 'entry') {
67             common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
68             // TRANS: Client error.
69             $this->clientError(_m('Salmon post must be an Atom entry.'));
70         }
71
72         $this->activity = new Activity($dom->documentElement);
73         return true;
74     }
75
76     /**
77      * Check the posted activity type and break out to appropriate processing.
78      */
79
80     function handle($args)
81     {
82         StatusNet::setApi(true); // Send smaller error pages
83
84         common_log(LOG_DEBUG, "Got a " . $this->activity->verb);
85         if (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::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         }
122     }
123
124     function handlePost()
125     {
126         // TRANS: Client exception.
127         throw new ClientException(_m("This target doesn't understand posts."));
128     }
129
130     function handleFollow()
131     {
132         // TRANS: Client exception.
133         throw new ClientException(_m("This target doesn't understand follows."));
134     }
135
136     function handleUnfollow()
137     {
138         // TRANS: Client exception.
139         throw new ClientException(_m("This target doesn't understand unfollows."));
140     }
141
142     function handleFavorite()
143     {
144         // TRANS: Client exception.
145         throw new ClientException(_m("This target doesn't understand favorites."));
146     }
147
148     function handleUnfavorite()
149     {
150         // TRANS: Client exception.
151         throw new ClientException(_m("This target doesn't understand unfavorites."));
152     }
153
154     function handleShare()
155     {
156         // TRANS: Client exception.
157         throw new ClientException(_m("This target doesn't understand share events."));
158     }
159
160     function handleJoin()
161     {
162         // TRANS: Client exception.
163         throw new ClientException(_m("This target doesn't understand joins."));
164     }
165
166     function handleLeave()
167     {
168         // TRANS: Client exception.
169         throw new ClientException(_m("This target doesn't understand leave events."));
170     }
171
172     /**
173      * Remote user sent us an update to their profile.
174      * If we already know them, accept the updates.
175      */
176     function handleUpdateProfile()
177     {
178         $oprofile = Ostatus_profile::getActorProfile($this->activity);
179         if ($oprofile) {
180             common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
181             $oprofile->updateFromActivityObject($this->activity->actor);
182         } else {
183             common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->activity->actor->id);
184         }
185     }
186
187     /**
188      * @return Ostatus_profile
189      */
190     function ensureProfile()
191     {
192         $actor = $this->activity->actor;
193         if (empty($actor->id)) {
194             common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
195             common_log(LOG_ERR, "activity with no actor: " . var_export($this->activity, true));
196             // TRANS: Exception.
197             throw new Exception(_m('Received a salmon slap from unidentified actor.'));
198         }
199
200         return Ostatus_profile::ensureActivityObjectProfile($actor);
201     }
202
203     function saveNotice()
204     {
205         $oprofile = $this->ensureProfile();
206         return $oprofile->processPost($this->activity, 'salmon');
207     }
208 }