From 6024c9331b581317bbb09f5ba15259e31ccbe504 Mon Sep 17 00:00:00 2001 From: Miles Lott Date: Sat, 31 Jan 2004 19:48:34 +0000 Subject: [PATCH] Adjust banner, cleanup formatting and use foreach() where possible --- phpgwapi/inc/class.vcard.inc.php | 205 +++++++++++++++---------------- 1 file changed, 100 insertions(+), 105 deletions(-) diff --git a/phpgwapi/inc/class.vcard.inc.php b/phpgwapi/inc/class.vcard.inc.php index 82b0e9ccd4..82a71db278 100644 --- a/phpgwapi/inc/class.vcard.inc.php +++ b/phpgwapi/inc/class.vcard.inc.php @@ -1,12 +1,9 @@ * + * eGroupWare API - VCard import/export class * + * This file written by Miles Lott * * Parse vcards->contacts class fields, and vice-versa * - * Copyright (C) 2001 Miles Lott * - * -------------------------------------------------------------------------* - * This library is part of the phpGroupWare API * - * http://www.phpgroupware.org/api * + * Copyright (C) 2001-2004 Miles Lott * * ------------------------------------------------------------------------ * * This library is free software; you can redistribute it and/or modify it * * under the terms of the GNU Lesser General Public License as published by * @@ -67,8 +64,8 @@ 'email_home_type' => 'EMAIL;TYPE;HOME', 'adr_one_street' => 'ADR;WORK;STREET', - 'adr_one_locality' => 'ADR;WORK;LOCALITY', - 'adr_one_region' => 'ADR;WORK;REGION', + 'adr_one_locality' => 'ADR;WORK;LOCALITY', + 'adr_one_region' => 'ADR;WORK;REGION', 'adr_one_postalcode' => 'ADR;WORK;POSTALCODE', 'adr_one_countryname' => 'ADR;WORK;COUNTRYNAME', 'address2' => 'EXT', @@ -116,27 +113,26 @@ */ function in_file($filename='') { - if (!$filename) + if(!$filename) { return array(); } $buffer = array(); - + $fp = fopen($filename,'r'); - while ($data = fgets($fp,8000)) + while($data = fgets($fp,8000)) { list($name,$value,$extra) = split(':', $data); - if (substr($value,0,5) == 'http') + if(substr($value,0,5) == 'http') { $value = $value . ':'.$extra; } - if ($name && $value) + if($name && $value) { - reset($this->import); - while ( list($fname,$fvalue) = each($this->import) ) + foreach($this->import as $fname => $fvalue) { - if ( strstr(strtolower($name), $this->import[$fname]) ) + if(strstr(strtolower($name), $this->import[$fname])) { $value = trim($value); $value = str_replace('=0D=0A',"\n",$value); @@ -163,7 +159,7 @@ } else { - return (str_replace('=0D=0A',"\n",$string)); + return(str_replace('=0D=0A',"\n",$string)); } } @@ -198,7 +194,7 @@ 'address3' => 'address3' ); - while (list($fname,$fvalue) = each($all_fields)) + foreach($all_fields as $fname => $fvalue) { if($buffer[$fname]) { @@ -217,7 +213,8 @@ function _parse_in($buffer) { /* Following is a lot of pain and little magic */ - while ( list($name,$value) = @each($buffer) ) + settype($buffer,'array'); + foreach($buffer as $name => $value) { $field = split(';',$name); $field[0] = ereg_replace("A\.",'',$field[0]); @@ -225,10 +222,10 @@ $field[0] = ereg_replace("C\.",'',$field[0]); $field[0] = ereg_replace("D\.",'',$field[0]); $values = split(';',$value); - if ($field[1]) + if($field[1]) { //echo $field[0]; - switch ($field[0]) + switch($field[0]) { case 'LABEL': $entry['label'] = $this->decode($values[0]); @@ -237,17 +234,17 @@ $entry['note'] = $this->decode($values[0]); break; case 'ADR': - switch ($field[1]) + switch($field[1]) { case 'INTL': - switch ($field[2]) + switch($field[2]) { case 'WORK': - if ( !stristr($buffer['adr_one_type'],$field[1])) + if(!stristr($buffer['adr_one_type'],$field[1])) { $entry['adr_one_type'] .= 'intl;'; } - if (!$buffer['adr_one_street']) + if(!$buffer['adr_one_street']) { $entry['address2'] = $values[1]; $entry['adr_one_street'] = $values[2]; @@ -258,11 +255,11 @@ } break; case 'HOME': - if ( !stristr($buffer['adr_two_type'],$field[1]) ) + if(!stristr($buffer['adr_two_type'],$field[1])) { $entry['adr_two_type'] .= 'intl;'; } - if (!$buffer['adr_two_street']) + if(!$buffer['adr_two_street']) { $entry['adr_two_street'] = $values[2]; $entry['adr_two_locality'] = $values[3]; @@ -276,14 +273,14 @@ } break; case 'DOM': - switch ($field[2]) + switch($field[2]) { case 'WORK': - if ( !stristr($buffer['adr_one_type'],$field[1])) + if(!stristr($buffer['adr_one_type'],$field[1])) { $entry['adr_one_type'] .= 'dom;'; } - if (!$buffer['adr_one_street']) + if(!$buffer['adr_one_street']) { $entry['address2'] = $values[1]; $entry['adr_one_street'] = $values[2]; @@ -294,11 +291,11 @@ } break; case 'HOME': - if ( !stristr($buffer['adr_two_type'],$field[1]) ) + if(!stristr($buffer['adr_two_type'],$field[1])) { $entry['adr_two_type'] .= 'dom;'; } - if (!$buffer['adr_two_street']) + if(!$buffer['adr_two_street']) { $entry['adr_two_street'] = $values[2]; $entry['adr_two_locality'] = $values[3]; @@ -312,14 +309,14 @@ } break; case 'PARCEL': - switch ($field[2]) + switch($field[2]) { case 'WORK': - if ( !stristr($buffer['adr_one_type'],$field[1])) + if(!stristr($buffer['adr_one_type'],$field[1])) { $entry['adr_one_type'] .= 'parcel;'; } - if (!$buffer['adr_one_street']) + if(!$buffer['adr_one_street']) { $entry['address2'] = $values[1]; $entry['adr_one_street'] = $values[2]; @@ -330,11 +327,11 @@ } break; case 'HOME': - if ( !stristr($buffer['adr_two_type'],$field[1]) ) + if(!stristr($buffer['adr_two_type'],$field[1])) { $entry['adr_two_type'] .= 'parcel;'; } - if (!$buffer['adr_two_street']) + if(!$buffer['adr_two_street']) { $entry['adr_two_street'] = $values[2]; $entry['adr_two_locality'] = $values[3]; @@ -348,14 +345,14 @@ } break; case 'POSTAL': - switch ($field[2]) + switch($field[2]) { case 'WORK': - if ( !stristr($buffer['adr_one_type'],$field[1])) + if(!stristr($buffer['adr_one_type'],$field[1])) { $entry['adr_one_type'] .= 'postal;'; } - if (!$buffer['adr_one_street']) + if(!$buffer['adr_one_street']) { $entry['address2'] = $values[1]; $entry['adr_one_street'] = $values[2]; @@ -366,11 +363,11 @@ } break; case 'HOME': - if ( !stristr($buffer['adr_two_type'],$field[1]) ) + if(!stristr($buffer['adr_two_type'],$field[1])) { $entry['adr_two_type'] .= 'postal;'; } - if (!$buffer['adr_two_street']) + if(!$buffer['adr_two_street']) { $entry['adr_two_street'] = $values[2]; $entry['adr_two_locality'] = $values[3]; @@ -384,7 +381,7 @@ } break; case 'WORK': - if (!$buffer['adr_one_street']) + if(!$buffer['adr_one_street']) { $entry['address2'] = $values[1]; $entry['adr_one_street'] = $values[2]; @@ -402,7 +399,7 @@ $entry['adr_two_countryname'] = $values[6]; break; default: - if (!$buffer['adr_one_street']) + if(!$buffer['adr_one_street']) { $entry['address2'] = $values[1]; $entry['adr_one_street'] = $values[2]; @@ -415,106 +412,105 @@ } break; case 'TEL': // RB 2001/05/07 added for Lotus Organizer ueses TEL;{WORK|HOME};{VOICE|FAX}[;PREF] - if ($field[2] == 'FAX' && ($field[1] == 'WORK' || $field[i] == 'HOME')) + if($field[2] == 'FAX' && ($field[1] == 'WORK' || $field[i] == 'HOME')) { - /* TODO This is PHP4 only */ array_shift($field); // --> ignore the WORK or HOME if FAX follows, HOME;FAX and HOME;TEL are maped later } - switch ($field[1]) + switch($field[1]) { case 'PREF': //echo $field[2].' is preferred'; - if ($field[2]) + if($field[2]) { $buffer['tel_prefer'] .= strtolower($field[2]) . ';'; } break; case 'WORK': // RB don't overwrite TEL;WORK;VOICE (main nr.) with TEL;WORK, TEL;WORK --> tel_isdn $entry[$buffer['tel_work'] ? 'tel_isdn' : 'tel_work'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'HOME': // RB don't overwrite TEL;HOME;VOICE (main nr.) with TEL;HOME, TEL;HOME --> ophone $entry[$buffer['tel_home'] ? 'ophone' : 'tel_home' ] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'VOICE': $entry['tel_voice'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'FAX': - if ($entry['tel_fax']) + if($entry['tel_fax']) { // RB don't overwrite TEL;WORK;FAX with TEL;HOME;FAX, TEL;HOME;FAX --> ophone - $entry['ophone'] = $values[0] . ' Fax'; - break; + $entry['ophone'] = $values[0] . ' Fax'; + break; } $entry['tel_fax'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'MSG': $entry['tel_msg'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'CELL': $entry['tel_cell'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'PAGER': $entry['tel_pager'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'BBS': $entry['tel_bbs'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'MODEM': $entry['tel_modem'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'CAR': $entry['tel_car'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'ISDN': $entry['tel_isdn'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } break; case 'VIDEO': $entry['tel_video'] = $values[0]; - if ($field[2] == 'PREF') + if($field[2] == 'PREF') { $entry['tel_prefer'] .= strtolower($field[1]) . ';'; } @@ -524,7 +520,7 @@ } break; case 'EMAIL': - switch ($field[1]) + switch($field[1]) { case 'WORK': $entry['email'] = $values[0]; @@ -539,7 +535,7 @@ { $entry['email_type'] = $field[2]; } - elseif (!$buffer['email']) + elseif(!$buffer['email']) { $entry['email'] = $values[0]; $entry['email_type'] = $field[1]; @@ -556,12 +552,11 @@ } else { - switch ($field[0]) + switch($field[0]) { case 'N': - reset($this->names); $j=0; - while(list($myname,$myval) = each($this->names) ) + foreach($this->names as $myname => $myval) { $namel = 'n_' . $myname; $entry[$namel] = $values[$j]; @@ -616,12 +611,12 @@ $entry['tel_prefer'] = substr($buffer['tel_prefer'],0,-1); $entry['adr_one_type'] = substr($buffer['adr_one_type'],0,-1); $entry['adr_two_type'] = substr($buffer['adr_two_type'],0,-1); - - if (count($street = split("\r*\n",$buffer['adr_one_street'],3)) > 1) + + if(count($street = split("\r*\n",$buffer['adr_one_street'],3)) > 1) { - $entry['adr_one_street'] = $street[0]; // RB 2001/05/08 added for Lotus Organizer to split multiline adresses - $entry['address2'] = $street[1]; - $entry['address3'] = $street[2]; + $entry['adr_one_street'] = $street[0]; // RB 2001/05/08 added for Lotus Organizer to split multiline adresses + $entry['address2'] = $street[1]; + $entry['address3'] = $street[2]; } return $entry; @@ -629,7 +624,8 @@ // Takes an array of contacts class fields/values, turns it into a vcard string: // - // for ($i=0;$ivcard->out($buffer[$i]); // } // @@ -640,26 +636,25 @@ $header .= 'VERSION:2.1' . "\n"; $header .= 'X-EGROUPWARE-FILE-AS:eGroupWare.org' . "\n"; - reset($this->export); - while ( list($name,$value) = each($this->export) ) + foreach($this->export as $name => $value) { - if (!empty($buffer[$value])) + if(!empty($buffer[$value])) { $mult = explode(';',$value); - if (!$mult[1]) + if(!$mult[1]) { // Normal if(strstr($buffer[$value],"\r\n") || strstr($buffer[$value],"\n")) { $buffer[$value] = $this->encode($buffer[$value]); $entry .= $value . ';QUOTED-PRINTABLE:' . $buffer[$value]."\n"; } - elseif ($value == 'BDAY') + elseif($value == 'BDAY') { $tmp = split('/',$buffer[$value]); # 12/31/1969 -> 1969-12-31 - if ($tmp[0]) + if($tmp[0]) { - if (strlen($tmp[0]) == 1) { $tmp[0] = '0'.$tmp[0]; } - if (strlen($tmp[1]) == 1) { $tmp[1] = '0'.$tmp[1]; } + if(strlen($tmp[0]) == 1) { $tmp[0] = '0'.$tmp[0]; } + if(strlen($tmp[1]) == 1) { $tmp[1] = '0'.$tmp[1]; } $entry .= 'BDAY:' . $tmp[2] . '-' . $tmp[0] . '-' . $tmp[1] . "\n"; } } @@ -670,10 +665,10 @@ } else { - switch ($mult[0]) + switch($mult[0]) { case 'N': - switch ($mult[1]) + switch($mult[1]) { case 'PREFIX': $prefix = ';' . $buffer[$value]; @@ -693,7 +688,7 @@ } break; case 'ORG': - switch ($mult[1]) + switch($mult[1]) { case 'NAME': $org_name = $buffer[$value]; @@ -704,18 +699,18 @@ } break; case 'ADR': - switch ($mult[1]) + switch($mult[1]) { case 'TYPE': $types = explode(';',$buffer[$value]); - if ($types[1]) + if($types[1]) { - while ( $type = each($types) ) + while($type = each($types)) { $typei[$i][$mult[2]] .= ';' . strtoupper($type[1]); } } - elseif ($types[0]) + elseif($types[0]) { $typei[$i][$mult[2]] .= ';' . strtoupper($types[0]); } @@ -742,20 +737,20 @@ { case 'PREFER': $prefer = explode(';',$buffer[$value]); - if ($prefer[1]) + if($prefer[1]) { - while ($pref = strtoupper(each($prefer))) + while($pref = strtoupper(each($prefer))) { $prefi[$i][$pref] = ';PREF'; } //echo 'PREF1'; } - elseif ($prefer[0]) + elseif($prefer[0]) { $prefi[$i][strtoupper($prefer[0])] = ';PREF'; //echo 'PREF='.strtoupper($prefer[0]); } - elseif ($buffer[$value]) + elseif($buffer[$value]) { $prefi[$i][$buffer[$value]] = ';PREF'; //echo 'PREF3'; @@ -777,8 +772,8 @@ switch($mult[1]) { case 'TYPE': - if ($mult[2] == 'WORK') { $emailtype = ';' . $buffer[$value]; } - if ($mult[2] == 'HOME') { $hemailtype = ';' . $buffer[$value]; } + if($mult[2] == 'WORK') { $emailtype = ';' . $buffer[$value]; } + if($mult[2] == 'HOME') { $hemailtype = ';' . $buffer[$value]; } break; case 'WORK': $newval = 'A.'.$value; @@ -794,36 +789,36 @@ break; default: break; - } //end switch ($mult[0]) + } //end switch($mult[0]) } //end else - } //end if (!empty) + } //end if(!empty) } //end while - if ($lastname && $firstname) + if($lastname && $firstname) { $entries .= $header; $entries .= 'N:' . $lastname . $firstname . $middle . $prefix . $suffix . "\n"; $entries .= $entry; - if (!$buffer['FN']) + if(!$buffer['FN']) { - if ($lastname || $firstname ) + if($lastname || $firstname) { $entries .= 'FN:' . substr($firstname,1) . ' ' . $lastname . "\n"; } } - if ($org_name || $org_unit) + if($org_name || $org_unit) { $entries .= 'ORG:' . $org_name . $org_unit . "\n"; } $workattr = str_replace('ADR;','',$workattr); $homeattr = str_replace('ADR;','',$homeattr); - if (!$buffer['EXT']) { $buffer['EXT'] = ';'; } - if ($workaddr) + if(!$buffer['EXT']) { $buffer['EXT'] = ';'; } + if($workaddr) { $work = 'A.ADR;' . $workattr . ':;' . $buffer['EXT'] . substr($workaddr,0,-1) . "\n"; - if (!$buffer['LABEL']) + if(!$buffer['LABEL']) { $wlabel = substr($workaddr,0,-1); $wlabel = str_replace(';','=0D=0A',$wlabel); @@ -832,7 +827,7 @@ $wlabel = 'LABEL;WORK;QUOTED-PRINTABLE:' . $wlabel . "\n"; } } - if ($homeaddr) + if($homeaddr) { $home = 'B.ADR;'.$homeattr.':;;'.substr($homeaddr,0,-1)."\n"; $hlabel = substr($homeaddr,0,-1);