1<? 2/* 3 * (C) Copyright 2003 Krzysztof Gibas <sdas (at) gibas org> 4 * - current maintainer 5 * 2001 Gonthar <gonthar (at) moon net pl> 6 * - developed scr_ipfm-0.1 7 * 8 * scr_ipfm generates graphical statistics from ipfm logs. 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 23 * USA. 24 * 25 * $Id: functions.php,v 1.82 2003/11/13 17:04:48 gibas Exp $ 26 */ 27 28function calc($bytes = "0") { 29 $unit = 0; 30 31 while($bytes > 1024) { 32 $bytes /= 1024; 33 $unit++; 34 } 35 36 $bytes = round($bytes, 2); 37 38 switch($unit) { 39 case "0": 40 $unit = "b"; 41 break; 42 case "1": 43 $unit = "kB"; 44 break; 45 case "2": 46 $unit = "MB"; 47 break; 48 case "3": 49 $unit = "GB"; 50 break; 51 case "4": 52 $unit = "TB"; 53 break; 54 default: 55 $unit = "MB"; 56 break; 57 } 58 return("$bytes $unit"); 59} 60 61function invert_flag($flag) { 62 if ($flag == "ASC") { 63 $flag = "DESC"; 64 } 65 else { 66 $flag = "ASC"; 67 } 68 return $flag; 69} 70 71function sortbycol($in, $s, $flag) { 72 $tmp = ""; 73 74 for($i = 0; $i < count($in); $i++) { 75 $ex = preg_split("/\s+/", $in[$i]); 76 $tmp[] = $ex[$s]; 77 } 78 if ($tmp) { 79 array_multisort($tmp, $flag, $in); 80 unset($tmp); 81 } 82 return $in; 83} 84 85 86function calc_scale($part, $s) { 87 $parts = preg_split("/\s+/", $part); 88 89 $scale = 0; 90 $scale = $parts[$s] / 800; 91 return $scale; 92} 93 94function check_dir_exists($dir = "") { 95 if (!is_dir($dir)) { 96 echo "<br /><p class=\"error\">directory: $dir - Bad data submitted. This directory doesn't exists. Check it in conf.php.</p><br />"; 97 exit(); 98 } 99} 100 101function check_file_exists($file = "", $what = "") { 102 if(!file_exists($file)) { 103 echo "<br /><p class=\"error\">file: $file - Bad data submitted. Logs for that $what don't exists.</p><br />"; 104 exit(); 105 } 106} 107 108function create_ar_years() { 109 global $ipfm_log_dir; 110 global $name; 111 112 $ar_years = array(); 113 114 if ($handle = opendir("$ipfm_log_dir")) { 115 while (false != ($file = readdir($handle))) { 116 if ($file != "." && $file != ".." && $file != "ipfm-global.log") { 117 $var = split("\.", $file); 118 $name = split('-', $var[0]); 119 120 if (stristr("$file", "ipfm-global-")) { 121 if(!in_array($name[2], $ar_years)) { 122 array_push($ar_years, "$name[2]"); 123 } 124 } 125 } 126 } 127 } 128 closedir($handle); 129 asort($ar_years); 130 131 return($ar_years); 132} 133 134function create_ar_months() { 135 global $ipfm_log_dir; 136 global $ar_years; 137 global $name; 138 139 $ar_months = array(); 140 141 if ($handle = opendir("$ipfm_log_dir")) { 142 while (false != ($file = readdir($handle))) { 143 if ($file != "." && $file != ".." && $file != "ipfm-global.log") { 144 $var = split("\.", $file); 145 $name = split('-', $var[0]); 146 147 if (isset($_GET['year'])) { 148 if (stristr("$file", "ipfm-global-" . $_GET['year'] . "-")) { 149 if(!in_array($name[3], $ar_months)) { 150 array_push($ar_months, "$name[3]"); 151 } 152 } 153 } 154 else { 155 if (stristr("$file", "ipfm-global-" . end($ar_years) . "-")) { 156 if(!in_array($name[3], $ar_months)) { 157 array_push($ar_months, "$name[3]"); 158 } 159 } 160 } 161 } 162 } 163 } 164 closedir($handle); 165 asort($ar_months); 166 167 return($ar_months); 168} 169 170function create_ar_days() { 171 global $ipfm_log_dir; 172 global $ar_years; 173 global $ar_months; 174 global $name; 175 176 $ar_days = array(); 177 178 if ($handle = opendir("$ipfm_log_dir")) { 179 while (false != ($file = readdir($handle))) { 180 if ($file != "." && $file != ".." && $file != "ipfm-global.log") { 181 $var = split("\.", $file); 182 $name = split('-', $var[0]); 183 184 if (isset($_GET['year']) && isset($_GET['month'])) { 185 if (stristr("$file", "ipfm-global-" . $_GET['year'] . "-" . $_GET['month'] . "-")) { 186 if(!in_array($name[4], $ar_days)) { 187 array_push($ar_days, "$name[4]"); 188 } 189 } 190 } 191 elseif (isset($_GET['year']) && !isset($_GET['month'])) { 192 if (stristr("$file", "ipfm-global-" . $_GET['year'] . "-" . end($ar_months) . "-")) { 193 array_push($ar_days, "$name[4]"); 194 } 195 } 196 else { 197 if (stristr("$file", "ipfm-global-" . end($ar_years) . "-" . end($ar_months) . "-")) { 198 if(!in_array($name[4], $ar_days)) { 199 array_push($ar_days, "$name[4]"); 200 } 201 } 202 } 203 } 204 } 205 } 206 closedir($handle); 207 asort($ar_days); 208 209 return($ar_days); 210} 211 212function set_style_flag_s() { 213 global $host_style; 214 global $in_style; 215 global $out_style; 216 global $total_style; 217 global $host_flag; 218 global $in_flag; 219 global $out_flag; 220 global $total_flag; 221 global $s; 222 223 $host_style = $in_style = $out_style = $total_style = "out"; 224 225 $host_flag = $in_flag = $out_flag = $total_flag = $_GET['flag']; 226 227 switch($_GET['sort']) { 228 case 'host': 229 $s = "0"; 230 $host_style = "in"; 231 $host_flag = invert_flag($host_flag); 232 break; 233 234 case 'in': 235 $s = "1"; 236 $in_style = "in"; 237 $in_flag = invert_flag($in_flag); 238 break; 239 240 case 'out': 241 $s = "2"; 242 $out_style = "in"; 243 $out_flag = invert_flag($out_flag); 244 break; 245 246 case 'total': 247 $s = "3"; 248 $total_style = "in"; 249 $total_flag = invert_flag($total_flag); 250 break; 251 252 default: 253 $s = "3"; 254 $total_style = "in"; 255 $total_flag = invert_flag($total_flag); 256 } 257} 258 259 260function secure_sort() { 261 if (!isset($_GET['sort']) or is_numeric($_GET['sort']) or strlen($_GET['sort']) == 0) { 262 $_GET['sort'] = "total"; 263 } 264 else { 265 $_GET['sort'] = addslashes($_GET['sort']); 266 } 267} 268 269function secure_flag() { 270 if (!isset($_GET['flag']) or is_numeric($_GET['flag']) or strlen($_GET['flag']) == 0) { 271 $_GET['flag'] = "ASC"; 272 } 273 else { 274 $_GET['flag'] = addslashes($_GET['flag']); 275 } 276} 277 278function secure_host() { 279 if (!isset($_GET['host']) or strlen($_GET['host']) == 0) { 280 $_GET['host'] = ""; 281 } 282 else { 283 $_GET['host'] = addslashes($_GET['host']); 284 } 285} 286 287function secure_year() { 288 global $ar_years; 289 290 if (!isset($_GET['year']) or !is_numeric($_GET['year']) or strlen($_GET['year']) == 0) { 291 $_GET['year'] = end($ar_years); 292 } 293 else { 294 $_GET['year'] = addslashes($_GET['year']); 295 } 296} 297 298function secure_month() { 299 global $ar_months; 300 301 if (!isset($_GET['month']) or !is_numeric($_GET['month']) or strlen($_GET['month']) == 0) { 302 $_GET['month'] = end($ar_months); 303 } 304 else { 305 $_GET['month'] = addslashes($_GET['month']); 306 } 307} 308 309function secure_day() { 310 global $ar_days; 311 312 if (!isset($_GET['day']) or !is_numeric($_GET['day']) or strlen($_GET['day']) == 0) { 313 $_GET['day'] = end($ar_days); 314 } 315 else { 316 $_GET['day'] = addslashes($_GET['day']); 317 } 318} 319 320function set_table_bg_style($element) { 321 322 $element = calc($element); 323 324 if(stristr($element, "B")) { 325 $style = "table_bg_b"; 326 } 327 if(stristr($element, "kB")) { 328 $style = "table_bg_kb"; 329 } 330 if(stristr($element, "MB")) { 331 $style = "table_bg_mb"; 332 } 333 if(stristr($element, "GB")) { 334 $style = "table_bg_gb"; 335 } 336 if(stristr($element, "TB")) { 337 $style = "table_bg_tb"; 338 } 339 return $style; 340} 341 342function theme_file($file = "default") { 343 $file = addslashes($file); 344 345 if (file_exists("themes/$file/style.css")) { 346 $theme = "themes/$file/style.css"; 347 return $theme; 348 } 349} 350function html_make_header_en($auto_refresh = "", $header="", $version="") { 351 global $theme; 352 353 echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?> 354 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"> 355 <html xmlns=\"http://www.w3.org/1999/xhtml\"> 356 <head> 357 <meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" /> 358 <meta http-equiv=\"content-language\" content=\"en\" /> 359 <meta name=\"description\" content=\"scr_ipfm php stats\" /> 360 <meta name=\"robots\" content=\"index\" />"; 361 362 if (isset($auto_refresh) && strlen($auto_refresh) > 0) { 363 $url = $_SERVER['PHP_SELF']; 364 echo "<meta http-equiv=\"refresh\" content=\"" . $auto_refresh . "; url=" . $url . "\" />"; 365 } 366 367 echo " 368 <title>$header generated by scr_ipfm-$version</title> 369 <link href=\"" . theme_file($theme). "\" rel=\"stylesheet\" type=\"text/css\" /> 370 </head> 371 <body> 372 "; 373} 374 375function html_make_sum($sum_in = 0, $sum_out = 0, $sum_total = 0) { 376 echo " 377 <tr class=\"tablemainsum\"> 378 <td align=\"left\" colspan=\"2\">Sum:</td> 379 <td align=\"right\">" . calc($sum_in) . "</td> 380 <td align=\"right\">" . calc($sum_out) . "</td> 381 <td align=\"right\">" . calc($sum_total) . "</td> 382 </tr> 383 </tbody> 384 </table>"; 385} 386 387function html_make_links_year($type = "daily") { 388 global $ar_years; 389 390 if (isset($_GET['host'])) { 391 $type = "host=" . $_GET['host']; 392 } 393 else { 394 $type = $type . "=1"; 395 } 396 397 echo "Year: "; 398 399 foreach($ar_years as $value) { 400 if($value == $_GET['year']) { 401 echo "<a class=\"linkred\" href=\"index.php?sort=" . $_GET['sort'] . "&flag=". $_GET['flag'] . "&" . $type . 402 "&year=" . $value . "\">$value</a> "; 403 } 404 else { 405 echo "<a class=\"link\" href=\"index.php?sort=" . $_GET['sort'] . "&flag=". $_GET['flag'] . "&" . $type . 406 "&year=" . $value . "\">$value</a> "; 407 } 408 } 409} 410 411function html_make_links_month($type = "daily") { 412 global $ar_months; 413 414 if (isset($_GET['host'])) { 415 $type = "host=" . $_GET['host']; 416 } 417 else { 418 $type = $type . "=1"; 419 } 420 421 echo "<br />Month: "; 422 423 foreach($ar_months as $value) { 424 if($value == $_GET['month']) { 425 echo "<a class=\"linkred\" href=\"index.php?sort=" . $_GET['sort'] . "&flag=". $_GET['flag'] . "&" . $type . 426 "&year=" . $_GET['year'] . "&month=" . $value . "\">$value</a> "; 427 } 428 else { 429 echo "<a class=\"link\" href=\"index.php?sort=" . $_GET['sort'] . "&flag=". $_GET['flag'] . "&" . $type . 430 "&year=" . $_GET['year'] . "&month=" . $value . "\">$value</a> "; 431 } 432 } 433} 434 435function html_make_links_day($type = "daily") { 436 global $ar_days; 437 438 if (isset($_GET['host'])) { 439 $type = "host=" . $_GET['host']; 440 } 441 else { 442 $type = $type . "=1"; 443 } 444 445 echo "<br />Day: "; 446 447 foreach($ar_days as $value) { 448 if($value == $_GET['day']) { 449 echo "<a class=\"linkred\" href=\"index.php?sort=" . $_GET['sort'] . "&flag=". $_GET['flag'] . "&" . $type . 450 "&year=" . $_GET['year'] . "&month=" . $_GET['month'] . "&day=" . $value . "\">$value</a> "; 451 } 452 else { 453 echo "<a class=\"link\" href=\"index.php?sort=" . $_GET['sort'] . "&flag=". $_GET['flag'] . "&" . $type . 454 "&year=" . $_GET['year'] . "&month=" . $_GET['month'] . "&day=" . $value . "\">$value</a> "; 455 } 456 } 457} 458 459function html_make_links($colored = "daily") { 460 461 $class_d = "link"; 462 $class_dim = "link"; 463 $class_m = "link"; 464 $class_miy = "link"; 465 $class_y = "link"; 466 $class_yig = "link"; 467 $class_g = "link"; 468 469 470 switch($colored) { 471 case 'daily': 472 $class_d = "linkred"; 473 break; 474 case 'dayinmonth': 475 $class_dim = "linkred"; 476 break; 477 case 'monthly': 478 $class_m = "linkred"; 479 break; 480 case 'monthinyear': 481 $class_miy = "linkred"; 482 break; 483 case 'global': 484 $class_g = "linkred"; 485 break; 486 case 'yearly': 487 $class_y = "linkred"; 488 break; 489 case 'yearinglobal': 490 $class_yig = "linkred"; 491 break; 492 } 493 494 echo " 495 <table class=\"makelinks\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\"> 496 <tbody> 497 <tr> 498 <td align=\"right\">Statistics: [ 499 <a class=\"$class_d\" href=\"index.php?daily=1\">daily</a> | 500 <a class=\"$class_dim\" href=\"index.php?dayinmonth=1\">day-in-month</a> | 501 <a class=\"$class_m\" href=\"index.php?monthly=1\">monthly</a> | 502 <a class=\"$class_miy\" href=\"index.php?monthinyear=1\">month-in-year</a> | 503 <a class=\"$class_y\" href=\"index.php?yearly=1\">yearly</a> | 504 <a class=\"$class_yig\" href=\"index.php?yearinglobal=1\">year-in-global</a> | 505 <a class=\"$class_g\" href=\"index.php?global=1\">global</a> ] 506 </td> 507 </tr> 508 </tbody> 509 </table>"; 510} 511 512function html_make_links_graph($colored = "daily") { 513 514 $class_d = "link"; 515 $class_m = "link"; 516 517 switch($colored) { 518 case 'daily': 519 $class_d = "linkred"; 520 break; 521 case 'monthly': 522 $class_m = "linkred"; 523 break; 524 } 525 526 // <a class=\"$class_d\" href=\"index.php?daily=1\">vertical</a> | 527 // <a class=\"$class_m\" href=\"index.php?monthly=1\">horizontal</a> ] 528 echo " 529 <table class=\"makelinks\"> 530 <tbody> 531 <tr> 532 <td align=\"right\"> 533 [ vertical | horizontal ] 534 </td> 535 </tr> 536 </tbody> 537 </table>"; 538} 539 540function html_make_result($style, $num = 0, $host = "", $in = 0, $out = 0, $total = 0) { 541 542 //echo "<tr onmouseover=\"this.bgColor = '". "$table_bg_highlight_color" . "'\" onmouseout =\"this.bgColor = '" . 543 // "$table_bg_color" . "'\" bgcolor=\"$table_bg_color\"> 544 echo "<tr class=\"$style\"> 545 <td>$num.</td> 546 <td align=\"left\"><a class=\"link\" href=\"index.php?host=$host\">$host</a></td> 547 <td align=\"right\">" . calc($in) . "</td> 548 <td align=\"right\">" . calc($out) . "</td> 549 <td align=\"right\">". calc($total) ."</td> 550 </tr>"; 551} 552 553function html_make_result_host($style, $num = 0, $host = "", $in = 0, $out = 0, $total = 0) { 554 555 //echo "<tr onmouseover=\"this.bgColor = '". "$table_bg_highlight_color" . "'\" onmouseout =\"this.bgColor = '" . 556 // "$table_bg_color" . "'\" bgcolor=\"$table_bg_color\"> 557 558 echo "<tr class=\"$style\"> 559 <td>$num.</td> 560 <td align=\"left\">$host</td> 561 <td align=\"right\">" . calc($in) . "</td> 562 <td align=\"right\">" . calc($out) . "</td> 563 <td align=\"right\">". calc($total) ."</td> 564 </tr>"; 565} 566 567function html_make_version_table($ipfm = "", $date = "", $freq = "", $int = "", $ver = "") { 568 569 echo " 570 <table class=\"tablebottom\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\"> 571 <tbody> 572 <tr class=\"tablebottom\"> 573 <td> 574 ipfm version: $ipfm -- stats for: $date -- frequency: $freq -- interface $int<br /> 575 Stats generated by <a class=\"link\" href=\"http://scripfm.sf.net/\">scr_ipfm v. $ver</a> 576 </td> 577 </tr> 578 </tbody> 579 </table>"; 580} 581 582function html_make_empty_table() { 583 echo "<table><tbody><tr><td> </td></tr></tbody></table>"; 584} 585 586function html_make_footer() { 587 echo "</body></html>"; 588} 589 590function html_graph_horizon($host, $total, $total_scaled) { 591 echo "<table cellspacing=\"0\" cellpadding=\"0\"> 592 <tbody> 593 <tr> 594 <td>$host - " . calc($total) . "</td> 595 </tr> 596 </tbody> 597 </table> 598 599 <table cellspacing=\"0\" cellpadding=\"0\"> 600 <tbody> 601 <tr> 602 <td background=\"images/wykres-poziom-first.png\" width=\"2\" height=\"22\"></td> 603 <td background=\"images/wykres-poziom.png\" width=\"$total_scaled\" height=\"22\"></td> 604 <td background=\"images/wykres-poziom-end.png\" width=\"1\" height=\"22\"></td> 605 </tr> 606 </tbody> 607 </table>"; 608} 609 610?> 611