| 1 | 238 | stefan | <?php
 | 
      
        | 2 | 1400 | FrankH | /**
 | 
      
        | 3 |  |  |  *
 | 
      
        | 4 |  |  |  * @category        admin
 | 
      
        | 5 | 1476 | Luisehahne |  * @package         media
 | 
      
        | 6 | 1400 | FrankH |  * @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 the current dir
 | 
      
        | 28 | 1457 | Luisehahne | $requestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
 | 
      
        | 29 |  |  | $directory = (isset(${$requestMethod}['dir'])) ? ${$requestMethod}['dir'] : '';
 | 
      
        | 30 | 1475 | Luisehahne | $directory = ($directory == '/') ?  '' : $directory;
 | 
      
        | 31 | 1400 | FrankH | 
 | 
      
        | 32 | 1475 | Luisehahne | $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 | 1400 | FrankH | if (!check_media_path($directory)) {
 | 
      
        | 38 | 1475 | Luisehahne | 	$admin->print_error($MESSAGE['MEDIA']['DIR_DOT_DOT_SLASH'],$rootlink, false);
 | 
      
        | 39 | 238 | stefan | }
 | 
      
        | 40 |  |  | 
 | 
      
        | 41 |  |  | // Get the temp id
 | 
      
        | 42 | 1475 | Luisehahne | $file_id = intval($admin->checkIDKEY('id', false, $_SERVER['REQUEST_METHOD']));
 | 
      
        | 43 | 1400 | FrankH | if (!$file_id) {
 | 
      
        | 44 | 1475 | Luisehahne | 	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'],$dirlink, false);
 | 
      
        | 45 | 238 | stefan | }
 | 
      
        | 46 |  |  | 
 | 
      
        | 47 | 1476 | Luisehahne | // Check for potentially malicious files
 | 
      
        | 48 |  |  | $forbidden_file_types  = preg_replace( '/\s*[,;\|#]\s*/','|',RENAME_FILES_ON_UPLOAD);
 | 
      
        | 49 | 238 | stefan | // 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 | 1475 | Luisehahne | 		$info = pathinfo($file);
 | 
      
        | 57 |  |  | 		$ext = isset($info['extension']) ? $info['extension'] : '';
 | 
      
        | 58 | 238 | stefan | 		if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
 | 
      
        | 59 | 1475 | Luisehahne | 			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 | 238 | stefan | 				}
 | 
      
        | 67 |  |  | 			}
 | 
      
        | 68 |  |  | 		}
 | 
      
        | 69 |  |  | 	}
 | 
      
        | 70 |  |  | 	$temp_id = 0;
 | 
      
        | 71 |  |  | 	if(isset($DIR)) {
 | 
      
        | 72 | 384 | Ruebenwurz | 		sort($DIR);
 | 
      
        | 73 | 238 | stefan | 		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 | 384 | Ruebenwurz | 		sort($FILE);
 | 
      
        | 83 | 238 | stefan | 		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 | 1475 | Luisehahne | 
 | 
      
        | 93 | 1457 | Luisehahne | $file_id = $admin->getIDKEY($file_id);
 | 
      
        | 94 | 1475 | Luisehahne | 
 | 
      
        | 95 | 238 | stefan | if(!isset($rename_file)) {
 | 
      
        | 96 | 1475 | Luisehahne | 	$admin->print_error($MESSAGE['MEDIA']['FILE_NOT_FOUND'], $dirlink, false);
 | 
      
        | 97 | 238 | stefan | }
 | 
      
        | 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 | 1475 | Luisehahne | $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 | 238 | stefan | // 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 | 1475 | Luisehahne | $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 | 238 | stefan | 
 | 
      
        | 148 | 1475 | Luisehahne | 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 | 238 | stefan | 
 | 
      
        | 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 | 1457 | Luisehahne | 	$usedFiles = array();
 | 
      
        | 164 |  |  |     // feature freeze
 | 
      
        | 165 |  |  | 	// require_once(ADMIN_PATH.'/media/dse.php');
 | 
      
        | 166 |  |  | 
 | 
      
        | 167 | 1475 | Luisehahne | 	$admin->print_success($MESSAGE['MEDIA']['RENAMED'], $dirlink);
 | 
      
        | 168 | 238 | stefan | } else {
 | 
      
        | 169 |  |  | 	$admin->print_error($MESSAGE['MEDIA']['CANNOT_RENAME'], "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 170 |  |  | }
 |