ultimix
predicates.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 
27 
38  var $Settings = false;
39  var $String = false;
40  var $SupportedDataTypes = false;
41 
52  var $ErrorMessage = false;
53 
64  function __construct()
65  {
66  try
67  {
68  $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
69  $this->String = get_package( 'string' , 'last' , __FILE__ );
70  $this->SupportedDataTypes = get_package( 'security::supported_data_types' , 'last' , __FILE__ );
71  }
72  catch( Exception $e )
73  {
74  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
75  }
76  }
77 
100  private function dispatch_error_message( $Predicates )
101  {
102  try
103  {
104  foreach( $Predicates as $p )
105  {
106  if( strpos( $p , 'err_msg_' ) === 0 )
107  {
108  return( str_replace( 'err_msg_' , '' , $p ) );
109  }
110  }
111  return( false );
112  }
113  catch( Exception $e )
114  {
115  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
116  }
117  }
118 
149  function validate_email( $Data , $Name , $Predicates )
150  {
151  try
152  {
153  $Value = get_field( $Data , $Name , '' );
154 
155  if( $Value == '' && $this->SecurityParser->allow_not_set( $Predicates ) )
156  {
157  return( true );
158  }
159 
160  if( $Value == '' || $this->SupportedDataTypes->compile_data( $Value , 'email' ) != $Value )
161  {
162  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
163  return( false );
164  }
165 
166  return( true );
167  }
168  catch( Exception $e )
169  {
170  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
171  }
172  }
173 
204  function validate_float( $Data , $Name , $Predicates )
205  {
206  try
207  {
208  $Value = get_field( $Data , $Name , '' );
209 
210  if( $Value === '' && $this->SecurityParser->allow_not_set( $Predicates ) )
211  {
212  return( true );
213  }
214 
215  if( $Value === '' || $this->SupportedDataTypes->compile_data( $Value , 'float' ) != $Value )
216  {
217  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
218  return( false );
219  }
220 
221  return( true );
222  }
223  catch( Exception $e )
224  {
225  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
226  }
227  }
228 
259  function validate_integer( $Data , $Name , $Predicates )
260  {
261  try
262  {
263  $Value = get_field( $Data , $Name , '' );
264 
265  if( $Value === '' && $this->SecurityParser->allow_not_set( $Predicates ) )
266  {
267  return( true );
268  }
269 
270  if( $Value === '' || $this->SupportedDataTypes->compile_data( $Value , 'integer' ) != $Value )
271  {
272  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
273  return( false );
274  }
275 
276  return( true );
277  }
278  catch( Exception $e )
279  {
280  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
281  }
282  }
283 
314  function validate_string( $Data , $Name , $Predicates )
315  {
316  try
317  {
318  $Value = get_field( $Data , $Name , '' );
319 
320  if( $Value == '' && $this->SecurityParser->allow_not_set( $Predicates ) === false )
321  {
322  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
323  return( false );
324  }
325 
326  return( true );
327  }
328  catch( Exception $e )
329  {
330  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
331  }
332  }
333 
364  function validate_command( $Data , $Name , $Predicates )
365  {
366  try
367  {
368  $Value = get_field( $Data , $Name , '' );
369 
370  if( $Value == '' && $this->SecurityParser->allow_not_set( $Predicates ) )
371  {
372  return( true );
373  }
374 
375  if( $this->SupportedDataTypes->compile_data( $Value , 'command' ) != $Value )
376  {
377  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
378  return( false );
379  }
380 
381  return( true );
382  }
383  catch( Exception $e )
384  {
385  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
386  }
387  }
388 
419  function validate_set( $Data , $Name , $Predicates )
420  {
421  try
422  {
423  if( isset( $Data[ $Name ] ) === false )
424  {
425  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
426  return( false );
427  }
428 
429  return( true );
430  }
431  catch( Exception $e )
432  {
433  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
434  }
435  }
436 
467  function validate_not_set( $Data , $Name , $Predicates )
468  {
469  try
470  {
471  if( isset( $Data[ $Name ] ) === true )
472  {
473  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
474  return( false );
475  }
476 
477  return( true );
478  }
479  catch( Exception $e )
480  {
481  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
482  }
483  }
484 
515  function validate_not_filled( $Data , $Name , $Predicates )
516  {
517  try
518  {
519  $Value = get_field( $Data , $Name , '' );
520  if( $Value != '' )
521  {
522  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
523  return( false );
524  }
525 
526  return( true );
527  }
528  catch( Exception $e )
529  {
530  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
531  }
532  }
533 
564  private function exec_set( $Data , $Name , $Predicates )
565  {
566  try
567  {
568  if( isset( $Data[ $Name ] ) === false )
569  {
570  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
571 
572  return( false );
573  }
574 
575  return( true );
576  }
577  catch( Exception $e )
578  {
579  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
580  }
581  }
582 
617  function exec_value( $Data , $Name , $Predicates , $j )
618  {
619  try
620  {
621  $Value = str_replace( 'value_' , '' , $Predicates[ $j ] );
622 
623  if( $this->SupportedDataTypes->compile_data( @$Data[ $Name ] , 'raw' ) != $Value )
624  {
625  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
626 
627  return( false );
628  }
629 
630  return( true );
631  }
632  catch( Exception $e )
633  {
634  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
635  }
636  }
637 
672  function exec_min( $Data , $Name , $Predicates , $j )
673  {
674  try
675  {
676  $Value = intval( str_replace( 'min_' , '' , $Predicates[ $j ] ) );
677 
678  if( $this->SupportedDataTypes->compile_data( $Data[ $Name ] , 'raw' ) < $Value )
679  {
680  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
681 
682  return( false );
683  }
684 
685  return( true );
686  }
687  catch( Exception $e )
688  {
689  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
690  }
691  }
692 
727  function exec_max( $Data , $Name , $Predicates , $j )
728  {
729  try
730  {
731  $Value = intval( str_replace( 'max_' , '' , $Predicates[ $j ] ) );
732 
733  if( $this->SupportedDataTypes->compile_data( $Data[ $Name ] , 'raw' ) > $Value )
734  {
735  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
736 
737  return( false );
738  }
739 
740  return( true );
741  }
742  catch( Exception $e )
743  {
744  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
745  }
746  }
747 
782  function exec_same_as( $Data , $Name , $Predicates , $j )
783  {
784  try
785  {
786  $SecondField = str_replace( 'same_as_' , '' , $Predicates[ $j ] );
787 
788  $SecondValue = $this->SupportedDataTypes->compile_data( $Data[ $SecondField ] , 'raw' );
789 
790  $First = $this->SupportedDataTypes->compile_data( $Data[ $Name ] , 'raw' );
791  $Second = $this->SupportedDataTypes->compile_data( $SecondValue , 'raw' );
792 
793  if( $First !== $Second )
794  {
795  $this->ErrorMessage = $this->dispatch_error_message( $Predicates );
796 
797  return( false );
798  }
799 
800  return( true );
801  }
802  catch( Exception $e )
803  {
804  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
805  }
806  }
807 
842  function exec_simple( $Data , $Name , $Predicates , $j )
843  {
844  try
845  {
846  if( $Predicates[ $j ] === 'set' )
847  {
848  return( $this->exec_set( $Data , $Name , $Predicates ) );
849  }
850  if( strpos( $Predicates[ $j ] , 'err_msg_' ) === 0 )
851  {
852  return( true );
853  }
854  if( strpos( $Predicates[ $j ] , 'default_' ) !== false )
855  {
856  return( true );
857  }
858 
859  throw( new Exception( "Undefined predicate '".$Predicates[ $j ]."'" ) );
860  }
861  catch( Exception $e )
862  {
863  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
864  }
865  }
866  }
867 
868 ?>