+/**
+ * Read link informations
+ * @return array containing informations.
+ */
+function
+jirafeau_get_link ($hash)
+{
+ $out = array ();
+ $link = VAR_LINKS . s2p ("$hash") . $hash;
+
+ if (!file_exists ($link))
+ return $out;
+
+ $c = file ($link);
+ $out['file_name'] = trim ($c[0]);
+ $out['mime_type'] = trim ($c[1]);
+ $out['file_size'] = trim ($c[2]);
+ $out['key'] = trim ($c[3], NL);
+ $out['time'] = trim ($c[4]);
+ $out['md5'] = trim ($c[5]);
+ $out['onetime'] = trim ($c[6]);
+ $out['upload_date'] = trim ($c[7]);
+ $out['ip'] = trim ($c[8]);
+ $out['link_code'] = trim ($c[9]);
+
+ return $out;
+}
+
+/**
+ * List files in admin interface.
+ */
+function
+jirafeau_admin_list ($name, $file_hash, $link_hash)
+{
+ echo '<fieldset><legend>';
+ if (!empty ($name))
+ echo $name . ' ';
+ if (!empty ($file_hash))
+ echo $file_hash . ' ';
+ if (!empty ($link_hash))
+ echo $link_hash . ' ';
+ if (empty ($name) && empty ($file_hash) && empty ($link_hash))
+ echo t('List all files');
+ echo '</legend>';
+ echo '<table>';
+ echo '<tr>';
+ echo '<td>' . t('Filename') . '</td>';
+ echo '<td>' . t('Type') . '</td>';
+ echo '<td>' . t('Size') . '</td>';
+ echo '<td>' . t('Expire') . '</td>';
+ echo '<td>' . t('Onetime') . '</td>';
+ echo '<td>' . t('Upload date') . '</td>';
+ echo '<td>' . t('Origin') . '</td>';
+ echo '<td>' . t('Action') . '</td>';
+ echo '</tr>';
+
+ /* Get all links files. */
+ $stack = array (VAR_LINKS);
+ while (($d = array_shift ($stack)) && $d != NULL)
+ {
+ $dir = scandir ($d);
+ foreach ($dir as $node)
+ {
+ if (strcmp ($node, '.') == 0 || strcmp ($node, '..') == 0 ||
+ preg_match ('/\.tmp/i', "$node"))
+ continue;
+ if (is_dir ($d . $node))
+ {
+ /* Push new found directory. */
+ $stack[] = $d . $node . '/';
+ }
+ elseif (is_file ($d . $node))
+ {
+ /* Read link informations. */
+ $l = jirafeau_get_link ($node);
+ if (!count ($l))
+ continue;
+
+ /* Filter. */
+ if (!empty ($name) && $name != $l['file_name'])
+ continue;
+ if (!empty ($file_hash) && $file_hash != $l['md5'])
+ continue;
+ if (!empty ($link_hash) && $link_hash != $link)
+ continue;
+ /* Print link informations. */
+ echo '<tr>';
+ echo '<td>' .
+ '<form action = "admin.php" method = "post">' .
+ '<input type = "hidden" name = "action" value = "download"/>' .
+ '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
+ '<input type = "submit" value = "' . $l['file_name'] . '" />' .
+ '</form>';
+ echo '</td>';
+ echo '<td>' . $l['mime_type'] . '</td>';
+ echo '<td>' . jirafeau_human_size ($l['file_size']) . '</td>';
+ echo '<td>' . ($l['time'] == -1 ? '' : strftime ('%c', $l['time'])) .
+ '</td>';
+ echo '<td>' . $l['onetime'] . '</td>';
+ echo '<td>' . strftime ('%c', $l['upload_date']) . '</td>';
+ echo '<td>' . $l['ip'] . '</td>';
+ echo '<td>' .
+ '<form action = "admin.php" method = "post">' .
+ '<input type = "hidden" name = "action" value = "delete_link"/>' .
+ '<input type = "hidden" name = "link" value = "' . $node . '"/>' .
+ '<input type = "submit" value = "' . t('Del link') . '" />' .
+ '</form>' .
+ '<form action = "admin.php" method = "post">' .
+ '<input type = "hidden" name = "action" value = "delete_file"/>' .
+ '<input type = "hidden" name = "md5" value = "' . $l['md5'] . '"/>' .
+ '<input type = "submit" value = "' . t('Del file and links') . '" />' .
+ '</form>' .
+ '</td>';
+ echo '</tr>';
+ }
+ }
+ }
+ echo '</table></fieldset>';
+}
+
+/**
+ * Clean expired files.
+ * @return number of cleaned files.
+ */
+function
+jirafeau_admin_clean ()
+{
+ $count = 0;
+ /* Get all links files. */
+ $stack = array (VAR_LINKS);
+ while (($d = array_shift ($stack)) && $d != NULL)
+ {
+ $dir = scandir ($d);
+
+ foreach ($dir as $node)
+ {
+ if (strcmp ($node, '.') == 0 || strcmp ($node, '..') == 0 ||
+ preg_match ('/\.tmp/i', "$node"))
+ continue;
+
+ if (is_dir ($d . $node))
+ {
+ /* Push new found directory. */
+ $stack[] = $d . $node . '/';
+ }
+ elseif (is_file ($d . $node))
+ {
+ /* Read link informations. */
+ $l = jirafeau_get_link (basename ($node));
+ if (!count ($l))
+ continue;
+ $p = s2p ($l['md5']);
+ if ($l['time'] > 0 && $l['time'] < time () || // expired
+ !file_exists (VAR_FILES . $p . $l['md5']) || // invalid
+ !file_exists (VAR_FILES . $p . $l['md5'] . '_count')) // invalid
+ {
+ jirafeau_delete_link ($node);
+ $count++;
+ }
+ }
+ }
+ }
+ return $count;
+}