SharedObjectHeaderFile.php 2.82 KB
<?php
/**
 * @class SharedObjectHeaderFile
 * @version 
 * 
 */

class SharedObjectHeaderFile {
	private $doc = null;
	private $filePath = "";
	
	function __construct($filePath) {
		if (!file_exists($filePath))
			$this->createFile($filePath);
		$this->loadFile($filePath);
		$this->filePath = $filePath;
	}
	
	public function getInfo() {
		return array(
			"name"        => $this->getName(),
			"description" => $this->getDescription(),
			"sharedBy"    => $this->getSharedBy(),
			"sharedDate"  => $this->getSharedDate()
		);
	}
	
	public function getName() {
		return $this->getOneInfo("name");
	}
	
	public function setName($name) {
		$this->setOneInfo("name", $name);
	}
	
	public function getDescription() {
		return $this->getOneInfo("description");
	}
	
	public function setDescription($description) {
		$this->setOneInfo("description", $description);
	}
	
	public function getSharedBy() {
		return $this->getOneInfo("sharedBy");
	}
	
	public function setSharedBy($sharedBy) {
		$this->setOneInfo("sharedBy", $sharedBy);
	}
	
	public function getSharedDate() {
		return $this->getOneInfo("sharedDate");
	}
	
	public function setSharedDate($sharedDate) {
		$this->setOneInfo("sharedDate", $sharedDate);
	}
	
	private function createFile($filePath) {
		$this->doc = new DOMDocument("1.0", "UTF-8");
		$this->doc->preserveWhiteSpace = false;
		$this->doc->formatOutput = true;
		
		$rootNode = $this->doc->createElement("shared-header");
		$this->doc->appendChild($rootNode);
		
		$nameNode = $this->doc->createElement("name");
		$rootNode->appendChild($nameNode);
		
		$descriptionNode = $this->doc->createElement("description");
		$rootNode->appendChild($descriptionNode);
		
		$sharedByNode = $this->doc->createElement("sharedBy");
		$rootNode->appendChild($sharedByNode);
		
		$sharedDateNode = $this->doc->createElement("sharedDate");
		$rootNode->appendChild($sharedDateNode);
		
		$this->doc->save($filePath);
		
		chgrp($filePath, APACHE_USER);
		chmod($filePath, 0775);
	}
	
	private function loadFile($filePath) {
		$this->doc = new DOMDocument("1.0", "UTF-8");
		$this->doc->preserveWhiteSpace = false;
		$this->doc->formatOutput = true;
		$this->doc->load($filePath);
	}
	
	private function getOneInfo($infoName) {
		if (!isset($this->doc))
			return "";
		
		$rootNode = $this->doc->documentElement;
		if (!isset($rootNode))
			return "";
		
		$infoNodes = $rootNode->getElementsByTagName($infoName);
		if (count($infoNodes) == 0)
			return "";
		
		return $infoNodes->item(0)->nodeValue;
	}
	
	private function setOneInfo($infoName, $infoValue) {
		if (!isset($this->doc))
			return;
	
		$rootNode = $this->doc->documentElement;
		if (!isset($rootNode))
			return "";
	
		$infoNodes = $rootNode->getElementsByTagName($infoName);
		if (count($infoNodes) == 0)
			return "";
	
		$infoNodes->item(0)->nodeValue = $infoValue;
		
		$this->doc->save($this->filePath);
	}
}

?>