<commit_before><?php

namespace app\modules\cii\controllers;

use Yii;

use yii\filters\VerbFilter;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;

use app\modules\cii\Permission;
use app\modules\cii\models\browser\UploadFileForm;
use app\modules\cii\models\browser\RenameFileForm;

use cii\backend\BackendController;
use cii\web\SecurityException;
use cii\base\SearchModel;

class BrowserBaseController extends BackendController {
    public function behaviors() {
        return parent::behaviors() + [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                    'upload' => ['POST'],
                ],
            ],
        ];
    }

    public function actionDownload($path = null) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }
        
        if(is_dir($dstPath)) {
            throw new SecurityException();
        }

        header('Content-Type: application/octet-stream');
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-Disposition: attachment; filename=\"" . basename($path) . "\""); 
        readfile($dstPath);
        die();
    }

    public function actionRemove($path) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }

        if(is_dir($dstPath)) {
            FileHelper::removeDirectory($dstPath);
        }else {
            @unlink($dstPath);
        }

        return $this->goBackToReferrer();
    }

    public function actionRename($path = null) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }

        $renameModel = new RenameFileForm();
        if($renameModel->load(Yii::$app->request->post()) && $renameModel->validate()) {
            rename($dstPath . '/' . $renameModel->original, $dstPath . '/' . $renameModel->name);
        }

        return $this->goBackToReferrer();
    }


    public function actionUpload($path = null) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }


        $uploadModel = new UploadFileForm();
        $uploadModel->files = UploadedFile::getInstances($uploadModel, 'files');
        foreach($uploadModel->files as $file) {
            $filePath = $dstPath . '/' . $file->name;
            $size = (int)Yii::$app->cii->package->setting('cii', 'size_uploaded_image');

            if(!
                (Yii::$app->cii->package->setting('cii', 'resize_uploaded_image')
                && $size > 0
                && ($img = Yii::$app->cii->image->load($file->tempName))
                && (($img->height > $size) || ($img->width > $size))
                && $img->resize($size, null, \cii\components\drivers\Kohana\Image::ADAPT)
                && $img->save($filePath)
                )
            ) {
                $file->saveAs($filePath);
            }
        }
        
        return $this->goBackToReferrer();
    }
}<commit_msg>adding try/catch to image resizer<commit_after><?php

namespace app\modules\cii\controllers;

use Yii;

use yii\filters\VerbFilter;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;

use app\modules\cii\Permission;
use app\modules\cii\models\browser\UploadFileForm;
use app\modules\cii\models\browser\RenameFileForm;

use cii\backend\BackendController;
use cii\web\SecurityException;
use cii\base\SearchModel;

class BrowserBaseController extends BackendController {
    public function behaviors() {
        return parent::behaviors() + [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                    'upload' => ['POST'],
                ],
            ],
        ];
    }

    public function actionDownload($path = null) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }
        
        if(is_dir($dstPath)) {
            throw new SecurityException();
        }

        header('Content-Type: application/octet-stream');
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-Disposition: attachment; filename=\"" . basename($path) . "\""); 
        readfile($dstPath);
        die();
    }

    public function actionRemove($path) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }

        if(is_dir($dstPath)) {
            FileHelper::removeDirectory($dstPath);
        }else {
            @unlink($dstPath);
        }

        return $this->goBackToReferrer();
    }

    public function actionRename($path = null) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }

        $renameModel = new RenameFileForm();
        if($renameModel->load(Yii::$app->request->post()) && $renameModel->validate()) {
            rename($dstPath . '/' . $renameModel->original, $dstPath . '/' . $renameModel->name);
        }

        return $this->goBackToReferrer();
    }


    public function actionUpload($path = null) {
        $basePath = Yii::$app->basePath;
        $dstPath = realpath($basePath . '/' . $path);
        if(strpos($dstPath, $basePath) !== 0) {
            throw new SecurityException();
        }


        $uploadModel = new UploadFileForm();
        $uploadModel->files = UploadedFile::getInstances($uploadModel, 'files');
        foreach($uploadModel->files as $file) {
            $filePath = $dstPath . '/' . $file->name;
            $size = (int)Yii::$app->cii->package->setting('cii', 'size_uploaded_image');

            try {
                if(!
                    (Yii::$app->cii->package->setting('cii', 'resize_uploaded_image')
                    && $size > 0
                    && ($img = Yii::$app->cii->image->load($file->tempName))
                    && (($img->height > $size) || ($img->width > $size))
                    && $img->resize($size, null, \cii\components\drivers\Kohana\Image::ADAPT)
                    && $img->save($filePath)
                    )
                ) {
                    $file->saveAs($filePath);
                }
            }catch(\Exception $e) {
                $file->saveAs($filePath);
            }
        }
        
        return $this->goBackToReferrer();
    }
}
{'fraction_non_alphanumeric': 0.16162757841198078, 'fraction_numerical': 0.0014128284826222096, 'mean_word_length': 1.970625262274444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6809227', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2086, 'n_words': 334}