<?php
	$fn = $_REQUEST['n'] ;
    
    $coyid = 0;
    if (isset($_REQUEST['id'])){
        $coyid = $_REQUEST['id'];
    }
         
	$imgUploaded = false ;
	$limitsize = 200 * 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 = 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);
                        }
                        $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 200KB. (test)" ;
			}
		} 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 ;
	}
	
?>