<?php

include_once("thumbs/simplethumbmaker.class.php");
	
class Wallpapers {

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

	function saveWallpaper($d){
 	 
		$dpFile = Config::get("private_system_path")."/config/users/".User::GetInstance()->userInfo->username."_dp.txt";
		$dpSrt = Util::readFile($dpFile);
	 	if($dpSrt===false){
	 		Response::sendResponse(false, null, "wallpaper", "user_wallpaper_doesnot_exist");
	 	}
	 	
		$dp = DataSerializer::deserialize($dpSrt);
	 	$dp->wallpaper = $d;
	 	
	 	if(!Util::writeFile($dpFile,DataSerializer::serialize($dp))){
	 	 	Response::sendResponse(false, null, "wallpaper", "user_wallpaper_cannot_write");
	 	}
 	 	
		Response::sendOKResponse((object) array("success"=>true));
	}	 
	function deleteWallpaper($d){
	 	
		$fileObj = $this->file->checkPathAndGetPathElements("private:/System/Wallpapers/".basename($d->wallpaperFilename) );
		
		$response =	$this->file->deleteFile($fileObj->realPath);
		if($response <0){
	 		Response::sendErrorResponse($this->file->getErrorID($response), $this->file->getErrorMsg($response));
		}
		
		//delete thumb if exista
		$fileObj = $this->file->checkPathAndGetPathElements("private:/System/Wallpapers/Thumbs/".basename($d->wallpaperFilename) );
		$this->file->deleteFile($fileObj->realPath);
		
		
		Response::sendOKResponse($d->wallpaperFilename);
		
	}
	function addWallpapers($d){

		$retArray = array();
		for($i=0;$i<count($d->wallpapersPaths);$i++){
			 $fileSourceObj = $this->file->checkPathAndGetPathElements($d->wallpapersPaths[$i]);
			 $filename = substr($fileSourceObj->realPath, strrpos($fileSourceObj->realPath, '/') + 1);
			 $fileDestObj = $this->file->checkPathAndGetPathElements("private:/System/Wallpapers/".$filename);
			 if($this->file->copy($fileSourceObj->realPath,	 $fileDestObj->realPath)==true){
			  	$retArray[]=$filename;
			 }
		}
		Response::sendOKResponse($retArray);
	}

	function getAllWallpapersImages(){

	 	$fileObj = $this->file->checkPathAndGetPathElements("private:/System/Wallpapers");
	 	
	 	$this->file->readOnlyFiles = true;
		
	 	$FaF = $this->file->getFoldersAndFiles(	$fileObj->realPath);
	 	
	 	if($FaF<0){
	 		Response::sendErrorResponse($this->file->getErrorID($FaF), $this->file->getErrorMsg($FaF));
		}
		Response::sendOKResponse($FaF);
	}

	function getWallpaperImage($a){
		 
	//	$ud = new UD();
	//	$file = new File();
		header("Content-type: image/".$this->file->getExtension($wallObj->realPath));
	
		$wallObj = $this->file->checkPathAndGetPathElements("private:/System/Wallpapers/".basename($a[0]));
		 
		readfile($wallObj->realPath);
		 
		exit;
	}

	function getWallpaperThumbnail($a){
	
		header("Content-type: image/".$this->file->getExtension($wallObj->realPath));

		$wallObj = 	$this->file->checkPathAndGetPathElements("private:/System/Wallpapers/".basename($a[0]));
		$thumbObj = $this->file->checkPathAndGetPathElements("private:/System/Wallpapers/Thumbs/".basename($a[0]));

		if(is_file($thumbObj->realPath)){
			readfile($thumbObj->realPath);
			exit;
		}
		
		$this->simplethumb->setMaxSize(320, 200);
	
		if($this->simplethumb->createThumb($wallObj->realPath, $thumbObj->realPath)==1){
			readfile($thumbObj->realPath);
		}else{
			readfile($wallObj->realPath);
		}
			
		exit;
	}
}

?>