Merge branch 'master' of ssh://luonnotar/var/git/misc/hallinta-vmail
[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.value = '';
112   var pw_pass1 = document.getElementById('pw_pass2');
113   pw_pass2.value = '';
114   pw_pass1.focus();
115
116   return false;
117 }
118 EOC;
119
120 $mask = array(
121               'table' => 'vmail_user',
122               'title' => 'Mailboxen und Weiterleitungen',
123               'join' => array('vmail_domain ON vmail_domain_id = vmail_domain.id'),
124               'list' => array(
125                               'id' => array(
126                                             'name' => 'ID',
127                                             'visible' => false,
128                                             'sql' => 'vmail_user.id',
129                                             ),
130                               'email' => array(
131                                             'name' => 'E-Mail',
132                                             'width' => 330,
133                                             'type' => 'text',
134                                             'sql' => "username || '@' || name",
135                                             'specs' => "filterUI: 't'",
136                                             ),
137                               'forward' => array(
138                                             'name' => 'Forward',
139                                             'width' => 330,
140                                             'specs' => "filterUI: 't'",
141                                             ),
142                               'pw' => array(
143                                             'name' => 'Pass',
144                                             'width' => 40,
145                                             'specs' => "ClassName: 'aligncenter'",
146                                             'control' => "new Rico.TableColumn.checkbox('t', 'f',0,1)",
147                                             'sql' => 'password IS NOT NULL AND length(password) > 0',
148                                             ),
149                               'active' => array(
150                                             'name' => 'on',
151                                             'width' => 40,
152                                             'specs' => "ClassName: 'aligncenter'",
153                                             'control' => "new Rico.TableColumn.checkbox(1, 0,0,1)",
154                                             ),
155                               ),
156               'edit' => array(
157                               'username' => array(
158                                             'name' => 'Usename',
159                                             'type' => 'text',
160                                             'size' => 21,
161                                             'required' => true,
162                                             ),
163                               'vmail_domain_id' => array(
164                                             'name' => 'Domain',
165                                             'type' => 'select',
166                                             'options' => 'SELECT id,name AS text FROM vmail_domain ORDER BY name',
167                                             'option_empty' => '-- select --',
168                                             'required' => true,
169                                             ),
170                               'forward' => array(
171                                             'name' => 'Forward',
172                                             'type' => 'text',
173                                             'size' => 21,
174                                             'null' => true,
175                                             ),
176 /*
177                               'password' => array(
178                                             'name' => 'Passwort',
179                                             'type' => 'passwd',
180                                             'size' => 21,
181                                             'null' => true,
182                                             'func' => 'passwd_encrypt',
183                                             ),
184 */
185                               'active' => array(
186                                             'name' => 'aktiviert',
187                                             'type' => 'boolean',
188                                             ),
189                               'buttons' => array(
190                                             'type' => 'html',
191                                             'code' => $buttons,
192                                             'sql' => false,
193                                             ),
194                               ),
195               'callbacks' => array(
196                                   'delete' => 'cb_delete',
197                                   'setpw' => 'cb_setpw',
198                                   ),
199               );
200
201 function passwd_encrypt($pw)
202 {
203   return md5($pw);
204 }
205
206 function cb_delete()
207 {
208   global $db;
209
210   $sql = sprintf("UPDATE vmail_user SET password = NULL WHERE id = %d", $_POST['id']);
211
212   $sth = $db->query($sql);
213
214   return true;
215 }
216
217 function cb_setpw()
218 {
219   global $db;
220
221   $sql = sprintf("UPDATE vmail_user SET password = '%s' WHERE id = %d",
222                  passwd_encrypt($_POST['passwd']),
223                  $_POST['id']);
224
225   $sth = $db->query($sql);
226
227   return true;
228 }
229
230 ?>