+
+function jirafeau_escape($string)
+{
+ return htmlspecialchars($string, ENT_QUOTES);
+}
+
+function jirafeau_admin_session_start()
+{
+ $_SESSION['admin_auth'] = true;
+ $_SESSION['admin_csrf'] = md5(uniqid(mt_rand(), true));
+}
+
+function jirafeau_session_end()
+{
+ $_SESSION = array();
+ session_destroy();
+}
+
+function jirafeau_admin_session_logged()
+{
+ return isset($_SESSION['admin_auth']) &&
+ isset($_SESSION['admin_csrf']) &&
+ isset($_POST['admin_csrf']) &&
+ $_SESSION['admin_auth'] === true &&
+ $_SESSION['admin_csrf'] === $_POST['admin_csrf'];
+}
+
+function jirafeau_admin_csrf_field()
+{
+ return "<input type='hidden' name='admin_csrf' value='". $_SESSION['admin_csrf'] . "'/>";
+}
+
+function jirafeau_user_session_start()
+{
+ $_SESSION['user_auth'] = true;
+}
+
+function jirafeau_user_session_logged()
+{
+ return isset($_SESSION['user_auth']) &&
+ $_SESSION['user_auth'] === true;
+}
+
+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;
+}
+
+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) == '/') ? '' : '/');
+}
+
+function jirafeau_default_web_root()
+{
+ $url_scheme = (isset($_SERVER['HTTPS'])) ? 'https://' : 'http://';
+ return $url_scheme . $_SERVER['HTTP_HOST'] . str_replace('install.php', '', $_SERVER['REQUEST_URI']);
+}
+
+function jirafeau_get_download_stats($hash)
+{
+ $filename = VAR_LINKS . s2p("$hash") . $hash . '_download';
+
+ if (!file_exists($filename)) {
+ return array('count'=>0);
+ }
+
+ $c = file($filename);
+ $data['count'] = trim($c[0]);
+ $data['date'] = trim($c[1]);
+ $data['ip'] = trim($c[2]);
+
+ return $data;
+}
+
+function jirafeau_write_download_stats($hash, $ip)
+{
+ $data = jirafeau_get_download_stats($hash);
+ $count = $data['count'];
+ $count++;
+
+ $filename = VAR_LINKS . s2p("$hash") . $hash . '_download';
+
+ $handle = fopen($filename, 'w');
+ fwrite($handle, $count . NL . time() . NL . $ip);
+ fclose($handle);
+}
+
+function jirafeau_create_upload_finished_box($preview = true) {
+ ?>
+
+ <div id="upload_finished">
+ <p><?php echo t('FILE_UP') ?></p>
+
+ <div id="upload_finished_download_page">
+ <p>
+ <a id="upload_link" href=""><?php echo t('DL_PAGE') ?></a>
+ <a id="upload_link_email" href=""><img id="upload_image_email"/></a>
+ </p><p>
+ <code id=upload_link_text></code>
+ <button id="upload_link_button">📋</button>
+ </p>
+ </div>
+
+ <?php if ($preview == true) {
+ ?>
+ <div id="upload_finished_preview">
+ <p>
+ <a id="preview_link" href=""><?php echo t('VIEW_LINK') ?></a>
+ </p><p>
+ <code id=preview_link_text></code>
+ <button id="preview_link_button">📋</button>
+ </p>
+ </div>
+ <?php
+} ?>
+
+ <div id="upload_direct_download">
+ <p>
+ <a id="direct_link" href=""><?php echo t('DIRECT_DL') ?></a>
+ </p><p>
+ <code id=direct_link_text></code>
+ <button id="direct_link_button">📋</button>
+ </p>
+ </div>
+
+ <div id="upload_delete">
+ <p>
+ <a id="delete_link" href=""><?php echo t('DELETE_LINK') ?></a>
+ </p><p>
+ <code id=delete_link_text></code>
+ <button id="delete_link_button">📋</button>
+ </p>
+ </div>
+
+ <div id="upload_validity">
+ <p><?php echo t('VALID_UNTIL'); ?>:</p>
+ <p id="date"></p>
+ </div>
+</div>
+<?php
+}
+
+function jirafeau_get_expiration_time_options() {
+ return
+ array(
+ array(
+ 'value' => 'minute',
+ 'label' => '1_MIN'
+ ),
+ array(
+ 'value' => 'hour',
+ 'label' => '1_H'
+ ),
+ array(
+ 'value' => 'day',
+ 'label' => '1_D'
+ ),
+ array(
+ 'value' => 'week',
+ 'label' => '1_W'
+ ),
+ array(
+ 'value' => 'fortnight',
+ 'label' => '2_W'
+ ),
+ array(
+ 'value' => 'month',
+ 'label' => '1_M'
+ ),
+ array(
+ 'value' => 'quarter',
+ 'label' => '1_Q'
+ ),
+ array(
+ 'value' => 'year',
+ 'label' => '1_Y'
+ ),
+ array(
+ 'value' => 'none',
+ 'label' => 'NONE'
+ )
+ );
+}
+
+
+
+ /**
+ *
+ * creates the time selection field
+ * @param mixed $cfg
+ * @return void
+ */
+ function jirafeau_create_selection_array($cfg) {
+ echo
+ '<select name="time" id="select_time">';
+
+
+ $expirationTimeOptions = jirafeau_get_expiration_time_options();
+
+ foreach ($expirationTimeOptions as $expirationTimeOption) {
+ $selected = ($expirationTimeOption['value'] === $cfg['availability_default'])? 'selected="selected"' : '';
+ if (true === $cfg['availabilities'][$expirationTimeOption['value']]) {
+ echo '<option value="' . $expirationTimeOption['value'] . '" ' .
+ $selected . '>' . t($expirationTimeOption['label']) . '</option>';
+ }
+ }
+ echo '</select>';
+ }
+
+ function jirafeau_datestr_to_int ($time_str) {
+ $time = time();
+ switch ($time_str) {
+ case 'minute':
+ $time += JIRAFEAU_MINUTE;
+ break;
+ case 'hour':
+ $time += JIRAFEAU_HOUR;
+ break;
+ case 'day':
+ $time += JIRAFEAU_DAY;
+ break;
+ case 'week':
+ $time += JIRAFEAU_WEEK;
+ break;
+ case 'fortnight':
+ $time += JIRAFEAU_FORTNIGHT;
+ break;
+ case 'month':
+ $time += JIRAFEAU_MONTH;
+ break;
+ case 'quarter':
+ $time += JIRAFEAU_QUARTER;
+ break;
+ case 'year':
+ $time += JIRAFEAU_YEAR;
+ break;
+ default:
+ $time = JIRAFEAU_INFINITY;
+ break;
+ }
+ return $time;
+}
+
+
+
+
+/**
+ * links or copy a local file
+ * TODO: boolean in config for linking
+ * @param string $filepath
+ * @param $one_time_download is the file a one time download ?
+ * @param $key if not empty, protect the file with this key
+ * @param $time the time of validity of the file
+ * @param $ip uploader's ip
+ * @param $crypt boolean asking to crypt or not
+ * @param $link_name_length size of the link name
+ * @returns an array containing some information
+ * 'error' => information on possible errors
+ * 'link' => the link name of the uploaded file
+ * 'delete_link' => the link code to delete file
+ */
+function jirafeau_copy_local_file($local_file_path, $one_time_download, $key, $time, $ip, $crypt, $link_name_length, $file_hash_method) {
+
+ if (!file_exists($local_file_path)) {
+ return (array(
+ 'error' =>
+ array('has_error' => true,
+ 'why' => t('INTERNAL_ERROR_FILE_NOT_EXIST')),
+ 'link' =>'',
+ 'delete_link' => ''));
+ }
+ if(
+ // sanity check if file can be opened
+ $file = fopen($local_file_path, "r")
+ )
+ {
+ // close file pointer - it's not needed here
+ fclose($file);
+ $time_in_int = jirafeau_datestr_to_int($time);
+ return jirafeau_add_file(
+ jirafeau_create_file_array($local_file_path),
+ $one_time_download,
+ $key,
+ $time_in_int,
+ $ip,
+ $crypt,
+ $link_name_length,
+ $file_hash_method,
+ false
+ );
+ }
+ else {
+ return (array(
+ 'error' =>
+ array('has_error' => true,
+ 'why' => t('INTERNAL_ERROR_FP_OPEN_LOCAL')),
+ 'link' =>'',
+ 'delete_link' => ''));
+ }
+
+}
+
+
+function jirafeau_create_file_array($file_path) {
+ return
+ [
+ 'type' => mime_content_type($file_path),
+ 'tmp_name' => $file_path,
+ 'name' => basename($file_path),
+ 'size' => filesize($file_path),
+ ];
+}