qa-page.php 26.8 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-page.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.

	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;
	}

Scott committed
28 29 30 31
	require_once QA_INCLUDE_DIR.'app/cookies.php';
	require_once QA_INCLUDE_DIR.'app/format.php';
	require_once QA_INCLUDE_DIR.'app/users.php';
	require_once QA_INCLUDE_DIR.'app/options.php';
Scott committed
32
	require_once QA_INCLUDE_DIR.'db/selects.php';
Scott committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129


//	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
*/
	{
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

		$pass_failure_type=$type;
		$pass_failure_errno=$errno;
		$pass_failure_error=$error;
		$pass_failure_query=$query;

		require_once QA_INCLUDE_DIR.'qa-install.php';

		qa_exit('error');
	}


	function qa_page_queue_pending()
/*
	Queue any pending requests which are required independent of which page will be shown
*/
	{
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

		qa_preload_options();
		$loginuserid=qa_get_logged_in_userid();

		if (isset($loginuserid)) {
			if (!QA_FINAL_EXTERNAL_USERS)
				qa_db_queue_pending_select('loggedinuser', qa_db_user_account_selectspec($loginuserid, true));

			qa_db_queue_pending_select('notices', qa_db_user_notices_selectspec($loginuserid));
			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));
		}

		qa_db_queue_pending_select('iplimits', qa_db_ip_limits_selectspec(qa_remote_ip_address()));
		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;

		$qa_state=qa_get('state');
		unset($_GET['state']); // to prevent being passed through on forms
	}


	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
*/
	{
		if ((!QA_FINAL_EXTERNAL_USERS) && !qa_is_logged_in()) {
			$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);
			}
		}
	}


	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.
*/
	{
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

		global $qa_page_error_html;

		if (qa_is_http_post())
			foreach ($_POST as $field => $value) {
				if (strpos($field, 'vote_')===0) { // voting...
					@list($dummy, $postid, $vote, $anchor)=explode('_', $field);

					if (isset($postid) && isset($vote)) {
						if (!qa_check_form_security_code('vote', qa_post_text('code')))
							$qa_page_error_html=qa_lang_html('misc/form_security_again');

						else {
Scott committed
130
							require_once QA_INCLUDE_DIR.'app/votes.php';
Scott committed
131
							require_once QA_INCLUDE_DIR.'db/selects.php';
Scott committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

							$userid=qa_get_logged_in_userid();

							$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());

							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;
						}
					}

				} elseif (strpos($field, 'favorite_')===0) { // favorites...
					@list($dummy, $entitytype, $entityid, $favorite)=explode('_', $field);

					if (isset($entitytype) && isset($entityid) && isset($favorite)) {
						if (!qa_check_form_security_code('favorite-'.$entitytype.'-'.$entityid, qa_post_text('code')))
							$qa_page_error_html=qa_lang_html('misc/form_security_again');

						else {
Scott committed
154
							require_once QA_INCLUDE_DIR.'app/favorites.php';
Scott committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

							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);
						}
					}

				} elseif (strpos($field, 'notice_')===0) { // notices...
					@list($dummy, $noticeid)=explode('_', $field);

					if (isset($noticeid)) {
						if (!qa_check_form_security_code('notice-'.$noticeid, qa_post_text('code')))
							$qa_page_error_html=qa_lang_html('misc/form_security_again');

						else {
							if ($noticeid=='visitor')
								setcookie('qa_noticed', 1, time()+86400*3650, '/', QA_COOKIE_DOMAIN);

							elseif ($noticeid=='welcome') {
Scott committed
173
								require_once QA_INCLUDE_DIR.'db/users.php';
Scott committed
174 175 176
								qa_db_user_set_flag(qa_get_logged_in_userid(), QA_USER_FLAGS_WELCOME_NOTICE, false);

							} else {
Scott committed
177
								require_once QA_INCLUDE_DIR.'db/notices.php';
Scott committed
178 179 180 181 182 183 184 185 186 187 188
								qa_db_usernotice_delete(qa_get_logged_in_userid(), $noticeid);
							}

							qa_redirect(qa_request(), $_GET);
						}
					}
				}
			}
	}


Scott committed
189 190 191
	/**
	 *	Run the appropriate qa-page-*.php file for this request and return back the $qa_content it passed
	 */
Scott committed
192 193 194 195
	function qa_get_request_content()
	{
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

Scott committed
196 197 198 199
		$requestlower = strtolower(qa_request());
		$requestparts = qa_request_parts();
		$firstlower = strtolower($requestparts[0]);
		$routing = qa_page_routing();
Scott committed
200 201 202

		if (isset($routing[$requestlower])) {
			qa_set_template($firstlower);
Scott committed
203
			$qa_content = require QA_INCLUDE_DIR.$routing[$requestlower];
Scott committed
204 205 206

		} elseif (isset($routing[$firstlower.'/'])) {
			qa_set_template($firstlower);
Scott committed
207
			$qa_content = require QA_INCLUDE_DIR.$routing[$firstlower.'/'];
Scott committed
208 209 210

		} elseif (is_numeric($requestparts[0])) {
			qa_set_template('question');
Scott committed
211
			$qa_content = require QA_INCLUDE_DIR.'pages/question.php';
Scott committed
212 213 214

		} else {
			qa_set_template(strlen($firstlower) ? $firstlower : 'qa'); // will be changed later
Scott committed
215
			$qa_content = require QA_INCLUDE_DIR.'pages/default.php'; // handles many other pages, including custom pages and page modules
Scott committed
216 217
		}

Scott committed
218 219
		if ($firstlower == 'admin') {
			$_COOKIE['qa_admin_last'] = $requestlower; // for navigation tab now...
Scott committed
220 221 222 223 224 225 226 227 228 229
			setcookie('qa_admin_last', $_COOKIE['qa_admin_last'], 0, '/', QA_COOKIE_DOMAIN); // ...and in future
		}

		if (isset($qa_content))
			qa_set_form_security_key();

		return $qa_content;
	}


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

		global $qa_template;

239
		$requestlower = strtolower(qa_request());
Scott committed
240 241 242

	//	Set appropriate selected flags for navigation (not done in qa_content_prepare() since it also applies to sub-navigation)

243 244 245 246 247 248
		foreach ($qa_content['navigation'] as $navtype => $navigation) {
			if (!is_array($navigation) || $navtype == 'cat') {
				continue;
			}

			foreach ($navigation as $navprefix => $navlink) {
249
				$selected =& $qa_content['navigation'][$navtype][$navprefix]['selected'];
250
				if (isset($navlink['selected_on'])) {
251
					// match specified paths
252
					foreach ($navlink['selected_on'] as $path) {
253
						if (strpos($requestlower.'$', $path) === 0)
254
							$selected = true;
255 256
					}
				}
257 258
				elseif ($requestlower === $navprefix || $requestlower.'$' === $navprefix) {
					// exact match for array key
259
					$selected = true;
260 261 262
				}
			}
		}
Scott committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

	//	Slide down notifications

		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');",
				);
			}

	//	Handle maintenance mode

		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(
						'^1' => '<a href="'.qa_path_html('admin/general').'">',
						'^2' => '</a>',
					));

			} else {
				$qa_content=qa_content_prepare();
				$qa_content['error']=qa_lang_html('misc/site_in_maintenance');
			}
		}

	//	Handle new users who must confirm their email now, or must be approved before continuing

		$userid=qa_get_logged_in_userid();
		if (isset($userid) && ($requestlower!='confirm') && ($requestlower!='account')) {
			$flags=qa_get_logged_in_flags();

			if ( ($flags & QA_USER_FLAGS_MUST_CONFIRM) && (!($flags & QA_USER_FLAGS_EMAIL_CONFIRMED)) && qa_opt('confirm_user_emails') ) {
				$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(
					'^1' => '<a href="'.qa_path_html('confirm').'">',
					'^2' => '</a>',
				));

			} 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(
					'^1' => '<a href="'.qa_path_html('account').'">',
					'^2' => '</a>',
				));
			}
		}

	//	Combine various Javascript elements in $qa_content into single array for theme layer

Scott committed
315
		$script = array('<script>');
Scott committed
316 317 318

		if (isset($qa_content['script_var']))
			foreach ($qa_content['script_var'] as $var => $value)
Scott committed
319
				$script[] = 'var '.$var.' = '.qa_js($value).';';
Scott committed
320 321 322

		if (isset($qa_content['script_lines']))
			foreach ($qa_content['script_lines'] as $scriptlines) {
Scott committed
323 324
				$script[] = '';
				$script = array_merge($script, $scriptlines);
Scott committed
325 326 327
			}

		if (isset($qa_content['focusid']))
Scott committed
328 329
			$qa_content['script_onloads'][] = array(
				"var elem = document.getElementById(".qa_js($qa_content['focusid']).");",
Scott committed
330 331 332 333 334 335 336 337 338
				"if (elem) {",
				"\telem.select();",
				"\telem.focus();",
				"}",
			);

		if (isset($qa_content['script_onloads'])) {
			array_push($script,
				'',
Scott committed
339 340 341
				'var qa_oldonload = window.onload;',
				'window.onload = function() {',
				"\tif (typeof qa_oldonload == 'function')",
Scott committed
342 343 344 345
				"\t\tqa_oldonload();"
			);

			foreach ($qa_content['script_onloads'] as $scriptonload) {
Scott committed
346
				$script[] = "\t";
Scott committed
347 348

				foreach ((array)$scriptonload as $scriptline)
Scott committed
349
					$script[] = "\t".$scriptline;
Scott committed
350 351
			}

Scott committed
352
			$script[] = '};';
Scott committed
353 354
		}

Scott committed
355
		$script[] = '</script>';
Scott committed
356 357

		if (isset($qa_content['script_rel'])) {
Scott committed
358
			$uniquerel = array_unique($qa_content['script_rel']); // remove any duplicates
Scott committed
359
			foreach ($uniquerel as $script_rel)
Scott committed
360
				$script[] = '<script src="'.qa_html(qa_path_to_root().$script_rel).'"></script>';
Scott committed
361 362 363
		}

		if (isset($qa_content['script_src'])) {
Scott committed
364
			$uniquesrc = array_unique($qa_content['script_src']); // remove any duplicates
Scott committed
365
			foreach ($uniquesrc as $script_src)
Scott committed
366
				$script[] = '<script src="'.qa_html($script_src).'"></script>';
Scott committed
367 368
		}

Scott committed
369
		$qa_content['script'] = $script;
Scott committed
370 371 372

	//	Load the appropriate theme class and output the page

Scott committed
373 374
		$tmpl = substr($qa_template, 0, 7) == 'custom-' ? 'custom' : $qa_template;
		$themeclass = qa_load_theme_class(qa_get_site_theme(), $tmpl, $qa_content, qa_request());
375
		$themeclass->initialize();
Scott committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

		header('Content-type: '.$qa_content['content_type']);

		$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'])) {
Scott committed
391
			require_once QA_INCLUDE_DIR.'db/hotness.php';
Scott committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
			qa_db_hotness_update($qa_content['inc_views_postid'], null, true);
			return true;
		}

		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
*/
	{
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

		return array(
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
			'account' => 'pages/account.php',
			'activity/' => 'pages/activity.php',
			'admin/' => 'pages/admin/admin-default.php',
			'admin/approve' => 'pages/admin/admin-approve.php',
			'admin/categories' => 'pages/admin/admin-categories.php',
			'admin/flagged' => 'pages/admin/admin-flagged.php',
			'admin/hidden' => 'pages/admin/admin-hidden.php',
			'admin/layoutwidgets' => 'pages/admin/admin-widgets.php',
			'admin/moderate' => 'pages/admin/admin-moderate.php',
			'admin/pages' => 'pages/admin/admin-pages.php',
			'admin/plugins' => 'pages/admin/admin-plugins.php',
			'admin/points' => 'pages/admin/admin-points.php',
			'admin/recalc' => 'pages/admin/admin-recalc.php',
			'admin/stats' => 'pages/admin/admin-stats.php',
			'admin/userfields' => 'pages/admin/admin-userfields.php',
			'admin/usertitles' => 'pages/admin/admin-usertitles.php',
			'answers/' => 'pages/answers.php',
			'ask' => 'pages/ask.php',
			'categories/' => 'pages/categories.php',
			'comments/' => 'pages/comments.php',
			'confirm' => 'pages/confirm.php',
			'favorites' => 'pages/favorites.php',
			'favorites/questions' => 'pages/favorites-list.php',
			'favorites/users' => 'pages/favorites-list.php',
			'favorites/tags' => 'pages/favorites-list.php',
			'feedback' => 'pages/feedback.php',
			'forgot' => 'pages/forgot.php',
			'hot/' => 'pages/hot.php',
			'ip/' => 'pages/ip.php',
			'login' => 'pages/login.php',
			'logout' => 'pages/logout.php',
			'messages/' => 'pages/messages.php',
			'message/' => 'pages/message.php',
			'questions/' => 'pages/questions.php',
			'register' => 'pages/register.php',
			'reset' => 'pages/reset.php',
			'search' => 'pages/search.php',
			'tag/' => 'pages/tag.php',
			'tags' => 'pages/tags.php',
			'unanswered/' => 'pages/unanswered.php',
			'unsubscribe' => 'pages/unsubscribe.php',
			'updates' => 'pages/updates.php',
			'user/' => 'pages/user.php',
			'users' => 'pages/users.php',
			'users/blocked' => 'pages/users-blocked.php',
			'users/special' => 'pages/users-special.php',
Scott committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
		);
	}


	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;
	}


	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)
*/
	{
		if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

		global $qa_template, $qa_page_error_html;

		if (QA_DEBUG_PERFORMANCE) {
			global $qa_usage;
			$qa_usage->mark('control');
		}

		$request=qa_request();
		$requestlower=qa_request();
		$navpages=qa_db_get_pending_result('navpages');
		$widgets=qa_db_get_pending_result('widgets');

		if (isset($categoryids) && !is_array($categoryids)) // accept old-style parameter
			$categoryids=array($categoryids);

		$lastcategoryid=count($categoryids) ? end($categoryids) : null;
		$charset = 'utf-8';

		$qa_content=array(
			'content_type' => 'text/html; charset='.$charset,
			'charset' => $charset,

501 502
			'direction' => qa_opt('site_text_direction'),

Scott committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
			'site_title' => qa_html(qa_opt('site_title')),

			'head_lines' => array(),

			'navigation' => array(
				'user' => array(),

				'main' => array(),

				'footer' => array(
					'feedback' => array(
						'url' => qa_path_html('feedback'),
						'label' => qa_lang_html('main/nav_feedback'),
					),
				),

			),

			'sidebar' => qa_opt('show_custom_sidebar') ? qa_opt('custom_sidebar') : null,

			'sidepanel' => qa_opt('show_custom_sidepanel') ? qa_opt('custom_sidepanel') : null,

			'widgets' => array(),
		);

		if (qa_opt('show_custom_in_head'))
			$qa_content['head_lines'][]=qa_opt('custom_in_head');

		if (qa_opt('show_custom_header'))
			$qa_content['body_header']=qa_opt('custom_header');

		if (qa_opt('show_custom_footer'))
			$qa_content['body_footer']=qa_opt('custom_footer');

		if (isset($categoryids))
			$qa_content['categoryids']=$categoryids;

		foreach ($navpages as $page)
			if ($page['nav']=='B')
				qa_navigation_add_page($qa_content['navigation']['main'], $page);

		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'),
			);

		$hascustomhome=qa_has_custom_home();

		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'),
			);

		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'),
			);

		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'),
586
				'selected_on' => array('tags$', 'tag/'),
Scott committed
587 588 589 590 591 592
			);

		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'),
593
				'selected_on' => array('categories$', 'categories/'),
Scott committed
594 595 596 597 598 599
			);

		if (qa_opt('nav_users'))
			$qa_content['navigation']['main']['user']=array(
				'url' => qa_path_html('users'),
				'label' => qa_lang_html('main/nav_users'),
600
				'selected_on' => array('users$', 'users/', 'user/'),
Scott committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
			);

		// 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'))
			$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'),
			);


		if (
			(qa_get_logged_in_level()>=QA_USER_LEVEL_ADMIN) ||
			(!qa_user_maximum_permit_error('permit_moderate')) ||
			(!qa_user_maximum_permit_error('permit_hide_show')) ||
			(!qa_user_maximum_permit_error('permit_delete_hidden'))
		)
			$qa_content['navigation']['main']['admin']=array(
				'url' => qa_path_html('admin'),
				'label' => qa_lang_html('main/nav_admin'),
621
				'selected_on' => array('admin/'),
Scott committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
			);


		$qa_content['search']=array(
			'form_tags' => 'method="get" action="'.qa_path_html('search').'"',
			'form_extra' => qa_path_form_html('search'),
			'title' => qa_lang_html('main/search_title'),
			'field_tags' => 'name="q"',
			'button_label' => qa_lang_html('main/search_button'),
		);

		if (!qa_opt('feedback_enabled'))
			unset($qa_content['navigation']['footer']['feedback']);

		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);

		$regioncodes=array(
			'F' => 'full',
			'M' => 'main',
			'S' => 'side',
		);

		$placecodes=array(
			'T' => 'top',
			'H' => 'high',
			'L' => 'low',
			'B' => 'bottom',
		);

		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)];

				if (isset($region) && isset($place)) { // check region/place codes recognized
					$module=qa_load_module('widget', $widget['title']);

					if (
						isset($module) &&
						method_exists($module, 'allow_template') &&
						$module->allow_template((substr($qa_template, 0, 7)=='custom-') ? 'custom' : $qa_template) &&
						method_exists($module, 'allow_region') &&
						$module->allow_region($region) &&
						method_exists($module, 'output_widget')
					)
						$qa_content['widgets'][$region][$place][]=$module; // if module loaded and happy to be displayed here, tell theme about it
				}
			}

		$logoshow=qa_opt('logo_show');
		$logourl=qa_opt('logo_url');
		$logowidth=qa_opt('logo_width');
		$logoheight=qa_opt('logo_height');

		if ($logoshow)
			$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>';
		else
			$qa_content['logo']='<a href="'.qa_path_html('').'" class="qa-logo-link">'.qa_html(qa_opt('site_title')).'</a>';

		$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, ''));

		$qa_content['navigation']['user']=array();

		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)
			);

			$qa_content['navigation']['user']['updates']=array(
				'url' => qa_path_html('updates'),
				'label' => qa_lang_html('main/nav_updates'),
			);

			if (!empty($userlinks['logout']))
				$qa_content['navigation']['user']['logout']=array(
					'url' => qa_html(@$userlinks['logout']),
					'label' => qa_lang_html('main/nav_logout'),
				);

			if (!QA_FINAL_EXTERNAL_USERS) {
				$source=qa_get_logged_in_source();

				if (strlen($source)) {
					$loginmodules=qa_load_modules_with('login', 'match_source');

					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());
						}
				}
			}

			$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);

		} else {
Scott committed
729
			require_once QA_INCLUDE_DIR.'util/string.php';
Scott committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768

			if (!QA_FINAL_EXTERNAL_USERS) {
				$loginmodules=qa_load_modules_with('login', 'login_html');

				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();

					if (strlen($label))
						$qa_content['navigation']['user'][implode('-', qa_string_to_words($tryname))]=array('label' => $label);
				}
			}

			if (!empty($userlinks['login']))
				$qa_content['navigation']['user']['login']=array(
					'url' => qa_html(@$userlinks['login']),
					'label' => qa_lang_html('main/nav_login'),
				);

			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 committed
769
		$qa_content['script_rel']=array('qa-content/jquery-1.11.3.min.js');
Scott committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
		$qa_content['script_rel'][]='qa-content/qa-page.js?'.QA_VERSION;

		if ($voting)
			$qa_content['error']=@$qa_page_error_html;

		$qa_content['script_var']=array(
			'qa_root' => qa_path_to_root(),
			'qa_request' => $request,
		);

		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);
	}


	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;
	}


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

	global $qa_usage;

	qa_report_process_stage('init_page');
	qa_db_connect('qa_page_db_fail_handler');

	qa_page_queue_pending();
	qa_load_state();
	qa_check_login_modules();

	if (QA_DEBUG_PERFORMANCE)
		$qa_usage->mark('setup');

	qa_check_page_clicks();

	$qa_content = qa_get_request_content();

	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');

		if (qa_do_content_stats($qa_content) && 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
Gideon Greenspan committed
842
*/