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

bernd authored 16 years ago

236) function delete_account($id)
237) {
238)   $account = get_account_details($id);
239)   db_query("DELETE FROM mail.virtual_mail WHERE id={$account['id']};");
240) }
241)