View file includes/wap/builder/page_restore.php

File size: 11.37Kb
<?php
# Чистка и восстановление страницы

# Читаем файл
$f=fopen($userpath.$currentdir.$editfile.'.wml','r');
$text=fread($f,16384);
fclose($f);
# Убираем пролог и антикеш
$text=preg_replace("|<\?xml.*\?>|","",$text);
$text=preg_replace("|<!DOCTYPE[^>]+>|","",$text);
$text=preg_replace("|<head.*head>|","",$text);

$text=str_replace('&lt;?xml version=&quot;1.0&quot; encoding=&quot;Utf-8&quot;?&gt;','',$text);
$text=str_replace('&quot;&gt;&lt;do name=&quot;back&quot; type=&quot;options&quot; label=&quot;Назад&quot;&gt;&lt;prev/&gt;&lt;/do&gt;','',$text);
$text=str_replace('&lt;card id=&quot;card0&quot; title=&quot;','',$text);
$text=str_replace('&lt;p align=&quot;center&quot;&gt;','',$text);

# Убираем начало деки
$text=str_replace('<wml>','',$text);
# Убираем уданенные элементы
$text=str_replace('<!-- delete -->','',$text);
# Убираем конец деки
$text=str_replace('</p></card></wml>','',$text);
# Убираем двойные пробелы
$text=str_replace('    ',' ',$text);
$text=str_replace('   ',' ',$text);
$text=str_replace('  ',' ',$text);
# Убираем двойные \n
$text=str_replace("\r","\n",$text);
$text=str_replace("\n\n\n\n","\n",$text);
$text=str_replace("\n\n\n","\n",$text);
$text=str_replace("\n\n","\n",$text);
# Чистим начало и конец
$text=trim($text);
# Создаем массив для обработки
$tmp=explode("\n",$text);unset($text);
array($tmp_element);
array($tmp_softkey);
for ($i=0;$i<sizeof($tmp);$i++)
	{
	if (substr(trim($tmp[$i]),0,12)=='<!-- cnt -->') continue;
	if (substr(trim($tmp[$i]),0,10)=='<!-- * -->') continue;
	if (substr(trim($tmp[$i]),0,12)=='<do name="sk') {$tmp_softkey[]=trim($tmp[$i]); continue;}
	$tmp_element[]=trim($tmp[$i]);
	}
unset($tmp);
$tmp=$tmp_element;unset($tmp_element);array($tmp_element);

# Восстановление начала карты
if     ((substr(trim($tmp[0]),0,10)=='<card id="')&&(substr(trim($tmp[1]),0,2)=='<p')) {$tmp_element[]=trim($tmp[0]);$tmp_element[]=trim($tmp[1]);$tmp[0]=$tmp[1]='';}
elseif ((substr(trim($tmp[0]),0,10)=='<card id="')&&(substr(trim($tmp[1]),0,2)!='<p')) {$tmp_element[]=trim($tmp[0]);$tmp_element[]='<p align="center">';$tmp[0]='';}
elseif  (substr(trim($tmp[0]),0,2 )=='<p')                                             {$tmp_element[]='<card id="card0" title="Page"><do name="back" type="options" label="Назад"><prev/></do>';$tmp_element[]=trim($tmp[0]);$tmp[0]='';}
elseif ((substr(trim($tmp[0]),0,10)!='<card id="')&&(substr(trim($tmp[1]),0,2)=='<p')) {$tmp_element[]='<card id="card0" title="Page"><do name="back" type="options" label="Назад"><prev/></do>';$tmp_element[]=trim($tmp[1]);$tmp[0]=$tmp[1]='';}
elseif ((substr(trim($tmp[0]),0,10)!='<card id="')&&(substr(trim($tmp[1]),0,2)!='<p')) {$tmp_element[]='<card id="card0" title="Page"><do name="back" type="options" label="Назад"><prev/></do>';$tmp_element[]='<p align="center">';}
for ($i=0;$i<sizeof($tmp);$i++)
	{
	if ((trim($tmp[$i]))=='') continue;
	$tmp_element[]=trim($tmp[$i]);
	}
$tmp=$tmp_element;unset($tmp_element);array($tmp_element);

# Проверка правильности элементов
$tmp_element[]='<?xml version="1.0" encoding="Utf-8"?><!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml12.dtd"><wml><head><meta forua="true" http-equiv="Cache-Control" content="max-age=0,proxy-revalidate,must-revalidate"/></head>';

for ($i=0;$i<sizeof($tmp);$i++)
	{
	$atr='';
	$stmp=$tmp[$i];
	if (strpos(' '.$stmp,'<i>'))          $atr.='.<i>';
	if (strpos(' '.$stmp,'<u>'))          $atr.='.<u>';
	if (strpos(' '.$stmp,'<b>'))          $atr.='.<b>';
	if (strpos(' '.$stmp,'<big>'))        $atr.='.<big>';
	if (strpos(' '.$stmp,'<small>'))      $atr.='.<small>';
	if (strpos(' '.$stmp,'<br/>'))        $br='<br/>'; else $br='';
	$stmp=str_replace('<i>','',$stmp);    $stmp=str_replace('</i>','',$stmp);
	$stmp=str_replace('<u>','',$stmp);    $stmp=str_replace('</u>','',$stmp);
	$stmp=str_replace('<b>','',$stmp);    $stmp=str_replace('</b>','',$stmp);
	$stmp=str_replace('<big>','',$stmp);  $stmp=str_replace('</big>','',$stmp);
	$stmp=str_replace('<small>','',$stmp);$stmp=str_replace('</small>','',$stmp);
	$stmp=str_replace('<br/>','',$stmp);
	if ($i==0)
		{
        $stra=explode('title="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $title=$stra[0];
        $stra=explode('ontimer="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $url=$stra[0];
        $stra=explode('value="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $second=$stra[0]+0;
		if (($url!='')&&($second>0)) $redirect=' ontimer="'.$url.'"><timer value="'.$second.'"/>'; else $redirect='>';
		$tmp_element[]='<card id="card0" title="'.$title.'"'.$redirect.'<do name="back" type="options" label="Назад"><prev/></do>';
		for ($j=0;$j<sizeof($tmp_softkey);$j++)
			{
			$stra=explode('label="',$tmp_softkey[$j]);
			$stra=explode('"',$stra[1]);
			$caption=$stra[0];
			$stra=explode('href="',$tmp_softkey[$j]);
			$stra=explode('"',$stra[1]);
			$url=$stra[0];
			if (($caption!='')&&($url!=''))
				{
				$tmp_element[]='<do name="sk'.rand(1000,9999).'" type="options" label="'.$caption.'"><go href="'.$url.'"/></do>';
				}
			}
		}
	elseif ($i==1)
		{
        $stra=explode('align="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $algn=$stra[0];
        $stra=explode('mode="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $wrp=$stra[0];
        if ($algn=='center') $align='center';
        elseif ($algn=='right') $align='right';
        else $align='left';
        if ($wrp=='nowrap') $wrap=' mode="nowrap"';
        elseif ($wrp=='wrap') $wrap=' mode="wrap"';
        else $wrap='';
		$tmp_element[]='<p align="'.$align.'"'.$wrap.'>';
		$tmp_element[]='<!-- * -->';
		}
	elseif (substr($tmp[$i],0,6)=='</p><p')
		{
        $stra=explode('align="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $algn=$stra[0];
        $stra=explode('mode="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $wrp=$stra[0];
        if ($algn=='center') $align='center';
        elseif ($algn=='right') $align='right';
        else $align='left';
        if ($wrp=='nowrap') $wrap=' mode="nowrap"';
        elseif ($wrp=='wrap') $wrap=' mode="wrap"';
        else $wrap='';
		$tmp_element[]='</p><p align="'.$align.'"'.$wrap.'>';
		}
	elseif (substr($tmp[$i],0,3)=='<br')
		{
		$tmp_element[]='<br/>';
		}
	elseif (substr($stmp,0,3)=='<a ')
		{
        $stra=explode('">',$stmp);
        $strb=explode(' ',$stra[1]);
        if ($strb[0]=='<img')
            {
			$tmp[$i]=$stmp;
            $stra=explode('href="',$tmp[$i]);
            $stra=explode('"',$stra[1]);
            $link_=$stra[0];
            $stra=explode('<img src="',$tmp[$i]);
            $stra=explode('"',$stra[1]);
            $imgurl=$stra[0];
            $stra=explode(' alt="',$tmp[$i]);
            $stra=explode('"',$stra[1]);
            $alttext=$stra[0];
            if (($imgurl!='')&&($link_!=''))
                {
                $tmp_element[]='<a href="'.$link_.'"><img src="'.$imgurl.'" alt="'.$alttext.'"/></a>'.$br;
                }
			}
		else
            {
			$tmp[$i]=$stmp;
            $stra=explode('href="',$tmp[$i]);
            $stra=explode('"',$stra[1]);
            $link_=$stra[0];
            $stra=explode('">',$tmp[$i]);
            $stra=explode('</a>',$stra[1]);
            $caption=htmlspecialchars(cyr2utf(utf2cyr(html_entity_decode($stra[0], ENT_QUOTES))), ENT_QUOTES);
            if (($caption!='')&&($link_!=''))
                {
				$tmp[$i]='<a href="'.$link_.'">'.$caption.'</a>';
		        if (strpos($atr,'<i>'))     $tmp[$i]='<i>'.$tmp[$i].'</i>';
				if (strpos($atr,'<u>'))     $tmp[$i]='<u>'.$tmp[$i].'</u>';
				if (strpos($atr,'<b>'))     $tmp[$i]='<b>'.$tmp[$i].'</b>';
				if (strpos($atr,'<big>'))   $tmp[$i]='<big>'.$tmp[$i].'</big>';
				if (strpos($atr,'<small>')) $tmp[$i]='<small>'.$tmp[$i].'</small>';
				$tmp_element[]=$tmp[$i].$br;
                }
			}
		}
	elseif (substr($tmp[$i],0,5)=='<img ')
		{
		$stra=explode('<img src="',$tmp[$i]);
		$stra=explode('"',$stra[1]);
		$imgurl=$stra[0];
		$stra=explode(' alt="',$tmp[$i]);
		$stra=explode('"',$stra[1]);
		$alttext=$stra[0];
		if ($imgurl!='')
			{
			$tmp_element[]='<img src="'.$imgurl.'" alt="'.$alttext.'"/>'.$br;
			}
		}
	elseif (substr($tmp[$i],0,7)=='<input ')
		{
        $stra=explode('name="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $var_=$stra[0];
        $stra=explode('maxlength="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $size=$stra[0]+0;
		if ($size<1) $size='999';
        $stra=explode('value="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $def=$stra[0];
        $stra=explode('title="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $caption=$stra[0];
        $stra=explode('type="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        if ($stra[0]!='password') $type='text'; else $type='password';
		if ($var_!='')
			{
			$tmp_element[]='<input name="'.$var_.'" type="'.$type.'" emptyok="true" title="'.$caption.'" maxlength="'.$size.'" value="'.$def.'"/>'.$br;
			}
		}
	elseif (substr($stmp,0,7)=='<anchor')
		{
		$tmp[$i]=$stmp;
        $stra=explode('href="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $url=$stra[0];
        $stra=explode('<anchor >',$tmp[$i]);
        $stra=explode('<',$stra[1]);
        $caption=htmlspecialchars(cyr2utf(utf2cyr(html_entity_decode($stra[0], ENT_QUOTES))), ENT_QUOTES);
        $stra=explode('method="',$tmp[$i]);
        $stra=explode('"',$stra[1]);
        $method=$stra[0];
		if (($url!='')&&($caption!=''))
			{
			$tmp[$i]='<anchor >'.$caption.'<go href="'.$url.'" method="'.$method.'"></go></anchor>';
			if (strpos($atr,'<i>'))     $tmp[$i]='<i>'.$tmp[$i].'</i>';
			if (strpos($atr,'<u>'))     $tmp[$i]='<u>'.$tmp[$i].'</u>';
			if (strpos($atr,'<b>'))     $tmp[$i]='<b>'.$tmp[$i].'</b>';
			if (strpos($atr,'<big>'))   $tmp[$i]='<big>'.$tmp[$i].'</big>';
			if (strpos($atr,'<small>')) $tmp[$i]='<small>'.$tmp[$i].'</small>';
			$tmp_element[]=$tmp[$i].$br;
			}
		}
	elseif (substr($tmp[$i],0,7)=='<!--var')
		{
        $stra=explode('|',$tmp[$i]);
		if (($stra[1]!='')&&($stra[2]!=''))
			{
			$tmp_element[]='<!--var |'.$stra[1].'|'.$stra[2].'|-->';
			}
		}
	elseif (substr($tmp[$i],0,5)=='<!--r')
		{
        $str=substr($tmp[$i],9);
		if (($str!='') && !(strpos(strtolower($str),'<card') && ($currentdir.$editfile=='./index')))
			{
			$tmp_element[]='<!--r -->'.$str;
			}
		}
	elseif (substr($tmp[$i],0,9)=='<!-- Copy')
		{
		$tmp_element[]=$tmp[$i];
		}
	else
		{
		$tmp[$i]=$stmp;
		if (strpos($atr,'<i>'))     $tmp[$i]='<i>'.$tmp[$i].'</i>';
		if (strpos($atr,'<u>'))     $tmp[$i]='<u>'.$tmp[$i].'</u>';
		if (strpos($atr,'<b>'))     $tmp[$i]='<b>'.$tmp[$i].'</b>';
		if (strpos($atr,'<big>'))   $tmp[$i]='<big>'.$tmp[$i].'</big>';
		if (strpos($atr,'<small>')) $tmp[$i]='<small>'.$tmp[$i].'</small>';
		if ($tmp[$i]!='')
			{
			$tmp_element[]=$tmp[$i].$br;
			}
		}
	}

if ($currentdir.$editfile=='./index') $tmp_element[]='<!-- cnt --><br/><a href="http://top.'.$sitedomain.'.ru/'.$user.'"><img src="http://cnt.'.$sitedomain.'.ru/'.$user.'/'.rand(10000,99999).rand(10000,99999).'" alt="TOP-KMX.Ru"/></a>';
$tmp_element[]='</p></card></wml>';

$npage='';
for ($i=0;$i<sizeof($tmp_element);$i++) $npage.=$tmp_element[$i]."\n";

$fp = fopen ($userpath.$currentdir.$editfile.'.wml', "w+");
fwrite ($fp, $npage);
fclose ($fp);
?>