<?
function updateData($file,$uri) {
if(!file_exists('data/'.$file.'.data') || !file_exists('data/'.$file.'.meta') || !file_exists('data/'.$file.'.date') || file_get_contents('data/'.$file.'.date')+900<time()) {
$fp=fsockopen('wapnot.ru',80,$errno,$errstr,30);
if($fp) {
fputs($fp,"GET ".$uri." HTTP/1.0\r\nUser-Agent: WAPNOT GRABBER\r\nHost: wapnot.ru\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n");
$tmp=array();
$body=false;
$fp2=fopen('data/'.$file.'.data','w');
if($fp2) {
for($i=0;!feof($fp);$i++) {
if($body) {
fwrite($fp2,fread($fp,1024));
} else {
$tmp[$i]=fread($fp,1024);
if(preg_match("/^.+\r\n\r\n([0-9 ]+)\n(.*)$/Usi",implode('',$tmp),$res)) {
fwrite($fp2,$res[2]);
$body=true;
}
}
}
fclose($fp2);
if(($fp2=fopen('data/'.$file.'.date','w'))) {
fwrite($fp2,time());
fclose($fp2);
}
if(($fp2=fopen('data/'.$file.'.meta','w'))) {
fwrite($fp2,$res[1]);
fclose($fp2);
}
}
fclose($fp);
}
}
}

function getData($file,$start=false,$length=false) {
$data=false;
if(($meta=file_get_contents('data/'.$file.'.meta'))) {
$size=explode(" ",$meta);
if(($fp=fopen('data/'.$file.'.data','r'))) {
if($start!==false) fseek($fp,$start*array_sum($size));
$j=0;
while(!feof($fp) && (($length && $j<$length) || !$length)) {
for($i=0;$i<sizeof($size);$i++) {
if(($tmp=fread($fp,$size[$i]))) {
$data[$j][$i]=trim($tmp);
} else {
break;
}
}
$j++;
}
fclose($fp);
}
}
return $data;
}

function getNumRows($file) {
$r=false;
if(($meta=file_get_contents('data/'.$file.'.meta')) && file_exists('data/'.$file.'.data') && ($fs=filesize('data/'.$file.'.data'))) {
$r=$fs/array_sum(explode(" ",$meta));
}
return $r;
}

function getFirstKey($key) {
$r=false;
if($key>0) {
foreach($GLOBALS['catgr'] as $key1=>$value1) {
foreach($value1 as $key2=>$value2) {
if($key==$key2) {$r=$key1;break;}
}
if($r!==false) {break;}
}
}
return $r;
}

$grpOpn=false;
function echoSelect($c=0,$hierr=array()) {
foreach($GLOBALS['catgr'][$c] as $key=>$value) {
if(isset($GLOBALS['catgr'][$key])) {
$Nhierr=array_merge($hierr,(array)$value);
if($GLOBALS['grpOpn']) {echo '</optgroup>';} else {$GLOBALS['grpOpn']=true;}
echo '<optgroup label="'.implode('/',$Nhierr).'">';
echoSelect($key,$Nhierr);
if($GLOBALS['grpOpn']) {
echo '</optgroup>';
$GLOBALS['grpOpn']=false;
}
} else {
echo '<option value="'.$key.'">'.$value.'</option>';
}
}
}
?>