@@ -502,6 +502,38 @@ public static function extract_tar($tar, $dir, $files = null, $ow = false) {
502502 return true ;
503503 }
504504
505+ public static function DownloadFile ($ file , $ del = false , $ filename = '' ) {
506+ $ mime = "application/octet-stream " ;
507+ $ name = basename ($ file );
508+ if (!file_exists ($ file ))
509+ return false ;
510+ if (is_string ($ filename ) && $ filename != '' )
511+ $ name = $ filename ;
512+ if (class_exists ('finfo ' )) {
513+ $ finfo = new finfo (FILEINFO_MIME );
514+ $ mime = $ finfo ->file ($ file );
515+ } elseif (function_exists ('mime_content_type ' )) {
516+ $ mime = mime_content_type ($ file );
517+ }
518+ ob_start ();
519+ header ("Expires: 0 " );
520+ header ("Pragma: public " );
521+ header ("Connection: Keep-Alive " );
522+ header ("Cache-Control: public " );
523+ header ("Cache-Control: must-revalidate, post-check=0, pre-check=0 " );
524+ header ("Content-Description: File Transfer " );
525+ header ("Content-Type: $ mime " );
526+ header ("Content-Disposition: attachment; filename= \"" . $ name . "\"" );
527+ header ("Content-Transfer-Encoding: Binary " );
528+ header ("Content-Length: " . filesize ($ file ));
529+ ob_end_flush ();
530+ readfile ($ file );
531+ if ($ del === true )
532+ unlink ($ file );
533+ exit ();
534+ 535+ }
536+ 505537}
506538
507539class SQL_Backup extends FILES {
@@ -525,6 +557,20 @@ class SQL_Backup extends FILES {
525557 public $ res = false ;
526558
527559
560+ /**
561+ * @var object $con Database connection already opened
562+ * @var string | array $table_name Database tables
563+ * @var string | array $ext File estensions output
564+ * @var string $fname Filename output
565+ * @var string $folder Folder output
566+ * @var int $query_limit Query interval insert
567+ * @var bool | string $archive Output compressed (zip) or archived (tar) or in a dir (false)
568+ * @var bool $phpmyadmin ...
569+ * @var bool $save Output saved in a file or outputed as string
570+ * @var bool $sql_unique Output (sql) unified in a unique file/query
571+ * @var bool $download Output automatic downloaded (SOON)
572+ **/
573+ 528574 function __construct ($ con = null , $ table_name = null , $ ext = null , $ fname = null , $ folder = null , $ query_limit = null , $ archive = null , $ phpmyadmin = null , $ save = null , $ sql_unique = null ) {
529575 parent ::__construct ();
530576 $ this ->con = $ con ;
@@ -547,33 +593,12 @@ function __construct($con = null, $table_name = null, $ext = null, $fname = null
547593 public function execute ($ debug = false ) {
548594 $ res = array ();
549595 $ res_x = true ;
550- $ con = $ this ->con ;
596+ $ this ->checking ();
597+ if ($ this ->check ($ this ->con , "con " ) == false )
598+ return $ debug === true ? $ this ->debug () : false ;
599+ if ($ this ->check ($ this ->folder , "folder " ) == false )
600+ return $ debug === true ? $ this ->debug () : false ;
551601 $ tables = $ this ->check ($ this ->table_name , "tables " );
552- 553- $ time = -microtime (true );
554- 555- if ($ this ->check ($ con , "con " ) == false ) {
556- 557- if ($ debug === true )
558- return $ this ->debug ();
559- return false ;
560- }
561- 562- if ($ this ->check ($ this ->folder , "folder " ) == false ) {
563- 564- if ($ debug === true )
565- return $ this ->debug ();
566- return false ;
567- }
568- 569- if ($ tables == false )
570- $ tables = $ this ->table_name = $ this ->query_table ($ con , $ this ->type );
571- $ this ->check ($ this ->ext , "ext " );
572- $ this ->check ($ this ->save , "save " );
573- $ this ->check ($ this ->fname , "filename " );
574- $ this ->check ($ this ->archive , "archive " );
575- $ this ->check ($ this ->phpmyadmin , "one_file " );
576- $ this ->check ($ this ->sql_unique , "unique_sql " );
577602 foreach ($ this ->ext as $ type_ext ) {
578603 $ type_ext = trim ($ type_ext );
579604 if ($ this ->save == false ) {
@@ -586,12 +611,43 @@ public function execute($debug = false) {
586611 $ res_x = false ;
587612 }
588613 }
589- $ this ->exec_time = $ time += microtime (true );
590- $ this ->res = empty ($ res ) ? $ res_x : $ res ;
614+ $ this ->res = $ res = empty ($ res ) ? $ res_x : $ res ;
591615 if ($ debug === true )
592616 return $ this ->debug ();
593617 $ this ->clean_var ();
594- return empty ($ res ) ? $ res_x : $ res ;
618+ return $ res ;
619+ }
620+ 621+ public function execute_down () {
622+ $ this ->checking ();
623+ if ($ this ->check ($ this ->con , "con " ) == false )
624+ return false ;
625+ if ($ this ->check ($ this ->folder , "folder " ) == false )
626+ return false ;
627+ $ tables = $ this ->check ($ this ->table_name , "tables " );
628+ $ name_temp = "temp_backup " . md5 (microtime (true ) . mt_rand ());
629+ foreach ($ this ->ext as $ type_ext ) {
630+ $ type_ext = trim ($ type_ext );
631+ $ create = $ this ->create ($ type_ext , $ tables );
632+ if ($ this ->last_err_db == null ) {
633+ $ n = 1 ;
634+ foreach ($ create as $ table => $ tb ) {
635+ $ this ->_save ($ tb , "TB " . $ n . "_Name[ " . $ table . "]_Date[ " . date ("d-m-Y-H-i-s " ) . "]_Crc32b[ " . hash ("crc32b " , $ tb ) . "]. " . $ type_ext , $ name_temp , $ type_ext , 'zip ' );
636+ ++$ n ;
637+ }
638+ }
639+ }
640+ $ this ->clean_var ();
641+ return $ this ->DownloadFile ($ name_temp . '.zip ' , true , "Backup_MYSQL( " . date ("Y-m-d-H-i-s " ) . ").zip " );
642+ }
643+ 644+ private function checking () {
645+ $ this ->check ($ this ->ext , "ext " );
646+ $ this ->check ($ this ->save , "save " );
647+ $ this ->check ($ this ->fname , "filename " );
648+ $ this ->check ($ this ->archive , "archive " );
649+ $ this ->check ($ this ->phpmyadmin , "one_file " );
650+ $ this ->check ($ this ->sql_unique , "unique_sql " );
595651 }
596652
597653 protected function create ($ ext , $ tables ) {
@@ -797,7 +853,7 @@ private function check($in, $t) {
797853 return true ;
798854 if (is_string ($ in ) && $ in != "* " && $ in != "" )
799855 return $ this ->table_name = explode (", " , $ in );
800- return false ;
856+ return $ this -> table_name = $ this -> query_table ( $ this -> con , $ this -> type ) ;
801857 break ;
802858
803859 case "filename " :
@@ -892,8 +948,7 @@ private function clean_var() {
892948 unset($ this ->err_c );
893949 unset($ this ->name_file );
894950 unset($ this ->path_file );
895- unset($ this ->exec_time );
896- unset($ this ->last_err_db );
951+ unset($ this ->last_err_db );
897952 }
898953
899954 private function debug () {
0 commit comments