<?php
/**
 * Implementation of the Quota API for Cyrus IMAP servers.
 *
 * $Horde: imp/lib/Quota/cyrus.php,v 1.22.10.4 2005/07/03 03:42:46 selsky Exp $
 *
 * Copyright 2002-2005 Mike Cochrane <mike@graftonhall.co.nz>
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @author  Mike Cochrane <mike@graftonhall.co.nz>
 * @since   IMP 4.0
 * @package IMP_Quota
 */
class IMP_Quota_cyrus extends IMP_Quota {

    /**
     * Constructor
     *
     * @param array $params  Hash containing connection parameters.
     */
    function IMP_Quota_cyrus($params = array())
    {
        $this->_params = $params;

        if (!isset($this->_params['login'])) {
            $this->_params['login'] = '';
        }
        if (!isset($this->_params['password'])) {
            $this->_params['password'] = '';
        }
    }

    /**
     * Get quota information (used/allocated), in bytes.
     *
     * @return mixed  An associative array.
     *                'limit' = Maximum quota allowed
     *                'usage' = Currently used portion of quota (in bytes)
     *                Returns PEAR_Error on failure.
     */
    function getQuota()
    {
        $quota = null;

        if (function_exists('imap_get_quotaroot')) {
            @$quota = imap_get_quotaroot($_SESSION['imp']['stream'], 'INBOX');
        } else {
            $stream = @imap_open(IMP::serverString(), $this->_params['login'], $this->_params['password'], OP_HALFOPEN);
            if ($stream !== false) {
                $quota = @imap_get_quota($stream, $this->_params['userhierarchy'] . $_SESSION['imp']['user']);
                @imap_close($stream);
            }
        }
        if (is_array($quota)) {
            if (isset($quota['limit'])) {
                return array('usage' => $quota['usage'] * 1024, 'limit' => $quota['limit'] * 1024);
            } elseif (isset($quota['STORAGE']['limit'])) {
                return array('usage' => $quota['STORAGE']['usage'] * 1024, 'limit' => $quota['STORAGE']['limit'] * 1024);
            }
            return array('usage' => 0, 'limit' => 0);
        }

        return PEAR::raiseError(_("Unable to retrieve quota"), 'horde.error');
    }

}
