Skip to content

Commit

Permalink
Improve behavior on errors when refreshing phone books
Browse files Browse the repository at this point in the history
- Handle case where FRITZ!Box user is missing the right permission
- On libxml_error, abort & show message
- On refresh, clear current downloads only if a new one was received,
  not on error!
- Note that $fritzbox_user is required with a new Fritz OS
  as reaction to SkyhawkXava#3
- Correct config file name in error messages
  locale/*/directory.locale.inc.php
  • Loading branch information
jyevon committed Sep 9, 2023
1 parent b5f7f0d commit d44455c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/directory.config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

$fritzbox_ip = 'fritz.box'; //Hier die IP oder den Hostname der FritzBox eintragen (meist fritz.box)
// $fritzbox_user = "User"; // Benutzername, wenn in der Fritz!Box aktiviert.
$fritzbox_user = "your_fritz1234"; // Benutzername (Pflicht ab Fritz OS 7.25!), wenn in der Fritz!Box aktiviert. Dieser Benutzer benötigt die Berechtigung "FRITZ!Box-Einstellungen"!
$fritzbox_password = 'yourpassword'; //Hier das Passwort eintragen


Expand Down
38 changes: 31 additions & 7 deletions directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,44 @@
$log->newEntry ("directory.php: execute: refresh > Login failed");
$menu = new CiscoIpPhoneText(PB_REFRESH, PB_NAME_GENERAL . ' ' . PB_LOGIN_FAILED, PB_ADMIN_CHECKPWD);
echo '<?xml version="1.0" encoding="utf-8" ?>';
echo (string) $menu;
return;
}
}
echo (string) $menu;

foreach(scandir("books") as $book){
if(is_file("books/$book")){
unlink("books/$book");
curl_close($ch);
return;
}
}

$clear_books = true;
$tmp_telefonbuch = $telefonbuch;

do {
$log->newEntry ("directory.php: execute: refresh > try to download phonebook: id=".$tmp_telefonbuch);
curl_setopt($ch, CURLOPT_URL, $fritzbox_cfg);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("sid" => $SID, "PhonebookId" => $tmp_telefonbuch, "PhonebookExportName" => "Telefonbuch", "PhonebookExport" => ""));
$book = curl_exec($ch);
if(empty($book)) {
$log->newEntry ("directory.php: execute: refresh > phonebook id=".$tmp_telefonbuch." - ERROR: response is empty - download-process is stopped. Please check permission");
$menu = new CiscoIpPhoneText(PB_REFRESH, PB_NAME_GENERAL . ' ' . PB_BOOKS_ERROR, PB_ADMIN_CHECKPERM);
echo '<?xml version="1.0" encoding="utf-8" ?>';
echo (string) $menu;

curl_close($ch);
return;
}
$xml = simplexml_load_string($book);
if ((bool) $xml === false) { // catch error!
foreach(libxml_get_errors() as $error) {
$log->newEntry ("directory.php: execute: refresh > phonebook id=".$tmp_telefonbuch." - ERROR: ". $error->message);
}

$menu = new CiscoIpPhoneText(PB_REFRESH, PB_NAME_GENERAL . ' ' . PB_BOOKS_ERROR, PB_NO_FURTHER_INFORMATION);
echo '<?xml version="1.0" encoding="utf-8" ?>';
echo (string) $menu;

curl_close($ch);
return;
}

if(!$xml->phonebook) {
if($tmp_telefonbuch < 240) { // no more local books, jump to external books
$tmp_telefonbuch = 240;
Expand All @@ -124,6 +138,16 @@
break;
}
}

if($clear_books) { // keep old downloads on errors - until first new arrives
foreach(scandir("books") as $old_book){
if(is_file("books/$old_book")){
unlink("books/$old_book");
}
}
$clear_books = false;
}

if ((bool) file_put_contents("books/$tmp_telefonbuch.xml",$book, LOCK_EX)) {
$log->newEntry ("directory.php: execute: refresh > phonebook id=".$tmp_telefonbuch." saved");
} else {
Expand Down
3 changes: 2 additions & 1 deletion locale/de/directory.locale.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
define ('PB_NAME_GENERAL', 'FRITZ!Box');
define ('PB_REFRESH', 'Aktualisieren');
define ('PB_LOGIN_FAILED', 'Login fehlgeschlagen');
define ('PB_ADMIN_CHECKPWD', 'Administrator: Bitte das ' . PB_NAME_GENERAL . '-Passwort in der config.php.inc überprüfen!');
define ('PB_ADMIN_CHECKPWD', 'Administrator: Bitte das ' . PB_NAME_GENERAL . '-Passwort in directory.config.inc.php überprüfen!');
define ('PB_ADMIN_CHECKPERM', 'Administrator: Bitte die Berechtigungen für den ' . PB_NAME_GENERAL . '-Benutzer aus directory.config.inc.php überprüfen!');
define ('PB_APP_CALLSMISSED', 'Anrufe in Abwesenheit');
define ('PB_APP_CALLSRECEIVED', 'Angenommene Anrufe');
define ('PB_APP_CALLSPLACED', 'Gewählte Rufnummern');
Expand Down
3 changes: 2 additions & 1 deletion locale/en/directory.locale.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
define ('PB_NAME_GENERAL', 'FRITZ!Box');
define ('PB_REFRESH', 'Refresh');
define ('PB_LOGIN_FAILED', 'Login failed');
define ('PB_ADMIN_CHECKPWD', 'Administrator: Please verify ' . PB_NAME_GENERAL . ' password in config.php.inc!');
define ('PB_ADMIN_CHECKPWD', 'Administrator: Please verify ' . PB_NAME_GENERAL . ' password in directory.config.inc.php!');
define ('PB_ADMIN_CHECKPERM', 'Administrator: Please verify permissions of ' . PB_NAME_GENERAL . ' user from directory.config.inc.php!');
define ('PB_APP_CALLSMISSED', 'Missed Calls');
define ('PB_APP_CALLSRECEIVED', 'Received Calls');
define ('PB_APP_CALLSPLACED', 'Calls Placed');
Expand Down

0 comments on commit d44455c

Please sign in to comment.