Project

General

Profile

1
<?php
2

    
3
/*
4

    
5
 Website Baker Project <http://www.websitebaker.org/>
6
 Copyright (C) 2004-2005, Ryan Djurovich
7

    
8
 Website Baker is free software; you can redistribute it and/or modify
9
 it under the terms of the GNU General Public License as published by
10
 the Free Software Foundation; either version 2 of the License, or
11
 (at your option) any later version.
12

    
13
 Website Baker is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 GNU General Public License for more details.
17

    
18
 You should have received a copy of the GNU General Public License
19
 along with Website Baker; if not, write to the Free Software
20
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21

    
22
*/
23

    
24
// Create admin object
25
require('../../config.php');
26
require_once(WB_PATH.'/framework/class.admin.php');
27
$admin = new admin('Media', 'media_rename', false);
28

    
29
// Include the WB functions file
30
require_once(WB_PATH.'/framework/functions.php');
31

    
32
// Include the basic header file
33
require(ADMIN_PATH.'/media/basic_header.html');
34

    
35
// Get list of file types to which we're supposed to append 'txt'
36
$get_result=$database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name='rename_files_on_upload' LIMIT 1");
37
$file_extension_string='';
38
if ($get_result->numRows()>0) {
39
	$fetch_result=$get_result->fetchRow();
40
	$file_extension_string=$fetch_result['value'];
41
}
42
$file_extensions=explode(",",$file_extension_string);
43

    
44

    
45
// Get the current dir
46
$directory = $admin->get_post('dir');
47
if($directory == '/') {
48
	$directory = '';
49
}
50
// Check to see if it contains ../
51
if(strstr($directory, '../')) {
52
	$admin->print_header();
53
	$admin->print_error($MESSAGE['MEDIA']['DIR_DOT_DOT_SLASH']);
54
}
55

    
56
// Get the temp id
57
if(!is_numeric($admin->get_post('id'))) {
58
	header("Location: browse.php?dir=$directory");
59
} else {
60
	$file_id = $admin->get_post('id');
61
}
62

    
63
// Get home folder not to show
64
$home_folders = get_home_folders();
65

    
66
// Figure out what folder name the temp id is
67
if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.'/'.$directory)) {
68
	// Loop through the files and dirs an add to list
69
   while (false !== ($file = readdir($handle))) {
70
		if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
71
			if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
72
				if(!isset($home_folders[$directory.'/'.$file])) {
73
					$DIR[] = $file;
74
				}
75
			} else {
76
				$FILE[] = $file;
77
			}
78
		}
79
	}
80
	$temp_id = 0;
81
	if(isset($DIR)) {
82
		foreach($DIR AS $name) {
83
			$temp_id++;
84
			if($file_id == $temp_id) {
85
				$rename_file = $name;
86
				$type = 'folder';
87
			}
88
		}
89
	}
90
	if(isset($FILE)) {
91
		foreach($FILE AS $name) {
92
			$temp_id++;
93
			if($file_id == $temp_id) {
94
				$rename_file = $name;
95
				$type = 'file';
96
			}
97
		}
98
	}
99
}
100

    
101
if(!isset($rename_file)) {
102
	$admin->print_error($MESSAGE['MEDIA']['FILE_NOT_FOUND'], "browse.php?dir=$directory", false);
103
}
104

    
105
// Check if they entered a new name
106
if(media_filename($admin->get_post('name')) == "") {
107
	$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
108
} else {
109
	$old_name = $admin->get_post('old_name');
110
	$new_name =  media_filename($admin->get_post('name'));
111
}
112

    
113
// Check if they entered an extension
114
if($type == 'file') {
115
	if(media_filename($admin->get_post('extension')) == "") {
116
		$admin->print_error($MESSAGE['MEDIA']['BLANK_EXTENSION'], "rename.php?dir=$directory&id=$file_id", false);
117
	} else {
118
		$extension = media_filename($admin->get_post('extension'));
119
	}
120
} else {
121
	$extension = '';
122
}
123

    
124
// Join new name and extension
125
$name = $new_name.$extension;
126

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

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

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

    
142
// Check for potentially malicious files and append 'txt' to their name
143
foreach($file_extensions as $file_ext) {
144
	$file_ext_len=strlen($file_ext);
145
	if (substr($name,-$file_ext_len)==$file_ext) {
146
		$name.='.txt';
147
	}
148
}		
149

    
150

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

    
160
// Try and rename the file/folder
161
if(rename(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file, WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name)) {
162
	$admin->print_success($MESSAGE['MEDIA']['RENAMED'], "browse.php?dir=$directory");
163
} else {
164
	$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
165
}
166
?>
(9-9/11)