/
Files.php
313 lines (280 loc) · 12.6 KB
/
Files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* Filebrowser Gadget
*
* @category Gadget
* @package FileBrowser
* @author Jonathan Hernandez <ion@suavizado.com>
* @author Pablo Fischer <pablo@pablo.com.mx>
* @author Ali Fazelzadeh <afz@php.net>
* @copyright 2004-2015 Jaws Development Group
* @license http://www.gnu.org/copyleft/gpl.html
*/
class FileBrowser_Actions_Files extends Jaws_Gadget_Action
{
/**
* Prints all the files with their titles and contents
*
* @access public
* @return mixed XHTML template content with titles and contents or false or error
*/
function Display()
{
if ($this->gadget->registry->fetch('frontend_avail') != 'true') {
return false;
}
if (!$this->gadget->GetPermission('OutputAccess')) {
if ($GLOBALS['app']->Session->Logged()) {
return _t('GLOBAL_ERROR_ACCESS_DENIED');
} else {
return _t('GLOBAL_ERROR_ACCESS_RESTRICTED',
$GLOBALS['app']->Map->GetURLFor('Users', 'LoginBox'),
$GLOBALS['app']->Map->GetURLFor('Users', 'Registration'));
}
}
$path = jaws()->request->fetch('path');
$path = trim((string)$path, '/');
$page = jaws()->request->fetch('page', 'get');
if (is_null($page) || $page <= 0 ) {
$page = 1;
}
$dModel = $this->gadget->model->load('Directory');
$fModel = $this->gadget->model->load('Files');
$locationTree = $dModel->GetCurrentRootDir($path);
if (Jaws_Error::IsError($locationTree)) {
return false;
}
$tpl = $this->gadget->template->load('FileBrowser.html');
$tpl->SetBlock('filebrowser');
$tpl->SetVariable('title', $this->gadget->title);
$this->SetTitle($this->gadget->title);
$parentPath = '';
$tpl->SetVariable('location', _t('FILEBROWSER_LOCATION'));
foreach ($locationTree as $_path => $dir) {
$_path = trim($_path, '/');
$dbFile = $fModel->DBFileInfo($parentPath, $dir);
if (Jaws_Error::IsError($dbFile) || empty($dbFile)) {
$dirTitle = $dir;
} else {
$dirTitle = $dbFile['title'];
// check directory access permission
if (empty($parentPath) && !$this->gadget->GetPermission('OutputAccess', $dbFile['id'])) {
return Jaws_HTTPError::Get(404);
}
}
$parentPath = $_path;
if (empty($_path)) {
$tpl->SetVariable('root', _t('FILEBROWSER_ROOT'));
$tpl->SetVariable('root-path', $this->gadget->urlMap('Display'));
} else {
$tpl->SetBlock('filebrowser/tree');
$tpl->SetVariable('dir-name', $dirTitle);
$tpl->SetVariable('dir-path', $this->gadget->urlMap('Display', array('path' => $_path)));
$tpl->ParseBlock('filebrowser/tree');
}
if ($path == $_path && !empty($dbFile)) {
$tpl->SetVariable('text', $this->gadget->ParseText($dbFile['description']));
}
}
$limit = (int) $this->gadget->registry->fetch('views_limit');
$offset = ($page - 1) * $limit;
$items = $dModel->ReadDir($path, $limit, $offset);
if (!Jaws_Error::IsError($items)) {
$date = Jaws_Date::getInstance();
foreach ($items as $item) {
$tpl->SetBlock('filebrowser/item');
$tpl->SetVariable('icon', $item['icon']);
$tpl->SetVariable('name', Jaws_XSS::filter($item['filename']));
$tpl->SetVariable('title', Jaws_XSS::filter($item['title']));
if ($item['is_dir']) {
$relative = Jaws_XSS::filter($item['relative']) . '/';
$url = $this->gadget->urlMap('Display', array('path' => $relative));
$tpl->SetVariable('url', $url);
} else {
if (empty($item['id'])) {
$tpl->SetVariable('url', Jaws_XSS::filter($item['url']));
} else {
$fid = empty($item['fast_url']) ? $item['id'] : Jaws_XSS::filter($item['fast_url']);
$tpl->SetVariable('url', $this->gadget->urlMap('Download',
array('id' => $fid)));
$tpl->SetBlock('filebrowser/item/info');
$tpl->SetVariable('lbl_info', _t('FILEBROWSER_FILEINFO'));
$tpl->SetVariable('info_url', $this->gadget->urlMap('FileInfo',
array('id' => $fid)));
$tpl->ParseBlock('filebrowser/item/info');
}
}
$tpl->SetVariable('date', $date->Format($item['date']));
$tpl->SetVariable('size', $item['size']);
$tpl->ParseBlock('filebrowser/item');
}
}
if ($tpl->VariableExists('navigation')) {
$total = $dModel->GetDirContentsCount($path);
$params = array('path' => $path);
$tpl->SetVariable('navigation',
$this->GetNumberedPageNavigation($page, $limit, $total, 'Display', $params));
}
$tpl->ParseBlock('filebrowser');
return $tpl->Get();
}
/**
* Get page navigation links
*
* @access private
* @param string $page
* @param string $page_size
* @param string $total
* @param string $action
* @param array $params
* @return string XHTML template content
*/
function GetNumberedPageNavigation($page, $page_size, $total, $action, $params = array())
{
$tpl = $this->gadget->template->load('PageNavigation.html');
$tpl->SetBlock('pager');
$model = $this->gadget->model->load('Files');
$pager = $model->GetEntryPagerNumbered($page, $page_size, $total);
if (count($pager) > 0) {
$tpl->SetBlock('pager/numbered-navigation');
$tpl->SetVariable('total', _t('FILEBROWSER_ENTRIES_COUNT', $pager['total']));
$pager_view = '';
foreach ($pager as $k => $v) {
$tpl->SetBlock('pager/numbered-navigation/item');
$params['page'] = $v;
if ($k == 'next') {
if ($v) {
$tpl->SetBlock('pager/numbered-navigation/item/next');
$tpl->SetVariable('lbl_next', _t('GLOBAL_NEXT'));
$url = $this->gadget->urlMap($action, $params);
$tpl->SetVariable('url_next', $url);
$tpl->ParseBlock('pager/numbered-navigation/item/next');
} else {
$tpl->SetBlock('pager/numbered-navigation/item/no_next');
$tpl->SetVariable('lbl_next', _t('GLOBAL_NEXT'));
$tpl->ParseBlock('pager/numbered-navigation/item/no_next');
}
} elseif ($k == 'previous') {
if ($v) {
$tpl->SetBlock('pager/numbered-navigation/item/previous');
$tpl->SetVariable('lbl_previous', _t('GLOBAL_PREVIOUS'));
$url = $this->gadget->urlMap($action, $params);
$tpl->SetVariable('url_previous', $url);
$tpl->ParseBlock('pager/numbered-navigation/item/previous');
} else {
$tpl->SetBlock('pager/numbered-navigation/item/no_previous');
$tpl->SetVariable('lbl_previous', _t('GLOBAL_PREVIOUS'));
$tpl->ParseBlock('pager/numbered-navigation/item/no_previous');
}
} elseif ($k == 'separator1' || $k == 'separator2') {
$tpl->SetBlock('pager/numbered-navigation/item/page_separator');
$tpl->ParseBlock('pager/numbered-navigation/item/page_separator');
} elseif ($k == 'current') {
$tpl->SetBlock('pager/numbered-navigation/item/page_current');
$url = $this->gadget->urlMap($action, $params);
$tpl->SetVariable('lbl_page', $v);
$tpl->SetVariable('url_page', $url);
$tpl->ParseBlock('pager/numbered-navigation/item/page_current');
} elseif ($k != 'total' && $k != 'next' && $k != 'previous') {
$tpl->SetBlock('pager/numbered-navigation/item/page_number');
$url = $this->gadget->urlMap($action, $params);
$tpl->SetVariable('lbl_page', $v);
$tpl->SetVariable('url_page', $url);
$tpl->ParseBlock('pager/numbered-navigation/item/page_number');
}
$tpl->ParseBlock('pager/numbered-navigation/item');
}
$tpl->ParseBlock('pager/numbered-navigation');
}
$tpl->ParseBlock('pager');
return $tpl->Get();
}
/**
* Action for display file info
*
* @access public
* @return string XHTML template content with titles and contents
*/
function FileInfo()
{
$id = jaws()->request->fetch('id', 'get');
$id = Jaws_XSS::defilter($id);
$fModel = $this->gadget->model->load('Files');
$dModel = $this->gadget->model->load('Directory');
$dbInfo = $fModel->DBFileInfoByIndex($id);
if (Jaws_Error::IsError($dbInfo) || empty($dbInfo)) {
return false;
}
$date = Jaws_Date::getInstance();
$tpl = $this->gadget->template->load('FileBrowser.html');
$tpl->SetBlock('fileinfo');
$Info = $fModel->GetFileProperties($dbInfo['path'], $dbInfo['filename']);
$tpl->SetVariable('icon', $Info['mini_icon']);
$tpl->SetVariable('name', Jaws_XSS::filter($Info['filename']));
$tpl->SetVariable('title', Jaws_XSS::filter($dbInfo['title']));
$tpl->SetVariable('url', Jaws_XSS::filter($Info['url']));
$tpl->SetVariable('date', $date->Format($Info['date']));
$tpl->SetVariable('size', $Info['size']);
$tpl->SetVariable('text', $this->gadget->ParseText($dbInfo['description']));
$locationTree = $dModel->GetCurrentRootDir($dbInfo['path']);
if (Jaws_Error::IsError($locationTree)) {
return false;
}
$parentPath = '';
$tpl->SetVariable('location', _t('FILEBROWSER_LOCATION'));
foreach ($locationTree as $path => $dir) {
if (!empty($dir) && $path{0} == '/') {
$path = substr($path, 1);
}
$dbFile = $fModel->DBFileInfo($parentPath, $dir);
if (Jaws_Error::IsError($dbFile) || empty($dbFile)) {
$dirTitle = $dir;
} else {
$dirTitle = $dbFile['title'];
}
$parentPath = $path;
if (empty($path)) {
$tpl->SetVariable('root', _t('FILEBROWSER_ROOT'));
$tpl->SetVariable('root-path', $this->gadget->urlMap('Display', array('path' => $path), false));
} else {
$tpl->SetBlock('fileinfo/tree');
$tpl->SetVariable('dir-name', $dirTitle);
$tpl->SetVariable('dir-path', $this->gadget->urlMap('Display', array('path' => $path), false));
$tpl->ParseBlock('fileinfo/tree');
}
}
$tpl->ParseBlock('fileinfo');
return $tpl->Get();
}
/**
* Action for providing download file
*
* @access public
* @return string Requested file content or HTML error page
*/
function Download()
{
$id = jaws()->request->fetch('id', 'get');
$id = Jaws_XSS::defilter($id);
$fModel = $this->gadget->model->load('Files');
$iFile = $fModel->DBFileInfoByIndex($id);
if (Jaws_Error::IsError($iFile)) {
$this->SetActionMode('Download', 'normal', 'standalone');
return Jaws_HTTPError::Get(500);
}
if (!empty($iFile)) {
$filepath = $fModel->GetFileBrowserRootDir(). $iFile['path']. '/'. $iFile['filename'];
if (file_exists($filepath)) {
// increase download hits
$fModel->HitFileDownload($iFile['id']);
if (Jaws_Utils::Download($filepath, $iFile['filename'])) {
return;
}
$this->SetActionMode('Download', 'normal', 'standalone');
return Jaws_HTTPError::Get(500);
}
}
$this->SetActionMode('Download', 'normal', 'standalone');
return Jaws_HTTPError::Get(404);
}
}