1 | <?php
|
---|
2 |
|
---|
3 | class DbFile
|
---|
4 | {
|
---|
5 | public string $Id;
|
---|
6 | public string $FileName;
|
---|
7 | public int $Size;
|
---|
8 | public string $Directory;
|
---|
9 | public string $DirectoryId;
|
---|
10 | public string $TempName;
|
---|
11 |
|
---|
12 | function DetectMimeType(): string
|
---|
13 | {
|
---|
14 | // For proper mime-type detection php-pecl-Fileinfo package should be installed on *nix systems
|
---|
15 | $FileInfo = new finfo(FILEINFO_MIME, '/usr/share/misc/magic');
|
---|
16 | $Result = $FileInfo->file($this->FileName);
|
---|
17 | //$FileInfo->close();
|
---|
18 | return $Result;
|
---|
19 | }
|
---|
20 |
|
---|
21 | function GetSize($Item): int
|
---|
22 | {
|
---|
23 | $FileName = $this->GetFullName($Item);
|
---|
24 | if (file_exists($FileName)) $Result = filesize($FileName);
|
---|
25 | else $Result = 0;
|
---|
26 | return $Result;
|
---|
27 | }
|
---|
28 |
|
---|
29 | function GetFullName(): string
|
---|
30 | {
|
---|
31 | $Path = '';
|
---|
32 | $ParentId = $this->Directory;
|
---|
33 | while ($ParentId != null)
|
---|
34 | {
|
---|
35 | $DbResult = $this->Database->select('FileDirectory', '*', 'Id='.$ParentId);
|
---|
36 | $DbRow = $DbResult->fetch_assoc();
|
---|
37 | $Path .= $DbRow['Name'].'/';
|
---|
38 | $ParentId = $DbRow['Parent'];
|
---|
39 | }
|
---|
40 | $Result = $this->UploadFileFolder.'/'.$Path.$this->FileName;
|
---|
41 | return $Result;
|
---|
42 | }
|
---|
43 |
|
---|
44 | function GetExt(): string
|
---|
45 | {
|
---|
46 | return substr($this->Name, 0, strpos($this->Name, '.') - 1);
|
---|
47 | }
|
---|
48 |
|
---|
49 | function Delete(): void
|
---|
50 | {
|
---|
51 | if (file_exists($this->GetFullName())) unlink($this->GetFullName());
|
---|
52 | }
|
---|
53 |
|
---|
54 | function GetContent(): string
|
---|
55 | {
|
---|
56 | if ($this->TempName != '') $Content = file_get_contents($this->TempName);
|
---|
57 | else $Content = file_get_contents($this->GetFullName());
|
---|
58 | return $Content;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | class TypeFile extends TypeBase
|
---|
63 | {
|
---|
64 | public string $UploadFileFolder;
|
---|
65 | public string $FileDownloadURL;
|
---|
66 | public string $DirectoryId;
|
---|
67 |
|
---|
68 | function __construct(FormManager $FormManager)
|
---|
69 | {
|
---|
70 | parent::__construct($FormManager);
|
---|
71 | $this->FileDownloadURL = 'file';
|
---|
72 | $this->DirectoryId = null;
|
---|
73 | }
|
---|
74 |
|
---|
75 | function OnView(array $Item): ?string
|
---|
76 | {
|
---|
77 | $File = &$Item['Value'];
|
---|
78 | return '<a href="'.$this->FileDownloadURL.'?id='.$File->Id.'">'.
|
---|
79 | $File.'</a> ('.HumanSize($File->Size).')';
|
---|
80 | }
|
---|
81 |
|
---|
82 | function OnEdit(array $Item): string
|
---|
83 | {
|
---|
84 | // Check max value of upload_max_filesize
|
---|
85 | // ini_set("upload_max_filesize", "100M");
|
---|
86 | // <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
|
---|
87 | $File = &$Item['Value'];
|
---|
88 | $Output = '<input type="file" name="'.$Item['Name'].'" value="'.$File->Name.'">';
|
---|
89 | return $Output;
|
---|
90 | }
|
---|
91 |
|
---|
92 | function OnLoad(array $Item): ?string
|
---|
93 | {
|
---|
94 | if (!is_object($Item['Value'])) $Item['Value'] = new DbFile();
|
---|
95 | $File = &$Item['Value'];
|
---|
96 | if (array_key_exists($Item['Name'], $_FILES) and ($_FILES[$Item['Name']]['name'] != ''))
|
---|
97 | {
|
---|
98 | $UploadFile = $_FILES[$Item['Name']];
|
---|
99 | if (file_exists($UploadFile['tmp_name']))
|
---|
100 | {
|
---|
101 | $File->Name = $UploadFile['name'];
|
---|
102 | $File->Size = $UploadFile['size'];
|
---|
103 | $File->TempName = $UploadFile['tmp_name'];
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return $File;
|
---|
107 | }
|
---|
108 |
|
---|
109 | function OnLoadDb(array $Item): ?string
|
---|
110 | {
|
---|
111 | if (!is_object($Item['Value'])) $Item['Value'] = new DbFile();
|
---|
112 | $File = &$Item['Value'];
|
---|
113 | $DbResult = $this->Database->select('File', '*', 'Id='.$File->Id);
|
---|
114 | if ($DbResult->num_rows > 0)
|
---|
115 | {
|
---|
116 | $DbRow = $DbResult->fetch_assoc();
|
---|
117 | $File->Name = $DbRow['Name'];
|
---|
118 | $File->Size = $DbRow['Size'];
|
---|
119 | $File->Directory = $DbRow['Directory'];
|
---|
120 | }
|
---|
121 | return $File;
|
---|
122 | }
|
---|
123 |
|
---|
124 | function OnSaveDb(array $Item): ?string
|
---|
125 | {
|
---|
126 | if (!is_object($Item['Value'])) $Item['Value'] = new DbFile();
|
---|
127 | $File = &$Item['Value'];
|
---|
128 | $Properties = array('Name' => $File->Name,
|
---|
129 | 'Size' => $File->GetSize(), 'Directory' => $File->Directory, 'Hash' => 'SHA1(CONCAT(Id,Name,Size,Time))');
|
---|
130 | $DbResult = $this->Database->select('File', '*', 'Id='.$File->Id);
|
---|
131 | if ($DbResult->num_rows > 0)
|
---|
132 | {
|
---|
133 | $DbRow = $DbResult->fetch_assoc();
|
---|
134 | if ($File->TempName != '')
|
---|
135 | {
|
---|
136 | $FileName = $File->GetFullName();
|
---|
137 | unlink($FileName);
|
---|
138 | }
|
---|
139 | $this->Database->update('File', 'Id='.$File->Id, $Properties);
|
---|
140 | } else
|
---|
141 | {
|
---|
142 | $this->Database->insert('File', $Properties);
|
---|
143 | $File->Id = $this->Database->insert_id;
|
---|
144 | }
|
---|
145 | if (!move_uploaded_file($File->TempName, $FileName))
|
---|
146 | $this->System->SystemMessage('Nahrání souboru', 'Cílová složka není dostupná!');
|
---|
147 | return '';
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|