Source for file common_functions.php

Documentation is available at common_functions.php

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

Documentation generated on Mon, 06 Feb 2012 01:10:27 +0100 by phpDocumentor 1.4.0