Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 271 lines (177 sloc) 8.709 kb
d0383db @pointybeard Initial commit
authored
1 <?php
2
3 require_once('lib/class.file.php');
4
5 Class extension_filemanager extends Extension{
6
7 public function about(){
8 return array('name' => 'File Manager',
9 'version' => '0.8',
10 'release-date' => '2008-01-31',
11 'author' => array('name' => 'Alistair Kearney',
12 'website' => 'http://www.pointybeard.com',
13 'email' => 'alistair@pointybeard.com'),
14 'description' => 'Upload, Edit and manage files and folders.'
15 );
16 }
17
18 public static function baseURL(){
19 return URL . '/symphony/extension/filemanager/';
20 }
21
22 public function fetchNavigation(){
23 return array(
24 array(
25 'location' => 100,
26 'name' => 'File Manager',
27 'children' => array(
28 array(
29 'name' => 'Browse',
30 'link' => '/browse/'
31 )
32 )
33 )
34
35 );
36 }
37
38 public function getSubscribedDelegates(){
39 return array(
40
41 array(
42 'page' => '/system/preferences/',
43 'delegate' => 'AddCustomPreferenceFieldsets',
44 'callback' => 'appendPreferences'
45 ),
46
47 array(
48
49 'page' => '/system/preferences/',
50 'delegate' => 'Save',
51 'callback' => 'savePreferences'
52
53 ),
54 );
55 }
56
57 public function savePreferences($context){
58
59 $conf = array_map('trim', $context['settings']['filemanager']);
60
61 $context['settings']['filemanager'] = array(
62 'show-hidden' => (isset($conf['show-hidden']) ? 'yes' : 'no'),
63 'start-location' => '/' . trim($conf['start-location'], '/ '),
64 );
65
66 }
67
68 public function appendPreferences($context){
69
70 $group = new XMLElement('fieldset');
71 $group->setAttribute('class', 'settings');
72 $group->appendChild(new XMLElement('legend', 'File Manager'));
73
74 $label = Widget::Label('Root Browse Location');
75 $label->appendChild(Widget::Input('settings[filemanager][start-location]', General::Sanitize(Administration::instance()->Configuration->get('start-location', 'filemanager'))));
76 $group->appendChild($label);
77
78 $group->appendChild(new XMLElement('p', 'This path is relative to the root Symphony installation folder, <code>'.DOCROOT.'</code>', array('class' => 'help')));
79
80 $label = Widget::Label();
81 $input = Widget::Input('settings[filemanager][show-hidden]', 'yes', 'checkbox');
82 if(Administration::instance()->Configuration->get('show-hidden', 'filemanager') == 'yes') $input->setAttribute('checked', 'checked');
83 $label->setValue($input->generate() . ' Show Hidden Files and Folders');
84 $group->appendChild($label);
85
86 $group->appendChild(new XMLElement('p', 'Hidden files will not be included in archives unless this is checked.', array('class' => 'help')));
87
88 $context['wrapper']->appendChild($group);
89
90 }
91
92 public static function getFileMIMEType($file){
93
94 $types = array(
95 '/.(jpg|jpeg)$/i' => 'image/jpeg',
96 '/.gif$/i' => 'image/gif',
97 '/.png$/i' => 'image/png',
98 '/.pdf$/i' => 'application/pdf'
99 );
100
101 foreach ($types as $pattern => $mimetype) {
102 if (preg_match($pattern, $file)) return $mimetype;
103 }
104
105 return 'application/octet-stream';
106
107 }
108
109 public function download($file){
110
111 $file = DOCROOT . $this->getStartLocation() . $file;
112
113 if(!file_exists($file))
114 $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'));
115
116 header('Pragma: public');
117 header('Expires: 0');
118 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
119 header('Content-Type: ' . self::getFileMIMEType($file));
120 header('Content-Disposition: attachment; filename=' . basename($file) . ';');
121 header('Content-Length: ' . filesize($file));
122
123 readfile($file);
124
125 break;
126
127 }
128
129 public function getStartLocation(){
130 return Administration::instance()->Configuration->get('start-location', 'filemanager');
131 }
132
133 public static function buildBreadCrumbs(array $crumbs){
134
135 if(empty($crumbs)) return;
136
137 $ArrayObject = new ArrayObject($crumbs);
138 $Iterator = $ArrayObject->getIterator();
139
d23f334 @cz Linkify Breadcrumbs
cz authored
140 $path = NULL;
d0383db @pointybeard Initial commit
authored
141 $result = NULL;
142
143 while($Iterator->valid()){
d23f334 @cz Linkify Breadcrumbs
cz authored
144 $path .= $Iterator->current() . '/';
145 $result .= '<a href="'. URL .'/symphony/extension/filemanager/browse/' . $path . '">' . $Iterator->current() . '</a>/';
d0383db @pointybeard Initial commit
authored
146 $Iterator->next();
147 }
148
149 return $result;
150 }
151
152 public function findAvailableArchiveName($path){
153
154 $count = 0;
155 $filename = NULL;
156
157 if(is_dir($path)):
158
159 do{
160
161 $filename = 'Archive' . ($count == 0 ? NULL : "-$count") . '.zip';
162 $count++;
163
164 }while(file_exists($path . '/' . $filename));
165
166
167
168 else:
169
170 do{
171
172 $filename = basename($path) . ($count == 0 ? NULL : "-$count") . '.zip';
173 $count++;
174
175 }while(file_exists(dirname($path) . '/' . $filename));
176
177 endif;
178
179 return $filename;
180
181 }
182
183 public function createArchive(array $files, $path=NULL){
184
185 require_once(TOOLKIT . '/class.archivezip.php');
186 $archive = new ArchiveZip;
187
188 $flag = (Administration::instance()->Configuration->get('show-hidden', 'filemanager') == 'yes' ? ArchiveZip::IGNORE_HIDDEN : NULL);
189
190 $root = DOCROOT . $this->getStartLocation();
191
192 foreach($files as $f){
193
194 if(is_dir($root . $f)) $archive->addDirectory($root . $f, $root . rtrim($path, '/'), $flag);
195 else $archive->addFromFile($root . $f, basename($f));
196
197 }
198
199 $zip_file = $root . rtrim($path, '/') . '/' . $this->findAvailableArchiveName($root . (count($files) > 1 || is_dir($root . $files[0]) ? rtrim($path, '/') : $f));
200
201 $archive->save($zip_file);
202
203 return (@file_exists($zip_file) ? $zip_file : NULL);
204
205 }
206
207 public function buildTableRow(DirectoryIterator $file, $includeParentDirectoryDots=true){
208
209 if(!$file->isDot() && substr($file->getFilename(), 0, 1) == '.' && Administration::instance()->Configuration->get('show-hidden', 'filemanager') != 'yes') return;
210 elseif($file->isDot() && !$includeParentDirectoryDots && $file->getFilename() == '..') return;
211 elseif($file->getFilename() == '.') return;
212
213 $relpath = str_replace(DOCROOT . $this->getStartLocation(), NULL, $file->getPathname());
214
215 if(!$file->isDir()){
216
217 //if(File::fileType($file->getFilename()) == self::CODE)
218 // $download_uri = self::baseURL() . 'edit/?file=' . urlencode($relpath);
219
220 //else
221 $download_uri = self::baseURL() . 'download/?file=' . urlencode($relpath);
222 }
223
224 else $download_uri = self::baseURL() . 'browse' . $relpath . '/';
225
226 if(!$file->isDot()){
227 $td1 = Widget::TableData(Widget::Anchor($file->getFilename(), $download_uri, NULL, 'file-type ' . ($file->isDir() ? 'folder' : File::fileType($file->getFilename()))));
228
229 //$group = (function_exists('posix_getgrgid') ? posix_getgrgid($file->getGroup()) : $file->getGroup());
230 //$owner = (function_exists('posix_getpwuid') ? posix_getpwuid($file->getOwner()) : $file->getOwner());
231
232 $group = $file->getGroup();
233 $owner = $file->getOwner();
234
235 $td3 = Widget::TableData(File::getReadablePerm($file->getPerms()), NULL, NULL, NULL, array('title' => File::getOctalPermission($file->getPerms()) . ', ' . (isset($owner['name']) ? $owner['name'] : $owner) . ':' . (isset($group['name']) ? $group['name'] : $group)));
236
237 if($file->isWritable())
238 $td4 = Widget::TableData(Widget::Anchor('Edit', self::baseURL() . 'properties/?file=' . urlencode($relpath)));
239
240 else
241 $td4 = Widget::TableData('-', 'inactive');
242
243 }
244
245 else{
246 $td1 = Widget::TableData(Widget::Anchor('&crarr;', $download_uri));
247 $td3 = Widget::TableData('-', 'inactive');
248 $td4 = Widget::TableData('-', 'inactive');
249 }
250
251 $td2 = Widget::TableData(($file->isDir() ? '-' : General::formatFilesize($file->getSize())), ($file->isDir() ? 'inactive' : NULL));
252
253
254 $startlocation = DOCROOT . $this->getStartLocation();
255
256 if(!$file->isDot()) $td4->appendChild(Widget::Input('items['.str_replace($startlocation, '', $file->getPathname()) . ($file->isDir() ? '/' : NULL).']', NULL, 'checkbox'));
257
258 return Widget::TableRow(array($td1, $td2, $td3, $td4));
259
260 }
261
262
263
264 private static function __countItemsInDirectory(DirectoryIterator $dir){
265 $count = iterator_count($dir);
266 return $count . ' item' . ($count > 1 ? 's' : NULL);
267 }
268 }
269
d23f334 @cz Linkify Breadcrumbs
cz authored
270 ?>
Something went wrong with that request. Please try again.