Project

General

Profile

1 4 ryan
<?php
2
3
// $Id: rename2.php,v 1.8 2005/04/07 07:43:36 rdjurovich Exp $
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, 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
// Include the basic header file
35
require(ADMIN_PATH.'/media/basic_header.html');
36
37
// Get the current dir
38
$directory = $admin->get_post('dir');
39
if($directory == '/') {
40
	$directory = '';
41
}
42
// Check to see if it contains ../
43
if(strstr($directory, '../')) {
44
	$admin->print_header();
45
	$admin->print_error($MESSAGE['MEDIA']['DIR_DOT_DOT_SLASH']);
46
}
47
48
// Get the temp id
49
if(!is_numeric($admin->get_post('id'))) {
50
	header("Location: browse.php?dir=$directory");
51
} else {
52
	$file_id = $admin->get_post('id');
53
}
54
55
// Get home folder not to show
56
$home_folders = get_home_folders();
57
58
// Figure out what folder name the temp id is
59
if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.'/'.$directory)) {
60
	// Loop through the files and dirs an add to list
61
   while (false !== ($file = readdir($handle))) {
62
		if(substr($file, 0, 1) != '.' AND $file != 'CVS' AND $file != 'index.php') {
63
			if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
64
				if(!isset($home_folders[$directory.'/'.$file])) {
65
					$DIR[] = $file;
66
				}
67
			} else {
68
				$FILE[] = $file;
69
			}
70
		}
71
	}
72
	$temp_id = 0;
73
	if(isset($DIR)) {
74
		foreach($DIR AS $name) {
75
			$temp_id++;
76
			if($file_id == $temp_id) {
77
				$rename_file = $name;
78
				$type = 'folder';
79
			}
80
		}
81
	}
82
	if(isset($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
if(!isset($rename_file)) {
94
	$admin->print_error($MESSAGE['MEDIA']['FILE_NOT_FOUND'], "browse.php?dir=$directory", false);
95
}
96
97
// Check if they entered a new name
98
if(media_filename($admin->get_post('name')) == "") {
99
	$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
100
} else {
101
	$old_name = $admin->get_post('old_name');
102
	$new_name =  media_filename($admin->get_post('name'));
103
}
104
105
// Check if they entered an extension
106
if($type == 'file') {
107
	if(media_filename($admin->get_post('extension')) == "") {
108
		$admin->print_error($MESSAGE['MEDIA']['BLANK_EXTENSION'], "rename.php?dir=$directory&id=$file_id", false);
109
	} else {
110
		$extension = media_filename($admin->get_post('extension'));
111
	}
112
} else {
113
	$extension = '';
114
}
115
116
// Join new name and extension
117
$name = $new_name.$extension;
118
119
// Check if the name contains ..
120
if(strstr($name, '..')) {
121
	$admin->print_error($MESSAGE['MEDIA']['NAME_DOT_DOT_SLASH'], "rename.php?dir=$directory&id=$file_id", false);
122
}
123
124
// Check if the name is index.php
125
if($name == 'index.php') {
126
	$admin->print_error($MESSAGE['MEDIA']['NAME_INDEX_PHP'], "rename.php?dir=$directory&id=$file_id", false);
127
}
128
129
// Check that the name still has a value
130
if($name == '') {
131
	$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
132
}
133
134
// Check if we should overwrite or not
135
if($admin->get_post('overwrite') != 'yes' AND file_exists(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name) == true) {
136
	if($type == 'folder') {
137
		$admin->print_error($MESSAGE['MEDIA']['DIR_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
138
	} else {
139
		$admin->print_error($MESSAGE['MEDIA']['FILE_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
140
	}
141
}
142
143
// Try and rename the file/folder
144
if(rename(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file, WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name)) {
145
	$admin->print_success($MESSAGE['MEDIA']['RENAMED'], "browse.php?dir=$directory");
146
} else {
147
	$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
148
}
149
150
?>