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