Просмотр файла znwap/wml.php

Размер файла: 4.63Kb
<?php
function replacetag($in) {
	global $alink, $form, $fields, $values, $action, $method, $select, $option;
	$f=$in[1];
	$type='o';
	if (preg_match("/^\s*\//", $f)) {$type='c';}
	if (preg_match("/\/\s*$/", $f)) {
		$type='s';
		$f=preg_replace("/\/\s*$/", "", $f);
	}
	if (preg_match("/^(\S+)\s/", $f, $a)) {
		$m=$a[1];
        preg_match_all("/(\w+)\s*\=\s*\"*([^\"]*)\"*/", $f, $q, PREG_SET_ORDER);
        for ($i=0; $i<count($q); $i++) {
        	$p[strtolower($q[$i][1])]=$q[$i][2];
        }
	} else {
        $m=$f;
	}
   	/*echo "<pre>".$m."\n";
   	print_r($p);
   	echo "\n</pre>";*/

	$m=str_replace("/", "", $m);
	if (($m=='a') && ($type=='o') && isset($p['href'])) {
		$alink=true;
		return "<a href=\"$p[href]\">";
	}
	if (($m=='a') && ($type=='c')) {
		$alink=false;
		return "</a>";
	}
	if ($m=='br') {return "<br/>";}
	if (($m=='img') && isset($p['src'])) {
		if (!isset($p['alt'])) {$p['alt']="";}
		return "<img src=\"$p[src]\" alt=\"$p[alt]\"/>";
	}
	if (($m=='b') && ($type=='o') && !$alink) {return "<b>";}
	if (($m=='i') && ($type=='o') && !$alink) {return "<i>";}
	if (($m=='u') && ($type=='o') && !$alink) {return "<u>";}
	if (($m=='b') && ($type=='c') && !$alink) {return "</b>";}
	if (($m=='i') && ($type=='c') && !$alink) {return "</i>";}
	if (($m=='u') && ($type=='c') && !$alink) {return "</u>";}
	if (($m=='form') && ($type=='o')) {
		$form=true;
		if (isset($p['action'])) {
			$action=$p['action'];
		} else {
			$action=$_SERVER['REQUEST_URI'];
		}
		if (isset($p['method'])) {
			$method=$p['method'];
		} else {
			$method='get';
		}
	}
	if (($m=='div') && ($type=='c')) {return "<br/>";}
	if (($m=='form') && ($type=='c')) {$form=false;}
	if (($m=='textarea') && (($type=='o') || ($type=='s')) && (isset($p['name']))) {
		if ($form) {
			$fields[]=$p['name'];
		}
		return "<input type=\"text\" name=\"$p[name]\"/>";
	}
	if (($m=='input') && isset($p['name']) && isset($p['type'])) {
		if ($form) {
			$fields[]=$p['name'];
			if (($p['type']=='hidden') && isset($p['value'])) {
				$values[$p['name']]=$p['value'];
			}
		}
    	if (($p['type']=='text') || ($p['type']=='password')) {
    		$atr="";
    		if (isset($p['value'])) {$atr.=" value=\"$p[value]\"";}
    		if (isset($p['format'])) {$atr.=" format=\"$p[format]\"";}
    		if (isset($p['emptytok'])) {$atr.=" emptytok=\"$p[emptytok]\"";}
    		if (isset($p['size'])) {$atr.=" size=\"$p[size]\"";}
    		if (isset($p['title'])) {$atr.=" title=\"$p[title]\"";}
    		if (isset($p['maxlength'])) {$atr.=" maxlength=\"$p[maxlength]\"";}
    		return "<input type=\"$p[type]\" name=\"$p[name]\"$atr/>";
    	}
	}
	if (($m=='input') && ($p['type']=='submit')) {
		$r="<anchor>";
		if (isset($p['value'])) {
			$r.=$p['value'];
		} else {
			$r.="OK";
		}
		$r.="<go href=\"$action\" accept-charset=\"UTF-8\" method=\"$method\">";
		for ($i=0; $i<count($fields); $i++) {
			if (isset($values[$fields[$i]])) {
				$r.='<postfield name="'.$fields[$i].'" value="'.$values[$fields[$i]].'"/>';
			} else {
				$r.='<postfield name="'.$fields[$i].'" value="$('.$fields[$i].')"/>';
			}
		}
		$r.="</go></anchor>";
		return $r;

	}
	if (($m=='select') && ($type='o') && (isset($p['name'])) && (!$select)) {
		$select=true;
		if ($form) {
			$fields[]=$p['name'];
		}
		$atr="";
    	return "<select name=\"$p[name]\"$atr>";
	}
    if (($m=='select') && ($type='c') && $select) {
    	$select=false;
    	return "</select>";
    }
    if (($m=='option') && ($type='o') && $select && (!$option)) {
		$option=true;
		$atr="";
		if (isset($p['value'])) {$atr.=" value=\"$p[value]\"";}
    	return "<option$atr>";
	}
	if (($m=='option') && ($type='c') && $select && $option) {
    	$option=false;
    	return "</option>";
    }

}



$s=$text;
$alink=false; $form=false; $fields=false; $action=false; $method=false; $values=false;
$select=false;$option=false;

$r=preg_replace("/^(!\<body\>)\<body\>/", "", $s);
$r=str_replace('$', '&#36;', $r);
$r=preg_replace("/\<\s*script[^\>]*\>[^\<]*\<\s*\/\s*script\s*\>/i", "", $r);
$r=preg_replace_callback("/\<([^\>]*)\>/", "replacetag", $r);
$r=str_replace('&nbsp;', ' ', $r);



$body.='<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"><wml>';

if (isset($autoupdate) && isset($redirecturl) && ($redirecturl!="") &&($autoupdate!=0)) {
	$body.='<card id="c1" title="'.$caption.'" ontimer="'.$redirecturl.'">';
	$body.='<timer value="'.$autoupdate.'0"/>';
} else {
	$body.='<card id="c1" title="'.$caption.'">';
}
$body.="<p>$banner</p>";
$body.='<p>'.$r.'</p></card></wml>';




?>