]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/modules/InboundEmail/InboundEmail.js
Release 6.1.5
[Github/sugarcrm.git] / jssource / src_files / modules / InboundEmail / InboundEmail.js
1 /*********************************************************************************
2  * SugarCRM is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36
37 SUGAR.inboundEmail = { };
38
39
40 Rot13 = {
41     map: null,
42
43     convert: function(a) {
44         Rot13.init();
45
46         var s = "";
47         for (i=0; i < a.length; i++) {
48             var b = a.charAt(i);
49             s += ((b>='A' && b<='Z') || (b>='a' && b<='z') ? Rot13.map[b] : b);
50         }
51         return s;
52     },
53
54     init: function() {
55         if (Rot13.map != null)
56             return;
57
58         var map = new Array();
59         var s   = "abcdefghijklmnopqrstuvwxyz";
60
61         for (i=0; i<s.length; i++)
62             map[s.charAt(i)] = s.charAt((i+13)%26);
63         for (i=0; i<s.length; i++)
64             map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
65
66         Rot13.map = map;
67     },
68
69     write: function(a) {
70         return Rot13.convert(a);
71     }
72 }
73
74
75 function getEncryptedPassword(login, password, mailbox) {
76         var words = new Array(login, password, mailbox);
77         for(i=0; i<3; i++) {
78                 word = words[i];
79                 if(word.indexOf('&') > 0) {
80                         fragment1 = word.substr(0, word.indexOf('&'));
81                         fragment2 = word.substr(word.indexOf('&') + 1, word.length);
82
83                         newWord = fragment1 + '::amp::' + fragment2;
84                         words[i] = newWord;
85                         word = newWord; // setting it locally to pass on to next IF
86                         fragment1 = '';
87                         fragment2 = '';
88                 }
89                 if(word.indexOf('+') > 0) {
90                         fragment1 = word.substr(0, word.indexOf('+'));
91                         fragment2 = word.substr(word.indexOf('+') + 1, word.length);
92
93                         newWord = fragment1 + '::plus::' + fragment2;
94                         words[i] = newWord;
95                         word = newWord; // setting it locally to pass on to next IF
96                         fragment1 = '';
97                         fragment2 = '';
98                 }
99                 if(word.indexOf('%') > 0) {
100                         fragment1 = word.substr(0, word.indexOf('%'));
101                         fragment2 = word.substr(word.indexOf('%') + 1, word.length);
102
103                         newWord = fragment1 + '::percent::' + fragment2;
104                         words[i] = newWord;
105                         word = newWord; // setting it locally to pass on to next IF
106                         fragment1 = '';
107                         fragment2 = '';
108                 }
109         } // for
110
111         return words;
112 } // fn
113
114 function ie_test_open_popup_with_submit(module_name, action, pageTarget, width, height, mail_server, protocol, port, login, password, mailbox, ssl, personal, formName)
115 {
116         if (!formName) formName = "testSettingsView";
117         var words = getEncryptedPassword(login, password, mailbox);
118         var isPersonal = (personal) ? 'true' : 'false';
119
120         if (!isDataValid(formName, true)) {
121                 return;
122         }
123         ie_id = document.getElementById(formName).ie_id.value;
124         // launch the popup
125         URL = 'index.php?'
126                 + 'module=' + module_name
127                 + '&to_pdf=1'
128                 + '&action=' + action
129                 + '&target=' + pageTarget
130                 + '&target1=' + pageTarget
131                 + '&server_url=' + mail_server
132                 + '&email_user=' + words[0]
133                 + '&protocol=' + protocol
134                 + '&port=' + port
135                 + '&email_password=' + words[1]
136                 + '&mailbox=' + words[2]
137                 + '&ssl=' + ssl
138                 + '&ie_id=' + ie_id
139                 + '&personal=' + isPersonal;
140
141         var SI = SUGAR.inboundEmail;
142         if (!SI.testDlg) {
143                 SI.testDlg = new YAHOO.widget.SimpleDialog("testSettingsDiv", {
144                 width: width + "px",
145                 draggable: true,
146                 dragOnly: true,
147                 close: true,
148                 constraintoviewport: true,
149                         modal: true,
150                         loadingText: SUGAR.language.get("app_strings", "LBL_EMAIL_LOADING")
151             });
152                 SI.testDlg._updateContent = function (o) {
153                 var w = this.cfg.config.width.value + "px";
154                 this.setBody(o.responseText);
155                 if (this.evalJS)
156                   SUGAR.util.evalScript(o.responseText);
157                 if (!SUGAR.isIE)
158                     this.body.style.width = w
159             }
160         }
161         var title = SUGAR.language.get('Emails', 'LBL_TEST_SETTINGS');
162         if (typeof(title) == "undefined" || title == "undefined")
163            title = SUGAR.language.get('InboundEmail', 'LBL_TEST_SETTINGS');
164         SI.testDlg.setHeader(title);
165         SI.testDlg.setBody(SUGAR.language.get("app_strings", "LBL_EMAIL_LOADING"));
166
167     SI.testDlg.render(document.body);
168         var Connect = YAHOO.util.Connect;
169         if (Connect.url) URL = Connect.url + "&" +  url;
170     Connect.asyncRequest("GET", URL, {success:SI.testDlg._updateContent, failure:SI.testDlg.hide, scope:SI.testDlg});
171     SI.testDlg.show();
172
173 }
174
175 function isDataValid(formName, validateMonitoredFolder) {
176         var formObject = document.getElementById(formName);
177     var errors = new Array();
178     var out = new String();
179
180     if(trim(formObject.server_url.value) == "") {
181         errors.push(SUGAR.language.get('app_strings', 'LBL_EMAIL_ERROR_SERVER'));
182     }
183     if(trim(formObject.email_user.value) == "") {
184         errors.push(SUGAR.language.get('app_strings', 'LBL_EMAIL_ERROR_USER'));
185     }
186     if(trim(formObject.email_password.value) == "" && trim(formObject.ie_id.value) == "") {
187         errors.push(SUGAR.language.get('app_strings', 'LBL_EMAIL_ERROR_PASSWORD'));
188     }
189     if(formObject.protocol.protocol == "") {
190         errors.push(SUGAR.language.get('app_strings', 'LBL_EMAIL_ERROR_PROTOCOL'));
191     }
192     if (formObject.protocol.value == 'imap' && validateMonitoredFolder) {
193         if (trim(formObject.mailbox.value) == "") {
194                 errors.push(SUGAR.language.get('app_strings', 'LBL_EMAIL_ERROR_MONITORED_FOLDER'));
195         } // if
196     }
197     if(formObject.port.value == "") {
198         errors.push(SUGAR.language.get('app_strings', 'LBL_EMAIL_ERROR_PORT'));
199     }
200
201     if(errors.length > 0) {
202         out = SUGAR.language.get('app_strings', 'LBL_EMAIL_ERROR_DESC');
203         for(i=0; i<errors.length; i++) {
204             if(out != "") {
205                 out += "\n";
206             }
207             out += errors[i];
208         }
209
210         alert(out);
211         return false;
212     } else {
213         return true;
214     }
215
216 } // fn
217
218 function getFoldersListForInboundAccount(module_name, action, pageTarget, width, height, mail_server, protocol, port, login, password, mailbox, ssl, personal, searchFieldValue, formName) {
219         if (!formName) formName = "testSettingsView";
220
221         var words = getEncryptedPassword(login, password, mailbox);
222         var isPersonal = (personal) ? 'true' : 'false';
223
224         // launch the popup
225         URL = 'index.php?'
226         + 'module=' + module_name
227         + '&to_pdf=1'
228         + '&action=' + action
229         + '&target=' + pageTarget
230         + '&target1=' + pageTarget
231         + '&server_url=' + mail_server
232         + '&email_user=' + words[0]
233         + '&protocol=' + protocol
234         + '&port=' + port
235         + '&email_password=' + words[1]
236         + '&mailbox=' + words[2]
237         + '&ssl=' + ssl
238         + '&personal=' + isPersonal
239                 + '&searchField='+ searchFieldValue;
240
241         var SI = SUGAR.inboundEmail;
242     if (!SI.listDlg) {
243         SI.listDlg = new YAHOO.widget.SimpleDialog("selectFoldersDiv", {
244             width: width + "px",
245             draggable: true,
246             dragOnly: true,
247             close: true,
248             constraintoviewport: true,
249             modal: true,
250             loadingText: SUGAR.language.get("app_strings", "LBL_EMAIL_LOADING")
251         });
252         SI.listDlg._updateContent = function (o) {
253             var w = this.cfg.config.width.value + "px";
254             this.setBody(o.responseText);
255             SUGAR.util.evalScript(o.responseText);
256             if (!SUGAR.isIE)
257                 this.body.style.width = w
258         }
259     }
260     SI.listDlg.setHeader(SUGAR.language.get("app_strings", "LBL_EMAIL_LOADING"));
261     SI.listDlg.setBody('');
262
263     SI.listDlg.render(document.body);
264     var Connect = YAHOO.util.Connect;
265     if (Connect.url) URL = Connect.url + "&" +  url;
266     Connect.asyncRequest("GET", URL, {success:SI.listDlg._updateContent, failure:SI.listDlg.hide, scope:SI.listDlg});
267     SI.listDlg.show();
268
269 } // fn
270
271 function setPortDefault() {
272         var prot        = document.getElementById('protocol');
273         var ssl         = document.getElementById('ssl');
274         var port        = document.getElementById('port');
275         var stdPorts= new Array("110", "143", "993", "995");
276         var stdBool     = new Boolean(false);
277
278         if(port.value == '') {
279                 stdBool.value = true;
280         } else {
281                 for(i=0; i<stdPorts.length; i++) {
282                         if(stdPorts[i] == port.value) {
283                                 stdBool.value = true;
284                         }
285                 }
286         }
287
288         if(stdBool.value == true) {
289                 if(prot.value == 'imap' && ssl.checked == false) { // IMAP
290                         port.value = "143";
291                 } else if(prot.value == 'imap' && ssl.checked == true) { // IMAP-SSL
292                         port.value = '993';
293                 } else if(prot.value == 'pop3' && ssl.checked == false) { // POP3
294                         port.value = '110';
295                 } else if(prot.value == 'pop3' && ssl.checked == true) { // POP3-SSL
296                         port.value = '995';
297                 }
298         }
299 }
300
301 function toggle_monitored_folder(field) {
302
303         var field1=document.getElementById('protocol');
304         //var target=document.getElementById('pop3_warn');
305         //var mark_read = document.getElementById('mark_read');
306         var mailbox = document.getElementById('mailbox');
307         //var inbox = document.getElementById('inbox');
308         var label_inbox = document.getElementById('label_inbox');
309         var subscribeFolderButton = document.getElementById('subscribeFolderButton');
310         var trashFolderRow = document.getElementById('trashFolderRow');
311         var trashFolderRow1 = document.getElementById('trashFolderRow1');
312         var sentFolderRow = document.getElementById('sentFolderRow');
313
314         if (field1.value == 'imap') {
315                 //target.style.display="none";
316                 mailbox.disabled=false;
317         // This is not supported in IE
318         try {
319           mailbox.style.display = '';
320                   trashFolderRow.style.display = '';
321                   sentFolderRow.style.display = '';
322                   trashFolderRow1.style.display = '';
323                   //mailbox.type='text';
324           subscribeFolderButton.style.display = '';
325         } catch(e) {};
326                 //inbox.style.display='';
327                 label_inbox.style.display='';
328         }
329         else {
330                 //target.style.display="";
331                 mailbox.value = "INBOX";
332         mailbox.disabled=false; // cannot disable, else the value is not passed
333         // This is not supported in IE
334         try {
335                   mailbox.style.display = "none";
336           trashFolderRow.style.display = "none";
337                   sentFolderRow.style.display = "none";
338                   trashFolderRow1.style.display = "none";
339           subscribeFolderButton.style.display = "none";
340
341                   //mailbox.type='hidden';
342         } catch(e) {};
343
344                 //inbox.style.display = "";
345                 label_inbox.style.display = "none";
346         }
347 }