<script language="JavaScript1.2" type="text/javascript">
tb = parent.AfianHandler['allTabs']['<?echo 
$_GET['fromtabid'];?>'];

<?
/* check user permission*/
if ($permissions['readonly']) {
	iframeAlert($lang->get("You are not alowed to rename files or folders"));
	echo "parent.MyFilesEndAction(tb);</script>";
	exit();
}


$oldName = $_POST['oldName'];
$newName = $_POST['newName'];
$fromfolder	= $str->fromHTML($_GET['fromfolder']);

for ($i = 0 ; $i < sizeof($oldName) ; $i++) {
	$oldName[$i] = $str->fromHTML($oldName[$i], false);
	if (!isCleanFilename($oldName[$i])) {
	iframeAlert($lang->get('Ilegal filename: ').$oldName[$i]);
	echo "parent.MyFilesEndAction(tb);</script>";
	exit();
	}
}
for ($i = 0 ; $i < sizeof($newName) ; $i++) {
	$newName[$i] = $str->fromHTML($newName[$i]);
	if (!isCleanFilename($newName[$i])) {
	iframeAlert($lang->get('Ilegal filename: ').$newName[$i]);
	echo "parent.MyFilesEndAction(tb);</script>";
	exit();
	}
}

for ($i = 0 ; $i < sizeof($newName) ; $i++) {
	$newName[$i] = $str->fromHTML($newName[$i]);
	if (strlen(trim($newName[$i])) == 0) {
	iframeAlert($lang->get('You must type a filename for renaming.'));
	echo "parent.MyFilesEndAction(tb);</script>";
	exit();
	}
}

if (!isCleanPath($fromfolder)) {
	iframeAlert($lang->get('Ilegal path: ').$fromfolder);
	echo "parent.MyFilesEndAction(tb);</script>";
	exit();
}



$sys['fullPath'] = gluePath($permissions['path'], $fromfolder);
$list = new iconlist($_GET['fromtabid']);

$renamedFolders = 0;
$renamedObjects = 0;
?>
if (tb != undefined) {
tb.destroyItems();
}
<?

for ($i = 0 ; $i < sizeof($oldName) ; $i++) {
		$old = $oldName[$i];
		$new = $str->fromHTML($newName[$i]);
		$oldRelativePath = gluePath($fromfolder, $old);
		$newRelativePath = gluePath($fromfolder, $new);
		$newPath = gluePath($sys['fullPath'], $new);
		$oldPath = gluePath($sys['fullPath'], $old);
			if ($old != $new) {
				if(@is_dir($oldPath)) {
					$type = "folder";

					//$isPublic = $public->isPublic($oldRelativePath);
					$isShared = $share->isShared($oldRelativePath);
					$flags->changeFlaggedPath($oldRelativePath, $newRelativePath);
					$flags->updateSubPaths($oldRelativePath, $newRelativePath);
					
				} else {
					$type = "file";
				}

				if (@$fm->rename($oldPath, $new)) {
					$renamedObjects++;
					$log->act("0", "rename", $lang->get("%1 renamed to %2", array(gluePath($sys['fullPath'], $old), $new)));
					$comments->move(gluePath($sys['fullPath'], $old), gluePath($sys['fullPath'], $new));
					if ($img->isImage($old)) {
						$thumb->renameCache($oldPath, $new);
					}

	/* insert new named item in list */
	$data['name'] = $new;
	$data['lcname'] = strtolower($data['name']);
	$data['mdate'] = filemtime($newPath);
	if (is_dir($newPath)) {
		$data['isdir'] = true;
		$data['size'] = $fm->getDirSize($newPath);
		$data['ext'] = "";
		$data['filetypedescription'] = $lang->get("Folder");
		
		if ($isShared && !$isPublic) {
			$icon = "folder-shared.gif";
		} elseif (!$isShared && $isPublic) {
			$icon = "folder-public.gif";
		}  elseif ($isShared && $isPublic) {
			$icon = "folder-shared-public.gif";
		} else {
			$icon = "folder.gif";
		}
		$data['icon_img'] = gluePath("images/icons/filetypes/big/", $icon);
		$data['thumb_img'] = gluePath("images/icons/filetypes/big/", $icon);
		$data['list_img'] = gluePath("images/icons/filetypes/small/", $icon);
		
		
		$data['filetype'] = "folder";
		$data['ondblclick'] = "cd('".$str->safeJS(gluePath($fromfolder,$new))."')";
		
		$renamedFolders++;
	} else {
		$data['isdir'] = false;
		$data['size'] = filesize($newPath);
		$extension = $fm->getExtension($new);
		$data['ext'] = $extension;
		if (strtolower($data['ext']) == "zip") {
			$data['iszip'] = true;
		} else {
			$data['iszip'] = false;
		}
		$fileinfo = $fm->fileTypeInfo($extension);
		$data['filetypedescription'] = $fileinfo['description'];
		$data['icon_img'] = gluePath("images/icons/filetypes/big/",$fileinfo['icon']);
		$iconfolder = "images/icons/filetypes/big/";
		$data['thumb_img'] =  $img->icon($newPath, 1, 1, $fromfolder);
		$data['bigthumb'] = ((strstr($data['thumb_img'], '?'))?true:false);
		$data['list_img'] = gluePath("images/icons/filetypes/small/",$fileinfo['icon']);
		$data['filetype'] = $img_prefix.$fileinfo['type'];
		$data['ondblclick'] = "get('".$str->safeJS($data['name'])."')";
		if ($fileinfo['type'] == "txt") {
			$data['txt'] = true;
		} else {
			$data['txt'] = false;
		}
	}
	$list->drawIcon($data);

/* rename ok, do js */
					?>
/*remove deleted item from tab*/
if (tb != undefined) {
tb.removeItemArray('<?echo $str->safeJS($old);?>');
}
<?
if ($data['isdir']) {
?>
/* close folder if opened in a tab */
parent.closeTabByPath('<?echo $str->safeJS($oldRelativePath);?>');
<?}?>
					<?	
				} else {
				$log->act("1", "document rename", "\"".gluePath($sys['fullPath'], $old)."\" TO \"".$new."\" (".$fm->errorMsg.")");
		if (!file_exists($oldPath)) {
			$iframeAlertMsg[] = $lang->get("Rename failed: '%1' not available.", array($oldRelativePath));
		} else if (file_exists($newPath)) {
			$iframeAlertMsg[] = $lang->get("Rename failed: '%1' already exists.", array($newRelativePath));
		}

				}
			}
	}
?>

<?
if ($renamedFolders > 0) {
?>
parent.reloadTreeByPath('<?echo $str->safeJS($fromfolder);?>');
<?}?>

<?
if ($renamedObjects > 0) {
?>
parent.AfianBubbleMessage('<?$lang->display("Object(s) renamed.");?>');
<?}?>

parent.MyFilesEndAction(tb);
if (tb != undefined) {
	tb.buildItems();
	tb.sortItems(tb.sortBy, 'desc');
	if (tb.active) {
		tb.showItems();
	}
}

<?iframeAlert($iframeAlertMsg);?>
</script>