Passed
Push — main ( 327d19...e70ca5 )
by Julian
05:02 queued 20s
created

LinkTypeViewController::blockAsset()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 26
rs 9.2222
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
10
class LinkTypeViewController extends Controller
11
{
12
    public function getParamForm($typename, $linkId = 0)
13
    {
14
        $data = [
15
            'title' => '',
16
            'link' => '',
17
            'button_id' => 0,
18
            'buttons' => [],
19
        ];
20
    
21
        if ($linkId) {
22
            $link = Link::find($linkId);
23
            $data['title'] = $link->title;
24
            $data['link'] = $link->link;
25
            if (Route::currentRouteName() != 'showButtons') {
26
                $data['button_id'] = $link->button_id;
27
            }
28
    
29
            // Check if type_params is not empty and is a valid JSON string
30
            if (!empty($link->type_params) && is_string($link->type_params)) {
31
                // Decode the JSON string into an associative array
32
                $typeParams = json_decode($link->type_params, true);
33
                if (is_array($typeParams)) {
34
                    // Merge the associative array into $data
35
                    $data = array_merge($data, $typeParams);
36
                }
37
            }
38
        }
39
        if ($typename === 'predefined') {
40
            $buttons = Button::select()->orderBy('name', 'asc')->get();
41
            foreach ($buttons as $btn) {
42
                $data['buttons'][] = [
43
                    'name' => $btn->name,
44
                    'title' => $btn->alt,
45
                    'exclude' => $btn->exclude,
46
                    'selected' => ($linkId && isset($link) && $link->button_id == $btn->id),
47
                ];
48
            }
49
            return view('components.pageitems.predefined-form', $data);
50
        }
51
    
52
        return view($typename . '.form', $data);
53
    }
54
55
    public function blockAsset(Request $request, $type)
56
    {
57
        $asset = $request->query('asset');
58
59
        // Prevent directory traversal in $type
60
        if (preg_match('/\.\.|\/|\\\\/', $type)) {
61
            abort(403, 'Unauthorized action.');
62
        }
63
64
        // Define allowed file extensions
65
        $allowedExtensions = ['js', 'css', 'img', 'svg', 'gif', 'jpg', 'jpeg', 'png', 'mp4', 'mp3'];
66
67
        $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

67
        $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

67
        $extension = strtolower(/** @scrutinizer ignore-type */ pathinfo($asset, PATHINFO_EXTENSION));
Loading history...
68
        if (!in_array($extension, $allowedExtensions)) {
69
            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...
70
        }
71
72
        $basePath = realpath(base_path("blocks/$type"));
73
74
        $fullPath = realpath(base_path("blocks/$type/$asset"));
75
76
        if (!$fullPath || !file_exists($fullPath) || strpos($fullPath, $basePath) !== 0) {
77
            return response('File not found', Response::HTTP_NOT_FOUND);
78
        }
79
80
        return response()->file($fullPath);
81
    }
82
}