This repository was archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_handler.php
More file actions
416 lines (347 loc) · 17 KB
/
action_handler.php
File metadata and controls
416 lines (347 loc) · 17 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
* Admin tool: Addon File Editor
*
* This tool allows you to "edit", "delete", "create", "upload" or "backup" files of installed
* Add-ons such as modules, templates and languages via LEPTON backend. This enables
* you to perform small modifications on installed Add-ons without downloading the files first.
*
*
* @author Christian Sommer (doc), Bianka Martinovic (BlackBird), Dietrich Roland Pehlke (aldus), LEPTON Project
* @copyright 2008-2012 Christian Sommer (doc), Bianka Martinovic (BlackBird), Dietrich Roland Pehlke (aldus)
* @copyright 2010-2015 LEPTON Project
* @license GNU General Public License
* @version see info.php
* @platform see info.php
*
*/
// include class.secure.php to protect this file and the whole CMS!
if (defined('LEPTON_PATH')) {
include(LEPTON_PATH.'/framework/class.secure.php');
} else {
$oneback = "../";
$root = $oneback;
$level = 1;
while (($level < 10) && (!file_exists($root.'/framework/class.secure.php'))) {
$root .= $oneback;
$level += 1;
}
if (file_exists($root.'/framework/class.secure.php')) {
include($root.'/framework/class.secure.php');
} else {
trigger_error(sprintf("[ <b>%s</b> ] Can't include class.secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
}
}
// end include class.secure.php
require_once('../../framework/class.admin.php');
// include module configuration and function file
require_once('config.inc.php');
require_once('functions.inc.php');
// load module language file
$lang = (dirname(__FILE__)) . '/languages/' . LANGUAGE . '.php';
require_once(!file_exists($lang) ? (dirname(__FILE__)) . '/languages/EN.php' : $lang );
// work out link to language specific help file
$help_file = 'help_' . (file_exists(dirname(__FILE__) . '/help/help_' . strtolower(LANGUAGE) . '.html') ? strtolower(LANGUAGE) : 'en') . '.html';
/**
* Ensure that only users with permissions to Admin-Tools section can access this file
*/
// check user permissions for admintools (redirect users with wrong permissions)
$admin = new admin('Admintools', 'admintools', false, false);
if ($admin->get_permission('admintools') == false) die(header('Location: ../../index.php'));
// check if the referer URL if available
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] :
(isset($HTTP_SERVER_VARS['HTTP_REFERER']) ? $HTTP_SERVER_VARS['HTTP_REFERER'] : '');
// if referer is set, check if script was invoked from "tool.php" or "action_handler.php"
if ($referer != '' && (!(strpos($referer, $url_admintools) !== false || strpos($referer, $url_action_handler) !== false)))
die(header('Location: ' . $url_admintools));
/**
* Make sanity check of user specified values
* $_GET['action'], $_GET['aid'] and $_GET['fid']
*/
// use modified admin handler to inject module backend.css, backend.js files into <head> section
$admin = myAdminHandler($module_folder, 'Admintools', 'admintools', true, false);
// make sanity check of user specified action handler, addon id and file id
$action = (isset($_GET['action']) && is_numeric($_GET['action'])) ? $_GET['action'] : '';
$aid = (isset($_GET['aid']) && isset($_SESSION['addon_id']) && ($_GET['aid'] == $_SESSION['addon_id'])) ? (int) $_GET['aid'] : '';
$fid = (isset($_GET['fid']) && isset($_SESSION['addon_file_infos'])
&& $_SESSION['addon_file_infos'] >= $_GET['fid']) ? (int) $_GET['fid'] : '';
// check if addon and file id is specified for action id: 1-3
if ($action < 4 && ($aid == '' || $fid == '')) $admin->print_error($LANG[3]['ERR_WRONG_PARAMETER'], $url_admintools);
/**
* Evaluate the action handler
*/
// include template class and set template directory
require_once(LEPTON_PATH . '/include/phplib/template.inc');
$tpl = new Template(dirname(__FILE__) . '/htt');
$tpl->set_unknowns('keep');
// create array with template files and language variables based on action handler
$tpl_files = array(
'1' => array('action_handler_edit_textfile.htt', $LANG[4]),
'2' => array('action_handler_rename_file_folder.htt', $LANG[5]),
'3' => array('action_handler_delete_file_folder.htt', $LANG[6]),
'4' => array('action_handler_create_file_folder.htt', $LANG[7]),
'5' => array('action_handler_upload_file.htt', $LANG[8])
);
// set template file depending on action handler
if ($action > 0 && $action < 6) $tpl->set_file('page', $tpl_files[$action][0]);
// remove the comment block
$tpl->set_block('page', 'comment_block', 'comment_replace');
$tpl->set_block('comment_replace', '');
// fetch placeholder values identical for all file handlers
$editor_info = getAddonInfos($module_folder);
$addon_info = getAddonInfos($aid);
$tpl_infos = array(
'TXT_HEADING_ADMINTOOLS'=> $HEADING['ADMINISTRATION_TOOLS'],
'TXT_BACK' => $TEXT['BACK'],
'TXT_HELP' => $LANG[1]['TXT_HELP'],
'URL_HELP_FILE' => $url_mod_path . '/help/' . $help_file,
'STATUS_MESSAGE' => '',
'CLASS_HIDDEN' => '',
'NAME_FILE_EDITOR' => $editor_info['name'],
'ADDON_TYPE' => $LANG[3]['TXT_' . strtoupper($addon_info['type'])],
'ADDON_NAME' => $addon_info['name'],
'URL_WB_ADMIN_TOOLS' => ADMIN_URL . '/admintools/index.php',
'URL_FILEMANAGER' => $url_admintools . '&aid=' . $aid,
);
// replace template placeholder with data from language files
foreach(array_merge($LANG[3], $tpl_files[$action][1], $tpl_infos) as $key => $value) {
$tpl->set_var($key, $value);
}
/**
* Evaluate the action handler
*/
switch ($action) {
case 1:
#####################################################################################
# edit text file
#####################################################################################
$actual_file = $_SESSION['addon_file_infos'][$fid]['path'];
// strip path up to ../modules/mod_directory/; for language files keep mod_directory (e.g. EN)
$strip_path = LEPTON_PATH . $path_sep . $addon_info['type'] . 's' .
(($addon_info['type'] != 'language') ? $path_sep . $addon_info['directory'] : '');
// fetch content of specified file (read from file or take over from textarea)
if ((isset($_POST['save_modified_textfile']) || isset($_POST['save_modified_textfile_back'])) && isset($_POST['code_area_text'])) {
// take content from save request
$file_content = stripslashes($_POST['code_area_text']);
} else {
// open file and save data in variable
$file_content = file_get_contents($actual_file);
}
$tpl->set_var(array(
'REGISTER_EDIT_AREA' => myRegisterEditArea($syntax = myGetEditAreaSyntax($actual_file)),
'ADDON_FILE' => str_replace($strip_path, '', $actual_file),
'FILE_CONTENT' => htmlspecialchars($file_content),
'URL_FORM_SUBMIT' => $url_action_handler . '?aid=' . $aid . '&fid=' . $fid . '&action=1',
'URL_FORM_CANCEL' => $url_admintools . '&aid=' . $aid
)
);
// action save modified text file
if ((isset($_POST['save_modified_textfile']) || isset($_POST['save_modified_textfile_back'])) && isset($_POST['code_area_text'])) {
// save changes to text file
$status = false;
if (is_writeable($actual_file) && $handle = fopen($actual_file, 'wb')) {
$status = fwrite($handle, $file_content);
}
// try FTP file upload if fwrite method failed (permissions)
if (!$status) {
$ftp_remote_file = str_replace(array(LEPTON_PATH . $path_sep, $path_sep), array('', '/'), $actual_file);
$ftp = ftpLogin();
$status = ftpWriteStringToFile($ftp, $file_content, $ftp_remote_file);
}
$status_message = ($status) ? $LANG[4]['TXT_SAVE_SUCCESS'] : $LANG[4]['TXT_SAVE_ERROR'];
$back_link = $url_admintools . '&aid=' . $aid . '&fid=' . $fid;
$tpl->set_var(array(
'STATUS_MESSAGE' => writeStatusMessage($message = $status_message, $back_url = $back_link, $sucess = $status,
$auto_redirect = ($status && isset($_POST['save_modified_textfile_back'])),
$redirect_timer = isset($_POST['save_modified_textfile_back']) ? 0 : 1500),
'CLASS_HIDDEN' => ''
)
);
}
$tpl->pparse('output', 'page');
break;
case 2:
#####################################################################################
# rename file or folder
#####################################################################################
$actual_file = $_SESSION['addon_file_infos'][$fid]['path'];
$file_extension = getFileExtension($actual_file);
$strip_path = LEPTON_PATH . $path_sep . $addon_info['type'] . 's' . $path_sep . $addon_info['directory'];
$tpl->set_var(array(
'ADDON_FILE' => str_replace($strip_path, '', $actual_file),
'FILE_EXT' => $file_extension,
'OLD_FILE_NAME' => str_replace('.' . $file_extension, '', basename($actual_file)),
'CLASS_HIDE' => ($_SESSION['addon_file_infos'][$fid]['type'] == 'folder') ? 'hidden' : '',
'URL_FORM_SUBMIT' => $url_action_handler . '?aid=' . $aid . '&fid=' . $fid . '&action=2',
'URL_FORM_CANCEL' => $url_admintools . '&aid=' . $aid
)
);
// action rename file / folder
if (isset($_POST['rename_file_folder']) && isset($_POST['new_file'])) {
$new_file_name = strip_tags($_POST['new_file']);
// rename the file or folder
$status = renameFileOrFolder($actual_file, $new_file_name);
// try to rename file/folder via FTP if PHP method failed (permissions)
if (!$status) {
// build old and new file name as required for FTP
$ftp_old = str_replace(array(LEPTON_PATH . $path_sep, $path_sep), array('', '/'), $actual_file);
$ftp_new = str_replace(basename($actual_file), $new_file_name, $ftp_old);
$ftp_new .= (($file_extension == '') ? '' : '.' . $file_extension);
$ftp = ftpLogin();
$status = ftpRenameFile($ftp, $ftp_old, $ftp_new);
}
$status_message = ($status) ? $LANG[5]['TXT_RENAME_SUCCESS'] : $LANG[5]['TXT_RENAME_ERROR'];
$back_link = $url_admintools . '&aid=' . $aid . '&reload';
$tpl->set_var(array(
'STATUS_MESSAGE' => writeStatusMessage($status_message, $back_link, $status),
'CLASS_HIDDEN' => ($status) ? 'hidden' : ''
)
);
}
$tpl->pparse('output', 'page');
break;
case 3:
#####################################################################################
# delete file or folder
#####################################################################################
$actual_file = $_SESSION['addon_file_infos'][$fid]['path'];
$strip_path = LEPTON_PATH . $path_sep . $addon_info['type'] . 's' . $path_sep . $addon_info['directory'];
$tpl->set_var(array(
'ADDON_FILE' => str_replace($strip_path, '', $actual_file),
'FILE_FOLDER_NAME' => basename($actual_file),
'TXT_ACTUAL_FILE' => ($_SESSION['addon_file_infos'][$fid]['type'] == 'folder')
? $LANG[6]['TXT_ACTUAL_FOLDER'] : $LANG[3]['TXT_ACTUAL_FILE'],
'URL_FORM_SUBMIT' => $url_action_handler . '?aid=' . $aid . '&fid=' . $fid . '&action=3&reload',
'URL_FORM_CANCEL' => $url_admintools . '&aid=' . $aid . '&fid=' . $fid,
'CLASS_HIDDEN' => '',
)
);
// action delete file / folder
if (isset($_POST['delete_file_folder'])) {
$status = removeFileOrFolder($actual_file);
// try to delete file/folder via FTP if PHP method failed (permissions)
if (!$status) {
$ftp_file = str_replace(array(LEPTON_PATH . $path_sep, $path_sep), array('', '/'), $actual_file);
$ftp = ftpLogin();
$status = is_dir($actual_file) ? ftpDeleteFolder($ftp, $ftp_file) : ftpDeleteFile($ftp, $ftp_file);
}
$status_message = ($status) ? $LANG[6]['TXT_DELETE_SUCCESS'] : $LANG[6]['TXT_DELETE_ERROR'];
$back_link = $url_admintools . '&aid=' . $aid . '&fid=' . $fid . '&reload';
$tpl->set_var(array(
'STATUS_MESSAGE' => writeStatusMessage($status_message, $back_link, $status),
'CLASS_HIDDEN' => 'hidden',
)
);
}
$tpl->pparse('output', 'page');
break;
case 4:
#####################################################################################
# create new file or folder
#####################################################################################
$strip_path = LEPTON_PATH . $path_sep . $addon_info['type'] . 's' . $path_sep;
$tpl->set_var(array(
'SEL_ENTRIES_FILE_EXTENSIONS' => createSelectEntries($text_extensions),
'SEL_ENTRIES_TARGET_FOLDER' => createTargetFolderSelectEntries($_SESSION['addon_folders'], $strip_path),
'URL_WB_ADMIN_TOOLS' => ADMIN_URL . '/admintools/index.php',
'URL_FORM_SUBMIT' => $url_action_handler . '?aid=' . $aid . '&action=4',
'URL_FORM_CANCEL' => $url_admintools . '&aid=' . $aid
)
);
// action create file / folder
if (isset($_POST['create_file_folder']) && isset($_POST['file_folder'])
&& isset($_POST['file_name']) && isset($_POST['target_folder'])) {
// extract specified file type (file/folder) and file/folder name to create
$file_type = ($_POST['file_folder'] == 'folder') ? 'folder' : 'file';
$file_name = strip_tags(stripslashes($_POST['file_name']));
if ($file_type == 'file') {
// extract file extension
$extension_id = isset($_POST['file_extensions']) ? (int) $_POST['file_extensions'] : 0;
$extension = (count($text_extensions) >= $extension_id) ? $text_extensions[$extension_id] : 0;
$file_name = (substr($file_name, -1, 1) == '.') ? ($file_name . $extension) : ($file_name . '.' . $extension);
}
$folder_id = (int) $_POST['target_folder'];
$target_folder = (count($_SESSION['addon_folders']) >= $folder_id) ? $_SESSION['addon_folders'][$folder_id] : '';
$back_link = $url_admintools . '&aid=' . $aid . '&reload';
$status = createFileOrFolder($file_type, $target_folder, $file_name);
// try to create file/folder via FTP if PHP method failed (permissions)
if (!$status) {
$ftp_file = $target_folder . $path_sep . $file_name;
$ftp_file = str_replace(array(LEPTON_PATH . $path_sep, $path_sep), array('', '/'), $ftp_file);
$ftp = ftpLogin();
$status = ($file_type == 'file') ? ftpWriteStringToFile($ftp, ' ', $ftp_file) : ftpCreateFolder($ftp, $ftp_file);
}
$status_message = ($status) ? $LANG[7]['TXT_CREATE_SUCCESS'] : $LANG[7]['TXT_CREATE_ERROR'];
$tpl->set_var(array(
'STATUS_MESSAGE' => writeStatusMessage($status_message, $back_link, $status),
'CLASS_HIDDEN' => ($status) ? 'hidden' : ''
)
);
}
$tpl->pparse('output', 'page');
break;
case 5:
#####################################################################################
# upload file
#####################################################################################
$strip_path = LEPTON_PATH . $path_sep . $addon_info['type'] . 's' . $path_sep;
// set template file
$tpl->set_var(array(
'SEL_ENTRIES_TARGET_FOLDER' => createTargetFolderSelectEntries($_SESSION['addon_folders'], $strip_path),
'MAX_FILE_SIZE' => $max_upload_size * 1024 * 1024,
'URL_WB_ADMIN_TOOLS' => ADMIN_URL . '/admintools/index.php',
'URL_FORM_SUBMIT' => $url_action_handler . '?aid=' . $aid . '&action=5',
'URL_FORM_CANCEL' => $url_admintools . '&aid=' . $aid
)
);
// action upload file
if (isset($_POST['upload_file']) && isset($_POST['target_folder']) && isset($_FILES['file_upload'])) {
// obtain the target folder
$folder_id = (int) $_POST['target_folder'];
$target_folder = (count($_SESSION['addon_folders']) >= $folder_id) ? $_SESSION['addon_folders'][$folder_id] : '';
$status = false;
// check if file upload was successfull
if ($_FILES['file_upload']['error'] == UPLOAD_ERR_OK && $target_folder != '') {
// extract file information
$file_infos = pathinfo($_FILES['file_upload']['name']);
$file_infos['size'] = $_FILES['file_upload']['size'] / (1024 * 1024);
// only accept file if file size is not exceeded
if ($file_infos['size'] <= $max_upload_size) {
// move file to specified target folder
$new_file = $target_folder . $path_sep . $_FILES['file_upload']['name'];
$status = @move_uploaded_file($_FILES['file_upload']['tmp_name'], $new_file);
// move file using FTP if PHP function failed (permissions)
if (!$status) {
// move uploaded file to temporary folder
$temp_file = LEPTON_PATH . $path_sep . 'temp' . $path_sep . md5(uniqid(rand(), true));
$temp_file .= '_' . $_FILES['file_upload']['name'];
$status = @move_uploaded_file($_FILES['file_upload']['tmp_name'], $temp_file);
if ($status) {
// file moved to temporary folder, use FTP to upload into target folder
$remote_file = str_replace(array(LEPTON_PATH . $path_sep, $path_sep), array('', '/'), $new_file);
$mode = in_array(getFileExtension($temp_file), $text_extensions) ? 'ASCII' : 'BIN';
$ftp = ftpLogin();
$status = ftpMoveFile($ftp, $temp_file, $remote_file, $mode);
unlink($temp_file);
}
}
}
}
// output a status message
$back_link = $url_admintools . '&aid=' . $aid . '&reload';
$status_message = ($status) ? $LANG[8]['TXT_UPLOAD_SUCCESS'] : $LANG[8]['TXT_UPLOAD_ERROR'];
$tpl->set_var(array(
'STATUS_MESSAGE' => writeStatusMessage($status_message, $back_link, $status),
'CLASS_HIDDEN' => ($status) ? 'hidden' : ''
)
);
}
$tpl->pparse('output', 'page');
break;
default:
$admin->print_error($LANG[3]['ERR_WRONG_PARAMETER'], $url_admintools);
break;
}
// print admin template footer
$admin->print_footer();
?>