<?php
/*------------------------------------------------------------------------*/
// Product: ActualAnalyzer
// Script: global.php
// Source: http://www.actualscripts.com/
// Copyright: (c) 2002-2006 ActualScripts, Company. All rights reserved.
//
// YOU DON'T NEED TO EDIT ANYTHING IN THIS SCRIPT.
// SEE LICENSE AGREEMENT FOR MORE DETAILS
/*------------------------------------------------------------------------*/
//read array from file
/*-------------------------------------------------------*/
function read_file($fname,&$fdata) {
global $err;
if(!file_exists($fname)) {$err->reason('global.php|read_file|the file '.$fname.' not found');return;}
$file=fopen($fname,'r');
if(!$file) {$err->reason('global.php|read_file|can\'t open the file '.$fname);return;}
flock($file,LOCK_SH);
while($str=fgets($file,1000)) $fdata[]=trim($str);
flock($file,LOCK_UN);
fclose($file);
}
//save array to file
/*-------------------------------------------------------*/
function save_file($fname,&$fdata) {
global $err;
$file=fopen($fname,'w');
if(!$file) {$err->reason('global.php|save_file|can\'t create the file '.$fname);return;}
flock($file,LOCK_EX);
$total=sizeof($fdata);
for ($c=0; $c<$total; $c++) {
if(!fwrite($file,$fdata[$c]."\n")) {$err->reason('global.php|save_file|can\'t write into the file '.$fname);return;}
}
flock($file,LOCK_UN);
fclose($file);
}
//attach array to file
/*-------------------------------------------------------*/
function attach($fname,&$fdata) {
global $err;
$file=fopen($fname,'a');
if(!$file) {$err->reason('global.php|attach|can\'t open the file '.$fname);return;}
flock($file,LOCK_EX);
$total=sizeof($fdata);
for ($c=0; $c<$total; $c++) {
if(!fwrite($file,$fdata[$c]."\n")) {$err->reason('global.php|attach|can\'t attach to the file '.$fname);return;}
}
flock($file,LOCK_UN);
fclose($file);
}
//init database
/*-------------------------------------------------------*/
function db_init () {
global $err,$conf;
$conf->link=db_connect($conf->dbhost,$conf->dbuser,$conf->dbpass);
if(!$conf->link) {$err->reason('global.php|db_init|connection with mysql server has failed');return;}
$rez=db_select_db($conf->dbase,$conf->link);
if(!$rez) {$err->reason('global.php|db_init|the request \'use '.$conf->dbase.'\' has failed -- '.db_error($conf->link));return;}
}
//close database
/*-------------------------------------------------------*/
function db_exit () {
global $err,$conf;
if($conf->link) {
$rez=db_close($conf->link);
if(!$rez) {$err->reason('global.php|db_exit|disconnection with mysql server has failed');return;}
}
}
//parse template
/*-------------------------------------------------------*/
function tparse(&$templ,&$vars) {
global $pagehtml;
$pagehtml.= preg_replace("/%%([A-Z0-9]+)%%/e","\$vars['\\1']",$templ);
}
//parse local template
/*-------------------------------------------------------*/
function ltparse(&$templ,&$vars) {
return preg_replace("/%%([A-Z0-9]+)%%/e","\$vars['\\1']",$templ);
}
//output HTML code of the page
/*-------------------------------------------------------*/
function out() {
global $pagehtml;
echo($pagehtml);
}
//===================================================================
function getpgs($id,&$mid) { // Get pages from group with id=$id
global $err,$conf;
$request='SELECT flags1,flags2,flags3,flags4,flags5,flags6,flags7 FROM aa_groups WHERE id='.$id;
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|getpgs|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
$mid=array();
if(!db_num_rows($result)) { db_free_result($result); return; }
$row=db_fetch_row($result);
db_free_result($result);
//cycle by flags
for($i=0;$i<7;$i++) {
$ind=31;
$tmp=(float)$row[$i];
while($tmp) {
if($tmp%2) $mid[$ind+$i*32]=1;
$tmp=(int)($tmp/2);
$ind--;
}
}
}
//===================================================================
function restrict($item,$tbl,$maxrec,$numadd,&$module,&$delids) {
global $err,$conf;
//Restrict size of sheaf 'base-log'
//$item - 'ref','key','frm','prx','prv'
//$tbl - checked table from sheaf 'base-log' for rotation: 'aa_ref_base','aa_ref_log','aa_key_base','aa_key_log', etc.
//$maxrec - max records in table $tbl
//$numadd - the number of added records to table $tbl
//$module - module: 'main','tracker', etc.
//get the number of records in table
if(!strcmp($tbl,'aa_ref_base')) $request='SELECT COUNT(*) AS nrec FROM '.$tbl.' WHERE url!=""';
elseif(!strcmp(substr($tbl,-4),'base')) $request='SELECT COUNT(*) AS nrec FROM '.$tbl.' WHERE name!=""';
else $request='SELECT COUNT(*) AS nrec FROM '.$tbl;
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
$row=db_fetch_object($result);
db_free_result($result);
$recintbl=$row->nrec;
//get the number of deleted records from base
$numdel=0;
if(($recintbl+$numadd)>$maxrec) $numdel=$recintbl-$maxrec+$numadd;
if($numdel<=0) return;
$numdel=30; //delete 30 items
//get dead ids from base
$request='SELECT '.$item.'id AS delid,IF(count!=0,('.$conf->ctime.'-added)/count,'.$conf->ctime.'-added) AS speed FROM aa_'.$item.'_base WHERE added!=0 ORDER BY speed DESC LIMIT '.$numdel;
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
$ids='';
while($row=db_fetch_object($result)) {
if(empty($ids)) $ids=' IN('.$row->delid;
else $ids.=','.$row->delid;
$delids[$row->delid]=1;
}
if(!empty($ids)) $ids.=')';
db_free_result($result);
if(!empty($ids)) {
//delete deleted ids from log (not for ref)
if(strcmp($item,'ref')) {
$request='DELETE FROM aa_'.$item.'_total WHERE '.$item.'id'.$ids;
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
}
if(!strcmp($item,'ref')) {
//ONLY for ref - processing domain
//get dead domain's ids
$request='SELECT domid AS delid FROM aa_ref_total WHERE id<221 AND refid'.$ids.' GROUP BY domid';
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
$idds='';
$iddm=array();
while($row=db_fetch_object($result)) {
if(empty($idds)) $idds=' IN('.$row->delid;
else $idds.=','.$row->delid;
$iddm[$row->delid]=1;
}
if(!empty($idds)) $idds.=')';
db_free_result($result);
//delete deleted ids from log (not for ref)
$request='DELETE FROM aa_'.$item.'_total WHERE '.$item.'id'.$ids;
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
if(!empty($idds)) {
//check dead domains on delete
$request='SELECT domid FROM aa_ref_total WHERE domid'.$idds.' GROUP BY domid';
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
//unset from deleted domains where used in log
while($row=db_fetch_object($result)) unset($iddm[$row->domid]);
db_free_result($result);
//form deleted string from domains
$idds='';
reset($iddm);
while($e=each($iddm)) {
$k=$e[0];
if(empty($idds)) $idds=' IN('.$k;
else $idds.=','.$k;
}
if(!empty($idds)) $idds.=')';
//delete deleted ids from domain base (set added=0)
if(!empty($idds)) {
$request='DELETE FROM aa_domains WHERE domid'.$idds;
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
}
}
}
$request='DELETE FROM aa_'.$item.'_base WHERE '.$item.'id'.$ids;
$result=db_query($request,$conf->link);
if(!$result) {$err->reason('global.php|restrict|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
}
}
//load module's variables /-------------------------------------------------//
function loadmod($pref,$rf,$lf) {
global $err,$conf,$configdb;
if(isset($conf->{$pref.'name'})) return;
//load local classes
require_once $lf.'common/lconfig.php';
//local config
$lconf = new lconfig($rf,$lf);
if($err->flag) {$err->reason('global.php|loadmod|constructor of lconfig class has failed');return;}
}
//---/ local for date and time /--------------------------------------------/
function ldate($flg,$ident,$tim) {
global $conf,$err; // $flg -> 1-time, 2-date
if(defined("_LOCALE")) {
$loc=_LOCALE;
$larr=split(',',$loc);
if(sizeof($larr)) {
$lstr='';
reset($larr);
while($e=each($larr)) {
$locst=setlocale(LC_TIME,$e[1]);
if($locst) {
if($flg==1) {
if(strftime("%p",$tim)) {
$trstr=strftime($conf->ltmas[$ident],$tim);
return date($trstr,$tim);
}
else {
return date($conf->tmas[$ident],$tim);
}
}
else {
if(strftime("%b",$tim)&&strftime("%B",$tim)) {
$trstr=str_replace('B','C',$conf->ldmas[$ident]);
$trstr=date($trstr,$tim);
$trstr=str_replace('C','B',$trstr);
return strftime($trstr,$tim);
}
else {
return date($conf->dmas[$ident],$tim);
}
}
}
}
}
}
if($flg==1) return date($conf->tmas[$ident],$tim);
else return date($conf->dmas[$ident],$tim);
}
//---/ single parameters of local date and time /---------------------------/
function sldate($ident,$tim) {
global $conf,$err;
if(defined("_LOCALE")) {
$loc=_LOCALE;
$larr=split(',',$loc);
if(sizeof($larr)) {
$lstr='';
reset($larr);
while($e=each($larr)) {
$locst=setlocale(LC_TIME,$e[1]);
if($locst) {
$trstr=0;
if(!strcmp($ident,'l')) $trstr=strftime("%A",$tim);
elseif(!strcmp($ident,'F')) $trstr=strftime("%B",$tim);
if($trstr) return $trstr;
else return date($ident,$tim);
}
}
}
}
return date($ident,$tim);
}
//---/ adjusting for mod security /---------------------------------------//
function modsec($text) {
$text=preg_replace("/__(https?)__/i","\\1://",$text);
return $text;
}
//---/ text processing to ascii /-----------------------------------------//
function txtproc($text) {
$text=str2unic($text);
$text=unic2asc($text);
return $text;
}
//---/ unicode to ascii /-------------------------------------------------//
function unic2asc($text) {
$text2=$text;
if(preg_match_all("/&#[0-9]+;/i",$text2,$matches)) {
reset($matches[0]);
while($e=each($matches[0])) {
$elem=substr($e[1],2,strlen($e[1])-3);
if($elem<256) $symb=chr($elem);
elseif($elem<2048) {
$ecd=$elem%128;
if($ecd<80) $symb=chr(176+$ecd);
else {
$text2='';
break;
}
}
else continue;
$text2=str_replace($e[1],$symb,$text2);
}
if(empty($text2)) $text2=$text;
}
return $text2;
}
//---/ string to unicode /------------------------------------------------//
function str2unic($text) {
$text=preg_replace("/%u([0-9a-fA-F]{4})/ei","'&#'.hexdec('\\1').';'",$text);
$text2='';
$pos=0;
$len=strlen($text);
while($pos<$len) {
$adf=false;
$char01=substr($text,$pos,1);
if((ord($char01)&224)==192) { // 2 bytes utf8
$char11=substr($text,$pos+1,1);
if((ord($char11)&192)==128) {
$unic=(ord($char01)%32)*64+(ord($char11)%64);
$text2.='&#'.$unic.';';
$pos+=2;
$adf=true;
}
}
if((ord($char01)&240)==224) { // 3 bytes utf8
$char11=substr($text,$pos+1,1);
if((ord($char11)&192)==128) {
$char21=substr($text,$pos+2,1);
if((ord($char11)&192)==128) {
$unic=(ord($char01)%16)*4096+(ord($char11)%64)*64+(ord($char21)%64);
$text2.='&#'.$unic.';';
$pos+=3;
$adf=true;
}
}
}
if(!$adf) {
$text2.=$char01;
$pos++;
}
}
return $text2;
}
//convert IP to signed int /------------------------------------------------//
function ip2longRE($ip) {
$tarr=preg_split("/\./",$ip);
if($tarr[0]<=127) return ip2long($ip);
$rez=0;
$rez1=$tarr[0]*256;
$rez1+=$tarr[1];
$rez1=~$rez1;
$rez1&=65535;
$rez=$rez1*65536;
$rez2=$tarr[2]*256;
$rez2+=$tarr[3];
$rez2=~$rez2;
$rez2&=65535;
$rez|=$rez2;
$rez++;
$rez*=-1;
return $rez;
}
//convert time to string /--------------------------------------------------//
function timtostr($tim) {
$th=floor($tim/3600);
$tim-=$th*3600;
$tm=floor($tim/60);
$tim-=$tm*60;
$ts=$tim;
return sprintf("%02.0d:%02.0d:%02.0d",$th,$tm,$ts);
}
//convert string to time /--------------------------------------------------//
function strtotim($tstr) {
$tarr=preg_split("/:/",$tstr);
$tim=0;
$a=1;
for($i=sizeof($tarr);$i>0;$i--) {
$loc=$tarr[$i-1];
if($a>60) {
if($loc>23) $loc=23;
elseif($loc<0) $loc=0;
}
else {
if($loc>59) $loc=59;
elseif($loc<0) $loc=0;
}
$tim+=$loc*$a;
$a*=60;
if($a>3600) break;
}
return $tim;
}
?>