20060528 License: BSD-style # # This small script tries its best to read a "abook"-exported CSV file # and create a suitable "vcf" file with can then be copied to your mobile # phone with a command like "obexftp -b $BT -U synch -p telecom/pb.vcf", # where $BT is the bluetooth address of your mobile phone. Make sure you # backup your old phonebook first and clean the phone memory before synching. # # LICENSE TEXT FOLLOWS: # # Copyright (c) 2006, Thomas Perl # All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of Thomas Perl nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THP AND CONTRIBUTORS ``AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # /* found this here: http://at.php.net/manual/de/function.split.php */ function quotesplit( $splitter=',', $s, $restore_quotes=0 ) { // hack because i'm a bad programmer - replace doubled "s with a ' $s = str_replace('""', "'", $s); //First step is to split it up into the bits that are surrounded by quotes //and the bits that aren't. Adding the delimiter to the ends simplifies //the logic further down $getstrings = explode('"', $splitter.$s.$splitter); //$instring toggles so we know if we are in a quoted string or not $delimlen = strlen($splitter); $instring = 0; while (list($arg, $val) = each($getstrings)) { if ($instring==1) { if( $restore_quotes ) { //Add the whole string, untouched to the previous value in the array $result[count($result)-1] = $result[count($result)-1].'"'.$val.'"'; } else { //Add the whole string, untouched to the array $result[] = $val; } $instring = 0; } else { // check that we have data between multiple $splitter delimiters if ((strlen($val)-$delimlen-$delimlen) >= 1) { //Break up the string according to the delimiter character //Each string has extraneous delimiters around it (inc the ones we added //above), so they need to be stripped off $temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen ) ); while(list($iarg, $ival) = each($temparray)) { $result[] = trim($ival); } } // else, the next element needing parsing is a quoted string and the comma // here is just a single separator and contains no data, so skip it $instring = 1; } } return $result; } /* Fields and their indexes, as exported by "abook" */ $fields = array(); $fields['NAME'] = 0; $fields['EMAIL'] = 1; $fields['ADDRESS'] = 2; $fields['ADDRESS2'] = 3; $fields['CITY'] = 4; $fields['STATE'] = 5; $fields['ZIP'] = 6; $fields['COUNTRY'] = 7; $fields['PHONE'] = 8; $fields['WORKPHONE'] = 9; $fields['FAX'] = 10; $fields['MOBILEPHONE'] = 11; $fields['NICK'] = 12; $fields['URL'] = 13; $fields['NOTES'] = 14; $fields['CUSTOM1'] = 15; $fields['CUSTOM2'] = 16; $fields['CUSTOM3'] = 17; $fields['CUSTOM4'] = 18; $fields['CUSTOM5'] = 19; /* check parameter count - output will be to stdout */ if( count( $argv) < 2) { printf( "usage: php %s filename.csv >outfile.vcf\n", $argv[0]); exit( 1); } /* prints out a vCard, name + number (+number typ) as parameters */ function print_vcard( $vorname, $nachname, $nummer, $typ = 'CELL') { echo "BEGIN:VCARD\n"; echo "VERSION:2.1\n"; echo "N:$nachname;$vorname\n"; echo "TEL;$typ:$nummer\n"; echo "END:VCARD\n"; } $data = explode( "\n", file_get_contents( $argv[1])); foreach( $data as $row) { # ignore comments in the file if( trim( $row)[0] == '#') continue; $rowdata = quotesplit( ',', $row); $name = $rowdata[$fields['NAME']]; # empty line or no data? skip! if( trim( $name) == '') continue; # extract name and split in first and family name $name = explode( ' ', $name); $vorname = $name[0]; $nachname = $name[1]; # get mobile phone number and print first vcard with mobile number $mobile = $rowdata[$fields['MOBILEPHONE']]; print_vcard( $vorname, $nachname, $mobile); # get "at home" number and print another vcard, with different name $phone = $rowdata[$fields['PHONE']]; if( $phone != "'" /* --> "'" should be "", blame quotesplit() .. hehe */) { print_vcard( $vorname, $nachname.' (Festnetz)', $phone, 'HOME'); } } ?>