<?
include "header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "renamefolder"; }
if(isset($_POST['f_id'])) { $f_id = $_POST['f_id']; } elseif(isset($_GET['f_id'])) { $f_id = $_GET['f_id']; } else { exit(); }
if(mysql_num_rows(mysql_query("SELECT * FROM ims_folders WHERE f_id='$f_id' AND u_id='$user_info[u_id]'")) != 1) { exit(); }
$folder_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_folders WHERE f_id='$f_id' AND u_id='$user_info[u_id]'"));

if($task == "renamefolder_do") {
$newname = str_replace(">", "&gt;", str_replace("<", "&lt;", str_replace("'", "&#39;", trim($_POST['newname']))));
$description = str_replace(">", "&gt;", str_replace("<", "&lt;", str_replace("'", "&#39;", $_POST[description])));

if($newname == "") {
echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='modify_folder.php?f_id=$f_id' method='POST'>
<td>
<b>An Error Has Occurred:</b><br>
You forgot to specify a new name for this folder.<br>
<br>
<input type='submit' class='button' value='Try Again'>
<input type='hidden' name='task' value='renamefolder'>
</td>
</tr>
</form>
</table>";
echo $footer;
exit;
}




$folders = mysql_num_rows(mysql_query("SELECT * FROM ims_folders WHERE u_id='$user_info[u_id]' AND name='$newname'"));
$newfolder_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_folders WHERE u_id='$user_info[u_id]' AND name='$newname'"));
if(($folders != 0) & ($folder_info[f_id] != $newfolder_info[f_id])) {
echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='modify_folder.php?f_id=$f_id' method='POST'>
<td>
<b>An Error Has Occurred:</b><br>
The folder could not be renamed because a folder with the same name already exists.
<br><br>
<input type='submit' class='button' value='Try Again'>
<input type='hidden' name='task' value='renamefolder'>
</td>
</tr>
</form>
</table>";
echo $footer;
exit;
}

mysql_query("UPDATE ims_folders SET name='$newname', description='$description' WHERE u_id='$user_info[u_id]' AND f_id='$f_id'");
header("Location: index.php");
exit;
}



if($task == "renamefolder") {
echo $header;

$stripped_foldername = str_replace("'","&#39;", $folder_info[name]);
$stripped_foldername = str_replace("\"","&#34;", $stripped_foldername);

echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<td>
<b>Modify Folder</b><br>
Please enter a new name or description for this folder.<br>
</td>
</tr>
</table>

<br>

<table width='520' cellpadding='0' cellspacing='0'>
<form action='modify_folder.php' method='POST'> 
<tr>
<td class='gray'>
<font class='small'>Folder Name</font><br>
<input name='newname' type='text' size='35' maxlength='40' value='$stripped_foldername' style='margin-bottom: 3px;'><br>
<font class='small'>Folder Description</font><br>
<textarea name='description' rows='3' cols='50' style='margin-bottom: 8px;'>$folder_info[description]</textarea><br><br>
<input type='submit' class='button' value='Modify Folder'>
<input type='button' class='button' value='Cancel' onClick='goview()'>
<input type='hidden' name='task' value='renamefolder_do'>
<input type='hidden' name='f_id' value='$f_id'>
</td>
</tr>
</form>
</table>";

echo $footer;
}

?>