<?php
$cfdef = array(
array(
'type' => 'main',
'bind' => 'conf',
'item' => array('login_timeout',
'def_checkout_days',
'min_passwd_length',
'min_login_length',
'range_op',
'maxrangeop',
'cartlimit',
'circu_chooseuserlimit',
'result_per_page',
'recent_return_window',
'itemno_fmt',
'itemno_igncp1',
'publish_checkoutby',
'image_resize_h',
'image_resize_w',
'image_jpeg_quality',
'zip',
'unzip',
'cksum',
'def_view',
'def_lang')
),
array(
'type' => 'view',
'bind' => 'viewconf',
'item' => array('Footer',
'banner_head')
),
array(
'type' => 'status_text',
'bind' => 'allstatus'
),
array(
'type' => 'series_lang',
'bind' => 'series_lang'
),
array(
'type' => 'chart',
'bind' => 'chart'
),
);
$bktbl = array(
'opt' => array("config"),
'inv' => array("series", "item", "image", "series_alias", "series_gp", "series_subj", "series_author"),
'mem' => array("user", "staff_gp", "patron_gp"),
'rec' => array("recommand", "recommand_archive", "feedback"),
'sts' => array("item_status"),
'log' => array("log", "log_item"),
);
$bktbl_preview = array(
'opt' => 1,
);
$tplsubs = array('index', 'adminindex');
///////////////////////////////////////////////////
require_once "odincom.php";
require_once "$ui_dir/uicom.inc";
require_once "$ui_dir/renderer.inc";
require_once "$ui_dir/dispcustomize.inc";
$script ="customize.php";
odin_staff_login();
// FIXME
$writeok = 1;
switch($_GET['step']) {
case "o": // submission of tpl data
// FIXME
//$writeok = check_staff_permission('siteconfig', 0);
$mainForm = $_GET['sv'];
update_config_tpl($tplsubs);
load_config_real();
break;
case "s": // submission of config data
$mainForm = $_GET['sv'];
update_config();
load_config_real();
break;
case "b": // request backup form
$mainForm = "backup";
break;
case "bs": // action save backup file
$mainForm = "backup";
save_config_set($_POST['setname'], $_POST['tb']);
break;
case "bd": // action del backup file
$mainForm = "backup";
del_config_set($_POST['setname'], $_POST['tb']);
break;
case "bl": // action load backup file
$mainForm = "backup";
load_config_set($_POST['setname'], $_POST['tb']);
break;
case "be": // action edit backup file
$mainForm = "backup";
edit_config_file($_POST['fname'], $_POST['tb'], $_POST['data']);
break;
default: // request option form
$mainForm = $_GET['sv'];
}
switch($mainForm) {
case "tpl": // request tpl form
$_SESSION['navbar']->register("template", $script."?sv=tpl&sub={$_GET['sub']}");
break;
case "backup":
$_SESSION['navbar']->register("backup", $script."?step=b&tb={$_GET['tb']}");
break;
default:
$_SESSION['navbar']->register("root_customize");
}
print_header();
switch($mainForm) {
case "tpl":
// FIXME
//$writeok = check_staff_permission('siteconfig', 0);
customize_form_one($_GET['sub'], $writeok);
break;
case "backup":
backup_form($_GET['tb']);
break;
default:
//$writeok = check_staff_permission('siteconfig', 0);
customize_form($writeok);
}
print_footer();
exit;
/////////////////////////////////////////////
function update_config_tpl($tplsubs) {
global $tplcfg;
// FIXME
//check_staff_permission('siteconfig', 0);
$tpl = $_POST['tpl'];
if (!is_array($tpl)) return;
foreach($tplsubs as $sub) {
if (array_key_exists($sub, $tpl)) {
$tpl[$sub] = preg_replace("/[\r\n]+/", "\n", $tpl[$sub]);
$tplcfg[$sub] = $tpl[$sub];
}
}
if (renew_config_type($tplcfg, 'template')) {
myresult(1, "Configuration saved");
}
}
/////////////////////////////////////////////
function update_config() {
global $cfdef;
check_staff_permission('siteconfig');
$anyerr = 0;
$query = array();
foreach ($cfdef as $a) {
$err = 0;
$bind = array();
$bindorig = $GLOBALS[$a['bind']];
$type = $a['type'];
if (array_key_exists("item", $a)) {
foreach ($a['item'] as $i) {
if (!isset($_POST[$type][$i])) {
if ($bindorig[$i] == "yes" || $bindorig[$i] == "no") {
// it's normal to find non-existance checkbox value
$bind[$i] = "no";
} else {
myresult(0, "Data not found type=$type key=$i");
$err = 1;
}
} else {
$bind[$i] = $_POST[$type][$i];
}
}
}
else {
if (!is_array($_POST[$type])) {
myresult(0, "Data not found type=$type");
$err = 1;
}
else foreach ($_POST[$type] as $x) {
if (!isset($x['key']) || empty($x['key'])) continue;
$bind[$x['key']] = $x['val'];
}
}
//print_r($bind);
if (!$err) {
if (!renew_config_type($bind, $type)) {
$anyerr = 1;
}
}
}
if (!$anyerr) {
/*foreach ($cfdef as $a) {
renew_config_type(${$a['bind']}, $a['type']);
}*/
myresult(1, "Configuration saved");
}
}
/////////////////////////////////////////////
function save_config_set($fname, $tb) {
global $bktbl;
if (!$tb) $tb = 'opt';
if (!is_array($bktbl[$tb])) {
myresult(0, "Unknown table set");
return;
}
if (!$fname || preg_match('/[\s.\/]/',$fname)) {
myresult(0, "Please enter proper file name");
return;
}
foreach($bktbl[$tb] as $tbl) {
$fullname = DUMP_DIR . "odin_$tbl.$fname.csv";
$fp = fopen($fullname, 'w');
if (!$fp) {
myresult(0, "Cannot open file $fullname");
return;
}
$query = "SELECT * FROM odin_$tbl";
if ($result = mysql_query($query)) {
$row = array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$row = array_map(addslashes, $row);
$line = implode('","', $row);
fwrite($fp, "\"$line\"\n");
}
mysql_free_result($result);
} else {
print ("Cannot load odin_$tbl : ".mysql_error());
return;
}
fclose($fp);
$ziplist[] = "odin_$tbl.$fname.csv";
}
if (count($bktbl[$tb]) > 1 && ($zipcmd = $GLOBALS['conf']['zip'])) {
// zip files
//myresult(1, "cd ".DUMP_DIR." && $zipcmd ".join(' ', $ziplist)." $ziplist");
$out = shell_exec("cd ".DUMP_DIR." && $zipcmd odin_$tb.$fname.zip ".join(' ', $ziplist));
foreach ($ziplist as $f) {
unlink (DUMP_DIR . $f);
}
}
myresult(1, "Data saved as : $fname");
return 1;
}
/////////////////////////////////////////////
function del_config_set($fname, $tb) {
global $bktbl;
if (!$fname || preg_match('/[\s.\/]/',$fname)) {
myresult(0, "Please enter proper file name");
return;
}
if (count($bktbl[$tb]) > 1 && $GLOBALS['conf']['zip']) {
$fullname = DUMP_DIR . "odin_$tb.$fname.zip";
} else {
$fullname = DUMP_DIR . "odin_{$bktbl[$tb][0]}.$fname.csv";
}
if (unlink($fullname)) {
myresult(1, "File $fullname deleted.");
return 1;
} else {
myresult(0, "Cannot unlink file $fullname.");
return;
}
}
/////////////////////////////////////////////
function load_config_set($fname, $tb) {
global $bktbl;
if (!is_array($bktbl[$tb])) {
myresult(0, "Unknown table set");
return;
}
if (!$fname || preg_match('/[\s.\/]/',$fname)) {
myresult(0, "Please enter proper file name");
return;
}
// unzip files
if (count($bktbl[$tb]) > 1 && ($unzipcmd = $GLOBALS['conf']['unzip'])) {
$dirhash = uniqid("");
$csvdir = DUMP_DIR . $dirhash . "/";
mkdir ($csvdir) or myresult(0, "Cannot mkdir $csvdir");
$out = shell_exec("cd $csvdir && $unzipcmd ../odin_$tb.$fname.zip");
//print("cd $csvdir && $unzipcmd ../odin_$tb.$fname.zip");
//print $out;
}
else {
$csvdir = DUMP_DIR;
}
odin_connect();
$insert_error = 0;
foreach($bktbl[$tb] as $tbl) {
$fullname = $csvdir . "odin_$tbl.$fname.csv";
if (!$fp = fopen($fullname, "r")) {
myresult(0, "Cannot open file: $fullname");
$insert_error = 1;
continue;
}
//initialize tables
if (!mysql_query("DROP TABLE IF EXISTS odin_temp_$tbl")) {
myresult(0,"Cannot drop odin_temp_$tbl : ".mysql_error());
$insert_error = 1;
continue;
}
if (!mysql_query("CREATE TABLE odin_temp_$tbl LIKE odin_$tbl")) {
myresult(0,"Cannot create odin_temp_$tbl : ".mysql_error());
$insert_error = 1;
continue;
}
//$header = fgets($fp);
while ($row = fgetcsv($fp, 10240)) {
$row = array_map(stripslashes, $row);
$row = array_map(mysql_escape_string, $row);
$line = implode("','", $row);
$query = "INSERT INTO odin_temp_$tbl VALUES ('$line')";
//print "$query<br>";
if (mysql_query($query) && (mysql_affected_rows() == 1)) {
// insert ok
} else {
myresult (0, "Insert odin_temp_$tbl failed : " . mysql_error() . "<br>$query");
// unlikely error. something must be wrong. quiting
$insert_error = 1;
}
}
}
if ($insert_error) {
myresult(0, "Load operation abort due to error");
if ($dirhash) $out = shell_exec("rm -Rf " . DUMP_DIR . $dirhash);
return;
}
//rename tables
foreach($bktbl[$tb] as $tbl) {
if (!mysql_query("DROP TABLE IF EXISTS odin_$tbl_loadbkup")) {
myresult(0, mysql_error());
return;
}
if (!mysql_query("RENAME TABLE odin_$tbl TO odin_$tbl_loadbkup")) {
myresult(0, mysql_error());
return;
}
if (!mysql_query("RENAME TABLE odin_temp_$tbl TO odin_$tbl")) {
myresult(0, mysql_error());
return;
}
myresult(1, "Table odin_$tbl load OK");
}
if ($dirhash) $out = shell_exec("rm -Rf " . DUMP_DIR . $dirhash);
myresult(1, "Set $tb load successfully completed");
}
/////////////////////////////////////////////
function edit_config_file($fname, $tb, $data) {
global $bktbl;
if (!$fname || preg_match('/[\s.\/]/',$fname)) {
myresult(0, "Please enter proper file name");
return;
}
$tbl = $bktbl[$tb][0];
$fullname = DUMP_DIR . "odin_$tbl." .$fname. ".csv";
if (!file_exists($fullname)) {
myresult(0, "File not exists : $fullname");
return;
}
if ($fp = fopen($fullname, 'w')) {
//$data = preg_replace("/[\r\n]+/", "\n", $data);
fwrite($fp, $data);
fclose($fp);
myresult(1, "File updated");
return 1;
}
}
?>