/**
  * downloads an image/thumbnail at a given size
  *
  * @param unknown_type $application
  * @param string $id
  * @param string $location
  * @param int $width
  * @param int $height
  * @param int $ratiomode
  */
 public function getImage($application, $id, $location, $width, $height, $ratiomode)
 {
     $this->checkAuth();
     // close session to allow other requests
     Tinebase_Session::writeClose(true);
     $clientETag = null;
     $ifModifiedSince = null;
     if (isset($_SERVER['If_None_Match'])) {
         $clientETag = trim($_SERVER['If_None_Match'], '"');
         $ifModifiedSince = trim($_SERVER['If_Modified_Since'], '"');
     } elseif (isset($_SERVER['HTTP_IF_NONE_MATCH']) && isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
         $clientETag = trim($_SERVER['HTTP_IF_NONE_MATCH'], '"');
         $ifModifiedSince = trim($_SERVER['HTTP_IF_MODIFIED_SINCE'], '"');
     }
     if ($application == 'Tinebase' && $location == 'tempFile') {
         $tempFile = Tinebase_TempFile::getInstance()->getTempFile($id);
         $imgInfo = Tinebase_ImageHelper::getImageInfoFromBlob(file_get_contents($tempFile->path));
         $image = new Tinebase_Model_Image($imgInfo + array('application' => $application, 'id' => $id, 'location' => $location));
     } else {
         $image = Tinebase_Controller::getInstance()->getImage($application, $id, $location);
     }
     $serverETag = sha1($image->blob . $width . $height . $ratiomode);
     // cache for 3600 seconds
     $maxAge = 3600;
     header('Cache-Control: private, max-age=' . $maxAge);
     header("Expires: " . gmdate('D, d M Y H:i:s', Tinebase_DateTime::now()->addSecond($maxAge)->getTimestamp()) . " GMT");
     // overwrite Pragma header from session
     header("Pragma: cache");
     // if the cache id is still valid
     if ($clientETag == $serverETag) {
         header("Last-Modified: " . $ifModifiedSince);
         header("HTTP/1.0 304 Not Modified");
         header('Content-Length: 0');
     } else {
         #$cache = Tinebase_Core::getCache();
         #if ($cache->test($serverETag) === true) {
         #    $image = $cache->load($serverETag);
         #} else {
         if ($width != -1 && $height != -1) {
             Tinebase_ImageHelper::resize($image, $width, $height, $ratiomode);
         }
         #    $cache->save($image, $serverETag);
         #}
         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
         header('Content-Type: ' . $image->mime);
         header('Etag: "' . $serverETag . '"');
         flush();
         die($image->blob);
     }
 }
 /**
  * saves image to db
  *
  * @param  string $contactId
  * @param  blob $imageData
  * @return blob|string
  */
 public function _saveImage($contactId, $imageData)
 {
     if (!empty($imageData)) {
         // make sure that we got a valid image blob
         try {
             Tinebase_ImageHelper::getImageInfoFromBlob($imageData);
         } catch (Exception $e) {
             Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Invalid image blob data, preserving old image');
             Tinebase_Exception::log($e);
             return $this->getImage($contactId);
         }
     }
     if (!empty($imageData)) {
         $currentImg = $this->getImage($contactId);
         if (empty($currentImg)) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Creating contact image.');
             }
             $this->_db->insert($this->_tablePrefix . 'addressbook_image', array('contact_id' => $contactId, 'image' => base64_encode($imageData)));
         } else {
             if ($currentImg !== $imageData) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating contact image.');
                 }
                 $where = array($this->_db->quoteInto($this->_db->quoteIdentifier('contact_id') . ' = ?', $contactId));
                 $this->_db->update($this->_tablePrefix . 'addressbook_image', array('image' => base64_encode($imageData)), $where);
             }
         }
     } else {
         $this->_db->delete($this->_tablePrefix . 'addressbook_image', $this->_db->quoteInto($this->_db->quoteIdentifier('contact_id') . ' = ?', $contactId));
     }
     return $imageData;
 }
 /**
  * returns contact image
  * 
  * @param   string $_identifier record identifier
  * @param   string $_location not used, required by interface
  * @return  Tinebase_Model_Image
  * @throws  Addressbook_Exception_NotFound if no image found
  */
 public function getImage($_identifier, $_location = '')
 {
     // get contact to ensure user has read rights
     $image = Addressbook_Controller_Contact::getInstance()->getImage($_identifier);
     if (empty($image)) {
         throw new Addressbook_Exception_NotFound('Contact has no image.');
     }
     $imageInfo = Tinebase_ImageHelper::getImageInfoFromBlob($image);
     return new Tinebase_Model_Image($imageInfo + array('id' => $_identifier, 'application' => $this->_applicationName, 'data' => $image));
 }
 /**
  * gets image info and data
  * 
  * @param   string $application application which manages the image
  * @param   string $identifier identifier of image/record
  * @param   string $location optional additional identifier
  * @return  Tinebase_Model_Image
  * @throws  Tinebase_Exception_NotFound
  * @throws  Tinebase_Exception_UnexpectedValue
  */
 public function getImage($application, $identifier, $location = '')
 {
     if ($location === 'vfs') {
         $node = Tinebase_FileSystem::getInstance()->get($identifier);
         $path = Tinebase_Model_Tree_Node_Path::STREAMWRAPPERPREFIX . Tinebase_FileSystem::getInstance()->getPathOfNode($node, true);
         $image = Tinebase_ImageHelper::getImageInfoFromBlob(file_get_contents($path));
     } else {
         if ($application == 'Tinebase' && $location == 'tempFile') {
             $tempFile = Tinebase_TempFile::getInstance()->getTempFile($identifier);
             $image = Tinebase_ImageHelper::getImageInfoFromBlob(file_get_contents($tempFile->path));
         } else {
             $appController = Tinebase_Core::getApplicationInstance($application);
             if (!method_exists($appController, 'getImage')) {
                 throw new Tinebase_Exception_NotFound("{$application} has no getImage function.");
             }
             $image = $appController->getImage($identifier, $location);
         }
     }
     if (!$image instanceof Tinebase_Model_Image) {
         if (is_array($image)) {
             $image = new Tinebase_Model_Image($image + array('application' => $application, 'id' => $identifier, 'location' => $location));
         } else {
             throw new Tinebase_Exception_UnexpectedValue('broken image');
         }
     }
     return $image;
 }
 /**
  * returns image from given blob
  *
  * @param  string $_blob
  * @return Tinebase_Model_Image
  */
 public static function getImageFromBlob($_blob)
 {
     return new Tinebase_Model_Image(Tinebase_ImageHelper::getImageInfoFromBlob($_blob), true);
 }
 /**
  * test that exception gets thrown when given blog does not represent a valid image
  *
  */
 public function testGetImageInfoFromBlobException()
 {
     $rwongBlob = file_get_contents(__FILE__);
     $this->setExpectedException('Tinebase_Exception_UnexpectedValue');
     Tinebase_ImageHelper::getImageInfoFromBlob($rwongBlob);
 }