-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directory.php
288 lines (260 loc) · 10.6 KB
/
Directory.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
<?php
/**
* Directory Gadget
*
* @category Gadget
* @package Directory
* @author Mohsen Khahani <mkhahani@gmail.com>
* @copyright 2013-2015 Jaws Development Group
* @license http://www.gnu.org/copyleft/gpl.html
*/
class Directory_Actions_Directory extends Jaws_Gadget_Action
{
/**
* Builds directory and file navigation UI
*
* @access public
* @return string XHTML UI
*/
function Directory()
{
$tpl = $this->gadget->template->load('Directory.html');
$tpl->SetBlock('directory');
$id = (int)jaws()->request->fetch('id');
if ($id == 0) {
$tpl->SetVariable('content', $this->ListFiles());
} else {
$model = $this->gadget->model->loadAdmin('Files');
$file = $model->GetFile($id);
if (Jaws_Error::IsError($file) || empty($file)) {
require_once JAWS_PATH . 'include/Jaws/HTTPError.php';
return Jaws_HTTPError::Get(404);
}
if ($file['is_dir']) {
$tpl->SetVariable('content', $this->ListFiles($id));
} else {
$tpl->SetVariable('content', $this->ViewFile($file));
}
$tpl->SetVariable('root', _t('DIRECTORY_HOME'));
$tpl->SetVariable('root_url', $this->gadget->urlMap('Directory'));
$tpl->SetVariable('path', $this->GetPath($id));
}
$tpl->ParseBlock('directory');
return $tpl->Get();
}
/**
* Fetches and displays list of dirs/files
*
* @access public
* @return string HTML content
*/
function ListFiles($parent = 0)
{
$params = jaws()->request->fetch(array('page'), 'get');
$page = (int)$params['page'];
$params['limit'] = (int)$this->gadget->registry->fetch('items_per_page');
$params['offset'] = ($page == 0)? 0 : $params['limit'] * ($page - 1);
$params['parent'] = $parent;
$params['hidden'] = false;
$model = $this->gadget->model->loadAdmin('Files');
$files = $model->GetFiles($params);
if (Jaws_Error::IsError($files)){
return '';
}
if (empty($files)){
return _t('DIRECTORY_INFO_NO_FILES');
}
$count = $model->GetFiles($params, true);
$tpl = $this->gadget->template->load('Directory.html');
$tpl->SetBlock('files');
$tpl->SetVariable('lbl_title', _t('DIRECTORY_FILE_TITLE'));
$tpl->SetVariable('lbl_created', _t('DIRECTORY_FILE_CREATED'));
$tpl->SetVariable('lbl_modified', _t('DIRECTORY_FILE_MODIFIED'));
$tpl->SetVariable('lbl_type', _t('DIRECTORY_FILE_TYPE'));
$tpl->SetVariable('lbl_size', _t('DIRECTORY_FILE_SIZE'));
$tpl->SetVariable('site_url', $GLOBALS['app']->getSiteURL('/'));
$theme = $GLOBALS['app']->GetTheme();
$iconUrl = is_dir($theme['url'] . 'mimetypes')?
$theme['url'] . 'mimetypes/' : 'images/mimetypes/';
$objDate = Jaws_Date::getInstance();
foreach ($files as $file) {
$url = $this->gadget->urlMap('Directory', array('id' => $file['id']));
$tpl->SetBlock('files/file');
$tpl->SetVariable('url', $url);
$tpl->SetVariable('title', $file['title']);
$tpl->SetVariable('type', empty($file['filetype'])? '-' : $file['filetype']);
$tpl->SetVariable('size', Jaws_Utils::FormatSize($file['filesize']));
$tpl->SetVariable('created', $objDate->Format($file['createtime'], 'n/j/Y g:i a'));
$tpl->SetVariable('modified', $objDate->Format($file['updatetime'], 'n/j/Y g:i a'));
if ($file['is_dir']) {
$tpl->SetVariable('icon', $iconUrl . 'folder.png');
} else {
// if (!empty($file['filetype'])) {
// $tpl->SetVariable('icon', $iconUrl . $file['filetype'] . '.png');
// } else {
$tpl->SetVariable('icon', $iconUrl . 'file-generic.png');
// }
}
$tpl->ParseBlock('files/file');
}
// Pagination
if ($tpl->VariableExists('pagination') && $params['limit'] > 0) {
$action = $this->gadget->action->load('Pagination');
$args = array();
if ($parent > 0) {
$args['id'] = $parent;
}
$tpl->setVariable('pagination', $action->Pagination($page, $params['limit'], $count, 'Directory', $args));
}
$tpl->ParseBlock('files');
return $tpl->Get();
}
/**
* Displays file properties
*
* @access public
* @return string HTML content
*/
function ViewFile($file)
{
$tpl = $this->gadget->template->load('Directory.html');
$tpl->SetBlock('file');
$tpl->SetVariable('lbl_title', _t('DIRECTORY_FILE_TITLE'));
$tpl->SetVariable('lbl_desc', _t('DIRECTORY_FILE_DESC'));
$tpl->SetVariable('lbl_filename', _t('DIRECTORY_FILE_FILENAME'));
$tpl->SetVariable('lbl_type', _t('DIRECTORY_FILE_TYPE'));
$tpl->SetVariable('lbl_size', _t('DIRECTORY_FILE_SIZE'));
$tpl->SetVariable('lbl_bytes', _t('DIRECTORY_BYTES'));
$tpl->SetVariable('lbl_created', _t('DIRECTORY_FILE_CREATED'));
$tpl->SetVariable('lbl_modified', _t('DIRECTORY_FILE_MODIFIED'));
$tpl->SetVariable('lbl_download', _t('DIRECTORY_DOWNLOAD'));
$objDate = Jaws_Date::getInstance();
$file['created'] = $objDate->Format($file['createtime'], 'n/j/Y g:i a');
$file['modified'] = $objDate->Format($file['updatetime'], 'n/j/Y g:i a');
$file['type'] = empty($file['filetype'])? '-' : $file['filetype'];
$file['size'] = Jaws_Utils::FormatSize($file['filesize']);
$file['download'] = $this->gadget->urlMap('Download', array('id' => $file['id']));
foreach ($file as $key => $value) {
$tpl->SetVariable($key, $value);
}
// display tags
if (Jaws_Gadget::IsGadgetInstalled('Tags')) {
$tagsHTML = Jaws_Gadget::getInstance('Tags')->action->load('Tags');
$tagsHTML->loadReferenceTags('Directory', 'file', $file['id'], $tpl, 'file');
}
// display file
$fileInfo = pathinfo($file['host_filename']);
if (isset($fileInfo['extension'])) {
$ext = $fileInfo['extension'];
$type = '';
if ($ext === 'txt') {
$type = 'text';
} else if (in_array($ext, array('jpg', 'jpeg', 'png', 'gif', 'svg'))) {
$type = 'image';
} else if (in_array($ext, array('wav', 'mp3'))) {
$type = 'audio';
} else if (in_array($ext, array('webm', 'mp4', 'ogg'))) {
$type = 'video';
}
if ($type != '') {
$tpl->SetVariable('preview', $this->PlayMedia($file, $type));
}
}
// display comments/comment-form
if (Jaws_Gadget::IsGadgetInstalled('Comments')) {
$allow_comments = $this->gadget->registry->fetch('allow_comments', 'Comments');
$cHTML = Jaws_Gadget::getInstance('Comments')->action->load('Comments');
$tpl->SetVariable('comments', $cHTML->ShowComments('Directory', 'File', $file['id'],
array('action' => 'Directory', 'params' => array('id' => $file['id']))));
if ($allow_comments == 'true') {
$redirect_to = $this->gadget->urlMap('Directory', array('id' => $file['id']));
$tpl->SetVariable('comment-form', $cHTML->ShowCommentsForm('Directory', 'File', $file['id'], $redirect_to));
} elseif ($allow_comments == 'restricted') {
$login_url = $GLOBALS['app']->Map->GetURLFor('Users', 'LoginBox');
$register_url = $GLOBALS['app']->Map->GetURLFor('Users', 'Registration');
$tpl->SetVariable('comment-form', _t('COMMENTS_COMMENTS_RESTRICTED', $login_url, $register_url));
}
}
// Show like rating
if (Jaws_Gadget::IsGadgetInstalled('Rating')) {
$ratingHTML = Jaws_Gadget::getInstance('Rating')->action->load('RatingTypes');
$ratingHTML->loadReferenceLike('Directory', 'File', $file['id'], 0, $tpl, 'file');
}
$tpl->ParseBlock('file');
return $tpl->Get();
}
/**
* Fetches path of a file/directory
*
* @access public
* @return array Directory hierarchy
*/
function GetPath($id)
{
$path = '';
$pathArr = array();
$model = $this->gadget->model->loadAdmin('Files');
$model->GetPath($id, $pathArr);
foreach(array_reverse($pathArr) as $i => $p) {
$url = $this->gadget->urlMap('Directory', array('id' => $p['id']));
$path .= ($i == count($pathArr) - 1)?
' > ' . $p['title'] :
" > <a href='$url'>" . $p['title'] . '</a>';
}
return $path;
}
/**
* Displays file
*
* @access public
* @return array Response array
*/
function PlayMedia($file, $type)
{
$tpl = $this->gadget->template->loadAdmin('Media.html');
$tpl->SetBlock($type);
if ($type === 'text') {
$filename = $GLOBALS['app']->getDataURL('directory/') . $file['host_filename'];
if (file_exists($filename)) {
$tpl->SetVariable('text', file_get_contents($filename));
}
} else {
$tpl->SetVariable('url', $this->gadget->urlMap('Download', array('id' => $file['id'])));
}
$tpl->ParseBlock($type);
return $this->gadget->ParseText($tpl->get(), 'Directory', 'index');
}
/**
* Downloads(streams) file
*
* @access public
* @return mixed File data or Jaws_Error
*/
function Download()
{
$id = jaws()->request->fetch('id');
if (is_null($id)) {
return Jaws_HTTPError::Get(500);
}
$id = (int)$id;
$model = $this->gadget->model->loadAdmin('Files');
// Validate file
$file = $model->GetFile($id);
if (Jaws_Error::IsError($file)) {
return Jaws_HTTPError::Get(500);
}
if (empty($file) || empty($file['user_filename'])) {
return Jaws_HTTPError::Get(404);
}
// Check for file existence
$filename = $GLOBALS['app']->getDataURL("directory/") . $file['host_filename'];
if (!file_exists($filename)) {
return Jaws_HTTPError::Get(404);
}
// Stream file
if (!Jaws_Utils::Download($filename, $file['user_filename'], $file['filetype'])) {
return Jaws_HTTPError::Get(500);
}
return true;
}
}