Example #1
2
 /**
  * Generate the Vcard for the current Contact
  *
  *@return Nothing (display)
  **/
 function generateVcard()
 {
     if (!$this->can($this->fields['id'], READ)) {
         return false;
     }
     // build the Vcard
     $vcard = new VObject\Component\VCard(['N' => [$this->fields["name"], $this->fields["firstname"]], 'EMAIL' => $this->fields["email"], 'NOTE' => $this->fields["comment"]]);
     $vcard->add('TEL', $this->fields["phone"], ['type' => 'PREF;WORK;VOICE']);
     $vcard->add('TEL', $this->fields["phone2"], ['type' => 'HOME;VOICE']);
     $vcard->add('TEL', $this->fields["mobile"], ['type' => 'WORK;CELL']);
     $vcard->add('URL', $this->GetWebsite(), ['type' => 'WORK']);
     $addr = $this->GetAddress();
     if (is_array($addr)) {
         $addr_string = implode(";", array_filter($addr));
         $vcard->add('ADR', $addr_string, ['type' => 'WORK;POSTAL']);
     }
     // send the  VCard
     $output = $vcard->serialize();
     $filename = $this->fields["name"] . "_" . $this->fields["firstname"] . ".vcf";
     @Header("Content-Disposition: attachment; filename=\"{$filename}\"");
     @Header("Content-Length: " . Toolbox::strlen($output));
     @Header("Connection: close");
     @Header("content-type: text/x-vcard; charset=UTF-8");
     echo $output;
 }
Example #2
0
 function assertVCard21serialization($propValue, $expected)
 {
     $doc = new VCard(array('VERSION' => '2.1', 'PROP' => $propValue), false);
     // Adding quoted-printable, because we're testing if it gets removed
     // automatically.
     $doc->PROP['ENCODING'] = 'QUOTED-PRINTABLE';
     $doc->PROP['P1'] = 'V1';
     $output = $doc->serialize();
     $this->assertEquals("BEGIN:VCARD\r\nVERSION:2.1\r\n{$expected}\r\nEND:VCARD\r\n", $output);
 }
 public function NAttribute()
 {
     $vcard = new VObject\Component\VCard();
     $vcard->add('N', ['Werner', 'Lukas', ['Roland', 'Thomas', 'Alfred', 'Joseph'], 'B.Sc.', 'Informatik']);
     $contact = new ContactInformation();
     $contact->setLastname('Werner');
     $contact->setFirstname('Lukas');
     $contact->setAdditionalNames(['Roland', 'Thomas', 'Alfred', 'Joseph']);
     $contact->setHonorificPrefixes('B.Sc.');
     $contact->setHonorificSuffixes('Informatik');
     $this->assertEquals($vcard->serialize(), $this->object->convert($contact)->serialize());
 }
 /**
  * Converts a ContactInformation object into a vcard
  * 
  * @param \Prototype\ContactInformation $contact contact information to convert
  * @return \Sabre\VObject\Component\VCard created vcard
  */
 public function convert(ContactInformation $contact)
 {
     $vcard = new VObject\Component\VCard();
     if ($contact->hasAttribute('lastname') || $contact->hasAttribute('firstname') || $contact->hasAttribute('additionalNames') || $contact->hasAttribute('honorificPrefixes') || $contact->hasAttribute('honorificSuffixes')) {
         $N = [$contact->hasAttribute('lastname') ? $contact->getAttribute('lastname') : '', $contact->hasAttribute('firstname') ? $contact->getAttribute('firstname') : '', $contact->hasAttribute('additionalNames') ? $contact->getAttribute('additionalNames') : '', $contact->hasAttribute('honorificPrefixes') ? $contact->getAttribute('honorificPrefixes') : '', $contact->hasAttribute('honorificSuffixes') ? $contact->getAttribute('honorificSuffixes') : ''];
         $vcard->add('N', $N);
     }
     foreach (self::$attributeMap as $key => $value) {
         if ($contact->hasAttribute($key)) {
             $vcard->add($value, $contact->getAttribute($key));
         }
     }
     return $vcard;
 }
Example #5
0
 function testGetDocumentType()
 {
     $vcard = new VCard([], false);
     $vcard->VERSION = '2.1';
     $this->assertEquals(VCard::VCARD21, $vcard->getDocumentType());
     $vcard = new VCard([], false);
     $vcard->VERSION = '3.0';
     $this->assertEquals(VCard::VCARD30, $vcard->getDocumentType());
     $vcard = new VCard([], false);
     $vcard->VERSION = '4.0';
     $this->assertEquals(VCard::VCARD40, $vcard->getDocumentType());
     $vcard = new VCard([], false);
     $this->assertEquals(VCard::UNKNOWN, $vcard->getDocumentType());
 }
Example #6
0
 public static function addingDummyContacts($iNum)
 {
     $active_addressbooks = array();
     $active_addressbooks = Addressbook::active(\OCP\USER::getUser());
     $contacts_addressbook = array();
     $ids = array();
     foreach ($active_addressbooks as $addressbook) {
         $ids[] = $addressbook['id'];
     }
     for ($i = 0; $i < $iNum; $i++) {
         //$dummyName=self::generateFnDummy();
         $dummySure = self::generateFnDummy(10);
         $dummyLast = self::generateFnDummy(10);
         $uid = substr(md5(rand() . time()), 0, 10);
         $aid = $ids[0];
         $vcard = new VObject\Component\VCard(array('N' => array($dummySure, $dummyLast, '', '', ''), 'UID' => $uid));
         $vcard->FN = $dummySure . ' ' . $dummyLast;
         $vcard->CATEGORIES = 'familie';
         $vcard->add('TEL', '+1 555 34567 456', array('type' => 'fax'));
         $vcard->add('TEL', '+1 555 34567 457', array('type' => 'voice', 'pref' => 1));
         $vcard->add('EMAIL', '*****@*****.**', array('type' => 'work'));
         $vcard->add('EMAIL', '*****@*****.**', array('type' => 'private', 'pref' => 1));
         $id = VCard::add($aid, $vcard, null, true);
     }
 }
Example #7
0
 /**
  * Generate vcard for the current user
  **/
 function generateVcard()
 {
     // prepare properties for the Vcard
     if (!empty($this->fields["realname"]) || !empty($this->fields["firstname"])) {
         $name = [$this->fields["realname"], $this->fields["firstname"], "", "", ""];
     } else {
         $name = [$this->fields["name"], "", "", "", ""];
     }
     // create vcard
     $vcard = new VObject\Component\VCard(['N' => $name, 'EMAIL' => $this->getDefaultEmail(), 'NOTE' => $this->fields["comment"]]);
     $vcard->add('TEL', $this->fields["phone"], ['type' => 'PREF;WORK;VOICE']);
     $vcard->add('TEL', $this->fields["phone2"], ['type' => 'HOME;VOICE']);
     $vcard->add('TEL', $this->fields["mobile"], ['type' => 'WORK;CELL']);
     // send the  VCard
     $output = $vcard->serialize();
     $filename = implode("_", array_filter($name)) . ".vcf";
     @Header("Content-Disposition: attachment; filename=\"{$filename}\"");
     @Header("Content-Length: " . Toolbox::strlen($output));
     @Header("Connection: close");
     @Header("content-type: text/x-vcard; charset=UTF-8");
     echo $output;
 }
Example #8
0
 /**
  * Validates the node for correctness.
  *
  * The following options are supported:
  *   - VCard::REPAIR - If something is broken, and automatic repair may
  *                    be attempted.
  *   - VCard::UPGRADE - If needed the vCard will be upgraded to version 3.0.
  *
  * An array is returned with warnings.
  *
  * Every item in the array has the following properties:
  *    * level - (number between 1 and 3 with severity information)
  *    * message - (human readable message)
  *    * node - (reference to the offending node)
  *
  * @param int $options
  * @return array
  */
 public function validate($options = 0)
 {
     $warnings = array();
     if ($options & self::UPGRADE) {
         $this->VERSION = self::DEFAULT_VERSION;
         foreach ($this->children as $idx => &$property) {
             $this->decodeProperty($property);
             $this->fixPropertyParameters($property);
             /* What exactly was I thinking here?
             			switch((string)$property->name) {
             				case 'LOGO':
             				case 'SOUND':
             				case 'PHOTO':
             					if(isset($property['TYPE']) && strpos((string)$property['TYPE'], '/') === false) {
             						$property['TYPE'] = 'image/' . strtolower($property['TYPE']);
             					}
             			}*/
         }
     }
     $version = $this->select('VERSION');
     if (count($version) !== 1) {
         $warnings[] = array('level' => 1, 'message' => 'The VERSION property must appear in the VCARD component exactly 1 time', 'node' => $this);
         if ($options & self::REPAIR) {
             $this->VERSION = self::DEFAULT_VERSION;
             if (!$options & self::UPGRADE) {
                 $options |= self::UPGRADE;
             }
         }
     } else {
         $version = (string) $this->VERSION;
         if ($version !== '2.1' && $version !== '3.0' && $version !== '4.0') {
             $warnings[] = array('level' => 1, 'message' => 'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.', 'node' => $this);
             if ($options & self::REPAIR) {
                 $this->VERSION = self::DEFAULT_VERSION;
                 if (!$options & self::UPGRADE) {
                     $options |= self::UPGRADE;
                 }
             }
         }
     }
     $fn = $this->select('FN');
     if (count($fn) !== 1 || trim((string) $this->FN) === '') {
         $warnings[] = array('level' => 1, 'message' => 'The FN property must appear in the VCARD component exactly 1 time', 'node' => $this);
         if ($options & self::REPAIR) {
             // We're going to try to see if we can use the contents of the
             // N property.
             if (isset($this->N) && substr((string) $this->N, 2) !== ';;' && (string) $this->N !== '') {
                 $value = explode(';', (string) $this->N);
                 if (isset($value[1]) && $value[1]) {
                     $this->FN = $value[1] . ' ' . $value[0];
                 } else {
                     $this->FN = $value[0];
                 }
                 // Otherwise, the ORG property may work
             } elseif (isset($this->ORG)) {
                 $this->FN = (string) $this->ORG;
             } elseif (isset($this->EMAIL)) {
                 $this->FN = (string) $this->EMAIL;
             }
         }
     }
     if (isset($this->BDAY)) {
         if ($options & self::REPAIR) {
             // If the BDAY has a format of e.g. 19960401
             $bday = (string) $this->BDAY;
             if (strlen($bday) >= 8 && is_int(substr($bday, 0, 4)) && is_int(substr($bday, 4, 2)) && is_int(substr($bday, 6, 2))) {
                 $this->BDAY = substr($bday, 0, 4) . '-' . substr($bday, 4, 2) . '-' . substr($bday, 6, 2);
                 $this->BDAY->VALUE = 'DATE';
             } else {
                 if (empty($bday)) {
                     // We don't want "New \DateTime($bday)" evaluate to current date because of empty value.
                     // So we'll leave this item empty.
                     \OCP\Util::writeLog('contacts', __METHOD__ . ' Removing invalid/empty BDAY.', \OCP\Util::DEBUG);
                     unset($this->BDAY);
                 } else {
                     if ($bday[5] !== '-' || $bday[7] !== '-') {
                         try {
                             // Skype exports as e.g. Jan 14, 1996
                             $date = new \DateTime($bday);
                             $this->BDAY = $date->format('Y-m-d');
                             $this->BDAY->VALUE = 'DATE';
                         } catch (\Exception $e) {
                             \OCP\Util::writeLog('contacts', __METHOD__ . ' Removing invalid BDAY: ' . $bday, \OCP\Util::DEBUG);
                             unset($this->BDAY);
                         }
                     }
                 }
             }
         }
     }
     $n = $this->select('N');
     if (count($n) !== 1) {
         $warnings[] = array('level' => 1, 'message' => 'The N property must appear in the VCARD component exactly 1 time', 'node' => $this);
         // TODO: Make a better effort parsing FN.
         if ($options & self::REPAIR && count($n) === 0) {
             // Take 2 first name parts of 'FN' and reverse.
             $slice = array_reverse(array_slice(explode(' ', (string) $this->FN), 0, 2));
             if (count($slice) < 2) {
                 // If not enought, add one more...
                 $slice[] = "";
             }
             $this->N = $slice;
         }
     }
     if (!isset($this->UID) || trim((string) $this->UID) === '') {
         $warnings[] = array('level' => 1, 'message' => 'Every vCard must have a UID', 'node' => $this);
         if ($options & self::REPAIR) {
             $this->UID = Utils\Properties::generateUID();
         }
     }
     if ($options & self::REPAIR || $options & self::UPGRADE) {
         $now = new \DateTime();
         $this->REV = $now->format(\DateTime::W3C);
     }
     return array_merge(parent::validate($options), $warnings);
 }
Example #9
0
 function testAnotherSerializeOrderProp()
 {
     $prop4s = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10');
     $comp = new VCard(array(), false);
     $comp->__set('SOMEPROP', 'FOO');
     $comp->__set('ANOTHERPROP', 'FOO');
     $comp->__set('THIRDPROP', 'FOO');
     foreach ($prop4s as $prop4) {
         $comp->add('PROP4', 'FOO ' . $prop4);
     }
     $comp->__set('PROPNUMBERFIVE', 'FOO');
     $comp->__set('PROPNUMBERSIX', 'FOO');
     $comp->__set('PROPNUMBERSEVEN', 'FOO');
     $comp->__set('PROPNUMBEREIGHT', 'FOO');
     $comp->__set('PROPNUMBERNINE', 'FOO');
     $comp->__set('PROPNUMBERTEN', 'FOO');
     $comp->__set('VERSION', '2.0');
     $comp->__set('UID', 'FOO');
     $str = $comp->serialize();
     $this->assertEquals("BEGIN:VCARD\r\nVERSION:2.0\r\nSOMEPROP:FOO\r\nANOTHERPROP:FOO\r\nTHIRDPROP:FOO\r\nPROP4:FOO 1\r\nPROP4:FOO 2\r\nPROP4:FOO 3\r\nPROP4:FOO 4\r\nPROP4:FOO 5\r\nPROP4:FOO 6\r\nPROP4:FOO 7\r\nPROP4:FOO 8\r\nPROP4:FOO 9\r\nPROP4:FOO 10\r\nPROPNUMBERFIVE:FOO\r\nPROPNUMBERSIX:FOO\r\nPROPNUMBERSEVEN:FOO\r\nPROPNUMBEREIGHT:FOO\r\nPROPNUMBERNINE:FOO\r\nPROPNUMBERTEN:FOO\r\nUID:FOO\r\nEND:VCARD\r\n", $str);
 }
 public function testUpdateProperties()
 {
     $bookId = 42;
     $cardUri = 'card-uri';
     $cardId = 2;
     $backend = $this->getMockBuilder('OCA\\DAV\\CardDAV\\CardDavBackend')->setConstructorArgs([$this->db, $this->principal, null])->setMethods(['getCardId'])->getMock();
     $backend->expects($this->any())->method('getCardId')->willReturn($cardId);
     // add properties for new vCard
     $vCard = new VCard();
     $vCard->add(new Text($vCard, 'UID', $cardUri));
     $vCard->add(new Text($vCard, 'FN', 'John Doe'));
     $this->invokePrivate($backend, 'updateProperties', [$bookId, $cardUri, $vCard->serialize()]);
     $query = $this->db->getQueryBuilder();
     $result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
     $this->assertSame(2, count($result));
     $this->assertSame('UID', $result[0]['name']);
     $this->assertSame($cardUri, $result[0]['value']);
     $this->assertSame($bookId, (int) $result[0]['addressbookid']);
     $this->assertSame($cardId, (int) $result[0]['cardid']);
     $this->assertSame('FN', $result[1]['name']);
     $this->assertSame('John Doe', $result[1]['value']);
     $this->assertSame($bookId, (int) $result[1]['addressbookid']);
     $this->assertSame($cardId, (int) $result[1]['cardid']);
     // update properties for existing vCard
     $vCard = new VCard();
     $vCard->add(new Text($vCard, 'FN', 'John Doe'));
     $this->invokePrivate($backend, 'updateProperties', [$bookId, $cardUri, $vCard->serialize()]);
     $query = $this->db->getQueryBuilder();
     $result = $query->select('*')->from('cards_properties')->execute()->fetchAll();
     $this->assertSame(1, count($result));
     $this->assertSame('FN', $result[0]['name']);
     $this->assertSame('John Doe', $result[0]['value']);
     $this->assertSame($bookId, (int) $result[0]['addressbookid']);
     $this->assertSame($cardId, (int) $result[0]['cardid']);
 }
Example #11
0
 /**
  * create empty vcard
  *
  * @param string $uid
  * @return VCard
  */
 protected function createEmptyVCard($uid)
 {
     $vCard = new VCard();
     $vCard->add(new Text($vCard, 'UID', $uid));
     return $vCard;
 }
Example #12
0
 /**
  * @expectedException \InvalidArgumentException
  */
 function testMimeDir()
 {
     $vcard = new VObject\Component\VCard();
     $vcard->add('PHOTO', ['a', 'b']);
 }
Example #13
0
 function testGetPartsNull()
 {
     $vcard = new VCard();
     $elem = $vcard->createProperty('ORG', null);
     $this->assertEquals(0, count($elem->getParts()));
 }
Example #14
0
 /**
  * @param CContact $oContact
  * @param \Sabre\VObject\Component\VCard $oVCard
  * @return void
  */
 public static function UpdateVCardPhonesFromContact($oContact, &$oVCard)
 {
     $bFindHome = false;
     $bFindWork = false;
     $bFindCell = false;
     $bFindHomeFax = false;
     $bFindWorkFax = false;
     $oVCardCopy = clone $oVCard;
     if (empty($oContact->HomePhone)) {
         $bFindHome = true;
     }
     if (empty($oContact->BusinessPhone)) {
         $bFindWork = true;
     }
     if (empty($oContact->HomeMobile)) {
         $bFindCell = true;
     }
     if (empty($oContact->HomeFax)) {
         $bFindHomeFax = true;
     }
     if (empty($oContact->BusinessFax)) {
         $bFindWorkFax = true;
     }
     if (isset($oVCardCopy->TEL)) {
         unset($oVCard->TEL);
         foreach ($oVCardCopy->TEL as $oTel) {
             if ($oTypes = $oTel['TYPE']) {
                 if ($oTypes->has('VOICE')) {
                     if ($oTypes->has('HOME')) {
                         if ($bFindHome) {
                             unset($oTel);
                         } else {
                             $oTel->setValue($oContact->HomePhone);
                             $bFindHome = true;
                         }
                     }
                     if ($oTypes->has('WORK')) {
                         if ($bFindWork) {
                             unset($oTel);
                         } else {
                             $oTel->setValue($oContact->BusinessPhone);
                             $bFindWork = true;
                         }
                     }
                     if ($oTypes->has('CELL')) {
                         if ($bFindCell) {
                             unset($oTel);
                         } else {
                             $oTel->setValue($oContact->HomeMobile);
                             $bFindCell = true;
                         }
                     }
                 } else {
                     if ($oTypes->has('FAX')) {
                         if ($oTypes->has('HOME')) {
                             if ($bFindHomeFax) {
                                 unset($oTel);
                             } else {
                                 $oTel->setValue($oContact->HomeFax);
                                 $bFindHomeFax = true;
                             }
                         }
                         if ($oTypes->has('WORK')) {
                             if ($bFindWorkFax) {
                                 unset($oTel);
                             } else {
                                 $oTel->setValue($oContact->BusinessFax);
                                 $bFindWorkFax = true;
                             }
                         }
                     }
                 }
             }
             if (isset($oTel)) {
                 $oVCard->add($oTel);
             }
         }
     }
     if (!$bFindHome) {
         $oVCard->add('TEL', $oContact->HomePhone, array('TYPE' => array('VOICE', 'HOME')));
     }
     if (!$bFindWork) {
         $oVCard->add('TEL', $oContact->BusinessPhone, array('TYPE' => array('VOICE', 'WORK')));
     }
     if (!$bFindCell) {
         $oVCard->add('TEL', $oContact->HomeMobile, array('TYPE' => array('VOICE', 'CELL')));
     }
     if (!$bFindHomeFax) {
         $oVCard->add('TEL', $oContact->HomeFax, array('TYPE' => array('FAX', 'HOME')));
     }
     if (!$bFindWorkFax) {
         $oVCard->add('TEL', $oContact->BusinessFax, array('TYPE' => array('FAX', 'WORK')));
     }
 }
Example #15
0
 /**
  * @param VCard $vCard
  * @param string $name
  * @param string|IImage $newValue
  * @return bool
  */
 private function propertyNeedsUpdate(VCard $vCard, $name, $newValue)
 {
     if (is_null($newValue)) {
         return false;
     }
     $value = $vCard->__get($name);
     if (!is_null($value)) {
         $value = $value->getValue();
         $newValue = $newValue instanceof IImage ? $newValue->data() : $newValue;
         return $value !== $newValue;
     }
     return true;
 }
Example #16
0
 function testValidateBadEncodingVCard21()
 {
     $document = new VCard(array('VERSION' => '2.1'));
     $property = $document->add('X-FOO', 'value');
     $property['ENCODING'] = 'B';
     $result = $property->validate();
     $this->assertEquals('ENCODING=B is not valid for this document type.', $result[0]['message']);
     $this->assertEquals(1, $result[0]['level']);
 }
Example #17
0
 public function testValidateBroken()
 {
     $datetime = '123';
     $vcard = new VObject\Component\VCard();
     $prop = $vcard->add('BDAY', $datetime);
     $this->assertEquals(array(array('level' => 3, 'message' => 'The supplied value (123) is not a correct DATE-AND-OR-TIME property', 'node' => $prop)), $prop->validate());
 }
Example #18
0
 /**
  * Validates the node for correctness.
  *
  * The following options are supported:
  *   - VCard::REPAIR - If something is broken, and automatic repair may
  *                    be attempted.
  *   - VCard::UPGRADE - If needed the vCard will be upgraded to version 3.0.
  *
  * An array is returned with warnings.
  *
  * Every item in the array has the following properties:
  *    * level - (number between 1 and 3 with severity information)
  *    * message - (human readable message)
  *    * node - (reference to the offending node)
  *
  * @param int $options
  * @return array
  */
 public function validate($options = 0)
 {
     $warnings = array();
     if ($options & self::UPGRADE) {
         $this->VERSION = self::DEFAULT_VERSION;
         foreach ($this->children as &$property) {
             $this->decodeProperty($property);
             $this->fixPropertyParameters($property);
             /* What exactly was I thinking here?
             			switch((string)$property->name) {
             				case 'LOGO':
             				case 'SOUND':
             				case 'PHOTO':
             					if(isset($property['TYPE']) && strpos((string)$property['TYPE'], '/') === false) {
             						$property['TYPE'] = 'image/' . strtolower($property['TYPE']);
             					}
             			}*/
         }
     }
     $version = $this->select('VERSION');
     if (count($version) !== 1) {
         $warnings[] = array('level' => 1, 'message' => 'The VERSION property must appear in the VCARD component exactly 1 time', 'node' => $this);
         if ($options & self::REPAIR) {
             $this->VERSION = self::DEFAULT_VERSION;
             if (!$options & self::UPGRADE) {
                 $options |= self::UPGRADE;
             }
         }
     } else {
         $version = (string) $this->VERSION;
         if ($version !== '2.1' && $version !== '3.0' && $version !== '4.0') {
             $warnings[] = array('level' => 1, 'message' => 'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.', 'node' => $this);
             if ($options & self::REPAIR) {
                 $this->VERSION = self::DEFAULT_VERSION;
                 if (!$options & self::UPGRADE) {
                     $options |= self::UPGRADE;
                 }
             }
         }
     }
     $fn = $this->select('FN');
     if (count($fn) !== 1) {
         $warnings[] = array('level' => 1, 'message' => 'The FN property must appear in the VCARD component exactly 1 time', 'node' => $this);
         if ($options & self::REPAIR && count($fn) === 0) {
             // We're going to try to see if we can use the contents of the
             // N property.
             if (isset($this->N)) {
                 $value = explode(';', (string) $this->N);
                 if (isset($value[1]) && $value[1]) {
                     $this->FN = $value[1] . ' ' . $value[0];
                 } else {
                     $this->FN = $value[0];
                 }
                 // Otherwise, the ORG property may work
             } elseif (isset($this->ORG)) {
                 $this->FN = (string) $this->ORG;
             } elseif (isset($this->EMAIL)) {
                 $this->FN = (string) $this->EMAIL;
             }
         }
     }
     $n = $this->select('N');
     if (count($n) !== 1) {
         $warnings[] = array('level' => 1, 'message' => 'The N property must appear in the VCARD component exactly 1 time', 'node' => $this);
         // TODO: Make a better effort parsing FN.
         if ($options & self::REPAIR && count($n) === 0) {
             // Take 2 first name parts of 'FN' and reverse.
             $slice = array_reverse(array_slice(explode(' ', (string) $this->FN), 0, 2));
             if (count($slice) < 2) {
                 // If not enought, add one more...
                 $slice[] = "";
             }
             $this->N = implode(';', $slice) . ';;;';
         }
     }
     if (!isset($this->UID)) {
         $warnings[] = array('level' => 1, 'message' => 'Every vCard must have a UID', 'node' => $this);
         if ($options & self::REPAIR) {
             $this->UID = Utils\Properties::generateUID();
         }
     }
     if ($options & self::REPAIR || $options & self::UPGRADE) {
         $now = new \DateTime();
         $this->REV = $now->format(\DateTime::W3C);
     }
     return array_merge(parent::validate($options), $warnings);
 }
Example #19
0
 /**
  * Creates a new or updates an existing vcard from save data.
  */
 private function create_vcard_from_save_data($save_data, $vcf = null)
 {
     unset($save_data['vcard']);
     if (!$vcf) {
         // create fresh minimal vcard
         $vcf = new VObject\Component\VCard(array('UID' => $save_data['cuid'], 'REV' => date('c')));
     } else {
         // update revision
         $vcf->REV = date("c");
     }
     // N is mandatory
     if (array_key_exists('kind', $save_data) && $save_data['kind'] === 'group') {
         $vcf->N = $save_data['name'];
     } else {
         $vcf->N = array($save_data['surname'], $save_data['firstname'], $save_data['middlename'], $save_data['prefix'], $save_data['suffix']);
     }
     $new_org_value = array();
     if (array_key_exists("organization", $save_data) && strlen($save_data['organization']) > 0) {
         $new_org_value[] = $save_data['organization'];
     }
     if (array_key_exists("department", $save_data)) {
         if (is_array($save_data['department'])) {
             foreach ($save_data['department'] as $key => $value) {
                 $new_org_value[] = $value;
             }
         } else {
             if (strlen($save_data['department']) > 0) {
                 $new_org_value[] = $save_data['department'];
             }
         }
     }
     if (count($new_org_value) > 0) {
         $vcf->ORG = $new_org_value;
     } else {
         unset($vcf->ORG);
     }
     // normalize date fields to RFC2425 YYYY-MM-DD date values
     foreach ($this->datefields as $key) {
         if (array_key_exists($key, $save_data) && strlen($save_data[$key]) > 0) {
             $val = rcube_strtotime($save_data[$key]);
             $save_data[$key] = date('Y-m-d', $val);
         }
     }
     // due to a bug in earlier versions of RCMCardDAV the PHOTO field was encoded base64 TWICE
     // This was recognized and fixed on 2013-01-09 and should be kept here until reasonable
     // certain that it's been fixed on users data, too.
     if (!array_key_exists('photo', $save_data) && strlen($vcf->PHOTO) > 0) {
         $save_data['photo'] = $vcf->PHOTO;
     }
     if (array_key_exists('photo', $save_data) && strlen($save_data['photo']) > 0 && base64_decode($save_data['photo'], true) !== FALSE) {
         self::$helper->debug("photo is base64 encoded. Decoding...");
         $i = 0;
         while (base64_decode($save_data['photo'], true) !== FALSE && $i++ < 10) {
             self::$helper->debug("Decoding {$i}...");
             $save_data['photo'] = base64_decode($save_data['photo'], true);
         }
         if ($i >= 10) {
             lef::$helper->warn("PHOTO of " . $save_data['uid'] . " does not decode after 10 attempts...");
         }
     }
     // process all simple attributes
     foreach ($this->vcf2rc['simple'] as $vkey => $rckey) {
         if (array_key_exists($rckey, $save_data)) {
             if (strlen($save_data[$rckey]) > 0) {
                 $vcf->{$vkey} = $save_data[$rckey];
             } else {
                 // delete the field
                 unset($vcf->{$vkey});
             }
         }
     }
     // Special handling for PHOTO
     if ($property = $vcf->PHOTO) {
         $property['ENCODING'] = 'B';
         $property['VALUE'] = 'BINARY';
     }
     // process all multi-value attributes
     foreach ($this->vcf2rc['multi'] as $vkey => $rckey) {
         // delete and fully recreate all entries
         // there is no easy way of mapping an address in the existing card
         // to an address in the save data, as subtypes may have changed
         unset($vcf->{$vkey});
         $stmap = array($rckey => 'other');
         foreach ($this->coltypes[$rckey]['subtypes'] as $subtype) {
             $stmap[$rckey . ':' . $subtype] = $subtype;
         }
         foreach ($stmap as $rcqkey => $subtype) {
             if (array_key_exists($rcqkey, $save_data)) {
                 $avalues = is_array($save_data[$rcqkey]) ? $save_data[$rcqkey] : array($save_data[$rcqkey]);
                 foreach ($avalues as $evalue) {
                     if (strlen($evalue) > 0) {
                         $prop = $vcf->add($vkey, $evalue);
                         $this->set_attr_label($vcf, $prop, $rckey, $subtype);
                         // set label
                     }
                 }
             }
         }
     }
     // process address entries
     unset($vcf->ADR);
     foreach ($this->coltypes['address']['subtypes'] as $subtype) {
         $rcqkey = 'address:' . $subtype;
         if (array_key_exists($rcqkey, $save_data)) {
             foreach ($save_data[$rcqkey] as $avalue) {
                 if (strlen($avalue['street']) || strlen($avalue['locality']) || strlen($avalue['region']) || strlen($avalue['zipcode']) || strlen($avalue['country'])) {
                     $prop = $vcf->add('ADR', array('', '', $avalue['street'], $avalue['locality'], $avalue['region'], $avalue['zipcode'], $avalue['country']));
                     $this->set_attr_label($vcf, $prop, 'address', $subtype);
                     // set label
                 }
             }
         }
     }
     return $vcf;
 }
Example #20
0
 /**
  * Get this task as a VObject. This can be turned into a vcard file data.
  * 
  * @return Sabre_VObject_Component 
  */
 public function toVObject()
 {
     $e = new Sabre\VObject\Component\VCard();
     $e->prodid = '-//Intermesh//NONSGML Group-Office ' . \GO::config()->version . '//EN';
     if (empty($this->uuid)) {
         $this->uuid = \GO\Base\Util\UUID::create('contact', $this->id);
         $this->save(true);
     }
     $e->uid = $this->uuid;
     $e->add('N', array($this->last_name, $this->first_name, $this->middle_name, $this->title, $this->suffix));
     $e->add('FN', $this->name);
     if (!empty($this->email)) {
         //			$p = new Sabre\VObject\Property('EMAIL',$this->email);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','INTERNET'));
         //			$e->add($p);
         $e->add('email', $this->email, array('type' => array('INTERNET')));
     }
     if (!empty($this->email2)) {
         //			$p = new Sabre\VObject\Property('EMAIL',$this->email2);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','HOME,INTERNET'));
         //			$e->add($p);
         $e->add('email', $this->email2, array('type' => array('HOME', 'INTERNET')));
     }
     if (!empty($this->email3)) {
         //			$p = new Sabre\VObject\Property('EMAIL',$this->email3);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','WORK,INTERNET'));
         //			$e->add($p);
         $e->add('email', $this->email3, array('type' => array('WORK', 'INTERNET')));
     }
     if (!empty($this->function)) {
         $e->add('TITLE', $this->function);
     }
     if (!empty($this->home_phone)) {
         //			$p = new Sabre\VObject\Property('TEL',$this->home_phone);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','HOME,VOICE'));
         //			$e->add($p);
         $e->add('TEL', $this->home_phone, array('type' => array('HOME', 'VOICE')));
     }
     if (!empty($this->work_phone)) {
         //			$p = new Sabre\VObject\Property('TEL',$this->work_phone);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','WORK,VOICE'));
         //			$e->add($p);
         $e->add('TEL', $this->work_phone, array('type' => array('WORK', 'VOICE')));
     }
     if (!empty($this->work_fax)) {
         //			$p = new Sabre\VObject\Property('TEL',$this->work_fax);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','WORK,FAX'));
         //			$e->add($p);
         $e->add('TEL', $this->work_fax, array('type' => array('WORK', 'FAX')));
     }
     if (!empty($this->fax)) {
         //			$p = new Sabre\VObject\Property('TEL',$this->fax);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','HOME,FAX'));
         //			$e->add($p);
         $e->add('TEL', $this->fax, array('type' => array('HOME', 'FAX')));
     }
     if (!empty($this->cellular)) {
         //			$p = new Sabre\VObject\Property('TEL',$this->cellular);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','CELL,VOICE'));
         //			$e->add($p);
         $e->add('TEL', $this->cellular, array('type' => array('CELL', 'VOICE')));
     }
     if (!empty($this->cellular2)) {
         //			$p = new Sabre\VObject\Property('TEL',$this->cellular2);
         //			$p->add(new \GO\Base\VObject\Parameter('TYPE','CELL,VOICE'));
         //			$e->add($p);
         $e->add('TEL', $this->cellular2, array('type' => array('CELL', 'VOICE')));
     }
     if (!empty($this->birthday)) {
         $e->add('BDAY', $this->birthday);
     }
     if (!empty($this->homepage)) {
         $e->add('URL', $this->homepage);
     }
     if (!empty($this->company)) {
         //			$e->add('ORG',$this->company->name,$this->department,$this->company->name2);
         //			$p = new Sabre\VObject\Property('ADR',';;'.$this->company->address.' '.$this->company->address_no,
         //				$this->company->city,$this->company->state,$this->company->zip,$this->company->country);
         //			$p->add('TYPE','WORK');
         //			$e->add($p);
         $e->add('ORG', array($this->company->name, $this->department, $this->company->name2));
         $e->add('ADR', array('', '', $this->company->address . ' ' . $this->company->address_no, $this->company->city, $this->company->state, $this->company->zip, $this->company->country), array('type' => 'WORK'));
         //			$p = new Sabre\VObject\Property('ADR',';;'.$this->company->post_address.' '.$this->company->post_address_no,
         //				$this->company->post_city,$this->company->post_state,$this->company->post_zip,$this->company->post_country);
         //			$e->add($p);
         $e->add('ADR', array('', '', $this->company->post_address . ' ' . $this->company->post_address_no, $this->company->post_city, $this->company->post_state, $this->company->post_zip, $this->company->post_country), array('type' => 'WORK'));
     }
     //		$p = new Sabre\VObject\Property('ADR',';;'.$this->address.' '.$this->address_no,
     //			$this->city,$this->state,$this->zip,$this->country);
     //		$p->add('TYPE','HOME');
     //		$e->add($p);
     //
     $e->add('ADR', array('', '', $this->address . ' ' . $this->address_no, $this->city, $this->state, $this->zip, $this->country), array('type' => 'HOME'));
     if (!empty($this->comment)) {
         $e->note = $this->comment;
     }
     //		$mtimeDateTime = new \DateTime('@'.$this->mtime);
     //		$rev = new Sabre_VObject_Element_DateTime('LAST-MODIFIED');
     //		$rev->setDateTime($mtimeDateTime, Sabre_VObject_Element_DateTime::UTC);
     //		$e->add($rev);
     $e->rev = gmdate("Y-m-d\\TH:m:s\\Z", $this->mtime);
     if ($this->getPhotoFile()->exists()) {
         $e->add('photo', $this->getPhotoFile()->getContents(), array('type' => 'JPEG', 'encoding' => 'b'));
     }
     //		$propModels = $this->vcardProperties->fetchAll(PDO::FETCH_ASSOC);
     //
     //		foreach ($propModels as $propModel) {
     //			$p = new Sabre\VObject\Property($propModel['name'],$propModel['value']);
     //			if(!empty($propModel['parameters'])){
     //				$paramStrings = explode(';',$propModel['parameters']);
     //				foreach ($paramStrings as $paramString) {
     //					if(!empty($paramString)){
     //						$paramStringArr = explode('=',$paramString);
     //
     //						$param = new \GO\Base\VObject\Parameter($paramStringArr[0]);
     //						if (!empty($paramStringArr[1]))
     //							$param->getValue() = $paramStringArr[1];
     //						$p->add($param);
     //					}
     //				}
     //			}
     //			$e->add($p);
     //		}
     return $e;
 }
Example #21
-1
 public function testCreateEmptyVCard()
 {
     $uid = 'uid';
     $expectedVCard = new VCard();
     $expectedVCard->add(new Text($expectedVCard, 'UID', $uid));
     $expectedVCardSerialized = $expectedVCard->serialize();
     $result = $this->invokePrivate($this->addressBookImpl, 'createEmptyVCard', [$uid]);
     $resultSerialized = $result->serialize();
     $this->assertSame($expectedVCardSerialized, $resultSerialized);
 }