/*
* Jirafeau, your web file repository
* Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
- * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
+ * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
* Copyright (C) 2015 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
*
* This program is free software: you can redistribute it and/or modify
return $count;
}
+
+/** hash file's content
+ * @param $method hash method, see 'file_hash' option. Valid methods are 'md5', 'md5_outside' or 'random'
+ * @param $file_path file to hash
+ * @returns hash string
+ */
+function jirafeau_hash_file($method, $file_path)
+{
+ switch ($method) {
+ case 'md5_outside':
+ return jirafeau_md5_outside($file_path);
+ case 'md5':
+ return md5_file($file_path);
+ case 'random':
+ return jirafeau_gen_random(32);
+ }
+ return md5_file($file_path);
+}
+
+/** hash part of file: start, end and size.
+ * This is a partial file hash, faster but weaker.
+ * @param $file_path file to hash
+ * @returns hash string
+ */
+function jirafeau_md5_outside($file_path)
+{
+ $out = false;
+ $handle = fopen($file_path, "r");
+ if ($handle === false) {
+ return false;
+ }
+ $size = filesize($file_path);
+ if ($size === false) {
+ goto err;
+ }
+ $first = fread($handle, 64);
+ if ($first === false) {
+ goto err;
+ }
+ if (fseek($handle, $size < 64 ? 0 : $size - 64) == -1) {
+ goto err;
+ }
+ $last = fread($handle, 64);
+ if ($last === false) {
+ goto err;
+ }
+ $out = md5($first . $last . $size);
+ err:
+ fclose($handle);
+ return $out;
+}
+
/**
* handles an uploaded file
* @param $file the file struct given by $_FILE[]
* 'link' => the link name of the uploaded file
* 'delete_link' => the link code to delete file
*/
-function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length)
+function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length, $file_hash_method)
{
if (empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) {
return (array(
}
/* file informations */
- $hash = md5_file($file['tmp_name']);
+ $hash = jirafeau_hash_file($file_hash_method, $file['tmp_name']);
$name = str_replace(NL, '', trim($file['name']));
$mime_type = $file['type'];
$size = $file['size'];
if (!is_writable(VAR_ASYNC)) {
add_error(t('ASYNC_DIR_W'), VAR_ASYNC);
}
+
+ if ($cfg['enable_crypt'] && $cfg['litespeed_workaround']) {
+ add_error(t('INCOMPATIBLE_OPTIONS_W'), 'enable_crypt=true<br>litespeed_workaround=true');
+ }
+
+ if ($cfg['one_time_download'] && $cfg['litespeed_workaround']) {
+ add_error(t('INCOMPATIBLE_OPTIONS_W'), 'one_time_download=true<br>litespeed_workaround=true');
+ }
}
/**
* @param $link_name_length link name length
* @return a string containing the download reference followed by a delete code or the string 'Error'
*/
-function jirafeau_async_end($ref, $code, $crypt, $link_name_length)
+function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_method)
{
/* Get async infos. */
$a = jirafeau_get_async_ref($ref);
}
}
- $hash = md5_file($p);
+ $hash = jirafeau_hash_file($file_hash_method, $p);
$size = filesize($p);
$np = s2p($hash);
$delete_link_code = jirafeau_gen_random(5);
{
return "<input type='hidden' name='admin_csrf' value='". $_SESSION['admin_csrf'] . "'/>";
}
+
+function jirafeau_dir_size($dir)
+{
+ $size = 0;
+ foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $entry) {
+ $size += is_file($entry) ? filesize($entry) : jirafeau_dir_size($entry);
+ }
+ return $size;
+}