ultimix
context.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 $Config = false;
39 
50  var $CachedMultyFS = false;
51  var $ContextUtilities = false;
52  var $CustomValidations = false;
53  var $Messages = false;
54  var $PermitAlgorithms = false;
55  var $Security = false;
56  var $SecurityValidator = false;
57  var $Settings = false;
58  var $Trace = false;
59 
74  function __construct()
75  {
76  try
77  {
78  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
79  $this->ContextUtilities = get_package( 'gui::context::context_utilities' , 'last' , __FILE__ );
80  $this->CustomValidations = get_package( 'gui::context::custom_validations' , 'last' , __FILE__ );
81  $this->Messages = get_package( 'page::messages' , 'last' , __FILE__ );
82  $this->Security = get_package( 'security' , 'last' , __FILE__ );
83  $this->SecurityValidator = get_package( 'security::security_validator' , 'last' , __FILE__ );
84  $this->Trace = get_package( 'trace' , 'last' , __FILE__ );
85  }
86  catch( Exception $e )
87  {
88  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
89  }
90  }
91 
110  function load_config( $ConfigFilePath )
111  {
112  try
113  {
114  $Tmp = $this->CachedMultyFS->file_get_contents( $ConfigFilePath );
115  $this->load_raw_config( $Tmp );
116  }
117  catch( Exception $e )
118  {
119  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
120  }
121  }
122 
149  function load_hinted_config( &$Options , $ContextFolder , $Contexts )
150  {
151  try
152  {
153  $Hint = $Options->get_setting( 'context' , false );
154 
155  if( $Hint !== false && in_array( $Hint , $Contexts ) )
156  {
157  $this->load_config( "$ContextFolder/conf/$Hint" );
158  }
159  else
160  {
161  throw( new Exception( 'Config was not hinted' ) );
162  }
163  }
164  catch( Exception $e )
165  {
166  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
167  }
168  }
169 
189  {
190  try
191  {
192  if( $this->Settings === false )
193  {
194  $this->Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
195  }
196  $this->Settings->load_settings( $Config );
197  $this->Config = $this->Settings->get_raw_settings();
198  }
199  catch( Exception $e )
200  {
201  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
202  }
203  }
204 
224  {
225  try
226  {
227  $this->Config = $Config->get_raw_settings();
228  }
229  catch( Exception $e )
230  {
231  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
232  }
233  }
234 
257  function compile_call_params_filter( &$Options )
258  {
259  try
260  {
261  if( $this->ContextUtilities->section_exists( $this->Config , 'call_params_filter' ) )
262  {
263  $Filter = $this->ContextUtilities->get_section( $this->Config , 'call_params_filter' );
264  if( $this->SecurityValidator->validate_custom_fields( $Options->get_raw_settings() ,
265  $Filter ) === false )
266  {
267  $this->Trace->add_trace_string(
268  "{lang:call_params_filter_not_passed} : $Filter" , COMMON
269  );
270  return( false );
271  }
272  }
273 
274  return( true );
275  }
276  catch( Exception $e )
277  {
278  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
279  }
280  }
281 
304  function compile_permits_filter( &$Options )
305  {
306  try
307  {
308  $this->PermitAlgorithms = get_package( 'permit::permit_algorithms' , 'last' , __FILE__ );
309 
310  if( $this->ContextUtilities->section_exists( $this->Config , 'permits_filter' ) )
311  {
312  $Filter = $this->ContextUtilities->get_section( $this->Config , 'permits_filter' );
313 
314  if( $this->PermitAlgorithms->fetch_permits_for_object( false , 'user' , $Filter ) === false )
315  {
316  $this->Trace->add_trace_string( "{lang:permits_filter_not_passed} : $Filter" , COMMON );
317  return( false );
318  }
319  }
320 
321  return( true );
322  }
323  catch( Exception $e )
324  {
325  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
326  }
327  }
328 
351  function compile_permits_validation( &$Options )
352  {
353  try
354  {
355  if( $this->ContextUtilities->section_exists( $this->Config , 'permits_validation' ) )
356  {
357  $Validation = $this->ContextUtilities->get_section( $this->Config , 'permits_validation' );
358 
359  if( $this->PermitAlgorithms->fetch_permits_for_object( false , 'user' , $Validation ) === false )
360  {
361  $this->Trace->add_trace_string(
362  "{lang:permits_validation_was_not_passed} : $Validation" , COMMON
363  );
364 
365  $this->ContextUtilities->compile_no_permits( $Validation );
366  return( false );
367  }
368  }
369 
370  return( true );
371  }
372  catch( Exception $e )
373  {
374  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
375  }
376  }
377 
400  function compile_custom_validation( &$Options )
401  {
402  try
403  {
404  if( $this->ContextUtilities->section_exists( $this->Config , 'custom_validation' ) )
405  {
406  $CustomValidation = $this->ContextUtilities->get_section( $this->Config , 'custom_validation' );
407 
408  if( $this->CustomValidations->custom_validation( $CustomValidation , $Options ) === false )
409  {
410  $TraceString = "{lang:custom_validation_not_passed} : $CustomValidation";
411 
412  $this->Trace->add_trace_string( $TraceString , COMMON );
413 
414  return( false );
415  }
416  }
417 
418  return( true );
419  }
420  catch( Exception $e )
421  {
422  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
423  }
424  }
425 
448  private function compile_custom_validation_func( &$Options )
449  {
450  try
451  {
452  if( $this->ContextUtilities->section_exists( $this->Config , 'custom_validation_func' ) )
453  {
454  $ValidationObject = $this->ContextUtilities->get_custom_validation_object( $this->Config );
455 
456  $Func = $this->ContextUtilities->get_section( $this->Config , 'custom_validation_func' );
457 
458  if( call_user_func( array( $ValidationObject , $Func ) , $Options ) === false )
459  {
460  $TraceString = "{lang:custom_validation_func_not_passed} : $Func";
461 
462  $this->Trace->add_trace_string( $TraceString , COMMON );
463 
464  return( false );
465  }
466  }
467 
468  return( true );
469  }
470  catch( Exception $e )
471  {
472  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
473  }
474  }
475 
498  function compile_error_func( &$Owner , &$Options )
499  {
500  try
501  {
502  $Func = $this->ContextUtilities->get_section( $this->Config , 'error_func' , false );
503  if( $Func === false )
504  {
505  $ErrMsg = $this->SecurityValidator->get_error_message();
506  $ErrMsg = $ErrMsg === '' ? 'an_error_occured' : $ErrMsg;
507  $this->Messages->add_error_message( $ErrMsg );
508  $this->Trace->add_trace_string( "{lang:_std_error_message} : ".$ErrMsg , COMMON );
509  }
510  else
511  {
512  $FuncBody = 'return( $Owner->'.$Func.'( $Options ) );';
513  $NewFunc = create_function( '$OwnerObject , $Options' , $FuncBody );
514  $NewFunc( $Owner , $Options );
515  }
516  return( false );
517  }
518  catch( Exception $e )
519  {
520  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
521  }
522  }
523 
546  function check_success_function( &$Owner , $Func )
547  {
548  try
549  {
550  if( method_exists( $Owner , $Func ) === false )
551  {
552  throw( new Exception( "Method \"$Func\" was not found in class ".get_class( $Owner ) ) );
553  }
554  }
555  catch( Exception $e )
556  {
557  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
558  }
559  }
560 
575  private function cleanup_fields()
576  {
577  try
578  {
579  if( $this->ContextUtilities->section_exists( $this->Config , 'cleanup_fields' ) )
580  {
581  $Items = explode( ',' , $this->ContextUtilities->get_section(
582  $this->Config , 'cleanup_fields' )
583  );
584  foreach( $Items as $key => $value )
585  {
586  $this->Security->reset_p( $value , '' );
587  }
588  }
589  }
590  catch( Exception $e )
591  {
592  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
593  }
594  }
595 
622  function compile_package_success_func( &$Options , &$Owner , $Func )
623  {
624  try
625  {
626  $this->check_success_function( $Owner , $Func );
627 
628  $FunctionBody = 'return( $OwnerObject->'.$Func.'( $Options ) );';
629  $NewFunc = create_function( '$OwnerObject , $Options' , $FunctionBody );
630 
631  $this->Trace->add_trace_string( "{lang:calling_method} ".get_class( $Owner )."->$Func" , COMMON );
632 
633  if( $NewFunc( $Owner , $Options ) !== false )
634  {
635  $this->cleanup_fields();
636  }
637  }
638  catch( Exception $e )
639  {
640  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
641  }
642  }
643 
670  private function run_success_function( &$Options , &$Owner )
671  {
672  try
673  {
674  $Func = $this->ContextUtilities->get_section( $this->Config , 'success_func' , '_std_redirect' );
675 
676  if( $Func === '_std_redirect' )
677  {
678  $Url = $this->ContextUtilities->get_section( $this->Config , 'redirect_url' , false );
679 
680  if( $Url !== false )
681  {
682  $FunctionBody = 'return( _std_redirect( "'.$Url.'" , $OwnerObject ) );';
683 
684  $NewFunc = create_function( '$OwnerObject' , $FunctionBody );
685 
686  $NewFunc( $Owner );
687  }
688  }
689  else
690  {
691  $this->compile_package_success_func( $Options , $Owner , $Func );
692  }
693 
694  $this->ContextUtilities->compile_success_messages( $Options );
695  }
696  catch( Exception $e )
697  {
698  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
699  }
700  }
701 
724  private function run_filters( &$Options )
725  {
726  try
727  {
728  if( $this->compile_call_params_filter( $Options ) === false )
729  {
730  return( false );
731  }
732 
733  if( $this->ContextUtilities->compile_get_post_filter( $this->Config , $Options ) === false )
734  {
735  return( false );
736  }
737 
738  if( $this->compile_permits_filter( $Options ) === false )
739  {
740  return( false );
741  }
742 
743  return( true );
744  }
745  catch( Exception $e )
746  {
747  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
748  }
749  }
750 
777  private function run_execution( &$Options , &$Owner )
778  {
779  try
780  {
781  if( $this->run_filters( $Options , $Owner ) === false ||
782  $this->compile_permits_validation( $Options ) === false )
783  {
784  return( false );
785  }
786 
787  if( $this->ContextUtilities->compile_get_post_validation( $this->Config , $Options ) )
788  {
789  if( $this->compile_custom_validation( $Options ) === false ||
790  $this->compile_custom_validation_func( $Options ) === false )
791  {
792  return( false );
793  }
794 
795  $this->run_success_function( $Options , $Owner );
796 
797  return( true );
798  }
799 
800  return( $this->compile_error_func( $Owner , $Options ) );
801  }
802  catch( Exception $e )
803  {
804  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
805  }
806  }
807 
834  function execute( &$Options , &$Owner )
835  {
836  try
837  {
838  $this->Trace->start_group( "Context::execute for class \"".get_class( $Owner )."\"" );
839 
840  $Result = $this->run_execution( $Options , $Owner );
841  $Msg = '{lang:'.( $Result ? 'state_was_processed' : 'state_was_not_processed' ).'}';
842  $this->Trace->add_trace_string( $Msg , COMMON );
843  $this->Trace->end_group();
844  return( $Result );
845  }
846  catch( Exception $e )
847  {
848  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
849  }
850  }
851  }
852 
853 ?>