qa-page.php 26.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.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Routing and utility functions for page requests


	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 42 43 44 45
	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-cookies.php';
	require_once QA_INCLUDE_DIR.'qa-app-format.php';
	require_once QA_INCLUDE_DIR.'qa-app-users.php';
	require_once QA_INCLUDE_DIR.'qa-app-options.php';
	require_once QA_INCLUDE_DIR.'qa-db-selects.php';


//	Functions which are called at the bottom of this file

	function qa_page_db_fail_handler($type, $errno=null, $error=null, $query=null)
/*
	Standard database failure handler function which bring up the install/repair/upgrade page
*/
	{
Gideon Greenspan committed
46
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
47

Gideon Greenspan committed
48 49 50 51
		$pass_failure_type=$type;
		$pass_failure_errno=$errno;
		$pass_failure_error=$error;
		$pass_failure_query=$query;
Scott Vivian committed
52

Gideon Greenspan committed
53
		require QA_INCLUDE_DIR.'qa-install.php';
Scott Vivian committed
54

Gideon Greenspan committed
55 56
		qa_exit('error');
	}
Scott Vivian committed
57 58


Gideon Greenspan committed
59 60 61 62 63
	function qa_page_queue_pending()
/*
	Queue any pending requests which are required independent of which page will be shown
*/
	{
Gideon Greenspan committed
64
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
65

Gideon Greenspan committed
66 67
		qa_preload_options();
		$loginuserid=qa_get_logged_in_userid();
Scott Vivian committed
68

Gideon Greenspan committed
69 70 71
		if (isset($loginuserid)) {
			if (!QA_FINAL_EXTERNAL_USERS)
				qa_db_queue_pending_select('loggedinuser', qa_db_user_account_selectspec($loginuserid, true));
Scott Vivian committed
72

Gideon Greenspan committed
73
			qa_db_queue_pending_select('notices', qa_db_user_notices_selectspec($loginuserid));
Gideon Greenspan committed
74 75 76
			qa_db_queue_pending_select('favoritenonqs', qa_db_user_favorite_non_qs_selectspec($loginuserid));
			qa_db_queue_pending_select('userlimits', qa_db_user_limits_selectspec($loginuserid));
			qa_db_queue_pending_select('userlevels', qa_db_user_levels_selectspec($loginuserid, true));
Gideon Greenspan committed
77
		}
Scott Vivian committed
78

Gideon Greenspan committed
79
		qa_db_queue_pending_select('iplimits', qa_db_ip_limits_selectspec(qa_remote_ip_address()));
Gideon Greenspan committed
80 81 82 83 84 85 86 87 88 89 90
		qa_db_queue_pending_select('navpages', qa_db_pages_selectspec(array('B', 'M', 'O', 'F')));
		qa_db_queue_pending_select('widgets', qa_db_widgets_selectspec());
	}


	function qa_load_state()
/*
	Check the page state parameter and then remove it from the $_GET array
*/
	{
		global $qa_state;
Scott Vivian committed
91

Gideon Greenspan committed
92 93 94
		$qa_state=qa_get('state');
		unset($_GET['state']); // to prevent being passed through on forms
	}
Scott Vivian committed
95 96


Gideon Greenspan committed
97 98 99 100 101
	function qa_check_login_modules()
/*
	If no user is logged in, call through to the login modules to see if they want to log someone in
*/
	{
Gideon Greenspan committed
102
		if ((!QA_FINAL_EXTERNAL_USERS) && !qa_is_logged_in()) {
Gideon Greenspan committed
103 104 105 106 107 108 109 110 111
			$loginmodules=qa_load_modules_with('login', 'check_login');

			foreach ($loginmodules as $loginmodule) {
				$loginmodule->check_login();
				if (qa_is_logged_in()) // stop and reload page if it worked
					qa_redirect(qa_request(), $_GET);
			}
		}
	}
Scott Vivian committed
112

Gideon Greenspan committed
113 114 115 116 117 118 119

	function qa_check_page_clicks()
/*
	React to any of the common buttons on a page for voting, favorites and closing a notice
	If the user has Javascript on, these should come through Ajax rather than here.
*/
	{
Gideon Greenspan committed
120
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
121

Gideon Greenspan committed
122
		global $qa_page_error_html;
Scott Vivian committed
123

Gideon Greenspan committed
124 125 126 127
		if (qa_is_http_post())
			foreach ($_POST as $field => $value) {
				if (strpos($field, 'vote_')===0) { // voting...
					@list($dummy, $postid, $vote, $anchor)=explode('_', $field);
Scott Vivian committed
128

Gideon Greenspan committed
129
					if (isset($postid) && isset($vote)) {
Gideon Greenspan committed
130 131
						if (!qa_check_form_security_code('vote', qa_post_text('code')))
							$qa_page_error_html=qa_lang_html('misc/form_security_again');
Scott Vivian committed
132

Gideon Greenspan committed
133 134 135
						else {
							require_once QA_INCLUDE_DIR.'qa-app-votes.php';
							require_once QA_INCLUDE_DIR.'qa-db-selects.php';
Scott Vivian committed
136

Gideon Greenspan committed
137
							$userid=qa_get_logged_in_userid();
Scott Vivian committed
138

Gideon Greenspan committed
139 140
							$post=qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $postid));
							$qa_page_error_html=qa_vote_error_html($post, $vote, $userid, qa_request());
Scott Vivian committed
141

Gideon Greenspan committed
142 143 144 145 146
							if (!$qa_page_error_html) {
								qa_vote_set($post, $userid, qa_get_logged_in_handle(), qa_cookie_get(), $vote);
								qa_redirect(qa_request(), $_GET, null, null, $anchor);
							}
							break;
Gideon Greenspan committed
147 148
						}
					}
Scott Vivian committed
149

Gideon Greenspan committed
150 151
				} elseif (strpos($field, 'favorite_')===0) { // favorites...
					@list($dummy, $entitytype, $entityid, $favorite)=explode('_', $field);
Scott Vivian committed
152

Gideon Greenspan committed
153
					if (isset($entitytype) && isset($entityid) && isset($favorite)) {
Gideon Greenspan committed
154 155
						if (!qa_check_form_security_code('favorite-'.$entitytype.'-'.$entityid, qa_post_text('code')))
							$qa_page_error_html=qa_lang_html('misc/form_security_again');
Scott Vivian committed
156

Gideon Greenspan committed
157 158
						else {
							require_once QA_INCLUDE_DIR.'qa-app-favorites.php';
Scott Vivian committed
159

Gideon Greenspan committed
160 161 162
							qa_user_favorite_set(qa_get_logged_in_userid(), qa_get_logged_in_handle(), qa_cookie_get(), $entitytype, $entityid, $favorite);
							qa_redirect(qa_request(), $_GET);
						}
Gideon Greenspan committed
163
					}
Scott Vivian committed
164

Gideon Greenspan committed
165 166
				} elseif (strpos($field, 'notice_')===0) { // notices...
					@list($dummy, $noticeid)=explode('_', $field);
Scott Vivian committed
167

Gideon Greenspan committed
168
					if (isset($noticeid)) {
Gideon Greenspan committed
169 170
						if (!qa_check_form_security_code('notice-'.$noticeid, qa_post_text('code')))
							$qa_page_error_html=qa_lang_html('misc/form_security_again');
Scott Vivian committed
171

Gideon Greenspan committed
172 173 174
						else {
							if ($noticeid=='visitor')
								setcookie('qa_noticed', 1, time()+86400*3650, '/', QA_COOKIE_DOMAIN);
Scott Vivian committed
175

Gideon Greenspan committed
176 177 178
							elseif ($noticeid=='welcome') {
								require_once QA_INCLUDE_DIR.'qa-db-users.php';
								qa_db_user_set_flag(qa_get_logged_in_userid(), QA_USER_FLAGS_WELCOME_NOTICE, false);
Scott Vivian committed
179

Gideon Greenspan committed
180 181 182 183
							} else {
								require_once QA_INCLUDE_DIR.'qa-db-notices.php';
								qa_db_usernotice_delete(qa_get_logged_in_userid(), $noticeid);
							}
Scott Vivian committed
184

Gideon Greenspan committed
185
							qa_redirect(qa_request(), $_GET);
Gideon Greenspan committed
186 187 188 189 190
						}
					}
				}
			}
	}
Scott Vivian committed
191

Gideon Greenspan committed
192 193 194

	function qa_get_request_content()
/*
Scott Vivian committed
195
	Run the appropriate qa-page-*.php file for this request and return back the $qa_content it passed
Gideon Greenspan committed
196 197
*/
	{
Gideon Greenspan committed
198
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
199

Gideon Greenspan committed
200 201 202 203
		$requestlower=strtolower(qa_request());
		$requestparts=qa_request_parts();
		$firstlower=strtolower($requestparts[0]);
		$routing=qa_page_routing();
Scott Vivian committed
204

Gideon Greenspan committed
205 206 207
		if (isset($routing[$requestlower])) {
			qa_set_template($firstlower);
			$qa_content=require QA_INCLUDE_DIR.$routing[$requestlower];
Scott Vivian committed
208

Gideon Greenspan committed
209 210 211
		} elseif (isset($routing[$firstlower.'/'])) {
			qa_set_template($firstlower);
			$qa_content=require QA_INCLUDE_DIR.$routing[$firstlower.'/'];
Scott Vivian committed
212

Gideon Greenspan committed
213 214 215
		} elseif (is_numeric($requestparts[0])) {
			qa_set_template('question');
			$qa_content=require QA_INCLUDE_DIR.'qa-page-question.php';
Scott Vivian committed
216

Gideon Greenspan committed
217 218 219 220
		} else {
			qa_set_template(strlen($firstlower) ? $firstlower : 'qa'); // will be changed later
			$qa_content=require QA_INCLUDE_DIR.'qa-page-default.php'; // handles many other pages, including custom pages and page modules
		}
Scott Vivian committed
221

Gideon Greenspan committed
222 223 224 225
		if ($firstlower=='admin') {
			$_COOKIE['qa_admin_last']=$requestlower; // for navigation tab now...
			setcookie('qa_admin_last', $_COOKIE['qa_admin_last'], 0, '/', QA_COOKIE_DOMAIN); // ...and in future
		}
Scott Vivian committed
226

Gideon Greenspan committed
227
		qa_set_form_security_key();
Gideon Greenspan committed
228 229 230 231

		return $qa_content;
	}

Scott Vivian committed
232

Gideon Greenspan committed
233 234 235 236 237
	function qa_output_content($qa_content)
/*
	Output the $qa_content via the theme class after doing some pre-processing, mainly relating to Javascript
*/
	{
Gideon Greenspan committed
238
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
239

Gideon Greenspan committed
240
		global $qa_template;
Scott Vivian committed
241

Gideon Greenspan committed
242
		$requestlower=strtolower(qa_request());
Scott Vivian committed
243

Gideon Greenspan committed
244
	//	Set appropriate selected flags for navigation (not done in qa_content_prepare() since it also applies to sub-navigation)
Scott Vivian committed
245

Gideon Greenspan committed
246 247 248
		foreach ($qa_content['navigation'] as $navtype => $navigation)
			if (is_array($navigation) && ($navtype!='cat'))
				foreach ($navigation as $navprefix => $navlink)
Gideon Greenspan committed
249
					if (substr($requestlower.'$', 0, strlen($navprefix)) == $navprefix)
Gideon Greenspan committed
250
						$qa_content['navigation'][$navtype][$navprefix]['selected']=true;
Scott Vivian committed
251

Gideon Greenspan committed
252
	//	Slide down notifications
Scott Vivian committed
253

Gideon Greenspan committed
254 255 256 257 258 259
		if (!empty($qa_content['notices']))
			foreach ($qa_content['notices'] as $notice) {
				$qa_content['script_onloads'][]=array(
					"qa_reveal(document.getElementById(".qa_js($notice['id'])."), 'notice');",
				);
			}
Scott Vivian committed
260

Gideon Greenspan committed
261
	//	Handle maintenance mode
Scott Vivian committed
262

Gideon Greenspan committed
263 264 265 266
		if (qa_opt('site_maintenance') && ($requestlower!='login')) {
			if (qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN) {
				if (!isset($qa_content['error']))
					$qa_content['error']=strtr(qa_lang_html('admin/maintenance_admin_only'), array(
Gideon Greenspan committed
267 268
						'^1' => '<a href="'.qa_path_html('admin/general').'">',
						'^2' => '</a>',
Gideon Greenspan committed
269
					));
Scott Vivian committed
270

Gideon Greenspan committed
271 272 273 274 275
			} else {
				$qa_content=qa_content_prepare();
				$qa_content['error']=qa_lang_html('misc/site_in_maintenance');
			}
		}
Scott Vivian committed
276

Gideon Greenspan committed
277
	//	Handle new users who must confirm their email now, or must be approved before continuing
Scott Vivian committed
278

Gideon Greenspan committed
279
		$userid=qa_get_logged_in_userid();
Gideon Greenspan committed
280 281
		if (isset($userid) && ($requestlower!='confirm') && ($requestlower!='account')) {
			$flags=qa_get_logged_in_flags();
Scott Vivian committed
282

Gideon Greenspan committed
283
			if ( ($flags & QA_USER_FLAGS_MUST_CONFIRM) && (!($flags & QA_USER_FLAGS_EMAIL_CONFIRMED)) && qa_opt('confirm_user_emails') ) {
Gideon Greenspan committed
284 285 286
				$qa_content=qa_content_prepare();
				$qa_content['title']=qa_lang_html('users/confirm_title');
				$qa_content['error']=strtr(qa_lang_html('users/confirm_required'), array(
Gideon Greenspan committed
287 288
					'^1' => '<a href="'.qa_path_html('confirm').'">',
					'^2' => '</a>',
Gideon Greenspan committed
289
				));
Scott Vivian committed
290

Gideon Greenspan committed
291 292 293 294
			} elseif ( ($flags & QA_USER_FLAGS_MUST_APPROVE) && (qa_get_logged_in_level()<QA_USER_LEVEL_APPROVED) && qa_opt('moderate_users') ) {
				$qa_content=qa_content_prepare();
				$qa_content['title']=qa_lang_html('users/approve_title');
				$qa_content['error']=strtr(qa_lang_html('users/approve_required'), array(
Gideon Greenspan committed
295 296
					'^1' => '<a href="'.qa_path_html('account').'">',
					'^2' => '</a>',
Gideon Greenspan committed
297
				));
Gideon Greenspan committed
298
			}
Gideon Greenspan committed
299
		}
Scott Vivian committed
300

Gideon Greenspan committed
301
	//	Combine various Javascript elements in $qa_content into single array for theme layer
Scott Vivian committed
302

Gideon Greenspan committed
303
		$script=array('<script type="text/javascript">');
Scott Vivian committed
304

Gideon Greenspan committed
305 306 307
		if (isset($qa_content['script_var']))
			foreach ($qa_content['script_var'] as $var => $value)
				$script[]='var '.$var.'='.qa_js($value).';';
Scott Vivian committed
308

Gideon Greenspan committed
309 310 311 312 313
		if (isset($qa_content['script_lines']))
			foreach ($qa_content['script_lines'] as $scriptlines) {
				$script[]='';
				$script=array_merge($script, $scriptlines);
			}
Scott Vivian committed
314

Gideon Greenspan committed
315 316 317 318 319 320 321 322
		if (isset($qa_content['focusid']))
			$qa_content['script_onloads'][]=array(
				"var elem=document.getElementById(".qa_js($qa_content['focusid']).");",
				"if (elem) {",
				"\telem.select();",
				"\telem.focus();",
				"}",
			);
Scott Vivian committed
323

Gideon Greenspan committed
324 325 326 327 328 329 330 331
		if (isset($qa_content['script_onloads'])) {
			array_push($script,
				'',
				'var qa_oldonload=window.onload;',
				'window.onload=function() {',
				"\tif (typeof qa_oldonload=='function')",
				"\t\tqa_oldonload();"
			);
Scott Vivian committed
332

Gideon Greenspan committed
333 334
			foreach ($qa_content['script_onloads'] as $scriptonload) {
				$script[]="\t";
Scott Vivian committed
335

Gideon Greenspan committed
336 337 338
				foreach ((array)$scriptonload as $scriptline)
					$script[]="\t".$scriptline;
			}
Scott Vivian committed
339

Gideon Greenspan committed
340
			$script[]='};';
Gideon Greenspan committed
341
		}
Scott Vivian committed
342

Gideon Greenspan committed
343
		$script[]='</script>';
Scott Vivian committed
344

Gideon Greenspan committed
345 346 347
		if (isset($qa_content['script_rel'])) {
			$uniquerel=array_unique($qa_content['script_rel']); // remove any duplicates
			foreach ($uniquerel as $script_rel)
Gideon Greenspan committed
348
				$script[]='<script src="'.qa_html(qa_path_to_root().$script_rel).'" type="text/javascript"></script>';
Gideon Greenspan committed
349
		}
Scott Vivian committed
350

Gideon Greenspan committed
351 352 353
		if (isset($qa_content['script_src'])) {
			$uniquesrc=array_unique($qa_content['script_src']); // remove any duplicates
			foreach ($uniquesrc as $script_src)
Gideon Greenspan committed
354
				$script[]='<script src="'.qa_html($script_src).'" type="text/javascript"></script>';
Gideon Greenspan committed
355
		}
Scott Vivian committed
356

Gideon Greenspan committed
357 358 359
		$qa_content['script']=$script;

	//	Load the appropriate theme class and output the page
Scott Vivian committed
360

Gideon Greenspan committed
361
		$themeclass=qa_load_theme_class(qa_get_site_theme(), (substr($qa_template, 0, 7)=='custom-') ? 'custom' : $qa_template, $qa_content, qa_request());
Scott Vivian committed
362

Gideon Greenspan committed
363
		header('Content-type: '.$qa_content['content_type']);
Scott Vivian committed
364

Gideon Greenspan committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		$themeclass->doctype();
		$themeclass->html();
		$themeclass->finish();
	}


	function qa_do_content_stats($qa_content)
/*
	Update any statistics required by the fields in $qa_content, and return true if something was done
*/
	{
		if (isset($qa_content['inc_views_postid'])) {
			require_once QA_INCLUDE_DIR.'qa-db-hotness.php';
			qa_db_hotness_update($qa_content['inc_views_postid'], null, true);
			return true;
		}
Scott Vivian committed
381

Gideon Greenspan committed
382 383 384 385 386 387 388 389 390 391 392 393
		return false;
	}


//	Other functions which might be called from anywhere

	function qa_page_routing()
/*
	Return an array of the default Q2A requests and which qa-page-*.php file implements them
	If the key of an element ends in /, it should be used for any request with that key as its prefix
*/
	{
Gideon Greenspan committed
394
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
395

Gideon Greenspan committed
396 397 398 399
		return array(
			'account' => 'qa-page-account.php',
			'activity/' => 'qa-page-activity.php',
			'admin/' => 'qa-page-admin-default.php',
Gideon Greenspan committed
400
			'admin/approve' => 'qa-page-admin-approve.php',
Gideon Greenspan committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
			'admin/categories' => 'qa-page-admin-categories.php',
			'admin/flagged' => 'qa-page-admin-flagged.php',
			'admin/hidden' => 'qa-page-admin-hidden.php',
			'admin/layoutwidgets' => 'qa-page-admin-widgets.php',
			'admin/moderate' => 'qa-page-admin-moderate.php',
			'admin/pages' => 'qa-page-admin-pages.php',
			'admin/plugins' => 'qa-page-admin-plugins.php',
			'admin/points' => 'qa-page-admin-points.php',
			'admin/recalc' => 'qa-page-admin-recalc.php',
			'admin/stats' => 'qa-page-admin-stats.php',
			'admin/userfields' => 'qa-page-admin-userfields.php',
			'admin/usertitles' => 'qa-page-admin-usertitles.php',
			'answers/' => 'qa-page-answers.php',
			'ask' => 'qa-page-ask.php',
			'categories/' => 'qa-page-categories.php',
			'comments/' => 'qa-page-comments.php',
			'confirm' => 'qa-page-confirm.php',
			'favorites' => 'qa-page-favorites.php',
			'feedback' => 'qa-page-feedback.php',
			'forgot' => 'qa-page-forgot.php',
			'hot/' => 'qa-page-hot.php',
			'ip/' => 'qa-page-ip.php',
			'login' => 'qa-page-login.php',
			'logout' => 'qa-page-logout.php',
			'message/' => 'qa-page-message.php',
			'questions/' => 'qa-page-questions.php',
			'register' => 'qa-page-register.php',
			'reset' => 'qa-page-reset.php',
			'search' => 'qa-page-search.php',
			'tag/' => 'qa-page-tag.php',
			'tags' => 'qa-page-tags.php',
			'unanswered/' => 'qa-page-unanswered.php',
			'unsubscribe' => 'qa-page-unsubscribe.php',
			'updates' => 'qa-page-updates.php',
			'user/' => 'qa-page-user.php',
			'users' => 'qa-page-users.php',
			'users/blocked' => 'qa-page-users-blocked.php',
			'users/special' => 'qa-page-users-special.php',
		);
	}
Scott Vivian committed
441 442


Gideon Greenspan committed
443 444 445 446 447 448 449 450
	function qa_set_template($template)
/*
	Sets the template which should be passed to the theme class, telling it which type of page it's displaying
*/
	{
		global $qa_template;
		$qa_template=$template;
	}
Scott Vivian committed
451 452


Gideon Greenspan committed
453 454 455 456 457 458
	function qa_content_prepare($voting=false, $categoryids=null)
/*
	Start preparing theme content in global $qa_content variable, with or without $voting support,
	in the context of the categories in $categoryids (if not null)
*/
	{
Gideon Greenspan committed
459
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
460

Gideon Greenspan committed
461
		global $qa_template, $qa_page_error_html;
Scott Vivian committed
462

Gideon Greenspan committed
463 464
		if (QA_DEBUG_PERFORMANCE)
			qa_usage_mark('control');
Scott Vivian committed
465

Gideon Greenspan committed
466 467 468 469
		$request=qa_request();
		$requestlower=qa_request();
		$navpages=qa_db_get_pending_result('navpages');
		$widgets=qa_db_get_pending_result('widgets');
Scott Vivian committed
470

Gideon Greenspan committed
471 472
		if (isset($categoryids) && !is_array($categoryids)) // accept old-style parameter
			$categoryids=array($categoryids);
Scott Vivian committed
473

Gideon Greenspan committed
474
		$lastcategoryid=count($categoryids) ? end($categoryids) : null;
Scott Vivian committed
475

Gideon Greenspan committed
476 477
		$qa_content=array(
			'content_type' => 'text/html; charset=utf-8',
Scott Vivian committed
478

Gideon Greenspan committed
479
			'site_title' => qa_html(qa_opt('site_title')),
Scott Vivian committed
480

Gideon Greenspan committed
481
			'head_lines' => array(),
Scott Vivian committed
482

Gideon Greenspan committed
483 484 485 486
			'navigation' => array(
				'user' => array(),

				'main' => array(),
Scott Vivian committed
487

Gideon Greenspan committed
488 489 490 491 492 493
				'footer' => array(
					'feedback' => array(
						'url' => qa_path_html('feedback'),
						'label' => qa_lang_html('main/nav_feedback'),
					),
				),
Scott Vivian committed
494

Gideon Greenspan committed
495
			),
Scott Vivian committed
496

Gideon Greenspan committed
497
			'sidebar' => qa_opt('show_custom_sidebar') ? qa_opt('custom_sidebar') : null,
Scott Vivian committed
498

Gideon Greenspan committed
499
			'sidepanel' => qa_opt('show_custom_sidepanel') ? qa_opt('custom_sidepanel') : null,
Scott Vivian committed
500

Gideon Greenspan committed
501 502 503 504 505
			'widgets' => array(),
		);

		if (qa_opt('show_custom_in_head'))
			$qa_content['head_lines'][]=qa_opt('custom_in_head');
Scott Vivian committed
506

Gideon Greenspan committed
507 508
		if (qa_opt('show_custom_header'))
			$qa_content['body_header']=qa_opt('custom_header');
Scott Vivian committed
509

Gideon Greenspan committed
510 511 512 513 514
		if (qa_opt('show_custom_footer'))
			$qa_content['body_footer']=qa_opt('custom_footer');

		if (isset($categoryids))
			$qa_content['categoryids']=$categoryids;
Scott Vivian committed
515

Gideon Greenspan committed
516 517 518
		foreach ($navpages as $page)
			if ($page['nav']=='B')
				qa_navigation_add_page($qa_content['navigation']['main'], $page);
Scott Vivian committed
519

Gideon Greenspan committed
520 521 522 523 524 525 526 527 528 529 530
		if (qa_opt('nav_home') && qa_opt('show_custom_home'))
			$qa_content['navigation']['main']['$']=array(
				'url' => qa_path_html(''),
				'label' => qa_lang_html('main/nav_home'),
			);

		if (qa_opt('nav_activity'))
			$qa_content['navigation']['main']['activity']=array(
				'url' => qa_path_html('activity'),
				'label' => qa_lang_html('main/nav_activity'),
			);
Scott Vivian committed
531

Gideon Greenspan committed
532
		$hascustomhome=qa_has_custom_home();
Scott Vivian committed
533

Gideon Greenspan committed
534 535 536 537 538
		if (qa_opt($hascustomhome ? 'nav_qa_not_home' : 'nav_qa_is_home'))
			$qa_content['navigation']['main'][$hascustomhome ? 'qa' : '$']=array(
				'url' => qa_path_html($hascustomhome ? 'qa' : ''),
				'label' => qa_lang_html('main/nav_qa'),
			);
Scott Vivian committed
539

Gideon Greenspan committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
		if (qa_opt('nav_questions'))
			$qa_content['navigation']['main']['questions']=array(
				'url' => qa_path_html('questions'),
				'label' => qa_lang_html('main/nav_qs'),
			);

		if (qa_opt('nav_hot'))
			$qa_content['navigation']['main']['hot']=array(
				'url' => qa_path_html('hot'),
				'label' => qa_lang_html('main/nav_hot'),
			);

		if (qa_opt('nav_unanswered'))
			$qa_content['navigation']['main']['unanswered']=array(
				'url' => qa_path_html('unanswered'),
				'label' => qa_lang_html('main/nav_unanswered'),
			);
Scott Vivian committed
557

Gideon Greenspan committed
558 559 560 561 562
		if (qa_using_tags() && qa_opt('nav_tags'))
			$qa_content['navigation']['main']['tag']=array(
				'url' => qa_path_html('tags'),
				'label' => qa_lang_html('main/nav_tags'),
			);
Scott Vivian committed
563

Gideon Greenspan committed
564 565 566 567 568 569 570 571 572 573 574
		if (qa_using_categories() && qa_opt('nav_categories'))
			$qa_content['navigation']['main']['categories']=array(
				'url' => qa_path_html('categories'),
				'label' => qa_lang_html('main/nav_categories'),
			);

		if (qa_opt('nav_users'))
			$qa_content['navigation']['main']['user']=array(
				'url' => qa_path_html('users'),
				'label' => qa_lang_html('main/nav_users'),
			);
Scott Vivian committed
575

Gideon Greenspan committed
576 577 578
		// Only the 'level' permission error prevents the menu option being shown - others reported on qa-page-ask.php

		if (qa_opt('nav_ask') && (qa_user_maximum_permit_error('permit_post_q')!='level'))
Gideon Greenspan committed
579 580 581 582
			$qa_content['navigation']['main']['ask']=array(
				'url' => qa_path_html('ask', (qa_using_categories() && strlen($lastcategoryid)) ? array('cat' => $lastcategoryid) : null),
				'label' => qa_lang_html('main/nav_ask'),
			);
Scott Vivian committed
583 584


Gideon Greenspan committed
585 586
		if (
			(qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN) ||
Gideon Greenspan committed
587 588 589
			(!qa_user_maximum_permit_error('permit_moderate')) ||
			(!qa_user_maximum_permit_error('permit_hide_show')) ||
			(!qa_user_maximum_permit_error('permit_delete_hidden'))
Gideon Greenspan committed
590 591 592 593 594 595
		)
			$qa_content['navigation']['main']['admin']=array(
				'url' => qa_path_html('admin'),
				'label' => qa_lang_html('main/nav_admin'),
			);

Scott Vivian committed
596

Gideon Greenspan committed
597
		$qa_content['search']=array(
Gideon Greenspan committed
598
			'form_tags' => 'method="get" action="'.qa_path_html('search').'"',
Gideon Greenspan committed
599 600
			'form_extra' => qa_path_form_html('search'),
			'title' => qa_lang_html('main/search_title'),
Gideon Greenspan committed
601
			'field_tags' => 'name="q"',
Gideon Greenspan committed
602 603
			'button_label' => qa_lang_html('main/search_button'),
		);
Scott Vivian committed
604

Gideon Greenspan committed
605 606
		if (!qa_opt('feedback_enabled'))
			unset($qa_content['navigation']['footer']['feedback']);
Scott Vivian committed
607

Gideon Greenspan committed
608 609 610
		foreach ($navpages as $page)
			if ( ($page['nav']=='M') || ($page['nav']=='O') || ($page['nav']=='F') )
				qa_navigation_add_page($qa_content['navigation'][($page['nav']=='F') ? 'footer' : 'main'], $page);
Scott Vivian committed
611

Gideon Greenspan committed
612 613 614 615 616
		$regioncodes=array(
			'F' => 'full',
			'M' => 'main',
			'S' => 'side',
		);
Scott Vivian committed
617

Gideon Greenspan committed
618 619 620 621 622 623
		$placecodes=array(
			'T' => 'top',
			'H' => 'high',
			'L' => 'low',
			'B' => 'bottom',
		);
Scott Vivian committed
624

Gideon Greenspan committed
625 626 627 628
		foreach ($widgets as $widget)
			if (is_numeric(strpos(','.$widget['tags'].',', ','.$qa_template.',')) || is_numeric(strpos(','.$widget['tags'].',', ',all,'))) { // see if it has been selected for display on this template
				$region=@$regioncodes[substr($widget['place'], 0, 1)];
				$place=@$placecodes[substr($widget['place'], 1, 2)];
Scott Vivian committed
629

Gideon Greenspan committed
630 631
				if (isset($region) && isset($place)) { // check region/place codes recognized
					$module=qa_load_module('widget', $widget['title']);
Scott Vivian committed
632

Gideon Greenspan committed
633
					if (
Gideon Greenspan committed
634 635
						isset($module) &&
						method_exists($module, 'allow_template') &&
Gideon Greenspan committed
636
						$module->allow_template((substr($qa_template, 0, 7)=='custom-') ? 'custom' : $qa_template) &&
Gideon Greenspan committed
637 638 639
						method_exists($module, 'allow_region') &&
						$module->allow_region($region) &&
						method_exists($module, 'output_widget')
Gideon Greenspan committed
640 641 642 643
					)
						$qa_content['widgets'][$region][$place][]=$module; // if module loaded and happy to be displayed here, tell theme about it
				}
			}
Scott Vivian committed
644

Gideon Greenspan committed
645 646 647 648
		$logoshow=qa_opt('logo_show');
		$logourl=qa_opt('logo_url');
		$logowidth=qa_opt('logo_width');
		$logoheight=qa_opt('logo_height');
Scott Vivian committed
649

Gideon Greenspan committed
650
		if ($logoshow)
Gideon Greenspan committed
651 652 653 654
			$qa_content['logo']='<a href="'.qa_path_html('').'" class="qa-logo-link" title="'.qa_html(qa_opt('site_title')).'">'.
				'<img src="'.qa_html(is_numeric(strpos($logourl, '://')) ? $logourl : qa_path_to_root().$logourl).'"'.
				($logowidth ? (' width="'.$logowidth.'"') : '').($logoheight ? (' height="'.$logoheight.'"') : '').
				' border="0" alt="'.qa_html(qa_opt('site_title')).'"/></a>';
Gideon Greenspan committed
655
		else
Gideon Greenspan committed
656
			$qa_content['logo']='<a href="'.qa_path_html('').'" class="qa-logo-link">'.qa_html(qa_opt('site_title')).'</a>';
Gideon Greenspan committed
657 658 659 660

		$topath=qa_get('to'); // lets user switch between login and register without losing destination page

		$userlinks=qa_get_login_links(qa_path_to_root(), isset($topath) ? $topath : qa_path($request, $_GET, ''));
Scott Vivian committed
661

Gideon Greenspan committed
662
		$qa_content['navigation']['user']=array();
Scott Vivian committed
663

Gideon Greenspan committed
664 665 666 667 668
		if (qa_is_logged_in()) {
			$qa_content['loggedin']=qa_lang_html_sub_split('main/logged_in_x', QA_FINAL_EXTERNAL_USERS
				? qa_get_logged_in_user_html(qa_get_logged_in_user_cache(), qa_path_to_root(), false)
				: qa_get_one_user_html(qa_get_logged_in_handle(), false)
			);
Scott Vivian committed
669

Gideon Greenspan committed
670 671 672 673
			$qa_content['navigation']['user']['updates']=array(
				'url' => qa_path_html('updates'),
				'label' => qa_lang_html('main/nav_updates'),
			);
Scott Vivian committed
674

Gideon Greenspan committed
675 676 677 678 679
			if (!empty($userlinks['logout']))
				$qa_content['navigation']['user']['logout']=array(
					'url' => qa_html(@$userlinks['logout']),
					'label' => qa_lang_html('main/nav_logout'),
				);
Scott Vivian committed
680

Gideon Greenspan committed
681 682
			if (!QA_FINAL_EXTERNAL_USERS) {
				$source=qa_get_logged_in_source();
Scott Vivian committed
683

Gideon Greenspan committed
684 685
				if (strlen($source)) {
					$loginmodules=qa_load_modules_with('login', 'match_source');
Scott Vivian committed
686

Gideon Greenspan committed
687 688 689 690 691 692 693 694
					foreach ($loginmodules as $module)
						if ($module->match_source($source) && method_exists($module, 'logout_html')) {
							ob_start();
							$module->logout_html(qa_path('logout', array(), qa_opt('site_url')));
							$qa_content['navigation']['user']['logout']=array('label' => ob_get_clean());
						}
				}
			}
Scott Vivian committed
695

Gideon Greenspan committed
696 697 698
			$notices=qa_db_get_pending_result('notices');
			foreach ($notices as $notice)
				$qa_content['notices'][]=qa_notice_form($notice['noticeid'], qa_viewer_html($notice['content'], $notice['format']), $notice);
Scott Vivian committed
699

Gideon Greenspan committed
700
		} else {
Gideon Greenspan committed
701
			require_once QA_INCLUDE_DIR.'qa-util-string.php';
Scott Vivian committed
702

Gideon Greenspan committed
703 704
			if (!QA_FINAL_EXTERNAL_USERS) {
				$loginmodules=qa_load_modules_with('login', 'login_html');
Scott Vivian committed
705

Gideon Greenspan committed
706 707 708 709
				foreach ($loginmodules as $tryname => $module) {
					ob_start();
					$module->login_html(isset($topath) ? (qa_opt('site_url').$topath) : qa_path($request, $_GET, qa_opt('site_url')), 'menu');
					$label=ob_get_clean();
Scott Vivian committed
710

Gideon Greenspan committed
711 712 713
					if (strlen($label))
						$qa_content['navigation']['user'][implode('-', qa_string_to_words($tryname))]=array('label' => $label);
				}
Gideon Greenspan committed
714
			}
Scott Vivian committed
715

Gideon Greenspan committed
716 717 718 719 720
			if (!empty($userlinks['login']))
				$qa_content['navigation']['user']['login']=array(
					'url' => qa_html(@$userlinks['login']),
					'label' => qa_lang_html('main/nav_login'),
				);
Scott Vivian committed
721

Gideon Greenspan committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
			if (!empty($userlinks['register']))
				$qa_content['navigation']['user']['register']=array(
					'url' => qa_html(@$userlinks['register']),
					'label' => qa_lang_html('main/nav_register'),
				);
		}

		if (QA_FINAL_EXTERNAL_USERS || !qa_is_logged_in()) {
			if (qa_opt('show_notice_visitor') && (!isset($topath)) && (!isset($_COOKIE['qa_noticed'])))
				$qa_content['notices'][]=qa_notice_form('visitor', qa_opt('notice_visitor'));

		} else {
			setcookie('qa_noticed', 1, time()+86400*3650, '/', QA_COOKIE_DOMAIN); // don't show first-time notice if a user has logged in

			if (qa_opt('show_notice_welcome') && (qa_get_logged_in_flags() & QA_USER_FLAGS_WELCOME_NOTICE) )
				if ( ($requestlower!='confirm') && ($requestlower!='account') ) // let people finish registering in peace
					$qa_content['notices'][]=qa_notice_form('welcome', qa_opt('notice_welcome'));
		}
Scott Vivian committed
740

Gideon Greenspan committed
741
		$qa_content['script_rel']=array('qa-content/jquery-1.7.2.min.js');
Gideon Greenspan committed
742
		$qa_content['script_rel'][]='qa-content/qa-page.js?'.QA_VERSION;
Scott Vivian committed
743

Gideon Greenspan committed
744
		if ($voting)
Gideon Greenspan committed
745
			$qa_content['error']=@$qa_page_error_html;
Scott Vivian committed
746

Gideon Greenspan committed
747 748 749 750
		$qa_content['script_var']=array(
			'qa_root' => qa_path_to_root(),
			'qa_request' => $request,
		);
Scott Vivian committed
751

Gideon Greenspan committed
752 753 754 755 756 757 758 759 760 761 762
		return $qa_content;
	}


	function qa_get_start()
/*
	Get the start parameter which should be used, as constrained by the setting in qa-config.php
*/
	{
		return min(max(0, (int)qa_get('start')), QA_MAX_LIMIT_START);
	}
Scott Vivian committed
763 764


Gideon Greenspan committed
765 766 767 768 769 770 771 772
	function qa_get_state()
/*
	Get the state parameter which should be used, as set earlier in qa_load_state()
*/
	{
		global $qa_state;
		return $qa_state;
	}
Scott Vivian committed
773

Gideon Greenspan committed
774 775 776 777 778

//	Below are the steps that actually execute for this file - all the above are function definitions

	qa_report_process_stage('init_page');
	qa_db_connect('qa_page_db_fail_handler');
Scott Vivian committed
779

Gideon Greenspan committed
780 781 782
	qa_page_queue_pending();
	qa_load_state();
	qa_check_login_modules();
Scott Vivian committed
783

Gideon Greenspan committed
784 785 786 787 788 789
	if (QA_DEBUG_PERFORMANCE)
		qa_usage_mark('setup');

	qa_check_page_clicks();

	$qa_content=qa_get_request_content();
Scott Vivian committed
790

Gideon Greenspan committed
791 792 793 794 795 796 797 798
	if (is_array($qa_content)) {
		if (QA_DEBUG_PERFORMANCE)
			qa_usage_mark('view');

		qa_output_content($qa_content);

		if (QA_DEBUG_PERFORMANCE)
			qa_usage_mark('theme');
Scott Vivian committed
799

Gideon Greenspan committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813
		if (qa_do_content_stats($qa_content))
			if (QA_DEBUG_PERFORMANCE)
				qa_usage_mark('stats');

		if (QA_DEBUG_PERFORMANCE)
			qa_usage_output();
	}

	qa_db_disconnect();


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