+ $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;
+}
+
+
+/**
+ * Clean old async transferts.
+ * @return number of cleaned files.
+ */
+function
+jirafeau_admin_clean_async ()
+{
+ $count = 0;
+ /* Get all links files. */
+ $stack = array (VAR_ASYNC);
+ 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 async informations. */
+ $a = jirafeau_get_async_ref (basename ($node));
+ if (!count ($a))
+ continue;
+ /* Delete transferts older than 1 hour. */
+ if (date ('U') - $a['last_edited'] > 3600)
+ {
+ jirafeau_async_delete (basename ($node));
+ $count++;
+ }
+ }
+ }
+ }
+ return $count;
+}
+/**
+ * Read async transfert informations
+ * @return array containing informations.
+ */
+function
+jirafeau_get_async_ref ($ref)
+{
+ $out = array ();
+ $refinfos = VAR_ASYNC . s2p ("$ref") . "$ref";
+
+ if (!file_exists ($refinfos))
+ return $out;
+
+ $c = file ($refinfos);
+ $out['file_name'] = trim ($c[0]);
+ $out['mime_type'] = trim ($c[1]);
+ $out['key'] = trim ($c[2], NL);
+ $out['time'] = trim ($c[3]);
+ $out['onetime'] = trim ($c[4]);
+ $out['ip'] = trim ($c[5]);
+ $out['last_edited'] = trim ($c[6]);
+ $out['next_code'] = trim ($c[7]);
+ return $out;
+}
+
+/**
+ * Delete async transfert informations
+ */
+function
+jirafeau_async_delete ($ref)
+{
+ $p = s2p ("$ref");
+ if (file_exists (VAR_ASYNC . $p . $ref))
+ unlink (VAR_ASYNC . $p . $ref);
+ if (file_exists (VAR_ASYNC . $p . $ref . '_data'))
+ unlink (VAR_ASYNC . $p . $ref . '_data');
+ $parse = VAR_ASYNC . $p;
+ $scan = array();
+ while (file_exists ($parse)
+ && ($scan = scandir ($parse))
+ && count ($scan) == 2 // '.' and '..' folders => empty.
+ && basename ($parse) != basename (VAR_ASYNC))
+ {
+ rmdir ($parse);
+ $parse = substr ($parse, 0, strlen($parse) - strlen(basename ($parse)) - 1);
+ }
+}
+
+/**
+ * Init a new asynchronous upload.
+ * @param $finename Name of the file to send
+ * @param $one_time One time upload parameter
+ * @param $key eventual password (or blank)
+ * @param $time time limit
+ * @param $ip ip address of the client
+ * @return a string containing a temporary reference followed by a code or the string "Error"
+ */
+function
+jirafeau_async_init ($filename, $type, $one_time, $key, $time, $ip)
+{
+ $res = 'Error';