Location: PHPKode > projects > OpenBizMap > obm2007.1/mydata/script/odb_hotfix.txt
<?php
/*
OpenDataBag - Data Web Interface
Copyright (C) 2004 Nawara

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

// INIT  ---------------------------------------------
define('cfg_system_path', getcwd().'/odb_www');
include(cfg_data_path.'/script/inc_functions.txt');
include(cfg_data_path.'/script/inc_data.txt');
include(cfg_data_path.'/script/sec_basic.txt');

// HOTFIX  ---------------------------------------------
$delim = ',';
$encl = '"';
$optional=1;
$deamonid=date('Ymd His ').rand(100,999);
set_time_limit(0);
echo(INFO.' '.$deamonid.CRLF);

while(check_version())
{
  if(0)
    $file_array[cfg_data_path.'/data/dat_chsaab220.txt']=1;
  else
  {
    $res=opendir(cfg_data_path.'/data');
    while($fl = readdir($res))
      if(substr($fl,0,4)=='dat_' and $fl!='dat_.txt')
        $file_array[cfg_data_path.'/data/'.$fl]=1;
    closedir($res);
  }

  $hotfix_array=array();
  $res=opendir(cfg_data_path.'/config/hotfix');
  while($fl = readdir($res))
    if(substr($fl,-4)=='.csv')
      $hotfix_array[]=cfg_data_path.'/config/hotfix/'.$fl;
  closedir($res);

  $bak_name='_source';
  for($bb=0;$bb<count($hotfix_array);$bb++)
  {
    $all_hotfixed=0;
    echo CRLF. date('d.m.Y H:i').' HOTFIX:'.substr(basename($hotfix_array[$bb]),0,-4).' (reading)';
    //get hotfix
    $reg = '/(('.$encl.')'.($optional?'?(?(2)':'(').
     '[^'.$encl.']*'.$encl.'|[^'.$delim.'\r\n]*))('.$delim.
     '|\r\n)/smi';

    preg_match_all($reg, file_get_contents($hotfix_array[$bb]), $treffer);

    $linecount = 0;
    $fieldcount=0;
    $hotfix_content_check_array=array();
    $hotfix_content_array=array();
    $variable_array=array();
    $name_array=array();
    $sizeof=sizeof($treffer[3]);
    $last_perc=0;

    for($ii=0;$ii<=$sizeof;$ii++)
    {
      $value=$treffer[1][$ii];

      if(substr($value,0,1)==$encl)
        $value=substr($value,1);

      if(substr($value,-1)==$encl)
        $value=substr($value,0,-1);

      if($linecount==0)
        $name_array[$fieldcount]=trim($value);
      else
        $variable_array[$name_array[$fieldcount]]=trim($value);

      $fieldcount++;

      if($treffer[3][$ii]!=$delim)
      {
        if($linecount>0)
        {
          $hotfix_content_array[$variable_array[$name_array[0]]]=$variable_array;
          if(!isset($hotfix_content_check_array[$variable_array[$name_array[0]]]['defined']))
          {
            $hotfix_content_check_array[$variable_array[$name_array[0]]]['defined']=1;
            $hotfix_content_check_array[$variable_array[$name_array[0]]]['used']=0;
          }
          else
            $hotfix_content_check_array[$variable_array[$name_array[0]]]['defined']++;
          $variable_array=array();
          //~ if($linecount%100==0)
            //~ echo "\r". date('d.m.Y H:i').' HOTFIX:'.substr(basename($hotfix_array[$bb]),0,-4).' ('.$linecount.')                 ';
        }
        $linecount++;
        $fieldcount=0;
      }
    }
    echo "\r". date('d.m.Y H:i').' HOTFIX:'.substr(basename($hotfix_array[$bb]),0,-4).' ('.$linecount.')';
    //~ echo '<pre>';
    //~ print_r($hotfix_array);
    //~ echo '</pre>';
    //~ exit;

    //showtime();
    reset($file_array);
    while(list($dat_file,$tmp)=each($file_array))
    {
      $curr_bag=substr(basename($dat_file),4,-4);

      echo CRLF. date('d.m.Y H:i').' BAG:'.$curr_bag;

      $hotfixed=0;
      $fp1=fopen(dirname($dat_file).'/hotfix_'.basename($dat_file),'wb');
      $fp=fopen($dat_file,'rb');
      while(!feof($fp))
      {
        // HOTFIXING ---------------------------------------------
        $line=fgets($fp,5000);
        if(substr($line,0,1)!='$')
          $line='$'.$line;

        $search_value=read($line,$name_array[0]);
        if($search_value and read($line,'sysValid')=='1' and read($line,'sysValidTo')=='99991231')
        {
          $variable_array=$hotfix_content_array[$search_value];
          if(count($variable_array))
          {
            reset($variable_array);
            while(list($variable_name,$variable_value)=each($variable_array))
            {
              if($variable_name!=$name_array[0])
              {
                if($variable_value!=read($line,$variable_name))
                {
                  //$line=str_replace('$'.$variable_name.'=','$HOTFIXED '.$variable_name.'=',$line);
                  $tmp_replace_from='$'.$variable_name.'='.read($line,$variable_name).'$';
                  $tmp_replace_to='$';
                  $line=str_replace($tmp_replace_from,$tmp_replace_to,$line);
                  
                  if($variable_value)
                    $line='$'.$variable_name.'='.$variable_value.$line;
                  $changed=1;
                }
              }
            }

            if($changed)
            {
              $line='$Hotfixed='.date('Ymd').' '.substr(basename($hotfix_array[$bb]),0,-4).$line;
              $hotfixed++;
              $all_hotfixed++;
              $hotfix_content_check_array[$search_value]['used']++;
            }
          }
        }

        fwrite($fp1,$line);
        //~ if($hotfixed%100==0)
          //~ echo "\r". date('d.m.Y H:i').' BAG:'.$curr_bag.' ('.$hotfixed.' '.$all_hotfixed.')                        ';
        // HOTFIXING ---------------------------------------------
      }
      echo "\r". date('d.m.Y H:i').' BAG:'.$curr_bag.' ('.$hotfixed.' '.$all_hotfixed.')                        ';

      fclose($fp);
      fclose($fp1);


      addpath(dirname($dat_file).'_'.$bak_name);
      rename($dat_file,dirname($dat_file).'_'.$bak_name.'/'.basename($dat_file));
      rename(dirname($dat_file).'/hotfix_'.basename($dat_file),$dat_file);
    }

    $bak_name=substr(basename($hotfix_array[$bb]),0,-4);

    reset($hotfix_content_check_array);
    while(list($search_value,$tmp)=each($hotfix_content_check_array))
      if($hotfix_content_check_array[$search_value]['used']!=1 or $hotfix_content_check_array[$search_value]['defined']!=1)
        echo CRLF.'   CHECK HOTFIX:'.substr(basename($hotfix_array[$bb]),0,-4).' '.$name_array[0].'='.$search_value.' Defined='.$hotfix_content_check_array[$search_value]['defined'].' Used='.$hotfix_content_check_array[$search_value]['used'];

    sleep(5);
  }
  exit; //all hotfixes set
}

?>
Return current item: OpenBizMap