ultimix
security_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 
27 
38  var $Security = false;
39  var $SupportedDataTypes = false;
40 
51  function __construct()
52  {
53  try
54  {
55  $this->SupportedDataTypes = get_package( 'security::supported_data_types' , 'last' , __FILE__ );
56  }
57  catch( Exception $e )
58  {
59  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
60  }
61  }
62 
89  private function get_alias( $Name , $Predicates )
90  {
91  try
92  {
93  foreach( $Predicates as $p )
94  {
95  if( strpos( $p , 'alias_' ) === 0 )
96  {
97  $Alias = str_replace( 'alias_' , '' , $p );
98  return( $Alias );
99  }
100  }
101 
102  return( $Name );
103  }
104  catch( Exception $e )
105  {
106  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
107  }
108  }
109 
136  private function get_default( $Name , $Predicates )
137  {
138  try
139  {
140  $Default = false;
141 
142  foreach( $Predicates as $p )
143  {
144  if( strpos( $p , 'default_' ) === 0 )
145  {
146  return( str_replace( 'default_' , '' , $p ) );
147  }
148  }
149 
150  return( $Default );
151  }
152  catch( Exception $e )
153  {
154  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
155  }
156  }
157 
180  function allow_not_set( $Predicates )
181  {
182  try
183  {
184  foreach( $Predicates as $p )
185  {
186  if( $p === 'allow_not_set' )
187  {
188  return( true );
189  }
190  }
191 
192  return( false );
193  }
194  catch( Exception $e )
195  {
196  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
197  }
198  }
199 
230  private function handle_allow_not_set( $ScriptLine , $GlobalPredicates , $Result )
231  {
232  try
233  {
234  $Name = $ScriptLine[ 0 ];
235  $Predicates = array_unique( array_merge( explode( ',' , $ScriptLine[ 1 ] ) , $GlobalPredicates ) );
236 
237  if( $this->allow_not_set( $Predicates ) )
238  {
239  $Default = $this->get_default( $Name , $Predicates );
240 
241  if( $Default !== false )
242  {
243  set_field( $Result , $Name , $Default );
244  }
245  return( $Result );
246  }
247  else
248  {
249  $ScriptLine = serialize( $ScriptLine );
250  throw( new Exception( "Field \"$Name\" must be set, but it has not been set in $ScriptLine" ) );
251  }
252  }
253  catch( Exception $e )
254  {
255  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
256  }
257  }
258 
293  private function parse_value( $Params , $ScriptLine , $Result , $GlobalPredicates )
294  {
295  try
296  {
297  $Name = $ScriptLine[ 0 ];
298  $Predicates = array_unique( array_merge( explode( ',' , $ScriptLine[ 1 ] ) , $GlobalPredicates ) );
299 
300  $Value = get_field( $Params , $Name );
301  $Type = $this->SupportedDataTypes->get_type( $Predicates );
302  $Alias = $this->get_alias( $Name , $Predicates );
303 
304  set_field( $Result , $Name , $this->Security->get( $Value , $Type ) );
305  set_field( $Result , $Alias , get_field( $Result , $Name ) );
306 
307  return( $Result );
308  }
309  catch( Exception $e )
310  {
311  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
312  }
313  }
314 
349  private function parse_script_line( $Params , $ScriptLine , $Result , $GlobalPredicates )
350  {
351  try
352  {
353  $ScriptLine = explode( ':' , $ScriptLine );
354  $Name = $ScriptLine[ 0 ];
355 
356  $ValueWasSet = is_field_set( $Params , $Name );
357  if( $ValueWasSet === false )
358  {
359  $Result = $this->handle_allow_not_set( $ScriptLine , $GlobalPredicates , $Result );
360  }
361  else
362  {
363  $Result = $this->parse_value( $Params , $ScriptLine , $Result , $GlobalPredicates );
364  }
365 
366  return( $Result );
367  }
368  catch( Exception $e )
369  {
370  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
371  }
372  }
373 
404  function parse_parameters( $Params , $ParsingScript , $GlobalPredicates = '' )
405  {
406  try
407  {
408  if( $this->Security === false )
409  {
410  $this->Security = get_package( 'security' , 'last' , __FILE__ );
411  }
412 
413  $GlobalPredicates = explode( ',' , $GlobalPredicates );
414 
415  $Result = new stdClass();
416 
417  $ParsingScript = str_replace( '#' , ';' , $ParsingScript );
418  $Script = explode( ';' , $ParsingScript );
419 
420  foreach( $Script as $ScriptLine )
421  {
422  $Result = $this->parse_script_line( $Params , $ScriptLine , $Result , $GlobalPredicates );
423  }
424 
425  return( $Result );
426  }
427  catch( Exception $e )
428  {
429  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
430  }
431  }
432 
457  function parse_http_parameters( $ParsingScript )
458  {
459  try
460  {
461  return( $this->parse_parameters( array_merge( $_GET , $_POST ) , $ParsingScript ) );
462  }
463  catch( Exception $e )
464  {
465  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
466  }
467  }
468  }
469 
470 ?>