qa-page-question-post.php 34.7 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-page-question-post.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: More control for question page if it's submitted by HTTP POST


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-app-limits.php';
	require_once QA_INCLUDE_DIR.'qa-page-question-submit.php';
Scott Vivian committed
34 35


Gideon Greenspan committed
36
	$code=qa_post_text('code');
Scott Vivian committed
37

Gideon Greenspan committed
38

Gideon Greenspan committed
39 40 41 42
//	Process general cancel button

	if (qa_clicked('docancel'))
		qa_page_q_refresh($pagestart);
Scott Vivian committed
43

Gideon Greenspan committed
44 45 46 47 48 49

//	Process incoming answer (or button)

	if ($question['answerbutton']) {
		if (qa_clicked('q_doanswer'))
			qa_page_q_refresh($pagestart, 'answer');
Scott Vivian committed
50

Gideon Greenspan committed
51 52 53
		// The 'approve', 'login', 'confirm', 'limit', 'userblock', 'ipblock' permission errors are reported to the user here
		// The other option ('level') prevents the answer button being shown, in qa_page_q_post_rules(...)

Gideon Greenspan committed
54
		if (qa_clicked('a_doadd') || ($pagestate=='answer'))
Gideon Greenspan committed
55
			switch (qa_user_post_permit_error('permit_post_a', $question, QA_LIMIT_ANSWERS)) {
Gideon Greenspan committed
56 57 58
				case 'login':
					$pageerror=qa_insert_login_links(qa_lang_html('question/answer_must_login'), qa_request());
					break;
Scott Vivian committed
59

Gideon Greenspan committed
60 61 62
				case 'confirm':
					$pageerror=qa_insert_login_links(qa_lang_html('question/answer_must_confirm'), qa_request());
					break;
Scott Vivian committed
63

Gideon Greenspan committed
64 65 66
				case 'approve':
					$pageerror=qa_lang_html('question/answer_must_be_approved');
					break;
Scott Vivian committed
67

Gideon Greenspan committed
68 69 70
				case 'limit':
					$pageerror=qa_lang_html('question/answer_limit');
					break;
Scott Vivian committed
71

Gideon Greenspan committed
72 73 74
				default:
					$pageerror=qa_lang_html('users/no_permission');
					break;
Scott Vivian committed
75

Gideon Greenspan committed
76 77 78
				case false:
					if (qa_clicked('a_doadd')) {
						$answerid=qa_page_q_add_a_submit($question, $answers, $usecaptcha, $anewin, $anewerrors);
Scott Vivian committed
79

Gideon Greenspan committed
80 81 82 83
						if (isset($answerid))
							qa_page_q_refresh(0, null, 'A', $answerid);
						else
							$formtype='a_add'; // show form again
Scott Vivian committed
84

Gideon Greenspan committed
85 86 87 88 89 90 91 92
					} else
						$formtype='a_add'; // show form as if first time
					break;
			}
	}


//	Process close buttons for question
Scott Vivian committed
93

Gideon Greenspan committed
94 95 96
	if ($question['closeable']) {
		if (qa_clicked('q_doclose'))
			qa_page_q_refresh($pagestart, 'close');
Scott Vivian committed
97

Gideon Greenspan committed
98 99 100 101 102 103 104 105 106
		elseif (qa_clicked('doclose') && qa_page_q_permit_edit($question, 'permit_close_q', $pageerror)) {
			if (qa_page_q_close_q_submit($question, $closepost, $closein, $closeerrors))
				qa_page_q_refresh($pagestart);
			else
				$formtype='q_close'; // keep editing if an error

		} elseif (($pagestate=='close') && qa_page_q_permit_edit($question, 'permit_close_q', $pageerror))
			$formtype='q_close';
	}
Scott Vivian committed
107 108


Gideon Greenspan committed
109
//	Process any single click operations or delete button for question
Gideon Greenspan committed
110 111 112

	if (qa_page_q_single_click_q($question, $answers, $commentsfollows, $closepost, $pageerror))
		qa_page_q_refresh($pagestart);
Scott Vivian committed
113

Gideon Greenspan committed
114
	if (qa_clicked('q_dodelete') && $question['deleteable'] && qa_page_q_click_check_form_code($question, $pageerror)) {
Gideon Greenspan committed
115 116 117 118 119 120 121 122 123 124
		qa_question_delete($question, $userid, qa_get_logged_in_handle(), $cookieid, $closepost);
		qa_redirect(''); // redirect since question has gone
	}


//	Process edit or save button for question

	if ($question['editbutton'] || $question['retagcatbutton']) {
		if (qa_clicked('q_doedit'))
			qa_page_q_refresh($pagestart, 'edit-'.$questionid);
Scott Vivian committed
125

Gideon Greenspan committed
126 127 128
		elseif (qa_clicked('q_dosave') && qa_page_q_permit_edit($question, 'permit_edit_q', $pageerror, 'permit_retag_cat')) {
			if (qa_page_q_edit_q_submit($question, $answers, $commentsfollows, $closepost, $qin, $qerrors))
				qa_redirect(qa_q_request($questionid, $qin['title'])); // don't use refresh since URL may have changed
Scott Vivian committed
129

Gideon Greenspan committed
130
			else {
Gideon Greenspan committed
131
				$formtype='q_edit'; // keep editing if an error
Gideon Greenspan committed
132 133
				$pageerror=@$qerrors['page']; // for security code failure
			}
Gideon Greenspan committed
134 135 136

		} else if (($pagestate==('edit-'.$questionid)) && qa_page_q_permit_edit($question, 'permit_edit_q', $pageerror, 'permit_retag_cat'))
			$formtype='q_edit';
Scott Vivian committed
137

Gideon Greenspan committed
138 139 140 141 142 143 144
		if ($formtype=='q_edit') { // get tags for auto-completion
			if (qa_opt('do_complete_tags'))
				$completetags=array_keys(qa_db_select_with_pending(qa_db_popular_tags_selectspec(0, QA_DB_RETRIEVE_COMPLETE_TAGS)));
			else
				$completetags=array();
		}
	}
Scott Vivian committed
145

Gideon Greenspan committed
146 147

//	Process adding a comment to question (shows form or processes it)
Scott Vivian committed
148

Gideon Greenspan committed
149 150 151
	if ($question['commentbutton']) {
		if (qa_clicked('q_docomment'))
			qa_page_q_refresh($pagestart, 'comment-'.$questionid);
Scott Vivian committed
152

Gideon Greenspan committed
153 154 155
		if (qa_clicked('c'.$questionid.'_doadd') || ($pagestate==('comment-'.$questionid)))
			qa_page_q_do_comment($question, $question, $commentsfollows, $pagestart, $usecaptcha, $cnewin, $cnewerrors, $formtype, $formpostid, $pageerror);
	}
Scott Vivian committed
156 157


Gideon Greenspan committed
158 159 160 161
//	Process clicked buttons for answers

	foreach ($answers as $answerid => $answer) {
		$prefix='a'.$answerid.'_';
Scott Vivian committed
162

Gideon Greenspan committed
163 164
		if (qa_page_q_single_click_a($answer, $question, $answers, $commentsfollows, true, $pageerror))
			qa_page_q_refresh($pagestart, null, 'A', $answerid);
Scott Vivian committed
165

Gideon Greenspan committed
166 167 168
		if ($answer['editbutton']) {
			if (qa_clicked($prefix.'doedit'))
				qa_page_q_refresh($pagestart, 'edit-'.$answerid);
Scott Vivian committed
169

Gideon Greenspan committed
170 171
			elseif (qa_clicked($prefix.'dosave') && qa_page_q_permit_edit($answer, 'permit_edit_a', $pageerror)) {
				$editedtype=qa_page_q_edit_a_submit($answer, $question, $answers, $commentsfollows, $aeditin[$answerid], $aediterrors[$answerid]);
Scott Vivian committed
172

Gideon Greenspan committed
173 174 175 176 177 178 179
				if (isset($editedtype))
					qa_page_q_refresh($pagestart, null, $editedtype, $answerid);

				else {
					$formtype='a_edit';
					$formpostid=$answerid; // keep editing if an error
				}
Scott Vivian committed
180

Gideon Greenspan committed
181 182 183 184 185
			} elseif (($pagestate==('edit-'.$answerid)) && qa_page_q_permit_edit($answer, 'permit_edit_a', $pageerror)) {
				$formtype='a_edit';
				$formpostid=$answerid;
			}
		}
Scott Vivian committed
186

Gideon Greenspan committed
187 188 189
		if ($answer['commentbutton']) {
			if (qa_clicked($prefix.'docomment'))
				qa_page_q_refresh($pagestart, 'comment-'.$answerid, 'A', $answerid);
Scott Vivian committed
190

Gideon Greenspan committed
191 192 193 194 195 196 197 198
			if (qa_clicked('c'.$answerid.'_doadd') || ($pagestate==('comment-'.$answerid)))
				qa_page_q_do_comment($question, $answer, $commentsfollows, $pagestart, $usecaptcha, $cnewin, $cnewerrors, $formtype, $formpostid, $pageerror);
		}

		if (qa_clicked($prefix.'dofollow')) {
			$params=array('follow' => $answerid);
			if (isset($question['categoryid']))
				$params['cat']=$question['categoryid'];
Scott Vivian committed
199

Gideon Greenspan committed
200 201 202 203 204 205 206 207 208 209 210 211
			qa_redirect('ask', $params);
		}
	}


//	Process hide, show, delete, flag, unflag, edit or save button for comments

	foreach ($commentsfollows as $commentid => $comment)
		if ($comment['basetype']=='C') {
			$commentparent=@$answers[$comment['parentid']];
			if (!isset($commentparent))
				$commentparent=$question;
Scott Vivian committed
212

Gideon Greenspan committed
213 214 215
			$commentparenttype=$commentparent['basetype'];

			$prefix='c'.$commentid.'_';
Scott Vivian committed
216

Gideon Greenspan committed
217 218
			if (qa_page_q_single_click_c($comment, $question, $commentparent, $pageerror))
				qa_page_q_refresh($pagestart, 'showcomments-'.$comment['parentid'], $commentparenttype, $comment['parentid']);
Scott Vivian committed
219

Gideon Greenspan committed
220 221 222 223
			if ($comment['editbutton']) {
				if (qa_clicked($prefix.'doedit')) {
					if (qa_page_q_permit_edit($comment, 'permit_edit_c', $pageerror)) // extra check here ensures error message is visible
						qa_page_q_refresh($pagestart, 'edit-'.$commentid, $commentparenttype, $comment['parentid']);
Scott Vivian committed
224

Gideon Greenspan committed
225 226 227 228
				} elseif (qa_clicked($prefix.'dosave') && qa_page_q_permit_edit($comment, 'permit_edit_c', $pageerror)) {

					if (qa_page_q_edit_c_submit($comment, $question, $commentparent, $ceditin[$commentid], $cediterrors[$commentid]))
						qa_page_q_refresh($pagestart, null, $commentparenttype, $comment['parentid']);
Scott Vivian committed
229

Gideon Greenspan committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
					else {
						$formtype='c_edit';
						$formpostid=$commentid; // keep editing if an error
					}

				} elseif (($pagestate==('edit-'.$commentid)) && qa_page_q_permit_edit($comment, 'permit_edit_c', $pageerror)) {
					$formtype='c_edit';
					$formpostid=$commentid;
				}
			}
		}


//	Functions used above - also see functions in qa-page-question-submit.php (which are shared with Ajax)

	function qa_page_q_refresh($start=0, $state=null, $showtype=null, $showid=null)
/*
	Redirects back to the question page, with the specified parameters
*/
	{
		$params=array();

		if ($start>0)
			$params['start']=$start;
		if (isset($state))
			$params['state']=$state;
Scott Vivian committed
256

Gideon Greenspan committed
257 258 259 260 261
		if (isset($showtype) && isset($showid)) {
			$anchor=qa_anchor($showtype, $showid);
			$params['show']=$showid;
		} else
			$anchor=null;
Scott Vivian committed
262

Gideon Greenspan committed
263 264 265
		qa_redirect(qa_request(), $params, null, null, $anchor);
	}

Scott Vivian committed
266

Gideon Greenspan committed
267 268 269 270 271 272
	function qa_page_q_permit_edit($post, $permitoption, &$error, $permitoption2=null)
/*
	Returns whether the editing operation (as specified by $permitoption or $permitoption2) on $post is permitted.
	If not, sets the $error variable appropriately
*/
	{
Gideon Greenspan committed
273 274 275 276
		// The 'login', 'confirm', 'userblock', 'ipblock' permission errors are reported to the user here
		// The other options ('approve', 'level') prevent the edit button being shown, in qa_page_q_post_rules(...)

		$permiterror=qa_user_post_permit_error($post['isbyuser'] ? null : $permitoption, $post);
Gideon Greenspan committed
277
			// if it's by the user, this will only check whether they are blocked
Scott Vivian committed
278

Gideon Greenspan committed
279
		if ($permiterror && isset($permitoption2)) {
Gideon Greenspan committed
280
			$permiterror2=qa_user_post_permit_error($post['isbyuser'] ? null : $permitoption2, $post);
Scott Vivian committed
281

Gideon Greenspan committed
282
			if ( ($permiterror=='level') || ($permiterror=='approve') || (!$permiterror2) ) // if it's a less strict error
Gideon Greenspan committed
283 284
				$permiterror=$permiterror2;
		}
Scott Vivian committed
285

Gideon Greenspan committed
286 287 288 289
		switch ($permiterror) {
			case 'login':
				$error=qa_insert_login_links(qa_lang_html('question/edit_must_login'), qa_request());
				break;
Scott Vivian committed
290

Gideon Greenspan committed
291 292 293
			case 'confirm':
				$error=qa_insert_login_links(qa_lang_html('question/edit_must_confirm'), qa_request());
				break;
Scott Vivian committed
294

Gideon Greenspan committed
295 296 297
			default:
				$error=qa_lang_html('users/no_permission');
				break;
Scott Vivian committed
298

Gideon Greenspan committed
299 300 301
			case false:
				break;
		}
Scott Vivian committed
302

Gideon Greenspan committed
303 304 305 306 307 308 309 310 311 312
		return !$permiterror;
	}


	function qa_page_q_edit_q_form(&$qa_content, $question, $in, $errors, $completetags, $categories)
/*
	Returns a $qa_content form for editing the question and sets up other parts of $qa_content accordingly
*/
	{
		$form=array(
Gideon Greenspan committed
313
			'tags' => 'method="post" action="'.qa_self_html().'"',
Scott Vivian committed
314

Gideon Greenspan committed
315
			'style' => 'tall',
Scott Vivian committed
316

Gideon Greenspan committed
317 318 319 320
			'fields' => array(
				'title' => array(
					'type' => $question['editable'] ? 'text' : 'static',
					'label' => qa_lang_html('question/q_title_label'),
Gideon Greenspan committed
321
					'tags' => 'name="q_title"',
Gideon Greenspan committed
322 323 324
					'value' => qa_html(($question['editable'] && isset($in['title'])) ? $in['title'] : $question['title']),
					'error' => qa_html(@$errors['title']),
				),
Scott Vivian committed
325

Gideon Greenspan committed
326 327 328 329
				'category' => array(
					'label' => qa_lang_html('question/q_category_label'),
					'error' => qa_html(@$errors['categoryid']),
				),
Scott Vivian committed
330

Gideon Greenspan committed
331 332 333 334
				'content' => array(
					'label' => qa_lang_html('question/q_content_label'),
					'error' => qa_html(@$errors['content']),
				),
Scott Vivian committed
335

Gideon Greenspan committed
336 337
				'extra' => array(
					'label' => qa_html(qa_opt('extra_field_prompt')),
Gideon Greenspan committed
338
					'tags' => 'name="q_extra"',
Gideon Greenspan committed
339 340 341
					'value' => qa_html(isset($in['extra']) ? $in['extra'] : $question['extra']),
					'error' => qa_html(@$errors['extra']),
				),
Scott Vivian committed
342

Gideon Greenspan committed
343 344 345 346 347
				'tags' => array(
					'error' => qa_html(@$errors['tags']),
				),

			),
Scott Vivian committed
348

Gideon Greenspan committed
349 350
			'buttons' => array(
				'save' => array(
Gideon Greenspan committed
351
					'tags' => 'onclick="qa_show_waiting_after(this, false);"',
Gideon Greenspan committed
352 353
					'label' => qa_lang_html('main/save_button'),
				),
Scott Vivian committed
354

Gideon Greenspan committed
355
				'cancel' => array(
Gideon Greenspan committed
356
					'tags' => 'name="docancel"',
Gideon Greenspan committed
357 358 359
					'label' => qa_lang_html('main/cancel_button'),
				),
			),
Scott Vivian committed
360

Gideon Greenspan committed
361 362
			'hidden' => array(
				'q_dosave' => '1',
Gideon Greenspan committed
363
				'code' => qa_get_form_security_code('edit-'.$question['postid']),
Gideon Greenspan committed
364 365
			),
		);
Scott Vivian committed
366

Gideon Greenspan committed
367 368 369
		if ($question['editable']) {
			$content=isset($in['content']) ? $in['content'] : $question['content'];
			$format=isset($in['format']) ? $in['format'] : $question['format'];
Scott Vivian committed
370

Gideon Greenspan committed
371 372
			$editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_qs');
			$editor=qa_load_editor($content, $format, $editorname);
Scott Vivian committed
373

Gideon Greenspan committed
374 375
			$form['fields']['content']=array_merge($form['fields']['content'],
				qa_editor_load_field($editor, $qa_content, $content, $format, 'q_content', 12, true));
Scott Vivian committed
376

Gideon Greenspan committed
377
			if (method_exists($editor, 'update_script'))
Gideon Greenspan committed
378
				$form['buttons']['save']['tags']='onclick="qa_show_waiting_after(this, false); '.$editor->update_script('q_content').'"';
Scott Vivian committed
379

Gideon Greenspan committed
380
			$form['hidden']['q_editor']=qa_html($editorname);
Scott Vivian committed
381

Gideon Greenspan committed
382 383
		} else
			unset($form['fields']['content']);
Scott Vivian committed
384

Gideon Greenspan committed
385 386
		if (qa_using_categories() && count($categories) && $question['retagcatable'])
			qa_set_up_category_field($qa_content, $form['fields']['category'], 'q_category', $categories,
Scott Vivian committed
387
				isset($in['categoryid']) ? $in['categoryid'] : $question['categoryid'],
Gideon Greenspan committed
388 389 390
				qa_opt('allow_no_category') || !isset($question['categoryid']), qa_opt('allow_no_sub_category'));
		else
			unset($form['fields']['category']);
Scott Vivian committed
391

Gideon Greenspan committed
392 393
		if (!($question['editable'] && qa_opt('extra_field_active')))
			unset($form['fields']['extra']);
Scott Vivian committed
394

Gideon Greenspan committed
395 396 397 398 399
		if (qa_using_tags() && $question['retagcatable'])
			qa_set_up_tag_field($qa_content, $form['fields']['tags'], 'q_tags', isset($in['tags']) ? $in['tags'] : qa_tagstring_to_tags($question['tags']),
				array(), $completetags, qa_opt('page_size_ask_tags'));
		else
			unset($form['fields']['tags']);
Scott Vivian committed
400

Gideon Greenspan committed
401 402 403 404
		if ($question['isbyuser']) {
			if (!qa_is_logged_in())
				qa_set_up_name_field($qa_content, $form['fields'], isset($in['name']) ? $in['name'] : @$question['name'], 'q_');

Gideon Greenspan committed
405 406 407
			qa_set_up_notify_fields($qa_content, $form['fields'], 'Q', qa_get_logged_in_email(),
				isset($in['notify']) ? $in['notify'] : !empty($question['notify']),
				isset($in['email']) ? $in['email'] : @$question['notify'], @$errors['email'], 'q_');
Gideon Greenspan committed
408
		}
Scott Vivian committed
409

Gideon Greenspan committed
410 411 412 413
		if (!qa_user_post_permit_error('permit_edit_silent', $question))
			$form['fields']['silent']=array(
				'type' => 'checkbox',
				'label' => qa_lang_html('question/save_silent_label'),
Gideon Greenspan committed
414
				'tags' => 'name="q_silent"',
Gideon Greenspan committed
415 416
				'value' => qa_html(@$in['silent']),
			);
Scott Vivian committed
417

Gideon Greenspan committed
418 419
		return $form;
	}
Scott Vivian committed
420 421


Gideon Greenspan committed
422 423 424 425 426 427
	function qa_page_q_edit_q_submit($question, $answers, $commentsfollows, $closepost, &$in, &$errors)
/*
	Processes a POSTed form for editing the question and returns true if successful
*/
	{
		$in=array();
Scott Vivian committed
428

Gideon Greenspan committed
429 430 431 432 433
		if ($question['editable']) {
			$in['title']=qa_post_text('q_title');
			qa_get_post_content('q_editor', 'q_content', $in['editor'], $in['content'], $in['format'], $in['text']);
			$in['extra']=qa_opt('extra_field_active') ? qa_post_text('q_extra') : null;
		}
Scott Vivian committed
434

Gideon Greenspan committed
435 436 437
		if ($question['retagcatable']) {
			if (qa_using_tags())
				$in['tags']=qa_get_tags_field_value('q_tags');
Scott Vivian committed
438

Gideon Greenspan committed
439 440 441
			if (qa_using_categories())
				$in['categoryid']=qa_get_category_field_value('q_category');
		}
Scott Vivian committed
442

Gideon Greenspan committed
443
		if (array_key_exists('categoryid', $in)) { // need to check if we can move it to that category, and if we need moderation
Gideon Greenspan committed
444 445 446
			$categories=qa_db_select_with_pending(qa_db_category_nav_selectspec($in['categoryid'], true));
			$categoryids=array_keys(qa_category_path($categories, $in['categoryid']));
			$userlevel=qa_user_level_for_categories($categoryids);
Scott Vivian committed
447

Gideon Greenspan committed
448 449
		} else
			$userlevel=null;
Scott Vivian committed
450

Gideon Greenspan committed
451
		if ($question['isbyuser']) {
Gideon Greenspan committed
452
			$in['name']=qa_post_text('q_name');
453
			$in['notify'] = !empty(qa_post_text('q_notify'));
Gideon Greenspan committed
454 455
			$in['email']=qa_post_text('q_email');
		}
Scott Vivian committed
456

Gideon Greenspan committed
457 458 459
		if (!qa_user_post_permit_error('permit_edit_silent', $question))
			$in['silent']=qa_post_text('q_silent');

Gideon Greenspan committed
460
		// here the $in array only contains values for parts of the form that were displayed, so those are only ones checked by filters
Scott Vivian committed
461

Gideon Greenspan committed
462
		$errors=array();
Scott Vivian committed
463

Gideon Greenspan committed
464 465
		if (!qa_check_form_security_code('edit-'.$question['postid'], qa_post_text('code')))
			$errors['page']=qa_lang_html('misc/form_security_again');
Scott Vivian committed
466

Gideon Greenspan committed
467 468
		else {
			$in['queued']=qa_opt('moderate_edited_again') && qa_user_moderation_reason($userlevel);
Scott Vivian committed
469

Gideon Greenspan committed
470 471 472 473
			$filtermodules=qa_load_modules_with('filter', 'filter_question');
			foreach ($filtermodules as $filtermodule) {
				$oldin=$in;
				$filtermodule->filter_question($in, $errors, $question);
Scott Vivian committed
474

Gideon Greenspan committed
475 476
				if ($question['editable'])
					qa_update_post_text($in, $oldin);
Gideon Greenspan committed
477
			}
Scott Vivian committed
478

Gideon Greenspan committed
479
			if (array_key_exists('categoryid', $in) && strcmp($in['categoryid'], $question['categoryid']))
Gideon Greenspan committed
480 481
				if (qa_user_permit_error('permit_post_q', null, $userlevel))
					$errors['categoryid']=qa_lang_html('question/category_ask_not_allowed');
Scott Vivian committed
482

Gideon Greenspan committed
483 484 485 486
			if (empty($errors)) {
				$userid=qa_get_logged_in_userid();
				$handle=qa_get_logged_in_handle();
				$cookieid=qa_cookie_get();
Scott Vivian committed
487

Gideon Greenspan committed
488 489
				// now we fill in the missing values in the $in array, so that we have everything we need for qa_question_set_content()
				// we do things in this way to avoid any risk of a validation failure on elements the user can't see (e.g. due to admin setting changes)
Scott Vivian committed
490

Gideon Greenspan committed
491 492 493 494 495 496 497
				if (!$question['editable']) {
					$in['title']=$question['title'];
					$in['content']=$question['content'];
					$in['format']=$question['format'];
					$in['text']=qa_viewer_text($in['content'], $in['format']);
					$in['extra']=$question['extra'];
				}
Scott Vivian committed
498

Gideon Greenspan committed
499 500
				if (!isset($in['tags']))
					$in['tags']=qa_tagstring_to_tags($question['tags']);
Scott Vivian committed
501

Gideon Greenspan committed
502 503
				if (!array_key_exists('categoryid', $in))
					$in['categoryid']=$question['categoryid'];
Scott Vivian committed
504

Gideon Greenspan committed
505 506
				if (!isset($in['silent']))
					$in['silent']=false;
Scott Vivian committed
507

Gideon Greenspan committed
508
				$setnotify=$question['isbyuser'] ? qa_combine_notify_email($question['userid'], $in['notify'], $in['email']) : $question['notify'];
Scott Vivian committed
509

Gideon Greenspan committed
510 511
				qa_question_set_content($question, $in['title'], $in['content'], $in['format'], $in['text'], qa_tags_to_tagstring($in['tags']),
					$setnotify, $userid, $handle, $cookieid, $in['extra'], @$in['name'], $in['queued'], $in['silent']);
Scott Vivian committed
512

Gideon Greenspan committed
513
				if (qa_using_categories() && strcmp($in['categoryid'], $question['categoryid']))
Gideon Greenspan committed
514 515
					qa_question_set_category($question, $in['categoryid'], $userid, $handle, $cookieid,
						$answers, $commentsfollows, $closepost, $in['silent']);
Scott Vivian committed
516

Gideon Greenspan committed
517 518
				return true;
			}
Gideon Greenspan committed
519
		}
Scott Vivian committed
520

Gideon Greenspan committed
521 522
		return false;
	}
Scott Vivian committed
523

Gideon Greenspan committed
524 525 526 527 528 529 530

	function qa_page_q_close_q_form(&$qa_content, $question, $id, $in, $errors)
/*
	Returns a $qa_content form for closing the question and sets up other parts of $qa_content accordingly
*/
	{
		$form=array(
Gideon Greenspan committed
531
			'tags' => 'method="post" action="'.qa_self_html().'"',
Scott Vivian committed
532

Gideon Greenspan committed
533
			'id' => $id,
Scott Vivian committed
534

Gideon Greenspan committed
535
			'style' => 'tall',
Scott Vivian committed
536

Gideon Greenspan committed
537
			'title' => qa_lang_html('question/close_form_title'),
Scott Vivian committed
538

Gideon Greenspan committed
539 540 541
			'fields' => array(
				'duplicate' => array(
					'type' => 'checkbox',
Gideon Greenspan committed
542
					'tags' => 'name="q_close_duplicate" id="q_close_duplicate" onchange="document.getElementById(\'q_close_details\').focus();"',
Gideon Greenspan committed
543 544 545
					'label' => qa_lang_html('question/close_duplicate'),
					'value' => @$in['duplicate'],
				),
Scott Vivian committed
546

Gideon Greenspan committed
547
				'details' => array(
Gideon Greenspan committed
548
					'tags' => 'name="q_close_details" id="q_close_details"',
Gideon Greenspan committed
549
					'label' =>
Gideon Greenspan committed
550 551 552
						'<span id="close_label_duplicate">'.qa_lang_html('question/close_original_title').' </span>'.
						'<span id="close_label_other">'.qa_lang_html('question/close_reason_title').'</span>',
					'note' => '<span id="close_note_duplicate" style="display:none;">'.qa_lang_html('question/close_original_note').'</span>',
Gideon Greenspan committed
553 554 555 556
					'value' => @$in['details'],
					'error' => qa_html(@$errors['details']),
				),
			),
Scott Vivian committed
557

Gideon Greenspan committed
558 559
			'buttons' => array(
				'close' => array(
Gideon Greenspan committed
560
					'tags' => 'onclick="qa_show_waiting_after(this, false);"',
Gideon Greenspan committed
561 562
					'label' => qa_lang_html('question/close_form_button'),
				),
Scott Vivian committed
563

Gideon Greenspan committed
564
				'cancel' => array(
Gideon Greenspan committed
565
					'tags' => 'name="docancel"',
Gideon Greenspan committed
566 567 568
					'label' => qa_lang_html('main/cancel_button'),
				),
			),
Scott Vivian committed
569

Gideon Greenspan committed
570 571
			'hidden' => array(
				'doclose' => '1',
Gideon Greenspan committed
572
				'code' => qa_get_form_security_code('close-'.$question['postid']),
Gideon Greenspan committed
573 574
			),
		);
Scott Vivian committed
575

Gideon Greenspan committed
576 577 578 579 580
		qa_set_display_rules($qa_content, array(
			'close_label_duplicate' => 'q_close_duplicate',
			'close_label_other' => '!q_close_duplicate',
			'close_note_duplicate' => 'q_close_duplicate',
		));
Scott Vivian committed
581

Gideon Greenspan committed
582 583 584 585
		$qa_content['focusid']='q_close_details';

		return $form;
	}
Scott Vivian committed
586 587


Gideon Greenspan committed
588 589 590 591 592 593 594 595 596
	function qa_page_q_close_q_submit($question, $closepost, &$in, &$errors)
/*
	Processes a POSTed form for closing the question and returns true if successful
*/
	{
		$in=array(
			'duplicate' => qa_post_text('q_close_duplicate'),
			'details' => qa_post_text('q_close_details'),
		);
Scott Vivian committed
597

Gideon Greenspan committed
598 599 600
		$userid=qa_get_logged_in_userid();
		$handle=qa_get_logged_in_handle();
		$cookieid=qa_cookie_get();
Scott Vivian committed
601

Gideon Greenspan committed
602 603
		if (!qa_check_form_security_code('close-'.$question['postid'], qa_post_text('code')))
			$errors['details']=qa_lang_html('misc/form_security_again');
Gideon Greenspan committed
604

Gideon Greenspan committed
605
		elseif ($in['duplicate']) {
Gideon Greenspan committed
606 607 608 609
			// be liberal in what we accept, but there are two potential unlikely pitfalls here:
			// a) URLs could have a fixed numerical path, e.g. http://qa.mysite.com/1/478/...
			// b) There could be a question title which is just a number, e.g. http://qa.mysite.com/478/12345/...
			// so we check if more than one question could match, and if so, show an error
Scott Vivian committed
610

Gideon Greenspan committed
611 612
			$parts=preg_split('|[=/&]|', $in['details'], -1, PREG_SPLIT_NO_EMPTY);
			$keypostids=array();
Scott Vivian committed
613

Gideon Greenspan committed
614 615 616
			foreach ($parts as $part)
				if (preg_match('/^[0-9]+$/', $part))
					$keypostids[$part]=true;
Scott Vivian committed
617

Gideon Greenspan committed
618
			$questionids=qa_db_posts_filter_q_postids(array_keys($keypostids));
Scott Vivian committed
619

Gideon Greenspan committed
620 621 622
			if ( (count($questionids)==1) && ($questionids[0]!=$question['postid']) ) {
				qa_question_close_duplicate($question, $closepost, $questionids[0], $userid, $handle, $cookieid);
				return true;
Scott Vivian committed
623

Gideon Greenspan committed
624 625
			} else
				$errors['details']=qa_lang('question/close_duplicate_error');
Scott Vivian committed
626

Gideon Greenspan committed
627 628 629 630
		} else {
			if (strlen($in['details'])>0) {
				qa_question_close_other($question, $closepost, $in['details'], $userid, $handle, $cookieid);
				return true;
Scott Vivian committed
631

Gideon Greenspan committed
632 633 634
			} else
				$errors['details']=qa_lang('main/field_required');
		}
Scott Vivian committed
635 636

		return false;
Gideon Greenspan committed
637
	}
Scott Vivian committed
638 639


Gideon Greenspan committed
640 641 642 643 644 645
	function qa_page_q_edit_a_form(&$qa_content, $id, $answer, $question, $answers, $commentsfollows, $in, $errors)
/*
	Returns a $qa_content form for editing an answer and sets up other parts of $qa_content accordingly
*/
	{
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
646

Gideon Greenspan committed
647 648
		$answerid=$answer['postid'];
		$prefix='a'.$answerid.'_';
Scott Vivian committed
649

Gideon Greenspan committed
650 651
		$content=isset($in['content']) ? $in['content'] : $answer['content'];
		$format=isset($in['format']) ? $in['format'] : $answer['format'];
Scott Vivian committed
652

Gideon Greenspan committed
653 654
		$editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_as');
		$editor=qa_load_editor($content, $format, $editorname);
Scott Vivian committed
655

Gideon Greenspan committed
656 657 658 659
		$hascomments=false;
		foreach ($commentsfollows as $commentfollow)
			if ($commentfollow['parentid']==$answerid)
				$hascomments=true;
Scott Vivian committed
660

Gideon Greenspan committed
661
		$form=array(
Gideon Greenspan committed
662
			'tags' => 'method="post" action="'.qa_self_html().'"',
Scott Vivian committed
663

Gideon Greenspan committed
664
			'id' => $id,
Scott Vivian committed
665

Gideon Greenspan committed
666
			'title' => qa_lang_html('question/edit_a_title'),
Scott Vivian committed
667

Gideon Greenspan committed
668
			'style' => 'tall',
Scott Vivian committed
669

Gideon Greenspan committed
670 671 672 673 674 675 676 677
			'fields' => array(
				'content' => array_merge(
					qa_editor_load_field($editor, $qa_content, $content, $format, $prefix.'content', 12),
					array(
						'error' => qa_html(@$errors['content']),
					)
				),
			),
Gideon Greenspan committed
678

Gideon Greenspan committed
679 680
			'buttons' => array(
				'save' => array(
Gideon Greenspan committed
681
					'tags' => 'onclick="qa_show_waiting_after(this, false); '.
Gideon Greenspan committed
682
						(method_exists($editor, 'update_script') ? $editor->update_script($prefix.'content') : '').'"',
Gideon Greenspan committed
683 684
					'label' => qa_lang_html('main/save_button'),
				),
Scott Vivian committed
685

Gideon Greenspan committed
686
				'cancel' => array(
Gideon Greenspan committed
687
					'tags' => 'name="docancel"',
Gideon Greenspan committed
688 689 690
					'label' => qa_lang_html('main/cancel_button'),
				),
			),
Scott Vivian committed
691

Gideon Greenspan committed
692 693 694
			'hidden' => array(
				$prefix.'editor' => qa_html($editorname),
				$prefix.'dosave' => '1',
Gideon Greenspan committed
695
				$prefix.'code' => qa_get_form_security_code('edit-'.$answerid),
Gideon Greenspan committed
696 697
			),
		);
Scott Vivian committed
698

Gideon Greenspan committed
699
	//	Show option to convert this answer to a comment, if appropriate
Scott Vivian committed
700

Gideon Greenspan committed
701 702 703 704
		$commentonoptions=array();

		$lastbeforeid=$question['postid']; // used to find last post created before this answer - this is default given
		$lastbeforetime=$question['created'];
Scott Vivian committed
705

Gideon Greenspan committed
706 707 708
		if ($question['commentable'])
			$commentonoptions[$question['postid']]=
				qa_lang_html('question/comment_on_q').qa_html(qa_shorten_string_line($question['title'], 80));
Scott Vivian committed
709

Gideon Greenspan committed
710 711 712 713
		foreach ($answers as $otheranswer)
			if (($otheranswer['postid']!=$answerid) && ($otheranswer['created']<$answer['created']) && $otheranswer['commentable'] && !$otheranswer['hidden']) {
				$commentonoptions[$otheranswer['postid']]=
					qa_lang_html('question/comment_on_a').qa_html(qa_shorten_string_line(qa_viewer_text($otheranswer['content'], $otheranswer['format']), 80));
Scott Vivian committed
714

Gideon Greenspan committed
715 716 717 718 719
				if ($otheranswer['created']>$lastbeforetime) {
					$lastbeforeid=$otheranswer['postid'];
					$lastebeforetime=$otheranswer['created'];
				}
			}
Scott Vivian committed
720

Gideon Greenspan committed
721 722
		if (count($commentonoptions)) {
			$form['fields']['tocomment']=array(
Gideon Greenspan committed
723 724 725
				'tags' => 'name="'.$prefix.'dotoc" id="'.$prefix.'dotoc"',
				'label' => '<span id="'.$prefix.'toshown">'.qa_lang_html('question/a_convert_to_c_on').'</span>'.
								'<span id="'.$prefix.'tohidden" style="display:none;">'.qa_lang_html('question/a_convert_to_c').'</span>',
Gideon Greenspan committed
726 727 728
				'type' => 'checkbox',
				'tight' => true,
			);
Scott Vivian committed
729

Gideon Greenspan committed
730
			$form['fields']['commenton']=array(
Gideon Greenspan committed
731
				'tags' => 'name="'.$prefix.'commenton"',
Gideon Greenspan committed
732 733 734 735 736 737
				'id' => $prefix.'commenton',
				'type' => 'select',
				'note' => qa_lang_html($hascomments ? 'question/a_convert_warn_cs' : 'question/a_convert_warn'),
				'options' => $commentonoptions,
				'value' => @$commentonoptions[$lastbeforeid],
			);
Scott Vivian committed
738

Gideon Greenspan committed
739 740 741 742 743 744
			qa_set_display_rules($qa_content, array(
				$prefix.'commenton' => $prefix.'dotoc',
				$prefix.'toshown' => $prefix.'dotoc',
				$prefix.'tohidden' => '!'.$prefix.'dotoc',
			));
		}
Scott Vivian committed
745

Gideon Greenspan committed
746
	//	Show name and notification field if appropriate
Scott Vivian committed
747

Gideon Greenspan committed
748 749 750
		if ($answer['isbyuser']) {
			if (!qa_is_logged_in())
				qa_set_up_name_field($qa_content, $form['fields'], isset($in['name']) ? $in['name'] : @$answer['name'], $prefix);
Scott Vivian committed
751

Gideon Greenspan committed
752 753 754
			qa_set_up_notify_fields($qa_content, $form['fields'], 'A', qa_get_logged_in_email(),
				isset($in['notify']) ? $in['notify'] : !empty($answer['notify']),
				isset($in['email']) ? $in['email'] : @$answer['notify'], @$errors['email'], $prefix);
Gideon Greenspan committed
755
		}
Scott Vivian committed
756

Gideon Greenspan committed
757 758 759 760
		if (!qa_user_post_permit_error('permit_edit_silent', $answer))
			$form['fields']['silent']=array(
				'type' => 'checkbox',
				'label' => qa_lang_html('question/save_silent_label'),
Gideon Greenspan committed
761
				'tags' => 'name="'.$prefix.'silent"',
Gideon Greenspan committed
762 763
				'value' => qa_html(@$in['silent']),
			);
Scott Vivian committed
764

Gideon Greenspan committed
765 766
		return $form;
	}
Scott Vivian committed
767 768


Gideon Greenspan committed
769 770 771 772 773 774 775
	function qa_page_q_edit_a_submit($answer, $question, $answers, $commentsfollows, &$in, &$errors)
/*
	Processes a POSTed form for editing an answer and returns the new type of the post if successful
*/
	{
		$answerid=$answer['postid'];
		$prefix='a'.$answerid.'_';
Scott Vivian committed
776

Gideon Greenspan committed
777 778 779 780
		$in=array(
			'dotoc' => qa_post_text($prefix.'dotoc'),
			'commenton' => qa_post_text($prefix.'commenton'),
		);
Scott Vivian committed
781

Gideon Greenspan committed
782
		if ($answer['isbyuser']) {
Gideon Greenspan committed
783
			$in['name']=qa_post_text($prefix.'name');
784
			$in['notify'] = !empty(qa_post_text($prefix.'notify'));
Gideon Greenspan committed
785 786
			$in['email']=qa_post_text($prefix.'email');
		}
Scott Vivian committed
787

Gideon Greenspan committed
788 789 790
		if (!qa_user_post_permit_error('permit_edit_silent', $answer))
			$in['silent']=qa_post_text($prefix.'silent');

Gideon Greenspan committed
791
		qa_get_post_content($prefix.'editor', $prefix.'content', $in['editor'], $in['content'], $in['format'], $in['text']);
Scott Vivian committed
792

Gideon Greenspan committed
793
		// here the $in array only contains values for parts of the form that were displayed, so those are only ones checked by filters
Scott Vivian committed
794

Gideon Greenspan committed
795
		$errors=array();
Scott Vivian committed
796

Gideon Greenspan committed
797 798
		if (!qa_check_form_security_code('edit-'.$answerid, qa_post_text($prefix.'code')))
			$errors['content']=qa_lang_html('misc/form_security_again');
Scott Vivian committed
799

Gideon Greenspan committed
800 801
		else {
			$in['queued']=qa_opt('moderate_edited_again') && qa_user_moderation_reason(qa_user_level_for_post($answer));
Scott Vivian committed
802

Gideon Greenspan committed
803 804 805 806 807 808
			$filtermodules=qa_load_modules_with('filter', 'filter_answer');
			foreach ($filtermodules as $filtermodule) {
				$oldin=$in;
				$filtermodule->filter_answer($in, $errors, $question, $answer);
				qa_update_post_text($in, $oldin);
			}
Scott Vivian committed
809

Gideon Greenspan committed
810 811 812 813
			if (empty($errors)) {
				$userid=qa_get_logged_in_userid();
				$handle=qa_get_logged_in_handle();
				$cookieid=qa_cookie_get();
Scott Vivian committed
814

Gideon Greenspan committed
815 816
				if (!isset($in['silent']))
					$in['silent']=false;
Scott Vivian committed
817 818 819

				$setnotify=$answer['isbyuser'] ? qa_combine_notify_email($answer['userid'], $in['notify'], $in['email']) : $answer['notify'];

Gideon Greenspan committed
820 821 822 823
				if ($in['dotoc'] && (
					(($in['commenton']==$question['postid']) && $question['commentable']) ||
					(($in['commenton']!=$answerid) && @$answers[$in['commenton']]['commentable'])
				)) { // convert to a comment
Scott Vivian committed
824

Gideon Greenspan committed
825 826 827
					if (qa_user_limits_remaining(QA_LIMIT_COMMENTS)) { // already checked 'permit_post_c'
						qa_answer_to_comment($answer, $in['commenton'], $in['content'], $in['format'], $in['text'], $setnotify,
							$userid, $handle, $cookieid, $question, $answers, $commentsfollows, @$in['name'], $in['queued'], $in['silent']);
Scott Vivian committed
828

Gideon Greenspan committed
829
						return 'C'; // to signify that redirect should be to the comment
Scott Vivian committed
830

Gideon Greenspan committed
831 832
					} else
						$errors['content']=qa_lang_html('question/comment_limit'); // not really best place for error, but it will do
Scott Vivian committed
833

Gideon Greenspan committed
834 835 836
				} else {
					qa_answer_set_content($answer, $in['content'], $in['format'], $in['text'], $setnotify,
						$userid, $handle, $cookieid, $question, @$in['name'], $in['queued'], $in['silent']);
Scott Vivian committed
837

Gideon Greenspan committed
838 839
					return 'A';
				}
Gideon Greenspan committed
840 841
			}
		}
Scott Vivian committed
842

Gideon Greenspan committed
843 844 845 846 847 848 849 850 851
		return null;
	}


	function qa_page_q_do_comment($question, $parent, $commentsfollows, $pagestart, $usecaptcha, &$cnewin, &$cnewerrors, &$formtype, &$formpostid, &$error)
/*
	Processes a request to add a comment to $parent, with antecedent $question, checking for permissions errors
*/
	{
Gideon Greenspan committed
852 853 854
		// The 'approve', 'login', 'confirm', 'userblock', 'ipblock' permission errors are reported to the user here
		// The other option ('level') prevents the comment button being shown, in qa_page_q_post_rules(...)

855 856
		$parentid = $parent['postid'];
		$answer = ($question['postid'] == $parentid) ? null : $parent;
Scott Vivian committed
857

Gideon Greenspan committed
858
		switch (qa_user_post_permit_error('permit_post_c', $parent, QA_LIMIT_COMMENTS)) {
Gideon Greenspan committed
859 860 861
			case 'login':
				$error=qa_insert_login_links(qa_lang_html('question/comment_must_login'), qa_request());
				break;
Scott Vivian committed
862

Gideon Greenspan committed
863 864 865
			case 'confirm':
				$error=qa_insert_login_links(qa_lang_html('question/comment_must_confirm'), qa_request());
				break;
Scott Vivian committed
866

Gideon Greenspan committed
867 868 869
			case 'approve':
				$error=qa_lang_html('question/comment_must_be_approved');
				break;
Scott Vivian committed
870

Gideon Greenspan committed
871 872 873
			case 'limit':
				$error=qa_lang_html('question/comment_limit');
				break;
Scott Vivian committed
874

Gideon Greenspan committed
875 876 877
			default:
				$error=qa_lang_html('users/no_permission');
				break;
Scott Vivian committed
878

Gideon Greenspan committed
879 880 881 882 883 884
			case false:
				if (qa_clicked('c'.$parentid.'_doadd')) {
					$commentid=qa_page_q_add_c_submit($question, $parent, $commentsfollows, $usecaptcha, $cnewin[$parentid], $cnewerrors[$parentid]);

					if (isset($commentid))
						qa_page_q_refresh($pagestart, null, $parent['basetype'], $parentid);
Scott Vivian committed
885

Gideon Greenspan committed
886 887 888 889 890 891 892 893 894 895 896 897 898
					else {
						$formtype='c_add';
						$formpostid=$parentid; // show form again
					}

				} else {
					$formtype='c_add';
					$formpostid=$parentid; // show form first time
				}
				break;
		}
	}

Scott Vivian committed
899

Gideon Greenspan committed
900 901 902 903 904 905 906
	function qa_page_q_edit_c_form(&$qa_content, $id, $comment, $in, $errors)
/*
	Returns a $qa_content form for editing a comment and sets up other parts of $qa_content accordingly
*/
	{
		$commentid=$comment['postid'];
		$prefix='c'.$commentid.'_';
Scott Vivian committed
907

Gideon Greenspan committed
908 909
		$content=isset($in['content']) ? $in['content'] : $comment['content'];
		$format=isset($in['format']) ? $in['format'] : $comment['format'];
Scott Vivian committed
910

Gideon Greenspan committed
911 912
		$editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_cs');
		$editor=qa_load_editor($content, $format, $editorname);
Scott Vivian committed
913

Gideon Greenspan committed
914
		$form=array(
Gideon Greenspan committed
915
			'tags' => 'method="post" action="'.qa_self_html().'"',
Scott Vivian committed
916

Gideon Greenspan committed
917
			'id' => $id,
Scott Vivian committed
918

Gideon Greenspan committed
919
			'title' => qa_lang_html('question/edit_c_title'),
Scott Vivian committed
920

Gideon Greenspan committed
921
			'style' => 'tall',
Scott Vivian committed
922

Gideon Greenspan committed
923 924 925 926 927 928 929 930
			'fields' => array(
				'content' => array_merge(
					qa_editor_load_field($editor, $qa_content, $content, $format, $prefix.'content', 4, true),
					array(
						'error' => qa_html(@$errors['content']),
					)
				),
			),
Scott Vivian committed
931

Gideon Greenspan committed
932 933
			'buttons' => array(
				'save' => array(
Gideon Greenspan committed
934
					'tags' => 'onclick="qa_show_waiting_after(this, false); '.
Gideon Greenspan committed
935
						(method_exists($editor, 'update_script') ? $editor->update_script($prefix.'content') : '').'"',
Gideon Greenspan committed
936 937
					'label' => qa_lang_html('main/save_button'),
				),
Scott Vivian committed
938

Gideon Greenspan committed
939
				'cancel' => array(
Gideon Greenspan committed
940
					'tags' => 'name="docancel"',
Gideon Greenspan committed
941 942 943
					'label' => qa_lang_html('main/cancel_button'),
				),
			),
Scott Vivian committed
944

Gideon Greenspan committed
945 946 947
			'hidden' => array(
				$prefix.'editor' => qa_html($editorname),
				$prefix.'dosave' => '1',
Gideon Greenspan committed
948
				$prefix.'code' => qa_get_form_security_code('edit-'.$commentid),
Gideon Greenspan committed
949 950
			),
		);
Scott Vivian committed
951

Gideon Greenspan committed
952 953 954 955
		if ($comment['isbyuser']) {
			if (!qa_is_logged_in())
				qa_set_up_name_field($qa_content, $form['fields'], isset($in['name']) ? $in['name'] : @$comment['name'], $prefix);

Gideon Greenspan committed
956 957 958
			qa_set_up_notify_fields($qa_content, $form['fields'], 'C', qa_get_logged_in_email(),
				isset($in['notify']) ? $in['notify'] : !empty($comment['notify']),
				isset($in['email']) ? $in['email'] : @$comment['notify'], @$errors['email'], $prefix);
Gideon Greenspan committed
959 960 961 962 963 964
		}

		if (!qa_user_post_permit_error('permit_edit_silent', $comment))
			$form['fields']['silent']=array(
				'type' => 'checkbox',
				'label' => qa_lang_html('question/save_silent_label'),
Gideon Greenspan committed
965
				'tags' => 'name="'.$prefix.'silent"',
Gideon Greenspan committed
966 967
				'value' => qa_html(@$in['silent']),
			);
Gideon Greenspan committed
968 969 970

		return $form;
	}
Scott Vivian committed
971 972


Gideon Greenspan committed
973 974 975 976 977 978 979
	function qa_page_q_edit_c_submit($comment, $question, $parent, &$in, &$errors)
/*
	Processes a POSTed form for editing a comment and returns true if successful
*/
	{
		$commentid=$comment['postid'];
		$prefix='c'.$commentid.'_';
Scott Vivian committed
980

Gideon Greenspan committed
981
		$in=array();
Scott Vivian committed
982

Gideon Greenspan committed
983
		if ($comment['isbyuser']) {
Gideon Greenspan committed
984
			$in['name']=qa_post_text($prefix.'name');
985
			$in['notify'] = !empty(qa_post_text($prefix.'notify'));
Gideon Greenspan committed
986 987 988
			$in['email']=qa_post_text($prefix.'email');
		}

Gideon Greenspan committed
989 990 991
		if (!qa_user_post_permit_error('permit_edit_silent', $comment))
			$in['silent']=qa_post_text($prefix.'silent');

Gideon Greenspan committed
992
		qa_get_post_content($prefix.'editor', $prefix.'content', $in['editor'], $in['content'], $in['format'], $in['text']);
Scott Vivian committed
993

Gideon Greenspan committed
994 995
		// here the $in array only contains values for parts of the form that were displayed, so those are only ones checked by filters

Gideon Greenspan committed
996
		$errors=array();
Scott Vivian committed
997

Gideon Greenspan committed
998 999
		if (!qa_check_form_security_code('edit-'.$commentid, qa_post_text($prefix.'code')))
			$errors['content']=qa_lang_html('misc/form_security_again');
Scott Vivian committed
1000

Gideon Greenspan committed
1001 1002
		else {
			$in['queued']=qa_opt('moderate_edited_again') && qa_user_moderation_reason(qa_user_level_for_post($comment));
Scott Vivian committed
1003

Gideon Greenspan committed
1004 1005 1006 1007 1008 1009
			$filtermodules=qa_load_modules_with('filter', 'filter_comment');
			foreach ($filtermodules as $filtermodule) {
				$oldin=$in;
				$filtermodule->filter_comment($in, $errors, $question, $parent, $comment);
				qa_update_post_text($in, $oldin);
			}
Scott Vivian committed
1010

Gideon Greenspan committed
1011 1012 1013 1014
			if (empty($errors)) {
				$userid=qa_get_logged_in_userid();
				$handle=qa_get_logged_in_handle();
				$cookieid=qa_cookie_get();
Scott Vivian committed
1015

Gideon Greenspan committed
1016 1017
				if (!isset($in['silent']))
					$in['silent']=false;
Scott Vivian committed
1018

Gideon Greenspan committed
1019
				$setnotify=$comment['isbyuser'] ? qa_combine_notify_email($comment['userid'], $in['notify'], $in['email']) : $comment['notify'];
Scott Vivian committed
1020

Gideon Greenspan committed
1021 1022
				qa_comment_set_content($comment, $in['content'], $in['format'], $in['text'], $setnotify,
					$userid, $handle, $cookieid, $question, $parent, @$in['name'], $in['queued'], $in['silent']);
Scott Vivian committed
1023

Gideon Greenspan committed
1024 1025
				return true;
			}
Gideon Greenspan committed
1026
		}
Scott Vivian committed
1027

Gideon Greenspan committed
1028 1029
		return false;
	}
Scott Vivian committed
1030

Gideon Greenspan committed
1031 1032 1033 1034

/*
	Omit PHP closing tag to help avoid accidental output
*/