1 | /** |
---|
2 | * @author diogenes |
---|
3 | */ |
---|
4 | |
---|
5 | function local_messages() { |
---|
6 | this.dbGears = null; |
---|
7 | this.localServer = null; |
---|
8 | this.store = null; |
---|
9 | this.filterSerch = ""; |
---|
10 | } |
---|
11 | |
---|
12 | function charOrdA (a, b){ |
---|
13 | a = a[0].toLowerCase(); |
---|
14 | b = b[0].toLowerCase(); |
---|
15 | if (a>b) return 1; |
---|
16 | if (a <b) return -1; |
---|
17 | return 0; |
---|
18 | } |
---|
19 | function charOrdD(a, b){ |
---|
20 | a = a[0].toLowerCase(); |
---|
21 | b = b[0].toLowerCase(); |
---|
22 | if (a<b) return 1; |
---|
23 | if (a>b) return -1; |
---|
24 | return 0; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | function HeaderFlags() |
---|
29 | { |
---|
30 | this.Answered = 0; |
---|
31 | //this.Draft = 0; |
---|
32 | this.Flagged = 0; |
---|
33 | this.Recent = 0; |
---|
34 | } |
---|
35 | |
---|
36 | HeaderFlags.prototype.getAnswered = function() |
---|
37 | { |
---|
38 | return this.Answered; |
---|
39 | } |
---|
40 | |
---|
41 | HeaderFlags.prototype.setAnswered = function(answered) |
---|
42 | { |
---|
43 | this.Answered = answered; |
---|
44 | } |
---|
45 | |
---|
46 | //HeaderFlags.prototype.getDraft = function() |
---|
47 | //{ |
---|
48 | // return this.Draft; |
---|
49 | //} |
---|
50 | |
---|
51 | HeaderFlags.prototype.setDraft = function(draft) |
---|
52 | { |
---|
53 | this.Draft = draft; |
---|
54 | } |
---|
55 | |
---|
56 | HeaderFlags.prototype.getFlagged = function() |
---|
57 | { |
---|
58 | return this.Flagged; |
---|
59 | } |
---|
60 | |
---|
61 | HeaderFlags.prototype.setFlagged = function(flagged) |
---|
62 | { |
---|
63 | this.Flagged = flagged; |
---|
64 | } |
---|
65 | |
---|
66 | HeaderFlags.prototype.getRecent = function() |
---|
67 | { |
---|
68 | return this.Recent; |
---|
69 | } |
---|
70 | |
---|
71 | HeaderFlags.prototype.setRecent = function(recent) |
---|
72 | { |
---|
73 | this.Recent = recent; |
---|
74 | } |
---|
75 | |
---|
76 | function FlagsParser(headerObj) |
---|
77 | { |
---|
78 | this.Header = headerObj; |
---|
79 | } |
---|
80 | |
---|
81 | FlagsParser.prototype.parse = function() |
---|
82 | { |
---|
83 | var tmp = null; |
---|
84 | if (typeof this.Header == 'string') |
---|
85 | { |
---|
86 | tmp = connector.unserialize(this.Header); |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | tmp = this.Header; |
---|
91 | } |
---|
92 | |
---|
93 | flags = new HeaderFlags(); |
---|
94 | |
---|
95 | if (tmp.Answered && tmp.Answered.match(/^A$/)) |
---|
96 | { |
---|
97 | flags.setAnswered(1); |
---|
98 | //if (tmp.Draft && tmp.Draft.match(/^X$/)) |
---|
99 | //{ |
---|
100 | // flags.setDraft(1); |
---|
101 | //} |
---|
102 | } |
---|
103 | |
---|
104 | if (tmp.Flagged && tmp.Flagged.match(/^F$/)){ |
---|
105 | flags.setFlagged(1); |
---|
106 | } |
---|
107 | |
---|
108 | if (tmp.Forwarded && tmp.Forwarded.match(/^F$/)){ |
---|
109 | flags.setAnswered(1); |
---|
110 | //flags.setDraft(1); |
---|
111 | } |
---|
112 | |
---|
113 | if (tmp.Recent && tmp.Recent.match(/^R$/)){ |
---|
114 | flags.setRecent(1); |
---|
115 | } |
---|
116 | |
---|
117 | return flags; |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | local_messages.prototype.installGears = function (){ |
---|
122 | temp = confirm(get_lang("To use local messages you have to install google gears. Would you like to install it now?")); |
---|
123 | if (temp && typeof(preferences.googlegears_url) != 'undefined'){ |
---|
124 | if (is_ie) |
---|
125 | location.href = preferences.googlegears_url + "/gears.exe"; |
---|
126 | else |
---|
127 | location.href = preferences.googlegears_url + "/gears.xpi"; |
---|
128 | return false; |
---|
129 | } |
---|
130 | if (temp) { |
---|
131 | location.href = "http://gears.google.com/?action=install&message="+ |
---|
132 | get_lang("To use local messages, install Google Gears")+"&return=" + document.location.href; |
---|
133 | } |
---|
134 | else return false; |
---|
135 | } |
---|
136 | |
---|
137 | local_messages.prototype.create_objects = function() { |
---|
138 | if(window.google){ |
---|
139 | if (this.dbGears == null) |
---|
140 | this.dbGears = google.gears.factory.create('beta.database'); |
---|
141 | if(this.localServer == null) |
---|
142 | this.localServer = google.gears.factory.create('beta.localserver'); |
---|
143 | if(this.store == null) |
---|
144 | this.store = this.localServer.createStore('test-store'); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | local_messages.prototype.init_local_messages = function(){ //starts only database operations |
---|
149 | |
---|
150 | if(this.dbGears==null || this.localServer==null || this.store == null) |
---|
151 | this.create_objects(); |
---|
152 | |
---|
153 | var db_in_other_use = true; |
---|
154 | var start_trying = new Date().getTime(); |
---|
155 | while (db_in_other_use) { |
---|
156 | try { |
---|
157 | this.dbGears.open('database-test'); |
---|
158 | db_in_other_use = false; |
---|
159 | } |
---|
160 | catch (ex) { |
---|
161 | if(new Date().getTime()-start_trying>10000) { //too much time trying, throw an exception |
---|
162 | throw ex; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | // this.dbGears.open('database-test'); |
---|
168 | this.dbGears.execute('create table if not exists folder (folder text,uid_usuario int,unique(folder,uid_usuario))'); |
---|
169 | this.dbGears.execute('create table if not exists mail' + |
---|
170 | ' (mail blob,original_id int,original_folder text,header blob,timestamp int,uid_usuario int,unseen int,id_folder int,' + |
---|
171 | ' ffrom text, subject text, fto text, cc text, body text, size int,unique (original_id,original_folder,uid_usuario,id_folder))'); |
---|
172 | this.dbGears.execute('create table if not exists anexo' + |
---|
173 | ' (id_mail int,nome_anexo text,url text,pid int)'); |
---|
174 | this.dbGears.execute('create table if not exists folders_sync' + |
---|
175 | ' (id_folder text,folder_name text,uid_usuario int)'); |
---|
176 | this.dbGears.execute('create table if not exists msgs_to_remove (id_msg int,folder text,uid_usuario int)'); |
---|
177 | this.dbGears.execute('create index if not exists idx_user3 on mail (id_folder,uid_usuario,timestamp)'); |
---|
178 | this.dbGears.execute('create INDEX if not exists idx_folder ON folder(uid_usuario,folder)'); |
---|
179 | |
---|
180 | //some people that used old version of local messages could not have the size column. If it's the first version |
---|
181 | //with local messages you're using in expresso, this part of code can be removed |
---|
182 | try { |
---|
183 | this.dbGears.execute('alter table mail add column size int'); |
---|
184 | }catch(Exception) { |
---|
185 | |
---|
186 | } |
---|
187 | var rs = this.dbGears.execute('select rowid,header from mail where size is null'); |
---|
188 | while(rs.isValidRow()) { |
---|
189 | var temp = connector.unserialize(rs.field(1)); |
---|
190 | |
---|
191 | this.dbGears.execute('update mail set size='+temp.Size+' where rowid='+rs.field(0)); |
---|
192 | rs.next(); |
---|
193 | } |
---|
194 | //end of temporary code |
---|
195 | |
---|
196 | try { |
---|
197 | this.dbGears.execute('begin transaction'); |
---|
198 | this.dbGears.execute('alter table mail add column answered int'); |
---|
199 | //this.dbGears.execute('alter table mail add column draft int'); |
---|
200 | this.dbGears.execute('alter table mail add column flagged int'); |
---|
201 | this.dbGears.execute('alter table mail add column recent int'); |
---|
202 | //this.dbGears.execute('commit transaction'); |
---|
203 | //transaction_ended = true; |
---|
204 | //if (transaction_ended){ |
---|
205 | rs = null; |
---|
206 | rs = this.dbGears.execute('select rowid,header from mail'); |
---|
207 | |
---|
208 | // Popular os valores das novas colunas. |
---|
209 | var tmp = null; |
---|
210 | //this.dbGears.execute('begin transaction'); |
---|
211 | while(rs.isValidRow()) { |
---|
212 | //tmp = connector.unserialize(rs.field(1)); |
---|
213 | parser = new FlagsParser(rs.field(1)); |
---|
214 | flags = parser.parse(); |
---|
215 | |
---|
216 | this.dbGears.execute('update mail set answered='+flags.getAnswered()+ |
---|
217 | ',flagged='+flags.getFlagged()+',recent='+flags.getRecent()+ |
---|
218 | //',draft='+flags.getDraft()+' where rowid='+rs.field(0)); |
---|
219 | ' where rowid='+rs.field(0)); |
---|
220 | |
---|
221 | rs.next(); |
---|
222 | } |
---|
223 | this.dbGears.execute('commit transaction'); |
---|
224 | |
---|
225 | //tmp = null; |
---|
226 | |
---|
227 | }catch(Exception) { |
---|
228 | this.dbGears.execute('rollback transaction'); |
---|
229 | } |
---|
230 | |
---|
231 | } |
---|
232 | |
---|
233 | local_messages.prototype.drop_tables = function() { |
---|
234 | this.init_local_messages(); |
---|
235 | var rs = this.dbGears.execute('select url from anexo'); |
---|
236 | while(rs.isValidRow()) { |
---|
237 | this.store.remove(rs.field(0)); |
---|
238 | rs.next(); |
---|
239 | } |
---|
240 | this.dbGears.execute('drop table folder'); |
---|
241 | this.dbGears.execute('drop table mail'); |
---|
242 | this.dbGears.execute('drop table anexo'); |
---|
243 | this.finalize(); |
---|
244 | } |
---|
245 | |
---|
246 | local_messages.prototype.insert_mail = function(msg_info,msg_header,anexos,folder) { |
---|
247 | try { |
---|
248 | this.init_local_messages(); |
---|
249 | var unseen = 0; |
---|
250 | var login = msg_info.login; |
---|
251 | var original_id = msg_info.msg_number; |
---|
252 | var original_folder = msg_info.msg_folder; |
---|
253 | |
---|
254 | //This fields needs to be separeted to search. |
---|
255 | var from = connector.serialize(msg_info.from); |
---|
256 | var subject = msg_info.subject; |
---|
257 | var body = msg_info.body; |
---|
258 | var to = connector.serialize(msg_info.toaddress2); |
---|
259 | var cc = connector.serialize(msg_info.cc); |
---|
260 | var size = msg_header.Size; |
---|
261 | |
---|
262 | //do not duplicate this information |
---|
263 | msg_info.from = null; |
---|
264 | msg_info.subject = null; |
---|
265 | msg_info.body = null; |
---|
266 | msg_info.to = null; |
---|
267 | msg_info.cc = null; |
---|
268 | msg_header.Size=null; |
---|
269 | //If the mail was archieved in the same date the user received it, the date cames with the time. |
---|
270 | //here I solved it |
---|
271 | if(msg_header.udate.indexOf(":")!=-1) { |
---|
272 | msg_header.udate = msg_header.aux_date; |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * The importance attribute can be empty, and javascript consider as null causing nullpointer. |
---|
277 | */ |
---|
278 | if((msg_header.Importance == null) || (msg_header.Importance == "")) |
---|
279 | msg_header.Importance = "Normal"; |
---|
280 | |
---|
281 | msg_header.aux_date = null; |
---|
282 | |
---|
283 | var mail = connector.serialize(msg_info); |
---|
284 | var header = connector.serialize(msg_header); |
---|
285 | |
---|
286 | var timestamp = msg_info.timestamp; |
---|
287 | var id_folder; |
---|
288 | |
---|
289 | if((folder==null) || (folder=="local_root")) |
---|
290 | folder = "Inbox"; |
---|
291 | else |
---|
292 | folder = folder.substr(6);//take off the word "local_" |
---|
293 | |
---|
294 | var rs = this.dbGears.execute("select rowid from folder where folder=? and uid_usuario=?",[folder,account_id]); |
---|
295 | if(rs.isValidRow()) |
---|
296 | id_folder=rs.field(0); |
---|
297 | else { |
---|
298 | this.dbGears.execute("insert into folder (folder,uid_usuario) values (?,?)",["Inbox",account_id]); |
---|
299 | id_folder = this.dbGears.lastInsertRowId; |
---|
300 | } |
---|
301 | |
---|
302 | if(msg_info.Unseen=="U") |
---|
303 | unseen = 1; |
---|
304 | |
---|
305 | //parse header |
---|
306 | parser = new FlagsParser(msg_header); |
---|
307 | flags = parser.parse(); |
---|
308 | |
---|
309 | //insere o e-mail |
---|
310 | //this.dbGears.execute("insert into mail (mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[mail,original_id,original_folder,header,timestamp,login,unseen,id_folder,from,subject,to,cc,body,size]); |
---|
311 | this.dbGears.execute("insert into mail (mail,original_id,original_folder,header,timestamp,uid_usuario,unseen,id_folder,ffrom,subject,fto,cc,body,size,answered,flagged,recent) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[mail,original_id,original_folder,header,timestamp,login,unseen,id_folder,from,subject,to,cc,body,size,flags.getAnswered(),flags.getFlagged(),flags.getRecent()]); |
---|
312 | var call_back = function() { |
---|
313 | } |
---|
314 | this.store.capture(msg_info.url_export_file,call_back); |
---|
315 | var id_mail = this.dbGears.lastInsertRowId; |
---|
316 | |
---|
317 | this.insert_attachments(id_mail,anexos); |
---|
318 | this.finalize(); |
---|
319 | return true; |
---|
320 | } catch (error) { |
---|
321 | this.finalize(); |
---|
322 | return false; |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * check if ID is no from main tab, if it's from main returns false, else |
---|
330 | * returns an array with all string in position 0, the mail id in position 1 |
---|
331 | * and the part of string relative to tab in position 2 |
---|
332 | * @param {Object} id_mail |
---|
333 | */ |
---|
334 | local_messages.prototype.parse_id_mail = function(id_mail) { |
---|
335 | if (this.isInt(id_mail)) |
---|
336 | return false; |
---|
337 | |
---|
338 | var matches = id_mail.match(/(.+)(_[a-zA-Z0-9]+)/); |
---|
339 | return matches; |
---|
340 | } |
---|
341 | |
---|
342 | local_messages.prototype.isInt = function(x) { |
---|
343 | var y=parseInt(x); |
---|
344 | if (isNaN(y)) return false; |
---|
345 | return x==y && x.toString()==y.toString(); |
---|
346 | } |
---|
347 | |
---|
348 | local_messages.prototype.get_local_mail = function(id_mail) { |
---|
349 | this.init_local_messages(); |
---|
350 | |
---|
351 | var plus_id = ''; |
---|
352 | var matches = ''; |
---|
353 | if(matches = this.parse_id_mail(id_mail)) { //Mails coming from other tab. |
---|
354 | id_mail = matches[1]; |
---|
355 | plus_id = matches[2]; |
---|
356 | } |
---|
357 | |
---|
358 | var rs = this.dbGears.execute("select mail.rowid,mail.mail,mail.ffrom,mail.subject,mail.body,mail.fto,mail.cc,folder.folder,mail.original_id from mail inner join folder on mail.id_folder=folder.rowid where mail.rowid="+id_mail); |
---|
359 | var retorno = null; |
---|
360 | if(rs.isValidRow()) { |
---|
361 | retorno = rs.field(1); |
---|
362 | } |
---|
363 | retorno = connector.unserialize(retorno); |
---|
364 | |
---|
365 | //alert('tipo retorno.source: ' + typeof(retorno.source)); |
---|
366 | |
---|
367 | if (typeof(retorno.source) == 'string') |
---|
368 | { |
---|
369 | retorno.msg_number=rs.field(0)+plus_id; |
---|
370 | retorno.original_ID=rs.field(8); |
---|
371 | retorno.msg_folder=rs.field(7); |
---|
372 | |
---|
373 | //alert('tipo retorno: '+typeof(retorno)) |
---|
374 | //show_msg(retorno); |
---|
375 | } |
---|
376 | else |
---|
377 | { |
---|
378 | retorno['from'] = connector.unserialize(rs.field(2)); |
---|
379 | retorno['subject'] = rs.field(3); |
---|
380 | retorno['body'] = rs.field(4); |
---|
381 | //Codigo que as imagens embutidas em emails (com multipart/related ou multipart/mixed) sejam corretamente mostradas em emails arquivados. Os links do |
---|
382 | //tipo "./inc/show_embedded_attach.php?msg_folder=[folder]&msg_num=[msg_num]&msg_part=[part]" |
---|
383 | //são substituidos pelos links dos anexos capturados pelo gears. |
---|
384 | |
---|
385 | var thumbs= retorno.thumbs; |
---|
386 | var anexos= retorno.array_attach; |
---|
387 | for (i in anexos) |
---|
388 | { |
---|
389 | if(anexos[i]['url'] && anexos[i]['url'].match(/((jpg)|(jpeg)|(png)|(gif)|(bmp))/gi)) |
---|
390 | { |
---|
391 | var er_imagens = new RegExp("\\.\\/inc\\/show_embedded_attach.php\\?msg_folder=[\\w/]+\\&msg_num=[0-9]+\\&msg_part="+anexos[i]['pid']); |
---|
392 | var Result_imagens = er_imagens.exec(retorno['body']); |
---|
393 | retorno['body'] = retorno['body'].replace(Result_imagens,anexos[i]['url']); |
---|
394 | if(thumbs && thumbs[i]){ |
---|
395 | er_imagens = new RegExp("\\.\\/inc\\/show_thumbs.php\\?file_type=image\\/[\\w]+\\&msg_num=[0-9]+\\&msg_folder=[\\w/%]+\\&msg_part="+anexos[i]['pid']); |
---|
396 | Result_imagens = er_imagens.exec(thumbs[i]); |
---|
397 | thumbs[i] = thumbs[i].replace(Result_imagens,"'"+anexos[i]['url']+"'"); |
---|
398 | er_imagens = new RegExp("\\.\\/inc\\/show_img.php\\?msg_num=[0-9]+\\&msg_folder=[\\w/%]+\\&msg_part="+anexos[i]['pid']); |
---|
399 | Result_imagens = er_imagens.exec(thumbs[i]); |
---|
400 | thumbs[i] = thumbs[i].replace(Result_imagens,anexos[i]['url']); |
---|
401 | thumbs[i] = thumbs[i].replace(/<IMG/i,'<img width="120"'); |
---|
402 | } |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | retorno['to'] = connector.unserialize(rs.field(5)); |
---|
407 | retorno['cc'] = connector.unserialize(rs.field(6)); |
---|
408 | |
---|
409 | retorno['local_message'] = true; |
---|
410 | retorno['msg_folder'] = "local_"+rs.field(7); //Now it's a local folder |
---|
411 | retorno['msg_number'] = rs.field(0)+plus_id; //the message number is the rowid |
---|
412 | |
---|
413 | } |
---|
414 | |
---|
415 | rs.close(); |
---|
416 | this.finalize(); |
---|
417 | return retorno; |
---|
418 | } |
---|
419 | |
---|
420 | local_messages.prototype.insert_attachments = function(id_msg,anexos) { |
---|
421 | //insert_mail already close and open gears. |
---|
422 | for (var i = 0; i < anexos.length; i++) { |
---|
423 | this.dbGears.execute("insert into anexo (id_mail,nome_anexo,url,pid) values (?,?,?,?)", [id_msg, anexos[i]['name'],anexos[i]['url'],anexos[i]['pid']]); |
---|
424 | this.capt_url(anexos[i]['url']); |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | local_messages.prototype.capt_url = function (url) { |
---|
429 | //insert_mail already close and open gears. |
---|
430 | var call_back = function(url,success,captureId) { |
---|
431 | //alert("Capturado: " + url); |
---|
432 | } |
---|
433 | //alert(url); |
---|
434 | this.store.capture(url,call_back); |
---|
435 | } |
---|
436 | |
---|
437 | local_messages.prototype.strip_tags = function (str) { |
---|
438 | return str.replace(/<\/?[^>]+>/gi, ''); |
---|
439 | } |
---|
440 | |
---|
441 | local_messages.prototype.get_local_range_msgs = function(folder,msg_range_begin,emails_per_page,sort,sort_reverse,search,preview_msg_subject,preview_msg_tip) { |
---|
442 | |
---|
443 | this.init_local_messages(); |
---|
444 | var retorno = new Array(); |
---|
445 | msg_range_begin--; |
---|
446 | |
---|
447 | mail_filter = " "; |
---|
448 | if(search=="FLAGGED") { |
---|
449 | mail_filter = "and (header like '%\"Flagged\";s:1:\"F%' or header like '%\"Importance\";s:5:\"High%') "; |
---|
450 | } |
---|
451 | if(search=="UNSEEN") { |
---|
452 | mail_filter = "and unseen = 1 "; |
---|
453 | } |
---|
454 | if(search=="SEEN") { |
---|
455 | mail_filter = "and unseen = 0 "; |
---|
456 | } |
---|
457 | if (search=="ANSWERED") { |
---|
458 | mail_filter = "and header like '%\"Answered\";s:1:\"A%' "; |
---|
459 | } |
---|
460 | |
---|
461 | sql = 'select mail.rowid as rowid,mail.header as header,mail.size as size,' + |
---|
462 | 'mail.timestamp as timestamp,mail.unseen as unseen,mail.body as body, mail.mail as mail, ' + |
---|
463 | 'case when lower(mail.ffrom) like ? then ' + |
---|
464 | 'case when ltrim(ltrim(substr(UPPER(fto),7,length(fto)),\':\'),\'"\') like \'5:%\' then ' + |
---|
465 | 'substr(ltrim(ltrim(substr(UPPER(fto),7,length(fto)),\':\'),\'"\'),17) ' + |
---|
466 | 'else ' + |
---|
467 | 'ltrim(ltrim(substr(UPPER(fto),7,length(fto)),\':\'),\'"\') ' + |
---|
468 | 'end ' + |
---|
469 | 'else ' + |
---|
470 | 'case when ltrim(ltrim(substr(UPPER(ffrom),21,length(ffrom)),\':\'),\'"\') like \'5:%\' then ' + |
---|
471 | 'substr(ltrim(ltrim(substr(UPPER(ffrom),21,length(ffrom)),\':\'),\'"\'),17) ' + |
---|
472 | 'else ' + |
---|
473 | 'ltrim(ltrim(substr(UPPER(ffrom),21,length(ffrom)),\':\'),\'"\') ' + |
---|
474 | 'end ' + |
---|
475 | 'end as order_from,mail.subject from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=? ' + |
---|
476 | mail_filter + ' order by '; |
---|
477 | |
---|
478 | if(sort == 'SORTFROM') { |
---|
479 | sql += 'order_from '; |
---|
480 | } |
---|
481 | if(sort == 'SORTARRIVAL') { |
---|
482 | sql += 'timestamp '; |
---|
483 | } |
---|
484 | if(sort == 'SORTSIZE') { |
---|
485 | sql += 'size '; |
---|
486 | } |
---|
487 | if(sort == 'SORTSUBJECT') { |
---|
488 | sql += 'UPPER(subject) '; |
---|
489 | } |
---|
490 | |
---|
491 | |
---|
492 | sql+= sort_reverse==0?"ASC ":"DESC "; |
---|
493 | sql +='limit ?,? '; |
---|
494 | |
---|
495 | |
---|
496 | var rs = this.dbGears.execute(sql,['%'+Element("user_email").value+'%',account_id,folder,msg_range_begin,emails_per_page]); |
---|
497 | var cont = 0; |
---|
498 | |
---|
499 | var rs3 = this.dbGears.execute('select count(*) from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario=? and folder.folder=?'+mail_filter,[account_id,folder]); |
---|
500 | |
---|
501 | while (rs.isValidRow()) { |
---|
502 | //var email = rs.field(1); |
---|
503 | var head = rs.field(1); |
---|
504 | var codigoMail = rs.field(0); |
---|
505 | var mail = rs.field(6); |
---|
506 | |
---|
507 | var msg_body = rs.field(5);//recebe o conteudo da coluna "body" do banco de dados; |
---|
508 | |
---|
509 | var rs2 = this.dbGears.execute('select count(*) from anexo where id_mail = '+codigoMail); |
---|
510 | var head_unserialized = connector.unserialize(head); |
---|
511 | var mail_unserialized = connector.unserialize(mail); |
---|
512 | |
---|
513 | head_unserialized.Size = rs.field(2); |
---|
514 | if(rs.field(4)==1) |
---|
515 | head_unserialized.Unseen = 'U'; |
---|
516 | |
---|
517 | |
---|
518 | head_unserialized.subject=(head_unserialized.subject==null)?"":head_unserialized.subject; |
---|
519 | |
---|
520 | //var email_unserialized = connector.unserialize(email); |
---|
521 | retorno[cont] = head_unserialized; |
---|
522 | retorno[cont]['msg_number'] = codigoMail; |
---|
523 | retorno[cont]['msg_day'] = mail_unserialized.msg_day; |
---|
524 | retorno[cont]['msg_hour'] = mail_unserialized.msg_hour; |
---|
525 | //declaracao do array() para receber o body de cada mensagem encontrada na busca sql realizada; |
---|
526 | |
---|
527 | retorno[cont]['msg_sample'] = new Array(); |
---|
528 | |
---|
529 | if( (preview_msg_subject == 0) && (preview_msg_tip == 0) ) |
---|
530 | { |
---|
531 | retorno[cont]['msg_sample']['body'] = ""; |
---|
532 | } |
---|
533 | else |
---|
534 | { |
---|
535 | msg_body += ">"; |
---|
536 | msg_body=this.strip_tags(msg_body); |
---|
537 | msg_body=msg_body.replace(/\ /ig," "); |
---|
538 | retorno[cont]['msg_sample']['body'] = " - " + msg_body.substr(2,msg_body.length-1); |
---|
539 | } |
---|
540 | |
---|
541 | cont++; |
---|
542 | rs.next(); |
---|
543 | } |
---|
544 | retorno['num_msgs'] = rs3.field(0); |
---|
545 | rs3.close(); |
---|
546 | rs.close(); |
---|
547 | if(cont>0) |
---|
548 | rs2.close(); |
---|
549 | this.finalize(); |
---|
550 | return retorno; |
---|
551 | } |
---|
552 | |
---|
553 | local_messages.prototype.get_url_anexo = function(msg_number,pid) { |
---|
554 | this.init_local_messages(); |
---|
555 | var matches = ''; |
---|
556 | if(matches = this.parse_id_mail(msg_number)) { |
---|
557 | msg_number = matches[1]; |
---|
558 | } |
---|
559 | |
---|
560 | var retorno; |
---|
561 | var rs = this.dbGears.execute("select url from anexo where id_mail="+msg_number+" and pid = '"+pid+"'"); |
---|
562 | retorno = rs.field(0) |
---|
563 | this.finalize(); |
---|
564 | |
---|
565 | return retorno; |
---|
566 | } |
---|
567 | |
---|
568 | local_messages.prototype.getInputFileFromAnexo = function (element,url) { |
---|
569 | this.init_local_messages(); |
---|
570 | fileSubmitter = this.store.createFileSubmitter(); |
---|
571 | fileSubmitter.setFileInputElement(element,url); |
---|
572 | this.finalize(); |
---|
573 | } |
---|
574 | |
---|
575 | local_messages.prototype.finalize = function() { |
---|
576 | this.dbGears.close(); |
---|
577 | this.dbGears = null; |
---|
578 | } |
---|
579 | |
---|
580 | local_messages.prototype.delete_msgs = function(msgs_number,border_ID) { |
---|
581 | this.init_local_messages(); |
---|
582 | var rs = this.dbGears.execute("select url from anexo where id_mail in ("+msgs_number+")"); |
---|
583 | while(rs.isValidRow()) { |
---|
584 | this.store.remove(rs.field(0)); |
---|
585 | rs.next(); |
---|
586 | } |
---|
587 | this.dbGears.execute("delete from anexo where id_mail in ("+msgs_number+")"); |
---|
588 | this.dbGears.execute("delete from mail where rowid in ("+msgs_number+")"); |
---|
589 | this.finalize(); |
---|
590 | if (msgs_number.length == 1) |
---|
591 | write_msg(get_lang("The message was deleted.")); |
---|
592 | else |
---|
593 | write_msg(get_lang("The messages were deleted.")); |
---|
594 | |
---|
595 | mail_msg = Element("tbody_box"); |
---|
596 | |
---|
597 | try { |
---|
598 | msgs_exploded = msgs_number.split(","); |
---|
599 | }catch(error) { |
---|
600 | msgs_exploded = new Array(); |
---|
601 | msgs_exploded[0] = msgs_number; |
---|
602 | } |
---|
603 | var msg_to_delete; |
---|
604 | for (var i=0; i<msgs_exploded.length; i++){ |
---|
605 | msg_to_delete = Element(msgs_exploded[i]); |
---|
606 | if (msg_to_delete){ |
---|
607 | if ( (msg_to_delete.style.backgroundColor != '') && (preferences.use_shortcuts == '1') ) |
---|
608 | select_msg('null', 'down'); |
---|
609 | mail_msg.removeChild(msg_to_delete); |
---|
610 | decrement_folder_unseen(); |
---|
611 | } |
---|
612 | } |
---|
613 | Element('chk_box_select_all_messages').checked = false; |
---|
614 | if (border_ID != 'null') |
---|
615 | delete_border(border_ID,'false'); |
---|
616 | |
---|
617 | } |
---|
618 | |
---|
619 | local_messages.prototype.get_source_msg = function(id_msg) { |
---|
620 | this.init_local_messages(); |
---|
621 | var rs = this.dbGears.execute("select mail from mail where rowid="+id_msg); |
---|
622 | |
---|
623 | |
---|
624 | mail = connector.unserialize(rs.field(0)); |
---|
625 | download_local_attachment(mail.url_export_file) |
---|
626 | |
---|
627 | this.finalize(); |
---|
628 | } |
---|
629 | |
---|
630 | |
---|
631 | |
---|
632 | local_messages.prototype.set_messages_flag = function(msgs_number, flag) { |
---|
633 | this.init_local_messages(); |
---|
634 | var no_errors = true; |
---|
635 | var msgs_to_set; |
---|
636 | var one_message = false; |
---|
637 | if (msgs_number == 'get_selected_messages') { |
---|
638 | var msgs_to_set = get_selected_messages(); |
---|
639 | msgs_to_set= msgs_to_set.split(","); |
---|
640 | } |
---|
641 | else { //Just one message |
---|
642 | one_message = true; |
---|
643 | msgs_to_set = new Array(); |
---|
644 | msgs_to_set[0] = msgs_number; |
---|
645 | } |
---|
646 | for (var i in msgs_to_set) { |
---|
647 | |
---|
648 | var matches = '';//Messages comming from other tabs. |
---|
649 | if(matches = this.parse_id_mail(msgs_to_set[i])) { |
---|
650 | msgs_to_set[i] = matches[1]; |
---|
651 | } |
---|
652 | |
---|
653 | var rs = this.dbGears.execute("select header,unseen,mail from mail where rowid=" + msgs_to_set[i]); |
---|
654 | header = connector.unserialize(rs.field(0)); |
---|
655 | var mail = connector.unserialize(rs.field(2)); |
---|
656 | unseen = rs.field(1); |
---|
657 | switch(flag) { |
---|
658 | case "unseen": |
---|
659 | set_msg_as_unread(msgs_to_set[i]); |
---|
660 | header["Unseen"] = "U"; |
---|
661 | mail["Unseen"] = "U"; |
---|
662 | mail["header"]["Unseen"] = "U"; |
---|
663 | unseen = 1; |
---|
664 | break; |
---|
665 | case "flagged": |
---|
666 | set_msg_as_flagged(msgs_to_set[i]); |
---|
667 | header["Flagged"] = "F"; |
---|
668 | mail["Flagged"] = "F"; |
---|
669 | mail["header"]["Flagged"] = "F"; |
---|
670 | break; |
---|
671 | case "unflagged": |
---|
672 | if (header["Importance"].indexOf("High") != -1) |
---|
673 | no_errors = false; |
---|
674 | else { |
---|
675 | set_msg_as_unflagged(msgs_to_set[i]); |
---|
676 | header["Flagged"] = "N"; |
---|
677 | mail["Flagged"] = "N"; |
---|
678 | mail["header"]["Flagged"] = "N"; |
---|
679 | } |
---|
680 | break; |
---|
681 | case "seen": |
---|
682 | header["Unseen"] = "N"; |
---|
683 | mail["Unseen"] = "N"; |
---|
684 | mail["header"]["Unseen"] = "N"; |
---|
685 | set_msg_as_read(msgs_to_set[i],true); |
---|
686 | unseen = 0; |
---|
687 | break; |
---|
688 | case "answered": |
---|
689 | header["Draft"]=""; |
---|
690 | mail["Draft"] = ""; |
---|
691 | mail["header"]["Draft"] = ""; |
---|
692 | header["Answered"]="A"; |
---|
693 | mail["Answered"] = "A"; |
---|
694 | mail["header"]["Answered"] = "A"; |
---|
695 | Element("td_message_answered_"+msgs_to_set[i]).innerHTML = '<img src=templates/default/images/answered.gif title=Respondida>'; |
---|
696 | break; |
---|
697 | case "forwarded": |
---|
698 | header["Draft"]="X"; |
---|
699 | mail["Draft"] = "X"; |
---|
700 | mail["header"]["Draft"] = "X"; |
---|
701 | header["Answered"]="A"; |
---|
702 | mail["Answered"] = "A"; |
---|
703 | mail["header"]["Answered"] = "A"; |
---|
704 | Element("td_message_answered_"+msgs_to_set[i]).innerHTML = '<img src=templates/default/images/forwarded.gif title=Encaminhada>'; |
---|
705 | break; |
---|
706 | } |
---|
707 | |
---|
708 | rs.close(); |
---|
709 | |
---|
710 | if(Element("check_box_message_" + msgs_to_set[i])) |
---|
711 | Element("check_box_message_" + msgs_to_set[i]).checked = false; |
---|
712 | |
---|
713 | for (var i in mail.thumbs) { |
---|
714 | /* |
---|
715 | * Os thumbs contêm aspas simples que impactam na sql. |
---|
716 | * Aqui eu substituo as aspas simples por duas simples(escape no sqlite), |
---|
717 | * Caso já venham duas juntas, elas continuam sem serem duplicadas. |
---|
718 | */ |
---|
719 | mail.thumbs[i] = mail.thumbs[i].replace(/([^'])'([^'])('?)/g,"$1''$2$3$3"); |
---|
720 | } |
---|
721 | |
---|
722 | this.dbGears.execute("update mail set mail='"+connector.serialize(mail)+"',header='"+connector.serialize(header)+ |
---|
723 | "',unseen="+unseen+" where rowid="+msgs_to_set[i]); |
---|
724 | } |
---|
725 | if(Element('chk_box_select_all_messages')) |
---|
726 | Element('chk_box_select_all_messages').checked = false; |
---|
727 | this.finalize(); |
---|
728 | if (!no_errors) { |
---|
729 | if(one_message) |
---|
730 | write_msg(get_lang("this message cant be marked as normal")); |
---|
731 | else |
---|
732 | write_msg(get_lang("At least one of selected message cant be marked as normal")); |
---|
733 | return false; |
---|
734 | } |
---|
735 | return true; |
---|
736 | |
---|
737 | } |
---|
738 | |
---|
739 | local_messages.prototype.set_message_flag = function(msg_number,flag,func_after_flag_change) { |
---|
740 | no_errors = this.set_messages_flag(msg_number,flag); |
---|
741 | if(no_errors && func_after_flag_change) |
---|
742 | func_after_flag_change(true); |
---|
743 | } |
---|
744 | |
---|
745 | local_messages.prototype.get_unseen_msgs_number = function() { |
---|
746 | this.init_local_messages(); |
---|
747 | var rs = this.dbGears.execute("select count(*) from mail where unseen=1"); |
---|
748 | var retorno = rs.field(0); |
---|
749 | rs.close(); |
---|
750 | this.finalize(); |
---|
751 | return retorno; |
---|
752 | } |
---|
753 | |
---|
754 | local_messages.prototype.create_folder = function(folder) { |
---|
755 | |
---|
756 | if (folder.indexOf("local_") != -1) |
---|
757 | return false; //can't create folder with string local_ |
---|
758 | |
---|
759 | this.init_local_messages(); |
---|
760 | try { |
---|
761 | this.dbGears.execute("insert into folder (folder,uid_usuario) values (?,?)",[folder,account_id]); |
---|
762 | } catch (error) { |
---|
763 | this.finalize(); |
---|
764 | return false; |
---|
765 | } |
---|
766 | this.finalize(); |
---|
767 | return true; |
---|
768 | } |
---|
769 | |
---|
770 | local_messages.prototype.list_local_folders = function(folder) { |
---|
771 | this.init_local_messages(); |
---|
772 | var retorno = new Array(); |
---|
773 | var retorno_defaults = new Array(); |
---|
774 | rs = this.dbGears.execute("select folder,rowid from folder where uid_usuario=?",[account_id]); |
---|
775 | // rs = this.dbGears.execute("select folder.folder,sum(mail.unseen) from folder left join mail on "+ |
---|
776 | // "folder.rowid=mail.id_folder where folder.uid_usuario=? group by folder.folder",[account_id]); |
---|
777 | var achouInbox = false,achouSent = false ,achouSentConf = false,achouTrash = false,achouDrafts = false; |
---|
778 | while(rs.isValidRow()) { |
---|
779 | var temp = new Array(); |
---|
780 | temp[0] = rs.field(0); |
---|
781 | var rs2 = this.dbGears.execute("select count(*) from mail where id_folder=? and unseen=1",[rs.field(1)]); |
---|
782 | rs2.field(0)? temp[1] = rs2.field(0):temp[1]=0; |
---|
783 | |
---|
784 | var rs3 = this.dbGears.execute("select * from folder where folder like ? limit 1",[temp[0]+"/%"]); |
---|
785 | if(rs3.isValidRow()) |
---|
786 | temp[2] = 1; |
---|
787 | else |
---|
788 | temp[2] = 0; |
---|
789 | |
---|
790 | if(sentfolder == preferences.save_in_folder.replace("INBOX/","") || preferences.save_in_folder.replace("INBOX/","") == trashfolder || preferences.save_in_folder.replace("INBOX/","") == draftsfolder) |
---|
791 | achouSentConf= true; |
---|
792 | |
---|
793 | switch (temp[0]) { |
---|
794 | case 'Inbox': |
---|
795 | retorno_defaults[0] = temp; |
---|
796 | achouInbox = true; |
---|
797 | break; |
---|
798 | case sentfolder : |
---|
799 | retorno_defaults[1] = temp; |
---|
800 | achouSent = true; |
---|
801 | break; |
---|
802 | case trashfolder: |
---|
803 | retorno_defaults[3] = temp; |
---|
804 | achouTrash = true; |
---|
805 | break; |
---|
806 | case draftsfolder: |
---|
807 | retorno_defaults[4] = temp; |
---|
808 | achouDrafts = true; |
---|
809 | break; |
---|
810 | case preferences.save_in_folder.replace("INBOX/",""): |
---|
811 | retorno_defaults[2] = temp; |
---|
812 | achouSentConf = true; |
---|
813 | break; |
---|
814 | default: |
---|
815 | retorno.push(temp); |
---|
816 | } |
---|
817 | |
---|
818 | rs.next(); |
---|
819 | } |
---|
820 | |
---|
821 | rs.close(); |
---|
822 | this.finalize(); |
---|
823 | |
---|
824 | if(preferences.auto_create_local=='0' || (achouInbox && achouSent && achouSentConf && achouTrash && achouDrafts)){ |
---|
825 | var retorno_final = retorno_defaults.concat(retorno.sort(charOrdA)); |
---|
826 | return retorno_final; |
---|
827 | }else{ |
---|
828 | if(!achouInbox) |
---|
829 | this.create_folder('Inbox'); |
---|
830 | if(!achouSent) |
---|
831 | this.create_folder(sentfolder); |
---|
832 | if(!achouTrash) |
---|
833 | this.create_folder(trashfolder); |
---|
834 | if(!achouDrafts) |
---|
835 | this.create_folder(draftsfolder); |
---|
836 | if(!achouSentConf) |
---|
837 | this.create_folder(preferences.save_in_folder.replace("INBOX/","")); |
---|
838 | return this.list_local_folders(); |
---|
839 | } |
---|
840 | |
---|
841 | } |
---|
842 | local_messages.prototype.rename_folder = function(folder,old_folder) { |
---|
843 | if (folder.indexOf("local_") != -1) |
---|
844 | return false; //can't create folder with string local_ |
---|
845 | this.init_local_messages(); |
---|
846 | if (old_folder.indexOf("/") != "-1") { |
---|
847 | final_pos = old_folder.lastIndexOf("/"); |
---|
848 | folder = old_folder.substr(0, final_pos) + "/" + folder; |
---|
849 | } |
---|
850 | try { |
---|
851 | this.dbGears.execute("update folder set folder=? where folder=? and uid_usuario=?",[folder,old_folder,account_id]); |
---|
852 | } catch (error) { |
---|
853 | this.finalize(); |
---|
854 | return false; |
---|
855 | } |
---|
856 | rs = this.dbGears.execute("select folder from folder where folder like ? and uid_usuario=?",[old_folder+'/%',account_id]); |
---|
857 | while(rs.isValidRow()) { |
---|
858 | folder_tmp = rs.field(0); |
---|
859 | folder_new = folder_tmp.replace(old_folder,folder); |
---|
860 | this.dbGears.execute("update folder set folder=? where folder=?",[folder_new,folder_tmp]); |
---|
861 | rs.next(); |
---|
862 | } |
---|
863 | |
---|
864 | |
---|
865 | this.finalize(); |
---|
866 | return true; |
---|
867 | } |
---|
868 | |
---|
869 | local_messages.prototype.remove_folder = function(folder) { |
---|
870 | this.init_local_messages(); |
---|
871 | var rs = this.dbGears.execute("select count(rowid) from folder where folder like ? and uid_usuario=?",[folder+"/%",account_id]); |
---|
872 | var sons = rs.field(0); |
---|
873 | rs.close(); |
---|
874 | |
---|
875 | if(sons == 0){ |
---|
876 | var rs = this.dbGears.execute("select rowid from folder where folder=? and uid_usuario=?",[folder,account_id]); |
---|
877 | var folder_id = rs.field(0); |
---|
878 | rs.close(); |
---|
879 | this.dbGears.execute("delete from folder where rowid=?",[folder_id]); |
---|
880 | rs = this.dbGears.execute("select rowid,mail from mail where id_folder=?",[folder_id]); |
---|
881 | while(rs.isValidRow()) { |
---|
882 | var rs2 = this.dbGears.execute("select url from anexo where id_mail=?",[rs.field(0)]); |
---|
883 | while(rs2.isValidRow()) { |
---|
884 | this.store.remove(rs2.field(0)); |
---|
885 | rs2.next(); |
---|
886 | } |
---|
887 | rs2.close(); |
---|
888 | this.dbGears.execute("delete from anexo where id_mail=?",[rs.field(0)]); |
---|
889 | var mail = connector.unserialize(rs.field(1)); |
---|
890 | this.store.remove(mail.url_export_file); |
---|
891 | rs.next(); |
---|
892 | } |
---|
893 | rs.close(); |
---|
894 | this.dbGears.execute("delete from mail where id_folder=?",[folder_id]); |
---|
895 | this.finalize(); |
---|
896 | return true; |
---|
897 | }else { |
---|
898 | this.finalize(); |
---|
899 | return false; |
---|
900 | } |
---|
901 | |
---|
902 | } |
---|
903 | |
---|
904 | local_messages.prototype.move_messages = function(new_folder,msgs_number) { |
---|
905 | this.init_local_messages(); |
---|
906 | var rs = this.dbGears.execute("select rowid from folder where folder=? and uid_usuario=?",[new_folder,account_id]); |
---|
907 | var id_folder = rs.field(0); |
---|
908 | rs.close(); |
---|
909 | this.dbGears.execute("update mail set id_folder="+id_folder+" where rowid in ("+msgs_number.toString()+")"); //usando statement não tava funcionando quando tinha mais de um email... |
---|
910 | this.finalize(); |
---|
911 | } |
---|
912 | |
---|
913 | local_messages.prototype.setFilter = function(sFilter) |
---|
914 | { |
---|
915 | this.filterSerch = sFilter; |
---|
916 | } |
---|
917 | |
---|
918 | local_messages.prototype.getFilter = function() |
---|
919 | { |
---|
920 | return this.filterSerch; |
---|
921 | } |
---|
922 | |
---|
923 | local_messages.prototype.search = function(folders,sFilter) { |
---|
924 | this.init_local_messages(); |
---|
925 | this.setFilter(sFilter); |
---|
926 | var filters = sFilter.replace(/^##|##$/g,"").split('##'); |
---|
927 | var friendly_filters = new Array(); |
---|
928 | |
---|
929 | if (sFilter.indexOf('ALL') != -1) { //all filters... |
---|
930 | filters[0] = sFilter.replace(/##/g,""); |
---|
931 | tmp = filters[0].split("<=>"); |
---|
932 | |
---|
933 | searchKey = new Array(); |
---|
934 | searchKey.push("SUBJECT"); |
---|
935 | searchKey.push(tmp[1]); |
---|
936 | friendly_filters.push(searchKey); |
---|
937 | |
---|
938 | searchKey = new Array(); |
---|
939 | searchKey.push("BODY"); |
---|
940 | searchKey.push(tmp[1]); |
---|
941 | friendly_filters.push(searchKey); |
---|
942 | |
---|
943 | searchKey = new Array(); |
---|
944 | searchKey.push("FROM"); |
---|
945 | searchKey.push(tmp[1]); |
---|
946 | friendly_filters.push(searchKey); |
---|
947 | |
---|
948 | searchKey = new Array(); |
---|
949 | searchKey.push("TO"); |
---|
950 | searchKey.push(tmp[1]); |
---|
951 | friendly_filters.push(searchKey); |
---|
952 | |
---|
953 | searchKey = new Array(); |
---|
954 | searchKey.push("CC"); |
---|
955 | searchKey.push(tmp[1]); |
---|
956 | friendly_filters.push(searchKey); |
---|
957 | } |
---|
958 | else { |
---|
959 | for (var i=0; i<filters.length; i++) |
---|
960 | { |
---|
961 | if (filters[i] != ""){ |
---|
962 | //tmp[0] = tmp[0].replace(/^\s+|\s+$/g,""); |
---|
963 | //tmp[1] = tmp[1].replace(/^\s+|\s+$/g,""); |
---|
964 | friendly_filters.push(filters[i].split("<=>")); |
---|
965 | } |
---|
966 | } |
---|
967 | } |
---|
968 | var sql = "select mail.header,folder.folder,mail.rowid,size from mail inner join folder on mail.id_folder=folder.rowid where mail.uid_usuario="+account_id + " and folder.folder in ("; |
---|
969 | for(var fnum in folders) { |
---|
970 | sql+="'"+folders[fnum]+"'"; |
---|
971 | if(fnum<folders.length-1) |
---|
972 | sql+=","; |
---|
973 | } |
---|
974 | sql += ") and ("; |
---|
975 | for (var z=0;z<friendly_filters.length;z++) { |
---|
976 | if (z != 0) { |
---|
977 | if (sFilter.indexOf('ALL') != -1) |
---|
978 | sql += " or"; |
---|
979 | else |
---|
980 | sql += " and"; |
---|
981 | } |
---|
982 | var cond = friendly_filters[z][0].replace(/^\s+|\s+$/g,""); |
---|
983 | if (cond == "SINCE" || cond == "BEFORE" | cond == "ON"){ |
---|
984 | |
---|
985 | tmpDate = friendly_filters[z][1].replace(/\%2F/g,"/").split('/'); |
---|
986 | |
---|
987 | // Date = url_decode(friendly_filters[z][1]); |
---|
988 | sql+=" mail.timestamp " + this.aux_convert_filter_field(friendly_filters[z][0], tmpDate); |
---|
989 | } |
---|
990 | else if (!friendly_filters[z][1]) |
---|
991 | { |
---|
992 | sql+=" mail."+this.aux_convert_filter_field(friendly_filters[z][0]); |
---|
993 | } |
---|
994 | else |
---|
995 | { |
---|
996 | sql+=" mail."+this.aux_convert_filter_field(friendly_filters[z][0])+" like '%"+url_decode(friendly_filters[z][1])+"%'"; |
---|
997 | } |
---|
998 | } |
---|
999 | sql += ")"; |
---|
1000 | var rs = this.dbGears.execute(sql); |
---|
1001 | var retorno = []; |
---|
1002 | var numRec = 0; |
---|
1003 | |
---|
1004 | while( rs.isValidRow() ) |
---|
1005 | { |
---|
1006 | var header = connector.unserialize( rs.field(0) ); |
---|
1007 | var date_formated = ( header["udate"] ).toString(); |
---|
1008 | /* |
---|
1009 | * Antigamente o udate vinha com o formato de data e foi alterado para vir o timestamp. |
---|
1010 | * Nesse caso, e-mails arquivados anteriormente usamos o udate, caso contrario, usamos o smalldate, |
---|
1011 | * pois o udate agora vem como timestamp |
---|
1012 | * verifica tambem no caso de mensagens antigas que nao exista o smalldate |
---|
1013 | * definir |
---|
1014 | */ |
---|
1015 | if( !date_formated.match(/\d{2}\/\d{2}\/\d{4}/) ) |
---|
1016 | { |
---|
1017 | if ( (typeof(header["smalldate"]) != "undefined") && (!header["smalldate"].match(/\d{2}\:\d{2}/) ) ){ |
---|
1018 | date_formated = header["smalldate"]; |
---|
1019 | }else{ |
---|
1020 | var day = new Date(); |
---|
1021 | day.setTime(header["udate"] * 1000); |
---|
1022 | aux_dia = day.getDate() < 10 ? '0' + day.getDate() : day.getDate(); |
---|
1023 | aux_mes = day.getMonth()+1; // array start with '0..11' |
---|
1024 | aux_mes = aux_mes < 10 ? '0' + aux_mes : aux_mes; |
---|
1025 | date_formated = aux_dia + '/' + aux_mes + '/' + day.getFullYear(); |
---|
1026 | } |
---|
1027 | } |
---|
1028 | |
---|
1029 | retorno[ numRec++ ] = |
---|
1030 | { |
---|
1031 | 'from' : header["from"]["name"], |
---|
1032 | 'subject' : header["subject"], |
---|
1033 | 'udate' : date_formated, |
---|
1034 | 'size' : rs.field(3), |
---|
1035 | 'flag' : header["Unseen"]+header["Recent"]+header["Flagged"]+header["Draft"], |
---|
1036 | 'boxname' : "local_"+rs.field(1), |
---|
1037 | 'uid' : rs.field(2) |
---|
1038 | } |
---|
1039 | |
---|
1040 | rs.next(); |
---|
1041 | } |
---|
1042 | |
---|
1043 | this.finalize(); |
---|
1044 | |
---|
1045 | return ( retorno.length > 0 ) ? retorno : false ; |
---|
1046 | } |
---|
1047 | |
---|
1048 | local_messages.prototype.aux_convert_size = function(size) { |
---|
1049 | var tmp = Math.floor(size/1024); |
---|
1050 | if(tmp >= 1){ |
---|
1051 | return tmp + " kb"; |
---|
1052 | }else{ |
---|
1053 | return size + " b"; |
---|
1054 | } |
---|
1055 | |
---|
1056 | } |
---|
1057 | |
---|
1058 | local_messages.prototype.aux_convert_filter_field = function(filter,date) |
---|
1059 | { |
---|
1060 | var dateObj; |
---|
1061 | |
---|
1062 | if (typeof date != 'undefined') |
---|
1063 | { |
---|
1064 | dateObj = new Date(date[2],date[1]-1,date[0]); |
---|
1065 | } |
---|
1066 | |
---|
1067 | if((filter=="SUBJECT ") || (filter=="SUBJECT")) |
---|
1068 | return "subject"; |
---|
1069 | else if((filter=="BODY ") || (filter=="BODY")) |
---|
1070 | return "body"; |
---|
1071 | else if((filter=="FROM ") || (filter=="FROM")) |
---|
1072 | return "ffrom"; |
---|
1073 | else if((filter=="TO ") || (filter=="TO")) |
---|
1074 | return "fto"; |
---|
1075 | else if((filter=="CC ") || (filter=="CC")) |
---|
1076 | return "cc"; |
---|
1077 | else if (filter.replace(/^\s+|\s+$/g,"") == "SINCE"){ |
---|
1078 | dateObj.setHours(0, 0, 0, 0); |
---|
1079 | return ">= " + dateObj.getTime().toString(10).substr(0, 10); |
---|
1080 | } |
---|
1081 | else if (filter.replace(/^\s+|\s+$/g,"") == "BEFORE"){ |
---|
1082 | dateObj.setHours(23, 59, 59, 999); |
---|
1083 | return "<= " + dateObj.getTime().toString(10).substr(0, 10); |
---|
1084 | } |
---|
1085 | else if (filter.replace(/^\s+|\s+$/g,"") == "ON"){ |
---|
1086 | dateObj.setHours(0, 0, 0, 0); |
---|
1087 | var ts1 = dateObj.getTime().toString(10).substr(0, 10); |
---|
1088 | dateObj.setHours(23, 59, 59, 999); |
---|
1089 | var ts2 = dateObj.getTime().toString(10).substr(0, 10); |
---|
1090 | return ">= " + ts1 + ") and (timestamp <= " + ts2; |
---|
1091 | } |
---|
1092 | else if (filter.replace(/^\s+|\s+$/g,"") == "FLAGGED") |
---|
1093 | return "flagged = 1"; |
---|
1094 | else if (filter.replace(/^\s+|\s+$/g,"") == "UNFLAGGED") |
---|
1095 | return "flagged = 0"; |
---|
1096 | else if (filter.replace(/^\s+|\s+$/g,"") == "UNSEEN") |
---|
1097 | return "unseen = 1"; |
---|
1098 | else if (filter.replace(/^\s+|\s+$/g,"") == "SEEN") |
---|
1099 | return "unseen = 0"; |
---|
1100 | else if (filter.replace(/^\s+|\s+$/g,"") == "ANSWERED") |
---|
1101 | return "answered = 1"; |
---|
1102 | else if (filter.replace(/^\s+|\s+$/g,"") == "UNANSWERED") |
---|
1103 | return "answered = 0"; |
---|
1104 | else if (filter.replace(/^\s+|\s+$/g,"") == "RECENT") |
---|
1105 | return "recent = 1"; |
---|
1106 | else if (filter.replace(/^\s+|\s+$/g,"") == "OLD") |
---|
1107 | return "recent = 0"; |
---|
1108 | |
---|
1109 | } |
---|
1110 | |
---|
1111 | local_messages.prototype.has_local_mails = function() { |
---|
1112 | this.init_local_messages(); |
---|
1113 | var rs = this.dbGears.execute("select rowid from folder limit 0,1"); |
---|
1114 | var retorno; |
---|
1115 | if(rs.isValidRow()) |
---|
1116 | retorno = true; |
---|
1117 | else |
---|
1118 | retorno = false; |
---|
1119 | this.finalize(); |
---|
1120 | return retorno; |
---|
1121 | } |
---|
1122 | |
---|
1123 | //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Simple AJAX function used to get the RFC822 email source. |
---|
1124 | local_messages.prototype.get_src = function(url){ |
---|
1125 | AJAX = false; |
---|
1126 | if (window.XMLHttpRequest) { // Mozilla, Safari,... |
---|
1127 | AJAX = new XMLHttpRequest(); |
---|
1128 | if (AJAX.overrideMimeType) { |
---|
1129 | AJAX.overrideMimeType('text/xml'); |
---|
1130 | } |
---|
1131 | } else if (window.ActiveXObject) { // IE |
---|
1132 | try { |
---|
1133 | AJAX = new ActiveXObject("Msxml2.XMLHTTP"); |
---|
1134 | } catch (e) { |
---|
1135 | try { |
---|
1136 | AJAX = new ActiveXObject("Microsoft.XMLHTTP"); |
---|
1137 | } catch (e) {} |
---|
1138 | } |
---|
1139 | } |
---|
1140 | |
---|
1141 | if (!AJAX) { |
---|
1142 | alert('ERRO :(Seu navegador não suporta a aplicação usada neste site'); |
---|
1143 | return false; |
---|
1144 | } |
---|
1145 | |
---|
1146 | AJAX.onreadystatechange = function() { |
---|
1147 | if (AJAX.readyState == 4) { |
---|
1148 | AJAX.src=AJAX.responseText; |
---|
1149 | if (AJAX.status == 200) { |
---|
1150 | return AJAX.responseText; |
---|
1151 | } else { |
---|
1152 | return false; |
---|
1153 | } |
---|
1154 | } |
---|
1155 | } |
---|
1156 | |
---|
1157 | AJAX.open('get', url, false); |
---|
1158 | AJAX.send(null); |
---|
1159 | return AJAX.responseText; |
---|
1160 | }; |
---|
1161 | |
---|
1162 | //Por Bruno Costa(bruno.vieira-costa@serpro.gov.br - Dessarquiva msgs locais pegando o codigo fonte das mesmas e mandando via POST para o servidor |
---|
1163 | //para que elas sejam inseridas no imap pela função imap_functions.unarchive_mail. |
---|
1164 | local_messages.prototype.unarchive_msgs = function (folder,new_folder,msgs_number){ |
---|
1165 | |
---|
1166 | if(!new_folder) |
---|
1167 | new_folder='INBOX'; |
---|
1168 | this.init_local_messages(); |
---|
1169 | // alert(folder+new_folder+msgs_number); |
---|
1170 | var handler_unarchive = function(data) |
---|
1171 | { |
---|
1172 | if(data.error == '') |
---|
1173 | write_msg(get_lang('All messages are successfully unarchived')); |
---|
1174 | else |
---|
1175 | alert(data.error); |
---|
1176 | } |
---|
1177 | |
---|
1178 | if(currentTab.toString().indexOf("_r") != -1){ |
---|
1179 | msgs_number = currentTab.toString().substr(0,currentTab.toString().indexOf("_r")); |
---|
1180 | } |
---|
1181 | |
---|
1182 | if(msgs_number =='selected' || !msgs_number) |
---|
1183 | { |
---|
1184 | msgs_number = get_selected_messages() |
---|
1185 | if (!msgs_number){ |
---|
1186 | write_msg(get_lang('No selected message.')); |
---|
1187 | return; |
---|
1188 | } |
---|
1189 | var rs = this.dbGears.execute("select mail,timestamp from mail where rowid in ("+msgs_number+")"); |
---|
1190 | var source=""; |
---|
1191 | var flags=""; |
---|
1192 | var timestamp=""; |
---|
1193 | |
---|
1194 | while(rs.isValidRow()) { |
---|
1195 | mail=connector.unserialize(rs.field(0)); |
---|
1196 | mail.msg_source?source_tmp = escape(mail.msg_source):source_tmp = escape(this.get_src(mail.url_export_file)); |
---|
1197 | flags+="#@#@#@"+mail["Answered"]+":"+mail["Draft"]+":"+mail["Flagged"]+":"+mail["Unseen"]; |
---|
1198 | source+="#@#@#@"+source_tmp; |
---|
1199 | timestamp+="#@#@#@"+rs.field(1); |
---|
1200 | rs.next(); |
---|
1201 | } |
---|
1202 | rs.close(); |
---|
1203 | this.finalize(); |
---|
1204 | } |
---|
1205 | else |
---|
1206 | { |
---|
1207 | var rs = this.dbGears.execute("select mail,timestamp from mail where rowid="+msgs_number); |
---|
1208 | mail=connector.unserialize(rs.field(0)); |
---|
1209 | var source =""; |
---|
1210 | |
---|
1211 | mail.msg_source?source = mail.msg_source:source = this.get_src(mail.url_export_file); |
---|
1212 | flags = mail["Answered"]+":"+mail["Draft"]+":"+mail["Flagged"]+":"+mail["Unseen"]; |
---|
1213 | timestamp=rs.field(1); |
---|
1214 | rs.close(); |
---|
1215 | this.finalize(); |
---|
1216 | } |
---|
1217 | params="&folder="+new_folder+"&source="+source+"×tamp="+timestamp+"&flags="+flags; |
---|
1218 | cExecute ("$this.imap_functions.unarchive_mail&", handler_unarchive, params); |
---|
1219 | } |
---|
1220 | |
---|
1221 | local_messages.prototype.get_msg_date = function (original_id, is_local){ |
---|
1222 | |
---|
1223 | this.init_local_messages(); |
---|
1224 | |
---|
1225 | if (typeof(is_local) == 'undefined') |
---|
1226 | { |
---|
1227 | is_local = false; |
---|
1228 | } |
---|
1229 | |
---|
1230 | var rs; |
---|
1231 | |
---|
1232 | if (is_local) |
---|
1233 | { |
---|
1234 | rs = this.dbGears.execute("select mail from mail where rowid="+original_id); |
---|
1235 | } |
---|
1236 | else |
---|
1237 | { |
---|
1238 | rs = this.dbGears.execute("select mail from mail where original_id="+original_id); |
---|
1239 | } |
---|
1240 | var tmp = connector.unserialize(rs.field(0)); |
---|
1241 | var ret = new Array(); |
---|
1242 | ret.fulldate = tmp.fulldate.substr(0,16); |
---|
1243 | ret.smalldate = tmp.msg_day; |
---|
1244 | ret.msg_day = tmp.msg_day; |
---|
1245 | ret.msg_hour = tmp.msg_day; |
---|
1246 | |
---|
1247 | rs.close(); |
---|
1248 | this.finalize(); |
---|
1249 | return ret; |
---|
1250 | } |
---|
1251 | |
---|
1252 | |
---|
1253 | local_messages.prototype.download_all_local_attachments = function(folder,id){ |
---|
1254 | this.init_local_messages(); |
---|
1255 | var rs = this.dbGears.execute("select mail from mail where rowid="+id); |
---|
1256 | var tmp = connector.unserialize(rs.field(0)); |
---|
1257 | rs.close(); |
---|
1258 | this.finalize(); |
---|
1259 | tmp.msg_source?source = tmp.msg_source:source = this.get_src(tmp.url_export_file); |
---|
1260 | //source = this.get_src(tmp.url_export_file); |
---|
1261 | source = escape(source); |
---|
1262 | var handler_source = function(data){ |
---|
1263 | download_attachments(null, null, data, null,null,'anexos.zip'); |
---|
1264 | } |
---|
1265 | cExecute("$this.imap_functions.download_all_local_attachments",handler_source,"source="+source); |
---|
1266 | } |
---|
1267 | |
---|
1268 | /*************************************************************************/ |
---|
1269 | /* Funcao usada para exportar mensagens arquivadas localmente. |
---|
1270 | * Rommel de Brito Cysne (rommel.cysne@serpro.gov.br) |
---|
1271 | * em 22/12/2008. |
---|
1272 | */ |
---|
1273 | local_messages.prototype.local_messages_to_export = function(){ |
---|
1274 | |
---|
1275 | if (openTab.type[currentTab] > 1){ |
---|
1276 | var msgs_to_export_id = currentTab.substring(0,currentTab.length-2,currentTab); |
---|
1277 | }else{ |
---|
1278 | var msgs_to_export_id = get_selected_messages(); |
---|
1279 | } |
---|
1280 | var handler_local_mesgs_to_export = function(data){ |
---|
1281 | download_attachments(null, null, data, null,null,'mensagens.zip'); |
---|
1282 | } |
---|
1283 | if(msgs_to_export_id){ |
---|
1284 | this.init_local_messages(); |
---|
1285 | var l_msg = "t"; |
---|
1286 | var mesgs =""; |
---|
1287 | var subjects =""; |
---|
1288 | var rs = this.dbGears.execute("select mail,subject from mail where rowid in ("+msgs_to_export_id+")"); |
---|
1289 | while(rs.isValidRow()){ |
---|
1290 | mail = connector.unserialize(rs.field(0)); |
---|
1291 | mail.msg_source?src = mail.msg_source:src = this.get_src(mail.url_export_file); |
---|
1292 | subject = rs.field(1); |
---|
1293 | mesgs += src; |
---|
1294 | mesgs += "@@"; |
---|
1295 | subjects += subject; |
---|
1296 | subjects += "@@"; |
---|
1297 | rs.next(); |
---|
1298 | } |
---|
1299 | rs.close(); |
---|
1300 | this.finalize(); |
---|
1301 | mesgs = escape(mesgs); |
---|
1302 | subjects = escape(subjects); |
---|
1303 | params = "subjects="+subjects+"&mesgs="+mesgs+"&l_msg="+l_msg+"&msgs_to_export="+msgs_to_export_id; |
---|
1304 | cExecute ("$this.exporteml.makeAll&", handler_local_mesgs_to_export, params); |
---|
1305 | } |
---|
1306 | return true; |
---|
1307 | } |
---|
1308 | |
---|
1309 | local_messages.prototype.get_all_local_folder_messages= function(folder_name){ |
---|
1310 | |
---|
1311 | |
---|
1312 | var mesgs = new Array(); |
---|
1313 | var subjects = new Array(); |
---|
1314 | var hoje = new Date(); |
---|
1315 | var msgs_to_export = new Array(); |
---|
1316 | var l_msg="t"; |
---|
1317 | |
---|
1318 | this.init_local_messages(); |
---|
1319 | var query = "select mail,subject,mail.rowid from mail inner join folder on (mail.id_folder=folder.rowid) where folder.folder='" + folder_name + "'"; |
---|
1320 | |
---|
1321 | var rs = this.dbGears.execute(" "+query) |
---|
1322 | |
---|
1323 | var handler_local_mesgs_to_export = function(data){ |
---|
1324 | //alert("data - " + data + " - tipo - " + typeof(data)); |
---|
1325 | download_attachments(null, null, data, null,null,'mensagens.zip'); |
---|
1326 | } |
---|
1327 | var j=0; |
---|
1328 | while (rs.isValidRow()){ |
---|
1329 | msgs_to_export[j]=rs.field(2) |
---|
1330 | mail = connector.unserialize(rs.field(0)); |
---|
1331 | mail.msg_source?src = mail.msg_source:src = this.get_src(mail.url_export_file); |
---|
1332 | subject = rs.field(1); |
---|
1333 | mesgs += src; |
---|
1334 | mesgs += "@@"; |
---|
1335 | subjects += subject; |
---|
1336 | subjects += "@@"; |
---|
1337 | rs.next(); |
---|
1338 | j++; |
---|
1339 | |
---|
1340 | } |
---|
1341 | rs.close(); |
---|
1342 | this.finalize(); |
---|
1343 | source = escape(mesgs); |
---|
1344 | subjects = escape(subjects); |
---|
1345 | params = "folder="+folder_name+"&subjects="+subjects+"&mesgs="+source+"&l_msg="+l_msg+"&msgs_to_export="+msgs_to_export; |
---|
1346 | cExecute ("$this.exporteml.makeAll&", handler_local_mesgs_to_export, params); |
---|
1347 | |
---|
1348 | |
---|
1349 | } |
---|
1350 | |
---|
1351 | |
---|
1352 | /*************************************************************************/ |
---|
1353 | |
---|
1354 | |
---|
1355 | /****************************************************************** |
---|
1356 | Offline Part |
---|
1357 | ******************************************************************/ |
---|
1358 | |
---|
1359 | local_messages.prototype.is_offline_installed = function() { |
---|
1360 | this.init_local_messages(); |
---|
1361 | var check = this.localServer.openManagedStore('expresso-offline'); |
---|
1362 | this.finalize(); |
---|
1363 | if(check==null) |
---|
1364 | return false; |
---|
1365 | else |
---|
1366 | return true; |
---|
1367 | |
---|
1368 | } |
---|
1369 | local_messages.prototype.update_offline = function(redirect) { |
---|
1370 | this.init_local_messages(); |
---|
1371 | var managedStore = this.localServer.openManagedStore('expresso-offline'); |
---|
1372 | |
---|
1373 | if(managedStore!=null){ |
---|
1374 | |
---|
1375 | managedStore.oncomplete = function(details){ |
---|
1376 | if(redirect!=null) |
---|
1377 | location.href=redirect; |
---|
1378 | } |
---|
1379 | |
---|
1380 | managedStore.checkForUpdate(); |
---|
1381 | } else if(redirect!=null) { |
---|
1382 | location.href=redirect; |
---|
1383 | } |
---|
1384 | this.finalize(); |
---|
1385 | } |
---|
1386 | |
---|
1387 | local_messages.prototype.uninstall_offline = function() { |
---|
1388 | if (!window.google || !google.gears) { |
---|
1389 | temp = confirm(document.getElementById('lang_gears_redirect').value); |
---|
1390 | if (temp) { |
---|
1391 | expresso_local_messages.installGears(); |
---|
1392 | } |
---|
1393 | return; |
---|
1394 | |
---|
1395 | } |
---|
1396 | this.init_local_messages(); |
---|
1397 | this.localServer.removeManagedStore('expresso-offline'); |
---|
1398 | alert(document.getElementById('lang_offline_uninstalled').value); |
---|
1399 | //this.dbGears.execute('drop table user'); |
---|
1400 | //this.dbGears.execute('drop table queue'); |
---|
1401 | //this.dbGears.execute('drop table attachments_queue'); |
---|
1402 | this.finalize(); |
---|
1403 | } |
---|
1404 | |
---|
1405 | local_messages.prototype.get_folders_to_sync = function() {//Precisa ter visibilidade ao array de linguagens. |
---|
1406 | this.init_local_messages(); |
---|
1407 | var rs = this.dbGears.execute("select id_folder,folder_name from folders_sync where uid_usuario="+account_id); |
---|
1408 | var retorno = new Array(); |
---|
1409 | while(rs.isValidRow()) { |
---|
1410 | temp = new Array(); |
---|
1411 | temp[0] = rs.field(0); |
---|
1412 | if(temp[0]=='INBOX/Drafts' ||temp[0]=='INBOX/Trash' || temp[0]=='INBOX/Sent') { |
---|
1413 | temp[1] = array_lang[rs.field(1).toLowerCase()]; |
---|
1414 | } |
---|
1415 | else { |
---|
1416 | temp[1] = rs.field(1); |
---|
1417 | } |
---|
1418 | |
---|
1419 | retorno.push(temp); |
---|
1420 | rs.next(); |
---|
1421 | } |
---|
1422 | this.finalize(); |
---|
1423 | return retorno; |
---|
1424 | } |
---|
1425 | |
---|
1426 | local_messages.prototype.install_offline = function(urlOffline,urlIcone,uid_usuario,login,pass,redirect) { |
---|
1427 | if (!window.google || !google.gears) { |
---|
1428 | temp = confirm(document.getElementById('lang_gears_redirect').value); |
---|
1429 | if (temp) { |
---|
1430 | expresso_local_messages.installGears(); |
---|
1431 | } |
---|
1432 | return; |
---|
1433 | |
---|
1434 | } |
---|
1435 | |
---|
1436 | if(pass.length>0) { |
---|
1437 | only_spaces = true; |
---|
1438 | for(cont=0;cont<pass.length;cont++) { |
---|
1439 | if(pass.charAt(cont)!=" ") |
---|
1440 | only_spaces = false; |
---|
1441 | } |
---|
1442 | if(only_spaces) { |
---|
1443 | alert(document.getElementById('lang_only_spaces_not_allowed').value); |
---|
1444 | return false; |
---|
1445 | } |
---|
1446 | } |
---|
1447 | |
---|
1448 | modal('loading'); |
---|
1449 | var desktop = google.gears.factory.create('beta.desktop'); |
---|
1450 | desktop.createShortcut('ExpressoMail Offline', |
---|
1451 | urlOffline, |
---|
1452 | {'32x32': urlIcone}, |
---|
1453 | 'ExpressoMail Offline'); |
---|
1454 | |
---|
1455 | |
---|
1456 | this.init_local_messages(); |
---|
1457 | |
---|
1458 | //user with offline needs to have at least the folder Inbox already created. |
---|
1459 | tmp_rs = this.dbGears.execute("select rowid from folder where folder='Inbox' and uid_usuario=?",[uid_usuario]); |
---|
1460 | if(!tmp_rs.isValidRow()) |
---|
1461 | this.dbGears.execute("insert into folder (folder,uid_usuario) values (?,?)",['Inbox',uid_usuario]); |
---|
1462 | |
---|
1463 | this.localServer.removeManagedStore('expresso-offline'); |
---|
1464 | |
---|
1465 | var managedStore = this.localServer.createManagedStore('expresso-offline'); |
---|
1466 | managedStore.manifestUrl = 'js/manifest'; |
---|
1467 | |
---|
1468 | managedStore.onerror = function (error) { |
---|
1469 | alert(error); |
---|
1470 | } |
---|
1471 | |
---|
1472 | managedStore.oncomplete = function(details) { |
---|
1473 | if (close_lightbox_div) { |
---|
1474 | close_lightbox(); |
---|
1475 | close_lightbox_div = false; |
---|
1476 | alert(document.getElementById('lang_offline_installed').value); |
---|
1477 | location.href=redirect; |
---|
1478 | } |
---|
1479 | } |
---|
1480 | |
---|
1481 | //create structure to user in db |
---|
1482 | this.dbGears.execute('create table if not exists user (uid_usuario int,login text,pass text, logged int,unique(uid_usuario))'); |
---|
1483 | this.dbGears.execute('create table if not exists queue (ffrom text, fto text, cc text, cco text,'+ |
---|
1484 | 'subject text, conf_receipt int, important int,body text,sent int,user int)'); |
---|
1485 | this.dbGears.execute('create table if not exists attachments_queue ('+ |
---|
1486 | 'id_queue int, url_attach text)'); |
---|
1487 | this.dbGears.execute('create table if not exists sent_problems (' + |
---|
1488 | 'id_queue int,message text)'); |
---|
1489 | |
---|
1490 | //d = new Date(); |
---|
1491 | |
---|
1492 | try { |
---|
1493 | var rs = this.dbGears.execute("select uid_usuario from user where uid_usuario=?",[uid_usuario]); |
---|
1494 | if(!rs.isValidRow()) |
---|
1495 | this.dbGears.execute("insert into user (uid_usuario,login,pass) values (?,?,?)",[uid_usuario,login,pass]); |
---|
1496 | else |
---|
1497 | this.dbGears.execute("update user set pass=? where uid_usuario=?",[pass,uid_usuario]); |
---|
1498 | } catch (error) { |
---|
1499 | this.finalize(); |
---|
1500 | alert(error); |
---|
1501 | return false; |
---|
1502 | } |
---|
1503 | managedStore.checkForUpdate(); |
---|
1504 | this.capt_url('controller.php?action=$this.db_functions.get_dropdown_contacts_to_cache'); |
---|
1505 | setTimeout(function(){ |
---|
1506 | managedStore.complete(); |
---|
1507 | }, 60000); |
---|
1508 | this.finalize(); |
---|
1509 | } |
---|
1510 | |
---|
1511 | /** |
---|
1512 | * Return all users in an array following the structure below. |
---|
1513 | * |
---|
1514 | * key: uid |
---|
1515 | * value: user login |
---|
1516 | */ |
---|
1517 | local_messages.prototype.get_all_users = function() { |
---|
1518 | this.init_local_messages(); |
---|
1519 | var users = new Array(); |
---|
1520 | var rs = this.dbGears.execute("select uid_usuario,login from user"); |
---|
1521 | while(rs.isValidRow()) { |
---|
1522 | users[rs.field(0)] = rs.field(1); |
---|
1523 | rs.next(); |
---|
1524 | } |
---|
1525 | this.finalize(); |
---|
1526 | return users; |
---|
1527 | } |
---|
1528 | |
---|
1529 | local_messages.prototype.set_as_logged = function(uid_usuario,pass,bypass) { |
---|
1530 | this.init_local_messages(); |
---|
1531 | if (!bypass) { |
---|
1532 | var rs = this.dbGears.execute("select pass from user where uid_usuario=?", [uid_usuario]); |
---|
1533 | if (!rs.isValidRow() || (pass != rs.field(0) && pass != MD5(rs.field(0)))) { |
---|
1534 | this.finalize(); |
---|
1535 | return false; |
---|
1536 | } |
---|
1537 | } |
---|
1538 | d = new Date(); |
---|
1539 | |
---|
1540 | this.dbGears.execute("update user set logged=null"); //Logoff in everybody |
---|
1541 | this.dbGears.execute("update user set logged=? where uid_usuario=?",[d.getTime(),uid_usuario]); //Login just in one... |
---|
1542 | this.finalize(); |
---|
1543 | return true; |
---|
1544 | } |
---|
1545 | |
---|
1546 | local_messages.prototype.unset_as_logged = function() { |
---|
1547 | this.init_local_messages(); |
---|
1548 | this.dbGears.execute("update user set logged=null"); //Logoff in everybody |
---|
1549 | this.finalize(); |
---|
1550 | } |
---|
1551 | |
---|
1552 | local_messages.prototype.user_logged = function() { |
---|
1553 | this.init_local_messages(); |
---|
1554 | var user_logged = new Array(); |
---|
1555 | var rs = this.dbGears.execute("select uid_usuario,logged from user where logged is not null"); |
---|
1556 | if(!rs.isValidRow()) { |
---|
1557 | this.finalize(); |
---|
1558 | return null; |
---|
1559 | } |
---|
1560 | user_logged[0] = rs.field(0); |
---|
1561 | user_logged[1] = rs.field(1); |
---|
1562 | this.finalize(); |
---|
1563 | return user_logged; |
---|
1564 | } |
---|
1565 | |
---|
1566 | local_messages.prototype.send_to_queue = function (form) { |
---|
1567 | this.init_local_messages(); |
---|
1568 | var mail_values = new Array(); |
---|
1569 | |
---|
1570 | for (var i=0;i<form.length;i++) { |
---|
1571 | if (form.elements[i].name != '') { //I.E made me to do that... |
---|
1572 | if(form.elements[i].name=='folder' || form.elements[i].name=='msg_id' || form.elements[i].name=='' || form.elements[i].name==null) |
---|
1573 | continue; |
---|
1574 | else if (form.elements[i].name == 'input_return_receipt' ) |
---|
1575 | mail_values['conf_receipt'] = form.elements[i].checked ? 1 : 0; |
---|
1576 | else if(form.elements[i].name == 'input_important_message') |
---|
1577 | mail_values['important'] = form.elements[i].checked ? 1 : 0; |
---|
1578 | else |
---|
1579 | if (form.elements[i].name == 'body') |
---|
1580 | mail_values['body'] = form.elements[i].value; |
---|
1581 | else |
---|
1582 | if (form.elements[i].name == 'input_from') |
---|
1583 | mail_values['ffrom'] = form.elements[i].value; |
---|
1584 | else |
---|
1585 | if (form.elements[i].name == 'input_to') |
---|
1586 | mail_values['fto'] = form.elements[i].value; |
---|
1587 | else |
---|
1588 | if (form.elements[i].name == 'input_cc') |
---|
1589 | mail_values['cc'] = form.elements[i].value; |
---|
1590 | else |
---|
1591 | if (form.elements[i].name == 'input_cco') |
---|
1592 | mail_values['cco'] = form.elements[i].value; |
---|
1593 | else |
---|
1594 | if (form.elements[i].name == 'input_subject') |
---|
1595 | mail_values['subject'] = form.elements[i].value; |
---|
1596 | } |
---|
1597 | } |
---|
1598 | //mail_values['fto'] = input_to; |
---|
1599 | //mail_values['cc'] = input_cc; |
---|
1600 | //mail_values['cco'] = input_cco; |
---|
1601 | //mail_values['subject'] = input_subject; |
---|
1602 | //mail_values['conf_receipt'] = input_return_receipt; |
---|
1603 | //mail_values['important'] = input_important_message; |
---|
1604 | |
---|
1605 | try { |
---|
1606 | this.dbGears.execute("insert into queue (ffrom,fto,cc,cco,subject,conf_receipt,important,body,sent,user) values (?,?,?,?,?,?,?,?,0,?)", [mail_values['ffrom'], mail_values['fto'], mail_values['cc'], mail_values['cco'], mail_values['subject'], mail_values['conf_receipt'], mail_values['important'], mail_values['body'], account_id]); |
---|
1607 | this.send_attach_to_queue(this.dbGears.lastInsertRowId,form); |
---|
1608 | }catch(error) { |
---|
1609 | alert(error); |
---|
1610 | return get_lang('Error sending a mail to queue. Verify if you have installed ExpressoMail Offline'); |
---|
1611 | } |
---|
1612 | this.finalize(); |
---|
1613 | return true; |
---|
1614 | } |
---|
1615 | |
---|
1616 | local_messages.prototype.send_attach_to_queue = function(id_queue,form) { |
---|
1617 | |
---|
1618 | for(i=0;i<form.elements.length;i++) { |
---|
1619 | |
---|
1620 | if(form.elements[i].name.indexOf("file_")!=-1) { |
---|
1621 | var tmp_input = form.elements[i]; |
---|
1622 | var d = new Date(); |
---|
1623 | var url_local = 'local_attachs/'+d.getTime(); |
---|
1624 | this.store.captureFile(tmp_input, url_local); |
---|
1625 | this.dbGears.execute("insert into attachments_queue (id_queue,url_attach) values (?,?)",[id_queue,url_local]); |
---|
1626 | } |
---|
1627 | else if(form.elements[i].name.indexOf("offline_forward_")!=-1){ |
---|
1628 | //alert(); |
---|
1629 | this.dbGears.execute("insert into attachments_queue (id_queue,url_attach) values (?,?)",[id_queue,form.elements[i].value]); |
---|
1630 | } |
---|
1631 | } |
---|
1632 | } |
---|
1633 | |
---|
1634 | |
---|
1635 | local_messages.prototype.get_num_msgs_to_send = function() { |
---|
1636 | this.init_local_messages(); |
---|
1637 | |
---|
1638 | var rs = this.dbGears.execute("select count(*) from queue where user=? and sent=0",[account_id]); |
---|
1639 | var to_return = rs.field(0); |
---|
1640 | |
---|
1641 | this.finalize(); |
---|
1642 | return to_return; |
---|
1643 | } |
---|
1644 | |
---|
1645 | local_messages.prototype.set_problem_on_sent = function(rowid_message,msg) { |
---|
1646 | this.init_local_messages(); |
---|
1647 | this.dbGears.execute("update queue set sent = 2 where rowid=?",[rowid_message]); |
---|
1648 | this.dbGears.execute("insert into sent_problems (id_queue,message) values (?,?)"[rowid_message,msg]); |
---|
1649 | this.finalize(); |
---|
1650 | } |
---|
1651 | |
---|
1652 | local_messages.prototype.set_as_sent = function(rowid_message) { |
---|
1653 | this.init_local_messages(); |
---|
1654 | this.dbGears.execute("update queue set sent = 1 where rowid=?",[rowid_message]); |
---|
1655 | this.finalize(); |
---|
1656 | } |
---|
1657 | |
---|
1658 | local_messages.prototype.get_form_msg_to_send = function() { |
---|
1659 | this.init_local_messages(); |
---|
1660 | var rs = this.dbGears.execute('select ffrom,fto,cc,cco,subject,conf_receipt,important,body,rowid from queue where sent=0 and user = ? limit 0,1',[account_id]); |
---|
1661 | if(!rs.isValidRow()) |
---|
1662 | return false; |
---|
1663 | |
---|
1664 | var form = document.createElement('form'); |
---|
1665 | form.method = 'POST'; |
---|
1666 | form.name = 'form_queue_'+rs.field(8); |
---|
1667 | form.style.display = 'none'; |
---|
1668 | form.onsubmit = function(){return false;} |
---|
1669 | if(!is_ie) |
---|
1670 | form.enctype="multipart/form-data"; |
---|
1671 | else |
---|
1672 | form.encoding="multipart/form-data"; |
---|
1673 | |
---|
1674 | var ffrom = document.createElement('TEXTAREA'); |
---|
1675 | ffrom.name = "input_from"; |
---|
1676 | ffrom.value = rs.field(0); |
---|
1677 | form.appendChild(ffrom); |
---|
1678 | |
---|
1679 | var fto = document.createElement('TEXTAREA'); |
---|
1680 | fto.name = "input_to"; |
---|
1681 | fto.value = rs.field(1); |
---|
1682 | form.appendChild(fto); |
---|
1683 | |
---|
1684 | var cc = document.createElement('TEXTAREA'); |
---|
1685 | cc.name = "input_cc"; |
---|
1686 | cc.value = rs.field(2); |
---|
1687 | form.appendChild(cc); |
---|
1688 | |
---|
1689 | var cco = document.createElement('TEXTAREA'); |
---|
1690 | cco.name = "input_cco"; |
---|
1691 | cco.value = rs.field(3); |
---|
1692 | form.appendChild(cco); |
---|
1693 | |
---|
1694 | var subject = document.createElement('TEXTAREA'); |
---|
1695 | subject.name = "input_subject"; |
---|
1696 | subject.value = rs.field(4); |
---|
1697 | form.appendChild(subject); |
---|
1698 | |
---|
1699 | var folder = document.createElement('input'); |
---|
1700 | folder.name='folder'; |
---|
1701 | folder.value=preferences.save_in_folder; |
---|
1702 | form.appendChild(folder); |
---|
1703 | |
---|
1704 | if (rs.field(5) == 1) { |
---|
1705 | var conf_receipt = document.createElement('input'); |
---|
1706 | conf_receipt.type='text'; |
---|
1707 | conf_receipt.name = "input_return_receipt"; |
---|
1708 | conf_receipt.value = 'on'; |
---|
1709 | form.appendChild(conf_receipt); |
---|
1710 | } |
---|
1711 | |
---|
1712 | if (rs.field(6) == 1) { |
---|
1713 | var important = document.createElement('input'); |
---|
1714 | important.type='text'; |
---|
1715 | important.name = "input_important_message"; |
---|
1716 | important.value = 'on'; |
---|
1717 | form.appendChild(important); |
---|
1718 | } |
---|
1719 | |
---|
1720 | var body = document.createElement('TEXTAREA'); |
---|
1721 | body.name = "body"; |
---|
1722 | body.value = rs.field(7); |
---|
1723 | form.appendChild(body); |
---|
1724 | |
---|
1725 | var rowid = document.createElement('input'); |
---|
1726 | rowid.type = 'hidden'; |
---|
1727 | rowid.name = 'rowid'; |
---|
1728 | rowid.value = rs.field(8); |
---|
1729 | form.appendChild(rowid); |
---|
1730 | |
---|
1731 | //Mounting the attachs |
---|
1732 | var divFiles = document.createElement("div"); |
---|
1733 | divFiles.id = 'divFiles_queue_'+rs.field(8); |
---|
1734 | |
---|
1735 | form.appendChild(divFiles); |
---|
1736 | |
---|
1737 | document.getElementById('forms_queue').appendChild(form); |
---|
1738 | |
---|
1739 | var is_local_forward = false; |
---|
1740 | try { |
---|
1741 | |
---|
1742 | var rs_attach = this.dbGears.execute('select url_attach from attachments_queue where id_queue=?', [rs.field(8)]); |
---|
1743 | while (rs_attach.isValidRow()) { |
---|
1744 | if(rs_attach.field(0).indexOf('../tmpLclAtt/')==-1) { |
---|
1745 | tmp_field = addForwardedFile('queue_' + rs.field(8), this.store.getCapturedFileName(rs_attach.field(0)), 'nothing'); |
---|
1746 | } |
---|
1747 | else { |
---|
1748 | var tempNomeArquivo = rs_attach.field(0).split("/"); |
---|
1749 | var nomeArquivo = tempNomeArquivo[tempNomeArquivo.length-1]; |
---|
1750 | nomeArquivo = nomeArquivo.substring(0,nomeArquivo.length - 4); //Anexos no gears sᅵo todos com extensᅵo .php. tenho que tirar a extensᅵo para ficar o nome real do arquivo. |
---|
1751 | is_local_forward = true; |
---|
1752 | tmp_field = addForwardedFile('queue_' + rs.field(8), nomeArquivo, 'nothing'); |
---|
1753 | } |
---|
1754 | fileSubmitter = this.store.createFileSubmitter(); |
---|
1755 | fileSubmitter.setFileInputElement(tmp_field,rs_attach.field(0)); |
---|
1756 | // alert(form.innerHTML); |
---|
1757 | // div.appendChild(tmp_field); |
---|
1758 | rs_attach.next(); |
---|
1759 | } |
---|
1760 | |
---|
1761 | if(is_local_forward) { |
---|
1762 | var is_lcl_fw = document.createElement('input'); |
---|
1763 | is_lcl_fw.type = 'hidden'; |
---|
1764 | is_lcl_fw.name = 'is_local_forward'; |
---|
1765 | is_lcl_fw.value = "1"; |
---|
1766 | form.appendChild(is_lcl_fw); |
---|
1767 | } |
---|
1768 | |
---|
1769 | } |
---|
1770 | catch(exception) { |
---|
1771 | alert(exception); |
---|
1772 | } |
---|
1773 | |
---|
1774 | this.finalize(); |
---|
1775 | return form; |
---|
1776 | } |
---|
1777 | |
---|
1778 | var expresso_local_messages; |
---|
1779 | expresso_local_messages = new local_messages(); |
---|
1780 | //expresso_local_messages.create_objects(); |
---|