<?
include "header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "newfolder"; }



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

if($foldername == "") {
echo "
$header
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='create_folder.php' method='POST'>
<td>
<b>Folder Creation Error</b><br>
You forgot to specify a name for this new folder.<br>
<br>
<input type='submit' class='button' value='Try Again'>
<input type='hidden' name='task' value='newfolder'>
</td>
</tr>
</form>
</table>
$footer
";
exit;
}

$folders = mysql_num_rows(mysql_query("SELECT * FROM ims_folders WHERE u_id='$user_info[u_id]' AND name='$foldername'"));

if($folders != 0) {
echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='create_folder.php' method='POST'>
<td>
<b>Folder Creation Error</b><br>
The folder could not be created because a folder named <b>\"".stripslashes($foldername)."\"</b> already exists.<br>
<br>
<input type='submit' class='button' value='Try Again'>
<input type='hidden' name='task' value='newfolder'>
</td>
</tr>
</form>
</table>";
echo $footer;
exit;
}

$in_album = $_POST['in_album'];
if($in_album != "yes") { $in_album == "no"; }

mysql_query("INSERT INTO ims_folders (u_id, name, description, in_album) VALUES ('$user_info[u_id]', '$foldername', '$description', '$in_album')");
$folder_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_folders WHERE u_id='$user_info[u_id]' AND name='$foldername' AND description='$description'"));

mkdir("./u/$user_info[u_id]/$folder_info[f_id]", 0700);
chmod("./u/$user_info[u_id]/$folder_info[f_id]", 0777);  
mkdir("./u/$user_info[u_id]/$folder_info[f_id]/thumbs", 0700);
chmod("./u/$user_info[u_id]/$folder_info[f_id]/thumbs", 0777);
header("Location: index.php");
exit;
}




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

// COUNT FOLDERS
$total_folders = mysql_num_rows(mysql_query("SELECT * FROM ims_folders WHERE u_id='$user_info[u_id]'"));

if($total_folders == $user_info[folders_avail]) {
echo "
<table width='500' cellpadding='0' cellspacing='0'>
<tr>
<form action='index.php' method='POST'>
<td>
<b>Folder Maximum Reached</b><br>
You cannot create another folder because you have already reached
the maximum number of folders that you may create ($user_info[folders_avail]).
To create a new folder you must delete an old one.<br>
<br>
<input type='submit' class='button' value='Cancel'>
</td>
</tr>
</form>
</table>
$footer
";
exit;
}

echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<td>
<b>Create New Folder</b><br>
Image folders are used to help you upload and organize your images.<br>
Please specify a name and brief description for your new folder below.
</td>
</tr>
</table>

<br>

<table width='500' cellpadding='0' cellspacing='0'>
<form action='create_folder.php' method='POST'>
<tr>
<td style='padding-bottom: 3px;'>
<font class='small'>Folder Name</font><br>
<input type='text' name='foldername' size='35' maxlength='40' style='margin-bottom: 3px;'><br>
<font class='small'>Folder Description</font><br>
<textarea name='description' rows='3' cols='50' style='margin-bottom: 8px;'></textarea><br>
"; 
$album = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$user_info[u_id]'"));
if($user_info[photo_album] == "yes" AND $album[albumprefs_on] == "yes") { 
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td><input type='checkbox' name='in_album' value='yes' CHECKED></td>
<td>Include this folder in my photo album.</td>
</tr>
</table><br>
"; 
} else { 
echo "
<input type='hidden' name='in_album' value='no'>
"; 
} 

echo "
<input class='button' type='submit' value='Create Folder'>
<input class='button' type='button' value='Cancel' onClick='goview()'>
<input type='hidden' name='task' value='newfolder_do'>
</td>
</tr>
</form>
</table>";
echo $footer;
}
?>