Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         media
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 1476 2011-07-13 15:46:19Z 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
48
$forbidden_file_types  = preg_replace( '/\s*[,;\|#]\s*/','|',RENAME_FILES_ON_UPLOAD);
49
// Get home folder not to show
50
$home_folders = get_home_folders();
51

    
52
// Figure out what folder name the temp id is
53
if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.'/'.$directory)) {
54
	// Loop through the files and dirs an add to list
55
   while (false !== ($file = readdir($handle))) {
56
		$info = pathinfo($file);
57
		$ext = isset($info['extension']) ? $info['extension'] : '';
58
		if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
59
			if( !preg_match('/'.$forbidden_file_types.'$/i', $ext) ) {
60
				if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
61
					if(!isset($home_folders[$directory.'/'.$file])) {
62
						$DIR[] = $file;
63
					}
64
				} else {
65
					$FILE[] = $file;
66
				}
67
			}
68
		}
69
	}
70
	$temp_id = 0;
71
	if(isset($DIR)) {
72
		sort($DIR);
73
		foreach($DIR AS $name) {
74
			$temp_id++;
75
			if($file_id == $temp_id) {
76
				$rename_file = $name;
77
				$type = 'folder';
78
			}
79
		}
80
	}
81
	if(isset($FILE)) {
82
		sort($FILE);
83
		foreach($FILE AS $name) {
84
			$temp_id++;
85
			if($file_id == $temp_id) {
86
				$rename_file = $name;
87
				$type = 'file';
88
			}
89
		}
90
	}
91
}
92

    
93
$file_id = $admin->getIDKEY($file_id);
94

    
95
if(!isset($rename_file)) {
96
	$admin->print_error($MESSAGE['MEDIA']['FILE_NOT_FOUND'], $dirlink, false);
97
}
98

    
99
// Check if they entered a new name
100
if(media_filename($admin->get_post('name')) == "") {
101
	$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
102
} else {
103
	$old_name = $admin->get_post('old_name');
104
	$new_name =  media_filename($admin->get_post('name'));
105
}
106

    
107
// Check if they entered an extension
108
if($type == 'file') {
109
	if(media_filename($admin->get_post('extension')) == "") {
110
		$admin->print_error($MESSAGE['MEDIA']['BLANK_EXTENSION'], "rename.php?dir=$directory&id=$file_id", false);
111
	} else {
112
		$extension = media_filename($admin->get_post('extension'));
113
	}
114
} else {
115
	$extension = '';
116
}
117

    
118
// Join new name and extension
119
$name = $new_name.$extension;
120

    
121
$info = pathinfo(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name);
122
$ext = isset($info['extension']) ? $info['extension'] : '';
123
$dots = (substr($info['basename'], 0, 1) == '.') || (substr($info['basename'], -1, 1) == '.');
124

    
125
if( preg_match('/'.$forbidden_file_types.'$/i', $ext) || $dots == '.' ) {
126
	$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
127
}
128

    
129
// Check if the name contains ..
130
if(strstr($name, '..')) {
131
	$admin->print_error($MESSAGE['MEDIA']['NAME_DOT_DOT_SLASH'], "rename.php?dir=$directory&id=$file_id", false);
132
}
133

    
134
// Check if the name is index.php
135
if($name == 'index.php') {
136
	$admin->print_error($MESSAGE['MEDIA']['NAME_INDEX_PHP'], "rename.php?dir=$directory&id=$file_id", false);
137
}
138

    
139
// Check that the name still has a value
140
if($name == '') {
141
	$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
142
}
143

    
144
$info = pathinfo(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file);
145
$ext = isset($info['extension']) ? $info['extension'] : '';
146
$dots = (substr($info['basename'], 0, 1) == '.') || (substr($info['basename'], -1, 1) == '.');
147

    
148
if( preg_match('/'.$forbidden_file_types.'$/i', $ext) || $dots == '.' ) {
149
	$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
150
}
151

    
152
// Check if we should overwrite or not
153
if($admin->get_post('overwrite') != 'yes' AND file_exists(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name) == true) {
154
	if($type == 'folder') {
155
		$admin->print_error($MESSAGE['MEDIA']['DIR_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
156
	} else {
157
		$admin->print_error($MESSAGE['MEDIA']['FILE_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
158
	}
159
}
160

    
161
// Try and rename the file/folder
162
if(rename(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file, WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name)) {
163
	$usedFiles = array();
164
    // feature freeze
165
	// require_once(ADMIN_PATH.'/media/dse.php');
166

    
167
	$admin->print_success($MESSAGE['MEDIA']['RENAMED'], $dirlink);
168
} else {
169
	$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
170
}
(12-12/16)