[ 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/ -> gpc_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: gpc_api.php,v 1.41.2.1 2007-10-13 22:35:28 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      ### GET, POST, and Cookie API ###
  25  
  26      # ---------------
  27      # Retrieve a GPC variable.
  28      # If the variable is not set, the default is returned.
  29      # If magic_quotes_gpc is on, slashes will be stripped from the value before being returned.
  30      #
  31      #  You may pass in any variable as a default (including null) but if
  32      #  you pass in *no* default then an error will be triggered if the field
  33      #  cannot be found
  34  	function gpc_get( $p_var_name, $p_default = null ) {
  35          if ( isset( $_POST[$p_var_name] ) ) {
  36              $t_result = gpc_strip_slashes( $_POST[$p_var_name] );
  37          } else if ( isset( $_GET[$p_var_name] ) ) {
  38              $t_result = gpc_strip_slashes( $_GET[$p_var_name] );
  39          } else if ( func_num_args() > 1 ) { #check for a default passed in (allowing null)
  40              $t_result = $p_default;
  41          } else {
  42              error_parameters( $p_var_name );
  43              trigger_error( ERROR_GPC_VAR_NOT_FOUND, ERROR );
  44              $t_result = null;
  45          }
  46  
  47          return $t_result;
  48      }
  49      # -----------------
  50      # Retrieve a string GPC variable. Uses gpc_get().
  51      #  If you pass in *no* default, an error will be triggered if
  52      #  the variable does not exist
  53  	function gpc_get_string( $p_var_name, $p_default = null ) {
  54          # Don't pass along a default unless one was given to us
  55          #  otherwise we prevent an error being triggered
  56          $args = func_get_args();
  57          $t_result = call_user_func_array( 'gpc_get', $args );
  58  
  59          if ( is_array( $t_result ) ) {
  60              error_parameters( $p_var_name );
  61              trigger_error( ERROR_GPC_ARRAY_UNEXPECTED, ERROR );
  62          }
  63  
  64          return $t_result;
  65      }
  66      # ------------------
  67      # Retrieve an integer GPC variable. Uses gpc_get().
  68      #  If you pass in *no* default, an error will be triggered if
  69      #  the variable does not exist
  70  	function gpc_get_int( $p_var_name, $p_default = null ) {
  71          # Don't pass along a default unless one was given to us
  72          #  otherwise we prevent an error being triggered
  73          $args = func_get_args();
  74          $t_result = call_user_func_array( 'gpc_get', $args );
  75  
  76          if ( is_array( $t_result ) ) {
  77              error_parameters( $p_var_name );
  78              trigger_error( ERROR_GPC_ARRAY_UNEXPECTED, ERROR );
  79          }
  80          $t_val = str_replace( " ", "", trim( $t_result ) );
  81          if ( ! preg_match( "/^-?([0-9])*$/", $t_val ) ) {
  82              error_parameters( $p_var_name );
  83              trigger_error( ERROR_GPC_NOT_NUMBER, ERROR );
  84          }
  85  
  86          return (int)$t_val;
  87      }
  88      # ------------------
  89      # Retrieve a boolean GPC variable. Uses gpc_get().
  90      #  If you pass in *no* default, false will be used
  91  	function gpc_get_bool( $p_var_name, $p_default = false ) {
  92          $t_result = gpc_get( $p_var_name, $p_default );
  93  
  94          if ( $t_result === $p_default ) {
  95              return $p_default;
  96          } else {
  97              if ( is_array( $t_result ) ) {
  98                  error_parameters( $p_var_name );
  99                  trigger_error( ERROR_GPC_ARRAY_UNEXPECTED, ERROR );
 100              }
 101  
 102              return gpc_string_to_bool( $t_result );
 103          }
 104      }
 105  
 106      #===================================
 107      # Custom Field Functions
 108      #===================================
 109  
 110      # ------------------
 111      # Retrieve a custom field variable.  Uses gpc_get().
 112      #  If you pass in *no* default, an error will be triggered if
 113      #  the variable does not exist
 114  	function gpc_get_custom_field( $p_var_name, $p_custom_field_type, $p_default = null ) {
 115          switch ($p_custom_field_type ) {
 116              case CUSTOM_FIELD_TYPE_MULTILIST:
 117              case CUSTOM_FIELD_TYPE_CHECKBOX:
 118                  $t_values = gpc_get_string_array( $p_var_name, $p_default );
 119                  if( null !== $t_values && '' != $t_values ) {
 120                      return implode( '|', $t_values );
 121                  } else {
 122                      return '';
 123                  }
 124                  break ;
 125              case CUSTOM_FIELD_TYPE_DATE:
 126                  $t_day = gpc_get_int( $p_var_name . "_day", 0) ;
 127                  $t_month = gpc_get_int( $p_var_name . "_month", 0) ;
 128                  $t_year = gpc_get_int( $p_var_name . "_year", 0) ;
 129                  if (($t_year == 0) || ($t_month == 0) || ($t_day == 0)) {
 130                      if ($p_default == null) {
 131                          return '' ;
 132                      } else {
 133                          return $p_default ;
 134                      }
 135                  } else {
 136                      return strtotime($t_year . "-" . $t_month . "-" . $t_day) ;
 137                  }
 138                  break ;
 139              default:
 140                  return gpc_get_string( $p_var_name, $p_default);
 141          }
 142      }
 143  
 144      #===================================
 145      # Array Functions
 146      #===================================
 147  
 148      # ------------------
 149      # Retrieve a string array GPC variable.  Uses gpc_get().
 150      #  If you pass in *no* default, an error will be triggered if
 151      #  the variable does not exist
 152  	function gpc_get_string_array( $p_var_name, $p_default = null ) {
 153          # Don't pass along a default unless one was given to us
 154          #  otherwise we prevent an error being triggered
 155          $args = func_get_args();
 156          $t_result = call_user_func_array( 'gpc_get', $args );
 157  
 158          # If we the result isn't the default we were given or an array, error
 159          if ( !( ( ( 1 < func_num_args() ) && ( $t_result === $p_default ) ) ||
 160              is_array( $t_result ) ) ) {
 161              error_parameters( $p_var_name );
 162              trigger_error( ERROR_GPC_ARRAY_EXPECTED, ERROR);
 163          }
 164  
 165          return $t_result;
 166      }
 167      # ------------------
 168      # Retrieve an integer array GPC variable.  Uses gpc_get().
 169      #  If you pass in *no* default, an error will be triggered if
 170      #  the variable does not exist
 171  	function gpc_get_int_array( $p_var_name, $p_default = null ) {
 172          # Don't pass along a default unless one was given to us
 173          #  otherwise we prevent an error being triggered
 174          $args = func_get_args();
 175          $t_result = call_user_func_array( 'gpc_get', $args );
 176  
 177          # If we the result isn't the default we were given or an array, error
 178          if ( !( ( ( 1 < func_num_args() ) && ( $t_result === $p_default ) ) ||
 179                   is_array( $t_result ) ) ) {
 180              error_parameters( $p_var_name );
 181              trigger_error( ERROR_GPC_ARRAY_EXPECTED, ERROR);
 182          }
 183  
 184          for ( $i=0 ; $i < sizeof( $t_result ) ; $i++ ) {
 185              $t_result[$i] = (int)$t_result[$i];
 186          }
 187  
 188          return $t_result;
 189      }
 190      # ------------------
 191      # Retrieve a boolean array GPC variable.  Uses gpc_get().
 192      #  If you pass in *no* default, an error will be triggered if
 193      #  the variable does not exist
 194  	function gpc_get_bool_array( $p_var_name, $p_default = null ) {
 195          # Don't pass along a default unless one was given to us
 196          #  otherwise we prevent an error being triggered
 197          $args = func_get_args();
 198          $t_result = call_user_func_array( 'gpc_get', $args );
 199  
 200          # If we the result isn't the default we were given or an array, error
 201          if ( !( ( ( 1 < func_num_args() ) && ( $t_result === $p_default ) ) ||
 202                   is_array( $t_result ) ) ) {
 203              error_parameters( $p_var_name );
 204              trigger_error( ERROR_GPC_ARRAY_EXPECTED, ERROR);
 205          }
 206  
 207          for ( $i=0 ; $i < sizeof( $t_result ) ; $i++ ) {
 208              $t_result[$i] = gpc_string_to_bool( $t_result[$i] );
 209          }
 210  
 211          return $t_result;
 212      }
 213  
 214      #===================================
 215      # Cookie Functions
 216      #===================================
 217  
 218      # ------------------
 219      # Retrieve a cookie variable
 220      #  You may pass in any variable as a default (including null) but if
 221      #  you pass in *no* default then an error will be triggered if the cookie
 222      #  cannot be found
 223  	function gpc_get_cookie( $p_var_name, $p_default = null ) {
 224          if ( isset( $_COOKIE[$p_var_name] ) ) {
 225              $t_result = gpc_strip_slashes( $_COOKIE[$p_var_name] );
 226          } else if ( func_num_args() > 1 ) { #check for a default passed in (allowing null)
 227              $t_result = $p_default;
 228          } else {
 229              error_parameters( $p_var_name );
 230              trigger_error(ERROR_GPC_VAR_NOT_FOUND, ERROR);
 231          }
 232  
 233          return $t_result;
 234      }
 235  
 236      # ------------------
 237      # Set a cookie variable
 238      # If $p_expire is false instead of a number, the cookie will expire when
 239      #  the browser is closed; if it is true, the default time from the config
 240      #  file will be used
 241      # If $p_path or $p_domain are omitted, defaults are used
 242      #
 243      # @@@ this function is to be modified by Victor to add CRC... for now it
 244      #  just passes the parameters through to setcookie()
 245  	function gpc_set_cookie( $p_name, $p_value, $p_expire=false, $p_path=null, $p_domain=null ) {
 246          if ( false === $p_expire ) {
 247              $p_expire = 0;
 248          } else if (true === $p_expire ) {
 249              $t_cookie_length = config_get( 'cookie_time_length' );
 250              $p_expire = time()+$t_cookie_length;
 251          }
 252          if ( null === $p_path ) {
 253              $p_path = config_get( 'cookie_path' );
 254          }
 255          if ( null === $p_domain ) {
 256              $p_domain = config_get( 'cookie_domain' );
 257          }
 258  
 259          return setcookie( $p_name, $p_value, $p_expire, $p_path, $p_domain );
 260      }
 261  
 262      # ------------------
 263      # Clear a cookie variable
 264  	function gpc_clear_cookie( $p_name, $p_path=null, $p_domain=null ) {
 265          if ( null === $p_path ) {
 266              $p_path = config_get( 'cookie_path' );
 267          }
 268          if ( null === $p_domain ) {
 269              $p_domain = config_get( 'cookie_domain' );
 270          }
 271  
 272          if ( isset( $_COOKIE[$p_name] ) ) {
 273              unset( $_COOKIE[$p_name] ) ;
 274          }
 275  
 276          # dont try to send cookie if headers are send (guideweb)
 277          if ( !headers_sent() ) {
 278              return setcookie( $p_name, '', -1, $p_path, $p_domain );
 279          } else {
 280              return false;
 281          }
 282      }
 283  
 284      #===================================
 285      # File Functions
 286      #===================================
 287  
 288      # ------------------
 289      # Retrieve a file variable
 290      #  You may pass in any variable as a default (including null) but if
 291      #  you pass in *no* default then an error will be triggered if the file
 292      #  cannot be found
 293  	function gpc_get_file( $p_var_name, $p_default = null ) {
 294          if ( isset ( $_FILES[$p_var_name] ) ) {
 295              # FILES are not escaped even if magic_quotes is ON, this applies to Windows paths.
 296              $t_result = $_FILES[$p_var_name];
 297          } else if ( func_num_args() > 1 ) { #check for a default passed in (allowing null)
 298              $t_result = $p_default;
 299          } else {
 300              error_parameters( $p_var_name );
 301              trigger_error(ERROR_GPC_VAR_NOT_FOUND, ERROR);
 302          }
 303  
 304          return $t_result;
 305      }
 306  
 307      #===================================
 308      # Helper Functions
 309      #===================================
 310  
 311      # ------------------
 312      # Convert a POST/GET parameter to an array if it is not already one.
 313      # $p_var_name - The name of the parameter
 314      # no return value.  The $_POST/$_GET are updated as appropriate. 
 315  	function gpc_make_array( $p_var_name ) {
 316          if ( isset( $_POST[$p_var_name] ) && !is_array( $_POST[$p_var_name] ) ) {
 317              $_POST[$p_var_name] = array( $_POST[$p_var_name] );
 318          }
 319  
 320          if ( isset( $_GET[$p_var_name] ) && !is_array( $_GET[$p_var_name] ) ) {
 321              $_GET[$p_var_name] = array( $_GET[$p_var_name] );
 322          }
 323      }
 324  
 325      # ------------------
 326      # Convert a string to a bool
 327  	function gpc_string_to_bool( $p_string ) {
 328          if ( 0 == strcasecmp( 'off', $p_string ) ||
 329               0 == strcasecmp( 'no', $p_string ) ||
 330               0 == strcasecmp( 'false', $p_string ) ||
 331               0 == strcasecmp( '', $p_string ) ||
 332               0 == strcasecmp( '0', $p_string ) ) {
 333              return false;
 334          } else {
 335              return true;
 336          }
 337      }
 338  
 339      # ------------------
 340      # Strip slashes if necessary (supports arrays)
 341  	function gpc_strip_slashes( $p_var ) {
 342          if ( 0 == get_magic_quotes_gpc() ) {
 343              return $p_var;
 344          } else if ( !is_array( $p_var ) ){
 345              return stripslashes( $p_var );
 346          } else {
 347              foreach ( $p_var as $key => $value ) {
 348                  $p_var[$key] = gpc_strip_slashes( $value );
 349              }
 350              return $p_var;
 351          }
 352      }
 353  ?>


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