Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

enviroment.php

Go to the documentation of this file.
00001 
00002 /** @file enviroment.php
00003 * Definition of class enviroment.
00004 */
00005 #include "env_req.php"
00006 #include "stdlib.php"
00007 #include "fa_xep_user.php"
00008 #include "user_auth.php"
00009 #include "auth_constants.php"
00010 #include "template_handler.php"
00011 #include "users.php"
00012 #include "models.php"
00013 #include "model_abstract.php"
00014 #include "ranks.php"
00015 #include "actions_id.php"
00016 /**
00017 * Represents enviroment.
00018 * This class is a representation of system that provides
00019 * configuration of system, user authentification and models choosing.
00020 */
00021 class enviroment : public  fa_xep_user
00022 {
00023 public:
00024   /** Enviroment configuration. */
00025   var properties = array();
00026   /** Locations configuration. */
00027   var locations = array();
00028   /** User. */
00029   var user;
00030   /** Templates handler. */
00031   var templates_handler = 0;
00032   /** Template entities. */
00033   var entities = array();
00034   /** 
00035   * Register entities.
00036   */
00037   function set_entities()
00038   {
00039     this->entities[ '%date%' ] = strftime( '%x' );
00040     this->entities[ '%time%' ] = strftime( '%X' );
00041     this->entities[ '%session_id%' ] = session_id();
00042     this->entities[ '%user_name%' ] = GLOBALS[ 'user_name' ];
00043     if( isset( _SESSION ) )
00044       this->entities[ '%user_fullname%' ] = _SESSION[ 'user_fullname' ];
00045     if( isset( GLOBALS[ 'env_request' ] ) )
00046       this->entities[ '%env_request%' ] = GLOBALS[ 'env_request' ];
00047     if( isset( _SESSION[ 'model_name' ] ) )
00048       this->entities[ '%model_name%' ] = _SESSION[ 'model_name' ];
00049     foreach( this->entities as entv => entk )
00050     {
00051       this->templates_handler->set( entv, entk );
00052     }
00053   }
00054   /** 
00055   * Process template.
00056   * Process HTML template converting entities.
00057   * @param filename - template file name
00058   * @return string contains processed template
00059   * @see set_entities(), templates_handler
00060   */
00061   function process_template( filename )
00062   {
00063     if( !is_file( filename ) )
00064       return this->err( 'Nonexisted file name is given: '.filename, 'process_template', __LINE__, __FILE__ );
00065     if( !this->templates_handler )
00066     {
00067       this->templates_handler = new template_handler;
00068     }
00069     this->set_entities();
00070     res = this->templates_handler->process_file( filename );
00071     return res;
00072   }
00073   /**
00074   * Process locations gathered during import.
00075   */
00076   function process_locations()
00077   {
00078     env_dir = dirname( DEF_ENVIROMENT );
00079     foreach( this->locations as name => location )
00080     {
00081       this->locations[ name ][ 'loc' ] =
00082         location_to_filename( location[ 'loc' ], env_dir );
00083       if( !is_dir( this->locations[ name ][ 'loc' ] ) &&
00084           !is_file( this->locations[ name ][ 'loc' ] ) )
00085         if( location[ 'critical' ] )
00086           this->err( 'Critical location '.name.' not found: '.this->locations[ name ][ 'loc' ], 'process_locations', __LINE__, __FILE__ );
00087         elseif( location[ 'create' ] )
00088           mkdir( this->locations[ name ][ 'loc' ], 0750 );
00089     }
00090   }
00091   /**
00092   * Return location.
00093   * @param string name - name of location to return.
00094   */
00095   function location( name )
00096   {
00097     if( isset( this->locations[ name ] ) )
00098       return this->locations[ name ][ 'loc' ];
00099     else
00100       return this->err( 'Request for undefined location: '.name, 'location', __LINE__, __FILE__ );
00101   }
00102   /**
00103   * Initialize enviroment.
00104   */
00105   function start( data )
00106   {
00107     this->import( DEF_ENVIROMENT );
00108     this->process_locations();
00109   }
00110   /**
00111   * Provide user authentification.
00112   * @return authentification status
00113   * @see auth_constants.php
00114   */
00115   function authentificate()
00116   {
00117    global user_name, user_logged; 
00118    if( !isset( _POST[ 'session_id' ] ) ||
00119       !_POST[ 'session_id' ] )
00120    {
00121       if( !isset( _POST ) ||
00122           !isset( _POST[ 'user_name' ] ) ||
00123           !isset( _POST[ 'user_pswd' ] ) )
00124         return AUTH_NO_AUTH;
00125       this->user = new user_auth;
00126       if( !this->user->authentificate(
00127         this->location( 'users' ),
00128         _POST[ 'user_name' ],
00129         _POST[ 'user_pswd' ] )
00130         )
00131         return AUTH_FAILED;
00132       user_name = _POST[ 'user_name' ];
00133       user_logged = 1;
00134       mt_srand( (double)microtime() * 1000000 );
00135       session_id( md5( uniqid( mt_rand(), 1 ) ) );
00136       session_name( 'SID' );
00137       session_start();
00138       _SESSION[ 'user_name' ] = user_name;
00139       _SESSION[ 'user_logged' ] = user_logged;
00140       _SESSION[ 'user_fullname' ] = this->user->get_fullname();
00141       return AUTH_OK;
00142     }
00143     else
00144     {
00145       session_name( 'SID' );
00146       session_id( _POST[ 'session_id' ] );
00147       session_start();
00148       GLOBALS[ 'user_name' ] = _SESSION[ 'user_name' ];
00149       GLOBALS[ 'user_logged' ] = _SESSION[ 'user_logged' ];
00150       if( !GLOBALS[ 'user_name' ] ||
00151           !GLOBALS[ 'user_logged' ] )
00152         return AUTH_NO_AUTH;
00153     }
00154     return AUTH_OK;
00155   }
00156   /**
00157   * Authentification failure respond.
00158   * @param reason - failure reason
00159   * @see auth_constants.php
00160   */
00161   function do_auth_failure( reason )
00162   {
00163     switch( reason )
00164     {
00165       case AUTH_NO_AUTH:
00166         echo this->process_template( this->location( 'form_auth' ) );
00167         if( GLOBALS[ 'user_name' ] )
00168           session_destroy();
00169         break;
00170       case AUTH_FAILED:
00171         echo this->process_template( this->location( 'form_auth_failed' ) );
00172         if( GLOBALS[ 'user_name' ] )
00173           session_destroy();
00174         break;
00175     }
00176   }
00177   /**
00178   * Run.
00179   */
00180   function run( data )
00181   {
00182     session_save_path( this->location( 'sessions' ) );
00183     //Provide authentification.
00184     auth_status = this->authentificate();
00185     if( auth_status != AUTH_OK )
00186       return this->do_auth_failure( auth_status );
00187     //Provide requests handling.
00188     // Request from form has priority under request from session.
00189     if( isset( _POST[ 'env_request' ] ) )
00190       GLOBALS[ 'env_request' ] = _POST[ 'env_request' ];
00191     else if( isset( _SESSION[ 'env_request' ] ) )
00192       GLOBALS[ 'env_request' ] = _SESSION[ 'env_request' ];
00193     else
00194       GLOBALS[ 'env_request' ] = '';
00195     // Synchronize session with current request.
00196     _SESSION[ 'env_request' ] = GLOBALS[ 'env_request' ];
00197     //Cache ranks and users in session
00198     if( !isset( _SESSION[ 'ranks' ] ) )
00199     {
00200       rnks = new ranks;
00201       rnks->import( this->location( 'ranks' ) );
00202       _SESSION[ 'ranks' ] = rnks;
00203     }
00204     if( !isset( _SESSION[ 'users' ] ) )
00205     {
00206       users = new users;
00207       users->import( this->location( 'users' ) );
00208       _SESSION[ 'users' ] = users;
00209     }
00210     rnks = _SESSION[ 'ranks' ];
00211     GLOBALS[ 'ranks' ] = _SESSION[ 'ranks' ];
00212     users = _SESSION[ 'users' ];
00213     GLOBALS[ 'users' ] = _SESSION[ 'users' ];
00214     //Service request
00215     switch( GLOBALS[ 'env_request' ] )
00216     {
00217       case ENV_REQ_LOG_OFF:
00218         session_destroy();
00219         echo this->process_template( this->location( 'form_auth' ) );
00220         break;
00221       case ENV_REQ_SELECT_MODEL:
00222         unset( _SESSION[ 'model_name' ] );
00223         return this->choose_model( '' );
00224         break;
00225       case ENV_REQ_EXPLORE:
00226         if( 
00227           !rnks->is_action_allowed( 
00228             users->get_rank( _SESSION[ 'user_name' ] ),
00229             AID_LEARN          
00230            ) )
00231         {
00232           echo this->process_template( this->location( 'form_act_not_allowed' ) );
00233           break;
00234         }
00235         if( !isset( _SESSION[ 'model_name' ] ) 
00236           || _SESSION[ 'model_name' ] == '' )
00237           return this->choose_model(
00238             isset( _POST[ 'model_name' ] ) ? _POST[ 'model_name' ] : '' );
00239         else
00240         {
00241           models = new models;
00242           models->import( this->location( 'models' ) );
00243           models->base_path =
00244             location_to_filename( models->base_path, dirname( DEF_ENVIROMENT ) );
00245           model = new model_abstract;
00246           model = model->produce( 
00247             array_merge( _SESSION, _POST, GLOBALS, array( 'action' => 'explore' ) ) );
00248           model->owner = &this;
00249           model->import( models->get_location( _SESSION[ 'model_name' ] ) );
00250           this->entities[ '%theory%' ] = model->get_theory_representation();
00251           echo this->process_template( this->location( 'form_explore_theory' ) );
00252           break;
00253         }
00254         break;
00255       case ENV_REQ_EXPLORE_TEST:
00256         if( 
00257           !rnks->is_action_allowed( 
00258             users->get_rank( _SESSION[ 'user_name' ] ),
00259             AID_LEARN          
00260            ) )
00261         {
00262           echo this->process_template( this->location( 'form_act_not_allowed' ) );
00263           break;
00264         }
00265         if( !isset( _SESSION[ 'model_name' ] )
00266           || _SESSION[ 'model_name' ] == '' )
00267           return this->choose_model(
00268             isset( _POST[ 'model_name' ] ) ? _POST[ 'model_name' ] : '' );
00269         else
00270         {
00271           models = new models;
00272           models->import( this->location( 'models' ) );
00273           models->base_path =
00274             location_to_filename( models->base_path, dirname( DEF_ENVIROMENT ) );
00275           model = new model_abstract;
00276           model = model->produce(
00277             array_merge( _SESSION, _POST, GLOBALS, array( 'action' => 'explore' ) ) );
00278           model->owner = &this;
00279           model->import( models->get_location( _SESSION[ 'model_name' ] ) );
00280           this->entities[ '%test%' ] = model->get_test_representation( '' );
00281           echo this->process_template( this->location( 'form_explore_test' ) );
00282           break;
00283         }
00284         break;
00285       case ENV_REQ_END_TEST:
00286         if( 
00287           !rnks->is_action_allowed( 
00288             users->get_rank( _SESSION[ 'user_name' ] ),
00289             AID_LEARN          
00290            ) )
00291         {
00292           echo this->process_template( this->location( 'form_act_not_allowed' ) );
00293           break;
00294         }
00295         if( !isset( _SESSION[ 'model_name' ] )
00296           || _SESSION[ 'model_name' ] == '' )
00297           return this->choose_model(
00298             isset( _POST[ 'model_name' ] ) ? _POST[ 'model_name' ] : '' );
00299         else
00300         {
00301           models = new models;
00302           models->import( this->location( 'models' ) );
00303           models->base_path =
00304             location_to_filename( models->base_path, dirname( DEF_ENVIROMENT ) );
00305           model = new model_abstract;
00306           model = model->produce(
00307             array_merge( _SESSION, _POST, GLOBALS, array( 'action' => 'explore' ) ) );
00308           model->owner = &this;
00309           model->import( models->get_location( _SESSION[ 'model_name' ] ) );
00310           this->entities[ '%test_results%' ] = model->get_test_results( _POST );
00311           echo this->process_template( this->location( 'form_test_results' ) );
00312           break;
00313         }
00314         break;
00315       case ENV_REQ_EDIT:
00316         if( 
00317           !rnks->is_action_allowed( 
00318             users->get_rank( _SESSION[ 'user_name' ] ),
00319             AID_EDIT          
00320            ) )
00321         {
00322           echo this->process_template( this->location( 'form_act_not_allowed' ) );
00323           break;
00324         }
00325         break;
00326       default:
00327       case '':
00328       case ENV_REQ_RESTART:
00329         echo this->process_template( this->location( 'form_env_request' ) );
00330         unset( _SESSION[ 'model_name' ] );
00331         break;
00332     }
00333   }
00334   /** Choose model and check choice. */
00335   function choose_model( name )
00336   {
00337     users = GLOBALS[ 'users' ];
00338     if( name != '' )
00339     {
00340       skills = users->get_skills( _SESSION[ 'user_name' ] );
00341       models = new models;
00342       models->import( this->location( 'models' ) );
00343       models->base_path =
00344         location_to_filename( models->base_path, dirname( DEF_ENVIROMENT ) );
00345       depends = models->get_relations( name );
00346       depends = depends[ 'depends' ];
00347       present = true;
00348       foreach( depends as depend )
00349       {
00350           present = false;
00351           foreach( skills as skill )
00352           {
00353             if( depend == skill )
00354             {
00355               present = true;
00356               break;
00357             }
00358           }
00359         if( !present )
00360           break;
00361       }
00362       // User has required skills
00363       if( present )
00364       {
00365         _SESSION[ 'model_name' ] = name;
00366         echo this->process_template( this->location( 'form_model_choice_accepted' ) );
00367       }
00368     }
00369     else
00370     {
00371       skills = users->get_skills( _SESSION[ 'user_name' ] );
00372       models = new models;
00373       models->import( this->location( 'models' ) );
00374       models->base_path =
00375         location_to_filename( models->base_path, dirname( DEF_ENVIROMENT ) );
00376       accepted_models = array();
00377       models_list = models->get_models_list();
00378       foreach( models_list as model )
00379       {
00380         present = true;
00381         depends = models->get_relations( model );
00382         depends = depends[ 'depends' ];
00383         foreach( depends as depend )
00384         {
00385           present = false;
00386           foreach( skills as skill )
00387           {
00388             if( depend == skill )
00389             {
00390               present = true;
00391               break;
00392             }
00393           }
00394           if( !present )
00395             break;
00396         }
00397         // User has required skills
00398         if( present )
00399         {
00400           accepted_models[] = model;
00401         }
00402       }
00403       models_list = '';
00404       models_infos = '';
00405       foreach( accepted_models as model )
00406       {
00407         models_list .= '<option value="'.model.'">'.model."\n";
00408         models_infos .=  '"'.models->get_info( model ).'", ';
00409         /**/
00410       }
00411       models_infos .= '"bug"';
00412       if( !this->templates_handler )
00413       {
00414         this->templates_handler = new template_handler;
00415         this->set_entities();
00416       }
00417       this->templates_handler->set( '%models_list%', models_list );
00418       this->templates_handler->set( '%models_infos%', models_infos );
00419       echo this->process_template( this->location( 'form_choose_model' ) );
00420     }
00421   }
00422   /**
00423   * Action method.
00424   * @see start(), run()
00425   */
00426   function do_it( data )
00427   {
00428     this->start( data );
00429     this->run( data );
00430   }
00431 
00432   function xep_open_enviroment_properties( attributes )
00433   {
00434     foreach( attributes as attribute => value )
00435     {
00436       this->properties[ attribute ] = value;
00437     }
00438   }
00439 
00440   function xep_open_enviroment_locations_location( attributes )
00441   {
00442     name = attributes[ 'name' ];
00443     loc = attributes[ 'loc' ];
00444     critical = 
00445       ( isset( attributes[ 'critical' ] ) ) ?
00446         ( ( attributes[ 'critical' ] == 'yes' ) ? 1 : 0  ) : 1;
00447     create =
00448       ( isset( attributes[ 'create' ] ) ) ?
00449         ( ( attributes[ 'create' ] == 'yes' ) ? 1 : 0  ) : 0;
00450     this->locations[ name ] = 
00451       array( 'loc' => loc, 'critical' => critical, 'create' => create );
00452   }
00453 }
00454 X-Powered-By: PHP/4.0.8-dev
00455 Content-type: text/html
00456 
00457 

Generated on Tue Mar 11 08:35:12 2003 for SES by doxygen1.2.16