Ansel_Api::browse PHP Method

browse() public method

Browse through Ansel's gallery tree.
public browse ( string $path = '', array $properties = [] ) : array
$path string The level of the tree to browse.
$properties array The item properties to return. Defaults to 'name', 'icon', and 'browseable'.
return array The contents of $path
    public function browse($path = '', array $properties = array())
    {
        global $injector, $registry;
        // Default properties.
        if (!$properties) {
            $properties = array('name', 'icon', 'browseable');
        }
        if (substr($path, 0, 5) == 'ansel') {
            $path = substr($path, 5);
        }
        $path = trim($path, '/');
        $parts = explode('/', $path);
        $storage = $injector->getInstance('Ansel_Storage');
        if (empty($path)) {
            $owners = array();
            $galleries = $storage->listGalleries(array('all_levels' => false));
            foreach ($galleries as $gallery) {
                $owners[$gallery->get('owner') ? $gallery->get('owner') : '-system-'] = true;
            }
            $results = array();
            foreach (array_keys($owners) as $owner) {
                $path = 'ansel/' . $registry->convertUsername($owner, false);
                if (in_array('name', $properties)) {
                    $results[$path]['name'] = $injector->getInstance('Horde_Core_Factory_Identity')->create($owner)->getName();
                }
                if (in_array('icon', $properties)) {
                    $results[$path]['icon'] = Horde_Themes::img('user.png');
                }
                if (in_array('browseable', $properties)) {
                    $results[$path]['browseable'] = true;
                }
            }
            return $results;
        } else {
            $dav = $injector->getInstance('Horde_Dav_Storage');
            $object = end($parts);
            try {
                $object = $dav->getInternalObjectId($object, $parts[count($parts) - 2]) ?: $object;
            } catch (Horde_Dav_Exception $e) {
            }
            if (count($parts) == 1) {
                // This request is for all galleries owned by the requested
                // user.
                $galleries = $storage->listGalleries(array('attributes' => $registry->convertUsername($parts[0], true), 'all_levels' => false));
                $images = array();
            } elseif ($this->galleryExists(end($parts))) {
                // This request if for a certain gallery, list all sub-galleries
                // and images.
                $gallery_id = end($parts);
                $galleries = $storage->listGalleries(array('parent' => $gallery_id, 'all_levels' => false, 'perm' => Horde_Perms::SHOW));
                $images = $this->listImages($gallery_id, array('perms' => Horde_Perms::SHOW, 'view' => 'mini'));
            } elseif (count($parts) > 2 && $this->galleryExists($parts[count($parts) - 2]) && ($image = $injector->getInstance('Ansel_Storage')->getImage($object))) {
                return array('data' => $image->raw(), 'mimetype' => $image->type, 'mtime' => $image->uploaded);
            } else {
                throw new Horde_Exception_NotFound(_("File not found."));
            }
            $results = array();
            foreach ($galleries as $gallery) {
                $retpath = 'ansel/' . implode('/', $parts) . '/' . $gallery->id;
                if (in_array('name', $properties)) {
                    $results[$retpath]['name'] = sprintf(_("Photos from %s"), $gallery->get('name'));
                }
                if (in_array('displayname', $properties)) {
                    $results[$retpath]['displayname'] = rawurlencode($gallery->get('name'));
                }
                if (in_array('icon', $properties)) {
                    $results[$retpath]['icon'] = Horde_Themes::img('ansel.png');
                }
                if (in_array('browseable', $properties)) {
                    $results[$retpath]['browseable'] = $gallery->hasPermission($registry->getAuth(), Horde_Perms::READ);
                }
            }
            $dav = $injector->getInstance('Horde_Dav_Storage');
            foreach ($images as $imageId => $image) {
                try {
                    $imageId = $dav->getExternalObjectId($imageId, $parts[count($parts) - 1]) ?: $imageId;
                } catch (Horde_Dav_Exception $e) {
                }
                $retpath = 'ansel/' . implode('/', $parts) . '/' . $imageId;
                if (in_array('name', $properties)) {
                    $results[$retpath]['name'] = $image['name'];
                }
                if (in_array('displayname', $properties)) {
                    $results[$retpath]['displayname'] = rawurlencode($image['name']);
                }
                if (in_array('icon', $properties)) {
                    $results[$retpath]['icon'] = $image['url'];
                }
                if (in_array('browseable', $properties)) {
                    $results[$retpath]['browseable'] = false;
                }
                if (in_array('contenttype', $properties)) {
                    $results[$retpath]['contenttype'] = $image['type'];
                }
                if (in_array('modified', $properties)) {
                    $results[$retpath]['modified'] = $image['uploaded'];
                }
                if (in_array('created', $properties)) {
                    $results[$retpath]['created'] = $image['uploaded'];
                }
                if (in_array('etag', $properties)) {
                    $results[$retpath]['etag'] = '"' . md5($imageId . '|' . $image['uploaded']) . '"';
                }
            }
            return $results;
        }
        throw Horde_Exception_NotFound(_("File not found."), 404);
    }