Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
325 richard 1
<?php 
2
/***************************************************************************
3
 *   Copyright (C) 2006 by phpSysInfo - A PHP System Information Script    *
4
 *   http://phpsysinfo.sourceforge.net/                                    *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU General Public License as published by  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
21
 
22
// $Id: common_functions.php,v 1.55 2007/02/20 19:20:20 bigmichi1 Exp $
23
 
24
// usefull during development
25
if( isset($showerrors) && $showerrors ) {
26
	error_reporting( E_ALL | E_NOTICE );
27
} else {
28
	error_reporting( E_ERROR | E_WARNING | E_PARSE );
29
}
30
 
31
// HTML/XML Comment
32
function created_by () {
33
	global $VERSION;
34
 
35
	return "<!--\n\tCreated By: phpSysInfo - " . $VERSION . "\n\thttp://phpsysinfo.sourceforge.net/\n-->\n";
36
} 
37
 
38
// print out the bar graph
39
// $value as full percentages
40
// $maximim as current maximum 
41
// $b as scale factor
42
// $type as filesystem type
43
function create_bargraph ($value, $maximum, $b, $type = "") {
44
	global $webpath;
45
 
46
	$textdir = direction();
47
	$imgpath = $webpath . 'templates/' . TEMPLATE_SET . '/images/';
48
	$maximum == 0 ? $barwidth = 0 : $barwidth = round((100  / $maximum) * $value) * $b;
49
	$red = 90 * $b;
50
	$yellow = 75 * $b;
51
	if (!file_exists(APP_ROOT . "/templates/" . TEMPLATE_SET . "/images/nobar_left.gif")) {
52
		if ($barwidth == 0) {
53
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_' . $textdir['left'] . '.gif">'
54
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_middle.gif" width="1">'
55
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_' . $textdir['right'] . '.gif">';
56
		} elseif ( file_exists( APP_ROOT . "/templates/" . TEMPLATE_SET . "/images/yellowbar_left.gif") && ( $barwidth > $yellow ) && ( $barwidth < $red ) ) {
57
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'yellowbar_' . $textdir['left'] . '.gif">'
58
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'yellowbar_middle.gif" width="' . $barwidth . '">'
59
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'yellowbar_' . $textdir['right'] . '.gif">';
60
		} elseif ( ( $barwidth < $red ) || ( $type == "iso9660" ) || ( $type == "CDFS" ) ) {
61
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_' . $textdir['left'] . '.gif">'
62
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_middle.gif" width="' . $barwidth . '">'
63
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_' . $textdir['right'] . '.gif">';
64
		} else {
65
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_' . $textdir['left'] . '.gif">'
66
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_middle.gif" width="' . $barwidth . '">'
67
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_' . $textdir['right'] . '.gif">';
68
		}
69
	} else {
70
		if ($barwidth == 0) {
71
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_' . $textdir['left'] . '.gif">'
72
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_middle.gif" width="' . ( 100 * $b ) . '">'
73
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif">';
74
		} elseif ( file_exists( APP_ROOT . "/templates/" . TEMPLATE_SET . "/images/yellowbar_left.gif" ) && ( $barwidth > $yellow ) && ( $barwidth < $red ) ) {
75
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'yellowbar_' . $textdir['left'] . '.gif">'
76
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'yellowbar_middle.gif" width="' . $barwidth . '">'
77
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_middle.gif" width="' . ( ( 100 * $b ) - $barwidth ) . '">'
78
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif">';
79
		} elseif ( ( $barwidth < $red ) || ( $type == "iso9660" ) || ( $type == "CDFS" ) ) {
80
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_' . $textdir['left'] . '.gif">'
81
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'bar_middle.gif" width="' . $barwidth . '">'
82
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_middle.gif" width="' . ( ( 100 * $b ) - $barwidth ) . '">'
83
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif">';
84
		} elseif ( $barwidth == ( 100 * $b ) ) {
85
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_' . $textdir['left'] . '.gif">'
86
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_middle.gif" width="' . ( 100 * $b ) . '">'
87
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_' . $textdir['right'] . '.gif">';
88
		} else {
89
			return '<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_' . $textdir['left'] . '.gif">'
90
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'redbar_middle.gif" width="' . $barwidth . '">'
91
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_middle.gif" width="' . ( ( 100 * $b ) - $barwidth ) . '">'
92
				  .'<img height="' . BAR_HEIGHT . '" alt="" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif">';
93
		}
94
	}
95
}
96
 
97
function create_bargraph_grad( $value, $maximum, $b, $type = "" ) {
98
	global $webpath;
99
 
100
	$maximum == 0 ? $barwidth = 0 : $barwidth = round( ( 100  / $maximum ) * $value );
101
	$startColor = '0ef424'; // green
102
	$endColor = 'ee200a'; // red
103
	if ( $barwidth > 100 ) {
104
		$barwidth = 0;
105
	}
106
 
107
	return '<img height="' . BAR_HEIGHT . '" width="300" src="' . $webpath . 'includes/indicator.php?height=' . BAR_HEIGHT . '&amp;percent=' . $barwidth . '&amp;color1=' . $startColor . '&amp;color2=' . $endColor . '" alt="">';
108
}
109
 
110
function direction() {
111
	global $text_dir;
112
 
113
	if( ! isset( $text_dir ) || ( $text_dir == "ltr" ) ) {
114
		$arrResult['direction'] = "ltr";
115
		$arrResult['left'] = "left";
116
		$arrResult['right'] = "right";
117
	} else {
118
		$arrResult['direction'] = "rtl";
119
		$arrResult['left'] = "right";
120
		$arrResult['right'] = "left";
121
	}
122
 
123
	return $arrResult;
124
}
125
 
126
// Find a system program.  Do path checking
127
function find_program ($strProgram) {
128
	global $addpaths;
129
 
1827 raphael.pi 130
	$arrPath = array( '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin' );
325 richard 131
	if( isset( $addpaths ) && is_array( $addpaths ) ) {
132
		$arrPath = array_merge( $arrPath, $addpaths );
133
	}
134
	if ( function_exists( "is_executable" ) ) {
135
		foreach ( $arrPath as $strPath ) {
136
			$strProgrammpath = $strPath . "/" . $strProgram;
137
			if( is_executable( $strProgrammpath ) ) {
138
				return $strProgrammpath;
139
			}
140
		}
141
	} else {
142
		return strpos( $strProgram, '.exe' );
143
	}
144
}
145
 
146
// Execute a system program. return a trim()'d result.
147
// does very crude pipe checking.  you need ' | ' for it to work
148
// ie $program = execute_program('netstat', '-anp | grep LIST');
149
// NOT $program = execute_program('netstat', '-anp|grep LIST');
150
function execute_program ($strProgramname, $strArgs = '', $booErrorRep = true ) {
151
	global $error;
152
	$strBuffer = '';
153
	$strError = '';
154
 
155
	$strProgram = find_program($strProgramname);
156
	if ( ! $strProgram ) {
157
		if( $booErrorRep ) {
158
			$error->addError( 'find_program(' . $strProgramname . ')', 'program not found on the machine', __LINE__, __FILE__);
159
		}
160
		return "ERROR";
161
	}
162
	// see if we've gotten a |, if we have we need to do patch checking on the cmd
163
	if( $strArgs ) {
164
		$arrArgs = explode( ' ', $strArgs );
165
		for( $i = 0; $i < count( $arrArgs ); $i++ ) {
166
			if ( $arrArgs[$i] == '|' ) {
167
				$strCmd = $arrArgs[$i + 1];
168
				$strNewcmd = find_program( $strCmd );
169
				$strArgs = ereg_replace( "\| " . $strCmd, "| " . $strNewcmd, $strArgs );
170
			}
171
		}
172
	}
173
	// no proc_open() below php 4.3
174
	if( function_exists( 'proc_open' ) ) {
175
		$descriptorspec = array(
176
 
177
			1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
178
			2 => array("pipe", "w")   // stderr is a pipe that the child will write to
179
		);
180
		$process = proc_open( $strProgram . " " . $strArgs, $descriptorspec, $pipes );
181
		if( is_resource( $process ) ) {
182
			while( !feof( $pipes[1] ) ) {
183
				$strBuffer .= fgets( $pipes[1], 1024 );
184
			}
185
			fclose( $pipes[1] );
186
			while( !feof( $pipes[2] ) ) {
187
				$strError .= fgets( $pipes[2], 1024 );
188
			}
189
			fclose( $pipes[2] );
190
		}
191
		$return_value = proc_close( $process );
192
	} else {
193
		if( $fp = popen( "(" . $strProgram . " " . $strArgs . " > /dev/null) 3>&1 1>&2 2>&3", 'r' ) ) {
194
			while( ! feof( $fp ) ) {
195
				$strError .= fgets( $fp, 4096 );
196
			}
197
			pclose( $fp );
198
		}
199
		$strError = trim( $strError );
200
		if( $fp = popen( $strProgram . " " . $strArgs, 'r' ) ) {
201
			while( ! feof( $fp ) ) {
202
				$strBuffer .= fgets( $fp, 4096 );
203
			}
204
			$return_value = pclose( $fp );
205
		}
206
	}
207
 
208
	$strError = trim( $strError );
209
	$strBuffer = trim( $strBuffer );
210
 
211
	if( ! empty( $strError ) || $return_value <> 0 ) {
212
		if( $booErrorRep ) {
213
			$error->addError( $strProgram, $strError . "\nReturn value: " . $return_value, __LINE__, __FILE__);
214
		}
215
	}
216
	return $strBuffer;
217
}
218
 
219
// A helper function, when passed a number representing KB,
220
// and optionally the number of decimal places required,
221
// it returns a formated number string, with unit identifier.
222
function format_bytesize ($intKbytes, $intDecplaces = 2) {
223
	global $text;
224
	$strSpacer = '&nbsp;';
225
 
226
	if( $intKbytes > 1048576 ) {
227
		$strResult = sprintf( '%.' . $intDecplaces . 'f', $intKbytes / 1048576 );
228
		$strResult .= $strSpacer . $text['gb'];
229
	} elseif( $intKbytes > 1024 ) {
230
		$strResult = sprintf( '%.' . $intDecplaces . 'f', $intKbytes / 1024);
231
		$strResult .= $strSpacer . $text['mb'];
232
	} else {
233
		$strResult = sprintf( '%.' . $intDecplaces . 'f', $intKbytes );
234
		$strResult .= $strSpacer . $text['kb'];
235
	}
236
 
237
	return $strResult;
238
}
239
 
240
function format_speed( $intHz ) {
241
	$strResult = "";
242
 
243
	if( $intHz < 1000 ) {
244
		$strResult = $intHz . " Mhz";
245
	} else {
246
		$strResult = round( $intHz / 1000, 2 ) . " GHz";
247
	}
248
 
249
	return $strResult;
250
}
251
 
252
function get_gif_image_height( $image ) { 
253
	// gives the height of the given GIF image, by reading it's LSD (Logical Screen Discriptor)
254
	// by Edwin Meester aka MillenniumV3
255
	// Header:
256
	//3bytes		Discription
257
	// 3bytes		Version
258
	// LSD:
259
	//2bytes		Logical Screen Width
260
	// 2bytes		Logical Screen Height
261
	// 1bit		Global Color Table Flag
262
	// 3bits		Color Resolution
263
	// 1bit		Sort Flag
264
	// 3bits		Size of Global Color Table
265
	// 1byte		Background Color Index
266
	// 1byte		Pixel Aspect Ratio
267
	// Open Image
268
	$fp = fopen( $image, 'rb' ); 
269
	// read Header + LSD
270
	$strHeaderandlsd = fread( $fp, 13 );
271
	fclose( $fp ); 
272
	// calc Height from Logical Screen Height bytes
273
	$intResult = ord( $strHeaderandlsd{8} ) + ord( $strHeaderandlsd{9} ) * 255;
274
 
275
	return $intResult;
276
} 
277
 
278
// Check if a string exist in the global $hide_mounts.
279
// Return true if this is the case.
280
function hide_mount( $strMount ) {
281
	global $hide_mounts;
282
 
283
	if( isset( $hide_mounts ) && is_array( $hide_mounts ) && in_array( $strMount, $hide_mounts ) ) {
284
		return true;
285
	} else {
286
		return false;
287
	}
288
}
289
 
290
// Check if a string exist in the global $hide_fstypes.
291
// Return true if this is the case.
292
function hide_fstype( $strFSType ) {
293
	global $hide_fstypes;
294
 
295
	if( isset( $hide_fstypes ) && is_array( $hide_fstypes ) && in_array( $strFSType, $hide_fstypes ) ) {
296
		return true;
297
	} else {
298
		return false;
299
	}
300
}
301
 
302
function uptime( $intTimestamp ) {
303
	global $text;
304
	$strUptime = '';
305
 
306
	$intMin = $intTimestamp / 60;
307
	$intHours = $intMin / 60;
308
	$intDays = floor( $intHours / 24 );
309
	$intHours = floor( $intHours - ( $intDays * 24 ) );
310
	$intMin = floor( $intMin - ( $intDays * 60 * 24 ) - ( $intHours * 60 ) );
311
 
312
	if( $intDays != 0 ) {
313
		$strUptime .= $intDays. "&nbsp;" . $text['days'] . "&nbsp;";
314
	}
315
	if( $intHours != 0 ) {
316
		$strUptime .= $intHours . "&nbsp;" . $text['hours'] . "&nbsp;";
317
	}
318
	$strUptime .= $intMin . "&nbsp;" . $text['minutes'];
319
 
320
	return $strUptime;
321
}
322
 
323
//Replace some chars which are not valid in xml with iso-8859-1 encoding
324
function replace_specialchars( &$strXml ) {
325
	$arrSearch = array( chr(174), chr(169), chr(228), chr(246), chr(252), chr(214), chr(220), chr(196) );
326
	$arrReplace = array( "(R)", "(C)", "ae", "oe", "ue", "Oe", "Ue", "Ae" );
327
	$strXml = str_replace( $arrSearch, $arrReplace, $strXml );
328
}
329
 
330
// find duplicate entrys and count them, show this value befor the duplicated name
331
function finddups( $arrInput ) {
332
	$arrResult = array();
333
 
334
	if( is_array( $arrInput ) ) {
335
		$arrBuffer = array_count_values( $arrInput );
336
		foreach( $arrBuffer as $strKey => $intValue) {
337
			if( $intValue > 1 ) {
338
				$arrResult[] = "(" . $intValue . "x) " . $strKey;
339
			} else {
340
				$arrResult[] = $strKey;
341
			}
342
		}
343
	}
344
 
345
	return $arrResult;
346
}
347
 
348
function rfts( $strFileName, $intLines = 0, $intBytes = 4096, $booErrorRep = true ) {
349
	global $error;
350
	$strFile = "";
351
	$intCurLine = 1;
352
 
353
	if( file_exists( $strFileName ) ) {
354
		if( $fd = fopen( $strFileName, 'r' ) ) {
355
			while( !feof( $fd ) ) {
356
				$strFile .= fgets( $fd, $intBytes );
357
				if( $intLines <= $intCurLine && $intLines != 0 ) {
358
					break;
359
				} else {
360
					$intCurLine++;
361
				}
362
			}
363
			fclose( $fd );
364
		} else {
365
			if( $booErrorRep ) {
366
				$error->addError( 'fopen(' . $strFileName . ')', 'file can not read by phpsysinfo', __LINE__, __FILE__ );
367
			}
368
			return "ERROR";
369
		}
370
	} else {
371
		if( $booErrorRep ) {
372
			$error->addError( 'file_exists(' . $strFileName . ')', 'the file does not exist on your machine', __LINE__, __FILE__ );
373
		}
374
		return "ERROR";
375
	}
376
 
377
	return $strFile;
378
}
379
 
380
function gdc( $strPath, $booErrorRep = true ) {
381
	global $error;
382
	$arrDirectoryContent = array();
383
 
384
	if( is_dir( $strPath ) ) {
385
		if( $handle = opendir( $strPath ) ) {
386
			while( ( $strFile = readdir( $handle ) ) !== false ) {
387
				if( $strFile != "." && $strFile != ".." && $strFile != "CVS" ) {
388
					$arrDirectoryContent[] = $strFile;
389
				}
390
			}
391
			closedir( $handle );
392
		} else {
393
			if( $booErrorRep ) {
394
				$error->addError( 'opendir(' . $strPath . ')', 'directory can not be read by phpsysinfo', __LINE__, __FILE__ );
395
			}
396
		}
397
	} else {
398
		if( $booErrorRep ) {
399
			$error->addError( 'is_dir(' . $strPath . ')', 'directory does not exist on your machine', __LINE__, __FILE__ );
400
		}
401
	}
402
 
403
	return $arrDirectoryContent;
404
}
405
 
406
function temperature( $floatTempC ) {
407
    global $temperatureformat, $text, $error;
408
    $strResult = "&nbsp;";
409
 
410
    switch( strtoupper( $temperatureformat ) ) {
411
	case "F":
412
	    $floatFahrenheit = $floatTempC * 1.8 + 32;
413
	    $strResult .= round( $floatFahrenheit ) . $text['degreeF'];
414
	    break;
415
	case "C":
416
	    $strResult .= round( $floatTempC ) . $text['degreeC'];
417
	    break;
418
	case "F-C":
419
	    $floatFahrenheit = $floatTempC * 1.8 + 32;
420
	    $strResult .= round( $floatFahrenheit ) . $text['degreeF'];
421
	    $strResult .= "&nbsp;(";
422
	    $strResult .= round( $floatTempC ) . $text['degreeC'];
423
	    $strResult .= ")";
424
	    break;
425
	case "C-F":
426
	    $floatFahrenheit = $floatTempC * 1.8 + 32;
427
	    $strResult .= round( $floatTempC ) . $text['degreeC'];
428
	    $strResult .= "&nbsp;(";
429
	    $strResult .= round( $floatFahrenheit ) . $text['degreeF'];
430
	    $strResult .= ")";
431
	    break;
432
	default:
433
	    $error->addError( 'temperature(' . $floatTempC . ')', 'wrong or unspecified temperature format', __LINE__, __FILE__ );
434
	    break;
435
    }
436
 
437
    return $strResult;
438
}
439
?>