ultimix
supported_data_types.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 
22  define( 'SECURE_OBJECT' , 0 );
23  define( 'SECURE_ARRAY' , 1 );
24 
25  define( 'POST' , 1 );
26  define( 'GET' , 2 );
27  define( 'COOKIE' , 4 );
28  define( 'SESSION' , 8 );
29  define( 'SERVER' , 16 );
30  define( 'PREFIX_NAME' , 32 );
31  define( 'KEYS' , 64 );
32  define( 'CHECKBOX_IDS' , POST | PREFIX_NAME | KEYS );
33 
45 
56  var $String = false;
57 
68  var $SupportedData = array();
69 
80  function __construct()
81  {
82  try
83  {
84  $this->String = get_package( 'string' , 'last' , __FILE__ );
85 
86  $this->init_supported_data_types();
87  }
88  catch( Exception $e )
89  {
90  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
91  }
92  }
93 
108  private function init_supported_data_types()
109  {
110  try
111  {
112  $this->SupportedData[ 'integer' ] = $this;
113  $this->SupportedData[ 'digits' ] = $this;
114  $this->SupportedData[ 'integer_list' ] = $this;
115  $this->SupportedData[ 'float' ] = $this;
116  $this->SupportedData[ 'command' ] = $this;
117  $this->SupportedData[ 'string' ] = $this;
118  $this->SupportedData[ 'script' ] = $this;
119  $this->SupportedData[ 'unsafe_string' ] = $this;
120  $this->SupportedData[ 'email' ] = $this;
121  $this->SupportedData[ 'raw' ] = $this;
122  }
123  catch( Exception $e )
124  {
125  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
126  }
127  }
128 
152  function show_safe_macro( $Str )
153  {
154  try
155  {
156  $Str = preg_replace( "/\[lang\:([\{\}\[\]]{0}.*)\]/U" , "{lang:\\1}" , $Str );
157 
158  return( $Str );
159  }
160  catch( Exception $e )
161  {
162  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
163  }
164  }
165 
189  function hide_safe_macro( $Str )
190  {
191  try
192  {
193  $Str = preg_replace( "/\{lang\:([\{\}\[\]]{0}.*)\}/U" , "[lang:\\1]" , $Str );
194 
195  return( $Str );
196  }
197  catch( Exception $e )
198  {
199  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
200  }
201  }
202 
225  function get_type( $Predicates )
226  {
227  try
228  {
229  $Types = array( 'integer' , 'float' , 'command' , 'string' , 'email' , 'email' , 'set' , 'raw' );
230 
231  foreach( $Predicates as $p )
232  {
233  $Key = array_search( $p , $Types );
234  if( $Key !== false )
235  {
236  return( $Types[ $Key ] );
237  }
238  }
239 
240  throw( new Exception( "Type was not found" ) );
241  }
242  catch( Exception $e )
243  {
244  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
245  }
246  }
247 
270  function compile_integer( $Data )
271  {
272  try
273  {
274  return( intval( $Data ) );
275  }
276  catch( Exception $e )
277  {
278  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
279  }
280  }
281 
308  function compile_digits( $Data )
309  {
310  try
311  {
312  $RetData = '';
313  $s = strlen( $Data = "$Data" );
314 
315  for( $i = 0 ; $i < $s ; $i++ )
316  {
317  if( ( $Data[ $i ] >= '0' && $Data[ $i ] <= '9' ) || $Data[ 0 ] == '-' )
318  {
319  $RetData .= $Data[ $i ];
320  }
321  }
322 
323  return( $RetData );
324  }
325  catch( Exception $e )
326  {
327  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
328  }
329  }
330 
353  function compile_integer_list( $Data )
354  {
355  try
356  {
357  if( is_array( $Data ) )
358  {
359  $Data = implode( ',' , $Data );
360  }
361 
362  $Matches = array();
363  preg_match( '/^[0-9\,]+$/' , $Data , $Matches );
364 
365  if( count( $Matches ) > 0 )
366  {
367  return( $Data );
368  }
369  else
370  {
371  throw( new Exception( "Illegal symbols were found '$Data'" ) );
372  }
373  }
374  catch( Exception $e )
375  {
376  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
377  }
378  }
379 
402  function compile_float( $Data )
403  {
404  try
405  {
406  return( floatval( $Data ) );
407  }
408  catch( Exception $e )
409  {
410  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
411  }
412  }
413 
436  function compile_command( $Data )
437  {
438  try
439  {
440  $RetData = '';
441  $s = strlen( $Data );
442  for( $i = 0 ; $i < $s ; $i++ )
443  {
444  if( ( $Data[ $i ] >= 'a' && $Data[ $i ] <= 'z' ) ||
445  ( $Data[ $i ] >= 'A' && $Data[ $i ] <= 'Z' ) ||
446  ( $Data[ $i ] >= '0' && $Data[ $i ] <= '9' ) ||
447  $Data[ $i ] == '_' || $Data[ $i ] == ':' || $Data[ $i ] == '-' || $Data[ $i ] == '.' )
448  {
449  $RetData .= $Data[ $i ];
450  }
451  }
452 
453  return( $RetData );
454  }
455  catch( Exception $e )
456  {
457  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
458  }
459  }
460 
483  function compile_string( $Data )
484  {
485  try
486  {
487  if( $this->String === false )
488  {
489  $this->String = get_package( 'string' , 'last' , __FILE__ );
490  }
491 
492  $Data = $this->hide_safe_macro( $Data );
493  $Data = htmlspecialchars( $Data , ENT_QUOTES , 'UTF-8' );
494 
495  $PlaceHolders = array( '{' , ';' , '=' , '#' , '}' , "\r" , "\n" );
496  $Replacements = array( '[lfb]' , '[dot_comma]' , '[eq]' , '[sharp]' , '[rfb]' , '[r]' , '[n]' );
497  $Data = str_replace( $PlaceHolders, $Replacements , $Data );
498 
499  $Data = $this->show_safe_macro( $Data );
500  $Data = str_replace( '&' , '[amp]' , $Data );
501 
502  return( $Data );
503  }
504  catch( Exception $e )
505  {
506  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
507  }
508  }
509 
532  function compile_script( $Data )
533  {
534  try
535  {
536  if( $this->String === false )
537  {
538  $this->String = get_package( 'string' , 'last' , __FILE__ );
539  }
540 
541  $Data = htmlspecialchars( $Data , ENT_QUOTES , 'UTF-8' );
542  $Data = str_replace( '{' , '[lfb]' , $Data );
543  $Data = str_replace( ';' , '[dot_comma]' , $Data );
544  $Data = str_replace( '=' , '[eq]' , $Data );
545  $Data = str_replace( '#' , '[sharp]' , $Data );
546  $Data = str_replace( '}' , '[rfb]' , $Data );
547  $Data = str_replace( "\r" , '[r]' , $Data );
548  $Data = str_replace( "\n" , '[n]' , $Data );
549 
550  $Data = str_replace( '&' , '[amp]' , $Data );
551 
552  return( $Data );
553  }
554  catch( Exception $e )
555  {
556  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
557  }
558  }
559 
582  function compile_unsafe_string( $Data )
583  {
584  try
585  {
586  if( $this->String === false )
587  {
588  $this->String = get_package( 'string' , 'last' , __FILE__ );
589  }
590 
591  $Data = str_replace( '[lfb]' , '{' , $Data );
592  $Data = str_replace( '[dot_comma]' , ';' , $Data );
593  $Data = str_replace( '[eq]' , '=' , $Data );
594  $Data = str_replace( '[sharp]' , '#' , $Data );
595  $Data = str_replace( '[rfb]' , '}' , $Data );
596  $Data = str_replace( '[amp]' , '&' , $Data );
597 
598  $Data = htmlspecialchars_decode( $Data , ENT_QUOTES );
599 
600  return( $Data );
601  }
602  catch( Exception $e )
603  {
604  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
605  }
606  }
607 
630  function compile_email( $Data )
631  {
632  try
633  {
634  $RetData = '';
635  $c = strlen( $Data );
636  for( $i = 0 ; $i < $c ; $i++ )
637  {
638  if( ( $Data[ $i ] >= 'a' && $Data[ $i ] <= 'z' ) ||
639  ( $Data[ $i ] >= 'A' && $Data[ $i ] <= 'Z' ) ||
640  ( $Data[ $i ] >= '0' && $Data[ $i ] <= '9' ) ||
641  $Data[ $i ] == '_' || $Data[ $i ] == ':' ||
642  $Data[ $i ] == '-' || $Data[ $i ] == '.' ||
643  $Data[ $i ] == '@' || $Data[ $i ] == '=' ||
644  $Data[ $i ] == '+' )
645  {
646  $RetData .= $Data[ $i ];
647  }
648  }
649 
650  return( $RetData );
651  }
652  catch( Exception $e )
653  {
654  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
655  }
656  }
657 
684  function compile_data( &$Data , $Type )
685  {
686  try
687  {
688  if( isset( $this->SupportedData[ $Type ] ) === false )
689  {
690  throw( new Exception( "Undefined data type \"$Type\"" ) );
691  }
692  if( $Type == 'raw' )
693  {
694  return( $Data );
695  }
696  return( call_user_func( array( $this->SupportedData[ $Type ] , "compile_$Type" ) , $Data ) );
697  }
698  catch( Exception $e )
699  {
700  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
701  }
702  }
703 
736  function dispatch_complex_data( &$Data , $Type )
737  {
738  try
739  {
740  $Ret = array();
741  foreach( $Data as $key => $d )
742  {
743  if( is_array( $d ) || is_object( $d ) )
744  {
745  $d = $this->compile_data( $d , $Type );
746  if( is_object( $d ) || count( $d ) )
747  {
748  $Ret[ $key ] = $d;
749  }
750  }
751  else
752  {
753  if( $Type == 'string' || $Type == 'command' || $Type == 'raw' || strlen( $d ) != 0 )
754  {
755  $Ret[ $key ] = $this->compile_data( $d , $Type );
756  }
757  }
758  }
759  return( $Ret );
760  }
761  catch( Exception $e )
762  {
763  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
764  }
765  }
766  }
767 
768 ?>