X-Git-Url: https://git.p6c8.net/jirafeau_mojo42.git/blobdiff_plain/2b4714c704f8ca5567c85a26e9202d5c9b3550e4..dddcc255110ee8219bfb41943c6e4430a02139aa:/lib/functions.php?ds=inline diff --git a/lib/functions.php b/lib/functions.php index 84f2a44..b3dc904 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -2,7 +2,7 @@ /* * 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 @@ -192,12 +192,16 @@ function jirafeau_ini_to_bytes($value) switch (strtoupper($modifier)) { case 'P': $bytes *= 1024; + // no break case 'T': $bytes *= 1024; + // no break case 'G': $bytes *= 1024; + // no break case 'M': $bytes *= 1024; + // no break case 'K': $bytes *= 1024; } @@ -210,8 +214,10 @@ function jirafeau_ini_to_bytes($value) */ function jirafeau_get_max_upload_size_bytes() { - return min(jirafeau_ini_to_bytes(ini_get('post_max_size')), - jirafeau_ini_to_bytes(ini_get('upload_max_filesize'))); + return min( + jirafeau_ini_to_bytes(ini_get('post_max_size')), + jirafeau_ini_to_bytes(ini_get('upload_max_filesize')) + ); } /** @@ -459,10 +465,12 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l /* create link file */ $link_tmp_name = VAR_LINKS . $hash . rand(0, 10000) . '.tmp'; $handle = fopen($link_tmp_name, 'w'); - fwrite($handle, - $name . NL. $mime_type . NL. $size . NL. $password . NL. $time . + fwrite( + $handle, + $name . NL. $mime_type . NL. $size . NL. $password . NL. $time . NL . $hash. NL . ($one_time_download ? 'O' : 'R') . NL . time() . - NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C' : 'O')); + NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C' : 'O') + ); fclose($handle); $hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length); $l = s2p("$hash_link"); @@ -627,15 +635,9 @@ function jirafeau_admin_list($name, $file_hash, $link_hash) echo t('LS_FILES'); } echo '</legend>'; - echo '<table border="1" width="1100">'; + echo '<table>'; echo '<tr>'; - echo '<th>' . t('FILENAME') . '</th>'; - echo '<th>' . t('TYPE') . '</th>'; - echo '<th>' . t('SIZE') . '</th>'; - echo '<th>' . t('EXPIRE') . '</th>'; - echo '<th>' . t('ONETIME') . '</th>'; - echo '<th>' . t('UPLOAD_DATE') . '</th>'; - echo '<th>' . t('ORIGIN') . '</th>'; + echo '<th></th>'; echo '<th>' . t('ACTION') . '</th>'; echo '</tr>'; @@ -672,22 +674,15 @@ function jirafeau_admin_list($name, $file_hash, $link_hash) echo '<tr>'; echo '<td>' . '<strong><a id="upload_link" href="f.php?h='. jirafeau_escape($node) .'" title="' . - t('DL_PAGE') . '">' . jirafeau_escape($l['file_name']) . '</a></strong>'; - echo '</td>'; - echo '<td>' . jirafeau_escape($l['mime_type']) . '</td>'; - echo '<td>' . jirafeau_human_size($l['file_size']) . '</td>'; - echo '<td>' . ($l['time'] == -1 ? 'â' : jirafeau_get_datetimefield($l['time'])) . '</td>'; - echo '<td>'; - if ($l['onetime'] == 'O') { - echo 'Y'; - } else { - echo 'N'; - } - echo '</td>'; - echo '<td>' . jirafeau_get_datetimefield($l['upload_date']) . '</td>'; - echo '<td>' . $l['ip'] . '</td>'; - echo '<td>' . - '<form method="post">' . + t('DL_PAGE') . '">' . jirafeau_escape($l['file_name']) . '</a></strong><br/>'; + echo t('TYPE') . ': ' . jirafeau_escape($l['mime_type']) . '<br/>'; + echo t('SIZE') . ': ' . jirafeau_human_size($l['file_size']) . '<br>'; + echo t('EXPIRE') . ': ' . ($l['time'] == -1 ? 'â' : jirafeau_get_datetimefield($l['time'])) . '<br/>'; + echo t('ONETIME') . ': ' . ($l['onetime'] == 'O' ? 'Yes' : 'No') . '<br/>'; + echo t('UPLOAD_DATE') . ': ' . jirafeau_get_datetimefield($l['upload_date']) . '<br/>'; + echo t('ORIGIN') . ': ' . $l['ip'] . '<br/>'; + echo '</td><td>'; + echo '<form method="post">' . '<input type = "hidden" name = "action" value = "download"/>' . '<input type = "hidden" name = "link" value = "' . $node . '"/>' . jirafeau_admin_csrf_field() . @@ -866,6 +861,10 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip) return; } + /* touch empty data file */ + $w_path = $p . $ref . '_data'; + touch($w_path); + /* md5 password or empty */ $password = ''; if (!empty($key)) { @@ -875,11 +874,13 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip) /* Store informations. */ $p .= $ref; $handle = fopen($p, 'w'); - fwrite($handle, - str_replace(NL, '', trim($filename)) . NL . + fwrite( + $handle, + str_replace(NL, '', trim($filename)) . NL . str_replace(NL, '', trim($type)) . NL . $password . NL . $time . NL . ($one_time ? 'O' : 'R') . NL . $ip . NL . - time() . NL . $code . NL); + time() . NL . $code . NL + ); fclose($handle); return $ref . NL . $code ; @@ -937,10 +938,12 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size) /* Update async file. */ $code = jirafeau_gen_random(4); $handle = fopen(VAR_ASYNC . $p . $ref, 'w'); - fwrite($handle, - $a['file_name'] . NL. $a['mime_type'] . NL. $a['key'] . NL . + fwrite( + $handle, + $a['file_name'] . NL. $a['mime_type'] . NL. $a['key'] . NL . $a['time'] . NL . $a['onetime'] . NL . $a['ip'] . NL . - time() . NL . $code . NL); + time() . NL . $code . NL + ); fclose($handle); return $code; } @@ -1004,16 +1007,18 @@ function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_m /* Create link. */ $link_tmp_name = VAR_LINKS . $hash . rand(0, 10000) . '.tmp'; $handle = fopen($link_tmp_name, 'w'); - fwrite($handle, - $a['file_name'] . NL . $a['mime_type'] . NL . $size . NL . + fwrite( + $handle, + $a['file_name'] . NL . $a['mime_type'] . NL . $size . NL . $a['key'] . NL . $a['time'] . NL . $hash . NL . $a['onetime'] . NL . - time() . NL . $a['ip'] . NL . $delete_link_code . NL . ($crypted ? 'C' : 'O')); + time() . NL . $a['ip'] . NL . $delete_link_code . NL . ($crypted ? 'C' : 'O') + ); fclose($handle); $hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length); $l = s2p("$hash_link"); if (!@mkdir(VAR_LINKS . $l, 0755, true) || !rename($link_tmp_name, VAR_LINKS . $l . $hash_link)) { - echo "Error"; + return 'Error'; } /* Clean async upload. */ @@ -1161,7 +1166,8 @@ function jirafeau_challenge_ip($allowedIpList, $challengedIp) * Check if Jirafeau has a restriction on the IP address for uploading. * @return true if uploading is IP restricted, false otherwise. */ -function jirafeau_upload_has_ip_restriction($cfg) { +function jirafeau_upload_has_ip_restriction($cfg) +{ return count($cfg['upload_ip']) > 0; } @@ -1199,7 +1205,7 @@ function jirafeau_challenge_upload_ip_without_password($cfg, $challengedIp) * @param $password password to be challenged * @return true if access is valid, false otherwise. */ -function jirafeau_challenge_upload ($cfg, $ip, $password) +function jirafeau_challenge_upload($cfg, $ip, $password) { return jirafeau_challenge_upload_ip_without_password($cfg, $ip) || (!jirafeau_has_upload_password($cfg) && !jirafeau_upload_has_ip_restriction($cfg)) || @@ -1353,3 +1359,70 @@ function jirafeau_dir_size($dir) } return $size; } + +function jirafeau_export_cfg($cfg) +{ + $content = '<?php' . NL; + $content .= '/* This file was generated by the install process. ' . + 'You can edit it. Please see config.original.php to understand the ' . + 'configuration items. */' . NL; + $content .= '$cfg = ' . var_export($cfg, true) . ';'; + + $fileWrite = file_put_contents(JIRAFEAU_CFG, $content); + + if (false === $fileWrite) { + jirafeau_fatal_error(t('Can not write local configuration file')); + } +} + +function jirafeau_mkdir($path) +{ + return !(!file_exists($path) && !@mkdir($path, 0755)); +} + +/** + * Returns true whether the path is writable or we manage to make it + * so, which essentially is the same thing. + * @param $path is the file or directory to be tested. + * @return true if $path is writable. + */ +function jirafeau_is_writable($path) +{ + /* "@" gets rid of error messages. */ + return is_writable($path) || @chmod($path, 0777); +} + +function jirafeau_check_var_dir($path) +{ + $mkdir_str1 = t('CANNOT_CREATE_DIR') . ':'; + $mkdir_str2 = t('MANUAL_CREATE'); + $write_str1 = t('DIR_NOT_W') . ':'; + $write_str2 = t('You should give the write permission to the web server on ' . + 'this directory.'); + $solution_str = t('HERE_SOLUTION') . ':'; + + if (!jirafeau_mkdir($path) || !jirafeau_is_writable($path)) { + return array('has_error' => true, + 'why' => $mkdir_str1 . '<br /><code>' . + $path . '</code><br />' . $solution_str . + '<br />' . $mkdir_str2); + } + + foreach (array('files', 'links', 'async') as $subdir) { + $subpath = $path.$subdir; + + if (!jirafeau_mkdir($subpath) || !jirafeau_is_writable($subpath)) { + return array('has_error' => true, + 'why' => $mkdir_str1 . '<br /><code>' . + $subpath . '</code><br />' . $solution_str . + '<br />' . $mkdir_str2); + } + } + + return array('has_error' => false, 'why' => ''); +} + +function jirafeau_add_ending_slash($path) +{ + return $path . ((substr($path, -1) == '/') ? '' : '/'); +}