ab379c617ad780cb78d4302ef16026684469ed0e
bernd Erste Version des vmail-Modul

bernd authored 16 years ago

1) <?php
2) require_once('inc/base.php');
3) require_once('inc/debug.php');
4) 
5) 
6) function user_has_vmail_domain() 
7) {
8) 	$role = $_SESSION['role'];
9) 	if (! ($role & ROLE_SYSTEMUSER)) {
10) 		return false;
11) 	}
12) 	$uid = (int) $_SESSION['userinfo']['uid'];
13) 	$result = db_query("SELECT COUNT(*) FROM mail.v_vmail_domains WHERE useraccount='{$uid}'");
14) 	$row = mysql_fetch_array($result);
15) 	$count = $row[0];
16) 	DEBUG("User has {$count} vmail-domains");
17) 	return ( (int) $count > 0 );
18) }
19) 
20) 
21) function empty_account()
22) {
23) 	$account = array(
24) 		'id' => NULL,
25) 		'local' => '',
26) 		'domain' => NULL,
27) 		'type' => 'mailbox',
28) 		'data' => NULL,
29) 		'spamfilter' => NULL,
30) 		'virusfilter' => NULL,
31) 		'spamexpire' => 7,
32) 		'virusexpire' => 7
33) 		);
34) 	return $account;
35) 
36) }
37) 
38) function get_account_details($id)
39) {
40) 	$id = (int) $id;
41) 	$uid = (int) $_SESSION['userinfo']['uid'];
42) 	$result = db_query("SELECT id, local, domainid as domain, type, data, spamfilter, virusfilter from mail.v_virtual_mail WHERE useraccount='{$uid}' AND id={$id} LIMIT 1");
43) 	if (mysql_num_rows($result) == 0)
44) 		system_failure('Ungültige ID oder kein eigener Account');
45) 	return mysql_fetch_assoc($result);;
46) 	
47) }
48) 
49) function get_vmail_accounts()
50) {
51) 	$uid = (int) $_SESSION['userinfo']['uid'];
52) 	$result = db_query("SELECT * from mail.v_virtual_mail WHERE useraccount='{$uid}'");
53) 	$ret = array();
54) 	while ($line = mysql_fetch_assoc($result))
55) 	{
56) 		array_push($ret, $line);
57) 	}
58) 	DEBUG($ret);
59) 	return $ret;
60) }
61) 
62) 
63) 
64) function get_vmail_domains()
65) {
66) 	$uid = (int) $_SESSION['userinfo']['uid'];
67) 	$result = db_query("SELECT id, domainname FROM mail.v_vmail_domains WHERE useraccount='{$uid}'");
68) 	if (mysql_num_rows($result) == 0)
69) 		system_failure('Sie haben keine Domains für virtuelle Mail-Verarbeitung');
70) 	$ret = array();
71) 	while ($tmp = mysql_fetch_object($result))
72) 		array_push($ret, $tmp);
73) 	return $ret;
74) }
75) 
76) 
77) 
78) function domainselect($selected = NULL, $selectattribute = '')
79) {
80)   global $domainlist;
81)   if ($domainlist == NULL)
82)     $domainlist = get_vmail_domains();
83)   $selected = (int) $selected;
84) 
85)   $ret = '<select id="domain" name="domain" size="1" '.$selectattribute.' >';
86)   foreach ($domainlist as $dom)
87)   {
88)     $s = ($selected == $dom->id) ? ' selected="selected" ': '';
89)     $ret .= "<option value=\"{$dom->id}\"{$s}>{$dom->domainname}</option>\n";
90)   }
91)   $ret .= '</select>';
92)   return $ret;
93) }
94) 
95) 
96) function encrypt_mail_password($pw)
97) {
98)   DEBUG("unencrypted PW: ".$pw);
99)   require_once('inc/base.php');
100)   $salt = random_string(8);
101)   $encpw = crypt($pw, "\$1\${$salt}\$");
102)   DEBUG("encrypted PW: ".$encpw);
103)   return chop($encpw);
104) 
105) }
106) 
107) 
108) 
109) function save_vmail_account($account)
110) {
111)   $uid = (int) $_SESSION['userinfo']['uid'];
112)   $id = $account['id'];
113)   if ($id != NULL)
114)   {
115)     $id = (int) $id;
116)     $oldaccount = get_account_details($id);
117)     // Erzeugt einen system_error() wenn ID ungültig
118)   }
119)   // Ab hier ist $id sicher, entweder NULL oder eine gültige ID des aktuellen users
120) 
121)   $account['local'] = filter_input_username($account['local']);
122)   if ($account['local'] == '')
123)   {
124)     input_error('Die E-Mail-Adresse braucht eine Angabe vor dem »@«!');
125)     return false;
126)   }
127)   $account['domain'] = (int) $account['domain'];
128)   $domainlist = get_vmail_domains();
129)   $valid_domain = false;
130)   foreach ($domainlist as $dom)
131)   {
132)     if ($dom->id == $account['domain'])
133)     {
134)       $valid_domain = true;
135)       break;
136)     }
137)   }
138)   if (($account['domain'] == 0) || (! $valid_domain))
139)   {
140)     input_error('Bitte wählen Sie eine Ihrer Domains aus!');
141)     return false;
142)   }
143)   $type = NULL;
144)   switch ($account['type'])
145)   {
146)     case 'forward':
bernd mehrere Adressen erlauben

bernd authored 16 years ago

147)                      $forward_to = preg_split("/[\s,]+/", $account['data']);
148) 		     foreach ($forward_to as $addr)
149) 		     {
150)                        $addr = filter_input_general($addr);
151)                        if (! check_emailaddr($addr))
bernd Fehlerhafte Adresse in die...

bernd authored 16 years ago

152)                          system_failure('Das Weiterleitungs-Ziel »'.$addr.'« ist keine E-Mail-Adresse!');
bernd mehrere Adressen erlauben

bernd authored 16 years ago

153) 		     }
154) 		     $account['data'] = implode(' ', $forward_to);
bernd Erste Version des vmail-Modul

bernd authored 16 years ago

155) 		     $type = 'forward';
156)                      break;
157)     case 'mailbox':
158)                      $account['data'] = stripslashes($account['data']);
159)                      if ($account['data'] != '')
160)                      {
161)                        $crack = strong_password($account['data']);
162)                        if ($crack !== true)
163)                        {
164)                          input_error('Ihr Passwort ist zu einfach. bitte wählen Sie ein sicheres Passwort!'."\nDie Fehlermeldung lautet: »{$crack}«");
165)                          return false;
166)                        }
167)                        $account['data'] = encrypt_mail_password($account['data']);
168)                      }
169)                      $type = 'mailbox';
170)                      break;
171)   }
172)   if ($type == NULL)
173)   {
174)     input_error('Problem mit der »type«-Variable!');
175)     return false;
176)   }
177) 
178)   $spam = 'NULL';
179)   switch ($account['spamfilter'])
180)   {
181)     case 'folder':
182)       if ($type == 'forward')
183)       {
184)         input_error('Sie können nicht in einen IMAP-Unterordner zustellen lassen, wenn Sie gar kein IMAP-Konto anlegen!');
185) 	return false;
186)       }
187)       $spam = "'folder'";
188)       break;
189)     case 'tag':
190)       $spam = "'tag'";
191)       break;
192)     case 'delete':
193)       $spam = "'delete'";
194)       break;
195)   }
196) 
197)   $virus = 'NULL';
198)   switch ($account['virusfilter'])
199)   {
200)     case 'folder':
201)       if ($type == 'forward')
202)       {
203)         input_error('Sie können nicht in einen IMAP-Unterordner zustellen lassen, wenn Sie gar kein IMAP-Konto anlegen!');
204) 	return false;
205)       }
206)       $virus = "'folder'";
207)       break;
208)     case 'tag':
209)       $virus = "'tag'";
210)       break;
211)     case 'delete':
212)       $virus = "'delete'";
213)       break;
214)   }
215) 
216)   $account['local'] = mysql_real_escape_string($account['local']);
217)   $account['data'] = mysql_real_escape_string($account['data']);
218)   $account['spamexpire'] = (int) $account['spamexpire'];
219)   $account['virusexpire'] = (int) $account['virusexpire'];
220) 
221)   $query = '';
222)   if ($id == NULL)
223)   {
224)     $query = "INSERT INTO mail.virtual_mail (local, domain, type, data, spamfilter, virusfilter, spamexpire, virusexpire) VALUES ";
225)     $query .= "('{$account['local']}', {$account['domain']}, '{$type}', '{$account['data']}', {$spam}, {$virus}, {$account['spamexpire']}, {$account['virusexpire']});";
226)   }
227)   else
228)   {
229)     $password = ", data='{$account['data']}'";
230)     if ($account['data'] == '')
231)       $password = '';
232)     $query = "UPDATE mail.virtual_mail SET local='{$account['local']}', domain={$account['domain']}, type='{$type}'{$password}, ";
233)     $query .= "spamfilter={$spam}, virusfilter={$virus}, spamexpire={$account['spamexpire']}, virusexpire={$account['virusexpire']} ";
234)     $query .= "WHERE id={$id} LIMIT 1;";
235)   }
236)   db_query($query); 
237) }
238) 
239) 
240) 
bernd Löschen geht jetzt auch

bernd authored 16 years ago

241) function delete_account($id)
242) {
243)   $account = get_account_details($id);
244)   db_query("DELETE FROM mail.virtual_mail WHERE id={$account['id']};");
245) }
246)