<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2007                                            */
/* http://www.php-maximus.org                                           */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/* Portage Coppermine Photo Gallery                                     */
/************************************************************************/
global $template,$template_main_menu;
define('COPPERMINE_VERSION', '1.4.0');
if ( !defined('IN_COPPERMINE') ) die('Not in Coppermine...');

// Start output buffering
//ob_start();

// Report all errors except E_NOTICE
// This is the default value set in php.ini
// error_reporting (E_ALL ^ E_NOTICE);
//error_reporting(E_ALL);

//set_magic_quotes_runtime(0);

// used for timing purpose
$query_stats = array();

function getmicrotime(){
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();

// Do some cleanup in GET, POST and cookie data and un-register global vars
$HTML_SUBST = array('"' => '&quot;', '<' => '&lt;', '>' => '&gt;');
if (get_magic_quotes_gpc()){
	if (is_array($_POST)) {
		foreach ($_POST as $key => $value){
			if (!is_array($value))
				$_POST[$key] = strtr(stripslashes($value), $HTML_SUBST);
			if (isset($$key)) unset($$key);
		}
	}

	if (is_array($_GET)) {
		foreach ($_GET as $key => $value){
			$_GET[$key] = strtr(stripslashes($value), $HTML_SUBST);
			if (isset($$key)) unset($$key);
		}
	}

	if (is_array($_COOKIE)) {
		foreach ($_COOKIE as $key => $value){
			if (!is_array($value))
 				$_COOKIE[$key] = stripslashes($value);
			if (isset($$key)) unset($$key);
		}
	}
} else {
	if (is_array($_POST)) {
		foreach ($_POST as $key => $value){
			if (!is_array($value))
				$_POST[$key] = strtr($value, $HTML_SUBST);
			if (isset($$key)) unset($$key);
		}
	}

	if (is_array($_GET)) {
		foreach ($_GET as $key => $value){
			$_GET[$key] = strtr($value, $HTML_SUBST);
			if (isset($$key)) unset($$key);
		}
	}

	if (is_array($_COOKIE)) {
		foreach ($_COOKIE as $key => $value){
			if (isset($$key)) unset($$key);
		}
	}
}

// Initialise the $CONFIG array and some other variables
$CONFIG=array();
$PHP_SELF = isset($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : $_SERVER['SCRIPT_NAME'];
$REFERER = urlencode( $PHP_SELF . (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : ''));
$ALBUM_SET ='';
$FORBIDDEN_SET ='';
$CURRENT_CAT_NAME ='';

$IMG_TYPES = array(
	1 => 'GIF',
	2 => 'JPG',
	3 => 'PNG',
	4 => 'SWF',
	5 => 'PSD',
	6 => 'BMP',
	7 => 'TIFF',
	8 => 'TIFF',
	9 => 'JPC',
	10 => 'JP2',
	11 => 'JPX',
	12 => 'JB2',
	13 => 'SWC',
	14 => 'IFF'
);

// Include functions files
if( is_file('modules/Coppermine/include/functions.inc.php') ) {
	include('modules/Coppermine/include/functions.inc.php');
}
// Include functions files
if( is_file('modules/Coppermine/include/constants.php') ) {
	include('modules/Coppermine/include/constants.php');
}
// Check for GD GIF Create support
if ($CONFIG['thumb_method'] == 'im' || function_exists('imagecreatefromgif'))
  $CONFIG['GIF_support'] = 1;
else
  $CONFIG['GIF_support'] = 0;

// Retrieve DB stored configuration
$results = $GLOBALS['db']->sql_query("SELECT `name`, `value` FROM `".TABLE_CPG_CONFIG."` ");
while($row = $GLOBALS['db']->sql_fetchrow($results)){
    $CONFIG[$row['name']] = secure( $row['value'], 'strip', 1 );
} // while
$GLOBALS['db']->sql_free($results);

// Set error logging level
if ($CONFIG['debug_mode']) {
    error_reporting (E_ALL);
} else {
	error_reporting (E_ALL ^ E_NOTICE);
}

// Parse cookie stored user profile
user_get_profile();



if (!isset($cookie[0]) || !isset($cookie[2])) {
	$cookie_uid  = 0;
	$cookie_pass = '*';
} else {
	$cookie_uid  = (int)$cookie[0];
	$cookie_pass = substr(addslashes($cookie[2]), 0, 32);
}

$sql = "SELECT * ".
	   "FROM `".TABLE_USERS."`, `".TABLE_CPG_USERGROUPS."` ".
	   "WHERE `user_group_cp` = group_id ".
	   "AND `user_id`='$cookie_uid'".
	   "AND `user_active_cp` = 'YES' ".
	   "AND `user_password` = '$cookie_pass'";
$results = $GLOBALS['db']->sql_query($sql);

if ($GLOBALS['db']->sql_numrows($results)){
	$USER_DATA = $GLOBALS['db']->sql_fetchrow($results);
	unset($USER_DATA['user_password']);

    define('USER_ID', (int)$USER_DATA['user_id']);
    define('username', $USER_DATA['username']);
    define('user_group_cp', $USER_DATA['group_name']);
    //define('USER_IS_ADMIN', (int)$USER_DATA['has_admin_access']);

		if(defined('ADMIN_ALLOW')) {
			define('USER_IS_ADMIN', 1);
		} else	{
			define('USER_IS_ADMIN', (int)$USER_DATA['has_admin_access']);
		}
    define('USER_CAN_SEND_ECARDS', (int)$USER_DATA['can_send_ecards']);
    define('USER_CAN_RATE_PICTURES', (int)$USER_DATA['can_rate_pictures']);
    define('USER_CAN_POST_COMMENTS', (int)$USER_DATA['can_post_comments']);
    define('USER_CAN_UPLOAD_PICTURES', (int)$USER_DATA['can_upload_pictures']);
    define('USER_CAN_CREATE_ALBUMS', (int)$USER_DATA['can_create_albums']);
	$GLOBALS['db']->sql_free($results);
} else {
    $results = $GLOBALS['db']->sql_query("SELECT * FROM `".TABLE_CPG_USERGROUPS."` WHERE `group_id` = '3'");
	if (!$GLOBALS['db']->sql_numrows($results)) die('<b>Coppermine critical error</b>:<br />The group table does not contain the Anonymous group !');
	$USER_DATA = $GLOBALS['db']->sql_fetchrow($results);
    define('USER_ID', 0);
    define('username', 'Anonymous');
    define('user_group_cp', $USER_DATA['group_name']);

		if(defined('ADMIN_ALLOW')) {
			define('USER_IS_ADMIN', 1);
		} else {
			define('USER_IS_ADMIN', (int)$USER_DATA['has_admin_access']);
		}
    define('USER_CAN_SEND_ECARDS', (int)$USER_DATA['can_send_ecards']);
    define('USER_CAN_RATE_PICTURES', (int)$USER_DATA['can_rate_pictures']);
    define('USER_CAN_POST_COMMENTS', (int)$USER_DATA['can_post_comments']);
    define('USER_CAN_UPLOAD_PICTURES', (int)$USER_DATA['can_upload_pictures']);
    define('USER_CAN_CREATE_ALBUMS', 0);
	$GLOBALS['db']->sql_free($results);
}



// Test if admin mode
$USER['am'] = isset($USER['am']) ? (int)$USER['am'] : 0;


define('GALLERY_ADMIN_MODE', USER_IS_ADMIN && $USER['am']);
define('USER_ADMIN_MODE', USER_ID && USER_CAN_CREATE_ALBUMS && $USER['am'] && !GALLERY_ADMIN_MODE);


// Load theme file
if (!file_exists("modules/Coppermine/themes/default/theme.php")) $CONFIG['theme'] = 'default';


// Include config and functions files

include "modules/Coppermine/themes/{$CONFIG['theme']}/theme.php";


$THEME_DIR = "modules/Coppermine/themes/{$CONFIG['theme']}/";



// Process language selection if present in URI or in user profile or try
// autodetection if default charset is utf-8
if (isset($_GET['lang'])) {
    $USER['lang'] = secure( $_GET['lang'], 'strip', 1 );
}

if (isset($USER['lang'])
	&& !strstr($USER['lang'], '/')
	&& file_exists('modules/Coppermine/lang/'.$USER['lang'].'.php')){
	$CONFIG['lang'] = strtr($USER['lang'], '$/\\:*?"\'<>|`', '____________');
} elseif($CONFIG['charset'] == 'utf-8'){
	include('include/select_lang.inc.php');
	if(file_exists('modules/Coppermine/lang/'.$USER['lang'].'.php')) $CONFIG['lang'] = $USER['lang'];
} else {
	unset($USER['lang']);
}

if (!file_exists("modules/Coppermine/lang/{$CONFIG['lang']}.php")) $CONFIG['lang'] = 'english';
include "modules/Coppermine/lang/{$CONFIG['lang']}.php";


// load the main template
load_template();


function new_cache() {
global $cache;
    $CACHE_CONFIG['data_dir'] = "".$cache."/Modules/";
     $jpc = @opendir($CACHE_CONFIG['data_dir']);
        while($filename = @readdir($jpc)){
           if ($filename === '.' OR $filename === '..' OR $filename === '.htaccess' OR $filename === 'index.html') continue;
		if ( strstr($filename, 'CPG') ) {
             		 @unlink($CACHE_CONFIG['data_dir'].$filename);
		}
        }
}