1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class IMP_Quota_Mdaemon extends IMP_Quota_Base
16: {
17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: public function __construct(array $params = array())
28: {
29: if (!isset($params['app_location'])) {
30: throw new IMP_Exception('Missing app_location parameter in quota config.');
31: }
32:
33: parent::__construct($params);
34: }
35:
36: 37: 38: 39: 40: 41: 42: 43:
44: public function getQuota()
45: {
46: $imap_ob = $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create();
47: $userDetails = $this->_getUserDetails(
48: $this->_params['username'],
49: $GLOBALS['session']->get('imp', 'maildomain')
50: );
51:
52: if ($userDetails !== false) {
53: $userHome = trim(substr($userDetails, 105, 90));
54: $total = intval(substr($userDetails, 229, 6)) * 1024;
55:
56: if ($total == 0) {
57: return array('usage' => 0, 'limit' => 0);
58: }
59:
60: if (($taken = $this->_mailboxSize($userHome)) !== false) {
61: return array('usage' => $taken, 'limit' => $total);
62: }
63: }
64:
65: throw new IMP_Exception(_("Unable to retrieve quota"));
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75: 76:
77: protected function _mailboxSize($path)
78: {
79: $contents = file_get_contents($path . '\imap.mrk');
80:
81: $c_len = strlen($contents);
82: $pointer = 36;
83: $size = 0;
84:
85: while ($pointer < $c_len) {
86: $details = unpack('a17Filename/a11Crap/VSize', substr($contents, $pointer, 36));
87: $size += $details['Size'];
88: $pointer += 36;
89: }
90:
91:
92: $di = new DirectoryIterator($path);
93: foreach ($di as $filename => $entry) {
94: if (!$di->isDot() &&
95: (substr($filename, -5) == '.IMAP')) {
96: $size += $this->_mailboxSize($entry->getPathname() . '\\');
97: }
98: }
99:
100: return $size;
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110:
111: protected function _getUserDetails($user, $realm)
112: {
113: $searchString = str_pad($realm, 45) . str_pad($user, 30);
114:
115: if (!($fp = fopen($this->_params['app_location'] . '/userlist.dat', 'rb'))) {
116: return false;
117: }
118:
119: while (!feof($fp)) {
120: $line = fgets($fp, 4096);
121: if (substr($line, 0, strlen($searchString)) == $searchString) {
122: fclose($fp);
123: return $line;
124: }
125: }
126: fclose($fp);
127:
128: return false;
129: }
130:
131: }
132: