<?php
/*************************
  Coppermine Photo Gallery
  ************************
  Copyright (c) 2003-2005 Coppermine Dev Team
  v1.1 originaly written by Gregory DEMAR

  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, or
  (at your option) any later version.
  ********************************************
  ********************************************
  This file is for the shop hack by casper, based on one by madeinhawaii
  ********************************************
  ********************************************
  Coppermine version: 1.4.3
  $Source: /cvsroot/coppermine/stable/admin.php,v $
  
  $Author: nibbler999 $
  $Date: 2005/12/15 17:47:24 $
**********************************************/

define('IN_COPPERMINE', true);
define('SHOP_PHP', true);
define('CONFIG_PHP', true); // added for backwards compatibility (language fallback)

require('include/init.inc.php');
require('include/sql_parse.php');

$lang_shop_data = isset($lang_config_data) ? $lang_config_data : $lang_shop_data;


if (!GALLERY_ADMIN_MODE) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

function form_label($text)
{
        global $lang_shop_php;
        global $sn1,$sn2,$sn3;

        static $cmi = 0;
        static $open = false;

        if ($sn1) echo '<tr><td colspan ="3" class="tableb_compact"><a name="notice1"></a>'.$lang_shop_php['notice1'].'</td></tr>';
        if ($sn2) echo '<tr><td colspan ="3" class="tableb_compact"><a name="notice2"></a>'.$lang_shop_php['notice2'].'</td></tr>';
        if ($sn3) echo '<tr><td colspan ="3" class="tableb_compact"><a name="notice3"></a>'.$lang_shop_php['notice3'].'</td></tr>';

        $sn1 = $sn2 = $sn3 = 0;

        if ($open){
        echo <<< EOT
                                </table>
                        </td>
                </tr>
EOT;
        }
        echo <<< EOT
                <tr>
                        <td class="tableh2" colspan="3" onclick="show_section('section{$cmi}')">
                                <span style="cursor:pointer"><img src="images/descending.gif" border="0" width="9" height="9" alt="" title="{$lang_shop_php['click_expand']}" /> <b>$text</b></span>
                        </td>
                </tr>
                <tr>
                        <td>
                                <table align="center" width="100%" cellspacing="1" cellpadding="0" class="maintable" id="section{$cmi}" border="0">
EOT;

        $open = true;
        $cmi++;
}

function form_input($text, $name, $help = '')
{
    global $CONFIG;

    $value = $CONFIG[$name];
    $help = cpg_display_help($help);

    $type = ($name == 'smtp_password') ? 'password' : 'text';


    echo <<<EOT
                <tr>
                        <td width="60%" class="tableb">
                                $text
                        </td>
                        <td width="50%" class="tableb" valign="top">
                            <input type="$type" class="textinput" maxlength="255" style="width: 100%" name="$name" value="$value"/>
                        </td>
                        <td class="tableb" width="10%">
                                $help
                        </td>
        </tr>

EOT;
}




function create_form(&$data)
{
        global $sn1, $sn2, $sn3, $options_to_disable, $CONFIG;

    foreach($data as $element) {
        if ((is_array($element))) {
                $element[3] = (isset($element[3])) ? $element[3] : '';
                if (UDB_INTEGRATION != 'coppermine' AND in_array($element[1],$options_to_disable) AND $CONFIG['bridge_enable']) $element[2] = 15;
                
            switch ($element[2]) {
                case 0 :
                    form_input($element[0], $element[1], $element[3]);
                    break;
                
                default:
                    die('Invalid action');
            } // switch
        } else {
                form_label($element);
        }
    }
}
if (count($_POST) > 0) {
    if (isset($_POST['update_config'])) {
        $need_to_be_positive = array('albums_per_page',
            'album_list_cols',
            'max_tabs',
            'picture_width',
            'subcat_level',
            'thumb_width',
            'thumbcols',
            'thumbrows',
            // Show filmstrip
            'max_film_strip_items');

        

        foreach ($need_to_be_positive as $parameter)
        $_POST[$parameter] = max(1, (int)$_POST[$parameter]);

        foreach($lang_shop_data as $element) {
            if ((is_array($element))) {
                if (!isset($_POST[$element[1]])) /*cpg_die(CRITICAL_ERROR, "Missing admin value for '{$element[1]}'", __FILE__, __LINE__);*/ continue;
                $value = addslashes($_POST[$element[1]]);
                if ($element[1] == 'ecards_more_pic_target' && substr($value, -1, 1) != '/') $value .= '/';
                if ($CONFIG[$element[1]] !== stripslashes($value))
                     {
                        cpg_db_query("UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '$value' WHERE name = '{$element[1]}'");
                                                if ($element[1]=='enable_encrypted_passwords' && $value) {
                                                        cpg_db_query("update {$CONFIG['TABLE_USERS']} set user_password=md5(user_password);");
                                                }
                        if ($CONFIG['log_mode'] == CPG_LOG_ALL) {
                                log_write('CONFIG UPDATE SQL: '.
                                          "UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '$value' WHERE name = '{$element[1]}'\n".
                                          'TIME: '.date("F j, Y, g:i a")."\n".
                                          'USER: '.$USER_DATA['user_name'],
                                          CPG_DATABASE_LOG
                                          );
                        }
                }
            }
        }
        pageheader($lang_shop_php['title']);
        msg_box($lang_shop_php['info'], $lang_shop_php['upd_success'], $lang_continue, 'index.php');

    } elseif (isset($_POST['restore_config'])) {
        $default_config = 'sql/basic.sql';
        $sql_query = fread(fopen($default_config, 'r'), filesize($default_config));
        $sql_query = preg_replace('/CPG_/', $CONFIG['TABLE_PREFIX'], $sql_query);
        cpg_db_query("TRUNCATE TABLE {$CONFIG['TABLE_CONFIG']}");
        cpg_db_query("TRUNCATE TABLE {$CONFIG['TABLE_FILETYPES']}");
        $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++) if (strpos($sql_query[$i],'config VALUES') || strpos($sql_query[$i],'filetypes VALUES')) cpg_db_query($sql_query[$i]);
		$f= cpg_db_query("UPDATE {$CONFIG['TABLE_CONFIG']} SET value = '{$CONFIG['enable_encrypted_passwords']}' WHERE name = 'enable_encrypted_passwords'");
		var_dump($f);
		var_dump(mysql_affected_rows());
		pageheader($lang_shop_php['title']);
            //msg_box($lang_shop_php['info'], $lang_shop_php['restore_success'], $lang_continue, $PHP_SELF);
            msg_box($lang_shop_php['info'], $lang_shop_php['restore_success'], $lang_continue, $_SERVER['PHP_SELF']);
    }
        pagefooter();
        exit;
}

pageheader($lang_shop_php['title']);

$signature = 'Coppermine Photo Gallery ';

?>
<script type="text/javascript">
        onload = hideall;
</script>
<?php
//echo "<form action=\"$PHP_SELF\" method=\"post\">";
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
starttable('100%', "{$lang_shop_php['title']} - $signature", 3);
echo <<<EOT
    <tr>
        <td class="tableh2" colspan="3">
            <a href="javascript:expand();" class="admin_menu">{$lang_shop_php['expand_all']}&nbsp;&nbsp;<img src="images/descending.gif" width="9" height="9" border="0" alt="" title="{$lang_shop_php['expand_all']}" /></a>
        </td>
    </tr>
EOT;

create_form($lang_shop_data);

        if ($sn1) echo '<tr><td colspan ="3" class="tableb_compact"><a name="notice1"></a>'.$lang_shop_php['notice1'].'</td></tr>';
        if ($sn2) echo '<tr><td colspan ="3" class="tableb_compact"><a name="notice2"></a>'.$lang_shop_php['notice2'].'</td></tr>';
        if ($sn3) echo '<tr><td colspan ="3" class="tableb_compact"><a name="notice3"></a>'.$lang_shop_php['notice3'].'</td></tr>';

echo '</table></td></tr>';

echo <<<EOT
                <tr>
                        <td align="left" class="tablef">
                            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                                <tr>
                                    <td width="33%">
                                        <a href="javascript:expand();" class="admin_menu">{$lang_shop_php['expand_all']}<img src="images/ascending.gif" width="9" height="9" border="0" alt="" title="{$lang_shop_php['expand_all']}" /></a>
                                    </td>
                                    <td width="67%" align="center">
                                        <input type="submit" class="button" name="update_config" value="{$lang_shop_php['save_cfg']}" />
                                &nbsp;&nbsp;
										
                                    </td>
                                </tr>
                            </table>
                        </td>
                </tr>
EOT;
endtable();
echo '</form>';
pagefooter();
ob_end_flush();
?>
