Issues (4)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Http/Controllers/Admin/ApiController.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Arcanesoft\Media\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanesoft\Media\Contracts\Media;
5
use Arcanesoft\Media\Policies\MediasPolicy;
6
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Str;
9
10
/**
11
 * Class     ApiController
12
 *
13
 * @package  Arcanesoft\Media\Http\Controllers\Admin
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class ApiController
17
{
18
    /* -----------------------------------------------------------------
19
     |  Traits
20
     | -----------------------------------------------------------------
21
     */
22
23
    use JsonResponses,
24
        AuthorizesRequests;
25
26
    /* -----------------------------------------------------------------
27
     |  Properties
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * The media instance.
33
     *
34
     * @var  \Arcanesoft\Media\Contracts\Media
35
     */
36
    protected $media;
37
38
    /* -----------------------------------------------------------------
39
     |  Constructor
40
     | -----------------------------------------------------------------
41
     */
42
43
    /**
44
     * ApiController constructor.
45
     *
46
     * @param  \Arcanesoft\Media\Contracts\Media  $media
47
     */
48 34
    public function __construct(Media $media)
49
    {
50 34
        $this->media = $media;
51 34
    }
52
53
    /* -----------------------------------------------------------------
54
     |  Main Methods
55
     | -----------------------------------------------------------------
56
     */
57
58
    /**
59
     * Get the the media files.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     *
63
     * @return \Illuminate\Http\JsonResponse
64
     */
65 4
    public function getAll(Request $request)
66
    {
67 4
        $this->authorize(MediasPolicy::PERMISSION_LIST);
68
69 4
        return $this->jsonResponseSuccess([
70 4
            'medias' => $this->media->all(
71 4
                $request->get('location', '/')
72
            ),
73
        ]);
74
    }
75
76
    /**
77
     * Upload a media file.
78
     *
79
     * @param  \Illuminate\Http\Request  $request
80
     *
81
     * @return \Illuminate\Http\JsonResponse
82
     */
83 4
    public function uploadMedia(Request $request)
84
    {
85 4
        $this->authorize(MediasPolicy::PERMISSION_CREATE);
86
87
        // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
88 4
        $validator = validator($request->all(), [
89 4
            'location' => ['required', 'string'],
90
            'medias'   => ['required', 'array'],
91
            'medias.*' => ['required', 'file']
92
        ]);
93
94 4
        if ($validator->fails()) {
95 2
            return $this->jsonResponseError([
96 2
                'messages' => $validator->messages(),
97 2
            ], 422);
98
        }
99
100 2
        $uploaded = $this->media->storeMany(
101 2
            $request->get('location'), $request->file('medias')
0 ignored issues
show
It seems like $request->file('medias') targeting Illuminate\Http\Concerns...eractsWithInput::file() can also be of type null or object<Illuminate\Http\UploadedFile>; however, Arcanesoft\Media\Contracts\Media::storeMany() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102
        );
103
104 2
        return $this->jsonResponseSuccess([
105 2
            'data' => compact('uploaded')
106
        ]);
107
    }
108
109
    /**
110
     * Create a directory.
111
     *
112
     * @param  \Illuminate\Http\Request  $request
113
     *
114
     * @return \Illuminate\Http\JsonResponse
115
     */
116 4
    public function createDirectory(Request $request)
117
    {
118 4
        $this->authorize(MediasPolicy::PERMISSION_CREATE);
119
120
        // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
121 4
        $validator = validator($data = $request->all(), [
122 4
            'name'     => ['required', 'string'], // TODO: check if the folder does not exists
123
            'location' => ['required', 'string'],
124
        ]);
125
126 4
        if ($validator->fails()) {
127 2
            return $this->jsonResponseError([
128 2
                'messages' => $validator->messages(),
129 2
            ], 422);
130
        }
131
132 2
        $this->media->makeDirectory(
133 2
            $path = trim($data['location'], '/').'/'.Str::slug($data['name'])
134
        );
135
136 2
        return $this->jsonResponseSuccess(['data' => compact('path')]);
137
    }
138
139 8
    public function renameMedia(Request $request)
140
    {
141 8
        $this->authorize(MediasPolicy::PERMISSION_UPDATE);
142
143
        // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
144
        // TODO: Check if the folder does not exists
145 8
        $validator = validator($data = $request->all(), [
146 8
            'media'      => ['required', 'array'],
147
            'media.type' => ['required', 'string'],
148
            'media.name' => ['required', 'string'],
149
            'newName'    => ['required', 'string'],
150
            'location'   => ['required', 'string'],
151
        ]);
152
153 8
        if ($validator->fails()) {
154 2
            return $this->jsonResponseError([
155 2
                'messages' => $validator->messages(),
156 2
            ], 422);
157
        }
158
159 6
        $path = $this->performMoveMedia(
160 6
            $data['media']['type'],
161 6
            trim($data['location'], '/'),
162 6
            $data['media']['name'],
163 6
            $data['newName']
164
        );
165
166 6
        if ($path === false) {
167 2
            return $this->jsonResponseError([
168 2
                'message' => 'Something wrong was happened while renaming the media.',
169 2
            ], 500);
170
        }
171 4
        return $this->jsonResponseSuccess([
172 4
            'data' => compact('path'),
173
        ]);
174
    }
175
176 6
    public function deleteMedia(Request $request)
177
    {
178 6
        $this->authorize(MediasPolicy::PERMISSION_DELETE);
179
180
        // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
181 6
        $validator = validator($data = $request->all(), [
182 6
            'media'      => ['required', 'array'],
183
            'media.type' => ['required', 'string'],
184
            'media.path' => ['required', 'string'],
185
        ]);
186
187 6
        if ($validator->fails()) {
188 2
            return $this->jsonResponseError([
189 2
                'messages' => $validator->messages(),
190 2
            ], 422);
191
        }
192
193 4
        return $this->performDeleteMedia($data['media']['type'], $data['media']['path'])
194 4
            ? $this->jsonResponseSuccess()
195 4
            : $this->jsonResponseError();
196
    }
197
198 2
    public function moveLocations(Request $request)
199
    {
200 2
        $this->authorize(MediasPolicy::PERMISSION_UPDATE);
201
202
        // TODO: Adding validation ?
203 2
        $destinations = $this->getDestinations(
204 2
            $request->get('name'),
205 2
            $request->get('location', '/')
206
        );
207
208 2
        return $this->jsonResponseSuccess(compact('destinations'));
209
    }
210
211 4
    public function moveMedia(Request $request)
212
    {
213 4
        $this->authorize(MediasPolicy::PERMISSION_UPDATE);
214
215
        // TODO: Refactor this with the Laravel 5.5 new Exception Handler & Form Request
216 4
        $validator = validator($data = $request->all(), [
217 4
            'old_path' => ['required', 'string'],
218
            'new_path' => ['required', 'string'],
219
        ]);
220
221 4
        if ($validator->fails()) {
222 2
            return $this->jsonResponseError([
223 2
                'messages' => $validator->messages(),
224 2
            ], 422);
225
        }
226
227 2
        return $this->media->move($data['old_path'], $data['new_path'])
228 2
            ? $this->jsonResponseSuccess()
229 2
            : $this->jsonResponseError(['message' => 'Something wrong happened !'], 500);
230
    }
231
232
    /* -----------------------------------------------------------------
233
     |  Other Methods
234
     | -----------------------------------------------------------------
235
     */
236
237
    /**
238
     * Get the destinations paths.
239
     *
240
     * @param  string  $name
241
     * @param  string  $location
242
     *
243
     * @return \Arcanesoft\Media\Entities\DirectoryCollection
244
     */
245 2
    private function getDestinations($name, $location)
246
    {
247 2
        $selected     = ($isHome = $location === '/') ? $name : "{$location}/{$name}";
248 2
        $destinations = $this->media->directories($location)
249 2
            ->pluck('path')
250 2
            ->reject(function ($path) use ($selected) {
251 2
                return $path === $selected;
252 2
            })
253 2
            ->values();
254
255 2
        if ( ! $isHome)
256 2
            $destinations->prepend('..');
257
258 2
        return $destinations;
259
    }
260
261
    /**
262
     * Perform the media movement.
263
     *
264
     * @param  string  $type
265
     * @param  string  $location
266
     * @param  string  $oldName
267
     * @param  string  $newName
268
     *
269
     * @return bool|string
270
     */
271 6
    private function performMoveMedia($type, $location, $oldName, $newName)
272
    {
273 6
        $from = "{$location}/{$oldName}";
274
275 6
        switch (Str::lower($type)) {
276 6
            case Media::MEDIA_TYPE_FILE:
277 2
                return $this->moveFile($from, $location, $newName);
278
279 4
            case Media::MEDIA_TYPE_DIRECTORY:
280 2
                return $this->moveDirectory($from, $location, $newName);
281
282
            default:
283 2
                return false;
284
        }
285
    }
286
287
    /**
288
     * Move file.
289
     *
290
     * @param  string  $location
291
     * @param  string  $location
292
     * @param  string  $from
293
     * @param  string  $newName
294
     *
295
     * @return string
296
     */
297 2
    private function moveFile($from, $location, $newName)
298
    {
299 2
        $filename  = Str::slug(pathinfo($newName, PATHINFO_FILENAME));
300 2
        $extension = pathinfo($newName, PATHINFO_EXTENSION);
301
302 2
        $this->media->move($from, $to = "{$location}/{$filename}.{$extension}");
303
304 2
        return $to;
305
    }
306
307
    /**
308
     * Move directory.
309
     *
310
     * @param  string  $from
311
     * @param  string  $location
312
     * @param  string  $newName
313
     *
314
     * @return string
315
     */
316 2
    private function moveDirectory($from, $location, $newName)
317
    {
318 2
        $newName = Str::slug($newName);
319
320 2
        return tap("{$location}/{$newName}", function ($to) use ($from) {
321 2
            $this->media->move($from, $to);
322 2
        });
323
    }
324
325
    /**
326
     * Perform the media deletion.
327
     *
328
     * @param  string  $type
329
     * @param  string  $path
330
     *
331
     * @return bool
332
     */
333 4
    private function performDeleteMedia($type, $path)
334
    {
335 4
        switch (Str::lower($type)) {
336 4
            case Media::MEDIA_TYPE_FILE:
337 2
                return $this->media->deleteFile($path);
338
339 2
            case Media::MEDIA_TYPE_DIRECTORY:
340 2
                return $this->media->deleteDirectory(trim($path, '/'));
341
342
            default:
343
                return false;
344
        }
345
    }
346
}
347