Merged changes up to version 2.3.4 into unstable.
[fa-stable.git] / admin / db / maintenance_db.inc
index ed14e56a1de1e438b9eef8eee6cf1baaca07fb0c..4e7d56ccdc443e14f3f8d3d20837c0e2304d352c 100644 (file)
@@ -251,55 +251,20 @@ function write_lang()
        return true;
 }
 
-function db_create_db($connection)
-{
-       $db = mysql_connect($connection["host"] ,
-               $connection["dbuser"], $connection["dbpassword"]);
-       if (!mysql_select_db($connection["dbname"], $db))
-       {
-               $sql = "CREATE DATABASE " . $connection["dbname"] . "";
-               if (!mysql_query($sql))
-                       return 0;
-               mysql_select_db($connection["dbname"], $db);
-       }
-       return $db;
-}
-
-function db_drop_db($connection)
-{
-
-       if ($connection["tbpref"] == "")
-       {
-               $sql = "DROP DATABASE " . $connection["dbname"] . "";
-               return mysql_query($sql);
-       }
-       else
-       {
-       $res = db_query("show table status");
-       $all_tables = array();
-       while($row = db_fetch($res))
-               $all_tables[] = $row;
-        // get table structures
-               foreach ($all_tables as $table)
-               {
-                       if (strpos($table['Name'], $connection["tbpref"]) === 0)
-                               db_query("DROP TABLE `".$table['Name'] . "`");
-               }
-               //deleting the tables, how??
-               return true;
-       }
-}
-
 function db_import($filename, $connection, $force=true)
 {
        global $db, $go_debug;
 
        $allowed_commands = array(
                "create"  => 'table_queries', 
+               "delimiter" => 'table_queries',
                "alter table" => 'table_queries', 
                "insert" => 'data_queries', 
                "update" => 'data_queries', 
-               "drop table if exists" => 'drop_queries');
+               "drop table if exists" => 'drop_queries',
+               "drop function if exists" => 'drop_queries',
+               "drop trigger if exists" => 'drop_queries',
+               );
        $ignored_mysql_errors = array( //errors ignored in normal (non forced) mode
                '1022', // duplicate key
                '1050', // Table %s already exists
@@ -325,6 +290,7 @@ function db_import($filename, $connection, $force=true)
 
        // parse input file
        $query_table = '';
+       $delimiter = ';';
        foreach($lines as $line_no => $line)
        {
                $line = trim($line);
@@ -337,6 +303,10 @@ function db_import($filename, $connection, $force=true)
                        {
                                if (strtolower(substr($line, 0, strlen($cmd))) == $cmd) 
                                {
+                                       if ($cmd == 'delimiter') {
+                                               $delimiter = trim(substr($line, 10));
+                                               continue 2;
+                                       }
                                        $query_table = $table;
                                        ${$query_table}[] = array('', $line_no+1);
                                        break;
@@ -346,9 +316,10 @@ function db_import($filename, $connection, $force=true)
                 if($query_table != '')  // inside allowed query
                 {
                        $table = $query_table;
-                       if (substr($line, -1) == ';') // end of query found
+
+                       if (substr($line, -strlen($delimiter)) == $delimiter) // end of query found
                        {
-                               $line = substr($line, 0, strlen($line) - 1); // strip ';'
+                               $line = substr($line, 0, strlen($line) - strlen($delimiter)); // strip delimiter
                                $query_table = '';
                        }
                        ${$table}[count(${$table}) - 1][0] .= $line . "\n";
@@ -406,6 +377,7 @@ function db_import($filename, $connection, $force=true)
        }
        
        db_query("SET foreign_key_checks=1");
+       if ($delimiter != ';') db_query("delimiter ;"); // just for any case
 
        if (count($sql_errors)) {
                // display first failure message; the rest are probably derivative 
@@ -470,23 +442,27 @@ function db_unzip($mode, $path)
        return explode("\n", $file_data);
 }
 
-function db_backup($conn, $ext='no', $comm='', $tbpref = TB_PREF)
+function db_backup($conn, $ext='no', $comm='', $path=null)
 {
        if ($conn['tbpref'] != "")
                $filename = $conn['dbname'] . "_" . $conn['tbpref'] . date("Ymd_Hi") . ".sql";
        else
                $filename = $conn['dbname'] . "_" . date("Ymd_Hi") . ".sql";
 
-       return db_export($conn, $filename, $ext, $comm, $tbpref);
+       if (!isset($path))
+               $path = BACKUP_PATH;
+
+       return db_export($conn, $path . clean_file_name($filename), $ext, $comm);
 }
 
 // generates a dump of $db database
 // $drop and $zip tell if to include the drop table statement or dry to pack
-function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, $no_default=false)
+function db_export($conn, $filename, $zip='no', $comment='', $no_default=false)
 {
 
        global $app_title, $version, $power_url, $path_to_root;
 
+
     $error = false;
     // set max string size before writing to file
     $max_size = 1048576 * 2; // 2 MB
@@ -501,7 +477,7 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF,
        $backupfile = $filename . ".zip";
     else
        $backupfile = $filename;
-    $company = get_company_pref('coy_name', $tbpref);
+    $company = $conn['name']; // get_company_pref('coy_name');
 
     //create comment
     $out="# MySQL dump of database '".$conn["dbname"]."' on host '".$conn["host"]."'\n";
@@ -626,7 +602,7 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF,
                        }
                        elseif ($error)
                        {
-                               @unlink(BACKUP_PATH . $backupfile);
+                               @unlink($backupfile);
                                return false;
                        }
 
@@ -644,13 +620,10 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF,
        }
        else
        {
-               @unlink(BACKUP_PATH . $backupfile);
+               @unlink($backupfile);
                return false;
        }
 
-       // if (mysql_error()) return "DB_ERROR";
-       //@mysql_close($con);
-
        //if ($zip == "zip")
        //      $zip = $time;
        if (save_to_file($backupfile, $zip, $out))
@@ -659,7 +632,7 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF,
        }
        else
        {
-               @unlink(BACKUP_PATH . $backupfile);
+               @unlink($backupfile);
                return false;
        }
     return $backupfile;
@@ -716,13 +689,15 @@ function order_sql_tables($tables, $fks)
 
 // 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 save_to_file($backupfile, $zip, $fileData)
+function save_to_file($path, $zip, $fileData)
 {
        global $path_to_root;
 
+       $backupfile = basename($path);
+
     if ($zip == "gzip")
     {
-        if ($zp = @gzopen(BACKUP_PATH . $backupfile, "a9"))
+        if ($zp = @gzopen($path, "a9"))
         {
                        @gzwrite($zp, $fileData);
                        @gzclose($zp);
@@ -762,7 +737,7 @@ function save_to_file($backupfile, $zip, $fileData)
         $c_len = strlen($zdata);
 
         // dos time
-        $timearray = getdate($zip);
+        $timearray = getdate();
         $dostime = (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
             ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
         $dtime = dechex($dostime);
@@ -799,7 +774,7 @@ function save_to_file($backupfile, $zip, $fileData)
         // total # of entries "on this disk", total # of entries overall, size of central dir, offset to start of central dir, .zip file comment length
         $fileData .= pack('v', 1) . pack('v', 1) . pack('V', strlen($cdrec)) . pack('V', strlen($fr)) . "\x00\x00";
 
-        if ($zp = @fopen(BACKUP_PATH . $backupfile, "a"))
+        if ($zp = @fopen($path, "a"))
         {
                        @fwrite($zp, $fileData);
                        @fclose($zp);
@@ -814,7 +789,7 @@ function save_to_file($backupfile, $zip, $fileData)
     }
     else
     {
-        if ($zp = @fopen(BACKUP_PATH . $backupfile, "a"))
+        if ($zp = @fopen($path, "a"))
         {
                        @fwrite($zp, $fileData);
                        @fclose($zp);
@@ -844,4 +819,39 @@ function create_comp_dirs($comp_path, $comp_subdirs)
                        @fclose($f);
            }
 }
+
+//
+//     Checks $field existence in $table with given field $properties
+//     $table - table name without prefix
+//  $field -  optional field name
+//  $properties - optional properties of field defined by MySQL:
+//             'Type', 'Null', 'Key', 'Default', 'Extra'
+//
+function check_table($pref, $table, $field=null, $properties=null)
+{
+       $tables = @db_query("SHOW TABLES LIKE '".$pref.$table."'");
+       if (!db_num_rows($tables))
+               return 1;               // no such table or error
+
+       $fields = @db_query("SHOW COLUMNS FROM ".$pref.$table);
+       if (!isset($field)) 
+               return 0;               // table exists
+
+       while( $row = db_fetch_assoc($fields)) 
+       {
+               if ($row['Field'] == $field) 
+               {
+                       if (!isset($properties)) 
+                               return 0;
+                       foreach($properties as $property => $value) 
+                       {
+                               if ($row[$property] != $value) 
+                                       return 3;       // failed type/length check
+                       }
+                       return 0; // property check ok.
+               }
+       }
+       return 2; // field not found
+}
+
 ?>
\ No newline at end of file