2004-10-14 23:04:03 +02:00
< ? php
2008-07-21 11:40:58 +02:00
/**
* eGroupWare API : Database backups
*
* @ link http :// www . egroupware . org
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ package api
* @ subpackage db
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ copyright ( c ) 2003 - 8 by Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ version $Id $
*/
2005-11-04 19:35:09 +01:00
2008-07-21 11:40:58 +02:00
/**
* DB independent backup and restore of eGW ' s DB
*/
class db_backup
{
2004-10-14 23:04:03 +02:00
/**
2008-07-21 11:40:58 +02:00
* replaces backslashes , used in cvs_split
*/
const BACKSLASH_TOKEN = '##!!**bAcKsLaSh**!!##' ;
/**
* Reference to schema_proc
*
* @ var schema_proc
*/
var $schema_proc ;
/**
* Reference to ADOdb ( connection ) object
*
* @ var ADOConnection
*/
var $adodb ;
/**
* DB schemas , as array tablename => schema
*
* @ var array
*/
var $schemas = array ();
/**
* Tables to exclude from the backup : sessions , diverse caches which get automatic rebuild
*
* @ var array
*/
var $exclude_tables = array (
'egw_sessions' , 'egw_app_sessions' , 'phpgw_sessions' , 'phpgw_app_sessions' , // eGW's session-tables
'phpgw_anglemail' , // email's cache
'egw_felamimail_cache' , 'egw_felamimail_folderstatus' , 'phpgw_felamimail_cache' , 'phpgw_felamimail_folderstatus' , // felamimail's cache
);
/**
* regular expression to identify system - tables => ignored for schema + backup
2004-10-14 23:04:03 +02:00
*
2008-07-21 11:40:58 +02:00
* @ var string | boolean
2004-10-14 23:04:03 +02:00
*/
2008-07-21 11:40:58 +02:00
var $system_tables = false ;
/**
* regurar expression to identify eGW tables => if set only they are used
*
* @ var string | boolean
*/
var $egw_tables = false ;
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
/**
* Constructor
*/
function __construct ()
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( isset ( $GLOBALS [ 'egw_setup' ]) && is_object ( $GLOBALS [ 'egw_setup' ]) && ! isset ( $GLOBALS [ 'egw_setup' ] -> db ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$GLOBALS [ 'egw_setup' ] -> loaddb (); // we run inside setup, but db object is not loaded
}
if ( isset ( $GLOBALS [ 'egw_setup' ] -> oProc ) && is_object ( $GLOBALS [ 'egw_setup' ] -> oProc )) // schema_proc already instanciated, use it
{
$this -> schema_proc = $GLOBALS [ 'egw_setup' ] -> oProc ;
}
else
{
$this -> schema_proc = new schema_proc ();
}
2004-10-16 01:06:34 +02:00
2008-07-21 11:40:58 +02:00
$this -> db = $this -> schema_proc -> m_odb ;
$this -> adodb = $this -> db -> Link_ID ;
if ( isset ( $GLOBALS [ 'egw_setup' ]) && is_object ( $GLOBALS [ 'egw_setup' ])) // called from setup
{
if ( $GLOBALS [ 'egw_setup' ] -> config_table && $GLOBALS [ 'egw_setup' ] -> table_exist ( array ( $GLOBALS [ 'egw_setup' ] -> config_table )))
2004-10-16 01:06:34 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> db -> query ( " SELECT config_value FROM { $GLOBALS [ 'egw_setup' ] -> config_table } WHERE config_app='phpgwapi' AND config_name='backup_dir' " , __LINE__ , __FILE__ );
$this -> db -> next_record ();
if ( ! ( $this -> backup_dir = $this -> db -> f ( 0 )))
2004-10-16 01:06:34 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> db -> query ( " SELECT config_value FROM { $GLOBALS [ 'egw_setup' ] -> config_table } WHERE config_app='phpgwapi' AND config_name='files_dir' " , __LINE__ , __FILE__ );
2004-10-16 01:06:34 +02:00
$this -> db -> next_record ();
2008-07-21 11:40:58 +02:00
$this -> backup_dir = $this -> db -> f ( 0 ) . '/db_backup' ;
2004-10-16 01:06:34 +02:00
}
2008-07-21 11:40:58 +02:00
$this -> db -> query ( " SELECT config_value FROM { $GLOBALS [ 'egw_setup' ] -> config_table } WHERE config_app='phpgwapi' AND config_name='system_charset' " , __LINE__ , __FILE__ );
$this -> db -> next_record ();
$this -> charset = $this -> db -> f ( 0 );
if ( ! $this -> charset )
2004-10-16 01:06:34 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> db -> query ( " SELECT content FROM { $GLOBALS [ 'egw_setup' ] -> lang_table } WHERE message_id='charset' AND app_name='common' AND lang!='en' " , __LINE__ , __FILE__ );
$this -> db -> next_record ();
$this -> charset = $this -> db -> f ( 0 );
2004-10-16 01:06:34 +02:00
}
2008-07-21 11:40:58 +02:00
$this -> db -> select ( $GLOBALS [ 'egw_setup' ] -> applications_table , 'app_version' , array ( 'app_name' => 'phpgwapi' ), __LINE__ , __FILE__ );
$this -> api_version = $this -> db -> next_record () ? $this -> db -> f ( 0 ) : false ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
if ( ! $this -> charset ) $this -> charset = 'iso-8859-1' ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
else // called from eGW
2004-10-16 01:06:34 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> schema_proc = CreateObject ( 'phpgwapi.schema_proc' );
if ( ! ( $this -> backup_dir = $GLOBALS [ 'egw_info' ][ 'server' ][ 'backup_dir' ]))
2004-10-16 01:06:34 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> backup_dir = $GLOBALS [ 'egw_info' ][ 'server' ][ 'files_dir' ] . '/db_backup' ;
2004-10-16 01:06:34 +02:00
}
2008-07-21 11:40:58 +02:00
$this -> charset = $GLOBALS [ 'egw' ] -> translation -> charset ();
$this -> api_version = $GLOBALS [ 'egw_info' ][ 'apps' ][ 'phpgwapi' ][ 'version' ];
}
if ( ! is_dir ( $this -> backup_dir ) && is_writable ( dirname ( $this -> backup_dir )))
{
mkdir ( $this -> backup_dir );
}
switch ( $this -> db -> Type )
{
case 'sapdb' :
case 'maxdb' :
//$this->system_tables = '/^(sql_cursor.*|session_roles|activeconfiguration|cachestatistics|commandcachestatistics|commandstatistics|datastatistics|datavolumes|hotstandbycomponent|hotstandbygroup|instance|logvolumes|machineconfiguration|machineutilization|memoryallocatorstatistics|memoryholders|omslocks|optimizerinformation|sessions|snapshots|spinlockstatistics|version)$/i';
$this -> egw_tables = '/^(egw_|phpgw_|g2_)/i' ;
break ;
}
}
/**
* Opens the backup - file using the highest availible compression
*
* @ param $name = false string / boolean filename to use , or false for the default one
* @ param $reading = false opening for reading ( 'rb' ) or writing ( 'wb' )
* @ return string / resource error - msg of file - handle
*/
function fopen_backup ( $name = false , $reading = false )
{
if ( ! $name )
{
if ( ! $this -> backup_dir || ! is_writable ( $this -> backup_dir ))
2004-10-16 01:06:34 +02:00
{
2008-07-21 11:40:58 +02:00
return lang ( " backupdir '%1' is not writeable by the webserver " , $this -> backup_dir );
2004-10-16 01:06:34 +02:00
}
2008-07-21 11:40:58 +02:00
$name = $this -> backup_dir . '/db_backup-' . date ( 'YmdHi' );
}
else // remove the extension, to use the correct wrapper based on the extension
{
$name = preg_replace ( '/\.(bz2|gz)$/i' , '' , $name );
}
$mode = $reading ? 'rb' : 'wb' ;
if ( ! ( $f = @ fopen ( $file = " compress.bzip2:// $name .bz2 " , $mode )) &&
! ( $f = @ fopen ( $file = " compress.zlib:// $name .gz " , $mode )) &&
! ( $f = @ fopen ( $file = " zlib: $name .gz " , $mode )) && // php < 4.3
! ( $f = @ fopen ( $file = $name , $mode )))
{
$lang_mode = $reading ? lang ( 'reading' ) : lang ( 'writing' );
return lang ( " Cant open '%1' for %2 " , $name , $lang_mode );
}
return $f ;
}
/**
* Backup all data in the form of a ( compressed ) csv file
*
* @ param resource $f file opened with fopen for reading
* @ param boolean $convert_to_system_charset = false convert the restored data to the selected system - charset
*/
function restore ( $f , $convert_to_system_charset = false )
{
@ set_time_limit ( 0 );
ini_set ( 'auto_detect_line_endings' , true );
$this -> db -> transaction_begin ();
2004-10-16 01:06:34 +02:00
2008-07-21 11:40:58 +02:00
// drop all existing tables
foreach ( $this -> adodb -> MetaTables ( 'TABLES' ) as $table )
{
if ( $this -> system_tables && preg_match ( $this -> system_tables , $table ) ||
$this -> egw_tables && ! preg_match ( $this -> egw_tables , $table ))
2004-10-16 01:06:34 +02:00
{
2008-07-21 11:40:58 +02:00
continue ;
2004-10-16 01:06:34 +02:00
}
2008-07-21 11:40:58 +02:00
$this -> schema_proc -> DropTable ( $table );
2004-10-16 01:06:34 +02:00
}
2008-07-21 11:40:58 +02:00
$table = False ;
$n = 0 ;
while ( ! feof ( $f ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$line = trim ( fgets ( $f )); ++ $n ;
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
if ( empty ( $line )) continue ;
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
if ( substr ( $line , 0 , 9 ) == 'version: ' )
{
$api_version = trim ( substr ( $line , 9 ));
continue ;
}
if ( substr ( $line , 0 , 9 ) == 'charset: ' )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$charset = trim ( substr ( $line , 9 ));
// needed if mbstring.func_overload > 0, else eg. substr does not work with non ascii chars
@ ini_set ( 'mbstring.internal_encoding' , $charset );
2004-11-05 10:34:46 +01:00
2008-07-21 11:40:58 +02:00
// set the DB's client encoding (for mysql only if api_version >= 1.0.1.019)
if (( ! $convert_to_system_charset || $this -> db -> capabilities [ 'client_encoding' ]) &&
( substr ( $this -> db -> Type , 0 , 5 ) != 'mysql' || ! is_object ( $GLOBALS [ 'egw_setup' ]) ||
$api_version && ! $GLOBALS [ 'egw_setup' ] -> alessthanb ( $api_version , '1.0.1.019' )))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> db -> Link_ID -> SetCharSet ( $charset );
if ( ! $convert_to_system_charset )
2005-11-04 19:35:09 +01:00
{
2008-07-21 11:40:58 +02:00
$this -> schema_proc -> system_charset = $charset ; // so schema_proc uses it for the creation of the tables
2005-11-04 19:35:09 +01:00
}
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
continue ;
}
if ( substr ( $line , 0 , 8 ) == 'schema: ' )
{
// create the tables in the backup set
$this -> schemas = unserialize ( trim ( substr ( $line , 8 )));
foreach ( $this -> schemas as $table_name => $schema )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
echo " <pre> $table_name => " . $this -> write_array ( $schema , 1 ) . " </pre> \n " ;
$this -> schema_proc -> CreateTable ( $table_name , $schema );
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
// make the schemas availible for the db-class
$GLOBALS [ 'egw_info' ][ 'apps' ][ 'all-apps' ][ 'table_defs' ] = & $this -> schemas ;
continue ;
}
if ( substr ( $line , 0 , 7 ) == 'table: ' )
{
$table = substr ( $line , 7 );
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
$cols = $this -> csv_split ( $line = fgets ( $f )); ++ $n ;
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
if ( feof ( $f )) break ;
continue ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
if ( $convert_to_system_charset && ! $this -> db -> capabilities [ 'client_encoding' ])
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( $GLOBALS [ 'egw_setup' ])
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( ! is_object ( $GLOBALS [ 'egw_setup' ] -> translation -> sql ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$GLOBALS [ 'egw_setup' ] -> translation -> setup_translation_sql ();
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
$translation =& $GLOBALS [ 'egw_setup' ] -> translation -> sql ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
else
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$translation =& $GLOBALS [ 'egw' ] -> translation ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
}
if ( $table ) // do we already reached the data part
{
$data = $this -> csv_split ( $line , $cols );
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
if ( count ( $data ) == count ( $cols ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( $convert_to_system_charset && ! $this -> db -> capabilities [ 'client_encoding' ])
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$translation -> convert ( $data , $charset );
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
$this -> db -> insert ( $table , $data , False , __LINE__ , __FILE__ , 'all-apps' , true );
2007-06-03 19:57:40 +02:00
}
2004-10-14 23:04:03 +02:00
else
{
2008-07-21 11:40:58 +02:00
echo '<p>' . lang ( " Line %1: '%2'<br><b>csv data does not match column-count of table %3 ==> ignored</b> " , $n , $line , $table ) . " </p> \n " ;
echo 'data=<pre>' . print_r ( $data , true ) . " </pre> \n " ;
2004-10-14 23:04:03 +02:00
}
}
}
2008-07-21 11:40:58 +02:00
// updated the sequences, if the DB uses them
foreach ( $this -> schemas as $table => $schema )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
foreach ( $schema [ 'fd' ] as $column => $definition )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( $definition [ 'type' ] == 'auto' )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> schema_proc -> UpdateSequence ( $table , $column );
break ; // max. one per table
2004-10-14 23:04:03 +02:00
}
}
}
2008-07-21 11:40:58 +02:00
if ( $convert_to_system_charset ) // store the changed charset
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> db -> insert ( $GLOBALS [ 'egw_setup' ] -> config_table , array (
'config_value' => $GLOBALS [ 'egw_setup' ] -> system_charset ,
), array (
'config_app' => 'phpgwapi' ,
'config_name' => 'system_charset' ,
), __LINE__ , __FILE__ );
}
$this -> db -> transaction_commit ();
}
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
/**
* Split one line of a csv file into an array and does all unescaping
*/
private function csv_split ( $line , $keys = False )
{
$fields = explode ( ',' , trim ( $line ));
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
$str_pending = False ;
$n = 0 ;
foreach ( $fields as $i => $field )
{
if ( $str_pending !== False )
{
$field = $str_pending . ',' . $field ;
$str_pending = False ;
}
$key = $keys ? $keys [ $n ] : $n ;
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
if ( $field [ 0 ] == '"' )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( substr ( $field , - 1 ) !== '"' || $field === '"' || ! preg_match ( '/[^\\\\]+(\\\\\\\\)*"$/' , $field ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$str_pending = $field ;
continue ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
$arr [ $key ] = str_replace ( self :: BACKSLASH_TOKEN , '\\' , str_replace ( array ( '\\\\' , '\\n' , '\\r' , '\\"' ), array ( self :: BACKSLASH_TOKEN , " \n " , " \r " , '"' ), substr ( $field , 1 , - 1 )));
}
elseif ( $keys && strlen ( $field ) > 24 )
{
$arr [ $key ] = base64_decode ( $field );
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
else
{
$arr [ $key ] = $field == 'NULL' ? NULL : $field ;
}
++ $n ;
}
return $arr ;
}
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
/**
* escape data for csv
*/
private function escape_data ( & $data , $col , $defs )
{
if ( is_null ( $data ))
{
$data = 'NULL' ;
}
else
{
switch ( $defs [ $col ][ 'type' ])
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
case 'int' :
case 'auto' :
case 'decimal' :
case 'date' :
case 'timestamp' :
break ;
case 'blob' :
$data = base64_encode ( $data );
break ;
default :
$data = '"' . str_replace ( array ( '\\' , " \n " , " \r " , '"' ), array ( '\\\\' , '\\n' , '\\r' , '\\"' ), $data ) . '"' ;
break ;
}
}
}
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
/**
* Backup all data in the form of a ( compressed ) csv file
*
* @ param f resource file opened with fopen for writing
*/
function backup ( $f )
{
@ set_time_limit ( 0 );
fwrite ( $f , " eGroupWare backup from " . date ( 'Y-m-d H:i:s' ) . " \n \n " );
fwrite ( $f , " version: $this->api_version\n\n " );
fwrite ( $f , " charset: $this->charset\n\n " );
$this -> schema_backup ( $f ); // add the schema in a human readable form too
/* not needed , already done by schema_backup
foreach ( $this -> adodb -> MetaTables ( 'TABLES' ) as $table )
{
if ( $this -> db -> Type == 'sapdb' || $this -> db -> Type == 'maxdb' )
{
$table = strtolower ( $table );
}
if ( ! ( $this -> schemas [ $table ] = $this -> schema_proc -> GetTableDefinition ( $table )))
{
unset ( $this -> schemas [ $table ]);
2004-10-14 23:04:03 +02:00
}
}
2008-07-21 11:40:58 +02:00
*/
fwrite ( $f , " \n schema: " . serialize ( $this -> schemas ) . " \n " );
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
foreach ( $this -> schemas as $table => $schema )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( in_array ( $table , $this -> exclude_tables )) continue ; // dont backup
$first_row = true ;
$this -> db -> select ( $table , '*' , false , __LINE__ , __FILE__ );
while (( $row = $this -> db -> row ( true )))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( $first_row )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
fwrite ( $f , " \n table: $table\n " . implode ( ',' , array_keys ( $row )) . " \n " );
$first_row = false ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
array_walk ( $row , array ( 'db_backup' , 'escape_data' ), $schema [ 'fd' ]);
fwrite ( $f , implode ( ',' , $row ) . " \n " );
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
}
return true ;
}
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
/**
* Backup all schemas in the form of a setup / tables_current . inc . php file
*
* @ param resource | boolean $f
*/
function schema_backup ( $f = False )
{
foreach ( $this -> adodb -> MetaTables ( 'TABLES' ) as $table )
{
if ( $this -> system_tables && preg_match ( $this -> system_tables , $table ) ||
$this -> egw_tables && ! preg_match ( $this -> egw_tables , $table ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
continue ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
if ( $this -> db -> Type == 'sapdb' || $this -> db -> Type == 'maxdb' )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$table = strtolower ( $table );
}
if ( ! ( $this -> schemas [ $table ] = $this -> schema_proc -> GetTableDefinition ( $table )))
{
unset ( $this -> schemas [ $table ]);
}
if (( $this -> db -> Type == 'sapdb' || $this -> db -> Type == 'maxdb' ) && $table == 'phpgw_anglemail' )
{
// sapdb does not differ between text and blob
$this -> schemas [ $table ][ 'fd' ][ 'content' ][ 'type' ] = 'blob' ;
2004-10-14 23:04:03 +02:00
}
}
2008-07-21 11:40:58 +02:00
$def = " \t \$ phpgw_baseline = " ;
$def .= $this -> write_array ( $this -> schemas , 1 );
$def .= " ; \n " ;
if ( $f )
{
fwrite ( $f , $def );
}
else
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
if ( ! is_object ( $this -> browser ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$this -> browser = new browser ();
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
$this -> browser -> content_header ( 'schema-backup-' . date ( 'YmdHi' ) . '.inc.php' , 'text/plain' , bytes ( $def ));
echo " <?php \n \t /* eGroupWare schema-backup from " . date ( 'Y-m-d H:i:s' ) . " */ \n \n " . $def ;
}
}
/**
* Dump an array as php source
*
* copied from etemplate / inc / class . db_tools . inc . php
*/
private function write_array ( $arr , $depth , $parent = '' )
{
if ( in_array ( $parent , array ( 'pk' , 'fk' , 'ix' , 'uc' )))
{
$depth = 0 ;
}
if ( $depth )
{
$tabs = " \n " ;
for ( $n = 0 ; $n < $depth ; ++ $n )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$tabs .= " \t " ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
++ $depth ;
}
$def = " array( $tabs " . ( $tabs ? " \t " : '' );
2004-10-14 23:04:03 +02:00
2008-07-21 11:40:58 +02:00
$n = 0 ;
foreach ( $arr as $key => $val )
{
if ( ! is_int ( $key ))
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$def .= " ' $key ' => " ;
}
if ( is_array ( $val ))
{
$def .= $this -> write_array ( $val , $parent == 'fd' ? 0 : $depth , $key );
}
else
{
if ( ! $only_vals && $key === 'nullable' )
2004-10-14 23:04:03 +02:00
{
2008-07-21 11:40:58 +02:00
$def .= $val ? 'True' : 'False' ;
2004-10-14 23:04:03 +02:00
}
else
{
2008-07-21 11:40:58 +02:00
$def .= " ' $val ' " ;
2004-10-14 23:04:03 +02:00
}
}
2008-07-21 11:40:58 +02:00
if ( $n < count ( $arr ) - 1 )
{
$def .= " , $tabs " . ( $tabs ? " \t " : '' );
}
++ $n ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
$def .= " $tabs ) " ;
return $def ;
2004-10-14 23:04:03 +02:00
}
2008-07-21 11:40:58 +02:00
}
/*
2005-11-04 19:35:09 +01:00
$line = '"de","ranking","use \\"yes\\", or \\"no, prefession\\"","benützen Sie \\"yes\\" oder \\"no, Beruf\\""' ;
echo " <p>line=' $line '</p> \n " ;
echo " <pre> " . print_r ( db_backup :: csv_split ( $line ), true ) . " </pre> \n " ;
*/