]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.php
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2012 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 jirafeau_human_size ($octets)
96 $u = array ('B', 'KB', 'MB', 'GB', 'TB');
97 $o = max ($octets, 0);
98 $p = min (floor (($o ?
log ($o) : 0) / log (1024)), count ($u) - 1);
100 return round ($o, 1) . $u[$p];
104 jirafeau_clean_rm_link ($link)
107 if (file_exists (VAR_LINKS
. $p . $link))
108 unlink (VAR_LINKS
. $p . $link);
109 $parse = VAR_LINKS
. $p;
111 while (file_exists ($parse)
112 && ($scan = scandir ($parse))
113 && count ($scan) == 2 // '.' and '..' folders => empty.
114 && basename ($parse) != basename (VAR_LINKS
))
117 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
122 jirafeau_clean_rm_file ($md5)
125 $f = VAR_FILES
. $p . $md5;
126 if (file_exists ($f) && is_file ($f))
128 if (file_exists ($f . '_count') && is_file ($f . '_count'))
129 unlink ($f . '_count');
130 $parse = VAR_FILES
. $p;
132 while (file_exists ($parse)
133 && ($scan = scandir ($parse))
134 && count ($scan) == 2 // '.' and '..' folders => empty.
135 && basename ($parse) != basename (VAR_FILES
))
138 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
143 * transforms a php.ini string representing a value in an integer
144 * @param $value the value from php.ini
145 * @returns an integer for this value
147 function jirafeau_ini_to_bytes ($value)
149 $modifier = substr ($value, -1);
150 $bytes = substr ($value, 0, -1);
151 switch (strtoupper ($modifier))
170 * gets the maximum upload size according to php.ini
171 * @returns the maximum upload size in bytes
174 jirafeau_get_max_upload_size_bytes ()
176 return min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
177 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
181 * gets the maximum upload size according to php.ini
182 * @returns the maximum upload size string
185 jirafeau_get_max_upload_size ()
187 return jirafeau_human_size(
188 min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
189 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize'))));
193 * gets a string explaining the error
194 * @param $code the error code
195 * @returns a string explaining the error
198 jirafeau_upload_errstr ($code)
202 case UPLOAD_ERR_INI_SIZE
:
203 case UPLOAD_ERR_FORM_SIZE
:
204 return t('Your file exceeds the maximum authorized file size. ');
207 case UPLOAD_ERR_PARTIAL
:
208 case UPLOAD_ERR_NO_FILE
:
211 ('Your file was not uploaded correctly. You may succeed in retrying. ');
214 case UPLOAD_ERR_NO_TMP_DIR
:
215 case UPLOAD_ERR_CANT_WRITE
:
216 case UPLOAD_ERR_EXTENSION
:
217 return t('Internal error. You may not succeed in retrying. ');
223 return t('Unknown error. ');
226 /** Remove link and it's file
227 * @param $link the link's name (hash)
231 jirafeau_delete_link ($link)
233 $l = jirafeau_get_link ($link);
237 jirafeau_clean_rm_link ($link);
243 if (file_exists (VAR_FILES
. $p . $md5. '_count'))
245 $content = file (VAR_FILES
. $p . $md5. '_count');
246 $counter = trim ($content[0]);
252 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
253 fwrite ($handle, $counter);
258 jirafeau_clean_rm_file ($md5);
262 * Delete a file and it's links.
265 jirafeau_delete_file ($md5)
268 /* Get all links files. */
269 $stack = array (VAR_LINKS
);
270 while (($d = array_shift ($stack)) && $d != NULL)
274 foreach ($dir as $node)
276 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
277 preg_match ('/\.tmp/i', "$node"))
280 if (is_dir ($d . $node))
282 /* Push new found directory. */
283 $stack[] = $d . $node . '/';
285 elseif (is_file ($d . $node))
287 /* Read link informations. */
288 $l = jirafeau_get_link (basename ($node));
291 if ($l['md5'] == $md5)
294 jirafeau_delete_link ($node);
299 jirafeau_clean_rm_file ($md5);
304 * handles an uploaded file
305 * @param $file the file struct given by $_FILE[]
306 * @param $one_time_download is the file a one time download ?
307 * @param $key if not empty, protect the file with this key
308 * @param $time the time of validity of the file
309 * @param $ip uploader's ip
310 * @param $crypt boolean asking to crypt or not
311 * @param $link_name_length size of the link name
312 * @returns an array containing some information
313 * 'error' => information on possible errors
314 * 'link' => the link name of the uploaded file
315 * 'delete_link' => the link code to delete file
318 jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length)
320 if (empty ($file['tmp_name']) ||
!is_uploaded_file ($file['tmp_name']))
324 array ('has_error' => true,
325 'why' => jirafeau_upload_errstr ($file['error'])),
327 'delete_link' => ''));
330 /* array representing no error */
331 $noerr = array ('has_error' => false, 'why' => '');
333 /* Crypt file if option is enabled. */
336 if ($crypt == true && extension_loaded('mcrypt'))
338 $crypt_key = jirafeau_encrypt_file ($file['tmp_name'], $file['tmp_name']);
339 if (strlen($crypt_key) > 0)
343 /* file informations */
344 $md5 = md5_file ($file['tmp_name']);
345 $name = str_replace (NL
, '', trim ($file['name']));
346 $mime_type = $file['type'];
347 $size = $file['size'];
349 /* does file already exist ? */
352 if (file_exists (VAR_FILES
. $p . $md5))
354 $rc = unlink ($file['tmp_name']);
356 elseif ((file_exists (VAR_FILES
. $p) || @mkdir
(VAR_FILES
. $p, 0755, true))
357 && move_uploaded_file ($file['tmp_name'], VAR_FILES
. $p . $md5))
365 array ('has_error' => true,
366 'why' => t('Internal error during file creation.')),
368 'delete_link' => ''));
371 /* Increment or create count file. */
373 if (file_exists (VAR_FILES
. $p . $md5 . '_count'))
375 $content = file (VAR_FILES
. $p . $md5. '_count');
376 $counter = trim ($content[0]);
379 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
380 fwrite ($handle, $counter);
383 /* Create delete code. */
384 $delete_link_code = jirafeau_gen_random (5);
386 /* md5 password or empty. */
389 $password = md5 ($key);
391 /* create link file */
392 $link_tmp_name = VAR_LINKS
. $md5 . rand (0, 10000) . '.tmp';
393 $handle = fopen ($link_tmp_name, 'w');
395 $name . NL
. $mime_type . NL
. $size . NL
. $password . NL
. $time .
396 NL
. $md5. NL
. ($one_time_download ?
'O' : 'R') . NL
. date ('U') .
397 NL
. $ip . NL
. $delete_link_code . NL
. ($crypted ?
'C' : 'O'));
399 $md5_link = substr(base_16_to_64 (md5_file ($link_tmp_name)), 0, $link_name_length);
400 $l = s2p ("$md5_link");
401 if (!@mkdir
(VAR_LINKS
. $l, 0755, true) ||
402 !rename ($link_tmp_name, VAR_LINKS
. $l . $md5_link))
404 if (file_exists ($link_tmp_name))
405 unlink ($link_tmp_name);
410 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
411 fwrite ($handle, $counter);
416 jirafeau_clean_rm_file ($md5_link);
420 array ('has_error' => true,
421 'why' => t('Internal error during file creation. ')),
423 'delete_link' => ''));
425 return (array ('error' => $noerr,
427 'delete_link' => $delete_link_code,
428 'crypt_key' => $crypt_key));
432 * tells if a mime-type is viewable in a browser
433 * @param $mime the mime type
434 * @returns a boolean telling if a mime type is viewable
437 jirafeau_is_viewable ($mime)
441 /* Actually, verify if mime-type is an image or a text. */
442 $viewable = array ('image', 'text');
443 $decomposed = explode ('/', $mime);
444 return in_array ($decomposed[0], $viewable);
449 // Error handling functions.
450 //! Global array that contains all registered errors.
451 $error_list = array ();
454 * Adds an error to the list of errors.
455 * @param $title the error's title
456 * @param $description is a human-friendly description of the problem.
459 add_error ($title, $description)
462 $error_list[] = '<p>' . $title. '<br />' . $description. '</p>';
466 * Informs whether any error has been registered yet.
467 * @return true if there are errors.
473 return !empty ($error_list);
477 * Displays all the errors.
485 echo '<div class="error">';
486 foreach ($error_list as $error)
494 function check_errors ()
496 if (file_exists (JIRAFEAU_ROOT
. 'install.php')
497 && !file_exists (JIRAFEAU_ROOT
. 'lib/config.local.php'))
499 header('Location: install.php');
503 /* check if the destination dirs are writable */
504 $writable = is_writable (VAR_FILES
) && is_writable (VAR_LINKS
);
506 /* Checking for errors. */
507 if (!is_writable (VAR_FILES
))
508 add_error (t('The file directory is not writable!'), VAR_FILES
);
510 if (!is_writable (VAR_LINKS
))
511 add_error (t('The link directory is not writable!'), VAR_LINKS
);
513 if (!is_writable (VAR_ASYNC
))
514 add_error (t('The async directory is not writable!'), VAR_ASYNC
);
516 if (!is_writable (VAR_BLOCK
))
517 add_error (t('The block directory is not writable!'), VAR_BLOCK
);
519 /* Check if the install.php script is still in the directory. */
520 if (file_exists (JIRAFEAU_ROOT
. 'install.php'))
521 add_error (t('Installer script still present'),
522 t('Please make sure to delete the installer script ' .
523 '"install.php" before continuing.'));
527 * Read link informations
528 * @return array containing informations.
531 jirafeau_get_link ($hash)
534 $link = VAR_LINKS
. s2p ("$hash") . $hash;
536 if (!file_exists ($link))
540 $out['file_name'] = trim ($c[0]);
541 $out['mime_type'] = trim ($c[1]);
542 $out['file_size'] = trim ($c[2]);
543 $out['key'] = trim ($c[3], NL
);
544 $out['time'] = trim ($c[4]);
545 $out['md5'] = trim ($c[5]);
546 $out['onetime'] = trim ($c[6]);
547 $out['upload_date'] = trim ($c[7]);
548 $out['ip'] = trim ($c[8]);
549 $out['link_code'] = trim ($c[9]);
550 if (trim ($c[10]) == 'C')
551 $out['crypted'] = true;
557 * List files in admin interface.
560 jirafeau_admin_list ($name, $file_hash, $link_hash)
562 echo '<fieldset><legend>';
564 echo t('Filename') . ": $name ";
565 if (!empty ($file_hash))
566 echo t('file') . ": $file_hash ";
567 if (!empty ($link_hash))
568 echo t('link') . ": $link_hash ";
569 if (empty ($name) && empty ($file_hash) && empty ($link_hash))
570 echo t('List all files');
574 echo '<td>' . t('Filename') . '</td>';
575 echo '<td>' . t('Type') . '</td>';
576 echo '<td>' . t('Size') . '</td>';
577 echo '<td>' . t('Expire') . '</td>';
578 echo '<td>' . t('Onetime') . '</td>';
579 echo '<td>' . t('Upload date') . '</td>';
580 echo '<td>' . t('Origin') . '</td>';
581 echo '<td>' . t('Action') . '</td>';
584 /* Get all links files. */
585 $stack = array (VAR_LINKS
);
586 while (($d = array_shift ($stack)) && $d != NULL)
589 foreach ($dir as $node)
591 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
592 preg_match ('/\.tmp/i', "$node"))
594 if (is_dir ($d . $node))
596 /* Push new found directory. */
597 $stack[] = $d . $node . '/';
599 elseif (is_file ($d . $node))
601 /* Read link informations. */
602 $l = jirafeau_get_link ($node);
607 if (!empty ($name) && !preg_match ("/$name/i", $l['file_name']))
609 if (!empty ($file_hash) && $file_hash != $l['md5'])
611 if (!empty ($link_hash) && $link_hash != $node)
613 /* Print link informations. */
616 '<form action = "admin.php" method = "post">' .
617 '<input type = "hidden" name = "action" value = "download"/>' .
618 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
619 '<input type = "submit" value = "' . $l['file_name'] . '" />' .
622 echo '<td>' . $l['mime_type'] . '</td>';
623 echo '<td>' . jirafeau_human_size ($l['file_size']) . '</td>';
624 echo '<td>' . ($l['time'] == -1 ?
'' : strftime ('%c', $l['time'])) .
626 echo '<td>' . $l['onetime'] . '</td>';
627 echo '<td>' . strftime ('%c', $l['upload_date']) . '</td>';
628 echo '<td>' . $l['ip'] . '</td>';
630 '<form action = "admin.php" method = "post">' .
631 '<input type = "hidden" name = "action" value = "delete_link"/>' .
632 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
633 '<input type = "submit" value = "' . t('Del link') . '" />' .
635 '<form action = "admin.php" method = "post">' .
636 '<input type = "hidden" name = "action" value = "delete_file"/>' .
637 '<input type = "hidden" name = "md5" value = "' . $l['md5'] . '"/>' .
638 '<input type = "submit" value = "' . t('Del file and links') . '" />' .
645 echo '</table></fieldset>';
649 * Clean expired files.
650 * @return number of cleaned files.
653 jirafeau_admin_clean ()
656 /* Get all links files. */
657 $stack = array (VAR_LINKS
);
658 while (($d = array_shift ($stack)) && $d != NULL)
662 foreach ($dir as $node)
664 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
665 preg_match ('/\.tmp/i', "$node"))
668 if (is_dir ($d . $node))
670 /* Push new found directory. */
671 $stack[] = $d . $node . '/';
673 elseif (is_file ($d . $node))
675 /* Read link informations. */
676 $l = jirafeau_get_link (basename ($node));
679 $p = s2p ($l['md5']);
680 if ($l['time'] > 0 && $l['time'] < time () ||
// expired
681 !file_exists (VAR_FILES
. $p . $l['md5']) ||
// invalid
682 !file_exists (VAR_FILES
. $p . $l['md5'] . '_count')) // invalid
684 jirafeau_delete_link ($node);
695 * Clean old async transferts.
696 * @return number of cleaned files.
699 jirafeau_admin_clean_async ()
702 /* Get all links files. */
703 $stack = array (VAR_ASYNC
);
704 while (($d = array_shift ($stack)) && $d != NULL)
708 foreach ($dir as $node)
710 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
711 preg_match ('/\.tmp/i', "$node"))
714 if (is_dir ($d . $node))
716 /* Push new found directory. */
717 $stack[] = $d . $node . '/';
719 elseif (is_file ($d . $node))
721 /* Read async informations. */
722 $a = jirafeau_get_async_ref (basename ($node));
725 /* Delete transferts older than 1 hour. */
726 if (date ('U') - $a['last_edited'] > 3600)
728 jirafeau_async_delete (basename ($node));
737 * Read async transfert informations
738 * @return array containing informations.
741 jirafeau_get_async_ref ($ref)
744 $refinfos = VAR_ASYNC
. s2p ("$ref") . "$ref";
746 if (!file_exists ($refinfos))
749 $c = file ($refinfos);
750 $out['file_name'] = trim ($c[0]);
751 $out['mime_type'] = trim ($c[1]);
752 $out['key'] = trim ($c[2], NL
);
753 $out['time'] = trim ($c[3]);
754 $out['onetime'] = trim ($c[4]);
755 $out['ip'] = trim ($c[5]);
756 $out['last_edited'] = trim ($c[6]);
757 $out['next_code'] = trim ($c[7]);
762 * Delete async transfert informations
765 jirafeau_async_delete ($ref)
768 if (file_exists (VAR_ASYNC
. $p . $ref))
769 unlink (VAR_ASYNC
. $p . $ref);
770 if (file_exists (VAR_ASYNC
. $p . $ref . '_data'))
771 unlink (VAR_ASYNC
. $p . $ref . '_data');
772 $parse = VAR_ASYNC
. $p;
774 while (file_exists ($parse)
775 && ($scan = scandir ($parse))
776 && count ($scan) == 2 // '.' and '..' folders => empty.
777 && basename ($parse) != basename (VAR_ASYNC
))
780 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
785 * Init a new asynchronous upload.
786 * @param $finename Name of the file to send
787 * @param $one_time One time upload parameter
788 * @param $key eventual password (or blank)
789 * @param $time time limit
790 * @param $ip ip address of the client
791 * @return a string containing a temporary reference followed by a code or the string "Error"
794 jirafeau_async_init ($filename, $type, $one_time, $key, $time, $ip)
798 /* Create temporary folder. */
801 $code = jirafeau_gen_random (4);
804 $ref = jirafeau_gen_random (32);
805 $p = VAR_ASYNC
. s2p ($ref);
806 } while (file_exists ($p));
807 @mkdir
($p, 0755, true);
808 if (!file_exists ($p))
814 /* md5 password or empty */
817 $password = md5 ($key);
819 /* Store informations. */
821 $handle = fopen ($p, 'w');
823 str_replace (NL
, '', trim ($filename)) . NL
.
824 str_replace (NL
, '', trim ($type)) . NL
. $password . NL
.
825 $time . NL
. ($one_time ?
'O' : 'R') . NL
. $ip . NL
.
826 date ('U') . NL
. $code . NL
);
829 return $ref . NL
. $code ;
833 * Append a piece of file on the asynchronous upload.
834 * @param $ref asynchronous upload reference
835 * @param $file piece of data
836 * @param $code client code for this operation
837 * @return a string containing a next code to use or the string "Error"
840 jirafeau_async_push ($ref, $data, $code)
842 /* Get async infos. */
843 $a = jirafeau_get_async_ref ($ref);
845 /* Check some errors. */
847 ||
$a['next_code'] != "$code"
848 ||
empty ($data['tmp_name'])
849 ||
!is_uploaded_file ($data['tmp_name']))
854 /* Concatenate data. */
855 $r = fopen ($data['tmp_name'], 'r');
856 $w = fopen (VAR_ASYNC
. $p . $ref . '_data', 'a');
859 if (fwrite ($w, fread ($r, 1024)) === false)
863 jirafeau_async_delete ($ref);
869 unlink ($data['tmp_name']);
871 /* Update async file. */
872 $code = jirafeau_gen_random (4);
873 $handle = fopen (VAR_ASYNC
. $p . $ref, 'w');
875 $a['file_name'] . NL
. $a['mime_type'] . NL
. $a['key'] . NL
.
876 $a['time'] . NL
. $a['onetime'] . NL
. $a['ip'] . NL
.
877 date ('U') . NL
. $code . NL
);
883 * Finalyze an asynchronous upload.
884 * @param $ref asynchronous upload reference
885 * @param $code client code for this operation
886 * @param $crypt boolean asking to crypt or not
887 * @param $link_name_length link name lenght
888 * @return a string containing the download reference followed by a delete code or the string "Error"
891 jirafeau_async_end ($ref, $code, $crypt, $link_name_length)
893 /* Get async infos. */
894 $a = jirafeau_get_async_ref ($ref);
896 ||
$a['next_code'] != "$code")
899 /* Generate link infos. */
900 $p = VAR_ASYNC
. s2p ($ref) . $ref . "_data";
901 if (!file_exists($p))
906 if ($crypt == true && extension_loaded('mcrypt'))
908 $cypt_key = jirafeau_encrypt_file ($p, $p);
909 if (strlen($crypt_key) > 0)
913 $md5 = md5_file ($p);
914 $size = filesize($p);
916 $delete_link_code = jirafeau_gen_random (5);
918 /* File already exist ? */
919 if (!file_exists (VAR_FILES
. $np))
920 @mkdir
(VAR_FILES
. $np, 0755, true);
921 if (!file_exists (VAR_FILES
. $np . $md5))
922 rename ($p, VAR_FILES
. $np . $md5);
924 /* Increment or create count file. */
926 if (file_exists (VAR_FILES
. $np . $md5 . '_count'))
928 $content = file (VAR_FILES
. $np . $md5. '_count');
929 $counter = trim ($content[0]);
932 $handle = fopen (VAR_FILES
. $np . $md5. '_count', 'w');
933 fwrite ($handle, $counter);
937 $link_tmp_name = VAR_LINKS
. $md5 . rand (0, 10000) . '.tmp';
938 $handle = fopen ($link_tmp_name, 'w');
940 $a['file_name'] . NL
. $a['mime_type'] . NL
. $size . NL
.
941 $a['key'] . NL
. $a['time'] . NL
. $md5 . NL
. $a['onetime'] . NL
.
942 date ('U') . NL
. $a['ip'] . NL
. $delete_link_code . NL
. ($crypted ?
'C' : 'O'));
944 $md5_link = substr(base_16_to_64 (md5_file ($link_tmp_name)), 0, $link_name_length);
945 $l = s2p ("$md5_link");
946 if (!@mkdir
(VAR_LINKS
. $l, 0755, true) ||
947 !rename ($link_tmp_name, VAR_LINKS
. $l . $md5_link))
950 /* Clean async upload. */
951 jirafeau_async_delete ($ref);
952 return $md5_link . NL
. $delete_link_code . NL
. urlencode($crypt_key);
957 * @param $id identifier of the block.
960 jirafeau_block_delete_ ($id)
962 $p = VAR_BLOCK
. s2p ($id);
963 if (!file_exists ($p))
966 if (file_exists ($p . $id))
968 if (file_exists ($p . $id . '_infos'))
969 unlink ($p . $id . '_infos');
972 while (file_exists ($parse)
973 && ($scan = scandir ($parse))
974 && count ($scan) == 2 // '.' and '..' folders => empty.
975 && basename ($parse) != basename (VAR_BLOCK
))
978 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
983 * Create a file filled with zeros.
984 * @param $size size of the file.
985 * @return a string corresponding to an id or the string "Error"
988 jirafeau_block_init ($size)
990 if (!ctype_digit ($size) ||
$size <= 0)
997 $id = jirafeau_gen_random (32);
998 $p = VAR_BLOCK
. s2p ($id);
999 } while (file_exists ($p));
1000 @mkdir
($p, 0755, true);
1001 if (!file_exists ($p))
1009 $h = fopen ($p, 'w');
1010 $fill = str_repeat ("\0", 1024);
1011 for ($cnt = 0; $cnt < $size; $cnt +
= 1024)
1013 if ($size - $cnt < 1024)
1014 $fill = str_repeat ("\0", $size - $cnt);
1015 if (fwrite ($h, $fill) === false)
1018 jirafeau_block_delete_ ($id);
1024 /* Generate a write/delete code. */
1025 $code = jirafeau_gen_random (12);
1027 /* Add block infos. */
1028 if (file_put_contents ($p . '_infos', date ('U') . NL
. $size . NL
. $code) === FALSE)
1030 jirafeau_block_delete_ ($id);
1034 return $id . NL
. $code;
1037 /** Get block size in bytes.
1038 * @param $id identifier of the block
1039 * @return block size in bytes
1042 jirafeau_block_get_size ($id)
1044 $p = VAR_BLOCK
. s2p ($id) . $id;
1045 if (!file_exists ($p))
1049 $f = file ($p . '_infos');
1050 $date = trim ($f[0]);
1051 $block_size = trim ($f[1]);
1052 $stored_code = trim ($f[2]);
1054 if (date ('U') - $date > JIRAFEAU_HOUR
1055 && date ('U') - $date < JIRAFEAU_MONTH
)
1057 if (file_put_contents ($p . '_infos', date ('U') . NL
. $block_size . NL
. $stored_code) === FALSE)
1059 jirafeau_block_delete_ ($id);
1064 elseif (date ('U') - $date >= JIRAFEAU_MONTH
)
1066 echo date ('U'). " $date ";
1067 jirafeau_block_delete_ ($id);
1075 * Read some data in a block.
1076 * @param $id identifier of the block
1077 * @param $start where to read data (starting from zero).
1078 * @param $length length to read.
1082 jirafeau_block_read ($id, $start, $length)
1084 if (!ctype_digit ($start) ||
$start < 0
1085 ||
!ctype_digit ($length) ||
$length <= 0)
1091 $p = VAR_BLOCK
. s2p ($id) . $id;
1092 if (!file_exists ($p))
1099 $f = file ($p . '_infos');
1100 $date = trim ($f[0]);
1101 $block_size = trim ($f[1]);
1102 $stored_code = trim ($f[2]);
1104 if (date ('U') - $date > JIRAFEAU_HOUR
1105 && date ('U') - $date < JIRAFEAU_MONTH
)
1107 if (file_put_contents ($p . '_infos', date ('U') . NL
. $block_size . NL
. $stored_code) === FALSE)
1109 jirafeau_block_delete_ ($id);
1115 elseif (date ('U') - $date >= JIRAFEAU_MONTH
)
1117 echo date ('U'). " $date ";
1118 jirafeau_block_delete_ ($id);
1123 if ($start +
$length > $block_size)
1130 header ('Content-Length: ' . $length);
1131 header ('Content-Disposition: attachment');
1133 $r = fopen ($p, 'r');
1134 if (fseek ($r, $start) != 0)
1140 for ($cnt = 0; $cnt < $length && !feof ($r); $cnt +
= 1024)
1142 if ($length - $cnt < 1024)
1143 $c = $length - $cnt;
1144 print fread ($r, $c);
1151 * Write some data in a block.
1152 * @param $id identifier of the block
1153 * @param $start where to writing data (starting from zero).
1154 * @param $data data to write.
1155 * @param $code code to allow writing.
1156 * @return string "Ok" or string "Error".
1159 jirafeau_block_write ($id, $start, $data, $code)
1161 if (!ctype_digit ($start) ||
$start < 0
1162 ||
strlen ($code) == 0)
1165 $p = VAR_BLOCK
. s2p ($id) . $id;
1166 if (!file_exists ($p))
1170 $f = file ($p . '_infos');
1171 $date = trim ($f[0]);
1172 $block_size = trim ($f[1]);
1173 $stored_code = trim ($f[2]);
1175 if (date ('U') - $date > JIRAFEAU_HOUR
1176 && date ('U') - $date < JIRAFEAU_MONTH
)
1178 if (file_put_contents ($p . '_infos', date ('U') . NL
. $block_size . NL
. $stored_code) === FALSE)
1180 jirafeau_block_delete_ ($id);
1185 elseif (date ('U') - $date >= JIRAFEAU_MONTH
)
1187 jirafeau_block_delete_ ($id);
1192 if ($stored_code != $code)
1199 $size = $data['size'];
1202 if ($start +
$size > $block_size)
1206 $r = fopen ($data['tmp_name'], 'r');
1209 $w = fopen ($p, 'r+');
1210 if (fseek ($w, $start) != 0)
1213 /* Write content. */
1215 for ($cnt = 0; $cnt <= $size && !feof ($w); $cnt +
= 1024)
1217 if ($size - $cnt < 1024)
1219 $d = fread ($r, $c);
1224 unlink ($data['tmp_name']);
1230 * @param $id identifier of the block.
1231 * @param $code code to allow writing.
1232 * @return string "Ok" or string "Error".
1235 jirafeau_block_delete ($id, $code)
1237 $p = VAR_BLOCK
. s2p ($id) . $id;
1239 if (!file_exists ($p))
1242 $f = file ($p . '_infos');
1243 $date = trim ($f[0]);
1244 $block_size = trim ($f[1]);
1245 $stored_code = trim ($f[2]);
1247 if ($code != $stored_code)
1250 jirafeau_block_delete_ ($id);
1255 * Clean old unused blocks.
1256 * @return number of cleaned blocks.
1259 jirafeau_admin_clean_block ()
1262 /* Get all blocks. */
1263 $stack = array (VAR_BLOCK
);
1264 while (($d = array_shift ($stack)) && $d != NULL)
1266 $dir = scandir ($d);
1268 foreach ($dir as $node)
1270 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0)
1273 if (is_dir ($d . $node))
1275 /* Push new found directory. */
1276 $stack[] = $d . $node . '/';
1278 elseif (is_file ($d . $node) && preg_match ('/\_infos/i', "$node"))
1280 /* Read block informations. */
1281 $f = file ($d . $node);
1282 $date = trim ($f[0]);
1283 $block_size = trim ($f[1]);
1284 if (date ('U') - $date >= JIRAFEAU_MONTH
)
1286 jirafeau_block_delete_ (substr($node, 0, -6));
1296 jirafeau_crypt_create_iv($base, $size)
1299 while (strlen ($iv) < $size)
1301 $iv = substr($iv, 0, $size);
1306 * Crypt file and returns decrypt key.
1307 * @param $fp_src file path to the file to crypt.
1308 * @param $fp_dst file path to the file to write crypted file (could be the same).
1309 * @return decrypt key composed of the key and the iv separated by a point ('.')
1312 jirafeau_encrypt_file ($fp_src, $fp_dst)
1314 $fs = filesize ($fp_src);
1315 if ($fs === false ||
$fs == 0 ||
!extension_loaded('mcrypt'))
1318 /* Prepare module. */
1319 $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
1321 $crypt_key = jirafeau_gen_random (10);
1322 $md5_key = md5($crypt_key);
1323 $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m));
1325 mcrypt_generic_init($m, $md5_key, $iv);
1327 $r = fopen ($fp_src, 'r');
1328 $w = fopen ($fp_dst, 'c');
1331 $enc = mcrypt_generic($m, fread ($r, 1024));
1332 if (fwrite ($w, $enc) === false)
1338 mcrypt_generic_deinit($m);
1339 mcrypt_module_close($m);
1345 * @param $fp_src file path to the file to decrypt.
1346 * @param $fp_dst file path to the file to write decrypted file (could be the same).
1347 * @param $k string composed of the key and the iv separated by a point ('.')
1348 * @return key used to decrypt. a string of length 0 is returned if failed.
1351 jirafeau_decrypt_file ($fp_src, $fp_dst, $k)
1353 $fs = filesize ($fp_src);
1354 if ($fs === false ||
$fs == 0 ||
!extension_loaded('mcrypt'))
1358 $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
1359 /* Extract key and iv. */
1361 $md5_key = md5($crypt_key);
1362 $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m));
1364 $r = fopen ($fp_src, 'r');
1365 $w = fopen ($fp_dst, 'c');
1368 $dec = mdecrypt_generic($m, fread ($r, 1024));
1369 if (fwrite ($w, $dec) === false)
1375 mcrypt_generic_deinit($m);
1376 mcrypt_module_close($m);
patrick-canterino.de