Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
file.php
1<?php
2
4
8
9class File extends Base
10{
11 private const TOKEN_SALT = 'bizproc.file.show';
12
13 public function configureActions(): array
14 {
15 $configureActions = parent::configureActions();
16 $configureActions['show'] = [
17 '-prefilters' => [
18 Main\Engine\ActionFilter\Csrf::class,
19 Main\Engine\ActionFilter\Authentication::class,
20 ],
21 '+prefilters' => [
23 ]
24 ];
25
26 return $configureActions;
27 }
28
29 public function showAction($token)
30 {
31 [$fileId] = self::extractToken($token);
32 $file = null;
33
34 if ($fileId)
35 {
36 $file = \CFile::getFileArray($fileId);
37 }
38
39 if (!$file)
40 {
41 $this->addError(new Error('No file'));
42 return null;
43 }
44
45 return BFile::createByFileData($file);
46 }
47
48 public static function getPublicLink(int $fileId): string
49 {
50 return Main\Engine\UrlManager::getInstance()->create(
51 'bizproc.file.show',
52 [
53 'token' => static::generateToken($fileId),
54 ],
55 true
56 )->getUri();
57 }
58
59 private static function generateToken(int $fileId)
60 {
61 $signer = new Main\Security\Sign\Signer;
62 return $signer->sign((string) $fileId, self::TOKEN_SALT);
63 }
64
65 private static function extractToken(string $token): array
66 {
67 $signer = new Main\Security\Sign\Signer;
68
69 try
70 {
71 $unsigned = $signer->unsign($token, self::TOKEN_SALT);
72 $result = [$unsigned];
73 }
74 catch (\Exception $e)
75 {
76 $result = [null];
77 }
78
79 return $result;
80 }
81}
static addError($error)
Definition base.php:278
static getPublicLink(int $fileId)
Definition file.php:48
sign($value, $salt=null)
Definition signer.php:111
unsign($signedValue, $salt=null)
Definition signer.php:153