source: trunk/Modules/File/File.php@ 899

Last change on this file since 899 was 899, checked in by chronos, 4 years ago
File size: 9.5 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/MimeTypes.php');
4
5class File extends Model
6{
7 public string $FilesDir;
8
9 function __construct(System $System)
10 {
11 parent::__construct($System);
12 $this->FilesDir = '';
13 }
14
15 static function GetModelDesc(): ModelDesc
16 {
17 $Desc = new ModelDesc('File');
18 $Desc->AddString('Name');
19 $Desc->AddInteger('Size');
20 $Desc->AddReference('Directory', FileDirectory::GetClassName(), true);
21 $Desc->AddDateTime('Time');
22 return $Desc;
23 }
24
25 function Delete($Id): void
26 {
27 $DbResult = $this->Database->select('File', 'Name', 'Id='.$Id);
28 if ($DbResult->num_rows > 0)
29 {
30 $DbRow = $DbResult->fetch_assoc();
31 $this->Database->delete('File', 'Id='.$Id);
32 unlink($this->FilesDir.'/'.$Id.'_'.$DbRow['Name']);
33 }
34 }
35
36 function CreateFromUpload(string $Name): string
37 {
38 // Submited form with file input have to be enctype="multipart/form-data"
39 $Result = 0;
40 if (array_key_exists($Name, $_FILES) and ($_FILES[$Name]['name'] != ''))
41 {
42 if (file_exists($_FILES[$Name]['tmp_name']))
43 {
44 $FileName = substr($_FILES[$Name]['name'], strrpos($_FILES[$Name]['name'], '/'));
45 $this->Database->query('INSERT INTO File (`Name`, `Size`) VALUES ("'.$FileName.'", '.filesize($_FILES[$Name]['tmp_name']).')');
46 $InsertId = $this->Database->insert_id;
47 if (move_uploaded_file($_FILES[$Name]['tmp_name'], $this->FilesDir.'/'.$InsertId.'_'.$FileName)) $Result = $InsertId;
48 }
49 }
50 return $Result;
51 }
52
53 function DetectMimeType(string $FileName): string
54 {
55 $MimeTypes = GetMimeTypes();
56 $MimeType = pathinfo($FileName, PATHINFO_EXTENSION);
57 $Result = $MimeTypes[$MimeType][0];
58 return $Result;
59 }
60
61 function Download(string $Id): void
62 {
63 $DbResult = $this->Database->select('File', '*', 'Id='.addslashes($Id));
64 if ($DbResult->num_rows > 0)
65 {
66 $DbRow = $DbResult->fetch_assoc();
67 if ($DbRow['Directory'] != '') $FileName = $this->GetDir($DbRow['Directory']);
68 else $FileName = $this->FilesDir;
69 $FileName .= $DbRow['Name'];
70 if (file_exists($FileName))
71 {
72 Header('Content-Type: '.$this->DetectMimeType($FileName));
73 Header('Content-Disposition: inline; filename="'.$DbRow['Name'].'"');
74 echo(file_get_contents($FileName));
75 } else echo('Soubor nenalezen!');
76 } else echo('Soubor nenalezen!');
77 }
78
79 function GetDir(string $Id): string
80 {
81 $DbResult = $this->Database->select('FileDirectory', '*', 'Id='.$Id);
82 $DbRow = $DbResult->fetch_assoc();
83 if ($DbRow['Parent'] != '') $Result = $this->GetDir($DbRow['Parent']);
84 else $Result = $this->FilesDir;
85 $Result .= $DbRow['Name'].'/';
86 return $Result;
87 }
88
89 function GetFullPath(string $Id): string
90 {
91 $DbResult = $this->Database->select('File', '*', 'Id='.addslashes($Id));
92 if ($DbResult->num_rows > 0)
93 {
94 $DbRow = $DbResult->fetch_assoc();
95 if ($DbRow['Directory'] != '') $FileName = $this->GetDir($DbRow['Directory']);
96 else $FileName = $this->FilesDir;
97 $FileName .= $DbRow['Name'];
98 return $FileName;
99 } else echo('Soubor nenalezen!');
100 }
101}
102
103class PageFile extends Page
104{
105 function Show(): string
106 {
107 if (array_key_exists('id', $_GET)) $Id = $_GET['id'];
108 else if (array_key_exists('i', $_GET)) $Id = $_GET['i'];
109 else return $this->SystemMessage('Chyba', 'Nezadáno id souboru');
110 $this->ClearPage = true;
111 $Output = ModuleFile::Cast($this->System->GetModule('File'))->File->Download($Id);
112 return $Output;
113 }
114}
115
116class FileDirectory extends Model
117{
118 static function GetModelDesc(): ModelDesc
119 {
120 $Desc = new ModelDesc('FileDirectory');
121 $Desc->AddString('Name');
122 $Desc->AddReference('Parent', FileDirectory::GetClassName(), true);
123 return $Desc;
124 }
125}
126
127class PageFileCheck extends Page
128{
129 function __construct(System $System)
130 {
131 parent::__construct($System);
132 $this->FullTitle = 'File check';
133 $this->ShortTitle = 'File check';
134 $this->ParentClass = 'PagePortal';
135 }
136
137 function GetAbsolutePath(string $Path): string
138 {
139 $Path = trim($Path);
140 $IsRoot = substr($Path, 0, 1) == '/';
141 $Path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $Path);
142 $Parts = array_filter(explode(DIRECTORY_SEPARATOR, $Path), 'strlen');
143 $Absolutes = array();
144 foreach ($Parts as $Part)
145 {
146 if ('.' == $Part) continue;
147 if ('..' == $Part)
148 {
149 array_pop($Absolutes);
150 } else
151 {
152 $Absolutes[] = $Part;
153 }
154 }
155 $Path = implode(DIRECTORY_SEPARATOR, $Absolutes);
156 if ($IsRoot) $Path = DIRECTORY_SEPARATOR.$Path;
157 return $Path;
158 }
159
160 function Show(): string
161 {
162 $Output = '<strong>Missing files:</strong><br>';
163 if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('File', 'Check'))
164 return 'Nemáte oprávnění';
165 $DbResult = $this->Database->select('File', 'Id');
166 while ($DbRow = $DbResult->fetch_assoc())
167 {
168 $Id = $DbRow['Id'];
169 $FileName = $this->GetAbsolutePath(ModuleFile::Cast($this->System->GetModule('File'))->File->GetFullPath($Id));
170 if (!file_exists($FileName))
171 $Output .= '<a href="'.$this->System->Link('/is/?a=view&amp;t=File&amp;i='.$Id).'">'.$FileName.'</a><br>';
172 }
173 return $Output;
174 }
175}
176
177class ModuleFile extends Module
178{
179 public File $File;
180
181 function __construct(System $System)
182 {
183 parent::__construct($System);
184 $this->Name = 'File';
185 $this->Version = '1.0';
186 $this->Creator = 'Chronos';
187 $this->License = 'GNU/GPLv3';
188 $this->Description = 'Base module for file management';
189 $this->Dependencies = array(ModuleUser::GetName());
190 $this->Models = array(FileDirectory::GetClassName(), File::GetClassName());
191
192 $this->File = new File($this->System);
193 }
194
195 function DoStart(): void
196 {
197 global $Config;
198
199 $this->System->RegisterPage(['file'], 'PageFile');
200 $this->System->RegisterPage(['file-check'], 'PageFileCheck');
201 $this->File->FilesDir = dirname(__FILE__).'/../../'.$Config['Web']['UploadFileFolder'];
202 $this->System->FormManager->RegisterClass('File', array(
203 'Title' => 'Soubor',
204 'Table' => 'File',
205 'Items' => array(
206 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
207 'Directory' => array('Type' => 'TDirectory', 'Caption' => 'Adresář', 'Default' => '', 'Null' => true),
208 'Size' => array('Type' => 'Integer', 'Caption' => 'Velikost', 'Default' => ''),
209 'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas vytvoření', 'Default' => ''),
210 'Invoices' => array('Type' => 'TFinanceInvoiceListFile', 'Caption' => 'Faktury', 'Default' => ''),
211 'Operations' => array('Type' => 'TFinanceOperationListFile', 'Caption' => 'Operace', 'Default' => ''),
212 'Contracts' => array('Type' => 'TContractListFile', 'Caption' => 'Smlouvy', 'Default' => ''),
213 'StockMoves' => array('Type' => 'TStockMoveListFile', 'Caption' => 'Skladové pohyby', 'Default' => ''),
214 ),
215 'ItemActions' => array(
216 array('Caption' => 'Stáhnout', 'URL' => '/file?i=#RowId'),
217 ),
218 'Actions' => array(
219 array('Caption' => 'Kontrola souborů', 'URL' => '/file-check'),
220 ),
221 ));
222 $this->System->FormManager->RegisterClass('FileDirectory', array(
223 'Title' => 'Adresář souborů',
224 'Table' => 'FileDirectory',
225 'Items' => array(
226 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
227 'Parent' => array('Type' => 'TDirectory', 'Caption' => 'Nadřazený adresář', 'Default' => '', 'Null' => true),
228 'Files' => array('Type' => 'TFileList', 'Caption' => 'Soubory', 'Default' => ''),
229 'Subdirectories' => array('Type' => 'TDirectoryList', 'Caption' => 'Podadresáře', 'Default' => ''),
230 ),
231 ));
232 $this->System->FormManager->RegisterFormType('TDirectory', array(
233 'Type' => 'Reference',
234 'Table' => 'FileDirectory',
235 'Id' => 'Id',
236 'Name' => 'Name',
237 'Filter' => '1',
238 ));
239 $this->System->FormManager->RegisterFormType('TFile', array(
240 'Type' => 'Reference',
241 'Table' => 'File',
242 'Id' => 'Id',
243 'Name' => 'Name',
244 'Filter' => '1',
245 ));
246 $this->System->FormManager->RegisterFormType('TFileList', array(
247 'Type' => 'ManyToOne',
248 'Table' => 'File',
249 'Id' => 'Id',
250 'Ref' => 'Directory',
251 'Filter' => '1',
252 ));
253 $this->System->FormManager->RegisterFormType('TDirectoryList', array(
254 'Type' => 'ManyToOne',
255 'Table' => 'FileDirectory',
256 'Id' => 'Id',
257 'Ref' => 'Parent',
258 'Filter' => '1',
259 ));
260 $this->System->FormManager->RegisterFormType('TFinanceInvoiceListFile', array(
261 'Type' => 'ManyToOne',
262 'Table' => 'FinanceInvoice',
263 'Id' => 'Id',
264 'Ref' => 'File',
265 'Filter' => '1',
266 ));
267 $this->System->FormManager->RegisterFormType('TFinanceOperationListFile', array(
268 'Type' => 'ManyToOne',
269 'Table' => 'FinanceOperation',
270 'Id' => 'Id',
271 'Ref' => 'File',
272 'Filter' => '1',
273 ));
274 $this->System->FormManager->RegisterFormType('TContractListFile', array(
275 'Type' => 'ManyToOne',
276 'Table' => 'Contract',
277 'Id' => 'Id',
278 'Ref' => 'File',
279 'Filter' => '1',
280 ));
281 $this->System->FormManager->RegisterFormType('TStockMoveListFile', array(
282 'Type' => 'ManyToOne',
283 'Table' => 'StockMove',
284 'Id' => 'Id',
285 'Ref' => 'File',
286 'Filter' => '1',
287 ));
288 }
289
290 static function Cast(Module $Module): ModuleFile
291 {
292 if ($Module instanceof ModuleFile)
293 {
294 return $Module;
295 }
296 throw new Exception('Expected ModuleFile type but got '.gettype($Module));
297 }
298}
Note: See TracBrowser for help on using the repository browser.