Passed
Push — master ( 22b305...33bedb )
by Thomas
02:16
created

DecryptController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 24
c 1
b 0
f 0
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C index() 0 41 12
1
<?php
2
3
namespace LeKoala\Encrypt;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Security\Permission;
8
use SilverStripe\Security\Security;
9
use SilverStripe\Versioned\Versioned;
10
11
/**
12
 * Easily decrypt your files
13
 */
14
class DecryptController extends Controller
15
{
16
    public function index()
17
    {
18
        $request = $this->getRequest();
19
        $ID = $request->getVar("ID");
20
        $Hash = $request->getVar("Hash");
21
22
        if (!$ID || !$Hash) {
23
            return $this->httpError(404);
24
        }
25
26
        /** @var File|EncryptedDBFile $File  */
27
        $File = File::get()->byID($ID);
28
        if (!$File) {
0 ignored issues
show
introduced by
$File is of type SilverStripe\Assets\File, thus it always evaluated to true.
Loading history...
29
            $File = Versioned::get_latest_version(File::class, $ID);
30
        }
31
        if (!$File) {
0 ignored issues
show
introduced by
$File is of type SilverStripe\Assets\File, thus it always evaluated to true.
Loading history...
32
            return $this->httpError(404);
33
        }
34
35
        // Verify hash
36
        $FileHash = substr($File->File->Hash, 0, 10);
37
        if ($Hash != $FileHash && !Permission::check("CMS_ACCESS")) {
38
            return $this->httpError(404);
39
        }
40
41
        // Check protected
42
        $sendProtected = $this->config()->send_protected;
43
        $adminSendProtected = $this->config()->admin_send_protected;
44
        $currentUserID = Security::getCurrentUser()->ID ?? 0;
45
        $isOwner = $File->OwnerID === $currentUserID;
46
        if ($File->getVisibility() == "protected") {
47
            if (!$sendProtected && !$isOwner) {
48
                if ($adminSendProtected && Permission::check("CMS_ACCESS")) {
49
                    // We can proceed
50
                } else {
51
                    return $this->httpError(404);
52
                }
53
            }
54
        }
55
56
        $File->sendDecryptedFile();
57
    }
58
}
59