/** * @brief reads jpegPhoto and set is as avatar if available * @param $uid string ownCloud user name * @param $dn string the user's LDAP DN * @return void */ private function updateAvatar($uid, $dn) { $hasLoggedIn = \OCP\Config::getUserValue($uid, 'user_ldap', 'firstLoginAccomplished', 0); $lastChecked = \OCP\Config::getUserValue($uid, 'user_ldap', 'lastJpegPhotoLookup', 0); if ($hasLoggedIn !== '1' || time() - intval($lastChecked) < 86400) { //update only once a day return; } $avatarImage = $this->getAvatarImage($uid, $dn); if ($avatarImage === false) { //not set, nothing left to do; return; } $image = new \OCP\Image(); $image->loadFromBase64(base64_encode($avatarImage)); if (!$image->valid()) { \OCP\Util::writeLog('user_ldap', 'jpegPhoto data invalid for ' . $dn, \OCP\Util::ERROR); return; } //make sure it is a square and not bigger than 128x128 $size = min(array($image->width(), $image->height(), 128)); if (!$image->centerCrop($size)) { \OCP\Util::writeLog('user_ldap', 'croping image for avatar failed for ' . $dn, \OCP\Util::ERROR); return; } if (!\OC\Files\Filesystem::$loaded) { \OC_Util::setupFS($uid); } $avatarManager = \OC::$server->getAvatarManager(); $avatar = $avatarManager->getAvatar($uid); $avatar->set($image); }
public function cacheThumbnail(\OCP\Image $image = null, $remove = false, $update = false) { $key = self::THUMBNAIL_PREFIX . $this->combinedKey(); //\OC_Cache::remove($key); if (\OC_Cache::hasKey($key) && $image === null && $remove === false && $update === false) { return \OC_Cache::get($key); } if ($remove) { \OC_Cache::remove($key); if (!$update) { return false; } } if (is_null($image)) { $this->retrieve(); $image = new \OCP\Image(); if (!isset($this->PHOTO) && !isset($this->LOGO)) { return false; } if (!$image->loadFromBase64((string) $this->PHOTO)) { if (!$image->loadFromBase64((string) $this->LOGO)) { return false; } } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month \OC_Cache::set($key, strval($image), 3000000); \OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG); return \OC_Cache::get($key); }
public static function cacheThumbnail($backendName, $addressBookId, $contactId, \OCP\Image $image = null, $vCard = null, $options = array()) { $cache = \OC::$server->getCache(); $key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId; //$cache->remove($key); $haskey = $cache->hasKey($key); if (!array_key_exists('remove', $options) && !array_key_exists('update', $options)) { if ($cache->hasKey($key) && $image === null) { return $cache->get($key); } } else { if (isset($options['remove']) && $options['remove'] === false && (isset($options['update']) && $options['update'] === false)) { return $cache->get($key); } } if (isset($options['remove']) && $options['remove']) { $cache->remove($key); if (!isset($options['update']) || !$options['update']) { return false; } } if (is_null($image)) { if (is_null($vCard)) { $app = new App(); $vCard = $app->getContact($backendName, $addressBookId, $contactId); } $image = new \OCP\Image(); if (!isset($vCard->PHOTO) || !$image->loadFromBase64((string) $vCard->PHOTO)) { return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month $cache->set($key, strval($image), 3000000); return $cache->get($key); }
public static function cacheThumbnail($id, \OC_Image $image = null, $remove = false, $update = false) { $key = self::THUMBNAIL_PREFIX . $id; if (\OC::$server->getCache()->hasKey($key) && $image === null && $remove === false && $update === false) { return \OC::$server->getCache()->get($key); } if ($remove) { \OC::$server->getCache()->remove($key); if (!$update) { return false; } } if (is_null($image)) { $vcard = self::getContactVCard($id); // invalid vcard if (is_null($vcard)) { \OCP\Util::writeLog(self::$appname, __METHOD__ . ' The VCard for ID ' . $id . ' is not RFC compatible', \OCP\Util::ERROR); return false; } $image = new \OCP\Image(); if (!isset($vcard->PHOTO)) { return false; } if (!$image->loadFromBase64((string) $vcard->PHOTO)) { return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog(self::$appname, 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog(self::$appname, 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id, \OCP\Util::ERROR); return false; } // Cache for around a month \OC::$server->getCache()->set($key, $image->data(), 3000000); \OCP\Util::writeLog(self::$appname, 'Caching ' . $id, \OCP\Util::DEBUG); return \OC::$server->getCache()->get($key); }
public static function cacheThumbnail($backendName, $addressBookId, $contactId, \OCP\Image $image = null, $vCard = null, $options = array()) { $cache = \OC::$server->getCache(); $key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId; //$cache->remove($key); $haskey = $cache->hasKey($key); if (!array_key_exists('remove', $options) && !array_key_exists('update', $options)) { if ($cache->hasKey($key) && $image === null) { return $cache->get($key); } } else { if (isset($options['remove']) && $options['remove'] === false && (isset($options['update']) && $options['update'] === false)) { return $cache->get($key); } } if (isset($options['remove']) && $options['remove']) { $cache->remove($key); if (!isset($options['update']) || !$options['update']) { return false; } } if (is_null($image)) { if (is_null($vCard)) { $app = new App(); $vCard = $app->getContact($backendName, $addressBookId, $contactId); } if (!isset($vCard->PHOTO)) { return false; } $image = new \OCP\Image(); $photostring = (string) $vCard->PHOTO; if ($vCard->PHOTO instanceof \Sabre\VObject\Property\Uri && substr($photostring, 0, 5) === 'data:') { $mimeType = substr($photostring, 5, strpos($photostring, ',') - 5); if (strpos($mimeType, ';')) { $mimeType = substr($mimeType, 0, strpos($mimeType, ';')); } $photostring = substr($photostring, strpos($photostring, ',') + 1); } if (!$image->loadFromBase64($photostring)) { #\OCP\Util::writeLog('contacts', __METHOD__.', photo: ' . print_r($photostring, true), \OCP\Util::DEBUG); return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month $cache->set($key, strval($image), 3000000); return $cache->get($key); }