<?php
	$fn = $_REQUEST['n'] ;
  
    $coyid = 0;
    if (isset($_REQUEST['id'])){
        $coyid = $_REQUEST['id'];
    }
	$image_type = "";
	if (isset($_REQUEST['f'])){
        $image_type = $_REQUEST['f'];
    }
        
	$imgUploaded = false ;
	$limitsize = 1024 * 1024;	//50k
	if (!empty($_FILES)) {
		$size = $_FILES[$fn]['size'];
		if($size > 0){
			if ($size < $limitsize) {
				$imgfile =  basename($_FILES[$fn]['name']);
				$pi = pathinfo($imgfile);
				$ext = $pi['extension'];
				$exts = array("png","jpg","jpeg") ;
				if (Util::validFileExt($ext,$exts)) {
					$newfile = "";
					if($image_type == "PAYNOW")
						$newfile = PATH_PICTURE . (!isset($_SESSION[SE_BU]) ? "TEMP" : $_SESSION[SE_BU]). "/paynowqr." . $ext; 
                    else
						$newfile = PATH_PICTURE . "company_" . $_SESSION[SE_ORGID] . "/logo_" . basename($_FILES[$fn]['name']); 
                    
                    //company Logo
                    if($coyid != 0){
                        if (!file_exists(PATH_PICTURE . "company_" . $coyid)){
                            mkdir(PATH_PICTURE . "company_" . $coyid, 1777);
                            
                        }
						if($image_type == "PAYNOW") {
                        	$newfile = PATH_PICTURE . (!isset($_SESSION[SE_BU]) ? "TEMP" : $_SESSION[SE_BU]). "/paynowqr." . $ext; 
							Util::removeCompanyPayNowQRFile(PATH_PICTURE, $coyid) ;
						} else {
							$newfile = PATH_PICTURE . "company_" . $coyid . "/logo_" . time() . "." . $ext; 
							Util::removeLogoFile(PATH_PICTURE."company_", $coyid) ;
						}
                    } else {
						if($image_type == "PAYNOW") {
							Util::removeCompanyPayNowQRFile(PATH_PICTURE, $_SESSION[SE_BU]) ;
						} else {
							Util::removeLogoFile(PATH_PICTURE."company_", $_SESSION[SE_ORGID]) ;
						}
                    }
                  
					//if (move_uploaded_file($_FILES['fileLogo']['tmp_name'], $newfile)) {
					if (copy($_FILES[$fn]['tmp_name'], $newfile)) {
						$imgUploaded = true ;
						//$tmpfile = PATH_PICTURE . "/" . $_SESSION[SE_ORGID] . "/" . basename($_FILES[$fn]['tmp_name']) ;
						//copy($newfile,$tmpfile) ;
						$err = "" ;
					} else {
						$err = "Error in processing uploaded file." ;
					}
					unlink($_FILES[$fn]['tmp_name']);
				} else {
					$err = "Unsupported upload file type." ;
				}
			} else {
				$err = "Upload file over limit. Upload file size must be less than 200KB." ;
			}
		} else {
			$err = "Missing upload file.";
		}
	} else {
		$err = "No upload file found." ;
	}
	if($imgUploaded){
        if ($coyid != 0){
			log::write(Status::Ok . "|" . "picture/company_" . $coyid . "/" . basename($newfile));
            echo Status::Ok . "|" . "picture/company_" . $coyid . "/" . basename($newfile);
        } else{
			if($image_type == "PAYNOW") {
				log::write(Status::Ok . "|" . "picture/" . (!isset($_SESSION[SE_BU]) ? "TEMP" : $_SESSION[SE_BU]). "/" . basename($newfile));
				echo Status::Ok . "|" . "picture/" . (!isset($_SESSION[SE_BU]) ? "TEMP" : $_SESSION[SE_BU]) . "/" . basename($newfile);
			}else{
				log::write(Status::Ok . "|" . "picture/company_" . $_SESSION[SE_ORGID] . "/" . basename($newfile));
				echo Status::Ok . "|" . "picture/company_" . $_SESSION[SE_ORGID] . "/" . basename($newfile);
			}
			
        }
		//echo Status::Ok . "|" . $newfile ;
	}else{
		echo Status::Error . "|" . $err ;
	}

?>