root/modules/branches/2.8/dashboard/phpsysinfo/common_functions.php

Revision 9635, 12.9 kB (checked in by p_lindheimer, 3 years ago)

fixes #4268 replace deprecated functions

Line 
1 <?php
2 // phpSysInfo - A PHP System Information Script
3 // http://phpsysinfo.sourceforge.net/
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 // This program 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 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15 // $Id: common_functions.php,v 1.42 2006/04/17 13:03:38 bigmichi1 Exp $
16 // HTML/XML Comment
17
18 // Copyright 2006 phpSysInfo
19
20 function created_by ()
21 {
22   global $VERSION;
23   return "<!--\n\tCreated By: phpSysInfo - $VERSION\n\thttp://phpsysinfo.sourceforge.net/\n-->\n\n";
24 }
25 // usefull during development
26 error_reporting(E_ALL | E_NOTICE);
27
28 // print out the bar graph
29 // $value as full percentages
30 // $maximim as current maximum
31 // $b as scale factor
32 // $type as filesystem type
33 function create_bargraph ($value, $maximum, $b, $type = "", $red_limit=90, $yellow_limit=75)
34 {
35   global $webpath;
36  
37   $textdir = direction();
38
39   $imgpath = $webpath . 'templates/' . TEMPLATE_SET . '/images/';
40   //$imgpath = 'modules/sysinfo/tinfo/templates/' . TEMPLATE_SET . '/images/';
41   $maximum == 0 ? $barwidth = 0 : $barwidth = round((100  / $maximum) * $value) * $b;
42   //$red = 90 * $b;
43   $red = $red_limit * $b;
44   //$yellow = 75 * $b;
45   $yellow = $yellow_limit * $b;
46
47   if (!file_exists(APP_ROOT . "/templates/" . TEMPLATE_SET . "/images/nobar_left.gif")) {
48     if ($barwidth == 0) {
49       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_' . $textdir['left'] . '.gif" alt="">'
50            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_middle.gif" width="1" alt="">'
51        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_' . $textdir['right'] . '.gif" alt="">';
52     } elseif (file_exists(APP_ROOT . "/templates/" . TEMPLATE_SET . "/images/yellowbar_left.gif") && $barwidth > $yellow && $barwidth < $red) {
53       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'yellowbar_' . $textdir['left'] . '.gif" alt="">'
54            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'yellowbar_middle.gif" width="' . $barwidth . '" alt="">'
55        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'yellowbar_' . $textdir['right'] . '.gif" alt="">';
56     } elseif (($barwidth < $red) || ($type == "iso9660") || ($type == "CDFS")) {
57       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_' . $textdir['left'] . '.gif" alt="">'
58            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_middle.gif" width="' . $barwidth . '" alt="">'
59        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_' . $textdir['right'] . '.gif" alt="">';
60     } else {
61       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_' . $textdir['left'] . '.gif" alt="">'
62            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_middle.gif" width="' . $barwidth . '" alt="">'
63        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_' . $textdir['right'] . '.gif" alt="">';
64     }
65   } else {
66     if ($barwidth == 0) {
67       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_' . $textdir['left'] . '.gif" alt="">'
68            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_middle.gif" width="' . (100 * $b) . '" alt="">'
69        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif" alt="">';
70     } elseif (file_exists(APP_ROOT . "/templates/" . TEMPLATE_SET . "/images/yellowbar_left.gif") && $barwidth > $yellow && $barwidth < $red) {
71       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'yellowbar_' . $textdir['left'] . '.gif" alt="">'
72            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'yellowbar_middle.gif" width="' . $barwidth . '" alt="">'
73        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_middle.gif" width="' . ((100 * $b) - $barwidth) . '" alt="">'
74        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif" alt="">';
75     } elseif (($barwidth < $red) || $type == "iso9660" || ($type == "CDFS")) {
76       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_' . $textdir['left'] . '.gif" alt="">'
77            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'bar_middle.gif" width="' . $barwidth . '" alt="">'
78        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_middle.gif" width="' . ((100 * $b) - $barwidth) . '" alt="">'
79        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif" alt="">';
80     } elseif ($barwidth == (100 * $b)) {
81       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_' . $textdir['left'] . '.gif" alt="">'
82            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_middle.gif" width="' . (100 * $b) . '" alt="">'
83        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_' . $textdir['right'] . '.gif" alt="">';
84     } else {
85       return '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_' . $textdir['left'] . '.gif" alt="">'
86            . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'redbar_middle.gif" width="' . $barwidth . '" alt="">'
87        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_middle.gif" width="' . ((100 * $b) - $barwidth) . '" alt="">'
88        . '<img height="' . BAR_HEIGHT . '" src="' . $imgpath . 'nobar_' . $textdir['right'] . '.gif" alt="">';
89     }
90   }
91 }
92
93 function create_bargraph_grad( $value, $maximum, $b, $type = "" ) {
94   global $webpath;
95
96   $maximum == 0 ? $barwidth = 0 : $barwidth = round((100  / $maximum) * $value);
97   $startColor = '0ef424'; // green
98   $endColor = 'ee200a'; // red
99   if ($barwidth > 100) {
100     $barwidth = 0;
101   }
102
103   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="">';
104 }
105
106 function direction() {
107   global $text_dir;
108
109   if(!isset($text_dir) || $text_dir == "ltr") {
110     $result['direction'] = "ltr";
111     $result['left'] = "left";
112     $result['right'] = "right";
113   } else {
114     $result['direction'] = "rtl";
115     $result['left'] = "right";
116     $result['right'] = "left";
117   }
118  
119   return $result;
120 }
121
122 // Find a system program.  Do path checking
123 function find_program ($program)
124 {
125   global $addpaths;
126
127   $path = array('/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
128
129   if( isset($addpaths) && is_array($addpaths) ) {
130     $path = array_merge( $path, $addpaths );
131   }
132
133   if (function_exists("is_executable")) {
134     while ($this_path = current($path)) {
135       if (is_executable("$this_path/$program")) {
136         return "$this_path/$program";
137       }
138       next($path);
139     }
140   } else {
141     return strpos($program, '.exe');
142   } ;
143
144   return;
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 ($programname, $args = '', $booErrorRep = true )
151 {
152   global $error;
153   $buffer = '';
154   $program = find_program($programname);
155
156   if (!$program) {
157     if( $booErrorRep ) {
158       $error->addError( 'find_program(' . $programname . ')', 'program not found on the machine', __LINE__, __FILE__);
159     }
160     return;
161   }
162  
163   // see if we've gotten a |, if we have we need to do patch checking on the cmd
164   if ($args) {
165     $args_list = preg_split('/\s/', $args);
166     for ($i = 0; $i < count($args_list); $i++) {
167       if ($args_list[$i] == '|') {
168         $cmd = $args_list[$i + 1];
169         $new_cmd = find_program($cmd);
170         $args = preg_replace("/\| $cmd/", "| $new_cmd", $args);
171       }
172     }
173   }
174   // we've finally got a good cmd line.. execute it
175   if ($fp = popen("($program $args > /dev/null) 3>&1 1>&2 2>&3", 'r')) {
176     while (!feof($fp)) {
177       $buffer .= fgets($fp, 4096);
178     }
179     pclose($fp);
180     $buffer = trim($buffer);
181     if (!empty($buffer)) {
182       if( $booErrorRep ) {
183         $error->addError( $program, $buffer, __LINE__, __FILE__);
184       }
185     }
186   }
187   if ($fp = popen("$program $args", 'r')) {
188     $buffer = "";
189     while (!feof($fp)) {
190       $buffer .= fgets($fp, 4096);
191     }
192     pclose($fp);
193   }
194   $buffer = trim($buffer);
195   return $buffer;
196 }
197
198 // A helper function, when passed a number representing KB,
199 // and optionally the number of decimal places required,
200 // it returns a formated number string, with unit identifier.
201 function format_bytesize ($kbytes, $dec_places = 2)
202 {
203   global $text;
204   $spacer = '&nbsp;';
205   if ($kbytes > 1048576) {
206     $result = sprintf('%.' . $dec_places . 'f', $kbytes / 1048576);
207     $result .= $spacer . $text['gb'];
208   } elseif ($kbytes > 1024) {
209     $result = sprintf('%.' . $dec_places . 'f', $kbytes / 1024);
210     $result .= $spacer . $text['mb'];
211   } else {
212     $result = sprintf('%.' . $dec_places . 'f', $kbytes);
213     $result .= $spacer . $text['kb'];
214   }
215   return $result;
216 }
217
218 function get_gif_image_height($image)
219 {
220   // gives the height of the given GIF image, by reading it's LSD (Logical Screen Discriptor)
221   // by Edwin Meester aka MillenniumV3
222   // Header: 3bytes     Discription
223   // 3bytes     Version
224   // LSD:        2bytes     Logical Screen Width
225   // 2bytes     Logical Screen Height
226   // 1bit         Global Color Table Flag
227   // 3bits   Color Resolution
228   // 1bit        Sort Flag
229   // 3bits        Size of Global Color Table
230   // 1byte        Background Color Index
231   // 1byte        Pixel Aspect Ratio
232   // Open Image
233   $fp = fopen($image, 'rb');
234   // read Header + LSD
235   $header_and_lsd = fread($fp, 13);
236   fclose($fp);
237   // calc Height from Logical Screen Height bytes
238   $result = ord($header_and_lsd{8}) + ord($header_and_lsd{9}) * 255;
239   return $result;
240 }
241
242 // Check if a string exist in the global $hide_mounts.
243 // Return true if this is the case.
244 function hide_mount($mount) {
245     global $hide_mounts;
246     if (isset($hide_mounts) && is_array($hide_mounts) && in_array($mount, $hide_mounts)) {
247         return true;
248     }
249     else {
250         return false;
251     }
252 }
253
254 function uptime($timestamp) {
255   global $text;
256   $uptime = '';
257     
258   $min = $timestamp / 60;
259   $hours = $min / 60;
260   $days = floor($hours / 24);
261   $hours = floor($hours - ($days * 24));
262   $min = floor($min - ($days * 60 * 24) - ($hours * 60));
263
264   if ($days != 0) {
265     $uptime .= $days. "&nbsp;" . $text['days'] . "&nbsp;";
266   }
267
268   if ($hours != 0) {
269     $uptime .= $hours . "&nbsp;" . $text['hours'] . "&nbsp;";
270   }
271  
272   $uptime .= $min . "&nbsp;" . $text['minutes'];
273   return $uptime;
274 }
275
276 //Replace some chars which are not valid in xml with iso-8859-1 encoding
277 function replace_specialchars(&$xmlstring) {
278     $xmlstring = str_replace( chr(174), "(R)", $xmlstring );
279     $xmlstring = str_replace( chr(169), "(C)", $xmlstring );
280 }
281
282 // find duplicate entrys and count them, show this value befor the duplicated name
283 function finddups( $arrInput ) {
284   $result = array();
285   $buffer = array_count_values($arrInput);
286   foreach ($buffer as $key => $value) {
287     if( $value > 1 ) {
288       $result[] = "(" . $value . "x) " . $key;
289     } else {
290       $result[] = $key;
291     }
292   }
293   return $result;
294 }
295
296 function rfts( $strFileName, $intLines = 0, $intBytes = 4096, $booErrorRep = true ) {
297     global $error;
298     $strFile = "";
299     $intCurLine = 1;
300  
301     if( file_exists( $strFileName ) ) {
302         if( $fd = fopen( $strFileName, 'r' ) ) {
303             while( !feof( $fd ) ) {
304                 $strFile .= fgets( $fd, $intBytes );
305                 if( $intLines <= $intCurLine && $intLines != 0 ) {
306                     break;
307                 } else {
308                     $intCurLine++;
309                 }
310             }
311             fclose( $fd );
312         } else {
313             if( $booErrorRep ) {
314                 $error->addError( 'fopen(' . $strFileName . ')', 'file can not read by phpsysinfo', __LINE__, __FILE__ );
315             }
316         return "ERROR";
317     }
318     } else {
319         if( $booErrorRep ) {
320             $error->addError( 'file_exists(' . $strFileName . ')', 'the file does not exist on your machine', __LINE__, __FILE__ );
321         }
322         return "ERROR";
323     }
324     return $strFile;
325 }
326
327 function gdc( $strPath, $booErrorRep = true ) {
328   global $error;
329   $arrDirectoryContent = array();
330  
331   if( is_dir( $strPath ) ) {
332     if( $handle = opendir( $strPath ) ) {
333       while( ( $strFile = readdir( $handle ) ) !== false ) {
334         if( $strFile != "." && $strFile != ".." && $strFile != "CVS" ) {
335           $arrDirectoryContent[] = $strFile;
336     }
337       }
338       closedir( $handle );
339     } else {
340       if( $booErrorRep ) {
341         $error->addError( 'opendir(' . $strPath . ')', 'directory can not be read by phpsysinfo', __LINE__, __FILE__ );
342       }
343     }
344   } else {
345     if( $booErrorRep ) {
346       $error->addError( 'is_dir(' . $strPath . ')', 'directory does not exist on your machine', __LINE__, __FILE__ );
347     }
348   }
349  
350   return $arrDirectoryContent;
351 }
352 ?>
353
Note: See TracBrowser for help on using the browser.