From b2fdbc0571dc977aec91e8f40f61007134ddfbf7 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 11 Nov 2015 14:14:06 +0900 Subject: [PATCH] Better autoload for required files Add autoload function to main config file. Add better DIR declarations in config file based on __DIR__ for libs & smarty classes. Load all class files with the new autoload function in header & direct file calls. --- www/admin/class_test.php | 6 ++--- www/admin/header.inc | 11 ++++----- www/admin/set_paths.inc | 6 ++--- www/admin/smarty.inc | 8 +++++-- www/admin/smarty_test.php | 7 ++---- www/configs/config.inc | 40 +++++++++++++++++++++++++------- www/libs/Class.Admin.Backend.inc | 13 ++++------- www/libs/Class.DB.Array.IO.inc | 7 +----- www/libs/Class.DB.IO.inc | 7 +----- www/libs/Class.Form.Generate.inc | 9 ++----- www/libs/Class.Login.inc | 9 ++----- www/libs/Class.Smarty.Extend.inc | 9 ++----- www/libs/Class.l10n.inc | 13 +++-------- 13 files changed, 63 insertions(+), 82 deletions(-) diff --git a/www/admin/class_test.php b/www/admin/class_test.php index d62c2ca6..b1e58f03 100644 --- a/www/admin/class_test.php +++ b/www/admin/class_test.php @@ -15,10 +15,8 @@ // session_name(EDIT_SESSION_NAME); // session_start(); // basic class test file -// require(LIBS."Class.Basic.inc"); -// require(LIBS."Class.DB.IO.inc"); - require(LIBS."Class.Login.inc"); - require(LIBS."Class.Admin.Backend.inc"); + foreach (array ('Login', 'Admin.Backend') as $class) + _spl_autoload('Class.'.$class.'.inc'); $lang = 'en_utf8'; diff --git a/www/admin/header.inc b/www/admin/header.inc index 4f169c4a..5faa8152 100644 --- a/www/admin/header.inc +++ b/www/admin/header.inc @@ -24,12 +24,9 @@ require("config.inc"); // set the session name define('SET_SESSION_NAME', EDIT_SESSION_NAME); - // login class - require(LIBS."Class.Login.inc"); - // DB connection & work time class - require(LIBS.'Class.Admin.Backend.inc'); - // Smarty: and the small extend for l10n calls - require(LIBS.'Class.Smarty.Extend.inc'); + // login class, DB connections & Admin class, Smarty extension + foreach (array ('Login', 'Admin.Backend', 'Smarty.Extend') as $class) + _spl_autoload('Class.'.$class.'.inc'); //------------------------------ library include end //------------------------------ basic variable settings start @@ -42,7 +39,7 @@ elseif (!$lang) $lang = DEFAULT_LANG; // end the stop of the output flow, but only if we didn't request a csv file download - if ($_POST['action'] != 'download_csv') + if (array_key_exists('action', $_POST) && $_POST['action'] != 'download_csv') { header("Content-type: text/html; charset=".$encoding); ob_end_flush(); diff --git a/www/admin/set_paths.inc b/www/admin/set_paths.inc index d7c3188e..015caf9d 100644 --- a/www/admin/set_paths.inc +++ b/www/admin/set_paths.inc @@ -35,7 +35,7 @@ // set include & template names $CONTENT_INCLUDE = str_replace(".php", ".tpl", $cms->page_name); - $FORM_NAME = !$FORM_NAME ? str_replace(".php", "", $cms->page_name) : $FORM_NAME; + $FORM_NAME = !isset($FORM_NAME) || !$FORM_NAME ? str_replace(".php", "", $cms->page_name) : $FORM_NAME; // strip tpl and replace it with inc // php include file per page $cms->INC_TEMPLATE_NAME = str_replace(".tpl", ".inc", $CONTENT_INCLUDE); @@ -44,9 +44,9 @@ // css per page $cms->CSS_TEMPLATE_NAME = str_replace(".tpl", ".css", $CONTENT_INCLUDE); // special CSS file - $cms->CSS_SPECIAL_TEMPLATE_NAME = $CSS_NAME; + $cms->CSS_SPECIAL_TEMPLATE_NAME = @$CSS_NAME; // special JS file - $cms->JS_SPECIAL_TEMPLATE_NAME = $JS_NAME; + $cms->JS_SPECIAL_TEMPLATE_NAME = @$JS_NAME; // set basic template path (tmp) $smarty->setTemplateDir(LAYOUT.$TEMPLATE_DIR.TEMPLATES.'/'); diff --git a/www/admin/smarty.inc b/www/admin/smarty.inc index f3bccf02..8da2c12e 100644 --- a/www/admin/smarty.inc +++ b/www/admin/smarty.inc @@ -24,21 +24,25 @@ include($cms->includes.$cms->INC_TEMPLATE_NAME); } // additional per page Javascript include + $cms->JS_INCLUDE = ''; if (file_exists($cms->javascript.$cms->JS_TEMPLATE_NAME) && is_file($cms->javascript.$cms->JS_TEMPLATE_NAME)) { $cms->JS_INCLUDE = $cms->javascript.$cms->JS_TEMPLATE_NAME; } // per page css file + $cms->CSS_INCLUDE = ''; if (file_exists($cms->css.$cms->CSS_TEMPLATE_NAME) && is_file($cms->css.$cms->CSS_TEMPLATE_NAME)) { $cms->CSS_INCLUDE = $cms->css.$cms->CSS_TEMPLATE_NAME; } // optional CSS file + $cms->CSS_SPECIAL_INCLUDE = ''; if (file_exists($cms->css.$cms->CSS_SPECIAL_TEMPLATE_NAME) && is_file($cms->css.$cms->CSS_SPECIAL_TEMPLATE_NAME)) { $cms->CSS_SPECIAL_INCLUDE = $cms->css.$cms->CSS_SPECIAL_TEMPLATE_NAME; } // optional JS file + $cms->JS_SPECIAL_INCLUDE = ''; if (file_exists($cms->javascript.$cms->JS_SPECIAL_TEMPLATE_NAME) && is_file($cms->javascript.$cms->JS_SPECIAL_TEMPLATE_NAME)) { $cms->JS_SPECIAL_INCLUDE = $cms->javascript.$cms->JS_SPECIAL_TEMPLATE_NAME; @@ -64,7 +68,7 @@ $cms->HEADER['STYLESHEET'] = $EDIT_STYLESHEET; $cms->HEADER['JAVASCRIPT'] = $EDIT_JAVASCRIPT; // html title - $cms->HEADER['HTML_TITLE'] = ((!$L_TITLE) ? $cms->l->__($G_TITLE) : $cms->l->__($L_TITLE)); + $cms->HEADER['HTML_TITLE'] = (!isset($L_TITLE) || !$L_TITLE) ? $cms->l->__($G_TITLE) : $cms->l->__($L_TITLE); $cms->DATA['table_width'] = $PAGE_WIDTH ? $PAGE_WIDTH : PAGE_WIDTH; // messages = array('msg' =>, 'class' => 'error/warning/...') @@ -85,7 +89,7 @@ // debug data, if DEBUG flag is on, this data is print out $cms->DEBUG_DATA['debug_error_msg'] = $cms->running_time(); - $cms->DEBUG_DATA['DEBUG'] = $DEBUG_TMPL; + $cms->DEBUG_DATA['DEBUG'] = @$DEBUG_TMPL; // create main data array $cms->CONTENT_DATA = array_merge($cms->HEADER, $cms->DATA, $cms->DEBUG_DATA); diff --git a/www/admin/smarty_test.php b/www/admin/smarty_test.php index 4ed39093..71639ddf 100644 --- a/www/admin/smarty_test.php +++ b/www/admin/smarty_test.php @@ -1,14 +1,11 @@ ); + + // function that will be called on top of each class include to load the class + function _spl_autoload($include_file) + { + // where to search for the files to include + $dirs = array ( + LIBDIR, + SMARTYDIR, + '', + LIBS, + SMARTY, + __DIR__.'/'.LIBS, + __DIR__.'/'.SMARTY + ); + // try to find and load the class ifle + foreach ($dirs as $folder) + { + if (file_exists($folder.$include_file)) + { + require_once($folder.$include_file); + return; + } + } + } ?> diff --git a/www/libs/Class.Admin.Backend.inc b/www/libs/Class.Admin.Backend.inc index 0a14345b..a4f8c1a1 100644 --- a/www/libs/Class.Admin.Backend.inc +++ b/www/libs/Class.Admin.Backend.inc @@ -26,12 +26,7 @@ *********************************************************************/ // try to include file from LIBS path, or from normal path - $include_file = 'Class.DB.IO.inc'; - foreach (array('', LIBS, __DIR__.'/') as $folder) - { - if (file_exists($folder.$include_file)) - require_once($folder.$include_file); - } + _spl_autoload('Class.DB.IO.inc'); class AdminBackend extends db_io { @@ -72,7 +67,7 @@ public function __construct($db_config, $lang, $debug = 0, $db_debug = 0, $echo = 1, $print = 0) { // get the language sub class & init it - require_once(LIBS."Class.l10n.inc"); + _spl_autoload('Class.l10n.inc'); $this->l = new l10n($lang); @@ -144,8 +139,8 @@ $q .= "ip, user_agent, referer, script_name, query_string, server_name, http_host, http_accept, http_accept_charset, http_accept_encoding, session_id, "; $q .= "action, action_id, action_yes, action_flag, action_menu, action_loaded, action_value, action_error) "; $q .= "VALUES "; - $q .= "(".$_SESSION['EUID'].", NOW(), '".$this->db_escape_string($event)."', '".$data."', '".$data_binary."', '".$this->page_name."', "; - $q .= "'".$_SERVER["REMOTE_ADDR"]."', '".$this->db_escape_string($_SERVER['HTTP_USER_AGENT'])."', '".$this->db_escape_string($_SERVER['HTTP_REFERER'])."', '".$this->db_escape_string($_SERVER['SCRIPT_FILENAME'])."', '".$this->db_escape_string($_SERVER['QUERY_STRING'])."', '".$this->db_escape_string($_SERVER['SERVER_NAME'])."', '".$this->db_escape_string($_SERVER['HTTP_HOST'])."', '".$this->db_escape_string($_SERVER['HTTP_ACCEPT'])."', '".$this->db_escape_string($_SERVER['HTTP_ACCEPT_CHARSET'])."', '".$this->db_escape_string($_SERVER['HTTP_ACCEPT_ENCODING'])."', '".session_id()."', "; + $q .= "(".@$_SESSION['EUID'].", NOW(), '".$this->db_escape_string($event)."', '".$data."', '".$data_binary."', '".$this->page_name."', "; + $q .= "'".@$_SERVER["REMOTE_ADDR"]."', '".$this->db_escape_string(@$_SERVER['HTTP_USER_AGENT'])."', '".$this->db_escape_string(@$_SERVER['HTTP_REFERER'])."', '".$this->db_escape_string(@$_SERVER['SCRIPT_FILENAME'])."', '".$this->db_escape_string(@$_SERVER['QUERY_STRING'])."', '".$this->db_escape_string(@$_SERVER['SERVER_NAME'])."', '".$this->db_escape_string(@$_SERVER['HTTP_HOST'])."', '".$this->db_escape_string(@$_SERVER['HTTP_ACCEPT'])."', '".$this->db_escape_string(@$_SERVER['HTTP_ACCEPT_CHARSET'])."', '".$this->db_escape_string(@$_SERVER['HTTP_ACCEPT_ENCODING'])."', '".session_id()."', "; $q .= "'".$this->db_escape_string($this->action)."', '".$this->db_escape_string($this->action_id)."', '".$this->db_escape_string($this->action_yes)."', '".$this->db_escape_string($this->action_flag)."', '".$this->db_escape_string($this->action_menu)."', '".$this->db_escape_string($this->action_loaded)."', '".$this->db_escape_string($this->action_value)."', '".$this->db_escape_string($this->action_error)."')"; $this->db_exec($q, 'edit_log_id'); } diff --git a/www/libs/Class.DB.Array.IO.inc b/www/libs/Class.DB.Array.IO.inc index 908bc1db..00aacd4e 100644 --- a/www/libs/Class.DB.Array.IO.inc +++ b/www/libs/Class.DB.Array.IO.inc @@ -39,12 +39,7 @@ // put into separete function in this class) // try to include file from LIBS path, or from normal path - $include_file = 'Class.DB.IO.inc'; - foreach (array('', LIBS, __DIR__.'/') as $folder) - { - if (file_exists($folder.$include_file)) - require_once($folder.$include_file); - } + _spl_autoload('Class.DB.IO.inc'); // subclass for one array handling class db_array_io extends db_io diff --git a/www/libs/Class.DB.IO.inc b/www/libs/Class.DB.IO.inc index 97256775..c778f39e 100644 --- a/www/libs/Class.DB.IO.inc +++ b/www/libs/Class.DB.IO.inc @@ -241,12 +241,7 @@ *********************************************************************/ // try to include file from LIBS path, or from normal path - $include_file = 'Class.Basic.inc'; - foreach (array('', LIBS, __DIR__.'/') as $folder) - { - if (file_exists($folder.$include_file)) - require_once($folder.$include_file); - } + _spl_autoload('Class.Basic.inc'); class db_io extends basic { diff --git a/www/libs/Class.Form.Generate.inc b/www/libs/Class.Form.Generate.inc index 95c405c3..ef6f8c70 100644 --- a/www/libs/Class.Form.Generate.inc +++ b/www/libs/Class.Form.Generate.inc @@ -213,12 +213,7 @@ *********************************************************************/ // try to include file from LIBS path, or from normal path - $include_file = 'Class.DB.Array.IO.inc'; - foreach (array('', LIBS, __DIR__.'/') as $folder) - { - if (file_exists($folder.$include_file)) - require_once($folder.$include_file); - } + _spl_autoload('Class.DB.Array.IO.inc'); class form extends db_array_io { @@ -260,7 +255,7 @@ { $this->my_page_name = $this->get_page_name(1); // init the language class - require_once(LIBS."Class.l10n.inc"); + _spl_autoload('Class.l10n.inc'); $this->l = new l10n($lang); // load config array // get table array definitions for current page name diff --git a/www/libs/Class.Login.inc b/www/libs/Class.Login.inc index c023d3b2..c5ab87e0 100644 --- a/www/libs/Class.Login.inc +++ b/www/libs/Class.Login.inc @@ -60,12 +60,7 @@ *********************************************************************/ // try to include file from LIBS path, or from normal path - $include_file = 'Class.DB.IO.inc'; - foreach (array('', LIBS, __DIR__.'/') as $folder) - { - if (file_exists($folder.$include_file)) - require_once($folder.$include_file); - } + _spl_autoload('Class.DB.IO.inc'); class login extends db_io { @@ -118,7 +113,7 @@ $this->file_name_ext = '_login_'.date('Y-m-d').'.log'; // get the language sub class & init it - require_once(LIBS."Class.l10n.inc"); + _spl_autoload('Class.l10n.inc'); $this->l = new l10n($lang); // if we have a search path we need to set it, to use the correct DB to login diff --git a/www/libs/Class.Smarty.Extend.inc b/www/libs/Class.Smarty.Extend.inc index 092c6543..dae1b523 100644 --- a/www/libs/Class.Smarty.Extend.inc +++ b/www/libs/Class.Smarty.Extend.inc @@ -11,12 +11,7 @@ // read in the Smarty class for definition // use smarty BC for backwards compability // try to include file from LIBS path, or from normal path - $include_file = 'SmartyBC.class.php'; - foreach (array('', SMARTY, __DIR__.'/../'.SMARTY) as $folder) - { - if (file_exists($folder.$include_file)) - require_once($folder.$include_file); - } + _spl_autoload('SmartyBC.class.php'); class SmartyML extends SmartyBC { @@ -26,7 +21,7 @@ public function __construct($lang) { SmartyBC::__construct(); - require_once(LIBS."Class.l10n.inc"); + _spl_autoload('Class.l10.inc'); $this->l10n = new l10n($lang); // variable variable register $this->register_modifier('getvar', array(&$this, 'get_template_vars')); diff --git a/www/libs/Class.l10n.inc b/www/libs/Class.l10n.inc index d91dc89a..286ee68f 100644 --- a/www/libs/Class.l10n.inc +++ b/www/libs/Class.l10n.inc @@ -26,12 +26,7 @@ *********************************************************************/ // try to include file from LIBS path, or from normal path - $include_file = 'Class.Basic.inc'; - foreach (array('', LIBS, __DIR__.'/') as $folder) - { - if (file_exists($folder.$include_file)) - require_once($folder.$include_file); - } + _spl_autoload('Class.Basic.inc'); class l10n extends basic { @@ -42,8 +37,8 @@ public function __construct($lang = '', $path = '') { - require_once(LIBS.'streams.php'); - require_once(LIBS.'gettext.php'); + foreach (array('streas.php', 'gettext.php') as $include_file) + _spl_autoload($include_file); if (!$lang) $this->lang = 'en'; @@ -109,6 +104,4 @@ return $this->l10n->ngettext($single, $plural, $number); } } - -//require(LIBS.'locale.php'); ?>