Location: PHPKode > projects > PhpMySphere > phpMySphere-devel/src/module/Server.php
<?php
/***************************************************************************
 *   Copyright (C) 2003 by Sebastian Hess                                  *
 *   hide@address.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
 
class Server {
	var $iniPath;
	var $SpherePort;
	var $SphereIP;
	var $rAdminUser;
	var $rAdminPass;
	var $Config;

	var $AccountPath;
	var $SavePath;
	function Server($p_iniPath,$miniReader=false) {
		$this->iniPath = $p_iniPath;
		$this->SpherePort = -1;
		/*
			Read the ini file
		*/
		$this->Config = array();
		$this->readINI($miniReader);
		/*
			Save the most important Data for quicker access
		*/
		$this->SpherePort = $this->Config["SPHERE"]["SERVPORT"];
		$this->SphereIP   = $this->Config["SPHERE"]["SERVIP"];

		$this->AccountPath = ($this->Config["SPHERE"]["ACCTFILES"]{0} == "/" || $this->Config["SPHERE"]["ACCTFILES"]{1} == ":") ? $this->Config["SPHERE"]["ACCTFILES"] : dirname($p_iniPath)."/".$this->Config["SPHERE"]["ACCTFILES"];
	}

	function readINI($miniReader=false) {

		/*
			Init the local Variables
		*/

		$handle = false;
		$lastCat = "";

		/*
			Open the handle for the sphere.ini,
			read only, NEVER touch it here
			Comments will be stripped out !
		*/
		$handle = fopen($this->iniPath,"r");


		/*
			The file doesn't exist or isn't readable
		*/
		if(!$handle)
			return false;

		/*
			Read out the file
		*/

		while (!feof($handle) && $handle) {

			/*
				Read one line and check for a [ at the beginning of it
				If one is founds, then add a new Entry in the Array
			*/
			$buffer = chop(fgets($handle, 4096));
			if($buffer{0} == "[") {
				/*
					We only need the [SPHERE] Part for the settings ?
				*/
				if($miniReader && strlen($lastCat)) {
					@fclose($handle);
					return true;
				}
				/*
					Save the Category
				*/
				$lastCat = strtoupper(substr($buffer,1,-1));
				continue;
			}

			/*
				Don't save the comments, who need's them if he got our
				tool ;)
			*/
			if($buffer{0} == "/" && $buffer{1} == "/")
				continue;
			/*
				Split the parameter and the value
			*/


			list($Parm,$Zuweisung) = split("=",$buffer);
			$Parm = strtoupper($Parm);

			/*
				Check is both the Name and the Value are avaible
				if not continue with next line
			*/
			if(empty($Parm) || empty($Zuweisung)) continue;

			$this->Config[$lastCat][$Parm] = $Zuweisung;
		}
		@fclose($handle);
		return true;
	}

	function resync() {

		$fp = $this->_connect2Server();

		if(!$fp)
			return false;

		fputs($fp,"R\r\n");
		fflush($fp);
		sleep(2);
		fputs($fp,"R\r\n");
		fflush($fp);
		fclose($fp);
		return true;
	}

	function worldsave() {

		$fp = $this->_connect2Server();

		if(!$fp)
			return false;

		fputs($fp,"#\r\n");
		fflush($fp);
		sleep(2);
		fclose($fp);
		return true;
	}

	function _connect2Server() {
		if($this->SpherePort<0)
			return false;

		$fp = fsockopen($this->SphereIP,$this->SpherePort);

		if(!$fp)
			return false;

		fputs($fp," ");

		sleep(1);

		if(feof($fp))
			return false;

		if($_GLOBALS["sphereversion"] == 0) {
			/*
				Version for 55i
			*/
			fputs($fp,$this->rAdminPass."\r\n");
		} else {
			/*
				Version for the 99x Series
			*/
			fputs($fp,$this->rAdminUser."\r\n");
			fputs($fp,$this->rAdminPass."\r\n");
		}
		fflush($fp);
		if(feof($fp))
			return false;
		else
			return $fp;
	}
}

?>
Return current item: PhpMySphere