ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

page_parser.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
16 //TODO: maybe it will be better to remove this package
17
28 class page_parser_1_0_0
29 {
30
41 var $CachedMultyFS = false;
42 var $String = false;
43
54 var $StartPosition = false;
55
66 var $EndPosition = false;
67
78 var $RawMacroParameters = false;
79
90 var $MacroParameters = false;
91
102 function __construct()
103 {
104 try
105 {
106 $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
107 $this->String = get_package( 'string' , 'last' , __FILE__ );
108 }
109 catch( Exception $e )
110 {
111 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
112 }
113 }
114
141 function validate_parameters( $Parameters , $Conditions )
142 {
143 try
144 {
145 foreach( $Parameters as $key1 => $p )
146 {
147 $p = explode( '=' , $p );
148 foreach( $Conditions as $key2 => $rev )
149 {
150 if( $key2 == $p[ 0 ] )
151 {
152 $Matches = array();
153 $Result = preg_match( $rev , $p[ 1 ] , $Matches );
154 if( count( $Matches ) == 0 )
155 {
156 return( false );
157 }
158 }
159 }
160 }
161
162 return( true );
163 }
164 catch( Exception $e )
165 {
166 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
167 }
168 }
169
196 function parse_parameters( $Parameters , $Conditions )
197 {
198 try
199 {
200 $Parameters = explode( ';' , $Parameters );
201
202 if( $this->validate_parameters( $Parameters , $Conditions ) )
203 {
204 $this->RawMacroParameters = $Parameters;
205 return( true );
206 }
207
208 return( false );
209 }
210 catch( Exception $e )
211 {
212 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
213 }
214 }
215
250 function other_macro_was_found( $TmpStartPos , $TmpEndPos , $Counter , $ParserCursor )
251 {
252 try
253 {
254 if( $TmpStartPos !== false && $TmpEndPos !== false )
255 {
256 if( $TmpStartPos < $TmpEndPos )
257 {
258 $ParserCursor = $TmpEndPos;
259 }
260 if( $TmpEndPos < $TmpStartPos )
261 {
262 $Counter--;
263 if( $Counter )$Counter++;
264 $ParserCursor = $TmpStartPos;
265 }
266 }
267
268 return( array( $Counter , $ParserCursor ) );
269 }
270 catch( Exception $e )
271 {
272 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
273 }
274 }
275
310 function handle_extra_cases( $TmpStartPos , $TmpEndPos , $Counter , $ParserCursor )
311 {
312 try
313 {
314 if( $TmpStartPos !== false && $TmpEndPos === false )
315 {
316 $Counter++;
317 $ParserCursor = $TmpStartPos;
318 }
319
320 if( $TmpStartPos === false && $TmpEndPos !== false )
321 {
322 $Counter--;
323 $ParserCursor = $TmpEndPos;
324 }
325
326 if( $TmpStartPos === false && $TmpEndPos === false )
327 {
328 throw( new Exception( 'Closing '.chr( 123 ).' was not found' ) );
329 }
330
331 return( array( $Counter , $ParserCursor ) );
332 }
333 catch( Exception $e )
334 {
335 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
336 }
337 }
338
369 function find_parameters_end( $StringData , $MacroName , $ParserCursor )
370 {
371 try
372 {
373 $Counter = 1;
374
375 do
376 {
377 $TmpStartPos = strpos( $StringData , chr( 123 ) , $ParserCursor + 1 );
378 $TmpEndPos = strpos( $StringData , chr( 125 ) , $ParserCursor + 1 );
379
380 list( $Counter , $ParserCursor ) = $this->other_macro_was_found(
381 $TmpStartPos , $TmpEndPos , $Counter , $ParserCursor
382 );
383
384 list( $Counter , $ParserCursor ) = $this->handle_extra_cases(
385 $TmpStartPos , $TmpEndPos , $Counter , $ParserCursor
386 );
387 }
388 while( $TmpStartPos && $Counter != 0 );
389
390 return( $Counter == 0 ? $TmpEndPos : false );
391 }
392 catch( Exception $e )
393 {
394 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
395 }
396 }
397
432 function parse_macro( $StringData , $MacroName , $Conditions , $StartPos )
433 {
434 try
435 {
436 $EndPos = $this->find_parameters_end( $StringData , $MacroName , $StartPos );
437
438 if( $EndPos !== false )
439 {
440 /* extracting parameters */
441 $Parameters = substr( $StringData , $StartPos , $EndPos - $StartPos );
442
443 return( $this->parse_parameters( $Parameters , $Conditions ) );
444 }
445
446 return( false );
447 }
448 catch( Exception $e )
449 {
450 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
451 }
452 }
453
484 function find_next_macro( $StringData , $MacroName , $Conditions )
485 {
486 try
487 {
488 $this->RawMacroParameters = array();
489 $this->MacroParameters = false;
490 $StartPos = -1;
491
492 $MacroStart = chr( 123 ).$MacroName.':';
493 $MacroLength = strlen( $MacroStart );
494
495 for( ; ( $StartPos = strpos( $StringData , $MacroStart , $StartPos + 1 ) ) !== false ; )
496 {
497 $StartPos += $MacroLength;
498
499 $Result = $this->parse_macro( $StringData , $MacroName , $Conditions , $StartPos );
500
501 if( $Result )
502 {
503 $this->StartPosition = $StartPos - $MacroLength;
504 return( true );
505 }
506 }
507
508 return( false );
509 }
510 catch( Exception $e )
511 {
512 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
513 }
514 }
515
534 function get_macro_parameters()
535 {
536 try
537 {
538 if( $this->MacroParameters === false )
539 {
540 $this->MacroParameters = get_package_object( 'settings::settings' , 'last' , __FILE__ );
541 $this->MacroParameters->load_settings( $this->RawMacroParameters );
542 }
543
544 return( $this->MacroParameters );
545 }
546 catch( Exception $e )
547 {
548 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
549 }
550 }
551
578 function get_positions( $StringData , $MacroName )
579 {
580 try
581 {
582 $Positions = array();
583
584 $StartPos = $this->StartPosition;
585 $EndPos = -1;
586
587 for( ; $StartPos = strpos( $StringData , chr( 123 ).$MacroName.':' , $StartPos + 1 ) ; )
588 {
589 $Positions [ $StartPos ] = 's';
590 }
591
592 for( ; $EndPos = strpos( $StringData , chr( 123 ).'~'.$MacroName.chr( 125 ) , $EndPos + 1 ) ; )
593 {
594 $Positions [ $EndPos ] = 'e';
595 }
596
597 ksort( $Positions );
598
599 return( $Positions );
600 }
601 catch( Exception $e )
602 {
603 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
604 }
605 }
606
645 function get_next_position( $Value , $Key , $StartPos , $EndPos , $c )
646 {
647 try
648 {
649 if( $StartPos === false && $Value === 's' )
650 {
651 $c++;
652 $StartPos = $Key;
653 }
654 elseif( $EndPos === false && $Value === 'e' && $c === 1 )
655 {
656 $EndPos = $Key;
657 }
658 elseif( $Value === 's' )
659 {
660 $c++;
661 }
662 elseif( $Value === 'e' )
663 {
664 $c--;
665 }
666
667 return( array( $StartPos , $EndPos , $c ) );
668 }
669 catch( Exception $e )
670 {
671 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
672 }
673 }
674
697 function get_macro_end_position( $StringData , $MacroName )
698 {
699 try
700 {
701 $Positions = $this->get_positions( $StringData , $MacroName );
702 $StartPos = $this->StartPosition;
703 $EndPos = false;
704 $c = 1;
705
706 foreach( $Positions as $Key => $Value )
707 {
708 if( $Value == 'e' && $Key > $this->StartPosition )
709 {
710 list( $StartPos , $EndPos , $c ) = $this->get_next_position(
711 $Value , $Key , $StartPos , $EndPos , $c
712 );
713 }
714 }
715
716 if( $EndPos === false )
717 {
718 throw_exception( 'Block end was not found' );
719 }
720
721 $this->EndPosition = $EndPos;
722 }
723 catch( Exception $e )
724 {
725 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
726 }
727 }
728
755 function hide_macro( $StringData , $MacroName )
756 {
757 try
758 {
759 $this->get_macro_end_position( $StringData , $MacroName );
760
761 $StringData = substr_replace( $StringData ,
762 '' ,
763 $this->StartPosition ,
764 $this->EndPosition - $this->StartPosition + strlen( chr( 123 ).'~'.$MacroName.chr( 125 ) )
765 );
766
767 return( $StringData );
768 }
769 catch( Exception $e )
770 {
771 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
772 }
773 }
774
801 function show_macro( $StringData , $MacroName )
802 {
803 try
804 {
805 $this->get_macro_end_position( $StringData , $MacroName );
806
807 $this->RawMacroParameters = implode( ';' , $this->RawMacroParameters );
808 $MacroLength = strlen( chr( 123 )."$MacroName:$this->RawMacroParameters".chr( 125 ) );
809
810 $Position = $this->EndPosition - $this->StartPosition - $MacroLength;
811 $MacroData = substr( $StringData , $this->StartPosition + $MacroLength , $Position );
812
813 $Position = $this->EndPosition - $this->StartPosition + strlen( chr( 123 ).'~'.$MacroName.chr( 125 ) );
814 $StringData = substr_replace( $StringData , $MacroData , $this->StartPosition , $Position );
815
816 return( $StringData );
817 }
818 catch( Exception $e )
819 {
820 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
821 }
822 }
823 }
824
825 ?>