<?php

class Fonts extends File{

	private $file;
	
	function __construct(){
		$this->file = new File();
	}

	private function checkIsFontExists($d, $exclude_id=-1){
		
		// check is font name, original name, class or SWF filename exist
		$retObj = (object) array();
		$retObj->any = $retObj->fontName = $retObj->fontFace = $retObj->fileName = $retObj->clazz =  false;

		$fontsContent = Util::readFile(Config::get("private_system_fonts_path")."/fonts.txt");
		$fontsArr = DataSerializer::deserialize($fontsContent);
		
		if(!is_array($fontsArr) || count($fontsArr)==0){
			return $retObj;
		}
		
		for($i=0; $i<count($fontsArr); $i++){
		 	if($fontsArr[$i]->id_font!=$exclude_id){
				 $retObj->fontName = $retObj->fontName || ($fontsArr[$i]->fontName==$d->fontObj->fontName);
				 $retObj->fontFace = $retObj->fontFace || ($fontsArr[$i]->fontFace==$d->fontObj->fontFace);
				 $retObj->fileName = $retObj->fileName || ($fontsArr[$i]->fileName==$d->fontObj->fileName);
				 $retObj->clazz =  $retObj->clazz || ($fontsArr[$i]->clazz==$d->fontObj->clazz);
				 $retObj->any = ($retObj->any || $retObj->fontName || $retObj->fontFace || $retObj->fileName || $retObj->clazz); 
			 }
		}
		
		return $retObj;
		
	}
	
	/*
	 * update font definition
	 */
	public function updateFont($d){
		  	
	
		$fontExistObj = $this->checkIsFontExists($d, $d->oldFontObject->id_font);
		
		if($fontExistObj->any==true){
			Response::sendOKResponse((object) array("success"=>false, "data"=>$fontExistObj));
		}
		
		// copy font file (swf) to users fonts directory
		if(strpos($d->swfPath,":")!==false){
			$fileSourceObj = $this->file->checkPathAndGetPathElements($d->swfPath);
			$destPath = Config::get("private_system_fonts_path")."/swf/".$d->fontObj->fileName;
			$success = $this->file->copy($fileSourceObj->realPath,$destPath);
			if($success<0){
		 		Response::sendErrorResponse($this->file->getErrorID($success), $this->file->getErrorMsg($success));
			}
		}
	
		// save font info
		
		$fontsFilepath = Config::get("private_system_fonts_path")."/fonts.txt";
		$rows_affected = JSONUtil::updateRow($fontsFilepath, $d->fontObj, $d->oldFontObject->id_font, "id_font");

		
		Response::sendOKResponse((object) array("success"=>($rows_affected>0)));
	
	}
	
	
	/*
	 * add font definition
	 */
	public function addFont($d){
		  	
		
		$fontExistObj = $this->checkIsFontExists($d);
		
		if($fontExistObj->any==true){
			Response::sendOKResponse((object) array("success"=>false, "data"=>$fontExistObj));
		}
		
		// copy font file (swf) to users fonts directory
		$fileSourceObj = $this->file->checkPathAndGetPathElements($d->swfPath);
		$destPath = Config::get("private_system_fonts_path")."/swf/".$d->fontObj->fileName;
		$success = $this->file->copy($fileSourceObj->realPath,$destPath);
		if($success<0){
	 		Response::sendErrorResponse($this->file->getErrorID($success), $this->file->getErrorMsg($success));
		}
	
		// save font info
		
		$fontsFilepath = Config::get("private_system_fonts_path")."/fonts.txt";
		JSONUtil::insertNewRow($fontsFilepath, $d->fontObj, "id_font");
		
		/*
		$fontsContentStr = Util::readFile($fontsContentFile);
	 	if($fontsContentStr===false){
	 		Response::sendResponse(false, null, "fonts", "fonts_list_doesnot_exist");
	 	}
	 	
		$fontsContentObj = DataSerializer::deserialize($fontsContentStr);
	 	$fontsContentObj[] = $d->fontObj;
	 	
	 	if(!Util::writeFile($fontsContentFile,DataSerializer::serialize($fontsContentObj))){
	 	 	Response::sendResponse(false, null, "fonts", "user_fonts_list_cannot_write");
	 	}
 	 	*/
		
		Response::sendOKResponse((object) array("success"=>true));
	
	}
	
	public function getFonts(){
	 	
	 	$fontsContent = Util::readFile(Config::get("private_system_fonts_path")."/fonts.txt");
	 	if($fontsContent!==false){
	 		Response::sendOKResponse(DataSerializer::deserialize($fontsContent));
	 	}
		else{
			Response::sendResponse(false, null, "fonts", "fonts_list_doesnot_exist");
		}
		
	}
	
	public function getFont($a){
		
		$notUnderPath = Config::get("private_system_fonts_path")."/swf";
		$realPath = Config::get("private_system_fonts_path")."/swf/".$a[0];
		
		if(FileSecurity::getInstance()->systemPathCheck($realPath, array("swf"), $notUnderPath)){
			header("Content-type: application/x-shockwave-flash");
			header("Content-Disposition: filename=". $this->file->getFileName($realPath));
			readfile($realPath);
		}

		exit;
		
	} 
	
	public function deleteFont($d){
	 
		global $config, $ds, $auth;
	
		$fontsFilepath = Config::get("private_system_fonts_path")."/fonts.txt";
		JSONUtil::deleteRow($fontsFilepath, $d->id_font, "id_font");
		
	
	/*	$fontsContent = Util::readFile(Config::get("private_system_fonts_path")."/fonts.txt");
	 	if($fontsContent===false){
	 		Response::sendResponse(false, null, "fonts", "fonts_list_doesnot_exist");
	 	}
		$fontsArray = DataSerializer::deserialize($fontsContent);
		
		$index = Util::getArrayItemIndexByItemPropertyValue($sourceArray, "fontName", $d->fontName);
		array_splice($fontsArray,$index, 1);
		
		Util::writeFile(Config::get("private_system_path")."/fonts.txt", DataSerializer::serialize($fontsArray));
		Util::deleteFile(Config::get("private_system_path")."/swf/".$d->fileName);*/
		
		Response::sendOKResponse((object) array("success"=>true));
		
	}
 
}

?>