WeakWiki


modules\forum\forum.support.include.php

WeakWiki (WeakWiki Forum module support library: forum.support.include.php) Copyright (C) 2010 Alexander Lang 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 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . Project Home: http://weakwiki.robnet.wmweb.at/ Contact: robbiblubber@robnet.wmweb.at



/********************************************************************************
WeakWiki (WeakWiki Forum module support library: forum.support.include.php)
Copyright (C) 2010 Alexander Lang

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 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this
program; if not, see .

Project Home: http://weakwiki.robnet.wmweb.at/
Contact:      robbiblubber@robnet.wmweb.at
********************************************************************************/



require_once('./modules/forum/forum.config.php');

define('_SEP',        '/' . chr(27) . chr(27));
define('_VALUE_SEP',  ':' . chr(27) . chr(27));
define('_DATA_SEP',   '.' . chr(27) . chr(27));

define('_FO_DEFAULT_PERMISSIONS', 'unknown=r;reader=rw;editor=rw;admin=rwa;');



/** This class represents a topic.												*/
class FoTopic
{
	//////////////////////////////////////////////////////////////////////////////
	// protected members														//
	//////////////////////////////////////////////////////////////////////////////
	
	/** Sub-topics of this topic.												*/
	protected $topics = Array();
	
	/** Topic objects for sub-topics.											*/
	protected $topicsv = null;
	
	/** Threads of this topic.													*/
	protected $threads = Array();
	
	/** Thread objects.															*/
	protected $threadsv = null;
	
	/** Privileges for this topic.												*/
	protected $privileges = _FO_DEFAULT_PERMISSIONS;
	
	/** This topic's title.														*/
	protected $title = "";
	
	/** This topic's text.														*/
	protected $text = "";
	
	/** File name.																*/
	protected $fileName = "";
	
	/** Parent topic.															*/
	protected $parent = null;
	
	/** Permissions.															*/
	protected $perm = null;
	
	
	//////////////////////////////////////////////////////////////////////////////
	// constructor																//
	//////////////////////////////////////////////////////////////////////////////
	
	/** Creates a new instance of the class.									*/
	public function __construct($file = '')
	{
		if($file != '')
		{
			$this->fileName = $file;
			$this->load();
		}
	}
	
	
	
	//////////////////////////////////////////////////////////////////////////////
	// public methods															//
	//////////////////////////////////////////////////////////////////////////////
	
	/** Gets the topic's title.
		@return				Returns the title.									*/
	public function getTitle()
	{
		return $this->title;
	}
	
	
	/** Sets the topic's title.
		@param $value		Title.												*/
	public function setTitle($value)
	{
		$this->title = $value;
	}
	
	
	/** Gets the topic's text.
		@return				Returns the text.									*/
	public function getText()
	{
		return $this->text;
	}
	
	
	/** Sets the topic's text.
		@param $value		Text.												*/
	public function setText($value)
	{
		$this->text = $value;
	}
	
	
	/** Gets the topic's privileges.
		@return				Returns the privileges.								*/
	public function getPrivileges()
	{
		return $this->privileges;
	}
	
	
	/** Sets the topic's privileges.
		@param $value		Privileges.											*/
	public function setPrivileges($value)
	{
		$this->privileges = $value;
	}
	
	
	/** Gets the topic's file name.
		@return				Returns the file name.								*/
	public function getFileName()
	{
		return $this->fileName;
	}
	
	
	/** Returns the topics in this topic.
		@return				Topics.												*/
	public function getTopics()
	{
		if($this->topicsv == null)
		{
			$this->topicsv = Array();
			
			foreach($this->topics as $i)
			{
				$f = new FoTopic($i[1]);
				
				array_push($this->topicsv, new FoTopic($i[1]));
			}
		}
		
		return $this->topicsv;
	}
	
	
	/** Returns the threads in this topic.
		@return				Threads.											*/
	public function getThreads()
	{
		if($this->threadsv == null)
		{
			$this->threadsv = Array();
			
			foreach($threads as $i)
			{
				//array_push($this->topicsv, new FoThread($i));
			}
		}
		
		return $this->threadv;
	}
	
	
	/** Serializes the object into a string.									*/
	public function serialize()
	{
		$rval = $this->title . _SEP . $this->text . _SEP;
		
		if($this->parent != null)
		{
			$rval .= $this->parent->getFileName();
		}
		$rval .= _SEP;
		
		for($i = 0; $i < count($this->topics); $i++)
		{
			if($i > 0) { $rval .=  _VALUE_SEP; }
			$rval .= $this->topics[$i][0] . _DATA_SEP . $this->topics[$i][1]; 
		}		
		$rval .= _SEP;
		
		for($i = 0; $i < count($this->threads); $i++)
		{
			if($i > 0) { $rval .=  _VALUE_SEP; }
			$rval .= $this->topics[$i][0] . _DATA_SEP . $this->topics[$i][1]; 
		}
		$rval .= _SEP;
		
		return $rval;
	}
		
	
	/** Serializes the object into a topic file.								*/
	public function save()
	{
		if($this->fileName == "")
		{
			$i = 0;
			while(file_exists(_FORUM_PATH . $i . '.' . strtolower($this->title) . '.thread')) { $i++; }
			
			$this->fileName = $i . '.' . strtolower($this->title) . '.topic';
		}
		
		file_put_contents(_FORUM_PATH . $this->fileName, $this->serialize());
	}
	
	
	/** Checks if an operation is allowed for the current user.
		@param $op		Operation string:
							OP_READ, OP_WRITE, OP_ADMIN.
		@return			TRUE if operation is permittet, otherwise FALSE.		*/
	public function request($op)
	{
		$u = currentUser();
		$this->readPermissions();
		
		if($u == null)	{ return (strpos($this->perm["." . UL_UNKNOWN], $op) !== false); }
		
		return (strpos($this->perm["." . $u->getLevel()], $op) !== false);
	}
	
	
	/** Returns if the current user is allowed to read the document.
		@return			TRUE if the document may be read, otherwise FALSE.		*/
	public function readable()
	{
		return $this->request(OP_READ);
	}
	
	
	/** Returns if the current user is allowed to edit the document.
		@return			TRUE if the document may be edited, otherwise FALSE.	*/
	public function editable()
	{
		return $this->request(OP_WRITE);
	}
	
	
	/** Gets the permissions for a user group.
		@param $ul		User level:
							UL_UNKNOWN, UL_READ, UL_EDIT, UL_ADMIN.
		@return			Permission set.											*/
	public function getPermissions($ul)
	{
		$this->readPermissions();
		
		return new DocumentPermissions($this->perm["." . $ul]);
	}
	
	
	/** Adds a subtopic to the topic.
		@param $topic		Topic.												*/
	public function addTopic($topic)
	{
		array_push($this->topics, Array($topic->getTitle(), $topic->getFileName()));
		$this->topicsv = null;
	}
	
	
	/** Returns the parent topic of this topic.
		@return				Parent topic.										*/
	public function getParent()
	{
		return $this->parent;
	}
	
	
	/** Sets the parent topic of this topic.
		@param value		Parent topic.										*/
	public function setParent($value)
	{
		$this->parent = $value;
	}
	
	
	
	//////////////////////////////////////////////////////////////////////////////
	// protected methods														//
	//////////////////////////////////////////////////////////////////////////////
	
	/** Loads topic from file.													*/
	protected function load()
	{	
		$c = file_get_contents(_FORUM_PATH . $this->fileName);
		
		$v = explode(_SEP, $c);
		
		$this->title = $v[0];
		$this->text  = $v[1];
		
		if($v[2] != '')
		{
			$this->parent = new FoTopic($v[2]);
		}
		
		foreach(explode(_VALUE_SEP, $v[3]) as $i)
		{
			if(strlen(trim($i)) > 2) { array_push($this->topics, explode(_DATA_SEP, $i)); }
		}
		
		foreach(explode(_VALUE_SEP, $v[4]) as $i)
		{
			array_push($this->threads, explode(_DATA_SEP, $i));
		}
	}
	
	
	/** Evaluates the topic's permissions.										*/
	protected function readPermissions()
	{
		if($this->perm != null) return;
		
		$this->perm = Array
		(
			"." . UL_UNKNOWN => '',
			"." . UL_READ    => '',
			"." . UL_EDIT    => '',
			"." . UL_ADMIN   => ''
		);
		
		
		$i = str_replace("->", "=", str_replace(",", ";", str_replace(" ", "", $this->privileges)));
		
		foreach(explode(";", $i) as $j)
		{
			$c = explode("=", strtolower($j));
			
			switch(substr($c[0], 0, 1))
			{
				case "u":
					$this->perm["." . UL_UNKNOWN] = $c[1];
					break;
				case "r":
					$this->perm["." . UL_READ] = $c[1];
					break;
				case "e":
					$this->perm["." . UL_EDIT] = $c[1];
					break;
				case "a":
					$this->perm["." . UL_ADMIN] = $c[1];
					break;
			}
		}
	}
}



/** Returns the start topic.
	@return					Start topic.										*/
function getStartTopic()
{
	return new FoTopic(_FORUM_START);
}


/** Returns the topic.
	@return					Topic.												*/
function getTopic($file)
{
	return new FoTopic($title);
}

?>

WeakWiki