ultimix
dump.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
26  class dump_1_0_0{
27 
38  var $CachedMultyFS = false;
39  var $Database = false;
40  var $DatabaseAlgorithms = false;
41  var $Security = false;
42 
57  function __construct()
58  {
59  try
60  {
61  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
62  $this->Database = get_package( 'database' , 'last' , __FILE__ );
63  $this->DatabaseAlgorithms = get_package( 'database::database_algorithms' , 'last' , __FILE__ );
64  $this->Security = get_package( 'security' , 'last' , __FILE__ );
65  }
66  catch( Exception $e )
67  {
68  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
69  }
70  }
71 
94  function get_list_of_tables( $DumpCreationConfig )
95  {
96  try
97  {
98  $ListOfTables = $DumpCreationConfig->get_setting( 'tables' );
99 
100  if( $ListOfTables === 'all' )
101  {
102  return( $this->DatabaseAlgorithms->get_list_of_tables() );
103  }
104  else
105  {
106  return( explode( ',' , $ListOfTables ) );
107  }
108  }
109  catch( Exception $e )
110  {
111  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
112  }
113  }
114 
141  function get_drop_dump( $TableDumpCreationConfig , $TableName )
142  {
143  try
144  {
145  if( $TableDumpCreationConfig->get_setting( 'delete_existing' , false ) )
146  {
147  $TableName = $this->Security->get( $TableName , 'command' );
148 
149  /* removing prefix */
150  $ClearTableName = str_replace(
151  array( $this->Database->TablenamePrefix , 'umx_' ) , array( '' , '' ) , $TableName
152  );
153 
154  return( "DROP TABLE IF EXISTS `[lfb]prefix[rfb]$ClearTableName`;\n" );
155  }
156  else
157  {
158  return( '' );
159  }
160  }
161  catch( Exception $e )
162  {
163  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
164  }
165  }
166 
193  private function get_field_script( $FieldInfo , &$Fields )
194  {
195  try
196  {
197  $FieldName = '`'.get_field( $FieldInfo , 'Field' ).'` ';
198  $FieldType = get_field( $FieldInfo , 'Type' ).' ';
199  $Null = get_field( $FieldInfo , 'Null' ) == 'NO' ? 'NOT NULL ' : '';
200  $Default = get_field( $FieldInfo , 'Default' );
201  $Default = strlen( $Default ) ? "default '$Default' " : '';
202  $Extra = get_field( $FieldInfo , 'Extra' );
203  if( $Extra == 'auto_increment' )
204  {
205  $Fields[] = " PRIMARY KEY ( $FieldName)";
206  }
207  $Extra = strlen( $Extra ) ? "$Extra " : '';
208  return( " $FieldName$FieldType$Null$Default$Extra" );
209  }
210  catch( Exception $e )
211  {
212  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
213  }
214  }
215 
242  function get_fields_dump( $TableDumpCreationConfig , $TableName )
243  {
244  try
245  {
246  $TableName = $this->Security->get( $TableName , 'command' );
247  $Fields = $this->DatabaseAlgorithms->get_list_of_fields( $TableName );
248 
249  foreach( $Fields as $i => $FieldInfo )
250  {
251  $Fields[ $i ] = $this->get_field_script( $FieldInfo , $Fields );
252  }
253 
254  return( implode( ",\n" , $Fields ) );
255  }
256  catch( Exception $e )
257  {
258  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
259  }
260  }
261 
288  function get_auto_increment( $TableDumpCreationConfig , $TableName )
289  {
290  try
291  {
292  $TableName = $this->Security->get( $TableName , 'command' );
293 
294  $Fields = $this->DatabaseAlgorithms->get_list_of_fields( $TableName );
295 
296  foreach( $Fields as $i => $FieldInfo )
297  {
298  $Extra = get_field( $FieldInfo , 'Extra' );
299 
300  if( $Extra == 'auto_increment' )
301  {
302  $FieldName = get_field( $FieldInfo , 'Field' );
303  $MaxValue = $this->Database->select( "MAX( $FieldName ) AS max_field_value" , "`$TableName`" );
304  $MaxValue = intval( get_field( $MaxValue[ 0 ] , 'max_field_value' ) ) + 1;
305 
306  return( "AUTO_INCREMENT=$MaxValue" );
307  }
308  }
309 
310  return( '' );
311  }
312  catch( Exception $e )
313  {
314  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
315  }
316  }
317 
344  function get_create_dump( $TableDumpCreationConfig , $TableName )
345  {
346  try
347  {
348  $TableName = $this->Security->get( $TableName , 'command' );
349 
350  /* removing prefix */
351  $ClearTableName = str_replace(
352  array( $this->Database->TablenamePrefix , 'umx_' ) , array( '' , '' ) , $TableName
353  );
354  $Dump = "CREATE TABLE `[lfb]prefix[rfb]$ClearTableName` (\n";
355 
356  $Dump .= $this->get_fields_dump( $TableDumpCreationConfig , $TableName );
357 
358  $Encoding = $TableDumpCreationConfig->get_setting( 'encoding' , 'utf8' );
359  $Autoincrement = $this->get_auto_increment( $TableDumpCreationConfig , $TableName );
360  $Dump .= "\n) $Autoincrement DEFAULT CHARSET=$Encoding;\n\n";
361 
362  return( $Dump );
363  }
364  catch( Exception $e )
365  {
366  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
367  }
368  }
369 
396  function get_structure_dump( $TableDumpCreationConfig , $TableName )
397  {
398  try
399  {
400  if( $TableDumpCreationConfig->get_setting( 'delete_existing' , false ) )
401  {
402  $Dump = $this->get_drop_dump( $TableDumpCreationConfig , $TableName );
403 
404  $Dump .= $this->get_create_dump( $TableDumpCreationConfig , $TableName );
405 
406  return( $Dump );
407  }
408  else
409  {
410  return( '' );
411  }
412  }
413  catch( Exception $e )
414  {
415  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
416  }
417  }
418 
441  private function prepare_record( $Record )
442  {
443  try
444  {
445  $PlaceHolders = array( "\r" , "\n" , '[r]' , '[n]' , "'" );
446 
447  $Data = array( '\r' , '\n' , '\r' , '\n' , '&#039;' );
448 
449  $Record = str_replace( $PlaceHolders , $Data , $Record );
450 
451  return( "( '".implode( "' , '" , $Record )."' )" );
452  }
453  catch( Exception $e )
454  {
455  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
456  }
457  }
458 
489  function get_insert_dump( $TableName , &$Fields , &$Records )
490  {
491  try
492  {
493  $Dump = '';
494 
495  if( isset( $Records[ 0 ] ) )
496  {
497  if( strpos( $TableName , $this->Database->TablenamePrefix ) === 0 )
498  {
499  $ClearTableName = substr( $TableName , strlen( $this->Database->TablenamePrefix ) );
500  }
501  $ClearTableName = str_replace( 'umx_' , '', $ClearTableName );
502  $Dump = "INSERT INTO `[lfb]prefix[rfb]$ClearTableName` ( $Fields ) VALUES \n";
503 
504  foreach( $Records as $i => $Record )
505  {
506  $Records[ $i ] = $this->prepare_record( $Record );
507  }
508 
509  $Dump .= implode( ",\n" , $Records ).";\n\n";
510  }
511 
512  return( $Dump );
513  }
514  catch( Exception $e )
515  {
516  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
517  }
518  }
519 
546  function get_data_dump( $TableDumpCreationConfig , $TableName )
547  {
548  try
549  {
550  $Dump = '';
551 
552  if( $TableDumpCreationConfig->get_setting( 'records' , false ) )
553  {
554  $Fields = get_field_ex( $this->DatabaseAlgorithms->get_list_of_fields( $TableName ) , 'Field' );
555  $Fields = '`'.implode( '` , `' , $Fields ).'`';
556  $this->Database->query_as( DB_ARRAY );
557  $Records = $this->Database->select( '*' , "`$TableName`" );
558 
559  $Dump = $this->get_insert_dump( $TableName , $Fields , $Records );
560  }
561 
562  return( $Dump );
563  }
564  catch( Exception $e )
565  {
566  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
567  }
568  }
569 
596  function get_dump_of_table( $TableName , &$DumpCreationConfig )
597  {
598  try
599  {
600  $DefaultConfig = 'records;structure;enecoding=utf8;delete_existing';
601  $TableDumpCreationConfig = get_package_object( 'settings::settings' , 'last' , __FILE__ );
602  $TableDumpCreationConfig->load_settings(
603  $DumpCreationConfig->get_setting( $TableName , $DefaultConfig )
604  );
605 
606  $Dump = $this->get_structure_dump( $TableDumpCreationConfig , $TableName );
607 
608  $Dump .= $this->get_data_dump( $TableDumpCreationConfig , $TableName );
609 
610  return( $Dump );
611  }
612  catch( Exception $e )
613  {
614  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
615  }
616  }
617 
640  function get_dump( $DumpName )
641  {
642  try
643  {
644  $DumpCreationConfig = get_package_object( 'settings::settings' , 'last' , __FILE__ );
645  $DumpName = $this->Security->get( $DumpName , 'command' );
646  $DumpCreationConfig->load_file( dirname( __FILE__ )."/conf/$DumpName" , '#' );
647 
648  $Dump = '';
649  $Tables = $this->get_list_of_tables( $DumpCreationConfig );
650  foreach( $Tables as $i => $TableName )
651  {
652  $Dump .= $this->get_dump_of_table( $TableName , $DumpCreationConfig );
653  }
654 
655  return( rtrim( $Dump , "\n" ) );
656  }
657  catch( Exception $e )
658  {
659  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
660  }
661  }
662 
685  function view( $Options )
686  {
687  try
688  {
689  if( $Options->get_setting( 'dump_settings_form' , 0 ) == 1 )
690  {
691  }
692  elseif( $Options->get_setting( 'dump' , 0 ) == 1 )
693  {
694  $Dump = $this->get_dump( 'full_dump' );
695 
696  header( 'HTTP/1.0 200 OK' );
697  header( 'Content-type: application/octet-stream' );
698  header( "Content-Length: ".strlen( $Dump ) );
699  header( "Content-Disposition: attachment; filename=\"data.sql\"" );
700  header( 'Connection: close' );
701 
702  return( $Dump );
703  }
704  else
705  {
706  return( '' );
707  }
708  }
709  catch( Exception $e )
710  {
711  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
712  }
713  }
714  }
715 
716 ?>