]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/actions/qnareviseanswer.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / QnA / actions / qnareviseanswer.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Revise an answer
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  QnA
24  * @package   StatusNet
25  * @author    Zach Copley <zach@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 if (!defined('STATUSNET')) {
31     // This check helps protect against security problems;
32     // your code file can't be executed directly from the web.
33     exit(1);
34 }
35
36 /**
37  * Revise an answer
38  *
39  * @category  QnA
40  * @package   StatusNet
41  * @author    Zach Copley <zach@status.net>
42  * @copyright 2010 StatusNet, Inc.
43  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44  * @link      http://status.net/
45  */
46 class QnareviseanswerAction extends Action
47 {
48     protected $user     = null;
49     protected $error    = null;
50     protected $question = null;
51     protected $answer   = null;
52     protected $content  = null;
53
54     /**
55      * Returns the title of the action
56      *
57      * @return string Action title
58      */
59     function title()
60     {
61         // TRANS: Page title for revising a question
62         return _m('Revise answer');
63     }
64
65     /**
66      * For initializing members of the class.
67      *
68      * @param array $argarray misc. arguments
69      *
70      * @return boolean true
71      */
72     function prepare($argarray)
73     {
74         parent::prepare($argarray);
75         if ($this->boolean('ajax')) {
76             GNUsocial::setApi(true);
77         }
78
79         $this->user = common_current_user();
80
81         if (empty($this->user)) {
82             throw new ClientException(
83                 // TRANS: Client exception thrown trying to answer a question while not logged in.
84                 _m("You must be logged in to answer to a question."),
85                 403
86             );
87         }
88
89         $id = substr($this->trimmed('id'), 7);
90
91         $this->answer   = QnA_Answer::getKV('id', $id);
92         $this->question = $this->answer->getQuestion();
93
94         if (empty($this->answer) || empty($this->question)) {
95             throw new ClientException(
96                 // TRANS: Client exception thrown trying to respond to a non-existing question.
97                 _m('Invalid or missing answer.'),
98                 404
99             );
100         }
101
102         $this->answerText = $this->trimmed('answer');
103
104         return true;
105     }
106
107     /**
108      * Handler method
109      *
110      * @param array $argarray is ignored since it's now passed in in prepare()
111      *
112      * @return void
113      */
114     function handle($argarray=null)
115     {
116         parent::handle($argarray);
117
118         if ($this->isPost()) {
119             $this->checkSessionToken();
120             if ($this->arg('revise')) {
121                 $this->showContent();
122                 return;
123             } else if ($this->arg('best')) {
124                 if ($this->user->id == $this->question->profile_id) {
125                     $this->markBest();
126                     return;
127                 }
128             } else {
129                 $this->reviseAnswer();
130                 return;
131             }
132         }
133
134         $this->showPage();
135     }
136
137     /**
138      * Revise the answer
139      *
140      * @return void
141      */
142     function reviseAnswer()
143     {
144         $answer = $this->answer;
145
146         try {
147             $orig = clone($answer);
148             $answer->content = $this->answerText;
149             $answer->revisions++;
150             $result = $answer->update($orig);
151         } catch (ClientException $ce) {
152             $this->error = $ce->getMessage();
153             $this->showPage();
154             return;
155         }
156         if ($this->boolean('ajax')) {
157             common_debug("ajaxy part");
158             $this->startHTML('text/xml;charset=utf-8');
159             $this->elementStart('head');
160             // TRANS: Page title after sending an answer.
161             $this->element('title', null, _m('Answer'));
162             $this->elementEnd('head');
163             $this->elementStart('body');
164             $form = new QnashowanswerForm($this, $answer);
165             $form->show();
166             $this->elementEnd('body');
167             $this->endHTML();
168         } else {
169             common_redirect($this->answer->getUrl(), 303);
170         }
171     }
172
173     /**
174      * Mark the answer as the "best" answer
175      *
176      * @return void
177      */
178     function markBest()
179     {
180         $question = $this->question;
181         $answer   = $this->answer;
182
183         try {
184             // close the question to further answers
185             $orig = clone($question);
186             $question->closed = 1;
187             $result = $question->update($orig);
188
189             // mark this answer an the best answer
190             $orig = clone($answer);
191             $answer->best = 1;
192             $result = $answer->update($orig);
193         } catch (ClientException $ce) {
194             $this->error = $ce->getMessage();
195             $this->showPage();
196             return;
197         }
198         if ($this->boolean('ajax')) {
199             common_debug("ajaxy part");
200             $this->startHTML('text/xml;charset=utf-8');
201             $this->elementStart('head');
202             // TRANS: Page title after sending an answer.
203             $this->element('title', null, _m('Answer'));
204             $this->elementEnd('head');
205             $this->elementStart('body');
206             $form = new QnashowanswerForm($this, $answer);
207             $form->show();
208             $this->elementEnd('body');
209             $this->endHTML();
210         } else {
211             common_redirect($this->answer->getUrl(), 303);
212         }
213     }
214
215     /**
216      * Show the revise answer form
217      *
218      * @return void
219      */
220     function showContent()
221     {
222         if (!empty($this->error)) {
223             $this->element('p', 'error', $this->error);
224         }
225
226         if ($this->boolean('ajax')) {
227             $this->showAjaxReviseForm();
228         } else {
229             $form = new QnareviseanswerForm($this->answer, $this);
230             $form->show();
231         }
232
233         return;
234     }
235
236     function showAjaxReviseForm()
237     {
238         $this->startHTML('text/xml;charset=utf-8');
239         $this->elementStart('head');
240         // TRANS: Form title for sending an answer.
241         $this->element('title', null, _m('TITLE','Answer'));
242         $this->elementEnd('head');
243         $this->elementStart('body');
244         $form = new QnareviseanswerForm($this->answer, $this);
245         $form->show();
246         $this->elementEnd('body');
247         $this->endHTML();
248     }
249
250     /**
251      * Return true if read only.
252      *
253      * MAY override
254      *
255      * @param array $args other arguments
256      *
257      * @return boolean is read only action?
258      */
259     function isReadOnly($args)
260     {
261         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
262             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
263             return true;
264         } else {
265             return false;
266         }
267     }
268 }