| 1 | 238 | stefan | <?php
 | 
      
        | 2 | 1400 | FrankH | /**
 | 
      
        | 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$
 | 
      
        | 14 |  |  |  * @filesource		$HeadURL:  $
 | 
      
        | 15 |  |  |  * @lastmodified    $Date:  $
 | 
      
        | 16 |  |  |  *
 | 
      
        | 17 |  |  |  */
 | 
      
        | 18 | 238 | stefan | 
 | 
      
        | 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 list of file types to which we're supposed to append 'txt'
 | 
      
        | 28 | 1400 | FrankH | $get_result = $database->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name='rename_files_on_upload' LIMIT 1");
 | 
      
        | 29 |  |  | $file_extension_string = '';
 | 
      
        | 30 | 238 | stefan | if ($get_result->numRows()>0) {
 | 
      
        | 31 | 1400 | FrankH | 	$fetch_result = $get_result->fetchRow();
 | 
      
        | 32 |  |  | 	$file_extension_string = $fetch_result['value'];
 | 
      
        | 33 | 238 | stefan | }
 | 
      
        | 34 |  |  | $file_extensions=explode(",",$file_extension_string);
 | 
      
        | 35 |  |  | 
 | 
      
        | 36 |  |  | // Get the current dir
 | 
      
        | 37 | 1457 | Luisehahne | // $directory = $admin->get_post('dir');
 | 
      
        | 38 |  |  | 
 | 
      
        | 39 |  |  | // Target location
 | 
      
        | 40 |  |  | $requestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
 | 
      
        | 41 |  |  | $directory = (isset(${$requestMethod}['dir'])) ? ${$requestMethod}['dir'] : '';
 | 
      
        | 42 | 238 | stefan | if($directory == '/') {
 | 
      
        | 43 |  |  | 	$directory = '';
 | 
      
        | 44 |  |  | }
 | 
      
        | 45 | 1400 | FrankH | 
 | 
      
        | 46 |  |  | // Check to see if it contains ..
 | 
      
        | 47 |  |  | if (!check_media_path($directory)) {
 | 
      
        | 48 | 238 | stefan | 	$admin->print_header();
 | 
      
        | 49 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['DIR_DOT_DOT_SLASH']);
 | 
      
        | 50 |  |  | }
 | 
      
        | 51 |  |  | 
 | 
      
        | 52 |  |  | // Get the temp id
 | 
      
        | 53 | 1400 | FrankH | $file_id = $admin->checkIDKEY('id', false, 'POST');
 | 
      
        | 54 |  |  | if (!$file_id) {
 | 
      
        | 55 | 1425 | Luisehahne | 	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS']);
 | 
      
        | 56 | 238 | stefan | }
 | 
      
        | 57 |  |  | 
 | 
      
        | 58 |  |  | // Get home folder not to show
 | 
      
        | 59 |  |  | $home_folders = get_home_folders();
 | 
      
        | 60 |  |  | 
 | 
      
        | 61 |  |  | // Figure out what folder name the temp id is
 | 
      
        | 62 |  |  | if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.'/'.$directory)) {
 | 
      
        | 63 |  |  | 	// Loop through the files and dirs an add to list
 | 
      
        | 64 |  |  |    while (false !== ($file = readdir($handle))) {
 | 
      
        | 65 |  |  | 		if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
 | 
      
        | 66 |  |  | 			if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
 | 
      
        | 67 |  |  | 				if(!isset($home_folders[$directory.'/'.$file])) {
 | 
      
        | 68 |  |  | 					$DIR[] = $file;
 | 
      
        | 69 |  |  | 				}
 | 
      
        | 70 |  |  | 			} else {
 | 
      
        | 71 |  |  | 				$FILE[] = $file;
 | 
      
        | 72 |  |  | 			}
 | 
      
        | 73 |  |  | 		}
 | 
      
        | 74 |  |  | 	}
 | 
      
        | 75 |  |  | 	$temp_id = 0;
 | 
      
        | 76 |  |  | 	if(isset($DIR)) {
 | 
      
        | 77 | 384 | Ruebenwurz | 		sort($DIR);
 | 
      
        | 78 | 238 | stefan | 		foreach($DIR AS $name) {
 | 
      
        | 79 |  |  | 			$temp_id++;
 | 
      
        | 80 |  |  | 			if($file_id == $temp_id) {
 | 
      
        | 81 |  |  | 				$rename_file = $name;
 | 
      
        | 82 |  |  | 				$type = 'folder';
 | 
      
        | 83 |  |  | 			}
 | 
      
        | 84 |  |  | 		}
 | 
      
        | 85 |  |  | 	}
 | 
      
        | 86 |  |  | 	if(isset($FILE)) {
 | 
      
        | 87 | 384 | Ruebenwurz | 		sort($FILE);
 | 
      
        | 88 | 238 | stefan | 		foreach($FILE AS $name) {
 | 
      
        | 89 |  |  | 			$temp_id++;
 | 
      
        | 90 |  |  | 			if($file_id == $temp_id) {
 | 
      
        | 91 |  |  | 				$rename_file = $name;
 | 
      
        | 92 |  |  | 				$type = 'file';
 | 
      
        | 93 |  |  | 			}
 | 
      
        | 94 |  |  | 		}
 | 
      
        | 95 |  |  | 	}
 | 
      
        | 96 |  |  | }
 | 
      
        | 97 | 1457 | Luisehahne | $file_id = $admin->getIDKEY($file_id);
 | 
      
        | 98 | 238 | stefan | if(!isset($rename_file)) {
 | 
      
        | 99 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['FILE_NOT_FOUND'], "browse.php?dir=$directory", false);
 | 
      
        | 100 |  |  | }
 | 
      
        | 101 |  |  | 
 | 
      
        | 102 |  |  | // Check if they entered a new name
 | 
      
        | 103 |  |  | if(media_filename($admin->get_post('name')) == "") {
 | 
      
        | 104 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 105 |  |  | } else {
 | 
      
        | 106 |  |  | 	$old_name = $admin->get_post('old_name');
 | 
      
        | 107 |  |  | 	$new_name =  media_filename($admin->get_post('name'));
 | 
      
        | 108 |  |  | }
 | 
      
        | 109 |  |  | 
 | 
      
        | 110 |  |  | // Check if they entered an extension
 | 
      
        | 111 |  |  | if($type == 'file') {
 | 
      
        | 112 |  |  | 	if(media_filename($admin->get_post('extension')) == "") {
 | 
      
        | 113 |  |  | 		$admin->print_error($MESSAGE['MEDIA']['BLANK_EXTENSION'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 114 |  |  | 	} else {
 | 
      
        | 115 |  |  | 		$extension = media_filename($admin->get_post('extension'));
 | 
      
        | 116 |  |  | 	}
 | 
      
        | 117 |  |  | } else {
 | 
      
        | 118 |  |  | 	$extension = '';
 | 
      
        | 119 |  |  | }
 | 
      
        | 120 |  |  | 
 | 
      
        | 121 |  |  | // Join new name and extension
 | 
      
        | 122 |  |  | $name = $new_name.$extension;
 | 
      
        | 123 |  |  | 
 | 
      
        | 124 |  |  | // Check if the name contains ..
 | 
      
        | 125 |  |  | if(strstr($name, '..')) {
 | 
      
        | 126 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['NAME_DOT_DOT_SLASH'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 127 |  |  | }
 | 
      
        | 128 |  |  | 
 | 
      
        | 129 |  |  | // Check if the name is index.php
 | 
      
        | 130 |  |  | if($name == 'index.php') {
 | 
      
        | 131 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['NAME_INDEX_PHP'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 132 |  |  | }
 | 
      
        | 133 |  |  | 
 | 
      
        | 134 |  |  | // Check that the name still has a value
 | 
      
        | 135 |  |  | if($name == '') {
 | 
      
        | 136 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['BLANK_NAME'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 137 |  |  | }
 | 
      
        | 138 |  |  | 
 | 
      
        | 139 |  |  | // Check for potentially malicious files and append 'txt' to their name
 | 
      
        | 140 |  |  | foreach($file_extensions as $file_ext) {
 | 
      
        | 141 |  |  | 	$file_ext_len=strlen($file_ext);
 | 
      
        | 142 |  |  | 	if (substr($name,-$file_ext_len)==$file_ext) {
 | 
      
        | 143 |  |  | 		$name.='.txt';
 | 
      
        | 144 |  |  | 	}
 | 
      
        | 145 |  |  | }
 | 
      
        | 146 |  |  | 
 | 
      
        | 147 |  |  | 
 | 
      
        | 148 |  |  | // Check if we should overwrite or not
 | 
      
        | 149 |  |  | if($admin->get_post('overwrite') != 'yes' AND file_exists(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name) == true) {
 | 
      
        | 150 |  |  | 	if($type == 'folder') {
 | 
      
        | 151 |  |  | 		$admin->print_error($MESSAGE['MEDIA']['DIR_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 152 |  |  | 	} else {
 | 
      
        | 153 |  |  | 		$admin->print_error($MESSAGE['MEDIA']['FILE_EXISTS'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 154 |  |  | 	}
 | 
      
        | 155 |  |  | }
 | 
      
        | 156 |  |  | 
 | 
      
        | 157 |  |  | // Try and rename the file/folder
 | 
      
        | 158 |  |  | if(rename(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file, WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name)) {
 | 
      
        | 159 | 1457 | Luisehahne | 	$usedFiles = array();
 | 
      
        | 160 |  |  |     // feature freeze
 | 
      
        | 161 |  |  | 	// require_once(ADMIN_PATH.'/media/dse.php');
 | 
      
        | 162 |  |  | 
 | 
      
        | 163 | 238 | stefan | 	$admin->print_success($MESSAGE['MEDIA']['RENAMED'], "browse.php?dir=$directory");
 | 
      
        | 164 |  |  | } else {
 | 
      
        | 165 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 166 |  |  | }
 |