<?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 = 500 * 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 . "/" . $_SESSION[SE_ORGID] . "/paynowqr." . $ext; 
                    else
						$newfile = PATH_PICTURE . "/" . $_SESSION[SE_ORGID] . "/logo_" . time() . "." . $ext; 

                    //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 . "/company-" . $coyid . "/paynowqr." . $ext; 
							Util::removeCompanyPayNowQRFile(PATH_PICTURE, $coyid) ;
						} else {
							$newfile = PATH_PICTURE . "/company-" . $coyid . "/logo_" . time() . "." . $ext;
							Util::removeCompanyLogoFile(PATH_PICTURE, $coyid) ;
						}
                    } else {
                        Util::removeLogoFile(PATH_PICTURE, $_SESSION[SE_ORGID]) ;
                    }
                    
					if (move_uploaded_file($_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." ;
					}
				} else {
					$err = "Unsupported upload file type." ;
				}
			} else {
				$err = "Upload file over limit. Upload file size must be less than 50KB." ;
			}
		} else {
			$err = "Missing upload file.";
		}
	} else {
		$err = "No upload file found." ;
	}
	if($imgUploaded){
        if ($coyid != 0){
            echo Status::Ok . "|" . "picture/company-" . $coyid . "/" . basename($newfile);
        } else{
            echo Status::Ok . "|" . "picture/" . $_SESSION[SE_ORGID] . "/" . basename($newfile);
        }
		//echo Status::Ok . "|" . $newfile ;
	}else{
		echo Status::Error . "|" . $err ;
	}
	
?>