Copyright 2001-2016 Horde LLC (http://www.horde.org/) See the enclosed file COPYING for license information (GPL). If you did not receive this file, see http://www.horde.org/licenses/gpl.
Author: Michael J. Rubinsky (mrubinsk@horde.org)
Inheritance: implements Serializable
示例#1
0
 /**
  * Handle extracting images from uploaded zip files.
  *
  * @param string $filename  The local path to the zip file.
  *
  * @return array  An array of the resulting image_ids.
  * @throws Ansel_Exception
  */
 private function _handleZip($filename)
 {
     $image_ids = array();
     /* See if we can use the zip extension for reading the file. */
     if (Horde_Util::extensionExists('zip')) {
         $zip = new ZipArchive();
         if ($zip->open($filename) !== true) {
             throw new Ansel_Exception(_("Could not open zip archive."));
         }
         /* Iterate the archive */
         for ($z = 0; $z < $zip->numFiles; $z++) {
             $zinfo = $zip->statIndex($z);
             if ($this->_isMetaFile($zinfo['name'])) {
                 continue;
             }
             /* Extract the image */
             $stream = $zip->getStream($zinfo['name']);
             $zdata = stream_get_contents($stream);
             if (!strlen($zdata)) {
                 throw new Ansel_Exception(_("Could not extract image data from zip archive."));
             }
             /* Save the image */
             $image_id = $this->_gallery->addImage(array('image_filename' => $zinfo['name'], 'image_caption' => '', 'data' => $zdata));
             $image_ids[] = $image_id;
             unset($zdata);
         }
         $zip->close();
         unset($zip);
     } else {
         /* Receiving zip data, but extension not loaded */
         $data = file_get_contents($filename);
         /* Get list of images */
         try {
             $zip = Horde_Compress::factory('zip');
             $files = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_LIST));
         } catch (Horde_Compress_Exception $e) {
             throw new Ansel_Exception($e);
             continue;
         }
         /* Iterate the archive */
         foreach ($files as $key => $zinfo) {
             if ($this->_isMetaFile($zinfo['name'])) {
                 continue;
             }
             /* Extract the image */
             try {
                 $zdata = $zip->decompress($data, array('action' => Horde_Compress_Zip::ZIP_DATA, 'info' => $files, 'key' => $key));
             } catch (Horde_Compress_Exception $e) {
                 throw new Ansel_Exception($e);
             }
             /* Add the image */
             $image_id = $this->_gallery->addImage(array('image_filename' => $zinfo['name'], 'image_caption' => '', 'data' => $zdata));
             $image_ids[] = $image_id;
             unset($zdata);
         }
         unset($zip);
         unset($data);
     }
     return $image_ids;
 }
示例#2
0
文件: Gallery.php 项目: horde/horde
 /**
  * @param boolean $retry
  *
  * @return Ansel_Gallery
  */
 private function _getGallery($retry = false)
 {
     // Make sure we haven't already selected a gallery.
     if ($this->_gallery instanceof Ansel_Gallery) {
         return $this->_gallery;
     }
     // Get the gallery object and cache it.
     if (isset($this->_params['gallery']) && $this->_params['gallery'] != '__random') {
         $this->_gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($this->_params['gallery']);
     } else {
         $this->_gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getRandomGallery();
     }
     // Protect at least a little bit against getting an empty gallery. We
     // can't just loop until we get one with images since it's possible we
     // actually don't *have* any with images yet.
     if ($this->_params['gallery'] == '__random' && !empty($this->_gallery) && !$this->_gallery->countImages() && $this->_gallery->hasSubGalleries() && !$retry) {
         $this->_gallery = null;
         $this->_gallery = $this->_getGallery(true);
     }
     if (empty($this->_gallery)) {
         throw new Horde_Exception_NotFound(_("Gallery does not exist."));
     } elseif (!$this->_gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW) || !$this->_gallery->isOldEnough() || $this->_gallery->hasPasswd()) {
         throw new Horde_Exception_PermissionDenied(_("Access denied viewing this gallery."));
     }
     // Return the gallery.
     return $this->_gallery;
 }
示例#3
0
 /**
  * @return Ansel_Gallery
  */
 private function _getGallery()
 {
     /* Make sure we haven't already selected a gallery. */
     if ($this->_gallery instanceof Ansel_Gallery) {
         return $this->_gallery;
     }
     /* Get the gallery object and cache it. */
     if (isset($this->_params['gallery']) && $this->_params['gallery'] != '__random') {
         $this->_gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($this->_params['gallery']);
     } else {
         $this->_gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getRandomGallery();
     }
     if (empty($this->_gallery)) {
         throw new Horde_Exception_NotFound(_("Gallery not found."));
     } elseif (!$this->_gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::READ)) {
         throw new Horde_Exception_PermissionDenied(_("Access denied viewing this gallery."));
     }
     /* Return a reference to the gallery. */
     return $this->_gallery;
 }
示例#4
0
文件: Base.php 项目: raz0rsdge/horde
 /**
  * JSON representation of this gallery's images. We don't use
  * Ansel_Gallery::toJson() on purpose since that is a general jsonification
  * of the gallery data. This method is specific to the view, paging, links
  * etc...
  *
  * @param Ansel_Gallery $gallery  The gallery to represent in this view
  * @param array $params           An array of parameters for this method:
  *   <pre>
  *      full       - Should a full URL be generated? [false]
  *      from       - Starting image count [0]
  *      count      - The number of images to include (starting at from) [0]
  *      image_view - The type of ImageGenerator to obtain the src url for. [screen]
  *      view_links - Should the JSON include links to the Image and/or Gallery View? [false]
  *      perpage    - Number of images per page [from user prefs]
  *   </pre>
  *
  * @return string  A serialized JSON array.
  */
 public static function json(Ansel_Gallery $gallery, $params = array())
 {
     global $conf, $prefs;
     $default = array('full' => false, 'from' => 0, 'count' => 0, 'image_view' => 'screen', 'view_links' => false, 'perpage' => $prefs->getValue('tilesperpage', $conf['thumbnail']['perpage']));
     $params = array_merge($default, $params);
     $json = array();
     $curimage = 0;
     $curpage = 0;
     if (empty($params['images'])) {
         $images = $gallery->getImages($params['from'], $params['count']);
     }
     $style = $gallery->getStyle();
     if ($params['image_view'] == 'thumb' && !empty($params['generator'])) {
         $style->thumbstyle = $params['generator'];
     }
     foreach ($images as $image) {
         // Calculate the page this image will appear on in the gallery view.
         if (++$curimage > $params['perpage']) {
             ++$curpage;
             $curimage = 0;
         }
         $data = array((string) Ansel::getImageUrl($image->id, $params['image_view'], $params['full'], $style), htmlspecialchars($image->filename), $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($image->caption, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL)), $image->id, $curpage);
         if ($params['view_links']) {
             $data[] = (string) Ansel::getUrlFor('view', array('gallery' => $gallery->id, 'slug' => $gallery->get('slug'), 'image' => $image->id, 'view' => 'Image', 'page' => $curpage), true);
             $data[] = (string) Ansel::getUrlFor('view', array('gallery' => $image->gallery, 'slug' => $gallery->get('slug'), 'view' => 'Gallery'), true);
         }
         // Source, Width, Height, Name, Caption, Image Id, Gallery Page
         $json[] = $data;
     }
     return Horde_Serialize::serialize($json, Horde_Serialize::JSON);
 }
示例#5
0
 /**
  * Removes an Ansel_Gallery.
  *
  * @param Ansel_Gallery $gallery  The gallery to delete
  *
  * @throws Ansel_Exception
  */
 public function removeGallery(Ansel_Gallery $gallery)
 {
     // Get any children and empty them
     $children = $gallery->getChildren(null, null, true);
     foreach ($children as $child) {
         $this->emptyGallery($child);
         $child->setTags(array());
     }
     // Now empty the selected gallery of images
     $this->emptyGallery($gallery);
     // Clear all the tags.
     $gallery->setTags(array());
     // Get the parent, if it exists, before we delete the gallery.
     $parent = $gallery->getParent();
     $id = $gallery->id;
     // Delete the gallery from storage
     try {
         $this->_shares->removeShare($gallery->getShare());
     } catch (Horde_Share_Exception $e) {
         throw new Ansel_Exception($e);
     } catch (Horde_Exception_NotFound $e) {
         throw new Ansel_Exception($e);
     }
     // Expire the cache
     if ($GLOBALS['conf']['ansel_cache']['usecache']) {
         $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $id);
     }
     // See if we need to clear the has_subgalleries field
     if ($parent instanceof Ansel_Gallery) {
         if (!$parent->countChildren($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW, false)) {
             $parent->set('has_subgalleries', 0, true);
             if ($GLOBALS['conf']['ansel_cache']['usecache']) {
                 $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $parent->id);
             }
         }
     }
 }
示例#6
0
文件: Date.php 项目: raz0rsdge/horde
 /**
  * Returns a child's direct parent
  *
  * @return Ansel_Gallery  The direct parent Horde_Share_Object
  */
 public function getParent()
 {
     return $this->_gallery->getParent($this);
 }
示例#7
0
 /**
  * Copy image and related data to specified gallery.
  *
  * @param array $images           An array of image ids.
  * @param Ansel_Gallery $gallery  The gallery to copy images to.
  *
  * @return integer The number of images copied
  * @throws Ansel_Exception
  */
 public function copyImagesTo(array $images, Ansel_Gallery $gallery)
 {
     if (!$gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
         throw new Horde_Exception_PermissionDenied(_("Access denied copying photos to this gallery."));
     }
     $imgCnt = 0;
     foreach ($images as $imageId) {
         $img = $this->getImage($imageId);
         // Note that we don't pass the tags when adding the image..see below
         $newId = $gallery->addImage(array('image_caption' => $img->caption, 'data' => $img->raw(), 'image_filename' => $img->filename, 'image_type' => $img->getType(), 'image_uploaded_date' => $img->uploaded));
         /* Copy any tags */
         $tags = $img->getTags();
         $GLOBALS['injector']->getInstance('Ansel_Tagger')->tag($newId, $tags, $gallery->get('owner'), 'image');
         // Check that new image_id doesn't have existing attributes,
         // throw exception if it does.
         $newAttributes = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImageAttributes($newId);
         if (count($newAttributes)) {
             throw new Ansel_Exception(_("Image already has existing attributes."));
         }
         $exif = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImageAttributes($imageId);
         if (is_array($exif) && count($exif) > 0) {
             foreach ($exif as $name => $value) {
                 $GLOBALS['injector']->getInstance('Ansel_Storage')->saveImageAttribute($newId, $name, $value);
             }
         }
         ++$imgCnt;
     }
     return $imgCnt;
 }
示例#8
0
文件: Date.php 项目: horde/horde
 /**
  * Moves images from one gallery to another. Since we're viewing by date
  * some images might belong to a subgallery so we need to take care to
  * udate the appropriate gallery data.
  *
  * @param array $images           An array of image_ids to move.
  * @param Ansel_Gallery $gallery  The Ansel_Gallery to move them to.
  *
  * @return boolean
  */
 public function moveImagesTo($images, $gallery)
 {
     if (!$gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
         throw new Horde_Exception_PermissionDenied(_("Access denied moving photos to this gallery."));
     } elseif (!$this->_gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::DELETE)) {
         throw new Horde_Exception_PermissionDenied(_("Access denied removing photos from this gallery."));
     }
     /* Sanitize image ids, and see if we're removing our key image. */
     $ids = array();
     foreach ($images as $imageId) {
         $ids[] = (int) $imageId;
         if ($imageId == $this->_gallery->get('default')) {
             $this->_gallery->set('default', null, true);
         }
     }
     /* If we have subgalleries, we need to go the more expensive route. Note
      * we can't use $gallery->hasSubgalleries() since that would be
      * overridden here since we are in date mode and thus would return false
      */
     if ($this->_gallery->get('has_subgalleries')) {
         $gallery_ids = array();
         $images = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImages(array('ids' => $ids));
         foreach ($images as $image) {
             if (empty($gallery_ids[$image->gallery])) {
                 $gallery_ids[$image->gallery] = 1;
             } else {
                 $gallery_ids[$image->gallery]++;
             }
         }
     }
     /* Bulk update the images to their new gallery_id */
     $GLOBALS['injector']->getInstance('Ansel_Storage')->setImagesGallery($ids, $gallery->id);
     /* Update the gallery counts for each affected gallery */
     if ($this->_gallery->get('has_subgalleries')) {
         foreach ($gallery_ids as $id => $count) {
             $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($id)->updateImageCount($count, false);
         }
     } else {
         $this->_gallery->updateImageCount(count($ids), false);
     }
     $gallery->updateImageCount(count($ids), true);
     /* Expire the cache since we have no reason to save() the gallery */
     if ($GLOBALS['conf']['ansel_cache']['usecache']) {
         $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $gallery->id);
         $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $this->_gallery->id);
     }
     return count($ids);
 }
示例#9
0
 /**
  * Move images from this gallery to another.
  *
  * @param array $images           The image ids to move.
  * @param Ansel_Gallery $gallery  The gallery to move images into.
  *
  * @return boolean
  * @throws Ansel_Exception
  * @throws Horde_Exception_PermissionDenied
  */
 public function moveImagesTo($images, $gallery)
 {
     if (!$gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT)) {
         throw new Horde_Exception_PermissionDenied("Access denied moving photos to this gallery.");
     } elseif (!$this->_gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::DELETE)) {
         throw new Horde_Exception_PermissionDenied(_("Access denied removing photos from this gallery."));
     }
     /* Sanitize image ids, and see if we're removing our key image. */
     $ids = array();
     foreach ($images as $imageId) {
         $ids[] = (int) $imageId;
         if ($imageId == $this->_gallery->get('default')) {
             $this->_gallery->set('default', null, true);
         }
     }
     $GLOBALS['injector']->getInstance('Ansel_Storage')->setImagesGallery($ids, $gallery->id);
     $this->_gallery->updateImageCount(count($ids), false);
     $gallery->updateImageCount(count($ids), true);
     /* Expire the cache since we have no reason to save() the gallery */
     if ($GLOBALS['conf']['ansel_cache']['usecache']) {
         $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $gallery->id);
         $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_Gallery' . $this->_gallery->id);
     }
     return count($ids);
 }
示例#10
0
文件: Gallery.php 项目: horde/horde
 /**
  * Outputs the html for a gallery tile.
  *
  * @param Ansel_Gallery $gallery  The Ansel_Gallery we are displaying.
  * @param Ansel_Style $style      A style object.
  * @param boolean $mini           Force the use of a mini thumbail?
  * @param array $params           An array containing additional parameters.
  *                                Currently, gallery_view_url and
  *                                image_view_url are used to override the
  *                                respective urls. %g and %i are replaced
  *                                with image id and gallery id, respectively
  *
  *
  * @return  Outputs the HTML for the tile.
  */
 public static function getTile(Ansel_Gallery $gallery, Ansel_Style $style = null, $mini = false, array $params = array())
 {
     global $prefs, $registry, $injector;
     // Create view
     $view = $injector->createInstance('Horde_View');
     $view->addTemplatePath(ANSEL_TEMPLATES . '/tile');
     $view->gallery = $gallery;
     $view_type = Horde_Util::getFormData('view', 'Gallery');
     $haveSearch = $view_type == 'Results' ? 1 : 0;
     if ($view_type == 'Results' || $view_type == 'List' || basename($_SERVER['PHP_SELF']) == 'index.php' && $prefs->getValue('defaultview') == 'galleries') {
         $showOwner = true;
     } else {
         $showOwner = false;
     }
     // Use the galleries style if not explicitly passed.
     if (is_null($style)) {
         $style = $gallery->getStyle();
     }
     // If the gallery has subgalleries, and no images, use one of the
     // subgalleries' stack image. hasSubGalleries already takes
     // permissions into account.
     if ($gallery->hasPermission($registry->getAuth(), Horde_Perms::READ) && !$gallery->countImages() && $gallery->hasSubGalleries()) {
         try {
             $galleries = $injector->getInstance('Ansel_Storage')->listGalleries(array('parent' => $gallery->id, 'all_levels' => false, 'perm' => Horde_Perms::READ));
             foreach ($galleries as $sgallery) {
                 if ($default_img = $sgallery->getKeyImage($style)) {
                     $view->gallery_image = Ansel::getImageUrl($default_img, $mini ? 'mini' : 'thumb', true, $style);
                 }
             }
         } catch (Ansel_Exception $e) {
         }
     } elseif ($gallery->hasPermission($registry->getAuth(), Horde_Perms::READ) && $gallery->countImages()) {
         $thumbstyle = $mini ? 'mini' : 'thumb';
         if ($gallery->hasPasswd()) {
             $view->gallery_image = Horde_Themes::img('gallery-locked.png');
         } else {
             $view->gallery_image = Ansel::getImageUrl($gallery->getKeyImage($style), $thumbstyle, true, $style);
         }
     }
     // If no image at this point, we can't get one.
     if (empty($view->gallery_image)) {
         $view->gallery_image = Horde_Themes::img('thumb-error.png');
     }
     // Check for being called via the api and generate correct view links
     if (!isset($params['gallery_view_url'])) {
         $view->view_link = Ansel::getUrlFor('view', array('gallery' => $gallery->id, 'view' => 'Gallery', 'havesearch' => $haveSearch, 'slug' => $gallery->get('slug')));
     } else {
         $view->view_link = new Horde_Url(str_replace(array('%g', '%s'), array($gallery->id, $gallery->get('slug')), urldecode($params['gallery_view_url'])));
     }
     if ($gallery->hasPermission($registry->getAuth(), Horde_Perms::EDIT) && !$mini) {
         $view->properties_link = Horde::url('gallery.php', true)->add(array('gallery' => $gallery->id, 'actionID' => 'modify', 'havesearch' => $haveSearch, 'url' => Horde::selfUrl(true, false, true)));
     }
     if ($showOwner && !$mini && $registry->getAuth() != $gallery->get('owner')) {
         $view->owner_link = Ansel::getUrlFor('view', array('view' => 'List', 'owner' => $gallery->get('owner'), 'groupby' => 'owner'), true);
         $view->owner_string = $gallery->getIdentity()->getValue('fullname');
         if (empty($view->owner_string)) {
             $view->owner_string = $gallery->get('owner');
         }
     }
     $view->background_color = $style->background;
     $view->gallery_count = $gallery->countImages(true);
     $view->date_format = $prefs->getValue('date_format');
     return $view->render('gallery' . ($mini ? 'mini' : ''));
 }