Exemple #1
0
 /**
  * @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);
 }
 /**
  * @NoAdminRequired
  * @NoCSRFRequired
  */
 public function getPhoto($maxSize = 170)
 {
     // TODO: Cache resized photo
     $params = $this->request->urlParams;
     $etag = null;
     //$maxSize = isset($this->request['maxSize']) ? $this->request['maxSize'] : 170;
     $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
     $contact = $addressBook->getChild($params['contactId']);
     if (!$contact) {
         $response = new JSONResponse();
         $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
         return $response;
     }
     $image = new \OCP\Image();
     if (isset($contact->PHOTO) && $image->loadFromBase64((string) $contact->PHOTO)) {
         // OK
         $etag = md5($contact->PHOTO);
     } else {
         // Logo :-/
         if (isset($contact->LOGO) && $image->loadFromBase64((string) $contact->LOGO)) {
             // OK
             $etag = md5($contact->LOGO);
         }
     }
     if ($image->valid()) {
         $response = new ImageResponse($image);
         $lastModified = $contact->lastModified();
         // Force refresh if modified within the last minute.
         if (!is_null($lastModified)) {
             $response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
         }
         if (!is_null($etag)) {
             $response->setETag($etag);
         }
         if ($image->width() > $maxSize || $image->height() > $maxSize) {
             $image->resize($maxSize);
         }
         return $response;
     } else {
         $response = new JSONResponse();
         $response->bailOut(App::$l10n->t('Error getting user photo'));
         return $response;
     }
 }
 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);
 }
Exemple #4
0
 /**
  * Get the PHOTO or LOGO
  *
  * @return \OCP\Image|null
  */
 public function getPhoto()
 {
     $image = new \OCP\Image();
     if (isset($this->PHOTO) && $image->loadFromBase64((string) $this->PHOTO)) {
         return $image;
     } elseif (isset($this->LOGO) && $image->loadFromBase64((string) $this->LOGO)) {
         return $image;
     }
 }
Exemple #5
0
 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);
 }
Exemple #6
0
 /**
  * Get the PHOTO or LOGO
  *
  * @return \OCP\Image|null
  */
 public function getPhoto()
 {
     $image = new \OCP\Image();
     if (isset($this->PHOTO)) {
         $photo = $this->PHOTO;
     } elseif (isset($this->LOGO)) {
         $photo = $this->LOGO;
     } else {
         return null;
     }
     $photovalue = $photo->getValue();
     if ($photo instanceof \Sabre\VObject\Property\Uri && substr($photovalue, 0, 5) === 'data:') {
         $mimeType = substr($photovalue, 5, strpos($photovalue, ',') - 5);
         if (strpos($mimeType, ';')) {
             $mimeType = substr($mimeType, 0, strpos($mimeType, ';'));
         }
         $photovalue = substr($photovalue, strpos($photovalue, ',') + 1);
         if ($image->loadFromBase64($photovalue)) {
             return $image;
         }
     } elseif ($image->loadFromData($photovalue)) {
         return $image;
     }
     return null;
 }
Exemple #7
0
 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($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);
 }