]> git.p6c8.net - jirafeau_project.git/blob - lib/functions.js.php
Merge remote-tracking branch 'origin/master'
[jirafeau_project.git] / lib / functions.js.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
5 * Copyright (C) 2015 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 header('Content-Type: text/javascript');
22
23 define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/../');
24 require (JIRAFEAU_ROOT . 'lib/config.original.php');
25 require (JIRAFEAU_ROOT . 'lib/settings.php');
26 require (JIRAFEAU_ROOT . 'lib/functions.php');
27 require (JIRAFEAU_ROOT . 'lib/lang.php');
28 ?>
29
30 function translate (expr)
31 {
32 var lang_array = <?php echo json_lang_generator () ?>;
33 if (lang_array.hasOwnProperty(expr))
34 return lang_array[expr];
35 return expr;
36 }
37
38 function show_link (url, reference, delete_code, crypt_key, date)
39 {
40 // Upload finished
41 document.getElementById('uploading').style.display = 'none';
42 document.getElementById('upload').style.display = 'none';
43 document.getElementById('upload_finished').style.display = '';
44 document.title = 'Jirafeau - 100%';
45
46 // Download page
47 var download_link = url + 'f.php?h=' + reference;
48 var download_link_href = url + 'f.php?h=' + reference;
49 if (crypt_key.length > 0)
50 {
51 download_link += '&amp;k=' + crypt_key;
52 download_link_href += '&k=' + crypt_key;
53 }
54 if (!!document.getElementById('upload_finished_download_page'))
55 {
56 document.getElementById('upload_link').innerHTML = download_link;
57 document.getElementById('upload_link').href = download_link_href;
58 }
59
60 // Email link
61 var filename = document.getElementById('file_select').files[0].name;
62 var b = encodeURIComponent("Download file \"" + filename + "\":") + "%0D";
63 b += encodeURIComponent(download_link_href) + "%0D";
64 if (date)
65 b += "%0D" + encodeURIComponent("This file will be available until " + date) + "%0D";
66 document.getElementById('upload_link_email').href = "mailto:?body=" + b + "&subject=" + encodeURIComponent(filename);
67
68 // Delete link
69 var delete_link = url + 'f.php?h=' + reference + '&amp;d=' + delete_code;
70 var delete_link_href = url + 'f.php?h=' + reference + '&d=' + delete_code;
71 document.getElementById('delete_link').innerHTML = delete_link;
72 document.getElementById('delete_link').href = delete_link_href;
73
74 // Validity date
75 if (date)
76 {
77 document.getElementById('date').innerHTML = date;
78 document.getElementById('validity').style.display = '';
79 }
80 else
81 document.getElementById('validity').style.display = 'none';
82
83 // Preview link (if allowed)
84 if (!!document.getElementById('preview_link'))
85 {
86 document.getElementById('upload_finished_preview').style.display = 'none';
87 var preview_link = url + 'f.php?h=' + reference + '&amp;p=1';
88 var preview_link_href = url + 'f.php?h=' + reference + '&p=1';
89 if (crypt_key.length > 0)
90 {
91 preview_link += '&amp;k=' + crypt_key;
92 preview_link_href += '&k=' + crypt_key;
93 }
94
95 // Test if content can be previewed
96 type = document.getElementById('file_select').files[0].type;
97 if (type.indexOf("image") > -1 ||
98 type.indexOf("audio") > -1 ||
99 type.indexOf("text") > -1 ||
100 type.indexOf("video") > -1)
101 {
102 document.getElementById('preview_link').innerHTML = preview_link;
103 document.getElementById('preview_link').href = preview_link_href;
104 document.getElementById('upload_finished_preview').style.display = '';
105 }
106 }
107
108 // Direct download link
109 var direct_download_link = url + 'f.php?h=' + reference + '&amp;d=1';
110 var direct_download_link_href = url + 'f.php?h=' + reference + '&d=1';
111 if (crypt_key.length > 0)
112 {
113 direct_download_link += '&amp;k=' + crypt_key;
114 direct_download_link_href += '&k=' + crypt_key;
115 }
116 document.getElementById('direct_link').innerHTML = direct_download_link;
117 document.getElementById('direct_link').href = direct_download_link_href;
118
119
120 // Hide preview and direct download link if password is set
121 if (document.getElementById('input_key').value.length > 0)
122 {
123 if (!!document.getElementById('preview_link'))
124 document.getElementById('upload_finished_preview').style.display = 'none';
125 document.getElementById('upload_direct_download').style.display = 'none';
126 }
127 }
128
129 function show_upload_progression (percentage, speed, time_left)
130 {
131 document.getElementById('uploaded_percentage').innerHTML = percentage;
132 document.getElementById('uploaded_speed').innerHTML = speed;
133 document.getElementById('uploaded_time').innerHTML = time_left;
134 document.title = 'Jirafeau - ' + percentage;
135 }
136
137 function hide_upload_progression ()
138 {
139 document.getElementById('uploaded_percentage').style.display = 'none';
140 document.getElementById('uploaded_speed').style.display = 'none';
141 document.getElementById('uploaded_time').style.display = 'none';
142 document.title = 'Jirafeau';
143 }
144
145 function upload_progress (e)
146 {
147 if (e == undefined || e == null || !e.lengthComputable)
148 return;
149
150 // Init time estimation if needed
151 if (upload_time_estimation_total_size == 0)
152 upload_time_estimation_total_size = e.total;
153
154 // Compute percentage
155 var p = Math.round (e.loaded * 100 / e.total);
156 var p_str = ' ';
157 if (p != 100)
158 p_str = p.toString() + '%';
159 // Update estimation speed
160 upload_time_estimation_add(e.loaded);
161 // Get speed string
162 var speed_str = upload_time_estimation_speed_string();
163 speed_str = upload_speed_refresh_limiter(speed_str);
164 // Get time string
165 var time_str = chrono_update(upload_time_estimation_time());
166
167 show_upload_progression (p_str, speed_str, time_str);
168 }
169
170 function control_selected_file_size(max_size, error_str)
171 {
172 f_size = document.getElementById('file_select').files[0].size;
173 if (max_size > 0 && f_size > max_size * 1024 * 1024)
174 {
175 pop_failure(error_str);
176 document.getElementById('send').style.display = 'none';
177 }
178 else
179 {
180 document.getElementById('options').style.display = '';
181 document.getElementById('send').style.display = '';
182 document.getElementById('error_pop').style.display = 'none';
183 document.getElementById('file_select').style.left = 'inherit';
184 document.getElementById('file_select').style.height = 'inherit';
185 document.getElementById('file_select').style.opacity = '1';
186 }
187 }
188
189 function pop_failure (e)
190 {
191 var text = "An error occured";
192 if (typeof e !== 'undefined')
193 text = e;
194 text = "<p>" + text + "</p>";
195 document.getElementById('error_pop').innerHTML = e;
196
197 document.getElementById('uploading').style.display = 'none';
198 document.getElementById('error_pop').style.display = '';
199 document.getElementById('upload').style.display = '';
200 document.getElementById('send').style.display = '';
201 }
202
203 function add_time_string_to_date(d, time)
204 {
205 if(typeof(d) != 'object' || !(d instanceof Date))
206 {
207 return false;
208 }
209
210 if (time == 'minute')
211 {
212 d.setSeconds (d.getSeconds() + 60);
213 return true;
214 }
215 if (time == 'hour')
216 {
217 d.setSeconds (d.getSeconds() + 3600);
218 return true;
219 }
220 if (time == 'day')
221 {
222 d.setSeconds (d.getSeconds() + 86400);
223 return true;
224 }
225 if (time == 'week')
226 {
227 d.setSeconds (d.getSeconds() + 604800);
228 return true;
229 }
230 if (time == 'month')
231 {
232 d.setSeconds (d.getSeconds() + 2419200);
233 return true;
234 }
235 if (time == 'year')
236 {
237 d.setSeconds (d.getSeconds() + 29030400);
238 return true;
239 }
240 return false;
241 }
242
243 function classic_upload (url, file, time, password, one_time, upload_password)
244 {
245 // Delay time estimation init as we can't have file size
246 upload_time_estimation_init(0);
247
248 var req = new XMLHttpRequest ();
249 req.upload.addEventListener ("progress", upload_progress, false);
250 req.addEventListener ("error", pop_failure, false);
251 req.addEventListener ("abort", pop_failure, false);
252 req.onreadystatechange = function ()
253 {
254 if (req.readyState == 4 && req.status == 200)
255 {
256 var res = req.responseText;
257 if (res == "Error")
258 {
259 pop_failure ();
260 return;
261 }
262 res = res.split ("\n");
263 if (time != 'none')
264 {
265 var d = new Date();
266 if(!add_time_string_to_date(d, time))
267 return;
268 show_link (url, res[0], res[1], res[2], d.toString());
269 }
270 else
271 show_link (url, res[0], res[1], res[2]);
272 }
273 }
274 req.open ("POST", url + 'script.php' , true);
275
276 var form = new FormData();
277 form.append ("file", file);
278 if (time)
279 form.append ("time", time);
280 if (password)
281 form.append ("key", password);
282 if (one_time)
283 form.append ("one_time_download", '1');
284 if (upload_password.length > 0)
285 form.append ("upload_password", upload_password);
286
287 req.send (form);
288 }
289
290 function check_html5_file_api ()
291 {
292 return window.File && window.FileReader && window.FileList && window.Blob;
293 }
294
295 var async_global_transfered = 0;
296 var async_global_url = '';
297 var async_global_file;
298 var async_global_ref = '';
299 var async_global_max_size = 0;
300 var async_global_time;
301 var async_global_transfering = 0;
302
303 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
304 {
305 async_global_transfered = 0;
306 async_global_url = url;
307 async_global_file = file;
308 async_global_max_size = max_size;
309 async_global_time = time;
310
311 var req = new XMLHttpRequest ();
312 req.addEventListener ("error", pop_failure, false);
313 req.addEventListener ("abort", pop_failure, false);
314 req.onreadystatechange = function ()
315 {
316 if (req.readyState == 4 && req.status == 200)
317 {
318 var res = req.responseText;
319 if (res == "Error")
320 {
321 pop_failure ();
322 return;
323 }
324 res = res.split ("\n");
325 async_global_ref = res[0];
326 var code = res[1];
327 async_upload_push (code);
328 }
329 }
330 req.open ("POST", async_global_url + 'script.php?init_async' , true);
331
332 var form = new FormData();
333 form.append ("filename", async_global_file.name);
334 form.append ("type", async_global_file.type);
335 if (time)
336 form.append ("time", time);
337 if (password)
338 form.append ("key", password);
339 if (one_time)
340 form.append ("one_time_download", '1');
341 if (upload_password.length > 0)
342 form.append ("upload_password", upload_password);
343
344 // Start time estimation
345 upload_time_estimation_init(async_global_file.size);
346
347 req.send (form);
348 }
349
350 function async_upload_progress (e)
351 {
352 if (e == undefined || e == null || !e.lengthComputable && async_global_file.size != 0)
353 return;
354
355 // Compute percentage
356 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
357 var p_str = ' ';
358 if (p != 100)
359 p_str = p.toString() + '%';
360 // Update estimation speed
361 upload_time_estimation_add(e.loaded + async_global_transfered);
362 // Get speed string
363 var speed_str = upload_time_estimation_speed_string();
364 speed_str = upload_speed_refresh_limiter(speed_str);
365 // Get time string
366 var time_str = chrono_update(upload_time_estimation_time());
367
368 show_upload_progression (p_str, speed_str, time_str);
369 }
370
371 function async_upload_push (code)
372 {
373 if (async_global_transfered == async_global_file.size)
374 {
375 hide_upload_progression ();
376 async_upload_end (code);
377 return;
378 }
379 var req = new XMLHttpRequest ();
380 req.upload.addEventListener ("progress", async_upload_progress, false);
381 req.addEventListener ("error", pop_failure, false);
382 req.addEventListener ("abort", pop_failure, false);
383 req.onreadystatechange = function ()
384 {
385 if (req.readyState == 4 && req.status == 200)
386 {
387 var res = req.responseText;
388 if (res == "Error")
389 {
390 pop_failure ();
391 return;
392 }
393 res = res.split ("\n");
394 var code = res[0]
395 async_global_transfered = async_global_transfering;
396 async_upload_push (code);
397 }
398 }
399 req.open ("POST", async_global_url + 'script.php?push_async' , true);
400
401 var chunk_size = parseInt (async_global_max_size * 0.50);
402 var start = async_global_transfered;
403 var end = start + chunk_size;
404 if (end >= async_global_file.size)
405 end = async_global_file.size;
406 var blob = async_global_file.slice (start, end);
407 async_global_transfering = end;
408
409 var form = new FormData();
410 form.append ("ref", async_global_ref);
411 form.append ("data", blob);
412 form.append ("code", code);
413 req.send (form);
414 }
415
416 function async_upload_end (code)
417 {
418 var req = new XMLHttpRequest ();
419 req.addEventListener ("error", pop_failure, false);
420 req.addEventListener ("abort", pop_failure, false);
421 req.onreadystatechange = function ()
422 {
423 if (req.readyState == 4 && req.status == 200)
424 {
425 var res = req.responseText;
426 if (res == "Error")
427 {
428 pop_failure ();
429 return;
430 }
431 res = res.split ("\n");
432 if (async_global_time != 'none')
433 {
434 var d = new Date();
435 if(!add_time_string_to_date(d, async_global_time))
436 return;
437 show_link (async_global_url, res[0], res[1], res[2], d.toString());
438 }
439 else
440 show_link (async_global_url, res[0], res[1], res[2]);
441 }
442 }
443 req.open ("POST", async_global_url + 'script.php?end_async' , true);
444
445 var form = new FormData();
446 form.append ("ref", async_global_ref);
447 form.append ("code", code);
448 req.send (form);
449 }
450
451 function upload (url, max_size)
452 {
453 if (check_html5_file_api ()
454 && document.getElementById('file_select').files[0].size >= max_size)
455 {
456 async_upload_start (url,
457 max_size,
458 document.getElementById('file_select').files[0],
459 document.getElementById('select_time').value,
460 document.getElementById('input_key').value,
461 document.getElementById('one_time_download').checked,
462 document.getElementById('upload_password').value
463 );
464 }
465 else
466 {
467 classic_upload (url,
468 document.getElementById('file_select').files[0],
469 document.getElementById('select_time').value,
470 document.getElementById('input_key').value,
471 document.getElementById('one_time_download').checked,
472 document.getElementById('upload_password').value
473 );
474 }
475 }
476
477 var upload_time_estimation_total_size = 42;
478 var upload_time_estimation_transfered_size = 42;
479 var upload_time_estimation_transfered_date = 42;
480 var upload_time_estimation_moving_average_speed = 42;
481
482 function upload_time_estimation_init(total_size)
483 {
484 upload_time_estimation_total_size = total_size;
485 upload_time_estimation_transfered_size = 0;
486 upload_time_estimation_moving_average_speed = 0;
487 var d = new Date();
488 upload_time_estimation_transfered_date = d.getTime();
489 }
490
491 function upload_time_estimation_add(total_transfered_size)
492 {
493 // Let's compute the current speed
494 var d = new Date();
495 var speed = upload_time_estimation_moving_average_speed;
496 if (d.getTime() - upload_time_estimation_transfered_date != 0)
497 speed = (total_transfered_size - upload_time_estimation_transfered_size)
498 / (d.getTime() - upload_time_estimation_transfered_date);
499 // Let's compute moving average speed on 30 values
500 var m = (upload_time_estimation_moving_average_speed * 29 + speed) / 30;
501 // Update global values
502 upload_time_estimation_transfered_size = total_transfered_size;
503 upload_time_estimation_transfered_date = d.getTime();
504 upload_time_estimation_moving_average_speed = m;
505 }
506
507 function upload_time_estimation_speed_string()
508 {
509 // speed ms -> s
510 var s = upload_time_estimation_moving_average_speed * 1000;
511 var res = 0;
512 var scale = '';
513 if (s <= 1000)
514 {
515 res = s.toString();
516 scale = "o/s";
517 }
518 else if (s < 1000000)
519 {
520 res = Math.floor(s/100) / 10;
521 scale = "Ko/s";
522 }
523 else
524 {
525 res = Math.floor(s/100000) / 10;
526 scale = "Mo/s";
527 }
528 if (res == 0)
529 return '';
530 return res.toString() + ' ' + scale;
531 }
532
533 function milliseconds_to_time_string (milliseconds)
534 {
535 function numberEnding (number) {
536 return (number > 1) ? 's' : '';
537 }
538
539 var temp = Math.floor(milliseconds / 1000);
540 var years = Math.floor(temp / 31536000);
541 if (years) {
542 return years + ' ' + translate ('year') + numberEnding(years);
543 }
544 var days = Math.floor((temp %= 31536000) / 86400);
545 if (days) {
546 return days + ' ' + translate ('day') + numberEnding(days);
547 }
548 var hours = Math.floor((temp %= 86400) / 3600);
549 if (hours) {
550 return hours + ' ' + translate ('hour') + numberEnding(hours);
551 }
552 var minutes = Math.floor((temp %= 3600) / 60);
553 if (minutes) {
554 return minutes + ' ' + translate ('minute') + numberEnding(minutes);
555 }
556 var seconds = temp % 60;
557 if (seconds) {
558 return seconds + ' ' + translate ('second') + numberEnding(seconds);
559 }
560 return translate ('less than a second');
561 }
562
563 function upload_time_estimation_time()
564 {
565 // Estimate remaining time
566 if (upload_time_estimation_moving_average_speed == 0)
567 return 0;
568 return (upload_time_estimation_total_size - upload_time_estimation_transfered_size)
569 / upload_time_estimation_moving_average_speed;
570 }
571
572 var chrono_last_update = 0;
573 var chrono_time_ms = 0;
574 var chrono_time_ms_last_update = 0;
575 function chrono_update(time_ms)
576 {
577 var d = new Date();
578 var chrono = 0;
579 // Don't update too often
580 if (d.getTime() - chrono_last_update < 3000 &&
581 chrono_time_ms_last_update > 0)
582 chrono = chrono_time_ms;
583 else
584 {
585 chrono_last_update = d.getTime();
586 chrono_time_ms = time_ms;
587 chrono = time_ms;
588 chrono_time_ms_last_update = d.getTime();
589 }
590
591 // Adjust chrono for smooth estimation
592 chrono = chrono - (d.getTime() - chrono_time_ms_last_update);
593
594 // Let's update chronometer
595 var time_str = '';
596 if (chrono > 0)
597 time_str = milliseconds_to_time_string (chrono);
598 return time_str;
599 }
600
601 var upload_speed_refresh_limiter_last_update = 0;
602 var upload_speed_refresh_limiter_last_value = '';
603 function upload_speed_refresh_limiter(speed_str)
604 {
605 var d = new Date();
606 if (d.getTime() - upload_speed_refresh_limiter_last_update > 1500)
607 {
608 upload_speed_refresh_limiter_last_value = speed_str;
609 upload_speed_refresh_limiter_last_update = d.getTime();
610 }
611 return upload_speed_refresh_limiter_last_value;
612 }

patrick-canterino.de