| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* xmlfunc.php -- Kannel's XML status output parsing functions. |
| 5 |
*/ |
| 6 |
|
| 7 |
function nf($number) { |
| 8 |
return number_format($number, 0, ",", "."); |
| 9 |
} |
| 10 |
|
| 11 |
function nfd($number) { |
| 12 |
return number_format($number, 2, ",", "."); |
| 13 |
} |
| 14 |
|
| 15 |
function get_timeout() { |
| 16 |
$refresh = intval($_REQUEST['refresh']); |
| 17 |
return ($refresh > 0) ? $refresh : DEFAULT_REFRESH; |
| 18 |
} |
| 19 |
|
| 20 |
function get_uptime($sec) { |
| 21 |
$d = floor($sec/(24*3600)); |
| 22 |
$sec -= ($d*24*3600); |
| 23 |
$h = floor($sec/3600); |
| 24 |
$sec -= ($h*3600); |
| 25 |
$m = floor($sec/60); |
| 26 |
$sec -= ($m*60); |
| 27 |
return sprintf("%dd %dh %dm %ds", $d, $h, $m, $sec); |
| 28 |
} |
| 29 |
|
| 30 |
function count_smsc_status($smscs) { |
| 31 |
$stats = array( |
| 32 |
'online' => 0, |
| 33 |
'disconnected' => 0, |
| 34 |
'connecting' => 0, |
| 35 |
're-connecting' => 0, |
| 36 |
'dead' => 0, |
| 37 |
'unknown' => 0 |
| 38 |
); |
| 39 |
foreach ($smscs as $smsc) { |
| 40 |
foreach ($stats as $st => $i) { |
| 41 |
if (substr($smsc['status'], 0, strlen($st)) == $st) { |
| 42 |
$stats[$st]++; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
return $stats; |
| 47 |
} |
| 48 |
|
| 49 |
function get_smscids($status, $smscs) { |
| 50 |
/* loop the smsc */ |
| 51 |
$n = ""; |
| 52 |
foreach ($smscs as $smsc) { |
| 53 |
if (substr($smsc['status'], 0, strlen($status)) == $status) { |
| 54 |
$n .= $smsc['admin-id']." "; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $n; |
| 59 |
} |
| 60 |
|
| 61 |
function format_status($st) { |
| 62 |
$span = 'text'; |
| 63 |
switch ($st) { |
| 64 |
case "online": |
| 65 |
$span = 'green'; |
| 66 |
break; |
| 67 |
case "disconnected": |
| 68 |
case "connecting": |
| 69 |
case "re-connecting": |
| 70 |
$span = 'red'; |
| 71 |
break; |
| 72 |
} |
| 73 |
return "<span class=\"$span\">$st</span>"; |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
* Parse start date, uptime and status from the status text |
| 78 |
*/ |
| 79 |
function parse_uptime($str) { |
| 80 |
$regs = array(); |
| 81 |
if (ereg("(.*), uptime (.*)d (.*)h (.*)m (.*)s", $str, $regs) || |
| 82 |
ereg("(.*) (.*)d (.*)h (.*)m (.*)s", $str, $regs)) { |
| 83 |
$ts = ($regs[2]*24*60*60) + ($regs[3]*60*60) + ($regs[4]*60) + $regs[5]; |
| 84 |
$bb_time[$inst] = mktime()-$ts; |
| 85 |
$started = date("Y-m-d H:i:s", mktime()-$ts); |
| 86 |
$uptime = sprintf("%dd %02d:%02d:%02d", $regs[2], $regs[3], $regs[4], $regs[5]); |
| 87 |
$status = $regs[1]; |
| 88 |
} else { |
| 89 |
$started = '-'; |
| 90 |
$uptime = '-'; |
| 91 |
$status = '-'; |
| 92 |
} |
| 93 |
return array($status, $started, $uptime); |
| 94 |
} |
| 95 |
|
| 96 |
/* |
| 97 |
* Create a link for the SMSC status with a detail popup |
| 98 |
*/ |
| 99 |
function make_link($smsc_status, $state, $mode='red') { |
| 100 |
global $status, $inst; |
| 101 |
if ($state == 'total') { |
| 102 |
return ($smsc_status > 0) ? "$smsc_status links":"none"; |
| 103 |
} elseif ($smsc_status[$state] == 0) { |
| 104 |
return "none"; |
| 105 |
} else { |
| 106 |
switch ($mode) { |
| 107 |
case 'red': |
| 108 |
return "<a href=\"#\" class=href onClick=\"do_alert('". |
| 109 |
"smsc-ids in $state state are\\n\\n". |
| 110 |
get_smscids($state, $status[$inst]['smscs']). |
| 111 |
"');\"><span class=red><b>". |
| 112 |
$smsc_status[$state]. |
| 113 |
"</b> links</span></a>"; |
| 114 |
break; |
| 115 |
case 'green': |
| 116 |
return "<span class=green><b>". |
| 117 |
$smsc_status[$state]. |
| 118 |
"</b> links</span></a>"; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* Split the load text into 3 <TD> |
| 126 |
*/ |
| 127 |
function split_load($str) { |
| 128 |
if (!$str) { |
| 129 |
return "<td>-</td><td>-</td><td>-</td>\n"; |
| 130 |
} else { |
| 131 |
return "<td>".implode("</td><td>", explode(",", $str))."</td>\n"; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* Create the admin link to change bearerbox status |
| 137 |
*/ |
| 138 |
function admin_link($mode) { |
| 139 |
global $config; |
| 140 |
return "<a class=\"href\" href=\"#\" onClick=\"admin_url('".$mode."', ". |
| 141 |
"'".$config["base_url"]."/".$mode."', '".$config["admin_passwd"]."');\">".$mode."</a>"; |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* Cleanup the whole array |
| 146 |
*/ |
| 147 |
function cleanup_array($arr) { |
| 148 |
if (is_array($arr) && is_array($arr['gateway'])) { |
| 149 |
$arr = $arr['gateway']; |
| 150 |
clean_branch($arr, 'wdp'); |
| 151 |
clean_branch($arr, 'sms'); |
| 152 |
clean_branch($arr, 'dlr'); |
| 153 |
$arr['boxes'] = $arr['boxes'][0]['box']; |
| 154 |
$arr['smscs'] = $arr['smscs'][0]['smsc']; |
| 155 |
} |
| 156 |
return $arr; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Cleanup the branches to fold unnecessary levels |
| 161 |
*/ |
| 162 |
function clean_branch(&$arr, $tag='') { |
| 163 |
$fields = array('received', 'sent'); |
| 164 |
if ($tag) { |
| 165 |
$arr[$tag] = array_shift($arr[$tag]); |
| 166 |
} |
| 167 |
foreach ($fields as $key) { |
| 168 |
if ($tag) { |
| 169 |
if (is_array($arr[$tag][$key])) { |
| 170 |
$arr[$tag][$key] = array_shift($arr[$tag][$key]); |
| 171 |
} |
| 172 |
} else { |
| 173 |
if (is_array($arr[$key])) { |
| 174 |
$arr[$key] = array_shift($arr[$key]); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* Get a path/of/xml/nodes from an array |
| 182 |
*/ |
| 183 |
function get_path($arr, $path) { |
| 184 |
$parts = explode("/", $path); |
| 185 |
if (!is_array($arr) || !is_array($parts)) { |
| 186 |
return $arr; |
| 187 |
} |
| 188 |
foreach($parts as $part) { |
| 189 |
$arr = $arr[$part]; |
| 190 |
} |
| 191 |
return $arr; |
| 192 |
} |
| 193 |
?> |