-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.driver.php
executable file
·285 lines (193 loc) · 9.9 KB
/
extension.driver.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
<?php
require_once('lib/class.file.php');
Class extension_filemanager extends Extension{
public function about(){
return array('name' => 'File Manager',
'version' => '0.9',
'release-date' => '2010-01-31',
'author' => array('name' => 'Symphony Team',
'website' => 'http://symphony-cms.com',
'email' => 'team@symphony-cms.com'),
'description' => 'Upload, Edit and manage files and folders.'
);
}
public static function baseURL(){
return URL . '/symphony/extension/filemanager/';
}
public function fetchNavigation() {
return array(
array(
'location' => 'System',
'name' => 'File Manager',
'link' => '/browse/'
)
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
);
}
public function savePreferences($context){
$conf = array_map('trim', $context['settings']['filemanager']);
$context['settings']['filemanager'] = array(
'show-hidden' => (isset($conf['show-hidden']) ? 'yes' : 'no'),
'start-location' => ($conf['start-location'] == '' || $conf['start-location'] == '/' ? '' : '/' . trim($conf['start-location'], '/ ')),
'archive-name' => (isset($conf['archive-name']) ? $conf['archive-name'] : 'Backup')
);
}
public function appendPreferences($context){
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', 'File Manager'));
$label = Widget::Label('Root Browse Location');
$label->appendChild(Widget::Input('settings[filemanager][start-location]', General::Sanitize(Administration::instance()->Configuration->get('start-location', 'filemanager'))));
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'This path is relative to the root Symphony installation folder, <code>'.DOCROOT.'</code>', array('class' => 'help')));
$label = Widget::Label('Archive Name');
$label->appendChild(Widget::Input('settings[filemanager][archive-name]', General::Sanitize(Administration::instance()->Configuration->get('archive-name', 'filemanager'))));
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'Default filename for archives generated by File Manager', array('class' => 'help')));
$label = Widget::Label();
$input = Widget::Input('settings[filemanager][show-hidden]', 'yes', 'checkbox');
if(Administration::instance()->Configuration->get('show-hidden', 'filemanager') == 'yes') $input->setAttribute('checked', 'checked');
$label->setValue($input->generate() . ' Show Hidden Files and Folders');
$group->appendChild($label);
$group->appendChild(new XMLElement('p', 'Hidden files will not be included in archives unless this is checked.', array('class' => 'help')));
$context['wrapper']->appendChild($group);
}
public static function getFileMIMEType($file){
$types = array(
'/.(jpg|jpeg)$/i' => 'image/jpeg',
'/.gif$/i' => 'image/gif',
'/.png$/i' => 'image/png',
'/.pdf$/i' => 'application/pdf'
);
foreach ($types as $pattern => $mimetype) {
if (preg_match($pattern, $file)) return $mimetype;
}
return 'application/octet-stream';
}
public function download($file){
$file = DOCROOT . $this->getStartLocation() . $file;
if(!file_exists($file))
$this->_Parent->customError(E_USER_ERROR, 'File Not Found', 'The file you requested, <code>'.$file.'</code>, does not exist.', false, true, 'error', array('header' => 'HTTP/1.0 404 Not Found'));
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: ' . self::getFileMIMEType($file));
header('Content-Disposition: attachment; filename=' . basename($file) . ';');
header('Content-Length: ' . filesize($file));
readfile($file);
break;
}
public function getStartLocation(){
return Administration::instance()->Configuration->get('start-location', 'filemanager');
}
public function getStartLocationLink(){
$start_location = Administration::instance()->Configuration->get('start-location', 'filemanager');
$start_name = explode('/',trim(DOCROOT . $start_location, '/'));
return Widget::Anchor(end($start_name), URL . '/symphony/extension/filemanager/browse')->generate();
}
public static function buildBreadCrumbs(array $crumbs){
if(empty($crumbs)) return;
$ArrayObject = new ArrayObject($crumbs);
$Iterator = $ArrayObject->getIterator();
$path = NULL;
$result = NULL;
while($Iterator->valid()){
$path .= $Iterator->current() . '/';
$result .= ' / ' . Widget::Anchor($Iterator->current(), URL . '/symphony/extension/filemanager/browse/' . $path)->generate();
$Iterator->next();
}
return $result;
}
public function findAvailableArchiveName($path){
$count = 0;
$filename = NULL;
$archivename = Administration::instance()->Configuration->get('archive-name', 'filemanager');
if(is_dir($path)):
if ($archivename == '' || $archivename == null) $archivename = basename($path);
do{
$filename = $archivename . ($count == 0 ? NULL : "-$count") . '.zip';
$count++;
}while(file_exists($path . '/' . $filename));
else:
do{
$filename = basename($path) . ($count == 0 ? NULL : "-$count") . '.zip';
$count++;
}while(file_exists(dirname($path) . '/' . $filename));
endif;
return $filename;
}
public function createArchive(array $files, $path=NULL){
require_once(TOOLKIT . '/class.archivezip.php');
$archive = new ArchiveZip;
$flag = (Administration::instance()->Configuration->get('show-hidden', 'filemanager') == 'yes' ? ArchiveZip::IGNORE_HIDDEN : NULL);
$root = DOCROOT . $this->getStartLocation();
foreach($files as $f){
if(is_dir($root . $f)) $archive->addDirectory($root . $f, $root . rtrim($path, '/'), $flag);
else $archive->addFromFile($root . $f, basename($f));
}
$zip_file = $root . rtrim($path, '/') . '/' . $this->findAvailableArchiveName($root . (count($files) > 1 || is_dir($root . $files[0]) ? rtrim($path, '/') : $f));
$archive->save($zip_file);
return (@file_exists($zip_file) ? $zip_file : NULL);
}
public function buildTableRow(DirectoryIterator $file, $includeParentDirectoryDots=true){
if(!$file->isDot() && substr($file->getFilename(), 0, 1) == '.' && Administration::instance()->Configuration->get('show-hidden', 'filemanager') != 'yes') return;
elseif($file->isDot() && !$includeParentDirectoryDots && $file->getFilename() == '..') return;
elseif($file->getFilename() == '.') return;
$relpath = str_replace(($this->getStartLocation() == '' ? DOCROOT : DOCROOT . $this->getStartLocation()), NULL, $file->getPathname());
if(!$file->isDir()){
//if(File::fileType($file->getFilename()) == self::CODE)
// $download_uri = self::baseURL() . 'edit/?file=' . urlencode($relpath);
//else
$download_uri = self::baseURL() . 'download/?file=' . urlencode($relpath);
}
else $download_uri = self::baseURL() . 'properties/?file=' . urlencode($relpath) . '/';
if(!$file->isDot()){
$td1 = Widget::TableData(Widget::Anchor($file->getFilename(), self::baseURL() . ($file->isDir() ? 'browse' . $relpath . '/' : 'properties/?file=' . urlencode($relpath)), NULL, 'file-type ' . ($file->isDir() ? 'folder' : File::fileType($file->getFilename()))));
//$group = (function_exists('posix_getgrgid') ? posix_getgrgid($file->getGroup()) : $file->getGroup());
//$owner = (function_exists('posix_getpwuid') ? posix_getpwuid($file->getOwner()) : $file->getOwner());
$group = $file->getGroup();
$owner = $file->getOwner();
$td2 = Widget::TableData(General::formatFilesize($file->getSize()), NULL);
$td3 = Widget::TableData(File::getOctalPermission($file->getPerms()) . ' <span class="inactive">' . File::getReadablePerm($file->getPerms()), NULL, NULL, NULL, array('title' => (isset($owner['name']) ? $owner['name'] : $owner) . ', ' . (isset($group['name']) ? $group['name'] : $group) . '</span>'));
$td4 = Widget::TableData(DateTimeObj::get(__SYM_DATETIME_FORMAT__, $file->getMTime()));
if($file->isWritable()) {
if($file->isDir()){
$td5 = Widget::TableData(Widget::Anchor('Edit', $download_uri));
} else {
$td5 = Widget::TableData(Widget::Anchor('Download', $download_uri));
}
}
else {
$td5 = Widget::TableData('-', 'inactive');
}
}
else{
$td1 = Widget::TableData(Widget::Anchor('↵', self::baseURL() . 'browse' . $relpath . '/'));
$td2 = Widget::TableData('-', 'inactive');
$td3 = Widget::TableData('-', 'inactive');
$td4 = Widget::TableData('-', 'inactive');
$td5 = Widget::TableData('-', 'inactive');
}
$startlocation = DOCROOT . $this->getStartLocation();
if(!$file->isDot()) $td5->appendChild(Widget::Input('items['.str_replace($startlocation, '', $file->getPathname()) . ($file->isDir() ? '/' : NULL).']', NULL, 'checkbox'));
return Widget::TableRow(array($td1, $td2, $td3, $td4, $td5));
}
private static function __countItemsInDirectory(DirectoryIterator $dir){
$count = iterator_count($dir);
return $count . ' item' . ($count > 1 ? 's' : NULL);
}
}
?>