I am having an issue when phones are rebooted the subscribe.php runs and over writes the existing line on the phone_db.txt file. The subscribe file is as follows.
<?php
/* This file captures the extension, IP, MAC and SetID during the Boot-up process.
These values are obtained as part of the Subscription feature of the Avaya IP Phones*/
$EXT = $_GET["Extn"]; //capture the extension
$IP = $_GET["ip"]; //capture the ip-address
$MAC = $_GET["MAC"]; //capture the MAC Address
$SET_ID = $_GET["SetID"]; //capture the Set ID
$CWD = getcwd(); // returns the current working directory
/* For simplicity the database is a simple text file (Phone_db.txt), in a real world application
any database such as MySQL, SQL Server, Oracle, etc. would be used */
// Phone_db.txt is a file of CSV lines: ext,ip
$handle = fopen("$CWD\\Phone_db.txt", "r");
// read the contents of file
if ($handle) {
while (!feof($handle)) {
// $line is an array of where each element is one of the CSV fields of the file
// Note that the newline chars have been stripped
$line = explode("\n", rtrim(fgets($handle)));
// $lines[] is an array of arrays, i.e., each element is one of the line arrays
$lines[] = $line;
}
}
fclose($handle);
// create the new file with old references to EXT or IP removed and the new one added
$handle = fopen("$CWD\\Phone_db.txt", "w");
foreach($lines as $line) {
// note: $line[0] = ext, $line[1] = ip etc.
// skip any line that has either the same IP or the same EXT
// as these are being replaced with the new values
if($EXT == $line[0] || $IP == $line[1] || $MAC == $line[2] || $SET_ID == $line[3])
continue;
fwrite($handle, $line[0] . "," . $line[1] . "," . $line[2] . "," . $line[3] . "\r\n");
}
// now write the new record
fwrite($handle, $EXT . "," . $IP . "," . $MAC . "," . $SET_ID);
fclose($handle); //close the file
?>
Thank you in advance for your help.
<?php
/* This file captures the extension, IP, MAC and SetID during the Boot-up process.
These values are obtained as part of the Subscription feature of the Avaya IP Phones*/
$EXT = $_GET["Extn"]; //capture the extension
$IP = $_GET["ip"]; //capture the ip-address
$MAC = $_GET["MAC"]; //capture the MAC Address
$SET_ID = $_GET["SetID"]; //capture the Set ID
$CWD = getcwd(); // returns the current working directory
/* For simplicity the database is a simple text file (Phone_db.txt), in a real world application
any database such as MySQL, SQL Server, Oracle, etc. would be used */
// Phone_db.txt is a file of CSV lines: ext,ip
$handle = fopen("$CWD\\Phone_db.txt", "r");
// read the contents of file
if ($handle) {
while (!feof($handle)) {
// $line is an array of where each element is one of the CSV fields of the file
// Note that the newline chars have been stripped
$line = explode("\n", rtrim(fgets($handle)));
// $lines[] is an array of arrays, i.e., each element is one of the line arrays
$lines[] = $line;
}
}
fclose($handle);
// create the new file with old references to EXT or IP removed and the new one added
$handle = fopen("$CWD\\Phone_db.txt", "w");
foreach($lines as $line) {
// note: $line[0] = ext, $line[1] = ip etc.
// skip any line that has either the same IP or the same EXT
// as these are being replaced with the new values
if($EXT == $line[0] || $IP == $line[1] || $MAC == $line[2] || $SET_ID == $line[3])
continue;
fwrite($handle, $line[0] . "," . $line[1] . "," . $line[2] . "," . $line[3] . "\r\n");
}
// now write the new record
fwrite($handle, $EXT . "," . $IP . "," . $MAC . "," . $SET_ID);
fclose($handle); //close the file
?>
Thank you in advance for your help.
Comment