<?php
/**
 * Browse through the object tree.
 *
 * @param string $path       The level of the tree to browse.
 * @param array $properties  The item properties to return. Defaults to 'name',
 *                           'icon', and 'browseable'.
 *
 * @return array  The contents of $path
 */
function _packages_browse($path = '', $properties = array())
{
    require_once dirname(__FILE__) . '/base.php';

    // Default properties.
    if (!$properties) {
        $properties = array('name', 'icon', 'browseable');
    }

    if (substr($path, 0, 8) == 'packages') {
        $path = substr($path, 8);
    }
    $path = trim($path, '/');

    $pm = new PackageMapper();
    $packages = _packages_listPackages();

    if (empty($path)) {
        $results = array();
        foreach ($packages as $id => $name) {
            $results['packages/' . rawurlencode($name) . '.txt'] =
                array('name' => rawurlencode($name),
                      'displayname' => rawurlencode($name),
                      'icon' => $GLOBALS['registry']->getImageDir('horde') . '/about.png',
                      'browseable' => false,
                      'contenttype' => 'text/plain',
                      'contentlength' => strlen($pm->find((int)$id)->toString()));
        }
        return $results;
    } else {
        $name = str_replace('.txt', '', $path);
        $p = $pm->find(Horde_RDO::FIND_FIRST, array('package_name' => $name));
        if ($p instanceof Package) {
            return array('data' => $p->toString(),
                         'mimetype' => 'text/plain');
        }
    }

    return PEAR::raiseError($path . ' does not exist or permission denied');
}