<?php
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$root1=strrpos($catalog,'/')!==false ? substr($catalog,0,strrpos($catalog,'/')+1) : ''; 
if(!file_exists("$catalog/css1.php"))exit("Не надо этот файл открывать!");
$phpself_array= explode("/",$_SERVER['PHP_SELF']); 
$filename = $phpself_array[count($phpself_array)-1];
$pos = strpos($filename, ".");
$stranica = substr($filename, 0, $pos);

include_once($root1."sg_cm/root.php");
$server= rtrim($_SERVER['SERVER_NAME'].'/'.$sg_root,"/");
if (count($phpself_array)>1 && $phpself_array[count($phpself_array)-2]!=$sg_root) $server.="/".$phpself_array[count($phpself_array)-2];
$fileNameIvygruz = $filename;
$fileNameIedit = '.htflag_edit_titles_'.$filename;
if (is_file($fileNameIedit))$lastEdit = filemtime($fileNameIedit); else $lastEdit=0;
$lastVygruz = filemtime($fileNameIvygruz);

if ($lastEdit < $lastVygruz || !file_exists($fileNameIedit)) { 
$descr_arr=array('','');
$base = file_get_contents($filename);
$basehref=stripos($base,'BASE HREF="http'); 
if ($base!==false && $base!='' && $basehref===false){
$base = preg_replace('!<h1([^\>]*?)>([^\<]*?)</h1>!', '<h1$1><? echo \$tegH1;?></h1>', $base,1);
$base = preg_replace('!<h2([^\>]*?)>([^\<]*?)</h2>!', '<h2$1><? echo \$tegH2;?></h2>', $base,1);
$base = preg_replace('!<title>(.*?)</title>!', '<? echo \$title;?>', $base,1);
$base = preg_replace('!<link rel!', '<BASE HREF="http://<? echo \$server;?>/">'."\r\n <link rel", $base,1);
preg_match('!<meta name="description" content="(.*?)" />!',$base, $descr_arr);
$base = preg_replace('!<meta name="description(.*?)>!','<? echo \$tegDescr."\r\n" ;?>', $base,1);
file_put_contents($filename, $base);
if (!isset($descr_arr[1])) $descr_arr[1]='';
file_put_contents($catalog.'/admin/db/descr.php', $descr_arr[1]);
touch($fileNameIedit);  
}
$base = file_get_contents($catalog.'/admin/db/zagl');
if ($base!==false && $base!=''){
$base = preg_replace('!indx=\'[^\']*?\';!', 'indx=\''.$stranica.'\';', $base);
file_put_contents($catalog.'/admin/db/zagl',$base);
}
}


$cat=array();
$id_cat=array();
$cat=file("$catalog/admin/db/cat.txt");
foreach($cat as $line)
{
$line=trim($line);
$expl=explode("*",$line);
$id_cat[$expl[0]]=$expl[1];
}
if(isset($_GET['id']) && is_file("$catalog/admin/db/news/$_GET[id]"))
{
$text=file("$catalog/admin/db/news/$_GET[id]");
for($n=0;$n<count($text);$n++)$text[$n]=trim($text[$n]);
if($text[2]!="") $ttl=htmlspecialchars(strip_tags($text[2]));
else $ttl=htmlspecialchars(strip_tags(mb_substr($text[3],0,70,'utf-8')))."...";
if($text[3]!="") $descr=htmlspecialchars(strip_tags(mb_substr($text[3],0,160,'utf-8')));
else $descr=htmlspecialchars(strip_tags(mb_substr($text[4],0,160,'utf-8')));

}
include($catalog."/admin/db/zagl");
if(!isset($_GET['c']) && !isset($_GET['id']) && !isset($_GET['sear']))
{
$ttl=$zagl;
if(isset($ZAGL01[4])) $descr=$ZAGL01[4];
else $descr=trim(file_get_contents($catalog.'/admin/db/descr.php'));
}
if(isset($_GET['sear']) && !isset($_GET['id']))
{
   $ttl="Результат поиска";
$descr=$ttl;
}	
if(isset($_GET['c']) && !isset($_GET['id']) && !isset($_GET['sear']) )
{ if(!isset($id_cat[$_GET['c']]))$id_cat[$_GET['c']]='';
$ttl=$zagl." :: ".$id_cat[$_GET['c']]."&nbsp;";
if(isset($ZAGL01[4])) $descr=$ZAGL01[4];
else 
$descr=trim(file_get_contents($catalog.'/admin/db/descr.php'));
$descr.=" - ".$id_cat[$_GET['c']];
}
if (mb_strlen($ttl)>70) $ttl=mb_substr($ttl,0,mb_strripos(mb_substr($ttl,0,70),' '),'utf-8')."...";
if ($zaglsite!='') $zaglsite=" :: ".$zaglsite;
if(isset($_GET['c']) || isset($_GET['id']) || isset($_GET['sear']) ){
$title="<title >".$ttl.$zaglsite."</title>";
$tegH1=$ttl.$zaglsite;  // содержимое тега H1
$tegH2=$ttl; // содержимое тега H2
$tegDescr="<meta name=\"description\" content=\"$descr\" />";
if (isset($text[5])&& $text[5]!='')  $tegKW="<meta name=\"keywords\" content=\"$text[5]\" />"; else $tegKW='';
}

else { if (!isset($title) || $title=='') $title="<title >".$ttl.$zaglsite."</title>";
if (!isset($tegH1) || $tegH1=='') $tegH1=$ttl.$zaglsite; // содержимое тега H1
if (!isset($tegH2) || $tegH2=='') $tegH2=$ttl; // содержимое тега H2
if (!isset($tegDescr) || $tegDescr=='') $tegDescr="<meta name=\"description\" content=\"$descr\" />";

}
?>