Source for file functions_mySQL.phpinc

Documentation is available at functions_mySQL.phpinc

  1. <?php
  2. /**
  3.   * MYSQL update functions
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    dbms
  7.   *
  8.   * @author     Peter Krebs (pk)<p.krebs@amicas.at>
  9.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   ***/
  12.  
  13. function DB_UPDATE_backup({
  14.     include __OCSP_PROJECTPATH__."config" _OCSP_DIRSEP_ "mySQL.conf";
  15.  
  16.     $cmd="mysqldump ".$DBCONF['ADMIN']['USR']." ".$DBCONF['ADMIN']['PWD'];
  17.     $cmd.=" ".$DBCONF['DATABASE']." >".$GLOBALS['PROJECT']['BACKUPPATH'].time().".mysqldump";
  18.  
  19.     require_once __OCSP_PHPINCPATH__."common" _OCSP_DIRSEP_ "pcf_directory.phpinc";
  20.     pcf_checkDir($GLOBALS['PROJECT']['BACKUPPATH'],TRUE);
  21.     // echo "<pre>$cmd</pre>";
  22.     system($cmd);
  23.  
  24. }
  25.  
  26. /**
  27.   * backups a table with serialized arrays
  28.   *
  29.   * @param string $table 
  30.   *
  31.   * @return boolean 
  32.   *
  33.   * @since pk-05-06-16
  34.   *
  35.   ***/
  36. function DB_UPDATE_backupTable($table{
  37.     if (empty($table)) return FALSE;
  38.  
  39.     $query="SELECT * FROM $table";
  40.     if ($cursor=OCSP_OBJ::defaultReadDBObj()->query($query)) {
  41.         $bakFileNameOCSP_CONF::getInstance('OCSP')->getValue('BACKUPPATH').$table.".bak";
  42.         require_once __OCSP_PHPINCPATH__."common/pcf_directory.phpinc";
  43.         if (!pcf_checkDir(OCSP_CONF::getInstance('OCSP')->getValue('BACKUPPATH'),True)) return False;
  44.  
  45.         if ($hBF=fopen($bakFileName,"w")) {
  46.             fputs($hBF,"<?php \n\n");
  47.             fputs($hBF,"/* Backup of $table created on".date("r",time())." */\n");
  48.             fputs($hBF,"\$timeStamp=".time().";\n");
  49.  
  50.             $i=1;
  51.             while($row=$cursor->fetchArrayFld()) {
  52.                  fputs($hBF,"\$row[".$i++."]=unserialize(base64_decode(\"".base64_encode(serialize($row))."\"));\n");
  53.             }
  54.             fputs($hBF,"\n?>");
  55.             fclose($hBF);
  56.         else {
  57.             return FALSE;
  58.         }
  59.     else {
  60.         return FALSE;
  61.     }
  62. }
  63.  
  64.  
  65. function DB_UPDATE_execCmd($cmd{
  66.     require_once dirname(dirname(__FILE__))."/OCSP_DB_mySQL.phpclass";
  67.     $DB=new OCSP_DB_mySQL();
  68.     $DB->connect(__OCSP_PROJECTPATH__ ."config/mySQL.conf",FALSE,TRUE);
  69.     try {
  70.         if (!$DB->executeCmd($cmd)) {
  71.             echo $DB->errorMsg();
  72.         }        
  73.     catch (Exception $e{
  74.         echo $e->getMessage();
  75.     }
  76.     $DB->disconnect();
  77. }
  78.  
  79. /**
  80.   * stores the new version of table in the database
  81.   * and clears $GLOBALS['PROJECT']['OBJCACHEPATH']
  82.   *
  83.   * @param array $tblArray data to store to T_SYS_TABLES
  84.   * @param boolean $debug 
  85.   *
  86.   * @version pk-06-05-24 (clear cache added)
  87.   *
  88.   ***/
  89. function DB_UPDATE_setVersion($tblArray,$debug=FALSE{
  90.  
  91.     echo "<p>UPDATING Table: ".$tblArray['TBL_NAME']." to Version: ".$tblArray['TBL_VERSION']."</p>";
  92.     if ($debugecho "<pre>".print_r($tblArray,TRUE)."</pre>";
  93.  
  94.     if (!array_key_exists("TBL_LASTCHANGE",$tblArray)) {
  95.         if (!OCSP_OBJ::defaultReadDBObj()->quickQuery("SELECT TBL_LASTCHANGE FROM T_SYS_TABLES")) {
  96.             DB_UPDATE_execCmd("ALTER TABLE T_SYS_TABLES ADD TBL_LASTCHANGE TIMESTAMP NOT NULL");
  97.         }
  98.     }
  99.     $tblArray['TBL_LASTCHANGE']="NULL";
  100.     $tblArray['TBL_NAME']="'".$tblArray['TBL_NAME']."'";
  101.  
  102.     $cmd="REPLACE INTO T_SYS_TABLES (";
  103.     $values=" VALUES(";
  104.     $sep="";
  105.  
  106.     if ($debugecho "<pre>".print_r($tblArray,TRUE)."</pre>";
  107.     foreach($tblArray as $fld=>$val{
  108.         $cmd   .=$sep.$fld;
  109.         $values.=$sep.$val;
  110.         $sep=",";
  111.     }
  112.     $cmd.=")".$values.")";
  113.     if ($debugecho "<pre>$cmd</pre>";
  114.     DB_UPDATE_execCmd($cmd);
  115.  
  116.     require_once __OCSP_PHPINCPATH__."common/pcf_directory.phpinc";
  117.     if ($a_cachefiles=pcf_getDirList(OCSP_OBJ::getConf('OBJCACHEPATH'),"!D",$debug)) {
  118.         foreach($a_cachefiles as $s_file{
  119.             unlink(OCSP_OBJ::getConf('OBJCACHEPATH')."/".$s_file);
  120.         }
  121.     }
  122.  
  123. }
  124.  
  125. /**
  126.   * executs a sql script file
  127.   *
  128.   * @param  string  $file   path tho file
  129.   * @param  bool    $debug 
  130.   *
  131.   ***/
  132. function DB_UPDATE_execScript($file,$debug=FALSE{
  133.     if ($debugecho "<p><b>DB_UPDATE_execScript($file,$debug)</b></p>";
  134.     if (file_exists($file)) {
  135.         if ($fp fopen($file'rb')) {
  136.             $content fread($fpfilesize($file));
  137.             fclose($fp);
  138.         else {
  139.             return FALSE;
  140.         }
  141.  
  142.         $clen=strlen($content);
  143.         $isComment=FALSE;       // # found and not eol reached
  144.         $strOpen="";            // empty if we are not in a string or the string start char " or '
  145.         $cmd="";                // the sql cmd to execute
  146.         for ($i 0$i $clen++$i{
  147.             $char $content[$i];
  148.             if ((empty($strOpen)) && ($char=="#")) {
  149.                 $isComment=TRUE;
  150.             else if (($isComment&& ($char=="\n")) {
  151.                 $isComment=FALSE;
  152.             else if ((empty($strOpen)) && ($char==";")) {
  153.                 $cmd=trim($cmd);
  154.                 if ($debugecho "<pre style='font-size=10px;'>".htmlspecialchars($cmd)."</pre><hr>"}
  155.                 DB_UPDATE_execCmd($cmd);
  156.                 $cmd="";
  157.             else if (!$isComment{
  158.                 $cmd.=$char;
  159.                 if (!empty($strOpen)) {
  160.                     if ($char==$strOpen)               $strOpen="";     // end of a string value
  161.                 else {
  162.                     if (($char=="\""|| ($char=="'")) $strOpen=$char;  // beginn of a string value
  163.                 }
  164.             }
  165.         }
  166.         return TRUE;
  167.     }
  168.     return FALSE;
  169. }
  170.  
  171. /**
  172.   * checks a form and adds fields if not in the form
  173.   *
  174.   * @param string $frmObj_b64 base64_encoded serialized object
  175.   * @param boolean $debug 
  176.   *
  177.   * @returns boolean
  178.   *
  179.   * @since pk-05-11-24
  180.   *
  181.   ***/
  182. function DB_UPDATE_checkForm($frmObj_b64,$debug=FALSE{
  183.     if (!empty($frmObj_b64)) {
  184.         require_once $GLOBALS['OCSP']['PHPFORMPATH']."forms.phpinc";
  185.         DBMS_field_IncludeAllSrc($debug);
  186.  
  187.         $o_frmObj=unserialize(base64_decode($frmObj_b64));
  188.         if ($o_frmObj->getName()) {
  189.             if ($o_oriForm=DBMS_form_loadName($o_frmObj->getName())) {
  190.                 echo "<p>form ".$o_frmObj->getName()." found</p>";
  191.                 foreach($o_frmObj->frmFields as $key => $fldObj{
  192.  
  193.                     if (!isset($o_oriForm->frmFields[$key])) {
  194.                         $fldObj->setFrmId($o_oriForm->getId());
  195.                         $o_oriForm->frmFields[$key]=$fldObj;
  196.                         echo "<p>Field$key added<p>";
  197.                     }
  198.                 }
  199.                 try {
  200.                     $o_oriForm->storeToDb();
  201.                 catch(Exception $e{
  202.                     echo $e->getMessage();
  203.                 }
  204.                 return TRUE;
  205.             else {
  206.                 $o_frmObj->frmId=0;
  207.                 $o_frmObj->storeToDb();
  208.                 echo "<p>form ".$o_frmObj->getName()." angelegt</p>";
  209.                 return TRUE;
  210.             }
  211.         }
  212.     }
  213.  
  214. }
  215.  
  216. ?>

Documentation generated on Thu, 08 Jan 2009 17:44:19 +0100 by phpDocumentor 1.4.0a2