qa-page-ask.php 9.67 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-ask.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Controller for ask a question page


	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 34 35 36 37 38 39 40 41
	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-format.php';
	require_once QA_INCLUDE_DIR.'qa-app-limits.php';
	require_once QA_INCLUDE_DIR.'qa-db-selects.php';
	require_once QA_INCLUDE_DIR.'qa-util-sort.php';


//	Check whether this is a follow-on question and get some info we need from the database

	$in=array();
Scott Vivian committed
42

Gideon Greenspan committed
43
	$followpostid=qa_get('follow');
Gideon Greenspan committed
44
	$in['categoryid']=qa_clicked('doask') ? qa_get_category_field_value('category') : qa_get('cat');
Gideon Greenspan committed
45
	$userid=qa_get_logged_in_userid();
Scott Vivian committed
46

Gideon Greenspan committed
47
	list($categories, $followanswer, $completetags)=qa_db_select_with_pending(
Gideon Greenspan committed
48 49 50 51
		qa_db_category_nav_selectspec($in['categoryid'], true),
		isset($followpostid) ? qa_db_full_post_selectspec($userid, $followpostid) : null,
		qa_db_popular_tags_selectspec(0, QA_DB_RETRIEVE_COMPLETE_TAGS)
	);
Scott Vivian committed
52

Gideon Greenspan committed
53 54
	if (!isset($categories[$in['categoryid']]))
		$in['categoryid']=null;
Scott Vivian committed
55

Gideon Greenspan committed
56 57
	if (@$followanswer['basetype']!='A')
		$followanswer=null;
Scott Vivian committed
58

Gideon Greenspan committed
59 60 61

//	Check for permission error

Gideon Greenspan committed
62
	$permiterror=qa_user_maximum_permit_error('permit_post_q', QA_LIMIT_QUESTIONS);
Gideon Greenspan committed
63 64 65

	if ($permiterror) {
		$qa_content=qa_content_prepare();
Scott Vivian committed
66

Gideon Greenspan committed
67 68 69
		// The 'approve', 'login', 'confirm', 'limit', 'userblock', 'ipblock' permission errors are reported to the user here
		// The other option ('level') prevents the menu option being shown, in qa_content_prepare(...)

Gideon Greenspan committed
70 71 72 73
		switch ($permiterror) {
			case 'login':
				$qa_content['error']=qa_insert_login_links(qa_lang_html('question/ask_must_login'), qa_request(), isset($followpostid) ? array('follow' => $followpostid) : null);
				break;
Scott Vivian committed
74

Gideon Greenspan committed
75 76 77
			case 'confirm':
				$qa_content['error']=qa_insert_login_links(qa_lang_html('question/ask_must_confirm'), qa_request(), isset($followpostid) ? array('follow' => $followpostid) : null);
				break;
Scott Vivian committed
78

Gideon Greenspan committed
79 80 81
			case 'limit':
				$qa_content['error']=qa_lang_html('question/ask_limit');
				break;
Scott Vivian committed
82

Gideon Greenspan committed
83 84 85
			case 'approve':
				$qa_content['error']=qa_lang_html('question/ask_must_be_approved');
				break;
Scott Vivian committed
86

Gideon Greenspan committed
87 88 89 90
			default:
				$qa_content['error']=qa_lang_html('users/no_permission');
				break;
		}
Scott Vivian committed
91

Gideon Greenspan committed
92 93
		return $qa_content;
	}
Scott Vivian committed
94

Gideon Greenspan committed
95 96

//	Process input
Scott Vivian committed
97

Gideon Greenspan committed
98
	$captchareason=qa_user_captcha_reason();
Scott Vivian committed
99

Gideon Greenspan committed
100 101
	$in['title']=qa_post_text('title'); // allow title and tags to be posted by an external form
	$in['extra']=qa_opt('extra_field_active') ? qa_post_text('extra') : null;
Gideon Greenspan committed
102 103
	if (qa_using_tags())
		$in['tags']=qa_get_tags_field_value('tags');
Gideon Greenspan committed
104 105 106 107

	if (qa_clicked('doask')) {
		require_once QA_INCLUDE_DIR.'qa-app-post-create.php';
		require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
108

Gideon Greenspan committed
109 110
		$categoryids=array_keys(qa_category_path($categories, @$in['categoryid']));
		$userlevel=qa_user_level_for_categories($categoryids);
Scott Vivian committed
111

Gideon Greenspan committed
112
		$in['name']=qa_post_text('name');
113
		$in['notify'] = strlen(qa_post_text('notify')) > 0;
Gideon Greenspan committed
114
		$in['email']=qa_post_text('email');
115
		$in['queued'] = qa_user_moderation_reason($userlevel) !== false;
Scott Vivian committed
116

Gideon Greenspan committed
117
		qa_get_post_content('editor', 'content', $in['editor'], $in['content'], $in['format'], $in['text']);
Scott Vivian committed
118

Gideon Greenspan committed
119
		$errors=array();
Gideon Greenspan committed
120 121 122

		if (!qa_check_form_security_code('ask', qa_post_text('code')))
			$errors['page']=qa_lang_html('misc/form_security_again');
Scott Vivian committed
123

Gideon Greenspan committed
124 125 126 127 128 129 130
		else {
			$filtermodules=qa_load_modules_with('filter', 'filter_question');
			foreach ($filtermodules as $filtermodule) {
				$oldin=$in;
				$filtermodule->filter_question($in, $errors, null);
				qa_update_post_text($in, $oldin);
			}
Scott Vivian committed
131

Gideon Greenspan committed
132 133 134 135
			if (qa_using_categories() && count($categories) && (!qa_opt('allow_no_category')) && !isset($in['categoryid']))
				$errors['categoryid']=qa_lang_html('question/category_required'); // check this here because we need to know count($categories)
			elseif (qa_user_permit_error('permit_post_q', null, $userlevel))
				$errors['categoryid']=qa_lang_html('question/category_ask_not_allowed');
Scott Vivian committed
136

Gideon Greenspan committed
137 138 139 140
			if ($captchareason) {
				require_once 'qa-app-captcha.php';
				qa_captcha_validate_post($errors);
			}
Scott Vivian committed
141

Gideon Greenspan committed
142 143
			if (empty($errors)) {
				$cookieid=isset($userid) ? qa_cookie_get() : qa_cookie_get_create(); // create a new cookie if necessary
Scott Vivian committed
144

Gideon Greenspan committed
145
				$questionid=qa_question_create($followanswer, $userid, qa_get_logged_in_handle(), $cookieid,
Gideon Greenspan committed
146
					$in['title'], $in['content'], $in['format'], $in['text'], isset($in['tags']) ? qa_tags_to_tagstring($in['tags']) : '',
Gideon Greenspan committed
147
					$in['notify'], $in['email'], $in['categoryid'], $in['extra'], $in['queued'], $in['name']);
Scott Vivian committed
148

Gideon Greenspan committed
149 150
				qa_redirect(qa_q_request($questionid, $in['title'])); // our work is done here
			}
Gideon Greenspan committed
151 152 153 154 155 156 157
		}
	}


//	Prepare content for theme

	$qa_content=qa_content_prepare(false, array_keys(qa_category_path($categories, @$in['categoryid'])));
Scott Vivian committed
158

Gideon Greenspan committed
159
	$qa_content['title']=qa_lang_html(isset($followanswer) ? 'question/ask_follow_title' : 'question/ask_title');
Gideon Greenspan committed
160
	$qa_content['error']=@$errors['page'];
Gideon Greenspan committed
161 162 163 164 165 166 167

	$editorname=isset($in['editor']) ? $in['editor'] : qa_opt('editor_for_qs');
	$editor=qa_load_editor(@$in['content'], @$in['format'], $editorname);

	$field=qa_editor_load_field($editor, $qa_content, @$in['content'], @$in['format'], 'content', 12, false);
	$field['label']=qa_lang_html('question/q_content_label');
	$field['error']=qa_html(@$errors['content']);
Scott Vivian committed
168

Gideon Greenspan committed
169 170 171
	$custom=qa_opt('show_custom_ask') ? trim(qa_opt('custom_ask')) : '';

	$qa_content['form']=array(
Gideon Greenspan committed
172
		'tags' => 'name="ask" method="post" action="'.qa_self_html().'"',
Scott Vivian committed
173

Gideon Greenspan committed
174
		'style' => 'tall',
Scott Vivian committed
175

Gideon Greenspan committed
176 177 178 179 180
		'fields' => array(
			'custom' => array(
				'type' => 'custom',
				'note' => $custom,
			),
Scott Vivian committed
181

Gideon Greenspan committed
182 183
			'title' => array(
				'label' => qa_lang_html('question/q_title_label'),
Gideon Greenspan committed
184
				'tags' => 'name="title" id="title" autocomplete="off"',
Gideon Greenspan committed
185 186 187
				'value' => qa_html(@$in['title']),
				'error' => qa_html(@$errors['title']),
			),
Scott Vivian committed
188

Gideon Greenspan committed
189 190
			'similar' => array(
				'type' => 'custom',
Gideon Greenspan committed
191
				'html' => '<span id="similar"></span>',
Gideon Greenspan committed
192
			),
Scott Vivian committed
193

Gideon Greenspan committed
194 195
			'content' => $field,
		),
Scott Vivian committed
196

Gideon Greenspan committed
197 198
		'buttons' => array(
			'ask' => array(
Gideon Greenspan committed
199
				'tags' => 'onclick="qa_show_waiting_after(this, false); '.
Gideon Greenspan committed
200
					(method_exists($editor, 'update_script') ? $editor->update_script('content') : '').'"',
Gideon Greenspan committed
201 202 203
				'label' => qa_lang_html('question/ask_button'),
			),
		),
Scott Vivian committed
204

Gideon Greenspan committed
205 206
		'hidden' => array(
			'editor' => qa_html($editorname),
Gideon Greenspan committed
207
			'code' => qa_get_form_security_code('ask'),
Gideon Greenspan committed
208 209 210
			'doask' => '1',
		),
	);
Scott Vivian committed
211

Gideon Greenspan committed
212 213 214 215 216
	if (!strlen($custom))
		unset($qa_content['form']['fields']['custom']);

	if (qa_opt('do_ask_check_qs') || qa_opt('do_example_tags')) {
		$qa_content['script_rel'][]='qa-content/qa-ask.js?'.QA_VERSION;
Gideon Greenspan committed
217
		$qa_content['form']['fields']['title']['tags'].=' onchange="qa_title_change(this.value);"';
Scott Vivian committed
218

Gideon Greenspan committed
219 220 221
		if (strlen(@$in['title']))
			$qa_content['script_onloads'][]='qa_title_change('.qa_js($in['title']).');';
	}
Scott Vivian committed
222

Gideon Greenspan committed
223 224
	if (isset($followanswer)) {
		$viewer=qa_load_viewer($followanswer['content'], $followanswer['format']);
Scott Vivian committed
225

Gideon Greenspan committed
226 227 228 229 230
		$field=array(
			'type' => 'static',
			'label' => qa_lang_html('question/ask_follow_from_a'),
			'value' => $viewer->get_html($followanswer['content'], $followanswer['format'], array('blockwordspreg' => qa_get_block_words_preg())),
		);
Scott Vivian committed
231

Gideon Greenspan committed
232 233
		qa_array_insert($qa_content['form']['fields'], 'title', array('follows' => $field));
	}
Scott Vivian committed
234

Gideon Greenspan committed
235 236 237 238 239
	if (qa_using_categories() && count($categories)) {
		$field=array(
			'label' => qa_lang_html('question/q_category_label'),
			'error' => qa_html(@$errors['categoryid']),
		);
Scott Vivian committed
240

Gideon Greenspan committed
241
		qa_set_up_category_field($qa_content, $field, 'category', $categories, $in['categoryid'], true, qa_opt('allow_no_sub_category'));
Scott Vivian committed
242

Gideon Greenspan committed
243 244
		if (!qa_opt('allow_no_category')) // don't auto-select a category even though one is required
			$field['options']['']='';
Scott Vivian committed
245

Gideon Greenspan committed
246 247
		qa_array_insert($qa_content['form']['fields'], 'content', array('category' => $field));
	}
Scott Vivian committed
248

Gideon Greenspan committed
249 250 251
	if (qa_opt('extra_field_active')) {
		$field=array(
			'label' => qa_html(qa_opt('extra_field_prompt')),
Gideon Greenspan committed
252
			'tags' => 'name="extra"',
Gideon Greenspan committed
253 254 255
			'value' => qa_html(@$in['extra']),
			'error' => qa_html(@$errors['extra']),
		);
Scott Vivian committed
256

Gideon Greenspan committed
257 258
		qa_array_insert($qa_content['form']['fields'], null, array('extra' => $field));
	}
Scott Vivian committed
259

Gideon Greenspan committed
260 261 262 263
	if (qa_using_tags()) {
		$field=array(
			'error' => qa_html(@$errors['tags']),
		);
Scott Vivian committed
264

Gideon Greenspan committed
265 266
		qa_set_up_tag_field($qa_content, $field, 'tags', isset($in['tags']) ? $in['tags'] : array(), array(),
			qa_opt('do_complete_tags') ? array_keys($completetags) : array(), qa_opt('page_size_ask_tags'));
Scott Vivian committed
267

Gideon Greenspan committed
268 269
		qa_array_insert($qa_content['form']['fields'], null, array('tags' => $field));
	}
Scott Vivian committed
270

Gideon Greenspan committed
271 272
	if (!isset($userid))
		qa_set_up_name_field($qa_content, $qa_content['form']['fields'], @$in['name']);
Scott Vivian committed
273

Gideon Greenspan committed
274 275
	qa_set_up_notify_fields($qa_content, $qa_content['form']['fields'], 'Q', qa_get_logged_in_email(),
		isset($in['notify']) ? $in['notify'] : qa_opt('notify_users_default'), @$in['email'], @$errors['email']);
Scott Vivian committed
276

Gideon Greenspan committed
277
	if ($captchareason) {
Gideon Greenspan committed
278
		require_once 'qa-app-captcha.php';
Gideon Greenspan committed
279
		qa_set_up_captcha_field($qa_content, $qa_content['form']['fields'], @$errors, qa_captcha_reason_note($captchareason));
Gideon Greenspan committed
280
	}
Scott Vivian committed
281

Gideon Greenspan committed
282 283
	$qa_content['focusid']='title';

Scott Vivian committed
284

Gideon Greenspan committed
285 286 287 288 289 290
	return $qa_content;


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