[cvs] [Wiki] created: AltQuota

Wiki Guest wikiguest at horde.org
Tue Jun 29 01:20:53 UTC 2010


guest [189.106.174.94]  Mon, 28 Jun 2010 21:20:53 -0400

Created page: http://wiki.horde.org/AltQuota

[[toc]]
+++ Notes
This describes modifications to IMP quota that can be configured to  
use one or two different devices (file systems). If using two devices,  
one is for INBOX and the other is for IMAP folders.

You can use one or two different devices (file systems) with quota  
enabled. This allows different quota values for INBOX and IMAP  
folders. Users use to keep messages on the INBOX. The bigger the  
quota, the bigger the INBOX file, with performance hit. In the other  
hand, messages are documents that must be filed, so you may want to  
give more space for IMAP folders than for the INBOX.

Uses system quota command.

Tested on Debian 5.0.5 (lenny), horde-3.3.8 and imp-h3-4.3.7.
----
+++ Modifications

||~ File ||~ Function(s) ||
|| imp/lib/Quota/command.php || IMP_Quota_command, getQuota ||
|| imp/lib/IMP.php || quota, quotaData ||
|| imp/lib/Quota.php || getQuota, getMessages ||
|| imp/config/servers.php ||   ||

You must modify the four files. Don't forget to change permission to  
each of them:
<code>
chown www-data command.php
</code>
Where www-data is the web user (Apache or equivalent).

You may need to give permission to your web user to execute quota:
<code>
chmod  +s  /usr/bin/quota
</code>

//Last updated 2010-06-28//
----
+++ Descriptions
----
++++ command.php
* Function IMP_Quota_command accepts 2 new parameters:
> '**dev_inbx**' => User´s INBOX file system device  - **REQUIRED**
> If you have INBOX and IMAP folders in the same device, use //ONLY//   
> this parameter.
> If you have INBOX and IMAP folders in different devices and quota  
> enabled on them you //MUST//  use 'dev_fldrs' parameter also.
> Usually maps to /var/mail, /var/spool/mail. Examples: '/dev/hda6',  
> '/dev/sdb2', '/dev/md2', '/dev/mapper/VOL1-VarM'.
> '**dev_fldrs**' => User´s home file system device  - **OPTIONAL**  
> Use only if you have INBOX and IMAP folders in different devices and  
> quota enabled on them. \
Used for IMAP folders. Usually maps to /home.
> Examples: '/dev/hda7', '/dev/sda3', '/dev/md1', '/dev/mapper/VOL2-Home'.
> Obsolete parameters: grep_path, partition
* function getQuota:
> Function now takes care of exceeded quotas, quota not defined for  
> that user and if it is using one or two devices (dev_inbx, dev_fldrs).

Backup your original imp/lib/Quota/command.php and create a new  
command.php with the following code:
<code type="php">
<?php
/**
  * Implementation of the Quota API for IMAP servers with a unix 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
  * authentication and file servers (e.g. via NIS/NFS).  And last, it (as
  * written) requires the POSIX PHP extensions.
  *
  * You must configure this driver in horde/imp/config/servers.php.  The
  * driver supports the following parameters:
  *   'quota_path' => Path to the quota binary - REQUIRED
  *   'dev_inbx'   => User´s INBOX file system device  - REQUIRED
  *                   If you have INBOX and IMAP folders in the same
  *                   device, use ONLY this parameter.
  *                   If you have INBOX and IMAP folders in different
  *                   devices and quota enabled on them you MUST use
  *                   'dev_fldrs' parameter also.
  *                   Usually: /,  /var/mail,  /var/spool/mail
  *                   Examples: '/dev/hda6', '/dev/sdb2', '/dev/md2',
  *                             '/dev/mapper/VOL1-VarM'
  *   'dev_fldrs'  => User´s home file system device  - OPTIONAL
  *                   Use only if you have INBOX and IMAP folders in
  *                   different devices and quota enabled on them.
  *                   For IMAP folders. Usually: /home
  *                   Examples: '/dev/hda7', '/dev/sda3', '/dev/md1',
  *                             '/dev/mapper/VOL2-Home'
  *
  * -------------------
  * Modified by Mauricio Jose T. Tecles <mtecles at biof.ufrj.br>
  * Functions IMP_Quota_command, blockSize, getQuota
  * Updated 2010 February 28
  *
  * -------------------
  * $Horde: imp/lib/Quota/command.php,v 1.11.10.17 2009-01-06 15:24:11  
jan Exp $
  *
  * Copyright 2002-2009 The Horde Project (http://www.horde.org/)
  *
  * 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  Eric Rostetter <eric.rostetter at physics.utexas.edu>
  * @package IMP_Quota
  */
class IMP_Quota_command extends IMP_Quota {

     /**
      * Constructor
      *
      * @param array $params  Hash containing connection parameters.
      */
     function IMP_Quota_command($params = array())
     {
         $params = array_merge(array('quota_path' => 'quota',
                                     'dev_inbx'  => null,
                                     'dev_fldrs'  => null),
                               $params);
         parent::IMP_Quota($params);
     }

     /**
      * Get the disk block size, if possible.
      *
      * We try to find out the disk block size from stat(). If not
      * available, stat() should return -1 for this value, in which
      * case we default to 1024 (for historical reasons). There are a
      * large number of reasons this may fail, such as OS support,
      * SELinux interference, the file being > 2 GB in size, the file
      * we're referring to not being readable, etc.
      */
     function blockSize()
     {
         $results = stat(__FILE__);
         if ($results['blksize'] > 1) {
             $blocksize = $results['blksize'];
         } else {
             $blocksize = 1024;
         }
		$blocksize = 1024;
         return $blocksize;
     }

     /**
      * Get quota information (used/allocated), in bytes.
      *
      * @return mixed  An associative array.
      *                'soflimithome' = Maximum quota allowed in dev_fldrs
      *                'usagehome' = Currently used portion of quota  
in dev_fldrs
      *                'soflimitvar' = Maximum quota allowed in dev_inbx
      *                'usagevar' = Currently used portion of quota in dev_inbx
      *                Returns PEAR_Error on failure.
      */
     function getQuota()
     {
         $imap_user = $_SESSION['imp']['user'];

         $cmdline = $this->_params['quota_path'] . ' -wu ' . $imap_user;
         unset($quota_data);
         $junk = exec($cmdline, $quota_data, $return_code);
         $junk = count( $quota_data);

         $blocksize = $this->blockSize();

         /*
         * Is quota exceeded?
         */

         if ($return_code == 0) {
             /*
             * Quota not exceeded
             * Is quota defined?
             */
             if (ereg("none$", $quota_data[0])) {
                 /*
                 * Quota not defined or user does not own any files.
                 */
                 if (empty($this->_params['dev_fldrs'])) {
                     return array('usagehome' => -1, 'soflimithome' =>  
-1, 'usagevar' => 0, 'soflimitvar' => 0);
                 } else {
                     return array('usagehome' => 0, 'soflimithome' =>  
0, 'usagevar' => 0, 'soflimitvar' => 0);
                 }
             } else {
                 /*
                 * Quota defined
                 */
                 if ( $junk == 4 ) {
                     /*
                     * Quotas defined for dev_fldrs and dev_inbx
                     */
                     if (ereg($this->_params['dev_fldrs'], $quota_data[2])) {
                         $quotahome = split("[[:blank:]]+",  
trim($quota_data[2]));
                         $quotavar = split("[[:blank:]]+",  
trim($quota_data[3]));
                         return array('usagehome' => $quotahome[1] *  
$blocksize, 'soflimithome' => $quotahome[2] * $blocksize, 'usagevar'  
=> $quotavar[1] * $blocksize, 'soflimitvar' => $quotavar[2] *  
$blocksize);
                     } elseif (ereg($this->_params['dev_inbx'],  
$quota_data[2])) {
                         $quotahome = split("[[:blank:]]+",  
trim($quota_data[3]));
                         $quotavar = split("[[:blank:]]+",  
trim($quota_data[2]));
                         return array('usagehome' => $quotahome[1] *  
$blocksize, 'soflimithome' => $quotahome[2] * $blocksize, 'usagevar'  
=> $quotavar[1] * $blocksize, 'soflimitvar' => $quotavar[2] *  
$blocksize);
                     }
                 } else {
                     /*
                     * Either quota is defined only for dev_fldrs or dev_inbx
                     * or user owns file in only one file system.
                     */
                     if (ereg($this->_params['dev_inbx'], $quota_data[2])) {
                         $quotavar = split("[[:blank:]]+",  
trim($quota_data[2]));
                         if (!empty($this->_params['dev_fldrs'])) {
                             return array('usagehome' => 0,  
'soflimithome' => 0, 'usagevar' => $quotavar[1] * $blocksize,  
'soflimitvar' => $quotavar[2] * $blocksize);
                         } else {
                             return array('usagehome' => -1,  
'soflimithome' => -1, 'usagevar' => $quotavar[1] * $blocksize,  
'soflimitvar' => $quotavar[2] * $blocksize);
                         }
                     } elseif (!empty($this->_params['dev_fldrs'])) {
                         if (ereg($this->_params['dev_fldrs'],  
$quota_data[2])) {
                             $quotahome = split("[[:blank:]]+",  
trim($quota_data[2]));
                             return array('usagehome' => $quotahome[1]  
* $blocksize, 'soflimithome' => $quotahome[2] * $blocksize, 'usagevar'  
=> 0, 'soflimitvar' => 0);
                         }
                     }
                 }
             }
         } else {
             /*
             * Some quota exceeded
             */
             if ( $junk == 4 ) {
                 /*
                 * Quotas defined for dev_fldrs and dev_inbx
                 */
                 if (ereg($this->_params['dev_fldrs'], $quota_data[2])) {
                     $quotahome = split("[[:blank:]]+", trim($quota_data[2]));
                     $quotavar = split("[[:blank:]]+", trim($quota_data[3]));
                 } elseif (ereg($this->_params['dev_inbx'], $quota_data[2])) {
                     $quotahome = split("[[:blank:]]+", trim($quota_data[3]));
                     $quotavar = split("[[:blank:]]+", trim($quota_data[2]));
                 }
                 /*
                 *
                 * Quota exceeded in dev_fldrs?
                 */
                 if (ereg("\*$", $quotahome[1])) {
                     $quotahome[1] = ereg_replace ("\*", "", $quotahome[1]);
                     $quotahome[4] = ereg_replace ("days", "", $quotahome[4]);
                 } else {
                     $quotahome[4] == "";
                 }
                 /*
                 * Quota exceeded in dev_inbx?
                 */
                 if (ereg("\*$", $quotavar[1])) {
                     $quotavar[1] = ereg_replace ("\*", "", $quotavar[1]);
                     $quotavar[4] = ereg_replace ("days", "", $quotavar[4]);
                 } else {
                     $quotavar[4] == "";
                 }
                 return array('usagehome' => $quotahome[1] *  
$blocksize, 'soflimithome' => $quotahome[2] * $blocksize, 'gracehome'  
=> $quotahome[4], 'usagevar' => $quotavar[1] * $blocksize,  
'soflimitvar' => $quotavar[2] * $blocksize, 'gracevar' => $quotavar[4]);
             } else {
                 /*
                 * Either quota is defined only for dev_fldrs or dev_inbx
                 * or user owns file in only one file system.
                 */
                 if (ereg($this->_params[dev_inbx], $quota_data[2])) {
                     /**
                     * Quota exceeded in dev_inbx.
                     */
                     $quotavar = split("[[:blank:]]+", trim($quota_data[2]));
                     $quotavar[1] = ereg_replace ("\*", "", $quotavar[1]);
                     $quotavar[4] = ereg_replace ("days", "", $quotavar[4]);
                     if (!empty($this->_params['dev_fldrs'])) {
                         return array('usagehome' => 0, 'soflimithome'  
=> 0, 'usagevar' => $quotavar[1] * $blocksize, 'soflimitvar' =>  
$quotavar[2] * $blocksize, 'gracevar' => $quotavar[4]);
                     } else {
                         return array('usagehome' => -1,  
'soflimithome' => -1, 'usagevar' => $quotavar[1] * $blocksize,  
'soflimitvar' => $quotavar[2] * $blocksize, 'gracevar' => $quotavar[4]);
                     }
                 } else {
                     /*
                     * Quota exceeded in dev_fldrs
                     */
                     $quotahome = split("[[:blank:]]+", trim($quota_data[2]));
                     $quotahome[1] = ereg_replace ("\*", "", $quotahome[1]);
                     $quotahome[4] = ereg_replace ("days", "", $quotahome[4]);
                     return array('usagehome' => $quotahome[1] *  
$blocksize, 'soflimithome' => $quotahome[2] * $blocksize, 'gracehome'  
=> $quotahome[4], 'usagevar' => 0, 'soflimitvar' => 0);
                 }
             }
         }
         return PEAR::raiseError(_("Unable to retrieve quota"), 'horde.error');
     }

}
</code>
----
++++ Configuration examples
(imp/config/servers.php)
* One device:
<code type="php">
$servers['imap'] = array(
     ...

     'quota' => array(
       'driver' => 'command',
       'params' => array(
         'quota_path' => '/usr/bin/quota',
         'dev_inbx' => '/dev/hda6')
      )
);
</code>
* Two devices:
<code type="php">
$servers['imap'] = array(
     ...

     'quota' => array(
       'driver' => 'command',
       'params' => array(
         'quota_path' => '/usr/bin/quota',
         'dev_fldrs' => '/dev/hda7',
         'dev_inbx' => '/dev/hda6')
      )
);
</code>
----
++++ Quota.php
* New messages:
> Alerts quota exceeded, grace time or expired;
> Alerts almost full;
> Normal quota information.

Backup your original imp/lib/Quota.php and create a new Quota.php with  
the following code:
<code type="php">
<?php
/**
  * IMP_Quota:: provides an API for retrieving Quota details from a mail
  * server.
  *
  * $Horde: imp/lib/Quota.php,v 1.23.10.15 2009-01-06 15:24:04 jan Exp $
  *
  * Copyright 2002-2009 The Horde Project (http://www.horde.org/)
  *
  * See the enclosed file COPYING for license information (GPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  *
  * -------------------
  * Modified by Mauricio Jose T. Tecles <mtecles at biof.ufrj.br>
  * Functions getQuota getMessages
  * Updated 2010 February 28
  *
  * -------------------
  * @author  Mike Cochrane <mike at graftonhall.co.nz>
  * @package IMP_Quota
  */
class IMP_Quota {

     /**
      * Hash containing connection parameters.
      *
      * @var array
      */
     var $_params = array();

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

         /* If 'password' exists in params, it has been encrypted in the
          * session so we need to decrypt. */
         if (isset($this->_params['password'])) {
             $this->_params['password'] =  
Secret::read(Secret::getKey('imp'), $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)
      *                'limithome' and 'usagehome' for dev_fldrs  
(/home) (mail folders)
      *                'limitvar' and 'usagevar' for dev_inbx  
(/var/mail) (INBOX)
      *                Returns PEAR_Error on failure.
      */
     function getQuota()
     {
         return array('usagehome' => 0, 'limithome' => 0, 'usagevar'  
=> 0, 'limitvar' => 0);
     }

     /**
      * Returns the quota messages variants, including sprintf placeholders


More information about the cvs mailing list