<?php

// load the config
require_once 'fritzbox.conf.php';
// load the fritzbox_api class
require_once '../lib/fritzbox_api.class.php';
if (count($argv) < 2 || $argv[1] != "--overwrite") {
    print "CAUTION: This will delete all current entries in your fritzbox phonebook";
    print $newline . "call with " . $argv[0] . " --overwrite" . $newline;
    die;
}
// do the job
try {
    $fritz = new fritzbox_api($password, $fritzbox_ip);
    $formfields = array('PhonebookId' => '0', 'PhonebookImportFile' => '@' . $fonbook_import_path . ';type=text/xml');
    $output = $fritz->doPostFile($formfields);
    print "Phonebook imported from " . $fonbook_import_path . $newline;
    $fritz = null;
    // destroy the object to log out
} catch (Exception $e) {
    echo $e->getMessage();
}
Example #2
0
 public function upload_to_fb()
 {
     // if the user wants to save the xml to a separate file, we do so now
     if (array_key_exists('output_file', $this->config)) {
         $output = fopen($this->config['output_file'], 'w');
         if ($output) {
             fwrite($output, $this->fbxml);
             fclose($output);
         }
         return 0;
     }
     // now we upload the photo jpgs first being stored in the
     // temp directory.
     // perform an ftps-connection to copy over the photos to a specified directory
     $ftp_server = $this->config['fritzbox_ip_ftp'];
     $conn_id = ftp_ssl_connect($ftp_server);
     ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 60);
     $login_result = ftp_login($conn_id, $this->config['fritzbox_user'], $this->config['fritzbox_pw']);
     ftp_pasv($conn_id, true);
     // create remote photo path on FRITZ!Box if it doesn't exist
     $remote_path = $this->config['usb_disk'] . "/FRITZ/fonpix";
     if (ftp_nlist($conn_id, $remote_path) == false) {
         ftp_mkdir($conn_id, $remote_path);
     }
     // now iterate through all jpg files in tempdir and upload them
     $dir = new DirectoryIterator($this->tmpdir);
     foreach ($dir as $fileinfo) {
         if (!$fileinfo->isDot()) {
             if ($fileinfo->getExtension() == "jpg") {
                 $file = $fileinfo->getFilename();
                 print " FTP-Upload: " . $file . PHP_EOL;
                 if (!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) {
                     // retry when a fault occurs.
                     print "  WARNING: an error occurred while uploading file " . $fileinfo->getFilename() . " - retrying" . PHP_EOL;
                     $conn_id = ftp_ssl_connect($ftp_server);
                     $login_result = ftp_login($conn_id, $this->config['fritzbox_user'], $this->config['fritzbox_pw']);
                     ftp_pasv($conn_id, true);
                     if (!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) {
                         print "  ERROR: an error occurred while uploading file " . $fileinfo->getFilename() . " - giving up" . PHP_EOL;
                     }
                 }
             }
         }
     }
     // close ftp connection
     ftp_close($conn_id);
     // lets post the phonebook xml to the FRITZ!Box
     print " Uploading Phonebook XML" . PHP_EOL;
     try {
         $fritz = new fritzbox_api($this->config['fritzbox_pw'], $this->config['fritzbox_user'], $this->config['fritzbox_ip'], $this->config['fritzbox_force_local_login']);
         $formfields = array('PhonebookId' => $this->config['phonebook_number']);
         $filefileds = array('PhonebookImportFile' => array('type' => 'text/xml', 'filename' => 'updatepb.xml', 'content' => $this->fbxml));
         $raw_result = $fritz->doPostFile($formfields, $filefileds);
         // send the command
         $msg = $this->_parse_fb_result($raw_result);
         $fritz = null;
         // destroy the object to log out
         print "  FRITZ!Box returned message: '" . $msg . "'" . PHP_EOL;
     } catch (Exception $e) {
         print "  ERROR: " . $e->getMessage() . PHP_EOL;
         // show the error message in anything failed
     }
 }
Example #3
0
 public function upload_to_fb()
 {
     // if the user wants to save the xml to a separate file, we do so now
     if (array_key_exists('output_file', $this->config)) {
         // build md5 hash of previous stored xml without <mod_time> Elements
         $oldphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/", "", file_get_contents($this->config['output_file'], 'r'), -1, $debugoldtsreplace));
         $output = fopen($this->config['output_file'], 'w');
         if ($output) {
             fwrite($output, $this->fbxml);
             fclose($output);
             print " Saved to file " . $this->config['output_file'] . PHP_EOL;
         }
         if (array_key_exists('output_and_upload', $this->config) and $this->config['output_and_upload']) {
             $newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/", "", file_get_contents($this->config['output_file'], 'r'), -1, $debugnewtsreplace));
             print " INFO: Compare old and new phonebook file versions." . PHP_EOL . " INFO: old version: " . $oldphonebhash . PHP_EOL . " INFO: new version: " . $newphonebhash . PHP_EOL;
             if ($oldphonebhash === $newphonebhash) {
                 print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box." . PHP_EOL;
                 return 0;
             } else {
                 print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL;
             }
         } else {
             return 0;
         }
     }
     // now we upload the photo jpgs first being stored in the
     // temp directory.
     // perform an ftps-connection to copy over the photos to a specified directory
     $ftp_server = $this->config['fritzbox_ip_ftp'];
     $conn_id = ftp_ssl_connect($ftp_server);
     if ($conn_id == false) {
         print " WARNING: Secure connection to FTP-server '" . $ftp_server . "' failed, retrying without SSL." . PHP_EOL;
         $conn_id = ftp_connect($ftp_server);
     }
     if ($conn_id != false) {
         ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 60);
         $login_result = ftp_login($conn_id, $this->config['fritzbox_user'], $this->config['fritzbox_pw']);
         if ($login_result === true) {
             ftp_pasv($conn_id, true);
             // create remote photo path on FRITZ!Box if it doesn't exist
             $remote_path = $this->config['usb_disk'] . "/FRITZ/fonpix";
             $all_existing_files = ftp_nlist($conn_id, $remote_path);
             if ($all_existing_files == false) {
                 ftp_mkdir($conn_id, $remote_path);
                 $all_existing_files = array();
             }
             // now iterate through all jpg files in tempdir and upload them if necessary
             $dir = new DirectoryIterator($this->tmpdir);
             foreach ($dir as $fileinfo) {
                 if (!$fileinfo->isDot()) {
                     if ($fileinfo->getExtension() == "jpg") {
                         $file = $fileinfo->getFilename();
                         print " FTP-Upload '" . $file . "'...";
                         if (!in_array($remote_path . "/" . $file, $all_existing_files)) {
                             if (!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) {
                                 // retry when a fault occurs.
                                 print " retrying... ";
                                 $conn_id = ftp_ssl_connect($ftp_server);
                                 if ($conn_id == false) {
                                     print " WARNING: Secure re-connection to FTP-server '" . $ftp_server . "' failed, retrying without SSL." . PHP_EOL;
                                     $conn_id = ftp_connect($ftp_server);
                                 }
                                 if ($conn_id == false) {
                                     print " ERROR: couldn't re-connect to FTP server '" . $ftp_server . "', abortіng." . PHP_EOL;
                                     break;
                                 }
                                 $login_result = ftp_login($conn_id, $this->config['fritzbox_user'], $this->config['fritzbox_pw']);
                                 if ($login_result === false) {
                                     print " ERROR: couldn't re-login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL;
                                     break;
                                 }
                                 ftp_pasv($conn_id, true);
                                 if (!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) {
                                     print " ERROR: while uploading file " . $fileinfo->getFilename() . PHP_EOL;
                                 } else {
                                     print " ok." . PHP_EOL;
                                 }
                             } else {
                                 print " ok." . PHP_EOL;
                             }
                             // cleanup old files
                             foreach ($all_existing_files as $existing_file) {
                                 if (strpos($existing_file, $remote_path . "/" . substr($file, 0, -10)) !== false) {
                                     print " FTP-Delete: " . $existing_file . PHP_EOL;
                                     ftp_delete($conn_id, $remote_path . "/" . basename($existing_file));
                                 }
                             }
                         } else {
                             print " already exists." . PHP_EOL;
                         }
                     }
                 }
             }
         } else {
             print " ERROR: couldn't login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL;
         }
         // close ftp connection
         ftp_close($conn_id);
     } else {
         print " ERROR: couldn't connect to FTP server '" . $ftp_server . "'." . PHP_EOL;
     }
     // in case numeric IP is given, try to resolve to hostname. Otherwise Fritzbox may decline login, because it is determine to be (prohibited) remote access
     $hostname = $this->config['fritzbox_ip'];
     if (filter_var($hostname, FILTER_VALIDATE_IP)) {
         $hostname = gethostbyaddr($hostname);
         if ($hostname == $this->config['fritzbox_ip']) {
             print " WARNING: Unable to get hostname for IP address (" . $this->config['fritzbox_ip'] . ") <" . $hostname . "<" . PHP_EOL;
         } else {
             print " INFO: Given IP address (" . $this->config['fritzbox_ip'] . ") has hostname " . $hostname . "." . PHP_EOL;
             $this->config['fritzbox_ip'] = $hostname;
         }
     }
     // lets post the phonebook xml to the FRITZ!Box
     print " Uploading Phonebook XML to " . $this->config['fritzbox_ip'] . PHP_EOL;
     try {
         $fritz = new fritzbox_api($this->config['fritzbox_pw'], $this->config['fritzbox_user'], $this->config['fritzbox_ip'], $this->config['fritzbox_force_local_login']);
         $formfields = array('PhonebookId' => $this->config['phonebook_number']);
         $filefileds = array('PhonebookImportFile' => array('type' => 'text/xml', 'filename' => 'updatepb.xml', 'content' => $this->fbxml));
         $raw_result = $fritz->doPostFile($formfields, $filefileds);
         // send the command
         $msg = $this->_parse_fb_result($raw_result);
         unset($fritz);
         // destroy the object to log out
         print "  FRITZ!Box returned message: '" . $msg . "'" . PHP_EOL;
     } catch (Exception $e) {
         print "  ERROR: " . $e->getMessage() . PHP_EOL;
         // show the error message in anything failed
     }
 }