Leases(); } function getLease() { /* Will return the lease array for the lease of the index that the iterator is currently on. */ if($this->leaseIterator == -1) $this->leaseIterator++; return $this->leases[$this->leaseIterator]; } function nextActive() { /* Will retrieve the next value in the $leases array that contains an active lease. If the last active lease has already been returned by a previous call, the returned value will be false. Pre: Leasefile must have already been processed by readLease(). Post: Returns an associative array containing all properties of the active lease. */ while($lease = $this->nextLease()) { if($lease["status"] === "active") return $lease; } return false; } function nextLease() { /* Will retrieve the next lease in the $leases array. Acts as an array iterator. If the iterator is set to the last index when called, false is returned. Pre: Leasefile must have been processed by readLease(). Post: Returns an associative array containing the properties of the next lease */ if($this->leaseIterator < count($this->leases)) return $this->leases[++$this->leaseIterator]; else return false; } function readLease($filename) { /* Opens a lease file, splits it into individual leases, then stores the results of each lease into the $leases array. Pre: Requires a valid dhcpd.leases file as parameter Post: $leases contains an array of all leases in file */ if(!($leaseFile = fopen($filename, "r"))) { print("ERROR: "); print("'$filename' could not be opened for reading\n"); exit; } while(!feof($leaseFile)) { $line = fgets($leaseFile, 1024); // Skips lines that don't matter if(preg_match("/^(\#)/", $line)) { continue; } if(preg_match("/^(})/", $line)) { $this->leases[] = $this->parseLease($entry); unset($entry); continue; } $entry[] = $line; } // End of while fclose($leaseFile); return; } function parseLease($rgLease) { /* Parses the one lease statement. Pre: Input should be an array containing each line of lease definition starting with lease statement, ending with line before closing '}' Post: Output should be an associative array with each key containing the keyword in each line of the lease statement. */ foreach($rgLease as $line){ // Skips lines that don't matter if(preg_match("/^(\#|\})/x", $line)) { continue; } if(preg_match("/^lease\s+(?!0)((?:(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d\d?|2[0-4]\d|25[0-5]))\s+\{/x", $line, $matches)) { $lease["ip_addr"] = $matches[1]; continue; } // Split the keywords from the values preg_match("/^\s+([a-z -]+[a-z-]+)\s*\"?(.+?)?\"?\;/x", $line, $matches); $key = $matches[1]; $value = $matches[2]; if($key === "hardware ethernet") { $key = "mac"; $value = strtoupper($value); } else if($key === "ends never") { $lease["ends"] = "never"; continue; } else if ($key === "starts" || $key === "ends") { // Need to trim the begining to put date into YYYY/MM/DD HH:MM:SS format $value = strtotime(substr($value,2)); if($key === "ends" && $value <= time()) $lease["status"] = "inactive"; else $lease["status"] = "active"; } else if ($key === "client-hostname") $key = "hostname"; if(isset($key) && isset($value)) $lease[$key] = $value; } // end of foreach return $lease; } // End of function } // End of class /* Example code on how to use this class */ /* Will produce a table of all known leases, active or not */ /* Use $test->nextActive() in place of $test->nextLease() for only active leases */ $test = new Leases; $test->readLease("/var/lib/dhcp/dhcpd.leases"); // BDL modifications below function de_dupe($v) { /** * de_dupe will remove duplicate items with the same MAC address */ static $last_mac = ""; $this_mac = $v; if ($last_mac == $this_mac) { $last_mac = $this_mac; return false; } else { $last_mac = $this_mac; return true; } } $macs = array(); // uncomment the below to see a list of all active leases for testing purposes /* echo "
    "; while($lease = $test->nextActive()) { $macs[] = $lease["mac"]; echo "
  1. " . $lease["mac"] . " " . $lease["hostname"] . " | Ends: " . date("H:i j-M-Y", $lease["ends"]) . "
  2. \n"; } array_multisort($macs, SORT_ASC); $macs = array_filter($macs, de_dupe); echo "
"; echo "
De-duped active MACs:\n";
print_r($macs);
echo "
"; */ while($lease = $test->nextActive()) { $macs[] = $lease["mac"]; } array_multisort($macs, SORT_ASC); $macs = array_filter($macs, de_dupe); $this_year = date("Y"); $this_month = date("n"); $this_day = date("j"); $this_hour = date("G"); $lease_count = count($macs); //echo "

$lease_count active wifi user(s) during the $this_hour:00 hour on $this_year-$this_month-$this_day.

"; // ######################################################################## function db_connect() { // set up the database connection; print a message in case of error $db = mysql_connect("localhost", "root", $mysql_pw) or die ("Could not connect to MySQL server"); mysql_select_db("wifi_stats", $db) or die("Could not select database"); return $db; } // ######################################################################## function db_close($db) { // close the database connection mysql_close($db); } // ######################################################################## function perform_query($query) { global $submit; $db = db_connect(); // perform the query and enter the result into a variable; print a message in case of error $result = mysql_query($query) or die("Query Failed: $query"); db_close($db); return $result; } /**********************/ /* Statistical Module */ /**********************/ $now = date("j-n-Y-G-w", strtotime("-1 hours")); $dot = explode("-",$now); $current_day = $dot[0]; $current_month = $dot[1]; $current_year = $dot[2]; $current_hour = $dot[3]; $current_weekday = $dot[4]; // Yearly Stats $result = perform_query("SELECT * FROM `year` WHERE `year` = '$current_year'"); $total_rows = mysql_num_rows($result); if (!$total_rows) { perform_query("INSERT `year` VALUES ('$current_year', '0')"); } for ($i = 0; $i < $total_rows; $i++) { $stats_year_uses = mysql_result($result, $i, "uses"); } $stats_year_uses = $lease_count + $stats_year_uses; perform_query("UPDATE `year` SET `uses` = '$stats_year_uses' WHERE `year` = '$current_year'"); // Monthly Stats $result = perform_query("SELECT * FROM `month` WHERE `year` = '$current_year' AND `month` = '$current_month'"); $total_rows = mysql_num_rows($result); if (!$total_rows) { perform_query("INSERT `month` VALUES ('$current_year', '$current_month', '0')"); } for ($i = 0; $i < $total_rows; $i++) { $stats_month_uses = mysql_result($result, $i, "uses"); } $stats_month_uses = $lease_count + $stats_month_uses; perform_query("UPDATE `month` SET `uses` = '$stats_month_uses' WHERE `year` = '$current_year' AND `month` = '$current_month'"); // Daily Stats $result = perform_query("SELECT * FROM `day` WHERE `year` = '$current_year' AND `month` = '$current_month' AND `day` = '$current_day' AND `weekday` = '$current_weekday'"); $total_rows = mysql_num_rows($result); if (!$total_rows) { perform_query("INSERT `day` VALUES ('$current_year', '$current_month', '$current_day', '$current_weekday', '0')"); } for ($i = 0; $i < $total_rows; $i++) { $stats_day_uses = mysql_result($result, $i, "uses"); } $stats_day_uses = $lease_count + $stats_day_uses; perform_query("UPDATE `day` SET `uses` = '$stats_day_uses' WHERE `year` = '$current_year' AND `month` = '$current_month' AND `day` = '$current_day' AND `weekday` = '$current_weekday'"); // Hourly Stats $result = perform_query("SELECT * FROM `hour` WHERE `year` = '$current_year' AND `month` = '$current_month' AND `day` = '$current_day' AND `hour` = '$current_hour'"); $total_rows = mysql_num_rows($result); if (!$total_rows) { perform_query("INSERT `hour` VALUES ('$current_year', '$current_month', '$current_day', '$current_weekday', '$current_hour', '0')"); } for ($i = 0; $i < $total_rows; $i++) { $stats_hour_uses = mysql_result($result, $i, "uses"); } $stats_hour_uses = $lease_count + $stats_hour_uses; perform_query("UPDATE `hour` SET `uses` = '$stats_hour_uses' WHERE `year` = '$current_year' AND `month` = '$current_month' AND `day` = '$current_day' AND `weekday` = '$current_weekday' AND `hour` = '$current_hour'"); ?>