]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/actions/newevent.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / Event / actions / newevent.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Add a new event
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Event
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Add a new event
39  *
40  * @category  Event
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class NeweventAction extends Action
48 {
49     protected $user        = null;
50     protected $error       = null;
51     protected $complete    = null;
52     protected $title       = null;
53     protected $location    = null;
54     protected $description = null;
55     protected $startTime   = null;
56     protected $endTime     = null;
57
58     /**
59      * Returns the title of the action
60      *
61      * @return string Action title
62      */
63     function title()
64     {
65         // TRANS: Title for new event form.
66         return _m('TITLE','New event');
67     }
68
69     /**
70      * For initializing members of the class.
71      *
72      * @param array $argarray misc. arguments
73      *
74      * @return boolean true
75      */
76     function prepare($argarray)
77     {
78         parent::prepare($argarray);
79
80         $this->user = common_current_user();
81
82         if (empty($this->user)) {
83             // TRANS: Client exception thrown when trying to post an event while not logged in.
84             throw new ClientException(_m('Must be logged in to post a event.'),
85                                       403);
86         }
87
88         if ($this->isPost()) {
89             $this->checkSessionToken();
90         }
91
92         try {
93
94             $this->title = $this->trimmed('title');
95
96             if (empty($this->title)) {
97                 // TRANS: Client exception thrown when trying to post an event without providing a title.
98                 throw new ClientException(_m('Title required.'));
99             }
100
101             $this->location    = $this->trimmed('location');
102             $this->url         = $this->trimmed('url');
103             $this->description = $this->trimmed('description');
104             $tz                = $this->trimmed('tz');
105
106             $startDate = $this->trimmed('startdate');
107
108             if (empty($startDate)) {
109                 // TRANS: Client exception thrown when trying to post an event without providing a start date.
110                 throw new ClientException(_m('Start date required.'));
111             }
112
113             $startTime = $this->trimmed('event-starttime');
114
115             if (empty($startTime)) {
116                 $startTime = '00:00';
117             }
118
119             $endDate   = $this->trimmed('enddate');
120
121             if (empty($endDate)) {
122                 // TRANS: Client exception thrown when trying to post an event without providing an end date.
123                 throw new ClientException(_m('End date required.'));
124             }
125
126             $endTime   = $this->trimmed('event-endtime');
127
128             if (empty($endTime)) {
129                 $endTime = '00:00';
130             }
131
132             $start = $startDate . ' ' . $startTime . ' ' . $tz;
133             $end   = $endDate . ' ' . $endTime . ' ' . $tz;
134
135             $this->startTime = strtotime($start);
136             $this->endTime   = strtotime($end);
137
138             if ($this->startTime == 0) {
139                 // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
140                 // TRANS: %s is the data that could not be processed.
141                 throw new ClientException(sprintf(_m('Could not parse date "%s".'),
142                                             $start));
143             }
144
145             if ($this->endTime == 0) {
146                 // TRANS: Client exception thrown when trying to post an event with a date that cannot be processed.
147                 // TRANS: %s is the data that could not be processed.
148                 throw new ClientException(sprintf(_m('Could not parse date "%s".'),
149                                             $end));
150             }
151         } catch (ClientException $ce) {
152             if ($this->boolean('ajax')) {
153                 $this->outputAjaxError($ce->getMessage());
154                 return false;
155             } else {
156                 $this->error = $ce->getMessage();
157                 $this->showPage();
158                 return false;
159             }
160         }
161
162         return true;
163     }
164
165     /**
166      * Handler method
167      *
168      * @param array $argarray is ignored since it's now passed in in prepare()
169      *
170      * @return void
171      */
172     function handle($argarray=null)
173     {
174         parent::handle($argarray);
175
176         if ($this->isPost()) {
177             $this->newEvent();
178         } else {
179             $this->showPage();
180         }
181
182         return;
183     }
184
185     /**
186      * Add a new event
187      *
188      * @return void
189      */
190     function newEvent()
191     {
192         try {
193
194             if (empty($this->title)) {
195                 // TRANS: Client exception thrown when trying to post an event without providing a title.
196                 throw new ClientException(_m('Event must have a title.'));
197             }
198
199             if (empty($this->startTime)) {
200                 // TRANS: Client exception thrown when trying to post an event without providing a start time.
201                 throw new ClientException(_m('Event must have a start time.'));
202             }
203
204             if (empty($this->endTime)) {
205                 // TRANS: Client exception thrown when trying to post an event without providing an end time.
206                 throw new ClientException(_m('Event must have an end time.'));
207             }
208
209             if (!empty($this->url) && Validate::uri($this->url) === false) {
210                 // TRANS: Client exception thrown when trying to post an event with an invalid URL.
211                 throw new ClientException(_m('URL must be valid.'));
212             }
213
214             $options = array();
215
216             // Does the heavy-lifting for getting "To:" information
217
218             ToSelector::fillOptions($this, $options);
219
220             $profile = $this->user->getProfile();
221
222             $saved = Happening::saveNew($profile,
223                                         common_sql_date($this->startTime),
224                                         common_sql_date($this->endTime),
225                                         $this->title,
226                                         $this->location,
227                                         $this->description,
228                                         $this->url,
229                                         $options);
230
231             $event = Happening::fromNotice($saved);
232
233             RSVP::saveNew($profile, $event, RSVP::POSITIVE);
234
235         } catch (ClientException $ce) {
236             if ($this->boolean('ajax')) {
237                 $this->outputAjaxError($ce->getMessage());
238                 return;
239             } else {
240                 $this->error = $ce->getMessage();
241                 $this->showPage();
242                 return;
243             }
244         }
245
246         if ($this->boolean('ajax')) {
247             $this->startHTML('text/xml;charset=utf-8');
248             $this->elementStart('head');
249             // TRANS: Page title after sending a notice.
250             $this->element('title', null, _m('Event saved'));
251             $this->elementEnd('head');
252             $this->elementStart('body');
253             $this->showNotice($saved);
254             $this->elementEnd('body');
255             $this->endHTML();
256         } else {
257             common_redirect($saved->getUrl(), 303);
258         }
259     }
260
261     // @todo factor this out into a base class
262     function outputAjaxError($msg)
263     {
264         $this->startHTML('text/xml;charset=utf-8');
265         $this->elementStart('head');
266         // TRANS: Page title after an AJAX error occurs
267         $this->element('title', null, _('Ajax Error'));
268         $this->elementEnd('head');
269         $this->elementStart('body');
270         $this->element('p', array('id' => 'error'), $msg);
271         $this->elementEnd('body');
272         $this->endHTML();
273         return;
274     }
275
276     /**
277      * Show the event form
278      *
279      * @return void
280      */
281     function showContent()
282     {
283         if (!empty($this->error)) {
284             $this->element('p', 'error', $this->error);
285         }
286
287         $form = new EventForm($this);
288
289         $form->show();
290
291         return;
292     }
293
294     /**
295      * Return true if read only.
296      *
297      * MAY override
298      *
299      * @param array $args other arguments
300      *
301      * @return boolean is read only action?
302      */
303     function isReadOnly($args)
304     {
305         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
306             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
307             return true;
308         } else {
309             return false;
310         }
311     }
312
313
314     /**
315      * Output a notice
316      *
317      * Used to generate the notice code for Ajax results.
318      *
319      * @param Notice $notice Notice that was saved
320      *
321      * @return void
322      */
323     function showNotice(Notice $notice)
324     {
325         $nli = new NoticeListItem($notice, $this);
326         $nli->show();
327     }
328 }