<?php
	header("Content-type: text/html;Charset=utf-8;");
	if(isset($_GET['type']) && isset($_FILES['upload'])){
		if($_GET['type']=="Images"){
			$valid_formats = array("jpg", "png", "gif");// Upload formats
			if ($_FILES['upload']['error'] == 0) {
				if( ! in_array(strtolower(pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION)), $valid_formats) ){
					echo $_FILES["upload"]["name"]." is not a valid format";
				}else{ // No error found! Move uploaded files
					if(move_uploaded_file($_FILES["upload"]["tmp_name"], "img/product/" . $_FILES["upload"]["name"])){
						$funcNum = preg_replace("/[^0-9]/", "", $_GET['CKEditorFuncNum']);
						echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction($funcNum, 'http://".htmlspecialchars($_SERVER["SERVER_NAME"])."/img/product/" . $_FILES["upload"]["name"]."', 'Completed');</script>";
					}
				}
			}else
				echo "Error";
		}
		if($_GET['type']=="Files"){
			$valid_formats = array("pdf", "doc", "docx");// Upload formats
			if ($_FILES['upload']['error'] == 0) {
				if( ! in_array(strtolower(pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION)), $valid_formats) ){
					echo $_FILES["upload"]["name"]." is not a valid format";
				}else{ // No error found! Move uploaded files
					if(move_uploaded_file($_FILES["upload"]["tmp_name"], "img/files/" . $_FILES["upload"]["name"])){
						$funcNum = preg_replace("/[^0-9]/", "", $_GET['CKEditorFuncNum']);
						echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction($funcNum, 'http://".htmlspecialchars($_SERVER["SERVER_NAME"])."/img/files/" . $_FILES["upload"]["name"]."', 'Completed');</script>";
					}
				}
			}else
				echo "Error";
		}
	}
?>