Exemple #1
0
 /**
  * sets the users avatar
  * @param \OC_Image|resource|string $data OC_Image, imagedata or path to set a new avatar
  * @throws Exception if the provided file is not a jpg or png image
  * @throws Exception if the provided image is not valid
  * @throws \OC\NotSquareException if the image is not square
  * @return void
  */
 public function set($data)
 {
     if ($data instanceof OC_Image) {
         $img = $data;
         $data = $img->data();
     } else {
         $img = new OC_Image($data);
     }
     $type = substr($img->mimeType(), -3);
     if ($type === 'peg') {
         $type = 'jpg';
     }
     if ($type !== 'jpg' && $type !== 'png') {
         $l = \OC_L10N::get('lib');
         throw new \Exception($l->t("Unknown filetype"));
     }
     if (!$img->valid()) {
         $l = \OC_L10N::get('lib');
         throw new \Exception($l->t("Invalid image"));
     }
     if (!($img->height() === $img->width())) {
         throw new \OC\NotSquareException();
     }
     $this->view->unlink('avatar.jpg');
     $this->view->unlink('avatar.png');
     $this->view->file_put_contents('avatar.' . $type, $data);
 }
Exemple #2
0
 public function testMimeType()
 {
     $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/testimage.png');
     $this->assertEquals('image/png', $img->mimeType());
     $img = new \OC_Image(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.jpg'));
     $this->assertEquals('image/jpeg', $img->mimeType());
     $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.gif')));
     $this->assertEquals('image/gif', $img->mimeType());
     $img = new \OC_Image(null);
     $this->assertEquals('', $img->mimeType());
 }
 public function testMimeType()
 {
     $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/testimage.png');
     $this->assertEquals('image/png', $img->mimeType());
     $img = new \OC_Image(null);
     $this->assertEquals('', $img->mimeType());
     if (\OC_Util::runningOnWindows()) {
         $this->markTestSkipped('[Windows] Images created with imagecreate() are pngs on windows');
     }
     $img = new \OC_Image(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.jpg'));
     $this->assertEquals('image/jpeg', $img->mimeType());
     $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.gif')));
     $this->assertEquals('image/gif', $img->mimeType());
 }
Exemple #4
0
 public static function postAvatar($args)
 {
     \OC_JSON::checkLoggedIn();
     \OC_JSON::callCheck();
     $user = \OC_User::getUser();
     if (isset($_POST['path'])) {
         $path = stripslashes($_POST['path']);
         $view = new \OC\Files\View('/' . $user . '/files');
         $fileInfo = $view->getFileInfo($path);
         if ($fileInfo['encrypted'] === true) {
             $fileName = $view->toTmpFile($path);
         } else {
             $fileName = $view->getLocalFile($path);
         }
     } elseif (!empty($_FILES)) {
         $files = $_FILES['files'];
         if ($files['error'][0] === 0 && is_uploaded_file($files['tmp_name'][0]) && !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])) {
             \OC\Cache::set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
             $view = new \OC\Files\View('/' . $user . '/cache');
             $fileName = $view->getLocalFile('avatar_upload');
             unlink($files['tmp_name'][0]);
         }
     } else {
         $l = new \OC_L10n('core');
         \OC_JSON::error(array("data" => array("message" => $l->t("No image or file provided"))));
         return;
     }
     try {
         $image = new \OC_Image();
         $image->loadFromFile($fileName);
         $image->fixOrientation();
         if ($image->valid()) {
             \OC\Cache::set('tmpavatar', $image->data(), 7200);
             \OC_JSON::error(array("data" => "notsquare"));
         } else {
             $l = new \OC_L10n('core');
             $mimeType = $image->mimeType();
             if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
                 \OC_JSON::error(array("data" => array("message" => $l->t("Unknown filetype"))));
             }
             if (!$image->valid()) {
                 \OC_JSON::error(array("data" => array("message" => $l->t("Invalid image"))));
             }
         }
     } catch (\Exception $e) {
         \OC_JSON::error(array("data" => array("message" => $e->getMessage())));
     }
 }
 /**
  * @NoAdminRequired
  *
  * @return DataResponse|DataDisplayResponse
  */
 public function getTmpAvatar()
 {
     $tmpAvatar = $this->cache->get('tmpAvatar');
     if (is_null($tmpAvatar)) {
         return new DataResponse(['data' => ['message' => $this->l->t("No temporary profile picture available, try again")]], Http::STATUS_NOT_FOUND);
     }
     $image = new \OC_Image($tmpAvatar);
     $resp = new DataDisplayResponse($image->data(), Http::STATUS_OK, ['Content-Type' => $image->mimeType()]);
     $resp->setETag(crc32($image->data()));
     $resp->cacheFor(0);
     $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
     return $resp;
 }
 /**
  * @brief modifies a vcard property array with the image
  */
 public function updateImageProperty(&$property, $entry, $version = null)
 {
     $image = new \OC_Image();
     $image->loadFromData($entry);
     if (strcmp($version, '4.0') == 0) {
         $type = $image->mimeType();
     } else {
         $arrayType = explode('/', $image->mimeType());
         $type = strtoupper(array_pop($arrayType));
     }
     $property->add('ENCODING', 'b');
     $property->add('TYPE', $type);
     $property->setValue($image->__toString());
 }
Exemple #7
0
if (is_null($contact)) {
    OCP\Util::writeLog('contacts', 'photo.php. The VCard for ID ' . $id . ' is not RFC compatible', OCP\Util::ERROR);
} else {
    OCP\Response::enableCaching($caching);
    OC_Contacts_App::setLastModifiedHeader($contact);
    // Photo :-)
    if ($image->loadFromBase64($contact->getAsString('PHOTO'))) {
        // OK
        OCP\Response::setETagHeader(md5($contact->getAsString('PHOTO')));
    } else {
        // Logo :-/
        if ($image->loadFromBase64($contact->getAsString('LOGO'))) {
            // OK
            OCP\Response::setETagHeader(md5($contact->getAsString('LOGO')));
        }
    }
    if ($image->valid()) {
        $max_size = 200;
        if ($image->width() > $max_size || $image->height() > $max_size) {
            $image->resize($max_size);
        }
    }
}
if (!$image->valid()) {
    // Not found :-(
    getStandardImage();
    //$image->loadFromFile('img/person_large.png');
}
header('Content-Type: ' . $image->mimeType());
$image->show();
//echo OC_Contacts_App::$l10n->t('This card does not contain a photo.');
Exemple #8
0
 if ($image->width() <= 200 && $image->height() <= 200 || $image->resize(200)) {
     $vcard = OCA\Contacts\App::getContactVCard($id);
     if (!$vcard) {
         OC_Cache::remove($tmpkey);
         bailOut(OCA\Contacts\App::$l10n->t('Error getting contact object.'));
     }
     if ($vcard->__isset('PHOTO')) {
         OCP\Util::writeLog('contacts', 'savecrop.php: PHOTO property exists.', OCP\Util::DEBUG);
         $property = $vcard->__get('PHOTO');
         if (!$property) {
             OC_Cache::remove($tmpkey);
             bailOut(OCA\Contacts\App::$l10n->t('Error getting PHOTO property.'));
         }
         $property->setValue($image->__toString());
         $property->parameters[] = new Sabre\VObject\Parameter('ENCODING', 'b');
         $property->parameters[] = new Sabre\VObject\Parameter('TYPE', $image->mimeType());
         $vcard->__set('PHOTO', $property);
     } else {
         OCP\Util::writeLog('contacts', 'savecrop.php: files: Adding PHOTO property.', OCP\Util::DEBUG);
         // 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
         $type = $vcard->VERSION == '4.0' ? $image->mimeType() : strtoupper(array_pop(explode('/', $image->mimeType())));
         $vcard->add('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $type));
     }
     $now = new DateTime();
     $vcard->{'REV'} = $now->format(DateTime::W3C);
     if (!OCA\Contacts\VCard::edit($id, $vcard)) {
         bailOut(OCA\Contacts\App::$l10n->t('Error saving contact.'));
     }
     OCA\Contacts\App::cacheThumbnail($id, $image);
Exemple #9
0
     if ($image->width() <= 200 && $image->height() <= 200 || $image->resize(200)) {
         $vcard = OC_Contacts_App::getContactVCard($id);
         if (!$vcard) {
             OC_Cache::remove($tmpkey);
             bailOut(OC_Contacts_App::$l10n->t('Error getting contact object.'));
         }
         if ($vcard->__isset('PHOTO')) {
             OCP\Util::writeLog('contacts', 'savecrop.php: PHOTO property exists.', OCP\Util::DEBUG);
             $property = $vcard->__get('PHOTO');
             if (!$property) {
                 OC_Cache::remove($tmpkey);
                 bailOut(OC_Contacts_App::$l10n->t('Error getting PHOTO property.'));
             }
             $property->setValue($image->__toString());
             $property->parameters[] = new Sabre_VObject_Parameter('ENCODING', 'b');
             $property->parameters[] = new Sabre_VObject_Parameter('TYPE', $image->mimeType());
             $vcard->__set('PHOTO', $property);
         } else {
             OCP\Util::writeLog('contacts', 'savecrop.php: files: Adding PHOTO property.', OCP\Util::DEBUG);
             $vcard->addProperty('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $image->mimeType()));
         }
         $now = new DateTime();
         $vcard->setString('REV', $now->format(DateTime::W3C));
         if (!OC_Contacts_VCard::edit($id, $vcard)) {
             bailOut(OC_Contacts_App::$l10n->t('Error saving contact.'));
         }
         OCP\JSON::success(array('data' => array('id' => $id, 'width' => $image->width(), 'height' => $image->height(), 'lastmodified' => OC_Contacts_App::lastModified($vcard)->format('U'))));
     } else {
         bailOut(OC_Contacts_App::$l10n->t('Error resizing image'));
     }
 } else {
Exemple #10
0
 OCP\Util::writeLog('contacts', 'savecrop.php, x: ' . $x1 . ' y: ' . $y1 . ' w: ' . $w . ' h: ' . $h, OCP\Util::DEBUG);
 if ($image->crop($x1, $y1, $w, $h)) {
     if ($image->width() <= 200 && $image->height() <= 200 || $image->resize(200)) {
         $vcard = OCA\Contacts\App::getContactVCard($id);
         if (!$vcard) {
             OC_Cache::remove($tmpkey);
             bailOut(OCA\Contacts\App::$l10n->t('Error getting contact object.'));
         }
         if ($vcard->__isset('PHOTO')) {
             OCP\Util::writeLog('contacts', 'savecrop.php: PHOTO property exists.', OCP\Util::DEBUG);
             $property = $vcard->__get('PHOTO');
             if (!$property) {
                 OC_Cache::remove($tmpkey);
                 bailOut(OCA\Contacts\App::$l10n->t('Error getting PHOTO property.'));
             }
             $type = imageType((string) $vcard->VERSION, $image->mimeType());
             $property->setValue($image->__toString());
             $property->parameters = array();
             $property->parameters[] = new Sabre\VObject\Parameter('ENCODING', 'b');
             $property->parameters[] = new Sabre\VObject\Parameter('TYPE', $type);
             $vcard->__set('PHOTO', $property);
         } else {
             OCP\Util::writeLog('contacts', 'savecrop.php: Adding PHOTO property.', OCP\Util::DEBUG);
             $type = imageType((string) $vcard->VERSION, $image->mimeType());
             $vcard->add('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $type));
         }
         $now = new DateTime();
         $vcard->{'REV'} = $now->format(DateTime::W3C);
         if (!OCA\Contacts\VCard::edit($id, $vcard)) {
             bailOut(OCA\Contacts\App::$l10n->t('Error saving contact.'));
         }
 /**
  * @brief modifies a vcard property array with the image
  */
 public function updateVCardImageProperty(&$v_property, $ldap_entry, $version)
 {
     for ($i = 0; $i < count($v_property); $i++) {
         $image = new \OC_Image();
         $image->loadFromData($ldap_entry);
         if (strcmp($version, '4.0') == 0) {
             $type = $image->mimeType();
         } else {
             $arrayType = explode('/', $image->mimeType());
             $type = strtoupper(array_pop($arrayType));
         }
         $v_property[$i]->add('ENCODING', 'b');
         $v_property[$i]->add('TYPE', $type);
         $v_property[$i]->setValue($image->__toString());
     }
 }
Exemple #12
0
 /**
  * Parse contact details into the Contact object
  * @param array $contact
  * @return Sabre\VObject\Component 
  */
 public static function toVcard($contact)
 {
     $vcard = new \Sabre\VObject\Component('VCARD');
     $vcard->setUID();
     $vcard = self::_addPropertyStrings($contact, $vcard);
     if (isset($contact[self::CONTACT_PHONE])) {
         foreach ($contact[self::CONTACT_PHONE] as $phone) {
             if (!isset($phone['value'])) {
                 continue;
             }
             $vcard->addProperty(self::CONTACT_PHONE, $phone['value']);
             $line = count($vcard->children) - 1;
             foreach ($phone['type'] as $type) {
                 $vcard->children[$line]->parameters[] = new \Sabre\VObject\Parameter('TYPE', $type);
             }
         }
     }
     if (isset($contact[self::CONTACT_CATEGORIES])) {
         $categories = array();
         foreach ($contact[self::CONTACT_CATEGORIES] as $categoryId) {
             $categoryData = Request::getGroupDetails($categoryId);
             preg_match('/<title>(.*)<\\/title>/i', $categoryData, $matches);
             if (@$matches[1]) {
                 $categories[] = $matches[1];
             }
         }
         if (count($categories)) {
             $vcard->setString(self::CONTACT_CATEGORIES, implode(',', $categories));
         }
     }
     if (isset($contact[self::CONTACT_ADDRESS])) {
         foreach ($contact[self::CONTACT_ADDRESS] as $address) {
             $vcard->addProperty(self::CONTACT_ADDRESS, $address);
             $line = count($vcard->children) - 1;
             $vcard->children[$line]->parameters[] = new \Sabre\VObject\Parameter('TYPE', $address['type']);
         }
     }
     if (isset($contact[self::CONTACT_EMAIL])) {
         foreach ($contact[self::CONTACT_EMAIL] as $email) {
             $vcard->addProperty(self::CONTACT_EMAIL, $email['value']);
             $line = count($vcard->children) - 1;
             $vcard->children[$line]->parameters[] = new \Sabre\VObject\Parameter('TYPE', $email['type']);
         }
     }
     if (isset($contact[self::CONTACT_PHOTO]) && !empty($contact[self::CONTACT_PHOTO])) {
         $data = Request::getContactImage($contact[self::CONTACT_PHOTO]);
         $img = new \OC_Image();
         if ($img->loadFromData($data)) {
             $vcard->addProperty(self::CONTACT_PHOTO, $img->__toString(), array('ENCODING' => 'b', 'TYPE' => $img->mimeType()));
         } else {
             App::log('Unable to parse the image provided by Google. ', \OCP\Util::WARN);
         }
     }
     return $vcard;
 }