<?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.
*/
//reading value from line
$filter_array=array();
function read($line,$key) //:$value
{
$val='';
if(strlen($key))
{
if(strpos('_'.$key,'[')>0)
{
global $filter_array;
$filter='';
$value=$key;
$pos=strpos('_'.$value,'[');
while($pos>0)
{
$endpos=strpos($value,']',$pos);
if($endpos>0)
{
$variable=substr($value.']',$pos,$endpos-$pos);
$read=read($line,$variable);
$value=str_replace('['.$variable.']',$read,$value);
$pos=strpos('_'.$value,'[');
if(strlen($read))
$filter.='['.$variable.'='.$read.'$] ';
else
$filter.='-[$'.$variable.'=] ';
}
else
$pos=0;
}
$val=trim($value);
$filter_array[$key.'***'.$val]=$filter;
}
else
{
$kpos=strpos('_'.$line,'$'.$key.'=');
if($kpos>0)
{
$vpos=strpos('_'.$line,'$',$kpos+2);
$klen=strlen($key);
$val=substr('_'.$line,$kpos+$klen+2,$vpos-$klen-$kpos-2);
}
/*
elseif($key=='FIELD1')
{
$kpos=strpos('_'.$line,'$');
while($kpos>0)
{
$vpos=strpos('_'.$line,'$',$kpos+1);
$epos=strpos('_'.$line,'=',$kpos+1);
if($epos<$vpos)
{
$key=substr('_'.$line,$kpos+1,$epos-$kpos-1);
$klen=strlen($key);
$val=substr('_'.$line,$kpos+$klen+2,$vpos-$klen-$kpos-2);
if(substr($key,0,3)!='sys')
break;
}
$kpos=strpos('_'.$line,'$',$vpos);
}
}
*/
}
}
return($val);
}
function delete($mybag,$id) //:id
{
//closing existing ones
//echo 'DELETE';
if($id)
{
$fp=fopen(cfg_data_path.'/data/dat_'.purename($mybag).'.txt','r+b');
while(!feof($fp))
{
$ftell=ftell($fp);
$line=odb_fgets($fp);
//echo read($line,'sysId').' ';
if(read($line,'sysId')==$id)
if(read($line,'sysValidTo')>date('Ymd') and read($line,'sysValid')=='1' and read($line,'sysValidFrom')<=date('Ymd'))
{
$old_line=$line;
//echo '------'.$ftell;
if(!read($line,'sysValidTo'))
$line=str_replace('$sysValid=1','$sysValid=0',$line);
else
$line=str_replace('$sysValidTo='.read($line,'sysValidTo'),'$sysValidTo='.date('Ymd'),$line);
if(read($line,'sysValidFrom')==date('Ymd'))
$line=str_replace('$sysValid=1','$sysValid=0',$line);
$line=trim($line)."\r\n";
//echo $line;
//exit;
fseek($fp,$ftell,SEEK_SET);
fwrite($fp,$line);
}
}
fclose($fp);
$fp=fopen(cfg_data_path.'/data/dat_'.purename($mybag).'.txt','ab');
fwrite($fp,'$sysId='.$id.'$sysModInfo='.date('Ymd H:i').' '.USERNAME.'$sysComment=Deleted$'."\r\n");
fclose($fp);
$fp=fopen(cfg_data_path.'/data/dat_deleted.txt','ab');
fwrite($fp,trim('$MyBag=deleted$'.str_replace('$MyBag=','$MyOldBag=',$old_line))."\r\n");
fclose($fp);
$fp=fopen(cfg_data_path.'/data/.lastmod','wb');
fwrite($fp,date('Ymd H:i:s'));
fclose($fp);
}
}
function array2line($variable_array)
{
$result='';
reset($variable_array);
while(list($key,$tmp)=each($variable_array))
{
$val=trim($variable_array[$key]);
$val=str_replace("\r\n",' CRLF ',$val);
$val=str_replace("\r",' ',$val);
$val=str_replace("\n",' ',$val);
$val=str_replace("$",' ',$val);
if(strlen($val)>0)
{
$result.='$'.$key.'='.$val;
}
}
return $result.'$';
}
function save($variable_array,$last_bag='--not--set--') //:id
{
$variable_array['sysValid']=1;
//if(!$variable_array['sysValidFrom'])
$variable_array['sysValidFrom']=date('Ymd');
if($last_bag=='--not--set--')
$last_bag=$variable_array['MyBag'];
//if(check_access($variable_array['MyBag'])>1 and check_access($last_bag)>1)
if(1)
{
//echo $variable_array['MyBag'].'='.check_access($variable_array['MyBag']).' ';
if($variable_array['sysId'])
{
//closing existing ones
if($fp=fopen(cfg_data_path.'/data/dat_'.purename($last_bag).'.txt','r+b'))
{
$sysFTell=0+substr($variable_array['sysFTell'],strpos($variable_array['sysFTell'],':')+1,-1);
fseek($fp,$sysFTell,SEEK_SET);
//echo 'DELETE'.$last_bag;
while(!feof($fp))
{
$ftell=ftell($fp);
$line=odb_fgets($fp);
//echo read($line,'sysId').' ';
if(read($line,'sysId')==$variable_array['sysId'])
//if(read($line,'sysValidTo')>date('Ymd') and read($line,'sysValid')=='1' and read($line,'sysValidFrom')<=date('Ymd'))
if(read($line,'sysValidTo')=='99991231')
{
//echo '------'.$ftell.'-----'.$sysFTell.'<br />';
$line=str_replace('$sysValidTo='.read($line,'sysValidTo'),'$sysValidTo='.date('Ymd'),$line);
if(read($line,'sysValidFrom')==date('Ymd'))
$line=str_replace('$sysValid=1','$sysValid=0',$line);
$line=trim($line)."\r\n";
//echo $line.ftell($fp).'<br />';
fseek($fp,$ftell,SEEK_SET);
fwrite($fp,$line);
//database is clear - no need to check anything else
break;
}
}
fclose($fp);
}
}
else
{
//check for imports
global $global_used_id;
$tmp_new_id=time().rand(10000,99999);
while(isset($global_used_id[$tmp_new_id]))
{
sleep(1);
$tmp_new_id=time().rand(10000,99999);
}
$global_used_id[$tmp_new_id]=1;
$variable_array['sysId']=$tmp_new_id;
}
global $_odb_import_username;
if($_odb_import_username)
$variable_array['sysModInfo']=date('Ymd H:i').' '.$_odb_import_username;
elseif(defined('USERNAME'))
$variable_array['sysModInfo']=date('Ymd H:i').' '.USERNAME;
else
$variable_array['sysModInfo']=date('Ymd H:i').' Unknown user';
if(defined('USERNAME'))
$variable_array['sysModBy']=USERNAME;
else
$variable_array['sysModBy']='Unknown user';
$variable_array['sysModDate']=date('Ymd');
if(!$variable_array['sysValidFrom'])
$variable_array['sysValidFrom']=date('Ymd');
if(!$variable_array['sysValidTo'])
$variable_array['sysValidTo']='99991231';
if(!$variable_array['sysGroupBy'])
$variable_array['sysGroupBy']='Record';
//System menu bag setting
if($variable_array['sysGroupBy']=='menu')
$variable_array['MyBag']='menu';
if($variable_array['sysGroupBy']=='dashboard')
$variable_array['MyBag']='dashboard';
if($variable_array['sysGroupBy']=='user')
$variable_array['MyBag']='access';
if($variable_array['sysGroupBy']=='test')
$variable_array['MyBag']='test';
if($variable_array['sysGroupBy']=='articles')
$variable_array['MyBag']='articles';
//Removing artificial value
$variable_array['sysFTell']='';
//print_r($variable_array);
//saving new fields for whole group
$changes=0;
$names_array=array();
$variable_array2=load_config($variable_array['sysGroupBy']);
//print_r($variable_array2);
reset($variable_array);
while(list($variable_name,$tmp)=each($variable_array2))
{
$pure_variable_name=purename($variable_name);
if(!isset($names_array[$pure_variable_name]))
$names_array[$pure_variable_name]=$variable_name;
}
reset($variable_array);
while(list($variable_name,$tmp)=each($variable_array))
{
$pure_variable_name=purename($variable_name);
if(!isset($names_array[$pure_variable_name]))
$names_array[$pure_variable_name]=$variable_name;
$variable_name=$names_array[$pure_variable_name];
if(!isset($variable_array2[$variable_name]))
{
$variable_array2[$variable_name]['tab']='...';
$changes=1;
//echo '--'.$variable_name.'('.$pure_variable_name.')'.CRLF;
}
}
if($changes)
{
save_config($variable_array['sysGroupBy'],$variable_array2);
//echo 'CHANGES:'.$variable_array['sysGroupBy'];
}
//echo $variable_array['sysGroupBy'];
//print_r($variable_array2);
//echo '-------------------------------------------------'.CRLF;
//$result=utf8(array2line($variable_array));
$result=(array2line($variable_array)); //is_utf8 not working for long strings
$tmp_sysLen='$sysLen='.strlen($result);
$result='$sysLen='.(strlen($result)+strlen($tmp_sysLen)+1).$result;
//addpath(cfg_data_path.'/data');
if($fp=fopen(cfg_data_path.'/data/dat_'.purename($variable_array['MyBag']).'.txt','ab'))
{
fwrite($fp,$result.CRLF);
fclose($fp);
$fp=fopen(cfg_data_path.'/data/.lastmod','wb');
fwrite($fp,date('Ymd H:i:s'));
fclose($fp);
}
else
{
echo 'Cannot write: '.cfg_data_path.'/data/dat_'.purename($variable_array['MyBag']).'.txt';
}
//echo $result;
return($variable_array['sysId']);
}
else
{
if(check_access($variable_array['MyBag'])<2)
{
$owner_list=get_owner_list($variable_array['MyBag']);
if($owner_list)
myecho('Cannot save data to bag %1 owned by %2.',$variable_array['MyBag'],$owner_list);
else
myecho('Cannot save data to bag %1.',$variable_array['MyBag']);
echo '<br />';
}
elseif(check_access($last_bag)<2)
{
$owner_list=get_owner_list($last_bag);
if($owner_list)
myecho('Cannot move data from bag %1 owned by %2.',$last_bag,$owner_list);
else
myecho('Cannot move data from bag %1.',$last_bag);
echo '<br />';
}
return(0);
}
}
function shrink() //:void
{
clearstatcache();
$shrink_name=date('Ymd');
addpath(cfg_data_path.'/data/shrink_'.$shrink_name);
//shrinking data
$res=opendir(cfg_data_path.'/data');
while($fl = readdir($res))
{
if(substr($fl,0,4)=='dat_' and substr($fl,-4)=='.txt')
{
$fl=$fl;
$flh='dath_'.substr($fl,4);
// Preparing history dath_ file
if(!file_exists(cfg_data_path.'/data/'.$flh))
{
fopen(cfg_data_path.'/data/'.$flh,'wb');
fclose($fp);
}
// Archiving current files
if(!file_exists(cfg_data_path.'/data/shrink_'.$shrink_name.'/'.$flh) and !file_exists(cfg_data_path.'/data/shrink_'.$shrink_name.'/'.$fl))
{
$cmd_dat=cfg_grep_path.' -haiF 99991231 '.$fl.' > tmp_'.$fl.'';
$cmd_dath=cfg_grep_path.' -vhaiF 99991231 '.$flh.' '.$fl.' > tmp_'.$flh.'';
$current_dir=getcwd();
chdir(cfg_data_path.'/data/');
exec($cmd_dat);
exec($cmd_dath);
chdir($current_dir);
if(
rename(cfg_data_path.'/data/'.$flh,cfg_data_path.'/data/shrink_'.$shrink_name.'/'.$flh)
and rename(cfg_data_path.'/data/'.$fl,cfg_data_path.'/data/shrink_'.$shrink_name.'/'.$fl)
and rename(cfg_data_path.'/data/tmp_'.$flh,cfg_data_path.'/data/'.$flh)
and rename(cfg_data_path.'/data/tmp_'.$fl,cfg_data_path.'/data/'.$fl)
)
{
mylog('DATA SHRINK OK '.$fl);
}
else
{
mylog('DATA SHRINK ERROR '.$fl);
}
}
}
}
closedir($res);
//shrinking dictionaries
$res=opendir(cfg_data_path.'/config/dict');
while($fl = readdir($res))
{
if((substr($fl,0,5)=='dict_' or substr($fl,0,7)=='static_') and substr($fl,-4)=='.txt')
{
$found_array=array();
$fp=fopen(cfg_data_path.'/config/dict/'.$fl,'rb');
while(!feof($fp))
{
$line=trim(fgets($fp,256));
if(substr($line,0,1)=='$')
$val=substr($line,strpos($line,'=')+1,-1);
else
$val=$line;
if(!isset($found_array[$val]))
{
$found_array[$val]=1;
}
}
fclose($fp);
$fp=fopen(cfg_data_path.'/config/dict/'.$fl,'wb');
reset($found_array);
foreach($found_array as $val => $tmp)
{
fwrite($fp,$val.CRLF);
}
fclose($fp);
}
elseif(substr($fl,-4)=='.bak')
{
unlink(cfg_data_path.'/config/dict/'.$fl);
}
}
closedir($res);
}
function odb_fgets($fp)
{
$tmp_pointer=ftell($fp);
$line=fgets($fp,5000);
if(substr($line,-2)!="\r\n")
{
if(strlen($line)>2500)
{
$tmp_sysLen=read($line,'sysLen');
if($tmp_sysLen==0)
$tmp_sysLen=5000;
fseek($fp,$tmp_pointer);
$line=fgets($fp,2*$tmp_sysLen);
}
}
return $line;
}
function search($words) //:$result_array[]:line
{
//if(STOP) exit(); // security violation
$id_array=array();
$pre_time=time();
//$const_today=date('Ymd');
$grep_key='';
global $query_count;
$query_count++;
//echo $query_count.':'.$words;
//if($query_count==4)
{
//$const_today='20060324';
//$const_today_ext=0;//date('Ymd');
}
if(strpos('_'.$words,'---')==0)
{
global $bagaccess_array;
global $history_array;
$tmp_bagaccess_array=$bagaccess_array;
//smart switching off
if(strpos('_'.$words,'deleted')==0 and strpos('_'.$words,'sysId=')==0)
$bagaccess_array['deleted']=0;
//smart switching off
if(strpos('_'.$words,'logs')==0 and strpos('_'.$words,'sysId=')==0)
$bagaccess_array['logs']=0;
//smart switching off
if(strpos('_'.$words,'articles')==0 and strpos('_'.$words,'sysId=')==0)
$bagaccess_array['articles']=0;
$history_array=array();
$collect_history=0;
if(strpos('_'.$words,'sysId')>0 and get('odb_history')==1)
$collect_history=1;
//un-tiny for current page :)
$page=get('page');
$script=get('script');
$file_array=array();
$mybag='';
if(strpos('_'.$words,'MyBag=')>0)
{
$pos1=strpos('_'.$words,'MyBag=');
$pos2=strpos('_'.$words.' ',' ',$pos1);
$mybag=purename(trim(substr($words,$pos1-1+6,$pos2-$pos1-6)));
$words=trim(substr($words,0,$pos1-1).substr($words,$pos2));
}
$word_array=index_explode($words);
if($mybag=='' or ($bagaccess_array['all']>0 and ($mybag=='all' or $mybag=='')))
{
//echo 'A';
$res=opendir(cfg_data_path.'/data');
while($fl = readdir($res))
if(substr($fl,0,4)=='dat_' and (!isset($bagaccess_array[substr($fl,4,-4)]) or $bagaccess_array[substr($fl,4,-4)]!=0))
{
$file_array[substr($fl,0,-4)]=1;
if($collect_history)
$file_array['dath_'.substr($fl,4,-4)]=1;
}
closedir($res);
}
elseif($mybag=='')// non-active
{
//echo 'B';
reset($bagaccess_array);
while(list($key,$tmp)=each($bagaccess_array))
{
if($tmp>0)
{
$file_array['dat_'.$key]=1;
if($collect_history)
$file_array['dath_'.$key]=1;
}
}
}
elseif(file_exists(cfg_data_path.'/data/bag_'.$mybag.'.txt'))
{
// use superbag to even redirect bag definition
//echo 'D';
$tmp_array=explode("\n",file_get_contents(cfg_data_path.'/data/bag_'.$mybag.'.txt'));
for($ii=0;$ii<count($tmp_array);$ii++)
{
$tmp_array[$ii]=trim($tmp_array[$ii]);
if($tmp_array[$ii]!='' and file_exists(cfg_data_path.'/data/dat_'.$tmp_array[$ii].'.txt'))
{
$file_array['dat_'.$tmp_array[$ii]]=1;
if($collect_history)
$file_array['dath_'.$tmp_array[$ii]]=1;
}
}
}
elseif(file_exists(cfg_data_path.'/data/dat_'.$mybag.'.txt'))
{
//echo 'C';
$file_array['dat_'.$mybag]=1;
if($collect_history)
$file_array['dath_'.$key]=1;
}
elseif(file_exists(cfg_data_path.'/data/cache_'.$mybag.'.txt'))
{
//echo 'C1';
$file_array['cache_'.$mybag]=1;
}
if(cfg_grep_available)// and $check_last_mod_only==0)
{
$grep_files='';
if($mybag=='' or ($bagaccess_array['all']>0 and ($mybag=='all' or $mybag=='')))
{
$grep_files=' dat_*.txt';
if($collect_history)
$grep_files.=' dath_*.txt';
}
else
{
reset($file_array);
while(list($dat_file,$tmp)=each($file_array))
{
$grep_files.=' '.$dat_file.'.txt';
}
}
$grep_key='';
reset($word_array);
while(list($key,$tmp)=each($word_array))
{
if(strpos('_'.$key,'sysId='))
{
$grep_key=str_replace('sysId=','',$key);
break;
}
}
if(!$grep_key)
{
reset($word_array);
while(list($key,$tmp)=each($word_array))
{
if(!strpos('_'.$key,'<') and !strpos('_'.$key,'>') /*and !strpos('_'.$key,'=')*/ and !strpos('_'.$key,'mybag') and !strpos('_'.$key,'sysvalid') and strpos('_'.$key,'-')!=1)
{
$grep_key=$key;
break;
}
}
}
if($grep_key)
{
$rand_file=rand(1000,9999);
$cmd_grep_key=$grep_key;
if(substr(cfg_data_path,0,1)!='/')
$cmd_grep_key='"'.$cmd_grep_key.'"';
else
$cmd_grep_key='\''.$cmd_grep_key.'\'';
//$cmd=cfg_grep_path.' -i '.$cmd_grep_key.''.$grep_files.' > '.cfg_data_path.'/data/part_'.$rand_file.'.txt';
//$cmd=cfg_grep_path.' -i '.$cmd_grep_key.''.$grep_files.' > part_'.$rand_file.'.txt';
//$cmd=cfg_grep_path.' -iF '.$cmd_grep_key.''.$grep_files.' > part_'.$rand_file.'.txt';
$file_array=array();
if($grep_files==' dat_*.txt' and file_exists(cfg_data_path.'/data/cache_'.purename($cmd_grep_key).'.txt'))
{
$file_array['cache_'.purename($cmd_grep_key)]=1;
}
elseif(trim($grep_files)=='')
{
//echo 'EMPTY SEARCH';
}
elseif(0 and $grep_files!=' dat_access.txt' and $grep_files!=' dat_menu.txt')
{
//----------------------------------------------------------------------
//count($cluster_slave_array) can be great enter cond
$cluster_slave_array[]='(ssh www-hide@address.com \'cd /srv/mydata/data; %GREPCMD%;\' > tmp_%RESULTFILE%; mv tmp_%RESULTFILE% %RESULTFILE%)>/dev/null &';
$cluster_slave_array[]='(cd /srv/mydata/data; %GREPCMD% > tmp_%RESULTFILE%; mv tmp_%RESULTFILE% %RESULTFILE%;)>/dev/null &';
$cluster_slave_array[]='(ssh www-hide@address.com \'cd /srv/mydata/data; %GREPCMD%\' > tmp_%RESULTFILE%; mv tmp_%RESULTFILE% %RESULTFILE%;)>/dev/null &';
$cluster_file_array=array();
$cluster_slave_ii=0;
$res=opendir(cfg_data_path.'/data');
while($fl = readdir($res))
{
if(substr($fl,0,4)=='dat_')
{
$cluster_file_array[$fl]=$cluster_slave_ii;
$cluster_slave_ii++;
if($cluster_slave_ii>=count($cluster_slave_array))
$cluster_slave_ii=0;
}
}
closedir($res);
//----------------------------------------------------------------------
// preparing files lists
$cmd_cluster_slave_array=array();
reset($cluster_file_array);
foreach($cluster_file_array as $fl => $tmp_slave_index)
{
//file access cond
if($grep_files==' dat_*.txt' or strpos($grep_files,$fl))
if(substr($fl,0,4)=='dat_' and (!isset($bagaccess_array[substr($fl,4,-4)]) or $bagaccess_array[substr($fl,4,-4)]!=0))
$cmd_cluster_slave_array[$tmp_slave_index].=' '.$fl;
}
reset($cmd_cluster_slave_array);
foreach($cmd_cluster_slave_array as $tmp_slave_index => $grep_files)
{
$rand_file=rand(1000,9999);
$grep_cmd=cfg_grep_path.' -d skip -HabiF '.$cmd_grep_key.''.$grep_files;
//$grep_cmd='ls';
$cmd=$cluster_slave_array[$tmp_slave_index];
$cmd=str_replace('%GREPCMD%',$grep_cmd,$cmd);
$cmd=str_replace('%RESULTFILE%','part_'.$rand_file.'.txt',$cmd);
$current_dir=getcwd();
chdir(cfg_data_path.'/data/');
exec($cmd);
//$grep_output=shell_exec($cmd);
chdir($current_dir);
//echo '----------------------'.$cmd;
$file_array['part_'.$rand_file]=1;
}
$tmp_break=1000;
while($tmp_break)
{
$tmp_break--;
$tmp_all_files_ready=1;
reset($file_array);
foreach($file_array as $tmp_file => $tmp)
{
if(!file_exists(cfg_data_path.'/data/'.$tmp_file.'.txt'))
{
//$tmp_cnt++;
//echo ' '.$tmp_cnt.' '.cfg_data_path.'/data/'.$tmp_file.'.txt';
$tmp_all_files_ready=0;
break;
}
}
if($tmp_all_files_ready)
break;
usleep(100000);
}
//echo '<pre>'; //exit;
//print_r($file_array);
//echo '</pre>';
}
else
{
//--mmap off becouse of shrink() possible conflicts
$cmd=cfg_grep_path.' -d skip -HabiF '.$cmd_grep_key.''.$grep_files.' > part_'.$rand_file.'.txt';
//$cmd=cfg_grep_path.' -d skip --mmap -HabiE '.$cmd_grep_key.''.$grep_files.' > part_'.$rand_file.'.txt';
//echo $cmd;
$current_dir=getcwd();
chdir(cfg_data_path.'/data/');
exec($cmd);
//$grep_output=shell_exec($cmd);
chdir($current_dir);
$file_array['part_'.$rand_file]=1;
}
}
}
else
{
//file_array ready
}
$result_array=array();
$denied_fields_array=array();
reset($bagaccess_array);
foreach($bagaccess_array as $key => $tmp_rights)
{
if($tmp_rights==0 and $key!='all' and $key!='deleted' and $key!='logs')
{
$denied_fields_array[]=$key;
}
}
//showtime();
//echo '<br />'.$words.' '; print_r($file_array);
//print_r($word_array);
//if(count($word_array))
{
$lines_found_array=array();
reset($file_array);
while(list($dat_file,$tmp)=each($file_array))
{
if(file_exists(cfg_data_path.'/data/'.purename($dat_file).'.txt'))
{
//echo '<br />'.cfg_data_path.'/data/'.purename($dat_file).'.txt';
//showtime();
$fp=fopen(cfg_data_path.'/data/'.purename($dat_file).'.txt','rb');
while(!feof($fp))
{
$line=odb_fgets($fp);
$tmp_access=check_access(read($line,'MyBag'));
if($tmp_access>0)
{
//cutting off classified info
reset($denied_fields_array);
foreach($denied_fields_array as $key)
{
$kpos=strpos('_'.$line,'$'.$key.'=');
if($kpos>0)
{
$vpos=strpos('_'.$line,'$',$kpos+2);
$klen=strlen($key);
$val=substr('_'.$line,$kpos+$klen+2,$vpos-$klen-$kpos-2);
$line=substr('_'.$line,0,$kpos).substr('_'.$line,$vpos);
//$line=trim($line).$key.'=********'.'$';
}
}
$filtered=1;
$tiny_content='';
reset($word_array);
while(list($key,$tmp)=each($word_array))
{
$lt_pos=strpos($key,'<');
$gt_pos=strpos($key,'>');
$minus_pos=strpos('_'.$key,'-');
if($grep_key and $key==$grep_key) //problem with field rights
{
//already filtered
}
elseif($lt_pos)
{
if($minus_pos==1)
{
$val=substr($key,$lt_pos+1);
$key=substr($key,1,$lt_pos-1);
//echo $key.' '.$val;
if(stristr($line,''.$key) and read($line,$key)<$val)
$filtered=0;
}
else
{
$val=substr($key,$lt_pos+1);
$key=substr($key,0,$lt_pos);
if(!stristr($line,''.$key))
$filtered=0;
elseif(read($line,$key)>=$val)
$filtered=0;
}
}
elseif($gt_pos)
{
if($minus_pos==1)
{
$val=substr($key,$gt_pos+1);
$key=substr($key,1,$gt_pos-1);
//echo $key.' '.$val;
if(stristr($line,''.$key) and read($line,$key)>$val)
$filtered=0;
}
else
{
$val=substr($key,$gt_pos+1);
$key=substr($key,0,$gt_pos);
//if(strpos('_'.$line,''.$key)==0)
if(!stristr($line,''.$key))
$filtered=0;
elseif(read($line,''.$key)<=$val)
$filtered=0;
}
}
elseif($minus_pos==1)
{
$key=substr($key,1);
if(strlen($key) and stristr('_'.$line,''.$key))
$filtered=0;
}
//elseif(strpos('_'.$line,''.$key)==0)
elseif(!stristr($line,''.$key))
{
$filtered=0;
}
if(!$filtered)
break;
}
if($filtered)
{
//$sysGroupBy=read($line,'sysGroupBy');
$sysId=read($line,'sysId');
//echo '<br />'.$sysId.'-'.read($line,'sysValidFrom');
if($collect_history)
$history_array[$sysId][]=$line;
//if(strlen($sysGroupBy) and read($line,'sysValid')=='1' and read($line,'sysValidFrom')<=$const_today and read($line,'sysValidTo')>$const_today)
if(strpos($line,'$sysValidTo=99991231$'))// read($line,'sysValidTo')=='99991231') //strlen($sysGroupBy) and
{
//if(!isset($id_array[$sysId]))
//{
// $id_array[$sysId]=count($result_array);
$result_array[]=$sysId.'$sysFTell='.$line;
//$result_array[$sysId]=$line;
//if(isset($lines_found_array[$sysGroupBy]))
// $lines_found_array[$sysGroupBy]++;
//else
// $lines_found_array[$sysGroupBy]=1;
//}
//else
//{
//There are more than one record active for this sysId
// $result_array[]=$sysId.'$'.$line;
//}
}
}
}
}
fclose($fp);
//exit;
if(substr($dat_file,0,5)=='part_')
unlink(cfg_data_path.'/data/'.purename($dat_file).'.txt');
//showtime('file:'.$dat_file);
}
}
}
//showtime('search');
//~ echo '<pre>';
//~ print_r($result_array);
//~ echo '</pre>';
$result_array=array_reverse($result_array);
$post_time=time();
if($post_time-$pre_time>4)
{
// Saving info about long running searches
// Reading existing too-long log array
$toolong_array=array();
if(file_exists(cfg_data_path.'/temp/errors/toolong'.date('Ym').'.php'))
{
include(cfg_data_path.'/temp/errors/toolong'.date('Ym').'.php');
}
// Saving current info
$problem='search:'.$words.' grep:'.$cmd_grep_key.' files:'.trim($grep_files);
$solution=cfg_grep_path.' -d skip --mmap -aiF '.$cmd_grep_key.''.$grep_files.' | '.cfg_grep_path.' -aF 99991231 > cache_'.purename($cmd_grep_key).'.txt';
$toolong_array[$problem]['Problem']=$problem;
$toolong_array[$problem]['Solution']=$solution;
$toolong_array[$problem]['Time']+=$post_time-$pre_time;
$toolong_array[$problem]['Count']++;
// Saving log array
addpath(cfg_data_path.'/temp/errors/');
$fp=fopen(cfg_data_path.'/temp/errors/toolong'.date('Ym').'.tmp','wb');
if(fwrite($fp,'<'.'?php $toolong_array='.var_export($toolong_array,true).'; ?'.'>') === FALSE)
{
fclose($fp);
}
else
{
fclose($fp);
if(file_exists(cfg_data_path.'/temp/errors/toolong'.date('Ym').'.tmp'))
unlink(cfg_data_path.'/temp/errors/toolong'.date('Ym').'.php');
rename(cfg_data_path.'/temp/errors/toolong'.date('Ym').'.tmp',cfg_data_path.'/temp/errors/toolong'.date('Ym').'.php');
}
}
//exit;
$bagaccess_array=$tmp_bagaccess_array;
return $result_array;
}
else
{
return array();
}
}
function search_dictionary($word,$start='') //:result_array
{
//showtime();
//if($word=='ALLWORDS')
$lc_word='$'.$word.'=';
$lc_start=$start;
global $bagaccess_array;
$creating_new=0;
$file_array=array();
$result_array=array();
$found_array=array();
if($word=='syssize')
{
$result_array[]='S';
$result_array[]='M';
$result_array[]='L';
$result_array[]='XL';
$result_array[]='XXL';
}
elseif($word=='systemplate')
{
//get templates
$res=opendir(cfg_data_path.'/script');
while($fl = readdir($res))
{
if(substr($fl,0,4)=='fld_' and strlen($fl)>8)
$result_array[]=substr($fl,4,-4);
}
closedir($res);
}
elseif($word=='MyBag')
{
reset($file_array);
while(list($dat_file,$tmp)=each($file_array))
{
$dat_file=substr(basename($dat_file),4,-4);
if(strlen($start)==0 or strpos('_='.strtolower($dat_file),$lc_start)==1)
{
$result_array[]=$dat_file;
}
}
}
elseif(file_exists(cfg_data_path.'/config/dict/dict_'.purename($word).'.txt') or file_exists(cfg_data_path.'/config/dict/static_'.purename($word).'.txt'))
{
if(strlen($word))
{
$cnt=0;
if(file_exists(cfg_data_path.'/config/dict/static_'.purename($word).'.txt'))
$fp=fopen(cfg_data_path.'/config/dict/static_'.purename($word).'.txt','rb');
else
$fp=fopen(cfg_data_path.'/config/dict/dict_'.purename($word).'.txt','rb');
while(!feof($fp))
{
$line=fgets($fp,256);
if(!$lc_start or stristr('_'.$line,$lc_start))
{
if(substr($line,0,1)=='$')
$val=read($line,$word);
else
$val=trim($line);
if(!isset($found_array[$val]))
{
$found_array[$val]=1;
$result_array[]=$val;
$cnt++;
if($cnt>25)
break;
}
}
}
fclose($fp);
}
}
/* Looking for errors... nawara 2007-02-20
else
{
//new dictionary
addpath(cfg_data_path.'/config/dict');
$creating_new=1;
$fp=fopen(cfg_data_path.'/config/dict/dict_'.purename($word).'.txt','wb');
fclose($fp);
$result_array=search('[$'.$word.'=]');
reset($result_array);
foreach($result_array as $tmp_line)
{
$tmp_dict[substr(read($tmp_line,$word),0,256)]++;
}
$result_array=arsort($result_array);
$fp=fopen(cfg_data_path.'/config/dict/dict_'.purename($word).'.txt','wb');
reset($tmp_dict);
foreach($tmp_dict as $val => $tmp)
{
if(trim($val)!='0')
fwrite($fp,$val.CRLF);
}
fclose($fp);
$result_array=array();
}
*/
//showtime('search');
//~ echo '<pre>';
//~ print_r($result_array);
//~ echo '</pre>';
return $result_array;
}
function line2array($line) //:variable_array
{
$variable_array2=load_config(read($line,'sysGroupBy'));
$variable_array=array();
reset($variable_array2);
while(list($variable_name,$tmp)=each($variable_array2))
$variable_array[$variable_name]=read($line,$variable_name);
/*
echo CRLF.'<!-- ';
echo $line;
print_r($variable_array2);
echo ' -->'.CRLF;
*/
return($variable_array);
}
function check_index()
{
//problem
return(array());
}
function find_similar($variable_array,$word_perc_filter=60,$all_perc_filter=90,$advanced_mode=1,$bag='') //:$result_array
{
$sim_perc_filter=$word_perc_filter;
$avg_perc_filter=$all_perc_filter;
//$advanced_mode=1;
$result_array=array();
$tmp_variable_array=array();
reset($variable_array);
while(list($variable_name,$variable_value)=each($variable_array))
{
if(strlen($variable_name) and strlen($variable_value))
$tmp_variable_array[$variable_name]=$variable_value;
}
$variable_array=$tmp_variable_array;
if(count($variable_array))
{
if($advanced_mode)
{
$pattern='';
reset($variable_array);
while(list($variable_name,$variable_value)=each($variable_array))
{
$pattern.=' '.$variable_value;
}
$pattern_array=explode(' ',trim($pattern));
sort($pattern_array);
for($ii=0;$ii<count($pattern_array);$ii++)
$lc_pattern_array[$ii]=strtolower(substr($pattern_array[$ii],0,3));
}
//scope for search
if(strlen($bag))
$file_array[cfg_data_path.'/data/dat_'.purename($bag).'.txt']=1;
else
{
$res=opendir(cfg_data_path.'/data');
while($fl = readdir($res))
if(substr($fl,0,4)=='dat_')
$file_array[cfg_data_path.'/data/'.$fl]=1;
closedir($res);
}
//showtime();
if(1)
{
reset($file_array);
while(list($dat_file,$tmp)=each($file_array))
{
$fp=fopen($dat_file,'rb');
while(!feof($fp))
{
$line=odb_fgets($fp);
if(read($line,'sysValidTo')=='99991231')//read($line,'sysValidTo')>date('Ymd'))// and read($line,'sysValid')=='1' and read($line,'sysValidFrom')<=date('Ymd'))
{
if(rand(0,200)<1)
sleep(1);
$replacements='';
$sim_cnt=0;
$sim_sum=0;
$words='';
if(!$advanced_mode)
{
//simple and quick
reset($variable_array);
while(list($variable_name,$variable_value)=each($variable_array))
{
$word=read($line,$variable_name);
$words.=' '.$word;//.read($line,'sysId');
if($word==$variable_value)
{
$sim_sum+=100;
$sim_cnt++;
}
else
{
similar_text($word,$variable_value,$sim_perc);
if($sim_perc<$sim_perc_filter) break;
$replacements.=$pattern.' '.$word.' ';
$sim_sum+=$sim_perc;
$sim_cnt++;
}
}
}
else
{
//advanced and slow
reset($variable_array);
while(list($variable_name,$variable_value)=each($variable_array))
$words.=' '.read($line,$variable_name);
$word_array=explode(' ',trim($words));
sort($word_array);
for($ii=0;$ii<count($pattern_array);$ii++)
{
$pattern=$pattern_array[$ii];
$word=$word_array[$ii];
$len=strlen($word)+strlen($pattern);
if($len>4)
{
similar_text($word,$pattern,$sim_perc);
//$sim_perc=2*100*similar_text($word,$pattern)/$len;
//$sim_perc=100*($len-levenshtein($word,$pattern))/$len;
if($sim_perc<$sim_perc_filter) break;
$replacements.=$pattern.' '.$word.' ';
$sim_sum+=$sim_perc;
$sim_cnt++;
}
}
//echo CRLF.$sim_sum.' '.$sim_cnt.' '.count($variable_array).' '.$words;
}
if($sim_cnt>0)
{
$replacements=trim($replacements);
$similar_value=round($sim_sum/$sim_cnt);
//~ if($similar_value==100)
//~ {
//~ $result_array=array();
//~ break;
//~ }
//else
if($similar_value<100 and $similar_value>$avg_perc_filter and $sim_cnt==count($variable_array))
{
if(!isset($result_array[$replacements]) or $result_array[$replacements]<$similar_value)
$result_array[$replacements]=$similar_value;
}
}
}
}
fclose($fp);
}
}
}
//showtime('search');
return $result_array;
}
function find_dublets($variable_array,$max_diff_cnt=3,$bag='') //:$result_array;
{
$result_array=array();
$sysId=$variable_array['sysId'];
$tmp_variable_array=array();
reset($variable_array);
while(list($variable_name,$variable_value)=each($variable_array))
{
if(strlen($variable_name) and strlen($variable_value) and substr($variable_name,0,3)!='sys' and $variable_name!='Auto Import Stamp' and substr($variable_name,-2)!='Id')
$tmp_variable_array[$variable_name]=$variable_value;
}
$variable_array=$tmp_variable_array;
if(count($variable_array)>$max_diff_cnt)
{
//scope for search
if(strlen($bag))
$file_array[cfg_data_path.'/data/dat_'.purename($bag).'.txt']=1;
else
{
$res=opendir(cfg_data_path.'/data');
while($fl = readdir($res))
if(substr($fl,0,4)=='dat_')
$file_array[cfg_data_path.'/data/'.$fl]=1;
closedir($res);
}
//showtime();
if(1)
{
reset($file_array);
while(list($dat_file,$tmp)=each($file_array))
{
$fp=fopen($dat_file,'rb');
while(!feof($fp))
{
$line=odb_fgets($fp);
if(read($line,'sysId')!=$sysId and strpos($line,'sysValid=1') and read($line,'sysValidTo')>date('Ymd') and read($line,'sysValidFrom')<=date('Ymd'))
{
$diff_cnt=0;
$diff_str='';
$all_cnt=0;
$diff_array=array();
reset($variable_array);
while(list($variable_name,$variable_value)=each($variable_array))
{
$diff_value=read($line,$variable_name);
$all_cnt++;
if($diff_value!=$variable_value)
{
$diff_cnt++;
$diff_array['field']=$variable_name;
$diff_array['win']=$variable_value;
$diff_array['del']=$diff_value;
if($diff_cnt>$max_diff_cnt)
break;
}
}
if($all_cnt>$max_diff_cnt and $diff_cnt<=$max_diff_cnt)
{
$diff_array['fields']=$all_cnt;
$result_array[]=$diff_array;
}
}
}
fclose($fp);
}
}
//showtime('reduction');
}
return $result_array;
}
?>