Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jens-maus committed Aug 24, 2014
1 parent ab6f7b0 commit d795619
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 59 deletions.
111 changes: 54 additions & 57 deletions carddav2fb.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Martin Rost
* Jens Maus <mail@jens-maus.de>
*
* version 1.4 2014-08-22
* version 1.5 2014-08-25
*
*/
error_reporting(E_ALL);
Expand Down Expand Up @@ -61,8 +61,8 @@

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 phonebook!!)
print 'Upload to fritzbox at '.$config['fritzbox_ip'].'...';
// upload the XML-file to the FRITZ!Box (CAUTION: this will overwrite all current entries in the phone book!!)
print 'Upload to FRITZ!Box at '.$config['fritzbox_ip'].'...';
$ul_msg = $client->upload_to_fb();
print 'Done.'.PHP_EOL;
print 'FritzBox: '.$ul_msg.PHP_EOL;
Expand Down Expand Up @@ -96,9 +96,15 @@ public function base64_to_jpeg( $inputfile, $outputfile ) {
public function get_carddav_entries() {
$entries = array();

// Perform an FTP-connection to copy over the photos to a specified directory
// format output of messages (info, debug, error)
$message_info = "[\033[32m INFO \033[0m] ";
$message_successful = "[\033[32m OK \033[0m] ";
$message_debug = "[\033[36m DEBUG \033[0m] ";
$message_error = "[\033[31m ERROR \033[0m] ";

// perform an ftps-connection to copy over the photos to a specified directory
$ftp_server = $this->config['fritzbox_ip_ftp'];
$conn_id = ftp_connect($ftp_server);
$conn_id = ftp_ssl_connect($ftp_server);
$login_result = ftp_login($conn_id, $this->config['fritzbox_user'], $this->config['fritzbox_pw']);
ftp_pasv($conn_id, true);

Expand All @@ -107,12 +113,12 @@ public function get_carddav_entries() {
$carddav->set_auth($conf['user'], $conf['pw']);
$xmldata = $carddav->get();

// convert everything to utf-8
// convert everything to utf-8 (for OpenXchange/Appsuite)
//$xmldata = utf8_encode($xmldata);

// DEBUG: writes an XML file of the addressbook to the server from which it will be execute
// DEBUG: writes an XML-file of the addressbook to the server from where the script is executed
/*
$filename = $conf['user']."-".basename($conf['url']) . ".xml";
$filename = $conf['user']."_".basename($conf['url']) . ".xml";
print PHP_EOL."Save whole addressbook as xml file: ".$filename;
file_put_contents($filename, $xmldata);
*/
Expand All @@ -135,19 +141,18 @@ public function get_carddav_entries() {

// name
$name_arr = $vcard_obj->n[0];
//$name = $this->_concat($name_arr['Prefixes'],$this->_concat($this->_concat($name_arr['LastName'],$name_arr['FirstName']),$name_arr['AdditionalNames']));
$name = $this->_concat($this->_concat($name_arr['LastName'],$name_arr['FirstName']),$name_arr['AdditionalNames']);

// if name is empty we take organization instead
if(empty($name))
{
if(empty($name)) {
$name_arr = $vcard_obj->org[0];
$name = $name_arr['Name'];
}

// 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'),$name);
array('','_','_','ae','oe','ue','Ae','Oe','Ue','ss','a','a','o','o','u','u','i'),$name);
} else {
$photo = '';
}
Expand All @@ -163,43 +168,35 @@ public function get_carddav_entries() {
// e-mail addresses
$email_add = array();

// retrieve photos, save them as jpg and put them via ftp to the fritzbox
// retrieve photos, save them as jpg and put them via ftp to the FRITZ!Box
if ($vcard_obj->photo) {
// get photos, rename and save as xml
$photo_jpg = $vcard_obj->photo;
$tempfile = basename($photo).".xml";
file_put_contents($tempfile, $photo_jpg[0]['Value']);

// convert base64 representation to jpg and delete tempfile afterwards
$this->base64_to_jpeg($tempfile, $photo.".jpg");
unlink($tempfile);

// copy photos via ftp to the fritzbox
$file = $photo.".jpg";
$remote_path = $this->config['usb_disk']."/FRITZ/fonpix";
$remote_file = $photo.".jpg";

// check if contact photo already exists
//$contents_on_server = ftp_nlist($conn_id, $remote_path);
//$check_file_exist = $remote_path."/".$remote_file;

//if (in_array($check_file_exist, $contents_on_server)) {
// unlink($file);
//}
//else {
// upload photo file. If successfull delete afterwards
if (ftp_put($conn_id, $remote_path."/".$file, $remote_file, FTP_BINARY)) {
unlink($file);
} else {
echo "While uploading file ".$file." an error occurred.".PHP_EOL;
}
//}
// get photos, rename and save as xml
$photo_jpg = $vcard_obj->photo;
$tempfile = basename($photo).".xml";
echo PHP_EOL.$message_info."Saving image as XML: ".$tempfile;
file_put_contents($tempfile, $photo_jpg[0]['Value']);

// convert base64 representation to jpg and delete tempfile afterwards
$this->base64_to_jpeg($tempfile, $photo.".jpg");
unlink($tempfile);

// copy photos via ftp to the FRITZ!Box
$file = $photo.".jpg";
$remote_path = $this->config['usb_disk']."/FRITZ/fonpix";
$remote_file = $photo.".jpg";

// upload photo file. If successful delete afterwards
if (ftp_put($conn_id, $remote_path."/".$file, $remote_file, FTP_BINARY)) {
echo PHP_EOL.$message_successful."Successful upload of photo: ".$file;
unlink($file);
} else {
echo PHP_EOL.$message_error."While uploading file ".$file." an error occurred.".PHP_EOL;
}
}

if (in_array($this->config['group_vip'],$categories)) {
$vip = 1;
}
else {
} else {
$vip = 0;
}

Expand All @@ -222,8 +219,7 @@ public function get_carddav_entries() {
if (empty($t['Type'])) {
$type = "mobile";
$phone_number = $t;
}
else {
} else {
$phone_number = $t['Value'];

$typearr_lower = unserialize(strtolower(serialize($t['Type'])));
Expand All @@ -246,6 +242,9 @@ public function get_carddav_entries() {
elseif (in_array("other", $typearr_lower)) {
$type = "other";
}
elseif (in_array("dom", $typearr_lower)) {
$type = "other";
}
else {
continue;
}
Expand All @@ -259,8 +258,7 @@ public function get_carddav_entries() {
if (empty($e['Type'])) {
$type_email = "work";
$email = $e;
}
else {
} else {
$email = $e['Value'];
$typearr_lower = unserialize(strtolower(serialize($e['Type'])));
if (in_array("work", $typearr_lower)) {
Expand Down Expand Up @@ -305,10 +303,10 @@ public function build_fb_xml() {
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('<?xml version="1.0" encoding="UTF-8"?><phonebooks><phonebook></phonebook></phonebooks>');
$pb = $root->phonebook;
$pb->addAttribute("name",$this->config['phonebook_name']);
// create FB XML in utf-8 format
$root = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><phonebooks><phonebook></phonebook></phonebooks>');
$pb = $root->phonebook;
$pb->addAttribute("name",$this->config['phonebook_name']);

foreach($this->entries as $entry) {

Expand All @@ -317,10 +315,12 @@ public function build_fb_xml() {
$person = $contact->addChild("person");
$person->addChild("realName", $this->_convert_text($entry['realName']));

// contact photo: set path to contact photo on fritzbox
if (($entry['photo']) and (array_key_exists('usb_disk',$this->config))) {
$person->addChild("imageURL","file:///var/media/ftp/".$this->config['usb_disk']."/FRITZ/fonpix/".$entry['photo'].".jpg");
}

// telephone: put the phonenumbers into the fritzbox xml file
$telephony = $contact->addChild("telephony");
$id = 0;
foreach($entry['telephony'] as $tel) {
Expand All @@ -332,7 +332,7 @@ public function build_fb_xml() {
$id++;
}

// put the email addresses into the fritzbox xml file
// email: put the email addresses into the fritzbox xml file
$email = $contact->addChild("services");
$id = 0;
foreach($entry['email'] as $mail) {
Expand All @@ -346,16 +346,13 @@ public function build_fb_xml() {
$contact->addChild("setup");
$contact->addChild("mod_time", (string)time());
}

$this->fbxml = $root->asXML();

}

public function _convert_text($text) {

$text = htmlspecialchars($text);
//$text = iconv("UTF-8", "ISO-8859-1//IGNORE", $text);

return $text;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/CardDAV-PHP
Submodule CardDAV-PHP updated 1 files
+8 −2 carddav.php
2 changes: 1 addition & 1 deletion lib/fritzbox_api_php

0 comments on commit d795619

Please sign in to comment.