Location: PHPKode > scripts > mk_t2h > mk_t2h/mk_t2h.inc.php
<?php

### mk_t2h - Text to HTML conversion class
### Version: 0.25 beta (released: 01.feb.2005)
### Class homepage: http://www.phpclasses.org/browse/package/2345.html
###
### Created by: Mariusz Kaczmarczyk <hide@address.com>, 2005+.
### Tools used: GNU/Linux Debian, KWrite 4.4, Mozilla Firefox, PHP 5.0.4-0.2.
### PHP version: class was designed/tested with PHP 5.0.4, but should work with 4.3+ - please write me if this is not the case, i can always make some corrections.
### This is free software released under GNU GPL license (http://www.gnu.org/licenses/gpl.html)
### But, if you want to use this software, you are obliged to send an e-mail to hide@address.com, subject beginning with '[mk_t2h]'. I want to know if my work is useful. :-) Nothing more is required.
###
###
### *How to use it*
### This class converts user input with HTML tags to form that can be accepted for displaying on website. 
### - you can set it to save only selected tags and remove all the rest (or to delete only selected tags and not modify others). 
### - you can strip unsafe parameters like ONMOUSEOVER or ONLOAD.
### - you can replace [%variables] by defined texts.
### - you can get info about tags and their parameters
### - you can convert typed URLs to HTML links
###
###
### 1. Initialization and class variables
###
### You can use filtered text as a parameter for class constructor, value will be copied to $this->input_text and $this->output_text.
### Functions with $input_text parameter will use $this->output_text as input instead if not defined or set NULL (you can build functions chain easily without additional variables outside class).
### $this->input_text is not modified while using class.
### $this->output_text is modified by some functions listed below.
### $this->tags_info is filled with results of $this->mk_read_html_tags(), and this is default input for $this->mk_write_tags.
###
### Miscellaneous class variables
###
### ARRAY this->protocols
### 	definition of supported URL protocols to replace by links. You can define/undefine protocols setting it. See below to mk_t2h() for example to find syntax rules (mostly self-explaining). ['return'] index means part of regex that defines returned text, internally it's used for ereg_replace() 'placeholders' (refer to PHP ereg_replace() manual for more info). Example of what 'return' index can be used for: <A HREF="mailto:hide@address.com">hide@address.com</A> - notice lack of 'mailto:' in displayed text.
###
### ARRAY tags_allow
### 	default list of tags allowed in text when $this->using mk_delete_html_tags().
###
### ARRAY tags_deny
### 	default list of tags to be cut from input when using $this->mk_delete_html_tags().
###
### ARRAY params_deny
### 	list of tags parameters that will be removed when using $this->mk_delete_html_tags().
###
### ARRAY html_translate_codes
### 	list of translations for numerical HTML entities, consists of integer codes (as array index) and char to be translated to (as value, if false then translation of entity is disabled). This translation is especially useful for texts copied from M$ Word or other text editors.
###
### BOOL allow_untranslated_2byte_entities
### 	allow 2-byte numerical HTML entities that are not listed in $html_translate_codes.
###
### INT error_id
### 	ID of last error
###
### STRING error_fn
### 	function in which last error occured
###
### STRING error_item
### 	item which has improper value causing error
###
###
### 2. Function reference
###
### BOOL mk_set_cvar(STRING variable_name[,STRING variable_value = NULL]) - 
### 	you can set class variables with it. Unsets previously defined value is necessary (i.e. old value is array and the new one is not array). Returns true when setting is successfull, false on error.
###
### BOOL mk_set_error([STRING error_function = NULL[,INT error_id = 0[,STRING error_item = NULL]]])
### 	used internally to set error codes. Returns true.
###
### STRING mk_addr2link([STRING input_text = $this->output_text[,ARRAY protocols_list = NULL]]) - 
### 	replace URLs in given text. if not undefined or NULL (means: all supported protocols) $protocols_list should be array('http'=>true,'ftp'=>true,'mailto'=>true,'protocolX'=>boolean_true_to_replace_or_false_to_not_replace, etc.). Returns text with replaces, output modifies $this->output_text.
###
### STRING mk_parse_cmdline_html(STRING cmd_line_to_parse) - 
### 	used internally to parse tags parameters. Input is HTML tag without '<' & '>'. Returns array with tag parameters - index [''] contains tag type, named indexes contain param=>value. For example, parsing tag <SELECT SIZE="1" MULTIPLE> will return array(''=>'SELECT',SIZE=>'1',MULTIPLE=>true).
###
### INT mk_strpos(STRING needle,STRING haystack[,INT offset = 0])
### INT mk_stripos(STRING needle,STRING haystack[,INT offset = 0])
### 	almost identical to PHP str[i]pos(), used internally. Written from scratch because of strange, unresolved problems (bug?) with original ones. Refer to PHP manual for more info. Returns $haystack with replaces.
###
### STRING mk_replace_by_index(ARRAY replaces[,STRING input_text = $this->output_text[,BOOL case_sensitive = true]])
### 	replace multiple strings. Does not use str_[i]replace(). $replaces should be array('string_to_replace'=>'replace_with','replace me'=>'replace with me', etc.). Similar to strtr($input_text,$replaces), but with case insensitivity as an option.
###
### STRING mk_html_resolve([STRING input_text = $this->output_text[,BOOL resolve_numeric = true]])
### 	similar to reversed htmlspecialchars(), but it does a little more. Optional parameter $resolve_numeric defines if HTML ASCII representation is converted to chars ("&#039;" to "'", etc. Warning! This implies high performance cost!). You can modify $this->html_replaces array variable to change list of replaces (refer to $this->mk_replace_by_index() $replaces param for syntax).
###
### ARRAY mk_read_html_tags([STRING input_text = $this->output_text])
### 	read and parse given input text for HTML tags. Returns array featuring tags information interlaced with tagged strings. For text part of input it's: array('type'=>0,'value'=>string). For tags it's: array('type'=>1,'value'=>output of $this->mk_parse_cmdline_html(),'open'=>0|1|2). ['open'] is: 0 for closing tags (</LI>), 1 for opening tags (<LI>), 2 for self-closing tags (<LI />). Output modifies $this->tags_info.
###
### STRING mk_write_html([ARRAY tags_info = $this->tags_info])
### 	reversed $this->mk_read_html_tags(). Output modifies $this->output_text.
###
### STRING mk_delete_html_tags([STRING input_text = $this->output_text[,ARRAY tag_list = $this->tags_allow|$this->tags_deny[, ARRAY strip_tags = true[,ARRAY delete_params[,BOOL addr2link_flag = true[,BOOL nl2br_flag = true]]]]]])
### 	clears input text from unwanted tags. Depending on value of parameter $strip_tags, tags in $tag_list are saved (true) or stripped (false). $tag_list should be array('tag'=>true,'another_tag'=>true, etc.). Tag parameters in $delete_params (syntax identical to $tags_list) will allways be removed. $addr2link defines to enable/disable URL to link conversion with $this->mk_addr2link(). $nl2br_flag defines to enable/disable conversion of (\r)\n to <BR /> with $this->mk_nl2br() outside HTML tags. Output modifies $this->output_text.
### 
### STRING mk_text2html([STRING input_text = $this->output_text[,BOOL addr2link_flag = true[,BOOL strip_tags_flag = true[,BOOL nl2br_flag = true[,ARRAY save_tags = $this->tags_allow]]]]])
### 	parse input text with multiple functions: convert URLs to links, convert to valid HTML without unwanted tags, convert line breaks to <BR>s. Parameters are self-explaining.
###
### STRING mk_replace_vars(ARRAY var_list[,STRING input_text = $this->output_text[,BOOL delete_invalid_flag = true]])
### 	replace in-text [%variables] with defined strings. $var_list should be array('var_name'=>'replace value','another var name'=>'another value',etc.). If $delete_invalid_flag is true, all [%bad_variable]s will be deleted.
###
### STRING mk_html_entities_all([STRING input_text])
### 	convert all input chars to HTML decimal ASCII codes (space -> '&#32;', etc.).
###
### STRING mk_at_anti_spam([STRING input_text])
### 	convert all @ (at) chars in input to HTML hexadecimal ASCII codes (&#x0040 in this case). Prevents detection of e-mail addresses by spam bots. Output modifies $this->output_text.
###
### STRING mk_ereg_filter(STRING haystack,STRING ereg_search,STRING filter_fn[,INT filter_placeholder = 0[,BOOL $case_sensitive = false]])
### or STRING mk_ereg_filter(STRING haystack,STRING ereg_search,ARRAY filter_fn[,INT filter_placeholder = 0[,BOOL $case_sensitive = false]])
### 	replace strings defined by $ereg_search regular expression with filtered value. Similar a bit to ereg(i)_replace (as it uses those functions internally), but replace string here is hardcoded 'placeholder' ('\0' for default, set $filter_placeholder to 1 for '\1' etc., refer to PHP ereg_replace() manual for more info), that's replaced value is additionally filtered with function defined in $filter_fn. Example: if you want to replace all letters with their ASCII code, use: mk_ereg_filter($input_string,'[a-zA-Z]{1}','ord',0). Function defined with $filter_fn gets 1 parameter, that being replacement for 'placeholder' string. If you want to use class method as filtering function, $filter_fn should be: array(0=>'class_name',1=>'class_method').
###
### STRING mk_html_chr(STRING input_code)
### 	replace ASCII codes with appropriate characters. Codes can be decimal (digits only), or hexadecimal prepended by 'x' ('x3F' for decimal '63', etc.). For codes <= 255 chr() is used internally for conversion, for codes > 255 replacement table stored in $this->html_translate_codes array is used, and if code isn't found in table it's dropped ('' as result).
###
### STRING mk_html_special_chars([STRING input_text])
### 	similar to PHP's htmlspecialchars(), but it doesn't replace '&' (ampersand) when it begins HTML entity (for example, it will convert '&copy; M & K' to '&copy; M &amp; K').
###
###
### HTML comments (<!-- -->) will be supported in future versions.
###
###
### Mariusz Kaczmarczyk <hide@address.com>
### http://www.ck-la.tk/
### If you find bug in class or manual, or have problems with syntax, don't hesitate to contact me, I can't promise I will answer all the questions but will try. :-)
### This class is for the moment used on websites:
### - http://ps.syndykalista.org/
### - http://gula.syndykalista.org/
### - http://www.ck-la.tk/

class mk_t2h {
	var $input_text=NULL;
	var $output_text=NULL;
	var $protocols=NULL;
	var $tags_info=NULL;
	var $tags_allow=NULL;
	var $tags_deny=NULL;
	var $params_deny=NULL;
	var $html_replaces=NULL;
	var $error_id=0;
	var $error_fn=NULL;
	var $error_item=NULL;
	var $html_syntax_def=NULL;
	var $var_format=NULL;
	
	function mk_t2h($input_text=NULL) {
		$proto_transfer_common='([0-9a-zA-Z/._+,;?&=^()%$#@!-]*)([0-9a-zA-Z/_+?&=^%$!-]+)';
		$protocols['mailto']['return']=2;
		$protocols['mailto']['regexp']='(mailto:[/]{0,2}){1}(([0-9a-zA-Z._+-]+)@([0-9a-zA-Z._+-]*)([0-9a-zA-Z_+-]+))';
		$protocols['mailto']['target']=NULL;
		$protocols['mailto']['filter_url']='$this->mk_html_entities_all';
		$protocols['mailto']['filter_name']='$this->mk_html_entities_all';
		$protocols['http']['return']=0;
		$protocols['http']['regexp']='(http://){1}('.$proto_transfer_common.')';
		$protocols['http']['target']='_blank';
		$protocols['ftp']['return']=0;
		$protocols['ftp']['regexp']='(ftp://){1}('.$proto_transfer_common.')';
		$protocols['ftp']['target']='_blank';
		$protocols['mms']['return']=0;
		$protocols['mms']['regexp']='(mms://){1}('.$proto_transfer_common.')';
		$protocols['mms']['target']='_blank';
		$protocols['https']['return']=0;
		$protocols['https']['regexp']='(https://){1}('.$proto_transfer_common.')';
		$protocols['https']['target']='_blank';
		$protocols['ftps']['return']=0;
		$protocols['ftps']['regexp']='(ftps://){1}('.$proto_transfer_common.')';
		$protocols['ftps']['target']='_blank';
		$protocols['ssh']['return']=0;
		$protocols['ssh']['regexp']='(ssh:){1}([/]{0,2})('.$proto_transfer_common.')';
		$protocols['ssh']['target']=NULL;
		$protocols['telnet']['return']=0;
		$protocols['telnet']['regexp']='(telnet:){1}([/]{0,2})('.$proto_transfer_common.')';
		$protocols['telnet']['target']=NULL;
		$protocols['file']['return']=3;
		$protocols['file']['regexp']='(file:[/]{2,3}){1}('.$proto_transfer_common.')';
		$protocols['file']['target']='_blank';
		$html_syntax_def['separator_char']=' ';
		$html_syntax_def['equal_char']='=';
		$html_syntax_def['quote_chars']=array('"'=>true,"'"=>true);
		$html_syntax_def['end_tag_char']='/';
		$tag_ereg="<[/]?[[:space:]!-;=?-~\x80-\xFF-]+>";
		$tags_allow=array(
			'span'=>true,
			'br'=>true,
			'b'=>true,
			'i'=>true,
			'u'=>true,
			'pre'=>true,
			'h1'=>true,
			'h2'=>true,
			'h3'=>true,
			'h4'=>true,
			'h5'=>true,
			'h6'=>true,
			'blockquote'=>true,
			'tt'=>true,
			'sup'=>true,
			'sub'=>true,
			'strike'=>true,
			'cite'=>true,
			'dfn'=>true,
			'abbr'=>true,
			'acronym'=>true,
			'strong'=>true,
			'em'=>true,
			'code'=>true,
			'kbd'=>true,
			'samp'=>true,
			'var'=>true,
			'table'=>true,
			'tr'=>true,
			'td'=>true,
			'hr'=>true,
			'li'=>true,
			'ol'=>true,
			'ul'=>true,
			'font'=>true,
			'p'=>false
		);
		$tags_deny=array(
			'img'=>true,
			'script'=>true,
			'link'=>true,
			'style'=>true,
			'head'=>true,
			'meta'=>true,
			'http-equiv'=>true,
			'html'=>true,
			'body'=>true,
			'nobr'=>true,
			'a'=>true
		);
		$params_deny=array(
			'onmouseover'=>true,
			'onmouseout'=>true,
			'onclick'=>true,
			'onload'=>true,
			'onunload'=>true,
			'onchange'=>true,
			'onsubmit'=>true,
			'size'=>true,
			'style'=>true,
			'class'=>true,
			'width'=>true,
			'height'=>true,
			'cellspacing'=>true,
			'cellpadding'=>true,
			'hspace'=>true,
			'vspace'=>true,
			'nowrap'=>true
		);
		$html_replaces=array(
###	you can uncomment/edit lines below to change behaviour of $this->mk_html_resolve(), but this is recommended only in some special cases
// 			"\r\n"=>' ',
// 			"\n\r"=>' ',
// 			"\n"=>' ',
// 			"\t"=>' ',
// 			'&quot;'=>'"',
// 			'&gt;'=>'>',
// 			'&lt;'=>'<',
// 			'&nbsp;'=>chr(160),
// 			'&amp;'=>'&',
		);
		$html_translate_codes=array(
			8208=>'-',
			8211=>'-',
			8212=>'-',
			8213=>'-',
			8216=>'`',
			8217=>"'",
			8218=>',',
			8219=>"'",
			8220=>'"',
			8221=>'"',
			8222=>'"',
// 			8226=>chr(249),
			8230=>'...',
			8242=>"'",
			8243=>'"',
			8249=>"<",
			8250=>'>',
			8252=>'!!',
			8260=>'/',
		);
		$allow_untranslated_2byte_entities=true;
		$var_format['prefix']='[%';
		$var_format['suffix']=']';
		$var_format['name_ereg']='[a-zA-Z0-9._]+';
		$line_break_tag='<BR />';
		if($input_text!==NULL) {
			$this->mk_set_cvar('input_text',$input_text);
			$this->mk_set_cvar('output_text',$input_text);
		};
		$this->mk_set_cvar('html_syntax_def',$html_syntax_def);
		$this->mk_set_cvar('protocols',$protocols);
		$this->mk_set_cvar('tag_ereg',$tag_ereg);
		$this->mk_set_cvar('tags_allow',$tags_allow);
		$this->mk_set_cvar('tags_deny',$tags_deny);
		$this->mk_set_cvar('params_deny',$params_deny);
		$this->mk_set_cvar('html_replaces',$html_replaces);
		$this->mk_set_cvar('html_translate_codes',$html_translate_codes);
		$this->mk_set_cvar('allow_untranslated_2byte_entities',$allow_untranslated_2byte_entities);
		$this->mk_set_cvar('var_format',$var_format);
		$this->mk_set_cvar('line_break_tag',$line_break_tag);
	}
	
	function mk_set_cvar($var_name=NULL,$var_value=NULL) {
		if($var_name===NULL) {
			return(false);
		};
		if(isset($this->{$var_name})) {
			if(is_array($this->{$var_name})!=is_array($var_value)) {
				unset($this->{$var_name});
			};
		};
		return($this->{$var_name}=$var_value);
	}
	
	function mk_set_error($error_fn=NULL,$error_id=0,$error_item=NULL) {
		$this->mk_set_cvar('error_fn',$error_fn);
		$this->mk_set_cvar('error_id',$error_id);
		$this->mk_set_cvar('error_item',$error_item);
		return(true);
	}
	
	function mk_addr2link($input_text=NULL,$protocol_list=NULL) {
		if((!is_array($protocol_list))&&($protocol_list!==NULL)) {
			$this->mk_set_error('mk_addr2link',1,'$protocol_list');
			return(false);
		};
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_addr2link',2,'$input_text');
				return(false);
			};
		};
		$url_suffix_regexp='';
		$edit_text=$input_text;
		$protocols=$this->protocols;
		if((isset($protocols))&&(is_array($protocols))){ 
			foreach($protocols as $id1=>$val1) {
				if((isset($val1['return']))&&(isset($val1['regexp']))) {
					if(($protocol_list==NULL)||((isset($protocol_list[$id1]))&&($protocol_list[$id1]))) {
						$tmp_replace='';
						$tmp_replace.='<A HREF="\0"';
						if((isset($val1['target']))&&($val1['target']!='')) {
							$tmp_replace.=' TARGET="'.$val1['target'].'"';
						};
						$tmp_replace.='>\\'.$val1['return'].'</A>';
						$tmp_find_ereg_str=$val1['regexp'];
						$edit_text=eregi_replace($tmp_find_ereg_str,$tmp_replace,$edit_text);
						unset($tmp_replace);
					};
				};
			};
		};
		$this->mk_set_cvar('output_text',$edit_text);
		$this->mk_set_error(NULL,0,NULL);
		return($edit_text);
	}
	
	function mk_parse_cmdline_html($cmd_line_str=NULL, $param0_upper = true, $paramx_upper = true) {
		if($cmd_line_str=='') {
			$this->mk_set_error('mk_parse_cmdline_html',1,'$cmd_line_str');
			return(false);
		};
		if((!isset($this->html_syntax_def['separator_char']))||(!isset($this->html_syntax_def['equal_char']))||(!isset($this->html_syntax_def['quote_chars']))||(!is_array($this->html_syntax_def['quote_chars']))) {
			$this->mk_set_error('mk_parse_cmdline_html',2,'$this->html_syntax_def[\'quote_chars\']');
			return(false);
		};
		$separator_char=$this->html_syntax_def['separator_char'];
		$equal_char=$this->html_syntax_def['equal_char'];
		$quote_chars=$this->html_syntax_def['quote_chars'];
		$end_tag_char=$this->html_syntax_def['end_tag_char'];
		if($separator_char=='') {
			$this->mk_set_error('mk_parse_cmdline_html',3,'$separator_char');
			return(false);
		};
		if($equal_char=='') {
			$this->mk_set_error('mk_parse_cmdline_html',4,'$equal_char');
			return(false);
		};
		if($end_tag_char=='') {
			$this->mk_set_error('mk_parse_cmdline_html',5,'$end_tag_char');
			return(false);
		};
		$tmp_cmd_str=trim($cmd_line_str).$separator_char;
		if($tmp_cmd_str==$separator_char){
			$this->mk_set_error('mk_parse_cmdline_html',6,'$tmp_cmd_str');
			return(false);
		};
		$cmd_len=strlen($tmp_cmd_str);
		$tmp_quoted_flag=false;
		$param_id=0;
		$tmp_part_id=0;
		$tmp_part[0]='';
		$tmp_part[1]='';
		$tmp_raw_part='';
		$tmp_prev_char='';
		$self_ending_flag=false;
		for($x1=0;$x1<$cmd_len;$x1++) {
			$tmp_char=$tmp_cmd_str[$x1];
			if((isset($quote_chars[$tmp_char]))&&($quote_chars[$tmp_char])) {
				$tmp_quoted_flag=!$tmp_quoted_flag;
			} else {
				if($tmp_quoted_flag) {
					$tmp_part[$tmp_part_id].=$tmp_char;
					$tmp_raw_part.=$tmp_char;
				} else {
					switch($tmp_char) {
						case $separator_char:
						case "\n":
						case "\r":
							if($tmp_part[0]!='') {
								$tmp_param_raw=trim($tmp_part[0]);
								if ((($param_id==0) && ($param0_upper)) || (($param_id!=0) && ($paramx_upper))) {
									$tmp_param_name=strtoupper($tmp_param_raw);
								} else {
									$tmp_param_name=$tmp_param_raw;
								};
								$tmp_rtrim_slash_flag=false;
								$tmp_raw_len=strlen($tmp_raw_part);
								if(($tmp_prev_char==$end_tag_char)&&($tmp_raw_len>1)) {
									$self_ending_flag=true;
									$tmp_rtrim_slash_flag=true;
								};
								if($tmp_param_name!=$end_tag_char) {
									if($param_id==0) {
										if($tmp_rtrim_slash_flag) {
											$param['']=rtrim($tmp_param_name,$end_tag_char);
										} else {
											$param['']=$tmp_param_name;
										};
									} else {
										if($tmp_part[1]=='') {
											if($tmp_rtrim_slash_flag) {
												$param[rtrim($tmp_param_name,$end_tag_char)]=true;
											} else {
												$param[$tmp_param_name]=true;
											};
										} else {
											if($tmp_rtrim_slash_flag) {
												$param[$tmp_param_name]=rtrim($tmp_part[1],$end_tag_char);
											} else {
												$param[$tmp_param_name]=$tmp_part[1];
											};
										};
									};
								} else {
									$self_ending_flag=true;
								};
								if($self_ending_flag) {
									$param[$end_tag_char]=true;
								};
								$tmp_part[0]='';
								$tmp_part[1]='';
								$tmp_raw_part='';
								$tmp_part_id=0;
								$self_ending_flag=false;
								$param_id++;
							};
						break;
						case $equal_char:
							if($tmp_part_id==0) {
								$tmp_part_id=1;
							};
						break;
						default:
							$tmp_part[$tmp_part_id].=$tmp_char;
							$tmp_raw_part.=$tmp_char;
						break;
					};
				};
			};
			$tmp_prev_char=$tmp_char;
		};
		if((!isset($param))||(!is_array($param))){
			$this->mk_set_error('mk_parse_cmdline_html',7,'$param');
			return(false);
		};
		$this->mk_set_error(NULL,0,NULL);
		return($param);
	}
	
	function mk_strpos($needle=NULL,$haystack=NULL,$offset=0) {
		if($haystack=='') {
			return(false);
		};
		if($needle=='') {
			$this->mk_set_error('mk_strpos',1,'$needle,$haystack');
			return(false);
		};
		$haystack_len=strlen($haystack);
		if($offset>$haystack_len) {
			$this->mk_set_error('mk_strpos',2,'$offset');
			return(false);
		};
		$haystack_cut=substr($haystack,$offset);
		$tmp_needle_fragm='';
		$x1=$offset;
		$x2=0;
		while(($tmp_needle_fragm!=$needle)&&($x1<$haystack_len)) {
			$tmp_char=$haystack[$x1];
			if($tmp_char==$needle[$x2]) {
				$x2++;
				$tmp_needle_fragm.=$tmp_char;
			} else {
				$x2=0;
				$tmp_needle_fragm='';
			};
			$x1++;
		};
		$this->mk_set_error(NULL,0,NULL);
		if($tmp_needle_fragm==$needle) {
			return($x1-strlen($tmp_needle_fragm));
		} else {
			return(false);
		};
	}
	
	function mk_stripos($needle=NULL,$haystack=NULL,$offset=0) {
		$this->mk_set_error(NULL,0,NULL);
		return($this->mk_strpos(strtolower($needle),strtolower($haystack),$offset));
	}
	
	function mk_replace_by_index($replaces=NULL,$input_text=NULL,$case_sensitive=true) {
		if(!is_array($replaces)) {
			$this->mk_set_error('mk_replace_by_index',1,'$replaces');
			return(false);
		};
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_replace_by_index',2,'$input_text');
				return(false);
			};
		};
		$edit_text=$input_text;
		foreach($replaces as $id1=>$val1) {
			$tmp_index=0;
			$tmp_oper_text=$edit_text;
			do {
				if($case_sensitive) {
					$tmp_found_at=$this->mk_strpos($id1,$tmp_oper_text,$tmp_index);
				} else {
					$tmp_found_at=$this->mk_stripos($id1,$tmp_oper_text,$tmp_index);
				};
				if($tmp_found_at!==false) {
					$tmp_old_piece_len=strlen($id1);
					$tmp_new_piece_len=strlen($val1);
					$tmp_after_replace_pos=$tmp_found_at+$tmp_old_piece_len;
					$tmp_oper_text=substr($tmp_oper_text,0,$tmp_found_at).$val1.substr($tmp_oper_text,$tmp_after_replace_pos);
					$tmp_index=$tmp_after_replace_pos+($tmp_new_piece_len-$tmp_old_piece_len);
				};
			} while($tmp_found_at!==false);
			$edit_text=$tmp_oper_text;
		};
		$this->mk_set_error(NULL,0,NULL);
		return($edit_text);
	}
	
	function mk_nl2br($input_text=NULL) {
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_nl2br',1,'$input_text');
				return(false);
			};
		};
		if(!isset($this->line_break_tag)) {
			$this->mk_set_error('mk_nl2br',2,'$this->line_break_tag');
			return(false);
		};
		$br_string=$this->line_break_tag;
		$edit_text=$input_text;
		$edit_text=$this->mk_replace_by_index(array("\r"=>'',"\n"=>$br_string."\n"),$edit_text);
		if($edit_text===false) {
			$this->mk_set_error('mk_nl2br',3,'$edit_text');
			return(false);
		} else {
			$this->mk_set_cvar('output_text',$edit_text);
		};
		$this->mk_set_error(NULL,0,NULL);
		return($edit_text);
	}
	
	function mk_html_resolve($input_text=NULL,$resolve_numeric=true,$resolve_misc_1byte=true) {
		if((isset($this->html_replaces))&&(is_array($this->html_replaces))) {
			$html_replaces=$this->html_replaces;
		} else {
			$this->mk_set_error('mk_html_resolve',1,'$html_replaces');
			return(false);
		};
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_html_resolve',2,'$input_text');
				return(false);
			};
		};
		$edit_text=$input_text;
		$edit_text=$this->mk_replace_by_index($html_replaces,$edit_text,false);
		if($resolve_numeric) {
			$edit_text=$this->mk_ereg_filter($edit_text,'&#(((x)?[0-9a-f]*)|([0-9]*));',array('mk_t2h','mk_html_chr'),1,false);
		};
		if($resolve_misc_1byte) {
			$edit_text=html_entity_decode($edit_text);
		};
		if($edit_text===false) {
			$this->mk_set_error('mk_html_resolve',3,'$edit_text');
			return(false);
		} else {
			$this->mk_set_cvar('output_text',$edit_text);
		};
		$this->mk_set_error(NULL,0,NULL);
		return($edit_text);
	}
	
	function mk_read_html_tags($input_text=NULL) {
		if(isset($this->tag_ereg)) {
			$tag_ereg=$this->tag_ereg;
		} else {
			$this->mk_set_error('mk_read_html_tags',1,'$this->tag_ereg');
			return(false);
		};
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_read_html_tags',2,'$input_text');
				return(false);
			};
		};
		if($input_text=='') {
			$this->mk_set_error(NULL,0,NULL);
			return(array());
		};
		$edit_text=$input_text;
		$x1=0;
		while(($edit_text=substr($edit_text,$x1))&&($edit_text!='')&&(eregi($tag_ereg,$edit_text,$tag_info))) {
			$tag_start_pos=$this->mk_stripos($tag_info[0],$edit_text);
			$tag_len=strlen($tag_info[0]);
			$tag_str=trim($tag_info[0],'<>');
			$tag_params=$this->mk_parse_cmdline_html($tag_str);
			$tmp_pretag_text=substr($edit_text,0,$tag_start_pos);
			if($tmp_pretag_text!=''){
				$tags_out[]=array('type'=>0,'value'=>$this->mk_html_special_chars($this->mk_html_resolve($tmp_pretag_text,true,false),ENT_QUOTES));
			};
			if(($tag_params['']!='')&&($tag_params[''][0]=='/')) {
				$tag_params=array(''=>substr($tag_params[''],1));
				$tmp_tag_type=0;
			} else {
				if((isset($tag_params['/']))&&($tag_params['/']===true)) {
					$tmp_tag_type=2;
				} else {
					$tmp_tag_type=1;
				};
			};
			$tags_out[]=array('type'=>1,'value'=>$tag_params,'open'=>$tmp_tag_type);
			$x1=$tag_start_pos+$tag_len;
			unset($tag_info);
		};
		$tags_out[]=array('type'=>0,'value'=>$this->mk_html_special_chars($this->mk_html_resolve($edit_text,true,false)));
		$this->mk_set_cvar('tags_info',$tags_out);
		$this->mk_set_error(NULL,0,NULL);
		return($tags_out);
	}
	
	function mk_write_html($tags_in=NULL) {
		if($tags_in===NULL) {
			if(isset($this->tags_info)) {
				$tags_in=$this->tags_info;
			} else {
				$this->mk_set_error('mk_write_html',1,'$tags_in');
				return(false);
			};
		};
		if(!is_array($tags_in)){
			$this->mk_set_error('mk_write_html',2,'$tags_in');
			return(false);
		};
		foreach($tags_in as $id1=>$val1){
			if((isset($val1['type']))&&(isset($val1['value']))) {
				switch($val1['type']) {
					case 0:
						$html_content[]=$val1['value'];
					break;
					case 1:
						$tmp_tag_prefix='';
						$tmp_tag_suffix='';
						if((isset($val1['value']['']))&&(isset($val1['open']))) {
							$tmp_html_tag='';
							$tmp_html_tag.='<';
							switch($val1['open']) {
								case 0:
									$tmp_tag_prefix.='/';
								break;
								case 2:
									if(!isset($val1['value']['/'])) {
										$tmp_tag_suffix.=' /';
									};
								break;
							};
							$tmp_html_tag.=$tmp_tag_prefix.strtoupper($val1['value']['']);
							foreach($val1['value'] as $id2=>$val2) {
								if(($id2!='')&&($val1['open']!=0)) {
									if($val2===true){
										$tmp_html_tag.=' '.strtoupper($id2);
									} else {
										$tmp_html_tag.=' '.strtoupper($id2).'="'.$this->mk_html_special_chars($val2).'"';
									};
								};
							};
							$tmp_html_tag.=$tmp_tag_suffix.'>';
							$html_content[]=$tmp_html_tag;
							unset($tmp_html_tag);
						};
					break;
				};
			};
		};
		if((!isset($html_content))||(!is_array($html_content))){
			$this->mk_set_error(NULL,0,NULL);
			return(false);
		};
		$output_text=implode('',$html_content);
		$this->mk_set_error(NULL,0,NULL);
		$this->mk_set_cvar('output_text',$output_text);
		return($output_text);
	}
	
	function mk_delete_html_tags($input_text=NULL,$tag_list=NULL,$default_strip_flag=true,$delete_params=NULL,$addr2link_flag=true,$nl2br_flag=true) {
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_delete_html_tags',1,'$input_text');
				return(false);
			};
		};
		if($input_text=='') {
			$this->mk_set_error(NULL,0,NULL);
			return('');
		};
		if($tag_list===NULL) {
			if($default_strip_flag) {
				if(isset($this->tags_allow)) {
					$tag_list_final=$this->tags_allow;
				} else {
					$tag_list_final=array();
				};
			} else {
				if(isset($this->tags_deny)) {
					$tag_list_final=$this->tags_deny;
				} else {
					$tag_list_final=array();
				};
			};
		} elseif(is_array($tag_list)) {
			$tag_list_final=$tag_list;
		} else {
			$this->mk_set_error('mk_delete_html_tags',3,'$tag_list');
			return(false);
		};
		$tag_list_final=array_change_key_case($tag_list_final,CASE_UPPER);
		if($delete_params===NULL) {
			if(isset($this->params_deny)) {
				$params_kill=$this->params_deny;
			} else {
				$params_kill=array();
			};
		} elseif(is_array($delete_params)) {
			$params_kill=$delete_params;
		} else {
			$this->mk_set_error('mk_delete_html_tags',4,'$params_kill');
			return(false);
		};
		$tags_info=$this->mk_read_html_tags($input_text);
		if(!is_array($tags_info)) {
			$this->mk_set_error('mk_delete_html_tags',5,'$tags_info');
			return(false);
		};
		if((isset($params_kill))&&(is_array($params_kill))) {
			$params_kill=array_change_key_case($params_kill,CASE_UPPER);
		};
		foreach($tags_info as $id1=>$val1) {
			if(isset($val1['type'])) {
				switch($val1['type']) {
					case 0:
						if(isset($val1['value'])) {
							$tmp_text_info=$val1;
							if($addr2link_flag) {
								$tmp_text_info['value']=$this->mk_addr2link($tmp_text_info['value']);
							};
							if($nl2br_flag) {
								$tmp_text_info['value']=$this->mk_nl2br($tmp_text_info['value']);
							};
							$return_tags[]=$tmp_text_info;
						};
					break;
					case 1:
						if(isset($val1['value'][''])) {
							$tmp_tag_name=strtoupper($val1['value']['']);
							$tmp_tag_listed=(isset($tag_list_final[$tmp_tag_name]))&&($tag_list_final[$tmp_tag_name]);
							if($tmp_tag_listed==$default_strip_flag) {
								if((isset($params_kill))&&(is_array($params_kill))) {
									foreach($val1['value'] as $id2=>$val2) {
										$tmp_tag_param_name=strtoupper($id2);
										if(((isset($params_kill[$tmp_tag_param_name]))&&($params_kill[$tmp_tag_param_name]))||((isset($params_kill['']))&&($params_kill['']))) {
											unset($val1['value'][$id2]);
										};
									};
								};
								$return_tags[]=$val1;
							};
						};
					break;
				};
			};
		};
		$this->mk_set_error(NULL,0,NULL);
		if((!isset($return_tags))||(!is_array($return_tags))) {
			return(false);
		};
		$output_text=$this->mk_write_html($return_tags);
		if($output_text!==false) {
			$this->mk_set_cvar('output_text',$output_text);
		};
		return($output_text);
	}
	
	function mk_text2html($input_text=NULL,$addr2link_flag=true,$strip_tags_flag=true,$nl2br_flag=true,$save_tags=NULL,$at_anti_spam=true) {
		if(($save_tags===NULL)||(is_array($save_tags))) {
			$save_tags_final=$save_tags;
		} else {
			$this->mk_set_error('mk_text2html',1,'$save_tags');
			return(false);
		};
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_text2html',2,'$input_text');
				return(false);
			};
		};
		if($input_text=='') {
			$this->mk_set_error(NULL,0,NULL);
			return('');
		};
		$edit_text=$input_text;
		if($strip_tags_flag) {
			$edit_text=$this->mk_delete_html_tags($edit_text,$save_tags_final,true,NULL,$addr2link_flag,$nl2br_flag);
		};
		if($at_anti_spam) {
			$edit_text=$this->mk_at_anti_spam($edit_text);
		};
		$this->mk_set_error(NULL,0,NULL);
		$this->mk_set_cvar('output_text',$edit_text);
		return($edit_text);
	}
	
	function mk_replace_vars($var_list=NULL,$input_text=NULL,$delete_invalid_flag=true) {
		if(!is_array($var_list)) {
			$this->mk_set_error('mk_replace_vars',1,'$var_list');
			return(false);
		};
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_replace_vars',2,'$input_text');
				return(false);
			};
		};
		if($input_text=='') {
			$this->mk_set_error(NULL,0,NULL);
			return(false);
		};
		$var_format=$this->var_format;
		if((!isset($var_format['name_ereg']))||(!isset($var_format['prefix']))||(!isset($var_format['suffix']))) {
			$this->mk_set_error('mk_replace_vars',3,'$var_format[\'name_ereg\'],$var_format[\'prefix\'],$var_format[\'suffix\']');
			return(false);
		};
		$output_text=$input_text;
		foreach($var_list as $id1=>$val1) {
			if(eregi($var_format['name_ereg'],$id1)) {
				$tmp_var_str=$var_format['prefix'].$id1.$var_format['suffix'];
				$output_text=str_replace($tmp_var_str,$val1,$output_text);
			};
		};
		if($delete_invalid_flag) {
			$tmp_ereg['prefix']=quotemeta($var_format['prefix']);
			$tmp_ereg['suffix']=quotemeta($var_format['suffix']);
			$tmp_ereg['name_ereg']=$var_format['name_ereg'];
			$output_text=eregi_replace($tmp_ereg['prefix'].$tmp_ereg['name_ereg'].$var_format['suffix'],'',$output_text);
		};
		$this->mk_set_error(NULL,0,NULL);
		$this->mk_set_cvar('output_text',$output_text);
		return($output_text);
	}
	
	function mk_html_entities_all($input_text=NULL) {
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_html_entities_all',1,'$input_text');
				return(false);
			};
		};
		$input_len=strlen($input_text);
		$edit_text='';
		for($x1=0;$x1<$input_len;$x1++) {
			$edit_text.='&#'.ord($input_text[$x1]).';';
		};
		$this->mk_set_error(NULL,0,NULL);
		$this->mk_set_cvar('output_text',$edit_text);
		return($edit_text);
	}
	
	function mk_at_anti_spam($input_text=NULL) {
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_at_anti_spam',1,'$input_text');
				return(false);
			};
		};
		$output_text=str_replace('@','&#x0040;',$input_text);
		$output_text=str_replace('mailto:','&#x004d;&#x0041;&#x0049;&#x004c;&#x0054;&#00111;&#0058;',$output_text);
		$this->mk_set_error(NULL,0,NULL);
		$this->mk_set_cvar('output_text',$output_text);
		return($output_text);
	}
	
	function mk_ereg_filter($haystack=NULL,$ereg_search=NULL,$filter_fn=NULL,$filter_placeholder=0,$case_sensitive=false) {
		if($haystack===NULL) {
			$this->mk_set_error('mk_ereg_filter',1,'$haystack');
			return(false);
		};
		if($ereg_search===NULL) {
			$this->mk_set_error('mk_ereg_filter',2,'$ereg_search');
			return(false);
		};
		if($filter_fn===NULL) {
			$this->mk_set_error('mk_ereg_filter',3,'$filter_fn');
			return(false);
		};
		if(is_array($filter_fn)) {
			if((isset($filter_fn[0]))&&(isset($filter_fn[1]))) {
				$filter_fn_code=$filter_fn[0].'::'.$filter_fn[1];
			} else {
				return(false);
			};
		} else {
			$filter_fn_code=$filter_fn;
		};
		if(!is_callable($filter_fn,false)) {
			$this->mk_set_error('mk_ereg_filter',4,'$filter_fn');
			return(false);
		};
		if($case_sensitive) {
			$ev_line="'".ereg_replace($ereg_search,"'.".$filter_fn_code."('\\".($filter_placeholder+0)."').'",addslashes($haystack))."'";
		} else {
			$ev_line="'".eregi_replace($ereg_search,"'.".$filter_fn_code."('\\".($filter_placeholder+0)."').'",addslashes($haystack))."'";
		};
		@eval('$edit_haystack=stripslashes('.$ev_line.');');
		if(!isset($edit_haystack)) {
			$this->mk_set_error('mk_ereg_filter',5,'$edit_haystack');
			return(false);
		};
		$this->mk_set_error(NULL,0,NULL);
		return($edit_haystack);
	}
	
	function mk_html_chr($input_code=NULL) {
		if($input_code===NULL) {
			$this->mk_set_error('mk_html_chr',1,'$input_code');
			return(false);
		};
		if(is_array($input_code)) {
			$this->mk_set_error('mk_html_chr',2,'$input_code');
			return(false);
		};
		if(strtolower($input_code[0])=='x') {
			$out_code=('0'.$input_code)+0;
		} else {
			$out_code=ltrim($input_code,'0')+0;
		};
		if((isset($this->html_translate_codes[$out_code]))&&($this->html_translate_codes[$out_code]!==false)) {
			$out_sign=$this->html_translate_codes[$out_code];
		} elseif($out_code<256) {
			$out_sign=chr($out_code);
		} elseif($this->allow_untranslated_2byte_entities) {
			$out_sign='&#'.$out_code.';';
		} else {
			$out_sign='';
		};
		$this->mk_set_error(NULL,0,NULL);
		return($out_sign);
	}
	
	function mk_html_special_chars($input_text=NULL) {
		if($input_text===NULL) {
			if(isset($this->output_text)) {
				$input_text=$this->output_text;
			} else {
				$this->mk_set_error('mk_html_special_chars',1,'$input_text');
				return(false);
			};
		};
		$edit_text='';
		$text_len=strlen($input_text);
		$special_chars=array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;');
		$x1=0;
		while($x1<$text_len) {
			$tmp_char=$input_text[$x1];
			if($tmp_char=='"') {
				$edit_text.='&quot;';
			} elseif(($tmp_char=='<')&&(($tmp_code_str=substr($input_text,$x1+1,3))&&($tmp_code_str=='!--'))) {
				$edit_text.='<!--';
				$x1+=3;
			} elseif($tmp_char=='<') {
				$edit_text.='&lt;';
			} elseif(($tmp_char=='>')&&(($tmp_code_str=substr($input_text,$x1-2,2))&&($tmp_code_str=='--'))) {
				$edit_text.='>';
			} elseif($tmp_char=='>') {
				$edit_text.='&gt;';
			} elseif($tmp_char=='&') {
				$tmp_next_semicolon_pos=$this->mk_strpos(';',$input_text,$x1+1);
				if(($tmp_next_semicolon_pos===false)||(!$tmp_code_str=substr($input_text,$x1+1,($tmp_next_semicolon_pos-($x1))))||(!eregi('(#)?(x)?[a-z0-9]+;',$tmp_code_str))) {
					$edit_text.='&amp;';
				} else {
					$edit_text.='&'.$tmp_code_str;
					$x1=$tmp_next_semicolon_pos;
				};
			} else {
				$edit_text.=$tmp_char;
			};
			$x1++;
		};
		$this->mk_set_error(NULL,0,NULL);
		$this->mk_set_cvar('output_text',$edit_text);
		return($edit_text);
	}
	
}

?>
Return current item: mk_t2h