]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
[CORE][StoreRemoteMedia] Fixed bug where sometimes images were written outside the...
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Handler for posting new notices
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @copyright 2013 Free Software Foundation, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('GNUSOCIAL')) { exit(1); }
34
35 /**
36  * Action for posting new notices
37  *
38  * @category Personal
39  * @package  StatusNet
40  * @author   Evan Prodromou <evan@status.net>
41  * @author   Zach Copley <zach@status.net>
42  * @author   Sarven Capadisli <csarven@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class NewnoticeAction extends FormAction
47 {
48     protected $form = 'Notice';
49
50     protected $inreplyto = null;
51
52     /**
53      * Title of the page
54      *
55      * Note that this usually doesn't get called unless something went wrong
56      *
57      * @return string page title
58      */
59     function title()
60     {
61         if ($this->getInfo() && $this->stored instanceof Notice) {
62             // TRANS: Page title after sending a notice.
63             return _('Notice posted');
64         }
65         if ($this->int('inreplyto')) {
66             return _m('TITLE', 'New reply');
67         }
68         // TRANS: Page title for sending a new notice.
69         return _m('TITLE','New notice');
70     }
71
72     protected function doPreparation()
73     {
74         foreach(array('inreplyto') as $opt) {
75             if ($this->trimmed($opt)) {
76                 $this->formOpts[$opt] = $this->trimmed($opt);
77             }
78         }
79
80         if ($this->int('inreplyto')) {
81             // Throws exception if the inreplyto Notice is given but not found.
82             $this->inreplyto = Notice::getByID($this->int('inreplyto'));
83         }
84
85         // Backwards compatibility for "share this" widget things.
86         // If no 'content', use 'status_textarea'
87         $this->formOpts['content'] = $this->trimmed('content') ?: $this->trimmed('status_textarea');
88     }
89
90     /**
91      * This doPost saves a new notice, based on arguments
92      *
93      * If successful, will show the notice, or return an Ajax-y result.
94      * If not, it will show an error message -- possibly Ajax-y.
95      *
96      * Also, if the notice input looks like a command, it will run the
97      * command and show the results -- again, possibly ajaxy.
98      *
99      * @return void
100      */
101     protected function doPost()
102     {
103         assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
104         $user = $this->scoped->getUser();
105         $content = $this->formOpts['content'];
106         $options = array('source' => 'web');
107         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
108
109         $upload = null;
110         try {
111             // throws exception on failure
112             $upload = MediaFile::fromUpload('attach', $this->scoped);
113             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options))) {
114                 $content .= ($content==='' ? '' : ' ') . $upload->shortUrl();
115             }
116             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options));
117
118             // We could check content length here if the URL was added, but I'll just let it slide for now...
119
120             $act->enclosures[] = $upload->getEnclosure();
121         } catch (NoUploadedMediaException $e) {
122             // simply no attached media to the new notice
123             if (empty($content)) {
124                 // TRANS: Client error displayed trying to send a notice without content.
125                 throw new ClientException(_('No content!'));
126             }
127         }
128
129         $inter = new CommandInterpreter();
130
131         $cmd = $inter->handle_command($user, $content);
132
133         if ($cmd) {
134             if (GNUsocial::isAjax()) {
135                 $cmd->execute(new AjaxWebChannel($this));
136             } else {
137                 $cmd->execute(new WebChannel($this));
138             }
139             return;
140         }
141
142         $act = new Activity();
143         $act->verb = ActivityVerb::POST;
144         $act->time = time();
145         $act->actor = $this->scoped->asActivityObject();
146
147         // Reject notice if it is too long (without the HTML)
148         // This is done after MediaFile::fromUpload etc. just to act the same as the ApiStatusesUpdateAction
149         if (Notice::contentTooLong($content)) {
150             // TRANS: Client error displayed when the parameter "status" is missing.
151             // TRANS: %d is the maximum number of character for a notice.
152             throw new ClientException(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
153                                                  'That\'s too long. Maximum notice size is %d characters.',
154                                                  Notice::maxContent()),
155                                               Notice::maxContent()));
156         }
157
158         $act->context = new ActivityContext();
159
160         if ($this->inreplyto instanceof Notice) {
161             $act->context->replyToID = $this->inreplyto->getUri();
162             $act->context->replyToUrl = $this->inreplyto->getUrl(true);  // maybe we don't have to send true here to force a URL?
163         }
164
165         if ($this->scoped->shareLocation()) {
166             // use browser data if checked; otherwise profile data
167             if ($this->arg('notice_data-geo')) {
168                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
169                                                       $this->trimmed('lon'),
170                                                       $this->trimmed('location_id'),
171                                                       $this->trimmed('location_ns'),
172                                                       $this->scoped);
173             } else {
174                 $locOptions = Notice::locationOptions(null,
175                                                       null,
176                                                       null,
177                                                       null,
178                                                       $this->scoped);
179             }
180
181             $act->context->location = Location::fromOptions($locOptions);
182         }
183
184         $content = $this->scoped->shortenLinks($content);
185
186         // FIXME: Make sure NoticeTitle plugin gets a change to add the title to our activityobject!
187         if (Event::handle('StartNoticeSaveWeb', array($this, $this->scoped, &$content, &$options))) {
188
189             // FIXME: We should be able to get the attentions from common_render_content!
190             // and maybe even directly save whether they're local or not!
191             $act->context->attention = common_get_attentions($content, $this->scoped, $this->inreplyto);
192
193             // $options gets filled with possible scoping settings
194             ToSelector::fillActivity($this, $act, $options);
195
196             $actobj = new ActivityObject();
197             $actobj->type = ActivityObject::NOTE;
198             $actobj->content = common_render_content($content, $this->scoped, $this->inreplyto);
199
200             // Finally add the activity object to our activity
201             $act->objects[] = $actobj;
202
203             $this->stored = Notice::saveActivity($act, $this->scoped, $options);
204
205             if ($upload instanceof MediaFile) {
206                 $upload->attachToNotice($this->stored);
207             }
208
209             Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
210         }
211
212         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content, &$options));
213
214         if (!GNUsocial::isAjax()) {
215             $url = common_local_url('shownotice', array('notice' => $this->stored->id));
216             common_redirect($url, 303);
217         }
218
219         return _('Saved the notice!');
220     }
221
222     protected function showContent()
223     {
224         if ($this->getInfo() && $this->stored instanceof Notice) {
225             $this->showNotice($this->stored);
226         } elseif (!$this->getError()) {
227             if (!GNUsocial::isAjax() && $this->inreplyto instanceof Notice) {
228                 $this->showNotice($this->inreplyto);
229             }
230             parent::showContent();
231         }
232     }
233
234     /**
235      * Output a notice
236      *
237      * Used to generate the notice code for Ajax results.
238      *
239      * @param Notice $notice Notice that was saved
240      *
241      * @return void
242      */
243     function showNotice(Notice $notice)
244     {
245         $nli = new NoticeListItem($notice, $this);
246         $nli->show();
247     }
248
249     public function showNoticeForm()
250     {
251         // pass
252     }
253 }