Initial commit
[misc/hallinta-vmail] / user.php
1 <?php
2
3
4 $buttons = <<<EOC
5 <p style="margin-top: 5px; margin-bottom: 4px; text-align: center;">
6 <button onclick="return passwd_delete()">Passwort löschen</button>
7 <button onclick="return passwd_set()">Passwort setzen</button>
8 </p>
9 EOC;
10
11 $jscode[] = <<<EOC
12 function passwd_delete_callback(data)
13 {
14     grid_update(grid);
15     info('Preis gespeichert');
16 }
17
18 function passwd_delete()
19 {
20     var eid = document.getElementById('edit_id');
21     if (!eid || !eid.value.length) return false;
22
23     var source = document.getElementById('source');
24     if (!source) return false;
25
26     var parms = 'source=' + source.innerHTML + '&callback=delete&';
27     parms += 'id=' + eid.value;
28
29     ajax_request('function', parms, passwd_delete_callback);
30
31     return false;
32 }
33
34 var passwd_popup = false;
35 function passwd_setpw_callback(data)
36 {
37     grid_update(grid);
38     info('Neues Passwort gespeichert');
39 }
40
41 function passwd_setpw()
42 {
43   passwd_popup.closePopup();
44
45   var pw1 = document.getElementById('pw_pass1');
46   var pw2 = document.getElementById('pw_pass2');
47
48   if (!pw1.value.length || !pw2.value.length || pw1.value != pw2.value) {
49     alert("Die Passwörter stimmen nicht überein!");
50     return false;
51   }
52
53   var source = document.getElementById('source');
54   var pw_id = document.getElementById('pw_id');
55   var pw_pass = document.getElementById('pw_pass1');
56
57   var parms = 'source=' + source.innerHTML + '&callback=setpw';
58   parms += '&id=' + pw_id.value;
59   parms += '&passwd=' + pw_pass.value;
60
61   ajax_request('function', parms, passwd_setpw_callback);
62
63   return false;
64 }
65
66 function passwd_set()
67 {
68   var edit_id = document.getElementById('edit_id');
69
70   if (!edit_id.value.length) return false;
71
72   var width = 245;
73   var height = 163;
74
75   if (!passwd_popup) {
76     var options = {hideOnClick: false, canDragFunc: true };
77     passwd_popup = new Rico.Popup(options);
78     passwd_popup.createWindow('<b>Neues Passwort setzen</b>','',height+'px',width+'px');
79     passwd_popup.contentDiv.style.backgroundColor='#e0e0e0';
80     passwd_popup.contentDiv.innerHTML = [
81                                          '<div class="form" style="margin-left: 10px; margin-right: 12px; padding-left: 10px; padding-bottom: 7px;">',
82                                          '<input type="hidden" name="pw_id" id="pw_id">',
83                                          '<label for="pw_email">E-Mail Adresse:</label><br>',
84                                          '<input type="text" name="pw_email" id="pw_email" size="23" readonly>',
85                                          '<div style="height: 5px;"></div>',
86                                          '<label for="pw_pass1">Neues Passwort:</label><br>',
87                                          '<input type="password" name="pw_pass1" id="pw_pass1" size="23">',
88                                          '<div style="height: 5px;"></div>',
89                                          '<label for="pw_pass2">erneut eingeben:</label><br>',
90                                          '<input type="password" name="pw_pass2" id="pw_pass2" size="23">',
91                                          '<div style="height: 5px;"></div>',
92                                          '<button onclick="return passwd_setpw()">Passwort setzen</button>',
93                                          '</div>',
94                                          ].join('');
95   }
96
97   var edit_username = document.getElementById('edit_username');
98   var edit_vmail_domain_id = document.getElementById('edit_vmail_domain_id');
99
100   var pw_id = document.getElementById('pw_id');
101   var pw_email = document.getElementById('pw_email');
102
103   pw_id.value = edit_id.value;
104   pw_email.value = edit_username.value + '@' + edit_vmail_domain_id.options[edit_vmail_domain_id.selectedIndex].innerHTML;
105
106   var x = Math.floor((RicoUtil.windowWidth()-width)/2);
107   var y = Math.floor((RicoUtil.windowHeight()-height)/2);
108   passwd_popup.openPopup(x,y);
109
110   var pw_pass1 = document.getElementById('pw_pass1');
111   pw_pass1.focus();
112
113   return false;
114 }
115 EOC;
116
117 $mask = array(
118               'table' => 'vmail_user',
119               'title' => 'Mailboxen und Weiterleitungen',
120               'join' => array('vmail_domain ON vmail_domain_id = vmail_domain.id'),
121               'list' => array(
122                               'id' => array(
123                                             'name' => 'ID',
124                                             'visible' => false,
125                                             'sql' => 'vmail_user.id',
126                                             ),
127                               'email' => array(
128                                             'name' => 'E-Mail',
129                                             'width' => 330,
130                                             'type' => 'text',
131                                             'sql' => "username || '@' || name",
132                                             'specs' => "filterUI: 't'",
133                                             ),
134                               'forward' => array(
135                                             'name' => 'Forward',
136                                             'width' => 330,
137                                             'specs' => "filterUI: 't'",
138                                             ),
139                               'pw' => array(
140                                             'name' => 'Pass',
141                                             'width' => 40,
142                                             'specs' => "ClassName: 'aligncenter'",
143                                             'control' => "new Rico.TableColumn.checkbox('t', 'f',0,1)",
144                                             'sql' => 'password IS NOT NULL AND length(password) > 0',
145                                             ),
146                               'active' => array(
147                                             'name' => 'on',
148                                             'width' => 40,
149                                             'specs' => "ClassName: 'aligncenter'",
150                                             'control' => "new Rico.TableColumn.checkbox(1, 0,0,1)",
151                                             ),
152                               ),
153               'edit' => array(
154                               'username' => array(
155                                             'name' => 'Usename',
156                                             'type' => 'text',
157                                             'size' => 21,
158                                             'required' => true,
159                                             ),
160                               'vmail_domain_id' => array(
161                                             'name' => 'Domain',
162                                             'type' => 'select',
163                                             'options' => 'SELECT id,name AS text FROM vmail_domain ORDER BY name',
164                                             'option_empty' => '-- select --',
165                                             'required' => true,
166                                             ),
167                               'forward' => array(
168                                             'name' => 'Forward',
169                                             'type' => 'text',
170                                             'size' => 21,
171                                             'null' => true,
172                                             ),
173 /*
174                               'password' => array(
175                                             'name' => 'Passwort',
176                                             'type' => 'passwd',
177                                             'size' => 21,
178                                             'null' => true,
179                                             'func' => 'passwd_encrypt',
180                                             ),
181 */
182                               'active' => array(
183                                             'name' => 'aktiviert',
184                                             'type' => 'boolean',
185                                             ),
186                               'buttons' => array(
187                                             'type' => 'html',
188                                             'code' => $buttons,
189                                             'sql' => false,
190                                             ),
191                               ),
192               'callbacks' => array(
193                                   'delete' => 'cb_delete',
194                                   'setpw' => 'cb_setpw',
195                                   ),
196               );
197
198 function passwd_encrypt($pw)
199 {
200   return md5($pw);
201 }
202
203 function cb_delete()
204 {
205   global $db;
206
207   $sql = sprintf("UPDATE vmail_user SET password = NULL WHERE id = %d", $_POST['id']);
208
209   $sth = $db->query($sql);
210
211   return true;
212 }
213
214 function cb_setpw()
215 {
216   global $db;
217
218   $sql = sprintf("UPDATE vmail_user SET password = '%s' WHERE id = %d",
219                  passwd_encrypt($_POST['passwd']),
220                  $_POST['id']);
221
222   $sth = $db->query($sql);
223
224   return true;
225 }
226
227 ?>