PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/mantisbt-1.2.8/core/utility_api.php

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