From ffa0939af58c2a67970263272cacb0566a8babc0 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 14 Nov 2008 15:10:44 -0700 Subject: [PATCH] Update quota drivers. --- imp/config/servers.php.dist | 7 ++- imp/lib/IMP.php | 2 +- imp/lib/Quota.php | 124 +++++++++++++++++++++----------------------- imp/lib/Quota/command.php | 36 ++++++------- imp/lib/Quota/hook.php | 15 +++--- imp/lib/Quota/imap.php | 34 +++++------- imp/lib/Quota/logfile.php | 62 +++++++++++----------- imp/lib/Quota/maildir.php | 99 +++++++++++++++++------------------ imp/lib/Quota/mdaemon.php | 16 +++--- imp/lib/Quota/mercury32.php | 12 ++--- imp/lib/Quota/sql.php | 18 +++---- 11 files changed, 203 insertions(+), 222 deletions(-) diff --git a/imp/config/servers.php.dist b/imp/config/servers.php.dist index 49fbb6e88..71bcc7497 100644 --- a/imp/config/servers.php.dist +++ b/imp/config/servers.php.dist @@ -82,7 +82,9 @@ * 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 @@ -223,7 +225,8 @@ $servers['cyrus'] = array( ), 'quota' => array( 'driver' => 'imap', - 'params' => array('hide_quota_when_unlimited' => true), + 'hide_when_unlimited' => true, + 'params' => array() ), 'acl' => true, 'cache' => false, diff --git a/imp/lib/IMP.php b/imp/lib/IMP.php index b1c1b3e2e..49c91092a 100644 --- a/imp/lib/IMP.php +++ b/imp/lib/IMP.php @@ -738,7 +738,7 @@ class IMP } } 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; } diff --git a/imp/lib/Quota.php b/imp/lib/Quota.php index 475061d56..cb697688b 100644 --- a/imp/lib/Quota.php +++ b/imp/lib/Quota.php @@ -1,6 +1,6 @@ * @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; @@ -39,12 +86,12 @@ class 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() { return array('usage' => 0, 'limit' => 0); } @@ -52,9 +99,9 @@ class IMP_Quota { /** * 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']) @@ -68,60 +115,7 @@ class IMP_Quota { : _("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") + ); } - } diff --git a/imp/lib/Quota/command.php b/imp/lib/Quota/command.php index 6a2943caf..82f9defda 100644 --- a/imp/lib/Quota/command.php +++ b/imp/lib/Quota/command.php @@ -1,6 +1,6 @@ * @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); } /** @@ -49,40 +49,36 @@ class IMP_Quota_command extends IMP_Quota { * 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); } diff --git a/imp/lib/Quota/hook.php b/imp/lib/Quota/hook.php index 9f24c74a9..48beb2b01 100644 --- a/imp/lib/Quota/hook.php +++ b/imp/lib/Quota/hook.php @@ -1,6 +1,6 @@ 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/) * @@ -18,17 +18,18 @@ * @author Michael Redinger * @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')) { diff --git a/imp/lib/Quota/imap.php b/imp/lib/Quota/imap.php index 837e3f38a..5166163f7 100644 --- a/imp/lib/Quota/imap.php +++ b/imp/lib/Quota/imap.php @@ -1,6 +1,6 @@ * @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'); } } diff --git a/imp/lib/Quota/logfile.php b/imp/lib/Quota/logfile.php index bcea52a05..33c248241 100644 --- a/imp/lib/Quota/logfile.php +++ b/imp/lib/Quota/logfile.php @@ -34,53 +34,53 @@ * @author Tim Gorter * @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); } } diff --git a/imp/lib/Quota/maildir.php b/imp/lib/Quota/maildir.php index 3c02f5500..8b131cffc 100644 --- a/imp/lib/Quota/maildir.php +++ b/imp/lib/Quota/maildir.php @@ -25,33 +25,30 @@ * @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'; @@ -61,52 +58,52 @@ class IMP_Quota_Maildir extends IMP_Quota { $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); } } diff --git a/imp/lib/Quota/mdaemon.php b/imp/lib/Quota/mdaemon.php index 766f6eeda..924aea86c 100644 --- a/imp/lib/Quota/mdaemon.php +++ b/imp/lib/Quota/mdaemon.php @@ -13,17 +13,17 @@ * @author Mike Cochrane * @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']); @@ -46,15 +46,13 @@ class IMP_Quota_mdaemon extends IMP_Quota { /** * 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'); @@ -88,7 +86,7 @@ class IMP_Quota_mdaemon extends IMP_Quota { * * @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); diff --git a/imp/lib/Quota/mercury32.php b/imp/lib/Quota/mercury32.php index aa5759e20..56cae3a87 100644 --- a/imp/lib/Quota/mercury32.php +++ b/imp/lib/Quota/mercury32.php @@ -38,17 +38,17 @@ * @author Frank Lupo * @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; @@ -59,7 +59,7 @@ class IMP_Quota_mercury32 extends IMP_Quota { } closedir($dir); - if ($quota !== null) { + if (!is_null($quota)) { return array('usage' => $quota, 'limit' => 0); } } diff --git a/imp/lib/Quota/sql.php b/imp/lib/Quota/sql.php index 3028b89df..b6f11968f 100644 --- a/imp/lib/Quota/sql.php +++ b/imp/lib/Quota/sql.php @@ -38,28 +38,28 @@ * @author Jan Schneider * @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'; @@ -77,10 +77,10 @@ class IMP_Quota_sql extends IMP_Quota { /** * 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() { -- 2.11.0