page.php 25.1 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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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
*/

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

Scott committed
27
require_once QA_INCLUDE_DIR . 'app/routing.php';
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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';
require_once QA_INCLUDE_DIR . 'db/selects.php';


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

	qa_preload_options();
	$loginuserid = qa_get_logged_in_userid();
44
	$dbSelect = qa_service('dbselect');
45 46 47

	if (isset($loginuserid)) {
		if (!QA_FINAL_EXTERNAL_USERS)
48
			$dbSelect->queuePending('loggedinuser', qa_db_user_account_selectspec($loginuserid, true));
49

50 51 52 53
		$dbSelect->queuePending('notices', qa_db_user_notices_selectspec($loginuserid));
		$dbSelect->queuePending('favoritenonqs', qa_db_user_favorite_non_qs_selectspec($loginuserid));
		$dbSelect->queuePending('userlimits', qa_db_user_limits_selectspec($loginuserid));
		$dbSelect->queuePending('userlevels', qa_db_user_levels_selectspec($loginuserid, true));
Scott committed
54 55
	}

56 57 58
	$dbSelect->queuePending('iplimits', qa_db_ip_limits_selectspec(qa_remote_ip_address()));
	$dbSelect->queuePending('navpages', qa_db_pages_selectspec(array('B', 'M', 'O', 'F')));
	$dbSelect->queuePending('widgets', qa_db_widgets_selectspec());
59
}
Scott committed
60 61


62 63 64 65 66 67
/**
 * Check the page state parameter and then remove it from the $_GET array
 */
function qa_load_state()
{
	global $qa_state;
Scott committed
68

69 70 71
	$qa_state = qa_get('state');
	unset($_GET['state']); // to prevent being passed through on forms
}
Scott committed
72 73


74 75 76 77 78 79 80
/**
 * If no user is logged in, call through to the login modules to see if they want to log someone in
 */
function qa_check_login_modules()
{
	if (!QA_FINAL_EXTERNAL_USERS && !qa_is_logged_in()) {
		$loginmodules = qa_load_modules_with('login', 'check_login');
Scott committed
81

82 83 84 85
		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 committed
86 87
		}
	}
88
}
Scott committed
89 90


91 92 93 94 95 96 97
/**
 * 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.
 */
function qa_check_page_clicks()
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott committed
98

99
	global $qa_page_error_html;
Scott committed
100

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

106 107 108
				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');
Scott committed
109

110 111 112
					else {
						require_once QA_INCLUDE_DIR . 'app/votes.php';
						require_once QA_INCLUDE_DIR . 'db/selects.php';
Scott committed
113

114
						$userid = qa_get_logged_in_userid();
Scott committed
115

116 117
						$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 committed
118

119 120 121
						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);
Scott committed
122
						}
123
						break;
Scott committed
124
					}
125
				}
Scott committed
126

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

130 131 132
				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');
Scott committed
133

134 135
					else {
						require_once QA_INCLUDE_DIR . 'app/favorites.php';
Scott committed
136

137 138
						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);
Scott committed
139
					}
140
				}
Scott committed
141

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

145 146 147
				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');
Scott committed
148

149 150 151
					else {
						if ($noticeid == 'visitor')
							setcookie('qa_noticed', 1, time() + 86400 * 3650, '/', QA_COOKIE_DOMAIN, (bool)ini_get('session.cookie_secure'), true);
Scott committed
152

153 154 155
						elseif ($noticeid == 'welcome') {
							require_once QA_INCLUDE_DIR . 'db/users.php';
							qa_db_user_set_flag(qa_get_logged_in_userid(), QA_USER_FLAGS_WELCOME_NOTICE, false);
Scott committed
156

157 158 159
						} else {
							require_once QA_INCLUDE_DIR . 'db/notices.php';
							qa_db_usernotice_delete(qa_get_logged_in_userid(), $noticeid);
Scott committed
160
						}
161 162

						qa_redirect(qa_request(), $_GET);
Scott committed
163 164 165
					}
				}
			}
166
		}
Scott committed
167
	}
168
}
Scott committed
169 170


171
/**
172
 *	Run the appropriate /qa-include/pages/*.php file for this request and return back the $qa_content it passed
173 174 175 176
 */
function qa_get_request_content()
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott committed
177

178 179 180
	$requestlower = strtolower(qa_request());
	$requestparts = qa_request_parts();
	$firstlower = strtolower($requestparts[0]);
181
	$qa_content = [];
Scott committed
182 183 184 185 186
	// old router
	$routing = qa_page_routing();
	// new router
	$router = qa_service('router');
	qa_controller_routing($router);
187

188
	try {
189
		// use new Controller system
Scott committed
190
		$route = $router->match($requestlower);
191 192 193
		if ($route !== null) {
			qa_set_template($route->getOption('template'));
			$controllerClass = $route->getController();
194
			$ctrl = new $controllerClass(qa_service('database'));
195

196 197
			$qa_content = $ctrl->executeAction($route->getAction(), $route->getParameters());
		}
198 199 200
	} catch (\Exception $e) {
		$qa_content = (new \Q2A\Exceptions\ExceptionHandler)->handle($e);
	}
Scott committed
201

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

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

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

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

221 222 223 224
	if ($firstlower == 'admin') {
		$_COOKIE['qa_admin_last'] = $requestlower; // for navigation tab now...
		setcookie('qa_admin_last', $_COOKIE['qa_admin_last'], 0, '/', QA_COOKIE_DOMAIN, (bool)ini_get('session.cookie_secure'), true); // ...and in future
	}
Scott committed
225

226 227
	if (isset($qa_content))
		qa_set_form_security_key();
Scott committed
228

229 230
	return $qa_content;
}
Scott committed
231 232


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

242
	global $qa_template;
Scott committed
243

244
	$requestlower = strtolower(qa_request());
Scott committed
245

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

248 249 250 251
	foreach ($qa_content['navigation'] as $navtype => $navigation) {
		if (!is_array($navigation) || $navtype == 'cat') {
			continue;
		}
Scott committed
252

253 254 255 256 257 258 259
		foreach ($navigation as $navprefix => $navlink) {
			$selected =& $qa_content['navigation'][$navtype][$navprefix]['selected'];
			if (isset($navlink['selected_on'])) {
				// match specified paths
				foreach ($navlink['selected_on'] as $path) {
					if (strpos($requestlower . '$', $path) === 0)
						$selected = true;
Scott committed
260
				}
261 262 263
			} elseif ($requestlower === $navprefix || $requestlower . '$' === $navprefix) {
				// exact match for array key
				$selected = true;
Scott committed
264 265
			}
		}
266
	}
Scott committed
267

268
	// Slide down notifications
Scott committed
269

270
	if (!empty($qa_content['notices'])) {
271 272 273 274
		foreach ($qa_content['notices'] as $notice) {
			$qa_content['script_onloads'][] = array(
				"qa_reveal(document.getElementById(" . qa_js($notice['id']) . "), 'notice');",
			);
Scott committed
275
		}
276
	}
Scott committed
277

278
	// Handle maintenance mode
Scott committed
279

280 281
	if (qa_opt('site_maintenance') && ($requestlower != 'login')) {
		if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) {
282
			if (!isset($qa_content['error'])) {
283 284
				$qa_content['error'] = strtr(qa_lang_html('admin/maintenance_admin_only'), array(
					'^1' => '<a href="' . qa_path_html('admin/general') . '">',
Scott committed
285 286
					'^2' => '</a>',
				));
287
			}
288 289 290
		} else {
			$qa_content = qa_content_prepare();
			$qa_content['error'] = qa_lang_html('misc/site_in_maintenance');
Scott committed
291
		}
292
	}
Scott committed
293

294 295 296 297 298 299 300 301 302 303 304 305 306
	// 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>',
			));
Scott committed
307
		}
308 309

		// we no longer block access here for unapproved users; this is handled by the Permissions settings
310
	}
Scott committed
311

312
	// Combine various Javascript elements in $qa_content into single array for theme layer
Scott committed
313

314
	$script = array('<script>');
Scott committed
315

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

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

329
	$script[] = '</script>';
Scott committed
330

331 332
	if (isset($qa_content['script_rel'])) {
		$uniquerel = array_unique($qa_content['script_rel']); // remove any duplicates
Scott committed
333
		foreach ($uniquerel as $script_rel) {
334
			$script[] = '<script src="' . qa_html(qa_path_to_root() . $script_rel) . '"></script>';
Scott committed
335
		}
336
	}
Scott committed
337

338 339
	if (isset($qa_content['script_src'])) {
		$uniquesrc = array_unique($qa_content['script_src']); // remove any duplicates
Scott committed
340
		foreach ($uniquesrc as $script_src) {
341
			$script[] = '<script src="' . qa_html($script_src) . '"></script>';
Scott committed
342
		}
343
	}
Scott committed
344

Scott committed
345 346
	// JS onloads must come after jQuery is loaded

347 348 349 350
	if (isset($qa_content['focusid'])) {
		$qa_content['script_onloads'][] = array(
			'$(' . qa_js('#' . $qa_content['focusid']) . ').focus();',
		);
Scott committed
351 352
	}

353
	if (isset($qa_content['script_onloads'])) {
Scott committed
354
		$script[] = '<script>';
Scott committed
355
		$script[] = '$(window).on(\'load\', function() {';
356 357

		foreach ($qa_content['script_onloads'] as $scriptonload) {
Scott committed
358
			foreach ((array)$scriptonload as $scriptline) {
359
				$script[] = "\t" . $scriptline;
Scott committed
360
			}
Scott committed
361 362
		}

363
		$script[] = '});';
Scott committed
364
		$script[] = '</script>';
Scott committed
365 366
	}

367 368 369 370
	if (!isset($qa_content['script'])) {
		$qa_content['script'] = array();
	}

371
	$qa_content['script'] = array_merge($script, $qa_content['script']);
Scott committed
372

373
	// Load the appropriate theme class and output the page
Scott committed
374

375 376 377
	$tmpl = substr($qa_template, 0, 7) == 'custom-' ? 'custom' : $qa_template;
	$themeclass = qa_load_theme_class(qa_get_site_theme(), $tmpl, $qa_content, qa_request());
	$themeclass->initialize();
Scott committed
378

379
	header('Content-type: ' . $qa_content['content_type']);
Scott committed
380

381 382 383 384
	$themeclass->doctype();
	$themeclass->html();
	$themeclass->finish();
}
Scott committed
385 386


387 388
/**
 * Update any statistics required by the fields in $qa_content, and return true if something was done
389
 * @param array $qa_content
Scott committed
390
 * @return bool
391 392 393
 */
function qa_do_content_stats($qa_content)
{
Scott committed
394 395 396
	if (!isset($qa_content['inc_views_postid'])) {
		return false;
	}
397

Scott committed
398
	require_once QA_INCLUDE_DIR . 'db/hotness.php';
399

400 401 402
	$viewsIncremented = qa_db_increment_views($qa_content['inc_views_postid']);

	if ($viewsIncremented && qa_opt('recalc_hotness_q_view')) {
Scott committed
403
		qa_db_hotness_update($qa_content['inc_views_postid']);
404
	}
Scott committed
405

Scott committed
406
	return true;
407 408 409 410 411 412 413
}


// Other functions which might be called from anywhere

/**
 * Sets the template which should be passed to the theme class, telling it which type of page it's displaying
414
 * @param string $template
415 416 417 418 419 420 421 422 423 424 425
 */
function qa_set_template($template)
{
	global $qa_template;
	$qa_template = $template;
}


/**
 * 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)
Scott committed
426 427 428
 * @param bool $voting
 * @param array $categoryids
 * @return array
429
 */
Scott committed
430
function qa_content_prepare($voting = false, $categoryids = array())
431 432 433 434 435 436 437 438 439
{
	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');
	}
Scott committed
440

441 442
	$request = qa_request();
	$requestlower = qa_request();
443 444 445
	$dbSelect = qa_service('dbselect');
	$navpages = $dbSelect->getPendingResult('navpages');
	$widgets = $dbSelect->getPendingResult('widgets');
Scott committed
446

Scott committed
447 448
	if (!is_array($categoryids)) {
		// accept old-style parameter
449
		$categoryids = array($categoryids);
Scott committed
450
	}
Scott committed
451

Scott committed
452
	$lastcategoryid = count($categoryids) > 0 ? end($categoryids) : null;
453 454 455
	$charset = 'utf-8';
	$language = qa_opt('site_language');
	$language = empty($language) ? 'en' : qa_html($language);
Scott committed
456

457 458 459
	$qa_content = array(
		'content_type' => 'text/html; charset=' . $charset,
		'charset' => $charset,
Scott committed
460

461
		'language' => $language,
Scott committed
462

463
		'direction' => qa_opt('site_text_direction'),
Scott committed
464

465 466 467
		'options' => array(
			'minify_html' => qa_opt('minify_html'),
		),
Scott committed
468

469
		'site_title' => qa_html(qa_opt('site_title')),
Scott committed
470

471
		'html_tags' => 'lang="' . $language . '"',
Scott committed
472

473
		'head_lines' => array(),
Scott committed
474

475 476
		'navigation' => array(
			'user' => array(),
Scott committed
477

478
			'main' => array(),
Scott committed
479

480 481 482 483
			'footer' => array(
				'feedback' => array(
					'url' => qa_path_html('feedback'),
					'label' => qa_lang_html('main/nav_feedback'),
Scott committed
484 485 486
				),
			),

487
		),
Scott committed
488

489 490 491 492
		'sidebar' => qa_opt('show_custom_sidebar') ? qa_opt('custom_sidebar') : null,
		'sidepanel' => qa_opt('show_custom_sidepanel') ? qa_opt('custom_sidepanel') : null,
		'widgets' => array(),
	);
Scott committed
493

494 495 496 497
	// add meta description if we're on the home page
	if ($request === '' || $request === array_search('', qa_get_request_map())) {
		$qa_content['description'] = qa_html(qa_opt('home_description'));
	}
Scott committed
498

499 500
	if (qa_opt('show_custom_in_head'))
		$qa_content['head_lines'][] = qa_opt('custom_in_head');
Scott committed
501

502 503
	if (qa_opt('show_custom_header'))
		$qa_content['body_header'] = qa_opt('custom_header');
Scott committed
504

505 506
	if (qa_opt('show_custom_footer'))
		$qa_content['body_footer'] = qa_opt('custom_footer');
Scott committed
507

508 509
	if (isset($categoryids))
		$qa_content['categoryids'] = $categoryids;
Scott committed
510

511 512 513 514
	foreach ($navpages as $page) {
		if ($page['nav'] == 'B')
			qa_navigation_add_page($qa_content['navigation']['main'], $page);
	}
Scott committed
515

516 517 518 519 520 521
	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'),
		);
	}
Scott committed
522

523 524 525 526 527 528
	if (qa_opt('nav_activity')) {
		$qa_content['navigation']['main']['activity'] = array(
			'url' => qa_path_html('activity'),
			'label' => qa_lang_html('main/nav_activity'),
		);
	}
Scott committed
529

530
	$hascustomhome = qa_has_custom_home();
Scott committed
531

532 533 534 535 536 537
	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 committed
538

539 540 541 542 543 544
	if (qa_opt('nav_questions')) {
		$qa_content['navigation']['main']['questions'] = array(
			'url' => qa_path_html('questions'),
			'label' => qa_lang_html('main/nav_qs'),
		);
	}
Scott committed
545

Félicie committed
546 547 548 549 550 551
	// if (qa_opt('nav_hot')) {
	// 	$qa_content['navigation']['main']['hot'] = array(
	// 		'url' => qa_path_html('hot'),
	// 		'label' => qa_lang_html('main/nav_hot'),
	// 	);
	// }
Scott committed
552

553 554 555 556 557 558
	// if (qa_opt('nav_unanswered')) {
	// 	$qa_content['navigation']['main']['unanswered'] = array(
	// 		'url' => qa_path_html('unanswered'),
	// 		'label' => qa_lang_html('main/nav_unanswered'),
	// 	);
	// }
Scott committed
559

560 561 562 563 564 565 566
	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'),
			'selected_on' => array('tags$', 'tag/'),
		);
	}
Scott committed
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'),
			'selected_on' => array('categories$', 'categories/'),
		);
	}
Scott committed
575

Félicie committed
576

Scott committed
577

578
	// Only the 'level' permission error prevents the menu option being shown - others reported on /qa-include/pages/ask.php
Scott committed
579

580 581 582 583
	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'),
Scott committed
584
		);
585
	}
Scott committed
586

587
	if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN || !qa_user_maximum_permit_error('permit_moderate') ||
Félicie committed
588
	!qa_user_maximum_permit_error('permit_hide_show') || !qa_user_maximum_permit_error('permit_delete_hidden')
589
	) {
Félicie committed
590 591 592 593 594
		$qa_content['navigation']['main']['user'] = array(
			'url' => qa_path_html('users'),
			'label' => qa_lang_html('main/nav_users'),
			'selected_on' => array('users$', 'users/', 'user/'),
		);
595 596 597 598
		$qa_content['navigation']['main']['admin'] = array(
			'url' => qa_path_html('admin'),
			'label' => qa_lang_html('main/nav_admin'),
			'selected_on' => array('admin/'),
Scott committed
599
		);
600
	}
Scott committed
601

Félicie committed
602 603


604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	$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') {
			$loc = ($page['nav'] == 'F') ? 'footer' : 'main';
			qa_navigation_add_page($qa_content['navigation'][$loc], $page);
		}
	}
Scott committed
621

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	$regioncodes = array(
		'F' => 'full',
		'M' => 'main',
		'S' => 'side',
	);

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

	foreach ($widgets as $widget) {
		$tagstring = ',' . $widget['tags'] . ',';
637 638 639 640 641
		$showOnTmpl = strpos($tagstring, ",$qa_template,") !== false || strpos($tagstring, ',all,') !== false;
		// special case for user pages
		$showOnUser = strpos($tagstring, ',user,') !== false && preg_match('/^user(-.+)?$/', $qa_template) === 1;

		if ($showOnTmpl || $showOnUser) {
642 643 644 645 646 647 648
			// widget 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)) {
				// region/place codes recognized
				$module = qa_load_module('widget', $widget['title']);
649
				$allowTmpl = (substr($qa_template, 0, 7) == 'custom-') ? 'custom' : $qa_template;
650

651 652 653
				if (isset($module) &&
					method_exists($module, 'allow_template') && $module->allow_template($allowTmpl) &&
					method_exists($module, 'allow_region') && $module->allow_region($region) &&
654 655 656 657
					method_exists($module, 'output_widget')
				) {
					// if module loaded and happy to be displayed here, tell theme about it
					$qa_content['widgets'][$region][$place][] = $module;
Scott committed
658 659 660
				}
			}
		}
661
	}
Scott committed
662

663 664 665 666 667 668 669 670 671
	$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 . '"') : '') .
Scott committed
672
			' alt="' . qa_html(qa_opt('site_title')) . '"/></a>';
673 674 675
	} else {
		$qa_content['logo'] = '<a href="' . qa_path_html('') . '" class="qa-logo-link">' . qa_html(qa_opt('site_title')) . '</a>';
	}
Scott committed
676

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

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

681
	$qa_content['navigation']['user'] = array();
Scott committed
682

683 684 685 686 687
	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 committed
688

689 690 691 692
		$qa_content['navigation']['user']['updates'] = array(
			'url' => qa_path_html('updates'),
			'label' => qa_lang_html('main/nav_updates'),
		);
Scott committed
693

694
		if (!empty($userlinks['logout'])) {
695 696 697
			$qa_content['navigation']['user']['logout'] = array(
				'url' => qa_html(@$userlinks['logout']),
				'label' => qa_lang_html('main/nav_logout'),
Scott committed
698
			);
699
		}
Scott committed
700

701 702
		if (!QA_FINAL_EXTERNAL_USERS) {
			$source = qa_get_logged_in_source();
Scott committed
703

704 705
			if (strlen($source)) {
				$loginmodules = qa_load_modules_with('login', 'match_source');
Scott committed
706

707
				foreach ($loginmodules as $module) {
708 709 710 711 712
					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());
					}
713
				}
Scott committed
714
			}
715
		}
Scott committed
716

717
		$notices = $dbSelect->getPendingResult('notices');
718 719
		foreach ($notices as $notice)
			$qa_content['notices'][] = qa_notice_form($notice['noticeid'], qa_viewer_html($notice['content'], $notice['format']), $notice);
Scott committed
720

721 722
	} else {
		require_once QA_INCLUDE_DIR . 'util/string.php';
Scott committed
723

724 725
		if (!QA_FINAL_EXTERNAL_USERS) {
			$loginmodules = qa_load_modules_with('login', 'login_html');
Scott committed
726

727 728 729 730
			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 committed
731

732 733
				if (strlen($label))
					$qa_content['navigation']['user'][implode('-', qa_string_to_words($tryname))] = array('label' => $label);
Scott committed
734
			}
735
		}
Scott committed
736

737 738 739 740 741
		if (!empty($userlinks['login'])) {
			$qa_content['navigation']['user']['login'] = array(
				'url' => qa_html(@$userlinks['login']),
				'label' => qa_lang_html('main/nav_login'),
			);
Scott committed
742 743
		}

744 745 746 747 748 749 750
		if (!empty($userlinks['register'])) {
			$qa_content['navigation']['user']['register'] = array(
				'url' => qa_html(@$userlinks['register']),
				'label' => qa_lang_html('main/nav_register'),
			);
		}
	}
Scott committed
751

752 753 754
	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'));
Scott committed
755

756 757
	} else {
		setcookie('qa_noticed', 1, time() + 86400 * 3650, '/', QA_COOKIE_DOMAIN, (bool)ini_get('session.cookie_secure'), true); // don't show first-time notice if a user has logged in
Scott committed
758

Scott committed
759 760
		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
761
				$qa_content['notices'][] = qa_notice_form('welcome', qa_opt('notice_welcome'));
Scott committed
762
		}
763
	}
Scott committed
764

Scott committed
765
	$qa_content['script_rel'] = array('qa-content/jquery-3.5.1.min.js');
766
	$qa_content['script_rel'][] = 'qa-content/qa-global.js?' . QA_VERSION;
Scott committed
767

768 769
	if ($voting)
		$qa_content['error'] = @$qa_page_error_html;
Scott committed
770

771 772 773 774
	$qa_content['script_var'] = array(
		'qa_root' => qa_path_to_root(),
		'qa_request' => $request,
	);
Scott committed
775

776 777
	return $qa_content;
}
Scott committed
778 779


780 781
/**
 * Get the start parameter which should be used, as constrained by the setting in qa-config.php
Scott committed
782
 * @return int
783 784 785 786 787
 */
function qa_get_start()
{
	return min(max(0, (int)qa_get('start')), QA_MAX_LIMIT_START);
}
Scott committed
788

789 790 791

/**
 * Get the state parameter which should be used, as set earlier in qa_load_state()
Scott committed
792
 * @return string
793 794 795 796 797 798
 */
function qa_get_state()
{
	global $qa_state;
	return $qa_state;
}
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821


/**
 * Generate a canonical URL for the current request. Preserves certain GET parameters.
 * @return string The full canonical URL.
 */
function qa_get_canonical()
{
	$params = array();

	// variable assignment intentional here
	if (($start = qa_get_start()) > 0) {
		$params['start'] = $start;
	}
	if ($sort = qa_get('sort')) {
		$params['sort'] = $sort;
	}
	if ($by = qa_get('by')) {
		$params['by'] = $by;
	}

	return qa_path_html(qa_request(), $params, qa_opt('site_url'));
}