PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/System/includes/common_functions.php

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