[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare - iCalendar Parser * 4 * http://www.egroupware.org * 5 * Written by Lars Kneschke <lkneschke@egroupware.org> * 6 * -------------------------------------------- * 7 * This program is free software; you can redistribute it and/or modify it * 8 * under the terms of the GNU General Public License as published by the * 9 * Free Software Foundation; either version 2 of the License. * 10 \**************************************************************************/ 11 12 /* $Id: class.vcaladdressbook.inc.php 22165 2006-07-23 18:14:49Z lkneschke $ */ 13 14 require_once EGW_SERVER_ROOT.'/addressbook/inc/class.boaddressbook.inc.php'; 15 require_once EGW_SERVER_ROOT.'/phpgwapi/inc/horde/Horde/iCalendar.php'; 16 17 class vcaladdressbook extends boaddressbook 18 { 19 #function vcaladdressbook() 20 #{ 21 # $this->boaddressbook(); 22 #} 23 24 /** 25 * @return int contact id 26 * @param string $_vcard the vcard 27 * @param int $_abID the internal addressbook id 28 * @param int $_vcardProfile profile id for mapping from vcard values to egw addressbook 29 * @desc import a vard into addressbook 30 */ 31 function addVCard($_vcard, $_abID) 32 { 33 if(!$contact = $this->vcardtoegw($_vcard)) { 34 return false; 35 } 36 37 if($_abID > 0) 38 { 39 // update entry 40 $contact['ab_id'] = $_abID; 41 return $this->update_entry($contact); 42 } 43 else 44 { 45 // add entry 46 return $this->add_entry($contact); 47 } 48 } 49 50 /** 51 * return a vcard 52 * 53 * @param int $_id the id of the contact 54 * @param int $_vcardProfile profile id for mapping from vcard values to egw addressbook 55 * @return string containing the vcard 56 */ 57 function getVCard($_id) 58 { 59 require_once (EGW_SERVER_ROOT.'/phpgwapi/inc/horde/Horde/iCalendar/vcard.php'); 60 61 $vCard =& new Horde_iCalendar_vcard; 62 63 if(!is_array($this->supportedFields)) 64 { 65 $this->setSupportedFields(); 66 } 67 68 foreach($this->supportedFields as $databaseFields) 69 { 70 foreach($databaseFields as $databaseField) 71 { 72 if(!empty($databaseField)) 73 { 74 $fields[] = $databaseField; 75 } 76 } 77 } 78 79 #_debug_array($fields); 80 81 if($this->check_perms($_id,EGW_ACL_READ)) 82 { 83 //$data = array('id' => $_id, 'fields' => $fields); 84 $entry = $this->so->read_entry($_id,$fields); 85 $entry = $this->strip_html($entry); 86 if($this->xmlrpc) 87 { 88 $entry = $this->data2xmlrpc($entry); 89 } 90 #_debug_array($entry); 91 $sysCharSet = $GLOBALS['egw']->translation->charset(); 92 93 foreach($this->supportedFields as $vcardField => $databaseFields) 94 { 95 $options = array(); 96 $value = ''; 97 foreach($databaseFields as $databaseField) 98 { 99 $tempVal = ';'; 100 if(!empty($databaseField)) 101 { 102 #$tempVal = trim('value').';'; 103 $tempVal = trim($entry[0][$databaseField]).';'; 104 } 105 $value .= $tempVal; 106 } 107 // remove the last ; 108 $value = substr($value, 0, -1); 109 110 switch($vcardField) 111 { 112 // TODO handle multiple categories 113 case 'CATEGORIES': 114 $catData = ExecMethod('phpgwapi.categories.return_single',$value); 115 $value = $catData[0]['name']; 116 break; 117 case 'CLASS': 118 $value = ($value == 'private' ? 'PRIVATE' : 'PUBLIC'); 119 break; 120 case 'BDAY': 121 if(!empty($value)) 122 { 123 $dateParts = explode('/',$value); 124 $value = sprintf('%04d%02d%02dT000000Z',$dateParts[2],$dateParts[0],$dateParts[1]); 125 } 126 break; 127 } 128 129 $value = $GLOBALS['egw']->translation->convert($value,$sysCharSet,'utf-8'); 130 $value = html_entity_decode($value, ENT_QUOTES, 'UTF-8'); 131 132 // don't add the entry if it contains only ';' 133 if(strlen(str_replace(';','',$value)) != 0) 134 { 135 $vCard->setAttribute($vcardField, $value); 136 } 137 if(preg_match('/([\000-\012\015\016\020-\037\075])/',$value)) 138 { 139 $options['ENCODING'] = 'QUOTED-PRINTABLE'; 140 } 141 if(preg_match('/([\177-\377])/',$value)) 142 { 143 $options['CHARSET'] = 'UTF-8'; 144 } 145 $vCard->setParameter($vcardField, $options); 146 } 147 148 $result = $vCard->exportvCalendar(); 149 150 return $result; 151 } 152 153 if($this->xmlrpc) 154 { 155 $GLOBALS['server']->xmlrpc_error($GLOBALS['xmlrpcerr']['no_access'],$GLOBALS['xmlrpcstr']['no_access']); 156 } 157 return False; 158 } 159 160 function search($_vcard) { 161 if(!$contact = $this->vcardtoegw($_vcard)) { 162 return false; 163 } 164 165 if($foundContacts = $this->read_entries(array('query' => $contact))) { 166 return $foundContacts[0][id]; 167 } 168 169 return false; 170 } 171 172 function setSupportedFields($_productManufacturer='file', $_productName='') 173 { 174 $defaultFields[0] = array( 175 'ADR;WORK' => array('','','adr_one_street','adr_one_locality','adr_one_region', 176 'adr_one_postalcode','adr_one_countryname'), 177 'CATEGORIES' => array('cat_id'), 178 'CLASS' => array('access'), 179 'EMAIL;INTERNET;WORK' => array('email'), 180 'N' => array('n_family','n_given','','',''), 181 'NOTE' => array('note'), 182 'ORG' => array('org_name',''), 183 'TEL;CELL;WORK' => array('tel_cell'), 184 'TEL;FAX;WORK' => array('tel_fax'), 185 'TEL;HOME' => array('tel_home'), 186 'TEL;WORK' => array('tel_work'), 187 'TITLE' => array('title'), 188 ); 189 190 $defaultFields[1] = array( 191 'ADR;WORK' => array('','','adr_one_street','adr_one_locality','adr_one_region', 192 'adr_one_postalcode','adr_one_countryname'), 193 'ADR;HOME' => array('','','adr_two_street','adr_two_locality','adr_two_region', 194 'adr_two_postalcode','adr_two_countryname'), 195 'BDAY' => array('bday'), 196 'CATEGORIES' => array('cat_id'), 197 'EMAIL;INTERNET;WORK' => array('email'), 198 'EMAIL;INTERNET;HOME' => array('email_home'), 199 'N' => array('n_family','n_given','n_middle','n_prefix','n_suffix'), 200 'NOTE' => array('note'), 201 'ORG' => array('org_name','org_unit'), 202 'TEL;CELL;WORK' => array('tel_cell'), 203 'TEL;FAX;WORK' => array('tel_fax'), 204 'TEL;HOME' => array('tel_home'), 205 'TEL;PAGER;WORK' => array('tel_pager'), 206 'TEL;WORK' => array('tel_work'), 207 'TITLE' => array('title'), 208 'URL;WORK' => array('url'), 209 ); 210 211 $defaultFields[2] = array( 212 'ADR;HOME' => array('','','adr_one_street','adr_one_locality','adr_one_region', 213 'adr_one_postalcode','adr_one_countryname'), 214 'BDAY' => array('bday'), 215 'CATEGORIES' => array('cat_id'), 216 'CLASS' => array('access'), 217 'EMAIL' => array('email'), 218 'N' => array('n_family','n_given','','',''), 219 'NOTE' => array('note'), 220 'ORG' => array('org_name',''), 221 'TEL;CELL;WORK' => array('tel_cell'), 222 'TEL;FAX;WORK' => array('tel_fax'), 223 'TEL;HOME' => array('tel_home'), 224 'TEL;WORK' => array('tel_work'), 225 'TITLE' => array('title'), 226 'URL' => array('url'), 227 ); 228 229 $defaultFields[3] = array( 230 'ADR;WORK' => array('','','adr_one_street','adr_one_locality','adr_one_region', 231 'adr_one_postalcode','adr_one_countryname'), 232 'ADR;HOME' => array('','','adr_two_street','adr_two_locality','adr_two_region', 233 'adr_two_postalcode','adr_two_countryname'), 234 'BDAY' => array('bday'), 235 'EMAIL;INTERNET;WORK' => array('email'), 236 'EMAIL;INTERNET;HOME' => array('email_home'), 237 'N' => array('n_family','n_given','','',''), 238 'NOTE' => array('note'), 239 'ORG' => array('org_name','org_unit'), 240 'TEL;CELL;WORK' => array('tel_cell'), 241 'TEL;FAX;WORK' => array('tel_fax'), 242 'TEL;HOME' => array('tel_home'), 243 'TEL;PAGER;WORK' => array('tel_pager'), 244 'TEL;WORK' => array('tel_work'), 245 'TITLE' => array('title'), 246 'URL;WORK' => array('url'), 247 ); 248 249 $defaultFields[4] = array( 250 'ADR;WORK' => array('','','adr_one_street','adr_one_locality','adr_one_region', 251 'adr_one_postalcode','adr_one_countryname'), 252 'ADR;HOME' => array('','','adr_two_street','adr_two_locality','adr_two_region', 253 'adr_two_postalcode','adr_two_countryname'), 254 'BDAY' => array('bday'), 255 'EMAIL;INTERNET;WORK' => array('email'), 256 'EMAIL;INTERNET;HOME' => array('email_home'), 257 'N' => array('n_family','n_given','','',''), 258 'NOTE' => array('note'), 259 'ORG' => array('org_name',''), 260 'TEL;CELL;WORK' => array('tel_cell'), 261 'TEL;FAX;WORK' => array('tel_fax'), 262 'TEL;HOME' => array('tel_home'), 263 'TEL;PAGER;WORK' => array('tel_pager'), 264 'TEL;WORK' => array('tel_work'), 265 'TITLE' => array('title'), 266 'URL;WORK' => array('url'), 267 ); 268 269 switch(strtolower($_productManufacturer)) 270 { 271 case 'nexthaus corporation': 272 switch(strtolower($_productName)) 273 { 274 case 'syncje outlook edition': 275 default: 276 $this->supportedFields = $defaultFields[1]; 277 break; 278 } 279 break; 280 281 case 'nokia': 282 switch(strtolower($_productName)) 283 { 284 case '6600': 285 default: 286 $this->supportedFields = $defaultFields[4]; 287 break; 288 } 289 break; 290 291 // multisync does not provide anymore information then the manufacturer 292 // we suppose multisync with evolution 293 case 'the multisync project': 294 switch(strtolower($_productName)) 295 { 296 default: 297 $this->supportedFields = $defaultFields[0]; 298 break; 299 } 300 break; 301 302 case 'siemens': 303 switch(strtolower($_productName)) 304 { 305 case 'sx1': 306 default: 307 $this->supportedFields = $defaultFields[3]; 308 break; 309 } 310 break; 311 312 case 'sonyericsson': 313 switch(strtolower($_productName)) 314 { 315 case 'd750i': 316 default: 317 $this->supportedFields = $defaultFields[2]; 318 break; 319 } 320 break; 321 322 case 'synthesis ag': 323 switch(strtolower($_productName)) 324 { 325 default: 326 $this->supportedFields = $defaultFields[0]; 327 break; 328 } 329 break; 330 331 case 'file': // used outside of SyncML, eg. by the calendar itself ==> all possible fields 332 $this->supportedFields = $defaultFields[1]; 333 break; 334 335 // the fallback for SyncML 336 default: 337 error_log("Client not found: $_productManufacturer $_productName"); 338 $this->supportedFields = $defaultFields[0]; 339 break; 340 } 341 } 342 343 function vcardtoegw($_vcard) { 344 if(!is_array($this->supportedFields)) 345 { 346 $this->setSupportedFields(); 347 } 348 349 $this->supportedFields[0] = array( 350 ); 351 352 require_once (EGW_SERVER_ROOT.'/phpgwapi/inc/horde/Horde/iCalendar.php'); 353 354 $vCard = Horde_iCalendar::newComponent('vcard', $container); 355 356 if(!$vCard->parsevCalendar($_vcard,'VCARD')) 357 { 358 return False; 359 } 360 $vcardValues = $vCard->getAllAttributes(); 361 362 #print "<pre>$_vcard</pre>"; 363 364 #_debug_array($vcardValues); 365 366 foreach($vcardValues as $key => $vcardRow) 367 { 368 $rowName = $vcardRow['name']; 369 $mailtype = ';INTERNET'; 370 $tempVal = ';WORK'; 371 372 if(isset($vcardRow['params']['INTERNET'])) 373 { 374 $rowName .= ";INTERNET"; 375 } 376 if(isset($vcardRow['params']['CELL'])) 377 { 378 $rowName .= ';CELL'; 379 } 380 if(isset($vcardRow['params']['FAX'])) 381 { 382 $rowName .= ';FAX'; 383 } 384 if(isset($vcardRow['params']['PAGER'])) 385 { 386 $rowName .= ';PAGER'; 387 } 388 if(isset($vcardRow['params']['WORK'])) 389 { 390 $rowName .= ';WORK'; 391 } 392 if(isset($vcardRow['params']['HOME'])) 393 { 394 $rowName .= ';HOME'; 395 } 396 397 $rowNames[$rowName] = $key; 398 } 399 400 #error_log('rowNames: '.print_r($rowNames,true)); 401 402 // now we have all rowNames the vcard provides 403 // we just need to map to the right addressbook fieldnames 404 // we need also to take care about ADR for example. we do not 405 // support this. We support only ADR;WORK or ADR;HOME 406 407 foreach($rowNames as $rowName => $vcardKey) 408 { 409 switch($rowName) 410 { 411 case 'ADR': 412 case 'TEL': 413 case 'TEL;FAX': 414 case 'TEL;CELL': 415 case 'TEL;PAGER': 416 if(!isset($rowNames[$rowName. ';WORK'])) 417 { 418 $finalRowNames[$rowName. ';WORK'] = $vcardKey; 419 } 420 break; 421 case 'EMAIL': 422 case 'EMAIL;WORK': 423 if(!isset($rowNames['EMAIL;INTERNET;WORK'])) 424 { 425 $finalRowNames['EMAIL;INTERNET;WORK'] = $vcardKey; 426 } 427 break; 428 case 'EMAIL;HOME': 429 if(!isset($rowNames['EMAIL;INTERNET;HOME'])) 430 { 431 $finalRowNames['EMAIL;INTERNET;HOME'] = $vcardKey; 432 } 433 break; 434 435 case 'CATEGORIES': 436 #cat_id = 7,8 437 $vcardData['category'] = array(); 438 if ($attributes['value']) 439 { 440 if (!is_object($this->cat)) 441 { 442 if (!is_object($GLOBALS['egw']->categories)) 443 { 444 $GLOBALS['egw']->categories =& CreateObject('phpgwapi.categories',$this->owner,'addressbook'); 445 } 446 $this->cat =& $GLOBALS['egw']->categories; 447 } 448 foreach(explode(',',$attributes['value']) as $cat_name) 449 { 450 if (!($cat_id = $this->cat->name2id($cat_name))) 451 { 452 $cat_id = $this->cat->add( array('name' => $cat_name,'descr' => $cat_name )); 453 } 454 $vcardData['category'][] = $cat_id; 455 } 456 } 457 break; 458 459 case 'VERSION': 460 break; 461 default: 462 $finalRowNames[$rowName] = $vcardKey; 463 break; 464 } 465 } 466 467 #_debug_array($finalRowNames); 468 $contact = array(); 469 470 foreach($finalRowNames as $key => $vcardKey) 471 { 472 if(isset($this->supportedFields[$key])) 473 { 474 $fieldNames = $this->supportedFields[$key]; 475 foreach($fieldNames as $fieldKey => $fieldName) 476 { 477 if(!empty($fieldName)) 478 { 479 switch($fieldName) 480 { 481 case 'access': 482 if($vcardValues[$vcardKey]['values'][$fieldKey] == 'PRIVATE') 483 { 484 $contact[$fieldName] = 'private'; 485 } 486 else 487 { 488 $contact[$fieldName] = 'public'; 489 } 490 break; 491 case 'cat_id': 492 if (!is_object($this->cat)) 493 { 494 if (!is_object($GLOBALS['egw']->categories)) 495 { 496 $GLOBALS['egw']->categories =& CreateObject('phpgwapi.categories',$GLOBALS['egw_info']['user']['account_id'],'addressbook'); 497 } 498 $this->cat =& $GLOBALS['egw']->categories; 499 } 500 foreach(explode(',',$vcardValues[$vcardKey]['values'][$fieldKey]) as $cat_name) 501 { 502 if (!($cat_id = $this->cat->name2id($cat_name))) 503 { 504 $cat_id = $this->cat->add( array('name' => $cat_name,'descr' => $cat_name )); 505 } 506 $contact[$fieldName] = $cat_id; 507 } 508 break; 509 default: 510 $contact[$fieldName] = $vcardValues[$vcardKey]['values'][$fieldKey]; 511 break; 512 } 513 } 514 } 515 } 516 } 517 518 #return true; 519 520 /* _debug_array($contact);exit; */ 521 $contact['fn'] = trim($contact['n_given'].' '.$contact['n_family']); 522 if(!$contact['tel_work']) 523 { 524 $contact['tel_work'] = ''; 525 } 526 if(!$contact['tel_home']) 527 { 528 $contact['tel_home'] = ''; 529 } 530 if(!$contact['tel_voice']) 531 { 532 $contact['tel_voice'] = ''; 533 } 534 if(!$contact['tel_fax']) 535 { 536 $contact['tel_fax'] = ''; 537 } 538 if(!$contact['tel_msg']) 539 { 540 $contact['tel_msg'] = ''; 541 } 542 if(!$contact['tel_cell']) 543 { 544 $contact['tel_cell'] = ''; 545 } 546 if(!$contact['tel_pager']) 547 { 548 $contact['tel_pager'] = ''; 549 } 550 if(!$contact['tel_car']) 551 { 552 $contact['tel_car'] = ''; 553 } 554 if(!$contact['tel_isdn']) 555 { 556 $contact['tel_isdn'] = ''; 557 } 558 if(!$contact['tel_video']) 559 { 560 $contact['tel_video'] = ''; 561 } 562 563 return $contact; 564 } 565 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |