1: <?php
2: /**
3: * Copyright 2010-2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2010-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * The abstract class that all quota drivers inherit from.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2010-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: abstract class IMP_Quota
24: {
25: /**
26: * Driver parameters.
27: *
28: * @var array
29: */
30: protected $_params = array(
31: 'hide_when_unlimited' => false,
32: 'unit' => 'MB'
33: );
34:
35: /**
36: * Constructor.
37: *
38: * @param array $params Parameters:
39: * - unit: (string) What storage unit the quota messages should be
40: * displayed in. Either 'GB', 'MB', or 'KB'.
41: * - username: (string) The username to query.
42: */
43: public function __construct(array $params = array())
44: {
45: $this->_params = array_merge(
46: $this->_params,
47: array(
48: 'format' => array(
49: 'nolimit_short' => _("%.0f %s"),
50: 'short' => _("%.0f%% of %.0f %s")
51: )
52: ),
53: $params
54: );
55: }
56:
57: /**
58: * Get quota information (used/allocated), in bytes.
59: *
60: * @param string $mailbox Mailbox to check.
61: *
62: * @return array An array with the following keys:
63: * - limit: Maximum quota allowed
64: * - usage: Currently used portion of quota (in bytes)
65: *
66: * @throws IMP_Exception
67: */
68: abstract public function getQuota($mailbox = null);
69:
70: /**
71: * Should quota be displayed if no limit is configured?
72: *
73: * @return boolean Whether to hide the quota.
74: */
75: public function isHiddenWhenUnlimited()
76: {
77: return $this->_params['hide_when_unlimited'];
78: }
79:
80: /**
81: * Returns the quota messages variants, including sprintf placeholders.
82: *
83: * @return array An array with quota message templates.
84: */
85: public function getMessages()
86: {
87: return $this->_params['format'];
88: }
89:
90: /**
91: * Determine the units of storage to display in the quota message.
92: *
93: * @return array An array of size and unit type.
94: */
95: public function getUnit()
96: {
97: $unit = $this->_params['unit'];
98:
99: switch ($unit) {
100: case 'GB':
101: $calc = 1024 * 1024 * 1024.0;
102: break;
103:
104: case 'KB':
105: $calc = 1024.0;
106: break;
107:
108: case 'MB':
109: default:
110: $calc = 1024 * 1024.0;
111: $unit = 'MB';
112: break;
113: }
114:
115: return array($calc, $unit);
116: }
117:
118: }
119: