/**
  * Moves uploaded files from temporary upload folder to a specified new folder.
  * This enables you to move the files from a successful submission to another folder and clean the files in temporary upload folder from time to time.
  *
  * TypoScript example:
  *
  * 1. Set the temporary upload folder
  * <code>
  * plugin.Tx_Formhandler.settings.files.tmpUploadFolder = uploads/formhandler/tmp
  * </code>
  *
  * 2. Set the folder to move the files to after submission
  * <code>
  * plugin.Tx_Formhandler.settings.finishers.1.class = Tx_Formhandler_Finisher_StoreUploadedFiles
  * plugin.Tx_Formhandler.settings.finishers.1.config.finishedUploadFolder = uploads/formhandler/finishedFiles/
  * plugin.Tx_Formhandler.settings.finishers.1.config.renameScheme = [filename]_[md5]_[time]
  * </code>
  *
  * @return void
  */
 protected function moveUploadedFiles()
 {
     $newFolder = $this->settings['finishedUploadFolder'];
     $newFolder = Tx_Formhandler_StaticFuncs::sanitizePath($newFolder);
     $uploadPath = Tx_Formhandler_StaticFuncs::getDocumentRoot() . $newFolder;
     $sessionFiles = Tx_Formhandler_Globals::$session->get('files');
     if (is_array($sessionFiles) && !empty($sessionFiles) && strlen($newFolder) > 0) {
         foreach ($sessionFiles as $field => $files) {
             $this->gp[$field] = array();
             foreach ($files as $key => $file) {
                 if ($file['uploaded_path'] != $uploadPath) {
                     $newFilename = $this->getNewFilename($file['uploaded_name']);
                     Tx_Formhandler_StaticFuncs::debugMessage('copy_file', array($file['uploaded_path'] . $file['uploaded_name'], $uploadPath . $newFilename));
                     copy($file['uploaded_path'] . $file['uploaded_name'], $uploadPath . $newFilename);
                     t3lib_div::fixPermissions($uploadPath . $newFilename);
                     unlink($file['uploaded_path'] . $file['uploaded_name']);
                     $sessionFiles[$field][$key]['uploaded_path'] = $uploadPath;
                     $sessionFiles[$field][$key]['uploaded_name'] = $newFilename;
                     $sessionFiles[$field][$key]['uploaded_folder'] = $newFolder;
                     $sessionFiles[$field][$key]['uploaded_url'] = t3lib_div::getIndpEnv('TYPO3_SITE_URL') . $newFolder . $newFilename;
                     if (!is_array($this->gp[$field])) {
                         $this->gp[$field] = array();
                     }
                     array_push($this->gp[$field], $newFilename);
                 }
             }
         }
         Tx_Formhandler_Globals::$session->set('files', $sessionFiles);
     }
 }
 /**
  * Deletes all files older than a specific time in a temporary upload folder.
  * Settings for the threshold time and the folder are made in TypoScript.
  *
  * Here is an example:
  * <code>
  * plugin.Tx_Formhandler.settings.files.clearTempFilesOlderThanHours = 24
  * plugin.Tx_Formhandler.settings.files.tmpUploadFolder = uploads/formhandler/tmp
  * </code>
  *
  * @param integer $olderThan Delete files older than $olderThan hours.
  * @return void
  * @author	Reinhard Führicht <*****@*****.**>
  */
 protected function clearTempFiles($uploadFolder, $olderThanValue, $olderThanUnit)
 {
     if (!$olderThanValue) {
         return;
     }
     //build absolute path to upload folder
     $path = Tx_Formhandler_StaticFuncs::getDocumentRoot() . $uploadFolder;
     //read files in directory
     $tmpFiles = t3lib_div::getFilesInDir($path);
     Tx_Formhandler_StaticFuncs::debugMessage('cleaning_temp_files', array($path));
     //calculate threshold timestamp
     //hours * 60 * 60 = millseconds
     $threshold = Tx_Formhandler_StaticFuncs::getTimestamp($olderThanValue, $olderThanUnit);
     //for all files in temp upload folder
     foreach ($tmpFiles as $idx => $file) {
         //if creation timestamp is lower than threshold timestamp
         //delete the file
         $creationTime = filemtime($path . $file);
         //fix for different timezones
         $creationTime += date('O') / 100 * 60;
         if ($creationTime < $threshold) {
             unlink($path . $file);
             Tx_Formhandler_StaticFuncs::debugMessage('deleting_file', array($file));
         }
     }
 }