1
|
<?php
|
2
|
/**
|
3
|
*
|
4
|
* @category admin
|
5
|
* @package admintools
|
6
|
* @author WebsiteBaker Project
|
7
|
* @copyright 2004-2009, Ryan Djurovich
|
8
|
* @copyright 2009-2011, Website Baker Org. e.V.
|
9
|
* @link http://www.websitebaker2.org/
|
10
|
* @license http://www.gnu.org/licenses/gpl.html
|
11
|
* @platform WebsiteBaker 2.8.x
|
12
|
* @requirements PHP 5.2.2 and higher
|
13
|
* @version $Id: rename2.php 1475 2011-07-12 23:07:10Z Luisehahne $
|
14
|
* @filesource $HeadURL: $
|
15
|
* @lastmodified $Date: $
|
16
|
*
|
17
|
*/
|
18
|
|
19
|
// Create admin object
|
20
|
require('../../config.php');
|
21
|
require_once(WB_PATH.'/framework/class.admin.php');
|
22
|
$admin = new admin('Media', 'media_rename', false);
|
23
|
|
24
|
// Include the WB functions file
|
25
|
require_once(WB_PATH.'/framework/functions.php');
|
26
|
|
27
|
// Get the current dir
|
28
|
$requestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
|
29
|
$directory = (isset(${$requestMethod}['dir'])) ? ${$requestMethod}['dir'] : '';
|
30
|
$directory = ($directory == '/') ? '' : $directory;
|
31
|
|
32
|
$dirlink = 'browse.php?dir='.$directory;
|
33
|
$rootlink = 'browse.php?dir=';
|
34
|
// $file_id = intval($admin->get_post('id'));
|
35
|
|
36
|
// first Check to see if it contains ..
|
37
|
if (!check_media_path($directory)) {
|
38
|
$admin->print_error($MESSAGE['MEDIA']['DIR_DOT_DOT_SLASH'],$rootlink, false);
|
39
|
}
|
40
|
|
41
|
// Get the temp id
|
42
|
$file_id = intval($admin->checkIDKEY('id', false, $_SERVER['REQUEST_METHOD']));
|
43
|
if (!$file_id) {
|
44
|
$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'],$dirlink, false);
|
45
|
}
|
46
|
|
47
|
// Check for potentially malicious files and append 'txt' to their name
|
48
|
$rename_file_types = str_replace(',','|',RENAME_FILES_ON_UPLOAD);
|
49
|
// hardcodet forbidden filetypes
|
50
|
$forbidden_file_types = 'phtml|php5|php4|php|cgi|pl|exe|com|bat|src|'.$rename_file_types;
|
51
|
// Get home folder not to show
|
52
|
$home_folders = get_home_folders();
|
53
|
|
54
|
// Figure out what folder name the temp id is
|
55
|
if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.'/'.$directory)) {
|
56
|
// Loop through the files and dirs an add to list
|
57
|
while (false !== ($file = readdir($handle))) {
|
58
|
$info = pathinfo($file);
|
59
|
$ext = isset($info['extension']) ? $info['extension'] : '';
|
60
|
if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
|
61
|
if( !preg_match('/'.$forbidden_file_types.'$/i', $ext) ) {
|
62
|
if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
|
63
|
if(!isset($home_folders[$directory.'/'.$file])) {
|
64
|
$DIR[] = $file;
|
65
|
}
|
66
|
} else {
|
67
|
$FILE[] = $file;
|
68
|
}
|
69
|
}
|
70
|
}
|
71
|
}
|
72
|
$temp_id = 0;
|
73
|
if(isset($DIR)) {
|
74
|
sort($DIR);
|
75
|
foreach($DIR AS $name) {
|
76
|
$temp_id++;
|
77
|
if($file_id == $temp_id) {
|
78
|
$rename_file = $name;
|
79
|
$type = 'folder';
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
if(isset($FILE)) {
|
84
|
sort($FILE);
|
85
|
foreach($FILE AS $name) {
|
86
|
$temp_id++;
|
87
|
if($file_id == $temp_id) {
|
88
|
$rename_file = $name;
|
89
|
$type = 'file';
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
|
95
|
$file_id = $admin->getIDKEY($file_id);
|
96
|
|
97
|
if(!isset($rename_file)) {
|
98
|
$admin->print_error($MESSAGE['MEDIA']['FILE_NOT_FOUND'], $dirlink, false);
|
99
|
}
|
100
|
|
101
|
// Check if they entered a new name
|
102
|
if(media_filename($admin->get_post('name')) == "") {
|
103
|
$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
|
104
|
} else {
|
105
|
$old_name = $admin->get_post('old_name');
|
106
|
$new_name = media_filename($admin->get_post('name'));
|
107
|
}
|
108
|
|
109
|
// Check if they entered an extension
|
110
|
if($type == 'file') {
|
111
|
if(media_filename($admin->get_post('extension')) == "") {
|
112
|
$admin->print_error($MESSAGE['MEDIA']['BLANK_EXTENSION'], "rename.php?dir=$directory&id=$file_id", false);
|
113
|
} else {
|
114
|
$extension = media_filename($admin->get_post('extension'));
|
115
|
}
|
116
|
} else {
|
117
|
$extension = '';
|
118
|
}
|
119
|
|
120
|
// Join new name and extension
|
121
|
$name = $new_name.$extension;
|
122
|
|
123
|
$info = pathinfo(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name);
|
124
|
$ext = isset($info['extension']) ? $info['extension'] : '';
|
125
|
$dots = (substr($info['basename'], 0, 1) == '.') || (substr($info['basename'], -1, 1) == '.');
|
126
|
|
127
|
if( preg_match('/'.$forbidden_file_types.'$/i', $ext) || $dots == '.' ) {
|
128
|
$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
|
129
|
}
|
130
|
|
131
|
// Check if the name contains ..
|
132
|
if(strstr($name, '..')) {
|
133
|
$admin->print_error($MESSAGE['MEDIA']['NAME_DOT_DOT_SLASH'], "rename.php?dir=$directory&id=$file_id", false);
|
134
|
}
|
135
|
|
136
|
// Check if the name is index.php
|
137
|
if($name == 'index.php') {
|
138
|
$admin->print_error($MESSAGE['MEDIA']['NAME_INDEX_PHP'], "rename.php?dir=$directory&id=$file_id", false);
|
139
|
}
|
140
|
|
141
|
// Check that the name still has a value
|
142
|
if($name == '') {
|
143
|
$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
|
144
|
}
|
145
|
|
146
|
$info = pathinfo(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file);
|
147
|
$ext = isset($info['extension']) ? $info['extension'] : '';
|
148
|
$dots = (substr($info['basename'], 0, 1) == '.') || (substr($info['basename'], -1, 1) == '.');
|
149
|
|
150
|
if( preg_match('/'.$forbidden_file_types.'$/i', $ext) || $dots == '.' ) {
|
151
|
$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
|
152
|
}
|
153
|
|
154
|
// Check if we should overwrite or not
|
155
|
if($admin->get_post('overwrite') != 'yes' AND file_exists(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name) == true) {
|
156
|
if($type == 'folder') {
|
157
|
$admin->print_error($MESSAGE['MEDIA']['DIR_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
|
158
|
} else {
|
159
|
$admin->print_error($MESSAGE['MEDIA']['FILE_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
// Try and rename the file/folder
|
164
|
if(rename(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file, WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name)) {
|
165
|
$usedFiles = array();
|
166
|
// feature freeze
|
167
|
// require_once(ADMIN_PATH.'/media/dse.php');
|
168
|
|
169
|
$admin->print_success($MESSAGE['MEDIA']['RENAMED'], $dirlink);
|
170
|
} else {
|
171
|
$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
|
172
|
}
|