Back to PhocaGalleryFileUpload class

Method realSingleUpload

public static
realSingleUpload
(mixed $frontEnd = 0)

Method realSingleUpload - Source code

public static function realSingleUpload($frontEnd = 0)
{
    //	$paramsC 		= JComponentHelper::getParams('com_phocagallery');
    //	$chunkMethod 	= $paramsC->get( 'multiple_upload_chunk', 0 );
    //	$uploadMethod 	= $paramsC->get( 'multiple_upload_method', 4 );
    $app = Factory::getApplication();
    Session::checkToken('request') or jexit('ERROR: ' . Text::_('COM_PHOCAGALLERY_INVALID_TOKEN'));
    $app->allowCache(false);
    $path = PhocaGalleryPath::getPath();
    $file = Factory::getApplication()->input->files->get('Filedata', null);
    $folder = Factory::getApplication()->input->get('folder', '', '', 'path');
    $format = Factory::getApplication()->input->get('format', 'html', '', 'cmd');
    $return = JFactory::getApplication()->input->get('return-url', null, 'post', 'base64');
    //includes field
    $viewBack = Factory::getApplication()->input->get('viewback', '', '', '');
    $tab = Factory::getApplication()->input->get('tab', '', '', 'string');
    $field = Factory::getApplication()->input->get('field');
    $errUploadMsg = '';
    $folderUrl = $folder;
    $tabUrl = '';
    $component = Factory::getApplication()->input->get('option', '', '', 'string');
    // In case no return value will be sent (should not happen)
    if ($component != '' && $frontEnd == 0) {
        $componentUrl = 'index.php?option=' . $component;
    } else {
        $componentUrl = 'index.php';
    }
    if ($tab != '') {
        $tabUrl = '&tab=' . (string) $tab;
    }
    $ftp = ClientHelper::setCredentialsFromRequest('ftp');
    // Make the filename safe
    if (isset($file['name'])) {
        $file['name'] = File::makeSafe($file['name']);
    }
    if (isset($folder) && $folder != '') {
        $folder = $folder . '/';
    }
    // All HTTP header will be overwritten with js message
    if (isset($file['name'])) {
        $filepath = Path::clean($path->image_abs . $folder . strtolower($file['name']));
        if (!PhocaGalleryFileUpload::canUpload($file, $errUploadMsg, $frontEnd)) {
            if ($errUploadMsg == 'COM_PHOCAGALLERY_WARNING_FILE_TOOLARGE') {
                $errUploadMsg = Text::_($errUploadMsg) . ' (' . PhocaGalleryFile::getFileSizeReadable($file['size']) . ')';
            } else {
                if ($errUploadMsg == 'COM_PHOCAGALLERY_WARNING_FILE_TOOLARGE_RESOLUTION') {
                    $imgSize = PhocaGalleryImage::getImageSize($file['tmp_name']);
                    $errUploadMsg = Text::_($errUploadMsg) . ' (' . (int) $imgSize[0] . ' x ' . (int) $imgSize[1] . ' px)';
                } else {
                    $errUploadMsg = Text::_($errUploadMsg);
                }
            }
            /*if ($return) {
            			$app->enqueueMessage( $errUploadMsg, 'error');
            			$app->redirect(base64_decode($return).'&folder='.$folderUrl);
            			exit;
            		} else {
            			$app->enqueueMessage( $errUploadMsg, 'error');
            			$app->redirect($componentUrl, $errUploadMsg, 'error');
            			exit;
            		}*/
            if ($return) {
                $app->enqueueMessage($errUploadMsg, 'error');
                if ($frontEnd > 0) {
                    $app->redirect(base64_decode($return));
                } else {
                    $app->redirect(base64_decode($return) . '&folder=' . $folderUrl);
                }
                exit;
            } else {
                $app->enqueueMessage($errUploadMsg, 'error');
                $app->redirect($componentUrl);
                exit;
            }
        }
        if (File::exists($filepath)) {
            if ($return) {
                $app->enqueueMessage(Text::_('COM_PHOCAGALLERY_FILE_ALREADY_EXISTS'), 'error');
                $app->redirect(base64_decode($return) . '&folder=' . $folderUrl);
                exit;
            } else {
                $app->enqueueMessage(Text::_('COM_PHOCAGALLERY_FILE_ALREADY_EXISTS'), 'error');
                $app->redirect($componentUrl);
                exit;
            }
        }
        if (!File::upload($file['tmp_name'], $filepath, false, true)) {
            if ($return) {
                $app->enqueueMessage(Text::_('COM_PHOCAGALLERY_ERROR_UNABLE_TO_UPLOAD_FILE'), 'error');
                $app->redirect(base64_decode($return) . '&folder=' . $folderUrl);
                exit;
            } else {
                $app->enqueueMessage(Text::_('COM_PHOCAGALLERY_ERROR_UNABLE_TO_UPLOAD_FILE'), 'error');
                $app->redirect($componentUrl);
                exit;
            }
        } else {
            if ((int) $frontEnd > 0) {
                return $file['name'];
            }
            if ($return) {
                $app->enqueueMessage(Text::_('COM_PHOCAGALLERY_SUCCESS_FILE_UPLOAD'));
                $app->redirect(base64_decode($return) . '&folder=' . $folderUrl);
                exit;
            } else {
                $app->enqueueMessage(Text::_('COM_PHOCAGALLERY_SUCCESS_FILE_UPLOAD'));
                $app->redirect($componentUrl);
                exit;
            }
        }
    } else {
        $msg = Text::_('COM_PHOCAGALLERY_ERROR_UNABLE_TO_UPLOAD_FILE');
        if ($return) {
            $app->enqueueMessage($msg);
            $app->redirect(base64_decode($return) . '&folder=' . $folderUrl);
            exit;
        } else {
            switch ($viewBack) {
                case 'phocagalleryi':
                    $app->enqueueMessage($msg, 'error');
                    $app->redirect('index.php?option=com_phocagallery&view=phocagalleryi&tmpl=component' . $tabUrl . '&folder=' . $folder . '&field=' . $field);
                    exit;
                    break;
                case 'phocagallerym':
                    $app->enqueueMessage($msg, 'error');
                    $app->redirect('index.php?option=com_phocagallery&view=phocagallerym&layout=form&hidemainmenu=1' . $tabUrl . '&folder=' . $folder);
                    exit;
                    break;
                default:
                    $app->enqueueMessage($msg, 'error');
                    $app->redirect('index.php?option=com_phocagallery');
                    exit;
                    break;
            }
        }
    }
}