Browse code

E-Mail-Modul auf JQuery umgestellt

Bernd Wurst authored on08/02/2014 02:30:24
Showing4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+<?php
2
+/*
3
+This file belongs to the Webinterface of schokokeks.org Hosting
4
+
5
+Written 2008-2013 by schokokeks.org Hosting, namely
6
+  Bernd Wurst <bernd@schokokeks.org>
7
+  Hanno Böck <hanno@schokokeks.org>
8
+
9
+To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
10
+
11
+You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12
+http://creativecommons.org/publicdomain/zero/1.0/
13
+
14
+Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15
+*/
16
+
17
+require_once('vmail.php');
18
+
19
+$email = filter_var($_REQUEST['target'], FILTER_VALIDATE_EMAIL);
20
+$type = forward_type($email);
21
+
22
+header("Content-Type: text/javascript");
23
+echo ' { "target": "'.$email.'", "type": "'.$type.'" } ';
24
+die();
25
+
26
+
0 27
new file mode 100644
... ...
@@ -0,0 +1,186 @@
1
+
2
+  function moreForward(e)
3
+  {
4
+    e.preventDefault();
5
+    last = $('div.vmail-forward:last');
6
+    last_id = parseInt(last.attr('id').match(/\d+/g));
7
+    new_id = ++last_id;
8
+ 
9
+    if (new_id > 50) {
10
+      alert("Jetzt wird's merkwürdig. Bitte nutzen Sie eine Mailingliste wenn Sie so viele Empfänger brauchen!");
11
+      return;
12
+    }
13
+
14
+
15
+    var $clone = last.clone();
16
+    $clone.attr('id',$clone.attr('id').replace(/\d+$/, function(str) { return parseInt(str) + 1; }) ); 
17
+
18
+    // Find all elements in $clone that have an ID, and iterate using each()
19
+    $clone.find('[id]').each(function() { 
20
+      //Perform the same replace as above
21
+      var $th = $(this);
22
+      var newID = $th.attr('id').replace(/\d+$/, function(str) { return parseInt(str) + 1; });
23
+      $th.attr('id', newID);
24
+    });
25
+    // Find all elements in $clone that have a name, and iterate using each()
26
+    $clone.find('[name]').each(function() { 
27
+      //Perform the same replace as above
28
+      var $th = $(this);
29
+      var newName = $th.attr('name').replace(/\d+$/, function(str) { return parseInt(str) + 1; });
30
+      $th.attr('name', newName);
31
+    });
32
+
33
+    $clone.find('input:first-of-type').val('');
34
+    $clone.find('select:first-of-type')
35
+          .find('option:first-of-type').attr('selected', true);
36
+    $clone.find('.warning').text('');
37
+    $clone.find('.warning').hide();
38
+    
39
+    $clone.find('div.delete_forward').click(removeForward);
40
+    $clone.find('input').on("change keyup paste", checkForward);
41
+    
42
+    last.after($clone);
43
+  }
44
+
45
+  function removeForward() 
46
+  {
47
+    div = $(this).closest('div.vmail-forward');
48
+    input = div.find('input:first');
49
+    input.val('');
50
+    select = div.find('select:first');
51
+    select.find('option:first').attr('selected', true);
52
+    if ($('div.vmail-forward').length > 1) {
53
+      div.remove();
54
+    }
55
+  }
56
+
57
+
58
+  function removeUnneededForwards() {
59
+    // Alle <div> nach dem Element mit der ID vmail_forward_1...
60
+    $('div#vmail_forward_1 ~ div').each( function (el) {
61
+      // ... die leere Eingabefelder haben ...
62
+      if ($(this).find('input:first').val() == '') {
63
+        // ... werden gelöscht
64
+        $(this).remove();
65
+      }
66
+      });
67
+  }
68
+
69
+  function clearPassword() {
70
+    var input = document.getElementById('password');
71
+    if (input.value == '**********') {
72
+      input.value = '';
73
+    }
74
+    input.style.color = '#000';
75
+    /* FIXME: Keine Ahnung, warum das notwendig ist. Mit dem tut es was es soll.  */
76
+    input.focus();
77
+  }
78
+
79
+  function refillPassword() {
80
+    var input = document.getElementById('password');
81
+    if (input.value == '') {
82
+      input.value = input.defaultValue;
83
+    }
84
+    if (input.value == '**********') {
85
+      input.style.color = '#aaa';
86
+    }
87
+  }
88
+
89
+
90
+function hideOrShowGroup( ev ) {
91
+  checkbox = ev.target;
92
+  the_id = checkbox.id;
93
+  checkbox = $('#'+the_id)
94
+  div = $('#'+the_id+'_config')
95
+  if (checkbox.is(':checked')) {
96
+    div.show(100);
97
+  } else {
98
+    div.hide(100);
99
+  }
100
+
101
+}
102
+
103
+
104
+function hideUnchecked() {
105
+  $('div.option_group').each( function(index) {
106
+    the_id = this.id.replace('_config', '');
107
+    checkbox = $('#'+the_id)
108
+    div = $('#'+the_id+'_config')
109
+    if (checkbox.is(':checked')) {
110
+      div.show();
111
+    } else {
112
+      div.hide();
113
+    }
114
+  });
115
+}
116
+
117
+
118
+function checkForwardCallback( result ) {
119
+  target = result.target;
120
+  type = result.type;
121
+  element = null;
122
+  $('div.vmail-forward input').each(function () {
123
+    if ($(this).val() == target) {
124
+      element = $(this).closest('div.vmail-forward');
125
+    }
126
+  });
127
+  if (! element) {
128
+    // Der User hat noch weiter getippt
129
+    return;
130
+  }
131
+  if (type == 'critical') {
132
+    element.find('select option[value="delete"]').attr('selected', true);
133
+    element.find('select option[value="none"]').attr('disabled', true);
134
+    element.find('select option[value="tag"]').attr('disabled', true);
135
+    element.find('select option[value="delete"]').attr('disabled', false);
136
+    element.find('.warning').text('Weiterleitungen zu einigen großen E-Mail-Providern dürfen nur mit aktiviertem Spamfilter eingerichtet werden.');
137
+    element.find('.warning').show()
138
+  } else if (type == 'local') {
139
+    element.find('select option[value="none"]').attr('selected', true);
140
+    element.find('select option[value="none"]').attr('disabled', false);
141
+    element.find('select option[value="tag"]').attr('disabled', false);
142
+    element.find('select option[value="delete"]').attr('disabled', false);
143
+    element.find('.warning').hide()
144
+  } else {
145
+    element.find('select option[value="delete"]').attr('selected', true);
146
+    element.find('select option[value="none"]').attr('disabled', false);
147
+    element.find('select option[value="tag"]').attr('disabled', false);
148
+    element.find('select option[value="delete"]').attr('disabled', false);
149
+    element.find('.warning').hide()
150
+  }
151
+  $('#submit').prop('disabled', false);
152
+}
153
+
154
+function checkForward( ) {
155
+  input = $(this);
156
+  val = input.val();
157
+  atpos = val.indexOf('@');
158
+  dot = val.lastIndexOf('.');
159
+  if (atpos < 0 || val.length < atpos + 3 || dot < atpos || dot > val.length - 2) {
160
+    return;
161
+  }
162
+  div = input.closest('div.vmail-forward');
163
+  $('#submit').prop('disabled', true);
164
+  $.getJSON("checkforward?target="+val, checkForwardCallback)
165
+    .fail( function () {
166
+      $('#submit').prop('disabled', false);
167
+      });
168
+}
169
+
170
+
171
+
172
+$(document).ready(function(){
173
+  // Automatisch Sternchen im Passwortfeld eintragen und entfernen
174
+  $('#password').on('blur', refillPassword);
175
+  $('#password').on('focus',clearPassword);    
176
+
177
+  hideUnchecked();
178
+  $('input.option_group').change(hideOrShowGroup);
179
+
180
+  removeUnneededForwards();
181
+  $('div.delete_forward').click(removeForward);
182
+  $('#more_forwards').click(moreForward);
183
+
184
+  $('div.vmail-forward input').on("change keyup paste", checkForward);
185
+});
186
+
... ...
@@ -20,6 +20,9 @@ require_once('inc/icons.php');
20 20
 
21 21
 require_once('vmail.php');
22 22
 
23
+require_once('inc/jquery.php');
24
+javascript();
25
+
23 26
 $section = 'email_vmail';
24 27
 require_role(array(ROLE_SYSTEMUSER, ROLE_VMAIL_ACCOUNT));
25 28
 
... ...
@@ -57,112 +60,6 @@ $is_forward = (count($account['forwards']) > 0);
57 60
 $is_mailbox = ($account['password'] != NULL  ||  $id == 0);
58 61
 $numforwards = max(count($account['forwards']), 1);
59 62
 
60
-output("<script type=\"text/javascript\">
61
-  
62
-  var numForwards = {$numforwards};
63
-  var forwardsCounter = {$numforwards};
64
-
65
-  function moreForward()
66
-  {
67
-    numForwards += 1;
68
-    forwardsCounter += 1;
69
-
70
-    if ( document.getElementById('vmail_forward_' + forwardsCounter) ) {
71
-      document.getElementById('vmail_forward_' + forwardsCounter).style.display = ''
72
-    }
73
-
74
-    P1 = document.createElement('p');
75
-
76
-    TXT1 = document.createTextNode('Weiterleiten an ');
77
-
78
-    INPUT = document.createElement('input');
79
-    INPUT.type = 'text';
80
-    INPUT.name = 'forward_to_' + forwardsCounter;
81
-    INPUT.id = 'forward_to_' + forwardsCounter;
82
-    INPUT.value = '';
83
-
84
-    P1.appendChild(TXT1);
85
-    P1.appendChild(INPUT);
86
-
87
-    P2 = document.createElement('p');
88
-
89
-    TXT2 = document.createTextNode('Spam-Mails an diese Adresse ');
90
-
91
-    SELECT = document.createElement('select');
92
-    SELECT.id = 'spamfilter_action_' + forwardsCounter;
93
-    SELECT.name = 'spamfilter_action_' + forwardsCounter;
94
-
95
-    SELECT.options[0] = new Option('nicht filtern', 'none', 0);
96
-    SELECT.options[1] = new Option('markieren und zustellen', 'tag', 0);
97
-    SELECT.options[2] = new Option('nicht zustellen', 'delete', 1);
98
-
99
-    P2.appendChild(TXT2);
100
-    P2.appendChild(SELECT);
101
-
102
-    DIV = document.createElement('div');
103
-    DIV.className = 'vmail-forward';
104
-    DIV.id = 'vmail_forward_' + forwardsCounter;
105
-
106
-    DELETE = document.getElementById('vmail_forward_1').getElementsByTagName('div')[0].cloneNode(true);
107
-
108
-    DIV.appendChild(DELETE);
109
-    DIV.appendChild(P1);
110
-    DIV.appendChild(P2);
111
-
112
-    parent = document.getElementById('forward_entries');
113
-    parent.appendChild(DIV);
114
-  }
115
-
116
-  function removeForward(elem) 
117
-  {
118
-    div_id = elem.parentNode.parentNode.id;
119
-    div = document.getElementById(div_id);
120
-    input = div.getElementsByTagName('input')[0];
121
-    input.value = '';
122
-    select = div.getElementsByTagName('select')[0];
123
-    select.options[0].selected = 'selected';
124
-    if (numForwards >= 1) {
125
-      numForwards -= 1;
126
-    }
127
-    if (numForwards >= 1) {
128
-      div.style.display = 'none';
129
-      document.getElementById('forward_entries').removeChild(div);
130
-    }
131
-  }
132
-
133
-  function toggleDisplay(checkbox_id, item_id) 
134
-  {
135
-    if (document.getElementById(checkbox_id).checked == true) {
136
-      document.getElementById(item_id).style.display = 'block';
137
-    } else {
138
-      document.getElementById(item_id).style.display = 'none';
139
-    }
140
-  }
141
-
142
-  function clearPassword() {
143
-    var input = document.getElementById('password');
144
-    if (input.value == '**********') {
145
-      input.value = '';
146
-    }
147
-    input.style.color = '#000';
148
-    /* FIXME: Keine Ahnung, warum das notwendig ist. Mit dem und dem Aufruf in 'onclick=' tut es was es soll.  */
149
-    input.focus();
150
-  }
151
-
152
-  function refillPassword() {
153
-    var input = document.getElementById('password');
154
-    if (input.value == '') {
155
-      input.value = input.defaultValue;
156
-    }
157
-    if (input.value == '**********') {
158
-      input.style.color = '#aaa';
159
-    }
160
-  }
161
-
162
-
163
-</script>
164
-");
165
-
166 63
 $form = '';
167 64
 
168 65
 if ($accountlogin) {
... ...
@@ -195,9 +92,9 @@ if ($accountlogin) {
195 92
   } 
196 93
   
197 94
   $form .= "
198
-    <p><input onchange=\"toggleDisplay('mailbox', 'mailbox_options')\" type=\"checkbox\" id=\"mailbox\" name=\"mailbox\" value=\"yes\" ".($is_mailbox ? 'checked="checked" ' : '')." /><label for=\"mailbox\">&#160;<strong>In Mailbox speichern</strong></label></p>
199
-    <div style=\"margin-left: 2em;".($is_mailbox ? '' : ' display: none;')."\" id=\"mailbox_options\">
200
-    <p>Passwort für Abruf:&#160;<input onclick=\"clearPassword()\" onfocus=\"clearPassword()\" onblur=\"refillPassword()\" style=\"color: #aaa;\" type=\"password\" id=\"password\" name=\"password\" value=\"{$password_value}\" />{$password_message}</p>";
95
+    <p><input class=\"option_group\" type=\"checkbox\" id=\"mailbox\" name=\"mailbox\" value=\"yes\" ".($is_mailbox ? 'checked="checked" ' : '')." /><label for=\"mailbox\">&#160;<strong>In Mailbox speichern</strong></label></p>
96
+    <div style=\"margin-left: 2em;\" id=\"mailbox_config\" class=\"option_group\">
97
+    <p>Passwort für Abruf:&#160;<input style=\"color: #aaa;\" type=\"password\" id=\"password\" name=\"password\" value=\"{$password_value}\" />{$password_message}</p>";
201 98
 
202 99
   $form.= "<p class=\"spamfilter_options\">Unerwünschte E-Mails (Spam, Viren) in diesem Postfach ".html_select('spamfilter_action', array("none" => 'nicht filtern', "folder" => 'in Unterordner »Spam« ablegen', "tag" => 'markieren und zustellen', "delete" => 'nicht zustellen (löschen)'), $account['spamfilter'])."</p>";
203 100
 
... ...
@@ -216,9 +113,9 @@ if ($accountlogin) {
216 113
 
217 114
 
218 115
 
219
-$form .= "<p><input onchange=\"toggleDisplay('autoresponder', 'autoresponder_config')\" type=\"checkbox\" id=\"autoresponder\" name=\"autoresponder\" value=\"yes\" ".($is_autoresponder ? 'checked="checked" ' : '')." /><label for=\"autoresponder\">&#160;<strong>Automatische Antwort versenden</strong></label></p>";
116
+$form .= "<p><input class=\"option_group\" type=\"checkbox\" id=\"autoresponder\" name=\"autoresponder\" value=\"yes\" ".($is_autoresponder ? 'checked="checked" ' : '')." /><label for=\"autoresponder\">&#160;<strong>Automatische Antwort versenden</strong></label></p>";
220 117
 
221
-$form .= "<div style=\"margin-left: 2em;".($is_autoresponder ? '' : ' display: none;')."\" id=\"autoresponder_config\">";
118
+$form .= "<div style=\"margin-left: 2em;\" id=\"autoresponder_config\" class=\"option_group\">";
222 119
 
223 120
 $ar = $account['autoresponder'];
224 121
 if (! $ar) {
... ...
@@ -292,36 +189,41 @@ $form .= '</div>';
292 189
 
293 190
 
294 191
 
295
-$form .= "<p><input onchange=\"toggleDisplay('forward', 'forward_config')\" type=\"checkbox\" id=\"forward\" name=\"forward\" value=\"yes\" ".($is_forward ? 'checked="checked" ' : '')." /><label for=\"forward\">&#160;<strong>Weiterleitung an andere E-Mail-Adressen</strong></label></p>";
192
+$form .= "<p><input class=\"option_group\" type=\"checkbox\" id=\"forward\" name=\"forward\" value=\"yes\" ".($is_forward ? 'checked="checked" ' : '')." /><label for=\"forward\">&#160;<strong>Weiterleitung an andere E-Mail-Adressen</strong></label></p>";
296 193
 
297 194
 
298
-$form .= "<div style=\"margin-left: 2em;".($is_forward ? '' : ' display: none;')."\" id=\"forward_config\">";
195
+$form .= "<div style=\"margin-left: 2em;\" id=\"forward_config\" class=\"option_group\">";
299 196
 
300 197
 $form .= '<div id="forward_entries">
301 198
 ';
302 199
 if (! isset($account['forwards'][0])) {
303 200
   $account['forwards'][0] = array('destination' => '', 'spamfilter' => 'delete');
304 201
 }
305
-for ($i = 0 ; $i < $numforwards ; $i++)
202
+while (count($account['forwards']) < 10) {
203
+  // Dummy-Einträge für Leute ohne JavaScript
204
+  $account['forwards'][] = array('destination' => '', 'spamfilter' => 'delete');
205
+}
206
+for ($i = 0 ; $i < max($numforwards,10) ; $i++)
306 207
 {
307 208
   $num = $i+1;
308 209
   $form .= "<div class=\"vmail-forward\" id=\"vmail_forward_{$num}\">
309
-  <div style=\"float: right;\"><a href=\"#\" onclick=\"removeForward(this);\">".icon_delete("Diese Weiterleitung entfernen")."</a></div>
210
+  <div style=\"float: right;\" class=\"delete_forward\">".icon_delete("Diese Weiterleitung entfernen")."</div>
310 211
   <p>Weiterleiten an <input type=\"text\" id=\"forward_to_{$num}\" name=\"forward_to_{$num}\" value=\"{$account['forwards'][$i]['destination']}\" /></p>
311 212
   <p>Spam-Mails an diese Adresse ".html_select('spamfilter_action_'.$num, array("none" => 'nicht filtern', "tag" => 'markieren und zustellen', "delete" => 'nicht zustellen'), $account['forwards'][$i]['spamfilter'])."</p>
213
+  <p class=\"warning\" style=\"display: none;\"></p>
312 214
   <p>Bitte beachten Sie unsere Hinweise zu <a href=\"http://wiki.schokokeks.org/E-Mail/Weiterleitungen\">Weiterleitungen und Spamfiltern</a>.</p>
313 215
   </div>\n";
314 216
 }
315 217
 $form .= '</div>';
316 218
 
317
-$form .= '<p><a href="#" onclick="moreForward();">'.icon_add().' Weiteren Empfänger hinzufügen</a></p>
219
+$form .= '<p><a href="#" id="more_forwards">'.icon_add().' Weiteren Empfänger hinzufügen</a></p>
318 220
 </div>';
319 221
 
320 222
 $target = 'vmail';
321 223
 if ($accountlogin) {
322 224
   $target = '../index/index';
323 225
 }
324
-$form .= '<p><input type="submit" value="Speichern" />&#160;&#160;&#160;&#160;'.internal_link($target, 'Abbrechen').'</p>';
226
+$form .= '<p><input id="submit" type="submit" value="Speichern" />&#160;&#160;&#160;&#160;'.internal_link($target, 'Abbrechen').'</p>';
325 227
 
326 228
 output(html_form('vmail_edit_mailbox', 'save', 'action=edit'.($id != 0 ? '&id='.$id : ''), $form));
327 229
 
... ...
@@ -28,21 +28,21 @@ $forced_spamfilter_domains = array(
28 28
   );
29 29
 
30 30
 
31
-function forward_spamfilter_options($target) {
31
+function forward_type($target) {
32 32
   global $forced_spamfilter_domains;
33 33
   list($l, $d) = explode('@', $target, 2);
34 34
   DEBUG('Weiterleitung an '.$l.' @ '.$d);
35 35
   if (in_array($d, $forced_spamfilter_domains)) {
36 36
     // Domain in der Liste => Spam darf nicht weiter geleitet werden
37
-    return array(array('delete'), 'delete');
37
+    return 'critical';
38 38
   }
39 39
   $result = db_query("SELECT id FROM kundendaten.domains WHERE CONCAT_WS('.', domainname, tld) = ?", array($d));
40 40
   if ($result->rowCount() > 0) {
41 41
     // Lokale Domain
42
-    return array(array('none', 'tag', 'delete'), 'none');
42
+    return 'local';
43 43
   }  
44 44
   // Auswärtige Domain aber keine aus der Liste
45
-  return array(array('none', 'tag', 'delete'), 'delete');
45
+  return 'external';
46 46
 }
47 47
 
48 48