Project

General

Profile

1
<?php
2

    
3
// $Id: rename2.php 944 2009-02-22 09:39:58Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, Ryan Djurovich
9

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

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

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

    
24
*/
25

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

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

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

    
43

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

    
55
// Get the temp id
56
if(!is_numeric($admin->get_post('id'))) {
57
	header("Location: browse.php?dir=$directory");
58
	exit(0);
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
		sort($DIR);
83
		foreach($DIR AS $name) {
84
			$temp_id++;
85
			if($file_id == $temp_id) {
86
				$rename_file = $name;
87
				$type = 'folder';
88
			}
89
		}
90
	}
91
	if(isset($FILE)) {
92
		sort($FILE);
93
		foreach($FILE AS $name) {
94
			$temp_id++;
95
			if($file_id == $temp_id) {
96
				$rename_file = $name;
97
				$type = 'file';
98
			}
99
		}
100
	}
101
}
102

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

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

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

    
126
// Join new name and extension
127
$name = $new_name.$extension;
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
// Check for potentially malicious files and append 'txt' to their name
145
foreach($file_extensions as $file_ext) {
146
	$file_ext_len=strlen($file_ext);
147
	if (substr($name,-$file_ext_len)==$file_ext) {
148
		$name.='.txt';
149
	}
150
}		
151

    
152

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

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