ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

string.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 DEFINE( 'TERMINAL_VALUE' , "/^[\{\}]{0}[^\{\}]*$/" );
17
28 class string_1_0_0{
29
40 var $MacroParser = false;
41 var $Settings = false;
42 var $Tags = false;
43
54 function __construct()
55 {
56 try
57 {
58 $this->MacroParser = get_package_object( 'string::macro_parser' , 'last' , __FILE__ );
59 $this->Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
60 $this->Tags = get_package( 'string::tags' , 'last' , __FILE__ );
61 }
62 catch( Exception $e )
63 {
64 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
65 }
66 }
67
106 function hide_block( $StringData , $BlockStart , $BlockEnd , &$Changed )
107 {
108 try
109 {
110 list( $StartPos , $EndPos ) = $this->get_block_positions( $StringData , $BlockStart , $BlockEnd );
111
112 if( $StartPos !== false )
113 {
114 $StringData = substr_replace( $StringData ,
115 '' ,
116 $StartPos ,
117 $EndPos - $StartPos + strlen( chr( 123 ).$BlockEnd.chr( 125 ) )
118 );
119
120 $Changed = true;
121 }
122
123 return( $StringData );
124 }
125 catch( Exception $e )
126 {
127 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
128 }
129 }
130
165 function show_block( $StringData , $BlockStart , $BlockEnd , &$Changed )
166 {
167 try
168 {
169 list( $StartPos , $EndPos ) = $this->get_block_positions( $StringData , $BlockStart , $BlockEnd );
170 if( $StartPos !== false )
171 {
172 $BlockData = $this->get_block_data( $StringData , $BlockStart , $BlockEnd );
173
174 $StringData = substr_replace( $StringData ,
175 $BlockData ,
176 $StartPos ,
177 $EndPos - $StartPos + strlen( chr( 123 ).$BlockEnd.chr( 125 ) )
178 );
179
180 $Changed = true;
181 }
182
183 return( $StringData );
184 }
185 catch( Exception $e )
186 {
187 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
188 }
189 }
190
221 private function apply_foreach_data( $Str , $Parameters , $Data )
222 {
223 try
224 {
225 $SubTemplate = $this->get_block_data( $Str , "foreach:$Parameters" , '~foreach' );
226
227 foreach( $Data as $k => $v )
228 {
229 $Str = str_replace(
230 "{foreach:$Parameters}" ,
231 $this->print_record( $SubTemplate , $v )."{foreach:$Parameters}" , $Str
232 );
233 }
234
235 return( $Str );
236 }
237 catch( Exception $e )
238 {
239 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
240 }
241 }
242
269 function compile_foreach( $Str , &$Record )
270 {
271 try
272 {
273 for( ; $Parameters = $this->get_macro_parameters( $Str , 'foreach' ) ; )
274 {
275 $this->Settings->load_settings( $Parameters );
276 $Var = $this->Settings->get_setting( 'var' );
277 $Data = get_field( $Record , $Var );
278
279 $Str = $this->apply_foreach_data( $Str , $Parameters , $Data );
280
281 $Str = $this->hide_block( $Str , "foreach:$Parameters" , '~foreach' , $Changed );
282 $Changed = false;
283 }
284
285 return( $Str );
286 }
287 catch( Exception $e )
288 {
289 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
290 }
291 }
292
319 function print_record( $Str , &$Record )
320 {
321 try
322 {
323 if( is_array( $Record ) === false && is_object( $Record ) === false )
324 {
325 throw( new Exception( 'Invalid record was passed' ) );
326 }
327
328 $Str = $this->compile_foreach( $Str , $Record );
329
330 foreach( $Record as $Field => $Value )
331 {
332 if( is_array( $Value ) || is_object( $Value ) )
333 {
334 $Str = $this->print_record( $Str , $Value );
335 }
336 else
337 {
338 $Str = str_replace( chr( 123 ).$Field.chr( 125 ) , $Value , $Str );
339 }
340 }
341
342 return( $Str );
343 }
344 catch( Exception $e )
345 {
346 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
347 }
348 }
349
380 function block_exists( $StringData , $BlockStart , $BlockEnd = false )
381 {
382 try
383 {
384 $StartPos = strpos( $StringData , chr( 123 ).$BlockStart.chr( 125 ) );
385 if( $StartPos == false )
386 {
387 return( false );
388 }
389
390 if( $BlockEnd !== false )
391 {
392 $EndPos = strpos( $StringData , chr( 123 ).$BlockEnd.chr( 125 ) );
393 if( $EndPos == false )
394 {
395 return( false );
396 }
397 }
398
399 return( true );
400 }
401 catch( Exception $e )
402 {
403 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
404 }
405 }
406
437 function get_macro_parameters( $StringData , $Name , $RegExValidators = array() )
438 {
439 try
440 {
441 $StartPos = -1;
442
443 for( ; ( $TmpStartPos = strpos( $StringData , chr( 123 ).$Name.':' , $StartPos + 1 ) ) !== false ; )
444 {
445 $Counter = 1;
446 $StartPos = $TmpEndPos = $TmpStartPos;
447
448 $MacroStartPos = $StartPos;
449 $ParamStartPos = $MacroStartPos + strlen( chr( 123 ).$Name.':' );
450
451 $Result = $this->MacroParser->find_macro( $StringData , $TmpStartPos , $TmpEndPos ,
452 $StartPos , $Counter , $MacroStartPos , $ParamStartPos , $RegExValidators );
453
454 if( $Result !== false )
455 {
456 return( $Result );
457 }
458 }
459
460 return( false );
461 }
462 catch( Exception $e )
463 {
464 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
465 }
466 }
467
498 function get_block_data( $StringData , $BlockStart , $BlockEnd )
499 {
500 try
501 {
502 list( $StartPos , $EndPos ) = $this->get_block_positions( $StringData , $BlockStart , $BlockEnd );
503
504 if( $StartPos !== false )
505 {
506 $BlockData = substr(
507 $StringData ,
508 $StartPos + strlen( chr( 123 ).$BlockStart.chr( 125 ) ) ,
509 $EndPos - $StartPos - strlen( chr( 123 ).$BlockStart.chr( 125 ) )
510 );
511
512 return( $BlockData );
513 }
514 else
515 {
516 throw( new Exception( 'An error occured while getting block data' ) );
517 }
518 }
519 catch( Exception $e )
520 {
521 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
522 }
523 }
524
559 function hide_unprocessed_blocks( $StringData , $BlockType , &$Changed )
560 {
561 try
562 {
563 $PlaceHolders = array();
564 do
565 {
566 $PlaceHolders = array();
567
568 preg_match( "/\{$BlockType:([a-zA-Z0-9_]+)\}/" , $StringData , $PlaceHolders );
569
570 if( count( $PlaceHolders ) > 1 )
571 {
572 $StringData = $this->hide_block( $StringData , "$BlockType:".$PlaceHolders[ 1 ] ,
573 "$BlockType:~".$PlaceHolders[ 1 ] , $Changed );
574 }
575 }
576 while( count( $PlaceHolders ) > 1 );
577
578 return( $StringData );
579 }
580 catch( Exception $e )
581 {
582 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
583 }
584 }
585
616 function get_all_block_positions( $StringData , $BlockStart , $BlockEnd )
617 {
618 try
619 {
620 $Positions = array();
621
622 $StartPos = -1;
623 $EndPos = -1;
624
625 for( ; $StartPos = strpos( $StringData , chr( 123 ).$BlockStart.chr( 125 ) , $StartPos + 1 ) ; )
626 {
627 $Positions [ $StartPos ] = 's';
628 }
629 for( ; $EndPos = strpos( $StringData , chr( 123 ).$BlockEnd.chr( 125 ) , $EndPos + 1 ) ; )
630 {
631 $Positions [ $EndPos ] = 'e';
632 }
633 ksort( $Positions );
634
635 return( $Positions );
636 }
637 catch( Exception $e )
638 {
639 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
640 }
641 }
642
665 function get_possible_block_positions( &$Positions )
666 {
667 try
668 {
669 $StartPos = $EndPos = false;
670 $c = 0;
671 foreach( $Positions as $Key => $Value )
672 {
673 if( $StartPos === false && $Value === 's' )
674 {
675 $c++;
676 $StartPos = $Key;
677 }
678 elseif( $EndPos === false && $Value === 'e' && $c === 1 )
679 {
680 $EndPos = $Key;
681 break;
682 }
683 elseif( $Value === 's' || $Value === 'e' && $c > 0 )
684 {
685 $c += $Value === 's' ? 1 : -1;
686 }
687 }
688
689 return( array( $StartPos , $EndPos ) );
690 }
691 catch( Exception $e )
692 {
693 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
694 }
695 }
696
727 function get_block_positions( $StringData , $BlockStart , $BlockEnd )
728 {
729 try
730 {
731 $Positions = $this->get_all_block_positions( $StringData , $BlockStart , $BlockEnd );
732
733 list( $StartPos , $EndPos ) = $this->get_possible_block_positions( $Positions );
734
735 if( $StartPos === false )
736 {
737 return( array( false , false ) );
738 }
739 if( $EndPos === false )
740 {
741 throw( new Exception( 'Block end was not found' ) );
742 }
743
744 return( array( $StartPos , $EndPos ) );
745 }
746 catch( Exception $e )
747 {
748 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
749 }
750 }
751
774 function clear_ultimix_tags( $Data )
775 {
776 try
777 {
778 if( strpos( $Data[ 'path' ] , '{http_host}/' ) === 0 )
779 {
780 $Data[ 'path' ] = str_replace( '{http_host}/' , './' , $Data[ 'path' ] );
781 }
782
783 $Data[ 'path' ] = $this->Tags->compile_ultimix_tags( $Data[ 'path' ] );
784
785 return( $Data );
786 }
787 catch( Exception $e )
788 {
789 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
790 }
791 }
792
815 function find_all_macro( &$Data )
816 {
817 try
818 {
819 $Matches = array();
820
821 preg_match_all( '/\{([a-zA-Z0-9_]+)/' , $Data , $Matches );
822
823 return( array_unique( $Matches[ 1 ] ) );
824 }
825 catch( Exception $e )
826 {
827 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
828 }
829 }
830 }
831 ?>