]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/salmonaction.php
9aac2ed52f802078cd683a2f51263598a0e20d0e
[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(_('This method requires a POST.'));
42         }
43
44         if (empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
45             $this->clientError(_('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         // TODO : Insert new $xml -> notice code
80
81         if (Event::handle('StartHandleSalmon', array($this->activity))) {
82             switch ($this->act->verb)
83             {
84             case ActivityVerb::POST:
85                 $this->handlePost();
86                 break;
87             case ActivityVerb::SHARE:
88                 $this->handleShare();
89                 break;
90             case ActivityVerb::FAVORITE:
91                 $this->handleFavorite();
92                 break;
93             case ActivityVerb::UNFAVORITE:
94                 $this->handleUnfavorite();
95                 break;
96             case ActivityVerb::FOLLOW:
97             case ActivityVerb::FRIEND:
98                 $this->handleFollow();
99                 break;
100             case ActivityVerb::UNFOLLOW:
101                 $this->handleUnfollow();
102                 break;
103             case ActivityVerb::JOIN:
104                 $this->handleJoin();
105                 break;
106             case ActivityVerb::LEAVE:
107                 $this->handleLeave();
108                 break;
109             default:
110                 throw new ClientException(_("Unimplemented."));
111             }
112             Event::handle('EndHandleSalmon', array($this->activity));
113         }
114     }
115
116     function handlePost()
117     {
118         throw new ClientException(_("Unimplemented!"));
119     }
120
121     function handleFollow()
122     {
123         throw new ClientException(_("Unimplemented!"));
124     }
125
126     function handleUnfollow()
127     {
128         throw new ClientException(_("Unimplemented!"));
129     }
130
131     function handleFavorite()
132     {
133         throw new ClientException(_("Unimplemented!"));
134     }
135
136     /**
137      * Remote user doesn't like one of our posts after all!
138      * Confirm the post is ours, and delete a local favorite event.
139      */
140
141     function handleUnfavorite()
142     {
143         throw new ClientException(_("Unimplemented!"));
144     }
145
146     /**
147      * Hmmmm
148      */
149     function handleShare()
150     {
151         throw new ClientException(_("Unimplemented!"));
152     }
153
154     /**
155      * Hmmmm
156      */
157     function handleJoin()
158     {
159         throw new ClientException(_("Unimplemented!"));
160     }
161
162     /**
163      * Hmmmm
164      */
165     function handleLeave()
166     {
167         throw new ClientException(_("Unimplemented!"));
168     }
169
170     /**
171      * @return Ostatus_profile
172      */
173     function ensureProfile()
174     {
175         $actor = $this->act->actor;
176         if (empty($actor->id)) {
177             common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
178             common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
179             throw new Exception("Received a salmon slap from unidentified actor.");
180         }
181
182         return Ostatus_profile::ensureActivityObjectProfile($actor);
183     }
184
185     function saveNotice()
186     {
187         $oprofile = $this->ensureProfile();
188         return $oprofile->processPost($this->act, 'salmon');
189     }
190 }