diff --git a/lib/contact.php b/lib/contact.php index b30ea3e9..8eeaad71 100644 --- a/lib/contact.php +++ b/lib/contact.php @@ -64,15 +64,15 @@ class Contact extends VObject\VCard implements IPIMObject { $this->props['retrieved'] = false; $this->props['saved'] = false; - if(!is_null($data)) { - if($data instanceof VObject\VCard) { - foreach($data->children as $child) { + if (!is_null($data)) { + if ($data instanceof VObject\VCard) { + foreach ($data->children as $child) { $this->add($child); } $this->setRetrieved(true); - } elseif(is_array($data)) { - foreach($data as $key => $value) { - switch($key) { + } elseif (is_array($data)) { + foreach ($data as $key => $value) { + switch ($key) { case 'id': $this->props['id'] = $value; break; @@ -112,11 +112,11 @@ class Contact extends VObject\VCard implements IPIMObject { * @return array|null */ public function getMetaData() { - if(!$this->hasPermission(\OCP\PERMISSION_READ)) { + if (!$this->hasPermission(\OCP\PERMISSION_READ)) { throw new \Exception(self::$l10n->t('You do not have permissions to see this contact'), 403); } - if(!isset($this->props['displayname'])) { - if(!$this->retrieve()) { + if (!isset($this->props['displayname'])) { + if (!$this->retrieve()) { \OCP\Util::writeLog('contacts', __METHOD__.' error reading: '.print_r($this->props, true), \OCP\Util::ERROR); return null; } @@ -145,7 +145,9 @@ class Contact extends VObject\VCard implements IPIMObject { * @return string|null */ public function getOwner() { - return $this->props['parent']->getOwner(); + return isset($this->props['owner']) + ? $this->props['owner'] + : $this->getParent()->getOwner(); } /** @@ -159,7 +161,7 @@ class Contact extends VObject\VCard implements IPIMObject { * @return string|null */ public function getDisplayName() { - if(!$this->hasPermission(\OCP\PERMISSION_READ)) { + if (!$this->hasPermission(\OCP\PERMISSION_READ)) { throw new \Exception(self::$l10n->t('You do not have permissions to see this contact'), 403); } return isset($this->props['displayname']) @@ -203,7 +205,7 @@ class Contact extends VObject\VCard implements IPIMObject { public function getPermissions() { return isset($this->props['permissions']) ? $this->props['permissions'] - : $this->props['parent']->getPermissions(); + : $this->getParent()->getPermissions(); } /** @@ -248,7 +250,7 @@ class Contact extends VObject\VCard implements IPIMObject { * @return bool */ public function delete() { - if(!$this->hasPermission(\OCP\PERMISSION_DELETE)) { + if (!$this->hasPermission(\OCP\PERMISSION_DELETE)) { throw new \Exception(self::$l10n->t('You do not have permissions to delete this contact'), 403); } return $this->props['backend']->deleteContact( @@ -263,23 +265,23 @@ class Contact extends VObject\VCard implements IPIMObject { * @return bool */ public function save($force = false) { - if(!$this->hasPermission(\OCP\PERMISSION_UPDATE)) { + if (!$this->hasPermission(\OCP\PERMISSION_UPDATE)) { throw new \Exception(self::$l10n->t('You do not have permissions to update this contact'), 403); } - if($this->isSaved() && !$force) { + if ($this->isSaved() && !$force) { \OCP\Util::writeLog('contacts', __METHOD__.' Already saved: ' . print_r($this->props, true), \OCP\Util::DEBUG); return true; } - if(isset($this->FN)) { + if (isset($this->FN)) { $this->props['displayname'] = (string)$this->FN; } - if($this->getId()) { - if(!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_UPDATE)) { + if ($this->getId()) { + if (!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_UPDATE)) { throw new \Exception(self::$l10n->t('The backend for this contact does not support updating it'), 501); } - if($this->getBackend() + if ($this->getBackend() ->updateContact( $this->getParent()->getId(), $this->getId(), @@ -293,7 +295,7 @@ class Contact extends VObject\VCard implements IPIMObject { return false; } } else { - if(!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_CREATE)) { + if (!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_CREATE)) { throw new \Exception(self::$l10n->t('This backend not support adding contacts'), 501); } $this->props['id'] = $this->getBackend()->createContact( @@ -311,7 +313,7 @@ class Contact extends VObject\VCard implements IPIMObject { * @return bool */ public function retrieve() { - if($this->isRetrieved() || count($this->children) > 1) { + if ($this->isRetrieved() || count($this->children) > 1) { //\OCP\Util::writeLog('contacts', __METHOD__. ' children', \OCP\Util::DEBUG); return true; } else { @@ -330,20 +332,20 @@ class Contact extends VObject\VCard implements IPIMObject { //$this->children = $this->props['vcard']->children(); unset($this->props['vcard']); return true; - } elseif(!isset($this->props['carddata'])) { + } elseif (!isset($this->props['carddata'])) { $result = $this->props['backend']->getContact( $this->getParent()->getId(), $this->getId() ); - if($result) { - if(isset($result['vcard']) + if ($result) { + if (isset($result['vcard']) && $result['vcard'] instanceof VObject\VCard) { - foreach($result['vcard']->children() as $child) { + foreach ($result['vcard']->children() as $child) { $this->add($child); } $this->setRetrieved(true); return true; - } elseif(isset($result['carddata'])) { + } elseif (isset($result['carddata'])) { // Save internal values $data = $result['carddata']; $this->props['carddata'] = $result['carddata']; @@ -362,7 +364,7 @@ class Contact extends VObject\VCard implements IPIMObject { } else { \OCP\Util::writeLog('contacts', __METHOD__.' Error getting contact: ' . $this->getId(), \OCP\Util::DEBUG); } - } elseif(isset($this->props['carddata'])) { + } elseif (isset($this->props['carddata'])) { $data = $this->props['carddata']; } try { @@ -370,8 +372,8 @@ class Contact extends VObject\VCard implements IPIMObject { $data, \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES ); - if($obj) { - foreach($obj->children as $child) { + if ($obj) { + foreach ($obj->children as $child) { $this->add($child); } $this->setRetrieved(true); @@ -414,15 +416,15 @@ class Contact extends VObject\VCard implements IPIMObject { // For vCard 3.0 the type must be e.g. JPEG or PNG // For version 4.0 the full mimetype should be used. // https://tools.ietf.org/html/rfc2426#section-3.1.4 - if(strval($this->VERSION) === '4.0') { + if (strval($this->VERSION) === '4.0') { $type = $photo->mimeType(); } else { $type = explode('/', $photo->mimeType()); $type = strtoupper(array_pop($type)); } - if(isset($this->PHOTO)) { + if (isset($this->PHOTO)) { $property = $this->PHOTO; - if(!$property) { + if (!$property) { return false; } $property->setValue(strval($photo)); @@ -454,8 +456,8 @@ class Contact extends VObject\VCard implements IPIMObject { public function getPropertyIndexByChecksum($checksum) { $this->retrieve(); $idx = 0; - foreach($this->children as $i => &$property) { - if(substr(md5($property->serialize()), 0, 8) == $checksum ) { + foreach ($this->children as $i => &$property) { + if (substr(md5($property->serialize()), 0, 8) == $checksum ) { return $idx; } $idx += 1; @@ -472,8 +474,8 @@ class Contact extends VObject\VCard implements IPIMObject { */ public function getPropertyByChecksum($checksum) { $this->retrieve(); - foreach($this->children as $i => &$property) { - if(substr(md5($property->serialize()), 0, 8) == $checksum ) { + foreach ($this->children as $i => &$property) { + if (substr(md5($property->serialize()), 0, 8) == $checksum ) { return $property; } } @@ -505,13 +507,13 @@ class Contact extends VObject\VCard implements IPIMObject { * @return string new checksum */ public function setPropertyByChecksum($checksum, $name, $value, $parameters=array()) { - if($checksum === 'new') { + if ($checksum === 'new') { $property = Property::create($name); $this->add($property); } else { $property = $this->getPropertyByChecksum($checksum); } - switch($name) { + switch ($name) { case 'EMAIL': $value = strtolower($value); $property->setValue($value); @@ -524,15 +526,15 @@ class Contact extends VObject\VCard implements IPIMObject { } break; case 'IMPP': - if(is_null($parameters) || !isset($parameters['X-SERVICE-TYPE'])) { + if (is_null($parameters) || !isset($parameters['X-SERVICE-TYPE'])) { throw new \InvalidArgumentException(self::$l10n->t(' Missing IM parameter for: ') . $name. ' ' . $value, 412); } $serviceType = $parameters['X-SERVICE-TYPE']; - if(is_array($serviceType)) { + if (is_array($serviceType)) { $serviceType = $serviceType[0]; } $impp = Utils\Properties::getIMOptions($serviceType); - if(is_null($impp)) { + if (is_null($impp)) { throw new \UnexpectedValueException(self::$l10n->t('Unknown IM: ') . $serviceType, 415); } $value = $impp['protocol'] . ':' . $value; @@ -559,7 +561,7 @@ class Contact extends VObject\VCard implements IPIMObject { */ public function setPropertyByName($name, $value, $parameters=array()) { // TODO: parameters are ignored for now. - switch($name) { + switch ($name) { case 'BDAY': try { $date = New \DateTime($value); @@ -579,14 +581,14 @@ class Contact extends VObject\VCard implements IPIMObject { case 'N': case 'ORG': $property = $this->select($name); - if(count($property) === 0) { + if (count($property) === 0) { $property = \Sabre\VObject\Property::create($name); $this->add($property); } else { // Actually no idea why this works $property = array_shift($property); } - if(is_array($value)) { + if (is_array($value)) { $property->setParts($value); } else { $this->{$name} = $value; @@ -602,34 +604,34 @@ class Contact extends VObject\VCard implements IPIMObject { } protected function setParameters($property, $parameters, $reset = false) { - if(!$parameters) { + if (!$parameters) { return; } - if($reset) { + if ($reset) { $property->parameters = array(); } //debug('Setting parameters: ' . print_r($parameters, true)); - foreach($parameters as $key => $parameter) { + foreach ($parameters as $key => $parameter) { //debug('Adding parameter: ' . $key); - if(is_array($parameter)) { - foreach($parameter as $val) { - if(is_array($val)) { - foreach($val as $val2) { - if(trim($key) && trim($val2)) { + if (is_array($parameter)) { + foreach ($parameter as $val) { + if (is_array($val)) { + foreach ($val as $val2) { + if (trim($key) && trim($val2)) { //debug('Adding parameter: '.$key.'=>'.print_r($val2, true)); $property->add($key, strip_tags($val2)); } } } else { - if(trim($key) && trim($val)) { + if (trim($key) && trim($val)) { //debug('Adding parameter: '.$key.'=>'.print_r($val, true)); $property->add($key, strip_tags($val)); } } } } else { - if(trim($key) && trim($parameter)) { + if (trim($key) && trim($parameter)) { //debug('Adding parameter: '.$key.'=>'.print_r($parameter, true)); $property->add($key, strip_tags($parameter)); } @@ -638,7 +640,7 @@ class Contact extends VObject\VCard implements IPIMObject { } public function lastModified() { - if(!isset($this->props['lastmodified']) && !$this->isRetrieved()) { + if (!isset($this->props['lastmodified']) && !$this->isRetrieved()) { $this->retrieve(); } return isset($this->props['lastmodified']) @@ -662,24 +664,24 @@ class Contact extends VObject\VCard implements IPIMObject { * @return bool */ public function mergeFromArray(array $data) { - foreach($data as $name => $properties) { - if(in_array($name, array('PHOTO', 'UID'))) { + foreach ($data as $name => $properties) { + if (in_array($name, array('PHOTO', 'UID'))) { continue; } - if(!is_array($properties)) { + if (!is_array($properties)) { \OCP\Util::writeLog('contacts', __METHOD__.' not an array?: ' .$name. ' '.print_r($properties, true), \OCP\Util::DEBUG); } - if(in_array($name, Utils\Properties::$multiProperties)) { + if (in_array($name, Utils\Properties::$multiProperties)) { unset($this->{$name}); } - foreach($properties as $parray) { + foreach ($properties as $parray) { \OCP\Util::writeLog('contacts', __METHOD__.' adding: ' .$name. ' '.print_r($parray['value'], true) . ' ' . print_r($parray['parameters'], true), \OCP\Util::DEBUG); - if(in_array($name, Utils\Properties::$multiProperties)) { + if (in_array($name, Utils\Properties::$multiProperties)) { // TODO: wrap in try/catch, check return value $this->setPropertyByChecksum('new', $name, $parray['value'], $parray['parameters']); } else { // TODO: Check return value - if(!isset($this->{$name})) { + if (!isset($this->{$name})) { $this->setPropertyByName($name, $parray['value'], $parray['parameters']); } } @@ -699,21 +701,21 @@ class Contact extends VObject\VCard implements IPIMObject { */ public function mergeFromVCard(VCard $vcard) { $updated = false; - foreach($vcard->children as $property) { - if(in_array($property->name, array('REV', 'UID'))) { + foreach ($vcard->children as $property) { + if (in_array($property->name, array('REV', 'UID'))) { continue; } \OCP\Util::writeLog('contacts', __METHOD__.' merging: ' .$property->name, \OCP\Util::DEBUG); - if(in_array($property->name, Utils\Properties::$multiProperties)) { + if (in_array($property->name, Utils\Properties::$multiProperties)) { $ownproperties = $this->select($property->name); - if(count($ownproperties) === 0) { + if (count($ownproperties) === 0) { // We don't have any instances of this property, so just add it. $this->add($property); $updated = true; continue; } else { - foreach($ownproperties as $ownproperty) { - if(strtolower($property->value) === strtolower($ownproperty->value)) { + foreach ($ownproperties as $ownproperty) { + if (strtolower($property->value) === strtolower($ownproperty->value)) { // We already have this property, so skip both loops continue 2; } @@ -737,7 +739,7 @@ class Contact extends VObject\VCard implements IPIMObject { } public function __get($key) { - if(!$this->isRetrieved()) { + if (!$this->isRetrieved()) { $this->retrieve(); } @@ -745,7 +747,7 @@ class Contact extends VObject\VCard implements IPIMObject { } public function __isset($key) { - if(!$this->isRetrieved()) { + if (!$this->isRetrieved()) { $this->retrieve(); } @@ -753,22 +755,22 @@ class Contact extends VObject\VCard implements IPIMObject { } public function __set($key, $value) { - if(!$this->isRetrieved()) { + if (!$this->isRetrieved()) { $this->retrieve(); } parent::__set($key, $value); - if($key === 'FN') { + if ($key === 'FN') { $this->props['displayname'] = $value; } $this->setSaved(false); } public function __unset($key) { - if(!$this->isRetrieved()) { + if (!$this->isRetrieved()) { $this->retrieve(); } parent::__unset($key); - if($key === 'PHOTO') { + if ($key === 'PHOTO') { Properties::cacheThumbnail( $this->getBackend()->name, $this->getParent()->getId(), @@ -803,7 +805,7 @@ class Contact extends VObject\VCard implements IPIMObject { * @return \Sabre\VObject\Component\VCalendar|null */ public function getBirthdayEvent() { - if(!isset($this->BDAY)) { + if (!isset($this->BDAY)) { return; } $birthday = $this->BDAY;