From cea5c69b7f298ecf943088e79d04080a1b80b940 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Mon, 2 May 2016 14:41:48 +0000 Subject: [PATCH] move CreateObject and ExecMethod into new api --- api/src/loader/common.php | 3 + api/src/loader/deprecated_factory.php | 195 ++++++++++++++++++++++ phpgwapi/inc/deprecated_functions.inc.php | 180 -------------------- setup/inc/functions.inc.php | 2 +- 4 files changed, 199 insertions(+), 181 deletions(-) create mode 100755 api/src/loader/deprecated_factory.php diff --git a/api/src/loader/common.php b/api/src/loader/common.php index 46aee5e66c..5bf0176f15 100755 --- a/api/src/loader/common.php +++ b/api/src/loader/common.php @@ -264,6 +264,9 @@ function check_load_extension($extension,$throw=false) return $loaded; } +// include deprecated factory methods: CreateObject, ExecMethod, ... +require_once EGW_SERVER_ROOT.'/api/src/loader/deprecated_factory.php'; + // include deprecated global functions, if phpgwapi is installed if (file_exists(EGW_SERVER_ROOT.'/phpgwapi')) { diff --git a/api/src/loader/deprecated_factory.php b/api/src/loader/deprecated_factory.php new file mode 100755 index 0000000000..673eaa57f7 --- /dev/null +++ b/api/src/loader/deprecated_factory.php @@ -0,0 +1,195 @@ +acl =& CreateObject('phpgwapi.acl'); + * + * @author RalfBecker@outdoor-training.de + * @param $classname name of class + * @param $p1,$p2,... class parameters (all optional) + * @deprecated use autoloadable class-names and new + * @return object reference to an object + */ +function CreateObject($class) +{ + list($appname,$classname) = explode('.',$class); + + if (!class_exists($classname)) + { + static $replace = array( + 'datetime' => 'egw_datetime', + 'uitimesheet' => 'timesheet_ui', + 'uiinfolog' => 'infolog_ui', + 'uiprojectmanager' => 'projectmanager_ui', + 'uiprojectelements' => 'projectmanager_elements_ui', + 'uiroles' => 'projectmanager_roles_ui', + 'uimilestones' => 'projectmanager_milestones_ui', + 'uipricelist' => 'projectmanager_pricelist_ui', + 'bowiki' => 'wiki_bo', + 'uicategories' => 'admin_categories', + 'defaultimap' => 'emailadmin_oldimap', + ); + if (!file_exists(EGW_INCLUDE_ROOT.'/'.$appname.'/inc/class.'.$classname.'.inc.php') || isset($replace[$classname])) + { + if (isset($replace[$classname])) + { + //throw new Exception(__METHOD__."('$class') old classname '$classname' used in menuaction=$_GET[menuaction]!"); + error_log(__METHOD__."('$class') old classname '$classname' used in menuaction=$_GET[menuaction]!"); + $classname = $replace[$classname]; + } + } + if (!file_exists($f=EGW_INCLUDE_ROOT.'/'.$appname.'/inc/class.'.$classname.'.inc.php')) + { + throw new Api\Exception\AssertionFailed(__FUNCTION__."($classname) file $f not found!"); + } + // this will stop php with a 500, if the class does not exist or there are errors in it (syntax error go into the error_log) + require_once(EGW_INCLUDE_ROOT.'/'.$appname.'/inc/class.'.$classname.'.inc.php'); + } + $args = func_get_args(); + switch(count($args)) + { + case 1: + $obj = new $classname; + break; + case 2: + $obj = new $classname($args[1]); + break; + case 3: + $obj = new $classname($args[1],$args[2]); + break; + case 4: + $obj = new $classname($args[1],$args[2],$args[3]); + break; + default: + $code = '$obj = new ' . $classname . '('; + foreach(array_keys($args) as $n) + { + if ($n) + { + $code .= ($n > 1 ? ',' : '') . '$args[' . $n . ']'; + } + } + $code .= ');'; + eval($code); + break; + } + if (!is_object($obj)) + { + echo "

CreateObject('$class'): Cant instanciate class!!!
\n".function_backtrace(1)."

\n"; + } + return $obj; +} + +/** + * Execute a function with multiple arguments + * We take object $GLOBALS[classname] from class if exists + * + * @param string app.class.method method to execute + * @example ExecObject('etemplates.so_sql.search',$criteria,$key_only,...); + * @deprecated use autoloadable class-names, instanciate and call method or use static methods + * @return mixed reference to returnvalue of the method + */ +function &ExecMethod2($acm) +{ + // class::method is php5.2.3+ + if (strpos($acm,'::') !== false && version_compare(PHP_VERSION,'5.2.3','<')) + { + list($class,$method) = explode('::',$acm); + $acm = array($class,$method); + } + if (!is_callable($acm)) + { + list(,$class,$method) = explode('.',$acm); + if (!is_object($obj =& $GLOBALS[$class])) + { + if (class_exists($class)) + { + $obj = new $class; + } + else + { + $obj = CreateObject($acm); + } + } + + if (!method_exists($obj,$method)) + { + echo "

".function_backtrace().": no methode '$method' in class '$class'

\n"; + return False; + } + $acm = array($obj,$method); + } + $args = func_get_args(); + unset($args[0]); + + return call_user_func_array($acm,$args); +} + +/** + * Execute a function, and load a class and include the class file if not done so already. + * + * This function is used to create an instance of a class, and if the class file has not been included it will do so. + * + * @author seek3r + * @param $method to execute + * @param $functionparam function param should be an array + * @param $loglevel developers choice of logging level + * @param $classparams params to be sent to the contructor + * @deprecated use autoloadable class-names, instanciate and call method or use static methods + * @return mixed returnvalue of method + */ +function ExecMethod($method, $functionparam = '_UNDEF_', $loglevel = 3, $classparams = '_UNDEF_') +{ + unset($loglevel); // not used + /* Need to make sure this is working against a single dimensional object */ + $partscount = count(explode('.',$method)) - 1; + + if (!is_callable($method) && $partscount == 2) + { + list($appname,$classname,$functionname) = explode(".", $method); + if (!is_object($GLOBALS[$classname])) + { + // please note: no reference assignment (=&) here, as $GLOBALS is a reference itself!!! + if ($classparams != '_UNDEF_' && ($classparams || $classparams != 'True')) + { + $GLOBALS[$classname] = CreateObject($appname.'.'.$classname, $classparams); + } + elseif (class_exists($classname)) + { + $GLOBALS[$classname] = new $classname; + } + else + { + $GLOBALS[$classname] = CreateObject($appname.'.'.$classname); + } + } + + if (!method_exists($GLOBALS[$classname],$functionname)) + { + error_log("ExecMethod('$method', ...) No methode '$functionname' in class '$classname'! ".function_backtrace()); + return false; + } + $method = array($GLOBALS[$classname],$functionname); + } + if (is_callable($method)) + { + return $functionparam != '_UNDEF_' ? call_user_func($method,$functionparam) : call_user_func($method); + } + error_log("ExecMethod('$method', ...) Error in parts! ".function_backtrace()); + return false; +} diff --git a/phpgwapi/inc/deprecated_functions.inc.php b/phpgwapi/inc/deprecated_functions.inc.php index 0f3837b14c..bf69cc90f2 100755 --- a/phpgwapi/inc/deprecated_functions.inc.php +++ b/phpgwapi/inc/deprecated_functions.inc.php @@ -11,186 +11,6 @@ * @version $Id$ */ -use EGroupware\Api; - -/** - * Load a class and include the class file if not done so already. - * - * This function is used to create an instance of a class, and if the class file has not been included it will do so. - * $GLOBALS['egw']->acl =& CreateObject('phpgwapi.acl'); - * - * @author RalfBecker@outdoor-training.de - * @param $classname name of class - * @param $p1,$p2,... class parameters (all optional) - * @return object reference to an object - */ -function CreateObject($class) -{ - list($appname,$classname) = explode('.',$class); - - if (!class_exists($classname)) - { - static $replace = array( - 'datetime' => 'egw_datetime', - 'uitimesheet' => 'timesheet_ui', - 'uiinfolog' => 'infolog_ui', - 'uiprojectmanager' => 'projectmanager_ui', - 'uiprojectelements' => 'projectmanager_elements_ui', - 'uiroles' => 'projectmanager_roles_ui', - 'uimilestones' => 'projectmanager_milestones_ui', - 'uipricelist' => 'projectmanager_pricelist_ui', - 'bowiki' => 'wiki_bo', - 'uicategories' => 'admin_categories', - 'defaultimap' => 'emailadmin_oldimap', - ); - if (!file_exists(EGW_INCLUDE_ROOT.'/'.$appname.'/inc/class.'.$classname.'.inc.php') || isset($replace[$classname])) - { - if (isset($replace[$classname])) - { - //throw new Exception(__METHOD__."('$class') old classname '$classname' used in menuaction=$_GET[menuaction]!"); - error_log(__METHOD__."('$class') old classname '$classname' used in menuaction=$_GET[menuaction]!"); - $classname = $replace[$classname]; - } - } - if (!file_exists($f=EGW_INCLUDE_ROOT.'/'.$appname.'/inc/class.'.$classname.'.inc.php')) - { - throw new Api\Exception\AssertionFailed(__FUNCTION__."($classname) file $f not found!"); - } - // this will stop php with a 500, if the class does not exist or there are errors in it (syntax error go into the error_log) - require_once(EGW_INCLUDE_ROOT.'/'.$appname.'/inc/class.'.$classname.'.inc.php'); - } - $args = func_get_args(); - switch(count($args)) - { - case 1: - $obj = new $classname; - break; - case 2: - $obj = new $classname($args[1]); - break; - case 3: - $obj = new $classname($args[1],$args[2]); - break; - case 4: - $obj = new $classname($args[1],$args[2],$args[3]); - break; - default: - $code = '$obj = new ' . $classname . '('; - foreach(array_keys($args) as $n) - { - if ($n) - { - $code .= ($n > 1 ? ',' : '') . '$args[' . $n . ']'; - } - } - $code .= ');'; - eval($code); - break; - } - if (!is_object($obj)) - { - echo "

CreateObject('$class'): Cant instanciate class!!!
\n".function_backtrace(1)."

\n"; - } - return $obj; -} - -/** - * Execute a function with multiple arguments - * We take object $GLOBALS[classname] from class if exists - * - * @param string app.class.method method to execute - * @example ExecObject('etemplates.so_sql.search',$criteria,$key_only,...); - * @return mixed reference to returnvalue of the method - */ -function &ExecMethod2($acm) -{ - // class::method is php5.2.3+ - if (strpos($acm,'::') !== false && version_compare(PHP_VERSION,'5.2.3','<')) - { - list($class,$method) = explode('::',$acm); - $acm = array($class,$method); - } - if (!is_callable($acm)) - { - list(,$class,$method) = explode('.',$acm); - if (!is_object($obj =& $GLOBALS[$class])) - { - if (class_exists($class)) - { - $obj = new $class; - } - else - { - $obj = CreateObject($acm); - } - } - - if (!method_exists($obj,$method)) - { - echo "

".function_backtrace().": no methode '$method' in class '$class'

\n"; - return False; - } - $acm = array($obj,$method); - } - $args = func_get_args(); - unset($args[0]); - - return call_user_func_array($acm,$args); -} - -/** - * Execute a function, and load a class and include the class file if not done so already. - * - * This function is used to create an instance of a class, and if the class file has not been included it will do so. - * - * @author seek3r - * @param $method to execute - * @param $functionparam function param should be an array - * @param $loglevel developers choice of logging level - * @param $classparams params to be sent to the contructor - * @return mixed returnvalue of method - */ -function ExecMethod($method, $functionparam = '_UNDEF_', $loglevel = 3, $classparams = '_UNDEF_') -{ - unset($loglevel); // not used - /* Need to make sure this is working against a single dimensional object */ - $partscount = count(explode('.',$method)) - 1; - - if (!is_callable($method) && $partscount == 2) - { - list($appname,$classname,$functionname) = explode(".", $method); - if (!is_object($GLOBALS[$classname])) - { - // please note: no reference assignment (=&) here, as $GLOBALS is a reference itself!!! - if ($classparams != '_UNDEF_' && ($classparams || $classparams != 'True')) - { - $GLOBALS[$classname] = CreateObject($appname.'.'.$classname, $classparams); - } - elseif (class_exists($classname)) - { - $GLOBALS[$classname] = new $classname; - } - else - { - $GLOBALS[$classname] = CreateObject($appname.'.'.$classname); - } - } - - if (!method_exists($GLOBALS[$classname],$functionname)) - { - error_log("ExecMethod('$method', ...) No methode '$functionname' in class '$classname'! ".function_backtrace()); - return false; - } - $method = array($GLOBALS[$classname],$functionname); - } - if (is_callable($method)) - { - return $functionparam != '_UNDEF_' ? call_user_func($method,$functionparam) : call_user_func($method); - } - error_log("ExecMethod('$method', ...) Error in parts! ".function_backtrace()); - return false; -} - /** * prepend a prefix to an array of table names * diff --git a/setup/inc/functions.inc.php b/setup/inc/functions.inc.php index d21bcd3f7a..29a88c6cff 100644 --- a/setup/inc/functions.inc.php +++ b/setup/inc/functions.inc.php @@ -50,7 +50,7 @@ if(!defined('EGW_SERVER_ROOT') && !defined('EGW_INCLUDE_ROOT')) define('EGW_API_INC',EGW_SERVER_ROOT.'/phpgwapi/inc'); } -require_once(EGW_INCLUDE_ROOT . '/phpgwapi/inc/common_functions.inc.php'); +require_once(EGW_INCLUDE_ROOT . '/api/src/loader/common.php'); /** * function to handle multilanguage support