Index: sandbox/workflow/branches/609/lib/BaseFactory.php
===================================================================
--- sandbox/workflow/branches/609/lib/BaseFactory.php	(revision 2206)
+++ sandbox/workflow/branches/609/lib/BaseFactory.php	(revision 2206)
@@ -0,0 +1,160 @@
+<?php
+
+
+/**
+ * Base Factory abstract class.
+ * This class implements all the common behaviour of
+ * it's specialized classes (ProcessFactory and WorkflowFactory).
+ *
+ * @author Pedro EugÃªnio Rocha
+ * @package Factory
+ * @abstract
+ */
+abstract class BaseFactory {
+
+	/**
+	 * @var array Store what classes we can instantiate.
+	 * @access private
+	 */
+	private $_fileInfo;
+
+
+	/**
+	 * Registers classes into '$_fileInfo' private
+	 * atribute.
+	 *
+	 * @todo Maybe here we should receive more parameters...
+	 * @access protected
+	 * @return void
+	 */
+	protected final function registerFileInfo($className, $fileName, $relativePath) {
+
+		/* default is to override */
+		$this->_fileInfo[$className] = array(	'filename' => $fileName,
+												'path' => $relativePath,
+												'instance' => null);
+	}
+
+
+	/**
+	 * Here we should do all the factory stuff.
+	 * The classes instatiated here will be stored
+	 * into our private object cache. If there is no
+	 * object of the given type into the cache, 'newInstance'
+	 * will be called.
+	 *
+	 * @access public
+	 * @return object
+	 */
+	public function &getInstance(){
+
+		/* at least the class name */
+		if (func_num_args() <= 0)
+			return null;
+
+		$args = func_get_args();
+
+		/* recovering class data */
+		if ($entry = $this->_getEntry($args[0])) {
+
+			if (is_null($entry['instance'])) {
+
+				/* must instantiate it */
+                if (($obj = call_user_func_array(array($this, "newInstance"), $args)) == null)
+					throw new Exception("Unable to instantiate '".$args[0]."'");
+
+				/* saving the object reference */
+				$this->_setEntryInstance($args[0], $obj);
+				return $obj;
+			}
+			return $entry['instance'];
+
+		}
+		/* class not allowed */
+		else
+			throw new Exception("You are not allowed to instantiate class '".$args[0]."'.");
+	}
+
+	/**
+	 * Instantiate classes.
+	 *
+	 * @todo I think that we don't need to use the reflection
+	 *		 class here. Future work...
+	 * @access public
+	 * @return object
+	 */
+	public function &newInstance(){
+
+		/* at least the class name */
+		if (func_num_args() <= 0)
+			return null;
+
+		$args = func_get_args();
+
+		$className = array_shift($args);
+		if(!$this->_import($className))
+			return null;
+
+		/* I dont know.. maybe we could do better here.. */
+		$reflectionObj = new ReflectionClass($className);
+
+		if (count($args) == 0)
+			return $reflectionObj->newInstance();
+		return $reflectionObj->newInstanceArgs($args);
+	}
+
+
+	/**
+	 * Private stuff. Handles the cache and information
+	 * '$_fileInfo' array.
+	 *
+	 * @access private
+	 * @return array
+	 */
+	private function _getEntry($className){
+		return $this->_fileInfo[$className];
+	}
+
+
+	/**
+	 * Stores the given object into the internal cache
+	 * for upcoming requests.
+	 *
+	 * @access private
+	 * @return boolean
+	 */
+	private function _setEntryInstance($className, $obj){
+
+		if (is_array($this->_fileInfo[$className])) {
+			$this->_fileInfo[$className]['instance'] = $obj;
+			return true;
+		}
+		return false;
+	}
+
+
+	/**
+	 * Including (requiring_once ;P ) the file(s) itself. Ideally,
+	 * it could be the only place to include files.
+	 *
+	 * @access private
+	 * @return boolean
+	 */
+	private function _import($className){
+
+		/* not found */
+		if (!($entry = $this->_getEntry($className)))
+			throw new Exception('You are not allowed to instantiate '.$className.' class.');
+
+		$fullPath = WF_SERVER_ROOT . $entry['path'] . SEP . $entry['filename'];
+
+		/* file not found */
+		if (!file_exists($fullPath))
+			throw new Exception("File '".$fullPath."' not found.");
+
+		/* including file */
+		require_once $fullPath;
+		return true;
+	}
+}
+?>
Index: sandbox/workflow/branches/609/lib/WorkflowFactory.php
===================================================================
--- sandbox/workflow/branches/609/lib/WorkflowFactory.php	(revision 2206)
+++ sandbox/workflow/branches/609/lib/WorkflowFactory.php	(revision 2206)
@@ -0,0 +1,36 @@
+<?php
+
+class WorkflowFactory extends BaseFactory {
+
+
+	/**
+	 * @var boolean $_instantiated Attribute that stores whether this
+	 *		class was instantiated or not. This is used to limit the
+	 * 		instantiation of this class.
+	 * @access private
+	 * @static
+	 */
+	private static $_instantiated = false;
+
+
+	/**
+	 * Construct the class. This function will inform which classes
+	 * you will be able to instantiate and where to find it.
+	 * This function implements a kind of singleton design pattern too.
+	 * @access public
+	 */
+	public function __construct() {
+
+		/* don't let the user to instantiate this class more than once. */
+		if (self::$_instantiated)
+			throw new Exception("You can't instantiate this class again.");
+
+		/* registering allowed classes */
+		$this->registerFileInfo('WorkflowObjects', 'class.WorkflowObjects.inc.php', 'inc');
+
+		/* ok. no more instances of this class.. */
+		self::$_instantiated = true;
+	}
+}
+
+?>
Index: sandbox/workflow/branches/609/lib/ProcessFactory.php
===================================================================
--- sandbox/workflow/branches/609/lib/ProcessFactory.php	(revision 2206)
+++ sandbox/workflow/branches/609/lib/ProcessFactory.php	(revision 2206)
@@ -0,0 +1,35 @@
+<?php
+
+class ProcessFactory extends BaseFactory {
+
+
+	/**
+	 * @var boolean $_instantiated Attribute that stores whether this
+	 *		class was instantiated or not. This is used to limit the
+	 * 		instantiation of this class.
+	 * @access private
+	 * @static
+	 */
+	private static $_instantiated = false;
+
+	/**
+	 * Construct the class. This function will inform which classes
+	 * you will be able to instantiate and where to find it.
+	 * This function implements a kind of singleton design pattern too.
+	 * @access public
+	 */
+	public function __construct() {
+
+		/* don't let the user to instantiate this class more than once. */
+		if (self::$_instantiated)
+			throw new Exception("You can't instantiate this class again.");
+
+		/* registering allowed classes */
+		//$this->registerFileInfo('WorkflowObjects', 'class.WorkflowObjects.inc.php', 'inc');
+
+		/* ok. no more instances of this class.. */
+		self::$_instantiated = true;
+	}
+}
+
+?>
Index: sandbox/workflow/branches/609/lib/Factory.php
===================================================================
--- sandbox/workflow/branches/609/lib/Factory.php	(revision 2206)
+++ sandbox/workflow/branches/609/lib/Factory.php	(revision 2206)
@@ -0,0 +1,46 @@
+<?php
+
+class Factory {
+
+	private static $_unsecuredFactory = null;
+
+	private static $_securedFactory = null;
+
+
+	public function __construct() {
+		throw new Exception("Oops! Static only class.");
+	}
+
+
+	public static function getInstance() {
+
+		/* oops. we are in the process space. */
+		if (Security::isEnabled()) {
+
+			/* must instatiate it */
+			if (is_null(self::$_securedFactory))
+				self::$_securedFactory = new ProcessFactory();
+
+			$args = func_get_args();
+			call_user_func_array(array(self::$_securedFactory, "getInstance"), $args);
+
+		}
+		/* regular module space */
+		else {
+
+			/* must instatiate it */
+			if (is_null(self::$_unsecuredFactory))
+				self::$_unsecuredFactory = new WorkflowFactory();
+
+			$args = func_get_args();
+			call_user_func_array(array(self::$_unsecuredFactory, "getInstance"), $args);
+		}
+
+	}
+
+	public static function newInstance() {
+
+	}
+}
+
+?>
Index: sandbox/workflow/branches/609/lib/Security.php
===================================================================
--- sandbox/workflow/branches/609/lib/Security.php	(revision 2206)
+++ sandbox/workflow/branches/609/lib/Security.php	(revision 2206)
@@ -0,0 +1,93 @@
+<?php
+
+/**
+ * Security class for Workflow module.
+ *
+ * @author Pedro EugÃªnio Rocha
+ * @package Security
+ * @static
+ */
+class Security {
+
+	/**
+	 * @var boolean $_protection Stores the current security mode.
+	 * @access private
+	 * @static
+	 */
+	private static $_protection = false;
+
+
+	/**
+	 * Disallow the instantiation of this class.
+	 * @access public
+	 * @return void
+	 */
+	public function __construct() {
+		throw new Exception("Oops! Static only class.");
+	}
+
+
+	/**
+	 * Returns the current security mode.
+	 * @access public
+	 * @return boolean
+	 */
+	public static function isEnabled() {
+		return self::$_protection;
+	}
+
+
+	/**
+	 * Change to secured mode.
+	 * @access public
+	 * @return boolean
+	 * @static
+	 */
+	public static function enable() {
+
+		if (self::_isAllowed())
+			self::$_protection = true;
+		else
+			throw new Exception('You are not allowed to change security mode.');
+		return true;
+	}
+
+	/**
+	 * Change to unsecured mode.
+	 * @access public
+	 * @return boolean
+	 * @static
+	 */
+	public static function disable() {
+
+		if (self::_isAllowed())
+			self::$_protection = false;
+		else
+			throw new Exception('You are not allowed to change security mode.');
+		return true;
+	}
+
+
+	/**
+	 * This function do all the security stuff.
+	 * Here we must define in which files we are able
+	 * to change the security mode.
+	 *
+	 * @access private
+	 * @return boolean
+	 * @static
+	 */
+	private static function _isAllowed() {
+		$backtrace = debug_backtrace();
+
+		/* TODO - These are not definitive validations */
+		/* $backtrace[1] specifies the imediate antecessor function */
+		$basedir = dirname($backtrace[1]['file']);
+
+		if ($basedir == dirname(__FILE__))
+			return true;
+		return false;
+	}
+}
+
+?>
Index: sandbox/workflow/branches/609/inc/common.inc.php
===================================================================
--- sandbox/workflow/branches/609/inc/common.inc.php	(revision 1484)
+++ sandbox/workflow/branches/609/inc/common.inc.php	(revision 2206)
@@ -1,5 +1,5 @@
 <?php
-if (file_exists(dirname(__FILE__) . '/../../header.session.inc.php')) {
-	require_once dirname(__FILE__) . '/../../header.session.inc.php';
+if (file_exists(PHPGW_SERVER_ROOT . '/header.session.inc.php')) {
+	require_once PHPGW_SERVER_ROOT . '/header.session.inc.php';
 }
 
@@ -9,21 +9,24 @@
 setlocale(LC_CTYPE, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8');
 
+
 /* define o umask para a criação de arquivos por parte do Workflow */
 umask(007);
 
-require_once dirname(__FILE__) . '/class.factory.inc.php';
 
-/**
- * Objeto que implementa uma Factory
- * @global object $GLOBALS['workflow']['factory']
- * @name $factory
- */
-$GLOBALS['workflow']['factory'] = new Factory();
+/* defining constants */
+define('WF_SERVER_ROOT', PHPGW_SERVER_ROOT.'/workflow/');
+define('WF_INC_ROOT', WF_SERVER_ROOT.'/inc/');
+define('WF_LIB_ROOT', WF_SERVER_ROOT.'/lib/');
 
-/**
- * Objeto que cria objetos voltados exclusivamente para o módulo Workflow
- * @global object $GLOBALS['workflow']['workflowObjects']
- * @name $workflowObjects
- */
-$GLOBALS['workflow']['workflowObjects'] = $GLOBALS['workflow']['factory']->newInstance('WorkflowObjects');
+
+/* including common classes */
+require_once WF_LIB_ROOT . 'Security.php';
+require_once WF_LIB_ROOT . 'Factory.php';
+require_once WF_LIB_ROOT . 'BaseFactory.php';
+require_once WF_LIB_ROOT . 'WorkflowFactory.php';
+require_once WF_LIB_ROOT . 'ProcessFactory.php';
+
+
+/* FIXME!! */
+$GLOBALS['workflow']['workflowObjects'] = Factory::getInstance('WorkflowObjects', 'asd', 123);
 ?>
