Index: class.vcard.inc.php
===================================================================
RCS file: /cvsroot/phpgwapi/phpgwapi/inc/class.vcard.inc.php,v
retrieving revision 1.4.4.8
diff -u -r1.4.4.8 class.vcard.inc.php
--- class.vcard.inc.php	11 Mar 2004 01:27:40 -0000	1.4.4.8
+++ class.vcard.inc.php	5 Apr 2004 18:11:29 -0000
@@ -684,40 +684,316 @@
 					$entries .= 'FN;ENCODING=QUOTED-PRINTABLE:' . $fn . "\r\n";
 				}
 			}
-			if ($org_name || $org_unit)
+
+			if(isset($org_name) && ($org_name!=''))
 			{
+				$org = $org_name;
+				if (isset($org_unit) && ($org_unit!=''))
+					$org .= $org_unit;
 				$entries .= 'ORG;ENCODING=QUOTED-PRINTABLE:' . $org . "\r\n";
 			}
+			else
+			{
+				if(isset($org_unit) && ($org_unit!=''))
+					$entries .= 'ORG;ENCODING=QUOTED-PRINTABLE:' . $org_unit . "\r\n";
+			}
+			
 
-			$workattr = ereg_replace('ADR;','',$workattr);
-			// remember the correct order of address fields!
-			$workaddr['POSTOFFICEBOX'] = $this->vCard_encode($workaddr['POSTOFFICEBOX'], 'QUOTED-PRINTABLE', false);
-			$workaddr['EXT']           = $this->vCard_encode($workaddr['EXT'], 'QUOTED-PRINTABLE', false);
-			$workaddr['STREET']        = $this->vCard_encode($workaddr['STREET'], 'QUOTED-PRINTABLE', false);
-			$workaddr['LOCALITY']      = $this->vCard_encode($workaddr['LOCALITY'], 'QUOTED-PRINTABLE', false);
-			$workaddr['REGION']        = $this->vCard_encode($workaddr['REGION'], 'QUOTED-PRINTABLE', false);
-			$workaddr['POSTALCODE']    = $this->vCard_encode($workaddr['POSTALCODE'], 'QUOTED-PRINTABLE', false);
-			$workaddr['COUNTRYNAME']   = $this->vCard_encode($workaddr['COUNTRYNAME'], 'QUOTED-PRINTABLE', false);
-			$workaddr = $workaddr['POSTOFFICEBOX'].';'.$workaddr['EXT'].';'.$workaddr['STREET'].';'.$workaddr['LOCALITY'].';'.$workaddr['REGION'].';'.$workaddr['POSTALCODE'].';'.$workaddr['COUNTRYNAME'];
-			$work = 'A.ADR;' . $workattr . ';ENCODING=QUOTED-PRINTABLE:' . $workaddr . "\r\n";
-			$wlabel = 'LABEL;TYPE=WORK;;ENCODING=QUOTED-PRINTABLE:' . $wlabel . "\r\n";
-
-			$homeattr = ereg_replace('ADR;','',$homeattr);
-			// remember the correct order of address fields!
-			$homeaddr['POSTOFFICEBOX'] = $this->vCard_encode($homeaddr['POSTOFFICEBOX'], 'QUOTED-PRINTABLE', false);
-			$homeaddr['EXT']           = $this->vCard_encode($homeaddr['EXT'], 'QUOTED-PRINTABLE', false);
-			$homeaddr['STREET']        = $this->vCard_encode($homeaddr['STREET'], 'QUOTED-PRINTABLE', false);
-			$homeaddr['LOCALITY']      = $this->vCard_encode($homeaddr['LOCALITY'], 'QUOTED-PRINTABLE', false);
-			$homeaddr['REGION']        = $this->vCard_encode($homeaddr['REGION'], 'QUOTED-PRINTABLE', false);
-			$homeaddr['POSTALCODE']    = $this->vCard_encode($homeaddr['POSTALCODE'], 'QUOTED-PRINTABLE', false);
-			$homeaddr['COUNTRYNAME']   = $this->vCard_encode($homeaddr['COUNTRYNAME'], 'QUOTED-PRINTABLE', false);
-			$homeaddr = $homeaddr['POSTOFFICEBOX'].';'.$homeaddr['EXT'].';'.$homeaddr['STREET'].';'.$homeaddr['LOCALITY'].';'.$homeaddr['REGION'].';'.$homeaddr['POSTALCODE'].';'.$homeaddr['COUNTRYNAME'];
-			$home = 'B.ADR;' . $homeattr . ';ENCODING=QUOTED-PRINTABLE:' . $homeaddr . "\r\n";
-			$hlabel = 'LABEL;TYPE=HOME;;ENCODING=QUOTED-PRINTABLE:' . $hlabel . "\r\n";
+			if(isset($workaddr) && is_array($workaddr))
+			{
+				$workattr = ereg_replace('ADR;','',$workattr);
+				// remember the correct order of address fields!
+				$workaddr['POSTOFFICEBOX'] = $this->vCard_encode($workaddr['POSTOFFICEBOX'], 'QUOTED-PRINTABLE', false);
+				$workaddr['EXT']           = $this->vCard_encode($workaddr['EXT'], 'QUOTED-PRINTABLE', false);
+				$workaddr['STREET']        = $this->vCard_encode($workaddr['STREET'], 'QUOTED-PRINTABLE', false);
+				$workaddr['LOCALITY']      = $this->vCard_encode($workaddr['LOCALITY'], 'QUOTED-PRINTABLE', false);
+				$workaddr['REGION']        = $this->vCard_encode($workaddr['REGION'], 'QUOTED-PRINTABLE', false);
+				$workaddr['POSTALCODE']    = $this->vCard_encode($workaddr['POSTALCODE'], 'QUOTED-PRINTABLE', false);
+				$workaddr['COUNTRYNAME']   = $this->vCard_encode($workaddr['COUNTRYNAME'], 'QUOTED-PRINTABLE', false);
+				$workaddr = $workaddr['POSTOFFICEBOX'].';'.$workaddr['EXT'].';'.$workaddr['STREET'].';'.$workaddr['LOCALITY'].';'.$workaddr['REGION'].';'.$workaddr['POSTALCODE'].';'.$workaddr['COUNTRYNAME'];
+				if (strlen($workaddr) > 6) 
+				  $work = 'A.ADR;' . $workattr . ';ENCODING=QUOTED-PRINTABLE:' . $workaddr . "\r\n";
+				else
+				  $work = '';
+				$wlabel = 'LABEL;TYPE=WORK;ENCODING=QUOTED-PRINTABLE:' . $wlabel . "\r\n";
+			}
+
+			if(isset($homeaddr) && is_array($homeaddr))
+			{
+				$homeattr = ereg_replace('ADR;','',$homeattr);
+				// remember the correct order of address fields!
+				$homeaddr['POSTOFFICEBOX'] = $this->vCard_encode($homeaddr['POSTOFFICEBOX'], 'QUOTED-PRINTABLE', false);
+				$homeaddr['EXT']           = $this->vCard_encode($homeaddr['EXT'], 'QUOTED-PRINTABLE', false);
+				$homeaddr['STREET']        = $this->vCard_encode($homeaddr['STREET'], 'QUOTED-PRINTABLE', false);
+				$homeaddr['LOCALITY']      = $this->vCard_encode($homeaddr['LOCALITY'], 'QUOTED-PRINTABLE', false);
+				$homeaddr['REGION']        = $this->vCard_encode($homeaddr['REGION'], 'QUOTED-PRINTABLE', false);
+				$homeaddr['POSTALCODE']    = $this->vCard_encode($homeaddr['POSTALCODE'], 'QUOTED-PRINTABLE', false);
+				$homeaddr['COUNTRYNAME']   = $this->vCard_encode($homeaddr['COUNTRYNAME'], 'QUOTED-PRINTABLE', false);
+				$homeaddr = $homeaddr['POSTOFFICEBOX'].';'.$homeaddr['EXT'].';'.$homeaddr['STREET'].';'.$homeaddr['LOCALITY'].';'.$homeaddr['REGION'].';'.$homeaddr['POSTALCODE'].';'.$homeaddr['COUNTRYNAME'];
+				if (strlen($homeaddr) > 6) 
+				  $home = 'B.ADR;' . $homeattr . ';ENCODING=QUOTED-PRINTABLE:' . $homeaddr . "\r\n";
+				else
+				  $home = '';
+				$hlabel = 'LABEL;TYPE=HOME;ENCODING=QUOTED-PRINTABLE:' . $hlabel . "\r\n";
+			}
 
 			$entries = ereg_replace('PUBKEY','KEY',$entries);
 			$entries .= $work . $home . $wlabel . $hlabel . 'END:VCARD' . "\r\n";
 			$entries .= "\r\n";
+
+			$buffer = $entries;
+			return $buffer;
+		} //end function
+
+
+		function out_version_30($buffer)
+		{
+			$entry   = '';
+			$header  = 'BEGIN:VCARD' . "\r\n";
+			$header .= 'VERSION:3.0' . "\r\n";
+			$header .= 'X-PHPGROUPWARE-FILE-AS:phpGroupWare.org' . "\r\n";
+			
+			$workaddr = $hoeaddr = $this->address;
+
+			reset($this->export);
+			while ( list($name,$value) = each($this->export) )
+			{
+				if (!empty($buffer[$value]))
+				{
+					$mult = explode(';',$value);
+					if (!$mult[1])
+					{ // Normal
+						if (strstr($buffer[$value],"\r\n") || strstr($buffer[$value],"\n") || (strtoupper($mult[0])=='FN'))
+						{
+							$buffer[$value] = $this->vCard_encode($buffer[$value], false, 'UTF-8');
+							$entry .= $value . ':' . $buffer[$value] . "\r\n";
+						}
+						else
+						{
+							$entry .= $value . ':' . $buffer[$value] . "\r\n";
+						}
+					}
+					else
+					{
+						switch ($mult[0])
+						{
+							case 'N':
+								switch ($mult[1])
+								{
+									case 'PREFIX':
+										$prefix    = ';' . $this->vCard_encode($buffer[$value], false, 'UTF-8');
+										break;
+									case 'GIVEN':
+										$firstname = ';' . $this->vCard_encode($buffer[$value], false, 'UTF-8');
+										break;
+									case 'MIDDLE':
+										$middle    = ';' . $this->vCard_encode($buffer[$value], false, 'UTF-8');
+										break;
+									case 'FAMILY':
+										$lastname  =       $this->vCard_encode($buffer[$value], false, 'UTF-8');
+										break;
+									case 'SUFFIX':
+										$suffix    = ';' . $this->vCard_encode($buffer[$value], false, 'UTF-8');
+										break;
+								}
+								break;
+							case 'ORG':
+								switch ($mult[1])
+								{
+									case 'NAME':
+										$org_name = $this->vCard_encode($buffer[$value], false, 'UTF-8');
+										break;
+									case 'UNIT':
+										$org_unit = ';' . $this->vCard_encode($buffer[$value], false, 'UTF-8');
+										break;
+								}
+								break;
+							case 'ADR':
+								switch ($mult[1])
+								{
+									case 'TYPE':
+										if(isset($typei[$mult[2]]))
+										{
+											$typei[$mult[2]] .= ';';	
+										}
+										
+										$types = explode(';',$buffer[$value]);
+										if ($types[1])
+										{
+											$typei[$mult[2]] .= strtoupper($types[0][1]);
+											for ($i=1; $i<count($types); $i++)
+											{
+													$typei[$mult[2]] .= ',' . strtoupper($types[$i][1]);
+											}
+										}
+										elseif ($types[0])
+										{
+											$typei[$mult[2]] .= strtoupper($types[0]);
+										}
+										else
+										{
+											$typei[$mult[2]] .= strtoupper($buffer[$value]);
+										}
+										//echo "TYPE=".$typei[$mult[2]];
+										break;
+									case 'WORK':
+										$workaddr[$mult[2]] = $buffer[$value];
+										$workattr = $mult[0] . ';TYPE=' . $typei[$mult[1]];
+										break;
+									case 'HOME':
+										$homeaddr[$mult[2]] = $buffer[$value];
+										$homeattr = $mult[0] . ';TYPE=' . $typei[$mult[1]];
+										break;
+									default:
+										break;
+								}
+								break;
+							case 'TEL':
+								switch($mult[1])
+								{
+									case 'PREFER':
+										$prefer = explode(';',$buffer[$value]);
+										if ($prefer[1])
+										{
+											while ($pref = strtoupper(each($prefer)))
+											{
+												$prefi[$pref] = ';PREF';
+											}
+											//echo 'PREF1';
+										}
+										elseif ($prefer[0])
+										{
+											$prefi[strtoupper($prefer[0])] = ';PREF';
+											//echo 'PREF='.strtoupper($prefer[0]);
+										}
+										elseif ($buffer[$value])
+										{
+											$prefi[$buffer[$value]] = ';PREF';
+											//echo 'PREF3';
+										}
+										break;
+									case 'WORK':
+										// Wow, this is fun!
+										//$entry .= 'A.' . $mult[0] . ';' . $mult[1] . $prefi[$mult[1]] . ';' . $mult[2] . ':' . $buffer[$value] . "\r\n";
+										$entry .= '' . $mult[0] . ';' . $mult[1] . $prefi[$mult[1]] . ';' . $mult[2] . ':' . $buffer[$value] . "\r\n";
+										break;
+									case 'HOME':
+										//$entry .= 'B.' . $mult[0] . ';' . $mult[1] . $prefi[$mult[1]] . ';' . $mult[2] . ':' . $buffer[$value] . "\r\n";
+										$entry .= '' . $mult[0] . ';' . $mult[1] . $prefi[$mult[1]] . ';' . $mult[2] . ':' . $buffer[$value] . "\r\n";
+										break;
+									default:
+//										echo $mult[0] . ';' . $mult[1] . $prefi[$mult[1]] . ':' . $buffer[$value] . "\r\n";
+//										echo '<br>';
+										$entry .= $mult[0] . ';' . $mult[1] . $prefi[$mult[1]] . ':' . $buffer[$value] . "\r\n";
+										break;
+								}
+								break;
+							case 'EMAIL':
+								switch($mult[1])
+								{
+									case 'TYPE':
+										if ($mult[2] == 'WORK') { $emailtype  = ';' . $buffer[$value]; }
+										if ($mult[2] == 'HOME') { $hemailtype = ';' . $buffer[$value]; }
+										break;
+									case 'WORK':
+										//$newval = 'A.'.$value;
+										$newval = ''.$value;
+										$entry .= $newval . $emailtype . ':' . $buffer[$value] . "\r\n";
+										break;
+									case 'HOME':
+										//$newval = 'B.' . $value;
+										$newval = '' . $value;
+										$entry .= $newval . $hemailtype . ':' . $buffer[$value] . "\r\n";
+										break;
+									default:
+										break;
+								}
+								break;
+							default:
+								break;
+						} //end switch ($mult[0])
+					} //end else
+				} //end if (!empty)
+			} //end while
+
+			$entries .= $header;
+
+			if(!$middle)
+			{
+				$middle = ';';
+			}
+
+			if(!$prefix)
+			{
+				$prefix = ';';
+			}
+
+			$n = $lastname . $firstname . $middle . $prefix . $suffix;
+			$entries .= 'N:' . $n . "\r\n";
+			$entries .= $entry;
+
+			if (!$buffer['FN'])
+			{
+				if ($lastname || $firstname)
+				{
+					$fn = substr($firstname,1) . ' ' . $lastname;
+					$entries .= 'FN:' . $fn . "\r\n";
+				}
+			}
+
+			if(isset($org_name) && ($org_name!=''))
+			{
+				$org = $org_name;
+				if (isset($org_unit) && ($org_unit!=''))
+					$org .= $org_unit;
+				$entries .= 'ORG:' . $this->vCard_encode($org, false, 'UTF-8') . "\r\n";
+			}
+			else
+			{
+				if(isset($org_unit) && ($org_unit!=''))
+					$entries .= 'ORG:' . $this->vCard_encode($org_unit, false, 'UTF-8') . "\r\n";
+			}
+
+			if(isset($workaddr) && is_array($workaddr))
+			{
+				$workattr = ereg_replace('ADR;','',$workattr);
+				// remember the correct order of address fields!
+				$workaddr['POSTOFFICEBOX'] = $this->vCard_encode($workaddr['POSTOFFICEBOX'], false, 'UTF-8');
+				$workaddr['EXT']           = $this->vCard_encode($workaddr['EXT'], false, 'UTF-8');
+				$workaddr['STREET']        = $this->vCard_encode($workaddr['STREET'], false, 'UTF-8');
+				$workaddr['LOCALITY']      = $this->vCard_encode($workaddr['LOCALITY'], false, 'UTF-8');
+				$workaddr['REGION']        = $this->vCard_encode($workaddr['REGION'], false, 'UTF-8');
+				$workaddr['POSTALCODE']    = $this->vCard_encode($workaddr['POSTALCODE'], false, 'UTF-8');
+				$workaddr['COUNTRYNAME']   = $this->vCard_encode($workaddr['COUNTRYNAME'], false, 'UTF-8');
+				$workaddr = $workaddr['POSTOFFICEBOX'].';'.$workaddr['EXT'].';'.$workaddr['STREET'].';'.$workaddr['LOCALITY'].';'.$workaddr['REGION'].';'.$workaddr['POSTALCODE'].';'.$workaddr['COUNTRYNAME'];
+				if (strlen($workaddr) > 6) 
+				  $work = 'ADR;' . $workattr . ':' . $workaddr . "\r\n";
+				else
+				  $work = '';
+		    }
+			
+			if(isset($homeaddr) && is_array($homeaddr))
+			{
+				$homeattr = ereg_replace('ADR;','',$homeattr);
+				// remember the correct order of address fields!
+				$homeaddr['POSTOFFICEBOX'] = $this->vCard_encode($homeaddr['POSTOFFICEBOX'], false, 'UTF-8');
+				$homeaddr['EXT']           = $this->vCard_encode($homeaddr['EXT'], false, 'UTF-8');
+				$homeaddr['STREET']        = $this->vCard_encode($homeaddr['STREET'], false, 'UTF-8');
+				$homeaddr['LOCALITY']      = $this->vCard_encode($homeaddr['LOCALITY'], false, 'UTF-8');
+				$homeaddr['REGION']        = $this->vCard_encode($homeaddr['REGION'], false, 'UTF-8');
+				$homeaddr['POSTALCODE']    = $this->vCard_encode($homeaddr['POSTALCODE'], false, 'UTF-8');
+				$homeaddr['COUNTRYNAME']   = $this->vCard_encode($homeaddr['COUNTRYNAME'], false, 'UTF-8');
+				$homeaddr = $homeaddr['POSTOFFICEBOX'].';'.$homeaddr['EXT'].';'.$homeaddr['STREET'].';'.$homeaddr['LOCALITY'].';'.$homeaddr['REGION'].';'.$homeaddr['POSTALCODE'].';'.$homeaddr['COUNTRYNAME'];
+				if (strlen($homeaddr) > 6) 
+				  $home = 'ADR;' . $homeattr . ':' . $homeaddr . "\r\n";
+				else
+				  $home = '';
+				
+			}
+			
+			$entries = ereg_replace('PUBKEY','KEY',$entries);
+			//$entries .= $work . $home . $wlabel . $hlabel . 'END:VCARD' . "\r\n";
+			$entries .= $work . $home . 'END:VCARD' . "\r\n";
 
 			$buffer = $entries;
 			return $buffer;
