kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file backups_lib.php

Documentation is available at backups_lib.php

  1. <?php
  2. /**
  3.  * Database Backup Library
  4.  *
  5.  * @package kernel
  6.  * @version $Header$
  7.  */
  8.  
  9. /**
  10.  * Database Backup Library
  11.  *
  12.  * @package kernel
  13.  */
  14. class BackupLib extends BitBase {
  15.  
  16.     function restore_database($filename) {
  17.         global $gBitDbType;
  18.         // Get the password before it's too late
  19.         $query = "select `hash` from `".BIT_DB_PREFIX."users_users` where `user_id`=?";
  20.         $pwd = $this->mDb->getOne($query,array(ROOT_USER_ID));
  21.  
  22.         switch ($gBitDbType) {
  23.             case "postgres":
  24.                 $query = "select `tablename` from `pg_tables` where `tablename` not like 'pg_%' and `tablename` not like 'sql_%'";
  25.                 break;
  26.             case "mysql":
  27.                 $query = "show tables";
  28.         }
  29.  
  30.         $result = $this->mDb->query($query);
  31.         $sql = '';
  32.         $part = '';
  33.  
  34.         while ($res = $result->fetchRow()) {
  35.             list($key, $val) = each($res);
  36.  
  37.             if (!strstr($val, 'babl')) {
  38.                 // Now delete the table contents
  39.                 $query2 = "delete from `$val`";
  40.                 $result2 = $this->mDb->query($query2);
  41.             }
  42.         }
  43.  
  44. //        $query = "update `".BIT_DB_PREFIX."users_users` set `hash`=? where `login`=?";
  45. //        $result = $this->mDb->query($query,array($pwd,'admin'));
  46.         @$fp = fopen($filename, "rb");
  47.  
  48.         if (!$fp) return false;
  49.  
  50.         while (!feof($fp)) {
  51.             $rlen = fread($fp, 4);
  52.             if (feof($fp)) break;
  53.  
  54.             $len = unpack("L", $rlen);
  55.             $len = array_pop($len);
  56.             $line = fread($fp, $len);
  57.  
  58.             //removing the de/cyphering stuff for now as it doesn't work on database restore
  59. //            $line = $this->RC4($pwd, $line);
  60.  
  61.             // EXECUTE SQL SENTENCE HERE
  62.             $result = $this->mDb->query($line,array());
  63.         }
  64.  
  65.         fclose ($fp);
  66.     }
  67.  
  68.     function RC4($pwd, $data) {
  69.         $key[] = "";
  70.  
  71.         $box[] = "";
  72.         $temp_swap = "";
  73.         $pwd_length = 0;
  74.         $pwd_length = strlen($pwd);
  75.  
  76.         for ($i = 0; $i <= 255; $i++) {
  77.             $key[$i] = ord(substr($pwd, ($i % $pwd_length) + 1, 1));
  78.  
  79.             $box[$i] = $i;
  80.         }
  81.  
  82.         $x = 0;
  83.  
  84.         for ($i = 0; $i < 255; $i++) {
  85.             $x = ($x + $box[$i] + $key[$i]) % 256;
  86.  
  87.             $temp_swap = $box[$i];
  88.             $box[$i] = $box[$x];
  89.             $box[$x] = $temp_swap;
  90.         }
  91.  
  92.         $temp = "";
  93.         $k = "";
  94.         $cipherby = "";
  95.         $cipher = "";
  96.         $a = 0;
  97.         $j = 0;
  98.  
  99.         for ($i = 0; $i < strlen($data); $i++) {
  100.             $a = ($a + 1) % 256;
  101.  
  102.             $j = ($j + $box[$a]) % 256;
  103.             $temp = $box[$a];
  104.             $box[$a] = $box[$j];
  105.             $box[$j] = $temp;
  106.             $k = $box[(($box[$a] + $box[$j]) % 256)];
  107.             $cipherby = ord(substr($data, $i, 1)) ^ $k;
  108.             $cipher .= chr($cipherby);
  109.         }
  110.  
  111.         return $cipher;
  112.     }
  113.  
  114.     // Functions to backup the database (mysql?)
  115.     function backup_database($filename) {
  116.         global $gBitDbType;
  117.         ini_set("max_execution_time", "3000");
  118.  
  119.         $query = "select `hash` from `".BIT_DB_PREFIX."users_users` where `user_id`=?";
  120.         $pwd = $this->mDb->getOne($query,array(ROOT_USER_ID));
  121.         @$fp = fopen($filename, "w");
  122.  
  123.         if (!$fp)
  124.             return false;
  125.  
  126.         switch ($gBitDbType) {
  127.             case "postgres":
  128.                 $query = "select `tablename` from `pg_tables` where `tablename` not like 'pg_%' and `tablename` not like 'sql_%'";
  129.                 break;
  130.             case "mysql":
  131.                 $query = "show tables";
  132.         }
  133.  
  134.         $result = $this->mDb->query($query);
  135.         $sql = '';
  136.         $part = '';
  137.  
  138.         while ($res = $result->fetchRow()) {
  139.             list($key, $val) = each($res);
  140.  
  141.             if (!strstr($val, 'babl')) {
  142.                 // Now dump the table
  143.                 $query2 = "select * from `$val`";
  144.  
  145.                 $result2 = $this->mDb->query($query2);
  146.  
  147.                 while ($res2 = $result2->fetchRow()) {
  148.                     $sentence = "values(";
  149.  
  150.                     $first = 1;
  151.  
  152.                     foreach ($res2 as $field => $value) {
  153.                         if (is_numeric($field) || empty($value))
  154.                             continue;
  155.                         if ($first) {
  156.                             $sentence .= "'" . addslashes($value). "'";
  157.                             $first = 0;
  158.                             $fields = '(`' . $field . '`';
  159.                         } else {
  160.                             $sentence .= ",'" . addslashes($value). "'";
  161.                             $fields .= ",`$field`";
  162.                         }
  163.                     }
  164.  
  165.                     $fields .= ')';
  166.                     $sentence .= ")";
  167.                     // Don't write out empty tables - chokes mysql
  168.                     if ($sentence != "values()") {
  169.                         $part = "insert into `$val` $fields $sentence;";
  170.                         $len = pack("L", strlen($part));
  171.                         fwrite($fp, $len);
  172.                         //removing the de/cyphering stuff for now as it doesn't work on database restore
  173. //                        $part = $this->RC4($pwd, $part);
  174.                         fwrite($fp, $part);
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.         // And now print!
  180.         fclose ($fp);
  181.         return true;
  182.     }
  183. }
  184.  
  185. $backuplib = new BackupLib();
  186.  
  187. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:36 +0000 by phpDocumentor 1.5.0-lsces