qa-index.php 5.93 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-index.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: The Grand Central of Q2A - most requests come through here


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

//	Try our best to set base path here just in case it wasn't set in index.php (pre version 1.0.1)

	if (!defined('QA_BASE_DIR'))
		define('QA_BASE_DIR', dirname(empty($_SERVER['SCRIPT_FILENAME']) ? dirname(__FILE__) : $_SERVER['SCRIPT_FILENAME']).'/');


//	If this is an special non-page request, branch off here

	if (@$_POST['qa']=='ajax')
		require 'qa-ajax.php';

	elseif (@$_GET['qa']=='image')
		require 'qa-image.php';

	elseif (@$_GET['qa']=='blob')
		require 'qa-blob.php';

	else {
Scott Vivian committed
45

Gideon Greenspan committed
46
	//	Otherwise, load the Q2A base file which sets up a bunch of crucial stuff
Scott Vivian committed
47

Gideon Greenspan committed
48
		require 'qa-base.php';
Scott Vivian committed
49 50


Gideon Greenspan committed
51
	//	Determine the request and root of the installation, and the requested start position used by many pages
Scott Vivian committed
52

Gideon Greenspan committed
53 54
		function qa_index_set_request()
		{
Gideon Greenspan committed
55
			if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
Scott Vivian committed
56

Gideon Greenspan committed
57
			$relativedepth=0;
Scott Vivian committed
58

Gideon Greenspan committed
59 60 61 62
			if (isset($_GET['qa-rewrite'])) { // URLs rewritten by .htaccess
				$urlformat=QA_URL_FORMAT_NEAT;
				$requestparts=explode('/', qa_gpc_to_string($_GET['qa-rewrite']));
				unset($_GET['qa-rewrite']);
Scott Vivian committed
63

Gideon Greenspan committed
64
				if (!empty($_SERVER['REQUEST_URI'])) { // workaround for the fact that Apache unescapes characters while rewriting
Gideon Greenspan committed
65 66
					$origpath=$_SERVER['REQUEST_URI'];
					$_GET=array();
Scott Vivian committed
67

Gideon Greenspan committed
68 69 70
					$questionpos=strpos($origpath, '?');
					if (is_numeric($questionpos)) {
						$params=explode('&', substr($origpath, $questionpos+1));
Scott Vivian committed
71

Gideon Greenspan committed
72
						foreach ($params as $param)
Gideon Greenspan committed
73 74 75 76
							if (preg_match('/^([^\=]*)(\=(.*))?$/', $param, $matches)) {
								$argument=strtr(urldecode($matches[1]), '.', '_'); // simulate PHP's $_GET behavior
								$_GET[$argument]=qa_string_to_gpc(urldecode(@$matches[3]));
							}
Scott Vivian committed
77

Gideon Greenspan committed
78 79
						$origpath=substr($origpath, 0, $questionpos);
					}
Scott Vivian committed
80

Gideon Greenspan committed
81 82 83 84 85 86
					// Generally we assume that $_GET['qa-rewrite'] has the right path depth, but this won't be the case if there's
					// a & or # somewhere in the middle of the path, due to apache unescaping. So we make a special case for that.
					$keepparts=count($requestparts);
					$requestparts=explode('/', urldecode($origpath)); // new request calculated from $_SERVER['REQUEST_URI']

					for ($part=count($requestparts)-1; $part>=0; $part--)
Scott Vivian committed
87
						if (is_numeric(strpos($requestparts[$part], '&')) || is_numeric(strpos($requestparts[$part], '#'))) {
Gideon Greenspan committed
88 89 90
							$keepparts+=count($requestparts)-$part-1; // this is how many parts we lost
							break;
						}
Scott Vivian committed
91

Gideon Greenspan committed
92
					$requestparts=array_slice($requestparts, -$keepparts); // remove any irrelevant parts from the beginning
Gideon Greenspan committed
93
				}
Gideon Greenspan committed
94 95

				$relativedepth=count($requestparts);
Scott Vivian committed
96

Gideon Greenspan committed
97 98 99 100 101
			} elseif (isset($_GET['qa'])) {
				if (strpos($_GET['qa'], '/')===false) {
					$urlformat=( (empty($_SERVER['REQUEST_URI'])) || (strpos($_SERVER['REQUEST_URI'], '/index.php')!==false) )
						? QA_URL_FORMAT_SAFEST : QA_URL_FORMAT_PARAMS;
					$requestparts=array(qa_gpc_to_string($_GET['qa']));
Scott Vivian committed
102

Gideon Greenspan committed
103 104 105 106 107
					for ($part=1; $part<10; $part++)
						if (isset($_GET['qa_'.$part])) {
							$requestparts[]=qa_gpc_to_string($_GET['qa_'.$part]);
							unset($_GET['qa_'.$part]);
						}
Scott Vivian committed
108

Gideon Greenspan committed
109 110 111 112
				} else {
					$urlformat=QA_URL_FORMAT_PARAM;
					$requestparts=explode('/', qa_gpc_to_string($_GET['qa']));
				}
Scott Vivian committed
113

Gideon Greenspan committed
114
				unset($_GET['qa']);
Scott Vivian committed
115

Gideon Greenspan committed
116 117 118 119
			} else {
				$phpselfunescaped=strtr($_SERVER['PHP_SELF'], '+', ' '); // seems necessary, and plus does not work with this scheme
				$indexpath='/index.php/';
				$indexpos=strpos($phpselfunescaped, $indexpath);
Scott Vivian committed
120

Gideon Greenspan committed
121 122 123 124
				if (is_numeric($indexpos)) {
					$urlformat=QA_URL_FORMAT_INDEX;
					$requestparts=explode('/', substr($phpselfunescaped, $indexpos+strlen($indexpath)));
					$relativedepth=1+count($requestparts);
Scott Vivian committed
125

Gideon Greenspan committed
126 127 128 129 130
				} else {
					$urlformat=null; // at home page so can't identify path type
					$requestparts=array();
				}
			}
Scott Vivian committed
131

Gideon Greenspan committed
132 133 134
			foreach ($requestparts as $part => $requestpart) // remove any blank parts
				if (!strlen($requestpart))
					unset($requestparts[$part]);
Scott Vivian committed
135

Gideon Greenspan committed
136 137
			reset($requestparts);
			$key=key($requestparts);
Scott Vivian committed
138

Gideon Greenspan committed
139 140 141
			$replacement=array_search(@$requestparts[$key], qa_get_request_map());
			if ($replacement!==false)
				$requestparts[$key]=$replacement;
Scott Vivian committed
142

Gideon Greenspan committed
143 144 145 146 147 148
			qa_set_request(
				implode('/', $requestparts),
				($relativedepth>1) ? str_repeat('../', $relativedepth-1) : './',
				$urlformat
			);
		}
Scott Vivian committed
149

Gideon Greenspan committed
150
		qa_index_set_request();
Scott Vivian committed
151 152


Gideon Greenspan committed
153
	//	Branch off to appropriate file for further handling
Scott Vivian committed
154

Gideon Greenspan committed
155
		$requestlower=strtolower(qa_request());
Scott Vivian committed
156

Gideon Greenspan committed
157 158
		if ($requestlower=='install')
			require QA_INCLUDE_DIR.'qa-install.php';
Scott Vivian committed
159

Gideon Greenspan committed
160 161
		elseif ($requestlower==('url/test/'.QA_URL_TEST_STRING))
			require QA_INCLUDE_DIR.'qa-url-test.php';
Scott Vivian committed
162

Gideon Greenspan committed
163
		else {
Scott Vivian committed
164

Gideon Greenspan committed
165
		//	Enable gzip compression for output (needs to come early)
Scott Vivian committed
166

Gideon Greenspan committed
167 168 169 170
			if (QA_HTML_COMPRESSION) // on by default
				if (substr($requestlower, 0, 6)!='admin/') // not for admin pages since some of these contain lengthy processes
					if (extension_loaded('zlib') && !headers_sent())
						ob_start('ob_gzhandler');
Scott Vivian committed
171

Gideon Greenspan committed
172 173 174 175 176 177
			if (substr($requestlower, 0, 5)=='feed/')
				require QA_INCLUDE_DIR.'qa-feed.php';
			else
				require QA_INCLUDE_DIR.'qa-page.php';
		}
	}
Scott Vivian committed
178

Gideon Greenspan committed
179 180 181 182 183 184
	qa_report_process_stage('shutdown');


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