]>
git.p6c8.net - jirafeau_project.git/blob - lib/functions.php
6d7cb342d9780bbf779782e274e57707721e3d5c
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 jirafeau_human_size ($octets)
37 $u = array ('B', 'KB', 'MB', 'GB', 'TB');
38 $o = max ($octets, 0);
39 $p = min (floor (($o ?
log ($o) : 0) / log (1024)), count ($u) - 1);
41 return round ($o, 1) . $u[$p];
45 jirafeau_clean_rm_link ($link)
48 if (file_exists (VAR_LINKS
. $p . $link))
49 unlink (VAR_LINKS
. $p . $link);
50 $parse = VAR_LINKS
. $p;
52 while (file_exists ($parse)
53 && ($scan = scandir ($parse))
54 && count ($scan) == 2 // '.' and '..' folders => empty.
55 && basename ($parse) != basename (VAR_LINKS
))
58 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
63 jirafeau_clean_rm_file ($md5)
66 if (file_exists (VAR_FILES
. $p . $md5))
67 unlink (VAR_FILES
. $p . $md5);
68 if (file_exists (VAR_FILES
. $p . $md5 . '_count'))
69 unlink (VAR_FILES
. $p . $md5 . '_count');
70 $parse = VAR_FILES
. $p;
72 while (file_exists ($parse)
73 && ($scan = scandir ($parse))
74 && count ($scan) == 2 // '.' and '..' folders => empty.
75 && basename ($parse) != basename (VAR_FILES
))
78 $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
83 * transforms a php.ini string representing a value in an integer
84 * @param $value the value from php.ini
85 * @returns an integer for this value
87 function jirafeau_ini_to_bytes ($value)
89 $modifier = substr ($value, -1);
90 $bytes = substr ($value, 0, -1);
91 switch (strtoupper ($modifier))
110 * gets the maximum upload size according to php.ini
111 * @returns the maximum upload size string
114 jirafeau_get_max_upload_size ()
116 return jirafeau_human_size(
117 min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
118 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize'))));
122 * gets a string explaining the error
123 * @param $code the error code
124 * @returns a string explaining the error
127 jirafeau_upload_errstr ($code)
131 case UPLOAD_ERR_INI_SIZE
:
132 case UPLOAD_ERR_FORM_SIZE
:
133 return t('Your file exceeds the maximum authorized file size. ');
136 case UPLOAD_ERR_PARTIAL
:
137 case UPLOAD_ERR_NO_FILE
:
140 ('Your file was not uploaded correctly. You may succeed in retrying. ');
143 case UPLOAD_ERR_NO_TMP_DIR
:
144 case UPLOAD_ERR_CANT_WRITE
:
145 case UPLOAD_ERR_EXTENSION
:
146 return t('Internal error. You may not succeed in retrying. ');
152 return t('Unknown error. ');
155 /** Remove link and it's file
156 * @param $link the link's name (hash)
160 jirafeau_delete_link ($link)
162 $l = jirafeau_get_link ($link);
166 jirafeau_clean_rm_link ($link);
172 if (file_exists (VAR_FILES
. $p . $md5. '_count'))
174 $content = file (VAR_FILES
. $p . $md5. '_count');
175 $counter = trim ($content[0]);
181 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
182 fwrite ($handle, $counter);
187 jirafeau_clean_rm_file ($md5);
191 * Delete a file and it's links.
194 jirafeau_delete_file ($md5)
197 /* Get all links files. */
198 $stack = array (VAR_LINKS
);
199 while (($d = array_shift ($stack)) && $d != NULL)
203 foreach ($dir as $node)
205 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
206 preg_match ('/\.tmp/i', "$node"))
209 if (is_dir ($d . $node))
211 /* Push new found directory. */
212 $stack[] = $d . $node . '/';
214 elseif (is_file ($d . $node))
216 /* Read link informations. */
217 $l = jirafeau_get_link (basename ($node));
220 if ($l['md5'] == $md5)
223 jirafeau_delete_link ($node);
228 jirafeau_clean_rm_file ($md5);
233 * handles an uploaded file
234 * @param $file the file struct given by $_FILE[]
235 * @param $one_time_download is the file a one time download ?
236 * @param $key if not empty, protect the file with this key
237 * @param $time the time of validity of the file
238 * @param $ip uploader's ip
239 * @returns an array containing some information
240 * 'error' => information on possible errors
241 * 'link' => the link name of the uploaded file
242 * 'delete_link' => the link code to delete file
245 jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
247 if (empty ($file['tmp_name']) ||
!is_uploaded_file ($file['tmp_name']))
251 array ('has_error' => true,
252 'why' => jirafeau_upload_errstr ($file['error'])),
254 'delete_link' => ''));
257 /* array representing no error */
258 $noerr = array ('has_error' => false, 'why' => '');
260 /* file informations */
261 $md5 = md5_file ($file['tmp_name']);
262 $name = trim ($file['name']);
263 $mime_type = $file['type'];
264 $size = $file['size'];
266 /* does file already exist ? */
269 if (file_exists (VAR_FILES
. $p . $md5))
271 $rc = unlink ($file['tmp_name']);
273 elseif ((file_exists (VAR_FILES
. $p) || @mkdir
(VAR_FILES
. $p, 0755, true))
274 && move_uploaded_file ($file['tmp_name'], VAR_FILES
. $p . $md5))
282 array ('has_error' => true,
283 'why' => t('Internal error during file creation.')),
285 'delete_link' => ''));
288 /* increment or create count file */
290 if (file_exists (VAR_FILES
. $p . $md5 . '_count'))
292 $content = file (VAR_FILES
. $p . $md5. '_count');
293 $counter = trim ($content[0]);
296 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
297 fwrite ($handle, $counter);
300 /* Create delete code. */
301 $delete_link_code = 0;
302 for ($i = 0; $i < 8; $i++
)
303 $delete_link_code .= dechex (rand (0, 16));
305 /* md5 password or empty */
308 $password = md5 ($key);
310 /* create link file */
311 $link_tmp_name = VAR_LINKS
. $md5 . rand (0, 10000) . ' .tmp';
312 $handle = fopen ($link_tmp_name, 'w');
314 $name . NL
. $mime_type . NL
. $size . NL
. $password . NL
. $time .
315 NL
. $md5. NL
. ($one_time_download ?
'O' : 'R') . NL
.date ('U') .
316 NL
. $ip . NL
. $delete_link_code . NL
);
318 $md5_link = md5_file ($link_tmp_name);
319 $l = s2p ("$md5_link");
320 if (!@mkdir
(VAR_LINKS
. $l, 0755, true) ||
321 !rename ($link_tmp_name, VAR_LINKS
. $l . $md5_link))
323 if (file_exists ($link_tmp_name))
324 unlink ($link_tmp_name);
329 $handle = fopen (VAR_FILES
. $p . $md5. '_count', 'w');
330 fwrite ($handle, $counter);
335 jirafeau_clean_rm_file ($md5_link);
339 array ('has_error' => true,
340 'why' => t('Internal error during file creation. ')),
342 'delete_link' => ''));
344 return (array ('error' => $noerr,
346 'delete_link' => $delete_link_code));
350 * tells if a mime-type is viewable in a browser
351 * @param $mime the mime type
352 * @returns a boolean telling if a mime type is viewable
355 jirafeau_is_viewable ($mime)
359 /* Actually, verify if mime-type is an image or a text. */
360 $viewable = array ('image', 'text');
361 $decomposed = explode ('/', $mime);
362 return in_array ($decomposed[0], $viewable);
368 // Error handling functions.
369 //! Global array that contains all registered errors.
370 $error_list = array ();
373 * Adds an error to the list of errors.
374 * @param $title the error's title
375 * @param $description is a human-friendly description of the problem.
378 add_error ($title, $description)
381 $error_list[] = '<p>' . $title. '<br />' . $description. '</p>';
385 * Informs whether any error has been registered yet.
386 * @return true if there are errors.
392 return !empty ($error_list);
396 * Displays all the errors.
404 echo '<div class="error">';
405 foreach ($error_list as $error)
414 * Read link informations
415 * @return array containing informations.
418 jirafeau_get_link ($hash)
421 $link = VAR_LINKS
. s2p ("$hash") . $hash;
423 if (!file_exists ($link))
427 $out['file_name'] = trim ($c[0]);
428 $out['mime_type'] = trim ($c[1]);
429 $out['file_size'] = trim ($c[2]);
430 $out['key'] = trim ($c[3], NL
);
431 $out['time'] = trim ($c[4]);
432 $out['md5'] = trim ($c[5]);
433 $out['onetime'] = trim ($c[6]);
434 $out['upload_date'] = trim ($c[7]);
435 $out['ip'] = trim ($c[8]);
436 $out['link_code'] = trim ($c[9]);
442 * List files in admin interface.
445 jirafeau_admin_list ($name, $file_hash, $link_hash)
447 echo '<fieldset><legend>';
450 if (!empty ($file_hash))
451 echo $file_hash . ' ';
452 if (!empty ($link_hash))
453 echo $link_hash . ' ';
454 if (empty ($name) && empty ($file_hash) && empty ($link_hash))
455 echo t('List all files');
459 echo '<td>' . t('Filename') . '</td>';
460 echo '<td>' . t('Type') . '</td>';
461 echo '<td>' . t('Size') . '</td>';
462 echo '<td>' . t('Expire') . '</td>';
463 echo '<td>' . t('Onetime') . '</td>';
464 echo '<td>' . t('Upload date') . '</td>';
465 echo '<td>' . t('Origin') . '</td>';
466 echo '<td>' . t('Action') . '</td>';
469 /* Get all links files. */
470 $stack = array (VAR_LINKS
);
471 while (($d = array_shift ($stack)) && $d != NULL)
474 foreach ($dir as $node)
476 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
477 preg_match ('/\.tmp/i', "$node"))
479 if (is_dir ($d . $node))
481 /* Push new found directory. */
482 $stack[] = $d . $node . '/';
484 elseif (is_file ($d . $node))
486 /* Read link informations. */
487 $l = jirafeau_get_link ($node);
492 if (!empty ($name) && $name != $l['file_name'])
494 if (!empty ($file_hash) && $file_hash != $l['md5'])
496 if (!empty ($link_hash) && $link_hash != $link)
498 /* Print link informations. */
501 '<form action = "admin.php" method = "post">' .
502 '<input type = "hidden" name = "action" value = "download"/>' .
503 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
504 '<input type = "submit" value = "' . $l['file_name'] . '" />' .
507 echo '<td>' . $l['mime_type'] . '</td>';
508 echo '<td>' . jirafeau_human_size ($l['file_size']) . '</td>';
509 echo '<td>' . ($l['time'] == -1 ?
'' : strftime ('%c', $l['time'])) .
511 echo '<td>' . $l['onetime'] . '</td>';
512 echo '<td>' . strftime ('%c', $l['upload_date']) . '</td>';
513 echo '<td>' . $l['ip'] . '</td>';
515 '<form action = "admin.php" method = "post">' .
516 '<input type = "hidden" name = "action" value = "delete_link"/>' .
517 '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
518 '<input type = "submit" value = "' . t('Del link') . '" />' .
520 '<form action = "admin.php" method = "post">' .
521 '<input type = "hidden" name = "action" value = "delete_file"/>' .
522 '<input type = "hidden" name = "md5" value = "' . $l['md5'] . '"/>' .
523 '<input type = "submit" value = "' . t('Del file and links') . '" />' .
530 echo '</table></fieldset>';
534 * Clean expired files.
535 * @return number of cleaned files.
538 jirafeau_admin_clean ()
541 /* Get all links files. */
542 $stack = array (VAR_LINKS
);
543 while (($d = array_shift ($stack)) && $d != NULL)
547 foreach ($dir as $node)
549 if (strcmp ($node, '.') == 0 ||
strcmp ($node, '..') == 0 ||
550 preg_match ('/\.tmp/i', "$node"))
553 if (is_dir ($d . $node))
555 /* Push new found directory. */
556 $stack[] = $d . $node . '/';
558 elseif (is_file ($d . $node))
560 /* Read link informations. */
561 $l = jirafeau_get_link (basename ($node));
564 $p = s2p ($l['md5']);
565 if ($l['time'] > 0 && $l['time'] < time () ||
// expired
566 !file_exists (VAR_FILES
. $p . $l['md5']) ||
// invalid
567 !file_exists (VAR_FILES
. $p . $l['md5'] . '_count')) // invalid
569 jirafeau_delete_link ($node);
patrick-canterino.de