<?

error_reporting(E_ALL);

ob_start();

require_once('./cms/config.inc.php');

require_once(CMS_FUNCTIONS_DIR.'my_sql.inc.php');

require_once(CMS_FUNCTIONS_DIR.'path_translation.inc.php');

require_once(CMS_FUNCTIONS_DIR.'login_visitors.inc.php');

require_once(CMS_FUNCTIONS_DIR.'unquote.inc.php');

require_once(CMS_CLASSES_DIR.'resource.inc.php');

require_once(CMS_CLASSES_DIR.'cache_file.inc.php');

require_once(CMS_CLASSES_DIR.'cache_db.inc.php');

CheckVisitorLogin();

$domain = unquote_string($_SERVER['SERVER_NAME']);

$aliases = DomainsAliases();

if (isset($aliases[$domain]))

$domain = $aliases[$domain];

$request = unquote_string($_SERVER['REDIRECT_URL']);

$query = unquote_string($_SERVER['QUERY_STRING']);

$path = '/domains/'.$domain.$request;

$cache_key = $domain.$request.'?'.$query;

$resource = '';

$resource = $query;

if (($pos = strpos($resource,'&')) !== false)

$resource = substr($resource,0,$pos);

if (strpos($resource,'=') !== false)

$resource = '';

$headers = false;

$content = false;

if (count($_POST) === 0) {

$data = CacheFile::Get($cache_key);

if ($data !== false) {

$answer = unserialize($data);

if ($answer->expires > time()) {

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {

foreach ($answer->Headers() as $header) {

if (preg_match("/^Last-Modified: .*/i",$header,$match)) {

require_once(CMS_FUNCTIONS_DIR.'date.inc.php');

$lmt = StringToUnixTime($match[1]);

$ims = StringToUnixTime($_SERVER['HTTP_IF_MODIFIED_SINCE']);

if ($lmt !== -1 & $ims !== -1 & $lmt<=$ims) {

NotModifiedAnswer();

exit;

}

}

}

}

ob_end_clean();

foreach($answer->Headers() as $header)

header($header);

//Logger($answer);

if ($answer->size < 65536) {

echo $answer->Content();

}

else {

$counter = 0;

while ($answer->size > $counter) {

echo $answer->Content($counter,65536);

$counter += 65536;

}

}

exit;

}

}

}

$class = PathToClass($path);

//var_dump($class);exit;

if (!is_object($class)) {

NotFoundError($domain, $request);

exit;

}

if (!$class->visitor_permissions['read']) {

AccessError($domain, $request);

exit;

}

if ($resource == '') {

if (isset($class->resources['default']))

$resource = 'default';

// foreach ($class->resources as $name=>$item) {

// $resource = $name;

// break;

//}

if ($resource == '') {

NotFoundError($domain, $request);

exit;

}

}

if ($resource!='' & !is_object($class->resources[$resource])) {

NotFoundError($domain, $request);

}

$answer = $class->resources[$resource]->Request($class);

if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {

foreach ($answer->Headers() as $header) {

if (preg_match("/^Last-Modified: (.*)/i", $header, $match)) {

require_once(CMS_FUNCTIONS_DIR.'date.inc.php');

$lmt = StringToUnixTime($match[1]);

$ims = StringToUnixTime($_SERVER['HTTP_IF_MODIFIED_SINCE']);

if ($lmt !== -1 & $ims !== -1 & $lmt<=$ims) {

NotModifiedAnswer();

if ($answer->expires>0) {

CacheFile::Place($answer->combined, $cache_key, $class->path, serialize($answer));

}

exit;

}

}

}

}

ob_end_clean();

foreach($answer->Headers() as $header)

header($header);

//Logger($answer);

if ($answer->size < 65536) {

echo $answer->Content();

}

else {

$counter = 0;

while ($answer->size > $counter) {

echo $answer->Content($counter,65536);

$counter += 65536;

}

}

if ($answer->expires>0) {

CacheFile::Place($answer->combined,$cache_key,$class->path,serialize($answer));

}

unset($class);

exit;

function Logger($answer)

{

include_once('./cms/logger/hit.inc.php');

if (function_exists('loggerHit'))

{

$request = unquote_string($_SERVER['REDIRECT_URL']);

$query = unquote_string($_SERVER['QUERY_STRING']);

$url = ' . $request;

if ($query != '')

$url .= '?' . $query;

$ref = unquote_string($_SERVER['HTTP_REFERER']);

$ua = unquote_string($_SERVER['HTTP_USER_AGENT']);

$ip = unquote_string($_SERVER['HTTP_X_REAL_IP']);

$mimeType = 'text/html';

$title = '';

foreach($answer->Headers() as $header)

{

if (preg_match('/^Content-Type: ([\w-]+\/[\w-]+)/', $header, $match))

{

$mimeType = $match[1];

}

}

if ($mimeType == 'text/html')

{

if (preg_match('/<title>(.*?)<\/title>/', $answer->Content(0, 1024), $match))

$title = $match[1];

}

loggerHit($url, $mimeType, $title, $ref, $ua, $ip);

}

}

function NotModifiedAnswer()

{

ob_end_clean();

header('HTTP/1.0 304 Not Modified');

return true;

}

function SystemError()

{

ob_end_clean();

header ('HTTP/1.0 500 Internal Server Error');

header ('Content-type: text/html');

echo '<HTML>';

echo '<HEAD<TITLE>500 Internal Server Error</TITLE>';

echo '<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">';

echo '</HEAD<BODY>';

echo '<H1>Internal Server Error</H1>The server encountered an internal error or';

echo 'misconfiguration and was unable to complete your request.';

echo '<P>Please contact the server administrator and';

echo 'inform them of the time the error occurred, and anything you might have done';

echo 'that may have caused the error.';

echo '<P>More information about this error may be available in the server error log.';

echo '</BODY</HTML>';

return true;

}

function AccessError($domain,$path)

{

$domain_class = PathToClass('/domains/'.$domain);

if (is_object($domain_class) & $domain_class->module == 'domain') {

if ($domain_class->e403) {

$error_class = PathToClass('/domains/'.$domain.'/'.$domain_class->e403);

if ($error_class) {

if ($error_class->module == 'html_doc' & isset($error_class->resources['default'])) {

$answer = $error_class->resources['default']->Request($error_class);

if (is_object($answer)) {

ob_end_clean();

header ('HTTP/1.0 403 Forbidden');

foreach($answer->Headers() as $header)

header($header);

echo $answer->Content();

unset($answer);

unset($error_class);

unset($domain_class);

return true;

}

}

unset($error_class);

}

}

unset($domain_class);

}

ob_end_clean();

header ('HTTP/1.0 403 Forbidden');

header ('Content-type: text/html');

echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';

echo '<HTML<HEAD>';

echo '<TITLE>403 Forbidden</TITLE>';

echo '</HEAD<BODY>';

echo '<H1>Forbidden</H1>';

echo 'You don\'t have permission to access '.$path.' on this server.';

echo '</BODY</HTML>';

return true;

}

function NotFoundError($domain,$path)

{

$domain_class = PathToClass('/domains/'.$domain);

if (is_object($domain_class) & $domain_class->module == 'domain') {

if ($domain_class->e404) {

$error_class = PathToClass('/domains/'.$domain.'/'.$domain_class->e404);

if ($error_class) {

if ($error_class->module == 'html_doc' & isset($error_class->resources['default'])) {

$answer = $error_class->resources['default']->Request($error_class);

if (is_object($answer)) {

ob_end_clean();

header ('HTTP/1.0 404 Not Found');

foreach($answer->Headers() as $header)

header($header);

echo $answer->Content();

unset($answer);

unset($error_class);

unset($domain_class);

return true;

}

}

unset($error_class);

}

}

unset($domain_class);

}

ob_end_clean();

//header ('Location:

//return true;

header ('HTTP/1.0 404 Not Found');

header ('Content-type: text/html');

echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';

echo '<html<head>';

echo '<title>404 Not Found</title>';

echo '</HEAD<BODY>';

echo '<H1>Not Found</H1>';

echo '<p>The requested URL '.$path.' was not found on this server.</p>';

echo '</BODY</HTML>';

return true;

}

function DomainsAliases()

{

$serialized = CacheDb::Get('domains_aliases','/domains');

if (!$serialized || !($aliases = unserialize($serialized))) {

$aliases = array();

$domains = PathToClass('/domains');

if (!is_object($domains)) {

SystemError();

exit;

}

foreach($domains->childrens as $children) {

if($children->module == 'domain' & is_array($children->aliases) & count($children->aliases)>0) {

foreach($children->aliases as $alias) {

$aliases[$alias] = $children->name;

}

}

}

unset($domains);

CacheDb::Place(CACHE_DB_COMBINED_CHAIN,'domains_aliases','/domains',serialize($aliases));

}

return $aliases;

}

?>