Created
January 26, 2013 12:46
-
-
Save managementboy/4642109 to your computer and use it in GitHub Desktop.
// 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 distr…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ("./a.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