summaryrefslogtreecommitdiff
path: root/tw/lang/TW_base.php
blob: ae84d6e8f40ab6d8570968556f98ac6bb88fb9d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?
/*
 * tag|wall                                           | PHP Tag Filter |
 * ---------------------------------------------------------------------

   Copyright (C) 2002  Juraj 'HVGE' Durech
   Copyright (C) 2002  www.designia.sk
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
   
 * ---------------------------------------------------------------------
 */

/*
 * $this->pt	text
 * $this->pti	text index
 */
class TW_base		// TW_lang extends this class
{
	var $error;		// error module object
	var $output;	// output module object
	var $out;		// output string
	
	var $content_off;
	
	var $tags;			// relationships between tags
	var $config;		// current configuration	(twParser::strip_tags())
	var $config_tags;	// tags in current configuration
	var $config_attr;	// attributes in current configuration
	var $config_req_attr;	// required attributes
		

	var $TAG;
	var $is_attributes;
	var $ATTRIBUTES;		// index => array("atr","value")
	var $ATTR;				// array(atr,value)
	var $VALUE;
	
/********************************************************************************************
 * BASE CONSTRUCTOR 
 */
	function TW_base()
	{
		global $tw_tag_relations;
		
		$this->stack();
		$this->tags = &$tw_tag_relations;
		$this->content_off = 0;
	}
/*
 * BASE "DESTRUCTOR" (parser call this function if end of string is rached)
 */
	function base_end()
	{
		while( $tag = $this->stack_pop() )
		{
			$this->out .= $this->output->close( $tag );
		}
		return null;
	}


/********************************************************************************************
 * TAG STACK implementation
 */
 	var $Tstack;

	function stack() 				{	$this->Tstack[0] = null;	}
	function stack_push( $tag )		{	array_unshift( $this->Tstack, $tag );	}
	function stack_pop()			{	return array_shift( $this->Tstack );	}
	function stack_search( $tag )	{	return in_array( $tag, $this->Tstack );	}

	function stack_top( $tag = null )
	{
		if( $tag )	return $this->Tstack[0] == $tag;
		return $this->Tstack[0];
	}




/********************************************************************************************
 *	S T A R T _ T A G _ f i l t e r			<tag atr=value>
 *
 */ 	
 	
 	function START_TAG_filter()
 	{
		$tag = $this->TAG;
		if( $tag == null )
		{
			//###### syntax error
			return;
		}
		if( in_array($tag,$this->config_tags) )
		{
			//enabled tag

			if(is_string($this->config_attr[$tag]))
			{
				$this->TAG = $tag = $this->config_attr[$tag];
				//tag substitution warning
			}
			
			// ------- perform attribute check -----------
			
			if($this->is_attributes)
			{
				if(!$this->config_attr[$tag])
				{
					$this->ATTRIBUTES = null;
					// remove attribute warning
				}
				else
				{
					foreach( $this->ATTRIBUTES as $key => $attr )
					{
						if( ! in_array($attr[0], $this->config_attr[$tag] ) )
						{
							$this->ATTRIBUTES[$key][1] = null;
							// remove attribute warning
							continue;
						}
						
						// --------- perform value check -----------
						
						if(($val = $attr[1])==null) continue;
						if(($cmda = &$this->config[$tag][$attr[0]])==null) continue; //null - accept all values
						switch($cmda[0] & 7)
						{
							case TW_URL:
								// V0.1.2: fixed some fatal bugs, big thanx to fczbkk
								//
								// TODO: make better url check with parse_url()
								
								$val = strtolower($val);
								if(strpos($val, "http://") === false)
								{
									if(strpos($val, "ftp://") === false)
									    if(strpos($val, "email:") === false)
										if(strpos($val, "https://") === false)
										    if(strpos($val, "./") === false)	// local relative url
											$val = "http://".$val;
								}
								$this->ATTRIBUTES[$key][1] = $val;
								break;
							
							case TW_LINK:
								//TODO: add link separator check here.
								//      Do not use this attribute in config!
								break;
							
							case TW_NUM:
								if( $val >= $cmda[2] && $val <= $cmda[3] ) break;
								$this->ATTRIBUTES[$key][1] = $cmda[1];
								break;
							
							case TW_CASE:
								if( !in_array($val, $cmda[2]) ) $this->ATTRIBUTES[$key][1] = $cmda[1];
								break;
						}
					}
				}
			}
			// check required attributes
			if($this->config_req_attr[$tag])
			{
				if( $this->is_attributes )
					foreach( $this->config_req_attr[$tag] as $required )
					{
						$req_found = 0;
						foreach( $this->ATTRIBUTES as $val)
							if( $val[0] == $required )
							{
								$req_found = 1;
								break;
							}
						if($req_found) continue;
						
						switch(	$this->config[$tag][$required][0] & 7 )
						{
							case TW_LINK:
							case TW_URL:
								// error
								break;
							
							default:
								$this->ATTRIBUTES[$required] = array($required, $this->config[$tag][$required][1]);
								break;
						}
					}
				else
				{
					foreach( $this->config_req_attr[$tag] as $required )
					{
						switch(	$this->config[$tag][$required][0] & 7 )
						{
							case TW_LINK:
							case TW_URL:
								
								// required tag error
								
								break;
							
							default:
								$this->ATTRIBUTES[$required] = array($required, $this->config[$tag][$required][1]);
								break;
						}						
					}
				}
					
			}			
			// cross tag removal algorithm
			
			$flag = $this->tags[$tag];
			$top = $this->stack_top();
			
			if( $flag[2] != null )		// check if tag before is specified
			{
				// yes, tag before is specified, check relationship
				if(! in_array($top, $flag[2]) )
				{
					if( $flag[0] & TW_OPT ) 
					{
						if( $top == $tag )
						{
							// End Tag is optional and current tag is the same as last tag (on stack).
							// Close previos for XHTML compatibility and open new the same tag.
							// Return, because no manipulation with stack is required.
							$this->out .= $this->output->close( $tag );
							$this->out .= $this->output->pair( $tag, $this->ATTRIBUTES );
							return;
						}
						if( $this->stack_search($tag) )
						{
							// repair stack
							while(($top = $this->stack_pop() ) != $tag )
							{
								/*if( !($this->tags[$top][0] & TW_OPT ) )
								{
									// auto close warning
								}*/
								$this->out .= $this->output->close( $top );
							}
							$this->stack_push($tag);
							$this->out .= $this->output->close( $tag );
							$this->out .= $this->output->pair( $tag, $this->ATTRIBUTES );
							return;
						}
					}
					// <th>...<td>  =>> <th>...</th><td>...	(<th> & <td> they have common parent tag (<tr>) )
					if( $this->tags[$top][0] & TW_OPT )
					{
						if( $this->tags[$top][2] == $flag[2] )
						{
							$this->out .= $this->output->close( $this->stack_pop() );
							$this->out .= $this->output->pair( $tag, $this->ATTRIBUTES );
							$this->stack_push( $tag );
							return;
						}
					}
					// invalid relation between tags #######
					return;
				}
				// valid relationship
			}
			if( $flag[0] & TW_NOP )
			{
				//tag without End Tag <br />
				$this->out .= $this->output->single( $tag, $this->ATTRIBUTES );
			}
			else
			{
				//Tag with End Tag, push to stack
				if( ($top == $tag) && ($flag[0] & TW_OPT) )
				{
					// '<p><p>' => '<p></p><p>'
					$this->out .= $this->output->close( $tag );
					$this->out .= $this->output->pair( $tag, $this->ATTRIBUTES );
					return;
				}
				$this->out .= $this->output->pair( $tag, $this->ATTRIBUTES );
				$this->stack_push( $tag );
			}
			return;
		}
		else
		{
			//disabled tag (or not supported) ######
			return;
		}
 	}
 	
/********************************************************************************************
 *	E N D _ T A G _ f i l t e r				</tag>	
 *
 */
 	function END_TAG_filter()
 	{
		$tag = $this->TAG;
		if( $tag == null ) 
		{
			// </>
			if ($tag = $this->stack_pop() )
			{
				$this->out .= $this->output->close($tag);
				return;
			}
			else
			{
				// kunda underflow :)
				return;
			}
		}
		if(in_array($tag,$this->config_tags))
		{
			// enabled tag
			if(is_string($this->config_attr[$tag]))
			{
				$this->TAG = $tag = $this->config_attr[$tag];
				//tag substitution warning
			}

			$top = $this->stack_top( $tag );
			if( $top )
			{
				$this->out .= $this->output->close( $tag );
				$this->stack_pop();
				return;
			}
			
			if( $this->stack_search($tag) )
			{
				// closing cross tags
				while( ($top = $this->stack_pop()) != $tag )
				{
					/*if( !($this->tags[$top][0] & TW_OPT ) )
					{
						// auto close warning
					}*/
					$this->out .= $this->output->close( $top );
				}
				$this->out .= $this->output->close( $tag );
			}
			else
			{
				// ###### cross tag error
				return;
			}
		}
		// drop out warning 		
 	}


/*
 * THERE ARE STATE IN, OUT & NEW FUNCTIONS
 *
 */
 
/********************************************************************************************
 * STATE T_begin 	'<'
 */
	function T_begin_in()
	{
		$this->tag_position = $this->pti;
		$this->TAG = null;
	}

	// --- MAIN TAG-FILTER FUNCTION ---

	function T_begin_out($word)
	{
		$this->START_TAG_filter();
	}

/********************************************************************************************
 * STATE TC_begin 	'</'
 */
	function T_Cbegin_in()
	{
		$this->tag_position = $this->pti;
		$this->TAG = null;
	}

	// TAG CLOSE function
	//
	function T_Cbegin_out($word)
	{
		$this->END_TAG_filter();
	}

/********************************************************************************************
 * STATE T_gettag 	'tagname'
 */
	function T_gettag_in() 
	{
		$this->is_attributes = 0;
		$this->ATTRIBUTES = null;
		return;
	}

	function T_gettag_new($word)	{	$this->TAG = strtolower($word);	}	
	function T_gettag_out($word)	{ return; }

/********************************************************************************************
 * STATE A_begin	'__atr...'
 */
	function A_begin_in()	{		$this->ATTR = null;	}
	function A_begin_new($word)	{	$this->ATTR[0] = strtolower($word);	}
	function A_begin_out($word)
	{
		if( $this->is_attributes )
		{
			foreach( $this->ATTRIBUTES as $akey => $aval )
			{
			    if($this->ATTR[0] == $aval[0])
			    {
				// duplicate warning
				$this->ATTRIBUTES[$akey] = $aval;
				return;
			    }
			}
		}
		else
		{
			$this->is_attributes = 1;
		}
		$this->ATTRIBUTES[] = $this->ATTR;
	}

/********************************************************************************************
 * STATE V_begin	'atr...'
 */
	function V_begin_in()	{		$this->VALUE = null;	}
	function V_begin_out($word)	{	$this->ATTR[1] = $this->VALUE;	}

/********************************************************************************************
 * STATE VALUE1
 */
 	function VALUE1_in() { return; }
 	function VALUE1_out($word) { $this->VALUE = substr($word,0,strlen($word)-1); }

/********************************************************************************************
 * STATE VALUE2
 */
 	function VALUE2_in() { return; }
 	function VALUE2_out($word) 
 	{
 		$this->VALUE = str_replace("\"", "&#34", substr($word, 0, strlen($word)-1) );
	}
/********************************************************************************************
 * STATE VALUE3
 */
 	function VALUE3_in() { return; }
 	function VALUE3_out($word) { $this->VALUE = $word; }
 	
 	
} // END class TW_base
?>