%title%
%title%
%content%
iconv_enabled=(function_exists('iconv'));
switch($this->get_request())
{
case 'default':
if ($this->check_last_update()){$this->save_errors_log();return;}
$this->force_update=false;
case 'force_update':
if (!$this->load_catalogue_data()){$this->save_errors_log();return;}
$this->clear_old_catalogue();
if (!$this->parse_xml_pack()){$this->save_errors_log();return;}
if ($this->status=='0'){echo 'площадка находится на модерации';return;}
if (!$this->create_pages()){$this->save_errors_log();return;}
$this->set_last_update_time();
break;
case 'script_update':
if (!$this->update_script()){$this->save_errors_log();return;}
break;
case 'check_response':
echo $this->check_install_status();
exit();
break;
case 'version':
echo $this->version;
exit();
break;
case 'phpinfo':
phpinfo();
exit();
break;
}
if (count($this->errors)>0)$this->save_errors_log();
}
function check_install_status()
{
list(,$directory)=explode('/',$_SERVER['REQUEST_URI']);
if (!is_writable('../'.$directory))return "1";
if (!is_writable('seolink.class.php'))return "2";
$files=array('index.php','seolink.class.php','template.tpl');
foreach($files as $file)if (!file_exists($file))return "3";
return 'ok';
}
function get_request()
{
return ((isset($_GET['request']) and in_array($_SERVER['REMOTE_ADDR'],$this->allow_ips))?$_GET['request']:'default');
}
function add_error($error)
{
$this->errors[]=$error;
}
function save_errors_log()
{
if (count($this->errors)==0)return;
if (@filesize($this->log_file)>512000)@unlink($this->log_file);
$fp = fopen ($this->log_file, 'a');
$string=date("d.m.Y H:i").': ';
foreach($this->errors as $error){$string.=$error.";n";}
fwrite ($fp,$string);fclose ($fp);
}
function check_version()
{
if (!$content=@file_get_contents($this->script_url_check)){$this->add_error('Не удалось получить версию апдейта');return false;}
return (trim($content)==$this->version);
}
function update_script()
{
if ($this->check_version())return true;
if (!$code=@file_get_contents($this->script_url)){$this->add_error('Ошибка вытягивания скрипта');return false;}
$self = @fopen ("seolink.class.php", 'w') ;
if (!$self) {$this->add_error('Не удалось открыть файл скрипта');return false;}
if (!fwrite ($self, $code)) {$this->add_error('Не удалось перезаписать файл скрипта');return false;}
@fclose ($self) ;
chmod($self,0777);
return true;
}
function clear_old_catalogue()
{
$dir=opendir("./");
while ($filename=readdir($dir))
{
if (strlen(stristr($filename,'.htm'))>0 and !in_array($filename,$this->system_htm_files))@unlink($filename);
}
}
function get_main_content()
{
if (is_file('main.htm'))echo file_get_contents('main.htm');
}
function check_last_update()
{
return (time()-$this->get_last_update_time()<$this->update_period_hour*3600);
}
function get_last_update_time()
{
if (!is_file($this->timestamp_file))fopen($this->timestamp_file,'a+');
return intval(file_get_contents($this->timestamp_file));
}
function set_last_update_time()
{
$handle = fopen($this->timestamp_file, 'w');
fwrite($handle, time());
fclose($handle);
}
function load_catalogue_data()
{
$host=$_SERVER['HTTP_HOST'];
if ($_SERVER['HTTP_HOST']==''){$this->add_error('Не удалось получить имя хоста.');return false;}
if (!$data=@file_get_contents($this->gate_url.'?host='.$host.'&force='.(($this->force_update)?1:0))){$this->add_error('Не удалось подключиться к серверу');return false;}
$this->data=$data;
return true;
}
function check_ip()
{
return (in_array($_SERVER["REMOTE_ADDR"], $this->allow));
}
function detect_utf8($string)
{
return preg_match('%(?:
[xC2-xDF][x80-xBF] # non-overlong 2-byte
|xE0[xA0-xBF][x80-xBF] # excluding overlongs
|[xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte
|xED[x80-x9F][x80-xBF] # excluding surrogates
|xF0[x90-xBF][x80-xBF]{2} # planes 1-3
|[xF1-xF3][x80-xBF]{3} # planes 4-15
|xF4[x80-x8F][x80-xBF]{2} # plane 16
)+%xs', $string);
}
function cp1251_utf8( $str )
{
$sOutput = "";
for ( $i = 0; $i < strlen( $str ); $i++ )
{
$iAscii = ord( $str[$i] );
if ( $iAscii >= 192 && $iAscii <= 255 )
$sOutput .= "".( 1040 + ( $iAscii - 192 ) ).";";
else if ( $iAscii == 168 )
$sOutput .= "".( 1025 ).";";
else if ( $iAscii == 184 )
$sOutput .= "".( 1105 ).";";
else
$sOutput .= $str[$i];
}
return $sOutput;
}
function to_utf8($string)
{
if ($this->detect_utf8($string)) return $string;
if ($this->iconv_enabled)
{
$string = iconv('windows-1251', 'utf-8//IGNORE', $string);
return $string;
}
else
{
return $this->cp1251_utf8($string);
}
}
function to_cp1251($string)
{
if (!$this->detect_utf8($string))return $string;
if ($this->iconv_enabled)
{
$string = iconv('utf-8', 'cp1251//IGNORE', $string);
return $string;
}
else
{
return $this->utf8_cp1251($string);
}
}
function set_encode_table()
{
if ($this->conv)return $this->conv;
$conv=array ();
for ( $x=128; $x <=143; $x++ )
{
$conv['utf'][]=chr(209).chr($x);
$conv['win'][]=chr($x+112);
}
for ( $x=144; $x <=191; $x++ )
{
$conv['utf'][]=chr(208).chr($x);
$conv['win'][]=chr($x+48);
}
$conv['utf'][]=chr(208).chr(129);
$conv['win'][]=chr(168);
$conv['utf'][]=chr(209).chr(145);
$conv['win'][]=chr(184);
$this->conv=&$conv;
}
function utf8_cp1251( $str)
{
$this->set_encode_table();
return str_replace ( $this->conv['utf'], $this->conv['win'], $str );
}
function create_pages()
{
$iconv=false;
$template=file_get_contents('template.tpl');
if ($this->detect_utf8($template))
{
$template=str_replace("п»ї","",$template);
$template=$this->to_cp1251($template);
}
$template=str_replace('%encoding%',$this->convert_to,$template);
if (is_array($this->pages))
foreach($this->pages as $page)
{
$data=$template;
if (!$this->is_php_4())$page['title']=$this->to_cp1251($page['title']);
if (!$this->is_php_4())$page['description']=$this->to_cp1251($page['description']);
if (!$this->is_php_4())$page['keywords']=$this->to_cp1251($page['keywords']);
if (!$this->is_php_4())$page['content']=$this->to_cp1251($page['content']);
$data=str_replace('%title%',$page['title'],$data);
$data=str_replace('%meta_description%',$page['description'],$data);
$data=str_replace('%meta_keywords%',$page['keywords'],$data);
$data=str_replace('%content%',html_entity_decode($page['content']),$data);
$filename=$page['filename'].'.htm'; //только htm
$handler = fopen($filename, "w");
$content=(($this->convert_to=='utf-8')?$this->to_utf8($data):$data);
fwrite($handler, $content);
fclose($handler);
chmod($filename,0666);
}
return true;
}
function is_php_4()
{
$phpversion = phpversion();
return ($phpversion < '5.0.0') ?true:false;
}
function parse_xml_pack()
{
if ($this->data=='Сайт не найден'){$this->add_error('Не удалось обнаружить этот сайт в системе.');return false;}
$xml_parser = ($this->is_php_4()?xml_parser_create('ISO-8859-1'):xml_parser_create('UTF-8'));
xml_parse_into_struct($xml_parser, $this->data, $data_array, $index);
xml_parser_free($xml_parser);
$current_page=0;
if (!is_array($data_array) or count($data_array)==0){$this->add_error('Ошибка форматирования XML.');return false;}
foreach($data_array as $node)
{
if ($node['tag']=='PAGE' and isset($node['attributes']['FILENAME']))
{
$current_page++;
$this->pages[$current_page]=array('title'=>'','filename'=>'','description'=>'','content'=>'','keywords'=>'');
$this->pages[$current_page]['filename']=$node['attributes']['FILENAME'];
}
if ($node['tag']=='CATALOGUE_ENCODING' and isset($node['value']))$this->convert_to=$node['value'];
if ($node['tag']=='STATUS' and isset($node['value']))$this->status=$node['value'];
if ($node['tag']=='VERSION' and isset($node['value']))$this->need_version=$node['value'];
if ($node['tag']=='DESCRIPTION' and isset($node['value'])) $this->pages[$current_page]['description']=$node['value'] ;
if ($node['tag']=='KEYWORDS' and isset($node['value'])) $this->pages[$current_page]['keywords']=$node['value'];
if ($node['tag']=='TITLE' and isset($node['value'])) $this->pages[$current_page]['title']=$node['value'] ;
if ($node['tag']=='CONTENT' and isset($node['value'])) $this->pages[$current_page]['content']=$node['value'] ;
}
unset($data_array);
return true;
}
}
?>
include('seolink.class.php');
$catalogue=new seolink;
$catalogue->get_main_content();
?>