[2222] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare * |
---|
| 4 | * http://www.egroupware.org * |
---|
| 5 | * -------------------------------------------- * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 7 | * under the terms of the GNU General Public License as published by the * |
---|
| 8 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 9 | * option) any later version. * |
---|
| 10 | \**************************************************************************/ |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * The Factory frontend class. |
---|
| 14 | * This class controls which concrete Factory |
---|
| 15 | * will be used, depending on the current |
---|
| 16 | * 'security mode'. It lazy instantiates both |
---|
| 17 | * factories (process and module) when they are |
---|
| 18 | * required, and stores these objects. All the |
---|
| 19 | * accesses to factories are done through this class, |
---|
| 20 | * implementing a kind of Proxy design pattern. |
---|
| 21 | * This class depends on Security frontend class |
---|
| 22 | * to decide which factory to call. |
---|
| 23 | * |
---|
| 24 | * @package Factory |
---|
| 25 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 26 | * @author Pedro Eugênio Rocha - pedro.eugenio.rocha@gmail.com |
---|
| 27 | */ |
---|
| 28 | class Factory { |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * @var object $_unsecuredFactory Stores WorkflowFactory object. |
---|
| 33 | * @access private |
---|
| 34 | * @static |
---|
| 35 | */ |
---|
| 36 | private static $_unsecuredFactory = null; |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * @var object $_securedFactory Stores ProcessFactory object. |
---|
| 41 | * @access private |
---|
| 42 | * @static |
---|
| 43 | */ |
---|
| 44 | private static $_securedFactory = null; |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Constructor. Just disable direct instantiation. |
---|
| 49 | * |
---|
| 50 | * @access public |
---|
| 51 | * @return void |
---|
| 52 | * @static |
---|
| 53 | */ |
---|
| 54 | public function __construct() { |
---|
| 55 | throw new Exception("Oops! Static only class."); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | /** |
---|
[2249] | 60 | * Just forward this call. |
---|
[2222] | 61 | * |
---|
| 62 | * @access public |
---|
[2249] | 63 | * @return object |
---|
[2222] | 64 | * @static |
---|
| 65 | */ |
---|
[2233] | 66 | public static function &getInstance() { |
---|
[2222] | 67 | |
---|
[2249] | 68 | $args = func_get_args(); |
---|
| 69 | return self::_callMethod(__FUNCTION__, $args); |
---|
| 70 | } |
---|
[2222] | 71 | |
---|
| 72 | |
---|
[2249] | 73 | /** |
---|
| 74 | * Just forward this call. |
---|
| 75 | * |
---|
| 76 | * @access public |
---|
| 77 | * @return object |
---|
| 78 | * @static |
---|
| 79 | */ |
---|
| 80 | public static function &newInstance() { |
---|
[2222] | 81 | |
---|
[2249] | 82 | $args = func_get_args(); |
---|
| 83 | return self::_callMethod(__FUNCTION__, $args); |
---|
[2222] | 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | /** |
---|
[2249] | 88 | * Selecting the proper factory to call. This function |
---|
| 89 | * should never be called with a random $methodName. Allowed |
---|
| 90 | * values are 'getInstance' and 'newInstance'. |
---|
[2222] | 91 | * |
---|
[2291] | 92 | * @param string $methodName Name of the BaseFactory method to call. |
---|
| 93 | * @param array $args Parameters to class's constructor. |
---|
[2249] | 94 | * @access private |
---|
| 95 | * @return object |
---|
[2222] | 96 | * @static |
---|
| 97 | */ |
---|
[2249] | 98 | private static function &_callMethod($methodName, $args) { |
---|
[2233] | 99 | |
---|
[2249] | 100 | /* security off (module space) */ |
---|
| 101 | if (!Security::isEnabled()) { |
---|
[2233] | 102 | |
---|
[2249] | 103 | /* it must be instatiated */ |
---|
| 104 | if (is_null(self::$_unsecuredFactory)) |
---|
| 105 | self::$_unsecuredFactory = new WorkflowFactory(); |
---|
| 106 | |
---|
[2278] | 107 | $className = array_shift($args); |
---|
| 108 | return self::$_unsecuredFactory->$methodName($className, $args); |
---|
[2249] | 109 | } |
---|
| 110 | /* oops. we are in the process space (restricted). */ |
---|
| 111 | else { |
---|
| 112 | |
---|
| 113 | /* it must be instatiated */ |
---|
[2233] | 114 | if (is_null(self::$_securedFactory)) |
---|
| 115 | self::$_securedFactory = new ProcessFactory(); |
---|
| 116 | |
---|
[2278] | 117 | $className = array_shift($args); |
---|
| 118 | |
---|
[2249] | 119 | /** |
---|
| 120 | * If the class is not allowed, we must check who is trying |
---|
| 121 | * to instantiate it. If it's a module guy, let's allow him. |
---|
| 122 | * Throw up the exception otherwise. |
---|
| 123 | */ |
---|
| 124 | try { |
---|
[2278] | 125 | $obj = &self::$_securedFactory->$methodName($className, $args); |
---|
[2249] | 126 | } |
---|
[2233] | 127 | |
---|
[2249] | 128 | /** |
---|
| 129 | * We are erroneously catching any exceptions. We should catch only the 'class not allowed' |
---|
| 130 | * types of exceptions. To do so, a custom exception class must be defined. |
---|
| 131 | */ |
---|
| 132 | catch(Exception $e) { |
---|
[2233] | 133 | |
---|
[2249] | 134 | /** |
---|
| 135 | * Here we are using depth 2 in isSafeDir method, because we are on a private |
---|
| 136 | * method. Thus, we need to know if the "caller's caller's" function is on a |
---|
| 137 | * safe dir, instead of the direct caller's method. |
---|
| 138 | */ |
---|
| 139 | if (Security::isSafeDir(2)) |
---|
[2278] | 140 | $obj = &self::$_unsecuredFactory->$methodName($className, $args); |
---|
[2233] | 141 | |
---|
[2278] | 142 | /* naaasty one. take this! */ |
---|
[2249] | 143 | else |
---|
| 144 | throw($e); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | // finally |
---|
| 148 | return $obj; |
---|
[2233] | 149 | } |
---|
[2222] | 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | ?> |
---|