Файл: wboard/source/index.php
Строк: 158
<?php
/**
* Wboard
* @author Screamer
* @copyright 2013
*/
date_default_timezone_set('UTC');
error_reporting(-1);
@set_time_limit(0);
ini_set('display_error', TRUE);
// Save exceptions to log? TRUE - yes; FALSE - no;
define('LOG_EXP', FALSE);
/** (string) Root directory */
$rootpath = rtrim(dirname(__FILE__), '/\') . DIRECTORY_SEPARATOR;
// Strip slashes
if (get_magic_quotes_gpc()) {
$in = array(&$_GET, &$_POST, &$_COOKIE);
while ((list($k, $v) = each($in)) !== false) {
foreach ($v as $key => $val) {
if (!is_array($val)) {
$in[$k][$key] = stripslashes($val);
continue;
}
$in[] =& $in[$k][$key];
}
}
unset($in);
if (!empty($_FILES)) {
foreach ($_FILES as $k => $v) {
$_FILES[$k]['name'] = stripslashes((string)$v['name']);
}
}
}
// Exceptions handler
set_exception_handler('exception_handler');
function exception_handler($exception)
{
global $rootpath;
if (LOG_EXP === FALSE) {
// Display exception
echo '<pre><b>' . get_class($exception) . ' [' . $exception->getCode() . ']</b><br /><b>Message:</b> ' . $exception->getMessage()
. '<br /><b>File:</b> ' . $exception->getFile()
. '<br /><b>Line:</b> ' . $exception->getLine()
. '<br /><b>Trace:</b><br />' . $exception->getTraceAsString() . '</pre>';
} else {
// Save exception to log
$file = $rootpath . 'files' . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . date('d-m-Y', time()) . '.log';
$data = is_file($file) ? file_get_contents($file) : '';
$data = get_class($exception) . ' [' . $exception->getCode() . '] '
. PHP_EOL . 'Time: ' . date('H:i:s', time())
. PHP_EOL . 'Error: ' . $exception->getMessage()
. PHP_EOL . 'File: ' . $exception->getFile()
. PHP_EOL . 'Line: ' . $exception->getLine()
. PHP_EOL . 'Trace: ' . $exception->getTraceAsString()
. PHP_EOL . str_repeat('=', 100) . PHP_EOL . $data;
file_put_contents($file, $data);
}
exit(1);
}
// Autoloader
spl_autoload_register('autoloader');
function autoloader($name) {
global $rootpath;
$name = strtolower($name);
$file = $rootpath . 'system' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $name . '.php';
if (is_file($file) && in_array($name, array('db', 'helper', 'language', 'module', 'model', 'template'))) {
require_once $file;
} else {
throw new Exception('Unable to load class "' . $name . '"');
}
}
/**
* Anchor
* @param (string) $uri URI
* @param (string) $title Title
* @param (string) $args Some properties
*/
function anchor($uri = '', $title = '', $args = '') {
static $http_host = '';
if (empty($http_host)) {
$http_host = 'http://' . trim($_SERVER['SERVER_NAME'], '/\') . '/';
}
$return = !empty($title)
? '<a href="' . $http_host . $uri . '"' . (!empty($args) ? ' ' . $args : ''). '>' . $title . '</a>'
: $http_host . $uri;
return $return;
}
// Loading config
$CONF = $rootpath . 'system' . DIRECTORY_SEPARATOR . 'conf.php';
if (is_file($CONF)) {
$CONF = require $CONF;
if (!is_array($CONF)) {
throw new Exception('Wrong format of configuration file');
}
} else {
throw new Exception('Configuration file is not exists');
}
// Get: IP, IP via PROXY, USER-AGENT, HTTP_HOST
$NETWORK = array('ip' => 0, 'ip_via_proxy' => 0, 'user_agent' => 'Not Recognised', 'http_host' => '');
if (isset($_SERVER['REMOTE_ADDR']) && ($ip = ip2long($_SERVER['REMOTE_ADDR']))) {
$NETWORK['ip'] = intval(sprintf("%u", $ip));
$NETWORK['ip_via_proxy'] = 0;
// IP via PROXY
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#d{1,3}.d{1,3}.d{1,3}.d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $vars)) {
foreach ($vars[0] as $var) {
$NETWORK['ip_via_proxy'] = ip2long($var);
if ($NETWORK['ip_via_proxy'] && $NETWORK['ip_via_proxy'] != $ip && !preg_match('#^(10|172.16|192.168).#', $var)) {
$NETWORK['ip_via_proxy'] = intval(sprintf("%u", $NETWORK['ip_via_proxy']));
break;
}
}
}
// User-Agent
if (isset($_SERVER["HTTP_X_OPERAMINI_PHONE_UA"]) && strlen(trim($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])) > 5) {
$NETWORK['user_agent'] = 'Opera Mini: ' . htmlspecialchars(substr(trim($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']), 0, 150));
} elseif (isset($_SERVER['HTTP_USER_AGENT'])) {
$NETWORK['user_agent'] = htmlspecialchars(substr(trim($_SERVER['HTTP_USER_AGENT']), 0, 150));
}
// Host
$NETWORK['http_host'] = 'http://' . trim($_SERVER['SERVER_NAME'], '/\') . '/';
} else {
throw new Exception('Invalid IP address');
}
// Start session
session_name('SESID');
session_start();
// MySQL
$db_conf = isset($CONF['db']) && is_array($CONF['db']) ? $CONF['db'] : array();
$db_required = array('host', 'db', 'user', 'password', 'charset');
$db_error = array();
foreach ($db_required as $db_value) {
if (!isset($db_conf[$db_value])) {
$db_error[] = $db_value;
}
}
if (!empty($db_error)) {
throw new Exception('The following items are not exists in configuration file: db => [' . implode(', ' , $db_error) . ']');
}
$DB = new Db($db_conf['host'], $db_conf['user'], $db_conf['password'], $db_conf['db'], $db_conf['charset']);
unset($db_conf, $db_required, $db_error);
// Router
$CONF['router'] = isset($CONF['router']) ? $CONF['router'] : array();
if (empty($CONF['router']['module']) || empty($CONF['router']['action'])) {
throw new Exception('Unable to load module. Check your configuration file (router section)');
}
$uri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$uri = array_map('trim', $uri);
$module = !empty($uri[0]) ? $uri[0] : '';
if ($module == 'w_action') {
// System action
$module = !empty($uri[1]) ? $uri[1] : $CONF['router']['module'];
$action = !empty($uri[2]) ? $uri[2] : $CONF['router']['action'];
$args = sizeof($uri) > 3 ? array_slice($uri, 3) : array();
} elseif (!empty($module)) {
// Board handler
$args = $uri;
$module = 'board';
$action = 'view';
} else {
// Default
$module = $CONF['router']['module'];
$action = $CONF['router']['action'];
$args = sizeof($uri) > 1 ? array_slice($uri, 1) : array();
}
$file = $rootpath . 'system' . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR . $module . '.php';
$redirect = TRUE;
if (is_file($file)) {
require $file;
$class = 'Module_' . ucwords($module);
if (class_exists($class)) {
$module = new $class($DB, $NETWORK, $rootpath);
if (method_exists($module, $action)) {
call_user_func_array(array($module, $action), $args);
$redirect = FALSE;
}
}
}
// Redirect to error page
if ($redirect === TRUE) {
if (!empty($CONF['router']['error']) && $module != $CONF['router']['error']) {
header('Location: ' . $NETWORK['http_host'] . 'w_action/' . $CONF['router']['error']);
exit;
} else {
throw new Exception('Unable to load error page. Check your configuration file (router section)');
}
}