[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> utility_api.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Utility API
  19   *
  20   * Utility functions are *small* functions that are used often and therefore
  21   * have *no* prefix, to keep their names short.
  22   *
  23   * Utility functions have *no* dependencies on any other APIs, since they are
  24   * included first in order to make them available to all the APIs.
  25   * Miscellaneous functions that provide functionality on top of other APIS
  26   * are found in the helper_api.
  27   *
  28   * @package CoreAPI
  29   * @subpackage UtilityAPI
  30   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  31   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  32   * @link http://www.mantisbt.org
  33   *
  34   * @uses config_api.php
  35   * @uses constant_inc.php
  36   * @uses error_api.php
  37   */
  38  
  39  require_api( 'config_api.php' );
  40  require_api( 'constant_inc.php' );
  41  require_api( 'error_api.php' );
  42  
  43  /**
  44   * converts a 1 value to X
  45   * converts a 0 value to a space
  46   * @param int $p_num boolean numeric
  47   * @return string X or space
  48   * @access public
  49   */
  50  function trans_bool( $p_num ) {
  51      if( 0 == $p_num ) {
  52          return '&#160;';
  53      } else {
  54          return 'X';
  55      }
  56  }
  57  
  58  /**
  59   * Add a trailing DIRECTORY_SEPARATOR to a string if it isn't present
  60   * @param string $p_path
  61   * @return string
  62   * @access public
  63   */
  64  function terminate_directory_path( $p_path ) {
  65      return rtrim( $p_path, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
  66  }
  67  
  68  /**
  69   * Return true if the parameter is an empty string or a string
  70   * containing only whitespace, false otherwise
  71   * @param string $p_var string to test
  72   * @return bool
  73   * @access public
  74   */
  75  function is_blank( $p_var ) {
  76      $p_var = trim( $p_var );
  77      $str_len = strlen( $p_var );
  78      if( 0 == $str_len ) {
  79          return true;
  80      }
  81      return false;
  82  }
  83  
  84  /**
  85   * Get the named php ini variable but return it as a bool
  86   * @param string $p_name
  87   * @return bool
  88   * @access public
  89   */
  90  function ini_get_bool( $p_name ) {
  91      $result = ini_get( $p_name );
  92  
  93      if( is_string( $result ) ) {
  94          switch( $result ) {
  95              case 'off':
  96              case 'false':
  97              case 'no':
  98              case 'none':
  99              case '':
 100              case '0':
 101                  return false;
 102                  break;
 103              case 'on':
 104              case 'true':
 105              case 'yes':
 106              case '1':
 107                  return true;
 108                  break;
 109          }
 110      } else {
 111          return (bool) $result;
 112      }
 113  }
 114  
 115  /**
 116   * Get the named php.ini variable but return it as a number after converting
 117   * the giga (g/G), mega (m/M) and kilo (k/K) postfixes. These postfixes do not
 118   * adhere to IEEE 1541 in that k=1024, not k=1000. For more information see
 119   * http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
 120   * @param string $p_name Name of the configuration option to read.
 121   * @return int Integer value of the configuration option.
 122   * @access public
 123   */
 124  function ini_get_number( $p_name ) {
 125      $t_value = ini_get( $p_name );
 126  
 127      $t_result = 0;
 128      switch( substr( $t_value, -1 ) ) {
 129          case 'G':
 130          case 'g':
 131              $t_result = (int)$t_value * 1073741824;
 132              break;
 133          case 'M':
 134          case 'm':
 135              $t_result = (int)$t_value * 1048576;
 136              break;
 137          case 'K':
 138          case 'k':
 139              $t_result = (int)$t_value * 1024;
 140              break;
 141          default:
 142              $t_result = (int)$t_value;
 143              break;
 144      }
 145      return $t_result;
 146  }
 147  
 148  /**
 149   * Sort a multi-dimensional array by one of its keys
 150   * @param array $p_array Array to sort
 151   * @param string $p_key key to sort array on
 152   * @param int $p_direction sort direction
 153   * @return array sorted array
 154   * @access public
 155   */
 156  function multi_sort( $p_array, $p_key, $p_direction = ASCENDING ) {
 157      if( DESCENDING == $p_direction ) {
 158          $t_factor = -1;
 159      } else {
 160          # might as well allow everything else to mean ASC rather than erroring
 161          $t_factor = 1;
 162      }
 163  
 164      if( empty( $p_array ) ) {
 165          return $p_array;
 166      }
 167      if( !is_array( current($p_array ) ) ) {
 168          error_parameters( 'tried to multisort an invalid multi-dimensional array' );
 169          trigger_error(ERROR_GENERIC, ERROR);
 170      }
 171  
 172      // Security measure: see http://www.mantisbt.org/bugs/view.php?id=9704 for details
 173      if( array_key_exists( $p_key, current($p_array) ) ) {
 174          $t_function = create_function( '$a, $b', "return $t_factor * strnatcasecmp( \$a['" . $p_key . "'], \$b['" . $p_key . "'] );" );
 175          uasort( $p_array, $t_function );
 176      } else {
 177          trigger_error(ERROR_INVALID_SORT_FIELD, ERROR);
 178      }
 179      return $p_array;
 180  }
 181  
 182  /**
 183   * Return GD version
 184   * It doesn't use gd_info() so it works with PHP < 4.3.0 as well
 185   * @return int represents gd version
 186   * @access public
 187   */
 188  function get_gd_version() {
 189      $t_GDfuncList = get_extension_funcs( 'gd' );
 190      if( !is_array( $t_GDfuncList ) ) {
 191          return 0;
 192      } else {
 193          if( in_array( 'imagegd2', $t_GDfuncList ) ) {
 194              return 2;
 195          } else {
 196              return 1;
 197          }
 198      }
 199  }
 200  
 201  /**
 202   * return true or false if string matches current page name
 203   * @param string $p_string page name
 204   * @return bool
 205   * @access public
 206   */
 207  function is_page_name( $p_string ) {
 208      return isset( $_SERVER['SCRIPT_NAME'] ) && ( 0 < strpos( $_SERVER['SCRIPT_NAME'], $p_string ) );
 209  }
 210  
 211  function is_windows_server() {
 212      if( defined( 'PHP_WINDOWS_VERSION_MAJOR' ) ) {
 213          return (PHP_WINDOWS_VERSION_MAJOR > 0);
 214      }
 215      return ('WIN' == substr( PHP_OS, 0, 3 ) );
 216  }
 217  
 218  function getClassProperties($className, $types='public', $return_object = false, $include_parent = false ) {
 219      $ref = new ReflectionClass($className);
 220      $props = $ref->getProperties();
 221      $props_arr = array();
 222      foreach($props as $prop){
 223          $f = $prop->getName();
 224          if($prop->isPublic() and (stripos($types, 'public') === FALSE)) continue;
 225          if($prop->isPrivate() and (stripos($types, 'private') === FALSE)) continue;
 226          if($prop->isProtected() and (stripos($types, 'protected') === FALSE)) continue;
 227          if($prop->isStatic() and (stripos($types, 'static') === FALSE)) continue;
 228          if ( $return_object )
 229              $props_arr[$f] = $prop;
 230          else
 231              $props_arr[$f] = true;
 232      }
 233      if ( $include_parent ) {
 234          if($parentClass = $ref->getParentClass()){
 235              $parent_props_arr = getClassProperties($parentClass->getName());//RECURSION
 236              if(count($parent_props_arr) > 0)
 237                  $props_arr = array_merge($parent_props_arr, $props_arr);
 238          }
 239      }
 240      return $props_arr;
 241  }
 242  
 243  function get_font_path() {
 244          $t_font_path = config_get_global( 'system_font_folder' );
 245          if( $t_font_path == '' ) {
 246              if ( is_windows_server() ) {
 247                  $sroot = $_SERVER['SystemRoot'];
 248                  if( empty($sroot) ) {
 249                      return '';
 250                  } else {
 251                      $t_font_path = $sroot.'/fonts/';
 252                  }
 253              } else {
 254                  if( file_exists( '/usr/share/fonts/corefonts/' ) ) {
 255                      $t_font_path = '/usr/share/fonts/corefonts/';
 256                  } else if( file_exists( '/usr/share/fonts/truetype/msttcorefonts/' ) ) {
 257                      $t_font_path = '/usr/share/fonts/truetype/msttcorefonts/';
 258                  } else if( file_exists( '/usr/share/fonts/msttcorefonts/' ) ) {
 259                      $t_font_path = '/usr/share/fonts/msttcorefonts/';
 260                  } else {
 261                      $t_font_path = '/usr/share/fonts/truetype/';
 262                  }
 263              }
 264          }
 265          return $t_font_path;
 266  }


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7