Skip to content

Instantly share code, notes, and snippets.

@managementboy
Created April 1, 2013 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save managementboy/5286399 to your computer and use it in GitHub Desktop.
Save managementboy/5286399 to your computer and use it in GitHub Desktop.
FritzBox Adressbook convert from CSV to XML
<?php
// Konvertiert CSV Dateien ins FRITZ!Box XML Telefonbuchdateien
// Copyleft 2011, tomix
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program If not, see <http://www.gnu.org/licenses/>.
// Erwartet wird eine CSV Datei 'adressen_privat.csv' im selben Verzeichnis wie dieses Script
// Aufbau der CSV Datei:
// Die ersten beiden Zeilen werden nicht eingelesen (Titel und Spaltenbeschriftung)
// Folgende Spalten werden ausgelesen (beginne bei 0 mit zählen)
// 0 Nachname
// 1 Vorname
// 5 Telefonnummer home
// 6 Telefonnummer mobile
// 7 Telefonnummer work
// 8 e-mail-Adresse
// Die Bilder müssen im Verzeichnis bilder liegen und der Dateiname muss wie folgt aufgebaut sein:
// nachname_vorname.jpg (kleingeschrieben, evtl. vorhande Leerzeichen im Nachnamen oder Vornamen werden durch "_" ersetzt)
// existiert das Bild im Verzeichnis bilder wird ein Links auf das Bild eingefügt:
// file:///var/InternerSpeicher/FRITZ/fonpix/nachname_vorname.jpg
// Die Bilder müssen manuel auf die FRITZ!Box kopiert werden, z.B. mit #96*7* Telnet einschalten (Ausschalten: #96*8*) und mit wget von einem Server runterladen
$row = 1; // Anzahl der Arrays
$handle = fopen ("./adressen_privat.csv","r"); // Datei zum Lesen öffnen
// zwei Einträge weiter
$data = fgetcsv ($handle, 1000, ",");
$data = fgetcsv ($handle, 1000, ",");
$phonebook = array();
while ( ($data = fgetcsv ($handle, 1000, ",")) !== FALSE ) { // Daten werden aus der Datei in ein Array $data gelesen
// Bild vorhanden
$nameperson = trim($data[0].' '.$data[1]);
$bild = strtr(strtolower($nameperson), ' ', '_').'.jpg';
if( file_exists('./bilder/'.$bild) ) {
$bild = 'file:///var/InternerSpeicher/FRITZ/fonpix/'.$bild;
}
else {
$bild = NULL;
}
$phonebook [] = array(
'category' => '0',
'realName' => $nameperson,
'imageURL' => $bild,
'number_mobile' => $data[6],
'number_home' => $data[5],
'number_work' => $data[7],
'email' => $data[8]
);
}
fclose ($handle);
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$r = $doc->createElement( "phonebook" );
$doc->appendChild( $r );
foreach( $phonebook as $contact )
{
$b = $doc->createElement( "contact" );
$category = $doc->createElement( "category" );
$category->appendChild(
$doc->createTextNode( $contact['category'] )
);
$b->appendChild( $category );
// Knoten person
$c = $doc->createElement( "person" );
$realName = $doc->createElement( "realName" );
$realName->appendChild(
$doc->createTextNode( $contact['realName'] )
);
$c->appendChild( $realName );
$imageURL = $doc->createElement( "imageURL" );
$imageURL->appendChild(
$doc->createTextNode( $contact['imageURL'] )
);
$c->appendChild( $imageURL );
$b->appendChild( $c );
// Knoten telephony
$c = $doc->createElement( "telephony" );
$number = $doc->createElement( "number" );
$number->appendChild(
$doc->createTextNode( $contact['number_mobile'] )
);
$number->setattribute('type', 'mobile');
$number->setattribute('vanity', '');
$number->setattribute('prio', '0');
$c->appendChild( $number );
$number = $doc->createElement( "number" );
$number->appendChild(
$doc->createTextNode( $contact['number_home'] )
);
$number->setattribute('type', 'home');
$number->setattribute('vanity', '');
$number->setattribute('prio', '0');
$c->appendChild( $number );
$number = $doc->createElement( "number" );
$number->appendChild(
$doc->createTextNode( $contact['number_work'] )
);
$number->setattribute('type', 'work');
$number->setattribute('vanity', '');
$number->setattribute('prio', '0');
$c->appendChild( $number );
$b->appendChild( $c );
// Knoten services
$c = $doc->createElement( "services" );
$email = $doc->createElement( "email" );
$email->appendChild(
$doc->createTextNode( $contact['email'] )
);
$email->setattribute('classifier', 'private');
$c->appendChild( $email );
$b->appendChild( $c );
// Knoten setup
$setup = $doc->createElement( "setup" );
$b->appendChild( $setup );
// Knoten mod_time
$mod_time = $doc->createElement( "mod_time" );
$mod_time->appendChild(
$doc->createTextNode( time() )
);
$b->appendChild( $mod_time );
$r->appendChild( $b );
}
echo $doc->saveXML();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment