LinkTypeViewController::blockAsset()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 25
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 44
rs 8.8977
1
<?php
2
namespace App\Http\Controllers;
3
4
use Illuminate\Http\Request;
5
use App\Models\LinkType;
6
use App\Models\Link;
7
use App\Models\Button;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\Facades\File;
10
11
class LinkTypeViewController extends Controller
12
{
13
    public function getParamForm($typename, $linkId = 0)
14
    {
15
        $data = [
16
            'title' => '',
17
            'link' => '',
18
            'button_id' => 0,
19
            'buttons' => [],
20
        ];
21
    
22
        if ($linkId) {
23
            $link = Link::find($linkId);
24
            $data['title'] = $link->title;
25
            $data['link'] = $link->link;
26
            if (Route::currentRouteName() != 'showButtons') {
27
                $data['button_id'] = $link->button_id;
28
            }
29
    
30
            if (!empty($link->type_params) && is_string($link->type_params)) {
31
                $typeParams = json_decode($link->type_params, true);
32
                if (is_array($typeParams)) {
33
                    $data = array_merge($data, $typeParams);
34
                }
35
            }
36
        }
37
        if ($typename === 'predefined') {
38
            $buttons = Button::select()->orderBy('name', 'asc')->get();
39
            foreach ($buttons as $btn) {
40
                $data['buttons'][] = [
41
                    'name' => $btn->name,
42
                    'title' => $btn->alt,
43
                    'exclude' => $btn->exclude,
44
                    'selected' => ($linkId && isset($link) && $link->button_id == $btn->id),
45
                ];
46
            }
47
            return view('components.pageitems.predefined-form', $data);
48
        }
49
    
50
        // Set the block asset context before returning the view
51
        setBlockAssetContext($typename);
52
    
53
        return view($typename . '.form', $data);
54
    }
55
56
    public function blockAsset(Request $request, $type)
57
    {
58
        $asset = $request->query('asset');
59
    
60
        // Prevent directory traversal in $type
61
        if (preg_match('/\.\.|\/|\\\\/', $type)) {
62
            abort(403, 'Unauthorized action.');
63
        }
64
    
65
        // Define allowed file extensions
66
        $allowedExtensions = ['js', 'css', 'img', 'svg', 'gif', 'jpg', 'jpeg', 'png', 'mp4', 'mp3'];
67
    
68
        $extension = strtolower(pathinfo($asset, PATHINFO_EXTENSION));
0 ignored issues
show
Bug introduced by
It seems like $asset can also be of type array and null; however, parameter $path of pathinfo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $extension = strtolower(pathinfo(/** @scrutinizer ignore-type */ $asset, PATHINFO_EXTENSION));
Loading history...
Bug introduced by
It seems like pathinfo($asset, App\Htt...ers\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $extension = strtolower(/** @scrutinizer ignore-type */ pathinfo($asset, PATHINFO_EXTENSION));
Loading history...
69
        if (!in_array($extension, $allowedExtensions)) {
70
            return response('File type not allowed', Response::HTTP_FORBIDDEN);
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
71
        }
72
    
73
        $basePath = realpath(base_path("blocks/$type"));
74
    
75
        $fullPath = realpath(base_path("blocks/$type/$asset"));
76
    
77
        if (!$fullPath || !file_exists($fullPath) || strpos($fullPath, $basePath) !== 0) {
78
            return response('File not found', Response::HTTP_NOT_FOUND);
79
        }
80
    
81
        // Map file extensions to MIME types
82
        $mimeTypes = [
83
            'js' => 'application/javascript',
84
            'css' => 'text/css',
85
            'img' => 'image/png',
86
            'svg' => 'image/svg+xml',
87
            'gif' => 'image/gif',
88
            'jpg' => 'image/jpeg',
89
            'jpeg' => 'image/jpeg',
90
            'png' => 'image/png',
91
            'mp4' => 'video/mp4',
92
            'mp3' => 'audio/mpeg',
93
        ];
94
    
95
        // Determine the MIME type using the mapping
96
        $mimeType = $mimeTypes[$extension] ?? 'application/octet-stream';
97
    
98
        return response()->file($fullPath, [
99
            'Content-Type' => $mimeType
100
        ]);
101
    }
102
}