/**
  * Prepare data before executing controller save function
  * @param mixed $data Form data (passed by reference)
  * @param unknown $files File to upload
  */
 protected function prepareDataBeforeSave(&$data, $files)
 {
     $user = JFactory::getUser();
     $param = JComponentHelper::getParams('com_simplefilemanager');
     // Check if it's a new item
     $isNew = $data["file_name"] == "" ? 1 : 0;
     // Check if no file has been selected
     if ($isNew and $files['test'][0]["size"] <= 0) {
         JFactory::getApplication()->enqueueMessage(JText::_('COM_SIMPLEFILEMANAGER_FIELD_NOFILE_ERROR'), 'error');
         return;
     }
     // Import Joomla!'s file library
     jimport('joomla.filesystem.file');
     // Check if item already have an old file and delete it
     if (!$isNew and $data["file_size"] > 0) {
         if (SimplefilemanagerHelper::deleteFile($data["file_name"])) {
             $path_parts = pathinfo($data["file_name"]);
             rmdir($path_parts['dirname']);
         }
     }
     // Check if item needs a file upload
     if ($files['test'][0]["size"]) {
         // Set up the source and destination of the file
         $src = $files['test'][0]['tmp_name'];
         // Check file extension
         if (!SimplefilemanagerHelper::hasSafeExtension($files['test'][0]["name"])) {
             JFactory::getApplication()->enqueueMessage(JText::_('COM_SIMPLEFILEMANAGER_FIELD_DANGEROUS_FILE_ERROR'), 'error');
             return;
         }
         // File upload
         $upload = SimplefilemanagerHelper::uploadFile($files['test'][0]["tmp_name"], $files['test'][0]["name"]);
         if (!$upload) {
             JFactory::getApplication()->enqueueMessage(JText::_('COM_SIMPLEFILEMANAGER_FIELD_UPLOAD_ERROR'), 'error');
             return;
         }
         $data["file_name"] = $upload;
         // Set MD5 Hash
         $data["md5hash"] = md5_file($data["file_name"]);
         $data["file_size"] = $files['test'][0]["size"];
         // Send email
         if ($param->get('sendmail') == 1 and $isNew == 1 and $data["reserved_user"] > 0 and $data["visibility"] == 3) {
             $send = SimplefilemanagerHelper::sendMail($data["reserved_user"]);
             if (!send) {
                 JFactory::getApplication()->enqueueMessage(JText::_('JERROR') . ": " . $send->__toString(), 'error');
             }
         }
     }
     if ($data["title"] == "") {
         $data["title"] = JFile::stripExt($files['test'][0]["name"]);
     }
     if (!$data["author"]) {
         $data["author"] = JFactory::getUser()->id;
     }
     // Suspend publication if user not authorized to edit state
     if (!$user->authorise('core.manage', 'com_simplefilemanager')) {
         $data["state"] = 0;
     }
     // Updating dates
     if ($isNew) {
         jimport('joomla.utilities.date');
         $date =& JFactory::getDate('now');
         $data["file_created"] = $date->format('Y-m-d H:m:s', false);
     }
     if (!$data["md5hash"]) {
         $data["md5hash"] = md5_file($data["file_name"]);
     }
     return;
 }
 public function save($key = null, $urlVar = null)
 {
     // Load libraries
     jimport('joomla.filesystem.file');
     jimport('joomla.utilities.date');
     // Load data
     $this->form = JFactory::getApplication()->input->post->get('jform', null, 'RAW');
     $this->files = JFactory::getApplication()->input->files->get('jform1');
     $this->params = JComponentHelper::getParams('com_simplefilemanager');
     // Check if user is creating a new file
     $isNew = $this->form["file_name"] ? false : true;
     // Checking file selection
     if ($this->files['test'][0]["size"]) {
         // Checking file extension
         if (!SimplefilemanagerHelper::hasSafeExtension($this->files['test'][0]["name"])) {
             JFactory::getApplication()->enqueueMessage(JText::_('COM_SIMPLEFILEMANAGER_FIELD_DANGEROUS_FILE_ERROR'), 'error');
             parent::save($key, $urlVar);
             return;
         }
         // Deleting previous file
         if ($this->form["file_name"]) {
             SimplefilemanagerHelper::deleteFile($this->form["file_name"]);
         }
         // File upload
         $upload = SimplefilemanagerHelper::uploadFile($this->files['test'][0]["tmp_name"], $this->files['test'][0]["name"]);
         if (!$upload or $this->form["file_name"] == $upload) {
             JFactory::getApplication()->enqueueMessage(JText::_('COM_SIMPLEFILEMANAGER_FIELD_UPLOAD_ERROR'), 'error');
             parent::save($key, $urlVar);
             return;
         }
         $this->form["file_name"] = $upload;
         $this->form["file_size"] = $this->files['test'][0]["size"];
         $this->form["md5hash"] = md5_file($this->form["file_name"]);
         if (!$this->form["file_created"] or !$this->form["author"]) {
             $this->form["file_created"] =& JFactory::getDate('now')->format('Y-m-d H:m:s', false);
             $this->form["author"] = JFactory::getUser()->id;
         }
     } elseif ($isNew) {
         JFactory::getApplication()->enqueueMessage(JText::_('COM_SIMPLEFILEMANAGER_FIELD_NOFILE_ERROR'), 'error');
         parent::save($key, $urlVar);
         return;
     } else {
         if ($this->task == "save2copy") {
             // File copy
             $copy = SimplefilemanagerHelper::copyFile($this->form["file_name"]);
             if ($copy === false) {
                 throw new Exception(JText::sprintf('COM_SIMPLEFILEMANAGER_FIELD_COPY_ERROR', $this->form["file_name"]), 500);
             }
             $this->form["file_name"] = $copy;
             $this->form["file_created"] = JFactory::getDate('now')->format('Y-m-d H:m:s', false);
             $this->form["author"] = JFactory::getUser()->id;
         }
     }
     // If save2copy, controller will automatically change items's id but won't change its publish state
     if ($this->task == "save2copy") {
         $this->form["state"] = 0;
     }
     // Send notify email
     if ($isNew and $this->params->get('sendmail') or isset($this->form["fl_send_mail"])) {
         SimplefilemanagerHelper::sendMail($this->form);
     }
     // Save data back to the $_POST global variable
     JFactory::getApplication()->input->post->set('jform', $this->form);
     parent::save($key, $urlVar);
 }