<?
$page = "attach.php";
include "manager_header.php";

if(isset($_GET['task'])) { $task = $_GET['task']; } elseif(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "main"; }


// SET VARS
$is_error = "";
$general_error = "";
$upload_error = "";
$image_url = "http://";



if($task == "attach_do") {

$image_url = $_POST['image_url'];
$file_name = $_FILES['image_file']['name'];
$file_type = $_FILES['image_file']['type'];
$file_size = $_FILES['image_file']['size'];
$file_tempname = $_FILES['image_file']['tmp_name'];
$file_error = $_FILES['image_file']['error'];



$allow_images = "yes";
if($group_info[allow_uploads] == "1") {
  $extensions = $group_info[uploads_extensions];
  if(mysql_num_rows(mysql_query("SELECT f_id FROM bhost_folders WHERE u_id='$user_info[u_id]' AND folder='Uploaded Images'")) != 1) {
   mysql_query("INSERT INTO bhost_folders (u_id, in_album, folder, description) VALUES ('$user_info[u_id]', '0', '$attach8', '$attach9')");
  }
  $folder_info = mysql_fetch_assoc(mysql_query("SELECT f_id FROM bhost_folders WHERE u_id='$user_info[u_id]' AND folder='$attach8'"));
  $f_id = $folder_info[f_id];
  $location = "folder";
} elseif($group_info[allow_album] == "1") {
  $extensions = $group_info[album_extensions];
  $f_id = 0;
  $location = "album";
} else {
  $allow_images = "no";
}


if(str_replace("http://", "", str_replace(" ", "", $image_url)) == "" & str_replace(" ", "", $file_name) == "") {
  $is_error = "yes";
  $general_error = "$attach10";
}

if(str_replace("http://", "", str_replace(" ", "", $image_url)) != "" & str_replace(" ", "", $file_name) != "") {
  $is_error = "yes";
  $general_error = "$attach11";
}


if(isset($file_name) & str_replace(" ", "", $file_name) != "" & $allow_images == "yes" & $is_error != "yes") {
  $file_name = str_replace(" ", "_", $file_name);
  $file_name = str_replace("?", "", $file_name); 
  $file_name = str_replace("&", "", $file_name);
  $file_name = str_replace("=", "", $file_name);
  $file_name = str_replace("#", "", $file_name);
  $file_name = str_replace("%", "", $file_name);
  $file_name = str_replace("'", "", $file_name);
  $file_name = str_replace("\"", "", $file_name);
  $file_name = stripslashes($file_name);
  
  $ext = strtolower(strrchr($file_name, "."));
  $ext = str_replace(".", "", $ext);

  $extlist = str_replace(" ", "", $extensions);
  $extlist = str_replace(",", ", ", $extlist);
  $extensions_case = strtolower($extensions);
  $extensions = explode(",", $extensions_case);

  if(!in_array($ext, $extensions)) {
  $is_error = "yes";
  $upload_error = "$attach12";
  }

  if($is_error != "yes") {
   $type = strtolower($file_type);
   if($ext == "jpg" OR $ext == "jpeg" OR $ext == "jpe") { 
   if($type != "image/jpeg" AND $type != "image/jpg" AND $type != "image/jpe" AND $type != "image/pjpeg" AND $type != "image/pjpg" AND $type != "image/x-jpeg" AND $type != "image/x-jpg") {
    $is_error = "yes";
    $upload_error = "$attach12";
   }}
   elseif($ext == "gif") { 
   if($type != "image/gif" AND $type != "image/x-gif") {
    $is_error = "yes";
    $upload_error = "$attach12";
   }}
   elseif($ext == "png") { 
   if($type != "image/png" AND $type != "image/x-png") {
    $is_error = "yes";
    $upload_error = "$attach12";
   }}
   elseif($ext == "tiff") { 
   if($type != "image/tif" AND $type != "image/tiff") {
    $is_error = "yes";
    $upload_error = "$attach12";
   }}
   elseif($ext == "bmp") { 
   if($type != "image/bmp" AND $type != "image/x-bmp") {
    $is_error = "yes";
    $upload_error = "$attach12";
   }}
  }

  if($is_error != "yes") {
   $space_allowed = $group_info[uploads_space] * 1024;
   $files = mysql_fetch_assoc(mysql_query("SELECT sum(filesize) AS size FROM bhost_uploads WHERE u_id='$user_info[u_id]'"));
   $space_used = $files[size]+$file_size;
   
   if($space_used > $space_allowed) {
    $is_error = "yes";
    $upload_error = "$attach13";
   }
  }

if($is_error != "yes") {
 $dimension = @getimagesize($file_tempname);
 if($dimension[0] != "" AND $dimension[1] != "") { $dimensions = "$dimension[0]x$dimension[1]"; }
 mysql_query("INSERT INTO bhost_uploads (u_id, f_id, location, ext, filesize, name, description, dimensions) VALUES ('$user_info[u_id]', '$f_id', '$location', '$ext', '$file_size', '$file_name', '', '$dimensions')") or die(mysql_error());
 $image_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND f_id='$f_id' AND location='$location' AND filesize='$file_size' AND name='$file_name' AND description='' ORDER BY up_id DESC LIMIT 1"));

 $newfilename = "$image_info[up_id].$ext";
 $letter = strtolower(substr($user_info[username], 0, 1));
 $path = "../uploads/$letter/$user_info[username]/$newfilename";
 $image_url = str_replace("manager/attach.php", "", $_SERVER['PHP_SELF']);
 $image_url = "http://".$_SERVER['HTTP_HOST'].$image_url."uploads/$letter/$user_info[username]/$image_info[up_id].$ext";

 if(!move_uploaded_file($file_tempname, $path)) {
  mysql_query("DELETE FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND f_id='$f_id' AND location='$location' AND filesize='$file_size' AND name='$name' AND description='$description'");
  $is_error = "yes";
  $upload_error = "$attach14";
 } else {

  //CREATE THUMBNAILS

	// GET FILE DIMENSIONS
	$dimension = explode("x", $image_info[dimensions]);
	$width = $dimension[0];
	$height = $dimension[1];

	// MAKE THUMBNAILS
	while($width > 150 OR $height > 150) {
		$width = $width * .95;
		$height = $height * .95;
	}
	$width = (int)$width;
	$height = (int)$height;

  if(function_exists('gd_info')) {
  $thumbpath = "../uploads/$letter/$user_info[username]/$image_info[up_id]"."_thumb.$ext";
  if($ext == "gif") {
    $thumb = imagecreatetruecolor($width, $height);
    $new = imagecreatefromgif("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
        for($i=0; $i<256; $i++) {
        imagecolorallocate($thumb, $i, $i, $i);
        }
     $size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
     imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
     imagejpeg($thumb, $thumbpath);
     ImageDestroy($new);
     ImageDestroy($thumb);
  } elseif($ext == "bmp") {
    $thumb = imagecreatetruecolor($width, $height);
    $new = imagecreatefrombmp("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
        for($i=0; $i<256; $i++) {
        imagecolorallocate($thumb, $i, $i, $i);
        }
     $size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
     imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
     imagejpeg($thumb, $thumbpath);
     ImageDestroy($new);
     ImageDestroy($thumb);
  } elseif($ext == "jpeg" | $ext == "jpg") {
    $thumb = imagecreatetruecolor($width, $height);
    $new = imagecreatefromjpeg("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
        for($i=0; $i<256; $i++) {
        imagecolorallocate($thumb, $i, $i, $i);
        }
     $size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
     imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
     imagejpeg($thumb, $thumbpath);
     ImageDestroy($new);
     ImageDestroy($thumb);
  } elseif($ext == "png") {
    $thumb = imagecreatetruecolor($width, $height);
    $new = imagecreatefrompng("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
        for($i=0; $i<256; $i++) {
        imagecolorallocate($thumb, $i, $i, $i);
        }
     $size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
     imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
     imagepng($thumb, $thumbpath);
     ImageDestroy($new);
     ImageDestroy($thumb);
  } 
  }

  @chmod($path, 0777);
 }
}
}



if($is_error != "yes") {
  echo "
  <html>
  <head>
  <title>$attach1</title>
  <script language='JavaScript' type='text/javascript' src='../include/richtext.js'></script>
  <script language='JavaScript'>
  <!-- 
  self.parent.addSmilie('".$image_url."', 'contents');
  window.close();
  // -->
  </script>
  </head>
  <body>
  $attach15<br>
  <input type='button' class='button' value='$attach16' onClick='window.close()'>
  </body>
  </html>
  ";
  exit();
}


$task = "main";
}






// SHOW ADD IMAGE PAGE
if($task == "main") {

echo "
<html>
<head>
<title>$attach1</title>
<style type='text/css'>

body { 
	margin: 0px; 
	padding: 10px; 
	background: #EEEEEE; 
}

td {
	font-family: verdana, arial, serif;
	font-size: 8pt;
}

td.form { 
	padding: 10px; 
	background: #FFFFFF; 
	border: 1px solid #AAAAAA; 
}

input.text {
	font-family: verdana, arial, serif;
	font-size: 8pt;
}

input.button {
	background: #DDDDDD; 
	color: #444444; 
	font-size: 8pt; 
	font-weight: bold; 
	font-family: verdana, arial, serif;
	padding: 3px;
}

td.label {
	font-weight: bold;
	padding-top: 10px;
	padding-bottom: 10px;
}

td.middle {
	font-weight: bold;
	padding-top: 10px;
	font-size: 16pt;
}

td.bottom {
	padding-top: 20px;
}

div.help {
	color: #666666; padding-top: 9px; font-weight: normal;
}

img.error {
	vertical-align: middle;
}

input.status {
	background: none;
	border: none;
	font-size: 8pt;
	font-weight: bold;
	margin-left: 10px;
	font-family: verdana, arial, serif;
}

</style>

<script lang='JavaScript'>
<!--
function doUpload() {
document.info.status.value='$attach17';
window.setTimeout(\"doMsg1()\", 400);
}

function doMsg1() {
document.info.status.value = document.info.status.value + '.';
if(document.info.status.value == '$attach17....') { document.info.status.value = '$attach17'; }
window.setTimeout(\"doMsg1()\", 400);
}
// -->
</script>

</head>
<body onload='window.focus()'>


";



// SHOW ERROR PAGE IF NECESSARY
if($general_error != "" OR $upload_error != "") {
echo "
<table cellpadding='0' cellspacing='0' width='100%' height='100%' align='center'>
<form action='attach.php' method='POST'>
<tr>
<td class='form' align='center' valign='middle'>
"; 

show_error($general_error);
show_error($upload_error); 

echo "
<br><br>
<input type='submit' class='button' value='$attach19'>
</td>
</tr>
</form>
</table>
</body>
</html>
";
exit;
}



echo "
<table cellpadding='0' cellspacing='0' width='100%' height='100%' align='center'>
<tr>
<form action='attach.php' method='POST' name='info' enctype='multipart/form-data'>
<td class='form' valign='top'>

<table cellpadding='0' cellspacing='0' align='center'>
<tr><td class='label' colspan='2'>$attach2</td></tr>
<tr>
<td align='right' NOWRAP>$attach18&nbsp;</td>
<td><input type='text' class='text' size='60' name='image_url' value='$image_url'></td>
</tr>
";

if($group_info[allow_album] == "1" | $group_info[allow_uploads] == "1") {
echo "
<tr><td class='middle' align='center' colspan='2'>$attach3</td></tr>
<tr><td class='label' colspan='2'>$attach4</td></tr>
<tr>
<td align='right' NOWRAP>$attach5&nbsp;</td>
<td><input type='file' class='text' size='48' name='image_file'></td>
</tr>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000'>
";
}

echo "
<tr>
<td class='bottom' colspan='2'>
  <table cellpadding='0' cellspacing='0'>
  <tr>
  <td><input type='submit' class='button' value='$attach6' name='submit' onClick='doUpload()'>&nbsp;</td>
  <td><input type='button' class='button' value='$attach7' onClick='window.close()'></td>
  <td><input type='text' class='status' name='status' READONLY></td>
  </tr>
  </table>
<br>
</td>
</tr>
</table>

</td>
<input type='hidden' name='task' value='attach_do'>
</form>
</tr>
</table>
</body>
</html>
";
}





?>