<?php
session_start();/**************************************************صفحة النسخ الاحتياطى*******************************************************************/

// ضبط اعدادات اللغة العربية للصفحة 
header("content-type:text/html;charset=utf-8"); 
		
	//استدعاء ملف الاعدادات
$conc_type='cp_exec_conc';
include '../conf.php';
include '../conf_sub.php';
	//استدعاء ملف الاتصال بقاعدة البيانات
//include $cp_exec_conc;
	
	
		
//ضمان توافق قاعدة البيانات مع اللغة العربية
mysqli_query($conn,"SET NAMES 'utf8'");
		mysqli_query($conn,'SET CHARACTER SET utf8');
		mysqli_query($conn,"SET character_set_client=utf8");
		mysqli_query($conn,"SET character_set_connection=utf8");
		mysqli_query($conn,"SET character_set_database=utf8");
		mysqli_query($conn,"SET character_set_results=utf8");
		mysqli_query($conn,"SET character_set_server=utf8");
		mysqli_query($conn," set character_set_server='utf8'"); 
   		$table = '*';
		$extname='';
    // Set the suffix of the backup filename
    if ($table == '*') {
        $extname = 'all';
		
    }else{
        $extname = str_replace(",", "_", $table);
        $extname = str_replace(" ", "_", $extname);
    }
 
    // Call the backup function for all tables in a DB
    
    backup_tables($table,$extname);
 
    // Backup the table and save it to a sql file
    function backup_tables($tables,$bckextname)
    {
      
		

        $return = "";
 
        // Get all of the tables
        if($tables == '*') {
            $tables = array();
            $result = mysqli_query($conn,'SHOW TABLES');
            
   /*بداية التكرار */  
 while($row = mysqli_fetch_row($result)) {
                $tables[] = $row[0];
            }
        } else {
            if (is_array($tables)) {
            $tables = explode(',', $tables);
        }
    }
 
    // Cycle through each provided table
    foreach($tables as $table) {
        $result = mysqli_query($conn,'SELECT * FROM '.$table);
        $num_fields = mysqli_num_fields($result);
 
        // First part of the output – remove the table
        $return .= 'DROP TABLE ' . $table . ';<|||||||>';
 
        // Second part of the output – create table
        $row2 = mysqli_fetch_row(mysqli_query($conn,'SHOW CREATE TABLE '.$table));
        $return .= "\n\n" . $row2[1] . ";<|||||||>\n\n";
 
        // Third part of the output – insert values into new table
        for ($i = 0; $i < $num_fields; $i++) {
            
   /*بداية التكرار */  
 while($row = mysqli_fetch_row($result)) {
                $return.= 'INSERT INTO '.$table.' VALUES(';
                for($j=0; $j<$num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    
                    if (isset($row[$j])) {
                        $return .= '"' . $row[$j] . '"';
                    } else {
                        $return .= '""';
                    }
                    if ($j<($num_fields-1)) {
                        $return.= ',';
                    }
                }
                $return.= ");<|||||||>\n";
            }
        }
        $return.="\n\n\n";
    }
    // Generate the filename for the sql file
    $filess = 'backup/dbbackup_' . date("d-m-Y_H-i-s") .$bckextname. '.sql';
    // Save the sql file
    $handle = fopen($filess,'w');
    fwrite($handle,$return);
    fclose($handle);
 
    // Print the message
    print("تم اخذ نسخة احتياطية من قاعدة البيانات لتحميلها <a href='download.php?file=$filess'>انقر هنا</a>.<br>\n");
 
    // Close mysqli Connection
    mysqli_close();
}
?>