<?php

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

class themeInstaller extends functions { 
	public function __construct ($file, $install) {
		$this->path = BASE_URL."themes";
		$this->tempPath = BASE_URL."temp";
		$this->file = $file;
		if($install == true) {
			return $this->install($this->file);		
		}
		else {
			return $this->test($this->file);
		}
	}
	public function formatFileSize($fs) { 
		$bytes = array('KB', 'KB', 'MB', 'GB', 'TB'); 
		// values are always displayed in at least 1 kilobyte: 
		if ($fs <= 999) { 
			$fs = 1; 
		} 
		for ($i = 0; $fs > 999; $i++) { 
			$fs /= 1024; 
		} 
		return ceil($fs).$bytes[$i]; 
	}
	public function findexts () { 
		$filename = $this->file;
		$filename = strtolower($filename);
		$exts = explode(".", $filename);
		$n = count($exts)-1;
		$exts[$n] = ""; 
		$exts = implode($exts, ".");
		$exts = substr($exts, 0, -1);
		return trim($exts); 
	} 
	public function test ($file) {
		$zip = new ZipArchive;
		if ($zip->open(BASE_URL.'temp/'.$file) === TRUE) {
			$zipx = $this->findexts();
			if(!is_dir($this->tempPath."/".$zipx)) {
				mkdir($this->tempPath."/".$zipx);
			}
		    $zip->extractTo($this->tempPath."/".$zipx.$name);
		    $zip->close();
			if(file_exists(BASE_URL.$this->tempPath.'/'.$zipx.'/index.tpl')) {
//				return true;
			}
			else {
//				return $this->message('error', 'Oh no! File missing!', 'There isn\'t a index.tpl file in the root the zip file. This is usally caused by having the theme folder inside the zip file.');
			}
			unlink($this->tempPath."/".$zipx);
		}
		else {
		    return false;
		}
	}
	public function install ($file) {
		$zip = new ZipArchive;
		if ($zip->open(BASE_URL.'temp/'.$file.'.zip') === TRUE) {
			$zipx = $this->findexts();
			if(!is_dir($this->path."/".$zipx)) {
				mkdir($this->path."/".$zipx);
			}
		    $zip->extractTo($this->path."/".$zipx.$name);
		    $zip->close();
			unlink(BASE_URL.'temp/'.$file.'.zip');
			return true;
		}
		else {
		    return false;
		}
	}
}
?>
