Example #1
0
 /**
  * Upload a file
  *
  * @since 1.5
  */
 function upload()
 {
     $dispatcher = JDispatcher::getInstance();
     $params = JComponentHelper::getParams('com_playjoom');
     // Check for request forgeries
     if (!JSession::checkToken('request')) {
         $response = array('status' => '0', 'error' => JText::_('JINVALID_TOKEN'));
         echo json_encode($response);
         return;
     }
     // Get the user
     $user = JFactory::getUser();
     $input = JFactory::getApplication()->input;
     JLog::addLogger(array('text_file' => 'upload.error.php'), JLog::ALL, array('upload'));
     // Get some data from the request
     $file = JRequest::getVar('Filedata', '', 'files', 'array');
     $folder = JRequest::getVar('folder', '', 'path');
     $return = $input->post->get('return-url', null, 'base64');
     $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Start uploading file.json: ' . $folder . DIRECTORY_SEPARATOR . $file['name'], 'priority' => JLog::INFO, 'section' => 'admin')));
     if ($_SERVER['CONTENT_LENGTH'] > $params->get('upload_maxsize', 100) * 1024 * 1024 || $_SERVER['CONTENT_LENGTH'] > (int) ini_get('upload_max_filesize') * 1024 * 1024 || $_SERVER['CONTENT_LENGTH'] > (int) ini_get('post_max_size') * 1024 * 1024 || $_SERVER['CONTENT_LENGTH'] > (int) ini_get('memory_limit') * 1024 * 1024) {
         $response = array('status' => '0', 'error' => JText::_('COM_PLAYJOOM_ERROR_WARNFILETOOLARGE'));
         echo json_encode($response);
         return;
     }
     // Set FTP credentials, if given
     JClientHelper::setCredentialsFromRequest('ftp');
     // Make the filename safe
     $file['name'] = JFile::makeSafe($file['name']);
     if (isset($file['name'])) {
         // The request is valid
         $err = null;
         $filepath = JPath::clean(PLAYJOOM_BASE_PATH . '/' . $folder . '/' . strtolower($file['name']));
         $allowableExtensions = $params->get('upload_audio_extensions', 'mp3,wav,flac');
         if (!PlayJoomMediaHelper::canUpload($file, $err, $allowableExtensions)) {
             JLog::add('Invalid: ' . $filepath . ': ' . $err, JLog::INFO, 'upload');
             $response = array('status' => '0', 'error' => JText::_($err));
             echo json_encode($response);
             return;
         }
         // Trigger the onContentBeforeSave event.
         JPluginHelper::importPlugin('content');
         $dispatcher = JEventDispatcher::getInstance();
         $object_file = new JObject($file);
         $object_file->filepath = $filepath;
         $result = $dispatcher->trigger('onContentBeforeSave', array('com_playjoom.file', &$object_file));
         if (in_array(false, $result, true)) {
             // There are some errors in the plugins
             JLog::add('Errors before save: ' . $filepath . ' : ' . implode(', ', $object_file->getErrors()), JLog::INFO, 'upload');
             $response = array('status' => '0', 'error' => JText::plural('COM_PLAYJOOM_ERROR_BEFORE_SAVE', count($errors = $object_file->getErrors()), implode('<br />', $errors)));
             echo json_encode($response);
             return;
         }
         if (JFile::exists($filepath)) {
             // File exists
             JLog::add('File exists: ' . $filepath . ' by user_id ' . $user->id, JLog::INFO, 'upload');
             $response = array('status' => '0', 'error' => JText::_('COM_PLAYJOOM_ERROR_FILE_EXISTS'));
             echo json_encode($response);
             return;
         } elseif (!$user->authorise('core.create', 'com_playjoom')) {
             // File does not exist and user is not authorised to create
             JLog::add('Create not permitted: ' . $filepath . ' by user_id ' . $user->id, JLog::INFO, 'upload');
             $response = array('status' => '0', 'error' => JText::_('COM_PLAYJOOM_ERROR_CREATE_NOT_PERMITTED'));
             echo json_encode($response);
             return;
         }
         $file = (array) $object_file;
         if (!JFile::upload($file['tmp_name'], $file['filepath'])) {
             // Error in upload
             JLog::add('Error on upload: ' . $filepath, JLog::INFO, 'upload');
             $response = array('status' => '0', 'error' => JText::_('COM_PLAYJOOM_ERROR_UNABLE_TO_UPLOAD_FILE'));
             echo json_encode($response);
             return;
         } else {
             // Trigger the onContentAfterSave event.
             $dispatcher->trigger('onContentAfterSave', array('com_playjoom.file', &$object_file, true));
             JLog::add($folder, JLog::INFO, 'upload');
             $response = array('status' => '1', 'error' => JText::sprintf('COM_PLAYJOOM_UPLOAD_COMPLETE', substr($file['filepath'], strlen(PLAYJOOM_BASE_PATH))));
             echo json_encode($response);
             return;
         }
     } else {
         $response = array('status' => '0', 'error' => JText::_('COM_PLAYJOOM_ERROR_BAD_REQUEST'));
         echo json_encode($response);
         return;
     }
 }
Example #2
0
 /**
  * Method for uploading a file
  *
  * @since 1.5
  * @return void
  */
 function save()
 {
     $dispatcher = JDispatcher::getInstance();
     $params = JComponentHelper::getParams('com_playjoom');
     $allowableExtensions = $params->get('upload_cover_extensions', 'jpg,jpeg,png,gif');
     // Check for request forgeries
     JRequest::checkToken('request') or jexit(JText::_('JINVALID_TOKEN'));
     // Get the user
     $user = JFactory::getUser();
     // Get some data from the request
     $file = JRequest::getVar('Filedata', '', 'files', 'array');
     $ArtistAlbum = JRequest::getVar('artistalbum');
     $this->folder = $this->input->get('folder', '', 'path');
     $return = null;
     $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Start uploading and save cover for: ' . $ArtistAlbum . ', file ' . $file['name'], 'priority' => JLog::INFO, 'section' => 'admin')));
     // Set the redirect
     //$this->setRedirect(JRoute::_('index.php?option=com_playjoom&view=covers'));
     $file['name'] = JFile::makeSafe($file['name']);
     if (isset($file['name'])) {
         // The request is valid
         $err = null;
         if (!PlayJoomMediaHelper::canUpload($file, $err, $allowableExtensions)) {
             // The file can't be upload
             JError::raiseNotice(100, JText::_($err));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'The file ' . $file['name'] . ' can\'t be upload. Error: ' . $err, 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         }
         //Get global tmp path
         $tmp_path = JFactory::getConfig()->get('tmp_path');
         $filepath = JPath::clean($tmp_path . '/image/' . strtolower($file['name']));
         // Trigger the onContentBeforeSave event.
         JPluginHelper::importPlugin('content');
         $object_file = new JObject($file);
         $object_file->filepath = $filepath;
         $result = $dispatcher->trigger('onContentBeforeSave', array('com_playjoom.file', &$object_file));
         if (in_array(false, $result, true)) {
             // There are some errors in the plugins
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Error occours before saving. ' . $object_file->getErrors(), 'priority' => JLog::ERROR, 'section' => 'admin')));
             JError::raiseWarning(100, JText::plural('COM_PLAYJOOM_ERROR_BEFORE_SAVE', count($errors = $object_file->getErrors()), implode('<br />', $errors)));
             return false;
         }
         $file = (array) $object_file;
         if (JFile::exists($filepath)) {
             // File exists
             JError::raiseWarning(100, JText::_('COM_PLAYJOOM_ERROR_FILE_EXISTS'));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'File already exists. ' . $filepath, 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         } elseif (!$user->authorise('core.create', 'com_playjoom')) {
             // File does not exist and user is not authorised to create
             JError::raiseWarning(403, JText::_('COM_PLAYJOOM_ERROR_CREATE_NOT_PERMITTED'));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'The User ' . $user->get('username') . ' has not permitted to upload the file: ' . $file, 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         }
         if (!JFile::upload($file['tmp_name'], $file['filepath'])) {
             // Error in upload
             JError::raiseWarning(100, JText::_('COM_PLAYJOOM_ERROR_UNABLE_TO_UPLOAD_FILE'));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Unable to upload file: ' . $file['tmp_name'] . ' in path: ' . $file['filepath'], 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         } else {
             if (PlayJoomControllerAddCover::AddCover($file['filepath'], $ArtistAlbum)) {
                 // Trigger the onContentAfterSave event.
                 $dispatcher->trigger('onContentAfterSave', array('com_playjoom.file', &$object_file, true));
                 $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Saving cover complete. File: ' . $file['filepath'], 'priority' => JLog::INFO, 'section' => 'admin')));
                 $link = JRoute::_('index.php?option=com_playjoom&view=covers', false);
                 $msg = JText::sprintf('COM_PLAYJOOM_UPLOAD_COMPLETE', substr($file['filepath'], strlen(PLAYJOOM_BASE_PATH)));
                 $this->setRedirect($link, $msg);
                 //Delete temp cover file, after adding in database
                 unlink($file['filepath']);
                 return true;
             } else {
                 $this->setMessage(JText::sprintf('COM_PLAYJOOM_FAULTY_TOADD_DATABASE', substr($file['filepath'], strlen(PLAYJOOM_BASE_PATH))));
                 $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Not Possible to add the cover into the database', 'priority' => JLog::ERROR, 'section' => 'admin')));
                 //Delete temp cover file, after adding in database
                 unlink($file['filepath']);
             }
             return true;
         }
     } else {
         $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => JText::_('COM_PLAYJOOM_INVALID_REQUEST'), 'priority' => JLog::ERROR, 'section' => 'admin')));
         return false;
     }
 }
Example #3
0
 /**
  * Method for uploading a file
  *
  * @since 1.5
  * @return void
  */
 function upload()
 {
     $dispatcher = JDispatcher::getInstance();
     $params = JComponentHelper::getParams('com_playjoom');
     // Check for request forgeries
     JRequest::checkToken('request') or jexit(JText::_('JINVALID_TOKEN'));
     // Get the user
     $user = JFactory::getUser();
     // Get some data from the request
     $file = JRequest::getVar('Filedata', '', 'files', 'array');
     $return = $this->input->post->get('return-url', null, 'base64');
     $this->folder = $this->input->get('folder', '', 'path');
     $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Start uploading file: ' . $this->folder . DIRECTORY_SEPARATOR . $file['name'], 'priority' => JLog::INFO, 'section' => 'admin')));
     // Set FTP credentials, if given
     JClientHelper::setCredentialsFromRequest('ftp');
     // Set the redirect
     if ($return) {
         $this->setRedirect(base64_decode($return) . '&view=media&folder=' . $this->folder);
     }
     // Make the filename safe
     $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Make file name safe: ' . $file['name'], 'priority' => JLog::INFO, 'section' => 'admin')));
     $file['name'] = JFile::makeSafe($file['name']);
     if (isset($file['name'])) {
         // The request is valid
         $err = null;
         $allowableExtensions = $params->get('upload_audio_extensions', 'mp3,wav,flac');
         if (!PlayJoomMediaHelper::canUpload($file, $err, $allowableExtensions)) {
             // The file can't be upload
             JError::raiseNotice(100, JText::_($err));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Can not uploading file: ' . $file['name'] . 'Error: ' . $err, 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         }
         $filepath = JPath::clean(PLAYJOOM_BASE_PATH . '/' . $this->folder . '/' . strtolower($file['name']));
         // Trigger the onContentBeforeSave event.
         JPluginHelper::importPlugin('content');
         $object_file = new JObject($file);
         $object_file->filepath = $filepath;
         $result = $dispatcher->trigger('onContentBeforeSave', array('com_playjoom.file', &$object_file));
         if (in_array(false, $result, true)) {
             // There are some errors in the plugins
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Error occours before saving. ' . $object_file->getErrors(), 'priority' => JLog::ERROR, 'section' => 'admin')));
             JError::raiseWarning(100, JText::plural('COM_PLAYJOOM_ERROR_BEFORE_SAVE', count($errors = $object_file->getErrors()), implode('<br />', $errors)));
             return false;
         }
         $file = (array) $object_file;
         if (JFile::exists($file['filepath'])) {
             // File exists
             JError::raiseWarning(100, JText::_('COM_PLAYJOOM_ERROR_FILE_EXISTS'));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'File already exists. ' . $file['name'], 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         } elseif (!$user->authorise('core.create', 'com_playjoom')) {
             // File does not exist and user is not authorised to create
             JError::raiseWarning(403, JText::_('COM_PLAYJOOM_ERROR_CREATE_NOT_PERMITTED'));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'The User ' . $user->get('username') . ' has not permitted to upload the file: ' . $file, 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         }
         if (!JFile::upload($file['tmp_name'], $file['filepath'])) {
             // Error in upload
             JError::raiseWarning(100, JText::_('COM_PLAYJOOM_ERROR_UNABLE_TO_UPLOAD_FILE'));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Unable to upload file: ' . $file['tmp_name'] . ' in path: ' . $file['filepath'], 'priority' => JLog::ERROR, 'section' => 'admin')));
             return false;
         } else {
             // Trigger the onContentAfterSave event.
             $dispatcher->trigger('onContentAfterSave', array('com_playjoom.file', &$object_file, true));
             $dispatcher->trigger('onEventLogging', array(array('method' => __METHOD__ . ":" . __LINE__, 'message' => 'Upload complete. Temp filename: ' . $file['tmp_name'] . ', filepath: ' . $file['filepath'], 'priority' => JLog::INFO, 'section' => 'admin')));
             $this->setMessage(JText::sprintf('COM_PLAYJOOM_UPLOAD_COMPLETE', substr($file['filepath'], strlen(PLAYJOOM_BASE_PATH))));
             return true;
         }
     } else {
         $this->setRedirect('index.php', JText::_('COM_PLAYJOOM_INVALID_REQUEST'), 'error');
         return false;
     }
 }