Beispiel #1
0
 function uploadImage()
 {
     $mainframe = JFactory::getApplication();
     // Check for request forgeries
     JRequest::checkToken('request') or jexit('Invalid Token');
     $file = JRequest::getVar('photo_path', '', 'files', 'array');
     $folder = JRequest::getVar('folder', '', '', 'path');
     $format = JRequest::getVar('format', 'html', '', 'cmd');
     $return = JRequest::getVar('return-url', null, 'post', 'base64');
     $err = null;
     // Set FTP credentials, if given
     jimport('joomla.client.helper');
     JClientHelper::setCredentialsFromRequest('ftp');
     require_once JPATH_COMPONENT . DS . 'helpers' . DS . 'media.php';
     // Make the filename safe
     jimport('joomla.filesystem.file');
     $file['name'] = JFile::makeSafe($file['name']);
     if (isset($file['name'])) {
         $filepath = JPath::clean(COM_WEBMAPPLUS_MEDIA_BASE . DS . $folder . DS . strtolower($file['name']));
         if (!MediaHelper::canUpload($file, $err)) {
             if ($format == 'json') {
                 jimport('joomla.error.log');
                 $log =& JLog::getInstance('upload.error.php');
                 $log->addEntry(array('comment' => 'Invalid: ' . $filepath . ': ' . $err));
                 header('HTTP/1.0 415 Unsupported Media Type');
                 jexit('Error. Unsupported Media Type!');
             } else {
                 JError::raiseNotice(100, JText::_($err));
                 // REDIRECT
                 if ($return) {
                     $mainframe->redirect(base64_decode($return) . '&folder=' . $folder);
                 }
                 return;
             }
         }
         if (JFile::exists($filepath)) {
             if ($format == 'json') {
                 jimport('joomla.error.log');
                 $log =& JLog::getInstance('upload.error.php');
                 $log->addEntry(array('comment' => 'File already exists: ' . $filepath));
                 header('HTTP/1.0 409 Conflict');
                 jexit('Error. File already exists');
             } else {
                 JError::raiseNotice(100, JText::_('Error. File already exists'));
                 // REDIRECT
                 if ($return) {
                     $mainframe->redirect(base64_decode($return) . '&folder=' . $folder);
                 }
                 return;
             }
         }
         if (!JFile::upload($file['tmp_name'], $filepath)) {
             if ($format == 'json') {
                 jimport('joomla.error.log');
                 $log =& JLog::getInstance('upload.error.php');
                 $log->addEntry(array('comment' => 'Cannot upload: ' . $filepath));
                 header('HTTP/1.0 400 Bad Request');
                 jexit('Error. Unable to upload file');
             } else {
                 JError::raiseWarning(100, JText::_('Error. Unable to upload file'));
                 // REDIRECT
                 if ($return) {
                     $mainframe->redirect(base64_decode($return) . '&folder=' . $folder);
                 }
                 return;
             }
         } else {
             if ($format == 'json') {
                 jimport('joomla.error.log');
                 $log =& JLog::getInstance();
                 $log->addEntry(array('comment' => $folder));
                 jexit('Upload complete');
             } else {
                 $mainframe->enqueueMessage(JText::_('Upload complete'));
                 // REDIRECT
                 if ($return) {
                     $mainframe->redirect(base64_decode($return) . '&folder=' . $folder);
                 }
                 $params =& JComponentHelper::getParams('com_webmapplus');
                 $height = $params->get('picture_height');
                 $width = $params->get('picture_width');
                 MediaHelper::createthumb($filepath, $filepath, $width, $height);
                 $filepath = str_replace(JPATH_ROOT, "", $filepath);
                 $file_information = pathinfo($filepath);
                 return $file_information['basename'];
             }
         }
     } else {
         $mainframe->redirect('index.php', 'Invalid Request', 'error');
     }
 }