1 | <?php |
||
2 | |||
3 | namespace LeKoala\Encrypt; |
||
4 | |||
5 | use SilverStripe\Assets\File; |
||
6 | use SilverStripe\Security\Security; |
||
7 | use SilverStripe\Control\Controller; |
||
8 | use SilverStripe\Security\Permission; |
||
9 | use SilverStripe\Versioned\Versioned; |
||
10 | use SilverStripe\Control\HTTPResponse; |
||
11 | |||
12 | /** |
||
13 | * Easily decrypt your files |
||
14 | */ |
||
15 | class DecryptController extends Controller |
||
16 | { |
||
17 | /** |
||
18 | * @return HTTPResponse|void |
||
19 | */ |
||
20 | public function index() |
||
21 | { |
||
22 | $request = $this->getRequest(); |
||
23 | $ID = (int) $request->getVar("ID"); |
||
24 | $Hash = $request->getVar("Hash"); |
||
25 | |||
26 | if (!$ID || !$Hash) { |
||
27 | return $this->httpError(404); |
||
28 | } |
||
29 | |||
30 | $sendDraft = $this->config()->send_draft; |
||
31 | |||
32 | /** @var File|null $File */ |
||
33 | $File = File::get()->byID($ID); |
||
34 | if (!$File && $sendDraft && class_exists(Versioned::class)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
35 | /** @var File|null $File */ |
||
36 | $File = Versioned::get_one_by_stage(File::class, Versioned::DRAFT, "ID = " . $ID); |
||
37 | } |
||
38 | if (!$File) { |
||
0 ignored issues
–
show
|
|||
39 | return $this->httpError(404); |
||
40 | } |
||
41 | |||
42 | // Verify hash |
||
43 | $FileHash = substr($File->File->Hash, 0, 10); |
||
44 | if ($Hash != $FileHash && !Permission::check("CMS_ACCESS")) { |
||
45 | return $this->httpError(404); |
||
46 | } |
||
47 | |||
48 | // Check protected |
||
49 | $sendProtected = $this->config()->send_protected; |
||
50 | $adminSendProtected = $this->config()->admin_send_protected; |
||
51 | $currentUserID = Security::getCurrentUser()->ID ?? 0; |
||
52 | $isOwner = $File->OwnerID === $currentUserID; |
||
53 | if ($File->getVisibility() == "protected") { |
||
54 | if (!$sendProtected && !$isOwner) { |
||
55 | if ($adminSendProtected && Permission::check("CMS_ACCESS")) { |
||
56 | // We can proceed |
||
57 | } else { |
||
58 | return $this->httpError(404); |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | |||
63 | EncryptHelper::sendDecryptedFile($File); |
||
64 | } |
||
65 | } |
||
66 |