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

Last change on this file since 708 was 708, checked in by chronos, 10 years ago
  • Přidáno: Zobrazování podadresářů a souborů v adresářích v ISu.
File size: 5.3 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/MimeTypes.php');
4
5class File extends Model
6{
7 var $FilesDir;
8
9 function __construct($System)
10 {
11 parent::__construct($System);
12 $this->FilesDir = '';
13 }
14
15 function Delete($Id)
16 {
17 $DbResult = $this->Database->select('File', 'Name', 'Id='.$Id);
18 if($DbResult->num_rows > 0)
19 {
20 $DbRow = $DbResult->fetch_assoc();
21 $this->Database->delete('File', 'Id='.$Id);
22 unlink($this->FilesDir.'/'.$Id.'_'.$DbRow['Name']);
23 }
24 }
25
26 function CreateFromUpload($Name)
27 {
28 // Submited form with file input have to be enctype="multipart/form-data"
29 $Result = 0;
30 if(array_key_exists($Name, $_FILES) and ($_FILES[$Name]['name'] != ''))
31 {
32 if(file_exists($_FILES[$Name]['tmp_name']))
33 {
34 $FileName = substr($_FILES[$Name]['name'], strrpos($_FILES[$Name]['name'], '/'));
35 $this->Database->query('INSERT INTO File (`Name`, `Size`) VALUES ("'.$FileName.'", '.filesize($_FILES[$Name]['tmp_name']).')');
36 $InsertId = $this->Database->insert_id;
37 if(move_uploaded_file($_FILES[$Name]['tmp_name'], $this->FilesDir.'/'.$InsertId.'_'.$FileName)) $Result = $InsertId;
38 }
39 }
40 return($Result);
41 }
42
43 function DetectMimeType($FileName)
44 {
45 global $MimeTypes;
46
47 $Result = $MimeTypes[pathinfo($FileName, PATHINFO_EXTENSION)][0];
48 return($Result);
49 }
50
51 function Download($Id)
52 {
53 $DbResult = $this->Database->select('File', '*', 'Id='.addslashes($Id));
54 if($DbResult->num_rows > 0)
55 {
56 $DbRow = $DbResult->fetch_assoc();
57 if($DbRow['Directory'] != '') $FileName = $this->GetDir($DbRow['Directory']);
58 else $FileName = $this->FilesDir;
59 $FileName .= $DbRow['Name'];
60 if(file_exists($FileName))
61 {
62 Header('Content-Type: '.$this->DetectMimeType($FileName));
63 Header('Content-Disposition: inline; filename="'.$DbRow['Name'].'"');
64 echo(file_get_contents($FileName));
65 } else echo('Soubor nenalezen!');
66 } else echo('Soubor nenalezen!');
67 }
68
69 function GetDir($Id)
70 {
71 $DbResult = $this->Database->select('FileDirectory', '*', 'Id='.$Id);
72 $DbRow = $DbResult->fetch_assoc();
73 if($DbRow['Parent'] != '') $Result = $this->GetDir($DbRow['Parent']);
74 else $Result = $this->FilesDir;
75 $Result .= $DbRow['Name'].'/';
76 return($Result);
77 }
78}
79
80
81class PageFile extends Page
82{
83 function Show()
84 {
85 if(array_key_exists('id', $_GET)) $Id = $_GET['id'];
86 else if(array_key_exists('i', $_GET)) $Id = $_GET['i'];
87 else return($this->SystemMessage('Chyba', 'Nezadáno id souboru'));
88 $this->ClearPage = true;
89 $Output = $this->System->Modules['File']->Download($Id);
90 return($Output);
91 }
92}
93
94class ModuleFile extends AppModule
95{
96 function __construct($System)
97 {
98 parent::__construct($System);
99 $this->Name = 'File';
100 $this->Version = '1.0';
101 $this->Creator = 'Chronos';
102 $this->License = 'GNU/GPLv3';
103 $this->Description = 'Base module for file management';
104 $this->Dependencies = array();
105 }
106
107 function DoInstall()
108 {
109 }
110
111 function DoUninstall()
112 {
113 }
114
115 function DoStart()
116 {
117 global $Config;
118
119 $this->System->RegisterPage('file', 'PageFile');
120 $this->System->AddModule(new File($this->System));
121 $this->System->Modules['File']->FilesDir = dirname(__FILE__).'/../../'.$Config['Web']['UploadFileFolder'];
122 $this->System->FormManager->RegisterClass('File', array(
123 'Title' => 'Soubor',
124 'Table' => 'File',
125 'Items' => array(
126 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
127 'Directory' => array('Type' => 'TDirectory', 'Caption' => 'Adresář', 'Default' => '', 'Null' => true),
128 'Size' => array('Type' => 'Integer', 'Caption' => 'Velikost', 'Default' => ''),
129 'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas vytvoření', 'Default' => ''),
130 ),
131 'ItemActions' => array(
132 array('Caption' => 'Stáhnout', 'URL' => '/file'),
133 ),
134 ));
135 $this->System->FormManager->RegisterClass('FileDirectory', array(
136 'Title' => 'Adresář souborů',
137 'Table' => 'FileDirectory',
138 'Items' => array(
139 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
140 'Parent' => array('Type' => 'TDirectory', 'Caption' => 'Nadřazený adresář', 'Default' => '', 'Null' => true),
141 'Subdirectories' => array('Type' => 'TDirectoryList', 'Caption' => 'Podadresáře', 'Default' => ''),
142 'Files' => array('Type' => 'TFileList', 'Caption' => 'Soubory', 'Default' => ''),
143 ),
144 ));
145 $this->System->FormManager->RegisterFormType('TDirectory', array(
146 'Type' => 'Reference',
147 'Table' => 'FileDirectory',
148 'Id' => 'Id',
149 'Name' => 'Name',
150 'Filter' => '1',
151 ));
152 $this->System->FormManager->RegisterFormType('TFile', array(
153 'Type' => 'Reference',
154 'Table' => 'File',
155 'Id' => 'Id',
156 'Name' => 'Name',
157 'Filter' => '1',
158 ));
159 $this->System->FormManager->RegisterFormType('TFileList', array(
160 'Type' => 'ManyToOne',
161 'Table' => 'File',
162 'Id' => 'Id',
163 'Ref' => 'Directory',
164 'Filter' => '1',
165 ));
166 $this->System->FormManager->RegisterFormType('TDirectoryList', array(
167 'Type' => 'ManyToOne',
168 'Table' => 'FileDirectory',
169 'Id' => 'Id',
170 'Ref' => 'Parent',
171 'Filter' => '1',
172 ));
173 }
174
175 function DoStop()
176 {
177 }
178}
Note: See TracBrowser for help on using the repository browser.