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

Source for file MySQLDatabaseHost.class.php

Documentation is available at MySQLDatabaseHost.class.php

  1. <?php
  2. /*
  3. if( !extension_loaded( 'mysql' ) )
  4.   dl( 'mysql.so' );
  5. */
  6. /**
  7.  * For including this file you have to define the constant "CLASSPATH".
  8.  * Because every include in the framework depends on the CLASSPATH definition.
  9.  * The CLASSPATH means the relative path to the folder that contains the
  10.  * framework GilliGan.
  11.  *
  12.  * @package    databases
  13.  * @subpackage mysql
  14.  */
  15.  
  16. if!defined"CLASSPATH" ) )
  17. {
  18.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  19.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  20.   exit();
  21. }
  22.  
  23. /**
  24.  *
  25.  */
  26. require_onceCLASSPATH."settings.inc.php" );
  27. /**
  28.  *
  29.  */
  30. require_onceCLASSPATH."databases/ABSTDatabaseHost.class.php" );
  31. /**
  32.  *
  33.  */
  34. include_onceCLASSPATH."databases/mysql/MySQLDatabase.class.php" );
  35. /**
  36.  *
  37.  */
  38. include_onceCLASSPATH."filesystem/Files.class.php" );
  39. /**
  40.  *
  41.  */
  42. include_onceCLASSPATH."filesystem/FilesystemToolkit.class.php" );
  43. /**
  44.  *
  45.  */
  46. include_onceCLASSPATH."communication/FTPHost.class.php" );
  47.  
  48. /**
  49.  * @package    databases
  50.  * @subpackage mysql
  51.  *
  52.  * @version    0.2.0
  53.  * @author     Daniel Plücken <daniel@debakel.net>
  54.  * @license    http://www.gnu.org/copyleft/lesser.html
  55.  *              GNU Lesser General Public License
  56.  * @copyright  Copyright (C) 2004 Daniel Plücken <daniel@debakel.net>
  57.  *
  58.  *  This library is free software; you can redistribute it and/or
  59.  *  modify it under the terms of the GNU Lesser General Public
  60.  *  License as published by the Free Software Foundation; either
  61.  *  version 2.1 of the License.
  62.  *
  63.  *  This library is distributed in the hope that it will be useful,
  64.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  65.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66.  *  GNU Lesser General Public License for more details.
  67.  *
  68.  *  You should have received a copy of the GNU Lesser General
  69.  *  Public License along with this library; if not, write to the
  70.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  71.  *  Boston, MA 02111-1307 USA
  72.  */
  73. {
  74.   /**
  75.    * This reference of a FTP object is for the use that dumps should save on the
  76.    * server via FTP, cause of SAFE MODE restriction.
  77.    *
  78.    * @access private
  79.    * @var    FTPHost $ftp 
  80.    */
  81.   var $ftp = null;
  82.   /**
  83.    * Stores whether a persistant connection should be use.
  84.    *
  85.    * @access private
  86.    * @var    FTPHost $ftp 
  87.    */
  88.   var $bool_persistant_connection = false;
  89.  
  90.  
  91.   /**
  92.     * Constructor
  93.     *
  94.     * @version 1.1
  95.     * @since   0.1.0
  96.     * @author  Daniel Plücken <daniel@debakel.net>
  97.     * @access  public
  98.     * @param   string  $host* 
  99.     * @param   string  $user* 
  100.     * @param   string  $password* 
  101.     * @param   string  $port* 
  102.     * @return  MySQLDatabaseHost 
  103.     */
  104.   function MySQLDatabaseHost(
  105.                                $host     "localhost",
  106.                                $user     "",
  107.                                $password "",
  108.                                $port     3306
  109.                             )
  110.   {
  111.     parent::__constructor(
  112.                              $host,
  113.                              $user,
  114.                              $password,
  115.                              $port
  116.                          );
  117.   }
  118.  
  119.  
  120.  
  121.   /**
  122.    * Sets whether a persistant connection should be use.
  123.    *
  124.    * @version 1.0
  125.    * @since   0.2.0
  126.    * @author  Daniel Plücken <daniel@debakel.net>
  127.    * @access  public
  128.    * @param   boolean $boolean 
  129.    * @return  void 
  130.    */
  131.   function setPersistantConnection$boolean true )
  132.   {
  133.      if is_bool$boolean ) )
  134.      $this->bool_persistant_connection = $boolean;
  135.   }
  136.  
  137.  
  138.  
  139.   /**
  140.    * Adds a databasetable's reference to this object of a database.
  141.    *
  142.    * @version 1.3
  143.    * @since   0.1.0
  144.    * @author  Daniel Plücken <daniel@debakel.net>
  145.    * @access  public
  146.    * @param   object  $dbt_obj 
  147.    * @return  void 
  148.    */
  149.   function addDB&$db_obj )
  150.   {
  151.     parent::addDB$db_obj );
  152.  
  153.     {
  154.       $db_obj->query(
  155.                        "CREATE TABLE IF NOT EXISTS __admin_mysql_errorlogs ( "
  156.                          ."id INT(10) UNSIGNED NOT NULL auto_increment, "
  157.                          ."script_name VARCHAR(255) DEFAULT NULL, "
  158.                          ."time_first DATETIME NOT NULL, "
  159.                          ."time_last DATETIME NOT NULL, "
  160.                          ."active TINYINT(1) UNSIGNED NOT NULL DEFAULT '1', "
  161.                          ."mysql_query TEXT, "
  162.                          ."mysql_error TEXT, "
  163.                          ."mysql_errno INT(5) UNSIGNED NOT NULL DEFAULT '0', "
  164.                          ."comment TEXT, "
  165.                          ."PRIMARY KEY ( `id` ) "
  166.                        .") TYPE = MyISAM;"true
  167.                     );
  168.     }
  169.   }
  170.  
  171.  
  172.  
  173.   /**
  174.    * Sets the references to objects of databases to the attribute $this -> db.
  175.    *
  176.    * @version 1.1
  177.    * @since   0.1.3
  178.    * @author  Daniel Plücken <daniel@debakel.net>
  179.    * @access  public
  180.    * @param   string  $regExp   Makes sure that only databases were stored, which
  181.    *                             names matching the given regular expression.
  182.    * @param   string  $regExp2  Makes sure that only tables of a Database were
  183.    *                             stored, which names matching the given regular
  184.    *                             expression.
  185.    * @param   boolean $resTab   Cause whether the tables of the researched
  186.    *                             databases should be research too.
  187.    * @param   boolean $resFld   Cause whether the tables's fields of the
  188.    *                             researched tables should be research too.
  189.    * @return  void 
  190.    */
  191.   function researchDatabases(
  192.                               $regExp  "",
  193.                               $regExp2 "",
  194.                               $resTab  true,
  195.                               $resFld  false
  196.                             )
  197.   {
  198.      if!$this->is_connected )
  199.        $this->connect();
  200.  
  201.      if$this->is_connected )
  202.      {
  203.         $db_list mysql_list_dbs$this->resID );
  204.  
  205.         unset$this->db );
  206.         $i 0;
  207.         $cnt mysql_num_rows$db_list );
  208.         while$i $cnt )
  209.         {
  210.            $tmp_name mysql_db_name$db_list$i );
  211.  
  212.            ifempty$regExp ) )
  213.            {
  214.               $this->db[$inew MySQLDatabase$tmp_name$this );
  215.               if(
  216.                   $resTab
  217.                || !empty$regExp2 )
  218.                 )
  219.                 $this->db[$i]->researchTables$regExp2$resFld );
  220.            }
  221.            else
  222.            ifpreg_match$regExp$tmp_name ) )
  223.            {
  224.               $t count$this->db );
  225.               $this->db[$tnew MySQLDatabase$tmp_name$this );
  226.               if(
  227.                   $resTab
  228.                || !empty$regExp2 )
  229.                 )
  230.                 $this->db[$t]->researchTables$regExp2$resFld );
  231.            }
  232.  
  233.            $i++;
  234.         }
  235.      }
  236.   }
  237.  
  238.  
  239.  
  240.   /**
  241.    * Dumps this Databasehost. If this object has stored ftp data the dump method
  242.    * "PHP" will be used and the dumps will be stored on the ftp server which is
  243.    * stated in the ftp data. Otherwise you can use two different methods to
  244.    * store the created dumps depending on the value of the constant
  245.    * "MYSQL_DUMP_KIND", which can be modified in the file "settings.inc.php".
  246.    * You can use the fast "MYSQLDUMP" method, if your hosting allows to use the
  247.    * function exec() and you have quoted the right path to mysqldump in the
  248.    * constant "PATH2MYSQLDUMP", also changeable in the file "settings.inc.php",
  249.    * or you can use the method "PHP". The method "PHP" do not need an external
  250.    * program but is slower than the method "MYSQLDUMP".
  251.    *
  252.    *
  253.    * @version 1.2
  254.    * @since   0.1.4
  255.    * @author  Daniel Plücken <daniel@debakel.net>
  256.    * @access  public
  257.    * @param   string  $target 
  258.    * @param   string  $regExp       Makes sure that only databases were stored,
  259.    *                                 which names matching the given regular
  260.    *                                 expression.
  261.    * @param   string  $regExp2      Makes sure that only tables of a Database
  262.    *                                 were stored, which names matching the given
  263.    *                                 regular expression.
  264.    * @param   boolean $full_inserts 
  265.    * @param   boolean $drop_order 
  266.    * @return  boolean 
  267.    */
  268.   function dump(
  269.                  $target       "./__dumps",
  270.                  $regExp       "",
  271.                  $regExp2      "",
  272.                  $full_inserts false,
  273.                  $drop_order   false
  274.                )
  275.   {
  276.      $this->researchDatabases$regExp$regExp2 );
  277.  
  278.      if (
  279.           !function_exists"is_a" )
  280.        && get_class$this->ftp == "ftphost"
  281.        || function_exists"is_a" )
  282.        && is_a$this->ftp"FTPHost" )
  283.         )
  284.      {
  285.          $tmp_arr FilesystemToolkit
  286.                   ::separatePathAndFile$_SERVER["SCRIPT_NAME");
  287.  
  288.          if (
  289.               empty $tmp_arr[0)
  290.            || strpos$target"/" === 0
  291.             )
  292.             $ftp_path $target;
  293.          else
  294.             $ftp_path "/".FilesystemToolkit
  295.                           ::getPurePathFromTo$tmp_arr[0]$target );
  296.  
  297.          if $this->ftp->file_exists$target ) )
  298.             $this->ftp->rename$ftp_path$ftp_path."_".date"Y-m-d_H:i" ) );
  299.  
  300.          if !$this->ftp->file_exists$target ) )
  301.             $this->ftp->mkdir$ftp_path0777 );
  302.  
  303.          for $i 0$i count$this->db )$i++ )
  304.          {
  305.              if !$this->ftp->file_exists$target."/".$this->db[$i]->name ) )
  306.                 $this->ftp->mkdir$ftp_path."/".$this->db[$i]->name0777 );
  307.  
  308.              for $k 0$k count$this->db[$i]->dbt )$k++ )
  309.              {
  310.                  echo $this->db[$i]->dbt[$k]->getLastQuery()."<br />\r\n";
  311.  
  312.                  $this->ftp->saveFile(
  313.                              $ftp_path."/".$this->db[$i]->name."/"
  314.                             .$this->db[$i]->dbt[$k]->name.".sql",
  315.                              $this->db[$i]->dbt[$k]->getDump(
  316.                                                          "",
  317.                                                          $full_inserts,
  318.                                                          $drop_order
  319.                                                             )
  320.                                      );
  321.              }
  322.          }
  323.      }
  324.      else
  325.      {
  326.          if!is_dir$target ) )
  327.              mkdir$target0777 );
  328.  
  329.          $old @umask);
  330.          @chmod$target0777 );
  331.          @umask$old );
  332.  
  333.          for$i 0$i count$this->db )$i++ )
  334.          {
  335.              if!is_dir$target."/".$this->db[$i]->name ) )
  336.                mkdir$target."/".$this->db[$i]->name0777 );
  337.  
  338.              $old @umask);
  339.              @chmod$target."/".$this->db[$i]->name0777 );
  340.              @umask$old );
  341.  
  342.              echo "<b>Database</b> ".$this->db[$i]->name."<br />\r\n";
  343.  
  344.              ifstrtoupperMYSQL_DUMP_KIND == "MYSQLDUMP" )
  345.                  for$k 0$k count$this->db[$i]->dbt )$k++ )
  346.                  {
  347.                    $tmp_data_order $this->db[$i]->name." "
  348.                                     .$this->db[$i]->dbt[$k]->name." > "
  349.                                     .$target."/".$this->db[$i]->name."/"
  350.                                     .$this->db[$i]->dbt[$k]->name.".sql";
  351.  
  352.                    $tmp_exec FilesystemToolkit::getCleanPathPATH2MYSQLDUMP )
  353.                               ."/mysqldump "
  354.                                          .(
  355.                                             $drop_order
  356.                                             ? "--add-drop-table "
  357.                                             : ""
  358.                                           )
  359.                                          ."--host=".$this->name." "
  360.                                          ."--password=".$this->password." "
  361.                                          ."--user=".$this->user." "
  362.                                          ."--port=".$this->port." "
  363.                                          #."--lines-terminated-by=\\r\\n "
  364.                               .$tmp_data_order;
  365.  
  366.                    exec$tmp_exec );
  367.  
  368.                    echo $tmp_data_order;
  369.                    echo "<br />\r\n";
  370.                  }
  371.              else
  372.                  for$k 0$k count$this->db[$i]->dbt )$k++ )
  373.                  {
  374.                    $f new Files(
  375.                                    $target."/".$this->db[$i]->name."/"
  376.                                   .$this->db[$i]->dbt[$k]->name.".sql"
  377.                                  );
  378.  
  379.                    $f->setContent(
  380.                                    $this->db[$i]->dbt[$k]->getDump(
  381.                                                                "",
  382.                                                                $full_inserts,
  383.                                                                $drop_order
  384.                                                                   )
  385.                                  );
  386.                    echo $this->db[$i]->dbt[$k]->getLastQuery()."<br />\r\n";
  387.                    $f->save();
  388.  
  389.                    $old @umask);
  390.                    @chmod(
  391.                             $target."/".$this->db[$i]->name."/"
  392.                            .$this->db[$i]->dbt[$k]->name.".sql",
  393.                             0777
  394.                          );
  395.                    @umask$old );
  396.                  }
  397.          }
  398.      }
  399.   }
  400.  
  401.  
  402.  
  403.   /**
  404.    * Researches a whole folder to import dumped data from it.
  405.    *
  406.    * @version 1.0
  407.    * @since   0.1.9
  408.    * @author  Daniel Plücken <daniel@debakel.net>
  409.    * @access  public
  410.    * @param   string  $source The source folder
  411.    * @param   string  $regExpFolder Makes sure that only folder will be
  412.    *                                 imported, which names matching the given
  413.    *                                 regular expression.
  414.    * @param   string  $regExpFiles  Makes sure that only files will be imported,
  415.    *                                 which names matching the given regular
  416.    *                                 expression.
  417.    * @return  boolean 
  418.    */
  419.   function importDump(
  420.               $source "./__dumps",
  421.               $regExpFolder "",
  422.               $regExpFiles ""
  423.                      )
  424.   {
  425.     $tmp_arr FilesystemToolkit::loadFolderContent(
  426.                                         $source"^ &$"$regExpFolder
  427.                                                    );
  428.  
  429.     $this->researchDatabases();
  430.  
  431.     for $i 0$i count$tmp_arr )$i++ )
  432.       if $tmp_arr[$i]["is_dir")
  433.         for $k 0$k count$this->db )$k++ )
  434.           if $this->db[$k]->name == $tmp_arr[$i]["name")
  435.              $this->db[$k]->importDump(
  436.                                 $source."/".$tmp_arr[$i]["name"],
  437.                                 $regExpFiles
  438.                                       );
  439.   }
  440.  
  441.  
  442.  
  443.   /**
  444.    * Adds ftp data to this object. This can be useful for writing dumps if the
  445.    * sever has SAFE MODE restrictions.
  446.    *
  447.    * @version 1.0
  448.    * @since   0.1.7
  449.    * @author  Daniel Plücken <daniel@debakel.net>
  450.    * @access  public
  451.    * @param   string  $host 
  452.    * @param   string  $user 
  453.    * @param   string  $password 
  454.    * @param   string  $port 
  455.    * @return  void 
  456.    */
  457.   function addFTPData(
  458.                        $host,
  459.                        $user,
  460.                        $password,
  461.                        $port 21
  462.                      )
  463.   $this->ftp = new FTPHost$host$user$password$port )}
  464.  
  465.  
  466.  
  467.   /**
  468.    * Opens a connection to the database server.
  469.    *
  470.    * @version 1.1
  471.    * @since   0.1.0
  472.    * @author  Daniel Plücken <daniel@debakel.net>
  473.    * @access  public
  474.    * @param   boolean $boolean_persitant 
  475.    * @return  boolean 
  476.    */
  477.   function connect$boolean_persitant false )
  478.   {
  479.     if(
  480.          (
  481.            $boolean_persitant
  482.         || $this->bool_persistant_connection
  483.          )
  484.       && $this->resID = @mysql_pconnect(
  485.                                          $this->name.":".$this->port,
  486.                                          $this->user,
  487.                                          $this->password
  488.                                        )
  489.       )
  490.     {
  491.       $this->is_connected = true;
  492.       return true;
  493.     }
  494.     else
  495.     if(
  496.          $this->resID = @mysql_connect(
  497.                                         $this->name.":".$this->port,
  498.                                         $this->user,
  499.                                         $this->password
  500.                                       )
  501.       )
  502.     {
  503.       $this->is_connected = true;
  504.       return true;
  505.     }
  506.  
  507.     $this->is_connected = false;
  508.     return false;
  509.   }
  510.  
  511.  
  512.  
  513.   /**
  514.    * Closes the current connection to the database server.
  515.    *
  516.    * @version 1.0
  517.    * @since   0.1.0
  518.    * @author  Daniel Plücken <daniel@debakel.net>
  519.    * @access  public
  520.    */
  521.   function disconnect()
  522.   {
  523.      if mysql_close() )
  524.         $this->is_connected = false;
  525.   }
  526. // End of class MySQLDatabase
  527. ?>

Documentation generated on Thu, 05 Jun 2008 19:14:06 +0200 by phpDocumentor 1.4.1