$http_host = explode(':', $_SERVER['HTTP_HOST'] );
if( (!empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) != 'off' || isset( $http_host[1] ) && $http_host[1] == 443) && substr( $mosConfig_live_site, 0, 8 ) != 'https://' ) {
$mosConfig_live_site = 'https://'.substr( $mosConfig_live_site, 7 );
}
require_once( 'includes/joomla.php' );
//Installation sub folder check, removed for work with SVN
if (file_exists( 'installation/index.php' ) && $_VERSION->SVN == 0) {
define( '_INSTALL_CHECK', 1 );
include ( $mosConfig_absolute_path .'/offline.php');
exit();
}
// displays offline/maintanance page or bar
if ($mosConfig_offline == 1) {
require( $mosConfig_absolute_path .'/offline.php' );
}
// load system bot group
$_MAMBOTS->loadBotGroup( 'system' );
// trigger the onStart events
$_MAMBOTS->trigger( 'onStart' );
if (file_exists( $mosConfig_absolute_path .'/components/com_sef/sef.php' )) {
require_once( $mosConfig_absolute_path .'/components/com_sef/sef.php' );
} else {
require_once( $mosConfig_absolute_path .'/includes/sef.php' );
}
require_once( $mosConfig_absolute_path .'/includes/frontend.php' );
// retrieve some expected url (or form) arguments
$option = strval( strtolower( mosGetParam( $_REQUEST, 'option' ) ) );
$Itemid = intval( mosGetParam( $_REQUEST, 'Itemid', null ) );
if ($option == '') {
if ($Itemid) {
$query = "SELECT id, link"
. "\n FROM #__menu"
. "\n WHERE menutype = 'mainmenu'"
. "\n AND id = " . (int) $Itemid
. "\n AND published = 1"
;
$database->setQuery( $query );
} else {
$query = "SELECT id, link"
. "\n FROM #__menu"
. "\n WHERE menutype = 'mainmenu'"
. "\n AND published = 1"
. "\n ORDER BY parent, ordering"
;
$database->setQuery( $query, 0, 1 );
}
$menu = new mosMenu( $database );
if ($database->loadObject( $menu )) {
$Itemid = $menu->id;
}
$link = $menu->link;
if (($pos = strpos( $link, '?' )) !== false) {
$link = substr( $link, $pos+1 ). '&Itemid='.$Itemid;
}
parse_str( $link, $temp );
/** this is a patch, need to rework when globals are handled better */
foreach ($temp as $k=>$v) {
$GLOBALS[$k] = $v;
$_REQUEST[$k] = $v;
if ($k == 'option') {
$option = $v;
}
}
}
if ( !$Itemid ) {
// when no Itemid give a default value
$Itemid = 99999999;
}
// mainframe is an API workhorse, lots of 'core' interaction routines
$mainframe = new mosMainFrame( $database, $option, '.' );
$mainframe->initSession();
// trigger the onAfterStart events
$_MAMBOTS->trigger( 'onAfterStart' );
// checking if we can find the Itemid thru the content
if ( $option == 'com_content' && $Itemid === 0 ) {
$id = intval( mosGetParam( $_REQUEST, 'id', 0 ) );
$Itemid = $mainframe->getItemid( $id );
}
/** do we have a valid Itemid yet?? */
if ( $Itemid === 0 ) {
/** Nope, just use the homepage then. */
$query = "SELECT id"
. "\n FROM #__menu"
. "\n WHERE menutype = 'mainmenu'"
. "\n AND published = 1"
. "\n ORDER BY parent, ordering"
;
$database->setQuery( $query, 0, 1 );
$Itemid = $database->loadResult();
}
// patch to lessen the impact on templates
if ($option == 'search') {
$option = 'com_search';
}
// loads english language file by default
if ($mosConfig_lang=='') {
$mosConfig_lang = 'english';
}
include_once( $mosConfig_absolute_path .'/language/' . $mosConfig_lang . '.php' );
// frontend login & logout controls
$return = strval( mosGetParam( $_REQUEST, 'return', NULL ) );
$message = intval( mosGetParam( $_POST, 'message', 0 ) );
if ($option == 'login') {
$mainframe->login();
// JS Popup message
if ( $message ) {
?>
logout();
// JS Popup message
if ( $message ) {
?>
getUser();
// detect first visit
$mainframe->detect();
// set for overlib check
$mainframe->set( 'loadOverlib', false );
$gid = intval( $my->gid );
// gets template for page
$cur_template = $mainframe->getTemplate();
/** temp fix - this feature is currently disabled */
/** @global A places to store information from processing of the component */
$_MOS_OPTION = array();
// precapture the output of the component
require_once( $mosConfig_absolute_path . '/editor/editor.php' );
ob_start();
if ($path = $mainframe->getPath( 'front' )) {
$task = strval( mosGetParam( $_REQUEST, 'task', '' ) );
$ret = mosMenuCheck( $Itemid, $option, $task, $gid );
if ($ret) {
require_once( $path );
} else {
mosNotAuth();
}
} else {
header( 'HTTP/1.0 404 Not Found' );
echo _NOT_EXIST;
}
$_MOS_OPTION['buffer'] = ob_get_contents();
ob_end_clean();
initGzip();
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
// display the offline alert if an admin is logged in
if (defined( '_ADMIN_OFFLINE' )) {
include( $mosConfig_absolute_path .'/offlinebar.php' );
}
// loads template file
if ( !file_exists( $mosConfig_absolute_path .'/templates/'. $cur_template .'/index.php' ) ) {
echo _TEMPLATE_WARN . $cur_template;
} else {
require_once( $mosConfig_absolute_path .'/templates/'. $cur_template .'/index.php' );
echo '';
}
/*
* cache implementation
*/
$usr_obj = $mainframe->getUser();
if ($usr_obj->name != "" ) {
/*
* Set the lifetime of the cookie, doesn't matter howlong, if logout the cookie is disabled
*/
$lifetime = time() + 365*24*60*60;
setcookie("bypass_cache", "true", $lifetime, "/");
$bypass_cache = true;
if ( $is_debug ) {
$debug_out = $debug_out . "
Bypassing cache because LOGIN ";
}
} else {
if (isset($_COOKIE["bypass_cache"]) == "true" ){
/*
* Make the invalid if no-user is active
*/
setcookie("bypass_cache", "false", time(), "/");
if ( $is_debug ) {
$debug_out = $debug_out . "
Bypassing cache because LOGOUT ";
}
}
}
if ( $bypass_cache == false ) {
/*
* Check if the URL may be cached;
*/
$generate = true;
$cache_disallow = $jrecache_config->getCfg('cache_disallow_filter');
if ( $cache_disallow != "") {
$lines = explode ( "\n", $cache_disallow );
foreach( $lines as $line ) {
if ( $line != "") {
if ( strstr ( $url_to_store, chop( $line)) ) {
$generate = false;
if ( $is_debug ) {
$debug_out = $debug_out . "
File not generated due to cache disallow filter: " . $line;
}
}
}
}
}
if ( $generate ){
$output = ob_get_contents();
/*
* Update the table #__jrecache_repository and check if the CACHE may be written;
*/
$url = $url_to_store;
$sql = "SELECT is_may_cached, datetime, cache_file FROM #__jrecache_repository WHERE url='$url' AND cookie_info='$cookie_string'";
$database->setQuery( $sql );
$cache_obj = null;
$cache_obj = $database->loadObjectList( $cache_obj);
$may_write = true;
$datetime = "";
if ( $cache_obj) {
if ($cache_obj[0]->is_may_cached == "0") {
$may_write = false;
if ( $is_debug ) {
$debug_out = $debug_out . "
File not generated due to cache disallow from cache repository database";
}
}
}
if ( $may_write ) {
$content_not_cached = $jrecache_config->getCfg('content_not_cached');
if ( preg_match ( $content_not_cached, $output ) ) {
$page_valid = false;
} else {
$page_valid = true;
}
/*
* final write the cache record
*/
if ( $page_valid) {
if ( !$jrecache_config->getCfg('enable_compressed_cache') ) {
if ( $is_debug ) {
$debug_out = $debug_out . "
Generating plain cache file: $file ";
}
} else {
$output = gzip($output, 9, $file);
if ( $is_debug ) {
$debug_out = $debug_out . "
Generating Compressed cache file: $file ";
}
}
/*
* final write the cache record
*/
$bytes = _file_put_contents($file, $output, LOCK_EX);
$filesize = round($bytes/1024,1);
$url = $url_to_store;
$datetime = date( 'Y-m-d H:i:s', time() );
if ( $cache_obj ) {
$sql = "UPDATE #__jrecache_repository SET datetime='$datetime', cache_size='$filesize', cache_file='$hash' WHERE url='$url' AND cookie_info='$cookie_string'";
} else {
$sql = "INSERT INTO #__jrecache_repository ( url, cookie_info, cache_file, datetime, cache_size, is_may_cached ) values ( '$url', '$cookie_string','$hash', '$datetime', '$filesize', '1' )";
}
$database->setQuery( $sql );
$database->query();
if ( $is_debug ) {
$debug_out = $debug_out . "
File size: " . $filesize . " Kb" ;
}
} else {
headers_sent($filename, $linenum);
$debug_out = $debug_out . "
Page not included within cache due to content disallow filter" ;
}
}
}
}
if ( $jrecache_config->getCfg('enable_cache') == "1" & $cache_cleanup ) {
$debug_out = $debug_out . "
Executing the cache garbage cleanup: deleting outdated cache files";
$cache_directory = $jrecache_config->getCfg('cache_directory');
if ($dh = opendir($cache_directory)) {
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != ".." && $file != "index.html" && $file != "garbage.cfg") {
$filename = $cache_directory . "/". $file;
if ( $is_all) {
@unlink ( $filename );
} else {
if ( time() - @filemtime($filename) > $cache_TTL ) {
@unlink ( $filename );
}
}
}
}
closedir($dh);
}
/*
* Empty the table #_jrecache_repository
*/
$sql = "DELETE FROM #__jrecache_repository WHERE time_to_sec(now())-time_to_sec(datetime) > $cache_TTL and is_may_cached = '1'";
$database->setquery( $sql );
$database->query();
_file_put_contents( $garbage_file, "don't remove this file", LOCK_EX);
}
if ( $is_debug ) {
$microstop = explode(' ',microtime());
$_stop_time = $microstop[0] + $microstop[1];
$debug_out = $debug_out . "
Generation time:" . round($_stop_time - $_start_time,7). " sec.";
echo $debug_out;
}
}
/*
* end of the cache building
*/
// displays queries performed for page
if ($mosConfig_debug) {
echo $database->_ticker . ' queries executed';
echo '';
foreach ($database->_log as $k=>$sql) {
echo $k+1 . "\n" . $sql . '
';
}
echo '';
}
doGzip();
function gzip($data = "", $level = 6, $filename = "", $comments = "")
{
$flags = (empty($comment)? 0 : 16) + (empty($filename)? 0 : 8);
$mtime = time();
return (pack("C1C1C1C1VC1C1", 0x1f, 0x8b, 8, $flags, $mtime, 2, 0xFF) .
(empty($filename) ? "" : $filename . "\0") .
(empty($comment) ? "" : $comment . "\0") .
gzdeflate($data, $level) .
pack("VV", crc32($data), strlen($data)));
}
function check_cookie ( $cookie ) {
$return_value = "";
if ( strpos( $cookie, "[" ) === false ) {
$debug_out = $debug_out . "
Cached cookie: $cookie value: $_COOKIE[$cookie] ";
if ( $_COOKIE[$cookie] != "") {
$return_value = $_COOKIE[$cookie];
}
} else {
/*
* Cookie is an array, extract the array and the info
*/
$start_at = strpos ( $cookie, "[");
$end_at = strpos ( $cookie, "]") - ( $start_at + 1);
if ( strpos ( $cookie, "]") ) {
$cookie_name = substr( $cookie, 0, $start_at );
$value_name = substr ( $cookie, $start_at + 1, $end_at );
/*
* Loop through the cookie to retrieve the value
*/
$sub_cookies = $_COOKIE[$cookie_name];
if ( is_array ( $sub_cookies ) ) {
foreach ( $sub_cookies as $name => $value ) {
if ( $name == $value_name ) {
$return_value = $value;
}
}
}
}
}
return $return_value;
}
function _file_put_contents( $filename, $content, $flags = null, $resource_context = null ) {
// If $content is an array, convert it to a string
if (is_array( $content ))
$content = implode( '', $content );
// If we don't have a string, throw an error
if (!is_scalar( $content )) {
$errormsg = 'file_put_contents() The 2nd parameter should be either a string or an array';
trigger_error( $errormsg, E_USER_WARNING );
return false;
}
// Get the length of date to write
$length = strlen( $content );
// Check what mode we are using and whether we're using the include path
$mode = ($flags & FILE_APPEND) ? 'a' : 'w';
$use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ? true : false;
// Open the file for writing
if (($fh = @fopen( $filename, $mode, $use_inc_path )) === false) {
$errormsg = 'file_put_contents() failed to open stream: Permission denied';
trigger_error( $errormsg, E_USER_WARNING );
return false;
}
// Write to the file
$bytes = 0;
if (($bytes = @fwrite( $fh, $content )) === false) {
$errormsg = sprintf( 'file_put_contents() Failed to write %d bytes to %s', $length, $filename );
trigger_error( $errormsg, E_USER_WARNING );
return false;
}
// Close the handle
@fclose( $fh );
// Check all the data was written
if ($bytes != $length) {
$errormsg = sprintf( 'file_put_contents() Only %d of %d bytes written, possibly out of free disk space.', $bytes, $length );
trigger_error( $errormsg, E_USER_WARNING );
return false;
}
// Return length written
return $bytes;
}
?>