From c463546087814af4fc0fc570dbb35d5ddb1c61ff Mon Sep 17 00:00:00 2001
From: andig
Date: Fri, 17 Nov 2017 17:58:17 +0100
Subject: [PATCH 001/165] Initial commit
---
.gitignore | 3 +
README.md | 95 +-
carddav2fb.old.php | 829 +++++++++++++++++
carddav2fb.php | 834 +-----------------
composer.json | 22 +
config.example.php | 108 +--
lib/fritzbox_api_php/fritzbox_api.class.php | 539 -----------
.../carddav.php => src/CardDav/Backend.php | 6 +-
src/CardDavLoaderCommand.php | 39 +
src/FritzBox/Api.php | 296 +++++++
src/FritzBox/Converter.php | 153 ++++
src/RunCommand.php | 37 +
src/UploadToFritzCommand.php | 58 ++
src/VcardToFritzCommand.php | 112 +++
14 files changed, 1602 insertions(+), 1529 deletions(-)
create mode 100755 carddav2fb.old.php
mode change 100755 => 100644 carddav2fb.php
create mode 100644 composer.json
delete mode 100644 lib/fritzbox_api_php/fritzbox_api.class.php
rename lib/CardDAV-PHP/carddav.php => src/CardDav/Backend.php (99%)
create mode 100644 src/CardDavLoaderCommand.php
create mode 100644 src/FritzBox/Api.php
create mode 100644 src/FritzBox/Converter.php
create mode 100644 src/RunCommand.php
create mode 100644 src/UploadToFritzCommand.php
create mode 100644 src/VcardToFritzCommand.php
diff --git a/.gitignore b/.gitignore
index b74decb0..1edb6043 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,4 @@
+/composer.lock
/config.php
+/cards.xml
+/vendor/
diff --git a/README.md b/README.md
index c428c610..1797f067 100644
--- a/README.md
+++ b/README.md
@@ -1,100 +1,9 @@
# CardDAV contacts import for AVM FRITZ!Box
-[](https://scrutinizer-ci.com/g/jens-maus/carddav2fb/build-status/master) [](https://scrutinizer-ci.com/g/jens-maus/carddav2fb/?branch=master)
-
-Features:
-
-* Allows to import CardDAV-based VCard contacts (e.g. from 'owncloud') to a phonebook in a AVM FRITZ!Box
-* CardDAV import includes photo images specified in VCards
-* No modification of FRITZ!Box firmware (aka FRITZ!OS) required
-* Definition of multiple CardDAV accounts and "folders" possible
-* Format of full name in FRITZ!Box phonebook can be designed
-
-**CAUTION: This script will overwrite your current contacts in the FritzBox without any warning!**
-
-## Information
-
-This version of carddav2fb is a forked version from carlos22 (https://github.com/carlos22/carddav2fb) and adding support for convenient image upload, different FRITZ!Box base paths (for example for FRITZ!Box 7490 (UI) OS: 6.50) and full name design support.
-
-## Requirements
-
-* PHP-version 5.3.6 or higher
-* PHP-curl module
-* PHP-ftp module
-* PHP-mbstring module
-
-## Installation
-
- Checkout the carddav2fb sources including its related subprojects using the following command:
-
- git clone https://github.com/jens-maus/carddav2fb.git
-
-Now you should have everything setup and checked out to a 'carddav2fb' directory.
-
-### Configuration
-1. Make sure you have `System -> FRITZ!Box-Users -> Login via Username+Password` in your FRITZ!Box activated.
-2. Make sure you have a separate user created under `System -> FRITZ!Box-Users` for which the following access rights have been granted:
- * `FRITZ!Box settings` (required to upload telephone book data)
- * `Access to NAS content` (required to upload photos via ftp).
-3. Make sure the telephone book you are going to update via carddav2fb exists on the FRITZ!Box, otherwise the upload will fail.
-4. Copy `config.example.php` to `config.php` and adapt it to your needs including setting the FRITZ!Box user settings.
-
-## Usage
-
-### Ubuntu
-
-1. Install PHP, PHP-curl, PHP-ftp, PHP-mbstring and php-xml module:
-
- sudo apt-get install php-cli php-curl php-ftp php-mbstring php-xml
-
-2. Open a Terminal and execute:
-
- php carddav2fb.php
-
-### Windows
-
-1. Download PHP from [php.net](http://windows.php.net/download/). Extract it to `C:\PHP`.
-2. Start -> cmd. Run `C:\PHP\php.exe C:\path\to\carddav2fb\carddav2fb.php`
-
-## config.php Example (owncloud)
-
- $config['fritzbox_ip'] = 'fritz.box';
- $config['fritzbox_user'] = '';
- $config['fritzbox_pw'] = '';
- $config['phonebook_number'] = '0';
- $config['phonebook_name'] = 'Telefonbuch';
- $config['fritzbox_path'] = 'file:///var/media/ftp/';
-
- // full name format options default 0
- // parts in '' will only added if existing and switched to true in config
- // 0 = 'Prefix' Lastname, Firstname, 'Additional Names', 'Suffix', 'orgname'
- // 1 = 'Prefix' Firstname Lastname 'AdditionalNames' 'Suffix' '(orgname)'
- // 2 = 'Prefix' Firstname 'AdditionalNames' Lastname 'Suffix' '(orgname)'
- $config['fullname_format'] = 0;
-
- // fullname parts
- $config['prefix'] = false; // include prefix in fullname if existing
- $config['suffix'] = false; // include suffix in fullname if existing
- $config['addnames'] = false; // include additionalnames in fullname if existing
- $config['orgname'] = false; // include organisation (company) in fullname if existing
-
- $config['quickdial_keyword'] = 'Quickdial:'; // once activated you may add 'Quickdial:+49030123456:**709' to the contact note field and the number will be set as quickdialnumber in your FRITZ!Box. It is possible to add more quickdials for one contact each in a new line
-
- // first
- $config['carddav'][0] = array(
- 'url' => 'https:///remote.php/carddav/addressbooks//contacts',
- 'user' => '',
- 'pw' => ''
- );
-
-## Note
-This script is using third-party libraries for downloading VCards from CardDAV servers based on the following packages
-* CardDAV-PHP (https://github.com/jens-maus/CardDAV-PHP.git)
-* FRITZ!Box-API-PHP (https://github.com/jens-maus/fritzbox_api_php.git)
-* VCard-Parser (https://github.com/jens-maus/vCard-parser.git)
+This is an entirely simplified version of https://github.com/jens-maus/carddav2fb
## License
This script is released under Public Domain.
## Authors
-Copyright (c) 2012-2016 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger
+Copyright (c) 2012-2017 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger, Andreas Götz
diff --git a/carddav2fb.old.php b/carddav2fb.old.php
new file mode 100755
index 00000000..bd9a25fd
--- /dev/null
+++ b/carddav2fb.old.php
@@ -0,0 +1,829 @@
+ (LICNECE: unknown)
+ * * CardDAV-PHP (LICENCE: AGPLv3)
+ * * fritzbox_api_php (LICENCE: CC-by-SA 3.0)
+ *
+ * LICENCE (of this file): MIT
+ *
+ * Autors: Karl Glatz (original author)
+ * Martin Rost
+ * Jens Maus
+ * Johannes Freiburger
+ *
+ */
+error_reporting(E_ALL);
+setlocale(LC_ALL, 'de_DE.UTF8');
+
+// Version identifier for CardDAV2FB
+$carddav2fb_version = '1.11 (2016-05-12)';
+
+// check for the minimum php version
+$php_min_version = '5.3.6';
+if(version_compare(PHP_VERSION, $php_min_version) < 0)
+{
+ print 'ERROR: PHP version ' . $php_min_version . ' is required. Found version: ' . PHP_VERSION . PHP_EOL;
+ exit(1);
+}
+
+require_once('lib/CardDAV-PHP/carddav.php');
+require_once('lib/vCard-parser/vCard.php');
+require_once('lib/fritzbox_api_php/fritzbox_api.class.php');
+
+if($argc == 2)
+ $config_file_name = $argv[1];
+else
+ $config_file_name = __DIR__ . '/config.php';
+
+// default/fallback config options
+$config['tmp_dir'] = sys_get_temp_dir();
+$config['fritzbox_ip'] = 'fritz.box';
+$config['fritzbox_ip_ftp'] = $config['fritzbox_ip'];
+$config['fritzbox_force_local_login'] = false;
+$config['phonebook_number'] = '0';
+$config['phonebook_name'] = 'Telefonbuch';
+$config['usb_disk'] = '';
+$config['fritzbox_path'] = 'file:///var/media/ftp/';
+$config['fullname_format'] = 0; // see config.example.php for options
+$config['prefix'] = false;
+$config['suffix'] = false;
+$config['addnames'] = false;
+$config['orgname'] = false;
+$config['build_photos'] = true;
+$config['quickdial_keyword'] = 'Quickdial:';
+
+if(is_file($config_file_name))
+ require($config_file_name);
+else
+{
+ print 'ERROR: No ' . $config_file_name . ' found, please take a look at config.example.php and create a ' . $config_file_name . ' file!' . PHP_EOL;
+ exit(1);
+}
+
+// ---------------------------------------------
+// MAIN
+print "carddav2fb.php " . $carddav2fb_version . " - CardDAV to FRITZ!Box phonebook conversion tool" . PHP_EOL;
+print "Copyright (c) 2012-2016 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger" . PHP_EOL . PHP_EOL;
+
+$client = new CardDAV2FB($config);
+
+// read vcards from webdav
+print 'Retrieving VCards from all CardDAV server(s):' . PHP_EOL;
+$client->get_carddav_entries();
+print 'Done.' . PHP_EOL;
+
+flush(); // in case this script runs by php-cgi
+
+// transform them to a fritzbox compatible xml file
+print 'Converting VCards to FritzBox XML format:' . PHP_EOL;
+$client->build_fb_xml();
+print 'Done.' . PHP_EOL;
+
+flush(); // in case this script runs by php-cgi
+
+// upload the XML-file to the FRITZ!Box (CAUTION: this will overwrite all current entries in the phone book!!)
+print 'Upload data to FRITZ!Box @ ' . $config['fritzbox_ip'] . PHP_EOL;
+if($client->upload_to_fb())
+ print 'Done.' . PHP_EOL;
+else
+ exit(1);
+
+flush(); // in case this script runs by php-cgi
+
+// ---------------------------------------------
+// Class definition
+class CardDAV2FB
+{
+ protected $entries = array();
+ protected $fbxml = "";
+ protected $config = null;
+ protected $tmpdir = null;
+
+ public function __construct($config)
+ {
+ $this->config = $config;
+
+ // create a temp directory where we store photos
+ $this->tmpdir = $this->mktemp($this->config['tmp_dir']);
+ }
+
+ public function __destruct()
+ {
+ // remote temp directory
+ $this->rmtemp($this->tmpdir);
+ }
+
+ // Source: https://php.net/manual/de/function.tempnam.php#61436
+ public function mktemp($dir, $prefix = '', $mode = 0700)
+ {
+ if(substr($dir, -1) != '/')
+ $dir .= '/';
+
+ do
+ {
+ $path = $dir . $prefix . mt_rand(0, 9999999);
+ }
+ while(!mkdir($path, $mode));
+
+ return $path;
+ }
+
+ public function rmtemp($dir)
+ {
+ if(is_dir($dir))
+ {
+ $objects = scandir($dir);
+ foreach($objects as $object)
+ {
+ if($object != "." && $object != "..")
+ {
+ if(filetype($dir . "/" . $object) == "dir")
+ rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object);
+ }
+ }
+ reset($objects);
+ rmdir($dir);
+ }
+ }
+
+ public function is_base64($str)
+ {
+ try
+ {
+ // Check if there are valid base64 characters
+ if(!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $str))
+ return false;
+
+ // Decode the string in strict mode and check the results
+ $decoded = base64_decode($str, true);
+ if($decoded === false)
+ return false;
+
+ // Encode the string again
+ if(base64_encode($decoded) === $str)
+ return true;
+ else
+ return false;
+ }
+ catch(Exception $e)
+ {
+ // If exception is caught, then it is not a base64 encoded string
+ return false;
+ }
+ }
+
+ public function base64_to_jpeg($inputfile, $outputfile)
+ {
+ // read data (binary)
+ $ifp = fopen($inputfile, "rb");
+ $imageData = fread($ifp, filesize($inputfile));
+ fclose($ifp);
+
+ // encode & write data (binary)
+ $ifp = fopen($outputfile, "wb");
+ fwrite($ifp, base64_decode($imageData));
+ fclose($ifp);
+
+ // return output filename
+ return($outputfile);
+ }
+
+ public function get_carddav_entries()
+ {
+ $entries = array();
+ $snum = 0;
+
+ if(is_array($this->config['carddav']))
+ {
+ foreach($this->config['carddav'] as $conf)
+ {
+ print " [" . $snum . "]: " . $conf['url'] . " ";
+ $carddav = new CardDavPHP\CardDavBackend($conf['url']);
+ $carddav->setAuth($conf['user'], $conf['pw']);
+
+ // set the vcard extension in case the user
+ // defined it in the config
+ if(isset($conf['extension']))
+ $carddav->setVcardExtension($conf['extension']);
+
+ // retrieve data from the CardDAV server now
+ $xmldata = $carddav->get();
+
+ // identify if we received UTF-8 encoded data from the
+ // CardDAV server and if not reencode it since the FRITZ!Box
+ // requires UTF-8 encoded data
+ if(iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata)
+ $xmldata = utf8_encode($xmldata);
+
+ // read raw_vcard data from xml response
+ $raw_vcards = array();
+ $xmlvcard = new SimpleXMLElement($xmldata);
+
+ foreach($xmlvcard->element as $vcard_element)
+ {
+ $id = $vcard_element->id->__toString();
+ $value = (string)$vcard_element->vcard->__toString();
+ $raw_vcards[$id] = $value;
+ }
+
+ print " " . count($raw_vcards) . " VCards retrieved." . PHP_EOL;
+
+ // parse raw_vcards
+ $quick_dial_arr = array();
+ foreach($raw_vcards as $v)
+ {
+ $vcard_obj = new vCard(false, $v);
+ $name_arr = null;
+ if(isset($vcard_obj->n[0]))
+ $name_arr = $vcard_obj->n[0];
+ $org_arr = null;
+ if(isset($vcard_obj->org[0]))
+ $org_arr = $vcard_obj->org[0];
+ $addnames = '';
+ $prefix = '';
+ $suffix = '';
+ $orgname = '';
+ $formattedname = '';
+
+ // Build name Parts if existing ans switch to true in config
+ if(isset($name_arr['prefixes']) and $this->config['prefix'])
+ $prefix = trim($name_arr['prefixes']);
+
+ if(isset($name_arr['suffixes']) and $this->config['suffix'])
+ $suffix = trim($name_arr['suffixes']);
+
+ if(isset($name_arr['additionalnames']) and $this->config['addnames'])
+ $addnames = trim($name_arr['additionalnames']);
+
+ if(isset($org_arr['name']) and $this->config['orgname'])
+ $orgname = trim($org_arr['name']);
+
+ if (isset($vcard_obj->fn[0]))
+ $formattedname = $vcard_obj->fn[0];
+
+ $firstname = trim($name_arr['firstname']);
+ $lastname = trim($name_arr['lastname']);
+
+ // the following section implemented different ways of constructing the
+ // final phonebook name entry depending on user preferred settings
+ // selectable in the config file. Possible options are:
+ //
+ // $this->config['fullname_format']:
+ //
+ // 0: "Prefix Lastname, Firstname AdditionalNames Suffix (orgname)"
+ // 1: "Prefix Firstname Lastname AdditionalNames Suffix (orgname)"
+ // 2: "Prefix Firstname AdditionalNames Lastname Suffix (orgname)"
+ //
+ $name = '';
+ $format = $this->config['fullname_format'];
+
+ // Prefix
+ if(!empty($prefix))
+ $name .= $prefix;
+
+ if($format == 0)
+ {
+ // Lastname
+ if(!empty($name) and !empty($lastname))
+ $name .= ' ' . $lastname;
+ else
+ $name .= $lastname;
+ }
+ else
+ {
+ // Firstname
+ if(!empty($name) and !empty($firstname))
+ $name .= ' ' . $firstname;
+ else
+ $name .= $firstname;
+ }
+
+ if($format == 2)
+ {
+ // AdditionalNames
+ if(!empty($name) and !empty($addnames))
+ $name .= ' ' . $addnames;
+ else
+ $name .= $addnames;
+ }
+
+ if($format == 0)
+ {
+ // Firstname
+ if(!empty($name) and !empty($firstname))
+ $name .= ', ' . $firstname;
+ else
+ $name .= $firstname;
+ }
+ else
+ {
+ // Lastname
+ if(!empty($name) and !empty($lastname))
+ $name .= ' ' . $lastname;
+ else
+ $name .= $lastname;
+ }
+
+ if($format != 2)
+ {
+ // AdditionalNames
+ if(!empty($name) and !empty($addnames))
+ $name .= ' ' . $addnames;
+ else
+ $name .= $addnames;
+ }
+
+ // Suffix
+ if(!empty($name) and !empty($suffix))
+ $name .= ' ' . $suffix;
+ else
+ $name .= $suffix;
+
+ // OrgName
+ if(!empty($name) and !empty($orgname))
+ $name .= ' (' . $orgname . ')';
+ else
+ $name .= $orgname;
+
+ // make sure to trim whitespaces and double spaces
+ $name = trim(str_replace(' ', ' ', $name));
+
+ // perform a fallback to formatted name, if we don't have any name and formatted name is available
+ if(empty($name) and !empty($formattedname))
+ $name = $formattedname;
+
+ if(empty($name))
+ {
+ print ' WARNING: No fullname, lastname, orgname or formatted name found!' . PHP_EOL;
+ $name = 'UNKNOWN';
+ }
+
+ // format filename of contact photo; remove special letters
+ if($vcard_obj->photo)
+ {
+ $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'),
+ array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name);
+ }
+ else
+ $photo = '';
+
+ // phone
+ $phone_no = array();
+ if($vcard_obj->categories)
+ $categories = $vcard_obj->categories[0];
+ else
+ $categories = array();
+
+ // check for quickdial entry
+ if(isset($vcard_obj->note[0]))
+ {
+ $note = $vcard_obj->note[0];
+ $notes = explode($this->config['quickdial_keyword'], $note);
+ foreach($notes as $linenr => $linecontent)
+ {
+ $found = strrpos($linecontent, ":**7");
+ if($found > 0)
+ {
+ $pos_qd_start = strrpos($linecontent, ":**7");
+ $quick_dial_for_nr = preg_replace("/[^0-9+]/", "", substr($linecontent, 0, $pos_qd_start));
+ $quick_dial_nr = intval(substr($linecontent, $pos_qd_start + 4, 3));
+ $quick_dial_arr[$quick_dial_for_nr] = $quick_dial_nr;
+ }
+ }
+ }
+
+ // e-mail addresses
+ $email_add = array();
+ $vip = isset($this->config['group_vip']) && in_array((string)$this->config['group_vip'], $categories);
+
+ if(array_key_exists('group_filter', $this->config) && is_array($this->config['group_filter']))
+ {
+ $add_entry = 0;
+ foreach($this->config['group_filter'] as $group_filter)
+ {
+ if(in_array($group_filter, $categories))
+ {
+ $add_entry = 1;
+ break;
+ }
+ }
+ }
+ else
+ $add_entry = 1;
+
+ if($add_entry == 1)
+ {
+ foreach($vcard_obj->tel as $t)
+ {
+ $prio = 0;
+ $quickdial = null;
+
+ if(!is_array($t) || empty($t['type']))
+ {
+ $type = "mobile";
+ $phone_number = $t;
+ }
+ else
+ {
+ $phone_number = $t['value'];
+
+ $phone_number_clean = preg_replace("/[^0-9+]/", "", $phone_number);
+ foreach($quick_dial_arr as $qd_phone_nr => $value)
+ {
+ if($qd_phone_nr == $phone_number_clean)
+ {
+ //Set quickdial
+ if($value == 1)
+ print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n";
+ elseif($value >= 100)
+ print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n";
+
+ $quickdial = $value;
+ }
+ }
+
+ $typearr_lower = unserialize(strtolower(serialize($t['type'])));
+
+ // find out priority
+ if(in_array("pref", $typearr_lower))
+ $prio = 1;
+
+ // set the proper type
+ if(in_array("cell", $typearr_lower))
+ $type = "mobile";
+ elseif(in_array("home", $typearr_lower))
+ $type = "home";
+ elseif(in_array("fax", $typearr_lower))
+ $type = "fax_work";
+ elseif(in_array("work", $typearr_lower))
+ $type = "work";
+ elseif(in_array("other", $typearr_lower))
+ $type = "other";
+ elseif(in_array("dom", $typearr_lower))
+ $type = "other";
+ else
+ continue;
+ }
+ $phone_no[] = array("type"=>$type, "prio"=>$prio, "quickdial"=>$quickdial, "value" => $this->_clear_phone_number($phone_number));
+ }
+
+ // request email address and type
+ if($vcard_obj->email)
+ {
+ foreach($vcard_obj->email as $e)
+ {
+ if(empty($e['type']))
+ {
+ $type_email = "work";
+ $email = $e;
+ }
+ else
+ {
+ $email = $e['value'];
+ $typearr_lower = unserialize(strtolower(serialize($e['type'])));
+ if(in_array("work", $typearr_lower))
+ $type_email = "work";
+ elseif(in_array("home", $typearr_lower))
+ $type_email = "home";
+ elseif(in_array("other", $typearr_lower))
+ $type_email = "other";
+ else
+ continue;
+ }
+
+ // DEBUG: print out the email address on the console
+ //print $type_email.": ".$email."\n";
+
+ $email_add[] = array("type"=>$type_email, "value" => $email);
+ }
+ }
+ $entries[] = array("realName" => $name, "telephony" => $phone_no, "email" => $email_add, "vip" => $vip, "photo" => $photo, "photo_data" => $vcard_obj->photo);
+ }
+ }
+
+ $snum++;
+ }
+ }
+
+ $this->entries = $entries;
+ }
+
+ private function _clear_phone_number($number)
+ {
+ return preg_replace("/[^0-9+]/", "", $number);
+ }
+
+ public function build_fb_xml()
+ {
+ if(empty($this->entries))
+ throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!');
+
+ // create FB XML in utf-8 format
+ $root = new SimpleXMLElement('');
+ $pb = $root->phonebook;
+ $pb->addAttribute("name", $this->config['phonebook_name']);
+
+ foreach($this->entries as $entry)
+ {
+ $contact = $pb->addChild("contact");
+ $contact->addChild("category", $entry['vip']);
+ $person = $contact->addChild("person");
+ $person->addChild("realName", $this->_convert_text($entry['realName']));
+
+ echo " VCard: '" . utf8_decode($entry['realName']) . "'" . PHP_EOL;
+
+ // telephone: put the phonenumbers into the fritzbox xml file
+ $telephony = $contact->addChild("telephony");
+ $id = 0;
+ foreach($entry['telephony'] as $tel)
+ {
+ $num = $telephony->addChild("number", $tel['value']);
+ $num->addAttribute("type", $tel['type']);
+ $num->addAttribute("vanity", "");
+ $num->addAttribute("prio", $tel['prio']);
+ $num->addAttribute("id", $id);
+
+ if(isset($tel['quickdial']))
+ {
+ $num->addAttribute("quickdial", $tel['quickdial']);
+ print " Added quickdial: " . $tel['quickdial'] . " for: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL;
+ }
+
+ $id++;
+ print " Added phone: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL;
+ }
+
+ // output a warning if no telephone number was found
+ if($id == 0)
+ print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL;
+
+ // email: put the email addresses into the fritzbox xml file
+ $email = $contact->addChild("services");
+ $id = 0;
+ foreach($entry['email'] as $mail)
+ {
+ $mail_adr = $email->addChild("email", $mail['value']);
+ $mail_adr->addAttribute("classifier", $mail['type']);
+ $mail_adr->addAttribute("id", $id);
+ $id++;
+
+ print " Added email: " . $mail['value'] . " (" . $mail['type'] . ")" . PHP_EOL;
+ }
+
+ // check for a photo being part of the VCard
+ if(($entry['photo']) and ($entry['photo_data']) and (is_array($entry['photo_data'])) and ($entry['photo_data'][0]))
+ {
+ // check if 'photo_data'[0] is an array as well because then
+ // we have to extract ['value'] and friends.
+ if(is_array($entry['photo_data'][0]) and (array_key_exists('value', $entry['photo_data'][0])))
+ {
+ // check if photo_data really contains JPEG data
+ if((array_key_exists('type', $entry['photo_data'][0])) and (is_array($entry['photo_data'][0]['type'])) and
+ ($entry['photo_data'][0]['type'][0] == 'jpeg' or $entry['photo_data'][0]['type'][0] == 'jpg' or $entry['photo_data'][0]['type'][0] == 'image/jpeg'))
+ {
+ // get photo, rename, base64 convert and save as jpg
+ $photo_data = $entry['photo_data'][0]['value'];
+ $photo_version = substr(sha1($photo_data), 0, 5);
+ $photo_file = $this->tmpdir . '/' . "{$entry['photo']}_{$photo_version}.jpg";
+
+ // check for base64 encoding of the photo data and convert it
+ // accordingly.
+ if(((array_key_exists('encoding', $entry['photo_data'][0])) and ($entry['photo_data'][0]['encoding'] == 'b')) or $this->is_base64($photo_data))
+ {
+ file_put_contents($photo_file . ".b64", $photo_data);
+ $this->base64_to_jpeg($photo_file . ".b64", $photo_file);
+ unlink($photo_file . ".b64");
+ }
+ else
+ {
+ print " WARNING: non-base64 encoded photo data found and used." . PHP_EOL;
+ file_put_contents($photo_file, $photo_data);
+ }
+
+ // add contact photo to xml
+ $person->addChild("imageURL", $this->config['fritzbox_path'] . $this->config['usb_disk'] . "FRITZ/fonpix/" . basename($photo_file));
+
+ print " Added photo: " . basename($photo_file) . PHP_EOL;
+ }
+ else
+ print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL;
+ }
+ elseif(substr($entry['photo_data'][0], 0, 4) == 'http')
+ {
+ // add contact photo to xml
+ $person->addChild("imageURL", $entry['photo_data'][0]);
+
+ print " Added photo: " . $entry['photo_data'][0] . PHP_EOL;
+ }
+ else
+ print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL;
+ }
+
+ $contact->addChild("services");
+ $contact->addChild("setup");
+ $contact->addChild("mod_time", (string)time());
+ }
+
+ if($root->asXML() !== false)
+ $this->fbxml = $root->asXML();
+ else
+ {
+ print " ERROR: created XML data isn't well-formed." . PHP_EOL;
+ exit(1);
+ }
+ }
+
+ public function _convert_text($text)
+ {
+ $text = htmlspecialchars($text);
+ return $text;
+ }
+
+ public function _concat($text1, $text2)
+ {
+ if($text1 == '')
+ return $text2;
+ elseif($text2 == '')
+ return $text1;
+ else
+ return $text1 . ", " . $text2;
+ }
+
+ public function _parse_fb_result($text)
+ {
+ if(preg_match("/\
([^\<]+)\<\/h2\>/", $text, $matches) == 1 && !empty($matches))
+ return $matches[1];
+ else
+ return "Error while uploading xml to fritzbox";
+ }
+
+ 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 Elements
+ $oldphonebhash = md5(preg_replace("/(\\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("/(\\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;
+
+ // 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
+ return false;
+ }
+ return true;
+ }
+}
diff --git a/carddav2fb.php b/carddav2fb.php
old mode 100755
new mode 100644
index bd9a25fd..3232012b
--- a/carddav2fb.php
+++ b/carddav2fb.php
@@ -1,829 +1,19 @@
(LICNECE: unknown)
- * * CardDAV-PHP (LICENCE: AGPLv3)
- * * fritzbox_api_php (LICENCE: CC-by-SA 3.0)
- *
- * LICENCE (of this file): MIT
- *
- * Autors: Karl Glatz (original author)
- * Martin Rost
- * Jens Maus
- * Johannes Freiburger
- *
- */
-error_reporting(E_ALL);
-setlocale(LC_ALL, 'de_DE.UTF8');
-// Version identifier for CardDAV2FB
-$carddav2fb_version = '1.11 (2016-05-12)';
+namespace Andig;
-// check for the minimum php version
-$php_min_version = '5.3.6';
-if(version_compare(PHP_VERSION, $php_min_version) < 0)
-{
- print 'ERROR: PHP version ' . $php_min_version . ' is required. Found version: ' . PHP_VERSION . PHP_EOL;
- exit(1);
-}
+use Symfony\Component\Console\Application;
-require_once('lib/CardDAV-PHP/carddav.php');
-require_once('lib/vCard-parser/vCard.php');
-require_once('lib/fritzbox_api_php/fritzbox_api.class.php');
+require_once('vendor/autoload.php');
+require_once(__DIR__ . '/config.php');
-if($argc == 2)
- $config_file_name = $argv[1];
-else
- $config_file_name = __DIR__ . '/config.php';
+$app = new Application('CardDAV to FritzBox converter');
-// default/fallback config options
-$config['tmp_dir'] = sys_get_temp_dir();
-$config['fritzbox_ip'] = 'fritz.box';
-$config['fritzbox_ip_ftp'] = $config['fritzbox_ip'];
-$config['fritzbox_force_local_login'] = false;
-$config['phonebook_number'] = '0';
-$config['phonebook_name'] = 'Telefonbuch';
-$config['usb_disk'] = '';
-$config['fritzbox_path'] = 'file:///var/media/ftp/';
-$config['fullname_format'] = 0; // see config.example.php for options
-$config['prefix'] = false;
-$config['suffix'] = false;
-$config['addnames'] = false;
-$config['orgname'] = false;
-$config['build_photos'] = true;
-$config['quickdial_keyword'] = 'Quickdial:';
+$app->addCommands(array(
+ new CardDAVLoaderCommand($config),
+ new VcardToFritzCommand($config),
+ new UploadToFritzCommand($config),
+ new RunCommand($config)
+));
-if(is_file($config_file_name))
- require($config_file_name);
-else
-{
- print 'ERROR: No ' . $config_file_name . ' found, please take a look at config.example.php and create a ' . $config_file_name . ' file!' . PHP_EOL;
- exit(1);
-}
-
-// ---------------------------------------------
-// MAIN
-print "carddav2fb.php " . $carddav2fb_version . " - CardDAV to FRITZ!Box phonebook conversion tool" . PHP_EOL;
-print "Copyright (c) 2012-2016 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger" . PHP_EOL . PHP_EOL;
-
-$client = new CardDAV2FB($config);
-
-// read vcards from webdav
-print 'Retrieving VCards from all CardDAV server(s):' . PHP_EOL;
-$client->get_carddav_entries();
-print 'Done.' . PHP_EOL;
-
-flush(); // in case this script runs by php-cgi
-
-// transform them to a fritzbox compatible xml file
-print 'Converting VCards to FritzBox XML format:' . PHP_EOL;
-$client->build_fb_xml();
-print 'Done.' . PHP_EOL;
-
-flush(); // in case this script runs by php-cgi
-
-// upload the XML-file to the FRITZ!Box (CAUTION: this will overwrite all current entries in the phone book!!)
-print 'Upload data to FRITZ!Box @ ' . $config['fritzbox_ip'] . PHP_EOL;
-if($client->upload_to_fb())
- print 'Done.' . PHP_EOL;
-else
- exit(1);
-
-flush(); // in case this script runs by php-cgi
-
-// ---------------------------------------------
-// Class definition
-class CardDAV2FB
-{
- protected $entries = array();
- protected $fbxml = "";
- protected $config = null;
- protected $tmpdir = null;
-
- public function __construct($config)
- {
- $this->config = $config;
-
- // create a temp directory where we store photos
- $this->tmpdir = $this->mktemp($this->config['tmp_dir']);
- }
-
- public function __destruct()
- {
- // remote temp directory
- $this->rmtemp($this->tmpdir);
- }
-
- // Source: https://php.net/manual/de/function.tempnam.php#61436
- public function mktemp($dir, $prefix = '', $mode = 0700)
- {
- if(substr($dir, -1) != '/')
- $dir .= '/';
-
- do
- {
- $path = $dir . $prefix . mt_rand(0, 9999999);
- }
- while(!mkdir($path, $mode));
-
- return $path;
- }
-
- public function rmtemp($dir)
- {
- if(is_dir($dir))
- {
- $objects = scandir($dir);
- foreach($objects as $object)
- {
- if($object != "." && $object != "..")
- {
- if(filetype($dir . "/" . $object) == "dir")
- rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object);
- }
- }
- reset($objects);
- rmdir($dir);
- }
- }
-
- public function is_base64($str)
- {
- try
- {
- // Check if there are valid base64 characters
- if(!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $str))
- return false;
-
- // Decode the string in strict mode and check the results
- $decoded = base64_decode($str, true);
- if($decoded === false)
- return false;
-
- // Encode the string again
- if(base64_encode($decoded) === $str)
- return true;
- else
- return false;
- }
- catch(Exception $e)
- {
- // If exception is caught, then it is not a base64 encoded string
- return false;
- }
- }
-
- public function base64_to_jpeg($inputfile, $outputfile)
- {
- // read data (binary)
- $ifp = fopen($inputfile, "rb");
- $imageData = fread($ifp, filesize($inputfile));
- fclose($ifp);
-
- // encode & write data (binary)
- $ifp = fopen($outputfile, "wb");
- fwrite($ifp, base64_decode($imageData));
- fclose($ifp);
-
- // return output filename
- return($outputfile);
- }
-
- public function get_carddav_entries()
- {
- $entries = array();
- $snum = 0;
-
- if(is_array($this->config['carddav']))
- {
- foreach($this->config['carddav'] as $conf)
- {
- print " [" . $snum . "]: " . $conf['url'] . " ";
- $carddav = new CardDavPHP\CardDavBackend($conf['url']);
- $carddav->setAuth($conf['user'], $conf['pw']);
-
- // set the vcard extension in case the user
- // defined it in the config
- if(isset($conf['extension']))
- $carddav->setVcardExtension($conf['extension']);
-
- // retrieve data from the CardDAV server now
- $xmldata = $carddav->get();
-
- // identify if we received UTF-8 encoded data from the
- // CardDAV server and if not reencode it since the FRITZ!Box
- // requires UTF-8 encoded data
- if(iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata)
- $xmldata = utf8_encode($xmldata);
-
- // read raw_vcard data from xml response
- $raw_vcards = array();
- $xmlvcard = new SimpleXMLElement($xmldata);
-
- foreach($xmlvcard->element as $vcard_element)
- {
- $id = $vcard_element->id->__toString();
- $value = (string)$vcard_element->vcard->__toString();
- $raw_vcards[$id] = $value;
- }
-
- print " " . count($raw_vcards) . " VCards retrieved." . PHP_EOL;
-
- // parse raw_vcards
- $quick_dial_arr = array();
- foreach($raw_vcards as $v)
- {
- $vcard_obj = new vCard(false, $v);
- $name_arr = null;
- if(isset($vcard_obj->n[0]))
- $name_arr = $vcard_obj->n[0];
- $org_arr = null;
- if(isset($vcard_obj->org[0]))
- $org_arr = $vcard_obj->org[0];
- $addnames = '';
- $prefix = '';
- $suffix = '';
- $orgname = '';
- $formattedname = '';
-
- // Build name Parts if existing ans switch to true in config
- if(isset($name_arr['prefixes']) and $this->config['prefix'])
- $prefix = trim($name_arr['prefixes']);
-
- if(isset($name_arr['suffixes']) and $this->config['suffix'])
- $suffix = trim($name_arr['suffixes']);
-
- if(isset($name_arr['additionalnames']) and $this->config['addnames'])
- $addnames = trim($name_arr['additionalnames']);
-
- if(isset($org_arr['name']) and $this->config['orgname'])
- $orgname = trim($org_arr['name']);
-
- if (isset($vcard_obj->fn[0]))
- $formattedname = $vcard_obj->fn[0];
-
- $firstname = trim($name_arr['firstname']);
- $lastname = trim($name_arr['lastname']);
-
- // the following section implemented different ways of constructing the
- // final phonebook name entry depending on user preferred settings
- // selectable in the config file. Possible options are:
- //
- // $this->config['fullname_format']:
- //
- // 0: "Prefix Lastname, Firstname AdditionalNames Suffix (orgname)"
- // 1: "Prefix Firstname Lastname AdditionalNames Suffix (orgname)"
- // 2: "Prefix Firstname AdditionalNames Lastname Suffix (orgname)"
- //
- $name = '';
- $format = $this->config['fullname_format'];
-
- // Prefix
- if(!empty($prefix))
- $name .= $prefix;
-
- if($format == 0)
- {
- // Lastname
- if(!empty($name) and !empty($lastname))
- $name .= ' ' . $lastname;
- else
- $name .= $lastname;
- }
- else
- {
- // Firstname
- if(!empty($name) and !empty($firstname))
- $name .= ' ' . $firstname;
- else
- $name .= $firstname;
- }
-
- if($format == 2)
- {
- // AdditionalNames
- if(!empty($name) and !empty($addnames))
- $name .= ' ' . $addnames;
- else
- $name .= $addnames;
- }
-
- if($format == 0)
- {
- // Firstname
- if(!empty($name) and !empty($firstname))
- $name .= ', ' . $firstname;
- else
- $name .= $firstname;
- }
- else
- {
- // Lastname
- if(!empty($name) and !empty($lastname))
- $name .= ' ' . $lastname;
- else
- $name .= $lastname;
- }
-
- if($format != 2)
- {
- // AdditionalNames
- if(!empty($name) and !empty($addnames))
- $name .= ' ' . $addnames;
- else
- $name .= $addnames;
- }
-
- // Suffix
- if(!empty($name) and !empty($suffix))
- $name .= ' ' . $suffix;
- else
- $name .= $suffix;
-
- // OrgName
- if(!empty($name) and !empty($orgname))
- $name .= ' (' . $orgname . ')';
- else
- $name .= $orgname;
-
- // make sure to trim whitespaces and double spaces
- $name = trim(str_replace(' ', ' ', $name));
-
- // perform a fallback to formatted name, if we don't have any name and formatted name is available
- if(empty($name) and !empty($formattedname))
- $name = $formattedname;
-
- if(empty($name))
- {
- print ' WARNING: No fullname, lastname, orgname or formatted name found!' . PHP_EOL;
- $name = 'UNKNOWN';
- }
-
- // format filename of contact photo; remove special letters
- if($vcard_obj->photo)
- {
- $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'),
- array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name);
- }
- else
- $photo = '';
-
- // phone
- $phone_no = array();
- if($vcard_obj->categories)
- $categories = $vcard_obj->categories[0];
- else
- $categories = array();
-
- // check for quickdial entry
- if(isset($vcard_obj->note[0]))
- {
- $note = $vcard_obj->note[0];
- $notes = explode($this->config['quickdial_keyword'], $note);
- foreach($notes as $linenr => $linecontent)
- {
- $found = strrpos($linecontent, ":**7");
- if($found > 0)
- {
- $pos_qd_start = strrpos($linecontent, ":**7");
- $quick_dial_for_nr = preg_replace("/[^0-9+]/", "", substr($linecontent, 0, $pos_qd_start));
- $quick_dial_nr = intval(substr($linecontent, $pos_qd_start + 4, 3));
- $quick_dial_arr[$quick_dial_for_nr] = $quick_dial_nr;
- }
- }
- }
-
- // e-mail addresses
- $email_add = array();
- $vip = isset($this->config['group_vip']) && in_array((string)$this->config['group_vip'], $categories);
-
- if(array_key_exists('group_filter', $this->config) && is_array($this->config['group_filter']))
- {
- $add_entry = 0;
- foreach($this->config['group_filter'] as $group_filter)
- {
- if(in_array($group_filter, $categories))
- {
- $add_entry = 1;
- break;
- }
- }
- }
- else
- $add_entry = 1;
-
- if($add_entry == 1)
- {
- foreach($vcard_obj->tel as $t)
- {
- $prio = 0;
- $quickdial = null;
-
- if(!is_array($t) || empty($t['type']))
- {
- $type = "mobile";
- $phone_number = $t;
- }
- else
- {
- $phone_number = $t['value'];
-
- $phone_number_clean = preg_replace("/[^0-9+]/", "", $phone_number);
- foreach($quick_dial_arr as $qd_phone_nr => $value)
- {
- if($qd_phone_nr == $phone_number_clean)
- {
- //Set quickdial
- if($value == 1)
- print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n";
- elseif($value >= 100)
- print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n";
-
- $quickdial = $value;
- }
- }
-
- $typearr_lower = unserialize(strtolower(serialize($t['type'])));
-
- // find out priority
- if(in_array("pref", $typearr_lower))
- $prio = 1;
-
- // set the proper type
- if(in_array("cell", $typearr_lower))
- $type = "mobile";
- elseif(in_array("home", $typearr_lower))
- $type = "home";
- elseif(in_array("fax", $typearr_lower))
- $type = "fax_work";
- elseif(in_array("work", $typearr_lower))
- $type = "work";
- elseif(in_array("other", $typearr_lower))
- $type = "other";
- elseif(in_array("dom", $typearr_lower))
- $type = "other";
- else
- continue;
- }
- $phone_no[] = array("type"=>$type, "prio"=>$prio, "quickdial"=>$quickdial, "value" => $this->_clear_phone_number($phone_number));
- }
-
- // request email address and type
- if($vcard_obj->email)
- {
- foreach($vcard_obj->email as $e)
- {
- if(empty($e['type']))
- {
- $type_email = "work";
- $email = $e;
- }
- else
- {
- $email = $e['value'];
- $typearr_lower = unserialize(strtolower(serialize($e['type'])));
- if(in_array("work", $typearr_lower))
- $type_email = "work";
- elseif(in_array("home", $typearr_lower))
- $type_email = "home";
- elseif(in_array("other", $typearr_lower))
- $type_email = "other";
- else
- continue;
- }
-
- // DEBUG: print out the email address on the console
- //print $type_email.": ".$email."\n";
-
- $email_add[] = array("type"=>$type_email, "value" => $email);
- }
- }
- $entries[] = array("realName" => $name, "telephony" => $phone_no, "email" => $email_add, "vip" => $vip, "photo" => $photo, "photo_data" => $vcard_obj->photo);
- }
- }
-
- $snum++;
- }
- }
-
- $this->entries = $entries;
- }
-
- private function _clear_phone_number($number)
- {
- return preg_replace("/[^0-9+]/", "", $number);
- }
-
- public function build_fb_xml()
- {
- if(empty($this->entries))
- throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!');
-
- // create FB XML in utf-8 format
- $root = new SimpleXMLElement('');
- $pb = $root->phonebook;
- $pb->addAttribute("name", $this->config['phonebook_name']);
-
- foreach($this->entries as $entry)
- {
- $contact = $pb->addChild("contact");
- $contact->addChild("category", $entry['vip']);
- $person = $contact->addChild("person");
- $person->addChild("realName", $this->_convert_text($entry['realName']));
-
- echo " VCard: '" . utf8_decode($entry['realName']) . "'" . PHP_EOL;
-
- // telephone: put the phonenumbers into the fritzbox xml file
- $telephony = $contact->addChild("telephony");
- $id = 0;
- foreach($entry['telephony'] as $tel)
- {
- $num = $telephony->addChild("number", $tel['value']);
- $num->addAttribute("type", $tel['type']);
- $num->addAttribute("vanity", "");
- $num->addAttribute("prio", $tel['prio']);
- $num->addAttribute("id", $id);
-
- if(isset($tel['quickdial']))
- {
- $num->addAttribute("quickdial", $tel['quickdial']);
- print " Added quickdial: " . $tel['quickdial'] . " for: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL;
- }
-
- $id++;
- print " Added phone: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL;
- }
-
- // output a warning if no telephone number was found
- if($id == 0)
- print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL;
-
- // email: put the email addresses into the fritzbox xml file
- $email = $contact->addChild("services");
- $id = 0;
- foreach($entry['email'] as $mail)
- {
- $mail_adr = $email->addChild("email", $mail['value']);
- $mail_adr->addAttribute("classifier", $mail['type']);
- $mail_adr->addAttribute("id", $id);
- $id++;
-
- print " Added email: " . $mail['value'] . " (" . $mail['type'] . ")" . PHP_EOL;
- }
-
- // check for a photo being part of the VCard
- if(($entry['photo']) and ($entry['photo_data']) and (is_array($entry['photo_data'])) and ($entry['photo_data'][0]))
- {
- // check if 'photo_data'[0] is an array as well because then
- // we have to extract ['value'] and friends.
- if(is_array($entry['photo_data'][0]) and (array_key_exists('value', $entry['photo_data'][0])))
- {
- // check if photo_data really contains JPEG data
- if((array_key_exists('type', $entry['photo_data'][0])) and (is_array($entry['photo_data'][0]['type'])) and
- ($entry['photo_data'][0]['type'][0] == 'jpeg' or $entry['photo_data'][0]['type'][0] == 'jpg' or $entry['photo_data'][0]['type'][0] == 'image/jpeg'))
- {
- // get photo, rename, base64 convert and save as jpg
- $photo_data = $entry['photo_data'][0]['value'];
- $photo_version = substr(sha1($photo_data), 0, 5);
- $photo_file = $this->tmpdir . '/' . "{$entry['photo']}_{$photo_version}.jpg";
-
- // check for base64 encoding of the photo data and convert it
- // accordingly.
- if(((array_key_exists('encoding', $entry['photo_data'][0])) and ($entry['photo_data'][0]['encoding'] == 'b')) or $this->is_base64($photo_data))
- {
- file_put_contents($photo_file . ".b64", $photo_data);
- $this->base64_to_jpeg($photo_file . ".b64", $photo_file);
- unlink($photo_file . ".b64");
- }
- else
- {
- print " WARNING: non-base64 encoded photo data found and used." . PHP_EOL;
- file_put_contents($photo_file, $photo_data);
- }
-
- // add contact photo to xml
- $person->addChild("imageURL", $this->config['fritzbox_path'] . $this->config['usb_disk'] . "FRITZ/fonpix/" . basename($photo_file));
-
- print " Added photo: " . basename($photo_file) . PHP_EOL;
- }
- else
- print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL;
- }
- elseif(substr($entry['photo_data'][0], 0, 4) == 'http')
- {
- // add contact photo to xml
- $person->addChild("imageURL", $entry['photo_data'][0]);
-
- print " Added photo: " . $entry['photo_data'][0] . PHP_EOL;
- }
- else
- print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL;
- }
-
- $contact->addChild("services");
- $contact->addChild("setup");
- $contact->addChild("mod_time", (string)time());
- }
-
- if($root->asXML() !== false)
- $this->fbxml = $root->asXML();
- else
- {
- print " ERROR: created XML data isn't well-formed." . PHP_EOL;
- exit(1);
- }
- }
-
- public function _convert_text($text)
- {
- $text = htmlspecialchars($text);
- return $text;
- }
-
- public function _concat($text1, $text2)
- {
- if($text1 == '')
- return $text2;
- elseif($text2 == '')
- return $text1;
- else
- return $text1 . ", " . $text2;
- }
-
- public function _parse_fb_result($text)
- {
- if(preg_match("/\
([^\<]+)\<\/h2\>/", $text, $matches) == 1 && !empty($matches))
- return $matches[1];
- else
- return "Error while uploading xml to fritzbox";
- }
-
- 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 Elements
- $oldphonebhash = md5(preg_replace("/(\\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("/(\\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;
-
- // 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
- return false;
- }
- return true;
- }
-}
+$app->run();
\ No newline at end of file
diff --git a/composer.json b/composer.json
new file mode 100644
index 00000000..0a40202f
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,22 @@
+{
+ "name": "andig/carddav2fb",
+ "type": "project",
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "andig",
+ "email": "cpuidle@gmx.de"
+ }
+ ],
+ "minimum-stability": "dev",
+ "require": {
+ "php": "^7.0",
+ "andig/vcard": "dev-master",
+ "symfony/console": "^4.0@dev"
+ },
+ "autoload": {
+ "psr-4": {
+ "Andig\\": "src/"
+ }
+ }
+}
diff --git a/config.example.php b/config.example.php
index 44b47b65..9e17baf0 100644
--- a/config.example.php
+++ b/config.example.php
@@ -1,75 +1,37 @@
use internal fritzbox storage
-//$config['usb_disk'] = 'Generic-FlashDisk-01';
-
-// many version Fritz!Box use 'file:///var/media/ftp/' others 'file:///var/InternerSpeicher/' to check just export an your current phonebook and have a look at any imageURL tag `file:///var/media/ftp/(HERE_config_from:usb_disk)/FRITZ/fonpix/9.jpg`.
-//$config['fritzbox_path'] = 'file:///var/media/ftp/';
-
-// multiple carddav adressbooks could be specified and will be merged together.
-
-// first
-$config['carddav'][0] = array(
- // URL of first CardDAV address book on cloud storage
- 'url' => 'https://raspserver/owncloud/remote.php/carddav/addressbooks/fritzbox/fb_contacts',
- // user name/password for CardDAV access
- 'user' => 'oc_username',
- 'pw' => 'oc_password',
- // vcf extension
- 'extension' => '.vcf'
-);
-
-// second
-//$config['carddav'][1] = array(
-// 'url' => 'https://raspserver/owncloud/remote.php/carddav/addressbooks/fritzbox/fb_contacts_second',
-// 'user' => 'oc_username',
-// 'pw' => 'oc_password',
-// 'extension' => '.vcf'
-//);
+$config = [
+ // local cards
+ 'file' => '',
+
+ // or server
+ 'server' => [
+ 'url' => 'https://...',
+ 'user' => '',
+ 'password' => '',
+ ],
+
+ // or fritzbox
+ 'fritzbox' => [
+ 'url' => 'http://fritz.box',
+ 'user' => '',
+ 'password' => '',
+ ],
+
+ 'conversions' => [
+ 'realName' => [
+ '{lastname}, {firstname}',
+ '{fullname}',
+ '{organization}'
+ ],
+ 'phoneTypes' => [
+ 'WORK' => 'work',
+ 'HOME' => 'home',
+ 'CELL' => 'mobile'
+ ],
+ 'emailTypes' => [
+ 'WORK' => 'work',
+ 'HOME' => 'home'
+ ]
+ ]
+];
diff --git a/lib/fritzbox_api_php/fritzbox_api.class.php b/lib/fritzbox_api_php/fritzbox_api.class.php
deleted file mode 100644
index bf4296ee..00000000
--- a/lib/fritzbox_api_php/fritzbox_api.class.php
+++ /dev/null
@@ -1,539 +0,0 @@
-
- * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
- * @version 0.5.0b7 2013-01-02
- * @package Fritz!Box PHP tools
- */
-
-/* A simple usage example
- *
- * try
- * {
- * // load the fritzbox_api class
- * require_once('fritzbox_api.class.php');
- * $fritz = new fritzbox_api();
- *
- * // init the output message
- * $message = date('Y-m-d H:i') . ' ';
- *
- * // update the setting
- * $formfields = array(
- * 'telcfg:command/Dial' => '**610',
- * );
- * $fritz->doPostForm($formfields);
- * $message .= 'Phone ' . $dial . ' ringed.';
- * }
- * catch (Exception $e)
- * {
- * $message .= $e->getMessage();
- * }
- *
- * // log the result
- * $fritz->logMessage($message);
- * $fritz = null; // destroy the object to log out
- */
-
-/**
- * the main Fritz!Box API class
- *
- */
-class fritzbox_api {
- /**
- * @var object config object
- */
- public $config = array();
-
- /**
- * @var string the session ID, set by method initSID() after login
- */
- protected $sid = '0000000000000000';
-
-
- /**
- * the constructor, initializes the object and calls the login method
- *
- * @access public
- */
- public function __construct($password = false,$user_name = false,$fritzbox_ip = 'fritz.box',$force_local_login = false)
- {
- // init the config object
- $this->config = new fritzbox_api_config();
-
- // try autoloading the config
- if (file_exists(__DIR__ . '/fritzbox_user.conf.php') && is_readable(__DIR__ . '/fritzbox_user.conf.php') ) {
- require_once(__DIR__ . '/fritzbox_user.conf.php');
- }
-
- // set FRITZ!Box-IP and URL
- $this->config->setItem('fritzbox_ip',$fritzbox_ip);
-
- // check if login on local network (fritz.box) or via a dynamic DNS-host
- if ($fritzbox_ip != 'fritz.box' && !$force_local_login) {
- $this->config->setItem('enable_remote_config',true);
- $this->config->setItem('remote_config_user',$user_name);
- $this->config->setItem('remote_config_password',$password);
- $this->config->setItem('fritzbox_url', 'https://'.$this->config->getItem('fritzbox_ip'));
- } else {
- $this->config->setItem('enable_remote_config',false);
- if($user_name != false){
- $this->config->setItem('username',$user_name);
- }
- if($password != false){
- $this->config->setItem('password',$password);
- }
- $this->config->setItem('fritzbox_url', 'http://' . $this->config->getItem('fritzbox_ip'));
- }
-
- // make some config consistency checks
- if ( $this->config->getItem('enable_remote_config') === true ){
- if ( !$this->config->getItem('remote_config_user') || !$this->config->getItem('remote_config_password') ){
- $this->error('ERROR: Remote config mode enabled, but no username or no password provided');
- }
- }
- else {
- $this->config->setItem('old_remote_config_user', null);
- $this->config->setItem('old_remote_config_password', null);
- }
- $this->sid = $this->initSID();
- }
-
-
- /**
- * the destructor just calls the logout method
- *
- * @access public
- */
- public function __destruct()
- {
- $this->logout();
- }
-
-
- /**
- * do a POST request on the box
- * the main cURL wrapper handles the command
- *
- * @param array $formfields an associative array with the POST fields to pass
- * @return string the raw HTML code returned by the Fritz!Box
- */
- public function doPostForm($formfields = array())
- {
- $ch = curl_init();
-
- if ( isset($formfields['getpage']) && strpos($formfields['getpage'], '.lua') > 0 )
- {
- curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . $formfields['getpage'] . '?sid=' . $this->sid);
- unset($formfields['getpage']);
- }
- else
- {
- // add the sid, if it is already set
- if ($this->sid != '0000000000000000')
- {
- $formfields['sid'] = $this->sid;
- }
- curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . '/cgi-bin/webcm');
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- if ( $this->config->getItem('enable_remote_config') == true )
- {
- // set name of SSL-certificate (must be same as remote-hostname (dynDNS) of FRITZ!Box) and remove port in address if alternate port is given
- if (strpos($this->config->getItem('fritzbox_ip'),":")){
- $ssl_cert_fritzbox = explode(":", $this->config->getItem('fritzbox_ip'));
- $ssl_cert_fritzbox = $ssl_cert_fritzbox[0];
- } else {
- $ssl_cert_fritzbox = $this->config->getItem('fritzbox_ip');
- }
-
- // set SSL-options and path to certificate
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($ch, CURLOPT_CAPATH, '/etc/ssl/certs');
-
- // support for pre FRITZ!OS 5.50 remote config
- if (!$this->config->getItem('use_lua_login_method')){
- curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password'));
- }
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formfields));
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
-
- public function doPostFile($formfields = array(), $filefileds = array())
- {
- $ch = curl_init();
-
- // add the sid, if it is already set
- if ($this->sid != '0000000000000000')
- {
- // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!)
- // therfore we use array_merge to ensuere the foreach outputs 'sid' fist
- $formfields = array_merge(array('sid' => $this->sid), $formfields);
- //$formfields['sid'] = $this->sid;
- }
- curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . '/cgi-bin/firmwarecfg');
- curl_setopt($ch, CURLOPT_POST, 1);
-
- // enable for debugging:
- //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine)
- if(empty( $filefileds )) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query
- }
- // post calculated raw data
- else {
- $header = $this->_create_custom_file_post_header($formfields, $filefileds);
- curl_setopt($ch, CURLOPT_HTTPHEADER , array(
- 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) )
- );
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']);
- }
-
- $output = curl_exec($ch);
-
- // curl error
- if(curl_errno($ch)) {
- $this->error(curl_error($ch)." (".curl_errno($ch).")");
- }
-
- // finger out an error message, if given
- preg_match('@
(.*?)
@is', $output, $matches);
- if (isset($matches[1]))
- {
- $this->error(str_replace(' ', ' ', $matches[1]));
- }
-
- curl_close($ch);
- return $output;
- }
-
- private function _create_custom_file_post_header($postFields, $fileFields) {
- // form field separator
- $delimiter = '-------------' . uniqid();
-
- /*
- // file upload fields: name => array(type=>'mime/type',content=>'raw data')
- $fileFields = array(
- 'file1' => array(
- 'type' => 'text/xml',
- 'content' => '...your raw file content goes here...',
- 'filename' = 'filename.xml'
- ),
- );
- // all other fields (not file upload): name => value
- $postFields = array(
- 'otherformfield' => 'content of otherformfield is this text',
- );
- */
-
- $data = '';
-
- // populate normal fields first (simpler)
- foreach ($postFields as $name => $content) {
- $data .= "--" . $delimiter . "\r\n";
- $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"';
- $data .= "\r\n\r\n";
- $data .= $content;
- $data .= "\r\n";
- }
- // populate file fields
- foreach ($fileFields as $name => $file) {
- $data .= "--" . $delimiter . "\r\n";
- // "filename" attribute is not essential; server-side scripts may use it
- $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' .
- ' filename="' . $file['filename'] . '"' . "\r\n";
-
- //$data .= 'Content-Transfer-Encoding: binary'."\r\n";
- // this is, again, informative only; good practice to include though
- $data .= 'Content-Type: ' . $file['type'] . "\r\n";
- // this endline must be here to indicate end of headers
- $data .= "\r\n";
- // the file itself (note: there's no encoding of any kind)
- $data .= $file['content'] . "\r\n";
- }
- // last delimiter
- $data .= "--" . $delimiter . "--\r\n";
-
- return array('delimiter' => $delimiter, 'data' => $data);
- }
-
- /**
- * do a GET request on the box
- * the main cURL wrapper handles the command
- *
- * @param array $params an associative array with the GET params to pass
- * @return string the raw HTML code returned by the Fritz!Box
- */
- public function doGetRequest($params = array())
- {
- // add the sid, if it is already set
- if ($this->sid != '0000000000000000')
- {
- $params['sid'] = $this->sid;
- }
-
- $ch = curl_init();
- if ( strpos($params['getpage'], '.lua') > 0 )
- {
- $getpage = $params['getpage'] . '?';
- unset($params['getpage']);
- }
- else
- {
- $getpage = '/cgi-bin/webcm?';
- }
- curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . $getpage . http_build_query($params));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPGET, 1);
- if ( $this->config->getItem('enable_remote_config') )
- {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
-
- // support for pre FRITZ!OS 5.50 remote config
- if ( !$this->config->getItem('use_lua_login_method') )
- {
- curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password'));
- }
- }
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
-
-
- /**
- * the login method, handles the secured login-process
- * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks
- * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details
- *
- * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message
- */
- protected function initSID()
- {
- // determine, wich login type we have to use
- if ( $this->config->getItem('use_lua_login_method') == true )
- {
- $loginpage = '/login_sid.lua';
- }
- else
- {
- $loginpage = '../html/login_sid.xml';
- }
-
- // read the current status
- $session_status_simplexml = simplexml_load_string($this->doGetRequest(array('getpage' => $loginpage)));
-
- if ( !is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement' )
- {
- $this->error('Response of initialization call ' . $loginpage . ' in ' . __FUNCTION__ . ' was not xml-formatted.');
- }
-
- // perhaps we already have a SID (i.e. when no password is set)
- if ( $session_status_simplexml->SID != '0000000000000000' )
- {
- return $session_status_simplexml->SID;
- }
- // we have to login and get a new SID
- else
- {
- // the challenge-response magic, pay attention to the mb_convert_encoding()
- $challenge = $session_status_simplexml->Challenge;
-
- // do the login
- $formfields = array(
- 'getpage' => $loginpage,
- );
- if ( $this->config->getItem('use_lua_login_method') )
- {
- if ( $this->config->getItem('enable_remote_config') )
- {
- $formfields['username'] = $this->config->getItem('remote_config_user');
- $response = $challenge . '-' . md5(mb_convert_encoding($challenge . '-' . $this->config->getItem('remote_config_password'), "UCS-2LE", "UTF-8"));
- }
- else
- {
- if ( $this->config->getItem('username') )
- {
- $formfields['username'] = $this->config->getItem('username');
- }
- $response = $challenge . '-' . md5(mb_convert_encoding($challenge . '-' . $this->config->getItem('password'), "UCS-2LE", "UTF-8"));
- }
- $formfields['response'] = $response;
- }
- else
- {
- $response = $challenge . '-' . md5(mb_convert_encoding($challenge . '-' . $this->config->getItem('password'), "UCS-2LE", "UTF-8"));
- $formfields['login:command/response'] = $response;
- }
- $output = $this->doPostForm($formfields);
-
- // finger out the SID from the response
- $session_status_simplexml = simplexml_load_string($output);
- if ( !is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement' )
- {
- $this->error('Response of login call to ' . $loginpage . ' in ' . __FUNCTION__ . ' was not xml-formatted.');
- }
-
- if ( $session_status_simplexml->SID != '0000000000000000' )
- {
- return (string)$session_status_simplexml->SID;
- }
- else
- {
- $this->error('ERROR: Login failed with an unknown response.');
- }
- }
- }
-
-
- /**
- * the logout method just sends a logout command to the Fritz!Box
- *
- */
- protected function logout()
- {
- if ( $this->config->getItem('use_lua_login_method') == true )
- {
- $this->doGetRequest(array('getpage' => '/home/home.lua', 'logout' => '1'));
- }
- else
- {
- $formfields = array(
- 'getpage' => '../html/de/menus/menu2.html',
- 'security:command/logout' => 'logout',
- );
- $this->doPostForm($formfields);
- }
- }
-
-
- /**
- * the error method just throws an Exception
- *
- * @param string $message an error message for the Exception
- */
- public function error($message = null)
- {
- throw new Exception($message);
- }
-
-
- /**
- * a getter for the session ID
- *
- * @return string $this->sid
- */
- public function getSID()
- {
- return $this->sid;
- }
-
- /**
- * log a message
- *
- * @param $message string the message to log
- */
- public function logMessage($message)
- {
- if ( $this->config->getItem('newline') == false )
- {
- $this->config->setItem('newline', (PHP_OS == 'WINNT') ? "\r\n" : "\n");
- }
-
- if ( $this->config->getItem('logging') == 'console' )
- {
- echo $message;
- }
- else if ( $this->config->getItem('logging') == 'silent' || $this->config->getItem('logging') == false )
- {
- // do nothing
- }
- else
- {
- if ( is_writable($this->config->getItem('logging')) || is_writable(dirname($this->config->getItem('logging'))) )
- {
- file_put_contents($this->config->getItem('logging'), $message . $this->config->getItem('newline'), FILE_APPEND);
- }
- else
- {
- echo('Error: Cannot log to non-writeable file or dir: ' . $this->config->getItem('logging'));
- }
- }
- }
-}
-
-class fritzbox_api_config {
- protected $config = array();
-
- public function __construct()
- {
- # use the new .lua login method in current (end 2012) labor and newer firmwares (Fritz!OS 5.50 and up)
- $this->setItem('use_lua_login_method', true);
-
- # set to your Fritz!Box IP address or DNS name (defaults to fritz.box), for remote config mode, use the dyndns-name like example.dyndns.org
- $this->setItem('fritzbox_ip', 'fritz.box');
-
- # if needed, enable remote config here
- #$this->setItem('enable_remote_config', true);
- #$this->setItem('remote_config_user', 'test');
- #$this->setItem('remote_config_password', 'test123');
-
- # set to your Fritz!Box username, if login with username is enabled (will be ignored, when remote config is enabled)
- $this->setItem('username', false);
-
- # set to your Fritz!Box password (defaults to no password)
- $this->setItem('password', false);
-
- # set the logging mechanism (defaults to console logging)
- $this->setItem('logging', 'console'); // output to the console
- #$this->setItem('logging', 'silent'); // do not output anything, be careful with this logging mode
- #$this->setItem('logging', 'tam.log'); // the path to a writeable logfile
-
- # the newline character for the logfile (does not need to be changed in most cases)
- $this->setItem('newline', (PHP_OS == 'WINNT') ? "\r\n" : "\n");
- }
-
- /* gets an item from the config
- *
- * @param $item string the item to get
- * @return mixed the value of the item
- */
- public function getItem($item = 'all')
- {
- if ( $item == 'all' )
- {
- return $this->config;
- }
- elseif ( isset($this->config[$item]) )
- {
- return $this->config[$item];
- }
- return false;
- }
-
- /* sets an item into the config
- *
- * @param $item string the item to set
- * @param $value mixed the value to store into the item
- */
- public function setItem($item, $value)
- {
- $this->config[$item] = $value;
- }
-}
diff --git a/lib/CardDAV-PHP/carddav.php b/src/CardDav/Backend.php
similarity index 99%
rename from lib/CardDAV-PHP/carddav.php
rename to src/CardDav/Backend.php
index 405a051e..3987bcc3 100644
--- a/lib/CardDAV-PHP/carddav.php
+++ b/src/CardDav/Backend.php
@@ -1,4 +1,6 @@
-config = $config;
+ parent::__construct();
+ }
+
+ protected function configure() {
+ $this->setName('carddav')
+ ->setDescription('Load from CardDAV server');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $server = $this->config['server'];
+ $xml = self::load($server['url'], $server['user'], $server['password']);
+
+ echo $xml;
+ }
+
+ public static function load($url, $user, $password) {
+ $backend = new Backend($url);
+ $backend->setAuth($user, $password);
+ return $backend->get();
+ }
+}
diff --git a/src/FritzBox/Api.php b/src/FritzBox/Api.php
new file mode 100644
index 00000000..41fbbd29
--- /dev/null
+++ b/src/FritzBox/Api.php
@@ -0,0 +1,296 @@
+
+ * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
+ * @version 0.5.0b7 2013-01-02
+ * @package Fritz!Box PHP tools
+ */
+
+/* A simple usage example
+ *
+ * try
+ * {
+ * // load the fritzbox_api class
+ * require_once('fritzbox_api.class.php');
+ * $fritz = new fritzbox_api();
+ *
+ * // init the output message
+ * $message = date('Y-m-d H:i') . ' ';
+ *
+ * // update the setting
+ * $formfields = array(
+ * 'telcfg:command/Dial' => '**610',
+ * );
+ * $fritz->doPostForm($formfields);
+ * $message .= 'Phone ' . $dial . ' ringed.';
+ * }
+ * catch (Exception $e)
+ * {
+ * $message .= $e->getMessage();
+ * }
+ *
+ * // log the result
+ * $fritz->logMessage($message);
+ * $fritz = null; // destroy the object to log out
+ */
+
+/**
+ * The main Fritz!Box API class
+ */
+class Api {
+
+ private $username;
+ private $password;
+ private $url;
+
+ /**
+ * @var string the session ID, set by method initSID() after login
+ */
+ protected $sid = '0000000000000000';
+
+ /**
+ * the constructor, initializes the object and calls the login method
+ *
+ * @access public
+ */
+ public function __construct($url = 'https://fritz.box', $user_name = false, $password = false,$force_local_login = false)
+ {
+ // init the config object
+ // $this->config = new Config();
+
+ // set FRITZ!Box-IP and URL
+ $this->url = $url;
+ $this->username = $user_name;
+ $this->password = $password;
+
+ $this->sid = $this->initSID();
+ }
+
+ /**
+ * do a POST request on the box
+ * the main cURL wrapper handles the command
+ *
+ * @param array $formfields an associative array with the POST fields to pass
+ * @return string the raw HTML code returned by the Fritz!Box
+ */
+ public function doPostForm($formfields = array())
+ {
+ $ch = curl_init();
+
+ if ( isset($formfields['getpage']) && strpos($formfields['getpage'], '.lua') > 0 )
+ {
+ curl_setopt($ch, CURLOPT_URL, $this->url . $formfields['getpage'] . '?sid=' . $this->sid);
+ unset($formfields['getpage']);
+ }
+ else
+ {
+ // add the sid, if it is already set
+ if ($this->sid != '0000000000000000')
+ {
+ $formfields['sid'] = $this->sid;
+ }
+ curl_setopt($ch, CURLOPT_URL, $this->url . '/cgi-bin/webcm');
+ }
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formfields));
+ $output = curl_exec($ch);
+ curl_close($ch);
+ return $output;
+ }
+
+ public function doPostFile($formfields = array(), $filefileds = array())
+ {
+ $ch = curl_init();
+
+ // add the sid, if it is already set
+ if ($this->sid != '0000000000000000')
+ {
+ // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!)
+ // therfore we use array_merge to ensuere the foreach outputs 'sid' fist
+ $formfields = array_merge(array('sid' => $this->sid), $formfields);
+ //$formfields['sid'] = $this->sid;
+ }
+ curl_setopt($ch, CURLOPT_URL, $this->url . '/cgi-bin/firmwarecfg');
+ curl_setopt($ch, CURLOPT_POST, 1);
+
+ // enable for debugging:
+ //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+
+ // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine)
+ if(empty( $filefileds )) {
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query
+ }
+ // post calculated raw data
+ else {
+ $header = $this->_create_custom_file_post_header($formfields, $filefileds);
+ curl_setopt($ch, CURLOPT_HTTPHEADER , array(
+ 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) )
+ );
+
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']);
+ }
+
+ $output = curl_exec($ch);
+
+ // curl error
+ if(curl_errno($ch)) {
+ throw new \Exception(curl_error($ch)." (".curl_errno($ch).")");
+ }
+
+ // finger out an error message, if given
+ preg_match('@
(.*?)
@is', $output, $matches);
+ if (isset($matches[1]))
+ {
+ throw new \Exception(str_replace(' ', ' ', $matches[1]));
+ }
+
+ curl_close($ch);
+ return $output;
+ }
+
+ private function _create_custom_file_post_header($postFields, $fileFields) {
+ // form field separator
+ $delimiter = '-------------' . uniqid();
+
+ /*
+ // file upload fields: name => array(type=>'mime/type',content=>'raw data')
+ $fileFields = array(
+ 'file1' => array(
+ 'type' => 'text/xml',
+ 'content' => '...your raw file content goes here...',
+ 'filename' = 'filename.xml'
+ ),
+ );
+ // all other fields (not file upload): name => value
+ $postFields = array(
+ 'otherformfield' => 'content of otherformfield is this text',
+ );
+ */
+
+ $data = '';
+
+ // populate normal fields first (simpler)
+ foreach ($postFields as $name => $content) {
+ $data .= "--" . $delimiter . "\r\n";
+ $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"';
+ $data .= "\r\n\r\n";
+ $data .= $content;
+ $data .= "\r\n";
+ }
+ // populate file fields
+ foreach ($fileFields as $name => $file) {
+ $data .= "--" . $delimiter . "\r\n";
+ // "filename" attribute is not essential; server-side scripts may use it
+ $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' .
+ ' filename="' . $file['filename'] . '"' . "\r\n";
+
+ //$data .= 'Content-Transfer-Encoding: binary'."\r\n";
+ // this is, again, informative only; good practice to include though
+ $data .= 'Content-Type: ' . $file['type'] . "\r\n";
+ // this endline must be here to indicate end of headers
+ $data .= "\r\n";
+ // the file itself (note: there's no encoding of any kind)
+ $data .= $file['content'] . "\r\n";
+ }
+ // last delimiter
+ $data .= "--" . $delimiter . "--\r\n";
+
+ return array('delimiter' => $delimiter, 'data' => $data);
+ }
+
+ /**
+ * do a GET request on the box
+ * the main cURL wrapper handles the command
+ *
+ * @param array $params an associative array with the GET params to pass
+ * @return string the raw HTML code returned by the Fritz!Box
+ */
+ public function doGetRequest($params = array())
+ {
+ // add the sid, if it is already set
+ if ($this->sid != '0000000000000000')
+ {
+ $params['sid'] = $this->sid;
+ }
+
+ if ( strpos($params['getpage'], '.lua') > 0 )
+ {
+ $getpage = $params['getpage'] . '?';
+ unset($params['getpage']);
+ }
+ else
+ {
+ $getpage = '/cgi-bin/webcm?';
+ }
+
+ $url = $this->url . $getpage . http_build_query($params);
+ $output = file_get_contents($url);
+
+ return $output;
+ }
+
+ /**
+ * the login method, handles the secured login-process
+ * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks
+ * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details
+ *
+ * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message
+ */
+ protected function initSID()
+ {
+ $loginpage = '/login_sid.lua';
+
+ // read the current status
+ $login = $this->doGetRequest(array('getpage' => $loginpage));
+
+ $xml = simplexml_load_string($login);
+ if ( $xml->SID != '0000000000000000' )
+ {
+ return $xml->SID;
+ }
+
+ // the challenge-response magic, pay attention to the mb_convert_encoding()
+ $response = $xml->Challenge . '-' . md5(mb_convert_encoding($xml->Challenge . '-' . $this->password, "UCS-2LE", "UTF-8"));
+
+ // do the login
+ $formfields = array(
+ 'getpage' => $loginpage,
+ 'username' => $this->username,
+ 'response' => $response
+ );
+
+ $output = $this->doGetRequest($formfields);
+
+ // finger out the SID from the response
+ $xml = simplexml_load_string($output);
+ if ( $xml->SID != '0000000000000000' )
+ {
+ return (string)$xml->SID;
+ }
+
+ throw new \Exception('ERROR: Login failed with an unknown response.');
+ }
+
+ /**
+ * a getter for the session ID
+ *
+ * @return string $this->sid
+ */
+ public function getSID()
+ {
+ return $this->sid;
+ }
+}
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
new file mode 100644
index 00000000..37e5decc
--- /dev/null
+++ b/src/FritzBox/Converter.php
@@ -0,0 +1,153 @@
+config = $config;
+ }
+
+ public function convert($card)
+ {
+ $this->card = $card;
+
+ // $contact = $xml->addChild('contact');
+ $this->contact = new \SimpleXMLElement('');
+
+ $person = $this->contact->addChild('person');
+ $name = htmlspecialchars($this->getProperty('realName'));
+ $person->addChild('realName', $name);
+ // $person->addChild('ImageURL');
+
+ $this->addPhone();
+ $this->addEmail();
+
+ $person = $this->contact->addChild('setup');
+
+ // print_r($this->contact);
+ // echo($this->contact->asXML().PHP_EOL);
+
+ return $this->contact;
+ }
+
+ private function addPhone()
+ {
+ //
+ // +490358179022
+ // +400746653254
+
+ $telephony = $this->contact->addChild('telephony');
+ $phoneTypes = $this->config->phoneTypes ??
+ ['WORK' => 'work', 'HOME' => 'home', 'CELL' => 'mobile'];
+
+ if (isset($this->card->phone)) {
+ foreach ($this->card->phone as $numberType => $numbers) {
+ foreach ($numbers as $idx => $number) {
+ $phone = $telephony->addChild('number', $number);
+ $phone->addAttribute('id', $idx);
+
+ $type = 'other';
+
+ foreach ($phoneTypes as $type => $value) {
+ if (strpos($numberType, $type) !== false) {
+ $type = $value;
+ if (strpos($numberType, 'FAX') !== false) {
+ $type = 'fax_' . $type;
+ }
+
+ break;
+ }
+ }
+
+ $phone->addAttribute('type', $type);
+
+ if (strpos($numberType, 'pref') !== false) {
+ $phone->addAttribute('prio', 1);
+ }
+
+ // $phone->addAttribute('vanity', '');
+ }
+ }
+ }
+ }
+
+ private function addEmail()
+ {
+ //
+ // KTS.Michaelis.Hannover@evlka.de
+ // Kindertagesstaette@michaelis-hannover.de
+
+ $services = $this->contact->addChild('services');
+ $emailTypes = $this->config->emailTypes ??
+ ['WORK' => 'work', 'HOME' => 'home'];
+
+ if (isset($this->card->email)) {
+ foreach ($this->card->email as $emailType => $addresses) {
+ foreach ($addresses as $idx => $addr) {
+ $email = $services->addChild('email', $addr);
+ $email->addAttribute('id', $idx);
+
+ foreach ($emailTypes as $type => $value) {
+ if (strpos($emailType, $type) !== false) {
+ $email->addAttribute('classifier', $value);
+ break;
+ }
+ }
+
+ // $email->addAttribute('vanity', '');
+ }
+ }
+ }
+ }
+
+ private function getProperty(string $property)
+ {
+ if (null === ($rules = $this->config[$property] ?? null)) {
+ throw new \Exception("Missing conversion definition for `$property`");
+ }
+
+ foreach ($rules as $rule) {
+ // parse rule into tokens
+ $token_format = '/{([^}]+)}/';
+ preg_match_all($token_format, $rule, $tokens);
+
+ if (!count($tokens)) {
+ throw new \Exception("Invalid conversion definition for `$property`");
+ }
+
+ // print_r($tokens);
+ $replacements = [];
+
+ // check card for tokens
+ foreach ($tokens[1] as $idx => $token) {
+ // echo $idx.PHP_EOL;
+ if (isset($this->card->$token) && $this->card->$token) {
+ // echo $tokens[0][$idx].PHP_EOL;
+ $replacements[$token] = $this->card->$token;
+ // echo $this->card->$token;
+ }
+ }
+
+ // check if all tokens found
+ if (count($replacements) !== count($tokens[0])) {
+ continue;
+ }
+
+ // replace
+ return preg_replace_callback($token_format, function($match) use ($replacements) {
+ $token = $match[1];
+ return $replacements[$token];
+ }, $rule);
+ }
+
+ throw new \Exception("No data for conversion `$property`");
+
+ print_r($rules);
+ print_r($this->card);
+ print_r($tokens);
+ }
+}
\ No newline at end of file
diff --git a/src/RunCommand.php b/src/RunCommand.php
new file mode 100644
index 00000000..155acf36
--- /dev/null
+++ b/src/RunCommand.php
@@ -0,0 +1,37 @@
+config = $config;
+ parent::__construct();
+ }
+
+ protected function configure() {
+ $this->setName('run')
+ ->setDescription('Download, convert and upload - all in one');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $server = $this->config['server'];
+ $xml = CardDavLoaderCommand::load($server['url'], $server['user'], $server['password']);
+
+ // parse and convert
+ $cards = VcardToFritzCommand::parse($xml);
+ $xml = VcardToFritzCommand::export($this->config['phonebook'] ?? null, $cards, $this->config['conversions']);
+
+ $fritzbox = $this->config['fritzbox'];
+ UploadToFritzCommand::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
+ }
+}
diff --git a/src/UploadToFritzCommand.php b/src/UploadToFritzCommand.php
new file mode 100644
index 00000000..b23c9a4b
--- /dev/null
+++ b/src/UploadToFritzCommand.php
@@ -0,0 +1,58 @@
+config = $config;
+ parent::__construct();
+ }
+
+ protected function configure() {
+ $this->setName('upload')
+ ->setDescription('Upload to FritzBox')
+ ->addArgument('filename', InputArgument::REQUIRED, 'filename');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $filename = $input->getArgument('filename');
+ $xml = file_get_contents($filename);
+
+ $fritzbox = $this->config['fritzbox'];
+ self::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
+ }
+
+ public static function upload($xml, $url, $user, $password)
+ {
+ $fritz = new Api($url, $user, $password, 1);
+
+ $formfields = array(
+ 'PhonebookId' => 0
+ );
+
+ $filefields = array(
+ 'PhonebookImportFile' => array(
+ 'type' => 'text/xml',
+ 'filename' => 'updatepb.xml',
+ 'content' => $xml,
+ )
+ );
+
+ $result = $fritz->doPostFile($formfields, $filefields); // send the command
+
+ if (strpos($result, 'Das Telefonbuch der FRITZ!Box wurde wiederhergestellt') === false) {
+ throw new \Exception('Upload failed');
+ }
+ }
+}
diff --git a/src/VcardToFritzCommand.php b/src/VcardToFritzCommand.php
new file mode 100644
index 00000000..a0dbfa37
--- /dev/null
+++ b/src/VcardToFritzCommand.php
@@ -0,0 +1,112 @@
+config = $config;
+ parent::__construct();
+ }
+
+ protected function configure() {
+ $this->setName('convert')
+ ->setDescription('Load from CardDAV server')
+ ->addArgument('filename', InputArgument::REQUIRED, 'filename');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $filename = $input->getArgument('filename');
+ $xml = simplexml_load_file($filename);
+
+ // parse
+ $cards = self::parse($xml);
+ // file_put_contents('output.json', print_r($cards, 1));
+
+ // convert
+ $xml = self::export($this->config['phonebook'] ?? null, $cards, $this->config['conversions']);
+
+ echo $xml->asXML();
+ }
+
+ public static function parse($xml)
+ {
+ $cards = [];
+ $groups = [];
+
+ // parse all vcards
+ foreach ($xml->element as $element) {
+ foreach ($element->vcard as $vcard) {
+ $parser = new VCardParser($vcard);
+ $card = $parser->getCardAtIndex(0);
+
+ // separate iCloud groups
+ if (isset($card->xabsmember)) {
+ $groups[$card->fullname] = $card->xabsmember;
+ continue;
+ }
+
+ $cards[] = $card;
+ // print_r($card);
+ }
+ }
+
+ // add category from group membership
+ foreach ($cards as $card) {
+ foreach ($groups as $key => $members) {
+ if (in_array($card->uid, $members)) {
+ $card->category = $key;
+ // print_r($card);
+ break;
+ }
+ }
+ }
+
+ return $cards;
+ }
+
+ // https://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
+ private static function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from) {
+ $toDom = dom_import_simplexml($to);
+ $fromDom = dom_import_simplexml($from);
+ $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
+ }
+
+ public static function export(string $phonebook=null, array $cards, $conversions)
+ {
+ $xml = new SimpleXMLElement(<<
+
+
+
+EOT
+ );
+
+ $root = $xml->xpath('//phonebook')[0];
+ if ($phonebook) {
+ $root->addAttribute('name', $phonebook);
+ }
+
+ $converter = new Converter($conversions);
+
+ foreach ($cards as $card) {
+ $contact = $converter->convert($card);
+ // $root->addChild('contact', $contact);
+ self::xml_adopt($root, $contact);
+ }
+
+ return $xml;
+ }
+}
From 4eb6f61c99412e7dfd05b9b11c7e484599a9dfae Mon Sep 17 00:00:00 2001
From: andig
Date: Fri, 17 Nov 2017 18:01:42 +0100
Subject: [PATCH 002/165] Minor
---
carddav2fb.old.php | 829 ------------------------------------
src/VcardToFritzCommand.php | 4 +-
2 files changed, 2 insertions(+), 831 deletions(-)
delete mode 100755 carddav2fb.old.php
diff --git a/carddav2fb.old.php b/carddav2fb.old.php
deleted file mode 100755
index bd9a25fd..00000000
--- a/carddav2fb.old.php
+++ /dev/null
@@ -1,829 +0,0 @@
- (LICNECE: unknown)
- * * CardDAV-PHP (LICENCE: AGPLv3)
- * * fritzbox_api_php (LICENCE: CC-by-SA 3.0)
- *
- * LICENCE (of this file): MIT
- *
- * Autors: Karl Glatz (original author)
- * Martin Rost
- * Jens Maus
- * Johannes Freiburger
- *
- */
-error_reporting(E_ALL);
-setlocale(LC_ALL, 'de_DE.UTF8');
-
-// Version identifier for CardDAV2FB
-$carddav2fb_version = '1.11 (2016-05-12)';
-
-// check for the minimum php version
-$php_min_version = '5.3.6';
-if(version_compare(PHP_VERSION, $php_min_version) < 0)
-{
- print 'ERROR: PHP version ' . $php_min_version . ' is required. Found version: ' . PHP_VERSION . PHP_EOL;
- exit(1);
-}
-
-require_once('lib/CardDAV-PHP/carddav.php');
-require_once('lib/vCard-parser/vCard.php');
-require_once('lib/fritzbox_api_php/fritzbox_api.class.php');
-
-if($argc == 2)
- $config_file_name = $argv[1];
-else
- $config_file_name = __DIR__ . '/config.php';
-
-// default/fallback config options
-$config['tmp_dir'] = sys_get_temp_dir();
-$config['fritzbox_ip'] = 'fritz.box';
-$config['fritzbox_ip_ftp'] = $config['fritzbox_ip'];
-$config['fritzbox_force_local_login'] = false;
-$config['phonebook_number'] = '0';
-$config['phonebook_name'] = 'Telefonbuch';
-$config['usb_disk'] = '';
-$config['fritzbox_path'] = 'file:///var/media/ftp/';
-$config['fullname_format'] = 0; // see config.example.php for options
-$config['prefix'] = false;
-$config['suffix'] = false;
-$config['addnames'] = false;
-$config['orgname'] = false;
-$config['build_photos'] = true;
-$config['quickdial_keyword'] = 'Quickdial:';
-
-if(is_file($config_file_name))
- require($config_file_name);
-else
-{
- print 'ERROR: No ' . $config_file_name . ' found, please take a look at config.example.php and create a ' . $config_file_name . ' file!' . PHP_EOL;
- exit(1);
-}
-
-// ---------------------------------------------
-// MAIN
-print "carddav2fb.php " . $carddav2fb_version . " - CardDAV to FRITZ!Box phonebook conversion tool" . PHP_EOL;
-print "Copyright (c) 2012-2016 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger" . PHP_EOL . PHP_EOL;
-
-$client = new CardDAV2FB($config);
-
-// read vcards from webdav
-print 'Retrieving VCards from all CardDAV server(s):' . PHP_EOL;
-$client->get_carddav_entries();
-print 'Done.' . PHP_EOL;
-
-flush(); // in case this script runs by php-cgi
-
-// transform them to a fritzbox compatible xml file
-print 'Converting VCards to FritzBox XML format:' . PHP_EOL;
-$client->build_fb_xml();
-print 'Done.' . PHP_EOL;
-
-flush(); // in case this script runs by php-cgi
-
-// upload the XML-file to the FRITZ!Box (CAUTION: this will overwrite all current entries in the phone book!!)
-print 'Upload data to FRITZ!Box @ ' . $config['fritzbox_ip'] . PHP_EOL;
-if($client->upload_to_fb())
- print 'Done.' . PHP_EOL;
-else
- exit(1);
-
-flush(); // in case this script runs by php-cgi
-
-// ---------------------------------------------
-// Class definition
-class CardDAV2FB
-{
- protected $entries = array();
- protected $fbxml = "";
- protected $config = null;
- protected $tmpdir = null;
-
- public function __construct($config)
- {
- $this->config = $config;
-
- // create a temp directory where we store photos
- $this->tmpdir = $this->mktemp($this->config['tmp_dir']);
- }
-
- public function __destruct()
- {
- // remote temp directory
- $this->rmtemp($this->tmpdir);
- }
-
- // Source: https://php.net/manual/de/function.tempnam.php#61436
- public function mktemp($dir, $prefix = '', $mode = 0700)
- {
- if(substr($dir, -1) != '/')
- $dir .= '/';
-
- do
- {
- $path = $dir . $prefix . mt_rand(0, 9999999);
- }
- while(!mkdir($path, $mode));
-
- return $path;
- }
-
- public function rmtemp($dir)
- {
- if(is_dir($dir))
- {
- $objects = scandir($dir);
- foreach($objects as $object)
- {
- if($object != "." && $object != "..")
- {
- if(filetype($dir . "/" . $object) == "dir")
- rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object);
- }
- }
- reset($objects);
- rmdir($dir);
- }
- }
-
- public function is_base64($str)
- {
- try
- {
- // Check if there are valid base64 characters
- if(!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $str))
- return false;
-
- // Decode the string in strict mode and check the results
- $decoded = base64_decode($str, true);
- if($decoded === false)
- return false;
-
- // Encode the string again
- if(base64_encode($decoded) === $str)
- return true;
- else
- return false;
- }
- catch(Exception $e)
- {
- // If exception is caught, then it is not a base64 encoded string
- return false;
- }
- }
-
- public function base64_to_jpeg($inputfile, $outputfile)
- {
- // read data (binary)
- $ifp = fopen($inputfile, "rb");
- $imageData = fread($ifp, filesize($inputfile));
- fclose($ifp);
-
- // encode & write data (binary)
- $ifp = fopen($outputfile, "wb");
- fwrite($ifp, base64_decode($imageData));
- fclose($ifp);
-
- // return output filename
- return($outputfile);
- }
-
- public function get_carddav_entries()
- {
- $entries = array();
- $snum = 0;
-
- if(is_array($this->config['carddav']))
- {
- foreach($this->config['carddav'] as $conf)
- {
- print " [" . $snum . "]: " . $conf['url'] . " ";
- $carddav = new CardDavPHP\CardDavBackend($conf['url']);
- $carddav->setAuth($conf['user'], $conf['pw']);
-
- // set the vcard extension in case the user
- // defined it in the config
- if(isset($conf['extension']))
- $carddav->setVcardExtension($conf['extension']);
-
- // retrieve data from the CardDAV server now
- $xmldata = $carddav->get();
-
- // identify if we received UTF-8 encoded data from the
- // CardDAV server and if not reencode it since the FRITZ!Box
- // requires UTF-8 encoded data
- if(iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata)
- $xmldata = utf8_encode($xmldata);
-
- // read raw_vcard data from xml response
- $raw_vcards = array();
- $xmlvcard = new SimpleXMLElement($xmldata);
-
- foreach($xmlvcard->element as $vcard_element)
- {
- $id = $vcard_element->id->__toString();
- $value = (string)$vcard_element->vcard->__toString();
- $raw_vcards[$id] = $value;
- }
-
- print " " . count($raw_vcards) . " VCards retrieved." . PHP_EOL;
-
- // parse raw_vcards
- $quick_dial_arr = array();
- foreach($raw_vcards as $v)
- {
- $vcard_obj = new vCard(false, $v);
- $name_arr = null;
- if(isset($vcard_obj->n[0]))
- $name_arr = $vcard_obj->n[0];
- $org_arr = null;
- if(isset($vcard_obj->org[0]))
- $org_arr = $vcard_obj->org[0];
- $addnames = '';
- $prefix = '';
- $suffix = '';
- $orgname = '';
- $formattedname = '';
-
- // Build name Parts if existing ans switch to true in config
- if(isset($name_arr['prefixes']) and $this->config['prefix'])
- $prefix = trim($name_arr['prefixes']);
-
- if(isset($name_arr['suffixes']) and $this->config['suffix'])
- $suffix = trim($name_arr['suffixes']);
-
- if(isset($name_arr['additionalnames']) and $this->config['addnames'])
- $addnames = trim($name_arr['additionalnames']);
-
- if(isset($org_arr['name']) and $this->config['orgname'])
- $orgname = trim($org_arr['name']);
-
- if (isset($vcard_obj->fn[0]))
- $formattedname = $vcard_obj->fn[0];
-
- $firstname = trim($name_arr['firstname']);
- $lastname = trim($name_arr['lastname']);
-
- // the following section implemented different ways of constructing the
- // final phonebook name entry depending on user preferred settings
- // selectable in the config file. Possible options are:
- //
- // $this->config['fullname_format']:
- //
- // 0: "Prefix Lastname, Firstname AdditionalNames Suffix (orgname)"
- // 1: "Prefix Firstname Lastname AdditionalNames Suffix (orgname)"
- // 2: "Prefix Firstname AdditionalNames Lastname Suffix (orgname)"
- //
- $name = '';
- $format = $this->config['fullname_format'];
-
- // Prefix
- if(!empty($prefix))
- $name .= $prefix;
-
- if($format == 0)
- {
- // Lastname
- if(!empty($name) and !empty($lastname))
- $name .= ' ' . $lastname;
- else
- $name .= $lastname;
- }
- else
- {
- // Firstname
- if(!empty($name) and !empty($firstname))
- $name .= ' ' . $firstname;
- else
- $name .= $firstname;
- }
-
- if($format == 2)
- {
- // AdditionalNames
- if(!empty($name) and !empty($addnames))
- $name .= ' ' . $addnames;
- else
- $name .= $addnames;
- }
-
- if($format == 0)
- {
- // Firstname
- if(!empty($name) and !empty($firstname))
- $name .= ', ' . $firstname;
- else
- $name .= $firstname;
- }
- else
- {
- // Lastname
- if(!empty($name) and !empty($lastname))
- $name .= ' ' . $lastname;
- else
- $name .= $lastname;
- }
-
- if($format != 2)
- {
- // AdditionalNames
- if(!empty($name) and !empty($addnames))
- $name .= ' ' . $addnames;
- else
- $name .= $addnames;
- }
-
- // Suffix
- if(!empty($name) and !empty($suffix))
- $name .= ' ' . $suffix;
- else
- $name .= $suffix;
-
- // OrgName
- if(!empty($name) and !empty($orgname))
- $name .= ' (' . $orgname . ')';
- else
- $name .= $orgname;
-
- // make sure to trim whitespaces and double spaces
- $name = trim(str_replace(' ', ' ', $name));
-
- // perform a fallback to formatted name, if we don't have any name and formatted name is available
- if(empty($name) and !empty($formattedname))
- $name = $formattedname;
-
- if(empty($name))
- {
- print ' WARNING: No fullname, lastname, orgname or formatted name found!' . PHP_EOL;
- $name = 'UNKNOWN';
- }
-
- // format filename of contact photo; remove special letters
- if($vcard_obj->photo)
- {
- $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'),
- array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name);
- }
- else
- $photo = '';
-
- // phone
- $phone_no = array();
- if($vcard_obj->categories)
- $categories = $vcard_obj->categories[0];
- else
- $categories = array();
-
- // check for quickdial entry
- if(isset($vcard_obj->note[0]))
- {
- $note = $vcard_obj->note[0];
- $notes = explode($this->config['quickdial_keyword'], $note);
- foreach($notes as $linenr => $linecontent)
- {
- $found = strrpos($linecontent, ":**7");
- if($found > 0)
- {
- $pos_qd_start = strrpos($linecontent, ":**7");
- $quick_dial_for_nr = preg_replace("/[^0-9+]/", "", substr($linecontent, 0, $pos_qd_start));
- $quick_dial_nr = intval(substr($linecontent, $pos_qd_start + 4, 3));
- $quick_dial_arr[$quick_dial_for_nr] = $quick_dial_nr;
- }
- }
- }
-
- // e-mail addresses
- $email_add = array();
- $vip = isset($this->config['group_vip']) && in_array((string)$this->config['group_vip'], $categories);
-
- if(array_key_exists('group_filter', $this->config) && is_array($this->config['group_filter']))
- {
- $add_entry = 0;
- foreach($this->config['group_filter'] as $group_filter)
- {
- if(in_array($group_filter, $categories))
- {
- $add_entry = 1;
- break;
- }
- }
- }
- else
- $add_entry = 1;
-
- if($add_entry == 1)
- {
- foreach($vcard_obj->tel as $t)
- {
- $prio = 0;
- $quickdial = null;
-
- if(!is_array($t) || empty($t['type']))
- {
- $type = "mobile";
- $phone_number = $t;
- }
- else
- {
- $phone_number = $t['value'];
-
- $phone_number_clean = preg_replace("/[^0-9+]/", "", $phone_number);
- foreach($quick_dial_arr as $qd_phone_nr => $value)
- {
- if($qd_phone_nr == $phone_number_clean)
- {
- //Set quickdial
- if($value == 1)
- print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n";
- elseif($value >= 100)
- print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n";
-
- $quickdial = $value;
- }
- }
-
- $typearr_lower = unserialize(strtolower(serialize($t['type'])));
-
- // find out priority
- if(in_array("pref", $typearr_lower))
- $prio = 1;
-
- // set the proper type
- if(in_array("cell", $typearr_lower))
- $type = "mobile";
- elseif(in_array("home", $typearr_lower))
- $type = "home";
- elseif(in_array("fax", $typearr_lower))
- $type = "fax_work";
- elseif(in_array("work", $typearr_lower))
- $type = "work";
- elseif(in_array("other", $typearr_lower))
- $type = "other";
- elseif(in_array("dom", $typearr_lower))
- $type = "other";
- else
- continue;
- }
- $phone_no[] = array("type"=>$type, "prio"=>$prio, "quickdial"=>$quickdial, "value" => $this->_clear_phone_number($phone_number));
- }
-
- // request email address and type
- if($vcard_obj->email)
- {
- foreach($vcard_obj->email as $e)
- {
- if(empty($e['type']))
- {
- $type_email = "work";
- $email = $e;
- }
- else
- {
- $email = $e['value'];
- $typearr_lower = unserialize(strtolower(serialize($e['type'])));
- if(in_array("work", $typearr_lower))
- $type_email = "work";
- elseif(in_array("home", $typearr_lower))
- $type_email = "home";
- elseif(in_array("other", $typearr_lower))
- $type_email = "other";
- else
- continue;
- }
-
- // DEBUG: print out the email address on the console
- //print $type_email.": ".$email."\n";
-
- $email_add[] = array("type"=>$type_email, "value" => $email);
- }
- }
- $entries[] = array("realName" => $name, "telephony" => $phone_no, "email" => $email_add, "vip" => $vip, "photo" => $photo, "photo_data" => $vcard_obj->photo);
- }
- }
-
- $snum++;
- }
- }
-
- $this->entries = $entries;
- }
-
- private function _clear_phone_number($number)
- {
- return preg_replace("/[^0-9+]/", "", $number);
- }
-
- public function build_fb_xml()
- {
- if(empty($this->entries))
- throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!');
-
- // create FB XML in utf-8 format
- $root = new SimpleXMLElement('');
- $pb = $root->phonebook;
- $pb->addAttribute("name", $this->config['phonebook_name']);
-
- foreach($this->entries as $entry)
- {
- $contact = $pb->addChild("contact");
- $contact->addChild("category", $entry['vip']);
- $person = $contact->addChild("person");
- $person->addChild("realName", $this->_convert_text($entry['realName']));
-
- echo " VCard: '" . utf8_decode($entry['realName']) . "'" . PHP_EOL;
-
- // telephone: put the phonenumbers into the fritzbox xml file
- $telephony = $contact->addChild("telephony");
- $id = 0;
- foreach($entry['telephony'] as $tel)
- {
- $num = $telephony->addChild("number", $tel['value']);
- $num->addAttribute("type", $tel['type']);
- $num->addAttribute("vanity", "");
- $num->addAttribute("prio", $tel['prio']);
- $num->addAttribute("id", $id);
-
- if(isset($tel['quickdial']))
- {
- $num->addAttribute("quickdial", $tel['quickdial']);
- print " Added quickdial: " . $tel['quickdial'] . " for: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL;
- }
-
- $id++;
- print " Added phone: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL;
- }
-
- // output a warning if no telephone number was found
- if($id == 0)
- print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL;
-
- // email: put the email addresses into the fritzbox xml file
- $email = $contact->addChild("services");
- $id = 0;
- foreach($entry['email'] as $mail)
- {
- $mail_adr = $email->addChild("email", $mail['value']);
- $mail_adr->addAttribute("classifier", $mail['type']);
- $mail_adr->addAttribute("id", $id);
- $id++;
-
- print " Added email: " . $mail['value'] . " (" . $mail['type'] . ")" . PHP_EOL;
- }
-
- // check for a photo being part of the VCard
- if(($entry['photo']) and ($entry['photo_data']) and (is_array($entry['photo_data'])) and ($entry['photo_data'][0]))
- {
- // check if 'photo_data'[0] is an array as well because then
- // we have to extract ['value'] and friends.
- if(is_array($entry['photo_data'][0]) and (array_key_exists('value', $entry['photo_data'][0])))
- {
- // check if photo_data really contains JPEG data
- if((array_key_exists('type', $entry['photo_data'][0])) and (is_array($entry['photo_data'][0]['type'])) and
- ($entry['photo_data'][0]['type'][0] == 'jpeg' or $entry['photo_data'][0]['type'][0] == 'jpg' or $entry['photo_data'][0]['type'][0] == 'image/jpeg'))
- {
- // get photo, rename, base64 convert and save as jpg
- $photo_data = $entry['photo_data'][0]['value'];
- $photo_version = substr(sha1($photo_data), 0, 5);
- $photo_file = $this->tmpdir . '/' . "{$entry['photo']}_{$photo_version}.jpg";
-
- // check for base64 encoding of the photo data and convert it
- // accordingly.
- if(((array_key_exists('encoding', $entry['photo_data'][0])) and ($entry['photo_data'][0]['encoding'] == 'b')) or $this->is_base64($photo_data))
- {
- file_put_contents($photo_file . ".b64", $photo_data);
- $this->base64_to_jpeg($photo_file . ".b64", $photo_file);
- unlink($photo_file . ".b64");
- }
- else
- {
- print " WARNING: non-base64 encoded photo data found and used." . PHP_EOL;
- file_put_contents($photo_file, $photo_data);
- }
-
- // add contact photo to xml
- $person->addChild("imageURL", $this->config['fritzbox_path'] . $this->config['usb_disk'] . "FRITZ/fonpix/" . basename($photo_file));
-
- print " Added photo: " . basename($photo_file) . PHP_EOL;
- }
- else
- print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL;
- }
- elseif(substr($entry['photo_data'][0], 0, 4) == 'http')
- {
- // add contact photo to xml
- $person->addChild("imageURL", $entry['photo_data'][0]);
-
- print " Added photo: " . $entry['photo_data'][0] . PHP_EOL;
- }
- else
- print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL;
- }
-
- $contact->addChild("services");
- $contact->addChild("setup");
- $contact->addChild("mod_time", (string)time());
- }
-
- if($root->asXML() !== false)
- $this->fbxml = $root->asXML();
- else
- {
- print " ERROR: created XML data isn't well-formed." . PHP_EOL;
- exit(1);
- }
- }
-
- public function _convert_text($text)
- {
- $text = htmlspecialchars($text);
- return $text;
- }
-
- public function _concat($text1, $text2)
- {
- if($text1 == '')
- return $text2;
- elseif($text2 == '')
- return $text1;
- else
- return $text1 . ", " . $text2;
- }
-
- public function _parse_fb_result($text)
- {
- if(preg_match("/\
([^\<]+)\<\/h2\>/", $text, $matches) == 1 && !empty($matches))
- return $matches[1];
- else
- return "Error while uploading xml to fritzbox";
- }
-
- 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 Elements
- $oldphonebhash = md5(preg_replace("/(\\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("/(\\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;
-
- // 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
- return false;
- }
- return true;
- }
-}
diff --git a/src/VcardToFritzCommand.php b/src/VcardToFritzCommand.php
index a0dbfa37..cf0de06a 100644
--- a/src/VcardToFritzCommand.php
+++ b/src/VcardToFritzCommand.php
@@ -9,7 +9,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use JeroenDesloovere\VCard\VCardParser;
-use Andig\Converter;
+use Andig\FritzBox\Converter;
use \SimpleXMLElement;
class VcardToFritzCommand extends Command {
@@ -23,7 +23,7 @@ public function __construct($config) {
protected function configure() {
$this->setName('convert')
- ->setDescription('Load from CardDAV server')
+ ->setDescription('Convert Vcard to FritzBox format')
->addArgument('filename', InputArgument::REQUIRED, 'filename');
}
From 1ee143c97089be229529f81c5f3b6bf0df51ee27 Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 18 Nov 2017 10:31:11 +0100
Subject: [PATCH 003/165] Fix command not found
---
carddav2fb.php | 2 +-
src/FritzBox/Api.php | 46 --------------------------------------------
2 files changed, 1 insertion(+), 47 deletions(-)
diff --git a/carddav2fb.php b/carddav2fb.php
index 3232012b..e6916941 100644
--- a/carddav2fb.php
+++ b/carddav2fb.php
@@ -10,7 +10,7 @@
$app = new Application('CardDAV to FritzBox converter');
$app->addCommands(array(
- new CardDAVLoaderCommand($config),
+ new CardDavLoaderCommand($config),
new VcardToFritzCommand($config),
new UploadToFritzCommand($config),
new RunCommand($config)
diff --git a/src/FritzBox/Api.php b/src/FritzBox/Api.php
index 41fbbd29..38c3b7c2 100644
--- a/src/FritzBox/Api.php
+++ b/src/FritzBox/Api.php
@@ -1,49 +1,6 @@
- * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
- * @version 0.5.0b7 2013-01-02
- * @package Fritz!Box PHP tools
- */
-
-/* A simple usage example
- *
- * try
- * {
- * // load the fritzbox_api class
- * require_once('fritzbox_api.class.php');
- * $fritz = new fritzbox_api();
- *
- * // init the output message
- * $message = date('Y-m-d H:i') . ' ';
- *
- * // update the setting
- * $formfields = array(
- * 'telcfg:command/Dial' => '**610',
- * );
- * $fritz->doPostForm($formfields);
- * $message .= 'Phone ' . $dial . ' ringed.';
- * }
- * catch (Exception $e)
- * {
- * $message .= $e->getMessage();
- * }
- *
- * // log the result
- * $fritz->logMessage($message);
- * $fritz = null; // destroy the object to log out
- */
/**
* The main Fritz!Box API class
@@ -54,9 +11,6 @@ class Api {
private $password;
private $url;
- /**
- * @var string the session ID, set by method initSID() after login
- */
protected $sid = '0000000000000000';
/**
From 00294a1af1101e8cc670998e24e7f2acf11074f8 Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 18 Nov 2017 10:31:25 +0100
Subject: [PATCH 004/165] Better version constraints
---
composer.json | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/composer.json b/composer.json
index 0a40202f..a2b85cd7 100644
--- a/composer.json
+++ b/composer.json
@@ -9,10 +9,11 @@
}
],
"minimum-stability": "dev",
+ "prefer-stable": true,
"require": {
"php": "^7.0",
"andig/vcard": "dev-master",
- "symfony/console": "^4.0@dev"
+ "symfony/console": "^3.0"
},
"autoload": {
"psr-4": {
From 2990055b1b36e281e92aac36bc55dae1cb92066c Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 18 Nov 2017 10:33:11 +0100
Subject: [PATCH 005/165] Remove fiel artefact
---
config.example.php | 3 ---
1 file changed, 3 deletions(-)
diff --git a/config.example.php b/config.example.php
index 9e17baf0..4958eb01 100644
--- a/config.example.php
+++ b/config.example.php
@@ -1,9 +1,6 @@
'',
-
// or server
'server' => [
'url' => 'https://...',
From 6972ccfdb72f98541097b280e6d3e74c68b5734c Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 18 Nov 2017 10:35:31 +0100
Subject: [PATCH 006/165] Make executable
---
carddav2fb.php | 1 +
1 file changed, 1 insertion(+)
diff --git a/carddav2fb.php b/carddav2fb.php
index e6916941..de8d5021 100644
--- a/carddav2fb.php
+++ b/carddav2fb.php
@@ -1,3 +1,4 @@
+#!/usr/bin/env php
Date: Sat, 18 Nov 2017 14:02:47 +0100
Subject: [PATCH 007/165] Fix run command
---
config.example.php | 3 +++
...{VcardToFritzCommand.php => ConvertCommand.php} | 6 +++---
...ardDavLoaderCommand.php => DownloadCommand.php} | 2 +-
src/RunCommand.php | 14 ++++++++++----
...{UploadToFritzCommand.php => UploadCommand.php} | 6 +++---
5 files changed, 20 insertions(+), 11 deletions(-)
rename src/{VcardToFritzCommand.php => ConvertCommand.php} (95%)
rename src/{CardDavLoaderCommand.php => DownloadCommand.php} (95%)
rename src/{UploadToFritzCommand.php => UploadCommand.php} (88%)
diff --git a/config.example.php b/config.example.php
index 4958eb01..dc4e5a58 100644
--- a/config.example.php
+++ b/config.example.php
@@ -1,6 +1,9 @@
'iCloud',
+
// or server
'server' => [
'url' => 'https://...',
diff --git a/src/VcardToFritzCommand.php b/src/ConvertCommand.php
similarity index 95%
rename from src/VcardToFritzCommand.php
rename to src/ConvertCommand.php
index cf0de06a..59168366 100644
--- a/src/VcardToFritzCommand.php
+++ b/src/ConvertCommand.php
@@ -12,7 +12,7 @@
use Andig\FritzBox\Converter;
use \SimpleXMLElement;
-class VcardToFritzCommand extends Command {
+class ConvertCommand extends Command {
private $config;
@@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
echo $xml->asXML();
}
- public static function parse($xml)
+ public static function parse(SimpleXMLElement $xml)
{
$cards = [];
$groups = [];
@@ -84,7 +84,7 @@ private static function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
- public static function export(string $phonebook=null, array $cards, $conversions)
+ public static function export(string $phonebook=null, array $cards, array $conversions)
{
$xml = new SimpleXMLElement(<<
diff --git a/src/CardDavLoaderCommand.php b/src/DownloadCommand.php
similarity index 95%
rename from src/CardDavLoaderCommand.php
rename to src/DownloadCommand.php
index ed21e4ce..c0d02442 100644
--- a/src/CardDavLoaderCommand.php
+++ b/src/DownloadCommand.php
@@ -10,7 +10,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Andig\CardDav\Backend;
-class CardDavLoaderCommand extends Command {
+class DownloadCommand extends Command {
private $config;
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 155acf36..8e4d5777 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -24,14 +24,20 @@ protected function configure() {
}
protected function execute(InputInterface $input, OutputInterface $output) {
+ // download
$server = $this->config['server'];
- $xml = CardDavLoaderCommand::load($server['url'], $server['user'], $server['password']);
+ $xmlStr = DownloadCommand::load($server['url'], $server['user'], $server['password']);
// parse and convert
- $cards = VcardToFritzCommand::parse($xml);
- $xml = VcardToFritzCommand::export($this->config['phonebook'] ?? null, $cards, $this->config['conversions']);
+ $xml = simplexml_load_string($xmlStr);
+ $cards = ConvertCommand::parse($xml);
+ $xml = ConvertCommand::export($this->config['phonebook'] ?? null, $cards, $this->config['conversions']);
+
+ // upload
+ $xmlStr = $xml->asXML();
+
$fritzbox = $this->config['fritzbox'];
- UploadToFritzCommand::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
+ UploadCommand::upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
}
}
diff --git a/src/UploadToFritzCommand.php b/src/UploadCommand.php
similarity index 88%
rename from src/UploadToFritzCommand.php
rename to src/UploadCommand.php
index b23c9a4b..3e7a563d 100644
--- a/src/UploadToFritzCommand.php
+++ b/src/UploadCommand.php
@@ -10,7 +10,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Andig\FritzBox\Api;
-class UploadToFritzCommand extends Command {
+class UploadCommand extends Command {
private $config;
@@ -33,12 +33,12 @@ protected function execute(InputInterface $input, OutputInterface $output) {
self::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
}
- public static function upload($xml, $url, $user, $password)
+ public static function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
{
$fritz = new Api($url, $user, $password, 1);
$formfields = array(
- 'PhonebookId' => 0
+ 'PhonebookId' => $phonebook
);
$filefields = array(
From c13468dc29b264cd321f42db5c7debb268df9876 Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 18 Nov 2017 14:03:07 +0100
Subject: [PATCH 008/165] Refactor
---
carddav2fb.php | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
mode change 100644 => 100755 carddav2fb.php
diff --git a/carddav2fb.php b/carddav2fb.php
old mode 100644
new mode 100755
index de8d5021..81a55c76
--- a/carddav2fb.php
+++ b/carddav2fb.php
@@ -11,10 +11,10 @@
$app = new Application('CardDAV to FritzBox converter');
$app->addCommands(array(
- new CardDavLoaderCommand($config),
- new VcardToFritzCommand($config),
- new UploadToFritzCommand($config),
- new RunCommand($config)
+ new RunCommand($config),
+ new DownloadCommand($config),
+ new ConvertCommand($config),
+ new UploadCommand($config)
));
-$app->run();
\ No newline at end of file
+$app->run();
From 2674dc00d37caa4e00174d26fa98661a51d0d7fe Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 18 Nov 2017 16:02:40 +0100
Subject: [PATCH 009/165] Refactor dependencies and conversions
---
composer.json | 1 -
config.example.php | 13 +-
src/ConvertCommand.php | 13 +-
src/FritzBox/Converter.php | 7 +
src/RunCommand.php | 9 +-
src/UploadCommand.php | 4 +-
src/Vcard/Parser.php | 314 +++++++++++++++++++++++++++++++++++++
7 files changed, 346 insertions(+), 15 deletions(-)
create mode 100644 src/Vcard/Parser.php
diff --git a/composer.json b/composer.json
index a2b85cd7..7c1b27b4 100644
--- a/composer.json
+++ b/composer.json
@@ -12,7 +12,6 @@
"prefer-stable": true,
"require": {
"php": "^7.0",
- "andig/vcard": "dev-master",
"symfony/console": "^3.0"
},
"autoload": {
diff --git a/config.example.php b/config.example.php
index dc4e5a58..5a113963 100644
--- a/config.example.php
+++ b/config.example.php
@@ -2,8 +2,11 @@
$config = [
// phonebook
- 'phonebook' => 'iCloud',
-
+ 'phonebook' => [
+ 'id' => 0,
+ 'name' => 'Telefonbuch'
+ ],
+
// or server
'server' => [
'url' => 'https://...',
@@ -32,6 +35,12 @@
'emailTypes' => [
'WORK' => 'work',
'HOME' => 'home'
+ ],
+ 'phoneReplaceCharacters' => [
+ '(' => '',
+ ')' => '',
+ '/' => '',
+ '-' => ' '
]
]
];
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index 59168366..c03d7004 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -8,7 +8,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-use JeroenDesloovere\VCard\VCardParser;
+use Andig\Vcard\Parser;
use Andig\FritzBox\Converter;
use \SimpleXMLElement;
@@ -36,7 +36,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// file_put_contents('output.json', print_r($cards, 1));
// convert
- $xml = self::export($this->config['phonebook'] ?? null, $cards, $this->config['conversions']);
+ $phonebook = $this->config['phonebook'];
+ $xml = self::export($phonebook['name'], $cards, $this->config['conversions']);
echo $xml->asXML();
}
@@ -49,7 +50,7 @@ public static function parse(SimpleXMLElement $xml)
// parse all vcards
foreach ($xml->element as $element) {
foreach ($element->vcard as $vcard) {
- $parser = new VCardParser($vcard);
+ $parser = new Parser($vcard);
$card = $parser->getCardAtIndex(0);
// separate iCloud groups
@@ -84,7 +85,7 @@ private static function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
- public static function export(string $phonebook=null, array $cards, array $conversions)
+ public static function export(string $name='Telefonbuch', array $cards, array $conversions)
{
$xml = new SimpleXMLElement(<<
@@ -95,9 +96,7 @@ public static function export(string $phonebook=null, array $cards, array $conve
);
$root = $xml->xpath('//phonebook')[0];
- if ($phonebook) {
- $root->addAttribute('name', $phonebook);
- }
+ $root->addAttribute('name', $name);
$converter = new Converter($conversions);
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index 37e5decc..c79489a6 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -41,12 +41,19 @@ private function addPhone()
// +400746653254
$telephony = $this->contact->addChild('telephony');
+
+ $replaceCharacters = $this->config['phoneReplaceCharacters'] ?? array();
$phoneTypes = $this->config->phoneTypes ??
['WORK' => 'work', 'HOME' => 'home', 'CELL' => 'mobile'];
if (isset($this->card->phone)) {
foreach ($this->card->phone as $numberType => $numbers) {
foreach ($numbers as $idx => $number) {
+ if (count($replaceCharacters)) {
+ $number = strtr($number, $replaceCharacters);
+ $number = preg_replace('/\s+/', ' ', $number);
+ }
+
$phone = $telephony->addChild('number', $number);
$phone->addAttribute('id', $idx);
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 8e4d5777..bdd6f1e7 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -30,14 +30,15 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// parse and convert
$xml = simplexml_load_string($xmlStr);
-
$cards = ConvertCommand::parse($xml);
- $xml = ConvertCommand::export($this->config['phonebook'] ?? null, $cards, $this->config['conversions']);
+
+ $phonebook = $this->config['phonebook'];
+ $xml = ConvertCommand::export($phonebook['name'], $cards, $this->config['conversions']);
// upload
$xmlStr = $xml->asXML();
-
+
$fritzbox = $this->config['fritzbox'];
- UploadCommand::upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
+ UploadCommand::upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
}
}
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index 3e7a563d..6b8e5f7c 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -30,7 +30,9 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$xml = file_get_contents($filename);
$fritzbox = $this->config['fritzbox'];
- self::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
+ $phonebook = $this->config['phonebook'];
+
+ self::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
}
public static function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
diff --git a/src/Vcard/Parser.php b/src/Vcard/Parser.php
new file mode 100644
index 00000000..c1204212
--- /dev/null
+++ b/src/Vcard/Parser.php
@@ -0,0 +1,314 @@
+content = $content;
+ $this->vcardObjects = [];
+ $this->parse();
+ }
+
+ /**
+ * IteratorAggregate
+ */
+ public function getIterator() {
+ foreach ($this->vcardObjects as $vcard) {
+ yield $vcard;
+ }
+ }
+
+ /**
+ * Fetch all the imported VCards.
+ *
+ * @return array
+ * A list of VCard card data objects.
+ */
+ public function getCards()
+ {
+ return $this->vcardObjects;
+ }
+
+ /**
+ * Fetch the imported VCard at the specified index.
+ *
+ * @throws OutOfBoundsException
+ *
+ * @param int $i
+ *
+ * @return stdClass
+ * The card data object.
+ */
+ public function getCardAtIndex($i)
+ {
+ if (isset($this->vcardObjects[$i])) {
+ return $this->vcardObjects[$i];
+ }
+ throw new \OutOfBoundsException();
+ }
+
+ /**
+ * Start the parsing process.
+ *
+ * This method will populate the data object.
+ */
+ protected function parse()
+ {
+ // Normalize new lines.
+ $this->content = str_replace(["\r\n", "\r"], "\n", $this->content);
+
+ // RFC2425 5.8.1. Line delimiting and folding
+ // Unfolding is accomplished by regarding CRLF immediately followed by
+ // a white space character (namely HTAB ASCII decimal 9 or. SPACE ASCII
+ // decimal 32) as equivalent to no characters at all (i.e., the CRLF
+ // and single white space character are removed).
+ $this->content = preg_replace("/\n(?:[ \t])/", "", $this->content);
+ $lines = explode("\n", $this->content);
+
+ // Parse the VCard, line by line.
+ foreach ($lines as $line) {
+ $line = trim($line);
+
+ if (strtoupper($line) == "BEGIN:VCARD") {
+ $cardData = new \stdClass();
+ } elseif (strtoupper($line) == "END:VCARD") {
+ $this->vcardObjects[] = $cardData;
+ } elseif (!empty($line)) {
+ // Strip grouping information. We don't use the group names. We
+ // simply use a list for entries that have multiple values.
+ // As per RFC, group names are alphanumerical, and end with a
+ // period (.).
+ $line = preg_replace('/^\w+\./', '', $line);
+
+ $type = '';
+ $value = '';
+ @list($type, $value) = explode(':', $line, 2);
+
+ $types = explode(';', $type);
+ $element = strtoupper($types[0]);
+
+ array_shift($types);
+
+ // Normalize types. A type can either be a type-param directly,
+ // or can be prefixed with "type=". E.g.: "INTERNET" or
+ // "type=INTERNET".
+ if (!empty($types)) {
+ $types = array_map(function($type) {
+ return preg_replace('/^type=/i', '', $type);
+ }, $types);
+ }
+
+ $i = 0;
+ $rawValue = false;
+ foreach ($types as $type) {
+ if (preg_match('/base64/', strtolower($type))) {
+ $value = base64_decode($value);
+ unset($types[$i]);
+ $rawValue = true;
+ } elseif (preg_match('/encoding=b/', strtolower($type))) {
+ $value = base64_decode($value);
+ unset($types[$i]);
+ $rawValue = true;
+ } elseif (preg_match('/quoted-printable/', strtolower($type))) {
+ $value = quoted_printable_decode($value);
+ unset($types[$i]);
+ $rawValue = true;
+ } elseif (strpos(strtolower($type), 'charset=') === 0) {
+ try {
+ $value = mb_convert_encoding($value, "UTF-8", substr($type, 8));
+ } catch (\Exception $e) {
+ }
+ unset($types[$i]);
+ }
+ $i++;
+ }
+
+ switch (strtoupper($element)) {
+ case 'FN':
+ $cardData->fullname = $value;
+ break;
+ case 'N':
+ foreach ($this->parseName($value) as $key => $val) {
+ $cardData->{$key} = $val;
+ }
+ break;
+ case 'BDAY':
+ $cardData->birthday = $this->parseBirthday($value);
+ break;
+ case 'ADR':
+ if (!isset($cardData->address)) {
+ $cardData->address = [];
+ }
+ $key = !empty($types) ? implode(';', $types) : 'WORK;POSTAL';
+ $cardData->address[$key][] = $this->parseAddress($value);
+ break;
+ case 'TEL':
+ if (!isset($cardData->phone)) {
+ $cardData->phone = [];
+ }
+ $key = !empty($types) ? implode(';', $types) : 'default';
+ $cardData->phone[$key][] = $value;
+ break;
+ case 'EMAIL':
+ if (!isset($cardData->email)) {
+ $cardData->email = [];
+ }
+ $key = !empty($types) ? implode(';', $types) : 'default';
+ $cardData->email[$key][] = $value;
+ break;
+ case 'REV':
+ $cardData->revision = $value;
+ break;
+ case 'VERSION':
+ $cardData->version = $value;
+ break;
+ case 'ORG':
+ $cardData->organization = $value;
+ break;
+ case 'URL':
+ if (!isset($cardData->url)) {
+ $cardData->url = [];
+ }
+ $key = !empty($types) ? implode(';', $types) : 'default';
+ $cardData->url[$key][] = $value;
+ break;
+ case 'TITLE':
+ $cardData->title = $value;
+ break;
+ case 'PHOTO':
+ if ($rawValue) {
+ $cardData->rawPhoto = $value;
+ } else {
+ $cardData->photo = $value;
+ }
+ break;
+ case 'LOGO':
+ if ($rawValue) {
+ $cardData->rawLogo = $value;
+ } else {
+ $cardData->logo = $value;
+ }
+ break;
+ case 'NOTE':
+ $cardData->note = $this->unescape($value);
+ break;
+ case 'CATEGORIES':
+ $cardData->categories = array_map('trim', explode(',', $value));
+ break;
+ case 'UID':
+ $cardData->uid = $value;
+ break;
+ /* iCloud extended attributes */
+ case 'X-ADDRESSBOOKSERVER-KIND':
+ $cardData->xabskind = $value;
+ break;
+ case 'X-ADDRESSBOOKSERVER-MEMBER':
+ if (!isset($cardData->xabsmember)) {
+ $cardData->xabsmember = [];
+ }
+ $cardData->xabsmember[] = preg_replace('/^urn:uuid:/', '', $value);
+ break;
+ }
+ }
+ }
+ }
+
+ protected function parseName($value)
+ {
+ @list(
+ $lastname,
+ $firstname,
+ $additional,
+ $prefix,
+ $suffix
+ ) = explode(';', $value);
+ return (object) [
+ 'lastname' => $lastname,
+ 'firstname' => $firstname,
+ 'additional' => $additional,
+ 'prefix' => $prefix,
+ 'suffix' => $suffix,
+ ];
+ }
+
+ protected function parseBirthday($value)
+ {
+ return new \DateTime($value);
+ }
+
+ protected function parseAddress($value)
+ {
+ @list(
+ $name,
+ $extended,
+ $street,
+ $city,
+ $region,
+ $zip,
+ $country,
+ ) = explode(';', $value);
+ return (object) [
+ 'name' => $name,
+ 'extended' => $extended,
+ 'street' => $street,
+ 'city' => $city,
+ 'region' => $region,
+ 'zip' => $zip,
+ 'country' => $country,
+ ];
+ }
+
+ /**
+ * Unescape newline characters according to RFC2425 section 5.8.4.
+ * This function will replace escaped line breaks with PHP_EOL.
+ *
+ * @link http://tools.ietf.org/html/rfc2425#section-5.8.4
+ * @param string $text
+ * @return string
+ */
+ protected function unescape($text)
+ {
+ return str_replace("\\n", PHP_EOL, $text);
+ }
+}
From 549e899b4d89f4320a38c0131398012700b7ec60 Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 18 Nov 2017 16:04:06 +0100
Subject: [PATCH 010/165] Updated
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 1797f067..833c3113 100644
--- a/README.md
+++ b/README.md
@@ -1,9 +1,9 @@
# CardDAV contacts import for AVM FRITZ!Box
-This is an entirely simplified version of https://github.com/jens-maus/carddav2fb
+This is an entirely simplified version of https://github.com/jens-maus/carddav2fb. The Vcard parser has been replaced by an extended version of https://github.com/jeroendesloovere/vcard.
## License
-This script is released under Public Domain.
+This script is released under Public Domain, some parts under MIT license. Make sure you understand which parts are which.
## Authors
Copyright (c) 2012-2017 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger, Andreas Götz
From afdd8a0ab39e144e4a3445a204ae1d20c71c2ff4 Mon Sep 17 00:00:00 2001
From: andig
Date: Sun, 19 Nov 2017 12:40:51 +0100
Subject: [PATCH 011/165] Allow specifying config file
---
carddav2fb.php | 9 ++++-----
src/CardDav/Backend.php | 5 ++++-
src/ConfigTrait.php | 28 ++++++++++++++++++++++++++++
src/ConvertCommand.php | 17 ++++++++++-------
src/DownloadCommand.php | 11 +++++------
src/FritzBox/Api.php | 3 ++-
src/FritzBox/Converter.php | 2 +-
src/RunCommand.php | 11 +++++------
src/UploadCommand.php | 11 +++++------
9 files changed, 64 insertions(+), 33 deletions(-)
create mode 100644 src/ConfigTrait.php
diff --git a/carddav2fb.php b/carddav2fb.php
index 81a55c76..23d16102 100755
--- a/carddav2fb.php
+++ b/carddav2fb.php
@@ -6,15 +6,14 @@
use Symfony\Component\Console\Application;
require_once('vendor/autoload.php');
-require_once(__DIR__ . '/config.php');
$app = new Application('CardDAV to FritzBox converter');
$app->addCommands(array(
- new RunCommand($config),
- new DownloadCommand($config),
- new ConvertCommand($config),
- new UploadCommand($config)
+ new RunCommand(),
+ new DownloadCommand(),
+ new ConvertCommand(),
+ new UploadCommand()
));
$app->run();
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 3987bcc3..88eaeb4f 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -2,6 +2,10 @@
namespace Andig\CardDav;
+/**
+ * Extended from https://github.com/jens-maus/carddav2fb
+ */
+
/**
* CardDAV PHP
*
@@ -97,7 +101,6 @@
* @since 24.05.2015
* @version 0.7
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
- *
*/
class Backend
diff --git a/src/ConfigTrait.php b/src/ConfigTrait.php
new file mode 100644
index 00000000..06c3afb2
--- /dev/null
+++ b/src/ConfigTrait.php
@@ -0,0 +1,28 @@
+addOption('config', 'c', InputOption::VALUE_REQUIRED, 'config file', $default);
+ }
+
+ protected function loadConfig(InputInterface $input) {
+ $configFile = $input->getOption('config');
+
+ if (!file_exists($configFile)) {
+ throw new \Exception('Config file ' . $configFile . ' does not exist');
+ }
+
+ require_once($configFile);
+ $this->config = $config;
+ }
+}
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index c03d7004..73d76e09 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -14,26 +14,29 @@
class ConvertCommand extends Command {
- private $config;
-
- public function __construct($config) {
- $this->config = $config;
- parent::__construct();
- }
+ use ConfigTrait;
protected function configure() {
$this->setName('convert')
->setDescription('Convert Vcard to FritzBox format')
+ ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export parse result to json file')
->addArgument('filename', InputArgument::REQUIRED, 'filename');
+
+ $this->addConfig();
}
protected function execute(InputInterface $input, OutputInterface $output) {
+ $this->loadConfig($input);
+
$filename = $input->getArgument('filename');
$xml = simplexml_load_file($filename);
// parse
$cards = self::parse($xml);
- // file_put_contents('output.json', print_r($cards, 1));
+
+ if ($json = $input->getOption('json')) {
+ file_put_contents($json, json_encode($cards, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
+ }
// convert
$phonebook = $this->config['phonebook'];
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index c0d02442..1688f6e4 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -12,19 +12,18 @@
class DownloadCommand extends Command {
- private $config;
-
- public function __construct($config) {
- $this->config = $config;
- parent::__construct();
- }
+ use ConfigTrait;
protected function configure() {
$this->setName('carddav')
->setDescription('Load from CardDAV server');
+
+ $this->addConfig();
}
protected function execute(InputInterface $input, OutputInterface $output) {
+ $this->loadConfig($input);
+
$server = $this->config['server'];
$xml = self::load($server['url'], $server['user'], $server['password']);
diff --git a/src/FritzBox/Api.php b/src/FritzBox/Api.php
index 38c3b7c2..005ffe3e 100644
--- a/src/FritzBox/Api.php
+++ b/src/FritzBox/Api.php
@@ -3,7 +3,8 @@
namespace Andig\FritzBox;
/**
- * The main Fritz!Box API class
+ * Extended from https://github.com/jens-maus/carddav2fb
+ * Public Domain
*/
class Api {
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index c79489a6..368cb4c2 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -51,7 +51,7 @@ private function addPhone()
foreach ($numbers as $idx => $number) {
if (count($replaceCharacters)) {
$number = strtr($number, $replaceCharacters);
- $number = preg_replace('/\s+/', ' ', $number);
+ $number = trim(preg_replace('/\s+/', ' ', $number));
}
$phone = $telephony->addChild('number', $number);
diff --git a/src/RunCommand.php b/src/RunCommand.php
index bdd6f1e7..83e608b5 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -11,19 +11,18 @@
class RunCommand extends Command {
- private $config;
-
- public function __construct($config) {
- $this->config = $config;
- parent::__construct();
- }
+ use ConfigTrait;
protected function configure() {
$this->setName('run')
->setDescription('Download, convert and upload - all in one');
+
+ $this->addConfig();
}
protected function execute(InputInterface $input, OutputInterface $output) {
+ $this->loadConfig($input);
+
// download
$server = $this->config['server'];
$xmlStr = DownloadCommand::load($server['url'], $server['user'], $server['password']);
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index 6b8e5f7c..fd87f4c5 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -12,20 +12,19 @@
class UploadCommand extends Command {
- private $config;
-
- public function __construct($config) {
- $this->config = $config;
- parent::__construct();
- }
+ use ConfigTrait;
protected function configure() {
$this->setName('upload')
->setDescription('Upload to FritzBox')
->addArgument('filename', InputArgument::REQUIRED, 'filename');
+
+ $this->addConfig();
}
protected function execute(InputInterface $input, OutputInterface $output) {
+ $this->loadConfig($input);
+
$filename = $input->getArgument('filename');
$xml = file_get_contents($filename);
From 882c3f07333091a9aaf6b1a6b11921e8b828b805 Mon Sep 17 00:00:00 2001
From: andig
Date: Sun, 19 Nov 2017 13:23:05 +0100
Subject: [PATCH 012/165] Add group mapping
---
config.example.php | 3 +++
src/ConvertCommand.php | 43 ++++++++++++++++++++++++------------------
src/RunCommand.php | 8 +++++---
3 files changed, 33 insertions(+), 21 deletions(-)
diff --git a/config.example.php b/config.example.php
index 5a113963..c04647ab 100644
--- a/config.example.php
+++ b/config.example.php
@@ -22,6 +22,9 @@
],
'conversions' => [
+ 'groupNameToCategory' => [
+ 'ORGA' => 'ORGA'
+ ],
'realName' => [
'{lastname}, {firstname}',
'{fullname}',
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index 73d76e09..f32eac73 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -31,21 +31,23 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$filename = $input->getArgument('filename');
$xml = simplexml_load_file($filename);
+ $conversions = $this->config['conversions'];
+ $phonebook = $this->config['phonebook'];
+
// parse
- $cards = self::parse($xml);
+ $cards = self::parse($xml, $conversions);
if ($json = $input->getOption('json')) {
file_put_contents($json, json_encode($cards, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
}
// convert
- $phonebook = $this->config['phonebook'];
- $xml = self::export($phonebook['name'], $cards, $this->config['conversions']);
+ $xml = self::export($phonebook['name'], $cards, $conversions);
echo $xml->asXML();
}
- public static function parse(SimpleXMLElement $xml)
+ public static function parse(SimpleXMLElement $xml, array $conversions)
{
$cards = [];
$groups = [];
@@ -67,13 +69,18 @@ public static function parse(SimpleXMLElement $xml)
}
}
- // add category from group membership
- foreach ($cards as $card) {
- foreach ($groups as $key => $members) {
- if (in_array($card->uid, $members)) {
- $card->category = $key;
- // print_r($card);
- break;
+ $groupConversions = $conversions['groupNameToCategory'] ?? null;
+
+ if ($groupConversions) {
+ // add category from group membership
+ foreach ($cards as $card) {
+ foreach ($groups as $key => $members) {
+ $category = $groupConversions[$key] ?? null;
+
+ if ($category && in_array($card->uid, $members)) {
+ $card->category = $category;
+ break;
+ }
}
}
}
@@ -81,13 +88,6 @@ public static function parse(SimpleXMLElement $xml)
return $cards;
}
- // https://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
- private static function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from) {
- $toDom = dom_import_simplexml($to);
- $fromDom = dom_import_simplexml($from);
- $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
- }
-
public static function export(string $name='Telefonbuch', array $cards, array $conversions)
{
$xml = new SimpleXMLElement(<<appendChild($toDom->ownerDocument->importNode($fromDom, true));
+ }
}
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 83e608b5..472b64dc 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -28,11 +28,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$xmlStr = DownloadCommand::load($server['url'], $server['user'], $server['password']);
// parse and convert
+ $phonebook = $this->config['phonebook'];
+ $conversions = $this->config['conversions'];
+
$xml = simplexml_load_string($xmlStr);
- $cards = ConvertCommand::parse($xml);
+ $cards = ConvertCommand::parse($xml, $conversions);
- $phonebook = $this->config['phonebook'];
- $xml = ConvertCommand::export($phonebook['name'], $cards, $this->config['conversions']);
+ $xml = ConvertCommand::export($phonebook['name'], $cards, $conversions);
// upload
$xmlStr = $xml->asXML();
From af1af53e2b5a2952ea5c771bc525dc75fc05949e Mon Sep 17 00:00:00 2001
From: andig
Date: Sun, 19 Nov 2017 13:57:46 +0100
Subject: [PATCH 013/165] Add vip marker
---
config.example.php | 5 ++++-
src/FritzBox/Converter.php | 13 +++++++++++++
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/config.example.php b/config.example.php
index c04647ab..49b33751 100644
--- a/config.example.php
+++ b/config.example.php
@@ -23,7 +23,10 @@
'conversions' => [
'groupNameToCategory' => [
- 'ORGA' => 'ORGA'
+ 'ORGA' => 'org'
+ ],
+ 'vipCategories' => [
+ 'org'
],
'realName' => [
'{lastname}, {firstname}',
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index 368cb4c2..ccf64758 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -18,6 +18,8 @@ public function convert($card)
// $contact = $xml->addChild('contact');
$this->contact = new \SimpleXMLElement('');
+ $this->addVip();
+
$person = $this->contact->addChild('person');
$name = htmlspecialchars($this->getProperty('realName'));
$person->addChild('realName', $name);
@@ -34,6 +36,17 @@ public function convert($card)
return $this->contact;
}
+ private function addVip()
+ {
+ if (isset($this->card->category)) {
+ $vipCategories = $this->config->vipCategories ?? array();
+
+ if (in_array($this->card->category, $vipCategories)) {
+ $this->contact->addChild('category', 1);
+ }
+ }
+ }
+
private function addPhone()
{
//
From 4f812525e5e4ae8a986493da6252e67f6a5a7c38 Mon Sep 17 00:00:00 2001
From: andig
Date: Sun, 19 Nov 2017 14:08:03 +0100
Subject: [PATCH 014/165] Added excluding by category
---
config.example.php | 6 +++++-
src/ConvertCommand.php | 14 +++++++++++++-
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/config.example.php b/config.example.php
index 49b33751..cfca84f3 100644
--- a/config.example.php
+++ b/config.example.php
@@ -23,9 +23,13 @@
'conversions' => [
'groupNameToCategory' => [
- 'ORGA' => 'org'
+ 'ORGA' => 'org',
+ 'PERS' => 'pers'
],
'vipCategories' => [
+ 'pers'
+ ],
+ 'excludeCategories' => [
'org'
],
'realName' => [
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index f32eac73..fef5e44f 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -27,7 +27,7 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output) {
$this->loadConfig($input);
-
+
$filename = $input->getArgument('filename');
$xml = simplexml_load_file($filename);
@@ -85,6 +85,18 @@ public static function parse(SimpleXMLElement $xml, array $conversions)
}
}
+ $excludeCategories = $conversions['excludeCategories'] ?? null;
+ if ($excludeCategories) {
+ $cards = array_filter($cards, function($card) use ($excludeCategories) {
+ // filter excluded categories
+ if (isset($card->category)) {
+ return !in_array($card->category, $excludeCategories);
+ }
+
+ return true;
+ });
+ }
+
return $cards;
}
From c3d0720566ea565dd57df165cafd527038fba181 Mon Sep 17 00:00:00 2001
From: andig
Date: Tue, 21 Nov 2017 10:29:20 +0100
Subject: [PATCH 015/165] Fixed conversion config not used
---
src/FritzBox/Converter.php | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index ccf64758..ad3604ea 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -39,7 +39,7 @@ public function convert($card)
private function addVip()
{
if (isset($this->card->category)) {
- $vipCategories = $this->config->vipCategories ?? array();
+ $vipCategories = $this->config['vipCategories'] ?? array();
if (in_array($this->card->category, $vipCategories)) {
$this->contact->addChild('category', 1);
@@ -56,8 +56,7 @@ private function addPhone()
$telephony = $this->contact->addChild('telephony');
$replaceCharacters = $this->config['phoneReplaceCharacters'] ?? array();
- $phoneTypes = $this->config->phoneTypes ??
- ['WORK' => 'work', 'HOME' => 'home', 'CELL' => 'mobile'];
+ $phoneTypes = $this->config['phoneTypes'] ?? array();
if (isset($this->card->phone)) {
foreach ($this->card->phone as $numberType => $numbers) {
@@ -102,8 +101,7 @@ private function addEmail()
// Kindertagesstaette@michaelis-hannover.de
$services = $this->contact->addChild('services');
- $emailTypes = $this->config->emailTypes ??
- ['WORK' => 'work', 'HOME' => 'home'];
+ $emailTypes = $this->config['emailTypes'] ?? array();
if (isset($this->card->email)) {
foreach ($this->card->email as $emailType => $addresses) {
From a305ff3aa1f19e503c2d60ec452dc45e9ae2a824 Mon Sep 17 00:00:00 2001
From: andig
Date: Tue, 21 Nov 2017 10:29:32 +0100
Subject: [PATCH 016/165] Added highlevel logging
---
src/ConvertCommand.php | 2 ++
src/DownloadCommand.php | 7 +++++--
src/RunCommand.php | 8 +++++++-
src/UploadCommand.php | 2 ++
4 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index fef5e44f..cd660881 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -41,6 +41,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
file_put_contents($json, json_encode($cards, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
}
+ error_log(sprintf("Converted %d vcards", count($cards)));
+
// convert
$xml = self::export($phonebook['name'], $cards, $conversions);
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 1688f6e4..f637dd9e 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -25,9 +25,12 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->loadConfig($input);
$server = $this->config['server'];
- $xml = self::load($server['url'], $server['user'], $server['password']);
+ $xmlStr = self::load($server['url'], $server['user'], $server['password']);
+ $xml = simplexml_load_string($xmlStr);
- echo $xml;
+ error_log(sprintf("Downloaded %d vcards", $xml->element));
+
+ echo $xmlStr;
}
public static function load($url, $user, $password) {
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 472b64dc..d80f0091 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -26,20 +26,26 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// download
$server = $this->config['server'];
$xmlStr = DownloadCommand::load($server['url'], $server['user'], $server['password']);
+ $xml = simplexml_load_string($xmlStr);
+
+ error_log(sprintf("Downloaded %d vcards", $xml->element));
// parse and convert
$phonebook = $this->config['phonebook'];
$conversions = $this->config['conversions'];
- $xml = simplexml_load_string($xmlStr);
$cards = ConvertCommand::parse($xml, $conversions);
+ error_log(sprintf("Converted %d vcards", count($cards)));
$xml = ConvertCommand::export($phonebook['name'], $cards, $conversions);
+ // error_log(sprintf("Exported fritz phonebook", count($cards)));
// upload
$xmlStr = $xml->asXML();
$fritzbox = $this->config['fritzbox'];
UploadCommand::upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
+
+ error_log("Uploaded fritz phonebook");
}
}
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index fd87f4c5..bc57090d 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -32,6 +32,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$phonebook = $this->config['phonebook'];
self::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
+
+ error_log("Uploaded fritz phonebook");
}
public static function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
From 76b2a3823dac7e443d1a3cac711f16c5680cd9bf Mon Sep 17 00:00:00 2001
From: andig
Date: Tue, 21 Nov 2017 10:30:26 +0100
Subject: [PATCH 017/165] Minor
---
composer.json | 2 --
1 file changed, 2 deletions(-)
diff --git a/composer.json b/composer.json
index 7c1b27b4..0ca4fe52 100644
--- a/composer.json
+++ b/composer.json
@@ -8,8 +8,6 @@
"email": "cpuidle@gmx.de"
}
],
- "minimum-stability": "dev",
- "prefer-stable": true,
"require": {
"php": "^7.0",
"symfony/console": "^3.0"
From bbafac4127e912f0631c6050c17ae40dd1c158c9 Mon Sep 17 00:00:00 2001
From: andig
Date: Fri, 24 Nov 2017 09:20:20 +0100
Subject: [PATCH 018/165] Fix counting
---
src/DownloadCommand.php | 18 +++++++++++++++---
src/RunCommand.php | 5 +++--
2 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index f637dd9e..57f12d91 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -15,7 +15,7 @@ class DownloadCommand extends Command {
use ConfigTrait;
protected function configure() {
- $this->setName('carddav')
+ $this->setName('download')
->setDescription('Load from CardDAV server');
$this->addConfig();
@@ -26,9 +26,9 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$server = $this->config['server'];
$xmlStr = self::load($server['url'], $server['user'], $server['password']);
- $xml = simplexml_load_string($xmlStr);
- error_log(sprintf("Downloaded %d vcards", $xml->element));
+ $count = self::countCards($xmlStr);
+ error_log(sprintf("\nDownloaded %d vcards", $count));
echo $xmlStr;
}
@@ -38,4 +38,16 @@ public static function load($url, $user, $password) {
$backend->setAuth($user, $password);
return $backend->get();
}
+
+ public static function countCards($xml) {
+ $count = 0;
+ $xml = simplexml_load_string($xml);
+
+ foreach ($xml->element as $element) {
+ foreach ($element->vcard as $vcard) {
+ $count++;
+ }
+ }
+ return $count;
+ }
}
diff --git a/src/RunCommand.php b/src/RunCommand.php
index d80f0091..0da617ba 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -26,14 +26,15 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// download
$server = $this->config['server'];
$xmlStr = DownloadCommand::load($server['url'], $server['user'], $server['password']);
- $xml = simplexml_load_string($xmlStr);
- error_log(sprintf("Downloaded %d vcards", $xml->element));
+ $count = DownloadCommand::countCards($xmlStr);
+ error_log(sprintf("\nDownloaded %d vcards", $count));
// parse and convert
$phonebook = $this->config['phonebook'];
$conversions = $this->config['conversions'];
+ $xml = simplexml_load_string($xmlStr);
$cards = ConvertCommand::parse($xml, $conversions);
error_log(sprintf("Converted %d vcards", count($cards)));
From f18728651d974cc0360f5fc8cf264b2a6c07cfeb Mon Sep 17 00:00:00 2001
From: andig
Date: Fri, 24 Nov 2017 15:26:00 +0100
Subject: [PATCH 019/165] Refactored code and config options
---
composer.json | 3 +-
config.example.php | 23 ++++---
src/ConvertCommand.php | 97 ++------------------------
src/DownloadCommand.php | 23 +------
src/FritzBox/Converter.php | 14 ++--
src/RunCommand.php | 15 ++--
src/UploadCommand.php | 2 +-
src/functions.php | 138 +++++++++++++++++++++++++++++++++++++
8 files changed, 180 insertions(+), 135 deletions(-)
create mode 100644 src/functions.php
diff --git a/composer.json b/composer.json
index 0ca4fe52..1f9b1fdc 100644
--- a/composer.json
+++ b/composer.json
@@ -15,6 +15,7 @@
"autoload": {
"psr-4": {
"Andig\\": "src/"
- }
+ },
+ "files": ["src/functions.php"]
}
}
diff --git a/config.example.php b/config.example.php
index cfca84f3..5c0375e3 100644
--- a/config.example.php
+++ b/config.example.php
@@ -21,16 +21,23 @@
'password' => '',
],
- 'conversions' => [
- 'groupNameToCategory' => [
- 'ORGA' => 'org',
- 'PERS' => 'pers'
+ 'excludes' => [
+ 'category' => [
+ 'ORGA', 'b'
],
- 'vipCategories' => [
- 'pers'
+ 'group' => [
+ 'c', 'd'
],
- 'excludeCategories' => [
- 'org'
+ ],
+
+ 'conversions' => [
+ 'vip' => [
+ 'category' => [
+ 'vip1'
+ ],
+ 'group' => [
+ 'PERS'
+ ],
],
'realName' => [
'{lastname}, {firstname}',
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index cd660881..ea9d25f8 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -8,9 +8,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-use Andig\Vcard\Parser;
-use Andig\FritzBox\Converter;
-use \SimpleXMLElement;
class ConvertCommand extends Command {
@@ -32,104 +29,22 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$xml = simplexml_load_file($filename);
$conversions = $this->config['conversions'];
+ $excludes = $this->config['excludes'];
$phonebook = $this->config['phonebook'];
// parse
- $cards = self::parse($xml, $conversions);
+ $cards = parse($xml, $conversions);
+ $filtered = filter($cards, $excludes);
if ($json = $input->getOption('json')) {
- file_put_contents($json, json_encode($cards, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
+ file_put_contents($json, json_encode($filtered, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
}
- error_log(sprintf("Converted %d vcards", count($cards)));
+ error_log(sprintf("Converted %d vcards", count($filtered)));
// convert
- $xml = self::export($phonebook['name'], $cards, $conversions);
+ $xml = export($phonebook['name'], $filtered, $conversions);
echo $xml->asXML();
}
-
- public static function parse(SimpleXMLElement $xml, array $conversions)
- {
- $cards = [];
- $groups = [];
-
- // parse all vcards
- foreach ($xml->element as $element) {
- foreach ($element->vcard as $vcard) {
- $parser = new Parser($vcard);
- $card = $parser->getCardAtIndex(0);
-
- // separate iCloud groups
- if (isset($card->xabsmember)) {
- $groups[$card->fullname] = $card->xabsmember;
- continue;
- }
-
- $cards[] = $card;
- // print_r($card);
- }
- }
-
- $groupConversions = $conversions['groupNameToCategory'] ?? null;
-
- if ($groupConversions) {
- // add category from group membership
- foreach ($cards as $card) {
- foreach ($groups as $key => $members) {
- $category = $groupConversions[$key] ?? null;
-
- if ($category && in_array($card->uid, $members)) {
- $card->category = $category;
- break;
- }
- }
- }
- }
-
- $excludeCategories = $conversions['excludeCategories'] ?? null;
- if ($excludeCategories) {
- $cards = array_filter($cards, function($card) use ($excludeCategories) {
- // filter excluded categories
- if (isset($card->category)) {
- return !in_array($card->category, $excludeCategories);
- }
-
- return true;
- });
- }
-
- return $cards;
- }
-
- public static function export(string $name='Telefonbuch', array $cards, array $conversions)
- {
- $xml = new SimpleXMLElement(<<
-
-
-
-EOT
- );
-
- $root = $xml->xpath('//phonebook')[0];
- $root->addAttribute('name', $name);
-
- $converter = new Converter($conversions);
-
- foreach ($cards as $card) {
- $contact = $converter->convert($card);
- // $root->addChild('contact', $contact);
- self::xml_adopt($root, $contact);
- }
-
- return $xml;
- }
-
- // https://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
- private static function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from) {
- $toDom = dom_import_simplexml($to);
- $fromDom = dom_import_simplexml($from);
- $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
- }
}
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 57f12d91..712a70e2 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -8,7 +8,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-use Andig\CardDav\Backend;
class DownloadCommand extends Command {
@@ -25,29 +24,11 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->loadConfig($input);
$server = $this->config['server'];
- $xmlStr = self::load($server['url'], $server['user'], $server['password']);
+ $xmlStr = download($server['url'], $server['user'], $server['password']);
- $count = self::countCards($xmlStr);
+ $count = countCards($xmlStr);
error_log(sprintf("\nDownloaded %d vcards", $count));
echo $xmlStr;
}
-
- public static function load($url, $user, $password) {
- $backend = new Backend($url);
- $backend->setAuth($user, $password);
- return $backend->get();
- }
-
- public static function countCards($xml) {
- $count = 0;
- $xml = simplexml_load_string($xml);
-
- foreach ($xml->element as $element) {
- foreach ($element->vcard as $vcard) {
- $count++;
- }
- }
- return $count;
- }
}
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index ad3604ea..45802648 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -2,6 +2,8 @@
namespace Andig\FritzBox;
+use \SimpleXMLElement;
+
class Converter
{
private $config;
@@ -11,12 +13,12 @@ public function __construct($config)
$this->config = $config;
}
- public function convert($card)
+ public function convert($card): SimpleXMLElement
{
$this->card = $card;
// $contact = $xml->addChild('contact');
- $this->contact = new \SimpleXMLElement('');
+ $this->contact = new SimpleXMLElement('');
$this->addVip();
@@ -122,7 +124,7 @@ private function addEmail()
}
}
- private function getProperty(string $property)
+ private function getProperty(string $property): string
{
if (null === ($rules = $this->config[$property] ?? null)) {
throw new \Exception("Missing conversion definition for `$property`");
@@ -162,10 +164,8 @@ private function getProperty(string $property)
}, $rule);
}
- throw new \Exception("No data for conversion `$property`");
+ error_log("No data for conversion `$property`");
- print_r($rules);
- print_r($this->card);
- print_r($tokens);
+ return '';
}
}
\ No newline at end of file
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 0da617ba..6c3dce96 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -25,27 +25,30 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// download
$server = $this->config['server'];
- $xmlStr = DownloadCommand::load($server['url'], $server['user'], $server['password']);
+ $xmlStr = download($server['url'], $server['user'], $server['password']);
- $count = DownloadCommand::countCards($xmlStr);
+ $count = countCards($xmlStr);
error_log(sprintf("\nDownloaded %d vcards", $count));
// parse and convert
$phonebook = $this->config['phonebook'];
$conversions = $this->config['conversions'];
+ $excludes = $this->config['excludes'];
$xml = simplexml_load_string($xmlStr);
- $cards = ConvertCommand::parse($xml, $conversions);
- error_log(sprintf("Converted %d vcards", count($cards)));
+ $cards = parse($xml, $conversions);
+ $filtered = filter($cards, $excludes);
+ error_log(sprintf("Converted %d vcards", count($filtered)));
- $xml = ConvertCommand::export($phonebook['name'], $cards, $conversions);
+ // fritzbox format
+ $xml = export($phonebook['name'], $filtered, $conversions);
// error_log(sprintf("Exported fritz phonebook", count($cards)));
// upload
$xmlStr = $xml->asXML();
$fritzbox = $this->config['fritzbox'];
- UploadCommand::upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
+ load($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
error_log("Uploaded fritz phonebook");
}
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index bc57090d..1c4ec37a 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -31,7 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$fritzbox = $this->config['fritzbox'];
$phonebook = $this->config['phonebook'];
- self::upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
+ upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
error_log("Uploaded fritz phonebook");
}
diff --git a/src/functions.php b/src/functions.php
new file mode 100644
index 00000000..bf43619a
--- /dev/null
+++ b/src/functions.php
@@ -0,0 +1,138 @@
+setAuth($user, $password);
+ return $backend->get();
+}
+
+function countCards($xml): int {
+ $count = 0;
+ $xml = simplexml_load_string($xml);
+
+ foreach ($xml->element as $element) {
+ foreach ($element->vcard as $vcard) {
+ $count++;
+ }
+ }
+ return $count;
+}
+
+function parse(SimpleXMLElement $xml, array $conversions): array
+{
+ $cards = [];
+ $groups = [];
+
+ // parse all vcards
+ foreach ($xml->element as $element) {
+ foreach ($element->vcard as $vcard) {
+ $parser = new Parser($vcard);
+ $card = $parser->getCardAtIndex(0);
+
+ // separate iCloud groups
+ if (isset($card->xabsmember)) {
+ $groups[$card->fullname] = $card->xabsmember;
+ continue;
+ }
+
+ $cards[] = $card;
+ // print_r($card);
+ }
+ }
+
+ // assign group memberships
+ foreach ($cards as $card) {
+ foreach ($groups as $group => $members) {
+ if (in_array($card->uid, $members)) {
+ if (!isset($card->group)) {
+ $card->group = array();
+ }
+
+ $card->group = $group;
+ break;
+ }
+ }
+ }
+
+ return $cards;
+}
+
+function filter(array $cards, array $filters): array
+{
+ $result = [];
+
+ foreach ($cards as $card) {
+ $filterMatched = false;
+
+ foreach ($filters as $filterAttribute => $filterValues) {
+ if (isset($card->$filterAttribute)) {
+ if (filterMatches($card->$filterAttribute, $filterValues)) {
+ $filterMatched = true;
+ break;
+ }
+ }
+ }
+
+ if ($filterMatched) {
+ break;
+ }
+
+ $result[] = $card;
+ }
+
+ return $result;
+}
+
+function filterMatches($attribute, $filterValues): bool {
+ if (!is_array($filterValues)) {
+ $filterValues = array($filterMatches);
+ }
+
+ foreach ($filterValues as $filter) {
+ if ($attribute === $filter) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+function export(string $name, array $cards, array $conversions): SimpleXMLElement
+{
+ $xml = new SimpleXMLElement(<<
+
+
+
+EOT
+ );
+
+ $root = $xml->xpath('//phonebook')[0];
+ $root->addAttribute('name', $name);
+
+ $converter = new Converter($conversions);
+
+ foreach ($cards as $card) {
+ $contact = $converter->convert($card);
+ // $root->addChild('contact', $contact);
+ xml_adopt($root, $contact);
+ }
+
+ return $xml;
+}
+
+// https://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
+function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
+{
+ $toDom = dom_import_simplexml($to);
+ $fromDom = dom_import_simplexml($from);
+ $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
+}
From 089950122cfa31fad341f91e3143f076fbdb52fb Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Fri, 24 Nov 2017 21:01:13 +0100
Subject: [PATCH 020/165] Preparations for launch
Adding information from testteam
---
README.md | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/README.md b/README.md
index 833c3113..2ddfd060 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,20 @@
This is an entirely simplified version of https://github.com/jens-maus/carddav2fb. The Vcard parser has been replaced by an extended version of https://github.com/jeroendesloovere/vcard.
+## Requirements
+PHP 7.0 (apt-get install php7.0 php7.0-curl php7.0-mbstring php7.0-xml)
+Composer (follow the installation guide at https://getcomposer.org/download/)
+
+## Installation
+Install requirements
+cammand: git clone https://github.com/andig/carddav2fb.git
+change into the new carddav2fb directory
+command: composer install
+edit config.example.php and save as config.php
+
+## Usage
+command: php carddav2fb.php run
+
## License
This script is released under Public Domain, some parts under MIT license. Make sure you understand which parts are which.
From aa6b430cf8a65880f52c00ea6bde2a733c9d8778 Mon Sep 17 00:00:00 2001
From: andig
Date: Sun, 26 Nov 2017 20:54:14 +0100
Subject: [PATCH 021/165] Fix function name
---
src/RunCommand.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 6c3dce96..24cdc865 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$xmlStr = $xml->asXML();
$fritzbox = $this->config['fritzbox'];
- load($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
+ upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
error_log("Uploaded fritz phonebook");
}
From b4c0c70cf0f04ee32434c8c51d2a59a17f7c9c45 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 10:33:23 +0100
Subject: [PATCH 022/165] Refactor upload function
---
src/UploadCommand.php | 24 ------------------------
src/functions.php | 25 +++++++++++++++++++++++++
2 files changed, 25 insertions(+), 24 deletions(-)
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index 1c4ec37a..0f9285fa 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -8,7 +8,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-use Andig\FritzBox\Api;
class UploadCommand extends Command {
@@ -35,27 +34,4 @@ protected function execute(InputInterface $input, OutputInterface $output) {
error_log("Uploaded fritz phonebook");
}
-
- public static function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
- {
- $fritz = new Api($url, $user, $password, 1);
-
- $formfields = array(
- 'PhonebookId' => $phonebook
- );
-
- $filefields = array(
- 'PhonebookImportFile' => array(
- 'type' => 'text/xml',
- 'filename' => 'updatepb.xml',
- 'content' => $xml,
- )
- );
-
- $result = $fritz->doPostFile($formfields, $filefields); // send the command
-
- if (strpos($result, 'Das Telefonbuch der FRITZ!Box wurde wiederhergestellt') === false) {
- throw new \Exception('Upload failed');
- }
- }
}
diff --git a/src/functions.php b/src/functions.php
index bf43619a..e4d27340 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -5,6 +5,7 @@
use Andig\CardDav\Backend;
use Andig\Vcard\Parser;
use Andig\FritzBox\Converter;
+use Andig\FritzBox\Api;
use \SimpleXMLElement;
@@ -136,3 +137,27 @@ function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
$fromDom = dom_import_simplexml($from);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
+
+
+function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
+{
+ $fritz = new Api($url, $user, $password, 1);
+
+ $formfields = array(
+ 'PhonebookId' => $phonebook
+ );
+
+ $filefields = array(
+ 'PhonebookImportFile' => array(
+ 'type' => 'text/xml',
+ 'filename' => 'updatepb.xml',
+ 'content' => $xml,
+ )
+ );
+
+ $result = $fritz->doPostFile($formfields, $filefields); // send the command
+
+ if (strpos($result, 'Das Telefonbuch der FRITZ!Box wurde wiederhergestellt') === false) {
+ throw new \Exception('Upload failed');
+ }
+}
\ No newline at end of file
From 6d29520969ee6b7de221c981a541f024ba4ea5ed Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 10:41:03 +0100
Subject: [PATCH 023/165] Add download progress indicator
---
src/CardDav/Backend.php | 1333 ++++++++++++++++++++-------------------
src/DownloadCommand.php | 10 +-
src/RunCommand.php | 10 +-
src/functions.php | 3 +-
4 files changed, 690 insertions(+), 666 deletions(-)
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 88eaeb4f..bdd76928 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -105,670 +105,677 @@
class Backend
{
- /**
- * CardDAV PHP Version
- *
- * @constant string
- */
- const VERSION = '0.7';
-
- /**
- * User agent displayed in http requests
- *
- * @constant string
- */
- const USERAGENT = 'CardDAV PHP/';
-
- /**
- * CardDAV server url
- *
- * @var string
- */
- private $url = null;
-
- /**
- * CardDAV server url_parts
- *
- * @var array
- */
- private $url_parts = null;
-
- /**
- * VCard File URL Extension
- *
- * @var string
- */
- private $url_vcard_extension = '.vcf';
-
- /**
- * Authentication string
- *
- * @var string
- */
- private $auth = null;
-
- /**
- * Authentication: username
- *
- * @var string
- */
- private $username = null;
-
- /**
- * Authentication: password
- *
- * @var string
- */
- private $password = null;
-
- /**
- * Characters used for vCard id generation
- *
- * @var array
- */
- private $vcard_id_chars = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F');
-
- /**
- * CardDAV server connection (curl handle)
- *
- * @var resource
- */
- private $curl;
-
- /**
- * Follow redirects (Location Header)
- *
- * @var boolean
- */
- private $follow_redirects = true;
-
- /**
- * Maximum redirects to follow
- *
- * @var integer
- */
- private $follow_redirects_count = 3;
-
- /**
- * Debug on or off
- *
- * @var boolean
- */
- private $debug = false;
-
- /**
- * All available debug information
- *
- * @var array
- */
- private $debug_information = array();
-
- /**
- * Exception codes
- */
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET = 1000;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD = 1001;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_XML_VCARD = 1002;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_DELETE = 1003;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_ADD = 1004;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE = 1005;
- const EXCEPTION_MALFORMED_XML_RESPONSE = 1006;
- const EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID = 1007;
-
-
- /**
- * Constructor
- * Sets the CardDAV server url
- *
- * @param string $url CardDAV server url
- */
- public function __construct($url = null)
- {
- if ($url !== null) {
- $this->setUrl($url);
- }
- }
-
- /**
- * Sets debug information
- *
- * @param array $debug_information Debug information
- * @return void
- */
- public function setDebug(array $debug_information)
- {
- $this->debug_information[] = $debug_information;
- }
-
- /**
- * Sets the CardDAV server url
- *
- * @param string $url CardDAV server url
- * @return void
- */
- public function setUrl($url)
- {
- $this->url = $url;
-
- if (substr($this->url, -1, 1) !== '/') {
- $this->url = $this->url . '/';
- }
-
- $this->url_parts = parse_url($this->url);
-
- // workaround for providers that don't use the default .vcf extension
- if (strpos($this->url, "google.com"))
- {
- $this->setVcardExtension("");
- }
- }
-
- /**
- * Sets the CardDAV vcard url extension
- *
- * Most providers do requests handling Vcards with .vcf, however
- * this isn't always the case and some providers (such as Google)
- * returned a 404 if the .vcf extension is used - or the other
- * way around, returning 404 unless .vcf is used.
- *
- * Both approaches are technically correct, see rfc635
- * http://tools.ietf.org/html/rfc6352
- *
- *
- * @param string $extension File extension
- * @return void
- */
- public function setVcardExtension($extension)
- {
- $this->url_vcard_extension = $extension;
- }
+ /**
+ * CardDAV PHP Version
+ *
+ * @constant string
+ */
+ const VERSION = '0.7';
+
+ /**
+ * User agent displayed in http requests
+ *
+ * @constant string
+ */
+ const USERAGENT = 'CardDAV PHP/';
+
+ /**
+ * CardDAV server url
+ *
+ * @var string
+ */
+ private $url = null;
+
+ /**
+ * CardDAV server url_parts
+ *
+ * @var array
+ */
+ private $url_parts = null;
+
+ /**
+ * VCard File URL Extension
+ *
+ * @var string
+ */
+ private $url_vcard_extension = '.vcf';
+
+ /**
+ * Authentication string
+ *
+ * @var string
+ */
+ private $auth = null;
+
+ /**
+ * Authentication: username
+ *
+ * @var string
+ */
+ private $username = null;
+
+ /**
+ * Authentication: password
+ *
+ * @var string
+ */
+ private $password = null;
+
+ /**
+ * Characters used for vCard id generation
+ *
+ * @var array
+ */
+ private $vcard_id_chars = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F');
+
+ /**
+ * CardDAV server connection (curl handle)
+ *
+ * @var resource
+ */
+ private $curl;
+
+ /**
+ * Follow redirects (Location Header)
+ *
+ * @var boolean
+ */
+ private $follow_redirects = true;
+
+ /**
+ * Maximum redirects to follow
+ *
+ * @var integer
+ */
+ private $follow_redirects_count = 3;
+
+ /**
+ * Debug on or off
+ *
+ * @var boolean
+ */
+ private $debug = false;
+
+ /**
+ * All available debug information
+ *
+ * @var array
+ */
+ private $debug_information = array();
+
+ /**
+ * Progress callback
+ */
+ private $callback;
+
+ /**
+ * Exception codes
+ */
+ const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET = 1000;
+ const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD = 1001;
+ const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_XML_VCARD = 1002;
+ const EXCEPTION_WRONG_HTTP_STATUS_CODE_DELETE = 1003;
+ const EXCEPTION_WRONG_HTTP_STATUS_CODE_ADD = 1004;
+ const EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE = 1005;
+ const EXCEPTION_MALFORMED_XML_RESPONSE = 1006;
+ const EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID = 1007;
+
+
+ /**
+ * Constructor
+ * Sets the CardDAV server url
+ *
+ * @param string $url CardDAV server url
+ */
+ public function __construct($url = null)
+ {
+ if ($url !== null) {
+ $this->setUrl($url);
+ }
+ }
+
+ /**
+ * Set progress callback
+ */
+ public function setProgress($callback = null)
+ {
+ $this->callback = $callback;
+ }
+
+ /**
+ * Sets debug information
+ *
+ * @param array $debug_information Debug information
+ * @return void
+ */
+ public function setDebug(array $debug_information)
+ {
+ $this->debug_information[] = $debug_information;
+ }
+
+ /**
+ * Sets the CardDAV server url
+ *
+ * @param string $url CardDAV server url
+ * @return void
+ */
+ public function setUrl($url)
+ {
+ $this->url = $url;
+
+ if (substr($this->url, -1, 1) !== '/') {
+ $this->url = $this->url . '/';
+ }
+
+ $this->url_parts = parse_url($this->url);
+
+ // workaround for providers that don't use the default .vcf extension
+ if (strpos($this->url, "google.com"))
+ {
+ $this->setVcardExtension("");
+ }
+ }
+
+ /**
+ * Sets the CardDAV vcard url extension
+ *
+ * Most providers do requests handling Vcards with .vcf, however
+ * this isn't always the case and some providers (such as Google)
+ * returned a 404 if the .vcf extension is used - or the other
+ * way around, returning 404 unless .vcf is used.
+ *
+ * Both approaches are technically correct, see rfc635
+ * http://tools.ietf.org/html/rfc6352
+ *
+ *
+ * @param string $extension File extension
+ * @return void
+ */
+ public function setVcardExtension($extension)
+ {
+ $this->url_vcard_extension = $extension;
+ }
- /**
- * Sets authentication information
- *
- * @param string $username CardDAV server username
- * @param string $password CardDAV server password
- * @return void
- */
- public function setAuth($username, $password)
- {
- $this->username = $username;
- $this->password = $password;
- $this->auth = $username . ':' . $password;
- }
-
- /**
- * Sets wether to follow redirects and if yes how often
- *
- * @param boolean $follow_redirects
- * @param integer $follow_redirects_count
- * @return void
- */
- public function setFollowRedirects($follow_redirects, $follow_redirects_count = 3)
- {
- $this->follow_redirects = $follow_redirects && $follow_redirects_count > 0;
- $this->follow_redirects_count = $follow_redirects_count > 0 ? $follow_redirects_count : 0;
- }
-
- /**
- * Gets all available debug information
- *
- * @return array $this->debug_information All available debug information
- */
- public function getDebug()
- {
- return $this->debug_information;
- }
-
- /**
- * Gets all vCards including additional information from the CardDAV server
- *
- * @param boolean $include_vcards Include vCards within the response (simplified only)
- * @param boolean $raw Get response raw or simplified
- * @return string Raw or simplified XML response
- */
- public function get($include_vcards = true, $raw = false)
- {
- // for owncloud&co. Doesn't work with OpenXchange/Appsuite
- $result = $this->query($this->url, 'PROPFIND');
-
- // for OpenXchange/Appsuite
- $content = '';
- $content_type = 'application/xml';
- //$result = $this->query($this->url, 'REPORT', $content, $content_type);
-
- // DEBUG: print the response of the carddav-server
- //print_r($result);
-
- switch ($result['http_code'])
- {
- case 200:
- case 207:
- if ($raw === true) {
- return $result['response'];
- } else {
- return $this->simplify($result['response'], $include_vcards);
- }
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET
- );
-
- }
-
- /**
- * Gets a clean vCard from the CardDAV server
- *
- * @param string $vcard_id vCard id on the CardDAV server
- * @return string vCard (text/vcard)
- */
- public function getVcard($vcard_id)
- {
- $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
- $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
-
- // DEBUG: print the response of the carddav-server
- // print_r($result);
-
- switch ($result['http_code'])
- {
- case 404:
- $altResult = $this->query($this->url . $vcard_id , 'GET');
- // DEBUG: print the response of the carddav-server
- // print_r($altResult);
- switch ($altResult['http_code'])
- {
- case 200:
- case 207:
- print "Ignoring given Vcard Extension (".$this->url_vcard_extension.")" . PHP_EOL. ".";
- $this->setVcardExtension("");
- return $altResult['response'];
- }
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD
- );
-
- case 200:
- case 207:
- print ".";
- return $result['response'];
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD
- );
- }
-
- /**
- * Gets a vCard + XML from the CardDAV Server
- *
- * @param string $vcard_id vCard id on the CardDAV Server
- * @return string Raw or simplified vCard (text/xml)
- */
- public function getXmlVcard($vcard_id)
- {
- $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
-
- $xml = new XMLWriter();
- $xml->openMemory();
- $xml->setIndent(4);
- $xml->startDocument('1.0', 'utf-8');
- $xml->startElement('C:addressbook-multiget');
- $xml->writeAttribute('xmlns:D', 'DAV:');
- $xml->writeAttribute('xmlns:C', 'urn:ietf:params:xml:ns:carddav');
- $xml->startElement('D:prop');
- $xml->writeElement('D:getetag');
- $xml->writeElement('D:getlastmodified');
- $xml->endElement();
- $xml->writeElement('D:href', $this->url_parts['path'] . $vcard_id . $this->url_vcard_extension);
- $xml->endElement();
- $xml->endDocument();
-
- $result = $this->query($this->url, 'REPORT', $xml->outputMemory(), 'text/xml');
-
- switch ($result['http_code'])
- {
- case 200:
- case 207:
- return $this->simplify($result['response'], true);
-
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_XML_VCARD
- );
- }
-
- /**
- * Enables the debug mode
- *
- * @return void
- */
- public function enableDebug()
- {
- $this->debug = true;
- }
-
- /**
- * Checks if the CardDAV server is reachable
- *
- * @return boolean
- */
- public function checkConnection()
- {
- $result = $this->query($this->url, 'OPTIONS');
-
- if ($result['http_code'] === 200) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Cleans the vCard
- *
- * @param string $vcard vCard
- * @return string $vcard vCard
- */
- private function cleanVcard($vcard)
- {
- $vcard = str_replace("\t", null, $vcard);
-
- return $vcard;
- }
-
- /**
- * Deletes an entry from the CardDAV server
- *
- * @param string $vcard_id vCard id on the CardDAV server
- * @return boolean
- */
- public function delete($vcard_id)
- {
- $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'DELETE');
-
- switch ($result['http_code'])
- {
- case 204:
- return true;
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_DELETE
- );
- }
-
- /**
- * Adds an entry to the CardDAV server
- *
- * @param string $vcard vCard
- * @param string $vcard_id vCard id on the CardDAV server
- * @return string The new vCard id
- */
- public function add($vcard, $vcard_id = null)
- {
- if ($vcard_id === null) {
- $vcard_id = $this->generateVcardId();
- }
- $vcard = $this->cleanVcard($vcard);
- $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'PUT', $vcard, 'text/vcard');
-
- switch($result['http_code'])
- {
- case 201:
- return $vcard_id;
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_ADD
- );
- }
-
- /**
- * Updates an entry to the CardDAV server
- *
- * @param string $vcard vCard
- * @param string $vcard_id vCard id on the CardDAV server
- * @return boolean
- */
- public function update($vcard, $vcard_id)
- {
- try {
- return $this->add($vcard, $vcard_id);
- } catch (Exception $e) {
- throw new \Exception($e->getMessage(), self::EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE);
- }
- }
-
- /**
- * Simplify CardDAV XML response
- *
- * @param string $response CardDAV XML response
- * @param boolean $include_vcards Include vCards or not
- * @return string Simplified CardDAV XML response
- */
- private function simplify($response, $include_vcards = true)
- {
- $response = $this->cleanResponse($response);
-
- try {
- $xml = new \SimpleXMLElement($response);
- } catch (Exception $e) {
- throw new \Exception(
- "The XML response seems to be malformed and can't be simplified!",
- self::EXCEPTION_MALFORMED_XML_RESPONSE,
- $e
- );
- }
-
- $simplified_xml = new \XMLWriter();
- $simplified_xml->openMemory();
- $simplified_xml->setIndent(4);
-
- $simplified_xml->startDocument('1.0', 'utf-8');
- $simplified_xml->startElement('response');
-
- if (!empty($xml->response)) {
- foreach ($xml->response as $response) {
- if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) &&
- !$response->propstat->prop->resourcetype->collection) {
- $id = basename($response->href);
- $id = str_replace($this->url_vcard_extension, null, $id);
-
- try {
- $vcardData = $this->getVcard($id);
- if (!empty($id)) {
- $simplified_xml->startElement('element');
- $simplified_xml->writeElement('id', $id);
- $simplified_xml->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag));
- $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
-
- if ($include_vcards === true) {
- $simplified_xml->writeElement('vcard', $vcardData);
- }
- $simplified_xml->endElement();
- }
- } catch (\Exception $e) {
- print("Error fetching vCard: {$id}: {$e->getMessage()}\n");
- }
- } elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) {
- if (isset($response->propstat->prop->href)) {
- $href = $response->propstat->prop->href;
- } elseif (isset($response->href)) {
- $href = $response->href;
- } else {
- $href = null;
- }
-
- $url = str_replace($this->url_parts['path'], null, $this->url) . $href;
- $simplified_xml->startElement('addressbook_element');
- $simplified_xml->writeElement('display_name', $response->propstat->prop->displayname);
- $simplified_xml->writeElement('url', $url);
- $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
- $simplified_xml->endElement();
- }
- }
- }
-
- $simplified_xml->endElement();
- $simplified_xml->endDocument();
-
- return $simplified_xml->outputMemory();
- }
-
- /**
- * Cleans CardDAV XML response
- *
- * @param string $response CardDAV XML response
- * @return string $response Cleaned CardDAV XML response
- */
- private function cleanResponse($response)
- {
- $response = utf8_encode($response);
- $response = str_replace('D:', null, $response);
- $response = str_replace('d:', null, $response);
- $response = str_replace('C:', null, $response);
- $response = str_replace('c:', null, $response);
-
- return $response;
- }
-
- /**
- * Curl initialization
- *
- * @return void
- */
- public function curlInit()
- {
- if (empty($this->curl)) {
- $this->curl = curl_init();
- curl_setopt($this->curl, CURLOPT_HEADER, true);
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curl, CURLOPT_USERAGENT, self::USERAGENT.self::VERSION);
-
- if ($this->auth !== null) {
- curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_setopt($this->curl, CURLOPT_USERPWD, $this->auth);
- }
-
- /* allow to follow redirects if activated */
- curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $this->follow_redirects);
- if ($this->follow_redirects)
- {
- curl_setopt($this->curl, CURLOPT_MAXREDIRS, $this->follow_redirects_count);
- }
- }
- }
-
- /**
- * Query the CardDAV server via curl and returns the response
- *
- * @param string $url CardDAV server URL
- * @param string $method HTTP method like (OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, COPY, MOVE)
- * @param string $content Content for CardDAV queries
- * @param string $content_type Set content type
- * @return array Raw CardDAV Response and http status code
- */
- private function query($url, $method, $content = null, $content_type = null)
- {
- $this->curlInit();
-
- curl_setopt($this->curl, CURLOPT_URL, $url);
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
-
- if ($content !== null) {
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
- } else {
- curl_setopt($this->curl, CURLOPT_POST, false);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, null);
- }
-
- if ($content_type !== null) {
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: '.$content_type, 'Depth: 1'));
- } else {
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Depth: 1'));
- }
-
- $complete_response = curl_exec($this->curl);
- $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
- $http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
- $header = trim(substr($complete_response, 0, $header_size));
- $response = substr($complete_response, $header_size);
-
- $return = array(
- 'response' => $response,
- 'http_code' => $http_code
- );
-
- if ($this->debug === true) {
- $debug = $return;
- $debug['url'] = $url;
- $debug['method'] = $method;
- $debug['content'] = $content;
- $debug['content_type'] = $content_type;
- $debug['header'] = $header;
- $this->setDebug($debug);
- }
-
- return $return;
- }
-
- /**
- * Returns a valid and unused vCard id
- *
- * @return string $vcard_id Valid vCard id
- */
- private function generateVcardId()
- {
- $vcard_id = null;
-
- for ($number = 0; $number <= 25; $number ++) {
- if ($number == 8 || $number == 17) {
- $vcard_id .= '-';
- } else {
- $vcard_id .= $this->vcard_id_chars[mt_rand(0, (count($this->vcard_id_chars) - 1))];
- }
- }
-
- try {
- $carddav = new CardDavBackend($this->url);
- $carddav->setAuth($this->username, $this->password);
-
- $result = $carddav->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
-
- if ($result['http_code'] !== 404) {
- $vcard_id = $this->generateVcardId();
- }
-
- return $vcard_id;
- } catch (Exception $e) {
- throw new \Exception($e->getMessage(), self::EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID);
- }
- }
-
- /**
- * Destructor
- * Close curl connection if it's open
- *
- * @return void
- */
- public function __destruct()
- {
- if (!empty($this->curl)) {
- curl_close($this->curl);
- }
- }
+ /**
+ * Sets authentication information
+ *
+ * @param string $username CardDAV server username
+ * @param string $password CardDAV server password
+ * @return void
+ */
+ public function setAuth($username, $password)
+ {
+ $this->username = $username;
+ $this->password = $password;
+ $this->auth = $username . ':' . $password;
+ }
+
+ /**
+ * Sets wether to follow redirects and if yes how often
+ *
+ * @param boolean $follow_redirects
+ * @param integer $follow_redirects_count
+ * @return void
+ */
+ public function setFollowRedirects($follow_redirects, $follow_redirects_count = 3)
+ {
+ $this->follow_redirects = $follow_redirects && $follow_redirects_count > 0;
+ $this->follow_redirects_count = $follow_redirects_count > 0 ? $follow_redirects_count : 0;
+ }
+
+ /**
+ * Gets all available debug information
+ *
+ * @return array $this->debug_information All available debug information
+ */
+ public function getDebug()
+ {
+ return $this->debug_information;
+ }
+
+ /**
+ * Gets all vCards including additional information from the CardDAV server
+ *
+ * @param boolean $include_vcards Include vCards within the response (simplified only)
+ * @param boolean $raw Get response raw or simplified
+ * @return string Raw or simplified XML response
+ */
+ public function get($include_vcards = true, $raw = false)
+ {
+ // for owncloud&co. Doesn't work with OpenXchange/Appsuite
+ $result = $this->query($this->url, 'PROPFIND');
+
+ // for OpenXchange/Appsuite
+ $content = '';
+ $content_type = 'application/xml';
+ //$result = $this->query($this->url, 'REPORT', $content, $content_type);
+
+ switch ($result['http_code'])
+ {
+ case 200:
+ case 207:
+ if ($raw === true) {
+ return $result['response'];
+ } else {
+ return $this->simplify($result['response'], $include_vcards);
+ }
+ }
+
+ throw new \Exception(
+ "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
+ self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET
+ );
+ }
+
+ /**
+ * Gets a clean vCard from the CardDAV server
+ *
+ * @param string $vcard_id vCard id on the CardDAV server
+ * @return string vCard (text/vcard)
+ */
+ public function getVcard($vcard_id)
+ {
+ $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
+ $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
+
+ switch ($result['http_code'])
+ {
+ case 404:
+ $altResult = $this->query($this->url . $vcard_id , 'GET');
+ switch ($altResult['http_code'])
+ {
+ case 200:
+ case 207:
+ print "Ignoring given Vcard Extension (".$this->url_vcard_extension.")" . PHP_EOL. ".";
+ $this->setVcardExtension("");
+ return $altResult['response'];
+ }
+ throw new \Exception(
+ "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.",
+ self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD
+ );
+
+ case 200:
+ case 207:
+ // print '.'
+ if (is_callable($this->callback)) {
+ ($this->callback)();
+ }
+ return $result['response'];
+ }
+
+ throw new \Exception(
+ "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.",
+ self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD
+ );
+ }
+
+ /**
+ * Gets a vCard + XML from the CardDAV Server
+ *
+ * @param string $vcard_id vCard id on the CardDAV Server
+ * @return string Raw or simplified vCard (text/xml)
+ */
+ public function getXmlVcard($vcard_id)
+ {
+ $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
+
+ $xml = new XMLWriter();
+ $xml->openMemory();
+ $xml->setIndent(4);
+ $xml->startDocument('1.0', 'utf-8');
+ $xml->startElement('C:addressbook-multiget');
+ $xml->writeAttribute('xmlns:D', 'DAV:');
+ $xml->writeAttribute('xmlns:C', 'urn:ietf:params:xml:ns:carddav');
+ $xml->startElement('D:prop');
+ $xml->writeElement('D:getetag');
+ $xml->writeElement('D:getlastmodified');
+ $xml->endElement();
+ $xml->writeElement('D:href', $this->url_parts['path'] . $vcard_id . $this->url_vcard_extension);
+ $xml->endElement();
+ $xml->endDocument();
+
+ $result = $this->query($this->url, 'REPORT', $xml->outputMemory(), 'text/xml');
+
+ switch ($result['http_code'])
+ {
+ case 200:
+ case 207:
+ return $this->simplify($result['response'], true);
+
+ }
+
+ throw new \Exception(
+ "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
+ self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_XML_VCARD
+ );
+ }
+
+ /**
+ * Enables the debug mode
+ *
+ * @return void
+ */
+ public function enableDebug()
+ {
+ $this->debug = true;
+ }
+
+ /**
+ * Checks if the CardDAV server is reachable
+ *
+ * @return boolean
+ */
+ public function checkConnection()
+ {
+ $result = $this->query($this->url, 'OPTIONS');
+
+ if ($result['http_code'] === 200) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Cleans the vCard
+ *
+ * @param string $vcard vCard
+ * @return string $vcard vCard
+ */
+ private function cleanVcard($vcard)
+ {
+ $vcard = str_replace("\t", null, $vcard);
+
+ return $vcard;
+ }
+
+ /**
+ * Deletes an entry from the CardDAV server
+ *
+ * @param string $vcard_id vCard id on the CardDAV server
+ * @return boolean
+ */
+ public function delete($vcard_id)
+ {
+ $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'DELETE');
+
+ switch ($result['http_code'])
+ {
+ case 204:
+ return true;
+ }
+
+ throw new \Exception(
+ "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
+ self::EXCEPTION_WRONG_HTTP_STATUS_CODE_DELETE
+ );
+ }
+
+ /**
+ * Adds an entry to the CardDAV server
+ *
+ * @param string $vcard vCard
+ * @param string $vcard_id vCard id on the CardDAV server
+ * @return string The new vCard id
+ */
+ public function add($vcard, $vcard_id = null)
+ {
+ if ($vcard_id === null) {
+ $vcard_id = $this->generateVcardId();
+ }
+ $vcard = $this->cleanVcard($vcard);
+ $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'PUT', $vcard, 'text/vcard');
+
+ switch($result['http_code'])
+ {
+ case 201:
+ return $vcard_id;
+ }
+
+ throw new \Exception(
+ "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
+ self::EXCEPTION_WRONG_HTTP_STATUS_CODE_ADD
+ );
+ }
+
+ /**
+ * Updates an entry to the CardDAV server
+ *
+ * @param string $vcard vCard
+ * @param string $vcard_id vCard id on the CardDAV server
+ * @return boolean
+ */
+ public function update($vcard, $vcard_id)
+ {
+ try {
+ return $this->add($vcard, $vcard_id);
+ } catch (Exception $e) {
+ throw new \Exception($e->getMessage(), self::EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE);
+ }
+ }
+
+ /**
+ * Simplify CardDAV XML response
+ *
+ * @param string $response CardDAV XML response
+ * @param boolean $include_vcards Include vCards or not
+ * @return string Simplified CardDAV XML response
+ */
+ private function simplify($response, $include_vcards = true)
+ {
+ $response = $this->cleanResponse($response);
+
+ try {
+ $xml = new \SimpleXMLElement($response);
+ } catch (Exception $e) {
+ throw new \Exception(
+ "The XML response seems to be malformed and can't be simplified!",
+ self::EXCEPTION_MALFORMED_XML_RESPONSE,
+ $e
+ );
+ }
+
+ $simplified_xml = new \XMLWriter();
+ $simplified_xml->openMemory();
+ $simplified_xml->setIndent(4);
+
+ $simplified_xml->startDocument('1.0', 'utf-8');
+ $simplified_xml->startElement('response');
+
+ if (!empty($xml->response)) {
+ foreach ($xml->response as $response) {
+ if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) &&
+ !$response->propstat->prop->resourcetype->collection) {
+ $id = basename($response->href);
+ $id = str_replace($this->url_vcard_extension, null, $id);
+
+ try {
+ $vcardData = $this->getVcard($id);
+ if (!empty($id)) {
+ $simplified_xml->startElement('element');
+ $simplified_xml->writeElement('id', $id);
+ $simplified_xml->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag));
+ $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
+
+ if ($include_vcards === true) {
+ $simplified_xml->writeElement('vcard', $vcardData);
+ }
+ $simplified_xml->endElement();
+ }
+ } catch (\Exception $e) {
+ error_log("Error fetching vCard: {$id}: {$e->getMessage()}\n");
+ }
+ } elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) {
+ if (isset($response->propstat->prop->href)) {
+ $href = $response->propstat->prop->href;
+ } elseif (isset($response->href)) {
+ $href = $response->href;
+ } else {
+ $href = null;
+ }
+
+ $url = str_replace($this->url_parts['path'], null, $this->url) . $href;
+ $simplified_xml->startElement('addressbook_element');
+ $simplified_xml->writeElement('display_name', $response->propstat->prop->displayname);
+ $simplified_xml->writeElement('url', $url);
+ $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
+ $simplified_xml->endElement();
+ }
+ }
+ }
+
+ $simplified_xml->endElement();
+ $simplified_xml->endDocument();
+
+ return $simplified_xml->outputMemory();
+ }
+
+ /**
+ * Cleans CardDAV XML response
+ *
+ * @param string $response CardDAV XML response
+ * @return string $response Cleaned CardDAV XML response
+ */
+ private function cleanResponse($response)
+ {
+ $response = utf8_encode($response);
+ $response = str_replace('D:', null, $response);
+ $response = str_replace('d:', null, $response);
+ $response = str_replace('C:', null, $response);
+ $response = str_replace('c:', null, $response);
+
+ return $response;
+ }
+
+ /**
+ * Curl initialization
+ *
+ * @return void
+ */
+ public function curlInit()
+ {
+ if (empty($this->curl)) {
+ $this->curl = curl_init();
+ curl_setopt($this->curl, CURLOPT_HEADER, true);
+ curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
+ curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
+ curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($this->curl, CURLOPT_USERAGENT, self::USERAGENT.self::VERSION);
+
+ if ($this->auth !== null) {
+ curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
+ curl_setopt($this->curl, CURLOPT_USERPWD, $this->auth);
+ }
+
+ /* allow to follow redirects if activated */
+ curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $this->follow_redirects);
+ if ($this->follow_redirects)
+ {
+ curl_setopt($this->curl, CURLOPT_MAXREDIRS, $this->follow_redirects_count);
+ }
+ }
+ }
+
+ /**
+ * Query the CardDAV server via curl and returns the response
+ *
+ * @param string $url CardDAV server URL
+ * @param string $method HTTP method like (OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, COPY, MOVE)
+ * @param string $content Content for CardDAV queries
+ * @param string $content_type Set content type
+ * @return array Raw CardDAV Response and http status code
+ */
+ private function query($url, $method, $content = null, $content_type = null)
+ {
+ $this->curlInit();
+
+ curl_setopt($this->curl, CURLOPT_URL, $url);
+ curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
+
+ if ($content !== null) {
+ curl_setopt($this->curl, CURLOPT_POST, true);
+ curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
+ } else {
+ curl_setopt($this->curl, CURLOPT_POST, false);
+ curl_setopt($this->curl, CURLOPT_POSTFIELDS, null);
+ }
+
+ if ($content_type !== null) {
+ curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: '.$content_type, 'Depth: 1'));
+ } else {
+ curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Depth: 1'));
+ }
+
+ $complete_response = curl_exec($this->curl);
+ $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
+ $http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
+ $header = trim(substr($complete_response, 0, $header_size));
+ $response = substr($complete_response, $header_size);
+
+ $return = array(
+ 'response' => $response,
+ 'http_code' => $http_code
+ );
+
+ if ($this->debug === true) {
+ $debug = $return;
+ $debug['url'] = $url;
+ $debug['method'] = $method;
+ $debug['content'] = $content;
+ $debug['content_type'] = $content_type;
+ $debug['header'] = $header;
+ $this->setDebug($debug);
+ }
+
+ return $return;
+ }
+
+ /**
+ * Returns a valid and unused vCard id
+ *
+ * @return string $vcard_id Valid vCard id
+ */
+ private function generateVcardId()
+ {
+ $vcard_id = null;
+
+ for ($number = 0; $number <= 25; $number ++) {
+ if ($number == 8 || $number == 17) {
+ $vcard_id .= '-';
+ } else {
+ $vcard_id .= $this->vcard_id_chars[mt_rand(0, (count($this->vcard_id_chars) - 1))];
+ }
+ }
+
+ try {
+ $carddav = new CardDavBackend($this->url);
+ $carddav->setAuth($this->username, $this->password);
+
+ $result = $carddav->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
+
+ if ($result['http_code'] !== 404) {
+ $vcard_id = $this->generateVcardId();
+ }
+
+ return $vcard_id;
+ } catch (Exception $e) {
+ throw new \Exception($e->getMessage(), self::EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID);
+ }
+ }
+
+ /**
+ * Destructor
+ * Close curl connection if it's open
+ *
+ * @return void
+ */
+ public function __destruct()
+ {
+ if (!empty($this->curl)) {
+ curl_close($this->curl);
+ }
+ }
}
?>
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 712a70e2..0532cfbc 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Helper\ProgressBar;
class DownloadCommand extends Command {
@@ -23,8 +24,15 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output) {
$this->loadConfig($input);
+ $progress = new ProgressBar($output);
+ $progress->start();
+
$server = $this->config['server'];
- $xmlStr = download($server['url'], $server['user'], $server['password']);
+ $xmlStr = download($server['url'], $server['user'], $server['password'], function() use ($progress) {
+ $progress->advance();
+ });
+
+ $progress->finish();
$count = countCards($xmlStr);
error_log(sprintf("\nDownloaded %d vcards", $count));
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 24cdc865..0482e208 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Helper\ProgressBar;
class RunCommand extends Command {
@@ -24,8 +25,15 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->loadConfig($input);
// download
+ $progress = new ProgressBar($output);
+ $progress->start();
+
$server = $this->config['server'];
- $xmlStr = download($server['url'], $server['user'], $server['password']);
+ $xmlStr = download($server['url'], $server['user'], $server['password'], function() use ($progress) {
+ $progress->advance();
+ });
+
+ $progress->finish();
$count = countCards($xmlStr);
error_log(sprintf("\nDownloaded %d vcards", $count));
diff --git a/src/functions.php b/src/functions.php
index e4d27340..66aade49 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -9,9 +9,10 @@
use \SimpleXMLElement;
-function download($url, $user, $password): string {
+function download($url, $user, $password, callable $callback=null): string {
$backend = new Backend($url);
$backend->setAuth($user, $password);
+ $backend->setProgress($callback);
return $backend->get();
}
From a662499eb940bea44e37de3687039cfef1c0d5a9 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 12:38:21 +0100
Subject: [PATCH 024/165] Simplify and speed-up download
---
composer.json | 4 +-
src/CardDav/Backend.php | 482 +++++-----------------------------------
src/RunCommand.php | 76 ++++---
3 files changed, 97 insertions(+), 465 deletions(-)
diff --git a/composer.json b/composer.json
index 1f9b1fdc..29ddc334 100644
--- a/composer.json
+++ b/composer.json
@@ -10,7 +10,9 @@
],
"require": {
"php": "^7.0",
- "symfony/console": "^3.0"
+ "symfony/console": "^3.0",
+ "guzzlehttp/guzzle": "^6.0",
+ "ringcentral/psr7": "^1.2"
},
"autoload": {
"psr-4": {
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index bdd76928..49f98623 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -1,99 +1,12 @@
-setAuth('username', 'password');
- * echo $carddav->get();
- *
- *
- * Simple vCard query
- * ------------------
- * $carddav = new CardDavBackend('https://davical.example.com/user/contacts/');
- * $carddav->setAuth('username', 'password');
- * echo $carddav->getVcard('0126FFB4-2EB74D0A-302EA17F');
- *
- *
- * XML vCard query
- * ------------------
- * $carddav = new CardDavBackend('https://davical.example.com/user/contacts/');
- * $carddav->setAuth('username', 'password');
- * echo $carddav->getXmlVcard('0126FFB4-2EB74D0A-302EA17F');
- *
- *
- * Check CardDAV server connection
- * -------------------------------
- * $carddav = new CardDavBackend('https://davical.example.com/user/contacts/');
- * $carddav->setAuth('username', 'password');
- * var_dump($carddav->checkConnection());
- *
- *
- * CardDAV delete query
- * --------------------
- * $carddav = new CardDavBackend('https://davical.example.com/user/contacts/');
- * $carddav->setAuth('username', 'password');
- * $carddav->delete('0126FFB4-2EB74D0A-302EA17F');
- *
- *
- * CardDAV add query
- * --------------------
- * $vcard = 'BEGIN:VCARD
- * VERSION:3.0
- * UID:1f5ea45f-b28a-4b96-25as-ed4f10edf57b
- * FN:Christian Putzke
- * N:Christian;Putzke;;;
- * EMAIL;TYPE=OTHER:christian.putzke@graviox.de
- * END:VCARD';
- *
- * $carddav = new CardDavBackend('https://davical.example.com/user/contacts/');
- * $carddav->setAuth('username', 'password');
- * $vcard_id = $carddav->add($vcard);
- *
- *
- * CardDAV update query
- * --------------------
- * $vcard = 'BEGIN:VCARD
- * VERSION:3.0
- * UID:1f5ea45f-b28a-4b96-25as-ed4f10edf57b
- * FN:Christian Putzke
- * N:Christian;Putzke;;;
- * EMAIL;TYPE=OTHER:christian.putzke@graviox.de
- * END:VCARD';
- *
- * $carddav = new CardDavBackend('https://davical.example.com/user/contacts/');
- * $carddav->setAuth('username', 'password');
- * $carddav->update($vcard, '0126FFB4-2EB74D0A-302EA17F');
- *
- *
- * CardDAV debug
- * -------------
- * $carddav = new CardDavBackend('https://davical.example.com/user/contacts/');
- * $carddav->enableDebug();
- * $carddav->setAuth('username', 'password');
- * $carddav->get();
- * var_dump($carddav->getDebug());
- *
- *
- * CardDAV server list
- * -------------------
- * DAViCal: https://example.com/{resource|principal|username}/{collection}/
- * Apple Addressbook Server: https://example.com/addressbooks/users/{resource|principal|username}/{collection}/
- * memotoo: https://sync.memotoo.com/cardDAV/
- * SabreDAV: https://example.com/addressbooks/{resource|principal|username}/{collection}/
- * ownCloud: https://example.com/apps/contacts/carddav.php/addressbooks/{resource|principal|username}/{collection}/
- * SOGo: https://example.com/SOGo/dav/{resource|principal|username}/Contacts/{collection}/
- * Google (direct): https://google.com/m8/carddav/principals/__uids__/{username}/lists/default/
- *
- *
* @author Christian Putzke
* @copyright Christian Putzke
* @link http://www.graviox.de/
@@ -135,7 +48,7 @@ class Backend
/**
* VCard File URL Extension
- *
+ *
* @var string
*/
private $url_vcard_extension = '.vcf';
@@ -177,11 +90,11 @@ class Backend
/**
* Follow redirects (Location Header)
- *
+ *
* @var boolean
*/
private $follow_redirects = true;
-
+
/**
* Maximum redirects to follow
*
@@ -208,19 +121,6 @@ class Backend
*/
private $callback;
- /**
- * Exception codes
- */
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET = 1000;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD = 1001;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_XML_VCARD = 1002;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_DELETE = 1003;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_ADD = 1004;
- const EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE = 1005;
- const EXCEPTION_MALFORMED_XML_RESPONSE = 1006;
- const EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID = 1007;
-
-
/**
* Constructor
* Sets the CardDAV server url
@@ -242,17 +142,6 @@ public function setProgress($callback = null)
$this->callback = $callback;
}
- /**
- * Sets debug information
- *
- * @param array $debug_information Debug information
- * @return void
- */
- public function setDebug(array $debug_information)
- {
- $this->debug_information[] = $debug_information;
- }
-
/**
* Sets the CardDAV server url
*
@@ -295,7 +184,7 @@ public function setVcardExtension($extension)
{
$this->url_vcard_extension = $extension;
}
-
+
/**
* Sets authentication information
*
@@ -337,34 +226,18 @@ public function getDebug()
* Gets all vCards including additional information from the CardDAV server
*
* @param boolean $include_vcards Include vCards within the response (simplified only)
- * @param boolean $raw Get response raw or simplified
* @return string Raw or simplified XML response
*/
- public function get($include_vcards = true, $raw = false)
+ public function get($include_vcards = true)
{
- // for owncloud&co. Doesn't work with OpenXchange/Appsuite
- $result = $this->query($this->url, 'PROPFIND');
-
- // for OpenXchange/Appsuite
- $content = '';
- $content_type = 'application/xml';
- //$result = $this->query($this->url, 'REPORT', $content, $content_type);
+ $response = $this->query($this->url, 'PROPFIND');
- switch ($result['http_code'])
- {
- case 200:
- case 207:
- if ($raw === true) {
- return $result['response'];
- } else {
- return $this->simplify($result['response'], $include_vcards);
- }
- }
+ if (in_array($response->getStatusCode(), [200,207])) {
+ $body = (string)$response->getBody();
+ return $this->simplify($body, $include_vcards);
+ }
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET
- );
+ throw new \Exception('Received HTTP ' . $response->getStatusCode());
}
/**
@@ -374,185 +247,21 @@ public function get($include_vcards = true, $raw = false)
* @return string vCard (text/vcard)
*/
public function getVcard($vcard_id)
- {
- $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
- $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
-
- switch ($result['http_code'])
- {
- case 404:
- $altResult = $this->query($this->url . $vcard_id , 'GET');
- switch ($altResult['http_code'])
- {
- case 200:
- case 207:
- print "Ignoring given Vcard Extension (".$this->url_vcard_extension.")" . PHP_EOL. ".";
- $this->setVcardExtension("");
- return $altResult['response'];
- }
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD
- );
-
- case 200:
- case 207:
- // print '.'
- if (is_callable($this->callback)) {
- ($this->callback)();
- }
- return $result['response'];
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD
- );
- }
-
- /**
- * Gets a vCard + XML from the CardDAV Server
- *
- * @param string $vcard_id vCard id on the CardDAV Server
- * @return string Raw or simplified vCard (text/xml)
- */
- public function getXmlVcard($vcard_id)
{
$vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
+ $response = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
- $xml = new XMLWriter();
- $xml->openMemory();
- $xml->setIndent(4);
- $xml->startDocument('1.0', 'utf-8');
- $xml->startElement('C:addressbook-multiget');
- $xml->writeAttribute('xmlns:D', 'DAV:');
- $xml->writeAttribute('xmlns:C', 'urn:ietf:params:xml:ns:carddav');
- $xml->startElement('D:prop');
- $xml->writeElement('D:getetag');
- $xml->writeElement('D:getlastmodified');
- $xml->endElement();
- $xml->writeElement('D:href', $this->url_parts['path'] . $vcard_id . $this->url_vcard_extension);
- $xml->endElement();
- $xml->endDocument();
-
- $result = $this->query($this->url, 'REPORT', $xml->outputMemory(), 'text/xml');
-
- switch ($result['http_code'])
- {
- case 200:
- case 207:
- return $this->simplify($result['response'], true);
+ if (in_array($response->getStatusCode(), [200,207])) {
+ $body = (string)$response->getBody();
- }
+ if (is_callable($this->callback)) {
+ ($this->callback)();
+ }
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_XML_VCARD
- );
- }
+ return $body;
+ }
- /**
- * Enables the debug mode
- *
- * @return void
- */
- public function enableDebug()
- {
- $this->debug = true;
- }
-
- /**
- * Checks if the CardDAV server is reachable
- *
- * @return boolean
- */
- public function checkConnection()
- {
- $result = $this->query($this->url, 'OPTIONS');
-
- if ($result['http_code'] === 200) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Cleans the vCard
- *
- * @param string $vcard vCard
- * @return string $vcard vCard
- */
- private function cleanVcard($vcard)
- {
- $vcard = str_replace("\t", null, $vcard);
-
- return $vcard;
- }
-
- /**
- * Deletes an entry from the CardDAV server
- *
- * @param string $vcard_id vCard id on the CardDAV server
- * @return boolean
- */
- public function delete($vcard_id)
- {
- $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'DELETE');
-
- switch ($result['http_code'])
- {
- case 204:
- return true;
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_DELETE
- );
- }
-
- /**
- * Adds an entry to the CardDAV server
- *
- * @param string $vcard vCard
- * @param string $vcard_id vCard id on the CardDAV server
- * @return string The new vCard id
- */
- public function add($vcard, $vcard_id = null)
- {
- if ($vcard_id === null) {
- $vcard_id = $this->generateVcardId();
- }
- $vcard = $this->cleanVcard($vcard);
- $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'PUT', $vcard, 'text/vcard');
-
- switch($result['http_code'])
- {
- case 201:
- return $vcard_id;
- }
-
- throw new \Exception(
- "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}.",
- self::EXCEPTION_WRONG_HTTP_STATUS_CODE_ADD
- );
- }
-
- /**
- * Updates an entry to the CardDAV server
- *
- * @param string $vcard vCard
- * @param string $vcard_id vCard id on the CardDAV server
- * @return boolean
- */
- public function update($vcard, $vcard_id)
- {
- try {
- return $this->add($vcard, $vcard_id);
- } catch (Exception $e) {
- throw new \Exception($e->getMessage(), self::EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE);
- }
+ throw new \Exception('Received HTTP ' . $response->getStatusCode());
}
/**
@@ -565,23 +274,14 @@ public function update($vcard, $vcard_id)
private function simplify($response, $include_vcards = true)
{
$response = $this->cleanResponse($response);
-
- try {
- $xml = new \SimpleXMLElement($response);
- } catch (Exception $e) {
- throw new \Exception(
- "The XML response seems to be malformed and can't be simplified!",
- self::EXCEPTION_MALFORMED_XML_RESPONSE,
- $e
- );
- }
+ $xml = new \SimpleXMLElement($response);
$simplified_xml = new \XMLWriter();
$simplified_xml->openMemory();
$simplified_xml->setIndent(4);
$simplified_xml->startDocument('1.0', 'utf-8');
- $simplified_xml->startElement('response');
+ $simplified_xml->startElement('response');
if (!empty($xml->response)) {
foreach ($xml->response as $response) {
@@ -592,6 +292,7 @@ private function simplify($response, $include_vcards = true)
try {
$vcardData = $this->getVcard($id);
+
if (!empty($id)) {
$simplified_xml->startElement('element');
$simplified_xml->writeElement('id', $id);
@@ -625,7 +326,7 @@ private function simplify($response, $include_vcards = true)
}
}
- $simplified_xml->endElement();
+ $simplified_xml->endElement();
$simplified_xml->endDocument();
return $simplified_xml->outputMemory();
@@ -648,35 +349,6 @@ private function cleanResponse($response)
return $response;
}
- /**
- * Curl initialization
- *
- * @return void
- */
- public function curlInit()
- {
- if (empty($this->curl)) {
- $this->curl = curl_init();
- curl_setopt($this->curl, CURLOPT_HEADER, true);
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($this->curl, CURLOPT_USERAGENT, self::USERAGENT.self::VERSION);
-
- if ($this->auth !== null) {
- curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_setopt($this->curl, CURLOPT_USERPWD, $this->auth);
- }
-
- /* allow to follow redirects if activated */
- curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $this->follow_redirects);
- if ($this->follow_redirects)
- {
- curl_setopt($this->curl, CURLOPT_MAXREDIRS, $this->follow_redirects_count);
- }
- }
- }
-
/**
* Query the CardDAV server via curl and returns the response
*
@@ -688,93 +360,45 @@ public function curlInit()
*/
private function query($url, $method, $content = null, $content_type = null)
{
- $this->curlInit();
-
- curl_setopt($this->curl, CURLOPT_URL, $url);
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
-
+/*
if ($content !== null) {
+error_log('POST');
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
} else {
curl_setopt($this->curl, CURLOPT_POST, false);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, null);
}
+*/
- if ($content_type !== null) {
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: '.$content_type, 'Depth: 1'));
- } else {
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Depth: 1'));
- }
-
- $complete_response = curl_exec($this->curl);
- $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
- $http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
- $header = trim(substr($complete_response, 0, $header_size));
- $response = substr($complete_response, $header_size);
-
- $return = array(
- 'response' => $response,
- 'http_code' => $http_code
- );
-
- if ($this->debug === true) {
- $debug = $return;
- $debug['url'] = $url;
- $debug['method'] = $method;
- $debug['content'] = $content;
- $debug['content_type'] = $content_type;
- $debug['header'] = $header;
- $this->setDebug($debug);
- }
-
- return $return;
- }
-
- /**
- * Returns a valid and unused vCard id
- *
- * @return string $vcard_id Valid vCard id
- */
- private function generateVcardId()
- {
- $vcard_id = null;
+ if (!isset($this->client)) {
+ $this->client = new Client();
+ }
- for ($number = 0; $number <= 25; $number ++) {
- if ($number == 8 || $number == 17) {
- $vcard_id .= '-';
- } else {
- $vcard_id .= $this->vcard_id_chars[mt_rand(0, (count($this->vcard_id_chars) - 1))];
- }
- }
+ $request = new Request($method, $url, [
+ 'Depth' => '1'
+ ]);
- try {
- $carddav = new CardDavBackend($this->url);
- $carddav->setAuth($this->username, $this->password);
+ if ($content_type) {
+ $request = $request->withHeader('Content-type', $content_type);
+ }
- $result = $carddav->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
+ if ($content) {
+ $request = $request->withBody($content);
+ }
- if ($result['http_code'] !== 404) {
- $vcard_id = $this->generateVcardId();
- }
+ if ($this->username) {
+ $credentials = base64_encode($this->username.':'.$this->password);
+ $request = $request->withHeader('Authorization', 'Basic '.$credentials);
+ }
- return $vcard_id;
- } catch (Exception $e) {
- throw new \Exception($e->getMessage(), self::EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID);
- }
- }
+ // error_log(Psr7\str($request));
+ $response = $this->client->send($request);
+// error_log("\nHEADERS");
+ // error_log(Psr7\str($response));
+// error_log("<<< HEADERS");
- /**
- * Destructor
- * Close curl connection if it's open
- *
- * @return void
- */
- public function __destruct()
- {
- if (!empty($this->curl)) {
- curl_close($this->curl);
- }
+ return $response;
}
}
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 0482e208..0c9183d2 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -12,52 +12,58 @@
class RunCommand extends Command {
- use ConfigTrait;
+ use ConfigTrait;
- protected function configure() {
- $this->setName('run')
- ->setDescription('Download, convert and upload - all in one');
+ protected function configure() {
+ $this->setName('run')
+ ->setDescription('Download, convert and upload - all in one');
- $this->addConfig();
- }
+ $this->addConfig();
+ }
- protected function execute(InputInterface $input, OutputInterface $output) {
- $this->loadConfig($input);
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $this->loadConfig($input);
- // download
- $progress = new ProgressBar($output);
- $progress->start();
+ // download
+ error_log("Downloading");
- $server = $this->config['server'];
- $xmlStr = download($server['url'], $server['user'], $server['password'], function() use ($progress) {
- $progress->advance();
- });
+ $progress = new ProgressBar($output);
+ $progress->start();
- $progress->finish();
+ $server = $this->config['server'];
+ $xmlStr = download($server['url'], $server['user'], $server['password'], function() use ($progress) {
+ $progress->advance();
+ });
- $count = countCards($xmlStr);
- error_log(sprintf("\nDownloaded %d vcards", $count));
+ $progress->finish();
- // parse and convert
- $phonebook = $this->config['phonebook'];
- $conversions = $this->config['conversions'];
- $excludes = $this->config['excludes'];
+ $count = countCards($xmlStr);
+ error_log(sprintf("\nDownloaded %d vcards", $count));
- $xml = simplexml_load_string($xmlStr);
- $cards = parse($xml, $conversions);
- $filtered = filter($cards, $excludes);
- error_log(sprintf("Converted %d vcards", count($filtered)));
+ // parse and convert
+ error_log("Converting");
- // fritzbox format
- $xml = export($phonebook['name'], $filtered, $conversions);
- // error_log(sprintf("Exported fritz phonebook", count($cards)));
+ $phonebook = $this->config['phonebook'];
+ $conversions = $this->config['conversions'];
+ $excludes = $this->config['excludes'];
- // upload
- $xmlStr = $xml->asXML();
+ $xml = simplexml_load_string($xmlStr);
+ $cards = parse($xml, $conversions);
+ $filtered = filter($cards, $excludes);
+ error_log(sprintf("Converted %d vcards", count($filtered)));
- $fritzbox = $this->config['fritzbox'];
- upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
+ // fritzbox format
+ $xml = export($phonebook['name'], $filtered, $conversions);
+ // error_log(sprintf("Exported fritz phonebook", count($cards)));
- error_log("Uploaded fritz phonebook");
- }
+ // upload
+ error_log("Uploading");
+
+ $xmlStr = $xml->asXML();
+
+ $fritzbox = $this->config['fritzbox'];
+ upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
+
+ error_log("Uploaded fritz phonebook");
+ }
}
From 091b1b1228c5274e9a2155a14ff123c69acbc3c9 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 12:38:49 +0100
Subject: [PATCH 025/165] Add editorconfig
---
.editorconfig | 15 +++++++++++++++
1 file changed, 15 insertions(+)
create mode 100644 .editorconfig
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 00000000..aa9a0470
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,15 @@
+# This file is for unifying the coding style for different editors and IDEs
+# editorconfig.org
+
+# PHP PSR-2 Coding Standards
+# http://www.php-fig.org/psr/psr-2/
+
+root = true
+
+[*.php]
+charset = utf-8
+end_of_line = lf
+insert_final_newline = true
+trim_trailing_whitespace = true
+indent_style = space
+indent_size = 4
From 7b31e1c7cbb1b36e3ffad56c1d9477d2ae4251b6 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 12:42:02 +0100
Subject: [PATCH 026/165] PSR2 coding style
---
src/CardDav/Backend.php | 664 ++++++++++++++++++-------------------
src/ConfigTrait.php | 42 +--
src/ConvertCommand.php | 78 ++---
src/DownloadCommand.php | 44 +--
src/FritzBox/Api.php | 478 +++++++++++++-------------
src/FritzBox/Converter.php | 326 +++++++++---------
src/RunCommand.php | 12 +-
src/UploadCommand.php | 38 ++-
src/Vcard/Parser.php | 5 +-
src/functions.php | 221 ++++++------
10 files changed, 949 insertions(+), 959 deletions(-)
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 49f98623..0e6194e6 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -18,238 +18,224 @@
class Backend
{
- /**
- * CardDAV PHP Version
- *
- * @constant string
- */
- const VERSION = '0.7';
-
- /**
- * User agent displayed in http requests
- *
- * @constant string
- */
- const USERAGENT = 'CardDAV PHP/';
-
- /**
- * CardDAV server url
- *
- * @var string
- */
- private $url = null;
-
- /**
- * CardDAV server url_parts
- *
- * @var array
- */
- private $url_parts = null;
-
- /**
- * VCard File URL Extension
- *
- * @var string
- */
- private $url_vcard_extension = '.vcf';
-
- /**
- * Authentication string
- *
- * @var string
- */
- private $auth = null;
-
- /**
- * Authentication: username
- *
- * @var string
- */
- private $username = null;
-
- /**
- * Authentication: password
- *
- * @var string
- */
- private $password = null;
-
- /**
- * Characters used for vCard id generation
- *
- * @var array
- */
- private $vcard_id_chars = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F');
-
- /**
- * CardDAV server connection (curl handle)
- *
- * @var resource
- */
- private $curl;
-
- /**
- * Follow redirects (Location Header)
- *
- * @var boolean
- */
- private $follow_redirects = true;
-
- /**
- * Maximum redirects to follow
- *
- * @var integer
- */
- private $follow_redirects_count = 3;
-
- /**
- * Debug on or off
- *
- * @var boolean
- */
- private $debug = false;
-
- /**
- * All available debug information
- *
- * @var array
- */
- private $debug_information = array();
-
- /**
- * Progress callback
- */
- private $callback;
-
- /**
- * Constructor
- * Sets the CardDAV server url
- *
- * @param string $url CardDAV server url
- */
- public function __construct($url = null)
- {
- if ($url !== null) {
- $this->setUrl($url);
- }
- }
-
- /**
- * Set progress callback
- */
- public function setProgress($callback = null)
- {
- $this->callback = $callback;
- }
-
- /**
- * Sets the CardDAV server url
- *
- * @param string $url CardDAV server url
- * @return void
- */
- public function setUrl($url)
- {
- $this->url = $url;
-
- if (substr($this->url, -1, 1) !== '/') {
- $this->url = $this->url . '/';
- }
-
- $this->url_parts = parse_url($this->url);
-
- // workaround for providers that don't use the default .vcf extension
- if (strpos($this->url, "google.com"))
- {
- $this->setVcardExtension("");
- }
- }
-
- /**
- * Sets the CardDAV vcard url extension
- *
- * Most providers do requests handling Vcards with .vcf, however
- * this isn't always the case and some providers (such as Google)
- * returned a 404 if the .vcf extension is used - or the other
- * way around, returning 404 unless .vcf is used.
- *
- * Both approaches are technically correct, see rfc635
- * http://tools.ietf.org/html/rfc6352
- *
- *
- * @param string $extension File extension
- * @return void
- */
- public function setVcardExtension($extension)
- {
- $this->url_vcard_extension = $extension;
- }
-
- /**
- * Sets authentication information
- *
- * @param string $username CardDAV server username
- * @param string $password CardDAV server password
- * @return void
- */
- public function setAuth($username, $password)
- {
- $this->username = $username;
- $this->password = $password;
- $this->auth = $username . ':' . $password;
- }
-
- /**
- * Sets wether to follow redirects and if yes how often
- *
- * @param boolean $follow_redirects
- * @param integer $follow_redirects_count
- * @return void
- */
- public function setFollowRedirects($follow_redirects, $follow_redirects_count = 3)
- {
- $this->follow_redirects = $follow_redirects && $follow_redirects_count > 0;
- $this->follow_redirects_count = $follow_redirects_count > 0 ? $follow_redirects_count : 0;
- }
-
- /**
- * Gets all available debug information
- *
- * @return array $this->debug_information All available debug information
- */
- public function getDebug()
- {
- return $this->debug_information;
- }
-
- /**
- * Gets all vCards including additional information from the CardDAV server
- *
- * @param boolean $include_vcards Include vCards within the response (simplified only)
- * @return string Raw or simplified XML response
- */
- public function get($include_vcards = true)
- {
- $response = $this->query($this->url, 'PROPFIND');
+ /**
+ * CardDAV PHP Version
+ *
+ * @constant string
+ */
+ const VERSION = '0.7';
+
+ /**
+ * User agent displayed in http requests
+ *
+ * @constant string
+ */
+ const USERAGENT = 'CardDAV PHP/';
+
+ /**
+ * CardDAV server url
+ *
+ * @var string
+ */
+ private $url = null;
+
+ /**
+ * CardDAV server url_parts
+ *
+ * @var array
+ */
+ private $url_parts = null;
+
+ /**
+ * VCard File URL Extension
+ *
+ * @var string
+ */
+ private $url_vcard_extension = '.vcf';
+
+ /**
+ * Authentication string
+ *
+ * @var string
+ */
+ private $auth = null;
+
+ /**
+ * Authentication: username
+ *
+ * @var string
+ */
+ private $username = null;
+
+ /**
+ * Authentication: password
+ *
+ * @var string
+ */
+ private $password = null;
+
+ /**
+ * Characters used for vCard id generation
+ *
+ * @var array
+ */
+ private $vcard_id_chars = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F');
+
+ /**
+ * CardDAV server connection (curl handle)
+ *
+ * @var resource
+ */
+ private $curl;
+
+ /**
+ * Follow redirects (Location Header)
+ *
+ * @var boolean
+ */
+ private $follow_redirects = true;
+
+ /**
+ * Maximum redirects to follow
+ *
+ * @var integer
+ */
+ private $follow_redirects_count = 3;
+
+ /**
+ * Debug on or off
+ *
+ * @var boolean
+ */
+ private $debug = false;
+
+ /**
+ * All available debug information
+ *
+ * @var array
+ */
+ private $debug_information = array();
+
+ /**
+ * Progress callback
+ */
+ private $callback;
+
+ /**
+ * Constructor
+ * Sets the CardDAV server url
+ *
+ * @param string $url CardDAV server url
+ */
+ public function __construct($url)
+ {
+ $this->url = $url;
+
+ if (substr($this->url, -1, 1) !== '/') {
+ $this->url = $this->url . '/';
+ }
+
+ $this->url_parts = parse_url($this->url);
+
+ // workaround for providers that don't use the default .vcf extension
+ if (strpos($this->url, "google.com")) {
+ $this->setVcardExtension("");
+ }
+ }
+
+ /**
+ * Set progress callback
+ */
+ public function setProgress($callback = null)
+ {
+ $this->callback = $callback;
+ }
+
+ /**
+ * Sets the CardDAV vcard url extension
+ *
+ * Most providers do requests handling Vcards with .vcf, however
+ * this isn't always the case and some providers (such as Google)
+ * returned a 404 if the .vcf extension is used - or the other
+ * way around, returning 404 unless .vcf is used.
+ *
+ * Both approaches are technically correct, see rfc635
+ * http://tools.ietf.org/html/rfc6352
+ *
+ *
+ * @param string $extension File extension
+ * @return void
+ */
+ public function setVcardExtension($extension)
+ {
+ $this->url_vcard_extension = $extension;
+ }
+
+ /**
+ * Sets authentication information
+ *
+ * @param string $username CardDAV server username
+ * @param string $password CardDAV server password
+ * @return void
+ */
+ public function setAuth($username, $password)
+ {
+ $this->username = $username;
+ $this->password = $password;
+ $this->auth = $username . ':' . $password;
+ }
+
+ /**
+ * Sets wether to follow redirects and if yes how often
+ *
+ * @param boolean $follow_redirects
+ * @param integer $follow_redirects_count
+ * @return void
+ */
+ public function setFollowRedirects($follow_redirects, $follow_redirects_count = 3)
+ {
+ $this->follow_redirects = $follow_redirects && $follow_redirects_count > 0;
+ $this->follow_redirects_count = $follow_redirects_count > 0 ? $follow_redirects_count : 0;
+ }
+
+ /**
+ * Gets all available debug information
+ *
+ * @return array $this->debug_information All available debug information
+ */
+ public function getDebug()
+ {
+ return $this->debug_information;
+ }
+
+ /**
+ * Gets all vCards including additional information from the CardDAV server
+ *
+ * @param boolean $include_vcards Include vCards within the response (simplified only)
+ * @return string Raw or simplified XML response
+ */
+ public function get($include_vcards = true)
+ {
+ $response = $this->query($this->url, 'PROPFIND');
if (in_array($response->getStatusCode(), [200,207])) {
$body = (string)$response->getBody();
return $this->simplify($body, $include_vcards);
}
- throw new \Exception('Received HTTP ' . $response->getStatusCode());
- }
-
- /**
- * Gets a clean vCard from the CardDAV server
- *
- * @param string $vcard_id vCard id on the CardDAV server
- * @return string vCard (text/vcard)
- */
- public function getVcard($vcard_id)
- {
- $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
- $response = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
+ throw new \Exception('Received HTTP ' . $response->getStatusCode());
+ }
+
+ /**
+ * Gets a clean vCard from the CardDAV server
+ *
+ * @param string $vcard_id vCard id on the CardDAV server
+ * @return string vCard (text/vcard)
+ */
+ public function getVcard($vcard_id)
+ {
+ $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id);
+ $response = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET');
if (in_array($response->getStatusCode(), [200,207])) {
$body = (string)$response->getBody();
@@ -262,114 +248,114 @@ public function getVcard($vcard_id)
}
throw new \Exception('Received HTTP ' . $response->getStatusCode());
- }
-
- /**
- * Simplify CardDAV XML response
- *
- * @param string $response CardDAV XML response
- * @param boolean $include_vcards Include vCards or not
- * @return string Simplified CardDAV XML response
- */
- private function simplify($response, $include_vcards = true)
- {
- $response = $this->cleanResponse($response);
- $xml = new \SimpleXMLElement($response);
-
- $simplified_xml = new \XMLWriter();
- $simplified_xml->openMemory();
- $simplified_xml->setIndent(4);
-
- $simplified_xml->startDocument('1.0', 'utf-8');
- $simplified_xml->startElement('response');
-
- if (!empty($xml->response)) {
- foreach ($xml->response as $response) {
- if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) &&
- !$response->propstat->prop->resourcetype->collection) {
- $id = basename($response->href);
- $id = str_replace($this->url_vcard_extension, null, $id);
-
- try {
- $vcardData = $this->getVcard($id);
-
- if (!empty($id)) {
- $simplified_xml->startElement('element');
- $simplified_xml->writeElement('id', $id);
- $simplified_xml->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag));
- $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
-
- if ($include_vcards === true) {
- $simplified_xml->writeElement('vcard', $vcardData);
- }
- $simplified_xml->endElement();
- }
- } catch (\Exception $e) {
- error_log("Error fetching vCard: {$id}: {$e->getMessage()}\n");
- }
- } elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) {
- if (isset($response->propstat->prop->href)) {
- $href = $response->propstat->prop->href;
- } elseif (isset($response->href)) {
- $href = $response->href;
- } else {
- $href = null;
- }
-
- $url = str_replace($this->url_parts['path'], null, $this->url) . $href;
- $simplified_xml->startElement('addressbook_element');
- $simplified_xml->writeElement('display_name', $response->propstat->prop->displayname);
- $simplified_xml->writeElement('url', $url);
- $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
- $simplified_xml->endElement();
- }
- }
- }
-
- $simplified_xml->endElement();
- $simplified_xml->endDocument();
-
- return $simplified_xml->outputMemory();
- }
-
- /**
- * Cleans CardDAV XML response
- *
- * @param string $response CardDAV XML response
- * @return string $response Cleaned CardDAV XML response
- */
- private function cleanResponse($response)
- {
- $response = utf8_encode($response);
- $response = str_replace('D:', null, $response);
- $response = str_replace('d:', null, $response);
- $response = str_replace('C:', null, $response);
- $response = str_replace('c:', null, $response);
-
- return $response;
- }
-
- /**
- * Query the CardDAV server via curl and returns the response
- *
- * @param string $url CardDAV server URL
- * @param string $method HTTP method like (OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, COPY, MOVE)
- * @param string $content Content for CardDAV queries
- * @param string $content_type Set content type
- * @return array Raw CardDAV Response and http status code
- */
- private function query($url, $method, $content = null, $content_type = null)
- {
-/*
- if ($content !== null) {
-error_log('POST');
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
- } else {
- curl_setopt($this->curl, CURLOPT_POST, false);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, null);
- }
-*/
+ }
+
+ /**
+ * Simplify CardDAV XML response
+ *
+ * @param string $response CardDAV XML response
+ * @param boolean $include_vcards Include vCards or not
+ * @return string Simplified CardDAV XML response
+ */
+ private function simplify($response, $include_vcards = true)
+ {
+ $response = $this->cleanResponse($response);
+ $xml = new \SimpleXMLElement($response);
+
+ $simplified_xml = new \XMLWriter();
+ $simplified_xml->openMemory();
+ $simplified_xml->setIndent(4);
+
+ $simplified_xml->startDocument('1.0', 'utf-8');
+ $simplified_xml->startElement('response');
+
+ if (!empty($xml->response)) {
+ foreach ($xml->response as $response) {
+ if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) &&
+ !$response->propstat->prop->resourcetype->collection) {
+ $id = basename($response->href);
+ $id = str_replace($this->url_vcard_extension, null, $id);
+
+ try {
+ $vcardData = $this->getVcard($id);
+
+ if (!empty($id)) {
+ $simplified_xml->startElement('element');
+ $simplified_xml->writeElement('id', $id);
+ $simplified_xml->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag));
+ $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
+
+ if ($include_vcards === true) {
+ $simplified_xml->writeElement('vcard', $vcardData);
+ }
+ $simplified_xml->endElement();
+ }
+ } catch (\Exception $e) {
+ error_log("Error fetching vCard: {$id}: {$e->getMessage()}\n");
+ }
+ } elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) {
+ if (isset($response->propstat->prop->href)) {
+ $href = $response->propstat->prop->href;
+ } elseif (isset($response->href)) {
+ $href = $response->href;
+ } else {
+ $href = null;
+ }
+
+ $url = str_replace($this->url_parts['path'], null, $this->url) . $href;
+ $simplified_xml->startElement('addressbook_element');
+ $simplified_xml->writeElement('display_name', $response->propstat->prop->displayname);
+ $simplified_xml->writeElement('url', $url);
+ $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
+ $simplified_xml->endElement();
+ }
+ }
+ }
+
+ $simplified_xml->endElement();
+ $simplified_xml->endDocument();
+
+ return $simplified_xml->outputMemory();
+ }
+
+ /**
+ * Cleans CardDAV XML response
+ *
+ * @param string $response CardDAV XML response
+ * @return string $response Cleaned CardDAV XML response
+ */
+ private function cleanResponse($response)
+ {
+ $response = utf8_encode($response);
+ $response = str_replace('D:', null, $response);
+ $response = str_replace('d:', null, $response);
+ $response = str_replace('C:', null, $response);
+ $response = str_replace('c:', null, $response);
+
+ return $response;
+ }
+
+ /**
+ * Query the CardDAV server via curl and returns the response
+ *
+ * @param string $url CardDAV server URL
+ * @param string $method HTTP method like (OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, COPY, MOVE)
+ * @param string $content Content for CardDAV queries
+ * @param string $content_type Set content type
+ * @return array Raw CardDAV Response and http status code
+ */
+ private function query($url, $method, $content = null, $content_type = null)
+ {
+ /*
+ if ($content !== null) {
+ error_log('POST');
+ curl_setopt($this->curl, CURLOPT_POST, true);
+ curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
+ } else {
+ curl_setopt($this->curl, CURLOPT_POST, false);
+ curl_setopt($this->curl, CURLOPT_POSTFIELDS, null);
+ }
+ */
if (!isset($this->client)) {
$this->client = new Client();
@@ -394,12 +380,10 @@ private function query($url, $method, $content = null, $content_type = null)
// error_log(Psr7\str($request));
$response = $this->client->send($request);
-// error_log("\nHEADERS");
+ // error_log("\nHEADERS");
// error_log(Psr7\str($response));
-// error_log("<<< HEADERS");
+ // error_log("<<< HEADERS");
- return $response;
- }
+ return $response;
+ }
}
-
-?>
diff --git a/src/ConfigTrait.php b/src/ConfigTrait.php
index 06c3afb2..d10d93a9 100644
--- a/src/ConfigTrait.php
+++ b/src/ConfigTrait.php
@@ -5,24 +5,26 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
-trait ConfigTrait {
-
- private $config;
-
- protected function addConfig() {
- $default = realpath(__DIR__ . '/../config.php');
-
- $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'config file', $default);
- }
-
- protected function loadConfig(InputInterface $input) {
- $configFile = $input->getOption('config');
-
- if (!file_exists($configFile)) {
- throw new \Exception('Config file ' . $configFile . ' does not exist');
- }
-
- require_once($configFile);
- $this->config = $config;
- }
+trait ConfigTrait
+{
+ private $config;
+
+ protected function addConfig()
+ {
+ $default = realpath(__DIR__ . '/../config.php');
+
+ $this->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'config file', $default);
+ }
+
+ protected function loadConfig(InputInterface $input)
+ {
+ $configFile = $input->getOption('config');
+
+ if (!file_exists($configFile)) {
+ throw new \Exception('Config file ' . $configFile . ' does not exist');
+ }
+
+ require_once($configFile);
+ $this->config = $config;
+ }
}
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index ea9d25f8..ce7eb16e 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -9,42 +9,44 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-class ConvertCommand extends Command {
-
- use ConfigTrait;
-
- protected function configure() {
- $this->setName('convert')
- ->setDescription('Convert Vcard to FritzBox format')
- ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export parse result to json file')
- ->addArgument('filename', InputArgument::REQUIRED, 'filename');
-
- $this->addConfig();
- }
-
- protected function execute(InputInterface $input, OutputInterface $output) {
- $this->loadConfig($input);
-
- $filename = $input->getArgument('filename');
- $xml = simplexml_load_file($filename);
-
- $conversions = $this->config['conversions'];
- $excludes = $this->config['excludes'];
- $phonebook = $this->config['phonebook'];
-
- // parse
- $cards = parse($xml, $conversions);
- $filtered = filter($cards, $excludes);
-
- if ($json = $input->getOption('json')) {
- file_put_contents($json, json_encode($filtered, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
- }
-
- error_log(sprintf("Converted %d vcards", count($filtered)));
-
- // convert
- $xml = export($phonebook['name'], $filtered, $conversions);
-
- echo $xml->asXML();
- }
+class ConvertCommand extends Command
+{
+ use ConfigTrait;
+
+ protected function configure()
+ {
+ $this->setName('convert')
+ ->setDescription('Convert Vcard to FritzBox format')
+ ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export parse result to json file')
+ ->addArgument('filename', InputArgument::REQUIRED, 'filename');
+
+ $this->addConfig();
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $this->loadConfig($input);
+
+ $filename = $input->getArgument('filename');
+ $xml = simplexml_load_file($filename);
+
+ $conversions = $this->config['conversions'];
+ $excludes = $this->config['excludes'];
+ $phonebook = $this->config['phonebook'];
+
+ // parse
+ $cards = parse($xml, $conversions);
+ $filtered = filter($cards, $excludes);
+
+ if ($json = $input->getOption('json')) {
+ file_put_contents($json, json_encode($filtered, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
+ }
+
+ error_log(sprintf("Converted %d vcards", count($filtered)));
+
+ // convert
+ $xml = export($phonebook['name'], $filtered, $conversions);
+
+ echo $xml->asXML();
+ }
}
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 0532cfbc..9b6e4278 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -10,33 +10,35 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
-class DownloadCommand extends Command {
+class DownloadCommand extends Command
+{
+ use ConfigTrait;
- use ConfigTrait;
+ protected function configure()
+ {
+ $this->setName('download')
+ ->setDescription('Load from CardDAV server');
- protected function configure() {
- $this->setName('download')
- ->setDescription('Load from CardDAV server');
+ $this->addConfig();
+ }
- $this->addConfig();
- }
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $this->loadConfig($input);
- protected function execute(InputInterface $input, OutputInterface $output) {
- $this->loadConfig($input);
+ $progress = new ProgressBar($output);
+ $progress->start();
- $progress = new ProgressBar($output);
- $progress->start();
+ $server = $this->config['server'];
+ $xmlStr = download($server['url'], $server['user'], $server['password'], function () use ($progress) {
+ $progress->advance();
+ });
- $server = $this->config['server'];
- $xmlStr = download($server['url'], $server['user'], $server['password'], function() use ($progress) {
- $progress->advance();
- });
+ $progress->finish();
- $progress->finish();
+ $count = countCards($xmlStr);
+ error_log(sprintf("\nDownloaded %d vcards", $count));
- $count = countCards($xmlStr);
- error_log(sprintf("\nDownloaded %d vcards", $count));
-
- echo $xmlStr;
- }
+ echo $xmlStr;
+ }
}
diff --git a/src/FritzBox/Api.php b/src/FritzBox/Api.php
index 005ffe3e..7b4b7f3e 100644
--- a/src/FritzBox/Api.php
+++ b/src/FritzBox/Api.php
@@ -1,251 +1,243 @@
config = new Config();
-
- // set FRITZ!Box-IP and URL
- $this->url = $url;
- $this->username = $user_name;
- $this->password = $password;
-
- $this->sid = $this->initSID();
- }
-
- /**
- * do a POST request on the box
- * the main cURL wrapper handles the command
- *
- * @param array $formfields an associative array with the POST fields to pass
- * @return string the raw HTML code returned by the Fritz!Box
- */
- public function doPostForm($formfields = array())
- {
- $ch = curl_init();
-
- if ( isset($formfields['getpage']) && strpos($formfields['getpage'], '.lua') > 0 )
- {
- curl_setopt($ch, CURLOPT_URL, $this->url . $formfields['getpage'] . '?sid=' . $this->sid);
- unset($formfields['getpage']);
- }
- else
- {
- // add the sid, if it is already set
- if ($this->sid != '0000000000000000')
- {
- $formfields['sid'] = $this->sid;
- }
- curl_setopt($ch, CURLOPT_URL, $this->url . '/cgi-bin/webcm');
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formfields));
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
-
- public function doPostFile($formfields = array(), $filefileds = array())
- {
- $ch = curl_init();
-
- // add the sid, if it is already set
- if ($this->sid != '0000000000000000')
- {
- // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!)
- // therfore we use array_merge to ensuere the foreach outputs 'sid' fist
- $formfields = array_merge(array('sid' => $this->sid), $formfields);
- //$formfields['sid'] = $this->sid;
- }
- curl_setopt($ch, CURLOPT_URL, $this->url . '/cgi-bin/firmwarecfg');
- curl_setopt($ch, CURLOPT_POST, 1);
-
- // enable for debugging:
- //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine)
- if(empty( $filefileds )) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query
- }
- // post calculated raw data
- else {
- $header = $this->_create_custom_file_post_header($formfields, $filefileds);
- curl_setopt($ch, CURLOPT_HTTPHEADER , array(
- 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) )
- );
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']);
- }
-
- $output = curl_exec($ch);
-
- // curl error
- if(curl_errno($ch)) {
- throw new \Exception(curl_error($ch)." (".curl_errno($ch).")");
- }
-
- // finger out an error message, if given
- preg_match('@
(.*?)
@is', $output, $matches);
- if (isset($matches[1]))
- {
- throw new \Exception(str_replace(' ', ' ', $matches[1]));
- }
-
- curl_close($ch);
- return $output;
- }
-
- private function _create_custom_file_post_header($postFields, $fileFields) {
- // form field separator
- $delimiter = '-------------' . uniqid();
-
- /*
- // file upload fields: name => array(type=>'mime/type',content=>'raw data')
- $fileFields = array(
- 'file1' => array(
- 'type' => 'text/xml',
- 'content' => '...your raw file content goes here...',
- 'filename' = 'filename.xml'
- ),
- );
- // all other fields (not file upload): name => value
- $postFields = array(
- 'otherformfield' => 'content of otherformfield is this text',
- );
- */
-
- $data = '';
-
- // populate normal fields first (simpler)
- foreach ($postFields as $name => $content) {
- $data .= "--" . $delimiter . "\r\n";
- $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"';
- $data .= "\r\n\r\n";
- $data .= $content;
- $data .= "\r\n";
- }
- // populate file fields
- foreach ($fileFields as $name => $file) {
- $data .= "--" . $delimiter . "\r\n";
- // "filename" attribute is not essential; server-side scripts may use it
- $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' .
- ' filename="' . $file['filename'] . '"' . "\r\n";
-
- //$data .= 'Content-Transfer-Encoding: binary'."\r\n";
- // this is, again, informative only; good practice to include though
- $data .= 'Content-Type: ' . $file['type'] . "\r\n";
- // this endline must be here to indicate end of headers
- $data .= "\r\n";
- // the file itself (note: there's no encoding of any kind)
- $data .= $file['content'] . "\r\n";
- }
- // last delimiter
- $data .= "--" . $delimiter . "--\r\n";
-
- return array('delimiter' => $delimiter, 'data' => $data);
- }
-
- /**
- * do a GET request on the box
- * the main cURL wrapper handles the command
- *
- * @param array $params an associative array with the GET params to pass
- * @return string the raw HTML code returned by the Fritz!Box
- */
- public function doGetRequest($params = array())
- {
- // add the sid, if it is already set
- if ($this->sid != '0000000000000000')
- {
- $params['sid'] = $this->sid;
- }
-
- if ( strpos($params['getpage'], '.lua') > 0 )
- {
- $getpage = $params['getpage'] . '?';
- unset($params['getpage']);
- }
- else
- {
- $getpage = '/cgi-bin/webcm?';
- }
-
- $url = $this->url . $getpage . http_build_query($params);
- $output = file_get_contents($url);
-
- return $output;
- }
-
- /**
- * the login method, handles the secured login-process
- * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks
- * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details
- *
- * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message
- */
- protected function initSID()
- {
- $loginpage = '/login_sid.lua';
-
- // read the current status
- $login = $this->doGetRequest(array('getpage' => $loginpage));
-
- $xml = simplexml_load_string($login);
- if ( $xml->SID != '0000000000000000' )
- {
- return $xml->SID;
- }
-
- // the challenge-response magic, pay attention to the mb_convert_encoding()
- $response = $xml->Challenge . '-' . md5(mb_convert_encoding($xml->Challenge . '-' . $this->password, "UCS-2LE", "UTF-8"));
-
- // do the login
- $formfields = array(
- 'getpage' => $loginpage,
- 'username' => $this->username,
- 'response' => $response
- );
-
- $output = $this->doGetRequest($formfields);
-
- // finger out the SID from the response
- $xml = simplexml_load_string($output);
- if ( $xml->SID != '0000000000000000' )
- {
- return (string)$xml->SID;
- }
-
- throw new \Exception('ERROR: Login failed with an unknown response.');
- }
-
- /**
- * a getter for the session ID
- *
- * @return string $this->sid
- */
- public function getSID()
- {
- return $this->sid;
- }
+class Api
+{
+ private $username;
+ private $password;
+ private $url;
+
+ protected $sid = '0000000000000000';
+
+ /**
+ * the constructor, initializes the object and calls the login method
+ *
+ * @access public
+ */
+ public function __construct($url = 'https://fritz.box', $user_name = false, $password = false, $force_local_login = false)
+ {
+ // init the config object
+ // $this->config = new Config();
+
+ // set FRITZ!Box-IP and URL
+ $this->url = $url;
+ $this->username = $user_name;
+ $this->password = $password;
+
+ $this->sid = $this->initSID();
+ }
+
+ /**
+ * do a POST request on the box
+ * the main cURL wrapper handles the command
+ *
+ * @param array $formfields an associative array with the POST fields to pass
+ * @return string the raw HTML code returned by the Fritz!Box
+ */
+ public function doPostForm($formfields = array())
+ {
+ $ch = curl_init();
+
+ if (isset($formfields['getpage']) && strpos($formfields['getpage'], '.lua') > 0) {
+ curl_setopt($ch, CURLOPT_URL, $this->url . $formfields['getpage'] . '?sid=' . $this->sid);
+ unset($formfields['getpage']);
+ } else {
+ // add the sid, if it is already set
+ if ($this->sid != '0000000000000000') {
+ $formfields['sid'] = $this->sid;
+ }
+ curl_setopt($ch, CURLOPT_URL, $this->url . '/cgi-bin/webcm');
+ }
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formfields));
+ $output = curl_exec($ch);
+ curl_close($ch);
+ return $output;
+ }
+
+ public function doPostFile($formfields = array(), $filefileds = array())
+ {
+ $ch = curl_init();
+
+ // add the sid, if it is already set
+ if ($this->sid != '0000000000000000') {
+ // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!)
+ // therfore we use array_merge to ensuere the foreach outputs 'sid' fist
+ $formfields = array_merge(array('sid' => $this->sid), $formfields);
+ //$formfields['sid'] = $this->sid;
+ }
+ curl_setopt($ch, CURLOPT_URL, $this->url . '/cgi-bin/firmwarecfg');
+ curl_setopt($ch, CURLOPT_POST, 1);
+
+ // enable for debugging:
+ //curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+
+ // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine)
+ if (empty($filefileds)) {
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query
+ }
+ // post calculated raw data
+ else {
+ $header = $this->_create_custom_file_post_header($formfields, $filefileds);
+ curl_setopt(
+ $ch,
+ CURLOPT_HTTPHEADER,
+ array(
+ 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) )
+ );
+
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']);
+ }
+
+ $output = curl_exec($ch);
+
+ // curl error
+ if (curl_errno($ch)) {
+ throw new \Exception(curl_error($ch)." (".curl_errno($ch).")");
+ }
+
+ // finger out an error message, if given
+ preg_match('@
(.*?)
@is', $output, $matches);
+ if (isset($matches[1])) {
+ throw new \Exception(str_replace(' ', ' ', $matches[1]));
+ }
+
+ curl_close($ch);
+ return $output;
+ }
+
+ private function _create_custom_file_post_header($postFields, $fileFields)
+ {
+ // form field separator
+ $delimiter = '-------------' . uniqid();
+
+ /*
+ // file upload fields: name => array(type=>'mime/type',content=>'raw data')
+ $fileFields = array(
+ 'file1' => array(
+ 'type' => 'text/xml',
+ 'content' => '...your raw file content goes here...',
+ 'filename' = 'filename.xml'
+ ),
+ );
+ // all other fields (not file upload): name => value
+ $postFields = array(
+ 'otherformfield' => 'content of otherformfield is this text',
+ );
+ */
+
+ $data = '';
+
+ // populate normal fields first (simpler)
+ foreach ($postFields as $name => $content) {
+ $data .= "--" . $delimiter . "\r\n";
+ $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"';
+ $data .= "\r\n\r\n";
+ $data .= $content;
+ $data .= "\r\n";
+ }
+ // populate file fields
+ foreach ($fileFields as $name => $file) {
+ $data .= "--" . $delimiter . "\r\n";
+ // "filename" attribute is not essential; server-side scripts may use it
+ $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' .
+ ' filename="' . $file['filename'] . '"' . "\r\n";
+
+ //$data .= 'Content-Transfer-Encoding: binary'."\r\n";
+ // this is, again, informative only; good practice to include though
+ $data .= 'Content-Type: ' . $file['type'] . "\r\n";
+ // this endline must be here to indicate end of headers
+ $data .= "\r\n";
+ // the file itself (note: there's no encoding of any kind)
+ $data .= $file['content'] . "\r\n";
+ }
+ // last delimiter
+ $data .= "--" . $delimiter . "--\r\n";
+
+ return array('delimiter' => $delimiter, 'data' => $data);
+ }
+
+ /**
+ * do a GET request on the box
+ * the main cURL wrapper handles the command
+ *
+ * @param array $params an associative array with the GET params to pass
+ * @return string the raw HTML code returned by the Fritz!Box
+ */
+ public function doGetRequest($params = array())
+ {
+ // add the sid, if it is already set
+ if ($this->sid != '0000000000000000') {
+ $params['sid'] = $this->sid;
+ }
+
+ if (strpos($params['getpage'], '.lua') > 0) {
+ $getpage = $params['getpage'] . '?';
+ unset($params['getpage']);
+ } else {
+ $getpage = '/cgi-bin/webcm?';
+ }
+
+ $url = $this->url . $getpage . http_build_query($params);
+ $output = file_get_contents($url);
+
+ return $output;
+ }
+
+ /**
+ * the login method, handles the secured login-process
+ * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks
+ * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details
+ *
+ * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message
+ */
+ protected function initSID()
+ {
+ $loginpage = '/login_sid.lua';
+
+ // read the current status
+ $login = $this->doGetRequest(array('getpage' => $loginpage));
+
+ $xml = simplexml_load_string($login);
+ if ($xml->SID != '0000000000000000') {
+ return $xml->SID;
+ }
+
+ // the challenge-response magic, pay attention to the mb_convert_encoding()
+ $response = $xml->Challenge . '-' . md5(mb_convert_encoding($xml->Challenge . '-' . $this->password, "UCS-2LE", "UTF-8"));
+
+ // do the login
+ $formfields = array(
+ 'getpage' => $loginpage,
+ 'username' => $this->username,
+ 'response' => $response
+ );
+
+ $output = $this->doGetRequest($formfields);
+
+ // finger out the SID from the response
+ $xml = simplexml_load_string($output);
+ if ($xml->SID != '0000000000000000') {
+ return (string)$xml->SID;
+ }
+
+ throw new \Exception('ERROR: Login failed with an unknown response.');
+ }
+
+ /**
+ * a getter for the session ID
+ *
+ * @return string $this->sid
+ */
+ public function getSID()
+ {
+ return $this->sid;
+ }
}
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index 45802648..3e5a19ac 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -6,166 +6,166 @@
class Converter
{
- private $config;
-
- public function __construct($config)
- {
- $this->config = $config;
- }
-
- public function convert($card): SimpleXMLElement
- {
- $this->card = $card;
-
- // $contact = $xml->addChild('contact');
- $this->contact = new SimpleXMLElement('');
-
- $this->addVip();
-
- $person = $this->contact->addChild('person');
- $name = htmlspecialchars($this->getProperty('realName'));
- $person->addChild('realName', $name);
- // $person->addChild('ImageURL');
-
- $this->addPhone();
- $this->addEmail();
-
- $person = $this->contact->addChild('setup');
-
- // print_r($this->contact);
- // echo($this->contact->asXML().PHP_EOL);
-
- return $this->contact;
- }
-
- private function addVip()
- {
- if (isset($this->card->category)) {
- $vipCategories = $this->config['vipCategories'] ?? array();
-
- if (in_array($this->card->category, $vipCategories)) {
- $this->contact->addChild('category', 1);
- }
- }
- }
-
- private function addPhone()
- {
- //
- // +490358179022
- // +400746653254
-
- $telephony = $this->contact->addChild('telephony');
-
- $replaceCharacters = $this->config['phoneReplaceCharacters'] ?? array();
- $phoneTypes = $this->config['phoneTypes'] ?? array();
-
- if (isset($this->card->phone)) {
- foreach ($this->card->phone as $numberType => $numbers) {
- foreach ($numbers as $idx => $number) {
- if (count($replaceCharacters)) {
- $number = strtr($number, $replaceCharacters);
- $number = trim(preg_replace('/\s+/', ' ', $number));
- }
-
- $phone = $telephony->addChild('number', $number);
- $phone->addAttribute('id', $idx);
-
- $type = 'other';
-
- foreach ($phoneTypes as $type => $value) {
- if (strpos($numberType, $type) !== false) {
- $type = $value;
- if (strpos($numberType, 'FAX') !== false) {
- $type = 'fax_' . $type;
- }
-
- break;
- }
- }
-
- $phone->addAttribute('type', $type);
-
- if (strpos($numberType, 'pref') !== false) {
- $phone->addAttribute('prio', 1);
- }
-
- // $phone->addAttribute('vanity', '');
- }
- }
- }
- }
-
- private function addEmail()
- {
- //
- // KTS.Michaelis.Hannover@evlka.de
- // Kindertagesstaette@michaelis-hannover.de
-
- $services = $this->contact->addChild('services');
- $emailTypes = $this->config['emailTypes'] ?? array();
-
- if (isset($this->card->email)) {
- foreach ($this->card->email as $emailType => $addresses) {
- foreach ($addresses as $idx => $addr) {
- $email = $services->addChild('email', $addr);
- $email->addAttribute('id', $idx);
-
- foreach ($emailTypes as $type => $value) {
- if (strpos($emailType, $type) !== false) {
- $email->addAttribute('classifier', $value);
- break;
- }
- }
-
- // $email->addAttribute('vanity', '');
- }
- }
- }
- }
-
- private function getProperty(string $property): string
- {
- if (null === ($rules = $this->config[$property] ?? null)) {
- throw new \Exception("Missing conversion definition for `$property`");
- }
-
- foreach ($rules as $rule) {
- // parse rule into tokens
- $token_format = '/{([^}]+)}/';
- preg_match_all($token_format, $rule, $tokens);
-
- if (!count($tokens)) {
- throw new \Exception("Invalid conversion definition for `$property`");
- }
-
- // print_r($tokens);
- $replacements = [];
-
- // check card for tokens
- foreach ($tokens[1] as $idx => $token) {
- // echo $idx.PHP_EOL;
- if (isset($this->card->$token) && $this->card->$token) {
- // echo $tokens[0][$idx].PHP_EOL;
- $replacements[$token] = $this->card->$token;
- // echo $this->card->$token;
- }
- }
-
- // check if all tokens found
- if (count($replacements) !== count($tokens[0])) {
- continue;
- }
-
- // replace
- return preg_replace_callback($token_format, function($match) use ($replacements) {
- $token = $match[1];
- return $replacements[$token];
- }, $rule);
- }
-
- error_log("No data for conversion `$property`");
-
- return '';
- }
-}
\ No newline at end of file
+ private $config;
+
+ public function __construct($config)
+ {
+ $this->config = $config;
+ }
+
+ public function convert($card): SimpleXMLElement
+ {
+ $this->card = $card;
+
+ // $contact = $xml->addChild('contact');
+ $this->contact = new SimpleXMLElement('');
+
+ $this->addVip();
+
+ $person = $this->contact->addChild('person');
+ $name = htmlspecialchars($this->getProperty('realName'));
+ $person->addChild('realName', $name);
+ // $person->addChild('ImageURL');
+
+ $this->addPhone();
+ $this->addEmail();
+
+ $person = $this->contact->addChild('setup');
+
+ // print_r($this->contact);
+ // echo($this->contact->asXML().PHP_EOL);
+
+ return $this->contact;
+ }
+
+ private function addVip()
+ {
+ if (isset($this->card->category)) {
+ $vipCategories = $this->config['vipCategories'] ?? array();
+
+ if (in_array($this->card->category, $vipCategories)) {
+ $this->contact->addChild('category', 1);
+ }
+ }
+ }
+
+ private function addPhone()
+ {
+ //
+ // +490358179022
+ // +400746653254
+
+ $telephony = $this->contact->addChild('telephony');
+
+ $replaceCharacters = $this->config['phoneReplaceCharacters'] ?? array();
+ $phoneTypes = $this->config['phoneTypes'] ?? array();
+
+ if (isset($this->card->phone)) {
+ foreach ($this->card->phone as $numberType => $numbers) {
+ foreach ($numbers as $idx => $number) {
+ if (count($replaceCharacters)) {
+ $number = strtr($number, $replaceCharacters);
+ $number = trim(preg_replace('/\s+/', ' ', $number));
+ }
+
+ $phone = $telephony->addChild('number', $number);
+ $phone->addAttribute('id', $idx);
+
+ $type = 'other';
+
+ foreach ($phoneTypes as $type => $value) {
+ if (strpos($numberType, $type) !== false) {
+ $type = $value;
+ if (strpos($numberType, 'FAX') !== false) {
+ $type = 'fax_' . $type;
+ }
+
+ break;
+ }
+ }
+
+ $phone->addAttribute('type', $type);
+
+ if (strpos($numberType, 'pref') !== false) {
+ $phone->addAttribute('prio', 1);
+ }
+
+ // $phone->addAttribute('vanity', '');
+ }
+ }
+ }
+ }
+
+ private function addEmail()
+ {
+ //
+ // KTS.Michaelis.Hannover@evlka.de
+ // Kindertagesstaette@michaelis-hannover.de
+
+ $services = $this->contact->addChild('services');
+ $emailTypes = $this->config['emailTypes'] ?? array();
+
+ if (isset($this->card->email)) {
+ foreach ($this->card->email as $emailType => $addresses) {
+ foreach ($addresses as $idx => $addr) {
+ $email = $services->addChild('email', $addr);
+ $email->addAttribute('id', $idx);
+
+ foreach ($emailTypes as $type => $value) {
+ if (strpos($emailType, $type) !== false) {
+ $email->addAttribute('classifier', $value);
+ break;
+ }
+ }
+
+ // $email->addAttribute('vanity', '');
+ }
+ }
+ }
+ }
+
+ private function getProperty(string $property): string
+ {
+ if (null === ($rules = $this->config[$property] ?? null)) {
+ throw new \Exception("Missing conversion definition for `$property`");
+ }
+
+ foreach ($rules as $rule) {
+ // parse rule into tokens
+ $token_format = '/{([^}]+)}/';
+ preg_match_all($token_format, $rule, $tokens);
+
+ if (!count($tokens)) {
+ throw new \Exception("Invalid conversion definition for `$property`");
+ }
+
+ // print_r($tokens);
+ $replacements = [];
+
+ // check card for tokens
+ foreach ($tokens[1] as $idx => $token) {
+ // echo $idx.PHP_EOL;
+ if (isset($this->card->$token) && $this->card->$token) {
+ // echo $tokens[0][$idx].PHP_EOL;
+ $replacements[$token] = $this->card->$token;
+ // echo $this->card->$token;
+ }
+ }
+
+ // check if all tokens found
+ if (count($replacements) !== count($tokens[0])) {
+ continue;
+ }
+
+ // replace
+ return preg_replace_callback($token_format, function ($match) use ($replacements) {
+ $token = $match[1];
+ return $replacements[$token];
+ }, $rule);
+ }
+
+ error_log("No data for conversion `$property`");
+
+ return '';
+ }
+}
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 0c9183d2..5e7abbd8 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -10,18 +10,20 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
-class RunCommand extends Command {
-
+class RunCommand extends Command
+{
use ConfigTrait;
- protected function configure() {
+ protected function configure()
+ {
$this->setName('run')
->setDescription('Download, convert and upload - all in one');
$this->addConfig();
}
- protected function execute(InputInterface $input, OutputInterface $output) {
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
$this->loadConfig($input);
// download
@@ -31,7 +33,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$progress->start();
$server = $this->config['server'];
- $xmlStr = download($server['url'], $server['user'], $server['password'], function() use ($progress) {
+ $xmlStr = download($server['url'], $server['user'], $server['password'], function () use ($progress) {
$progress->advance();
});
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index 0f9285fa..0c7b9779 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -9,29 +9,31 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-class UploadCommand extends Command {
+class UploadCommand extends Command
+{
+ use ConfigTrait;
- use ConfigTrait;
+ protected function configure()
+ {
+ $this->setName('upload')
+ ->setDescription('Upload to FritzBox')
+ ->addArgument('filename', InputArgument::REQUIRED, 'filename');
- protected function configure() {
- $this->setName('upload')
- ->setDescription('Upload to FritzBox')
- ->addArgument('filename', InputArgument::REQUIRED, 'filename');
+ $this->addConfig();
+ }
- $this->addConfig();
- }
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $this->loadConfig($input);
- protected function execute(InputInterface $input, OutputInterface $output) {
- $this->loadConfig($input);
+ $filename = $input->getArgument('filename');
+ $xml = file_get_contents($filename);
- $filename = $input->getArgument('filename');
- $xml = file_get_contents($filename);
+ $fritzbox = $this->config['fritzbox'];
+ $phonebook = $this->config['phonebook'];
- $fritzbox = $this->config['fritzbox'];
- $phonebook = $this->config['phonebook'];
+ upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
- upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
-
- error_log("Uploaded fritz phonebook");
- }
+ error_log("Uploaded fritz phonebook");
+ }
}
diff --git a/src/Vcard/Parser.php b/src/Vcard/Parser.php
index c1204212..f8941488 100644
--- a/src/Vcard/Parser.php
+++ b/src/Vcard/Parser.php
@@ -53,7 +53,8 @@ public function __construct($content)
/**
* IteratorAggregate
*/
- public function getIterator() {
+ public function getIterator()
+ {
foreach ($this->vcardObjects as $vcard) {
yield $vcard;
}
@@ -134,7 +135,7 @@ protected function parse()
// or can be prefixed with "type=". E.g.: "INTERNET" or
// "type=INTERNET".
if (!empty($types)) {
- $types = array_map(function($type) {
+ $types = array_map(function ($type) {
return preg_replace('/^type=/i', '', $type);
}, $types);
}
diff --git a/src/functions.php b/src/functions.php
index 66aade49..c40a9dde 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -8,127 +8,130 @@
use Andig\FritzBox\Api;
use \SimpleXMLElement;
-
-function download($url, $user, $password, callable $callback=null): string {
- $backend = new Backend($url);
- $backend->setAuth($user, $password);
- $backend->setProgress($callback);
- return $backend->get();
+function download($url, $user, $password, callable $callback=null): string
+{
+ $backend = new Backend($url);
+ $backend->setAuth($user, $password);
+ $backend->setProgress($callback);
+ return $backend->get();
}
-function countCards($xml): int {
- $count = 0;
- $xml = simplexml_load_string($xml);
-
- foreach ($xml->element as $element) {
- foreach ($element->vcard as $vcard) {
- $count++;
- }
- }
- return $count;
+function countCards($xml): int
+{
+ $count = 0;
+ $xml = simplexml_load_string($xml);
+
+ foreach ($xml->element as $element) {
+ foreach ($element->vcard as $vcard) {
+ $count++;
+ }
+ }
+ return $count;
}
function parse(SimpleXMLElement $xml, array $conversions): array
{
- $cards = [];
- $groups = [];
-
- // parse all vcards
- foreach ($xml->element as $element) {
- foreach ($element->vcard as $vcard) {
- $parser = new Parser($vcard);
- $card = $parser->getCardAtIndex(0);
-
- // separate iCloud groups
- if (isset($card->xabsmember)) {
- $groups[$card->fullname] = $card->xabsmember;
- continue;
- }
-
- $cards[] = $card;
- // print_r($card);
- }
- }
-
- // assign group memberships
- foreach ($cards as $card) {
- foreach ($groups as $group => $members) {
- if (in_array($card->uid, $members)) {
- if (!isset($card->group)) {
- $card->group = array();
- }
-
- $card->group = $group;
- break;
- }
- }
- }
-
- return $cards;
+ $cards = [];
+ $groups = [];
+
+ // parse all vcards
+ foreach ($xml->element as $element) {
+ foreach ($element->vcard as $vcard) {
+ $parser = new Parser($vcard);
+ $card = $parser->getCardAtIndex(0);
+
+ // separate iCloud groups
+ if (isset($card->xabsmember)) {
+ $groups[$card->fullname] = $card->xabsmember;
+ continue;
+ }
+
+ $cards[] = $card;
+ // print_r($card);
+ }
+ }
+
+ // assign group memberships
+ foreach ($cards as $card) {
+ foreach ($groups as $group => $members) {
+ if (in_array($card->uid, $members)) {
+ if (!isset($card->group)) {
+ $card->group = array();
+ }
+
+ $card->group = $group;
+ break;
+ }
+ }
+ }
+
+ return $cards;
}
function filter(array $cards, array $filters): array
{
- $result = [];
+ $result = [];
- foreach ($cards as $card) {
- $filterMatched = false;
+ foreach ($cards as $card) {
+ $filterMatched = false;
- foreach ($filters as $filterAttribute => $filterValues) {
- if (isset($card->$filterAttribute)) {
- if (filterMatches($card->$filterAttribute, $filterValues)) {
- $filterMatched = true;
- break;
- }
- }
- }
+ foreach ($filters as $filterAttribute => $filterValues) {
+ if (isset($card->$filterAttribute)) {
+ if (filterMatches($card->$filterAttribute, $filterValues)) {
+ $filterMatched = true;
+ break;
+ }
+ }
+ }
- if ($filterMatched) {
- break;
- }
+ if ($filterMatched) {
+ break;
+ }
- $result[] = $card;
- }
+ $result[] = $card;
+ }
- return $result;
+ return $result;
}
-function filterMatches($attribute, $filterValues): bool {
- if (!is_array($filterValues)) {
- $filterValues = array($filterMatches);
- }
+function filterMatches($attribute, $filterValues): bool
+{
+ if (!is_array($filterValues)) {
+ $filterValues = array($filterMatches);
+ }
- foreach ($filterValues as $filter) {
- if ($attribute === $filter) {
- return true;
- }
- }
+ foreach ($filterValues as $filter) {
+ if ($attribute === $filter) {
+ return true;
+ }
+ }
- return false;
+ return false;
}
function export(string $name, array $cards, array $conversions): SimpleXMLElement
{
- $xml = new SimpleXMLElement(<<
EOT
- );
+ );
- $root = $xml->xpath('//phonebook')[0];
- $root->addAttribute('name', $name);
+ $root = $xml->xpath('//phonebook')[0];
+ $root->addAttribute('name', $name);
- $converter = new Converter($conversions);
+ $converter = new Converter($conversions);
- foreach ($cards as $card) {
- $contact = $converter->convert($card);
- // $root->addChild('contact', $contact);
- xml_adopt($root, $contact);
- }
+ foreach ($cards as $card) {
+ $contact = $converter->convert($card);
+ // $root->addChild('contact', $contact);
+ xml_adopt($root, $contact);
+ }
- return $xml;
+ return $xml;
}
// https://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
@@ -142,23 +145,23 @@ function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
{
- $fritz = new Api($url, $user, $password, 1);
-
- $formfields = array(
- 'PhonebookId' => $phonebook
- );
-
- $filefields = array(
- 'PhonebookImportFile' => array(
- 'type' => 'text/xml',
- 'filename' => 'updatepb.xml',
- 'content' => $xml,
- )
- );
-
- $result = $fritz->doPostFile($formfields, $filefields); // send the command
-
- if (strpos($result, 'Das Telefonbuch der FRITZ!Box wurde wiederhergestellt') === false) {
- throw new \Exception('Upload failed');
- }
-}
\ No newline at end of file
+ $fritz = new Api($url, $user, $password, 1);
+
+ $formfields = array(
+ 'PhonebookId' => $phonebook
+ );
+
+ $filefields = array(
+ 'PhonebookImportFile' => array(
+ 'type' => 'text/xml',
+ 'filename' => 'updatepb.xml',
+ 'content' => $xml,
+ )
+ );
+
+ $result = $fritz->doPostFile($formfields, $filefields); // send the command
+
+ if (strpos($result, 'Das Telefonbuch der FRITZ!Box wurde wiederhergestellt') === false) {
+ throw new \Exception('Upload failed');
+ }
+}
From 790fa93c6a942f69005cc7d4cf7b8e0beca3edc5 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 12:43:04 +0100
Subject: [PATCH 027/165] More cleanup
---
src/CardDav/Backend.php | 64 +++--------------------------------------
1 file changed, 4 insertions(+), 60 deletions(-)
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 0e6194e6..5f3d93ae 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -18,33 +18,19 @@
class Backend
{
- /**
- * CardDAV PHP Version
- *
- * @constant string
- */
- const VERSION = '0.7';
-
- /**
- * User agent displayed in http requests
- *
- * @constant string
- */
- const USERAGENT = 'CardDAV PHP/';
-
/**
* CardDAV server url
*
* @var string
*/
- private $url = null;
+ private $url;
/**
* CardDAV server url_parts
*
* @var array
*/
- private $url_parts = null;
+ private $url_parts;
/**
* VCard File URL Extension
@@ -53,26 +39,19 @@ class Backend
*/
private $url_vcard_extension = '.vcf';
- /**
- * Authentication string
- *
- * @var string
- */
- private $auth = null;
-
/**
* Authentication: username
*
* @var string
*/
- private $username = null;
+ private $username;
/**
* Authentication: password
*
* @var string
*/
- private $password = null;
+ private $password;
/**
* Characters used for vCard id generation
@@ -81,41 +60,6 @@ class Backend
*/
private $vcard_id_chars = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F');
- /**
- * CardDAV server connection (curl handle)
- *
- * @var resource
- */
- private $curl;
-
- /**
- * Follow redirects (Location Header)
- *
- * @var boolean
- */
- private $follow_redirects = true;
-
- /**
- * Maximum redirects to follow
- *
- * @var integer
- */
- private $follow_redirects_count = 3;
-
- /**
- * Debug on or off
- *
- * @var boolean
- */
- private $debug = false;
-
- /**
- * All available debug information
- *
- * @var array
- */
- private $debug_information = array();
-
/**
* Progress callback
*/
From af4bc4cc210c7c9b4ab5dc544e15de2637695189 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 12:56:53 +0100
Subject: [PATCH 028/165] More cleanup
---
src/CardDav/Backend.php | 125 +++++++---------------------------------
1 file changed, 21 insertions(+), 104 deletions(-)
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 5f3d93ae..7e951b94 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -53,13 +53,6 @@ class Backend
*/
private $password;
- /**
- * Characters used for vCard id generation
- *
- * @var array
- */
- private $vcard_id_chars = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F');
-
/**
* Progress callback
*/
@@ -83,7 +76,7 @@ public function __construct($url)
// workaround for providers that don't use the default .vcf extension
if (strpos($this->url, "google.com")) {
- $this->setVcardExtension("");
+ $this->url_vcard_extension = '';
}
}
@@ -95,63 +88,6 @@ public function setProgress($callback = null)
$this->callback = $callback;
}
- /**
- * Sets the CardDAV vcard url extension
- *
- * Most providers do requests handling Vcards with .vcf, however
- * this isn't always the case and some providers (such as Google)
- * returned a 404 if the .vcf extension is used - or the other
- * way around, returning 404 unless .vcf is used.
- *
- * Both approaches are technically correct, see rfc635
- * http://tools.ietf.org/html/rfc6352
- *
- *
- * @param string $extension File extension
- * @return void
- */
- public function setVcardExtension($extension)
- {
- $this->url_vcard_extension = $extension;
- }
-
- /**
- * Sets authentication information
- *
- * @param string $username CardDAV server username
- * @param string $password CardDAV server password
- * @return void
- */
- public function setAuth($username, $password)
- {
- $this->username = $username;
- $this->password = $password;
- $this->auth = $username . ':' . $password;
- }
-
- /**
- * Sets wether to follow redirects and if yes how often
- *
- * @param boolean $follow_redirects
- * @param integer $follow_redirects_count
- * @return void
- */
- public function setFollowRedirects($follow_redirects, $follow_redirects_count = 3)
- {
- $this->follow_redirects = $follow_redirects && $follow_redirects_count > 0;
- $this->follow_redirects_count = $follow_redirects_count > 0 ? $follow_redirects_count : 0;
- }
-
- /**
- * Gets all available debug information
- *
- * @return array $this->debug_information All available debug information
- */
- public function getDebug()
- {
- return $this->debug_information;
- }
-
/**
* Gets all vCards including additional information from the CardDAV server
*
@@ -206,12 +142,12 @@ private function simplify($response, $include_vcards = true)
$response = $this->cleanResponse($response);
$xml = new \SimpleXMLElement($response);
- $simplified_xml = new \XMLWriter();
- $simplified_xml->openMemory();
- $simplified_xml->setIndent(4);
+ $xmlRes = new \XMLWriter();
+ $xmlRes->openMemory();
+ $xmlRes->setIndent(4);
- $simplified_xml->startDocument('1.0', 'utf-8');
- $simplified_xml->startElement('response');
+ $xmlRes->startDocument('1.0', 'utf-8');
+ $xmlRes->startElement('response');
if (!empty($xml->response)) {
foreach ($xml->response as $response) {
@@ -224,15 +160,15 @@ private function simplify($response, $include_vcards = true)
$vcardData = $this->getVcard($id);
if (!empty($id)) {
- $simplified_xml->startElement('element');
- $simplified_xml->writeElement('id', $id);
- $simplified_xml->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag));
- $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
+ $xmlRes->startElement('element');
+ $xmlRes->writeElement('id', $id);
+ $xmlRes->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag));
+ $xmlRes->writeElement('last_modified', $response->propstat->prop->getlastmodified);
if ($include_vcards === true) {
- $simplified_xml->writeElement('vcard', $vcardData);
+ $xmlRes->writeElement('vcard', $vcardData);
}
- $simplified_xml->endElement();
+ $xmlRes->endElement();
}
} catch (\Exception $e) {
error_log("Error fetching vCard: {$id}: {$e->getMessage()}\n");
@@ -247,19 +183,19 @@ private function simplify($response, $include_vcards = true)
}
$url = str_replace($this->url_parts['path'], null, $this->url) . $href;
- $simplified_xml->startElement('addressbook_element');
- $simplified_xml->writeElement('display_name', $response->propstat->prop->displayname);
- $simplified_xml->writeElement('url', $url);
- $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified);
- $simplified_xml->endElement();
+ $xmlRes->startElement('addressbook_element');
+ $xmlRes->writeElement('display_name', $response->propstat->prop->displayname);
+ $xmlRes->writeElement('url', $url);
+ $xmlRes->writeElement('last_modified', $response->propstat->prop->getlastmodified);
+ $xmlRes->endElement();
}
}
}
- $simplified_xml->endElement();
- $simplified_xml->endDocument();
+ $xmlRes->endElement();
+ $xmlRes->endDocument();
- return $simplified_xml->outputMemory();
+ return $xmlRes->outputMemory();
}
/**
@@ -290,21 +226,7 @@ private function cleanResponse($response)
*/
private function query($url, $method, $content = null, $content_type = null)
{
- /*
- if ($content !== null) {
- error_log('POST');
- curl_setopt($this->curl, CURLOPT_POST, true);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
- } else {
- curl_setopt($this->curl, CURLOPT_POST, false);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, null);
- }
- */
-
- if (!isset($this->client)) {
- $this->client = new Client();
- }
-
+ $this->client = $this->client ?? new Client();
$request = new Request($method, $url, [
'Depth' => '1'
]);
@@ -322,12 +244,7 @@ private function query($url, $method, $content = null, $content_type = null)
$request = $request->withHeader('Authorization', 'Basic '.$credentials);
}
- // error_log(Psr7\str($request));
$response = $this->client->send($request);
- // error_log("\nHEADERS");
- // error_log(Psr7\str($response));
- // error_log("<<< HEADERS");
-
return $response;
}
}
From 6a5b091cac6b866017694494f7da1410615ece66 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 27 Nov 2017 16:19:38 +0100
Subject: [PATCH 029/165] Fix setAuth
---
src/CardDav/Backend.php | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 7e951b94..38d489d0 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -88,6 +88,15 @@ public function setProgress($callback = null)
$this->callback = $callback;
}
+ /**
+ * Set credentials
+ */
+ public function setAuth(string $username, string $password)
+ {
+ $this->username = $username;
+ $this->password = $password;
+ }
+
/**
* Gets all vCards including additional information from the CardDAV server
*
From eb690d38340caba500c05c38628faa0de3997069 Mon Sep 17 00:00:00 2001
From: andig
Date: Tue, 28 Nov 2017 18:08:31 +0100
Subject: [PATCH 030/165] Fix filtering
---
src/functions.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/functions.php b/src/functions.php
index c40a9dde..4c6f4fed 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -45,7 +45,7 @@ function parse(SimpleXMLElement $xml, array $conversions): array
$groups[$card->fullname] = $card->xabsmember;
continue;
}
-
+
$cards[] = $card;
// print_r($card);
}
@@ -85,7 +85,7 @@ function filter(array $cards, array $filters): array
}
if ($filterMatched) {
- break;
+ continue;
}
$result[] = $card;
From 056ffe1f458c516c068efcc0275bbf9bb4846bf5 Mon Sep 17 00:00:00 2001
From: andig
Date: Wed, 29 Nov 2017 09:11:33 +0100
Subject: [PATCH 031/165] Fix vip filter
---
src/FritzBox/Converter.php | 13 ++++++-------
src/functions.php | 26 ++++++++++++++------------
2 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index 3e5a19ac..9ce3f6f3 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -2,6 +2,7 @@
namespace Andig\FritzBox;
+use Andig;
use \SimpleXMLElement;
class Converter
@@ -40,12 +41,10 @@ public function convert($card): SimpleXMLElement
private function addVip()
{
- if (isset($this->card->category)) {
- $vipCategories = $this->config['vipCategories'] ?? array();
-
- if (in_array($this->card->category, $vipCategories)) {
- $this->contact->addChild('category', 1);
- }
+ $vipCategories = $this->config['vip'] ?? array();
+
+ if (Andig\filtersMatch($this->card, $vipCategories)) {
+ $this->contact->addChild('category', 1);
}
}
@@ -56,7 +55,7 @@ private function addPhone()
// +400746653254
$telephony = $this->contact->addChild('telephony');
-
+
$replaceCharacters = $this->config['phoneReplaceCharacters'] ?? array();
$phoneTypes = $this->config['phoneTypes'] ?? array();
diff --git a/src/functions.php b/src/functions.php
index 4c6f4fed..6ffb5c02 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -73,18 +73,7 @@ function filter(array $cards, array $filters): array
$result = [];
foreach ($cards as $card) {
- $filterMatched = false;
-
- foreach ($filters as $filterAttribute => $filterValues) {
- if (isset($card->$filterAttribute)) {
- if (filterMatches($card->$filterAttribute, $filterValues)) {
- $filterMatched = true;
- break;
- }
- }
- }
-
- if ($filterMatched) {
+ if (filtersMatch($card, $filters)) {
continue;
}
@@ -94,6 +83,19 @@ function filter(array $cards, array $filters): array
return $result;
}
+function filtersMatch(object $card, array $filters): bool
+{
+ foreach ($filters as $attribute => $values) {
+ if (isset($card->$attribute)) {
+ if (filterMatches($card->$attribute, $values)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
function filterMatches($attribute, $filterValues): bool
{
if (!is_array($filterValues)) {
From ec73c0226197234d0697e468672037d821d27fbf Mon Sep 17 00:00:00 2001
From: andig
Date: Tue, 28 Nov 2017 18:03:05 +0100
Subject: [PATCH 032/165] wip
---
src/CardDav/Backend.php | 102 +++++++++++++++-------------------------
src/ConvertCommand.php | 16 +++++--
src/DownloadCommand.php | 16 +++++--
src/RunCommand.php | 10 ++--
src/functions.php | 70 +++++++++++++++------------
5 files changed, 103 insertions(+), 111 deletions(-)
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 38d489d0..53396517 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -25,13 +25,6 @@ class Backend
*/
private $url;
- /**
- * CardDAV server url_parts
- *
- * @var array
- */
- private $url_parts;
-
/**
* VCard File URL Extension
*
@@ -64,7 +57,13 @@ class Backend
*
* @param string $url CardDAV server url
*/
- public function __construct($url)
+ public function __construct(string $url=null) {
+ if ($url) {
+ $this->setUrl($url);
+ }
+ }
+
+ public function setUrl(string $url)
{
$this->url = $url;
@@ -72,8 +71,6 @@ public function __construct($url)
$this->url = $this->url . '/';
}
- $this->url_parts = parse_url($this->url);
-
// workaround for providers that don't use the default .vcf extension
if (strpos($this->url, "google.com")) {
$this->url_vcard_extension = '';
@@ -103,7 +100,7 @@ public function setAuth(string $username, string $password)
* @param boolean $include_vcards Include vCards within the response (simplified only)
* @return string Raw or simplified XML response
*/
- public function get($include_vcards = true)
+ public function getVcards($include_vcards = true)
{
$response = $this->query($this->url, 'PROPFIND');
@@ -115,6 +112,25 @@ public function get($include_vcards = true)
throw new \Exception('Received HTTP ' . $response->getStatusCode());
}
+ public function fetchImage($uri)
+ {
+ $this->client = $this->client ?? new Client();
+ $request = new Request('GET', $uri);
+
+ if ($this->username) {
+ $credentials = base64_encode($this->username.':'.$this->password);
+ $request = $request->withHeader('Authorization', 'Basic '.$credentials);
+ }
+
+ $response = $this->client->send($request);
+
+ if (200 !== $response->getStatusCode()) {
+ throw new \Exception('Received HTTP ' . $response->getStatusCode());
+ }
+
+ return (string)$response->getBody();
+ }
+
/**
* Gets a clean vCard from the CardDAV server
*
@@ -143,68 +159,26 @@ public function getVcard($vcard_id)
* Simplify CardDAV XML response
*
* @param string $response CardDAV XML response
- * @param boolean $include_vcards Include vCards or not
* @return string Simplified CardDAV XML response
*/
- private function simplify($response, $include_vcards = true)
+ private function simplify(string $response): array
{
$response = $this->cleanResponse($response);
$xml = new \SimpleXMLElement($response);
- $xmlRes = new \XMLWriter();
- $xmlRes->openMemory();
- $xmlRes->setIndent(4);
-
- $xmlRes->startDocument('1.0', 'utf-8');
- $xmlRes->startElement('response');
-
- if (!empty($xml->response)) {
- foreach ($xml->response as $response) {
- if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) &&
- !$response->propstat->prop->resourcetype->collection) {
- $id = basename($response->href);
- $id = str_replace($this->url_vcard_extension, null, $id);
-
- try {
- $vcardData = $this->getVcard($id);
-
- if (!empty($id)) {
- $xmlRes->startElement('element');
- $xmlRes->writeElement('id', $id);
- $xmlRes->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag));
- $xmlRes->writeElement('last_modified', $response->propstat->prop->getlastmodified);
-
- if ($include_vcards === true) {
- $xmlRes->writeElement('vcard', $vcardData);
- }
- $xmlRes->endElement();
- }
- } catch (\Exception $e) {
- error_log("Error fetching vCard: {$id}: {$e->getMessage()}\n");
- }
- } elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) {
- if (isset($response->propstat->prop->href)) {
- $href = $response->propstat->prop->href;
- } elseif (isset($response->href)) {
- $href = $response->href;
- } else {
- $href = null;
- }
-
- $url = str_replace($this->url_parts['path'], null, $this->url) . $href;
- $xmlRes->startElement('addressbook_element');
- $xmlRes->writeElement('display_name', $response->propstat->prop->displayname);
- $xmlRes->writeElement('url', $url);
- $xmlRes->writeElement('last_modified', $response->propstat->prop->getlastmodified);
- $xmlRes->endElement();
- }
+ $cards = [];
+
+ foreach ($xml->response as $response) {
+ if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) &&
+ !$response->propstat->prop->resourcetype->collection) {
+ $id = basename($response->href);
+ $id = str_replace($this->url_vcard_extension, null, $id);
+
+ $cards[] = $this->getVcard($id);
}
}
- $xmlRes->endElement();
- $xmlRes->endDocument();
-
- return $xmlRes->outputMemory();
+ return $cards;
}
/**
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index ce7eb16e..05410896 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -17,7 +17,7 @@ protected function configure()
{
$this->setName('convert')
->setDescription('Convert Vcard to FritzBox format')
- ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export parse result to json file')
+ ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export result to json file')
->addArgument('filename', InputArgument::REQUIRED, 'filename');
$this->addConfig();
@@ -26,17 +26,23 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadConfig($input);
-
+
$filename = $input->getArgument('filename');
- $xml = simplexml_load_file($filename);
+ $cards = json_decode(file_get_contents($filename));
$conversions = $this->config['conversions'];
$excludes = $this->config['excludes'];
$phonebook = $this->config['phonebook'];
// parse
- $cards = parse($xml, $conversions);
- $filtered = filter($cards, $excludes);
+ $vcards = parse($cards, $conversions);
+ $filtered = filter($vcards, $excludes);
+
+ $server = $this->config['server'];
+ $backend = backendProvider($server);
+ foreach ($vcards as $vcard) {
+ downloadImages($backend, $vcard);
+ }
if ($json = $input->getOption('json')) {
file_put_contents($json, json_encode($filtered, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 9b6e4278..4a5f4dcb 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -17,7 +17,8 @@ class DownloadCommand extends Command
protected function configure()
{
$this->setName('download')
- ->setDescription('Load from CardDAV server');
+ ->setDescription('Load from CardDAV server')
+ ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export result to json file');
$this->addConfig();
}
@@ -30,15 +31,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
$progress->start();
$server = $this->config['server'];
- $xmlStr = download($server['url'], $server['user'], $server['password'], function () use ($progress) {
+ $cards = download(backendProvider($server), function () use ($progress) {
$progress->advance();
});
$progress->finish();
- $count = countCards($xmlStr);
- error_log(sprintf("\nDownloaded %d vcards", $count));
+ error_log(sprintf("\nDownloaded %d vcards", count($cards)));
- echo $xmlStr;
+ $jsonStr = json_encode($cards, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE);
+
+ if ($json = $input->getOption('json')) {
+ file_put_contents($json, $jsonStr);
+ }
+
+ echo $jsonStr;
}
}
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 5e7abbd8..c745f29e 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -33,14 +33,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$progress->start();
$server = $this->config['server'];
- $xmlStr = download($server['url'], $server['user'], $server['password'], function () use ($progress) {
+ $cards = download($server['url'], $server['user'], $server['password'], function () use ($progress) {
$progress->advance();
});
$progress->finish();
- $count = countCards($xmlStr);
- error_log(sprintf("\nDownloaded %d vcards", $count));
+ error_log(sprintf("\nDownloaded %d vcards", count($cards)));
// parse and convert
error_log("Converting");
@@ -49,9 +48,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$conversions = $this->config['conversions'];
$excludes = $this->config['excludes'];
- $xml = simplexml_load_string($xmlStr);
- $cards = parse($xml, $conversions);
- $filtered = filter($cards, $excludes);
+ $vcards = parse($cards, $conversions);
+ $filtered = filter($vcards, $excludes);
error_log(sprintf("Converted %d vcards", count($filtered)));
// fritzbox format
diff --git a/src/functions.php b/src/functions.php
index 6ffb5c02..af155e02 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -8,64 +8,72 @@
use Andig\FritzBox\Api;
use \SimpleXMLElement;
-function download($url, $user, $password, callable $callback=null): string
+function backendProvider(array $config): Backend
+{
+ $server = $config['server'] ?? $config;
+
+ $backend = new Backend();
+ $backend->setUrl($server['url']);
+ $backend->setAuth($server['user'], $server['password']);
+
+ return $backend;
+}
+
+function download(Backend $backend, callable $callback=null): array
{
$backend = new Backend($url);
$backend->setAuth($user, $password);
$backend->setProgress($callback);
- return $backend->get();
+ return $backend->getVcards();
}
-function countCards($xml): int
+function downloadImages(Backend $backend, $card): object
{
- $count = 0;
- $xml = simplexml_load_string($xml);
-
- foreach ($xml->element as $element) {
- foreach ($element->vcard as $vcard) {
- $count++;
- }
+ if (isset($card->photo)) {
+ $uri = $card->photo;
+ $image = $backend->fetchImage($uri);
+ $card->photo_data = $image;
+ // print_r($card);
+ die;
}
- return $count;
+
+ return $card;
}
-function parse(SimpleXMLElement $xml, array $conversions): array
+function parse(array $cards, array $conversions): array
{
- $cards = [];
+ $vcards = [];
$groups = [];
// parse all vcards
- foreach ($xml->element as $element) {
- foreach ($element->vcard as $vcard) {
- $parser = new Parser($vcard);
- $card = $parser->getCardAtIndex(0);
-
- // separate iCloud groups
- if (isset($card->xabsmember)) {
- $groups[$card->fullname] = $card->xabsmember;
- continue;
- }
+ foreach ($cards as $card) {
+ $parser = new Parser($card);
+ $vcard = $parser->getCardAtIndex(0);
- $cards[] = $card;
- // print_r($card);
+ // separate iCloud groups
+ if (isset($vcard->xabsmember)) {
+ $groups[$vcard->fullname] = $vcard->xabsmember;
+ continue;
}
+
+ $vcards[] = $vcard;
}
// assign group memberships
- foreach ($cards as $card) {
+ foreach ($vcards as $vcard) {
foreach ($groups as $group => $members) {
- if (in_array($card->uid, $members)) {
- if (!isset($card->group)) {
- $card->group = array();
+ if (in_array($vcard->uid, $members)) {
+ if (!isset($vcard->group)) {
+ $vcard->group = array();
}
- $card->group = $group;
+ $vcard->group = $group;
break;
}
}
}
- return $cards;
+ return $vcards;
}
function filter(array $cards, array $filters): array
From 0023e5c4c2d33e905d43eb556b7e3db9db8fdcd0 Mon Sep 17 00:00:00 2001
From: andig
Date: Wed, 29 Nov 2017 22:54:49 +0100
Subject: [PATCH 033/165] Major refactor
---
src/ConfigTrait.php | 2 +-
src/ConvertCommand.php | 21 ++++++----------
src/DownloadCommand.php | 56 ++++++++++++++++++++++++++++++++---------
src/FritzBox/Api.php | 44 +++++++++++++++++++-------------
src/RunCommand.php | 38 +++++++++++++++++++---------
src/functions.php | 37 +++++++++++++++++++--------
6 files changed, 132 insertions(+), 66 deletions(-)
diff --git a/src/ConfigTrait.php b/src/ConfigTrait.php
index d10d93a9..681cd159 100644
--- a/src/ConfigTrait.php
+++ b/src/ConfigTrait.php
@@ -19,7 +19,7 @@ protected function addConfig()
protected function loadConfig(InputInterface $input)
{
$configFile = $input->getOption('config');
-
+
if (!file_exists($configFile)) {
throw new \Exception('Config file ' . $configFile . ' does not exist');
}
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index 05410896..f636ee3e 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -13,11 +13,13 @@ class ConvertCommand extends Command
{
use ConfigTrait;
+ const JSON_OPTIONS = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE;
+
protected function configure()
{
$this->setName('convert')
->setDescription('Convert Vcard to FritzBox format')
- ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export result to json file')
+ ->addOption('raw', 'r', InputOption::VALUE_REQUIRED, 'export raw conversion result to json file')
->addArgument('filename', InputArgument::REQUIRED, 'filename');
$this->addConfig();
@@ -34,21 +36,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$excludes = $this->config['excludes'];
$phonebook = $this->config['phonebook'];
- // parse
- $vcards = parse($cards, $conversions);
- $filtered = filter($vcards, $excludes);
-
- $server = $this->config['server'];
- $backend = backendProvider($server);
- foreach ($vcards as $vcard) {
- downloadImages($backend, $vcard);
- }
+ // filter
+ $filtered = filter($cards, $excludes);
- if ($json = $input->getOption('json')) {
- file_put_contents($json, json_encode($filtered, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE));
+ if ($json = $input->getOption('raw')) {
+ file_put_contents($json, json_encode($filtered, self::JSON_OPTIONS));
}
- error_log(sprintf("Converted %d vcards", count($filtered)));
+ error_log(sprintf("Converted %d cards", count($filtered)));
// convert
$xml = export($phonebook['name'], $filtered, $conversions);
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 4a5f4dcb..8d1053bf 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -14,11 +14,15 @@ class DownloadCommand extends Command
{
use ConfigTrait;
+ const JSON_OPTIONS = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE;
+
protected function configure()
{
$this->setName('download')
->setDescription('Load from CardDAV server')
- ->addOption('json', 'j', InputOption::VALUE_REQUIRED, 'export result to json file');
+ ->addArgument('filename', InputArgument::OPTIONAL, 'raw vcards json file')
+ ->addOption('image', 'i', InputOption::VALUE_NONE, 'download images')
+ ->addOption('raw', 'r', InputOption::VALUE_REQUIRED, 'export raw vcards to json file');
$this->addConfig();
}
@@ -27,24 +31,52 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadConfig($input);
+ $server = $this->config['server'];
+ $backend = backendProvider($server);
$progress = new ProgressBar($output);
- $progress->start();
- $server = $this->config['server'];
- $cards = download(backendProvider($server), function () use ($progress) {
- $progress->advance();
- });
+ if ($inputFile = $input->getArgument('filename')) {
+ // read from file
+ $vcards = json_decode(file_get_contents($inputFile));
+ }
+ else {
+ // download
+ error_log("Downloading vcards");
+
+ $progress->start();
+ $vcards = download($backend, function () use ($progress) {
+ $progress->advance();
+ });
+ $progress->finish();
+
+ error_log(sprintf("\nDownloaded %d vcard(s)", count($vcards)));
+
+ if ($file = $input->getOption('json')) {
+ $json = json_encode($vcards, self::JSON_OPTIONS);
+ file_put_contents($file, $json);
+ }
+ }
+
+ // parsing
+ error_log("Parsing vcards");
- $progress->finish();
+ $cards = parse($vcards);
+ $json = json_encode($cards, self::JSON_OPTIONS);
- error_log(sprintf("\nDownloaded %d vcards", count($cards)));
+ // images
+ if ($input->getOption('image')) {
+ error_log("Downloading images");
- $jsonStr = json_encode($cards, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE);
+ $progress->start();
+ $cards = downloadImages($backend, $cards, function() use ($progress) {
+ $progress->advance();
+ });
+ $progress->finish();
- if ($json = $input->getOption('json')) {
- file_put_contents($json, $jsonStr);
+ error_log(sprintf("\nDownloaded %d image(s)", countImages($cards)));
}
- echo $jsonStr;
+ $json = json_encode($cards, self::JSON_OPTIONS);
+ echo $json;
}
}
diff --git a/src/FritzBox/Api.php b/src/FritzBox/Api.php
index 7b4b7f3e..0cbb6b59 100644
--- a/src/FritzBox/Api.php
+++ b/src/FritzBox/Api.php
@@ -2,6 +2,10 @@
namespace Andig\FritzBox;
+use GuzzleHttp\Client;
+use GuzzleHttp\Psr7\Request;
+use Ringcentral\Psr7;
+
/**
* Extended from https://github.com/jens-maus/carddav2fb
* Public Domain
@@ -11,9 +15,9 @@ class Api
private $username;
private $password;
private $url;
-
+
protected $sid = '0000000000000000';
-
+
/**
* the constructor, initializes the object and calls the login method
*
@@ -31,7 +35,7 @@ public function __construct($url = 'https://fritz.box', $user_name = false, $pas
$this->sid = $this->initSID();
}
-
+
/**
* do a POST request on the box
* the main cURL wrapper handles the command
@@ -64,7 +68,7 @@ public function doPostForm($formfields = array())
public function doPostFile($formfields = array(), $filefileds = array())
{
$ch = curl_init();
-
+
// add the sid, if it is already set
if ($this->sid != '0000000000000000') {
// 'sid' MUST be the first POST variable!!! (otherwise it will not work!!)
@@ -74,11 +78,11 @@ public function doPostFile($formfields = array(), $filefileds = array())
}
curl_setopt($ch, CURLOPT_URL, $this->url . '/cgi-bin/firmwarecfg');
curl_setopt($ch, CURLOPT_POST, 1);
-
+
// enable for debugging:
//curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
+
// if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine)
if (empty($filefileds)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query
@@ -92,10 +96,10 @@ public function doPostFile($formfields = array(), $filefileds = array())
array(
'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) )
);
-
+
curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']);
}
-
+
$output = curl_exec($ch);
// curl error
@@ -117,7 +121,7 @@ private function _create_custom_file_post_header($postFields, $fileFields)
{
// form field separator
$delimiter = '-------------' . uniqid();
-
+
/*
// file upload fields: name => array(type=>'mime/type',content=>'raw data')
$fileFields = array(
@@ -132,7 +136,7 @@ private function _create_custom_file_post_header($postFields, $fileFields)
'otherformfield' => 'content of otherformfield is this text',
);
*/
-
+
$data = '';
// populate normal fields first (simpler)
@@ -149,7 +153,7 @@ private function _create_custom_file_post_header($postFields, $fileFields)
// "filename" attribute is not essential; server-side scripts may use it
$data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' .
' filename="' . $file['filename'] . '"' . "\r\n";
-
+
//$data .= 'Content-Transfer-Encoding: binary'."\r\n";
// this is, again, informative only; good practice to include though
$data .= 'Content-Type: ' . $file['type'] . "\r\n";
@@ -160,10 +164,10 @@ private function _create_custom_file_post_header($postFields, $fileFields)
}
// last delimiter
$data .= "--" . $delimiter . "--\r\n";
-
+
return array('delimiter' => $delimiter, 'data' => $data);
}
-
+
/**
* do a GET request on the box
* the main cURL wrapper handles the command
@@ -177,7 +181,7 @@ public function doGetRequest($params = array())
if ($this->sid != '0000000000000000') {
$params['sid'] = $this->sid;
}
-
+
if (strpos($params['getpage'], '.lua') > 0) {
$getpage = $params['getpage'] . '?';
unset($params['getpage']);
@@ -186,11 +190,17 @@ public function doGetRequest($params = array())
}
$url = $this->url . $getpage . http_build_query($params);
- $output = file_get_contents($url);
- return $output;
+ $this->client = $this->client ?? new Client();
+ $response = $this->client->send(new Request('GET', $url));
+
+ if (200 !== $response->getStatusCode()) {
+ throw new \Exception('Received HTTP ' . $response->getStatusCode());
+ }
+
+ return (string)$response->getBody();
}
-
+
/**
* the login method, handles the secured login-process
* newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks
diff --git a/src/RunCommand.php b/src/RunCommand.php
index c745f29e..8e8cf392 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -17,7 +17,8 @@ class RunCommand extends Command
protected function configure()
{
$this->setName('run')
- ->setDescription('Download, convert and upload - all in one');
+ ->setDescription('Download, convert and upload - all in one')
+ ->addOption('image', 'i', InputOption::VALUE_NONE, 'download images');
$this->addConfig();
}
@@ -25,36 +26,49 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadConfig($input);
+ $progress = new ProgressBar($output);
// download
error_log("Downloading");
- $progress = new ProgressBar($output);
- $progress->start();
-
$server = $this->config['server'];
- $cards = download($server['url'], $server['user'], $server['password'], function () use ($progress) {
+ $backend = backendProvider($server);
+
+ $progress->start();
+ $vcards = download($backend, function () use ($progress) {
$progress->advance();
});
-
$progress->finish();
- error_log(sprintf("\nDownloaded %d vcards", count($cards)));
+ error_log(sprintf("\nDownloaded %d vcard(s)", count($vcards)));
// parse and convert
- error_log("Converting");
+ error_log("Parsing vcards");
+ $cards = parse($vcards);
+
+ // images
+ if ($input->getOption('image')) {
+ error_log("Downloading images");
+
+ $progress->start();
+ $cards = downloadImages($backend, $cards, function() use ($progress) {
+ $progress->advance();
+ });
+ $progress->finish();
+
+ error_log(sprintf("\nDownloaded %d image(s)", countImages($cards)));
+ }
+ // conversion
$phonebook = $this->config['phonebook'];
$conversions = $this->config['conversions'];
$excludes = $this->config['excludes'];
- $vcards = parse($cards, $conversions);
- $filtered = filter($vcards, $excludes);
- error_log(sprintf("Converted %d vcards", count($filtered)));
+ $filtered = filter($cards, $excludes);
+ error_log(sprintf("Converted %d vcard(s)", count($filtered)));
// fritzbox format
$xml = export($phonebook['name'], $filtered, $conversions);
- // error_log(sprintf("Exported fritz phonebook", count($cards)));
// upload
error_log("Uploading");
diff --git a/src/functions.php b/src/functions.php
index af155e02..64c77fd3 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -21,26 +21,41 @@ function backendProvider(array $config): Backend
function download(Backend $backend, callable $callback=null): array
{
- $backend = new Backend($url);
- $backend->setAuth($user, $password);
$backend->setProgress($callback);
return $backend->getVcards();
}
-function downloadImages(Backend $backend, $card): object
+function downloadImages(Backend $backend, array $cards, callable $callback=null): array
{
- if (isset($card->photo)) {
- $uri = $card->photo;
- $image = $backend->fetchImage($uri);
- $card->photo_data = $image;
- // print_r($card);
- die;
+ foreach ($cards as $card) {
+ if (isset($card->photo)) {
+ $uri = $card->photo;
+ $image = $backend->fetchImage($uri);
+ $card->photo_data = utf8_encode($image);
+
+ if (is_callable($callback)) {
+ $callback();
+ }
+ }
+ }
+
+ return $cards;
+}
+
+function countImages(array $cards): int
+{
+ $images = 0;
+
+ foreach ($cards as $card) {
+ if (isset($card->photo_data)) {
+ $images++;
+ }
}
- return $card;
+ return $images;
}
-function parse(array $cards, array $conversions): array
+function parse(array $cards): array
{
$vcards = [];
$groups = [];
From 2174760238de47814b612e9256a90f68a1524b54 Mon Sep 17 00:00:00 2001
From: andig
Date: Thu, 30 Nov 2017 08:13:36 +0100
Subject: [PATCH 034/165] Remove object typehint
---
src/functions.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/functions.php b/src/functions.php
index 64c77fd3..53a5c721 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -106,7 +106,7 @@ function filter(array $cards, array $filters): array
return $result;
}
-function filtersMatch(object $card, array $filters): bool
+function filtersMatch($card, array $filters): bool
{
foreach ($filters as $attribute => $values) {
if (isset($card->$attribute)) {
From 7f61b36c39c13d0b3a7081d93f5b9a90910778b4 Mon Sep 17 00:00:00 2001
From: andig
Date: Tue, 5 Dec 2017 10:36:20 +0100
Subject: [PATCH 035/165] Add include filter
---
config.example.php | 30 ++++++++++++++++++------------
src/ConvertCommand.php | 9 ++++-----
src/RunCommand.php | 8 ++++----
src/functions.php | 39 ++++++++++++++++++++++++++++++++-------
4 files changed, 58 insertions(+), 28 deletions(-)
diff --git a/config.example.php b/config.example.php
index 5c0375e3..c5683c2f 100644
--- a/config.example.php
+++ b/config.example.php
@@ -6,7 +6,7 @@
'id' => 0,
'name' => 'Telefonbuch'
],
-
+
// or server
'server' => [
'url' => 'https://...',
@@ -21,14 +21,20 @@
'password' => '',
],
- 'excludes' => [
- 'category' => [
- 'ORGA', 'b'
- ],
- 'group' => [
- 'c', 'd'
- ],
- ],
+ 'filters' => [
+ 'include' => [
+ // if empty include all by default
+ ],
+
+ 'exclude' => [
+ 'category' => [
+ 'a', 'b'
+ ],
+ 'group' => [
+ 'c', 'd'
+ ],
+ ],
+ ],
'conversions' => [
'vip' => [
@@ -45,12 +51,12 @@
'{organization}'
],
'phoneTypes' => [
- 'WORK' => 'work',
- 'HOME' => 'home',
+ 'WORK' => 'work',
+ 'HOME' => 'home',
'CELL' => 'mobile'
],
'emailTypes' => [
- 'WORK' => 'work',
+ 'WORK' => 'work',
'HOME' => 'home'
],
'phoneReplaceCharacters' => [
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index f636ee3e..1a0d69fb 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -32,12 +32,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filename = $input->getArgument('filename');
$cards = json_decode(file_get_contents($filename));
- $conversions = $this->config['conversions'];
- $excludes = $this->config['excludes'];
- $phonebook = $this->config['phonebook'];
-
// filter
- $filtered = filter($cards, $excludes);
+ $filters = $this->config['filters'];
+ $filtered = filter($cards, $filters);
if ($json = $input->getOption('raw')) {
file_put_contents($json, json_encode($filtered, self::JSON_OPTIONS));
@@ -46,6 +43,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
error_log(sprintf("Converted %d cards", count($filtered)));
// convert
+ $phonebook = $this->config['phonebook'];
+ $conversions = $this->config['conversions'];
$xml = export($phonebook['name'], $filtered, $conversions);
echo $xml->asXML();
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 8e8cf392..aa3b5dfd 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -60,14 +60,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
// conversion
- $phonebook = $this->config['phonebook'];
- $conversions = $this->config['conversions'];
- $excludes = $this->config['excludes'];
+ $filters = $this->config['filters'];
+ $filtered = filter($cards, $filters);
- $filtered = filter($cards, $excludes);
error_log(sprintf("Converted %d vcard(s)", count($filtered)));
// fritzbox format
+ $phonebook = $this->config['phonebook'];
+ $conversions = $this->config['conversions'];
$xml = export($phonebook['name'], $filtered, $conversions);
// upload
diff --git a/src/functions.php b/src/functions.php
index 53a5c721..c12d42a1 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -91,19 +91,44 @@ function parse(array $cards): array
return $vcards;
}
+/**
+ * Filter included/excluded vcards
+ *
+ * @param array $cards
+ * @param array $filters
+ * @return array
+ */
function filter(array $cards, array $filters): array
{
- $result = [];
-
- foreach ($cards as $card) {
- if (filtersMatch($card, $filters)) {
- continue;
+ // include selected
+ $includeFilter = $filters['include'] ?? [];
+ if (count($includeFilter)) {
+ $step1 = [];
+
+ foreach ($cards as $card) {
+ if (filtersMatch($card, $includeFilter)) {
+ $step1[] = $card;
+ }
}
+ }
+ else {
+ // include all by default
+ $step1 = $cards;
+ }
+
+ $excludeFilter = $filters['exclude'] ?? [];
+ if (!count($excludeFilter)) {
+ return $step1;
+ }
- $result[] = $card;
+ $step2 = [];
+ foreach ($step1 as $card) {
+ if (!filtersMatch($card, $excludeFilter)) {
+ $step2[] = $card;
+ }
}
- return $result;
+ return $step2;
}
function filtersMatch($card, array $filters): bool
From 0323aa57b607c65fa0b03901af411156440f814d Mon Sep 17 00:00:00 2001
From: andig
Date: Wed, 6 Dec 2017 20:33:23 +0100
Subject: [PATCH 036/165] Add matching of array attributes
---
src/functions.php | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/src/functions.php b/src/functions.php
index c12d42a1..4b4cf39c 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -151,8 +151,18 @@ function filterMatches($attribute, $filterValues): bool
}
foreach ($filterValues as $filter) {
- if ($attribute === $filter) {
- return true;
+ if (is_array($attribute)) {
+ // check if any attribute matches
+ foreach ($attribute as $childAttribute) {
+ if ($childAttribute === $filter) {
+ return true;
+ }
+ }
+ } else {
+ // check if simple attribute matches
+ if ($attribute === $filter) {
+ return true;
+ }
}
}
From bc4f09c6178d2d2d6e10d60c889c00d08ad5fea5 Mon Sep 17 00:00:00 2001
From: andig
Date: Sun, 10 Dec 2017 19:46:55 +0100
Subject: [PATCH 037/165] Fix json option does not exist
---
src/DownloadCommand.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 8d1053bf..4a0cba46 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -51,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
error_log(sprintf("\nDownloaded %d vcard(s)", count($vcards)));
- if ($file = $input->getOption('json')) {
+ if ($file = $input->getOption('raw')) {
$json = json_encode($vcards, self::JSON_OPTIONS);
file_put_contents($file, $json);
}
From 5a07bd11b8def5a54f2fd38bf9d82b73feb7bdf8 Mon Sep 17 00:00:00 2001
From: andig
Date: Mon, 11 Dec 2017 21:40:22 +0100
Subject: [PATCH 038/165] Added comments
---
src/functions.php | 100 ++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 97 insertions(+), 3 deletions(-)
diff --git a/src/functions.php b/src/functions.php
index 4b4cf39c..53d633de 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -8,6 +8,12 @@
use Andig\FritzBox\Api;
use \SimpleXMLElement;
+/**
+ * Initialize backend from configuration
+ *
+ * @param array $config
+ * @return Backend
+ */
function backendProvider(array $config): Backend
{
$server = $config['server'] ?? $config;
@@ -19,12 +25,25 @@ function backendProvider(array $config): Backend
return $backend;
}
+/**
+ * Download vcards from CardDAV server
+ *
+ * @param Backend $backend
+ * @param callable $callback
+ * @return array
+ */
function download(Backend $backend, callable $callback=null): array
{
$backend->setProgress($callback);
return $backend->getVcards();
}
+/**
+ * Download images from CardDAV server
+ *
+ * @param array $cards
+ * @return int
+ */
function downloadImages(Backend $backend, array $cards, callable $callback=null): array
{
foreach ($cards as $card) {
@@ -42,6 +61,12 @@ function downloadImages(Backend $backend, array $cards, callable $callback=null)
return $cards;
}
+/**
+ * Count downloaded images contained in list of vcards
+ *
+ * @param array $cards
+ * @return int
+ */
function countImages(array $cards): int
{
$images = 0;
@@ -55,6 +80,12 @@ function countImages(array $cards): int
return $images;
}
+/**
+ * Parse an array of raw vcards into POPOs
+ *
+ * @param array $cards
+ * @return array
+ */
function parse(array $cards): array
{
$vcards = [];
@@ -102,7 +133,8 @@ function filter(array $cards, array $filters): array
{
// include selected
$includeFilter = $filters['include'] ?? [];
- if (count($includeFilter)) {
+
+ if (countFilters($includeFilter)) {
$step1 = [];
foreach ($cards as $card) {
@@ -112,6 +144,11 @@ function filter(array $cards, array $filters): array
}
}
else {
+ // filter defined but empty sub-rules?
+ if (count($includeFilter)) {
+ error_log('Include filter empty- including all cards');
+ }
+
// include all by default
$step1 = $cards;
}
@@ -131,6 +168,32 @@ function filter(array $cards, array $filters): array
return $step2;
}
+/**
+ * Count populated filter rules
+ *
+ * @param array $filters
+ * @return int
+ */
+function countFilters(array $filters): int
+{
+ $filterCount = 0;
+
+ foreach ($filters as $key => $value) {
+ if (is_array($value)) {
+ $filterCount += count($value);
+ }
+ }
+
+ return $filterCount;
+}
+
+/**
+ * Check a list of filters against a card
+ *
+ * @param [type] $card
+ * @param array $filters
+ * @return bool
+ */
function filtersMatch($card, array $filters): bool
{
foreach ($filters as $attribute => $values) {
@@ -144,6 +207,13 @@ function filtersMatch($card, array $filters): bool
return false;
}
+/**
+ * Check a filter against a single attribute
+ *
+ * @param [type] $attribute
+ * @param [type] $filterValues
+ * @return bool
+ */
function filterMatches($attribute, $filterValues): bool
{
if (!is_array($filterValues)) {
@@ -169,6 +239,14 @@ function filterMatches($attribute, $filterValues): bool
return false;
}
+/**
+ * Export cards to fritzbox xml
+ *
+ * @param string $name
+ * @param array $cards
+ * @param array $conversions
+ * @return SimpleXMLElement
+ */
function export(string $name, array $cards, array $conversions): SimpleXMLElement
{
$xml = new SimpleXMLElement(
@@ -194,7 +272,14 @@ function export(string $name, array $cards, array $conversions): SimpleXMLElemen
return $xml;
}
-// https://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
+/**
+ * Attach xml element to parent
+ * https://stackoverflow.com/questions/4778865/php-simplexml-addchild-with-another-simplexmlelement
+ *
+ * @param SimpleXMLElement $to
+ * @param SimpleXMLElement $from
+ * @return void
+ */
function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
{
$toDom = dom_import_simplexml($to);
@@ -202,7 +287,16 @@ function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
-
+/**
+ * Upload cards to fritzbox
+ *
+ * @param string $xml
+ * @param string $url
+ * @param string $user
+ * @param string $password
+ * @param int $phonebook
+ * @return void
+ */
function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
{
$fritz = new Api($url, $user, $password, 1);
From c0a735df2d8c2ed98d2cf8e29b6ccbcc18fceaa5 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Thu, 14 Dec 2017 12:01:53 +0100
Subject: [PATCH 039/165] Diverse Korrekturen: Gliederung/Rechtschreibung
---
README.md | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/README.md b/README.md
index 2ddfd060..000d15cd 100644
--- a/README.md
+++ b/README.md
@@ -3,18 +3,23 @@
This is an entirely simplified version of https://github.com/jens-maus/carddav2fb. The Vcard parser has been replaced by an extended version of https://github.com/jeroendesloovere/vcard.
## Requirements
-PHP 7.0 (apt-get install php7.0 php7.0-curl php7.0-mbstring php7.0-xml)
-Composer (follow the installation guide at https://getcomposer.org/download/)
+* PHP 7.0 (apt-get install php7.0 php7.0-curl php7.0-mbstring php7.0-xml)
+* Composer (follow the installation guide at https://getcomposer.org/download/)
## Installation
-Install requirements
-cammand: git clone https://github.com/andig/carddav2fb.git
-change into the new carddav2fb directory
-command: composer install
-edit config.example.php and save as config.php
+* Install requirements
+* command: mv composer.phar /usr/local/bin/composer
+* command: git clone https://github.com/andig/carddav2fb.git
+* change into the new carddav2fb directory
+* command: composer install
+* edit config.example.php and save as config.php
## Usage
-command: php carddav2fb.php run
+command: php carddav2fb.php run (complete processing)
+
+Optional:
+command: php carddav2fb.php run -h (providing help for step by step processing and arguments)
+
## License
This script is released under Public Domain, some parts under MIT license. Make sure you understand which parts are which.
From d48c728a10f34bd7b68311823ccc4c01f3ed410b Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Thu, 14 Dec 2017 12:22:47 +0100
Subject: [PATCH 040/165] Polishing
---
src/UploadCommand.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index 0c7b9779..d5e29ee9 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -16,7 +16,7 @@ class UploadCommand extends Command
protected function configure()
{
$this->setName('upload')
- ->setDescription('Upload to FritzBox')
+ ->setDescription('Upload to Fritz!Box')
->addArgument('filename', InputArgument::REQUIRED, 'filename');
$this->addConfig();
@@ -34,6 +34,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
- error_log("Uploaded fritz phonebook");
+ error_log("Uploaded Fritz!Box phonebook");
}
}
From 512f54fa8986de4851e3fb92f45da4093d0d858d Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Thu, 14 Dec 2017 12:30:56 +0100
Subject: [PATCH 041/165] Serving suggestion
---
config.example.php | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/config.example.php b/config.example.php
index c5683c2f..fb84a149 100644
--- a/config.example.php
+++ b/config.example.php
@@ -60,10 +60,11 @@
'HOME' => 'home'
],
'phoneReplaceCharacters' => [
- '(' => '',
- ')' => '',
- '/' => '',
- '-' => ' '
+ '+49' => '', //Router steht default in DE; '0049' könnte auch Teil einer Rufnummer sein
+ '(' => '',
+ ')' => '',
+ '/' => '',
+ '-' => ' '
]
]
];
From cb463caf8e80a6ba922bbdb326c0ec02609c9642 Mon Sep 17 00:00:00 2001
From: andig
Date: Thu, 14 Dec 2017 13:05:25 +0100
Subject: [PATCH 042/165] Update README.md
---
README.md | 33 ++++++++++++++++++++++-----------
1 file changed, 22 insertions(+), 11 deletions(-)
diff --git a/README.md b/README.md
index 000d15cd..78cf24ef 100644
--- a/README.md
+++ b/README.md
@@ -3,22 +3,33 @@
This is an entirely simplified version of https://github.com/jens-maus/carddav2fb. The Vcard parser has been replaced by an extended version of https://github.com/jeroendesloovere/vcard.
## Requirements
-* PHP 7.0 (apt-get install php7.0 php7.0-curl php7.0-mbstring php7.0-xml)
-* Composer (follow the installation guide at https://getcomposer.org/download/)
+
+ * PHP 7.0 (`apt-get install php7.0 php7.0-curl php7.0-mbstring php7.0-xml`)
+ * Composer (follow the installation guide at https://getcomposer.org/download/)
## Installation
-* Install requirements
-* command: mv composer.phar /usr/local/bin/composer
-* command: git clone https://github.com/andig/carddav2fb.git
-* change into the new carddav2fb directory
-* command: composer install
-* edit config.example.php and save as config.php
+
+Install requirements
+
+ git clone https://github.com/andig/carddav2fb.git
+ cd carddav2fb
+ composer install
+
+edit `config.example.php` and save as `config.php`
## Usage
-command: php carddav2fb.php run (complete processing)
-Optional:
-command: php carddav2fb.php run -h (providing help for step by step processing and arguments)
+List all commands:
+
+ php carddav2fb.php run
+
+Complete processing:
+
+ php carddav2fb.php run
+
+Get help for a command:
+
+ php carddav2fb.php run -h
## License
From 8f9daa29478ba6c982354d90d7bff6baad515032 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Tue, 16 Jan 2018 08:33:58 +0100
Subject: [PATCH 043/165] Support nick names (#35)
---
config.example.php | 9 ++++++---
src/Vcard/Parser.php | 3 +++
2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/config.example.php b/config.example.php
index fb84a149..eb9e956b 100644
--- a/config.example.php
+++ b/config.example.php
@@ -45,10 +45,13 @@
'PERS'
],
],
- 'realName' => [
+ 'realName' => [
+ '{lastname}, {prefix} {nickname}',
+ '{lastname}, {prefix} {firstname}',
+ '{lastname}, {nickname}',
'{lastname}, {firstname}',
- '{fullname}',
- '{organization}'
+ '{organization}',
+ '{fullname}'
],
'phoneTypes' => [
'WORK' => 'work',
diff --git a/src/Vcard/Parser.php b/src/Vcard/Parser.php
index f8941488..836d4de0 100644
--- a/src/Vcard/Parser.php
+++ b/src/Vcard/Parser.php
@@ -174,6 +174,9 @@ protected function parse()
$cardData->{$key} = $val;
}
break;
+ case 'NICKNAME':
+ $cardData->nickname = $value;
+ break;
case 'BDAY':
$cardData->birthday = $this->parseBirthday($value);
break;
From 330b178f342fe4453b0b545b66dcc22f4ce2f613 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Thu, 25 Jan 2018 18:59:02 +0100
Subject: [PATCH 044/165] Allow reading multiple sources (#34)
---
config.example.php | 29 ++++++++++++++++++-----------
src/DownloadCommand.php | 28 +++++++++++++++-------------
src/RunCommand.php | 27 ++++++++++++++-------------
3 files changed, 47 insertions(+), 37 deletions(-)
diff --git a/config.example.php b/config.example.php
index eb9e956b..194315dc 100644
--- a/config.example.php
+++ b/config.example.php
@@ -8,18 +8,25 @@
],
// or server
- 'server' => [
- 'url' => 'https://...',
- 'user' => '',
- 'password' => '',
- ],
+ 'server' => [
+ [
+ 'url' => 'https://...',
+ 'user' => '',
+ 'password' => '',
+ ], /* add as many as you need
+ [
+ 'url' => 'https://...',
+ 'user' => '',
+ 'password' => '',
+ ], */
+ ],
- // or fritzbox
- 'fritzbox' => [
- 'url' => 'http://fritz.box',
- 'user' => '',
- 'password' => '',
- ],
+ // or fritzbox
+ 'fritzbox' => [
+ 'url' => 'http://fritz.box',
+ 'user' => '',
+ 'password' => '',
+ ],
'filters' => [
'include' => [
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 4a0cba46..d9981b9d 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -31,25 +31,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadConfig($input);
- $server = $this->config['server'];
- $backend = backendProvider($server);
- $progress = new ProgressBar($output);
-
+ $vcards = array();
+ $xcards = array();
+
if ($inputFile = $input->getArgument('filename')) {
// read from file
$vcards = json_decode(file_get_contents($inputFile));
}
else {
// download
- error_log("Downloading vcards");
-
- $progress->start();
- $vcards = download($backend, function () use ($progress) {
- $progress->advance();
- });
- $progress->finish();
-
- error_log(sprintf("\nDownloaded %d vcard(s)", count($vcards)));
+ foreach($this->config['server'] as $server) {
+ $progress = new ProgressBar($output);
+ error_log("Downloading vCard(s) from account ".$server['user']);
+ $backend = backendProvider($server);
+ $progress->start();
+ $xcards = download ($backend, function () use ($progress) {
+ $progress->advance();
+ });
+ $progress->finish();
+ $vcards = array_merge($vcards, $xcards);
+ error_log(sprintf("\nDownloaded %d vCard(s)", count($vcards)));
+ }
if ($file = $input->getOption('raw')) {
$json = json_encode($vcards, self::JSON_OPTIONS);
diff --git a/src/RunCommand.php b/src/RunCommand.php
index aa3b5dfd..716acd6f 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -26,21 +26,22 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadConfig($input);
- $progress = new ProgressBar($output);
- // download
- error_log("Downloading");
+ $vcards = array();
+ $xcards = array();
- $server = $this->config['server'];
- $backend = backendProvider($server);
-
- $progress->start();
- $vcards = download($backend, function () use ($progress) {
- $progress->advance();
- });
- $progress->finish();
-
- error_log(sprintf("\nDownloaded %d vcard(s)", count($vcards)));
+ foreach($this->config['server'] as $server) {
+ $progress = new ProgressBar($output);
+ error_log("Downloading vCard(s) from account ".$server['user']);
+ $backend = backendProvider($server);
+ $progress->start();
+ $xcards = download ($backend, function () use ($progress) {
+ $progress->advance();
+ });
+ $progress->finish();
+ $vcards = array_merge($vcards, $xcards);
+ error_log(sprintf("\nDownloaded %d vCard(s)", count($vcards)));
+ }
// parse and convert
error_log("Parsing vcards");
From 795c3bec2ae3e8f1403850d3f462816a31c7db3a Mon Sep 17 00:00:00 2001
From: Stefan300381 <33487900+Stefan300381@users.noreply.github.com>
Date: Thu, 12 Apr 2018 12:25:10 +0200
Subject: [PATCH 045/165] [carddav2fb] update of README.MD (#44)
corrected "list all commands" command and use parameter "list" instead of "run"
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 78cf24ef..92709ef6 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@ edit `config.example.php` and save as `config.php`
List all commands:
- php carddav2fb.php run
+ php carddav2fb.php list
Complete processing:
From 756f49e5b49b9b882db8df34afc671ed00c7c891 Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 14 Apr 2018 14:40:13 +0200
Subject: [PATCH 046/165] Validate reading intermediate files (#46)
---
src/ConvertCommand.php | 4 ++++
src/DownloadCommand.php | 20 +++++++++++++-------
2 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index 1a0d69fb..41e13196 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -32,6 +32,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filename = $input->getArgument('filename');
$cards = json_decode(file_get_contents($filename));
+ if (!is_array($cards)) {
+ throw new \Exception(sprintf('Could not read parsed vcards from %s', $filename));
+ }
+
// filter
$filters = $this->config['filters'];
$filtered = filter($cards, $filters);
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index d9981b9d..433baa4f 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -32,24 +32,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->loadConfig($input);
$vcards = array();
- $xcards = array();
-
- if ($inputFile = $input->getArgument('filename')) {
+
+ $progress = new ProgressBar($output);
+
+ if ($filename = $input->getArgument('filename')) {
// read from file
- $vcards = json_decode(file_get_contents($inputFile));
+ $vcards = json_decode(file_get_contents($filename));
+
+ if (!is_array($vcards)) {
+ throw new \Exception(sprintf('Could not read unparsed vcards from %s', $filename));
+ }
}
else {
// download
foreach($this->config['server'] as $server) {
- $progress = new ProgressBar($output);
error_log("Downloading vCard(s) from account ".$server['user']);
$backend = backendProvider($server);
+
$progress->start();
- $xcards = download ($backend, function () use ($progress) {
+ $downloaded = download($backend, function () use ($progress) {
$progress->advance();
});
$progress->finish();
- $vcards = array_merge($vcards, $xcards);
+
+ $vcards = array_merge($vcards, $downloaded);
error_log(sprintf("\nDownloaded %d vCard(s)", count($vcards)));
}
From c8650a7b39fb448024b08b7d1c51389cc3844282 Mon Sep 17 00:00:00 2001
From: andig
Date: Wed, 12 Sep 2018 14:27:55 +0200
Subject: [PATCH 047/165] Add digest authentication (#56)
---
config.example.php | 7 +++--
src/CardDav/Backend.php | 58 +++++++++++++++++++++++++----------------
src/functions.php | 3 ++-
3 files changed, 43 insertions(+), 25 deletions(-)
diff --git a/config.example.php b/config.example.php
index 194315dc..bd1bc26d 100644
--- a/config.example.php
+++ b/config.example.php
@@ -13,12 +13,15 @@
'url' => 'https://...',
'user' => '',
'password' => '',
- ], /* add as many as you need
+ // 'authentication' => 'digest' // uncomment for digest auth
+ ],
+/* add as many as you need
[
'url' => 'https://...',
'user' => '',
'password' => '',
- ], */
+ ],
+*/
],
// or fritzbox
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index 53396517..c3ffd195 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -33,19 +33,26 @@ class Backend
private $url_vcard_extension = '.vcf';
/**
- * Authentication: username
- *
- * @var string
- */
+ * Authentication: username
+ *
+ * @var string
+ */
private $username;
/**
- * Authentication: password
- *
- * @var string
- */
+ * Authentication: password
+ *
+ * @var string
+ */
private $password;
+ /**
+ * Authentication: method
+ *
+ * @var string|null
+ */
+ private $authentication;
+
/**
* Progress callback
*/
@@ -88,10 +95,11 @@ public function setProgress($callback = null)
/**
* Set credentials
*/
- public function setAuth(string $username, string $password)
+ public function setAuth(string $username, string $password, string $method = null)
{
$this->username = $username;
$this->password = $password;
+ $this->authentication = $method;
}
/**
@@ -112,17 +120,29 @@ public function getVcards($include_vcards = true)
throw new \Exception('Received HTTP ' . $response->getStatusCode());
}
- public function fetchImage($uri)
+ private function getClient()
{
- $this->client = $this->client ?? new Client();
- $request = new Request('GET', $uri);
+ if (!$this->client) {
+ $this->client = new Client($this->getClientOptions());
+ }
+
+ return $this->client;
+ }
+ private function getClientOptions($options = [])
+ {
if ($this->username) {
- $credentials = base64_encode($this->username.':'.$this->password);
- $request = $request->withHeader('Authorization', 'Basic '.$credentials);
+ $options['auth'] = [$this->username, $this->password, $this->authentication];
}
- $response = $this->client->send($request);
+ return $options;
+ }
+
+ public function fetchImage($uri)
+ {
+ $request = new Request('GET', $uri);
+
+ $response = $this->getClient()->send($request);
if (200 !== $response->getStatusCode()) {
throw new \Exception('Received HTTP ' . $response->getStatusCode());
@@ -209,7 +229,6 @@ private function cleanResponse($response)
*/
private function query($url, $method, $content = null, $content_type = null)
{
- $this->client = $this->client ?? new Client();
$request = new Request($method, $url, [
'Depth' => '1'
]);
@@ -222,12 +241,7 @@ private function query($url, $method, $content = null, $content_type = null)
$request = $request->withBody($content);
}
- if ($this->username) {
- $credentials = base64_encode($this->username.':'.$this->password);
- $request = $request->withHeader('Authorization', 'Basic '.$credentials);
- }
-
- $response = $this->client->send($request);
+ $response = $this->getClient()->send($request);
return $response;
}
}
diff --git a/src/functions.php b/src/functions.php
index 53d633de..811c54f7 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -17,10 +17,11 @@
function backendProvider(array $config): Backend
{
$server = $config['server'] ?? $config;
+ $authentication = $server['authentication'] ?? null;
$backend = new Backend();
$backend->setUrl($server['url']);
- $backend->setAuth($server['user'], $server['password']);
+ $backend->setAuth($server['user'], $server['password'], $authentication);
return $backend;
}
From 5ab0057e5f72f06c324226713c3b0dcd7ef3af04 Mon Sep 17 00:00:00 2001
From: Simon Uhrmann
Date: Mon, 17 Sep 2018 20:32:00 +0200
Subject: [PATCH 048/165] Fix undefined property (#58)
---
src/CardDav/Backend.php | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index c3ffd195..e0467ea0 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -58,6 +58,13 @@ class Backend
*/
private $callback;
+ /**
+ * Do not use this directly! Rather use {@see getClient()}
+ *
+ * @var Client
+ */
+ private $client;
+
/**
* Constructor
* Sets the CardDAV server url
From b510882ca554b81e58790ad591138e0452aec3af Mon Sep 17 00:00:00 2001
From: Bernd Fallert
Date: Sat, 8 Dec 2018 07:46:37 +0100
Subject: [PATCH 049/165] Update README.md (#62)
Space before headline to correct markdown
---
lib/fritzbox_api_php/README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/fritzbox_api_php/README.md b/lib/fritzbox_api_php/README.md
index cbecf8f2..d7a508eb 100644
--- a/lib/fritzbox_api_php/README.md
+++ b/lib/fritzbox_api_php/README.md
@@ -8,7 +8,7 @@ Provides an interface to your FritzBox! using the webinterface. Thus, no modific
Install on Debian/Ubuntu: sudo apt-get install php5-cli php5-curl
-###Sample Scripts
+### Sample Scripts
* Enable/Disable TAM
* Download Phone call list
From 894d17439b0bd4e0257dc193b4a68c9f982c27c4 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Tue, 11 Dec 2018 18:36:18 +0100
Subject: [PATCH 050/165] deletes semicolons that sometimes appear at the end
of "ORG:" names (#63)
---
src/Vcard/Parser.php | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/Vcard/Parser.php b/src/Vcard/Parser.php
index 836d4de0..c5e3d432 100644
--- a/src/Vcard/Parser.php
+++ b/src/Vcard/Parser.php
@@ -208,6 +208,8 @@ protected function parse()
$cardData->version = $value;
break;
case 'ORG':
+ if (substr($value, -1) == ';') // deletes semicolons that sometimes appear at the end
+ $value = substr($value, 0, -1);
$cardData->organization = $value;
break;
case 'URL':
From f9ce4daf123d843460f7a8b7756112d72e3dff8d Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Wed, 19 Dec 2018 17:44:22 +0100
Subject: [PATCH 051/165] Add image upload (#59)
---
config.example.php | 6 +-
src/CardDav/Backend.php | 109 +++++++++++++++++++++++++++++++++++--
src/ConvertCommand.php | 8 +--
src/DownloadCommand.php | 35 +++---------
src/FritzBox/Converter.php | 70 +++++++++++++-----------
src/RunCommand.php | 63 +++++++++++----------
src/UploadCommand.php | 11 ++--
src/Vcard/Parser.php | 16 +++---
src/functions.php | 105 ++++++++++++++++-------------------
9 files changed, 246 insertions(+), 177 deletions(-)
diff --git a/config.example.php b/config.example.php
index bd1bc26d..c2988c95 100644
--- a/config.example.php
+++ b/config.example.php
@@ -3,8 +3,9 @@
$config = [
// phonebook
'phonebook' => [
- 'id' => 0,
- 'name' => 'Telefonbuch'
+ 'id' => 0, // only '0' can store images
+ 'name' => 'Telefonbuch',
+ 'imagepath' => 'file:///var/InternerSpeicher/[YOURUSBSTICK]/FRITZ/fonpix/', // mandatory if you use the -i option
],
// or server
@@ -29,6 +30,7 @@
'url' => 'http://fritz.box',
'user' => '',
'password' => '',
+ 'fonpix' => '/[YOURUSBSTICK]/FRITZ/fonpix', // the storage on your usb stick for uploading images
],
'filters' => [
diff --git a/src/CardDav/Backend.php b/src/CardDav/Backend.php
index e0467ea0..8d9bea93 100644
--- a/src/CardDav/Backend.php
+++ b/src/CardDav/Backend.php
@@ -5,6 +5,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Ringcentral\Psr7;
+use Andig\Vcard\Parser;
/**
* @author Christian Putzke
@@ -64,6 +65,11 @@ class Backend
* @var Client
*/
private $client;
+
+ /**
+ * Set substitutions of links to embedded data
+ */
+ private $substitutes = [];
/**
* Constructor
@@ -76,6 +82,18 @@ public function __construct(string $url=null) {
$this->setUrl($url);
}
}
+
+ /**
+ * setting the properties/elements which should be substituted
+ *
+ * @param array $elements the properties whose value should be replaced ('LOGO', 'KEY', 'PHOTO' or 'SOUND')
+ */
+ public function setSubstitutes($elements)
+ {
+ foreach ($elements as $element) {
+ $this->substitutes[] = strtolower($element);
+ }
+ }
public function setUrl(string $url)
{
@@ -145,17 +163,88 @@ private function getClientOptions($options = [])
return $options;
}
- public function fetchImage($uri)
+ /**
+ * If elements are declared as to be substituted,
+ * the data from possibly linked sources are embedded directly into the vCard
+ *
+ * @param string $vcard single parsed vCard
+ * @param string $substituteID the property whose value is to be replaced ('logo', 'key', 'photo' or 'sound')
+ * @param string $server the current CardDAV server adress
+ * @return string single vCard with embedded value
+ */
+ private function embedBase64($vcard, $substituteID, $server)
{
- $request = new Request('GET', $uri);
+ if (!array_key_exists($substituteID, $vcard)) {
+ return $vcard;
+ }
+ if (!preg_match("/http/", $vcard->{$substituteID})) { // no external URL set -> must be already base64 or local
+ return $vcard;
+ }
+ // check if mime is linked onto the same server
+ $serv = explode('/', $server, 4); // get the beginning of the current server adress
+ $link = explode('/', $vcard->{$substituteID}, 4); // get the beginning of the linked adress
+ if (strcasecmp($serv[2], $link[2]) !== 0) { // if they aren´t equal authorisation will fail!
+ return $vcard;
+ }
+ $embedded = $this->getLinkedData($vcard->{$substituteID}); // get the data from the external URL
+ $types = '';
+ switch ($vcard->version) { // the different vCard versions must be considered
+ case '3.0':
+ $types = "TYPE=" . strtoupper($embedded['subtype']) . ";ENCODING=b";
+ break;
+ case '4.0':
+ $types = "data:" . $embedded['mimetype'] . ";base64";
+ break;
+ }
+ $rawField = "raw" . ucfirst($substituteID);
+ $dataField = $substituteID . "Data";
+ $vcard->$rawField = $embedded['data'];
+ $vcard->$dataField = $types;
+ return $vcard;
+ }
- $response = $this->getClient()->send($request);
+ /**
+ * delivers an array including the previously linked data and its mime type details
+ * a mime type is composed of a type, a subtype, and optional parameters (e.g. "; charset=UTF-8")
+ *
+ * @param string $uri URL of the external linked data
+ * @return array ['mimetype', e.g. "image/jpeg"
+ * 'type', e.g. "audio"
+ * 'subtype', e.g. "mpeg"
+ * 'parameters', whatever
+ * 'data'] the base64 encoded data
+ */
+ public function getLinkedData($uri)
+ {
+ $externalData = array();
+
+ $this->client = $this->client ?? new Client();
+ $request = new Request('GET', $uri);
+ if ($this->username) {
+ $credentials = base64_encode($this->username . ':' . $this->password);
+ $request = $request->withHeader('Authorization', 'Basic ' . $credentials);
+ }
+ $response = $this->client->send($request);
+
if (200 !== $response->getStatusCode()) {
throw new \Exception('Received HTTP ' . $response->getStatusCode());
}
-
- return (string)$response->getBody();
+ else {
+ $contentType = $response->getHeader('Content-Type');
+
+ @list($mimeType,$parameters) = explode(';', $contentType[0], 2);
+ @list($type, $subType) = explode('/', $mimeType);
+
+ $externalData = [
+ 'mimetype' => $mimeType ?? '',
+ 'type' => $type ?? '',
+ 'subtype' => $subType ?? '',
+ 'parameters' => $parameters ?? '',
+ 'data' => (string)$response->getBody(),
+ ];
+ }
+ return $externalData;
}
/**
@@ -172,11 +261,19 @@ public function getVcard($vcard_id)
if (in_array($response->getStatusCode(), [200,207])) {
$body = (string)$response->getBody();
+ $parser = new Parser($body);
+ $vcard = $parser->getCardAtIndex(0);
+
+ if (isset($this->substitutes)) {
+ foreach ($this->substitutes as $substitute) {
+ $vcard = $this->embedBase64($vcard, $substitute, $this->url);
+ }
+ }
if (is_callable($this->callback)) {
($this->callback)();
}
- return $body;
+ return $vcard;
}
throw new \Exception('Received HTTP ' . $response->getStatusCode());
diff --git a/src/ConvertCommand.php b/src/ConvertCommand.php
index 41e13196..cbe2ecd3 100644
--- a/src/ConvertCommand.php
+++ b/src/ConvertCommand.php
@@ -33,7 +33,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$cards = json_decode(file_get_contents($filename));
if (!is_array($cards)) {
- throw new \Exception(sprintf('Could not read parsed vcards from %s', $filename));
+ throw new \Exception(sprintf('Could not read parsed vCards from %s', $filename));
}
// filter
@@ -47,10 +47,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
error_log(sprintf("Converted %d cards", count($filtered)));
// convert
- $phonebook = $this->config['phonebook'];
- $conversions = $this->config['conversions'];
- $xml = export($phonebook['name'], $filtered, $conversions);
+ $xml = export($filtered, $this->config);
echo $xml->asXML();
}
-}
+}
\ No newline at end of file
diff --git a/src/DownloadCommand.php b/src/DownloadCommand.php
index 433baa4f..1b5ef1ba 100644
--- a/src/DownloadCommand.php
+++ b/src/DownloadCommand.php
@@ -32,13 +32,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->loadConfig($input);
$vcards = array();
-
- $progress = new ProgressBar($output);
-
+ $substitutes = ($input->getOption('image')) ? ['PHOTO'] : [];
+
if ($filename = $input->getArgument('filename')) {
// read from file
$vcards = json_decode(file_get_contents($filename));
-
if (!is_array($vcards)) {
throw new \Exception(sprintf('Could not read unparsed vcards from %s', $filename));
}
@@ -46,15 +44,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
else {
// download
foreach($this->config['server'] as $server) {
+ $progress = new ProgressBar($output);
error_log("Downloading vCard(s) from account ".$server['user']);
+
$backend = backendProvider($server);
-
$progress->start();
- $downloaded = download($backend, function () use ($progress) {
+ $downloaded = download($backend, $substitutes, function () use ($progress) {
$progress->advance();
});
$progress->finish();
-
$vcards = array_merge($vcards, $downloaded);
error_log(sprintf("\nDownloaded %d vCard(s)", count($vcards)));
}
@@ -65,26 +63,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
- // parsing
- error_log("Parsing vcards");
-
- $cards = parse($vcards);
+ // dissolve
+ error_log("Dissolving groups (e.g. iCloud)");
+ $cards = dissolveGroups($vcards);
$json = json_encode($cards, self::JSON_OPTIONS);
- // images
- if ($input->getOption('image')) {
- error_log("Downloading images");
-
- $progress->start();
- $cards = downloadImages($backend, $cards, function() use ($progress) {
- $progress->advance();
- });
- $progress->finish();
-
- error_log(sprintf("\nDownloaded %d image(s)", countImages($cards)));
- }
-
- $json = json_encode($cards, self::JSON_OPTIONS);
echo $json;
}
-}
+}
\ No newline at end of file
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index 9ce3f6f3..156420e8 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -8,34 +8,40 @@
class Converter
{
private $config;
+ private $imagePath;
public function __construct($config)
{
- $this->config = $config;
+ $this->config = $config['conversions'];
+ $this->imagePath = $config['phonebook']['imagepath'] ?? NULL;
}
public function convert($card): SimpleXMLElement
{
$this->card = $card;
- // $contact = $xml->addChild('contact');
$this->contact = new SimpleXMLElement('');
+ $this->contact->addChild('carddav_uid',$this->card->uid); // reference for image upload
+
$this->addVip();
+ // add Person
$person = $this->contact->addChild('person');
$name = htmlspecialchars($this->getProperty('realName'));
$person->addChild('realName', $name);
- // $person->addChild('ImageURL');
-
+
+ // add photo
+ if (isset($this->card->rawPhoto)) {
+ if (isset($this->imagePath)) {
+ $person->addChild('imageURL', $this->imagePath . $this->card->uid . '.jpg');
+ }
+ }
+
$this->addPhone();
+
$this->addEmail();
- $person = $this->contact->addChild('setup');
-
- // print_r($this->contact);
- // echo($this->contact->asXML().PHP_EOL);
-
return $this->contact;
}
@@ -51,45 +57,47 @@ private function addVip()
private function addPhone()
{
//
- // +490358179022
- // +400746653254
-
- $telephony = $this->contact->addChild('telephony');
+ // +490358179022
+ // +400746653254
$replaceCharacters = $this->config['phoneReplaceCharacters'] ?? array();
$phoneTypes = $this->config['phoneTypes'] ?? array();
if (isset($this->card->phone)) {
+ $telephony = $this->contact->addChild('telephony');
+ $idnum = -1;
foreach ($this->card->phone as $numberType => $numbers) {
foreach ($numbers as $idx => $number) {
+ $idnum++;
if (count($replaceCharacters)) {
+ $number = str_replace("\xc2\xa0", "\x20", $number); // delete the wrong ampersand conversion
$number = strtr($number, $replaceCharacters);
$number = trim(preg_replace('/\s+/', ' ', $number));
}
$phone = $telephony->addChild('number', $number);
- $phone->addAttribute('id', $idx);
+ $phone->addAttribute('id', $idnum);
$type = 'other';
+ $numberType = strtolower($numberType);
- foreach ($phoneTypes as $type => $value) {
- if (strpos($numberType, $type) !== false) {
- $type = $value;
- if (strpos($numberType, 'FAX') !== false) {
- $type = 'fax_' . $type;
+ if (stripos($numberType, 'fax') !== false) {
+ $type = 'fax_work';
+ }
+ else {
+ foreach ($phoneTypes as $type => $value) {
+ if (stripos($numberType, $type) !== false) {
+ $type = $value;
+ break;
}
-
- break;
}
}
$phone->addAttribute('type', $type);
- if (strpos($numberType, 'pref') !== false) {
- $phone->addAttribute('prio', 1);
- }
-
- // $phone->addAttribute('vanity', '');
+ }
+ if (strpos($numberType, 'pref') !== false) {
+ $phone->addAttribute('prio', 1);
}
}
}
@@ -98,13 +106,13 @@ private function addPhone()
private function addEmail()
{
//
- // KTS.Michaelis.Hannover@evlka.de
- // Kindertagesstaette@michaelis-hannover.de
+ // no-reply@dummy.de
+ // no-reply@dummy.de
- $services = $this->contact->addChild('services');
$emailTypes = $this->config['emailTypes'] ?? array();
if (isset($this->card->email)) {
+ $services = $this->contact->addChild('services');
foreach ($this->card->email as $emailType => $addresses) {
foreach ($addresses as $idx => $addr) {
$email = $services->addChild('email', $addr);
@@ -116,8 +124,6 @@ private function addEmail()
break;
}
}
-
- // $email->addAttribute('vanity', '');
}
}
}
@@ -167,4 +173,4 @@ private function getProperty(string $property): string
return '';
}
-}
+}
\ No newline at end of file
diff --git a/src/RunCommand.php b/src/RunCommand.php
index 716acd6f..4ef47220 100644
--- a/src/RunCommand.php
+++ b/src/RunCommand.php
@@ -29,56 +29,55 @@ protected function execute(InputInterface $input, OutputInterface $output)
$vcards = array();
$xcards = array();
-
+ $substitutes = ($input->getOption('image')) ? ['PHOTO'] : [];
+
foreach($this->config['server'] as $server) {
$progress = new ProgressBar($output);
error_log("Downloading vCard(s) from account ".$server['user']);
+
$backend = backendProvider($server);
$progress->start();
- $xcards = download ($backend, function () use ($progress) {
+ $xcards = download ($backend, $substitutes, function () use ($progress) {
$progress->advance();
});
$progress->finish();
$vcards = array_merge($vcards, $xcards);
- error_log(sprintf("\nDownloaded %d vCard(s)", count($vcards)));
+ $quantity = count($vcards);
+ error_log(sprintf("\nDownloaded %d vCard(s)", $quantity));
}
- // parse and convert
- error_log("Parsing vcards");
- $cards = parse($vcards);
-
- // images
- if ($input->getOption('image')) {
- error_log("Downloading images");
-
- $progress->start();
- $cards = downloadImages($backend, $cards, function() use ($progress) {
- $progress->advance();
- });
- $progress->finish();
-
- error_log(sprintf("\nDownloaded %d image(s)", countImages($cards)));
- }
-
- // conversion
+ // dissolve
+ error_log("Dissolving groups (e.g. iCloud)");
+ $cards = dissolveGroups($vcards);
+ $remain = count($cards);
+ error_log(sprintf("Dissolved %d group(s)", $quantity - $remain));
+
+ // filter
+ error_log(sprintf("Filtering %d vCard(s)", $remain));
$filters = $this->config['filters'];
$filtered = filter($cards, $filters);
-
- error_log(sprintf("Converted %d vcard(s)", count($filtered)));
-
+ error_log(sprintf("Filtered out %d vCard(s)", $remain - count($filtered)));
+
+ // image upload
+ if ($input->getOption('image')) {
+ error_log("Detaching and uploading image(s)");
+ $pictures = uploadImages($filtered, $this->config['fritzbox']);
+ error_log(sprintf("Uploaded %d image file(s)", $pictures));
+ }
+ else {
+ unset($this->config['phonebook']['imagepath']); // otherwise convert will set wrong links
+ }
+
// fritzbox format
- $phonebook = $this->config['phonebook'];
- $conversions = $this->config['conversions'];
- $xml = export($phonebook['name'], $filtered, $conversions);
+ $xml = export($filtered, $this->config);
+ error_log(sprintf("Converted %d vCard(s)", count($filtered)));
// upload
error_log("Uploading");
$xmlStr = $xml->asXML();
- $fritzbox = $this->config['fritzbox'];
- upload($xmlStr, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id']);
-
- error_log("Uploaded fritz phonebook");
+ upload($xmlStr, $this->config);
+ error_log("Successful uploaded new Fritz!Box phonebook");
}
-}
+}
\ No newline at end of file
diff --git a/src/UploadCommand.php b/src/UploadCommand.php
index d5e29ee9..de48957b 100644
--- a/src/UploadCommand.php
+++ b/src/UploadCommand.php
@@ -29,11 +29,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filename = $input->getArgument('filename');
$xml = file_get_contents($filename);
- $fritzbox = $this->config['fritzbox'];
- $phonebook = $this->config['phonebook'];
-
- upload($xml, $fritzbox['url'], $fritzbox['user'], $fritzbox['password'], $phonebook['id'] ?? 0);
-
- error_log("Uploaded Fritz!Box phonebook");
+ error_log("Uploading Fritz!Box phonebook");
+ upload($xml, $this->config);
+ error_log("Successful uploaded new Fritz!Box phonebook");
}
-}
+}
\ No newline at end of file
diff --git a/src/Vcard/Parser.php b/src/Vcard/Parser.php
index c5e3d432..8e0c7776 100644
--- a/src/Vcard/Parser.php
+++ b/src/Vcard/Parser.php
@@ -223,17 +223,17 @@ protected function parse()
$cardData->title = $value;
break;
case 'PHOTO':
- if ($rawValue) {
- $cardData->rawPhoto = $value;
- } else {
- $cardData->photo = $value;
- }
- break;
case 'LOGO':
+ case 'SOUND':
+ case 'KEY':
+ $element = strtolower($element);
if ($rawValue) {
- $cardData->rawLogo = $value;
+ $rawField = "raw" . ucfirst($element);
+ $dataField = $element . "Data";
+ $cardData->$rawField = $value;
+ $cardData->$dataField = implode(';',$types);
} else {
- $cardData->logo = $value;
+ $cardData->$element = $value;
}
break;
case 'NOTE':
diff --git a/src/functions.php b/src/functions.php
index 811c54f7..9ecb3d14 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -33,79 +33,70 @@ function backendProvider(array $config): Backend
* @param callable $callback
* @return array
*/
-function download(Backend $backend, callable $callback=null): array
+function download(Backend $backend, $substitutes, callable $callback=null): array
{
$backend->setProgress($callback);
+ $backend->setSubstitutes($substitutes);
return $backend->getVcards();
}
/**
- * Download images from CardDAV server
+ * upload image files via ftp to the fritzbox fonpix directory
*
- * @param array $cards
- * @return int
+ * @param $vcards array downloaded vCards
+ * @param $config array
+ * @return number of transfered files
*/
-function downloadImages(Backend $backend, array $cards, callable $callback=null): array
-{
- foreach ($cards as $card) {
- if (isset($card->photo)) {
- $uri = $card->photo;
- $image = $backend->fetchImage($uri);
- $card->photo_data = utf8_encode($image);
-
- if (is_callable($callback)) {
- $callback();
+function uploadImages(array $vcards, $config)
+{
+ $options = array('ftp' => array('overwrite' => true));
+ $context = stream_context_create($options);
+ $i = 0;
+
+ foreach ($vcards as $vcard) {
+ if (isset($vcard->rawPhoto)) { // skip all other vCards
+ if (preg_match("/JPEG/", strtoupper($vcard->photoData))) { // Fritz!Box only accept jpg-files
+ $imgFile = imagecreatefromstring($vcard->rawPhoto);
+ if ($imgFile !== false) {
+ $ftp_destination = sprintf('ftp://%1$s:%2$s@%3$s/%4$s/%5$s.jpg',
+ $config['user'],
+ $config['password'],
+ $config['url'],
+ $config['fonpix'],
+ $vcard->uid
+ );
+ ob_start();
+ imagejpeg($imgFile, NULL);
+ $contents = ob_get_clean();
+ if (file_put_contents($ftp_destination, $contents, 0, $context) !== false) {
+ $i++;
+ }
+ imagedestroy($imgFile);
+ }
}
}
}
-
- return $cards;
-}
-
-/**
- * Count downloaded images contained in list of vcards
- *
- * @param array $cards
- * @return int
- */
-function countImages(array $cards): int
-{
- $images = 0;
-
- foreach ($cards as $card) {
- if (isset($card->photo_data)) {
- $images++;
- }
- }
-
- return $images;
+ return $i;
}
/**
- * Parse an array of raw vcards into POPOs
+ * Dissolve the groups of iCloud contacts
*
* @param array $cards
* @return array
*/
-function parse(array $cards): array
+function dissolveGroups (array $vcards): array
{
- $vcards = [];
$groups = [];
- // parse all vcards
- foreach ($cards as $card) {
- $parser = new Parser($card);
- $vcard = $parser->getCardAtIndex(0);
-
- // separate iCloud groups
+ foreach ($vcards as $key => $vcard) { // separate iCloud groups
if (isset($vcard->xabsmember)) {
$groups[$vcard->fullname] = $vcard->xabsmember;
+ unset($vcards[$key]);
continue;
}
-
- $vcards[] = $vcard;
}
-
+ $vcards = array_values($vcards);
// assign group memberships
foreach ($vcards as $vcard) {
foreach ($groups as $group => $members) {
@@ -113,13 +104,11 @@ function parse(array $cards): array
if (!isset($vcard->group)) {
$vcard->group = array();
}
-
$vcard->group = $group;
break;
}
}
}
-
return $vcards;
}
@@ -243,12 +232,11 @@ function filterMatches($attribute, $filterValues): bool
/**
* Export cards to fritzbox xml
*
- * @param string $name
* @param array $cards
* @param array $conversions
* @return SimpleXMLElement
*/
-function export(string $name, array $cards, array $conversions): SimpleXMLElement
+function export(array $cards, array $conversions): SimpleXMLElement
{
$xml = new SimpleXMLElement(
<<xpath('//phonebook')[0];
- $root->addAttribute('name', $name);
+ $root->addAttribute('name', $conversions['phonebook']['name']);
$converter = new Converter($conversions);
@@ -292,18 +280,17 @@ function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
* Upload cards to fritzbox
*
* @param string $xml
- * @param string $url
- * @param string $user
- * @param string $password
- * @param int $phonebook
+ * @param array $config
* @return void
*/
-function upload(string $xml, string $url, string $user, string $password, int $phonebook=0)
+function upload(string $xml, $config)
{
- $fritz = new Api($url, $user, $password, 1);
+ $fritzbox = $config['fritzbox'];
+
+ $fritz = new Api($fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
$formfields = array(
- 'PhonebookId' => $phonebook
+ 'PhonebookId' => $config['phonebook']['id']
);
$filefields = array(
From 4e2a913061b550a1841a772ab975f31d97f384cc Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Wed, 19 Dec 2018 21:03:56 +0100
Subject: [PATCH 052/165] Update copyright (#65)
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 92709ef6..ff8877a8 100644
--- a/README.md
+++ b/README.md
@@ -36,4 +36,4 @@ Get help for a command:
This script is released under Public Domain, some parts under MIT license. Make sure you understand which parts are which.
## Authors
-Copyright (c) 2012-2017 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger, Andreas Götz
+Copyright (c) 2012-2017 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger, Andreas Götz, Volker Püschel
From 8434a999fbd449176fc9cf10658259fb9b0016d0 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Wed, 26 Dec 2018 12:40:16 +0100
Subject: [PATCH 053/165] Image upload documentation (#67)
* deletes semicolons that sometimes appear at the end of "ORG:" names
* Additional information for a smooth operation
---
README.md | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/README.md b/README.md
index ff8877a8..a50c5726 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,11 @@ Get help for a command:
php carddav2fb.php run -h
+## Precondition for using image upload (command -i)
+
+ * your memory (USB stick) is indexed [Heimnetz -> Speicher (NAS) -> Speicher an der FRITZ!Box]
+ * ftp access is activ [Heimnetz -> Speicher (NAS) -> Heimnetzfreigabe]
+ * you use an standalone user (NOT! dslf-config) which has explicit permissions for FRITZ!Box settings, access to NAS content and read/write permission to all available memory [System -> FRITZ!Box-Benutzer -> [user] -> Berechtigungen]
## License
This script is released under Public Domain, some parts under MIT license. Make sure you understand which parts are which.
From 0d42c555f46284fcb4a5e7426f69f0cfe886f4d5 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Wed, 26 Dec 2018 23:55:10 +0100
Subject: [PATCH 054/165] Image upload error handling (#68)
---
src/functions.php | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/src/functions.php b/src/functions.php
index 9ecb3d14..cb84225c 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -66,11 +66,15 @@ function uploadImages(array $vcards, $config)
$vcard->uid
);
ob_start();
- imagejpeg($imgFile, NULL);
+ imagejpeg($imgFile, null);
$contents = ob_get_clean();
- if (file_put_contents($ftp_destination, $contents, 0, $context) !== false) {
+ if (@file_put_contents($ftp_destination, $contents, 0, $context) !== false) {
$i++;
}
+ else {
+ unset($vcard->rawPhoto); // no wrong link will set in phonebook
+ error_log(sprintf("ftp access denied for %s.jpg to %s!", $vcard->uid, $config['fonpix']));
+ }
imagedestroy($imgFile);
}
}
From 1cc2e938f4d552acc005aad3bce7e7aac5686411 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Sun, 30 Dec 2018 09:49:41 +0100
Subject: [PATCH 055/165] Implements the assignment of quick dial and vanity
numbers (#70)
---
src/FritzBox/Converter.php | 41 +++++++++++++++++++++++++++++++++-----
src/Vcard/Parser.php | 11 ++++++++++
2 files changed, 47 insertions(+), 5 deletions(-)
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index 156420e8..b0b851b7 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -9,6 +9,7 @@ class Converter
{
private $config;
private $imagePath;
+ private $uniqueDials = array();
public function __construct($config)
{
@@ -23,23 +24,23 @@ public function convert($card): SimpleXMLElement
$this->contact = new SimpleXMLElement('');
$this->contact->addChild('carddav_uid',$this->card->uid); // reference for image upload
-
+
$this->addVip();
// add Person
$person = $this->contact->addChild('person');
$name = htmlspecialchars($this->getProperty('realName'));
$person->addChild('realName', $name);
-
+
// add photo
if (isset($this->card->rawPhoto)) {
if (isset($this->imagePath)) {
$person->addChild('imageURL', $this->imagePath . $this->card->uid . '.jpg');
}
}
-
+
$this->addPhone();
-
+
$this->addEmail();
return $this->contact;
@@ -95,10 +96,40 @@ private function addPhone()
$phone->addAttribute('type', $type);
- }
+ }
if (strpos($numberType, 'pref') !== false) {
$phone->addAttribute('prio', 1);
}
+
+ // add quick dial number; Fritz!Box will add the prefix **7 automatically
+ if (isset($this->card->xquickdial)) {
+ if (!in_array($this->card->xquickdial, $this->uniqueDials)) { // quick dial number really unique?
+ if (strpos($numberType, 'pref') !== false) {
+ $phone->addAttribute('quickdial', $this->card->xquickdial);
+ $this->uniqueDials[] = $this->card->xquickdial; // keep quick dial number for cross check
+ unset($this->card->xquickdial); // flush used quick dial number
+ }
+ }
+ else {
+ $format = "The quick dial number >%s< has been assigned more than once (%s)!";
+ error_log(sprintf($format, $this->card->xquickdial, $number));
+ }
+ }
+
+ // add vanity number; Fritz!Box will add the prefix **8 automatically
+ if (isset($this->card->xvanity)) {
+ if (!in_array($this->card->xvanity, $this->uniqueDials)) { // vanity string really unique?
+ if (strpos($numberType, 'pref') !== false) {
+ $phone->addAttribute('vanity', $this->card->xvanity);
+ $this->uniqueDials[] = $this->card->xvanity; // keep vanity string for cross check
+ unset($this->card->xvanity); // flush used vanity number
+ }
+ }
+ else {
+ $format = "The vanity string >%s< has been assigned more than once (%s)!";
+ error_log(sprintf($format, $this->card->xvanity, $number));
+ }
+ }
}
}
}
diff --git a/src/Vcard/Parser.php b/src/Vcard/Parser.php
index 8e0c7776..0fe5a388 100644
--- a/src/Vcard/Parser.php
+++ b/src/Vcard/Parser.php
@@ -255,6 +255,17 @@ protected function parse()
}
$cardData->xabsmember[] = preg_replace('/^urn:uuid:/', '', $value);
break;
+ /* User extended attributes for FritzBox */
+ case 'X-FB-QUICKDIAL': // 00-99; Fritz!Box will add the prefix **7 internal
+ if ($value >= 0 && $value <= 99) {
+ $cardData->xquickdial = $value;
+ }
+ break;
+ case 'X-FB-VANITY': // max. 8 CAPITAL letters; Fritz!Box will add the prefix **8 internal
+ if (ctype_alpha($value)) {
+ $cardData->xvanity = substr(strtoupper($value), 0, 8);
+ }
+ break;
}
}
}
From 1a2131a6c0c6518e9ccdd9ad4cbeb2e6db3bc3d5 Mon Sep 17 00:00:00 2001
From: Black Senator <31481294+BlackSenator@users.noreply.github.com>
Date: Sat, 5 Jan 2019 13:59:54 +0100
Subject: [PATCH 056/165] Handler contacts with >9 numbers (#71)
* if more than nine phone numbers are included, the contact will be divided into a corresponding number of phonebook entries (any existing email addresses are assigned to the first set [there is no quantity limit!])
---
README.md | 9 +++
src/FritzBox/Converter.php | 149 ++++++++++++++++++++++++-------------
src/functions.php | 20 ++---
3 files changed, 116 insertions(+), 62 deletions(-)
diff --git a/README.md b/README.md
index a50c5726..c62a3deb 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,15 @@
This is an entirely simplified version of https://github.com/jens-maus/carddav2fb. The Vcard parser has been replaced by an extended version of https://github.com/jeroendesloovere/vcard.
+## Features
+
+ * download from any number of CardDAV servers
+ * selection (include/exclude) by categories or groups (e.g. iCloud)
+ * upload of contact pictures to display them on the FRITZ!Fon (handling see below)
+ * transfer of quick dial and vanity numbers (see wiki for handling details)
+ * if more than nine phone numbers are included, the contact will be divided into a corresponding number of phonebook entries (any existing email addresses are assigned to the first set [there is no quantity limit!])
+ * the contact's UID of the CardDAV server is added to the phonebook entry (not visible in the FRITZ! Box GUI)
+
## Requirements
* PHP 7.0 (`apt-get install php7.0 php7.0-curl php7.0-mbstring php7.0-xml`)
diff --git a/src/FritzBox/Converter.php b/src/FritzBox/Converter.php
index b0b851b7..fe0ccc1d 100644
--- a/src/FritzBox/Converter.php
+++ b/src/FritzBox/Converter.php
@@ -6,9 +6,12 @@
use \SimpleXMLElement;
class Converter
+
{
private $config;
private $imagePath;
+ private $numbers;
+ private $adresses;
private $uniqueDials = array();
public function __construct($config)
@@ -17,33 +20,39 @@ public function __construct($config)
$this->imagePath = $config['phonebook']['imagepath'] ?? NULL;
}
- public function convert($card): SimpleXMLElement
+ public function convert($card)
{
$this->card = $card;
-
- $this->contact = new SimpleXMLElement('');
-
- $this->contact->addChild('carddav_uid',$this->card->uid); // reference for image upload
-
- $this->addVip();
-
- // add Person
- $person = $this->contact->addChild('person');
- $name = htmlspecialchars($this->getProperty('realName'));
- $person->addChild('realName', $name);
-
- // add photo
- if (isset($this->card->rawPhoto)) {
- if (isset($this->imagePath)) {
- $person->addChild('imageURL', $this->imagePath . $this->card->uid . '.jpg');
+ $contacts = array();
+
+ $this->numbers = $this->getPhoneNumbers(); // get array of prequalified phone numbers
+ $this->adresses = $this->getEmailAdresses(); // get array of prequalified email adresses
+
+ while ((count($this->numbers)) || (count($this->adresses))) {
+ $this->contact = new SimpleXMLElement('');
+ $this->contact->addChild('carddav_uid',$this->card->uid); // reference for image upload
+ $this->addVip();
+ // add Person
+ $person = $this->contact->addChild('person');
+ $name = htmlspecialchars($this->getProperty('realName'));
+ $person->addChild('realName', $name);
+ // add photo
+ if (isset($this->card->rawPhoto)) {
+ if (isset($this->imagePath)) {
+ $person->addChild('imageURL', $this->imagePath . $this->card->uid . '.jpg');
+ }
+ }
+ // add Phone
+ if (count($this->numbers)) {
+ $this->addPhone();
}
+ // add eMail
+ if (count($this->adresses)) {
+ $this->addEmail();
+ }
+ $contacts[] = $this->contact;
}
-
- $this->addPhone();
-
- $this->addEmail();
-
- return $this->contact;
+ return $contacts;
}
private function addVip()
@@ -57,31 +66,70 @@ private function addVip()
private function addPhone()
{
- //
- // +490358179022
- // +400746653254
+ $telephony = $this->contact->addChild('telephony');
+ $phoneCounter = 0;
+ while (count($this->numbers)) {
+ $phone = $telephony->addChild('number', $this->numbers[0]['number']);
+ $phone->addAttribute('id', $phoneCounter);
+ if (isset($this->numbers[0]['type'])) {
+ $phone->addAttribute('type', $this->numbers[0]['type']);
+ }
+ if (isset($this->numbers[0]['pref'])) {
+ $phone->addAttribute('prio', $this->numbers[0]['pref']);
+ }
+ if (isset($this->numbers[0]['quickdial'])) {
+ $phone->addAttribute('quickdial', $this->numbers[0]['quickdial']);
+ }
+ if (isset($this->numbers[0]['vanity'])) {
+ $phone->addAttribute('vanity', $this->numbers[0]['vanity']);
+ }
+ array_shift($this->numbers);
+ $phoneCounter++;
+ // not more than nine phone numbers per contact
+ if ($phoneCounter == 9) {
+ break;
+ }
+ }
+ }
+
+ private function addEmail()
+ {
+ $services = $this->contact->addChild('services');
+ $eMailCounter = 0;
+ while (count($this->adresses)) {
+ $email = $services->addChild('email', $this->adresses[0]['email']);
+ $email->addAttribute('id', $eMailCounter);
+ if (isset($this->adresses[0]['classifier'])) {
+ $email->addAttribute('classifier', $this->adresses[0]['classifier']);
+ }
+ array_shift($this->adresses);
+ $eMailCounter++;
+ }
+ }
+
+ /**
+ * delivers an array of prequalified phone numbers. This is neccesseary to
+ * handle the maximum of nine phone numbers per FRITZ!Box phonebook contacts
+ */
+ private function getPhoneNumbers()
+ {
+ $phoneNumbers = array();
$replaceCharacters = $this->config['phoneReplaceCharacters'] ?? array();
$phoneTypes = $this->config['phoneTypes'] ?? array();
-
if (isset($this->card->phone)) {
- $telephony = $this->contact->addChild('telephony');
$idnum = -1;
foreach ($this->card->phone as $numberType => $numbers) {
- foreach ($numbers as $idx => $number) {
+ foreach ($numbers as $number) {
$idnum++;
if (count($replaceCharacters)) {
$number = str_replace("\xc2\xa0", "\x20", $number); // delete the wrong ampersand conversion
$number = strtr($number, $replaceCharacters);
$number = trim(preg_replace('/\s+/', ' ', $number));
}
-
- $phone = $telephony->addChild('number', $number);
- $phone->addAttribute('id', $idnum);
-
+ $phoneNumbers[$idnum]['number'] = $number;
$type = 'other';
$numberType = strtolower($numberType);
-
if (stripos($numberType, 'fax') !== false) {
$type = 'fax_work';
}
@@ -93,19 +141,16 @@ private function addPhone()
}
}
}
-
- $phone->addAttribute('type', $type);
-
+ $phoneNumbers[$idnum]['type'] = $type;
}
if (strpos($numberType, 'pref') !== false) {
- $phone->addAttribute('prio', 1);
+ $phoneNumbers[$idnum]['pref'] = 1;
}
-
// add quick dial number; Fritz!Box will add the prefix **7 automatically
if (isset($this->card->xquickdial)) {
if (!in_array($this->card->xquickdial, $this->uniqueDials)) { // quick dial number really unique?
if (strpos($numberType, 'pref') !== false) {
- $phone->addAttribute('quickdial', $this->card->xquickdial);
+ $phoneNumbers[$idnum]['quickdial'] = $this->card->xquickdial;
$this->uniqueDials[] = $this->card->xquickdial; // keep quick dial number for cross check
unset($this->card->xquickdial); // flush used quick dial number
}
@@ -115,12 +160,11 @@ private function addPhone()
error_log(sprintf($format, $this->card->xquickdial, $number));
}
}
-
// add vanity number; Fritz!Box will add the prefix **8 automatically
if (isset($this->card->xvanity)) {
if (!in_array($this->card->xvanity, $this->uniqueDials)) { // vanity string really unique?
if (strpos($numberType, 'pref') !== false) {
- $phone->addAttribute('vanity', $this->card->xvanity);
+ $phoneNumbers[$idnum]['vanity'] = $this->card->xvanity;
$this->uniqueDials[] = $this->card->xvanity; // keep vanity string for cross check
unset($this->card->xvanity); // flush used vanity number
}
@@ -132,32 +176,33 @@ private function addPhone()
}
}
}
+ return $phoneNumbers;
}
- private function addEmail()
+ /**
+ * delivers an array of prequalified email adresses. There is no limitation
+ * for the amount of email adresses in FRITZ!Box phonebook contacts.
+ */
+ private function getEmailAdresses()
{
- //
- // no-reply@dummy.de
- // no-reply@dummy.de
-
+ $mailAdresses = array();
$emailTypes = $this->config['emailTypes'] ?? array();
if (isset($this->card->email)) {
- $services = $this->contact->addChild('services');
foreach ($this->card->email as $emailType => $addresses) {
foreach ($addresses as $idx => $addr) {
- $email = $services->addChild('email', $addr);
- $email->addAttribute('id', $idx);
-
+ $mailAdresses[$idx]['email'] = $addr;
+ $mailAdresses[$idx]['id'] = $idx;
foreach ($emailTypes as $type => $value) {
if (strpos($emailType, $type) !== false) {
- $email->addAttribute('classifier', $value);
+ $mailAdresses[$idx]['classifier'] = $value;
break;
}
}
}
}
}
+ return $mailAdresses;
}
private function getProperty(string $property): string
diff --git a/src/functions.php b/src/functions.php
index cb84225c..630081ed 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -48,20 +48,20 @@ function download(Backend $backend, $substitutes, callable $callback=null): arra
* @return number of transfered files
*/
function uploadImages(array $vcards, $config)
-{
+{
$options = array('ftp' => array('overwrite' => true));
$context = stream_context_create($options);
$i = 0;
-
+
foreach ($vcards as $vcard) {
if (isset($vcard->rawPhoto)) { // skip all other vCards
if (preg_match("/JPEG/", strtoupper($vcard->photoData))) { // Fritz!Box only accept jpg-files
$imgFile = imagecreatefromstring($vcard->rawPhoto);
if ($imgFile !== false) {
$ftp_destination = sprintf('ftp://%1$s:%2$s@%3$s/%4$s/%5$s.jpg',
- $config['user'],
- $config['password'],
- $config['url'],
+ $config['user'],
+ $config['password'],
+ $config['url'],
$config['fonpix'],
$vcard->uid
);
@@ -257,11 +257,11 @@ function export(array $cards, array $conversions): SimpleXMLElement
$converter = new Converter($conversions);
foreach ($cards as $card) {
- $contact = $converter->convert($card);
- // $root->addChild('contact', $contact);
- xml_adopt($root, $contact);
+ $contacts = $converter->convert($card);
+ foreach ($contacts as $contact) {
+ xml_adopt($root, $contact);
+ }
}
-
return $xml;
}
@@ -290,7 +290,7 @@ function xml_adopt(SimpleXMLElement $to, SimpleXMLElement $from)
function upload(string $xml, $config)
{
$fritzbox = $config['fritzbox'];
-
+
$fritz = new Api($fritzbox['url'], $fritzbox['user'], $fritzbox['password']);
$formfields = array(
From dbb834bbe68fc76920b9e524b4593381b81d9617 Mon Sep 17 00:00:00 2001
From: Wolfram Esser
Date: Sat, 5 Jan 2019 15:02:56 +0100
Subject: [PATCH 057/165] Allow correct filtering even if multiple servers
share the same group names (#77)
---
src/functions.php | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/functions.php b/src/functions.php
index 630081ed..82c9e01e 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -95,7 +95,11 @@ function dissolveGroups (array $vcards): array
foreach ($vcards as $key => $vcard) { // separate iCloud groups
if (isset($vcard->xabsmember)) {
- $groups[$vcard->fullname] = $vcard->xabsmember;
+ if (array_key_exists($vcard->fullname, $groups)) {
+ $groups[$vcard->fullname] = array_merge($groups[$vcard->fullname], $vcard->xabsmember);
+ } else {
+ $groups[$vcard->fullname] = $vcard->xabsmember;
+ }
unset($vcards[$key]);
continue;
}
From 41c15a0156588eb6e782d2458a42a9a11c9d2f4c Mon Sep 17 00:00:00 2001
From: andig
Date: Sat, 5 Jan 2019 15:31:19 +0100
Subject: [PATCH 058/165] Various cleanups (#79)
---
lib/CardDAV-PHP/CHANGELOG | 115 ---
lib/CardDAV-PHP/LICENSE | 661 -----------------
lib/CardDAV-PHP/README.md | 33 -
lib/fritzbox_api_php/CHANGELOG.txt | 79 --
lib/fritzbox_api_php/README.md | 80 --
lib/fritzbox_api_php/README.txt | 58 --
.../fritzbox_delete_foncallslist.php | 50 --
.../fritzbox_diversity0on.php | 37 -
.../fritzbox_get_foncallslist.php | 73 --
.../fritzbox_get_foncallslist_xml.php | 72 --
.../fritzbox_guestlan_on_off.php | 127 ----
.../fritzbox_guestwlan_on_off.php | 143 ----
lib/fritzbox_api_php/fritzbox_ring_phone.php | 33 -
lib/fritzbox_api_php/fritzbox_tam_on_off.php | 109 ---
lib/fritzbox_api_php/fritzbox_tam_swap.php | 56 --
lib/fritzbox_api_php/fritzbox_user.conf.php | 40 -
lib/vCard-parser/.gitignore | 3 -
lib/vCard-parser/Example.vcf | 24 -
lib/vCard-parser/Example2.1.vcf | 15 -
lib/vCard-parser/Example3.0.vcf | 36 -
lib/vCard-parser/LICENSE | 20 -
lib/vCard-parser/README.md | 48 --
lib/vCard-parser/test.php | 216 ------
lib/vCard-parser/vCard.php | 692 ------------------
lib/vCard-parser/write-test.php | 20 -
src/CardDav/Backend.php | 71 +-
src/FritzBox/Api.php | 5 +-
27 files changed, 44 insertions(+), 2872 deletions(-)
delete mode 100644 lib/CardDAV-PHP/CHANGELOG
delete mode 100644 lib/CardDAV-PHP/LICENSE
delete mode 100644 lib/CardDAV-PHP/README.md
delete mode 100644 lib/fritzbox_api_php/CHANGELOG.txt
delete mode 100644 lib/fritzbox_api_php/README.md
delete mode 100644 lib/fritzbox_api_php/README.txt
delete mode 100644 lib/fritzbox_api_php/fritzbox_delete_foncallslist.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_diversity0on.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_get_foncallslist.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_get_foncallslist_xml.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_guestlan_on_off.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_guestwlan_on_off.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_ring_phone.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_tam_on_off.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_tam_swap.php
delete mode 100644 lib/fritzbox_api_php/fritzbox_user.conf.php
delete mode 100644 lib/vCard-parser/.gitignore
delete mode 100644 lib/vCard-parser/Example.vcf
delete mode 100644 lib/vCard-parser/Example2.1.vcf
delete mode 100644 lib/vCard-parser/Example3.0.vcf
delete mode 100644 lib/vCard-parser/LICENSE
delete mode 100644 lib/vCard-parser/README.md
delete mode 100644 lib/vCard-parser/test.php
delete mode 100644 lib/vCard-parser/vCard.php
delete mode 100644 lib/vCard-parser/write-test.php
diff --git a/lib/CardDAV-PHP/CHANGELOG b/lib/CardDAV-PHP/CHANGELOG
deleted file mode 100644
index a443275c..00000000
--- a/lib/CardDAV-PHP/CHANGELOG
+++ /dev/null
@@ -1,115 +0,0 @@
-Changes from v0.5.2 to v0.6
-- Added Exception handling
-- Each method now analyzes the http status code on it's own
-- Minor comment, documentation and phpdoc changes
-
-Changes from v0.5.1 to v0.5.2
-- Added debug function
-- Some minor class structure changes
-- Some comment, documentation and phpdoc changes
-
-Changes from v0.5 to v0.5.1
-- Added SOGo to the CardDAV server list
-- Deleted the method get_last_vcard_id() -> the add method does now returns the vCard id
-- Added posibility to get a raw XML vCard response to the get_xml_vcard() method
-- Some comment, documentation and phpdoc changes
-
-Changes from v0.4.9 to v0.5
-- Added SOGo support
-- Special thanks to xbgmsharp for the great feedback!
-- Cache curl connection so that only one authentication process is needed
-- Added boolean return value for add-, edit- and delete-methods
-- Added addressbook handling
-
-Changes from v0.4.8 to v0.4.9
-- New method: clean_vcard
-- New method: get_last_vcard_id
-
-Changes from v0.4.7 to v0.4.8
-- Extended clean_response() method and added utf-encoding
-
-Changes from v0.4.6 to v0.4.7
-- Changed license from LGPLv2 to AGPLv3
-- Changed minor wordings in the changelog
-
-Changes from v0.4.5 to v0.4.6
-- Added "get XML vCard"-method to get a single vCard with more detailed information
-
-Changes from v0.4.4 to v0.4.5
-- Minor comment and class changes
-
-Changes from v0.4.3 to v0.4.4
-- Verified ownCloud support
-- Store username and password for class internal use
-- Cleaned the class
-- Automaticly adds a / at the end of an url if it's missing
-- Added a URL-Schema list
-
-Changes from v0.4.2 to v0.4.3
-- Added sabreDAV support
-
-Changes from v0.4.1 to v0.4.2
-- Standardized "get last modified" seems not to work with every server configuration -> no standardized date anymore!
-
-Changes from v0.4 to v0.4.1
-- Added memotoo support
-- Bugfix: mostly check_connection() returned "true" if the server connection failed
-- Removed unused code
-- Standardized "get last modified" to unix-timestamp
-- Changed version naming
-
-Changes from v0.3.3 to v0.4
-- Switched from "fopen" to curl for all server requests
-- get() uses now PROPFIND instead of REPORT
-- Simplified response additionally returns "get last modified"
-- Added Apple addressbook server support
-
-Changes from v0.3.3 to v0.3.4
-- vCard id returned by get() excludes now the file extension ".vcf"
-
-Changes from v0.3.2 to v0.3.3
-- Minor comment changes
-
-Changes from v0.3.1 to v0.3.2
-- Minor phpdoc changes / extensions
-
-Changes from v0.3 to v0.3.1
-- Get only entries if a vCard id exists
-- Get etag bugfix (PROFIND -> REPORT)
-- vCard id generator bugfix
-- Minor class-structure and comment changes
-
-Changes from v0.2.2 to v0.3
-- Deleted unused methods
-- Added server connection check
-- Added some more examples
-- get() can now return complete vCards
-
-Changes from v0.2.1 to v0.2.2
-- Added error handling
-- Added phpdoc support
-- Cleaned comments / variables
-
-Changes from v0.2 to v0.2.1
-- XML requests will be built by the XMLWriter
-- Added etag to simplified response
-
-Changes from v0.1.2 to v0.2
-- Added add functionality
-- Added update functionality
-- Added get vCard functionality
-- Added user-agent to http-requests
-- Added simplified response
-- Set vCard standard fields (Version, N, FN)
-
-Changes from v0.1.1 to v0.1.2
-- Added delete functionality
-
-Changes from v0.1 to v0.1.1
-- Added filter type (OR / AND)
-
-release v0.1
-- Query CardDAV server with XML response (only tested on davical)
-- Filter functionality for search implementations
-- Fieldsets for minimal vCards within the XML response
-- Authentification
diff --git a/lib/CardDAV-PHP/LICENSE b/lib/CardDAV-PHP/LICENSE
deleted file mode 100644
index 2def0e88..00000000
--- a/lib/CardDAV-PHP/LICENSE
+++ /dev/null
@@ -1,661 +0,0 @@
- GNU AFFERO GENERAL PUBLIC LICENSE
- Version 3, 19 November 2007
-
- Copyright (C) 2007 Free Software Foundation, Inc.
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
- Preamble
-
- The GNU Affero General Public License is a free, copyleft license for
-software and other kinds of works, specifically designed to ensure
-cooperation with the community in the case of network server software.
-
- The licenses for most software and other practical works are designed
-to take away your freedom to share and change the works. By contrast,
-our General Public Licenses are intended to guarantee your freedom to
-share and change all versions of a program--to make sure it remains free
-software for all its users.
-
- When we speak of free software, we are referring to freedom, not
-price. Our General Public Licenses are designed to make sure that you
-have the freedom to distribute copies of free software (and charge for
-them if you wish), that you receive source code or can get it if you
-want it, that you can change the software or use pieces of it in new
-free programs, and that you know you can do these things.
-
- Developers that use our General Public Licenses protect your rights
-with two steps: (1) assert copyright on the software, and (2) offer
-you this License which gives you legal permission to copy, distribute
-and/or modify the software.
-
- A secondary benefit of defending all users' freedom is that
-improvements made in alternate versions of the program, if they
-receive widespread use, become available for other developers to
-incorporate. Many developers of free software are heartened and
-encouraged by the resulting cooperation. However, in the case of
-software used on network servers, this result may fail to come about.
-The GNU General Public License permits making a modified version and
-letting the public access it on a server without ever releasing its
-source code to the public.
-
- The GNU Affero General Public License is designed specifically to
-ensure that, in such cases, the modified source code becomes available
-to the community. It requires the operator of a network server to
-provide the source code of the modified version running there to the
-users of that server. Therefore, public use of a modified version, on
-a publicly accessible server, gives the public access to the source
-code of the modified version.
-
- An older license, called the Affero General Public License and
-published by Affero, was designed to accomplish similar goals. This is
-a different license, not a version of the Affero GPL, but Affero has
-released a new version of the Affero GPL which permits relicensing under
-this license.
-
- The precise terms and conditions for copying, distribution and
-modification follow.
-
- TERMS AND CONDITIONS
-
- 0. Definitions.
-
- "This License" refers to version 3 of the GNU Affero General Public License.
-
- "Copyright" also means copyright-like laws that apply to other kinds of
-works, such as semiconductor masks.
-
- "The Program" refers to any copyrightable work licensed under this
-License. Each licensee is addressed as "you". "Licensees" and
-"recipients" may be individuals or organizations.
-
- To "modify" a work means to copy from or adapt all or part of the work
-in a fashion requiring copyright permission, other than the making of an
-exact copy. The resulting work is called a "modified version" of the
-earlier work or a work "based on" the earlier work.
-
- A "covered work" means either the unmodified Program or a work based
-on the Program.
-
- To "propagate" a work means to do anything with it that, without
-permission, would make you directly or secondarily liable for
-infringement under applicable copyright law, except executing it on a
-computer or modifying a private copy. Propagation includes copying,
-distribution (with or without modification), making available to the
-public, and in some countries other activities as well.
-
- To "convey" a work means any kind of propagation that enables other
-parties to make or receive copies. Mere interaction with a user through
-a computer network, with no transfer of a copy, is not conveying.
-
- An interactive user interface displays "Appropriate Legal Notices"
-to the extent that it includes a convenient and prominently visible
-feature that (1) displays an appropriate copyright notice, and (2)
-tells the user that there is no warranty for the work (except to the
-extent that warranties are provided), that licensees may convey the
-work under this License, and how to view a copy of this License. If
-the interface presents a list of user commands or options, such as a
-menu, a prominent item in the list meets this criterion.
-
- 1. Source Code.
-
- The "source code" for a work means the preferred form of the work
-for making modifications to it. "Object code" means any non-source
-form of a work.
-
- A "Standard Interface" means an interface that either is an official
-standard defined by a recognized standards body, or, in the case of
-interfaces specified for a particular programming language, one that
-is widely used among developers working in that language.
-
- The "System Libraries" of an executable work include anything, other
-than the work as a whole, that (a) is included in the normal form of
-packaging a Major Component, but which is not part of that Major
-Component, and (b) serves only to enable use of the work with that
-Major Component, or to implement a Standard Interface for which an
-implementation is available to the public in source code form. A
-"Major Component", in this context, means a major essential component
-(kernel, window system, and so on) of the specific operating system
-(if any) on which the executable work runs, or a compiler used to
-produce the work, or an object code interpreter used to run it.
-
- The "Corresponding Source" for a work in object code form means all
-the source code needed to generate, install, and (for an executable
-work) run the object code and to modify the work, including scripts to
-control those activities. However, it does not include the work's
-System Libraries, or general-purpose tools or generally available free
-programs which are used unmodified in performing those activities but
-which are not part of the work. For example, Corresponding Source
-includes interface definition files associated with source files for
-the work, and the source code for shared libraries and dynamically
-linked subprograms that the work is specifically designed to require,
-such as by intimate data communication or control flow between those
-subprograms and other parts of the work.
-
- The Corresponding Source need not include anything that users
-can regenerate automatically from other parts of the Corresponding
-Source.
-
- The Corresponding Source for a work in source code form is that
-same work.
-
- 2. Basic Permissions.
-
- All rights granted under this License are granted for the term of
-copyright on the Program, and are irrevocable provided the stated
-conditions are met. This License explicitly affirms your unlimited
-permission to run the unmodified Program. The output from running a
-covered work is covered by this License only if the output, given its
-content, constitutes a covered work. This License acknowledges your
-rights of fair use or other equivalent, as provided by copyright law.
-
- You may make, run and propagate covered works that you do not
-convey, without conditions so long as your license otherwise remains
-in force. You may convey covered works to others for the sole purpose
-of having them make modifications exclusively for you, or provide you
-with facilities for running those works, provided that you comply with
-the terms of this License in conveying all material for which you do
-not control copyright. Those thus making or running the covered works
-for you must do so exclusively on your behalf, under your direction
-and control, on terms that prohibit them from making any copies of
-your copyrighted material outside their relationship with you.
-
- Conveying under any other circumstances is permitted solely under
-the conditions stated below. Sublicensing is not allowed; section 10
-makes it unnecessary.
-
- 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
-
- No covered work shall be deemed part of an effective technological
-measure under any applicable law fulfilling obligations under article
-11 of the WIPO copyright treaty adopted on 20 December 1996, or
-similar laws prohibiting or restricting circumvention of such
-measures.
-
- When you convey a covered work, you waive any legal power to forbid
-circumvention of technological measures to the extent such circumvention
-is effected by exercising rights under this License with respect to
-the covered work, and you disclaim any intention to limit operation or
-modification of the work as a means of enforcing, against the work's
-users, your or third parties' legal rights to forbid circumvention of
-technological measures.
-
- 4. Conveying Verbatim Copies.
-
- You may convey verbatim copies of the Program's source code as you
-receive it, in any medium, provided that you conspicuously and
-appropriately publish on each copy an appropriate copyright notice;
-keep intact all notices stating that this License and any
-non-permissive terms added in accord with section 7 apply to the code;
-keep intact all notices of the absence of any warranty; and give all
-recipients a copy of this License along with the Program.
-
- You may charge any price or no price for each copy that you convey,
-and you may offer support or warranty protection for a fee.
-
- 5. Conveying Modified Source Versions.
-
- You may convey a work based on the Program, or the modifications to
-produce it from the Program, in the form of source code under the
-terms of section 4, provided that you also meet all of these conditions:
-
- a) The work must carry prominent notices stating that you modified
- it, and giving a relevant date.
-
- b) The work must carry prominent notices stating that it is
- released under this License and any conditions added under section
- 7. This requirement modifies the requirement in section 4 to
- "keep intact all notices".
-
- c) You must license the entire work, as a whole, under this
- License to anyone who comes into possession of a copy. This
- License will therefore apply, along with any applicable section 7
- additional terms, to the whole of the work, and all its parts,
- regardless of how they are packaged. This License gives no
- permission to license the work in any other way, but it does not
- invalidate such permission if you have separately received it.
-
- d) If the work has interactive user interfaces, each must display
- Appropriate Legal Notices; however, if the Program has interactive
- interfaces that do not display Appropriate Legal Notices, your
- work need not make them do so.
-
- A compilation of a covered work with other separate and independent
-works, which are not by their nature extensions of the covered work,
-and which are not combined with it such as to form a larger program,
-in or on a volume of a storage or distribution medium, is called an
-"aggregate" if the compilation and its resulting copyright are not
-used to limit the access or legal rights of the compilation's users
-beyond what the individual works permit. Inclusion of a covered work
-in an aggregate does not cause this License to apply to the other
-parts of the aggregate.
-
- 6. Conveying Non-Source Forms.
-
- You may convey a covered work in object code form under the terms
-of sections 4 and 5, provided that you also convey the
-machine-readable Corresponding Source under the terms of this License,
-in one of these ways:
-
- a) Convey the object code in, or embodied in, a physical product
- (including a physical distribution medium), accompanied by the
- Corresponding Source fixed on a durable physical medium
- customarily used for software interchange.
-
- b) Convey the object code in, or embodied in, a physical product
- (including a physical distribution medium), accompanied by a
- written offer, valid for at least three years and valid for as
- long as you offer spare parts or customer support for that product
- model, to give anyone who possesses the object code either (1) a
- copy of the Corresponding Source for all the software in the
- product that is covered by this License, on a durable physical
- medium customarily used for software interchange, for a price no
- more than your reasonable cost of physically performing this
- conveying of source, or (2) access to copy the
- Corresponding Source from a network server at no charge.
-
- c) Convey individual copies of the object code with a copy of the
- written offer to provide the Corresponding Source. This
- alternative is allowed only occasionally and noncommercially, and
- only if you received the object code with such an offer, in accord
- with subsection 6b.
-
- d) Convey the object code by offering access from a designated
- place (gratis or for a charge), and offer equivalent access to the
- Corresponding Source in the same way through the same place at no
- further charge. You need not require recipients to copy the
- Corresponding Source along with the object code. If the place to
- copy the object code is a network server, the Corresponding Source
- may be on a different server (operated by you or a third party)
- that supports equivalent copying facilities, provided you maintain
- clear directions next to the object code saying where to find the
- Corresponding Source. Regardless of what server hosts the
- Corresponding Source, you remain obligated to ensure that it is
- available for as long as needed to satisfy these requirements.
-
- e) Convey the object code using peer-to-peer transmission, provided
- you inform other peers where the object code and Corresponding
- Source of the work are being offered to the general public at no
- charge under subsection 6d.
-
- A separable portion of the object code, whose source code is excluded
-from the Corresponding Source as a System Library, need not be
-included in conveying the object code work.
-
- A "User Product" is either (1) a "consumer product", which means any
-tangible personal property which is normally used for personal, family,
-or household purposes, or (2) anything designed or sold for incorporation
-into a dwelling. In determining whether a product is a consumer product,
-doubtful cases shall be resolved in favor of coverage. For a particular
-product received by a particular user, "normally used" refers to a
-typical or common use of that class of product, regardless of the status
-of the particular user or of the way in which the particular user
-actually uses, or expects or is expected to use, the product. A product
-is a consumer product regardless of whether the product has substantial
-commercial, industrial or non-consumer uses, unless such uses represent
-the only significant mode of use of the product.
-
- "Installation Information" for a User Product means any methods,
-procedures, authorization keys, or other information required to install
-and execute modified versions of a covered work in that User Product from
-a modified version of its Corresponding Source. The information must
-suffice to ensure that the continued functioning of the modified object
-code is in no case prevented or interfered with solely because
-modification has been made.
-
- If you convey an object code work under this section in, or with, or
-specifically for use in, a User Product, and the conveying occurs as
-part of a transaction in which the right of possession and use of the
-User Product is transferred to the recipient in perpetuity or for a
-fixed term (regardless of how the transaction is characterized), the
-Corresponding Source conveyed under this section must be accompanied
-by the Installation Information. But this requirement does not apply
-if neither you nor any third party retains the ability to install
-modified object code on the User Product (for example, the work has
-been installed in ROM).
-
- The requirement to provide Installation Information does not include a
-requirement to continue to provide support service, warranty, or updates
-for a work that has been modified or installed by the recipient, or for
-the User Product in which it has been modified or installed. Access to a
-network may be denied when the modification itself materially and
-adversely affects the operation of the network or violates the rules and
-protocols for communication across the network.
-
- Corresponding Source conveyed, and Installation Information provided,
-in accord with this section must be in a format that is publicly
-documented (and with an implementation available to the public in
-source code form), and must require no special password or key for
-unpacking, reading or copying.
-
- 7. Additional Terms.
-
- "Additional permissions" are terms that supplement the terms of this
-License by making exceptions from one or more of its conditions.
-Additional permissions that are applicable to the entire Program shall
-be treated as though they were included in this License, to the extent
-that they are valid under applicable law. If additional permissions
-apply only to part of the Program, that part may be used separately
-under those permissions, but the entire Program remains governed by
-this License without regard to the additional permissions.
-
- When you convey a copy of a covered work, you may at your option
-remove any additional permissions from that copy, or from any part of
-it. (Additional permissions may be written to require their own
-removal in certain cases when you modify the work.) You may place
-additional permissions on material, added by you to a covered work,
-for which you have or can give appropriate copyright permission.
-
- Notwithstanding any other provision of this License, for material you
-add to a covered work, you may (if authorized by the copyright holders of
-that material) supplement the terms of this License with terms:
-
- a) Disclaiming warranty or limiting liability differently from the
- terms of sections 15 and 16 of this License; or
-
- b) Requiring preservation of specified reasonable legal notices or
- author attributions in that material or in the Appropriate Legal
- Notices displayed by works containing it; or
-
- c) Prohibiting misrepresentation of the origin of that material, or
- requiring that modified versions of such material be marked in
- reasonable ways as different from the original version; or
-
- d) Limiting the use for publicity purposes of names of licensors or
- authors of the material; or
-
- e) Declining to grant rights under trademark law for use of some
- trade names, trademarks, or service marks; or
-
- f) Requiring indemnification of licensors and authors of that
- material by anyone who conveys the material (or modified versions of
- it) with contractual assumptions of liability to the recipient, for
- any liability that these contractual assumptions directly impose on
- those licensors and authors.
-
- All other non-permissive additional terms are considered "further
-restrictions" within the meaning of section 10. If the Program as you
-received it, or any part of it, contains a notice stating that it is
-governed by this License along with a term that is a further
-restriction, you may remove that term. If a license document contains
-a further restriction but permits relicensing or conveying under this
-License, you may add to a covered work material governed by the terms
-of that license document, provided that the further restriction does
-not survive such relicensing or conveying.
-
- If you add terms to a covered work in accord with this section, you
-must place, in the relevant source files, a statement of the
-additional terms that apply to those files, or a notice indicating
-where to find the applicable terms.
-
- Additional terms, permissive or non-permissive, may be stated in the
-form of a separately written license, or stated as exceptions;
-the above requirements apply either way.
-
- 8. Termination.
-
- You may not propagate or modify a covered work except as expressly
-provided under this License. Any attempt otherwise to propagate or
-modify it is void, and will automatically terminate your rights under
-this License (including any patent licenses granted under the third
-paragraph of section 11).
-
- However, if you cease all violation of this License, then your
-license from a particular copyright holder is reinstated (a)
-provisionally, unless and until the copyright holder explicitly and
-finally terminates your license, and (b) permanently, if the copyright
-holder fails to notify you of the violation by some reasonable means
-prior to 60 days after the cessation.
-
- Moreover, your license from a particular copyright holder is
-reinstated permanently if the copyright holder notifies you of the
-violation by some reasonable means, this is the first time you have
-received notice of violation of this License (for any work) from that
-copyright holder, and you cure the violation prior to 30 days after
-your receipt of the notice.
-
- Termination of your rights under this section does not terminate the
-licenses of parties who have received copies or rights from you under
-this License. If your rights have been terminated and not permanently
-reinstated, you do not qualify to receive new licenses for the same
-material under section 10.
-
- 9. Acceptance Not Required for Having Copies.
-
- You are not required to accept this License in order to receive or
-run a copy of the Program. Ancillary propagation of a covered work
-occurring solely as a consequence of using peer-to-peer transmission
-to receive a copy likewise does not require acceptance. However,
-nothing other than this License grants you permission to propagate or
-modify any covered work. These actions infringe copyright if you do
-not accept this License. Therefore, by modifying or propagating a
-covered work, you indicate your acceptance of this License to do so.
-
- 10. Automatic Licensing of Downstream Recipients.
-
- Each time you convey a covered work, the recipient automatically
-receives a license from the original licensors, to run, modify and
-propagate that work, subject to this License. You are not responsible
-for enforcing compliance by third parties with this License.
-
- An "entity transaction" is a transaction transferring control of an
-organization, or substantially all assets of one, or subdividing an
-organization, or merging organizations. If propagation of a covered
-work results from an entity transaction, each party to that
-transaction who receives a copy of the work also receives whatever
-licenses to the work the party's predecessor in interest had or could
-give under the previous paragraph, plus a right to possession of the
-Corresponding Source of the work from the predecessor in interest, if
-the predecessor has it or can get it with reasonable efforts.
-
- You may not impose any further restrictions on the exercise of the
-rights granted or affirmed under this License. For example, you may
-not impose a license fee, royalty, or other charge for exercise of
-rights granted under this License, and you may not initiate litigation
-(including a cross-claim or counterclaim in a lawsuit) alleging that
-any patent claim is infringed by making, using, selling, offering for
-sale, or importing the Program or any portion of it.
-
- 11. Patents.
-
- A "contributor" is a copyright holder who authorizes use under this
-License of the Program or a work on which the Program is based. The
-work thus licensed is called the contributor's "contributor version".
-
- A contributor's "essential patent claims" are all patent claims
-owned or controlled by the contributor, whether already acquired or
-hereafter acquired, that would be infringed by some manner, permitted
-by this License, of making, using, or selling its contributor version,
-but do not include claims that would be infringed only as a
-consequence of further modification of the contributor version. For
-purposes of this definition, "control" includes the right to grant
-patent sublicenses in a manner consistent with the requirements of
-this License.
-
- Each contributor grants you a non-exclusive, worldwide, royalty-free
-patent license under the contributor's essential patent claims, to
-make, use, sell, offer for sale, import and otherwise run, modify and
-propagate the contents of its contributor version.
-
- In the following three paragraphs, a "patent license" is any express
-agreement or commitment, however denominated, not to enforce a patent
-(such as an express permission to practice a patent or covenant not to
-sue for patent infringement). To "grant" such a patent license to a
-party means to make such an agreement or commitment not to enforce a
-patent against the party.
-
- If you convey a covered work, knowingly relying on a patent license,
-and the Corresponding Source of the work is not available for anyone
-to copy, free of charge and under the terms of this License, through a
-publicly available network server or other readily accessible means,
-then you must either (1) cause the Corresponding Source to be so
-available, or (2) arrange to deprive yourself of the benefit of the
-patent license for this particular work, or (3) arrange, in a manner
-consistent with the requirements of this License, to extend the patent
-license to downstream recipients. "Knowingly relying" means you have
-actual knowledge that, but for the patent license, your conveying the
-covered work in a country, or your recipient's use of the covered work
-in a country, would infringe one or more identifiable patents in that
-country that you have reason to believe are valid.
-
- If, pursuant to or in connection with a single transaction or
-arrangement, you convey, or propagate by procuring conveyance of, a
-covered work, and grant a patent license to some of the parties
-receiving the covered work authorizing them to use, propagate, modify
-or convey a specific copy of the covered work, then the patent license
-you grant is automatically extended to all recipients of the covered
-work and works based on it.
-
- A patent license is "discriminatory" if it does not include within
-the scope of its coverage, prohibits the exercise of, or is
-conditioned on the non-exercise of one or more of the rights that are
-specifically granted under this License. You may not convey a covered
-work if you are a party to an arrangement with a third party that is
-in the business of distributing software, under which you make payment
-to the third party based on the extent of your activity of conveying
-the work, and under which the third party grants, to any of the
-parties who would receive the covered work from you, a discriminatory
-patent license (a) in connection with copies of the covered work
-conveyed by you (or copies made from those copies), or (b) primarily
-for and in connection with specific products or compilations that
-contain the covered work, unless you entered into that arrangement,
-or that patent license was granted, prior to 28 March 2007.
-
- Nothing in this License shall be construed as excluding or limiting
-any implied license or other defenses to infringement that may
-otherwise be available to you under applicable patent law.
-
- 12. No Surrender of Others' Freedom.
-
- If conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License. If you cannot convey a
-covered work so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you may
-not convey it at all. For example, if you agree to terms that obligate you
-to collect a royalty for further conveying from those to whom you convey
-the Program, the only way you could satisfy both those terms and this
-License would be to refrain entirely from conveying the Program.
-
- 13. Remote Network Interaction; Use with the GNU General Public License.
-
- Notwithstanding any other provision of this License, if you modify the
-Program, your modified version must prominently offer all users
-interacting with it remotely through a computer network (if your version
-supports such interaction) an opportunity to receive the Corresponding
-Source of your version by providing access to the Corresponding Source
-from a network server at no charge, through some standard or customary
-means of facilitating copying of software. This Corresponding Source
-shall include the Corresponding Source for any work covered by version 3
-of the GNU General Public License that is incorporated pursuant to the
-following paragraph.
-
- Notwithstanding any other provision of this License, you have
-permission to link or combine any covered work with a work licensed
-under version 3 of the GNU General Public License into a single
-combined work, and to convey the resulting work. The terms of this
-License will continue to apply to the part which is the covered work,
-but the work with which it is combined will remain governed by version
-3 of the GNU General Public License.
-
- 14. Revised Versions of this License.
-
- The Free Software Foundation may publish revised and/or new versions of
-the GNU Affero General Public License from time to time. Such new versions
-will be similar in spirit to the present version, but may differ in detail to
-address new problems or concerns.
-
- Each version is given a distinguishing version number. If the
-Program specifies that a certain numbered version of the GNU Affero General
-Public License "or any later version" applies to it, you have the
-option of following the terms and conditions either of that numbered
-version or of any later version published by the Free Software
-Foundation. If the Program does not specify a version number of the
-GNU Affero General Public License, you may choose any version ever published
-by the Free Software Foundation.
-
- If the Program specifies that a proxy can decide which future
-versions of the GNU Affero General Public License can be used, that proxy's
-public statement of acceptance of a version permanently authorizes you
-to choose that version for the Program.
-
- Later license versions may give you additional or different
-permissions. However, no additional obligations are imposed on any
-author or copyright holder as a result of your choosing to follow a
-later version.
-
- 15. Disclaimer of Warranty.
-
- THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
-APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
-HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
-OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
-THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
-IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
-ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
-
- 16. Limitation of Liability.
-
- IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
-THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
-GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
-USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
-DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
-PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
-EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGES.
-
- 17. Interpretation of Sections 15 and 16.
-
- If the disclaimer of warranty and limitation of liability provided
-above cannot be given local legal effect according to their terms,
-reviewing courts shall apply local law that most closely approximates
-an absolute waiver of all civil liability in connection with the
-Program, unless a warranty or assumption of liability accompanies a
-copy of the Program in return for a fee.
-
- END OF TERMS AND CONDITIONS
-
- How to Apply These Terms to Your New Programs
-
- If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
- To do so, attach the following notices to the program. It is safest
-to attach them to the start of each source file to most effectively
-state the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
-
- Copyright (C)
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see .
-
-Also add information on how to contact you by electronic and paper mail.
-
- If your software can interact with users remotely through a computer
-network, you should also make sure that it provides a way for users to
-get its source. For example, if your program is a web application, its
-interface could display a "Source" link that leads users to an archive
-of the code. There are many ways you could offer source, and different
-solutions will be better for different programs; see section 13 for the
-specific requirements.
-
- You should also get your employer (if you work as a programmer) or school,
-if any, to sign a "copyright disclaimer" for the program, if necessary.
-For more information on this, and how to apply and follow the GNU AGPL, see
-.
\ No newline at end of file
diff --git a/lib/CardDAV-PHP/README.md b/lib/CardDAV-PHP/README.md
deleted file mode 100644
index fbafef66..00000000
--- a/lib/CardDAV-PHP/README.md
+++ /dev/null
@@ -1,33 +0,0 @@
-Description
------------
-This is a simple CardDAV client written in PHP called "CardDAV PHP".
-
-
-Features
---------
-* Query your CardDAV server with XML response (simplified or raw)
-* Supports many authentication methods
-* You can add / update / delete vCards from your CardDAV server
-* Get single vCards (raw or extended XML) from your CardDAV server
-* Check CardDAV server connection
-* Tested CardDAV servers: DAViCal, Apple Addressbook Server, memotoo, SabreDAV, ownCloud, SOGo
-
-
-Installation
-------------
-* Nothing to install - it's standalone
-* Requires PHP 5.3 and CURL
-* The documentation can be found in the class itself
-
-
-Special thanks
---------------
-* xbgmsharp for the great feedback!
-
-
-Contact
--------
-* Author: Christian Putzke
-* Report feature requests and bugs here: https://github.com/graviox/CardDAV-PHP/issues
-* Visit my website: http://www.graviox.de/
-* Follow me on Twitter: https://twitter.com/cputzke/
\ No newline at end of file
diff --git a/lib/fritzbox_api_php/CHANGELOG.txt b/lib/fritzbox_api_php/CHANGELOG.txt
deleted file mode 100644
index 95030d28..00000000
--- a/lib/fritzbox_api_php/CHANGELOG.txt
+++ /dev/null
@@ -1,79 +0,0 @@
-0.5.0b7 2013-01-02:
----
-
-* added a parameter to the constructor of the api to load a different config file (i.e. new frizbox_api('alt') loads fritzbox_user_alt.conf.php
-* fixed the logfile-logging in all bundled scripts
-
-
-0.5.0b6 2012-12-23:
----
-
-* added a username config item to login with a local user, when user-based login is enabled in the box config
-
-
-0.5.0b5 2012-12-18:
----
-
-* fixed the remoteconfig login code to work with new user system
-* fixed the logger calls to echo the message, when the logger is not initialized
-
-
-0.5.0b4 2012-12-16:
----
-
-* fixed a typo in the remoteconfig login code
-
-
-0.5.0b3 2012-12-09:
----
-
-* changed fritzbox_get_foncallslist.php to work with new firmwares, moved old method to fritzbox_get_foncallslist_xml.php
-
-
-0.5.0b3 2012-12-07:
----
-
-* fixed internal SID login/logut-handling
-
-
-0.5.0b1 2012-12-05:
----
-
-* added support for the new .lua-loginpage in newest Fritz Labor firmwares
-* refactored the whole codebase, introduced a new autoloaded config object, so check the new config file fritzbox_user.conf.php
-
-
-0.4.1 2012-03-15:
----
-
-* added a new script fritzbox_guestlan_on_off.php to switch the new Guest-LAN on and off
-* tested all builtin scripts with new FRITZ!OS 05.20
-
-
-0.4 2011-05-15:
----
-
-* added support for the new .lua forms like the WLAN guest access settings
-
-
-0.3 2010-05-19:
----
-
-* added a doGetRequest method in the API
-* some minor changes
-* a new module for downloading the foncalls list
-
-
-0.2 2010-04-07:
----
-
-* added a capability to use the Fritz!Box remote admin mode
- Important limitation: does not check the certificate, so the password is sent to any computer responding at the configured address.
- Feel free to hack the cURL calls to accept only a given certrificate
-
-
-0.1:
----
-
-* First release
-
diff --git a/lib/fritzbox_api_php/README.md b/lib/fritzbox_api_php/README.md
deleted file mode 100644
index d7a508eb..00000000
--- a/lib/fritzbox_api_php/README.md
+++ /dev/null
@@ -1,80 +0,0 @@
-# FritzBox! PHP API
-
-Provides an interface to your FritzBox! using the webinterface. Thus, no modification the to firmware (aka FritzOS) is required!
-
-### Requirements
-* PHP 5 (tested with 5.3)
-* PHP-curl
-
-Install on Debian/Ubuntu: sudo apt-get install php5-cli php5-curl
-
-### Sample Scripts
-
-* Enable/Disable TAM
-* Download Phone call list
-* Enable/Disable Guest (W)LAN
-* Ring a Phone
-* Reboot
-* Export the Phonebook
-* **Import the Phonebook (only tested with FB 71xx) [not available in the original version]**
-
-**Newer Version available at**: [IPF discussion thread](http://www.ip-phone-forum.de/showthread.php?t=196309)
-
-AUTHORS: [Gregor Nathanael Meyer](http://spackmat.de/spackblog), Karl Glatz, (maybe others)
-
-----------
-
-### Important! ###
-Do not use Windows Notepad as it will break the files due to their UTF-8 coding and UNIX linebreaks. Download a serious editor like Notepad++ instead.
-Also do not use or modify this scripts, if you do not know, what you do. This script is itended for advanced users
-
-
-
-### Basic usage advisory ####
-
-The scripts are well documented, please take a look to understand them.
-This is also the right moment to check the config file "fritzbox.conf.php"!
-
-All files must be in the same directory. If this condition is fulfilled, you don't need to pay any more attention to "fritzbox_api.class.php".
-
-To use "fritzbox_tam_on_off.php", you have to call it in a terminal, it will then explain its usage. This script is a reference implementation of the PHP Fritz!Box API, use it as a base for your own implementations
-
-
-
-### Usage on a Windows system ###
-
-The script works fine on my Windows Vista machine and a simply extraced PHP 5.3.0 distribution (get it at http://windows.php.net/download/, PHP 5.4.0 works as well). The cURL extension and the mbstring extenstion must be enabled in the php.ini and the correct timezone must be set. To do this, copy the php.ini-production to php.ini and uncomment/change the following directives:
-
-extension_dir = "ext"
-extension=php_curl.dll
-extension=php_mbstring.dll
-date.timezone = Europe/Berlin
-
-After you have done that, you can call the script in a terminal:
-
- C:\>path\to\php.exe path\to\fritzbox_tam_on_off.php
-
-i.e. if you extracted PHP to c:\php and the Fritz!Box PHP API to a subfolder named fritz_api, you call it this way:
- C:\>cd php
- C:\php>php fritz_api\fritzbox_tam_on_off.php
-
-If you use the Windows taskplanner, configure the logger to a logfile or to silent mode and call via php-win.exe instead of php.exe. This will prevent the terminal from being opened.
-
-
-
-### Usage on a UNIX system ###
-
-If you use UNIX, you should be familiar with calling a PHP script via the PHP-CLI. If you don't have the PHP-CLI installed, you can edit the $argv part in "fritz_tam_on_off.php" to work with PHP-CGI and $_GET or you hardcode the necessary arguments.
-
-Ensure that the cURL and the mbstring extensions are available and a valid timezone is set. The script was tested on a Ubuntu Server machine with PHP 5.2.
-
-
-
-### Feedback, license, support ###
-You are welcome to send me feedback via email (Gregor [at] der-meyer.de) or visit my personal blog at http://spackmat.de/spackblog (in German)
-
-The whole work is licensed under a Creative Commons cc-by-sa license (http://creativecommons.org/licenses/by-sa/3.0/de/). You are free to use and modify it, even for commercial use. If you redistribute it, you have to ensure my name is kept in the code and you use the same conditions.
-
-Feel free to add (and contribute) other nice scripts. The API class should work with any config item, which is available in the Fritz!Box Web UI. Use Firebug or LiveHeaders to figure out the name of the proper POST-Field.
-
-I won't give any personal support for this script, unless you pay for it. :) I'm a freelance IT-consultant and webdeveloper, loocated in Düsseldorf, Germany.
diff --git a/lib/fritzbox_api_php/README.txt b/lib/fritzbox_api_php/README.txt
deleted file mode 100644
index 1e69fbdb..00000000
--- a/lib/fritzbox_api_php/README.txt
+++ /dev/null
@@ -1,58 +0,0 @@
-### Important! ###
-Do not use Windows Notepad as it will break the files due to their UTF-8 coding and UNIX linebreaks. Download a serious editor like Notepad++ instead.
-Also do not use or modify this scripts, if you do not know, what you do. This script is itended for advanced users
-
-
-### Upgrade advisory for version 0.5.0 and up ####
-
-Version 0.5.0 introduced a new config format with graceful default values, so check the new config file fritzbox_user.conf.php
-
-
-### Basic usage advisory ####
-
-The scripts are well documented, please take a look to understand them.
-This is also the right moment to check the config file "fritzbox.conf.php"!
-
-All files must be in the same directory. If this condition is fulfilled, you don't need to pay any more attention to "fritzbox_api.class.php".
-
-To use "fritzbox_tam_on_off.php", you have to call it in a terminal, it will then explain its usage. This script is a reference implementation of the PHP Fritz!Box API, use it as a base for your own implementations
-
-
-
-### Usage on a Windows system ###
-
-The script works fine on my Windows 7 machine and a simply extraced PHP 5.3 distribution (get it at http://windows.php.net/download/, PHP 5.4 works as well). The cURL extension and the mbstring extenstion must be enabled in the php.ini and the correct timezone must be set. To do this, copy the php.ini-production to php.ini and uncomment/change the following directives:
-
-extension_dir = "ext"
-extension=php_curl.dll
-extension=php_mbstring.dll
-date.timezone = Europe/Berlin
-
-After you have done that, you can call the script in a terminal:
-
- C:\>"path\to\php.exe" "path\to\fritzbox_tam_on_off.php"
-
-i.e. if you extracted PHP to c:\php and the Fritz!Box PHP API to a subfolder named fritz_api, you call it this way:
- C:\>cd php
- C:\php>"php.exe" "fritz_api\fritzbox_tam_on_off.php"
-
-If you use the Windows taskplanner, configure the logger to a logfile or to silent mode and call via php-win.exe instead of php.exe. This will prevent the terminal from being opened.
-
-
-
-### Usage on a UNIX system ###
-
-If you use UNIX, you should be familiar with calling a PHP script via the PHP-CLI. If you don't have the PHP-CLI installed, you can edit the $argv part in "fritz_tam_on_off.php" to work with PHP-CGI and $_GET or you hardcode the necessary arguments.
-
-Ensure that the cURL and the mbstring extensions are available and a valid timezone is set. The script was tested on a Ubuntu Server machine with PHP 5.3.
-
-
-
-### Feedback, license, support ###
-You are welcome to send me feedback via email (Gregor [at] der-meyer.de) or visit my personal blog at http://spackmat.de/spackblog (in German)
-
-The whole work is licensed under a Creative Commons cc-by-sa license (http://creativecommons.org/licenses/by-sa/3.0/de/). You are free to use and modify it, even for commercial use. If you redistribute it, you have to ensure my name is kept in the code and you use the same conditions.
-
-Feel free to add (and contribute) other nice scripts. The API class should work with any config item, which is available in the Fritz!Box Web UI. Use Firebug or LiveHeaders to figure out the name of the proper POST-Field.
-
-I won't give any personal support for this script, unless you pay for it. :) I'm a freelance IT-consultant and webdeveloper, loocated in Düsseldorf, Germany.
\ No newline at end of file
diff --git a/lib/fritzbox_api_php/fritzbox_delete_foncallslist.php b/lib/fritzbox_api_php/fritzbox_delete_foncallslist.php
deleted file mode 100644
index 01cb055d..00000000
--- a/lib/fritzbox_api_php/fritzbox_delete_foncallslist.php
+++ /dev/null
@@ -1,50 +0,0 @@
-
- * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
- * @version 0.1 2015-06-11
- * @package Fritz!Box PHP tools
- */
-
-try
-{
- // load the fritzbox_api class
- require_once('fritzbox_api.class.php');
- $fritz = new fritzbox_api();
-
- // init the output message
- $message = date('Y-m-d H:i') . ' ';
-
- // delete calllist form fields
- $formfields = array(
- 'getpage' => '/fon_num/foncalls_list.lua',
- 'usejournal' => '1',
- 'callstab' => 'all',
- 'submit' => 'clear',
- 'clear' => '1',
- );
-
- $fritz->doPostForm($formfields);
- // set a log message
- $message .= 'Call list sucessfully deleted';
-}
-catch (Exception $e)
-{
- $message .= $e->getMessage();
-}
-
-// log the result
-if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
-{
- $fritz->logMessage($message);
-}
-else
-{
- echo($message);
-}
-$fritz = null; // destroy the object to log out
-?>
diff --git a/lib/fritzbox_api_php/fritzbox_diversity0on.php b/lib/fritzbox_api_php/fritzbox_diversity0on.php
deleted file mode 100644
index 21b6ad0d..00000000
--- a/lib/fritzbox_api_php/fritzbox_diversity0on.php
+++ /dev/null
@@ -1,37 +0,0 @@
- '../html/de/menus/menu2.html',
- 'telcfg:settings/Diversity' . $diversity . '/Active' => $mode,
- );
- $fritz->doPostForm($formfields);
-}
-catch (Exception $e)
-{
- $message .= $e->getMessage();
-}
-
-// log the result
-if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
-{
- $fritz->logMessage($message);
-}
-else
-{
- echo($message);
-}
-$fritz = null; // destroy the object to log out
-?>
\ No newline at end of file
diff --git a/lib/fritzbox_api_php/fritzbox_get_foncallslist.php b/lib/fritzbox_api_php/fritzbox_get_foncallslist.php
deleted file mode 100644
index b7232500..00000000
--- a/lib/fritzbox_api_php/fritzbox_get_foncallslist.php
+++ /dev/null
@@ -1,73 +0,0 @@
-
- * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
- * @version 0.4 2013-01-02
- * @package Fritz!Box PHP tools
- */
-
-try
-{
- // load the fritzbox_api class
- require_once('fritzbox_api.class.php');
- $fritz = new fritzbox_api();
-
- // init the output message
- $message = date('Y-m-d H:i') . ' ';
-
- if ( !$fritz->config->getItem('foncallslist_path') )
- {
- throw new Exception('Mandatory config Item foncallslist_path not set.');
- }
- if ( ( file_exists($fritz->config->getItem('foncallslist_path')) && !is_writable($fritz->config->getItem('foncallslist_path')) ) || ( !file_exists($fritz->config->getItem('foncallslist_path')) && !is_writable(dirname($fritz->config->getItem('foncallslist_path'))) ) )
- {
- throw new Exception('Config item foncallslist_path (' . $fritz->config->getItem('foncallslist_path') . ') is not writeable.');
- }
-
- // get the phone calls list
- $params = array(
- //'getpage' => '../html/de/home/foncallsdaten.xml',
- //'getpage' => '../html/de/FRITZ!Box_Anrufliste.csv',
- 'getpage' => '/fon_num/foncalls_list.lua',
- );
- $fritz->doGetRequest($params);
-
- // get the phone calls list
- $params = array(
- //'getpage' => '../html/de/home/foncallsdaten.xml',
- //'getpage' => '../html/de/FRITZ!Box_Anrufliste.csv',
- 'getpage' => '/fon_num/foncalls_list.lua',
- 'csv' => '',
- );
- $output = $fritz->doGetRequest($params);
-
- // write out the call list to the desired path
- file_put_contents($fritz->config->getItem('foncallslist_path'), $output);
-
- // set a log message
- $message .= 'Call list sucessfully downloaded';
-}
-catch (Exception $e)
-{
- $message .= $e->getMessage();
-}
-
-// log the result
-if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
-{
- $fritz->logMessage($message);
-}
-else
-{
- echo($message);
-}
-$fritz = null; // destroy the object to log out
-?>
\ No newline at end of file
diff --git a/lib/fritzbox_api_php/fritzbox_get_foncallslist_xml.php b/lib/fritzbox_api_php/fritzbox_get_foncallslist_xml.php
deleted file mode 100644
index 8d3ed3bf..00000000
--- a/lib/fritzbox_api_php/fritzbox_get_foncallslist_xml.php
+++ /dev/null
@@ -1,72 +0,0 @@
-
- * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
- * @version 0.3 2013-01-02
- * @package Fritz!Box PHP tools
- */
-
-try
-{
- // load the fritzbox_api class
- require_once('fritzbox_api.class.php');
- $fritz = new fritzbox_api();
-
- // init the output message
- $message = date('Y-m-d H:i') . ' ';
-
- if ( !$fritz->config->getItem('foncallslist_path') )
- {
- throw new Exception('Mandatory config Item foncallslist_path not set.');
- }
- if ( ( file_exists($fritz->config->getItem('foncallslist_path')) && !is_writable($fritz->config->getItem('foncallslist_path')) ) || ( !file_exists($fritz->config->getItem('foncallslist_path')) && !is_writable(dirname($fritz->config->getItem('foncallslist_path'))) ) )
- {
- throw new Exception('Config item foncallslist_path (' . $fritz->config->getItem('foncallslist_path') . ') is not writeable.');
- }
-
- // get the frontend-page to refresh the list
- $params = array(
- 'getpage' => '../html/de/menus/menu2.html',
- 'var:menu' => 'fon',
- 'var:pagename' => 'foncalls',
- 'var:errorpagename' => 'foncalls',
- 'var:type' => '0',
- );
- $fritz->doPostForm($params);
-
- // get the phone calls list
- $params = array(
- 'getpage' => '../html/de/home/foncallsdaten.xml',
- );
- $output = $fritz->doGetRequest($params);
-
- // write out the call list to the desired path
- file_put_contents($fritz->config->getItem('foncallslist_path'), $output);
-
- // set a log message
- $message .= 'Call list sucessfully downloaded';
-}
-catch (Exception $e)
-{
- $message .= $e->getMessage();
-}
-
-// log the result
-if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
-{
- $fritz->logMessage($message);
-}
-else
-{
- echo($message);
-}
-$fritz = null; // destroy the object to log out
-?>
\ No newline at end of file
diff --git a/lib/fritzbox_api_php/fritzbox_guestlan_on_off.php b/lib/fritzbox_api_php/fritzbox_guestlan_on_off.php
deleted file mode 100644
index 1f5e9bac..00000000
--- a/lib/fritzbox_api_php/fritzbox_guestlan_on_off.php
+++ /dev/null
@@ -1,127 +0,0 @@
-
- * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
- * @version 0.3 2013-01-02
- * @package Fritz!Box PHP tools
- */
-
-try
-{
- // load the fritzbox_api class
- require_once('fritzbox_api.class.php');
- $fritz = new fritzbox_api();
-
- // init the output message
- $message = date('Y-m-d H:i') . ' ';
-
- // handle the CLI arguments or give a help message
- if (isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1) )
- {
- $mode = (bool)$argv[1];
- }
- else
- {
- if ( $fritz->config->getItem('logging') == 'console' )
- {
- echo '
- Enables or disables the LAN4 guest access of a Fritz!Box
-
- Usage on UNIX systems:
- /path/to/php ' . $argv[0] . ' {0|1}
-
- Usage on Windows systems:
- "c:\path\to\php.exe" ' . $argv[0] . ' {0|1}
-
- 0 disables the guest access
- 1 enables the guest access
- ';
- }
- else
- {
- $fritz->logMessage($message . 'ERROR: Script was called without or with an invalid argument');
- }
- exit;
- }
-
-
- // read the current settings
- $formfields = array(
- 'getpage' => '/net/network_settings.lua',
- );
- $output = $fritz->doGetRequest($formfields);
-
-
- // read time_server_activ setting
- preg_match('@name="time_server_activ"[^>]+(checked)[^>]*@', $output, $matches);
- if ( isset($matches[1]) )
- {
- $formfields['time_server_activ'] = 'on';
- }
- // read time_server setting
- preg_match('@name="time_server"[^>]+value="([^"]*)"[^>]*@', $output, $matches);
- $formfields['time_server'] = isset($matches[1]) ? $matches[1] : '0.europe.pool.ntp.org';
- // read other_prefix_allowed setting
- preg_match('@name="other_prefix_allowed"[^>]+(checked)[^>]*@', $output, $matches);
- if ( isset($matches[1]) )
- {
- $formfields['other_prefix_allowed'] = 'on';
- }
- // read dnsv6_server_activ setting
- preg_match('@name="dnsv6_server_activ"[^>]+(checked)[^>]*@', $output, $matches);
- if ( isset($matches[1]) )
- {
- $formfields['dnsv6_server_activ'] = 'on';
- }
-
- // set new given setting
- if ( $mode == true )
- {
- $formfields['guest_enabled'] = 'on';
- }
-
- // do the update
- $formfields['btnSave'] = '';
- $output = $fritz->doPostForm($formfields);
-
- preg_match('@name="guest_enabled"[^>]+(checked)[^>]*@', $output, $matches);
- if ( isset($matches[1]) && $mode == true )
- {
- $message .= 'LAN4 guest access is now active.';
- }
- else if ( !isset($matches[1]) && $mode == false )
- {
- $message .= 'LAN4 guest access is now inactive.';
- }
- else if ( isset($matches[1]) && $mode == false )
- {
- $message .= 'ERROR: LAN4 guest access status change failed, should be inactive, but is still active.';
- }
- else if ( !isset($matches[1]) && $mode == true )
- {
- $message .= 'ERROR: LAN4 guest access status change failed, should be active, but is still inactive.';
- }
-}
-catch (Exception $e)
-{
- $message .= $e->getMessage();
-}
-
-// log the result
-if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
-{
- $fritz->logMessage($message);
-}
-else
-{
- echo($message);
-}
-$fritz = null; // destroy the object to log out
-?>
\ No newline at end of file
diff --git a/lib/fritzbox_api_php/fritzbox_guestwlan_on_off.php b/lib/fritzbox_api_php/fritzbox_guestwlan_on_off.php
deleted file mode 100644
index f009496f..00000000
--- a/lib/fritzbox_api_php/fritzbox_guestwlan_on_off.php
+++ /dev/null
@@ -1,143 +0,0 @@
-
- * @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
- * @version 0.3 2013-01-02
- * @package Fritz!Box PHP tools
- */
-
-try
-{
- // load the fritzbox_api class
- require_once('fritzbox_api.class.php');
- $fritz = new fritzbox_api();
-
- // init the output message
- $message = date('Y-m-d H:i') . ' ';
-
- // handle the CLI arguments or give a help message
- if (isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1) )
- {
- $mode = (bool)$argv[1];
- }
- else
- {
- if ( $fritz->config->getItem('logging') == 'console' )
- {
- echo '
- Enables or disables the WLAN guest access of a Fritz!Box
-
- Usage on UNIX systems:
- /path/to/php ' . $argv[0] . ' {0|1} [optional: PASSWORD]
-
- Usage on Windows systems:
- c:\path\to\php.exe ' . $argv[0] . ' {0|1} [optional: PASSWORD]
-
- 0 disables the guest access
- 1 enables the guest access
-
- The optional argument PASSWORD sets a new guest access password (min 8 chars)
- Defaults to false, so the current password is kept.
- ';
- }
- else
- {
- $fritz->logMessage($message . 'ERROR: Script was called without or with an invalid argument');
- }
- exit;
- }
- $wpa_key = (isset($argv[2]) && strlen($argv[2]) >= 8 && strlen($argv[2]) <= 63) ? $argv[2] : false;
-
-
- // read the current settings
- $formfields = array(
- 'getpage' => '/wlan/guest_access.lua',
- );
- $output = $fritz->doGetRequest($formfields);
-
- // read down_time_activ setting
- preg_match('@name="down_time_activ"[^>]+(checked)[^>]*@', $output, $matches);
- if ( isset($matches[1]) )
- {
- $formfields['down_time_activ'] = 'on';
- }
- // read down_time_value setting
- preg_match('@name="down_time_value".*?
';
-
- if ($vCard -> PHOTO)
- {
- foreach ($vCard -> PHOTO as $Photo)
- {
- if ($Photo['encoding'] == 'b')
- {
- echo ' ';
- }
- else
- {
- echo ' ';
- }
-
- /*
- // It can also be saved to a file
- try
- {
- $vCard -> SaveFile('photo', 0, 'test_image.jpg');
- // The parameters are:
- // - name of the file we want to save (photo, logo or sound)
- // - index of the file in case of multiple files (defaults to 0)
- // - target path to save to, including the filenam
- }
- catch (Exception $E)
- {
- // Target path not writable
- }
- */
- }
- }
-
- foreach ($vCard -> N as $Name)
- {
- echo '