<?php
/*
 +--------------------------------------------------------------------------+
 | phpMyBackupPro                                                           |
 +--------------------------------------------------------------------------+
 | Copyright (c) 2004-2007 by Dirk Randhahn                                 |                               
 | http://www.phpMyBackupPro.net                                            |
 | version information can be found in definitions.php.                     |
 |                                                                          |
 | 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 program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
 | GNU General Public License for more details.                             |
 |                                                                          |
 | You should have received a copy of the GNU General Public License        |
 | along with this program; if not, write to the Free Software              |
 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.|
 +--------------------------------------------------------------------------+
*/


require_once("login.php");

// items which are checkboxes and the config page basic(1) or ext(2)
$checkbox=array("ftp_use"=>1,"ftp_pasv"=>1,"email_use"=>1,"import_error"=>2,"no_login"=>2,"ftp_del"=>1,"dir_backup"=>2,"dir_rec"=>2,"login"=>2);

// check if all configuration settings are available
// login, stylesheet and lang are checked before
if (!isset($CONF['sitename'])) $CONF['sitename']='';
if (!isset($CONF['ftp_use'])) $CONF['ftp_use']='';
if (!isset($CONF['ftp_server'])) $CONF['ftp_server']='';
if (!isset($CONF['ftp_user'])) $CONF['ftp_user']='';
if (!isset($CONF['sql_passwd'])) $CONF['sql_passwd']='';
if (!isset($CONF['sql_host'])) $CONF['sql_host']='';
if (!isset($CONF['sql_user'])) $CONF['sql_user']='';
if (!isset($CONF['sql_db'])) $CONF['sql_db']='';
if (!isset($CONF['ftp_passwd'])) $CONF['ftp_passwd']='';
if (!isset($CONF['ftp_path'])) $CONF['ftp_path']='';
if (!isset($CONF['ftp_pasv'])) $CONF['ftp_pasv']='';
if (!isset($CONF['ftp_port'])) $CONF['ftp_port']='';
if (!isset($CONF['ftp_del'])) $CONF['ftp_del']='';
if (!isset($CONF['email_use'])) $CONF['email_use']='';
if (!isset($CONF['email'])) $CONF['email']='';
if (!isset($CONF['date'])) $CONF['date']='';
if (!isset($CONF['del_time'])) $CONF['del_time']='';
if (!isset($CONF['del_number'])) $CONF['del_number']='';
if (!isset($CONF['timelimit'])) $CONF['timelimit']='';
if (!isset($CONF['confirm'])) $CONF['confirm']='';
if (!isset($CONF['import_error'])) $CONF['import_error']='';
if (!isset($CONF['no_login'])) $CONF['no_loginxxxx']='';
if (!isset($CONF['dir_backup'])) $CONF['dir_backup']='';
if (!isset($CONF['dir_rec'])) $CONF['dir_rec']='';
    
// if save button was clicked
if (isset($_POST['submit'])) {

 		
        // update $CONF
        foreach($CONF as $item=>$value) {
            // don't save settings for several servers in conf.php
            if ($item=="sql_host_s" || $item=="sql_user_s" || $item=="sql_passwd_s" || $item=="sql_db_s") continue;
            
            // don't save data of settings the user isn't allowed to change
            if ($_SESSION['multi_user_mode']) {
                if ($item=="sitename" && $PMBP_MU_CONF['sitename']) continue;
                if (($item=="email_use"|$item=="email") && !$PMBP_MU_CONF['allow_email']) continue;
                
                if (!$PMBP_MU_CONF['allow_ftp'] && !$PMBP_MU_CONF['allow_dir_backup']) {
                    if ($item=="ftp_server") continue;
                    if ($item=="ftp_user") continue;
                    if ($item=="ftp_passwd") continue;
                    if ($item=="ftp_path") continue;
                    if ($item=="ftp_pasv") continue;
                    if ($item=="ftp_port") continue;
                    if ($item=="ftp_use") continue;
                    if ($item=="dir_backup") continue;
                    if ($item=="dir_rec") continue;
                }
                if ($PMBP_MU_CONF['allow_dir_backup']) {
                    if ($item=="dir_backup") continue;
                    if ($item=="dir_rec") continue;
                }
                if ($PMBP_MU_CONF['allow_ftp']) {
                    if ($item=="ftp_use") continue;
                }                
            }
            
            // check if the value was posted
            if (isset($_POST[$item])) {
                if (isset($checkbox[$item])) {
                    $CONF[$item]=1;
                } else {
                    $CONF[$item]=$_POST[$item];
                }
            }
        }       
               
    // save $CONF to global_conf.php
    if (PMBP_save_global_conf()) {
        $out="<div class=\"green\">".C_SAVED."!</div>\n";
    } else {
        $out="<div class=\"red\">".PMBP_GLOBAL_CONF." ".C_WRITEABLE."!</div>\n";
    }
}


PMBP_print_header(ereg_replace(".*/","",$_SERVER['SCRIPT_NAME']));
if (isset($out)) echo $out;

// validation type of some variables
$validate=array('timelimit'=>"int",'del_time'=>"float",'email'=>"email",'ftp_port'=>"int");
$conf=array(C_TIMELIMIT=>"timelimit",C_FTP_PORT=>"ftp_port",C_DEL_TIME=>"del_time",C_EMAIL=>"email");

// validate
foreach($CONF as $key=>$value) {
    if (array_key_exists($key,$validate)) {
        switch($validate[$key]) {
            case "int": if (!eregi("^[0-9]*$",$value)) echo "<div class=\"red\">'".array_search($key,$conf)."' ".C_WRONG_TYPE."</div>\n";
                break;
            case "float": if (!eregi("^[0-9]*\.?[0-9]*$",$value)) echo "<div class=\"red\">'".array_search($key,$conf)."' ".C_WRONG_TYPE."</div>\n";
                break;
            case "email": if ($value||$CONF['email_use']) {
                    foreach(explode(",",$value) as $value2) {
                        if (!eregi("^\ *[äöüÄÖÜa-zA-Z0-9_-]+(\.[äöüÄÖÜa-zA-Z0-9\._-]+)*@([äöüÄÖÜa-zA-Z0-9-]+\.)+([a-z]{2,4})$",$value2)) {
                            echo "<div class=\"red\">'".array_search($key,$conf)."' ".C_WRONG_TYPE."</div>\n";
                            break;
                        }
                    }
                }
                break;
        }
    }
}

// if no db connection possible
if (isset($_SESSION['sql_host_org'])) {
    if (!@mysql_connect($_SESSION['sql_host_org'],$_SESSION['sql_user_org'],$_SESSION['sql_passwd_org'])) echo "<div class=\"red\">".C_WRONG_SQL."</div>";     
    if ($_SESSION['sql_db_org']) if (!@mysql_select_db($_SESSION['sql_db_org'])) echo "<div class=\"red\">".C_WRONG_DB."</div>";    
} else {
    if (!@mysql_connect($CONF['sql_host'],$CONF['sql_user'],$CONF['sql_passwd'])) echo "<div class=\"red\">".C_WRONG_SQL."</div>";
    if ($CONF['sql_db']) if (!@mysql_select_db($CONF['sql_db'])) echo "<div class=\"red\">".C_WRONG_DB."</div>";
}

// only if 'good internet connection' and if no ftp connection possible
if ($CONF['ftp_use'] || $CONF['dir_backup']) {
	
    if (!$CONF['ftp_server']) {
        echo "<div class=\"red\">".C_WRONG_FTP."</div>";
    // check only if internet connection seems to be good
    } elseif(!$_SESSION['PMBP_VERSION']) {
        if (!$conn_id=@ftp_connect($CONF['ftp_server'],$CONF['ftp_port'],$PMBP_SYS_VAR['ftp_timeout']))
            echo "<div class=\"red\">".C_WRONG_FTP."</div>";
    }
}

    echo SQ_SELECT_DB.": ";
	$db = PMBP_get_db_list();
    echo '<span class="red_left">'.inGenioX_str_antes_resto($db[0],INGENIO_PREFIJO_DB,2).'</span>';
    echo '<br><br>';
	
	//echo "<form action=\"config.php\" method=\"post\">\n";
	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";
	echo '<tr>';
	echo "<form action=\"config.php\" method=\"post\">\n";

	echo '<td valign="top">';
	
	echo C_LANG.': '.PMBP_config_print("lang");
	echo '<br><br>';
	echo C_DATE.': '.PMBP_config_print("date");
	echo '<br><br>';
	echo C_STYLESHEET.': '.PMBP_config_print("stylesheet");
	echo '<br><br>';
	echo '<div class="div_boton"><input name="submit" type="submit" value="'.C_SAVE.'" class="button"></div>';	
	
	echo '</td></tr></form></table>';

PMBP_print_footer();

// two functions only used in config.php

// returns the html code for the different configuration items and controls
function PMBP_config_print($item) {
    global $CONF;
    
    // vars whos type is checkbox, password, select or a filelist
    global $checkbox;
    $password=array("sql_passwd","ftp_passwd");
    $select['confirm']=array(C_CONFIRM_1,C_CONFIRM_2,C_CONFIRM_3,C_CONFIRM_4);
    $time=time();
    $select['date']=array(BD_DATE_FORMAT=>strftime(BD_DATE_FORMAT,$time),
        "%x %X"=>strftime("%x %X",$time),"%x %H:%M"=>strftime("%x %H:%M",$time),
        "%m/%d/%y %X"=>strftime("%m/%d/%y %X",$time),"%m/%d/%y %H:%M"=>strftime("%m/%d/%y %H:%M",$time),
        "%b %d %Y %X"=>strftime("%b %d %Y %X",$time),"%b %d %Y %H:%M"=>strftime("%b %d %Y %H:%M",$time),
        "%Y/%m/%d %X"=>strftime("%Y/%m/%d %X",$time),"%Y/%m/%d %H:%M"=>strftime("%Y/%m/%d %H:%M",$time));
    $filelist['lang']=array(PMBP_LANGUAGE_DIR,".inc.php");
    $filelist['stylesheet']=array(PMBP_STYLESHEET_DIR,".css");

    // create the html code
    if (isset($checkbox[$item])) {
        if ($CONF[$item]) $status=" checked"; else $status=" ";        
        $out="<input type=\"checkbox\" name=\"".$item."\" value=\"".$CONF[$item]."\"".$status.PMBP_config_disable($item).">";
    } elseif (in_array($item,$password)) {
        $out="<input type=\"password\" size=\"10\" name=\"".$item."\" value=\"".$CONF[$item]."\"".PMBP_config_disable($item).">";
    } elseif (isset($select[$item])) {
        $out="<select name=\"".$item."\"".PMBP_config_disable($item).">\n";
        foreach($select[$item] as $opt_no=>$print_value)
            if ($opt_no==$CONF[$item]) $out.="<option value=\"".$opt_no."\" selected>".$print_value."</option>\n";
                else $out.="<option value=\"".$opt_no."\">".$print_value."</option>\n";
        $out.="</select>";
    } elseif(isset($filelist[$item])) {
        $files=FALSE;
        $handle=opendir("./".$filelist[$item][0]);
        while ($file=readdir($handle))
            if (substr($file,-(strlen($filelist[$item][1])),strlen($filelist[$item][1]))==$filelist[$item][1])
                $files[]=substr($file,0,strlen($file)-strlen($filelist[$item][1]));
        $out="<select name=\"".$item."\">\n";
        foreach($files as $file)
            if ($file==$CONF[$item])
            {
            	if ($file == "espanol")
            	{
            		$out.="<option value=\"".$file."\" selected>español</option>\n";
            	} else {
             		$out.="<option value=\"".$file."\" selected>".$file."</option>\n";
             	}
           } else {
            	if ($file == "espanol")
            	{
            		$out.="<option value=\"".$file."\">español</option>\n";
            	} else {
             		$out.="<option value=\"".$file."\">".$file."</option>\n";
             	}
            }
        $out.="</select>";
    } else {
        $out="<input type=\"text\" size=\"40\" name=\"".$item."\" value=\"".$CONF[$item]."\"".PMBP_config_disable($item)."><br>";
    }
    return $out;
}

// checks if a item should be disabled and returns the code to disable it
function PMBP_config_disable($item) {
    global $CONF;
    global $PMBP_SYS_VAR;
    global $PMBP_MU_CONF;
    
    // availability check for some functions
    $disable=array('ftp_use'=>"!function_exists(\"ftp_connect\")",'ftp_server'=>"!function_exists(\"ftp_connect\")",
        'ftp_user'=>"!function_exists(\"ftp_connect\")",'ftp_passwd'=>"!function_exists(\"ftp_connect\")",
        'ftp_path'=>"!function_exists(\"ftp_connect\")",'ftp_pasv'=>"!function_exists(\"ftp_connect\")",
        'ftp_port'=>"!function_exists(\"ftp_connect\")",'email_use'=>"!function_exists(\"mail\")",'email'=>"!function_exists(\"mail\")",
        'no_login'=>($_SESSION['multi_user_mode'] || $CONF['login'])?1:0,
        'timelimit'=>"ini_get(\"safe_mode\")");

    // disable selected items due to system restrictions
    $out="";
    if (isset($disable[$item])) {
        if (eval("return(".$disable[$item].");")) $out=" disabled";
        // special case 'timelimit'
        if ($item=="timelimit" && $out) {
            $timelimit=ini_get("max_execution_time");
            if ($CONF['timelimit']!=$timelimit) {
                $CONF['timelimit']=$timelimit;
                PMBP_save_global_conf();
            }
        }
    }
    return $out;
}
?>
