]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
Merge commit 'origin/0.9.x' into 0.9.x
[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/magic-envelope+xml') {
45             $this->clientError(_m('Salmon requires application/magic-envelope+xml'));
46         }
47
48         $xml = file_get_contents('php://input');
49
50
51         // Check the signature
52         $salmon = new Salmon;
53         if (!$salmon->verifyMagicEnv($xml)) {
54             common_log(LOG_DEBUG, "Salmon signature verification failed.");
55             $this->clientError(_m('Salmon signature verification failed.'));
56         } else {
57             $magic_env = new MagicEnvelope();
58             $env = $magic_env->parse($xml);
59             $xml = $magic_env->unfold($env);
60         }
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             $this->clientError(_m('Salmon post must be an Atom entry.'));
68         }
69
70         $this->act = new Activity($dom->documentElement);
71         return true;
72     }
73
74     /**
75      * Check the posted activity type and break out to appropriate processing.
76      */
77
78     function handle($args)
79     {
80         StatusNet::setApi(true); // Send smaller error pages
81
82         common_log(LOG_DEBUG, "Got a " . $this->act->verb);
83         if (Event::handle('StartHandleSalmon', array($this->activity))) {
84             switch ($this->act->verb)
85             {
86             case ActivityVerb::POST:
87                 $this->handlePost();
88                 break;
89             case ActivityVerb::SHARE:
90                 $this->handleShare();
91                 break;
92             case ActivityVerb::FAVORITE:
93                 $this->handleFavorite();
94                 break;
95             case ActivityVerb::UNFAVORITE:
96                 $this->handleUnfavorite();
97                 break;
98             case ActivityVerb::FOLLOW:
99             case ActivityVerb::FRIEND:
100                 $this->handleFollow();
101                 break;
102             case ActivityVerb::UNFOLLOW:
103                 $this->handleUnfollow();
104                 break;
105             case ActivityVerb::JOIN:
106                 $this->handleJoin();
107                 break;
108             case ActivityVerb::LEAVE:
109                 $this->handleLeave();
110                 break;
111             case ActivityVerb::UPDATE_PROFILE:
112                 $this->handleUpdateProfile();
113                 break;
114             default:
115                 throw new ClientException(_m("Unrecognized activity type."));
116             }
117             Event::handle('EndHandleSalmon', array($this->activity));
118         }
119     }
120
121     function handlePost()
122     {
123         throw new ClientException(_m("This target doesn't understand posts."));
124     }
125
126     function handleFollow()
127     {
128         throw new ClientException(_m("This target doesn't understand follows."));
129     }
130
131     function handleUnfollow()
132     {
133         throw new ClientException(_m("This target doesn't understand unfollows."));
134     }
135
136     function handleFavorite()
137     {
138         throw new ClientException(_m("This target doesn't understand favorites."));
139     }
140
141     function handleUnfavorite()
142     {
143         throw new ClientException(_m("This target doesn't understand unfavorites."));
144     }
145
146     function handleShare()
147     {
148         throw new ClientException(_m("This target doesn't understand share events."));
149     }
150
151     function handleJoin()
152     {
153         throw new ClientException(_m("This target doesn't understand joins."));
154     }
155
156     function handleLeave()
157     {
158         throw new ClientException(_m("This target doesn't understand leave events."));
159     }
160
161     /**
162      * Remote user sent us an update to their profile.
163      * If we already know them, accept the updates.
164      */
165     function handleUpdateProfile()
166     {
167         $oprofile = Ostatus_profile::getActorProfile($this->act);
168         if ($oprofile) {
169             common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
170             $oprofile->updateFromActivityObject($this->act->actor);
171         } else {
172             common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->act->actor->id);
173         }
174     }
175
176     /**
177      * @return Ostatus_profile
178      */
179     function ensureProfile()
180     {
181         $actor = $this->act->actor;
182         if (empty($actor->id)) {
183             common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
184             common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
185             throw new Exception("Received a salmon slap from unidentified actor.");
186         }
187
188         return Ostatus_profile::ensureActivityObjectProfile($actor);
189     }
190
191     function saveNotice()
192     {
193         $oprofile = $this->ensureProfile();
194         return $oprofile->processPost($this->act, 'salmon');
195     }
196 }