Backend\Modules\Extensions\Actions\UploadTheme::validateForm PHP Method

validateForm() private method

Validate a submitted form and process it.
private validateForm ( )
    private function validateForm()
    {
        // The form is submitted
        if (!$this->frm->isSubmitted()) {
            return;
        }
        /** @var $fileFile \SpoonFormFile */
        $fileFile = $this->frm->getField('file');
        $zip = null;
        $zipFiles = null;
        // Validate the file. Check if the file field is filled and if it's a zip.
        if ($fileFile->isFilled(BL::err('FieldIsRequired')) && $fileFile->isAllowedExtension(array('zip'), sprintf(BL::getError('ExtensionNotAllowed'), 'zip'))) {
            // Create ziparchive instance
            $zip = new ZipArchive();
            // Try and open it
            if ($zip->open($fileFile->getTempFileName()) === true) {
                // zip file needs to contain some files
                if ($zip->numFiles > 0) {
                    $infoXml = $this->findInfoFileInZip($zip);
                    // Throw error if info.xml is not found
                    if ($infoXml === null) {
                        return $fileFile->addError(sprintf(BL::getError('NoInformationFile'), $fileFile->getFileName()));
                    }
                    // Parse xml
                    try {
                        // Load info.xml
                        $infoXml = @new \SimpleXMLElement($infoXml, LIBXML_NOCDATA, false);
                        // Convert xml to useful array
                        $this->info = BackendExtensionsModel::processThemeXml($infoXml);
                        // Empty data (nothing useful)
                        if (empty($this->info)) {
                            return $fileFile->addError(BL::getMessage('InformationFileIsEmpty'));
                        }
                        // Define the theme name, based on the info.xml file.
                        $this->themeName = $this->info['name'];
                    } catch (Exception $e) {
                        // Warning that the information file is corrupt
                        return $fileFile->addError(BL::getMessage('InformationFileCouldNotBeLoaded'));
                    }
                    // Wow wow, you are trying to upload an already existing theme
                    if (BackendExtensionsModel::existsTheme($this->themeName)) {
                        return $fileFile->addError(sprintf(BL::getError('ThemeAlreadyExists'), $this->themeName));
                    }
                    $zipFiles = $this->getValidatedFilesList($zip);
                } else {
                    // Empty zip file
                    $fileFile->addError(BL::getError('FileIsEmpty'));
                }
            } else {
                // Something went very wrong, probably corrupted
                return $fileFile->addError(BL::getError('CorruptedFile'));
            }
        }
        // Passed all validation
        if ($this->frm->isCorrect() && $zip !== null) {
            // Unpack the zip. If the files were not found inside a parent directory, we create the theme directory.
            $themePath = FRONTEND_PATH . '/Themes';
            if ($this->parentFolderName === null) {
                $themePath .= "/{$this->themeName}";
            }
            $zip->extractTo($themePath, $zipFiles);
            // Rename the original name of the parent folder from the zip to the correct theme foldername.
            $fs = new Filesystem();
            $parentZipFolderPath = $themePath . '/' . $this->parentFolderName;
            if ($this->parentFolderName !== $this->themeName && $this->parentFolderName !== null && $fs->exists($parentZipFolderPath)) {
                $fs->rename($parentZipFolderPath, "{$themePath}/{$this->themeName}");
            }
            // Run installer
            BackendExtensionsModel::installTheme($this->themeName);
            // Redirect with fireworks
            $this->redirect(BackendModel::createURLForAction('Themes') . '&report=theme-installed&var=' . $this->themeName);
        }
    }