PageRenderTime 69ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/frontends/php/include/func.inc.php

https://github.com/rennhak/zabbix
PHP | 636 lines | 422 code | 100 blank | 114 comment | 89 complexity | 1cb3509e4723a137ddbe5d0efe064ef3 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. <?php
  2. /*
  3. ** ZABBIX
  4. ** Copyright (C) 2000-2005 SIA Zabbix
  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 Free Software
  18. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. **/
  20. ?>
  21. <?php
  22. /************* DYNAMIC REFRESH *************/
  23. function add_doll_objects($ref_tab, $pmid='mainpage'){
  24. $upd_script = array();
  25. foreach($ref_tab as $id => $doll){
  26. $upd_script[$doll['id']] = format_doll_init($doll);
  27. }
  28. zbx_add_post_js('initPMaster('.zbx_jsvalue($pmid).','.zbx_jsvalue($upd_script).');');
  29. }
  30. function format_doll_init($doll){
  31. global $USER_DETAILS;
  32. $args = array('frequency' => 60,
  33. 'url' => '',
  34. 'counter' => 0,
  35. 'darken' => 0,
  36. 'params' => array()
  37. );
  38. foreach($args as $key => $def){
  39. if(isset($doll[$key])) $obj[$key] = $doll[$key];
  40. else $obj[$key] = $def;
  41. }
  42. $obj['url'].= (zbx_empty($obj['url'])?'?':'&').'output=html';
  43. $obj['params']['favobj'] = 'refresh';
  44. $obj['params']['favid'] = $doll['id'];
  45. return $obj;
  46. }
  47. function get_update_doll_script($pmasterid, $dollid, $key, $value=''){
  48. $script = 'PMasters['.zbx_jsvalue($pmasterid).'].dolls['.zbx_jsvalue($dollid).'].'.$key.'('.zbx_jsvalue($value).');';
  49. return $script;
  50. }
  51. function make_refresh_menu($pmid,$dollid,$cur_interval,$params=null,&$menu,&$submenu){
  52. $menu['menu_'.$dollid][] = array(S_REFRESH, null, null, array('outer'=> array('pum_oheader'), 'inner'=>array('pum_iheader')));
  53. $intervals = array('10','30','60','120','600','900');
  54. foreach($intervals as $key => $value){
  55. $menu['menu_'.$dollid][] = array(
  56. S_EVERY.SPACE.$value.SPACE.S_SECONDS_SMALL,
  57. 'javascript: setRefreshRate('.zbx_jsvalue($pmid).','.zbx_jsvalue($dollid).','.$value.','.zbx_jsvalue($params).');'.
  58. 'void(0);',
  59. null,
  60. array('outer' => ($value == $cur_interval)?'pum_b_submenu':'pum_o_submenu', 'inner'=>array('pum_i_submenu')
  61. ));
  62. }
  63. $submenu['menu_'.$dollid][] = array();
  64. }
  65. /************* END REFRESH *************/
  66. /************ REQUEST ************/
  67. function get_request($name, $def=NULL){
  68. if(isset($_REQUEST[$name]))
  69. return $_REQUEST[$name];
  70. else
  71. return $def;
  72. }
  73. function inarr_isset($keys, $array=null){
  74. if(is_null($array)) $array =& $_REQUEST;
  75. if(is_array($keys)){
  76. foreach($keys as $id => $key){
  77. if( !isset($array[$key]) )
  78. return false;
  79. }
  80. return true;
  81. }
  82. return isset($array[$keys]);
  83. }
  84. /************ END REQUEST ************/
  85. /************ COOKIES ************/
  86. /* function:
  87. * get_cookie
  88. *
  89. * description:
  90. * return cookie value by name,
  91. * if cookie is not present return $default_value.
  92. *
  93. * author: Eugene Grigorjev
  94. */
  95. function get_cookie($name, $default_value=null){
  96. if(isset($_COOKIE[$name])) return $_COOKIE[$name];
  97. // else
  98. return $default_value;
  99. }
  100. /* function:
  101. * zbx_setcookie
  102. *
  103. * description:
  104. * set cookies.
  105. *
  106. * author: Eugene Grigorjev
  107. */
  108. function zbx_setcookie($name, $value, $time=null){
  109. setcookie($name, $value, isset($time) ? $time : (0));
  110. $_COOKIE[$name] = $value;
  111. }
  112. /* function:
  113. * zbx_unsetcookie
  114. *
  115. * description:
  116. * unset and clear cookies.
  117. *
  118. * author: Aly
  119. */
  120. function zbx_unsetcookie($name){
  121. zbx_setcookie($name, null, -99999);
  122. unset($_COOKIE[$name]);
  123. }
  124. /* function:
  125. * zbx_flush_post_cookies
  126. *
  127. * description:
  128. * set posted cookies.
  129. *
  130. * author: Eugene Grigorjev
  131. */
  132. function zbx_flush_post_cookies($unset=false){
  133. global $ZBX_PAGE_COOKIES;
  134. if(isset($ZBX_PAGE_COOKIES)){
  135. foreach($ZBX_PAGE_COOKIES as $cookie){
  136. if($unset)
  137. zbx_unsetcookie($cookie[0]);
  138. else
  139. zbx_setcookie($cookie[0], $cookie[1], $cookie[2]);
  140. }
  141. unset($ZBX_PAGE_COOKIES);
  142. }
  143. }
  144. /* function:
  145. * zbx_set_post_cookie
  146. *
  147. * description:
  148. * set cookies after authorisation.
  149. * require calling 'zbx_flush_post_cookies' function
  150. * Called from:
  151. * a) in 'include/page_header.php'
  152. * b) from 'redirect()'
  153. *
  154. * author: Eugene Grigorjev
  155. */
  156. function zbx_set_post_cookie($name, $value, $time=null){
  157. global $ZBX_PAGE_COOKIES;
  158. $ZBX_PAGE_COOKIES[] = array($name, $value, isset($time)?$time:0);
  159. }
  160. /************ END COOKIES ************/
  161. /************* DATE *************/
  162. /* function:
  163. * zbx_date2str
  164. *
  165. * description:
  166. * Convert timestamp to string representation. Retun 'Never' if 0.
  167. *
  168. * author: Alexei Vladishev
  169. */
  170. function zbx_date2str($format, $timestamp){
  171. return ($timestamp==0)?S_NEVER:date($format,$timestamp);
  172. }
  173. /* function:
  174. * zbx_date2age
  175. *
  176. * description:
  177. * Calculate and convert timestamp to string representation.
  178. *
  179. * author: Aly
  180. */
  181. function zbx_date2age($start_date,$end_date=0,$utime = false){
  182. if(!$utime){
  183. $start_date=date('U',$start_date);
  184. if($end_date)
  185. $end_date=date('U',$end_date);
  186. else
  187. $end_date = time();
  188. }
  189. $time = abs($end_date-$start_date);
  190. //SDI($start_date.' - '.$end_date.' = '.$time);
  191. $years = (int) ($time / (365*86400));
  192. $time -= $years*365*86400;
  193. //$months = (int ) ($time / (30*86400));
  194. //$time -= $months*30*86400;
  195. $weeks = (int ) ($time / (7*86400));
  196. $time -= $weeks*7*86400;
  197. $days = (int) ($time / 86400);
  198. $time -= $days*86400;
  199. $hours = (int) ($time / 3600);
  200. $time -= $hours*3600;
  201. $minutes = (int) ($time / 60);
  202. $time -= $minutes*60;
  203. if($time >= 1){
  204. $seconds = round($time,2);
  205. $ms = 0;
  206. }
  207. else{
  208. $seconds = 0;
  209. $ms = round($time,3) * 1000;
  210. }
  211. $str = (($years)?$years.'y ':'').
  212. // (($months)?$months.'m ':'').
  213. (($weeks)?$weeks.'w ':'').
  214. (($days)?$days.'d ':'').
  215. (($hours && !$years)?$hours.'h ':'').
  216. (($minutes && !$years && !$weeks)?$minutes.'m ':'').
  217. ((!$years && !$weeks && !$days && (!$ms || $seconds))?$seconds.'s ':'').
  218. (($ms && !$years && !$weeks && !$days && !$hours)?$ms.'ms':'');return $str;
  219. }
  220. function getmicrotime(){
  221. list($usec, $sec) = explode(" ",microtime());
  222. return ((float)$usec + (float)$sec);
  223. }
  224. function getDateStringByType($type, $timestamp){
  225. $str = 'Wrong type';
  226. switch($type){
  227. case TIMEPERIOD_TYPE_HOURLY:
  228. $str = date('H:i', $timestamp);
  229. break;
  230. case TIMEPERIOD_TYPE_DAILY:
  231. $str = date('D H:i', $timestamp);
  232. break;
  233. case TIMEPERIOD_TYPE_WEEKLY:
  234. $str = S_WEEK.' '.date('W', $timestamp);
  235. break;
  236. case TIMEPERIOD_TYPE_MONTHLY:
  237. $str = date('M', $timestamp);
  238. break;
  239. case TIMEPERIOD_TYPE_YEARLY:
  240. $str = date('Y', $timestamp);
  241. break;
  242. }
  243. return $str;
  244. }
  245. /************* END DATE *************/
  246. /************* SORT *************/
  247. function natksort(&$array) {
  248. $keys = array_keys($array);
  249. natcasesort($keys);
  250. $new_array = array();
  251. foreach ($keys as $k) {
  252. $new_array[$k] = $array[$k];
  253. }
  254. $array = $new_array;
  255. return true;
  256. }
  257. function asort_by_key(&$array, $key){
  258. if(!is_array($array)) {
  259. error('Incorrect type of asort_by_key');
  260. return array();
  261. }
  262. $key = htmlspecialchars($key);
  263. uasort($array, create_function('$a,$b', 'return $a[\''.$key.'\'] - $b[\''.$key.'\'];'));
  264. return $array;
  265. }
  266. /* function:
  267. * zbx_rksort
  268. *
  269. * description:
  270. * Recursively sort an array by key
  271. *
  272. * author: Eugene Grigorjev
  273. */
  274. function zbx_rksort(&$array, $flags=NULL){
  275. if(is_array($array)){
  276. foreach($array as $id => $data)
  277. zbx_rksort($array[$id]);
  278. ksort($array,$flags);
  279. }
  280. return $array;
  281. }
  282. /************* END SORT *************/
  283. /*************** CONVERTING ******************/
  284. function rgb2hex($color){
  285. $HEX = array(
  286. dechex($color[0]),
  287. dechex($color[1]),
  288. dechex($color[2])
  289. );
  290. foreach($HEX as $id => $value){
  291. if(strlen($value) != 2) $HEX[$id] = '0'.$value;
  292. }
  293. return $HEX[0].$HEX[1].$HEX[2];
  294. }
  295. function zbx_num2bitstr($num,$rev=false){
  296. if(!is_numeric($num)) return 0;
  297. $sbin = 0;
  298. $strbin = '';
  299. $len = 32;
  300. if($num > 2147483647) $len = 64;
  301. for($i=0;$i<$len;$i++){
  302. $sbin= 1 << $i;
  303. $bit = ($sbin & $num)?'1':'0';
  304. if($rev){
  305. $strbin.=$bit;
  306. }
  307. else{
  308. $strbin = $bit.$strbin;
  309. }
  310. }
  311. return $strbin;
  312. }
  313. function empty2null($var){
  314. return ($var == "") ? null : $var;
  315. }
  316. function str2mem($val){
  317. $val = trim($val);
  318. $last = strtolower($val{strlen($val)-1});
  319. switch($last){
  320. // The 'G' modifier is available since PHP 5.1.0
  321. case 'g':
  322. $val *= 1024;
  323. case 'm':
  324. $val *= 1024;
  325. case 'k':
  326. $val *= 1024;
  327. }
  328. return $val;
  329. }
  330. function mem2str($size){
  331. $prefix = 'B';
  332. if($size > 1048576) { $size = $size/1048576; $prefix = 'M'; }
  333. elseif($size > 1024) { $size = $size/1024; $prefix = 'K'; }
  334. return round($size, 6).$prefix;
  335. }
  336. /* Do not forget to sync it with add_value_suffix in evalfunc.c! */
  337. function convert_units($value,$units){
  338. // Special processing for unix timestamps
  339. if($units=='unixtime'){
  340. $ret=date('Y.m.d H:i:s',$value);
  341. return $ret;
  342. }
  343. //Special processing of uptime
  344. if($units=='uptime'){
  345. $ret='';
  346. $days=floor($value/(24*3600));
  347. if($days>0){
  348. $value=$value-$days*(24*3600);
  349. }
  350. $hours=floor($value/(3600));
  351. if($hours>0){
  352. $value=$value-$hours*3600;
  353. }
  354. $min=floor($value/(60));
  355. if($min>0){
  356. $value=$value-$min*(60);
  357. }
  358. if($days==0){
  359. $ret = sprintf("%02d:%02d:%02d", $hours, $min, $value);
  360. }
  361. else{
  362. $ret = sprintf("%d days, %02d:%02d:%02d", $days, $hours, $min, $value);
  363. }
  364. return $ret;
  365. }
  366. // Special processing for seconds
  367. if($units=='s'){
  368. return zbx_date2age(0,$value,true);
  369. }
  370. $u='';
  371. // Special processing for bits (kilo=1000, not 1024 for bits)
  372. if( ($units=='b') || ($units=='bps')){
  373. $abs=abs($value);
  374. if($abs<1000){
  375. $u="";
  376. }
  377. else if($abs<1000*1000){
  378. $u='K';
  379. $value=$value/1000;
  380. }
  381. else if($abs<1000*1000*1000){
  382. $u='M';
  383. $value=$value/(1000*1000);
  384. }
  385. else{
  386. $u='G';
  387. $value=$value/(1000*1000*1000);
  388. }
  389. if(round($value) == round($value,2)){
  390. $s=sprintf('%.0f',$value);
  391. }
  392. else{
  393. $s=sprintf('%.2f',$value);
  394. }
  395. return "$s $u$units";
  396. }
  397. if($units==''){
  398. if(round($value) == round($value,2)){
  399. return sprintf('%.0f',$value);
  400. }
  401. else{
  402. return sprintf('%.2f',$value);
  403. }
  404. }
  405. $abs=abs($value);
  406. if($abs<1024){
  407. $u='';
  408. }
  409. else if($abs<1024*1024){
  410. $u='K';
  411. $value=$value/1024;
  412. }
  413. else if($abs<1024*1024*1024){
  414. $u='M';
  415. $value=$value/(1024*1024);
  416. }
  417. else if($abs<1024*1024*1024*1024){
  418. $u='G';
  419. $value=$value/(1024*1024*1024);
  420. }
  421. else{
  422. $u='T';
  423. $value=$value/(1024*1024*1024*1024);
  424. }
  425. if(round($value) == round($value,2)){
  426. $s=sprintf('%.0f',$value);
  427. }
  428. else{
  429. $s=sprintf('%.2f',$value);
  430. }
  431. return "$s $u$units";
  432. }
  433. /*************** END CONVERTING ******************/
  434. /************* ZBX MISC *************/
  435. // accepts parametr as integer either
  436. function zbx_ctype_digit($x){
  437. return preg_match('/^\\d+$/',$x);
  438. }
  439. function zbx_numeric($value){
  440. if(is_array($value)) return false;
  441. if(zbx_empty($value)) return false;
  442. $value = strval($value);
  443. return preg_match('/^[-|+]?\\d+$/',$value);
  444. }
  445. function zbx_empty($value){
  446. if(is_null($value)) return true;
  447. if(is_array($value) && empty($value)) return true;
  448. if(is_string($value) && ($value === '')) return true;
  449. return false;
  450. }
  451. function zbx_strlen(&$str){
  452. if(!$strlen = strlen($str)) return $strlen;
  453. $reallen = 0;
  454. $fbin= 1 << 7;
  455. $sbin= 1 << 6;
  456. // check first byte for 11xxxxxx or 0xxxxxxx
  457. for($i=0; $i < $strlen; $i++){
  458. if(((ord($str[$i]) & $fbin) && (ord($str[$i]) & $sbin)) || !(ord($str[$i]) & $fbin)) $reallen++;
  459. }
  460. return $reallen;
  461. }
  462. function zbx_strstr($haystack,$needle){
  463. $pos = strpos($haystack,$needle);
  464. if($pos !== FALSE){
  465. $pos = substr($haystack,$pos);
  466. }
  467. return $pos;
  468. }
  469. function zbx_stristr($haystack,$needle){
  470. $haystack_B = strtoupper($haystack);
  471. $needle = strtoupper($needle);
  472. $pos = strpos($haystack_B,$needle);
  473. if($pos !== FALSE){
  474. $pos = substr($haystack,$pos);
  475. }
  476. return $pos;
  477. }
  478. function zbx_substring($haystack, $start, $end=null){
  479. if($end < $start) return '';
  480. $len = zbx_strlen($haystack);
  481. if(is_null($end))
  482. $result = substr($haystack, $start);
  483. else
  484. $result = substr($haystack, $start, ($end - $start));
  485. return $result;
  486. }
  487. function zbx_str_revert(&$str){
  488. $result = '';
  489. $str_rep = str_split($str);
  490. foreach($str_rep as $num => $symb){
  491. $result = $symb.$result;
  492. }
  493. return $result;
  494. }
  495. function uint_in_array($needle,$haystack){
  496. foreach($haystack as $id => $value)
  497. if(bccomp($needle,$value) == 0) return true;
  498. return false;
  499. }
  500. function zbx_uint_array_intersect(&$array1, &$array2){
  501. $result = array();
  502. foreach($array1 as $key => $value)
  503. if(uint_in_array($value, $array2)) $result[$key] = $value;
  504. return $result;
  505. }
  506. function str_in_array($needle,$haystack,$strict=false){
  507. if(is_array($needle)){
  508. return in_array($needle,$haystack,$strict);
  509. }
  510. else if($strict){
  511. foreach($haystack as $id => $value)
  512. if($needle === $value) return true;
  513. }
  514. else{
  515. foreach($haystack as $id => $value)
  516. if(strcmp($needle,$value) == 0) return true;
  517. }
  518. return false;
  519. }
  520. function zbx_nl2br(&$str){
  521. $str_res = array();
  522. $str_arr = explode("\n",$str);
  523. foreach($str_arr as $id => $str_line){
  524. array_push($str_res,$str_line,BR());
  525. }
  526. return $str_res;
  527. }
  528. function zbx_value2array(&$values){
  529. if(!is_array($values) && !is_null($values)){
  530. $tmp = array();
  531. $tmp[$values] = $values;
  532. $values = $tmp;
  533. }
  534. }
  535. /************* END ZBX MISC *************/
  536. ?>