| 1 | 238 | stefan | <?php
 | 
      
        | 2 | 1400 | FrankH | /**
 | 
      
        | 3 |  |  |  *
 | 
      
        | 4 |  |  |  * @category        admin
 | 
      
        | 5 | 1476 | Luisehahne |  * @package         media
 | 
      
        | 6 | 1818 | Luisehahne |  * @author          Ryan Djurovich (2004-2009), WebsiteBaker Project
 | 
      
        | 7 |  |  |  * @copyright       2009-2012, WebsiteBaker Org. e.V.
 | 
      
        | 8 | 1400 | FrankH |  * @link			http://www.websitebaker2.org/
 | 
      
        | 9 |  |  |  * @license         http://www.gnu.org/licenses/gpl.html
 | 
      
        | 10 |  |  |  * @platform        WebsiteBaker 2.8.x
 | 
      
        | 11 |  |  |  * @requirements    PHP 5.2.2 and higher
 | 
      
        | 12 |  |  |  * @version         $Id$
 | 
      
        | 13 | 1818 | Luisehahne |  * @filesource		$HeadURL$
 | 
      
        | 14 |  |  |  * @lastmodified    $Date$
 | 
      
        | 15 | 1400 | FrankH |  *
 | 
      
        | 16 |  |  |  */
 | 
      
        | 17 | 238 | stefan | 
 | 
      
        | 18 | 1818 | Luisehahne | if(!defined('WB_URL'))
 | 
      
        | 19 |  |  | {
 | 
      
        | 20 |  |  |     $config_file = realpath('../../config.php');
 | 
      
        | 21 |  |  |     if(file_exists($config_file) && !defined('WB_URL'))
 | 
      
        | 22 |  |  |     {
 | 
      
        | 23 |  |  |     	require($config_file);
 | 
      
        | 24 |  |  |     }
 | 
      
        | 25 |  |  | }
 | 
      
        | 26 | 2098 | darkviper | //if(!class_exists('admin', false)){ include(WB_PATH.'/framework/class.admin.php'); }
 | 
      
        | 27 |  |  | $oTrans = Translate::getInstance();
 | 
      
        | 28 |  |  | $oTrans->enableAddon('admin\\media');
 | 
      
        | 29 | 238 | stefan | 
 | 
      
        | 30 | 1818 | Luisehahne | $admin = new admin('Media', 'media', false);
 | 
      
        | 31 |  |  | 
 | 
      
        | 32 | 238 | stefan | // Include the WB functions file
 | 
      
        | 33 | 1824 | Luisehahne | if(!function_exists('directory_list')) { require(WB_PATH.'/framework/functions.php'); }
 | 
      
        | 34 | 238 | stefan | 
 | 
      
        | 35 |  |  | // Get the current dir
 | 
      
        | 36 | 1457 | Luisehahne | $requestMethod = '_'.strtoupper($_SERVER['REQUEST_METHOD']);
 | 
      
        | 37 |  |  | $directory = (isset(${$requestMethod}['dir'])) ? ${$requestMethod}['dir'] : '';
 | 
      
        | 38 | 1475 | Luisehahne | $directory = ($directory == '/') ?  '' : $directory;
 | 
      
        | 39 | 1400 | FrankH | 
 | 
      
        | 40 | 1475 | Luisehahne | $dirlink = 'browse.php?dir='.$directory;
 | 
      
        | 41 |  |  | $rootlink = 'browse.php?dir=';
 | 
      
        | 42 |  |  | // $file_id = intval($admin->get_post('id'));
 | 
      
        | 43 |  |  | 
 | 
      
        | 44 | 1818 | Luisehahne | // Get the current dir
 | 
      
        | 45 |  |  | $currentHome = $admin->get_home_folder();
 | 
      
        | 46 |  |  | // check for correct directory
 | 
      
        | 47 |  |  | if ($currentHome && stripos(WB_PATH.MEDIA_DIRECTORY.$rootlink,WB_PATH.MEDIA_DIRECTORY.$currentHome)===false) {
 | 
      
        | 48 |  |  | 	$rootlink = $currentHome;
 | 
      
        | 49 |  |  | }
 | 
      
        | 50 |  |  | 
 | 
      
        | 51 | 1475 | Luisehahne | // first Check to see if it contains ..
 | 
      
        | 52 | 1400 | FrankH | if (!check_media_path($directory)) {
 | 
      
        | 53 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_DIR_DOT_DOT_SLASH, $rootlink, false);
 | 
      
        | 54 | 238 | stefan | }
 | 
      
        | 55 |  |  | 
 | 
      
        | 56 |  |  | // Get the temp id
 | 
      
        | 57 | 1475 | Luisehahne | $file_id = intval($admin->checkIDKEY('id', false, $_SERVER['REQUEST_METHOD']));
 | 
      
        | 58 | 1400 | FrankH | if (!$file_id) {
 | 
      
        | 59 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_GENERIC_SECURITY_ACCESS, $dirlink, false);
 | 
      
        | 60 | 238 | stefan | }
 | 
      
        | 61 |  |  | 
 | 
      
        | 62 | 1476 | Luisehahne | // Check for potentially malicious files
 | 
      
        | 63 |  |  | $forbidden_file_types  = preg_replace( '/\s*[,;\|#]\s*/','|',RENAME_FILES_ON_UPLOAD);
 | 
      
        | 64 | 238 | stefan | // Get home folder not to show
 | 
      
        | 65 |  |  | $home_folders = get_home_folders();
 | 
      
        | 66 |  |  | 
 | 
      
        | 67 |  |  | // Figure out what folder name the temp id is
 | 
      
        | 68 |  |  | if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.'/'.$directory)) {
 | 
      
        | 69 |  |  | 	// Loop through the files and dirs an add to list
 | 
      
        | 70 |  |  |    while (false !== ($file = readdir($handle))) {
 | 
      
        | 71 | 1475 | Luisehahne | 		$info = pathinfo($file);
 | 
      
        | 72 |  |  | 		$ext = isset($info['extension']) ? $info['extension'] : '';
 | 
      
        | 73 | 238 | stefan | 		if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
 | 
      
        | 74 | 1475 | Luisehahne | 			if( !preg_match('/'.$forbidden_file_types.'$/i', $ext) ) {
 | 
      
        | 75 |  |  | 				if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
 | 
      
        | 76 |  |  | 					if(!isset($home_folders[$directory.'/'.$file])) {
 | 
      
        | 77 |  |  | 						$DIR[] = $file;
 | 
      
        | 78 |  |  | 					}
 | 
      
        | 79 |  |  | 				} else {
 | 
      
        | 80 |  |  | 					$FILE[] = $file;
 | 
      
        | 81 | 238 | stefan | 				}
 | 
      
        | 82 |  |  | 			}
 | 
      
        | 83 |  |  | 		}
 | 
      
        | 84 |  |  | 	}
 | 
      
        | 85 |  |  | 	$temp_id = 0;
 | 
      
        | 86 |  |  | 	if(isset($DIR)) {
 | 
      
        | 87 | 384 | Ruebenwurz | 		sort($DIR);
 | 
      
        | 88 | 238 | stefan | 		foreach($DIR AS $name) {
 | 
      
        | 89 |  |  | 			$temp_id++;
 | 
      
        | 90 |  |  | 			if($file_id == $temp_id) {
 | 
      
        | 91 |  |  | 				$rename_file = $name;
 | 
      
        | 92 |  |  | 				$type = 'folder';
 | 
      
        | 93 |  |  | 			}
 | 
      
        | 94 |  |  | 		}
 | 
      
        | 95 |  |  | 	}
 | 
      
        | 96 |  |  | 	if(isset($FILE)) {
 | 
      
        | 97 | 384 | Ruebenwurz | 		sort($FILE);
 | 
      
        | 98 | 238 | stefan | 		foreach($FILE AS $name) {
 | 
      
        | 99 |  |  | 			$temp_id++;
 | 
      
        | 100 |  |  | 			if($file_id == $temp_id) {
 | 
      
        | 101 |  |  | 				$rename_file = $name;
 | 
      
        | 102 |  |  | 				$type = 'file';
 | 
      
        | 103 |  |  | 			}
 | 
      
        | 104 |  |  | 		}
 | 
      
        | 105 |  |  | 	}
 | 
      
        | 106 |  |  | }
 | 
      
        | 107 | 1475 | Luisehahne | 
 | 
      
        | 108 | 1457 | Luisehahne | $file_id = $admin->getIDKEY($file_id);
 | 
      
        | 109 | 1475 | Luisehahne | 
 | 
      
        | 110 | 238 | stefan | if(!isset($rename_file)) {
 | 
      
        | 111 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_FILE_NOT_FOUND, $dirlink, false);
 | 
      
        | 112 | 238 | stefan | }
 | 
      
        | 113 |  |  | 
 | 
      
        | 114 |  |  | // Check if they entered a new name
 | 
      
        | 115 |  |  | if(media_filename($admin->get_post('name')) == "") {
 | 
      
        | 116 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_BLANK_NAME, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 117 | 238 | stefan | } else {
 | 
      
        | 118 |  |  | 	$old_name = $admin->get_post('old_name');
 | 
      
        | 119 |  |  | 	$new_name =  media_filename($admin->get_post('name'));
 | 
      
        | 120 |  |  | }
 | 
      
        | 121 |  |  | 
 | 
      
        | 122 |  |  | // Check if they entered an extension
 | 
      
        | 123 |  |  | if($type == 'file') {
 | 
      
        | 124 |  |  | 	if(media_filename($admin->get_post('extension')) == "") {
 | 
      
        | 125 | 2098 | darkviper | 		$admin->print_error($oTrans->MESSAGE_MEDIA_BLANK_EXTENSION, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 126 | 238 | stefan | 	} else {
 | 
      
        | 127 |  |  | 		$extension = media_filename($admin->get_post('extension'));
 | 
      
        | 128 |  |  | 	}
 | 
      
        | 129 |  |  | } else {
 | 
      
        | 130 |  |  | 	$extension = '';
 | 
      
        | 131 |  |  | }
 | 
      
        | 132 |  |  | 
 | 
      
        | 133 |  |  | // Join new name and extension
 | 
      
        | 134 |  |  | $name = $new_name.$extension;
 | 
      
        | 135 |  |  | 
 | 
      
        | 136 | 1475 | Luisehahne | $info = pathinfo(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name);
 | 
      
        | 137 |  |  | $ext = isset($info['extension']) ? $info['extension'] : '';
 | 
      
        | 138 |  |  | $dots = (substr($info['basename'], 0, 1) == '.') || (substr($info['basename'], -1, 1) == '.');
 | 
      
        | 139 |  |  | 
 | 
      
        | 140 |  |  | if( preg_match('/'.$forbidden_file_types.'$/i', $ext) || $dots == '.' ) {
 | 
      
        | 141 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_CANNOT_RENAME, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 142 | 1475 | Luisehahne | }
 | 
      
        | 143 |  |  | 
 | 
      
        | 144 | 238 | stefan | // Check if the name contains ..
 | 
      
        | 145 |  |  | if(strstr($name, '..')) {
 | 
      
        | 146 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_NAME_DOT_DOT_SLASH, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 147 | 238 | stefan | }
 | 
      
        | 148 |  |  | 
 | 
      
        | 149 |  |  | // Check if the name is index.php
 | 
      
        | 150 |  |  | if($name == 'index.php') {
 | 
      
        | 151 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_NAME_INDEX_PHP, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 152 | 238 | stefan | }
 | 
      
        | 153 |  |  | 
 | 
      
        | 154 |  |  | // Check that the name still has a value
 | 
      
        | 155 |  |  | if($name == '') {
 | 
      
        | 156 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_BLANK_NAME, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 157 | 238 | stefan | }
 | 
      
        | 158 |  |  | 
 | 
      
        | 159 | 1475 | Luisehahne | $info = pathinfo(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file);
 | 
      
        | 160 |  |  | $ext = isset($info['extension']) ? $info['extension'] : '';
 | 
      
        | 161 |  |  | $dots = (substr($info['basename'], 0, 1) == '.') || (substr($info['basename'], -1, 1) == '.');
 | 
      
        | 162 | 238 | stefan | 
 | 
      
        | 163 | 1475 | Luisehahne | if( preg_match('/'.$forbidden_file_types.'$/i', $ext) || $dots == '.' ) {
 | 
      
        | 164 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_CANNOT_RENAME, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 165 | 1475 | Luisehahne | }
 | 
      
        | 166 | 238 | stefan | 
 | 
      
        | 167 |  |  | // Check if we should overwrite or not
 | 
      
        | 168 |  |  | if($admin->get_post('overwrite') != 'yes' AND file_exists(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name) == true) {
 | 
      
        | 169 |  |  | 	if($type == 'folder') {
 | 
      
        | 170 | 2098 | darkviper | 		$admin->print_error($oTrans->MESSAGE_MEDIA_DIR_EXISTS, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 171 | 238 | stefan | 	} else {
 | 
      
        | 172 | 2098 | darkviper | 		$admin->print_error($oTrans->MESSAGE_MEDIA_FILE_EXISTS, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 173 | 238 | stefan | 	}
 | 
      
        | 174 |  |  | }
 | 
      
        | 175 |  |  | 
 | 
      
        | 176 |  |  | // Try and rename the file/folder
 | 
      
        | 177 |  |  | if(rename(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$rename_file, WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$name)) {
 | 
      
        | 178 | 1457 | Luisehahne | 	$usedFiles = array();
 | 
      
        | 179 |  |  |     // feature freeze
 | 
      
        | 180 |  |  | 	// require_once(ADMIN_PATH.'/media/dse.php');
 | 
      
        | 181 |  |  | 
 | 
      
        | 182 | 2098 | darkviper | 	$admin->print_success($oTrans->MESSAGE_MEDIA_RENAMED, $dirlink);
 | 
      
        | 183 | 238 | stefan | } else {
 | 
      
        | 184 | 2098 | darkviper | 	$admin->print_error($oTrans->MESSAGE_MEDIA_CANNOT_RENAME, "rename.php?dir=$directory&id=$file_id", false);
 | 
      
        | 185 | 238 | stefan | }
 |