Beispiel #1
0
 /**
  * convert contact from xml to Addressbook_Model_Contact
  *
  * @param SimpleXMLElement $_data
  * @return Addressbook_Model_Contact
  */
 public function toTineModel(SimpleXMLElement $_data, $_entry = null)
 {
     if ($_entry instanceof Addressbook_Model_Contact) {
         $contact = $_entry;
     } else {
         $contact = new Addressbook_Model_Contact(null, true);
     }
     unset($contact->jpegphoto);
     $xmlData = $_data->children('uri:Contacts');
     $airSyncBase = $_data->children('uri:AirSyncBase');
     foreach ($this->_mapping as $fieldName => $value) {
         switch ($value) {
             case 'jpegphoto':
                 // do not change if not set
                 if (isset($xmlData->{$fieldName})) {
                     if (!empty($xmlData->{$fieldName})) {
                         $devicePhoto = base64_decode((string) $xmlData->{$fieldName});
                         try {
                             $currentPhoto = Tinebase_Controller::getInstance()->getImage('Addressbook', $contact->getId())->getBlob('image/jpeg', 36000);
                         } catch (Exception $e) {
                         }
                         if (isset($currentPhoto) && $currentPhoto == $devicePhoto) {
                             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                                 Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . " photo did not change on device -> preserving server photo");
                             }
                         } else {
                             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                                 Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . " using new contact photo from device (" . strlen($devicePhoto) . "KB)");
                             }
                             $contact->jpegphoto = $devicePhoto;
                         }
                     } else {
                         if ($_entry && !empty($_entry->jpegphoto)) {
                             $contact->jpegphoto = '';
                             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                                 Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . ' Deleting contact photo on device request (contact id: ' . $contact->getId() . ')');
                             }
                         }
                     }
                 }
                 break;
             case 'bday':
                 if (isset($xmlData->{$fieldName})) {
                     $isoDate = (string) $xmlData->{$fieldName};
                     $contact->bday = new Tinebase_DateTime($isoDate);
                     if ($this->_device->devicetype == Syncope_Model_Device::TYPE_IPHONE && $this->_device->getMajorVersion() < 800 || preg_match("/^\\d{4}-\\d{2}-\\d{2}\$/", $isoDate)) {
                         // iOS < 4 & webow < 2.1 send birthdays to the entered date, but the time the birthday got entered on the device
                         // acutally iOS < 4 somtimes sends the bday at noon but the timezone is not clear
                         // -> we don't trust the time part and set the birthdays timezone to the timezone the user has set in tine
                         $userTimezone = Tinebase_Core::get(Tinebase_Core::USERTIMEZONE);
                         $contact->bday = new Tinebase_DateTime($contact->bday->setTime(0, 0, 0)->format(Tinebase_Record_Abstract::ISO8601LONG), $userTimezone);
                         $contact->bday->setTimezone('UTC');
                     }
                 } else {
                     $contact->bday = null;
                 }
                 break;
             case 'adr_one_countryname':
             case 'adr_two_countryname':
                 $contact->{$value} = Tinebase_Translation::getRegionCodeByCountryName((string) $xmlData->{$fieldName});
                 break;
             case 'adr_one_street':
                 if (strtolower($this->_device->devicetype) == 'palm') {
                     // palm pre sends the whole address in the <Contacts:BusinessStreet> tag
                     unset($contact->adr_one_street);
                 } else {
                     // default handling for all other devices
                     if (isset($xmlData->{$fieldName})) {
                         $contact->{$value} = (string) $xmlData->{$fieldName};
                     } else {
                         $contact->{$value} = null;
                     }
                 }
                 break;
             case 'email':
             case 'email_home':
                 // android send email address as
                 // Lars Kneschke <*****@*****.**>
                 if (preg_match('/(.*)<(.+@[^@]+)>/', (string) $xmlData->{$fieldName}, $matches)) {
                     $contact->{$value} = trim($matches[2]);
                 } else {
                     $contact->{$value} = (string) $xmlData->{$fieldName};
                 }
                 break;
             default:
                 if (isset($xmlData->{$fieldName})) {
                     $contact->{$value} = (string) $xmlData->{$fieldName};
                 } else {
                     $contact->{$value} = null;
                 }
                 break;
         }
     }
     // get body
     if (version_compare($this->_device->acsversion, '12.0', '>=') === true) {
         $contact->note = isset($airSyncBase->Body) ? (string) $airSyncBase->Body->Data : null;
     } else {
         $contact->note = isset($xmlData->Body) ? (string) $xmlData->Body : null;
     }
     // force update of n_fileas and n_fn
     $contact->setFromArray(array('n_given' => $contact->n_given, 'n_family' => $contact->n_family, 'org_name' => $contact->org_name));
     // contact should be valid now
     $contact->isValid();
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . " contactData " . print_r($contact->toArray(), true));
     }
     return $contact;
 }
 /**
  * convert contact from xml to Addressbook_Model_Contact
  *
  * @param SimpleXMLElement $_data
  * @return Addressbook_Model_Contact
  */
 public function toTineModel(Syncroton_Model_IEntry $data, $entry = null)
 {
     if ($entry instanceof Addressbook_Model_Contact) {
         $contact = $entry;
     } else {
         $contact = new Addressbook_Model_Contact(null, true);
     }
     unset($contact->jpegphoto);
     foreach ($this->_mapping as $fieldName => $value) {
         if (!isset($data->{$fieldName})) {
             $contact->{$value} = null;
             continue;
         }
         switch ($value) {
             case 'jpegphoto':
                 if (!empty($data->{$fieldName})) {
                     $devicePhoto = $data->{$fieldName};
                     $contact->setSmallContactImage($devicePhoto);
                 } else {
                     if ($entry && !empty($entry->jpegphoto)) {
                         $contact->jpegphoto = '';
                         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                             Tinebase_Core::getLogger()->INFO(__METHOD__ . '::' . __LINE__ . ' Deleting contact photo on device request (contact id: ' . $contact->getId() . ')');
                         }
                     }
                 }
                 break;
             case 'bday':
                 $contact->{$value} = new Tinebase_DateTime($data->{$fieldName});
                 if ($this->_device->devicetype == Syncroton_Model_Device::TYPE_IPHONE && $this->_device->getMajorVersion() < 800) {
                     // iOS < 4 & webos < 2.1 send birthdays to the entered date, but the time the birthday got entered on the device
                     // actually iOS < 4 sometimes sends the bday at noon but the timezone is not clear
                     // -> we don't trust the time part and set the birthdays timezone to the timezone the user has set in tine
                     $userTimezone = Tinebase_Core::getUserTimezone();
                     $contact->{$value} = new Tinebase_DateTime($contact->bday->setTime(0, 0, 0)->format(Tinebase_Record_Abstract::ISO8601LONG), $userTimezone);
                     $contact->{$value}->setTimezone('UTC');
                 } elseif ($this->_device->devicetype == Syncroton_Model_Device::TYPE_BLACKBERRY && version_compare($this->_device->getMajorVersion(), '10', '>=')) {
                     // BB 10+ expects birthday to be at noon
                     $contact->{$value}->subHour(12);
                 }
                 break;
             case 'adr_one_countryname':
             case 'adr_two_countryname':
                 $contact->{$value} = Tinebase_Translation::getRegionCodeByCountryName($data->{$fieldName});
                 break;
             case 'adr_one_street':
                 if (strtolower($this->_device->devicetype) == 'palm') {
                     // palm pre sends the whole address in the <Contacts:BusinessStreet> tag
                     unset($contact->adr_one_street);
                 } else {
                     $contact->{$value} = $data->{$fieldName};
                 }
                 break;
             case 'email':
             case 'email_home':
                 // android sends email address as
                 // Lars Kneschke <*****@*****.**>
                 if (preg_match('/(.*)<(.+@[^@]+)>/', $data->{$fieldName}, $matches)) {
                     $contact->{$value} = trim($matches[2]);
                 } else {
                     $contact->{$value} = $data->{$fieldName};
                 }
                 break;
             case 'note':
                 // @todo check $data->$fieldName->Type and convert to/from HTML if needed
                 if ($data->{$fieldName} instanceof Syncroton_Model_EmailBody) {
                     $contact->{$value} = $data->{$fieldName}->data;
                 } else {
                     $contact->{$value} = null;
                 }
                 break;
             case 'url':
                 // remove facebook urls
                 if (!preg_match('/^fb:\\/\\//', $data->{$fieldName})) {
                     $contact->{$value} = $data->{$fieldName};
                 }
                 break;
             default:
                 $contact->{$value} = $data->{$fieldName};
                 break;
         }
     }
     // force update of n_fileas and n_fn
     $contact->setFromArray(array('n_given' => $contact->n_given, 'n_family' => $contact->n_family, 'org_name' => $contact->org_name));
     // either "org_name" or "n_family" must be given!
     if (empty($contact->org_name) && empty($contact->n_family)) {
         $contact->n_family = 'imported';
     }
     // contact should be valid now
     $contact->isValid();
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . " contactData " . print_r($contact->toArray(), true));
     }
     return $contact;
 }