Automad
 All Classes Functions Variables Pages
files.php
1 <?php
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  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
44 /*
45  * File Manager. In case $_POST['url'] is defined, the files of that page will be managed.
46  * Else, the files under "/shared" will be managed instead.
47  *
48  * Basically the inner (!) HTML form the calling form will be replaced with the updated file list.
49  * So that means, that the outer form tags are NOT part of the HTML output!
50  */
51 
52 
53 $output = array();
54 
55 
56 // Check if file from a specified page or the shared files will be listed and managed.
57 // To display a file list of a certain page, its URL has to be submitted along with the form data.
58 if (isset($_POST['url']) && array_key_exists($_POST['url'], $this->collection)) {
59 
60  $url = $_POST['url'];
61  $Page = $this->collection[$url];
62  $path = AM_BASE_DIR . AM_DIR_PAGES . $Page->path;
63 
64 } else {
65 
66  $url = '';
67  $path = AM_BASE_DIR . AM_DIR_SHARED . '/';
68 
69 }
70 
71 
72 // Delete files in $_POST['delete'].
73 if (isset($_POST['delete'])) {
74 
75  // Check if directory is writable.
76  if (is_writable($path)) {
77 
78  $success = array();
79  $errors = array();
80 
81  foreach ($_POST['delete'] as $f) {
82 
83  // Make sure submitted filename has no '../' (basename).
84  $file = $path . basename($f);
85 
86  if (is_writable($file)) {
87  if (unlink($file)) {
88  $success[] = $this->tb['success_remove'] . ' <strong>' . basename($file) . '</strong>';
89  }
90  } else {
91  $errors[] = $this->tb['error_remove'] . ' <strong>' . basename($file) . '</strong>';
92  }
93 
94  }
95 
96  // Clear cache to update galleries and sliders.
97  $Cache = new Cache();
98  $Cache->clear();
99 
100  $output['success'] = implode('<br />', $success);
101  $output['error'] = implode('<br />', $errors);
102 
103  } else {
104 
105  $output['error'] = $this->tb['error_permission'] . '<p>' . $path . '</p>';
106 
107  }
108 
109 }
110 
111 
112 // Get the allowed file types from const.php.
113 $allowedFileTypes = Parse::allowedFileTypes();
114 
115 
116 // Define image file extensions.
117 $imageTypes = array('jpg', 'png', 'gif');
118 
119 
120 // Get files for each allowed file type.
121 $files = array();
122 
123 foreach ($allowedFileTypes as $type) {
124 
125  if ($f = glob($path . '*.{' . strtolower($type) . ',' . strtoupper($type) . '}', GLOB_BRACE)) {
126  $files = array_merge($files, $f);
127  }
128 
129 }
130 
131 
132 ob_start();
133 
134 
135 if ($files) {
136 
137  sort($files);
138 
139  foreach ($files as $file) {
140 
141  ?>
142 
143  <div class="box">
144 
145  <div class="row">
146 
147  <?php
148 
149  $extension = pathinfo($file, PATHINFO_EXTENSION);
150 
151  if (in_array(strtolower($extension), $imageTypes)) {
152 
153  $img = new Image($file, 125, 125, true);
154 
155  echo '<div class="col-xs-3">' .
156  '<a href="' . str_replace(AM_BASE_DIR, AM_BASE_URL, $file) . '" target="_blank" title="Download" tabindex=-1>' .
157  '<img class="img-responsive" src="' . AM_BASE_URL . $img->file . '" width="' . $img->width . '" height="' . $img->height . '" />' .
158  '</a>' .
159  '</div>' .
160  '<div class="col-xs-8">' .
161  '<h4><a title="' . $this->tb['btn_rename_file'] . '" href="#" data-target="#automad-rename-file-modal" data-toggle="modal" data-file="' . basename($file) . '">' . basename($file) . ' <span class="glyphicon glyphicon-pencil"></span></a></h4>';
162 
163  if (strtolower($extension) == 'jpg' && $img->description) {
164  echo '<h6 title="Exif description"><span class="glyphicon glyphicon-comment"></span> ' . $img->description . '</h6>';
165  }
166 
167  echo '<h6 title="Modification time"><span class="glyphicon glyphicon-time"></span> ' . date('F j, Y / H:i', filemtime($file)) . '</h6>' .
168  '<h6 title="Path relative to the Automad base directory"><span class="glyphicon glyphicon-hdd"></span> ' . str_replace(AM_BASE_DIR, '', $file) . '</h6>' .
169  '<div class="badge">' . $img->originalWidth . 'x' . $img->originalHeight . '</div>' .
170  '</div>';
171 
172  } else {
173 
174  echo '<div class="col-xs-3">' .
175  '<a class="filetype img-responsive" href="' . str_replace(AM_BASE_DIR, AM_BASE_URL, $file) . '" target="_blank" title="Download" tabindex=-1>' .
176  '<span class="glyphicon glyphicon-file"></span> ' . $extension .
177  '</a>' .
178  '</div>' .
179  '<div class="col-xs-8">' .
180  '<h4><a title="' . $this->tb['btn_rename_file'] . '" href="#" data-target="#automad-rename-file-modal" data-toggle="modal" data-file="' . basename($file) . '">' . basename($file) . ' <span class="glyphicon glyphicon-pencil"></span></a></h4>' .
181  '<h6 title="Modification time"><span class="glyphicon glyphicon-time"></span> ' . date('F j, Y / H:i', filemtime($file)) . '</h6>' .
182  '<h6 title="Path relative to the Automad base directory"><span class="glyphicon glyphicon-hdd"></span> ' . str_replace(AM_BASE_DIR, '', $file) . '</h6>' .
183  '</div>';
184 
185  }
186 
187  ?>
188 
189  <div class="col-xs-1">
190  <div class="pull-right btn-group" data-toggle="buttons">
191  <label class="btn btn-default btn-xs">
192  <input type="checkbox" name="delete[]" value="<?php echo basename($file); ?>"><span class="glyphicon glyphicon-ok"></span>
193  </label>
194  </div>
195  </div>
196 
197  </div>
198 
199  </div>
200 
201  <?php
202 
203  }
204 
205 } else {
206 
207  ?><div class="alert alert-warning"><?php echo $this->tb['error_no_files']; ?></div><?php
208 
209 }
210 
211 ?>
212 
213 <hr>
214 
215 <div class="btn-group btn-group-justified">
216  <div class="btn-group">
217  <button type="submit" class="btn btn-danger" data-loading-text="<?php echo $this->tb['btn_loading']; ?>"><span class="glyphicon glyphicon-trash"></span> <?php echo $this->tb['btn_remove_selected']; ?></button>
218  </div>
219  <div class="btn-group">
220  <a class="btn btn-primary" href="#" data-target="#automad-upload-modal"><span class="glyphicon glyphicon-open"></span> <?php echo $this->tb['btn_upload']; ?></a>
221  </div>
222 </div>
223 
224 
225 <!-- Rename File Modal -->
226 <div class="modal fade" id="automad-rename-file-modal" tabindex="-1" data-automad-url="<?php echo $url; ?>">
227  <div class="modal-dialog">
228  <div class="modal-content">
229  <div class="modal-header">
230  <h3 class="modal-title"><?php echo $this->tb['btn_rename_file']; ?></h3>
231  </div>
232  <div class="modal-body"><!-- Input fields get created by JS --></div>
233  <div class="modal-footer">
234  <div class="btn-group btn-group-justified">
235  <div class="btn-group"><button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> <?php echo $this->tb['btn_close']; ?></button></div>
236  <div class="btn-group"><button id="rename-file" type="button" class="btn btn-primary" data-loading-text="<?php echo $this->tb['btn_loading']; ?>"><span class="glyphicon glyphicon-ok"></span> <?php echo $this->tb['btn_rename_file']; ?></button></div>
237  </div>
238  </div>
239  </div>
240  </div>
241 </div>
242 
243 
244 <!-- Upload Modal -->
245 <div class="modal fade" id="automad-upload-modal" tabindex="-1" data-automad-url="<?php echo $url; ?>" data-automad-dropzone-text="<?php echo $this->tb['dropzone']; ?>" data-automad-browse-text="<?php echo $this->tb['btn_browse']; ?>">
246  <div class="modal-dialog">
247  <div class="modal-content">
248  <div class="modal-header">
249  <h3 class="modal-title"><?php echo $this->tb['btn_upload']; ?></h3>
250  </div>
251  <div id="automad-upload" class="modal-body"></div>
252  <div class="modal-footer">
253  <button type="button" class="btn btn-default" data-dismiss="modal" data-loading-text="<?php echo $this->tb['btn_loading']; ?>"><span class="glyphicon glyphicon-remove"></span> <?php echo $this->tb['btn_close']; ?></button>
254  </div>
255  </div>
256  </div>
257 </div>
258 
259 <?php
260 
261 
262 $output['html'] = ob_get_contents();
263 ob_end_clean();
264 
265 
266 echo json_encode($output);
267 
268 
269 ?>
static allowedFileTypes()
Definition: parse.php:61