]> git.p6c8.net - jirafeau_project.git/blob - lib/functions.js.php
Jirafeau version 1.2.0
[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 == 'quarter')
236 {
237 d.setSeconds (d.getSeconds() + 7257600);
238 return true;
239 }
240 if (time == 'year')
241 {
242 d.setSeconds (d.getSeconds() + 29030400);
243 return true;
244 }
245 return false;
246 }
247
248 function classic_upload (url, file, time, password, one_time, upload_password)
249 {
250 // Delay time estimation init as we can't have file size
251 upload_time_estimation_init(0);
252
253 var req = new XMLHttpRequest ();
254 req.upload.addEventListener ("progress", upload_progress, false);
255 req.addEventListener ("error", pop_failure, false);
256 req.addEventListener ("abort", pop_failure, false);
257 req.onreadystatechange = function ()
258 {
259 if (req.readyState == 4 && req.status == 200)
260 {
261 var res = req.responseText;
262 if (res == "Error")
263 {
264 pop_failure ();
265 return;
266 }
267 res = res.split ("\n");
268 if (time != 'none')
269 {
270 var d = new Date();
271 if(!add_time_string_to_date(d, time))
272 return;
273 show_link (url, res[0], res[1], res[2], d.toString());
274 }
275 else
276 show_link (url, res[0], res[1], res[2]);
277 }
278 }
279 req.open ("POST", url + 'script.php' , true);
280
281 var form = new FormData();
282 form.append ("file", file);
283 if (time)
284 form.append ("time", time);
285 if (password)
286 form.append ("key", password);
287 if (one_time)
288 form.append ("one_time_download", '1');
289 if (upload_password.length > 0)
290 form.append ("upload_password", upload_password);
291
292 req.send (form);
293 }
294
295 function check_html5_file_api ()
296 {
297 return window.File && window.FileReader && window.FileList && window.Blob;
298 }
299
300 var async_global_transfered = 0;
301 var async_global_url = '';
302 var async_global_file;
303 var async_global_ref = '';
304 var async_global_max_size = 0;
305 var async_global_time;
306 var async_global_transfering = 0;
307
308 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
309 {
310 async_global_transfered = 0;
311 async_global_url = url;
312 async_global_file = file;
313 async_global_max_size = max_size;
314 async_global_time = time;
315
316 var req = new XMLHttpRequest ();
317 req.addEventListener ("error", pop_failure, false);
318 req.addEventListener ("abort", pop_failure, false);
319 req.onreadystatechange = function ()
320 {
321 if (req.readyState == 4 && req.status == 200)
322 {
323 var res = req.responseText;
324 if (res == "Error")
325 {
326 pop_failure ();
327 return;
328 }
329 res = res.split ("\n");
330 async_global_ref = res[0];
331 var code = res[1];
332 async_upload_push (code);
333 }
334 }
335 req.open ("POST", async_global_url + 'script.php?init_async' , true);
336
337 var form = new FormData();
338 form.append ("filename", async_global_file.name);
339 form.append ("type", async_global_file.type);
340 if (time)
341 form.append ("time", time);
342 if (password)
343 form.append ("key", password);
344 if (one_time)
345 form.append ("one_time_download", '1');
346 if (upload_password.length > 0)
347 form.append ("upload_password", upload_password);
348
349 // Start time estimation
350 upload_time_estimation_init(async_global_file.size);
351
352 req.send (form);
353 }
354
355 function async_upload_progress (e)
356 {
357 if (e == undefined || e == null || !e.lengthComputable && async_global_file.size != 0)
358 return;
359
360 // Compute percentage
361 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
362 var p_str = ' ';
363 if (p != 100)
364 p_str = p.toString() + '%';
365 // Update estimation speed
366 upload_time_estimation_add(e.loaded + async_global_transfered);
367 // Get speed string
368 var speed_str = upload_time_estimation_speed_string();
369 speed_str = upload_speed_refresh_limiter(speed_str);
370 // Get time string
371 var time_str = chrono_update(upload_time_estimation_time());
372
373 show_upload_progression (p_str, speed_str, time_str);
374 }
375
376 function async_upload_push (code)
377 {
378 if (async_global_transfered == async_global_file.size)
379 {
380 hide_upload_progression ();
381 async_upload_end (code);
382 return;
383 }
384 var req = new XMLHttpRequest ();
385 req.upload.addEventListener ("progress", async_upload_progress, false);
386 req.addEventListener ("error", pop_failure, false);
387 req.addEventListener ("abort", pop_failure, false);
388 req.onreadystatechange = function ()
389 {
390 if (req.readyState == 4 && req.status == 200)
391 {
392 var res = req.responseText;
393 if (res == "Error")
394 {
395 pop_failure ();
396 return;
397 }
398 res = res.split ("\n");
399 var code = res[0]
400 async_global_transfered = async_global_transfering;
401 async_upload_push (code);
402 }
403 }
404 req.open ("POST", async_global_url + 'script.php?push_async' , true);
405
406 var chunk_size = parseInt (async_global_max_size * 0.50);
407 var start = async_global_transfered;
408 var end = start + chunk_size;
409 if (end >= async_global_file.size)
410 end = async_global_file.size;
411 var blob = async_global_file.slice (start, end);
412 async_global_transfering = end;
413
414 var form = new FormData();
415 form.append ("ref", async_global_ref);
416 form.append ("data", blob);
417 form.append ("code", code);
418 req.send (form);
419 }
420
421 function async_upload_end (code)
422 {
423 var req = new XMLHttpRequest ();
424 req.addEventListener ("error", pop_failure, false);
425 req.addEventListener ("abort", pop_failure, false);
426 req.onreadystatechange = function ()
427 {
428 if (req.readyState == 4 && req.status == 200)
429 {
430 var res = req.responseText;
431 if (res == "Error")
432 {
433 pop_failure ();
434 return;
435 }
436 res = res.split ("\n");
437 if (async_global_time != 'none')
438 {
439 var d = new Date();
440 if(!add_time_string_to_date(d, async_global_time))
441 return;
442 show_link (async_global_url, res[0], res[1], res[2], d.toString());
443 }
444 else
445 show_link (async_global_url, res[0], res[1], res[2]);
446 }
447 }
448 req.open ("POST", async_global_url + 'script.php?end_async' , true);
449
450 var form = new FormData();
451 form.append ("ref", async_global_ref);
452 form.append ("code", code);
453 req.send (form);
454 }
455
456 function upload (url, max_size)
457 {
458 if (check_html5_file_api ()
459 && document.getElementById('file_select').files[0].size >= max_size)
460 {
461 async_upload_start (url,
462 max_size,
463 document.getElementById('file_select').files[0],
464 document.getElementById('select_time').value,
465 document.getElementById('input_key').value,
466 document.getElementById('one_time_download').checked,
467 document.getElementById('upload_password').value
468 );
469 }
470 else
471 {
472 classic_upload (url,
473 document.getElementById('file_select').files[0],
474 document.getElementById('select_time').value,
475 document.getElementById('input_key').value,
476 document.getElementById('one_time_download').checked,
477 document.getElementById('upload_password').value
478 );
479 }
480 }
481
482 var upload_time_estimation_total_size = 42;
483 var upload_time_estimation_transfered_size = 42;
484 var upload_time_estimation_transfered_date = 42;
485 var upload_time_estimation_moving_average_speed = 42;
486
487 function upload_time_estimation_init(total_size)
488 {
489 upload_time_estimation_total_size = total_size;
490 upload_time_estimation_transfered_size = 0;
491 upload_time_estimation_moving_average_speed = 0;
492 var d = new Date();
493 upload_time_estimation_transfered_date = d.getTime();
494 }
495
496 function upload_time_estimation_add(total_transfered_size)
497 {
498 // Let's compute the current speed
499 var d = new Date();
500 var speed = upload_time_estimation_moving_average_speed;
501 if (d.getTime() - upload_time_estimation_transfered_date != 0)
502 speed = (total_transfered_size - upload_time_estimation_transfered_size)
503 / (d.getTime() - upload_time_estimation_transfered_date);
504 // Let's compute moving average speed on 30 values
505 var m = (upload_time_estimation_moving_average_speed * 29 + speed) / 30;
506 // Update global values
507 upload_time_estimation_transfered_size = total_transfered_size;
508 upload_time_estimation_transfered_date = d.getTime();
509 upload_time_estimation_moving_average_speed = m;
510 }
511
512 function upload_time_estimation_speed_string()
513 {
514 // speed ms -> s
515 var s = upload_time_estimation_moving_average_speed * 1000;
516 var res = 0;
517 var scale = '';
518 if (s <= 1000)
519 {
520 res = s.toString();
521 scale = "o/s";
522 }
523 else if (s < 1000000)
524 {
525 res = Math.floor(s/100) / 10;
526 scale = "Ko/s";
527 }
528 else
529 {
530 res = Math.floor(s/100000) / 10;
531 scale = "Mo/s";
532 }
533 if (res == 0)
534 return '';
535 return res.toString() + ' ' + scale;
536 }
537
538 function milliseconds_to_time_string (milliseconds)
539 {
540 function numberEnding (number) {
541 return (number > 1) ? 's' : '';
542 }
543
544 var temp = Math.floor(milliseconds / 1000);
545 var years = Math.floor(temp / 31536000);
546 if (years) {
547 return years + ' ' + translate ('year') + numberEnding(years);
548 }
549 var days = Math.floor((temp %= 31536000) / 86400);
550 if (days) {
551 return days + ' ' + translate ('day') + numberEnding(days);
552 }
553 var hours = Math.floor((temp %= 86400) / 3600);
554 if (hours) {
555 return hours + ' ' + translate ('hour') + numberEnding(hours);
556 }
557 var minutes = Math.floor((temp %= 3600) / 60);
558 if (minutes) {
559 return minutes + ' ' + translate ('minute') + numberEnding(minutes);
560 }
561 var seconds = temp % 60;
562 if (seconds) {
563 return seconds + ' ' + translate ('second') + numberEnding(seconds);
564 }
565 return translate ('less than a second');
566 }
567
568 function upload_time_estimation_time()
569 {
570 // Estimate remaining time
571 if (upload_time_estimation_moving_average_speed == 0)
572 return 0;
573 return (upload_time_estimation_total_size - upload_time_estimation_transfered_size)
574 / upload_time_estimation_moving_average_speed;
575 }
576
577 var chrono_last_update = 0;
578 var chrono_time_ms = 0;
579 var chrono_time_ms_last_update = 0;
580 function chrono_update(time_ms)
581 {
582 var d = new Date();
583 var chrono = 0;
584 // Don't update too often
585 if (d.getTime() - chrono_last_update < 3000 &&
586 chrono_time_ms_last_update > 0)
587 chrono = chrono_time_ms;
588 else
589 {
590 chrono_last_update = d.getTime();
591 chrono_time_ms = time_ms;
592 chrono = time_ms;
593 chrono_time_ms_last_update = d.getTime();
594 }
595
596 // Adjust chrono for smooth estimation
597 chrono = chrono - (d.getTime() - chrono_time_ms_last_update);
598
599 // Let's update chronometer
600 var time_str = '';
601 if (chrono > 0)
602 time_str = milliseconds_to_time_string (chrono);
603 return time_str;
604 }
605
606 var upload_speed_refresh_limiter_last_update = 0;
607 var upload_speed_refresh_limiter_last_value = '';
608 function upload_speed_refresh_limiter(speed_str)
609 {
610 var d = new Date();
611 if (d.getTime() - upload_speed_refresh_limiter_last_update > 1500)
612 {
613 upload_speed_refresh_limiter_last_value = speed_str;
614 upload_speed_refresh_limiter_last_update = d.getTime();
615 }
616 return upload_speed_refresh_limiter_last_value;
617 }

patrick-canterino.de