Commit f3810d35 by Scott

Coding style (index)

parent daeb8c4a
...@@ -20,13 +20,8 @@ ...@@ -20,13 +20,8 @@
More about this license: http://www.question2answer.org/license.php More about this license: http://www.question2answer.org/license.php
*/ */
// Set base path here so this works with symbolic links for multiple installations // Set base path here so this works with symbolic links for multiple installations
define('QA_BASE_DIR', dirname(empty($_SERVER['SCRIPT_FILENAME']) ? __FILE__ : $_SERVER['SCRIPT_FILENAME']).'/'); define('QA_BASE_DIR', dirname(empty($_SERVER['SCRIPT_FILENAME']) ? __FILE__ : $_SERVER['SCRIPT_FILENAME']) . '/');
require 'qa-include/qa-index.php'; require 'qa-include/qa-index.php';
/*
Omit PHP closing tag to help avoid accidental output
*/
\ No newline at end of file
...@@ -20,175 +20,176 @@ ...@@ -20,175 +20,176 @@
More about this license: http://www.question2answer.org/license.php 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) // 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')) if (!defined('QA_BASE_DIR')) {
define('QA_BASE_DIR', dirname(empty($_SERVER['SCRIPT_FILENAME']) ? dirname(__FILE__) : $_SERVER['SCRIPT_FILENAME']).'/'); 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 this is an special non-page request, branch off here
if (isset($_POST['qa']) && $_POST['qa'] == 'ajax') if (isset($_POST['qa']) && $_POST['qa'] == 'ajax') {
require 'qa-ajax.php'; require 'qa-ajax.php';
}
elseif (isset($_GET['qa']) && $_GET['qa'] == 'image') elseif (isset($_GET['qa']) && $_GET['qa'] == 'image') {
require 'qa-image.php'; require 'qa-image.php';
}
elseif (isset($_GET['qa']) && $_GET['qa'] == 'blob') elseif (isset($_GET['qa']) && $_GET['qa'] == 'blob') {
require 'qa-blob.php'; require 'qa-blob.php';
}
else { else {
// Otherwise, load the Q2A base file which sets up a bunch of crucial stuff // Otherwise, load the Q2A base file which sets up a bunch of crucial stuff
require 'qa-base.php'; require 'qa-base.php';
/** /**
* Determine the request and root of the installation, and the requested start position used by many pages. * Determine the request and root of the installation, and the requested start position used by many pages.
* *
* Apache and Nginx behave slightly differently: * Apache and Nginx behave slightly differently:
* Apache qa-rewrite unescapes characters, converts `+` to ` `, cuts off at `#` or `&` * Apache qa-rewrite unescapes characters, converts `+` to ` `, cuts off at `#` or `&`
* Nginx qa-rewrite unescapes characters, retains `+`, contains true path * Nginx qa-rewrite unescapes characters, retains `+`, contains true path
*/ */
function qa_index_set_request() function qa_index_set_request()
{ {
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$relativedepth = 0; $relativedepth = 0;
if (isset($_GET['qa-rewrite'])) { // URLs rewritten by .htaccess or Nginx if (isset($_GET['qa-rewrite'])) { // URLs rewritten by .htaccess or Nginx
$urlformat = QA_URL_FORMAT_NEAT; $urlformat = QA_URL_FORMAT_NEAT;
$qa_rewrite = strtr(qa_gpc_to_string($_GET['qa-rewrite']), '+', ' '); // strtr required by Nginx $qa_rewrite = strtr(qa_gpc_to_string($_GET['qa-rewrite']), '+', ' '); // strtr required by Nginx
$requestparts = explode('/', $qa_rewrite); $requestparts = explode('/', $qa_rewrite);
unset($_GET['qa-rewrite']); unset($_GET['qa-rewrite']);
if (!empty($_SERVER['REQUEST_URI'])) { // workaround for the fact that Apache unescapes characters while rewriting if (!empty($_SERVER['REQUEST_URI'])) { // workaround for the fact that Apache unescapes characters while rewriting
$origpath = $_SERVER['REQUEST_URI']; $origpath = $_SERVER['REQUEST_URI'];
$_GET = array(); $_GET = array();
$questionpos = strpos($origpath, '?'); $questionpos = strpos($origpath, '?');
if (is_numeric($questionpos)) { if (is_numeric($questionpos)) {
$params = explode('&', substr($origpath, $questionpos+1)); $params = explode('&', substr($origpath, $questionpos + 1));
foreach ($params as $param) { foreach ($params as $param) {
if (preg_match('/^([^\=]*)(\=(.*))?$/', $param, $matches)) { if (preg_match('/^([^\=]*)(\=(.*))?$/', $param, $matches)) {
$argument = strtr(urldecode($matches[1]), '.', '_'); // simulate PHP's $_GET behavior $argument = strtr(urldecode($matches[1]), '.', '_'); // simulate PHP's $_GET behavior
$_GET[$argument] = qa_string_to_gpc(urldecode(@$matches[3])); $_GET[$argument] = qa_string_to_gpc(urldecode(@$matches[3]));
}
} }
$origpath = substr($origpath, 0, $questionpos);
} }
// Generally we assume that $_GET['qa-rewrite'] has the right path depth, but this won't be the case if there's $origpath = substr($origpath, 0, $questionpos);
// a & or # somewhere in the middle of the path, due to Apache unescaping. So we make a special case for that.
// If 'REQUEST_URI' and 'qa-rewrite' already match (as on Nginx), we can skip this.
$normalizedpath = urldecode($origpath);
if (substr($normalizedpath, -strlen($qa_rewrite)) !== $qa_rewrite) {
$keepparts = count($requestparts);
$requestparts = explode('/', urldecode($origpath)); // new request calculated from $_SERVER['REQUEST_URI']
// loop forwards so we capture all parts
for ($part = 0, $max = count($requestparts); $part < $max; $part++) {
if (is_numeric(strpos($requestparts[$part], '&')) || is_numeric(strpos($requestparts[$part], '#'))) {
$keepparts += count($requestparts) - $part - 1; // this is how many parts remain
break;
}
}
$requestparts = array_slice($requestparts, -$keepparts); // remove any irrelevant parts from the beginning
}
} }
$relativedepth = count($requestparts); // 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.
elseif (isset($_GET['qa'])) { // If 'REQUEST_URI' and 'qa-rewrite' already match (as on Nginx), we can skip this.
if (strpos($_GET['qa'], '/') === false) { $normalizedpath = urldecode($origpath);
$urlformat = ( empty($_SERVER['REQUEST_URI']) || strpos($_SERVER['REQUEST_URI'], '/index.php') !== false ) if (substr($normalizedpath, -strlen($qa_rewrite)) !== $qa_rewrite) {
? QA_URL_FORMAT_SAFEST : QA_URL_FORMAT_PARAMS; $keepparts = count($requestparts);
$requestparts = array(qa_gpc_to_string($_GET['qa'])); $requestparts = explode('/', urldecode($origpath)); // new request calculated from $_SERVER['REQUEST_URI']
for ($part = 1; $part < 10; $part++) { // loop forwards so we capture all parts
if (isset($_GET['qa_'.$part])) { for ($part = 0, $max = count($requestparts); $part < $max; $part++) {
$requestparts[] = qa_gpc_to_string($_GET['qa_'.$part]); if (is_numeric(strpos($requestparts[$part], '&')) || is_numeric(strpos($requestparts[$part], '#'))) {
unset($_GET['qa_'.$part]); $keepparts += count($requestparts) - $part - 1; // this is how many parts remain
break;
} }
} }
}
else {
$urlformat = QA_URL_FORMAT_PARAM;
$requestparts = explode('/', qa_gpc_to_string($_GET['qa']));
}
unset($_GET['qa']); $requestparts = array_slice($requestparts, -$keepparts); // remove any irrelevant parts from the beginning
}
} }
else {
$normalizedpath = strtr($_SERVER['PHP_SELF'], '+', ' '); // seems necessary, and plus does not work with this scheme
$indexpath = '/index.php/';
$indexpos = strpos($normalizedpath, $indexpath);
if (!empty($_SERVER['REQUEST_URI'])) { // workaround for the fact that Apache unescapes characters $relativedepth = count($requestparts);
$origpath = $_SERVER['REQUEST_URI']; } elseif (isset($_GET['qa'])) {
$questionpos = strpos($origpath, '?'); if (strpos($_GET['qa'], '/') === false) {
if ($questionpos !== false) { $urlformat = (empty($_SERVER['REQUEST_URI']) || strpos($_SERVER['REQUEST_URI'], '/index.php') !== false)
$origpath = substr($origpath, 0, $questionpos); ? QA_URL_FORMAT_SAFEST : QA_URL_FORMAT_PARAMS;
$requestparts = array(qa_gpc_to_string($_GET['qa']));
for ($part = 1; $part < 10; $part++) {
if (isset($_GET['qa_' . $part])) {
$requestparts[] = qa_gpc_to_string($_GET['qa_' . $part]);
unset($_GET['qa_' . $part]);
} }
$normalizedpath = urldecode($origpath);
$indexpos = strpos($normalizedpath, $indexpath);
} }
} else {
$urlformat = QA_URL_FORMAT_PARAM;
$requestparts = explode('/', qa_gpc_to_string($_GET['qa']));
}
if (is_numeric($indexpos)) { unset($_GET['qa']);
$urlformat = QA_URL_FORMAT_INDEX; } else {
$requestparts = explode('/', substr($normalizedpath, $indexpos + strlen($indexpath))); $normalizedpath = strtr($_SERVER['PHP_SELF'], '+', ' '); // seems necessary, and plus does not work with this scheme
$relativedepth = 1 + count($requestparts); $indexpath = '/index.php/';
} $indexpos = strpos($normalizedpath, $indexpath);
else {
$urlformat = null; // at home page so can't identify path type if (!empty($_SERVER['REQUEST_URI'])) { // workaround for the fact that Apache unescapes characters
$requestparts = array(); $origpath = $_SERVER['REQUEST_URI'];
$questionpos = strpos($origpath, '?');
if ($questionpos !== false) {
$origpath = substr($origpath, 0, $questionpos);
} }
$normalizedpath = urldecode($origpath);
$indexpos = strpos($normalizedpath, $indexpath);
} }
foreach ($requestparts as $part => $requestpart) { // remove any blank parts if (is_numeric($indexpos)) {
if (!strlen($requestpart)) $urlformat = QA_URL_FORMAT_INDEX;
unset($requestparts[$part]); $requestparts = explode('/', substr($normalizedpath, $indexpos + strlen($indexpath)));
$relativedepth = 1 + count($requestparts);
} else {
$urlformat = null; // at home page so can't identify path type
$requestparts = array();
} }
}
reset($requestparts); foreach ($requestparts as $part => $requestpart) { // remove any blank parts
$key = key($requestparts); if (!strlen($requestpart))
unset($requestparts[$part]);
}
$requestkey = isset($requestparts[$key]) ? $requestparts[$key] : ''; reset($requestparts);
$replacement = array_search($requestkey, qa_get_request_map()); $key = key($requestparts);
if ($replacement !== false)
$requestparts[$key] = $replacement;
qa_set_request( $requestkey = isset($requestparts[$key]) ? $requestparts[$key] : '';
implode('/', $requestparts), $replacement = array_search($requestkey, qa_get_request_map());
($relativedepth > 1 ? str_repeat('../', $relativedepth - 1) : './'), if ($replacement !== false)
$urlformat $requestparts[$key] = $replacement;
);
} qa_set_request(
implode('/', $requestparts),
($relativedepth > 1 ? str_repeat('../', $relativedepth - 1) : './'),
$urlformat
);
}
qa_index_set_request(); qa_index_set_request();
// Branch off to appropriate file for further handling // Branch off to appropriate file for further handling
$requestlower = strtolower(qa_request()); $requestlower = strtolower(qa_request());
if ($requestlower == 'install') if ($requestlower == 'install') {
require QA_INCLUDE_DIR.'qa-install.php'; require QA_INCLUDE_DIR . 'qa-install.php';
elseif ($requestlower == 'url/test/'.QA_URL_TEST_STRING) } elseif ($requestlower == 'url/test/' . QA_URL_TEST_STRING) {
require QA_INCLUDE_DIR.'qa-url-test.php'; require QA_INCLUDE_DIR . 'qa-url-test.php';
else { } else {
// enable gzip compression for output (needs to come early) // enable gzip compression for output (needs to come early)
qa_initialize_buffering($requestlower); qa_initialize_buffering($requestlower);
if (substr($requestlower, 0, 5) == 'feed/') if (substr($requestlower, 0, 5) == 'feed/') {
require QA_INCLUDE_DIR.'qa-feed.php'; require QA_INCLUDE_DIR . 'qa-feed.php';
else } else {
require QA_INCLUDE_DIR.'qa-page.php'; require QA_INCLUDE_DIR . 'qa-page.php';
} }
} }
}
qa_report_process_stage('shutdown'); qa_report_process_stage('shutdown');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment