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