* IMP pages. Set 'driver' equal to the mailserver and 'params'
* equal to any extra parameters needed by the driver (see the
* comments located at the top of imp/lib/Quota/[quotadriver].php
- * for the parameters needed for each driver).
+ * for the parameters needed for each driver). Set
+ * 'hide_when_unlimited' to true if you want to hide quota output
+ * when the server reports an unlimited quota.
*
* The optional 'format' parameter is supported by all drivers and
* specifies the formats of the quota messages in the user
),
'quota' => array(
'driver' => 'imap',
- 'params' => array('hide_quota_when_unlimited' => true),
+ 'hide_when_unlimited' => true,
+ 'params' => array()
),
'acl' => true,
'cache' => false,
}
} else {
// Hide unlimited quota message?
- if (!empty($_SESSION['imp']['quota']['params']['hide_quota_when_unlimited'])) {
+ if (!empty($_SESSION['imp']['quota']['hide_when_unlimited'])) {
return false;
}
<?php
/**
- * IMP_Quota:: provides an API for retrieving Quota details from a mail
+ * IMP_Quota:: provides an API for retrieving quota details from a mail
* server.
*
* Copyright 2002-2008 The Horde Project (http://www.horde.org/)
* @author Mike Cochrane <mike@graftonhall.co.nz>
* @package IMP_Quota
*/
-class IMP_Quota {
-
+class IMP_Quota
+{
/**
* Hash containing connection parameters.
*
* @var array
*/
- var $_params = array();
+ protected $_params = array();
+
+ /**
+ * Attempts to return a reference to a concrete IMP_Quota instance based on
+ * $driver.
+ *
+ * It will only create a new instance if no instance with the same
+ * parameters currently exists.
+ *
+ * This method must be invoked as: $var = &IMP_Quota::singleton()
+ *
+ * @param string $driver The type of concrete subclass to return.
+ * @param array $params A hash containing any additional configuration
+ * or connection parameters a subclass might need.
+ *
+ * @return mixed The created concrete instance, or false on error.
+ */
+ static public function &singleton($driver, $params = array())
+ {
+ static $instances = array();
+
+ $signature = serialize(array($driver, $params));
+ if (!isset($instances[$signature])) {
+ $instances[$signature] = IMP_Quota::factory($driver, $params);
+ }
+
+ return $instances[$signature];
+ }
+
+ /**
+ * Attempts to return a concrete instance based on $driver.
+ *
+ * @param string $driver The type of concrete subclass to return.
+ * @param array $params A hash containing any additional configuration or
+ * connection parameters a subclass might need.
+ *
+ * @return mixed The newly created concrete instance, or false on error.
+ */
+ static public function factory($driver, $params = array())
+ {
+ $driver = basename($driver);
+ require_once dirname(__FILE__) . '/Quota/' . $driver . '.php';
+ $class = 'IMP_Quota_' . $driver;
+
+ return class_exists($class)
+ ? new $class($params)
+ : false;
+ }
/**
* Constructor.
*
* @param array $params Hash containing connection parameters.
*/
- function IMP_Quota($params = array())
+ function __construct($params = array())
{
$this->_params = $params;
/**
* Get quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ public function getQuota()
{
return array('usage' => 0, 'limit' => 0);
}
/**
* Returns the quota messages variants, including sprintf placeholders.
*
- * @return array A hash with quota message templates.
+ * @return array An array with quota message templates.
*/
- function getMessages()
+ public function getMessages()
{
return array(
'long' => isset($this->_params['format']['long'])
: _("Quota status: %.2f MB / NO LIMIT"),
'nolimit_short' => isset($this->_params['format']['nolimit_short'])
? $this->_params['format']['nolimit_short']
- : _("%.0f MB"));
- }
-
- /**
- * Attempts to return a concrete Quota instance based on $driver.
- *
- * @param string $driver The type of concrete Quota subclass to return.
- * @param array $params A hash containing any additional configuration or
- * connection parameters a subclass might need.
- *
- * @return mixed The newly created concrete Quota instance, or false
- * on error.
- */
- function &factory($driver, $params = array())
- {
- $driver = basename($driver);
- require_once dirname(__FILE__) . '/Quota/' . $driver . '.php';
- $class = 'IMP_Quota_' . $driver;
- if (class_exists($class)) {
- $quota = new $class($params);
- } else {
- $quota = false;
- }
-
- return $quota;
- }
-
- /**
- * Attempts to return a reference to a concrete Quota instance based on
- * $driver.
- *
- * It will only create a new instance if no Quota instance with the same
- * parameters currently exists.
- *
- * This should be used if multiple quota sources are required.
- *
- * This method must be invoked as: $var = &Quota::singleton()
- *
- * @param string $driver The type of concrete Quota subclass to return.
- * @param array $params A hash containing any additional configuration or
- * connection parameters a subclass might need.
- *
- * @return mixed The created concrete Quota instance, or false on error.
- */
- function &singleton($driver, $params = array())
- {
- static $instances = array();
-
- $signature = serialize(array($driver, $params));
- if (!isset($instances[$signature])) {
- $instances[$signature] = &IMP_Quota::factory($driver, $params);
- }
-
- return $instances[$signature];
+ : _("%.0f MB")
+ );
}
-
}
<?php
/**
- * Implementation of the Quota API for IMAP servers with a unix quota command.
+ * Implementation of IMP_Quota API for IMAP servers with a *nix quota command.
* This requires a modified "quota" command that allows the httpd server
* account to get quotas for other users. It also requires that your
* web server and imap server be the same server or at least have shared
* @author Eric Rostetter <eric.rostetter@physics.utexas.edu>
* @package IMP_Quota
*/
-class IMP_Quota_command extends IMP_Quota {
-
+class IMP_Quota_command extends IMP_Quota
+{
/**
* Constructor
*
* @param array $params Hash containing connection parameters.
*/
- function IMP_Quota_command($params = array())
+ function __construct($params = array())
{
$params = array_merge(array('quota_path' => 'quota',
'grep_path' => 'grep',
'partition' => null),
$params);
- parent::IMP_Quota($params);
+ parent::__construct($params);
}
/**
* SELinux interference, the file being > 2 GB in size, the file
* we're referring to not being readable, etc.
*/
- function blockSize()
+ protected function _blockSize()
{
$results = stat(__FILE__);
- if ($results['blksize'] > 1) {
- $blocksize = $results['blksize'];
- } else {
- $blocksize = 1024;
- }
- return $blocksize;
+ return ($results['blksize'] > 1)
+ ? $results['blksize']
+ : 1024;
}
/**
* Get quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ */
+ public function getQuota()
{
- $imap_user = $_SESSION['imp']['user'];
if (empty($this->_params['partition'])) {
- $passwd_array = posix_getpwnam($imap_user);
+ $passwd_array = posix_getpwnam($_SESSION['imp']['user']);
list($junk, $search_string, $junk) = explode('/', $passwd_array['dir']);
} else {
$search_string = $this->_params['partition'];
}
- $cmdline = $this->_params['quota_path'] . ' -u ' . $imap_user . ' | ' .
- $this->_params['grep_path'] . ' ' . $search_string;
+ $cmdline = $this->_params['quota_path'] . ' -u ' . $_SESSION['imp']['user'] . ' | ' . $this->_params['grep_path'] . ' ' . $search_string;
exec($cmdline, $quota_data, $return_code);
if (($return_code == 0) && (count($quota_data) == 1)) {
$quota = split("[[:blank:]]+", trim($quota_data[0]));
- $blocksize = $this->blockSize();
+ $blocksize = $this->_blockSize();
return array('usage' => $quota[1] * $blocksize,
'limit' => $quota[2] * $blocksize);
}
<?php
/**
- * Implementation of the Quota API for a generic hook function. This
+ * Implementation of IMP_Quota API for a generic hook function. This
* requires hook_get_quota to be set in config/hooks.php . The
* function takes an array as argument and returns an array where the
* first item is the disk space used in bytes and the second the
*
* You must configure this driver in horde/imp/config/servers.php. The
* driver supports the following parameters:
- * 'params' => Array of parameters to pass to the quota function.
+ * 'params' => Array of parameters to pass to the quota function.
*
* Copyright 2002-2008 The Horde Project (http://www.horde.org/)
*
* @author Michael Redinger <Michael.Redinger@uibk.ac.at>
* @package IMP_Quota
*/
-class IMP_Quota_hook extends IMP_Quota {
-
+class IMP_Quota_hook extends IMP_Quota
+{
/**
* Get quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ */
+ public function getQuota()
{
$quota = Horde::callHook('_imp_hook_quota', $this->_params, 'imp');
if (is_a($quota, 'PEAR_Error')) {
<?php
/**
- * Implementation of the Quota API for IMAP servers.
+ * Implementation of the IMP_Quota API for IMAP servers.
*
* Copyright 2002-2008 The Horde Project (http://www.horde.org/)
*
* @author Mike Cochrane <mike@graftonhall.co.nz>
* @package IMP_Quota
*/
-class IMP_Quota_imap extends IMP_Quota {
-
+class IMP_Quota_imap extends IMP_Quota
+{
/**
* Get quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ public function getQuota()
{
- $imp_imap = &IMP_IMAP::singleton();
- $quota = @imap_get_quotaroot($imp_imap->stream(),
- $GLOBALS['imp_search']->isSearchMbox($GLOBALS['imp_mbox']['mailbox']) ? 'INBOX' : $GLOBALS['imp_mbox']['mailbox']);
-
- if (is_array($quota)) {
- if (count($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);
+ try {
+ $quota = $GLOBALS['imp_imap']->ob->getQuotaRoot($GLOBALS['imp_search']->isSearchMbox($GLOBALS['imp_mbox']['mailbox']) ? 'INBOX' : $GLOBALS['imp_mbox']['mailbox']);
+ $quota_val = reset($quota);
+ return array('usage' => $quota['storage']['usage'] * 1024, 'limit' => $quota['storage']['limit'] * 1024);
+ } catch (Horde_Imap_Client_Exception $e) {
+ $GLOBALS['imp_imap']->logException($e);
+ return PEAR::raiseError(_("Unable to retrieve quota"), 'horde.error');
}
-
- return PEAR::raiseError(_("Unable to retrieve quota"), 'horde.error');
}
}
* @author Tim Gorter <email@teletechnics.co.nz>
* @package IMP_Quota
*/
-class IMP_Quota_logfile extends IMP_Quota {
-
+class IMP_Quota_logfile extends IMP_Quota
+{
/**
* Constructor
*
* @param array $params Hash containing connection parameters.
*/
- function IMP_Quota_logfile($params = array())
+ function __construct($params = array())
{
- $params = array_merge(array('logfile' => '',
- 'taillines' => 10,
- 'FTPmail' => 'FTP',
- 'beginocc' => 'usage = ',
- 'midocc' => ' of ',
- 'endocc' => ' bytes'),
- $params);
- parent::IMP_Quota($params);
+ $params = array_merge(array(
+ 'logfile' => '',
+ 'taillines' => 10,
+ 'FTPmail' => 'FTP',
+ 'beginocc' => 'usage = ',
+ 'midocc' => ' of ',
+ 'endocc' => ' bytes'
+ ), $params);
+ parent::__construct($params);
}
/**
* Get quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ public function getQuota()
{
- if (is_file($this->_params['logfile'])) {
- $full = file($this->_params['logfile']);
- for (; $this->_params['taillines'] > 0; $this->_params['taillines']--) {
- $tail[] = $full[count($full) - $this->_params['taillines']];
- }
- $uname = $_SESSION['imp']['user'];
- $FTPmail = $this->_params['FTPmail'];
- $virtline = preg_grep("[$uname: $FTPmail]", $tail);
- $virtline = array_values($virtline);
- $usage = substr($virtline[0],
- strpos($virtline[0], $this->_params['beginocc']) + strlen($this->_params['beginocc']),
- strpos($virtline[0], $this->_params['midocc']));
- $storage = substr($virtline[0],
- strpos($virtline[0], $this->_params['midocc']) + strlen($this->_params['midocc']),
- strpos($virtline[0], $this->_params['endocc']));
- return array('usage' => $usage, 'limit' => $storage);
+ if (!is_file($this->_params['logfile'])) {
+ return PEAR::raiseError(_("Unable to retrieve quota"), 'horde.error');
+ }
+
+ $full = file($this->_params['logfile']);
+ for (; $this->_params['taillines'] > 0; --$this->_params['taillines']) {
+ $tail[] = $full[count($full) - $this->_params['taillines']];
}
- return PEAR::raiseError(_("Unable to retrieve quota"), 'horde.error');
+
+ $uname = $_SESSION['imp']['user'];
+ $FTPmail = $this->_params['FTPmail'];
+ $virtline = preg_grep("[$uname: $FTPmail]", $tail);
+ $virtline = array_values($virtline);
+ $usage = substr($virtline[0], strpos($virtline[0], $this->_params['beginocc']) + strlen($this->_params['beginocc']), strpos($virtline[0], $this->_params['midocc']));
+ $storage = substr($virtline[0], strpos($virtline[0], $this->_params['midocc']) + strlen($this->_params['midocc']), strpos($virtline[0], $this->_params['endocc']));
+
+ return array('usage' => $usage, 'limit' => $storage);
}
}
* @author Eric Rostetter
* @package IMP_Quota
*/
-class IMP_Quota_Maildir extends IMP_Quota {
-
+class IMP_Quota_Maildir extends IMP_Quota
+{
/**
* Constructor.
*
* @param array $params Hash containing connection parameters.
*/
- function IMP_Quota_Maildir( $params = array() )
+ function __construct($params = array())
{
$params = array_merge(array('path' => ''), $params);
- parent::IMP_Quota($params);
+ parent::__construct($params);
}
/**
* Returns quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ public function getQuota()
{
- $storage_limit = 0;
- $message_limit = 0;
- $storage_used = 0;
- $message_used = 0;
+ $storage_limit = $message_limit = $storage_used = $message_used = 0;
// Get the full path to the quota file.
$full = $this->_params['path'] . '/maildirsize';
$full = str_replace('~U', $uname, $full);
// Read in the quota file and parse it, if possible.
- if (is_file($full)) {
- // Read in maildir quota file.
- $lines = file($full);
+ if (!is_file($full)) {
+ return PEAR::raiseError(_("Unable to retrieve quota"));
+ }
- // Parse the lines.
- foreach ($lines as $line_number => $line) {
- if ($line_number == 0) {
- // First line, quota header.
- $line = preg_replace('/[ \t\n\r\0\x0B]/', '', $line);
- list($v1, $t1, $v2, $t2) = sscanf($line, '%ld%[CS],%ld%[CS]');
- if ($v1 == null || $t1 == null) {
- $v1 = 0;
- }
- if ($v2 == null || $t2 == null) {
- $v2 = 0;
- }
+ // Read in maildir quota file.
+ $lines = file($full);
- if ($t1 == 'S') {
- $storage_limit = $v1;
- }
- if ($t1 == 'C') {
- $message_limit = $v1;
- }
- if ($t2 == 'S') {
- $storage_limit = $v2;
- }
- if ($t2 == 'C') {
- $message_limit = $v2;
- }
- } else {
- // Any line other than the first line.
- // The quota used is the sum of all lines found.
- list($storage, $message) = sscanf(trim($line), '%ld %d');
- if ($storage != null) {
- $storage_used += $storage;
- }
- if ($message != null) {
- $message_used += $message;
- }
+ // Parse the lines.
+ foreach ($lines as $line_number => $line) {
+ if ($line_number == 0) {
+ // First line, quota header.
+ $line = preg_replace('/[ \t\n\r\0\x0B]/', '', $line);
+ list($v1, $t1, $v2, $t2) = sscanf($line, '%ld%[CS],%ld%[CS]');
+ if (is_null($v1) || is_null($t1)) {
+ $v1 = 0;
+ }
+ if (is_null($v2) || is_null($t2)) {
+ $v2 = 0;
}
- }
- return array('usage' => $storage_used, 'limit' => $storage_limit);
+ if ($t1 == 'S') {
+ $storage_limit = $v1;
+ }
+ if ($t1 == 'C') {
+ $message_limit = $v1;
+ }
+ if ($t2 == 'S') {
+ $storage_limit = $v2;
+ }
+ if ($t2 == 'C') {
+ $message_limit = $v2;
+ }
+ } else {
+ // Any line other than the first line.
+ // The quota used is the sum of all lines found.
+ list($storage, $message) = sscanf(trim($line), '%ld %d');
+ if (!is_null($storage)) {
+ $storage_used += $storage;
+ }
+ if (!is_null($message)) {
+ $message_used += $message;
+ }
+ }
}
- return PEAR::raiseError(_("Unable to retrieve quota"));
+ return array('usage' => $storage_used, 'limit' => $storage_limit);
}
}
* @author Mike Cochrane <mike@graftonhall.co.nz>
* @package IMP_Quota
*/
-class IMP_Quota_mdaemon extends IMP_Quota {
-
+class IMP_Quota_mdaemon extends IMP_Quota
+{
/**
* Get quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ public function getQuota()
{
$userDetails = $this->_getUserDetails($_SESSION['imp']['user'], $_SESSION['imp']['maildomain']);
/**
* Get the size of a mailbox
*
- * @access private
- *
* @param string $path The full path of the mailbox to fetch the quota
* for including trailing backslash.
*
* @return mixed The number of bytes in the mailbox (integer) or false
* (boolean) on error.
*/
- function _mailboxSize($path)
+ protected function _mailboxSize($path)
{
$contents = file_get_contents($path . '\imap.mrk');
*
* @return mixed Line from userlist.dat (string) or false (boolean).
*/
- function _getUserDetails($user, $realm)
+ protected function _getUserDetails($user, $realm)
{
$searchString = str_pad($realm, 45) . str_pad($user, 30);
* @author Frank Lupo <frank_lupo@email.it>
* @package IMP_Quota
*/
-class IMP_Quota_mercury32 extends IMP_Quota {
-
+class IMP_Quota_mercury32 extends IMP_Quota
+{
/**
* Get quota information (used/allocated), in bytes.
*
- * @return mixed An associative array.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
* 'limit' = Maximum quota allowed
* 'usage' = Currently used portion of quota (in bytes)
- * Returns PEAR_Error on failure.
*/
- function getQuota()
+ public function getQuota()
{
$quota = null;
}
closedir($dir);
- if ($quota !== null) {
+ if (!is_null($quota)) {
return array('usage' => $quota, 'limit' => 0);
}
}
* @author Jan Schneider <jan@horde.org>
* @package IMP_Quota
*/
-class IMP_Quota_sql extends IMP_Quota {
-
+class IMP_Quota_sql extends IMP_Quota
+{
/**
* SQL connection object.
*
* @var DB
*/
- var $_db;
+ protected $_db;
/**
* State of SQL connection.
*
* @var boolean
*/
- var $_connected = false;
+ protected $_connected = false;
/**
* Connects to the database
*
* @return boolean True on success, PEAR_Error on failure.
*/
- function _connect()
+ protected function _connect()
{
if (!$this->_connected) {
require_once 'DB.php';
/**
* Returns quota information.
*
- * @return mixed An associative array:
- * 'limit' => Maximum quota allowed (in bytes),
- * 'usage' => Currently used space (in bytes).
- * PEAR_Error on failure.
+ * @return mixed Returns PEAR_Error on failure. Otherwise, returns an
+ * array with the following keys:
+ * 'limit' = Maximum quota allowed
+ * 'usage' = Currently used portion of quota (in bytes)
*/
function getQuota()
{