backend/controllers/ThemeController.php
/**
     * Register new theme.
     * Theme zip uploaded to temporary directory and extracted there.
     * Check the theme directory and move the first directory of the extracted theme.
     * If registration is successful, the browser will be redirected to the 'index' page.
     *
     * @return string
     */
Register new theme.
Theme zip uploaded to temporary directory and extracted there.
Check the theme directory and move the first directory of the extracted theme.
If registration is successful, the browser will be redirected to the 'index' page.

@return string
Register new theme . Theme zip uploaded to temporary directory and extracted there . Check the theme directory and move the first directory of the extracted theme . If registration is successful the browser will be redirected to the ' index ' page . @return string
public function actionUpload()
    {
        $errors = [];
        $model = new DynamicModel(['file']);
        $model->addRule(['file'], 'required')
            ->addRule(['file'], 'file', ['extensions' => 'zip']);
        if (!is_dir($this->_dir)) {
            FileHelper::createDirectory($this->_dir, 0755);
        }
        if (!is_dir($this->_tmp)) {
            FileHelper::createDirectory($this->_tmp, 0755);
        }
        if (!is_dir($this->_thumbDir)) {
            FileHelper::createDirectory($this->_thumbDir, 0755);
        }
        if (($model->file = UploadedFile::getInstance($model, 'file')) && $model->validate()) {
            $themeTempPath = $this->_tmp . $model->file->name;
            if (!$model->file->saveAs($themeTempPath)) {
                return $this->render('upload', [
                    'model' => $model,
                    'errors' => [Yii::t('writesdown', 'Failed to move uploaded file')],
                ]);
            }
            $zipArchive = new \ZipArchive();
            $zipArchive->open($themeTempPath);
            if (!$zipArchive->extractTo($this->_tmp)) {
                $zipArchive->close();
                FileHelper::removeDirectory($this->_tmp);
                return $this->render('upload', [
                    'model' => $model,
                    'errors' => [Yii::t('writesdown', 'Failed to extract file.')],
                ]);
            }
            $baseDir = substr($zipArchive->getNameIndex(0), 0, strpos($zipArchive->getNameIndex(0), '/'));
            $zipArchive->close();
            if (is_dir($this->_dir . $baseDir)) {
                FileHelper::removeDirectory($this->_tmp);
                $errors[] = Yii::t('writesdown', 'Theme with the same directory already exist.');
            } else {
                rename($this->_tmp . $baseDir, $this->_dir . $baseDir);
                FileHelper::removeDirectory($this->_tmp);
                if (is_file($this->_dir . $baseDir . '/screenshot.png')) {
                    copy($this->_dir . $baseDir . '/screenshot.png', $this->_thumbDir . $baseDir . '.png');
                }
                foreach (ArrayHelper::getValue($this->getConfig($baseDir), 'upload', []) as $type) {
                    try {
                        Yii::createObject($type);
                    } catch (Exception $e) {
                    }
                }
                Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Theme successfully uploaded'));
                return $this->redirect(['index']);
            }
        }
        return $this->render('upload', [
            'model' => $model,
            'errors' => $errors,
        ]);
    }
public function actionUpload ( ) { $ errors = [ ] ; $ model = new DynamicModel ( [ 'file' ] ) ; $ model -> addRule ( [ 'file' ] , 'required' ) -> addRule ( [ 'file' ] , 'file' , [ 'extensions' => 'zip' ] ) ; if ( ! is_dir ( $ this -> _dir ) ) { FileHelper :: createDirectory ( $ this -> _dir , 0755 ) ; } if ( ! is_dir ( $ this -> _tmp ) ) { FileHelper :: createDirectory ( $ this -> _tmp , 0755 ) ; } if ( ! is_dir ( $ this -> _thumbDir ) ) { FileHelper :: createDirectory ( $ this -> _thumbDir , 0755 ) ; } if ( ( $ model -> file = UploadedFile :: getInstance ( $ model , 'file' ) ) && $ model -> validate ( ) ) { $ themeTempPath = $ this -> _tmp . $ model -> file -> name ; if ( ! $ model -> file -> saveAs ( $ themeTempPath ) ) { return $ this -> render ( 'upload' , [ 'model' => $ model , 'errors' => [ Yii :: t ( 'writesdown' , 'Failed to move uploaded file' ) ] , ] ) ; } $ zipArchive = new \ ZipArchive ( ) ; $ zipArchive -> open ( $ themeTempPath ) ; if ( ! $ zipArchive -> extractTo ( $ this -> _tmp ) ) { $ zipArchive -> close ( ) ; FileHelper :: removeDirectory ( $ this -> _tmp ) ; return $ this -> render ( 'upload' , [ 'model' => $ model , 'errors' => [ Yii :: t ( 'writesdown' , 'Failed to extract file.' ) ] , ] ) ; } $ baseDir = substr ( $ zipArchive -> getNameIndex ( 0 ) , 0 , strpos ( $ zipArchive -> getNameIndex ( 0 ) , '/' ) ) ; $ zipArchive -> close ( ) ; if ( is_dir ( $ this -> _dir . $ baseDir ) ) { FileHelper :: removeDirectory ( $ this -> _tmp ) ; $ errors [ ] = Yii :: t ( 'writesdown' , 'Theme with the same directory already exist.' ) ; } else { rename ( $ this -> _tmp . $ baseDir , $ this -> _dir . $ baseDir ) ; FileHelper :: removeDirectory ( $ this -> _tmp ) ; if ( is_file ( $ this -> _dir . $ baseDir . '/screenshot.png' ) ) { copy ( $ this -> _dir . $ baseDir . '/screenshot.png' , $ this -> _thumbDir . $ baseDir . '.png' ) ; } foreach ( ArrayHelper :: getValue ( $ this -> getConfig ( $ baseDir ) , 'upload' , [ ] ) as $ type ) { try { Yii :: createObject ( $ type ) ; } catch ( Exception $ e ) { } } Yii :: $ app -> getSession ( ) -> setFlash ( 'success' , Yii :: t ( 'writesdown' , 'Theme successfully uploaded' ) ) ; return $ this -> redirect ( [ 'index' ] ) ; } } return $ this -> render ( 'upload' , [ 'model' => $ model , 'errors' => $ errors , ] ) ; }
Register new theme.
Register new theme .