|
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) { |
|
|
|
|
|
|
29
|
|
|
$File = Versioned::get_latest_version(File::class, $ID); |
|
30
|
|
|
} |
|
31
|
|
|
if (!$File) { |
|
|
|
|
|
|
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
|
|
|
|