]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.php
c2cad4653d209eab87a083dff6e359f90644f9d1
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
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.
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.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Transform a string in a path by seperating each letters by a '/'.
23 * @return path finishing with a '/'
29 for ($i = 0; $i < strlen ($s); $i++
)
35 * Convert base 16 to base 64
36 * @returns A string based on 64 characters (0-9, a-z, A-Z, "-" and "_")
41 $m = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
42 $hex2bin = array ('0000', # 0
61 # Convert long hex string to bin.
62 $size = strlen ($num);
63 for ($i = 0; $i < $size; $i++
)
64 $b .= $hex2bin{hexdec ($num{$i})};
65 # Convert long bin to base 64.
67 for ($i = $size - 6; $i >= 0; $i -= 6)
68 $o = $m{bindec (substr ($b, $i, 6))} . $o;
69 # Some few bits remaining ?
70 if ($i < 0 && $i > -6)
71 $o = $m{bindec (substr ($b, 0, $i +
6))} . $o;
76 * Generate a random code.
77 * @param $l code length
78 * @return random code.
81 jirafeau_gen_random ($l)
87 for ($i = 0; $i < $l; $i++
)
88 $code .= dechex (rand (0, 15));
94 if ( isset($_SERVER['HTTPS']) ) {
95 if ( 'on' == strtolower($_SERVER['HTTPS']) )
97 if ( '1' == $_SERVER['HTTPS'] )
99 } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
101 } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
102 if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
109 jirafeau_human_size ($octets)
111 $u = array ('B', 'KB', 'MB', 'GB', 'TB');
112 $o = max ($octets, 0);
113 $p = min (floor (($o ?
log ($o) : 0) / log (1024)), count ($u) - 1);
114 $o /= pow (1024, $p);
115 return round ($o, 1) . $u[$p];
119 jirafeau_clean_rm_link ($link)
122 if (file_exists (VAR_LINKS
. $p . $link))
123 unlink (VAR_LINKS
. $p . $link);
124 $parse = VAR_LINKS
. $p;
126 while (file_exists ($parse)
127 && ($scan = scandir ($parse))
128 && count ($scan) == 2 // '.' and '..' folders => empty.
129 && basename ($parse) != basename (VAR_LINKS
))
132 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
137 jirafeau_clean_rm_file ($md5)
140 $f = VAR_FILES
. $p . $md5;
141 if (file_exists ($f) && is_file ($f))
143 if (file_exists ($f . '_count') && is_file ($f . '_count'))
144 unlink ($f . '_count');
145 $parse = VAR_FILES
. $p;
147 while (file_exists ($parse)
148 && ($scan = scandir ($parse))
149 && count ($scan) == 2 // '.' and '..' folders => empty.
150 && basename ($parse) != basename (VAR_FILES
))
153 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
158 * transforms a php.ini string representing a value in an integer
159 * @param $value the value from php.ini
160 * @returns an integer for this value
162 function jirafeau_ini_to_bytes ($value)
164 $modifier = substr ($value, -1);
165 $bytes = substr ($value, 0, -1);
166 switch (strtoupper ($modifier))
185 * gets the maximum upload size according to php.ini
186 * @returns the maximum upload size in bytes
189 jirafeau_get_max_upload_size_bytes ()
191 return min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
192 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
196 * gets the maximum upload size according to php.ini
197 * @returns the maximum upload size string
200 jirafeau_get_max_upload_size ()
202 return jirafeau_human_size(
203 min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
204 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize'))));
208 * gets a string explaining the error
209 * @param $code the error code
210 * @returns a string explaining the error
213 jirafeau_upload_errstr ($code)
217 case UPLOAD_ERR_INI_SIZE
:
218 case UPLOAD_ERR_FORM_SIZE
:
219 return t('Your file exceeds the maximum authorized file size. ');
222 case UPLOAD_ERR_PARTIAL
:
223 case UPLOAD_ERR_NO_FILE
:
226 ('Your file was not uploaded correctly. You may succeed in retrying. ');
229 case UPLOAD_ERR_NO_TMP_DIR
:
230 case UPLOAD_ERR_CANT_WRITE
:
231 case UPLOAD_ERR_EXTENSION
:
232 return t('Internal error. You may not succeed in retrying. ');
238 return t('Unknown error. ');
241 /** Remove link and it's file
242 * @param $link the link's name (hash)
246 jirafeau_delete_link ($link)
248 $l = jirafeau_get_link ($link);
252 jirafeau_clean_rm_link ($link);
258 if (file_exists (VAR_FILES
. $p . $md5. '_count'))
260 $content = file (VAR_FILES
. $p . $md5. '_count');
261 $counter = trim ($content[0]);
267 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
268 fwrite ($handle, $counter);
273 jirafeau_clean_rm_file ($md5);
277 * Delete a file and it's links.
280 jirafeau_delete_file ($md5)
283 /* Get all links files. */
284 $stack = array (VAR_LINKS
);
285 while (($d = array_shift ($stack)) && $d != NULL)
289 foreach ($dir as $node)
291 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
292 preg_match ('/\.tmp/i', "$node"))
295 if (is_dir ($d . $node))
297 /* Push new found directory. */
298 $stack[] = $d . $node . '/';
300 elseif (is_file ($d . $node))
302 /* Read link informations. */
303 $l = jirafeau_get_link (basename ($node));
306 if ($l['md5'] == $md5)
309 jirafeau_delete_link ($node);
314 jirafeau_clean_rm_file ($md5);
319 * handles an uploaded file
320 * @param $file the file struct given by $_FILE[]
321 * @param $one_time_download is the file a one time download ?
322 * @param $key if not empty, protect the file with this key
323 * @param $time the time of validity of the file
324 * @param $ip uploader's ip
325 * @param $crypt boolean asking to crypt or not
326 * @param $link_name_length size of the link name
327 * @returns an array containing some information
328 * 'error' => information on possible errors
329 * 'link' => the link name of the uploaded file
330 * 'delete_link' => the link code to delete file
333 jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length)
335 if (empty ($file['tmp_name']) ||
!is_uploaded_file ($file['tmp_name']))
339 array ('has_error' => true,
340 'why' => jirafeau_upload_errstr ($file['error'])),
342 'delete_link' => ''));
345 /* array representing no error */
346 $noerr = array ('has_error' => false, 'why' => '');
348 /* Crypt file if option is enabled. */
351 if ($crypt == true && !(extension_loaded('mcrypt') == true))
352 error_log ("PHP extension mcrypt not loaded, won't encrypt in Jirafeau");
353 if ($crypt == true && extension_loaded('mcrypt') == true)
355 $crypt_key = jirafeau_encrypt_file ($file['tmp_name'], $file['tmp_name']);
356 if (strlen($crypt_key) > 0)
360 /* file informations */
361 $md5 = md5_file ($file['tmp_name']);
362 $name = str_replace (NL
, '', trim ($file['name']));
363 $mime_type = $file['type'];
364 $size = $file['size'];
366 /* does file already exist ? */
369 if (file_exists (VAR_FILES
. $p . $md5))
371 $rc = unlink ($file['tmp_name']);
373 elseif ((file_exists (VAR_FILES
. $p) || @mkdir
(VAR_FILES
. $p, 0755, true))
374 && move_uploaded_file ($file['tmp_name'], VAR_FILES
. $p . $md5))
382 array ('has_error' => true,
383 'why' => t('Internal error during file creation.')),
385 'delete_link' => ''));
388 /* Increment or create count file. */
390 if (file_exists (VAR_FILES
. $p . $md5 . '_count'))
392 $content = file (VAR_FILES
. $p . $md5. '_count');
393 $counter = trim ($content[0]);
396 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
397 fwrite ($handle, $counter);
400 /* Create delete code. */
401 $delete_link_code = jirafeau_gen_random (5);
403 /* md5 password or empty. */
406 $password = md5 ($key);
408 /* create link file */
409 $link_tmp_name = VAR_LINKS
. $md5 . rand (0, 10000) . '.tmp';
410 $handle = fopen ($link_tmp_name, 'w');
412 $name . NL
. $mime_type . NL
. $size . NL
. $password . NL
. $time .
413 NL
. $md5. NL
. ($one_time_download ?
'O' : 'R') . NL
. date ('U') .
414 NL
. $ip . NL
. $delete_link_code . NL
. ($crypted ?
'C' : 'O'));
416 $md5_link = substr(base_16_to_64 (md5_file ($link_tmp_name)), 0, $link_name_length);
417 $l = s2p ("$md5_link");
418 if (!@mkdir
(VAR_LINKS
. $l, 0755, true) ||
419 !rename ($link_tmp_name, VAR_LINKS
. $l . $md5_link))
421 if (file_exists ($link_tmp_name))
422 unlink ($link_tmp_name);
427 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
428 fwrite ($handle, $counter);
433 jirafeau_clean_rm_file ($md5_link);
437 array ('has_error' => true,
438 'why' => t('Internal error during file creation. ')),
440 'delete_link' => ''));
442 return (array ('error' => $noerr,
444 'delete_link' => $delete_link_code,
445 'crypt_key' => $crypt_key));
449 * Tells if a mime-type is viewable in a browser
450 * @param $mime the mime type
451 * @returns a boolean telling if a mime type is viewable
454 jirafeau_is_viewable ($mime)
458 /* Actually, verify if mime-type is an image or a text. */
459 $viewable = array ('image', 'text', 'video', 'audio');
460 $decomposed = explode ('/', $mime);
461 return in_array ($decomposed[0], $viewable);
466 // Error handling functions.
467 //! Global array that contains all registered errors.
468 $error_list = array ();
471 * Adds an error to the list of errors.
472 * @param $title the error's title
473 * @param $description is a human-friendly description of the problem.
476 add_error ($title, $description)
479 $error_list[] = '<p>' . $title. '<br />' . $description. '</p>';
483 * Informs whether any error has been registered yet.
484 * @return true if there are errors.
490 return !empty ($error_list);
494 * Displays all the errors.
502 echo '<div class="error">';
503 foreach ($error_list as $error)
511 function check_errors ($cfg)
513 if (file_exists (JIRAFEAU_ROOT
. 'install.php')
514 && !($cfg['installation_done'] === true))
516 header('Location: install.php');
520 /* check if the destination dirs are writable */
521 $writable = is_writable (VAR_FILES
) && is_writable (VAR_LINKS
);
523 /* Checking for errors. */
524 if (!is_writable (VAR_FILES
))
525 add_error (t('The file directory is not writable!'), VAR_FILES
);
527 if (!is_writable (VAR_LINKS
))
528 add_error (t('The link directory is not writable!'), VAR_LINKS
);
530 if (!is_writable (VAR_ASYNC
))
531 add_error (t('The async directory is not writable!'), VAR_ASYNC
);
535 * Read link informations
536 * @return array containing informations.
539 jirafeau_get_link ($hash)
542 $link = VAR_LINKS
. s2p ("$hash") . $hash;
544 if (!file_exists ($link))
548 $out['file_name'] = trim ($c[0]);
549 $out['mime_type'] = trim ($c[1]);
550 $out['file_size'] = trim ($c[2]);
551 $out['key'] = trim ($c[3], NL
);
552 $out['time'] = trim ($c[4]);
553 $out['md5'] = trim ($c[5]);
554 $out['onetime'] = trim ($c[6]);
555 $out['upload_date'] = trim ($c[7]);
556 $out['ip'] = trim ($c[8]);
557 $out['link_code'] = trim ($c[9]);
558 if (trim ($c[10]) == 'C')
559 $out['crypted'] = true;
561 $out['crypted'] = false;
567 * List files in admin interface.
570 jirafeau_admin_list ($name, $file_hash, $link_hash)
572 echo '<fieldset><legend>';
574 echo t('Filename') . ": $name ";
575 if (!empty ($file_hash))
576 echo t('file') . ": $file_hash ";
577 if (!empty ($link_hash))
578 echo t('link') . ": $link_hash ";
579 if (empty ($name) && empty ($file_hash) && empty ($link_hash))
580 echo t('List all files');
584 echo '<td>' . t('Filename') . '</td>';
585 echo '<td>' . t('Type') . '</td>';
586 echo '<td>' . t('Size') . '</td>';
587 echo '<td>' . t('Expire') . '</td>';
588 echo '<td>' . t('Onetime') . '</td>';
589 echo '<td>' . t('Upload date') . '</td>';
590 echo '<td>' . t('Origin') . '</td>';
591 echo '<td>' . t('Action') . '</td>';
594 /* Get all links files. */
595 $stack = array (VAR_LINKS
);
596 while (($d = array_shift ($stack)) && $d != NULL)
599 foreach ($dir as $node)
601 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
602 preg_match ('/\.tmp/i', "$node"))
604 if (is_dir ($d . $node))
606 /* Push new found directory. */
607 $stack[] = $d . $node . '/';
609 elseif (is_file ($d . $node))
611 /* Read link informations. */
612 $l = jirafeau_get_link ($node);
617 if (!empty ($name) && !preg_match ("/$name/i", $l['file_name']))
619 if (!empty ($file_hash) && $file_hash != $l['md5'])
621 if (!empty ($link_hash) && $link_hash != $node)
623 /* Print link informations. */
626 '<form action = "admin.php" method = "post">' .
627 '<input type = "hidden" name = "action" value = "download"/>' .
628 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
629 '<input type = "submit" value = "' . $l['file_name'] . '" />' .
632 echo '<td>' . $l['mime_type'] . '</td>';
633 echo '<td>' . jirafeau_human_size ($l['file_size']) . '</td>';
634 echo '<td>' . ($l['time'] == -1 ?
'' : strftime ('%c', $l['time'])) .
636 echo '<td>' . $l['onetime'] . '</td>';
637 echo '<td>' . strftime ('%c', $l['upload_date']) . '</td>';
638 echo '<td>' . $l['ip'] . '</td>';
640 '<form action = "admin.php" method = "post">' .
641 '<input type = "hidden" name = "action" value = "delete_link"/>' .
642 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
643 '<input type = "submit" value = "' . t('Del link') . '" />' .
645 '<form action = "admin.php" method = "post">' .
646 '<input type = "hidden" name = "action" value = "delete_file"/>' .
647 '<input type = "hidden" name = "md5" value = "' . $l['md5'] . '"/>' .
648 '<input type = "submit" value = "' . t('Del file and links') . '" />' .
655 echo '</table></fieldset>';
659 * Clean expired files.
660 * @return number of cleaned files.
663 jirafeau_admin_clean ()
666 /* Get all links files. */
667 $stack = array (VAR_LINKS
);
668 while (($d = array_shift ($stack)) && $d != NULL)
672 foreach ($dir as $node)
674 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
675 preg_match ('/\.tmp/i', "$node"))
678 if (is_dir ($d . $node))
680 /* Push new found directory. */
681 $stack[] = $d . $node . '/';
683 elseif (is_file ($d . $node))
685 /* Read link informations. */
686 $l = jirafeau_get_link (basename ($node));
689 $p = s2p ($l['md5']);
690 if ($l['time'] > 0 && $l['time'] < time () ||
// expired
691 !file_exists (VAR_FILES
. $p . $l['md5']) ||
// invalid
692 !file_exists (VAR_FILES
. $p . $l['md5'] . '_count')) // invalid
694 jirafeau_delete_link ($node);
705 * Clean old async transferts.
706 * @return number of cleaned files.
709 jirafeau_admin_clean_async ()
712 /* Get all links files. */
713 $stack = array (VAR_ASYNC
);
714 while (($d = array_shift ($stack)) && $d != NULL)
718 foreach ($dir as $node)
720 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
721 preg_match ('/\.tmp/i', "$node"))
724 if (is_dir ($d . $node))
726 /* Push new found directory. */
727 $stack[] = $d . $node . '/';
729 elseif (is_file ($d . $node))
731 /* Read async informations. */
732 $a = jirafeau_get_async_ref (basename ($node));
735 /* Delete transferts older than 1 hour. */
736 if (date ('U') - $a['last_edited'] > 3600)
738 jirafeau_async_delete (basename ($node));
747 * Read async transfert informations
748 * @return array containing informations.
751 jirafeau_get_async_ref ($ref)
754 $refinfos = VAR_ASYNC
. s2p ("$ref") . "$ref";
756 if (!file_exists ($refinfos))
759 $c = file ($refinfos);
760 $out['file_name'] = trim ($c[0]);
761 $out['mime_type'] = trim ($c[1]);
762 $out['key'] = trim ($c[2], NL
);
763 $out['time'] = trim ($c[3]);
764 $out['onetime'] = trim ($c[4]);
765 $out['ip'] = trim ($c[5]);
766 $out['last_edited'] = trim ($c[6]);
767 $out['next_code'] = trim ($c[7]);
772 * Delete async transfert informations
775 jirafeau_async_delete ($ref)
778 if (file_exists (VAR_ASYNC
. $p . $ref))
779 unlink (VAR_ASYNC
. $p . $ref);
780 if (file_exists (VAR_ASYNC
. $p . $ref . '_data'))
781 unlink (VAR_ASYNC
. $p . $ref . '_data');
782 $parse = VAR_ASYNC
. $p;
784 while (file_exists ($parse)
785 && ($scan = scandir ($parse))
786 && count ($scan) == 2 // '.' and '..' folders => empty.
787 && basename ($parse) != basename (VAR_ASYNC
))
790 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
795 * Init a new asynchronous upload.
796 * @param $finename Name of the file to send
797 * @param $one_time One time upload parameter
798 * @param $key eventual password (or blank)
799 * @param $time time limit
800 * @param $ip ip address of the client
801 * @return a string containing a temporary reference followed by a code or the string "Error"
804 jirafeau_async_init ($filename, $type, $one_time, $key, $time, $ip)
808 /* Create temporary folder. */
811 $code = jirafeau_gen_random (4);
814 $ref = jirafeau_gen_random (32);
815 $p = VAR_ASYNC
. s2p ($ref);
816 } while (file_exists ($p));
817 @mkdir
($p, 0755, true);
818 if (!file_exists ($p))
824 /* md5 password or empty */
827 $password = md5 ($key);
829 /* Store informations. */
831 $handle = fopen ($p, 'w');
833 str_replace (NL
, '', trim ($filename)) . NL
.
834 str_replace (NL
, '', trim ($type)) . NL
. $password . NL
.
835 $time . NL
. ($one_time ?
'O' : 'R') . NL
. $ip . NL
.
836 date ('U') . NL
. $code . NL
);
839 return $ref . NL
. $code ;
843 * Append a piece of file on the asynchronous upload.
844 * @param $ref asynchronous upload reference
845 * @param $file piece of data
846 * @param $code client code for this operation
847 * @param $max_file_size maximum allowed file size
848 * @return a string containing a next code to use or the string "Error"
851 jirafeau_async_push ($ref, $data, $code, $max_file_size)
853 /* Get async infos. */
854 $a = jirafeau_get_async_ref ($ref);
856 /* Check some errors. */
858 ||
$a['next_code'] != "$code"
859 ||
empty ($data['tmp_name'])
860 ||
!is_uploaded_file ($data['tmp_name']))
866 $r_path = $data['tmp_name'];
867 $w_path = VAR_ASYNC
. $p . $ref . '_data';
869 /* Check that file size is not above upload limit. */
870 if ($max_file_size > 0 &&
871 filesize ($r_path) +
filesize ($w_path) > $max_file_size * 1024 * 1024)
873 jirafeau_async_delete ($ref);
877 /* Concatenate data. */
878 $r = fopen ($r_path, 'r');
879 $w = fopen ($w_path, 'a');
882 if (fwrite ($w, fread ($r, 1024)) === false)
886 jirafeau_async_delete ($ref);
894 /* Update async file. */
895 $code = jirafeau_gen_random (4);
896 $handle = fopen (VAR_ASYNC
. $p . $ref, 'w');
898 $a['file_name'] . NL
. $a['mime_type'] . NL
. $a['key'] . NL
.
899 $a['time'] . NL
. $a['onetime'] . NL
. $a['ip'] . NL
.
900 date ('U') . NL
. $code . NL
);
906 * Finalyze an asynchronous upload.
907 * @param $ref asynchronous upload reference
908 * @param $code client code for this operation
909 * @param $crypt boolean asking to crypt or not
910 * @param $link_name_length link name length
911 * @return a string containing the download reference followed by a delete code or the string "Error"
914 jirafeau_async_end ($ref, $code, $crypt, $link_name_length)
916 /* Get async infos. */
917 $a = jirafeau_get_async_ref ($ref);
919 ||
$a['next_code'] != "$code")
922 /* Generate link infos. */
923 $p = VAR_ASYNC
. s2p ($ref) . $ref . "_data";
924 if (!file_exists($p))
929 if ($crypt == true && extension_loaded('mcrypt') == true)
931 $crypt_key = jirafeau_encrypt_file ($p, $p);
932 if (strlen($crypt_key) > 0)
936 $md5 = md5_file ($p);
937 $size = filesize($p);
939 $delete_link_code = jirafeau_gen_random (5);
941 /* File already exist ? */
942 if (!file_exists (VAR_FILES
. $np))
943 @mkdir
(VAR_FILES
. $np, 0755, true);
944 if (!file_exists (VAR_FILES
. $np . $md5))
945 rename ($p, VAR_FILES
. $np . $md5);
947 /* Increment or create count file. */
949 if (file_exists (VAR_FILES
. $np . $md5 . '_count'))
951 $content = file (VAR_FILES
. $np . $md5. '_count');
952 $counter = trim ($content[0]);
955 $handle = fopen (VAR_FILES
. $np . $md5. '_count', 'w');
956 fwrite ($handle, $counter);
960 $link_tmp_name = VAR_LINKS
. $md5 . rand (0, 10000) . '.tmp';
961 $handle = fopen ($link_tmp_name, 'w');
963 $a['file_name'] . NL
. $a['mime_type'] . NL
. $size . NL
.
964 $a['key'] . NL
. $a['time'] . NL
. $md5 . NL
. $a['onetime'] . NL
.
965 date ('U') . NL
. $a['ip'] . NL
. $delete_link_code . NL
. ($crypted ?
'C' : 'O'));
967 $md5_link = substr(base_16_to_64 (md5_file ($link_tmp_name)), 0, $link_name_length);
968 $l = s2p ("$md5_link");
969 if (!@mkdir
(VAR_LINKS
. $l, 0755, true) ||
970 !rename ($link_tmp_name, VAR_LINKS
. $l . $md5_link))
973 /* Clean async upload. */
974 jirafeau_async_delete ($ref);
975 return $md5_link . NL
. $delete_link_code . NL
. urlencode($crypt_key);
979 jirafeau_crypt_create_iv($base, $size)
982 while (strlen ($iv) < $size)
984 $iv = substr($iv, 0, $size);
989 * Crypt file and returns decrypt key.
990 * @param $fp_src file path to the file to crypt.
991 * @param $fp_dst file path to the file to write crypted file (could be the same).
992 * @return decrypt key composed of the key and the iv separated by a point ('.')
995 jirafeau_encrypt_file ($fp_src, $fp_dst)
997 $fs = filesize ($fp_src);
998 if ($fs === false ||
$fs == 0 ||
!(extension_loaded('mcrypt') == true))
1001 /* Prepare module. */
1002 $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
1004 $crypt_key = jirafeau_gen_random (10);
1005 $md5_key = md5($crypt_key);
1006 $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m));
1008 mcrypt_generic_init($m, $md5_key, $iv);
1010 $r = fopen ($fp_src, 'r');
1011 $w = fopen ($fp_dst, 'c');
1014 $enc = mcrypt_generic($m, fread ($r, 1024));
1015 if (fwrite ($w, $enc) === false)
1021 mcrypt_generic_deinit($m);
1022 mcrypt_module_close($m);
1028 * @param $fp_src file path to the file to decrypt.
1029 * @param $fp_dst file path to the file to write decrypted file (could be the same).
1030 * @param $k string composed of the key and the iv separated by a point ('.')
1031 * @return key used to decrypt. a string of length 0 is returned if failed.
1034 jirafeau_decrypt_file ($fp_src, $fp_dst, $k)
1036 $fs = filesize ($fp_src);
1037 if ($fs === false ||
$fs == 0 ||
!(extension_loaded('mcrypt') == true))
1041 $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
1042 /* Extract key and iv. */
1044 $md5_key = md5($crypt_key);
1045 $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m));
1047 $r = fopen ($fp_src, 'r');
1048 $w = fopen ($fp_dst, 'c');
1051 $dec = mdecrypt_generic($m, fread ($r, 1024));
1052 if (fwrite ($w, $dec) === false)
1058 mcrypt_generic_deinit($m);
1059 mcrypt_module_close($m);
1064 * Check if Jirafeau is password protected for visitors.
1065 * @return true if Jirafeau is password protected, false otherwise.
1067 function jirafeau_has_upload_password ($cfg)
1069 return count ($cfg['upload_password']) > 0;
1073 * Challenge password for a visitor.
1074 * @param $password password to be challenged
1075 * @return true if password is valid, false otherwise.
1077 function jirafeau_challenge_upload_password ($cfg, $password)
1079 if (!jirafeau_has_upload_password($cfg))
1081 forEach ($cfg['upload_password'] as $p)
1082 if ($password == $p)
patrick-canterino.de