FilesController.php 2.43 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
3
4
5
6
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
7
8
9
10
 * @see https://www.open2.0.regione.lombardia.it Developers'community
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
 *
Marcello Pivanti's avatar
Marcello Pivanti committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 * @package    arter
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\helpers\FileHelper;
use yii\web\NotFoundHttpException;

class FilesController extends \yii\web\Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => [
                            'index'
                        ],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * Return the file or exception
     * @param $file
     * @return $this
     * @throws NotFoundHttpException
     */
    public function actionIndex($file)
    {
        $filePath = __DIR__ . '/../web/' . $file;

        //If file exists
        if (file_exists($filePath) && !strstr($file, '../')) {
            /**
             * @var $fileInfo array
             */
            $fileInfo = pathinfo($filePath);

            /**
             * Extensions locked
             */
            $notExpectedFormats = [
                'cgi-script',
                'php',
                'php2',
                'php3',
                'php4',
                'php5',
                'php6',
                'php7',
                'php8',
                'pl',
                'py',
                'js',
                'jsp',
                'asp',
                'htm',
                'html',
                'shtml',
                'sh',
                'cgi',
                'git',
                'htaccess',
                'htpasswd'
            ];

            /**
             * If the extension is not alowed
             */
            if (in_array($fileInfo['extension'], $notExpectedFormats)) {
                throw new NotFoundHttpException('File Not Found');
            }

            return Yii::$app->response->sendFile($filePath);
        }

        throw new NotFoundHttpException('File Not Found');
    }
}