]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.php
10fc0ab1d9ba171a2d5394f4b79184a29a8b7be7
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-_';
45 $size = strlen ($num);
46 for ($i = 0; $i < $size; $i++
)
47 $b .= base_convert ($num{$i}, 16, 2);
49 for ($i = $size - 6; $i >= 0; $i -= 6)
50 $o = $m{bindec (substr ($b, $i, 6))} . $o;
51 if ($i < 0 && $i > -6)
52 $o = $m{bindec (substr ($b, 0, $i +
6))} . $o;
57 jirafeau_human_size ($octets)
59 $u = array ('B', 'KB', 'MB', 'GB', 'TB');
60 $o = max ($octets, 0);
61 $p = min (floor (($o ?
log ($o) : 0) / log (1024)), count ($u) - 1);
63 return round ($o, 1) . $u[$p];
67 jirafeau_clean_rm_link ($link)
70 if (file_exists (VAR_LINKS
. $p . $link))
71 unlink (VAR_LINKS
. $p . $link);
72 $parse = VAR_LINKS
. $p;
74 while (file_exists ($parse)
75 && ($scan = scandir ($parse))
76 && count ($scan) == 2 // '.' and '..' folders => empty.
77 && basename ($parse) != basename (VAR_LINKS
))
80 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
85 jirafeau_clean_rm_file ($md5)
88 if (file_exists (VAR_FILES
. $p . $md5))
89 unlink (VAR_FILES
. $p . $md5);
90 if (file_exists (VAR_FILES
. $p . $md5 . '_count'))
91 unlink (VAR_FILES
. $p . $md5 . '_count');
92 $parse = VAR_FILES
. $p;
94 while (file_exists ($parse)
95 && ($scan = scandir ($parse))
96 && count ($scan) == 2 // '.' and '..' folders => empty.
97 && basename ($parse) != basename (VAR_FILES
))
100 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
105 * transforms a php.ini string representing a value in an integer
106 * @param $value the value from php.ini
107 * @returns an integer for this value
109 function jirafeau_ini_to_bytes ($value)
111 $modifier = substr ($value, -1);
112 $bytes = substr ($value, 0, -1);
113 switch (strtoupper ($modifier))
132 * gets the maximum upload size according to php.ini
133 * @returns the maximum upload size string
136 jirafeau_get_max_upload_size ()
138 return jirafeau_human_size(
139 min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
140 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize'))));
144 * gets a string explaining the error
145 * @param $code the error code
146 * @returns a string explaining the error
149 jirafeau_upload_errstr ($code)
153 case UPLOAD_ERR_INI_SIZE
:
154 case UPLOAD_ERR_FORM_SIZE
:
155 return t('Your file exceeds the maximum authorized file size. ');
158 case UPLOAD_ERR_PARTIAL
:
159 case UPLOAD_ERR_NO_FILE
:
162 ('Your file was not uploaded correctly. You may succeed in retrying. ');
165 case UPLOAD_ERR_NO_TMP_DIR
:
166 case UPLOAD_ERR_CANT_WRITE
:
167 case UPLOAD_ERR_EXTENSION
:
168 return t('Internal error. You may not succeed in retrying. ');
174 return t('Unknown error. ');
177 /** Remove link and it's file
178 * @param $link the link's name (hash)
182 jirafeau_delete_link ($link)
184 $l = jirafeau_get_link ($link);
188 jirafeau_clean_rm_link ($link);
194 if (file_exists (VAR_FILES
. $p . $md5. '_count'))
196 $content = file (VAR_FILES
. $p . $md5. '_count');
197 $counter = trim ($content[0]);
203 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
204 fwrite ($handle, $counter);
209 jirafeau_clean_rm_file ($md5);
213 * Delete a file and it's links.
216 jirafeau_delete_file ($md5)
219 /* Get all links files. */
220 $stack = array (VAR_LINKS
);
221 while (($d = array_shift ($stack)) && $d != NULL)
225 foreach ($dir as $node)
227 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
228 preg_match ('/\.tmp/i', "$node"))
231 if (is_dir ($d . $node))
233 /* Push new found directory. */
234 $stack[] = $d . $node . '/';
236 elseif (is_file ($d . $node))
238 /* Read link informations. */
239 $l = jirafeau_get_link (basename ($node));
242 if ($l['md5'] == $md5)
245 jirafeau_delete_link ($node);
250 jirafeau_clean_rm_file ($md5);
255 * handles an uploaded file
256 * @param $file the file struct given by $_FILE[]
257 * @param $one_time_download is the file a one time download ?
258 * @param $key if not empty, protect the file with this key
259 * @param $time the time of validity of the file
260 * @param $ip uploader's ip
261 * @returns an array containing some information
262 * 'error' => information on possible errors
263 * 'link' => the link name of the uploaded file
264 * 'delete_link' => the link code to delete file
267 jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
269 if (empty ($file['tmp_name']) ||
!is_uploaded_file ($file['tmp_name']))
273 array ('has_error' => true,
274 'why' => jirafeau_upload_errstr ($file['error'])),
276 'delete_link' => ''));
279 /* array representing no error */
280 $noerr = array ('has_error' => false, 'why' => '');
282 /* file informations */
283 $md5 = md5_file ($file['tmp_name']);
284 $name = trim ($file['name']);
285 $mime_type = $file['type'];
286 $size = $file['size'];
288 /* does file already exist ? */
291 if (file_exists (VAR_FILES
. $p . $md5))
293 $rc = unlink ($file['tmp_name']);
295 elseif ((file_exists (VAR_FILES
. $p) || @mkdir
(VAR_FILES
. $p, 0755, true))
296 && move_uploaded_file ($file['tmp_name'], VAR_FILES
. $p . $md5))
304 array ('has_error' => true,
305 'why' => t('Internal error during file creation.')),
307 'delete_link' => ''));
310 /* increment or create count file */
312 if (file_exists (VAR_FILES
. $p . $md5 . '_count'))
314 $content = file (VAR_FILES
. $p . $md5. '_count');
315 $counter = trim ($content[0]);
318 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
319 fwrite ($handle, $counter);
322 /* Create delete code. */
323 $delete_link_code = 0;
324 for ($i = 0; $i < 8; $i++
)
325 $delete_link_code .= dechex (rand (0, 16));
327 /* md5 password or empty */
330 $password = md5 ($key);
332 /* create link file */
333 $link_tmp_name = VAR_LINKS
. $md5 . rand (0, 10000) . ' .tmp';
334 $handle = fopen ($link_tmp_name, 'w');
336 $name . NL
. $mime_type . NL
. $size . NL
. $password . NL
. $time .
337 NL
. $md5. NL
. ($one_time_download ?
'O' : 'R') . NL
.date ('U') .
338 NL
. $ip . NL
. $delete_link_code . NL
);
340 $md5_link = base_16_to_64 (md5_file ($link_tmp_name));
341 $l = s2p ("$md5_link");
342 if (!@mkdir
(VAR_LINKS
. $l, 0755, true) ||
343 !rename ($link_tmp_name, VAR_LINKS
. $l . $md5_link))
345 if (file_exists ($link_tmp_name))
346 unlink ($link_tmp_name);
351 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
352 fwrite ($handle, $counter);
357 jirafeau_clean_rm_file ($md5_link);
361 array ('has_error' => true,
362 'why' => t('Internal error during file creation. ')),
364 'delete_link' => ''));
366 return (array ('error' => $noerr,
368 'delete_link' => $delete_link_code));
372 * tells if a mime-type is viewable in a browser
373 * @param $mime the mime type
374 * @returns a boolean telling if a mime type is viewable
377 jirafeau_is_viewable ($mime)
381 /* Actually, verify if mime-type is an image or a text. */
382 $viewable = array ('image', 'text');
383 $decomposed = explode ('/', $mime);
384 return in_array ($decomposed[0], $viewable);
390 // Error handling functions.
391 //! Global array that contains all registered errors.
392 $error_list = array ();
395 * Adds an error to the list of errors.
396 * @param $title the error's title
397 * @param $description is a human-friendly description of the problem.
400 add_error ($title, $description)
403 $error_list[] = '<p>' . $title. '<br />' . $description. '</p>';
407 * Informs whether any error has been registered yet.
408 * @return true if there are errors.
414 return !empty ($error_list);
418 * Displays all the errors.
426 echo '<div class="error">';
427 foreach ($error_list as $error)
436 * Read link informations
437 * @return array containing informations.
440 jirafeau_get_link ($hash)
443 $link = VAR_LINKS
. s2p ("$hash") . $hash;
445 if (!file_exists ($link))
449 $out['file_name'] = trim ($c[0]);
450 $out['mime_type'] = trim ($c[1]);
451 $out['file_size'] = trim ($c[2]);
452 $out['key'] = trim ($c[3], NL
);
453 $out['time'] = trim ($c[4]);
454 $out['md5'] = trim ($c[5]);
455 $out['onetime'] = trim ($c[6]);
456 $out['upload_date'] = trim ($c[7]);
457 $out['ip'] = trim ($c[8]);
458 $out['link_code'] = trim ($c[9]);
464 * List files in admin interface.
467 jirafeau_admin_list ($name, $file_hash, $link_hash)
469 echo '<fieldset><legend>';
471 echo t('Filename') . ": $name ";
472 if (!empty ($file_hash))
473 echo t('file') . ": $file_hash ";
474 if (!empty ($link_hash))
475 echo t('link') . ": $link_hash ";
476 if (empty ($name) && empty ($file_hash) && empty ($link_hash))
477 echo t('List all files');
481 echo '<td>' . t('Filename') . '</td>';
482 echo '<td>' . t('Type') . '</td>';
483 echo '<td>' . t('Size') . '</td>';
484 echo '<td>' . t('Expire') . '</td>';
485 echo '<td>' . t('Onetime') . '</td>';
486 echo '<td>' . t('Upload date') . '</td>';
487 echo '<td>' . t('Origin') . '</td>';
488 echo '<td>' . t('Action') . '</td>';
491 /* Get all links files. */
492 $stack = array (VAR_LINKS
);
493 while (($d = array_shift ($stack)) && $d != NULL)
496 foreach ($dir as $node)
498 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
499 preg_match ('/\.tmp/i', "$node"))
501 if (is_dir ($d . $node))
503 /* Push new found directory. */
504 $stack[] = $d . $node . '/';
506 elseif (is_file ($d . $node))
508 /* Read link informations. */
509 $l = jirafeau_get_link ($node);
514 if (!empty ($name) && !preg_match ("/$name/i", $l['file_name']))
516 if (!empty ($file_hash) && $file_hash != $l['md5'])
518 if (!empty ($link_hash) && $link_hash != $node)
520 /* Print link informations. */
523 '<form action = "admin.php" method = "post">' .
524 '<input type = "hidden" name = "action" value = "download"/>' .
525 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
526 '<input type = "submit" value = "' . $l['file_name'] . '" />' .
529 echo '<td>' . $l['mime_type'] . '</td>';
530 echo '<td>' . jirafeau_human_size ($l['file_size']) . '</td>';
531 echo '<td>' . ($l['time'] == -1 ?
'' : strftime ('%c', $l['time'])) .
533 echo '<td>' . $l['onetime'] . '</td>';
534 echo '<td>' . strftime ('%c', $l['upload_date']) . '</td>';
535 echo '<td>' . $l['ip'] . '</td>';
537 '<form action = "admin.php" method = "post">' .
538 '<input type = "hidden" name = "action" value = "delete_link"/>' .
539 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
540 '<input type = "submit" value = "' . t('Del link') . '" />' .
542 '<form action = "admin.php" method = "post">' .
543 '<input type = "hidden" name = "action" value = "delete_file"/>' .
544 '<input type = "hidden" name = "md5" value = "' . $l['md5'] . '"/>' .
545 '<input type = "submit" value = "' . t('Del file and links') . '" />' .
552 echo '</table></fieldset>';
556 * Clean expired files.
557 * @return number of cleaned files.
560 jirafeau_admin_clean ()
563 /* Get all links files. */
564 $stack = array (VAR_LINKS
);
565 while (($d = array_shift ($stack)) && $d != NULL)
569 foreach ($dir as $node)
571 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
572 preg_match ('/\.tmp/i', "$node"))
575 if (is_dir ($d . $node))
577 /* Push new found directory. */
578 $stack[] = $d . $node . '/';
580 elseif (is_file ($d . $node))
582 /* Read link informations. */
583 $l = jirafeau_get_link (basename ($node));
586 $p = s2p ($l['md5']);
587 if ($l['time'] > 0 && $l['time'] < time () ||
// expired
588 !file_exists (VAR_FILES
. $p . $l['md5']) ||
// invalid
589 !file_exists (VAR_FILES
. $p . $l['md5'] . '_count')) // invalid
591 jirafeau_delete_link ($node);
patrick-canterino.de