<?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                                     */
/************************************************************************/

define('IN_COPPERMINE', true);
define('CONFIG_PHP', true);

defined('MAXIMUS') or require_once("mainfile.php");

if ( !defined('ADMIN_ALLOW') ) {
	redirect_url('modules.php?name=Coppermine');
}

$pagetitle = "- Coppermine";
incoh();
require('modules/Coppermine/include/init.inc.php');
require('modules/Coppermine/include/sql_parse.php');


if (!GALLERY_ADMIN_MODE) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

function form_label($text)
{
	echo <<<EOT
	<tr>
		<td class="tableh2" colspan="2">
			<b>$text</b>
		</td>
	</tr>

EOT;
}

function form_input($text, $name)
{
	global $CONFIG;

	$value = $CONFIG[$name];

	echo <<<EOT
	<tr>
    	<td width="60%" class="tableb">
			$text
        </td>
        <td width="40%" class="tableb" valign="top">
        	<input type="text" class="textinput" style="width: 100%" name="$name" value="$value" />
		</td>
	</tr>

EOT;
}

function form_yes_no($text, $name)
{
	global $CONFIG, $lang_yes, $lang_no;

	$value = $CONFIG[$name];
	$yes_selected = $value ? 'selected' : '';
	$no_selected  = !$value ? 'selected' : '';

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$name" class="listbox">
				<option value="1" $yes_selected>$lang_yes</option>
				<option value="0" $no_selected>$lang_no</option>
			</select>
		</td>
	</tr>

EOT;
}

function form_img_pkg($text, $name)
{
	global $CONFIG;

	$value = $CONFIG[$name];
	$im_selected   = ($value == 'im')  ? 'selected' : '';
	$gd1_selected  = ($value == 'gd1') ? 'selected' : '';
	$gd2_selected  = ($value == 'gd2') ? 'selected' : '';

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$name" class="listbox">
				<option value="im" $im_selected>Image Magick</option>
				<option value="gd1" $gd1_selected>GD version 1.x</option>
				<option value="gd2" $gd2_selected>GD version 2.x</option>
			</select>
		</td>
	</tr>

EOT;
}

function form_sort_order($text, $name)
{
	global $CONFIG, $lang_config_php;

	$value = $CONFIG[$name];
	$na_selected  = ($value == 'na')  ? 'selected' : '';
	$nd_selected  = ($value == 'nd') ? 'selected' : '';
	$da_selected  = ($value == 'da') ? 'selected' : '';
	$dd_selected  = ($value == 'dd') ? 'selected' : '';

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$name" class="listbox">
				<option value="na" $na_selected>{$lang_config_php['name_a']}</option>
				<option value="nd" $nd_selected>{$lang_config_php['name_d']}</option>
				<option value="da" $da_selected>{$lang_config_php['date_a']}</option>
				<option value="dd" $dd_selected>{$lang_config_php['date_d']}</option>
			</select>
		</td>
	</tr>

EOT;
}

function form_charset($text, $name)
{
	global $CONFIG;

	$charsets=array(
		'Default' => 'language file',
		'Arabic' => 'iso-8859-6',
		'Baltic' => 'iso-8859-4',
		'Central European' => 'iso-8859-2',
		'Chinese Simplified' => 'euc-cn',
		'Chinese Traditional' => 'big5',
		'Cyrillic' => 'koi8-r',
		'Greek' => 'iso-8859-7',
		'Hebrew' => 'iso-8859-8-i',
		'Icelandic' => 'x-mac-icelandic',
		'Japanese' => 'euc-jp',
		'Korean' => 'euc-kr',
		'Maltese' => 'iso-8859-3',
		'Thai' => 'windows-874 ',
		'Turkish' => 'iso-8859-9',
		'Unicode' => 'utf-8',
		'Vietnamese' => 'windows-1258',
		'Western' => 'iso-8859-1'
	);

	$value = strtolower($CONFIG[$name]);

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$name" class="listbox">

EOT;
	foreach ($charsets as $country => $charset){
		echo "				<option value=\"$charset\" ". ($value == $charset ? 'selected' : '').">$country ($charset)</option>\n";
	}
	echo <<<EOT
			</select>
		</td>
	</tr>

EOT;
}

function form_language($text, $name)
{
	global $CONFIG;

	$value = strtolower($CONFIG[$name]);
	$lang_dir = 'modules/Coppermine/lang/';

	$dir = opendir($lang_dir);
	while($file = readdir($dir)){
		if(is_file($lang_dir.$file) && strtolower(substr($file, -4)) == '.php') {
			$lang_array[] = strtolower(substr($file,0 , -4));
		}
	}
	closedir($dir);

	natcasesort($lang_array);

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$name" class="listbox">

EOT;
	foreach ($lang_array as $language){
		echo "				<option value=\"$language\" ". ($value == $language ? 'selected' : '').">".ucfirst($language)."</option>\n";
	}
	echo <<<EOT
			</select>
		</td>
	</tr>

EOT;
}

function form_theme($text, $name)
{
	global $CONFIG;

	$value = $CONFIG[$name];
	$theme_dir = 'modules/Coppermine/themes/';

	$dir = opendir($theme_dir);
	while($file = readdir($dir)){
		if(is_dir($theme_dir.$file) && $file != "." && $file != "..") {
			$theme_array[] = $file;
		}
	}
	closedir($dir);

	natcasesort($theme_array);

	echo <<<EOT
	<tr>
    	<td class="tableb">
			$text
        </td>
        <td class="tableb" valign="top">
			<select name="$name" class="listbox">

EOT;
	foreach ($theme_array as $theme){
		echo "<option value=\"$theme\" ". ($value == $theme ? 'selected' : '').">".strtr(ucfirst($theme),'_', ' ')."</option>\n";
	}
	echo <<<EOT
			</select>
		</td>
	</tr>

EOT;
}

function create_form(&$data)
{
	foreach($data as $element){
		if ((is_array($element))) {
		    switch($element[2]){
		    	case 0 :
		    		form_input($element[0], $element[1]);
		    		break;
		    	case 1 :
		    		form_yes_no($element[0], $element[1]);
		    		break;
		    	case 2 :
		    		form_img_pkg($element[0], $element[1]);
		    		break;
		    	case 3 :
		    		form_sort_order($element[0], $element[1]);
		    		break;
		    	case 4 :
		    		form_charset($element[0], $element[1]);
		    		break;
		    	case 5 :
		    		form_language($element[0], $element[1]);
		    		break;
		    	case 6 :
		    		form_theme($element[0], $element[1]);
		    		break;
		    	default:
					die('Invalid action');
		    } // switch
		} else {
			form_label($element);
		}
	}
}


if (count($HTTP_POST_VARS) > 0) {

	if (isset($HTTP_POST_VARS['update_config'])) {
	$need_to_be_positive = array(
		'albums_per_page',
		'album_list_cols',
		'max_tabs',
		'picture_width',
		'subcat_level',
		'thumb_width',
		'thumbcols',
		'thumbrows');

	foreach ($need_to_be_positive as $parameter)
		$HTTP_POST_VARS[$parameter] = max(1,(int)$HTTP_POST_VARS[$parameter]);

	foreach($lang_config_data as $element){
		if ((is_array($element))) {
			if ((!isset($HTTP_POST_VARS[$element[1]]))) cpg_die(CRITICAL_ERROR, "Missing config value for '{$element[1]}'", __FILE__, __LINE__);
			$value = addslashes($HTTP_POST_VARS[$element[1]]);
			$GLOBALS['db']->sql_query("UPDATE `".TABLE_CPG_CONFIG."` SET  value = '$value' WHERE name = '{$element[1]}'");
		}
	}
	pageheader($lang_config_php['title']);
        msg_box($lang_config_php['info'], $lang_config_php['upd_success'], $lang_continue, 'modules.php?name=Coppermine&amp;op=config');
	$file = @fopen("".$cache."/Configuration/".$cache_prefix."_coppermine.php", "w");
	$content  = "";
	$content .= "<?php\n defined(\"MAXIMUS\") or  Header(\"Location: ../index.php\"); \n";
	$content .= "\n";
	$content .= "define( 'CACHE_ACOPPERMINE', '".secure($HTTP_POST_VARS['retention_admin'], 'num', '1')."' );\n";
	$content .= "define( 'CACHE_UCOPPERMINE', '".secure($HTTP_POST_VARS['retention_user'], 'num', '1')."' );\n";
	$content .= "define( 'CACHE_VCOPPERMINE', '".secure($HTTP_POST_VARS['retention_visit'], 'num', '1')."' );\n";
	@fwrite($file, $content);
	@fclose($file);
	new_cache();
	pagefooter();
	incof();
	exit;
	} elseif (isset($HTTP_POST_VARS['restore_config'])) {
		$default_config = 'sql/restore_config.sql';
		$sql_query = fread(fopen($default_config, 'r'), filesize($default_config));
		$sql_query = preg_replace('/CPG_/', $CONFIG['TABLE_PREFIX'], $sql_query);
		$sql_query = preg_replace('/INSERT /', 'REPLACE ', $sql_query);

		$sql_query = remove_remarks($sql_query);
		$sql_query = split_sql_file($sql_query, ';');

		$sql_count = count($sql_query);
		for($i = 0; $i < $sql_count; $i++) $GLOBALS['db']->sql_query($sql_query[$i]);
	}
	pageheader($lang_config_php['title']);
        msg_box($lang_config_php['info'], $lang_config_php['restore_success'], $lang_continue, 'modules.php?name=Coppermine&amp;op=config');
	$file = @fopen("".$cache."/Configuration/".$cache_prefix."_coppermine.php", "w");
	$content  = "";
	$content .= "<?php\n defined(\"MAXIMUS\") or  Header(\"Location: ../index.php\"); \n";
	$content .= "\n";
	$content .= "define( 'CACHE_ACOPPERMINE', '0' );\n";
	$content .= "define( 'CACHE_UCOPPERMINE', '0' );\n";
	$content .= "define( 'CACHE_VCOPPERMINE', '0' );\n";
	@fwrite($file, $content);
	@fclose($file);
	new_cache();
	pagefooter();
	incof();
	exit;
}

pageheader($lang_config_php['title']);
$signature = 'Coppermine Photo Gallery '.COPPERMINE_VERSION;
starttable('100%');
echo <<<EOT
<tr>
    <form action="modules.php?name=Coppermine&amp;op=config" method="post">
	<td class="tableh1" colspan="2"><h2>{$lang_config_php['title']} - $signature</h2></td>
</tr>

EOT;
create_form($lang_config_data);

$selected0 = $selected1 = $selected2 = $selected3 = $selected4 = $selected5 = '';
if ( CACHE_ACOPPERMINE == 2419200 ) {
	$selected5 = 'selected';
} elseif ( CACHE_ACOPPERMINE == 604800 ) {
	$selected4 = 'selected';
} elseif ( CACHE_ACOPPERMINE == 86400 ) {
	$selected3 = 'selected';
} elseif ( CACHE_ACOPPERMINE == 43200 ) {
	$selected2 = 'selected';
} elseif ( CACHE_ACOPPERMINE == 21600 ) {
	$selected1 = 'selected';
} else {
	$selected0 = 'selected';
}

echo <<<EOT
	<tr>
		<td class="tableh2" colspan="2">
			<b>{$lang_config_php['paramsystem']}</b>
		</td>
	</tr>
	<tr>
    	<td class="tableb">
			{$lang_config_php['cacheadmin']}
        </td>
        <td class="tableb" valign="top">
			<select name="retention_admin" class="listbox">
				<option value="0" $selected0>{$lang_config_php['realtime']}</option>
				<option value="21600" $selected1>{$lang_config_php['sixhours']}</option>
				<option value="43200" $selected2>{$lang_config_php['douzehours']}</option>
				<option value="86400" $selected3>{$lang_config_php['oneday']}</option>
				<option value="604800" $selected4>{$lang_config_php['oneweek']}</option>
				<option value="2419200" $selected5>{$lang_config_php['onemonth']}</option>
			</select>
		</td>
	</tr>
EOT;
$selected0 = $selected1 = $selected2 = $selected3 = $selected4 = $selected5 = '';
if ( CACHE_UCOPPERMINE == 2419200 ) {
	$selected5 = 'selected';
} elseif ( CACHE_UCOPPERMINE == 604800 ) {
	$selected4 = 'selected';
} elseif ( CACHE_UCOPPERMINE == 86400 ) {
	$selected3 = 'selected';
} elseif ( CACHE_UCOPPERMINE == 43200 ) {
	$selected2 = 'selected';
} elseif ( CACHE_UCOPPERMINE == 21600 ) {
	$selected1 = 'selected';
} else {
	$selected0 = 'selected';
}

echo <<<EOT
	<tr>
    	<td class="tableb">
			{$lang_config_php['cacheusers']}
        </td>
        <td class="tableb" valign="top">
			<select name="retention_user" class="listbox">
				<option value="0" $selected0>{$lang_config_php['realtime']}</option>
				<option value="21600" $selected1>{$lang_config_php['sixhours']}</option>
				<option value="43200" $selected2>{$lang_config_php['douzehours']}</option>
				<option value="86400" $selected3>{$lang_config_php['oneday']}</option>
				<option value="604800" $selected4>{$lang_config_php['oneweek']}</option>
				<option value="2419200" $selected5>{$lang_config_php['onemonth']}</option>
			</select>
		</td>
	</tr>

EOT;

$selected0 = $selected1 = $selected2 = $selected3 = $selected4 = $selected5 = '';
if ( CACHE_VCOPPERMINE == 2419200 ) {
	$selected5 = 'selected';
} elseif ( CACHE_VCOPPERMINE == 604800 ) {
	$selected4 = 'selected';
} elseif ( CACHE_VCOPPERMINE == 86400 ) {
	$selected3 = 'selected';
} elseif ( CACHE_VCOPPERMINE == 43200 ) {
	$selected2 = 'selected';
} elseif ( CACHE_VCOPPERMINE == 21600 ) {
	$selected1 = 'selected';
} else {
	$selected0 = 'selected';
}

echo <<<EOT

	<tr>
    	<td class="tableb">
			{$lang_config_php['cachevisitors']}
        </td>
        <td class="tableb" valign="top">
			<select name="retention_visit" class="listbox">
				<option value="0" $selected0>{$lang_config_php['realtime']}</option>
				<option value="21600" $selected1>{$lang_config_php['sixhours']}</option>
				<option value="43200" $selected2>{$lang_config_php['douzehours']}</option>
				<option value="86400" $selected3>{$lang_config_php['oneday']}</option>
				<option value="604800" $selected4>{$lang_config_php['oneweek']}</option>
				<option value="2419200" $selected5>{$lang_config_php['onemonth']}</option>
			</select>
		</td>
	</tr>

EOT;

echo <<<EOT
	<tr>
    	<td colspan="2" align="center" class="tablef">
			<input type="submit" class="button" name="update_config" value="{$lang_config_php['save_cfg']}" />
			&nbsp;&nbsp;
			<input type="submit" class="button" name="restore_config" value="{$lang_config_php['restore_cfg']}" />
		</td>
	</form>
	</tr>

EOT;
endtable();
pagefooter();
ob_end_flush();

incof();