6.0.0-git
2018-12-12
Last Modified 2013-09-04 by Jan Schneider

This information is valid for Horde 5 and later only. See AltQuotaH4 for Horde 4 or AltQuotaH3 for Horde 3.

Notes

IMP 6, Horde 5
Modifies traditional and dynamic views to display quotas.

This describes modifications to IMP 6 (Horde 5) quota to use two different partitions (file systems) with quota enabled. One is used for INBOX and the other is used for IMAP folders.

This allows different quota values for INBOX and IMAP folders.

It uses system quota command.

Tested on Debian 6.0 (squeeze), Horde 5.0.3-IMP 6.0.3, Horde 5.0.4-IMP 6.0.4


Modifications

File Variables, Function(s)
imp/config/backends.php quota
imp/lib/Quota/Command.php getQuota
imp/lib/Quota.php construct
imp/lib/View/Subinfo.php construct
imp/templates/basic/subinfo.html.php quotaClassV, quotaClassH
imp/templates/dynamic/mailbox_subinfo.html.php quota-text
imp/lib/Ui/Quota.php quota
imp/lib/Ajax/Queue.php m, p, l, add
imp/js/dimpbase.js (IMP 6.0.3) quotaV, quotaH, quotaCallback
imp/js/dimpbase.js (IMP 6.0.4) quotaV, quotaH, quotaCallback

Last updated 2013-02-23


Descriptions


Configuration example (imp/config/backends.php)

  • Two partitions (file systems)
See Comand.php bellow for parameters.
Quota command must support "w" (do not wrap).

$servers['imap'] = array(
    ...

    'quota' => array(
        'driver' => 'command',
        'params' => array(
            'quota_path' => '/usr/bin/quota',
            'dev_inbx' => '/dev/disk/by-uuid/734e96a4-af8f-4c83-a12c-4ab11b139a13',
            'dev_fldrs' => '/dev/sdb2',
            'unit' => 'GB',
        )
    ),
    ...
);

imp/lib/Quota/Command.php

  • Have INBOX and IMAP folders in different partitions and quota enabled on them.
  • Function IMP_Quota_command accepts 2 new parameters:
'dev_inbx' (string) [REQUIRED] User´s INBOX file system device.
Usually maps to /var/mail, /var/spool/mail.
Examples: '/dev/hda6', '/dev/sdb2', '/dev/md2', '/dev/disk/by-uuid/097a934f-8fb1-4c9d-a330-817194b6e8a8'.
'dev_fldrs' (string) [REQUIRED] User´s home file system device. 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 and quota not defined for that user.

Backup your original imp/lib/Quota/Command.php and create a new Command.php with the following code:
<?php
/**
 * 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
 * authentication and file servers (e.g. via NIS/NFS).  And last, it (as
 * written) requires the POSIX PHP extensions.
 *
 * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @author   Eric Rostetter <eric.rostetter@physics.utexas.edu>
 * @category Horde
 * @license  http://www.horde.org/licenses/gpl GPL
 * @package  IMP
 * 
 * Modified by Mauricio Jose T. Tecles <mtecles@biof.ufrj.br>
 * Updated 2013 February 19
 */
class IMP_Quota_Command extends IMP_Quota
{
    /**
     * Constructor.
     *
     * @param array $params  Parameters:
     *   - grep_path: obsolete.
     *   - partition: obsolete.
     * 'dev_inbx'   - (string) [REQUIRED] User´s INBOX file system device
     *                   Usually: /,  /var/mail,  /var/spool/mail
     *                   Examples: '/dev/hda6', '/dev/sdb2', '/dev/md2', 
     *                             '/dev/mapper/VOL1-VarM'
     * 'dev_fldrs'  - (string) [REQUIRED] User´s home file system device
     *                   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'
     *   - quota_path: (string) [REQUIRED] Path to the quota binary.
     */
    public function __construct(array $params = array())
    {
        $params = array_merge(array(
            'quota_path' => 'quota',
            'dev_inbx'  => null,
            'dev_fldrs'  => null
        ), $params);

        parent::__construct($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.
     *
     * @return integer  The disk block size.
     */
    protected function _blockSize()
    {
        $results = stat(__FILE__);
        return ($results['blksize'] > 1)
            ? $results['blksize']
            : 1024;
    }

    /**
     * Get quota information (used/allocated), in bytes.
     *
     * @return array  An array with the following keys:
     *                'limit' = Maximum quota allowed
     *                'usage' = Currently used portion of quota (in bytes)
     * @throws IMP_Exception
     */
    public function getQuota()
    {

        $cmdline = $this->_params['quota_path'] . ' -uw ' . escapeshellarg($this->_params['username']);
        exec($cmdline, $quota_data, $return_code);
        
        $junk = count( $quota_data);
        $blocksize = 1024;

        /* 
        * Is quota exceeded? 
        */

        if ($return_code == 0) {
            /* 
            * Quota not exceeded 
            * Is quota defined? 
            */

            if (ereg("none$", $quota_data[0])) {
                /*
                * Quota not defined.
                */

                return array('usagehome' => 0, 'limithome' => 0, 'usagevar' => 0, 'limitvar' => 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, 'limithome' => $quotahome[2] * $blocksize, 'usagevar' => $quotavar[1] * $blocksize, 'limitvar' => $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, 'limithome' => $quotahome[2] * $blocksize, 'usagevar' => $quotavar[1] * $blocksize, 'limitvar' => $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]));

                        return array('usagehome' => 0, 'limithome' => 0, 'usagevar' => $quotavar[1] * $blocksize, 'limitvar' => $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, 'limithome' => $quotahome[2] * $blocksize, 'usagevar' => 0, 'limitvar' => 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, 'limithome' => $quotahome[2] * $blocksize, 'gracehome' => $quotahome[4], 'usagevar' => $quotavar[1] * $blocksize, 'limitvar' => $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]);

                    return array('usagehome' => 0, 'limithome' => 0, 'usagevar' => $quotavar[1] * $blocksize, 'limitvar' => $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, 'limithome' => $quotahome[2] * $blocksize, 'gracehome' => $quotahome[4], 'usagevar' => 0, 'limitvar' => 0);
                }
            }
        }

        throw new IMP_Exception(_("Unable to retrieve quota"));
    }

}

imp/lib/Quota.php

  • New formats.
Only short formats.

Backup your original imp/lib/Quota.php and replace function construct with the following code:
    public function __construct(array $params = array())
    {
        $this->_params = array_merge($this->_params, $params);

        $this->_params['format'] = array(
            'shortv' => isset($this->_params['format']['short'])
                ? $this->_params['format']['short']
                : _("Entrada: %.0f%% of %.1f %s"),
            'shorth' => isset($this->_params['format']['shorth'])
                ? $this->_params['format']['shorth']
                : _(" - Pastas: %.0f%% de %.1f %s"),
            'sshorth' => isset($this->_params['format']['sshorth'])
                ? $this->_params['format']['sshorth']
                : _("Pastas: %.0f%% de %.1f %s"),
            'nolimit_shortv' => isset($this->_params['format']['nolimit_short'])
                ? $this->_params['format']['nolimit_short']
                : _("Entrada: %.1f %s"),
            'nolimit_shorth' => isset($this->_params['format']['nolimit_shorth'])
                ? $this->_params['format']['nolimit_shorth']
                : _(" - Pastas: %.1f %s"),
            'nolimit_sshorth' => isset($this->_params['format']['nolimit_sshorth'])
                ? $this->_params['format']['nolimit_sshorth']
                : _("Pastas: %.1f %s"),
        );
    }

imp/lib/View/Subinfo.php

Backup your original imp/lib/View/Subinfo.php and replace function construct with the following code:

    public function __construct($config = array())
    {
        $config['templatePath'] = IMP_TEMPLATES . '/basic';
        parent::__construct($config);

        $quotadata = $GLOBALS['injector']->getInstance('IMP_Ui_Quota')->quota();
        if (!empty($quotadata)) {
            $this->quotaClassV = $quotadata['classvar'];
            $this->quotaTextV = $quotadata['messagevar'];
            $this->quotaClassH = $quotadata['classhome'];
            $this->quotaTextH = $quotadata['messagehome'];
        }
    }


imp/templates/basic/subinfo.html.php

Backup your original imp/templates/basic/subinfo.html.php and replace the following code:
From:

<?php if ($this->quotaText): ?>
<span class="<?php echo $this->quotaClass ?>"><?php echo $this->quotaText ?></span>
<?php endif ?>

To:
<?php if ($this->quotaTextV): ?>
<span class="<?php echo $this->quotaClassV ?>"><?php echo $this->quotaTextV ?></span>
<?php endif ?>
<?php if ($this->quotaTextV): ?>
<span class="<?php echo $this->quotaClassH ?>"><?php echo $this->quotaTextH ?></span>
<?php endif ?>


imp/templates/dynamic/mailbox_subinfo.html.php

Backup your original imp/templates/dynamic/mailbox_subinfo.html.php and replace the following code:
From:

<span id="quota-text"></span>

To:
<span id="quota-textV"></span><span id="quota-textH"></span>


imp/lib/Ui/Quota.php

Backup your original imp/lib/Ui/Quota.php and create a new Quota.php with the following code:

<?php
/**
 * Common code dealing with quota handling.
 *
 * Copyright 2012 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @author   Michael Slusarz <slusarz@horde.org>
 * @category Horde
 * @license  http://www.horde.org/licenses/gpl GPL
 * @package  IMP
 * 
 * Modified by Mauricio Jose T. Tecles <mtecles@biof.ufrj.br>
 * Updated 2013 February 23
 */
class IMP_Ui_Quota
{
    /**
     * Returns data needed to output quota.
     *
     * @return array  Array with these keys: class, message, percent.
     */
    public function quota()
    {
        global $injector, $session;

        if (!$session->get('imp', 'imap_quota')) {
            return false;
        }

        try {
            $quotaDriver = $injector->getInstance('IMP_Quota');
            $quota = $quotaDriver->getQuota();
        } catch (IMP_Exception $e) {
            Horde::log($e, 'ERR');
            return false;
        }

        if (empty($quota)) {
            return false;
        }

        $strings = $quotaDriver->getMessages();
        list($calc, $unit) = $quotaDriver->getUnit();
        $ret = array(
            'classvar' => '',
            'percentvar' => 0,
            'classhome' => '',
            'percenthome' => 0
        );

        /* Quota for dev_fldrs */
        unset($ret['messagehome']);
        if ($quota['limithome'] != 0) {
            $quota['usagehome'] = $quota['usagehome'] / $calc;
            $quota['limithome'] = $quota['limithome'] / $calc;
            $ret['percenthome'] = ($quota['usagehome'] * 100) / $quota['limithome'];

            if ($ret['percenthome'] >= 100) {
                $ret['gracehome'] = $quota['gracehome'];
                $ret['classhome'] = 'quotaalert';
            } elseif ($ret['percenthome'] >= 90) {
                $ret['classhome'] = 'quotawarn';
            }

            if ($quota['usagevar'] != 0) {
                $ret['messagehome'] = sprintf($strings['shorth'], $ret['percenthome'], $quota['limithome'], $unit);
                $ret['percenthome'] = sprintf("%.2f", $ret['percenthome']);
            } else {
                $ret['messagehome'] = sprintf($strings['sshorth'], $ret['percenthome'], $quota['limithome'], $unit);
                $ret['percenthome'] = sprintf("%.2f", $ret['percenthome']);
            }

        } else {
            if ($quota['usagehome'] != 0) {
                if ($quota['usagevar'] != 0) {
                    $quota['usagehome'] = $quota['usagehome'] / $calc;
                    $ret['messagehome'] = sprintf($strings['nolimit_shorth'], $quota['usagehome'], $unit);
                } else {
                    $quota['usagehome'] = $quota['usagehome'] / $calc;
                    $ret['messagehome'] = sprintf($strings['nolimit_sshorth'], $quota['usagehome'], $unit);
                }
            } else {
                $ret['messagehome'] = _(" ");
            }
        }

        /* Quota for dev_inbx */
        if ($quota['limitvar'] != 0) {
            $quota['usagevar'] = $quota['usagevar'] / $calc;
            $quota['limitvar'] = $quota['limitvar'] / $calc;
            $ret['percentvar'] = ($quota['usagevar'] * 100) / $quota['limitvar'];

            if ($ret['percentvar'] >= 100) {
                $ret['gracevar'] = $quota['gracevar'];
                $ret['classvar'] = 'quotaalert';
            } elseif ($ret['percentvar'] >= 90) {
                $ret['classvar'] = 'quotawarn';
            }

            $ret['messagevar'] = sprintf($strings['shortv'], $ret['percentvar'], $quota['limitvar'], $unit);
            $ret['percentvar'] = sprintf("%.2f", $ret['percentvar']);
        } else {
            if ($quota['usagevar'] != 0) {
                $quota['usagevar'] = $quota['usagevar'] / $calc;

                $ret['messagevar'] = sprintf($strings['nolimit_shortv'], $quota['usagevar'], $unit);
            } else {
                $ret['messagevar'] = _(" ");
            }
        }
        
        return $ret;
    }

}


imp/lib/Ajax/Queue.php

  • mv, pv, lv: quota message, percentage and class for Inbox
  • mh, ph, lh: quota message, percentage and class for "home"

Backup your original imp/lib/Ajax/Queue.php and replace function add with the following code:

    public function add(IMP_Ajax_Application $ajax)
    {
        /* Add flag information. */
        if (!empty($this->_flag)) {
            $ajax->addTask('flag', $this->_flag);
            $this->_flag = array();
        }

        /* Add folder tree information. */
        $imptree = $GLOBALS['injector']->getInstance('IMP_Imap_Tree');
        $imptree->setIteratorFilter(IMP_Imap_Tree::FLIST_NOSPECIALMBOXES);
        $out = $imptree->getAjaxResponse();
        if (!empty($out)) {
            $ajax->addTask('mailbox', array_merge($out, $this->_mailboxOpts));
        }

        /* Add mail log information. */
        if (!empty($this->_maillog)) {
            $imp_maillog = $GLOBALS['injector']->getInstance('IMP_Maillog');
            $maillog = array();

            foreach ($this->_maillog as $val) {
                if ($tmp = $imp_maillog->getLogObs($val['msg_id'])) {
                    $log_ob = new stdClass;
                    $log_ob->log = $tmp;
                    $log_ob->mbox = $val['mailbox']->form_to;
                    $log_ob->uid = $val['uid'];
                    $maillog[] = $log_ob;
                }
            }

            if (!empty($maillog)) {
                $ajax->addTask('maillog', $maillog);
            }
        }

        /* Add message information. */
        if (!empty($this->_messages)) {
            $ajax->addTask('message', $this->_messages);
            $this->_messages = array();
        }

        /* Add poll information. */
        $poll = $poll_list = array();
        foreach ($this->_poll as $val) {
            $poll_list[strval($val)] = 1;
        }

        $imap_ob = $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create();
        if ($imap_ob->ob) {
            foreach ($imap_ob->statusMultiple(array_keys($poll_list), Horde_Imap_Client::STATUS_UNSEEN) as $key => $val) {
                $poll[IMP_Mailbox::formTo($key)] = intval($val['unseen']);
            }
        }

        if (!empty($poll)) {
            $ajax->addTask('poll', $poll);
            $this->_poll = array();
        }

        /* Add quota information. */
        if ($this->_quota &&
            ($quotadata = $GLOBALS['injector']->getInstance('IMP_Ui_Quota')->quota())) {
            /* Quota for dev_inbx */
            $merrov = null;
            if (round($quotadata['percentvar']) >= 100) {
                $mvclasse = 'horde.error';

                if (ereg("none", $quotadata['gracevar'])) {
                    $merrov = sprintf("Inbox above limit. Grace time expired.");
                } else {
                    $merrov = sprintf("Inbox above limit. Solve in %s day(s)", $quotadata['gracevar']);
                }
            } else if ($quotadata['percentvar'] >= 90) {
                
                    $merrov = sprintf("Inbox above 90%%.");
                    $mvclasse = 'horde.warning';
            }
            /* Quota for dev_fldrs */
            $merroh = null;
            if (round($quotadata['percenthome'] >= 100)) {
                $mhclasse = 'horde.error';
                if (ereg("none", $quotadata['gracehome'])) {
                    $merroh = sprintf("Folders above limit. Grace time expired.");
                } else {
                    $merroh = sprintf("Folders above limit. Solve in %s day(s)", $quotadata['gracehome']);
                }
            } elseif ($quotadata['percenthome'] >= 90) {
                $merroh = sprintf("Folders above 90%%.");
                $mhclasse = 'horde.warning';
            }

            if (!empty($merrov)) {
                $GLOBALS['notification']->push($merrov, $mvclasse);
            }
            if (!empty($merroh)) {
                $GLOBALS['notification']->push($merroh, $mhclasse);
            }

            $ajax->addTask('quota', array(
                'mv' => $quotadata['messagevar'],
                'pv' => round($quotadata['percentvar']),
                'lv' => $quotadata['percentvar'] >= 100
                    ? 'alert'
                    : ($quotadata['percentvar'] >= 90 ? 'warn' : 'control'),
                'mh' => $quotadata['messagehome'],
                'ph' => round($quotadata['percenthome']),
                'lh' => $quotadata['percenthome'] >= 100
                    ? 'alert'
                    : ($quotadata['percenthome'] >= 90 ? 'warn' : 'control')
            ));
            $this->_quota = false;
        }
    }


imp/js/dimpbase.js (IMP 6.0.3)

  • quotaCallback
quotaV, mv, lv
quotaH, mh, lh

Backup your original imp/js/dimpbase.js. Edit dimpbase.js and replace function quotaCallback with the following code:
    quotaCallback: function(r)
    {
        var quotaV = $('quota-textV');
        var quotaH = $('quota-textH');
        quotaV.setText(r.mv);
        switch (r.lv) {
        case 'alert':
            quotaV.removeClassName('quotawarn');
            quotaV.addClassName('quotaalert');
            break;
        case 'warn':
            quotaV.removeClassName('quotaalert');
            quotaV.addClassName('quotawarn');
            break;
        case 'control':
            quotaV.removeClassName('quotawarn');
            quotaV.removeClassName('quotaalert');
            break;
        }
        quotaH.setText(r.mh);
        switch (r.lh) {
        case 'alert':
            quotaH.removeClassName('quotawarn');
            quotaH.addClassName('quotaalert');
            break;
        case 'warn':
            quotaH.removeClassName('quotaalert');
            quotaH.addClassName('quotawarn');
            break;
        case 'control':
            quotaH.removeClassName('quotawarn');
            quotaH.removeClassName('quotaalert');
            break;
        }
    },

imp/js/dimpbase.js (IMP 6.0.4)

  • quotaCallback
quotaV, mv, lv
quotaH, mh, lh

Backup your original imp/js/dimpbase.js. Edit dimpbase.js and replace function quotaCallback with the following code:
    quotaCallback: function(r)
    {
        var quotaV = $('quota-textV');
        var quotaH = $('quota-textH');
        quotaV.removeClassName('quotaalert').
            removeClassName('quotawarn').
            setText(r.mv);
        quotaH.removeClassName('quotaalert').
            removeClassName('quotawarn').
            setText(r.mh);

        switch (r.lv) {
        case 'alert':
        case 'warn':
            quotaV.addClassName('quota' + r.lv);
            break;
        }

        switch (r.lh) {
        case 'alert':
        case 'warn':
            quotaH.addClassName('quota' + r.lh);
            break;
        }
    },