]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into testing
[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             $this->clientError(_m('This method requires a POST.'));
42         }
43
44         if (empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
45             $this->clientError(_m('Salmon requires application/atom+xml'));
46         }
47
48         $xml = file_get_contents('php://input');
49
50         $dom = DOMDocument::loadXML($xml);
51
52         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
53             $dom->documentElement->localName != 'entry') {
54             common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
55             $this->clientError(_m('Salmon post must be an Atom entry.'));
56         }
57
58         // Check the signature
59         $salmon = new Salmon;
60         if (!common_config('ostatus', 'skip_signatures')) {
61             if (!$salmon->verifyMagicEnv($dom)) {
62                 common_log(LOG_DEBUG, "Salmon signature verification failed.");
63                 $this->clientError(_m('Salmon signature verification failed.'));
64             }
65         }
66
67         $this->act = new Activity($dom->documentElement);
68         return true;
69     }
70
71     /**
72      * Check the posted activity type and break out to appropriate processing.
73      */
74
75     function handle($args)
76     {
77         StatusNet::setApi(true); // Send smaller error pages
78
79         common_log(LOG_DEBUG, "Got a " . $this->act->verb);
80         if (Event::handle('StartHandleSalmon', array($this->activity))) {
81             switch ($this->act->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::UPDATE_PROFILE:
109                 $this->handleUpdateProfile();
110                 break;
111             default:
112                 throw new ClientException(_m("Unrecognized activity type."));
113             }
114             Event::handle('EndHandleSalmon', array($this->activity));
115         }
116     }
117
118     function handlePost()
119     {
120         throw new ClientException(_m("This target doesn't understand posts."));
121     }
122
123     function handleFollow()
124     {
125         throw new ClientException(_m("This target doesn't understand follows."));
126     }
127
128     function handleUnfollow()
129     {
130         throw new ClientException(_m("This target doesn't understand unfollows."));
131     }
132
133     function handleFavorite()
134     {
135         throw new ClientException(_m("This target doesn't understand favorites."));
136     }
137
138     function handleUnfavorite()
139     {
140         throw new ClientException(_m("This target doesn't understand unfavorites."));
141     }
142
143     function handleShare()
144     {
145         throw new ClientException(_m("This target doesn't understand share events."));
146     }
147
148     function handleJoin()
149     {
150         throw new ClientException(_m("This target doesn't understand joins."));
151     }
152
153     function handleLeave()
154     {
155         throw new ClientException(_m("This target doesn't understand leave events."));
156     }
157
158     /**
159      * Remote user sent us an update to their profile.
160      * If we already know them, accept the updates.
161      */
162     function handleUpdateProfile()
163     {
164         $oprofile = Ostatus_profile::getActorProfile($this->act);
165         if ($oprofile) {
166             common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
167             $oprofile->updateFromActivityObject($this->act->actor);
168         } else {
169             common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->act->actor->id);
170         }
171     }
172
173     /**
174      * @return Ostatus_profile
175      */
176     function ensureProfile()
177     {
178         $actor = $this->act->actor;
179         if (empty($actor->id)) {
180             common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
181             common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
182             throw new Exception("Received a salmon slap from unidentified actor.");
183         }
184
185         return Ostatus_profile::ensureActivityObjectProfile($actor);
186     }
187
188     function saveNotice()
189     {
190         $oprofile = $this->ensureProfile();
191         return $oprofile->processPost($this->act, 'salmon');
192     }
193 }