<?php 
session_start();
include('userchat-config.php'); 

if($_GET['type']=="userpic"){
$allowed = array("image/jpg","image/jpeg");
$size =  getimagesize($_FILES['userchat-upload']['tmp_name']);
if(!in_array($_FILES['userchat-upload']['type'], $allowed)){
$error = 'Upload failed : Invalid File type detected. Only jpg or jpeg files are allowed.';
} elseif($size[0]!=30 || $size[1]!=30){
$error = 'Upload failed : Invalid image dimension detected. Image must be 30 x 30 pixels.';
} else {
move_uploaded_file($_FILES['userchat-upload']['tmp_name'], 'userpics/'.$_GET['userid'].'.jpg');
$error = 'profilepic_uploaded';
}
header('location:userchat-upload.php?e='.urlencode($error).'&type='.$_GET['type']);
}

if($_GET['type']=="file"){
$ext = pathinfo(strtolower($_FILES['userchat-upload']['name']), PATHINFO_EXTENSION);
$fname = pathinfo($_FILES['userchat-upload']['name'], PATHINFO_FILENAME);
$size = 2*1024*1024;
$allowed = array("aac", "ai", "aiff", "avi", "bmp", "dmg", "css", "doc", "dotx", "dwg", "dxf", "eps", "flv", "gif", "html", "ics", "iso", "jpg", "js", "key", "less", "mid", "mp3", "mp4", "mpg", "odf", "ods", "odt", "odp", "ots", "ott", "pdf", "png", "ppt", "psd", "qt", "rar", "rb", "rtf", "sass", "scss", "sql", "tga", "tgz", "tiff", "txt", "wave", "xls", "xlsx", "xml", "yml", "zip");
if(!in_array($ext, $allowed)){
$error = 'Upload failed : Invalid File type detected. Try again with a supported file type.';
} elseif($_FILES['userchat-upload']['size']>$size){
$error = 'Upload failed : File is too big than allowed size of '.$size.' Bytes. Please try uploading a smaller file.';
} else {
$newname = uniqid().'-'.$fname.'.'.$ext;
move_uploaded_file($_FILES['userchat-upload']['tmp_name'], 'uploads/'.$newname);
$error = 'file_uploaded';
}
header('location:userchat-upload.php?e='.urlencode($error).'&type='.$_GET['type'].'&xdata=file:'.$_FILES['userchat-upload']['size'].':'.$newname.'&uid='.$_GET['uid']);
}


mysqli_close($con);
?>