[ Index ]
 

Code source de Mantis 1.1.0rc3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/ -> config_api.php (source)

   1  <?php
   2  # Mantis - a php based bugtracking system
   3  
   4  # Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
   5  # Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net
   6  
   7  # Mantis is free software: you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation, either version 2 of the License, or
  10  # (at your option) any later version.
  11  #
  12  # Mantis is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  #
  17  # You should have received a copy of the GNU General Public License
  18  # along with Mantis.  If not, see <http://www.gnu.org/licenses/>.
  19  
  20      # --------------------------------------------------------
  21      # $Id: config_api.php,v 1.39.2.1 2007-10-13 22:35:19 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'error_api.php' );
  25  
  26      # cache for config variables
  27      $g_cache_config = array();
  28      $g_cache_config_access = array();
  29      $g_cache_filled = false;
  30      $g_cache_can_set_in_database = '';
  31      
  32      # cache environment to speed up lookups
  33      $g_cache_db_table_exists = false;
  34  
  35      ### Configuration API ###
  36  
  37      # ------------------
  38      # Retrieves the value of a config option
  39      #  This function will return one of (in order of preference):
  40      #    1. value from cache
  41      #    2. value from database
  42      #     looks for specified config_id + current user + current project.
  43      #     if not found, config_id + current user + all_project
  44      #     if not found, config_id + default user + current project
  45      #     if not found, config_id + default user + all_project.
  46      #    3.use GLOBAL[config_id]
  47  	function config_get( $p_option, $p_default = null, $p_user = null, $p_project = null ) {
  48          global $g_cache_config, $g_cache_config_access, $g_cache_db_table_exists, $g_cache_filled;
  49  
  50          # @@ debug @@ echo "lu o=$p_option ";
  51  
  52          # bypass table lookup for certain options
  53          $t_bypass_lookup = !config_can_set_in_database( $p_option );
  54          # @@ debug @@ if ($t_bypass_lookup) { echo "bp=$p_option match=$t_match_pattern <br />"; }
  55  
  56          if ( ! $t_bypass_lookup ) {
  57              $t_config_table = config_get_global( 'mantis_config_table' );
  58              # @@ debug @@ if ( ! db_is_connected() ) { echo "no db "; }
  59              # @@ debug @@ echo "lu table=" . ( db_table_exists( $t_config_table ) ? "yes " : "no " );
  60              if ( ! $g_cache_db_table_exists ) {
  61                  $g_cache_db_table_exists = ( TRUE === db_is_connected() ) &&
  62                      db_table_exists( $t_config_table );
  63              }
  64  
  65              if ( $g_cache_db_table_exists ) {
  66                  # @@ debug @@ echo " lu db $p_option ";
  67                  # @@ debug @@ error_print_stack_trace();
  68  
  69                  # prepare the user's list
  70                  $t_users = array();
  71                  if ( null === $p_user ) {
  72                      $t_users[] = auth_is_user_authenticated() ? auth_get_current_user_id() : ALL_USERS;
  73                  } else {
  74                      $t_users[] = $p_user;
  75                  }
  76                  if ( ! in_array( ALL_USERS, $t_users ) ) {
  77                      $t_users[] = ALL_USERS;
  78                  }
  79  
  80                  # prepare the projects list
  81                  $t_projects = array();
  82                  if ( ( null === $p_project )  ) {
  83                      $t_projects[] = auth_is_user_authenticated() ? helper_get_current_project() : ALL_PROJECTS;
  84                  } else {
  85                      $t_projects[] = $p_project;
  86                  }
  87                  if ( ! in_array( ALL_PROJECTS, $t_projects ) ) {
  88                      $t_projects[] = ALL_PROJECTS;
  89                  }
  90                  # @@ debug @@ echo 'pr= '; var_dump($t_projects);
  91                  # @@ debug @@ echo 'u= '; var_dump($t_users);
  92                  
  93                  if ( ! $g_cache_filled ) {
  94                      
  95                      $query = "SELECT config_id, user_id, project_id, type, value, access_reqd FROM $t_config_table";
  96                      $result = db_query( $query );
  97                      while ( false <> ( $row = db_fetch_array( $result ) ) ) {
  98                          $t_config = $row['config_id'];
  99                          $t_user = $row['user_id'];
 100                          $t_project = $row['project_id'];
 101                          $g_cache_config[$t_config][$t_user][$t_project] = $row['type'] . ';' . $row['value'];
 102                          $g_cache_config_access[$t_config][$t_user][$t_project] = $row['access_reqd'];
 103                      }
 104                      $g_cache_filled = true;
 105                  }
 106  
 107                  if( isset( $g_cache_config[$p_option] ) ) {
 108                      $t_found = false;
 109                      reset( $t_users );
 110                      while ( ( list( , $t_user ) = each( $t_users ) ) && ! $t_found ) {
 111                         reset( $t_projects );
 112                         while ( ( list( , $t_project ) = each( $t_projects ) ) && ! $t_found ) {
 113                             if ( isset( $g_cache_config[$p_option][$t_user][$t_project] ) ) {
 114                                  $t_value = $g_cache_config[$p_option][$t_user][$t_project];
 115                                  $t_found = true;
 116                                  # @@ debug @@ echo "clu found u=$t_user, p=$t_project, v=$t_value ";
 117                              }
 118                          }
 119                      }
 120                  
 121                      if ( $t_found ) {
 122                          list( $t_type, $t_raw_value ) = explode( ';', $t_value, 2 );
 123  
 124                          switch ( $t_type ) {
 125                              case CONFIG_TYPE_INT:
 126                                  $t_value = (int) $t_raw_value;
 127                                  break;
 128                              case CONFIG_TYPE_COMPLEX:
 129                                  $t_value = unserialize( $t_raw_value );
 130                                  break;
 131                              case CONFIG_TYPE_STRING:
 132                              default:
 133                                  $t_value = config_eval( $t_raw_value );
 134                          }
 135                          return $t_value;
 136                      }
 137                  }
 138              }
 139          }
 140          return config_get_global( $p_option, $p_default );
 141      }
 142  
 143      # ----------------------
 144      # force config variable from a global to avoid recursion
 145  	function config_get_global( $p_option, $p_default = null ) {
 146  
 147          if ( isset( $GLOBALS['g_' . $p_option] ) ) {
 148              $t_value = config_eval( $GLOBALS['g_' . $p_option] );
 149              if ( $t_value !== $GLOBALS['g_' . $p_option] ) {
 150                  $GLOBALS['g_' . $p_option] = $t_value;
 151              }
 152              return $t_value;
 153          } else {
 154              # unless we were allowing for the option not to exist by passing
 155              #  a default, trigger a WARNING
 156              if ( null === $p_default ) {
 157                  error_parameters( $p_option );
 158                  trigger_error( ERROR_CONFIG_OPT_NOT_FOUND, WARNING );
 159              }
 160              return $p_default;
 161          }
 162      }
 163  
 164      # ------------------
 165      # Retrieves the access level needed to change a config value
 166  	function config_get_access( $p_option, $p_user = null, $p_project = null ) {
 167          global $g_cache_config, $g_cache_config_access, $g_cache_filled;
 168  
 169          # @@ debug @@ echo "lu o=$p_option ";
 170  
 171          if ( ! $g_cache_filled ) {
 172              $t = config_get( $p_option, -1, $p_user, $p_project );
 173          }
 174          
 175          # prepare the user's list
 176          $t_users = array( );
 177          if ( ( null === $p_user ) && ( auth_is_user_authenticated() ) ) {
 178              $t_users[] = auth_get_current_user_id();
 179          } else if ( ! in_array( $p_user, $t_users ) ) {
 180              $t_users[] = $p_user;
 181          }
 182          $t_users[] = ALL_USERS;
 183  
 184          # prepare the projects list
 185          $t_projects = array( );
 186          if ( ( null === $p_project ) && ( auth_is_user_authenticated() ) ) {
 187              $t_selected_project = helper_get_current_project();
 188              if ( ALL_PROJECTS <> $t_selected_project ) {
 189                  $t_projects[] = $t_selected_project;
 190              }
 191          } else if ( ! in_array( $p_project, $t_projects ) ) {
 192              $t_projects[] = $p_project;
 193          }
 194          # @@ debug @@ echo 'pr= '; var_dump($t_projects);
 195          # @@ debug @@ echo 'u= '; var_dump($t_users);
 196                  
 197          $t_found = false;
 198          if ( isset( $g_cache_config[$p_option] ) ) {
 199              reset( $t_users );
 200              while ( ( list( , $t_user ) = each( $t_users ) ) && ! $t_found ) {
 201                  reset( $t_projects );
 202                  while ( ( list( , $t_project ) = each( $t_projects ) ) && ! $t_found ) {
 203                      if ( isset( $g_cache_config[$p_option][$t_user][$t_project] ) ) {
 204                          $t_access = $g_cache_config_access[$p_option][$t_user][$t_project];
 205                          $t_found = true;
 206                          # @@ debug @@ echo "clua found u=$t_user, p=$t_project, a=$t_access ";
 207                      }
 208                  }
 209              }
 210          }
 211  
 212          return $t_found ? $t_access : ADMINISTRATOR;
 213      }
 214  
 215      # ------------------
 216      # Returns true if the specified config option exists (ie. a
 217      #  value or default can be found), false otherwise
 218  	function config_is_set( $p_option, $p_user = null, $p_project = null ) {
 219          global $g_cache_config, $g_cache_filled;
 220          
 221          if ( ! $g_cache_filled ) {
 222              $t = config_get( $p_option, -1, $p_user, $p_project );
 223          }
 224          
 225          # prepare the user's list
 226          $t_users = array( ALL_USERS );
 227          if ( ( null === $p_user ) && ( auth_is_user_authenticated() ) ) {
 228              $t_users[] = auth_get_current_user_id();
 229          } else if ( ! in_array( $p_user, $t_users ) ) {
 230              $t_users[] = $p_user;
 231          }
 232          $t_users[] = ALL_USERS;
 233  
 234          # prepare the projects list
 235          $t_projects = array( ALL_PROJECTS );
 236          if ( ( null === $p_project ) && ( auth_is_user_authenticated() ) ) {
 237              $t_selected_project = helper_get_current_project();
 238              if ( ALL_PROJECTS <> $t_selected_project ) {
 239                  $t_projects[] = $t_selected_project;
 240              }
 241          } else if ( ! in_array( $p_project, $t_projects ) ) {
 242              $t_projects[] = $p_project;
 243          }
 244                  
 245          $t_found = false;
 246          reset( $t_users );
 247          while ( ( list( , $t_user ) = each( $t_users ) ) && ! $t_found ) {
 248              reset( $t_projects );
 249              while ( ( list( , $t_project ) = each( $t_projects ) ) && ! $t_found ) {
 250                  if ( isset( $g_cache_config[$p_option][$t_user][$t_project] ) ) {
 251                      $t_found = true;
 252                  }
 253              }
 254          }
 255                  
 256          if ( $t_found ) {
 257              return true;
 258          }
 259  
 260          return isset( $GLOBALS['g_' . $p_option] ) ;
 261      }
 262  
 263      # ------------------
 264      # Sets the value of the given config option to the given value
 265      #  If the config option does not exist, an ERROR is triggered
 266  	function config_set( $p_option, $p_value, $p_user = NO_USER, $p_project = ALL_PROJECTS, $p_access = ADMINISTRATOR ) {
 267          if ( is_array( $p_value ) || is_object( $p_value ) ) {
 268              $t_type = CONFIG_TYPE_COMPLEX;
 269              $c_value = db_prepare_string( serialize( $p_value ) );
 270          } else if ( is_int( $p_value ) || is_numeric( $p_value ) ) {
 271              $t_type = CONFIG_TYPE_INT;
 272              $c_value = db_prepare_int( $p_value );
 273          } else {
 274              $t_type = CONFIG_TYPE_STRING;
 275              $c_value = db_prepare_string( $p_value );
 276          }
 277  
 278          if ( config_can_set_in_database( $p_option ) ) {
 279              $c_option = db_prepare_string( $p_option );
 280              $c_user = db_prepare_int( $p_user );
 281              $c_project = db_prepare_int( $p_project );
 282              $c_access = db_prepare_int( $p_access );
 283  
 284              $t_config_table = config_get_global( 'mantis_config_table' );
 285              $query = "SELECT COUNT(*) from $t_config_table
 286                  WHERE config_id = '$c_option' AND
 287                      project_id = $c_project AND
 288                      user_id = $c_user";
 289              $result = db_query( $query );
 290  
 291              if ( 0 < db_result( $result ) ) {
 292                  $t_set_query = "UPDATE $t_config_table
 293                      SET value='$c_value', type=$t_type, access_reqd=$c_access
 294                      WHERE config_id = '$c_option' AND
 295                          project_id = $c_project AND
 296                          user_id = $c_user";
 297              } else {
 298                  $t_set_query = "INSERT INTO $t_config_table
 299                      ( value, type, access_reqd, config_id, project_id, user_id )
 300                      VALUES 
 301                      ('$c_value', $t_type, $c_access, '$c_option', $c_project, $c_user )";
 302              }
 303  
 304              $result = db_query( $t_set_query );
 305          }
 306  
 307          config_set_cache( $p_option, $p_value, $p_user, $p_project, $p_access );
 308  
 309          return true;
 310      }
 311      
 312      # ------------------
 313      # Sets the value of the given config option to the given value
 314      #  If the config option does not exist, an ERROR is triggered
 315  	function config_set_cache( $p_option, $p_value, $p_user = NO_USER, $p_project = ALL_PROJECTS, $p_access = ADMINISTRATOR ) {
 316          global $g_cache_config, $g_cache_config_access;
 317          $g_cache_config[$p_option][$p_user][$p_project] = $p_value;
 318          $g_cache_config_access[$p_option][$p_user][$p_project] = $p_access;
 319  
 320          return true;
 321      }
 322      
 323      # ------------------
 324      # Checks if the specific configuration option can be set in the database, otherwise it can only be set
 325      # in the configuration file (config_inc.php / config_defaults_inc.php).
 326  	function config_can_set_in_database( $p_option ) {
 327          global $g_cache_can_set_in_database;
 328          # bypass table lookup for certain options
 329          if ( $g_cache_can_set_in_database == '' ) {
 330              $g_cache_can_set_in_database = '/' . implode( '|', config_get_global( 'global_settings' ) ) . '/';
 331          }
 332          $t_bypass_lookup = ( 0 < preg_match( $g_cache_can_set_in_database, $p_option ) );
 333  
 334          return !$t_bypass_lookup;
 335      }
 336      
 337      # ------------------
 338      # Checks if the specific configuration option can be deleted from the database.
 339  	function config_can_delete( $p_option ) {
 340          return ( strtolower( $p_option ) != 'database_version' );
 341      }
 342  
 343      # ------------------
 344      # delete the config entry
 345  	function config_delete( $p_option, $p_user = ALL_USERS, $p_project = ALL_PROJECTS ) {
 346          global $g_cache_config, $g_cache_config_access;
 347  
 348          # bypass table lookup for certain options
 349          $t_bypass_lookup = !config_can_set_in_database( $p_option );
 350          # @@ debug @@ if ($t_bypass_lookup) { echo "bp=$p_option match=$t_match_pattern <br />"; }
 351          # @@ debug @@ if ( ! db_is_connected() ) { echo "no db"; }
 352  
 353          if ( ( ! $t_bypass_lookup ) && ( TRUE === db_is_connected() )
 354                  && ( db_table_exists( config_get_global( 'mantis_config_table' ) ) ) ) {
 355              if ( !config_can_delete( $p_option ) ) {
 356                  return;
 357              }
 358  
 359              $t_config_table = config_get_global( 'mantis_config_table' );
 360              # @@ debug @@ echo "lu table=" . ( db_table_exists( $t_config_table ) ? "yes" : "no" );
 361              # @@ debug @@ error_print_stack_trace();
 362  
 363              $c_option = db_prepare_string( $p_option );
 364              $c_user = db_prepare_int( $p_user );
 365              $c_project = db_prepare_int( $p_project );
 366              $query = "DELETE FROM $t_config_table
 367                  WHERE config_id = '$c_option' AND
 368                      project_id=$c_project AND
 369                      user_id=$c_user";
 370  
 371              $result = @db_query( $query);
 372          }
 373  
 374          config_flush_cache( $p_option, $p_user, $p_project );
 375      }        
 376  
 377      # ------------------
 378      # delete the config entry
 379  	function config_delete_project( $p_project = ALL_PROJECTS ) {
 380          global $g_cache_config, $g_cache_config_access;
 381          $t_config_table = config_get_global( 'mantis_config_table' );
 382          $c_project = db_prepare_int( $p_project );
 383          $query = "DELETE FROM $t_config_table
 384                  WHERE project_id=$c_project";
 385  
 386          $result = @db_query( $query);
 387          
 388          # flush cache here in case some of the deleted configs are in use.
 389          config_flush_cache(); 
 390      }        
 391          
 392      # ------------------
 393      # delete the config entry from the cache
 394      # @@@ to be used sparingly
 395  	function config_flush_cache( $p_option='', $p_user = ALL_USERS, $p_project = ALL_PROJECTS ) {
 396          global $g_cache_config, $g_cache_config_access, $g_cache_filled;
 397      
 398          if ( '' !== $p_option ) {
 399              unset( $GLOBALS['g_cache_config'][$p_option][$p_user][$p_project] );
 400              unset( $GLOBALS['g_cache_config_access'][$p_option][$p_user][$p_project] );
 401          } else {
 402              unset( $GLOBALS['g_cache_config'] );
 403              unset( $GLOBALS['g_cache_config_access'] );
 404              $g_cache_filled = false;
 405          }
 406              
 407      }
 408          
 409  
 410      # ------------------
 411      # Checks if an obsolete configuration variable is still in use.  If so, an error
 412      # will be generated and the script will exit.  This is called from admin_check.php.
 413  	function config_obsolete( $p_var, $p_replace ) {
 414          # @@@ we could trigger a WARNING here, once we have errors that can
 415          #     have extra data plugged into them (we need to give the old and
 416          #     new config option names in the warning text)
 417  
 418          if ( config_is_set( $p_var ) ) {
 419              PRINT '<p><b>Warning:</b> The configuration option <tt>$g_' . $p_var . '</tt> is now obsolete';
 420              if ( is_array( $p_replace ) ) {
 421                  PRINT ', please see the following options: <ul>';
 422                  foreach ( $p_replace as $t_option ) {
 423                      PRINT '<li>$g_' . $t_option . '</li>';
 424                  }
 425                  PRINT '</ul>';
 426              } else if ( !is_blank( $p_replace ) ) {
 427                  PRINT ', please use <tt>$g_' . $p_replace . '</tt> instead.';
 428              }
 429              PRINT '</p>';
 430          }
 431      }
 432  
 433      # ------------------
 434      # check for recursion in defining config variables
 435      # If there is a %text% in the returned value, re-evaluate the "text" part and replace
 436      #  the string
 437  	function config_eval( $p_value ) {
 438          $t_value = $p_value;
 439          if ( is_string( $t_value ) && !is_numeric( $t_value ) ) {
 440              if ( 0 < preg_match_all( '/%(.*)%/U', $t_value, $t_matches ) ) {
 441                  $t_count = count( $t_matches[0] );
 442                  for ($i=0; $i<$t_count; $i++) {
 443                      # $t_matches[0][$i] is the matched string including the delimiters
 444                      # $t_matches[1][$i] is the target parameter string
 445                      $t_repl = config_get( $t_matches[1][$i] );
 446                      $t_value = str_replace( $t_matches[0][$i], $t_repl, $t_value );
 447                  }
 448              }
 449          }
 450          return $t_value;
 451      }
 452  ?>


Généré le : Thu Nov 29 09:42:17 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics