File: utility_api.php

package info (click to toggle)
mantis 1.1.6%2Bdfsg-2lenny6
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,576 kB
  • ctags: 14,851
  • sloc: php: 54,817; sql: 348; sh: 181; makefile: 56
file content (245 lines) | stat: -rw-r--r-- 6,956 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
# Mantis - a php based bugtracking system

# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net

# Mantis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Mantis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mantis.  If not, see <http://www.gnu.org/licenses/>.

	# --------------------------------------------------------
	# $Id: utility_api.php,v 1.22.2.1 2007-10-13 22:35:47 giallu Exp $
	# --------------------------------------------------------

	### Utility API ###

	# Utility functions are *small* functions that are used often and therefore
	#  have *no* prefix, to keep their names short.
	#
	# Utility functions have *no* dependencies on any other APIs, since they are
	#  included first in order to make them available to all the APIs.
	#  Miscellaneous functions that provide functionality on top of other APIS
	#  are found in the helper_api.

	# --------------------
	# converts a 1 value to X
	# converts a 0 value to a space
	function trans_bool( $p_num ) {
		if ( 0 == $p_num ) {
			return '&nbsp;';
		} else {
			return 'X';
		}
	}

	# --------------------
	# Breaks up an enum string into num:value elements
	function explode_enum_string( $p_enum_string ) {
		return explode( ',', $p_enum_string );
	}

	# --------------------
	# Given one num:value pair it will return both in an array
	# num will be first (element 0) value second (element 1)
	function explode_enum_arr( $p_enum_elem ) {
		return explode( ':', $p_enum_elem );
	}

	# --------------------
	# Get the string associated with the $p_enum value
	function get_enum_to_array( $p_enum_string ) {
		$t_arr = explode_enum_string( $p_enum_string );
		$enum_count = count( $t_arr );
		for ($i=0; $i < $enum_count;$i++) {
			$t_s = explode_enum_arr( $t_arr[$i] );
			$t_index = (int) $t_s[0];
			$t_array[$t_index] = $t_s[1];
		}
		return $t_array;
	}

	# --------------------
	# Get the string associated with the $p_enum value
	function get_enum_to_string( $p_enum_string, $p_num ) {
		$t_arr = explode_enum_string( $p_enum_string );
		$enum_count = count( $t_arr );
		for ($i=0; $i < $enum_count;$i++) {
			$t_s = explode_enum_arr( $t_arr[$i] );
			if ( $t_s[0] == $p_num ) {
				return $t_s[1];
			}
		}
		return '@' . $p_num . '@';
	}

	# --------------------
	# Contributed by Peter Palmreuther
	function mime_encode( $p_string = '' ) {
		$output = '';
		$str_len = strlen( $p_string );
		for ( $i=0; $i < $str_len; $i++ ) {
			if (( ord( $p_string[$i] ) < 33 ) ||
				( ord( $p_string[$i] ) > 127 ) ||
				( eregi( "[\%\[\]\{\}\(\)]", $p_string[$i] ) )) {
				$output .= sprintf( '%%%02X', ord( $p_string[$i] ) );
			} else {
				$output .= $p_string[$i];
			}
		}
		return( $output );
	}

	# --------------------
	# This function checks to see if a variable is set
	# if it is not then it assigns the default value
	# otherwise it does nothing
	function check_varset( &$p_var, $p_default_value ) {
	     if ( !isset( $p_var ) ) {
	         $p_var = $p_default_value;
	     }
	}

	# --------------------
	# Add a trailing DIRECTORY_SEPARATOR to a string if it isn't present
	function terminate_directory_path( $p_path ) {
		$str_len = strlen($p_path);
		if ( $p_path && $p_path[$str_len-1] != DIRECTORY_SEPARATOR ) {
			$p_path = $p_path.DIRECTORY_SEPARATOR;
		}

		return $p_path;
	}

	# --------------------
	# Print a debug string by generating a notice
	function debug( $p_string ) {
		trigger_error( $p_string, NOTICE );
	}

	# --------------------
	# Return true if the parameter is an empty string or a string
	#  containing only whitespace, false otherwise
	function is_blank( $p_var ) {
		$p_var = trim( $p_var );
		$str_len = strlen( $p_var );
		if ( 0 == $str_len ) {
			return true;
		}
		return false;
	}

	# --------------------
	# Get the named php ini variable but return it as a bool
	function ini_get_bool( $p_name ) {
		$result = ini_get( $p_name );

		if ( is_string( $result ) ) {
			switch ( $result ) {
				case 'off':
				case 'false':
				case 'no':
				case 'none':
				case '':
				case '0':
					return false;
					break;
				case 'on':
				case 'true':
				case 'yes':
				case '1':
					return true;
					break;
			}
		} else {
			return (bool)$result;
		}
	}

	# --------------------
	# Get the named php ini variable but return it as a number after converting "K" and "M"
	function ini_get_number( $p_name ) {
		$t_result = ini_get( $p_name );
		$t_val = spliti( 'M', $t_result);
		if ( $t_val[0] != $t_result ) {
			return $t_val[0] * 1000000;
		}
		$t_val = spliti( 'K', $t_result);
		if ( $t_val[0] != $t_result ) {
			return $t_val[0] * 1000;
		}
		return $t_result;
	}



	# --------------------
	# Sort a multi-dimensional array by one of its keys
	function multi_sort( $p_array, $p_key, $p_direction=ASCENDING ) {
		if ( DESCENDING == $p_direction ) {
			$t_factor = -1;
		} else {
			# might as well allow everything else to mean ASC rather than erroring
			$t_factor = 1;
		}

		if( empty( $p_array ) ) {
			return $p_array;
		}
		if( !is_array( current($p_array ) ) ) {
			error_parameters( 'tried to multisort an invalid multi-dimensional array' );
			trigger_error(ERROR_GENERIC, ERROR);
		}

		// Security measure: see http://www.mantisbt.org/bugs/view.php?id=9704 for details
		if( array_key_exists( $p_key, current($p_array) ) ) {
			$t_function = create_function( '$a, $b', "return $t_factor * strnatcasecmp( \$a['" . $p_key . "'], \$b['" . $p_key . "'] );" );
			uasort( $p_array, $t_function );
		} else {
			trigger_error(ERROR_INVALID_SORT_FIELD, ERROR);
		}
		return $p_array;
	}

	# --------------------
	# Copies item with given key from source array to the destination,
	# if the key exists in the source. If not - does nothing.
	function copy_array_item_if_exist( &$p_arr_src, &$p_arr_dst, $key ) {
		if( array_key_exists( $key, $p_arr_src ) ) {
			$p_arr_dst[$key] = $p_arr_src[$key];
		}
	}

	# --------------------
	# Return GD version
	# It doesn't use gd_info() so it works with PHP < 4.3.0 as well
	function get_gd_version()
	{
		$t_GDfuncList = get_extension_funcs('gd');
		if( ! is_array( $t_GDfuncList ) ) {
			return 0;
		} else {
			if( in_array('imagegd2',$t_GDfuncList) ) {
				return 2;
			} else {
				return 1;
			}
		}
	}
	
	# ---------------------
	# return true or false if string matches current page name
	function is_page_name( $p_string ) {
	   return isset( $_SERVER['PHP_SELF'] ) && ( 0 < strpos( $_SERVER['PHP_SELF'], $p_string ) );
	}
	
?>