/**
  * Processes uploaded files, moves them to a temporary upload folder, renames them if they already exist and
  * stores the information in user session
  *
  *
  * @return void
  * @author	Reinhard Führicht <*****@*****.**>
  */
 protected function processFiles()
 {
     $sessionFiles = Tx_Formhandler_Globals::$session->get('files');
     $tempFiles = $sessionFiles;
     //if files were uploaded
     if (isset($_FILES) && is_array($_FILES) && !empty($_FILES)) {
         //get upload folder
         $uploadFolder = Tx_Formhandler_StaticFuncs::getTempUploadFolder();
         //build absolute path to upload folder
         $uploadPath = Tx_Formhandler_StaticFuncs::getTYPO3Root() . $uploadFolder;
         if (!file_exists($uploadPath)) {
             Tx_Formhandler_StaticFuncs::debugMessage('folder_doesnt_exist', array($uploadPath), 3);
             return;
         }
         //for all file properties
         foreach ($_FILES as $sthg => $files) {
             //if a file was uploaded
             if (isset($files['name']) && is_array($files['name'])) {
                 //for all file names
                 foreach ($files['name'] as $field => $name) {
                     if (!isset($this->errors[$field])) {
                         $exists = FALSE;
                         if (is_array($sessionFiles[$field])) {
                             foreach ($sessionFiles[$field] as $idx => $fileOptions) {
                                 if ($fileOptions['name'] === $name) {
                                     $exists = TRUE;
                                 }
                             }
                         }
                         if (!$exists) {
                             $filename = substr($name, 0, strpos($name, '.'));
                             if (strlen($filename) > 0) {
                                 $ext = substr($name, strpos($name, '.'));
                                 $suffix = 1;
                                 $filename = str_replace(' ', '_', $filename);
                                 //build file name
                                 $uploadedFileName = $filename . $ext;
                                 //rename if exists
                                 while (file_exists($uploadPath . $uploadedFileName)) {
                                     $uploadedFileName = $filename . '_' . $suffix . $ext;
                                     $suffix++;
                                 }
                                 $files['name'][$field] = $uploadedFileName;
                                 //move from temp folder to temp upload folder
                                 move_uploaded_file($files['tmp_name'][$field], $uploadPath . $uploadedFileName);
                                 t3lib_div::fixPermissions($uploadPath . $uploadedFileName);
                                 $files['uploaded_name'][$field] = $uploadedFileName;
                                 //set values for session
                                 $tmp['name'] = $name;
                                 $tmp['uploaded_name'] = $uploadedFileName;
                                 $tmp['uploaded_path'] = $uploadPath;
                                 $tmp['uploaded_folder'] = $uploadFolder;
                                 $uploadedUrl = t3lib_div::getIndpEnv('TYPO3_SITE_URL') . $uploadFolder . $uploadedFileName;
                                 $uploadedUrl = str_replace('//', '/', $uploadedUrl);
                                 $tmp['uploaded_url'] = $uploadedUrl;
                                 $tmp['size'] = $files['size'][$field];
                                 $tmp['type'] = $files['type'][$field];
                                 if (!is_array($tempFiles[$field]) && strlen($field) > 0) {
                                     $tempFiles[$field] = array();
                                 }
                                 array_push($tempFiles[$field], $tmp);
                                 if (!is_array($this->gp[$field])) {
                                     $this->gp[$field] = array();
                                 }
                                 array_push($this->gp[$field], $uploadedFileName);
                             }
                         }
                     }
                 }
             }
         }
     }
     Tx_Formhandler_Globals::$session->set('files', $tempFiles);
     Tx_Formhandler_StaticFuncs::debugMessage('Files:', array(), 1, (array) $tempFiles);
 }
 /**
  * Searches for upload folder settings in TypoScript setup.
  * If no settings is found, the default upload folder is set.
  *
  * Here is an example:
  * <code>
  * plugin.Tx_Formhandler.settings.files.tmpUploadFolder = uploads/formhandler/tmp
  * </code>
  *
  * The default upload folder is: '/uploads/formhandler/tmp/'
  *
  * @return void
  * @static
  * @author	Reinhard Führicht <*****@*****.**>
  */
 public static function getTempUploadFolder()
 {
     //set default upload folder
     $uploadFolder = '/uploads/formhandler/tmp/';
     //if temp upload folder set in TypoScript, take that setting
     $settings = Tx_Formhandler_Globals::$session->get('settings');
     if ($settings['files.']['uploadFolder']) {
         $uploadFolder = Tx_Formhandler_StaticFuncs::getSingle($settings['files.'], 'uploadFolder');
         $uploadFolder = Tx_Formhandler_StaticFuncs::sanitizePath($uploadFolder);
     }
     //if the set directory doesn't exist, print a message and try to create
     if (!is_dir(Tx_Formhandler_StaticFuncs::getTYPO3Root() . $uploadFolder)) {
         Tx_Formhandler_StaticFuncs::debugMessage('folder_doesnt_exist', array(Tx_Formhandler_StaticFuncs::getTYPO3Root() . '/' . $uploadFolder), 2);
         t3lib_div::mkdir_deep(Tx_Formhandler_StaticFuncs::getTYPO3Root() . '/', $uploadFolder);
     }
     return $uploadFolder;
 }