"Fossies" - the Fresh Open Source Software Archive 
Member "openmailadmin-1.0.1/inc/lib/imap/Fake_IMAP.php" (26 May 2007, 7073 Bytes) of package /linux/privat/old/openmailadmin-1.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "Fake_IMAP.php" see the
Fossies "Dox" file reference documentation.
1 <?php
2 /**
3 * For emulating cyrus-imapd client by utilizing database as storage.
4 */
5 class Fake_IMAP
6 implements IMAP_Administrator
7 {
8 private $connection_data;
9 private $db;
10 private $tablenames;
11 private $separator = '.';
12
13 public $error_msg = '';
14
15 function __construct(array $connection_data, ADOConnection $adodb_handler, array $tablenames) {
16 $this->connection_data = $connection_data;
17 $this->db = $adodb_handler;
18 $this->tablenames = $tablenames;
19 }
20
21 public function gethierarchyseparator() {
22 return $this->separator;
23 }
24
25 /**
26 * @param cmd Fully formatted command.
27 * @return Unless additional data is provided the return will be either true or false. On additional data an array will be returned.
28 */
29 private function command($line) {
30 global $oma;
31
32 switch($line) {
33 case '. list "" *':
34 // query for all visible folders
35 $ret = array();
36 $result = $this->db->Execute('SELECT mailbox as folder,'
37 .' (SELECT COUNT(*) FROM '.$this->tablenames['imap_demo'].' WHERE mailbox LIKE '.$this->db->Concat('folder', $this->db->qstr('.%')).') AS children'
38 .' FROM '.$this->tablenames['imap_demo']
39 .' WHERE ACL LIKE '.$this->db->qstr('% '.$oma->current_user->mbox.' l%').' OR ACL LIKE '.$this->db->qstr($oma->current_user->mbox.' l%').' OR ACL LIKE '.$this->db->qstr('% anyone l%').' OR ACL LIKE '.$this->db->qstr('anyone l%'));
40 if(!$result === false) {
41 while(!$result->EOF) {
42 $ret[] = '* LIST '
43 .($result->fields['children'] == 0 ? '(\HasNoChildren)' : '(\HasChildren)')
44 .' "." "'.$result->fields['folder'].'"';
45 $result->MoveNext();
46 }
47 // $ret[] = '. OK Completed (0.003 secs '.mysql_num_rows($result).' calls)';
48 }
49 return $ret;
50 break;
51 default:
52 trigger_error('Unknown command in fake-cyradm: "'.$line.'" - please report.');
53 return array();
54 break;
55 }
56 }
57
58 public function createmb($mb) {
59 global $oma;
60
61 if(isset($_GET['folder'])) {
62 $newacl = $this->db->GetOne('SELECT ACL FROM '.$this->tablenames['imap_demo'].' WHERE mailbox='.$this->db->qstr($_GET['folder']));
63 $acl = $this->getacl($_GET['folder']);
64 if(isset($acl[$oma->current_user->mbox]) && stristr($acl[$oma->current_user->mbox], 'a')
65 || isset($acl['anyone']) && stristr($acl['anyone'], 'a')) {
66 $this->db->Execute('INSERT INTO '.$this->tablenames['imap_demo'].' (mailbox, ACL) VALUES (?,?)', array($mb, $newacl));
67 } else {
68 $this->error_msg = 'You need "a"-rights on that mailbox.';
69 return false;
70 }
71 } else if(isset($_POST['mbox'])) {
72 $this->db->Execute('INSERT INTO '.$this->tablenames['imap_demo'].' (mailbox, ACL) VALUES (?,?)', array($mb, $_POST['mbox'].' lrswipcda'));
73 } else {
74 $this->db->Execute('INSERT INTO '.$this->tablenames['imap_demo'].' (mailbox, ACL) VALUES (?,?)', array($mb, $oma->current_user->mbox.' lrswipcda'));
75 }
76 if($this->db->Affected_Rows() < 1) {
77 $this->error_msg = $this->db->ErrorMsg();
78 return false;
79 } else {
80 return true;
81 }
82 }
83
84 public function deletemb($mb) {
85 $this->db->Execute('DELETE FROM '.$this->tablenames['imap_demo'].' WHERE mailbox='.$this->db->qstr($mb).' OR mailbox LIKE '.$this->db->qstr($mb.'%'));
86 if($this->db->Affected_Rows() < 1) {
87 return false;
88 }
89 return true;
90 }
91
92 public function renamemb($from_mb, $to_mb) {
93 $this->db->Execute('UPDATE '.$this->tablenames['imap_demo']
94 .' SET mailbox=REPLACE(mailbox, '.$this->db->qstr($from_mb).', '.$this->db->qstr($to_mb).'), '
95 .'ACL=REPLACE(ACL, '.$this->db->qstr(str_replace('user.', '', $from_mb)).', '.$this->db->qstr(str_replace('user.', '', $to_mb)).')'
96 .' WHERE mailbox = '.$this->db->qstr($from_mb).' OR mailbox LIKE '.$this->db->qstr($from_mb.'%'));
97 if($this->db->ErrorMsg() != '') {
98 return false;
99 }
100 return true;
101 }
102
103 public function getmailboxes($ref = '', $pat = '*') {
104 $result = array();
105
106 foreach($this->command('. list "" *') as $folder) {
107 if(preg_match('/\*\sLIST\s\((.*)\)\s\"(.*?)\"\s\"(.*?)\"/', $folder, $arr)) {
108 $result[]
109 = array('attributes' => $arr[1],
110 'delimiter' => $arr[2],
111 'name' => trim($arr[3]));
112 }
113 }
114
115 return $result;
116 }
117
118 public function setquota($mb, $many, $storage = '') {
119 if(is_numeric($many)) {
120 $this->db->Execute('UPDATE '.$this->tablenames['imap_demo'].' SET qmax='.intval(max(1, $many)).', used=FLOOR(RAND()*'.intval(max(1, $many)).') WHERE mailbox='.$this->db->qstr($mb));
121 } else if(is_null($many)) {
122 $this->db->Execute('UPDATE '.$this->tablenames['imap_demo'].' SET qmax=0 WHERE mailbox='.$this->db->qstr($mb));
123 } else {
124 $this->error_msg = 'Quota has either to be numeric or null!';
125 return false;
126 }
127 if($this->db->Affected_Rows() < 1) {
128 $this->error_msg = 'Given mailbox does not exist.';
129 return false;
130 }
131
132 return true;
133 }
134
135 public function getquota($mb) {
136 $row = $this->db->GetRow('SELECT qmax,used FROM '.$this->tablenames['imap_demo'].' WHERE mailbox='.$this->db->qstr($mb));
137 if($row === false || !isset($row['qmax'])) {
138 $this->error_msg = 'Given mailbox does not exist.';
139 return new Quota();
140 } else {
141 if($row['qmax'] == 0) {
142 return new Quota();
143 }
144 return new Quota($row['used'], $row['qmax']);
145 }
146 }
147
148 public function get_users_quota($username) {
149 return $this->getquota($this->format_user($username));
150 }
151
152 public function get_acl_available() {
153 $assumed = array('l', 'r', 's', 'w', 'i', 'p', 'c', 'd', 'a');
154 return $assumed;
155 }
156
157 public function setacl($mb, $user, $acl) {
158 global $oma;
159
160 // does the user exist?
161 $user = $this->db->GetOne('SELECT mbox FROM '.$this->tablenames['user'].' WHERE mbox='.$this->db->qstr($user));
162 if(!$user === false) {
163 // fetch old ACL
164 $facl = $this->getacl($mb);
165 if(isset($facl[$oma->current_user->mbox]) && stristr($facl[$oma->current_user->mbox], 'a')
166 || isset($facl['anyone']) && stristr($facl['anyone'], 'a')) {
167 // modify ACL
168 if($acl == 'none') {
169 unset($facl[$user]);
170 } else {
171 $facl[$user] = trim($acl);
172 }
173 // unify keys and values for storage
174 $store = '';
175 foreach($facl as $user=>$rgh) {
176 $store .= $user.' '.$rgh.' ';
177 }
178
179 // write to MySQL
180 $this->db->Execute('UPDATE '.$this->tablenames['imap_demo'].' SET ACL='.$this->db->qstr(trim($store)).' WHERE mailbox='.$this->db->qstr($mb));
181 return true;
182 }
183 } else {
184 $this->error_msg = 'User does not exist.';
185 }
186 return false;
187 }
188
189 public function getacl($mb) {
190 $acl = $this->db->GetOne('SELECT ACL FROM '.$this->tablenames['imap_demo'].' WHERE mailbox='.$this->db->qstr($mb));
191 if($acl === false) {
192 return array();
193 } else {
194 return hsys_getACLInfo(array('* ACL '.$mb.' '.$acl), $mb);
195 }
196 }
197
198 public function getversion() {
199 return '2.2.12';
200 }
201
202 public function format_user($username, $folder = null) {
203 if(is_null($folder)) {
204 return('user'.$this->gethierarchyseparator().$username.$this->connection_data['VDOM']);
205 } else {
206 return($this->format_user($username).$this->gethierarchyseparator().$folder);
207 }
208 }
209
210 public function is_valid_username($username) {
211 return preg_match('/^\w{2,16}$/', $username);
212 }
213
214 }
215 ?>