<?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.|
 +--------------------------------------------------------------------------+
*/
/*
  ===============================================================

  inGenio X
  http://ingenio.area421.com

  Created by 
  Julian Moreno Hidalgo (Software developer)
  Jesus Moreno Hidalgo (Communication's tree)

  inGenio X is a free software licensed under CC-GNU GPL

  ===============================================================
*/

/* MODIFICACION DE ESTAS FUNCIONES ORIGINALES DE functions.inc.php */
/* modificadas para inGenio X */
/*
	NOTAS:
		este PHP esta incluido en definitions.php
		se han modificado:
			definitions.php
			index.php
			login.php
			import.php
			big_import.php
			config.php
			sql_query.php
			file_info.php
			espanol.inc.php
			english.inc.php
			javascript.js
			
		
		y tambien:
			functions.inc.php



*/

/**********************************************************
* Funcion para encontrar la cadena buscada y devolver
* lo anterior o el resto, con o sin la cadena que se busca
* $str -> es la cadena donde se busca
* $chars -> es la cadena que se busca
* $modo -> es el modo:
*			1=antes sin $chars | 2=resto sin $chars
*			3=antes con $chars | 4=resto con $chars
**********************************************************/

function inGenioX_str_antes_resto( $str, $chars, $modo )
{
	if ($chars != "")
	{
		$num = strpos($str, $chars);
		// si integer
		if (is_int($num))
		{
			if ($modo == 1)
			{
				return substr($str, 0, $num);
		
			} else if ($modo == 2)
			{
				return substr($str, $num + strlen($chars));
		
			} else if ($modo == 3)
			{
		
				return substr($str, 0, $num + strlen($chars));
		
			} else if ($modo == 4)
			{
		
				return strstr($str, $chars);
		
			}
	
		} else {
	
			return $str;
		}

	// no hay $chars
	} else {
	
		return $str;
	}
	
	
}

// nueva funcion para los creditos
function PMBP_print_powered() {

	//$powered_print = '<div class="pwd">Powered by: <a href="javascript:popUp(\''.PMBP_WEBSITE.'\',\'pwv\',\'\',\'\');" class="pwd_conex">phpMyBackupPro</a><div>';
	$powered_print = '';

	return $powered_print;
}

// prints the basis html header in the $lang language with $scriptname scriptname
function PMBP_print_header($scriptname) {
    global $CONF;
    global $_POST;
    global $PMBP_SYS_VAR;
    global $PMBP_MU_CONF;
    
    if (!isset($CONF['stylesheet'])) $CONF['stylesheet']="standard";
    echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html".ARABIC_HTML.">
<head>
<title>phpMyBackupPro : inGenio X"; // nuevo title
    // print mu mode info in browser title
    if ($_SESSION['multi_user_mode']) echo " (Multi User Mode)";
    if (isset($_SESSION['sql_user']) && isset($_SESSION['sql_passwd']))
        if ($_SESSION['sql_user'] && $_SESSION['sql_user']==$PMBP_MU_CONF['sql_user_admin'] && $_SESSION['sql_passwd']==$PMBP_MU_CONF['sql_passwd_admin'])
            echo " (Multi User Mode Administration)";

	if (!file_exists(PMBP_STYLESHEET_DIR.$CONF['stylesheet'].".css"))
		if (!file_exists(PMBP_STYLESHEET_DIR.($CONF['stylesheet']="standard")."css"))
			echo "STYLESHEET IS MISSING!";
    echo "</title>
<meta http-equiv=\"Content-Type\" content=\"text/html;charset=".BD_CHARSET_HTML."\">
<meta name=\"robots\" content=\"noindex\">
<meta name=\"robots\" content=\"nofollow\">
<link href=\"images/favicon.png\" type=\"image/png\" rel=\"icon\">
<link rel=\"stylesheet\" href=\"".PMBP_STYLESHEET_DIR.$CONF['stylesheet'].".css\" type=\"text/css\">
";
    readfile(PMBP_JAVASCRIPTS);
    // define menue
   // $menu=array("index.php"=>F_START,"config.php"=>F_CONFIG,"import.php"=>F_IMPORT,"backup.php"=>F_BACKUP,"scheduled.php"=>F_SCHEDULE,"db_info.php"=>F_DB_INFO);
    
    $menu=array("backup.php"=>F_BACKUP,"import.php"=>F_IMPORT,"config.php"=>F_CONFIG); //  nuevo menus importantes
    
    // disable sql queries in mu mode if allow_sql_queries is false
    if (($_SESSION['multi_user_mode'] && $PMBP_MU_CONF['allow_sql_queries']) || !$_SESSION['multi_user_mode']) {
        
        $menu=array_merge($menu, array("sql_query.php"=>F_SQL_QUERY)); // nuevo menu consola
    
    }
     
    $menu=array_merge($menu, array("login.php?logout=TRUE"=>F_LOGOUT)); // nuevo menu salir
    
    $accesskeys=array("index.php"=>"m","config.php"=>"c","import.php"=>"i","backup.php"=>"b","scheduled.php"=>"s","db_info.php"=>"d","sql_query.php"=>"q","logout"=>"l","help"=>"h");
    
    $num_menus = count($menu); // nuevo numero menus + salir
    $width_100 = "100%"; // nuevo ancho
    
    $simple_width=140;
    $width=count($menu)*$simple_width;
    
    echo "</head>

<body>
<table width=\"".$width_100."\">
 <colgroup>
  <col span=\"".$num_menus."\" width=\"".$simple_width."\">
 </colgroup>";
 
echo "
 <!-- MENU -->
 <tr>\n";

    // generate menu
    foreach($menu as $filename=>$title) {

        // print active link
        if ($filename==$scriptname && $filename!="login.php?logout=TRUE" && $filename!="HELP") {
           //echo "  <th class=\"active\">\n   <a href=\"".$filename."\" accesskey=\"".$accesskeys[$filename]."\" title=\"[access key = ".$accesskeys[$filename]."]\">".PMBP_image_tag(substr($filename,0,strpos($filename,".")).".gif","","[accesskey = ".$accesskeys[$filename]."]").$title."</a>\n  </th>\n";
            echo "  <th class=\"active\">\n   <a href=\"".$filename."\">".PMBP_image_tag(substr($filename,0,strpos($filename,".")).".gif","","").$title."</a>\n  </th>\n";

        // print lasting menu
        //} elseif ($filename!="login.php?logout=TRUE" && $filename!="HELP") {
        } elseif ($filename!="HELP") {
          //echo "  <th>\n   <a href=\"".$filename."\" accesskey=\"".$accesskeys[$filename]."\" title=\"[access key = ".$accesskeys[$filename]."]\">".PMBP_image_tag(substr($filename,0,strpos($filename,".")).".gif","","[accesskey = ".$accesskeys[$filename]."]").$title."</a>\n  </th>\n";
           echo "  <th>\n   <a href=\"".$filename."\">".PMBP_image_tag(substr($filename,0,strpos($filename,".")).".gif","","").$title."</a>\n  </th>\n";
        }
        
    }

    echo " </tr>
</table>
<table width=\"".$width_100."\">
 <tr>
  <td valign=\"top\">
  <div class=\"main\">
<!-- HEADER END -->
";
}


// print basis html footer
function PMBP_print_footer() {
    global $PMBP_SYS_VAR;
    global $PMBP_MU_CONF;

    // adjust width of table to the number of menu items
    if (($_SESSION['multi_user_mode'] && $PMBP_MU_CONF['allow_sql_queries']) || !$_SESSION['multi_user_mode']) {    
        $tabe_width="100%";
    } else {
        $tabe_width="100%";
    }
    
    echo "\n<!-- FOOTER -->
  </div>
  </td>
 </tr>
</table>\n";
	
	echo PMBP_print_powered();
	echo "\n</body></html>";

}

function PMBP_image_tag($image,$alt="",$title="",$link=""){
    if (strpos($image,"/")==0) {
        $image=PMBP_IMAGE_DIR.$image;
        $size=getimagesize($image);
    } else {
        $size=getimagesize(PMBP_IMAGE_DIR.basename($image));
    }
    if ($link != "") // modificada
        return "<a href=\"".$link."\"><img src=\"".$image."\" alt=\"".$alt."\" title=\"".$title."\" ".$size[3]."></a>";
    else
        return "<img src=\"".$image."\" alt=\"".$alt."\" title=\"".$title."\" ".$size[3].">";
}

// prints html export form used on several pages
function PMBP_print_export_form($dirs1=FALSE) {
    global $CONF;
    global $PMBP_SYS_VAR;

    // find the availabe compression methods and set which are disabled and which is selected
    if (!@function_exists("gzopen") || !@function_exists("gzcompress")) $disable_gzip=" disabled"; else $disable_gzip="";
 
    echo SQ_SELECT_DB.": "; // era F_SELECT_DB
	$db = PMBP_get_db_list();
    echo '<span class="red_left">'.inGenioX_str_antes_resto($db[0],INGENIO_PREFIJO_DB,2).'</span>';
    echo '<input type="hidden" value="'.$db[0].'" name="db[]">';
    echo '<br><br>';

    echo "\n<table width=\"100%\">\n";
    echo "<tr>\n<td>\n";

   	echo F_COMMENTS.":";
    echo "</td>\n</tr>";
    
   	echo "<tr>\n<td>\n";
   echo "<textarea name=\"comments\" rows=\"6\" cols=\"80\">".$PMBP_SYS_VAR['F_comment']."</textarea>\n<br>";
    if($PMBP_SYS_VAR['F_tables']) $checked="checked"; else $checked="";
    echo "<input type=\"checkbox\" name=\"tables\" ".$checked.">".F_EX_TABLES." | ";
    if($PMBP_SYS_VAR['F_data']) $checked="checked"; else $checked="";    
    echo "<input type=\"checkbox\" name=\"data\" ".$checked.">".F_EX_DATA." | ";
    if($PMBP_SYS_VAR['F_drop']) $checked="checked"; else $checked="";    
    echo "<input type=\"checkbox\" name=\"drop\" ".$checked.">".F_EX_DROP." | ";

    $comp_off=$comp_gzip=$comp_zip="";
    if($PMBP_SYS_VAR['F_compression']=="gzip" && !$disable_gzip) $comp_gzip=" selected";
        elseif($PMBP_SYS_VAR['F_compression']=="zip") $comp_zip=" selected";
            else $comp_off=" selected";
            
    echo F_EX_COMP."
<select name=\"zip\">
<option".$comp_off." value=\"\">".F_EX_OFF."</option>
<option ".$comp_gzip." ".$disable_gzip." value=\"gzip\">".F_EX_GZIP."</option>
<option".$comp_zip." value=\"zip\">".F_EX_ZIP."</option>
</select>\n</td>\n</tr>\n</table>\n<p></p>\n";

    // show directory backup form
    if ($CONF['dir_backup']) {
        if (!is_array($dirs1) && $PMBP_SYS_VAR['dir_lists']>=1) $dirs1=PMBP_get_dirs("../");
        
        $last_dirs=explode("|",$PMBP_SYS_VAR['F_ftp_dirs']);

        echo "\n\n<table width=\"940\">\n";
        echo "<tr>\n<td>\n";
        echo EX_DIRS.":<br>(<a href=\"scheduled.php?update_dir_list=TRUE\">".PMBP_EXS_UPDATE_DIRS."</a>)<br>\n";
        echo "</td>\n<td>&nbsp;</td>\n<td>\n";
        echo EX_DIRS_MAN.":<br>\n";
        echo "</td>\n</tr><tr>\n<td>";
        echo "<select name='dirs[]' multiple=\"multiple\" size=\"9\">";
        foreach($dirs1 as $value) {
            if (in_array("../".$value, $last_dirs)) {            
                echo "<option value=\""."../".$value."\" selected>"."../".$value."</option>\n";
            } else {
                echo "<option value=\""."../".$value."\">"."../".$value."</option>\n";            
            }
        }
        echo "</select>\n";
        echo "\n</td>\n<td>&nbsp;</td>\n<td>\n";
        echo "<textarea rows=\"6\" cols=\"50\" name=\"man_dirs\">".$PMBP_SYS_VAR['F_ftp_dirs_2']."</textarea><br>\n";
        if($PMBP_SYS_VAR['F_packed']) $checked="checked"; else $checked="";        
        echo "<input type=\"checkbox\" name=\"packed\" ".$checked."> Packed in one ZIP file\n";       
       echo "</td>\n</tr>\n</table>\n<p></p>\n";
    }
}

// returns list of databases on $host host using $user user and $passwd password
function PMBP_get_db_list() {
    global $CONF;

    // if there is given the name of a single database
    if ($CONF['sql_db']) {
        @mysql_connect($CONF['sql_host'],$CONF['sql_user'],$CONF['sql_passwd']);
        if (@mysql_select_db($CONF['sql_db'])) $dbs=array($CONF['sql_db']);
            else $dbs=array();
        return $dbs;
    }
    
/***************************** linea nueva *///////

   /*
   // else try to get a list of all available databases on the server
    $list=array();
    @mysql_connect($CONF['sql_host'],$CONF['sql_user'],$CONF['sql_passwd']);
    $db_list=@mysql_list_dbs();
    while ($row=@mysql_fetch_array($db_list))
        if (@mysql_select_db($row['Database'])) $list[]=$row['Database'];
    return $list;
    */

/***************************** linea nueva *///////

}

// generates javascript confirm dialog
// if $popupType is "view" or something else, a pop up like in PMBP_pop_up will be opened after the confirmation
function PMBP_confirm($text,$path,$link,$popupType=false){
    global $CONF;
    switch ($CONF['confirm']) {
        case 0:
            if ($popupType) return "<a href='javascript:popUp(\"".$path."\",\"".$popupType."\",true,\"".$text."\")'>".$link."</a>";
                else return "<a href='javascript:confirmClick(\"".$text."\",\"".$path."\")'>".$link."</a>";
        case 1:
            if ($popupType) {
                if (strstr($path,"all") || strstr($path,"ALL")) return "<a href='javascript:popUp(\"".$path."\",\"".$popupType."\",true,\"".$text."\")'>".$link."</a>";
                    else return "<a href='javascript:popUp(\"".$path."\",\"".$popupType."\",false,\"\")'>".$link."</a>";
            } else {
                /*if (strstr($path,"all") || strstr($path,"ALL")) return "<a href='javascript:confirmClick(\"".$text."\",\"".$path."\")'>".$link."</a>";
                    else return "<a href=\"".$path."\">".$link."</a>"; */           
/***************************** linea nueva *///////
                   return "<a href='javascript:confirmClick(\"".$text."\",\"".$path."\")'>".$link."</a>";            
 /***************************** linea nueva *///////
          }
        case 2:
            if ($popupType) {
                if (strstr($path,"ALL")) return "<a href='javascript:popUp(\"".$path."\",\"".$popupType."\",true,\"".$text."\")'>".$link."</a>";
                    else return "<a href='javascript:popUp(\"".$path."\",\"".$popupType."\",false,\"\")'>".$link."</a>";
            } else {
                if (strstr($path,"ALL")) return "<a href='javascript:confirmClick(\"".$text."\",\"".$path."\")'>".$link."</a>";
                    else return "<a href=\"".$path."\">".$link."</a>";            
            }
        case 3:
            if ($popupType) {
                return "<a href='javascript:popUp(\"".$path."\",\"".$popupType."\",false,\"\")'>".$link."</a>";
            } else {
                return "<a href=\"".$path."\">".$link."</a>";
            }
    }
}

// checks if settings on the export form where made and saves them
function PMBP_save_export_settings() {
    global $PMBP_SYS_VAR;

    // check if any settings have changed
    if ($PMBP_SYS_VAR['F_data']!=$_POST['data'] OR $PMBP_SYS_VAR['F_tables']!=$_POST['tables'] OR
    $PMBP_SYS_VAR['F_compression']!=$_POST['zip'] OR $PMBP_SYS_VAR['F_drop']!=$_POST['drop'] OR $PMBP_SYS_VAR['F_packed']!=$_POST['packed']) {            
        $PMBP_SYS_VAR['F_data']=$_POST['data'];
        $PMBP_SYS_VAR['F_tables']=$_POST['tables'];
        $PMBP_SYS_VAR['F_compression']=$_POST['zip'];
        $PMBP_SYS_VAR['F_drop']=$_POST['drop'];
        $PMBP_SYS_VAR['F_packed']=$_POST['packed'];
    }

/***************************** linea nueva *///////			
        $PMBP_SYS_VAR['F_dbs']="";
/***************************** linea nueva *///////			

   /* if (isset($_POST['db'])) {
        if (is_array($_POST['db'])) {
             if ($PMBP_SYS_VAR['F_dbs']!=implode("|",$_POST['db'])) {
                 $PMBP_SYS_VAR['F_dbs']=implode("|",$_POST['db']);
             }
        } else {
            $PMBP_SYS_VAR['F_dbs']="";
        }
    } else {
        $PMBP_SYS_VAR['F_dbs']="";
    }*/
            
     if ($PMBP_SYS_VAR['F_comment']!=$_POST['comments']) {
        $PMBP_SYS_VAR['F_comment']=$_POST['comments'];
    }

    if (isset($_POST['dirs'])) {
         if ($PMBP_SYS_VAR['F_ftp_dirs']!=implode("|",$_POST['dirs'])) {
             $PMBP_SYS_VAR['F_ftp_dirs']=implode("|",$_POST['dirs']);
         }
    } else {
        $PMBP_SYS_VAR['F_ftp_dirs']="";
    }

    if ($PMBP_SYS_VAR['F_ftp_dirs_2']!=$_POST['man_dirs']) {
        $PMBP_SYS_VAR['F_ftp_dirs_2']=$_POST['man_dirs'];
    }
    
    // update global_conf.php
    PMBP_save_global_conf();
}

// saves the string in $fileData to the file $backupfile as gz file or not ($zip)
// returns backup file name if name has changed (zip), else TRUE. If saving failed, return value is FALSE
function PMBP_save_to_file($backupfile,$zip,&$fileData,$mode) {
	// save to a gzip file
    if ($zip=="gzip") {
        if ($zp=@gzopen("./".$backupfile,$mode."9")) {
            @gzwrite($zp,$fileData);
            @gzclose($zp);            
            return $backupfile;
        } else {
            return FALSE;
        }

    // save to a plain text file (uncompressed)
    } else {
        if ($zp=@fopen("./".$backupfile,$mode)) {
            @fwrite($zp,$fileData);
            @fclose($zp);
            return $backupfile;
        } else {
            return FALSE;
        }
    }
}


// updates the content in global_conf.php
function PMBP_save_global_conf($global_conf_path="") {
    global $CONF;
    global $PMBP_SYS_VAR;
    
    // to ensure that all configuration settings are saved
    @ignore_user_abort(TRUE);
    
    // create content for global.conf
/***************************** linea nueva *///////			
    $file="<?php\n\n// This file is automatically generated and modified by phpMyBackupPro ".PMBP_VERSION."";
    $file.="\n// ".INGENIO_CREDITOS_SQL."\n\n";
/***************************** linea nueva *///////			
    if (is_array($CONF)) {
        foreach($CONF as $item=>$conf) {
            // don't save multi server settings to gloabl_conf.php
            if ($item=="sql_host_s" || $item=="sql_user_s" || $item=="sql_passwd_s" || $item=="sql_db_s") continue;
            
            // don't store sql data in mu mode
            if ($_SESSION['multi_user_mode'] && ($item=="sql_passwd" || $item=="sql_host" || $item=="sql_user" || $item=="sql_db")) continue;
    
            // update $_SESSION['sql_host_org'] etc. if new sql data were entered on the config page
            if (basename($_SERVER['SCRIPT_NAME'])=="config.php") {
                $_SESSION['sql_host_org']=$CONF['sql_host'];
                $_SESSION['sql_user_org']=$CONF['sql_user'];
                $_SESSION['sql_passwd_org']=$CONF['sql_passwd'];
                $_SESSION['sql_db_org']=$CONF['sql_db'];
            }    

/***************************** linea nueva *///////			
			// no guardar datos de base
			if ($item == "sql_host" || $item == "sql_user" || $item == "sql_passwd" || $item == "sql_db")
			{
            	//  no guarda 
            	$file.="";
            } else {

            	// guarda los restantes valores
            	$file.="\$CONF['".$item."']=\"".$conf."\";\n";
            	
            }
/***************************** linea nueva *///////
        
        } // del foreach
    }

    // unset 'last_scheduled_' values in sys vars which no longer belong to an account
    foreach($PMBP_SYS_VAR as $key=>$value) {
        if (substr($key,0,15)=="last_scheduled_" && substr($key,15)>=count($CONF['sql_host_s'])) unset($PMBP_SYS_VAR[$key]);
    }
    
    // add system variables    
    $file.="\n";
    foreach($PMBP_SYS_VAR as $item=>$sys_var) $file.="\$PMBP_SYS_VAR['".$item."']=\"".$sys_var."\";\n";
    
    $file.="\n?>";
        
    if (!$global_conf_path) $global_conf_path=PMBP_GLOBAL_CONF;
    return PMBP_save_to_file($global_conf_path,FALSE,$file,"w");
}


?>