Filters should be executed even when sql_patches is out-dated
[mailer.git] / inc / load_extensions.php
index 3fea0700f1513b4bd27cc4501934b4b0e4308f2a..1c38a2a7e4456d8d8aa519af35714cc5a26b6ae4 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Alle Erweiterungen laden                         *
  * -------------------------------------------------------------------- *
- *                                                                      *
+ * @TODO Rewrite this whole file to load_cache-extensions.php           *
+ * -------------------------------------------------------------------- *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
+ * $Tag:: 0.2.1-FINAL                                                 $ *
+ * $Author::                                                          $ *
+ * Needs to be in all Files and every File needs "svn propset           *
+ * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
  * For more information visit: http://www.mxchange.org                  *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
+       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
        require($INC);
-}
-//
-global $EXT_CSS_FILES;
-$EXT_CSS_FILES = array();
-$ADD = "";
+} // END - if
+
+// Init variables
+EXT_INIT_CSS_FILES();
+$add = '';
+
+// Init cache instance and array
+$GLOBALS['cache_instance'] = null;
+$GLOBALS['cache_array'] = array();
 
 // Skip loading extensions
-if ((!defined('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return;
-
-// Load default sql_patches extension if present
-if (FILE_READABLE(PATH."inc/extensions/ext-sql_patches.php")) {
-       // Load it...
-       $EXT_LOAD_MODE = "";
-       require_once(PATH."inc/extensions/ext-sql_patches.php");
-       $cacheArray['active_extensions'] = array('sql_patches' => 'Y'); // KEEP THIS ALWAYS ACTIVE!
-} else {
-       // Initialize array for "always keep active extensions"
-       $cacheArray['active_extensions'] = array();
-}
+if ((!isInstalled()) || (isInstalling())) {
+       // Init filter system here
+       initFilterSystem();
+
+       // Skip loading
+       return;
+} // END - if
+
+// Initialize array for "always keep active extensions"
+$GLOBALS['cache_array']['active_extensions'] = array();
+
+// By default no cache is set
+$GLOBALS['cache_mode'] = 'no';
+
+// Load sql_patchrs extension alone
+if (!LOAD_EXTENSION('sql_patches')) {
+       // Could not load sql_patches! ARGH!!!
+       trigger_error('Cannot load extension sql_patches.');
+} // END - if
+
+// Init inc pool array
+INIT_INC_POOL();
 
 //
 // Load extensions
 //
-if (EXT_IS_ACTIVE("cache")) {
+if (EXT_IS_ACTIVE('cache')) {
        // Load cache extension alone
-       include_once(PATH."inc/libs/cache_functions.php");
-       $cacheMode = "";
-       include_once(PATH."inc/extensions/ext-cache.php");
-       switch($cacheInstance->cache_file("extensions", true)) {
-               case true : $cacheMode = "load"; break;
-               case false: $cacheMode = "init"; break;
+       if (!LOAD_EXTENSION('cache')) {
+               // Extension 'cache' was not loaded
+               trigger_error('Cannot load extension cache.');
+       } // END - if
+
+       // Check extension cache
+       switch (($GLOBALS['cache_instance']->loadCacheFile('extensions', true)) && ($GLOBALS['cache_instance']->extensionVersionMatches('sql_patches'))) {
+               case true : $GLOBALS['cache_mode'] = 'load'; break;
+               case false: $GLOBALS['cache_mode'] = 'init'; break;
+       } // END - switch
+
+       // Do we need to init the cache? But not in "CSS mode".
+       if (($GLOBALS['cache_mode'] == 'init') && (getConfig('cache_exts') == 'Y') && (getOutputMode() == '0')) {
+               // Init cache file
+               $GLOBALS['cache_instance']->init('EXTENSIONS');
+               $GLOBALS['cache_instance']->storeExtensionVersion('sql_patches');
+       } elseif (getConfig('cache_exts') != 'Y') {
+               // Cache will not be created for extensions
+               $GLOBALS['cache_mode'] = 'skip';
        }
+} // END - if
 
-       // Do not recreate cache file when it's switched off!
-       if (($cacheMode == "init") && ($_CONFIG['cache_exts'] == "N")) $cacheMode = "skip";
+// Load cache?
+if ($GLOBALS['cache_mode'] == 'load') {
+       // Init include array
+       $EXT_POOL = array();
 
-       // Load language
-       if ($cacheMode == "load") include(PATH."inc/language/cache_".GET_LANGUAGE().".php");
-} else {
-       // Cache extension not active
-       $cacheMode = "no";
-}
-
-if ($cacheMode == "load") {
        // Re-initialize handler
-       $cacheInstance->cache_file("extensions", true);
+       $GLOBALS['cache_instance']->loadCacheFile('extensions', true);
 
        // Load extension data from cache file
-       $EXT_DUMMY = $cacheInstance->cache_load();
+       $EXT_DUMMY = $GLOBALS['cache_instance']->getArrayFromCache();
+
+       // Is the cache file fine?
+       if (!isset($EXT_DUMMY['ext_name'])) {
+               // Cache file is damaged so kill it
+               $GLOBALS['cache_instance']->destroyCacheFile();
+
+               // Retry it
+               require(__FILE__);
+               return;
+       } // END -  if
 
        // Begin with the cache preparation of extensions
        $EXT_NAMES = array();
        foreach ($EXT_DUMMY['ext_name'] as $k => $name) {
-               // Load functions file
-               if ($EXT_DUMMY['ext_funcs'][$k] == "Y") {
-                       require_once(PATH."inc/libs/".$name."_functions.php");
-               } // END - if
-
-               // Load Language file
-               if ($EXT_DUMMY['ext_lang'][$k] == "Y") {
-                       $INC = sprintf("%sinc/language/%s_%s.php", PATH, $name, GET_LANGUAGE());
-                       if (FILE_READABLE($INC)) require_once($INC);
-               } // END - if
-
                // Load CSS file
-               if ($EXT_DUMMY['ext_css'][$k] == "Y") $EXT_CSS_FILES[] = "".$name.".css";
+               if ($EXT_DUMMY['ext_css'][$k] == 'Y') EXT_ADD_CSS_FILE($name . '.css');
 
                // Load extension file itself
-               if (($EXT_DUMMY['ext_active'][$k] == "Y") || ($EXT_DUMMY['ext_keep'][$k] == "Y") || (IS_ADMIN())) {
-                       require_once(PATH."inc/extensions/ext-".$name.".php");
+               if ((($EXT_DUMMY['ext_active'][$k] == 'Y') || ($EXT_DUMMY['ext_keep'][$k] == 'Y') || (IS_ADMIN())) && (!in_array($name, array('sql_patches', 'cache')))) {
+                       $EXT_POOL[] = $name;
                } // END - if
 
                // Version number
                $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
                unset($EXT_DUMMY['ext_version'][$k]);
+
                // Extension is active
                $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
                unset($EXT_DUMMY['ext_active'][$k]);
+
                // Ext menu
                $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
                unset($EXT_DUMMY['ext_menu'][$k]);
+
                // Extension id
                $EXT_DUMMY['ext_id'][$name] = $EXT_DUMMY['ext_id'][$k];
                $id = $EXT_DUMMY['ext_id'][$name];
@@ -124,50 +155,55 @@ if ($cacheMode == "load") {
                // Add ext name
                $EXT_NAMES[$id] = $name;
 
+               // Add deprecated flag (defaults to 'not deprecated')
+               $EXT_DUMMY['ext_deprecated'][$name] = 'N';
+
                // Mark it as active extension
-               $cacheArray['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
+               $GLOBALS['cache_array']['active_extensions'][$name] = $EXT_DUMMY['ext_keep'][$k];
                unset($EXT_DUMMY['ext_keep'][$k]);
 
                // Remove unneccessary data from memory
-               unset($EXT_DUMMY['ext_lang'][$k]);
                unset($EXT_DUMMY['ext_css'][$k]);
-               unset($EXT_DUMMY['ext_funcs'][$k]);
        } // END - foreach
 
        // Write dummy array back
        $EXT_DUMMY['ext_name'] = $EXT_NAMES;
        unset($EXT_NAMES);
 
-       // Close cache file
-       $cacheInstance->cache_close();
-
        // Loading cache is done so let's free some memory!
-       unset($EXT_DUMMY['ext_lang']);
        unset($EXT_DUMMY['ext_keep']);
        unset($EXT_DUMMY['ext_css']);
-       unset($EXT_DUMMY['ext_funcs']);
-       $cacheArray['extensions'] = $EXT_DUMMY;
+       $GLOBALS['cache_array']['extensions'] = $EXT_DUMMY;
        unset($EXT_DUMMY);
 
        // No database load needed
        $res_ext_crt = false;
 
+       // Load all extension files
+       foreach ($EXT_POOL as $ext) {
+               LOAD_EXTENSION($ext);
+       } // END - foreach
+
        // Load more cache files (like admins)
-       require_once(PATH."inc/load_cache.php");
+       loadIncludeOnce('inc/load_cache.php');
+
+       // Remove array
+       unset($EXT_POOL);
 } else {
-       // If current user is not admin load only activated extensions
-       // The admin shall use every available extension for testing purposes
-       if ((!IS_ADMIN()) && ($cacheMode != "init")) $ADD = " WHERE ext_active='Y'";
+       // If current user is not admin load only activated extensions. But load
+       // them all if we are going to init the cache files. The admin shall use
+       // every available extension for testing purposes.
+       if ((!IS_ADMIN()) && ($GLOBALS['cache_mode'] != 'init')) $add = " WHERE `ext_active`='Y'";
 
-       if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
+       if (GET_EXT_VERSION('sql_patches') >= '0.0.6') {
                // Query with CSS file from DB
-               $res_ext_crt = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_has_css, ext_active, ext_version
-FROM "._MYSQL_PREFIX."_extensions".$ADD."
+               $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_has_css AS ext_css, ext_active, ext_version
+FROM `{!_MYSQL_PREFIX!}_extensions`" . $add . "
 ORDER BY ext_name", __FILE__, __LINE__);
        } else {
-               // Old obsulete query string
-               $res_ext_crt = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_name, ext_active, ext_version
-FROM "._MYSQL_PREFIX."_extensions".$ADD."
+               // Old obsolete query string
+               $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_name, ext_active, ext_version
+FROM `{!_MYSQL_PREFIX!}_extensions`" . $add . "
 ORDER BY ext_name", __FILE__, __LINE__);
        }
 }
@@ -176,128 +212,100 @@ ORDER BY ext_name", __FILE__, __LINE__);
 $DEL = array();
 
 // At least one found?
-if ((SQL_NUMROWS($res_ext_crt) > 0) && (($cacheMode == "init") || ($cacheMode == "no")) && ($CSS != "1") && ($CSS != "-1")) {
-       // Load theme management
-       require_once(PATH."inc/theme-manager.php");
-
-       // If we need to init the cache init it now
-       if ($cacheMode == "init") $cacheInstance->cache_init("EXTENSIONS");
-
+if ((SQL_NUMROWS($res_ext_crt) > 0) && ((($GLOBALS['cache_mode'] == 'init') && (getOutputMode() != '1') && (getOutputMode() != '-1')) || ($GLOBALS['cache_mode'] == 'no'))) {
        // Extensions are registered so we load them
-       while (list($EXT_ID, $name, $lang, $css, $active, $version) = SQL_FETCHROW($res_ext_crt)) {
+       while ($content = SQL_FETCHARRAY($res_ext_crt)) {
                // Get menu entry
-               $menu = "N";
-               if (MODULE_HAS_MENU($name, true)) {
-                       $menu = "Y";
+               $content['ext_menu'] = 'N';
+               if (MODULE_HAS_MENU($content['ext_name'], true)) {
+                       $content['ext_menu'] = 'Y';
                } // END - if
 
-               // Load extensions
-               $file1 = sprintf("%sinc/extensions/ext-%s.php", PATH, $name);
-               $file2 = $file1; $EXT_CSS = "N"; $EXT_ALWAYS_ACTIVE = "N";
-
-               // Special functions file
-               $file3 = sprintf("%sinc/libs/%s_functions.php", PATH, $name);
+               // Generate FQFN for extension
+               $FQFN = sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $content['ext_name']);
 
                // Does the extension file exists?
-               if (FILE_READABLE($file1)) {
-                       // If there's no language file specified we don't need to load one... ;-)
-                       if (!empty($lang)) {
-                               // Create language file
-                               $file2 = sprintf("%sinc/language/%s_%s.php", PATH, $lang, GET_LANGUAGE());
-                       }
+               if (isFileReadable($FQFN)) {
+                       // By default no extension is always active, except sql_patches
+                       EXT_SET_ALWAYS_ACTIVE('N');
 
-                       if (FILE_READABLE($file3)) {
-                               // Special functions file
-                               $funcs = "Y";
-                               require_once($file3);
+                       // Load extension
+                       if (($content['ext_name'] != 'sql_patches') && (($content['ext_name'] != 'cache') || (!EXT_IS_ACTIVE('cache')))) {
+                               // Load extension
+                               LOAD_EXTENSION($content['ext_name']);
                        } else {
-                               // Don't load functions file
-                               $funcs = "N";
+                               // Keep sql_patches always active
+                               EXT_SET_ALWAYS_ACTIVE('Y');
                        }
 
-                       // Do we need a language file?
-                       if (($file1 != $file2) && (FILE_READABLE($file2))) {
-                               // Load language file
-                               $lang = "Y";
-                               include($file2);
-                       } else {
-                               // Don't load language file
-                               $lang = "N";
-                       }
+                       // Transfer EXT_ALWAYS_ACTIVE flag
+                       $content['ext_keep'] = EXT_GET_ALWAYS_ACTIVE();
 
-                       // Load extension
-                       if ($name != "sql_patches") {
-                               // Load extension's file
-                               include_once($file1);
-                       } else {
-                               // KEEP sql_patches ALWAYS ACTIVE!
-                               $EXT_ALWAYS_ACTIVE = "Y";
-                       }
+                       // CSS file handling:
+                       if ((!isset($content['ext_css'])) || ($content['ext_css'] == 'Y')) {
+                               // Create FQFN for the CSS file
+                               $FQFN = sprintf("%stheme/%s/css/%s.css", constant('PATH'), getCurrentTheme(), $content['ext_name']);
 
-                       if ($css == "Y") {
-                               $CSS_FILE = sprintf("%stheme/%s/css/%s.css", PATH, GET_CURR_THEME(), $name);
-                               if (FILE_READABLE($CSS_FILE)) {
+                               // Is the file there?
+                               if (isFileReadable($FQFN)) {
                                        // CSS file for extension was found (use only relative path for now!)
-                                       $EXT_CSS_FILES[] = $name.".css";
+                                       EXT_ADD_CSS_FILE($content['ext_name'] . '.css');
+                                       $content['ext_css'] = 'Y';
                                } else {
                                        // Don't load CSS file
-                                       $css = "N";
+                                       $content['ext_css'] = 'N';
                                }
                        } // END - if
 
-                       // Add cache row
-                       if ($cacheMode == "init") {
-                               $cacheInstance->add_row(array(
-                                       'ext_id'      => $EXT_ID,
-                                       'ext_name'    => $name,
-                                       'ext_lang'    => $lang,
-                                       'ext_css'     => $css,
-                                       'ext_menu'    => $menu,
-                                       'ext_funcs'   => $funcs,
-                                       'ext_active'  => $active,
-                                       'ext_version' => $version,
-                                       'ext_keep'    => $EXT_ALWAYS_ACTIVE,
-                               ));
-                       } elseif ($cacheMode == "no") {
+                       // Shall we cache?
+                       if ($GLOBALS['cache_mode'] == 'init') {
+                               // Add cache row
+                               $GLOBALS['cache_instance']->addRow($content);
+                       } elseif ($GLOBALS['cache_mode'] == 'no') {
                                // Remember this value for later usage
-                               $cacheArray['active_extensions'][$name] = $EXT_ALWAYS_ACTIVE;
+                               $GLOBALS['cache_array']['active_extensions'][$content['ext_name']] = EXT_GET_ALWAYS_ACTIVE();
                        }
-               } elseif (!FILE_READABLE($file1)) {
+               } elseif (!isFileReadable($FQFN)) {
                        // Deleted extension file so we mark it for removal from DB
-                       $DEL[] = $name;
+                       $DEL[] = $content['ext_name'];
                }
        } // END - while
 
-       if ($cacheMode == "init") {
+       if ($GLOBALS['cache_mode'] == 'init') {
                // Close cache file
-               $cacheInstance->cache_close();
+               $GLOBALS['cache_instance']->finalize();
 
                // Load more cache files (like admins)
-               require_once(PATH."inc/load_cache.php");
-       } // END - if
-}
+               loadIncludeOnce('inc/load_cache.php');
+       } else {
+               // Init filter system for non-init mode
+               initFilterSystem();
+       }
 
-// Free memory
-SQL_FREERESULT($res_ext_crt);
+       // Free memory
+       SQL_FREERESULT($res_ext_crt);
+} elseif ((!EXT_IS_ACTIVE('cache')) || (($GLOBALS['cache_mode'] == 'init') && (getOutputMode() != 0))) {
+       // Init filter system even when there are no extensions installed. #16
+       initFilterSystem();
+} elseif ($GLOBALS['cache_mode'] != 'load') {
+       // Something unexpected!
+       debug_report_bug('Unexpected state in '.basename(__FILE__).': cache_mode='.$GLOBALS['cache_mode'].', numRows='.SQL_NUMROWS($res_ext_crt));
+}
 
-// Load include files
-if (!empty($INC_POOL[0])) {
-       foreach ($INC_POOL as $inc) {
-               require_once($inc);
-       } // END - foreach
-} // END - if
+// Run the filter
+runFilterChain('load_includes');
 
 // Uninstall extensions that are no longer in our system
 if (!empty($DEL[0])) {
        // Remove extensions from two tables: extension registry and tasks table
-       foreach ($DEL as $name) {
+       foreach ($DEL as $del_ext) {
                // First remove entry from extensions table
-               SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
-                       array($name), __FILE__, __LINE__);
+               SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
+                       array($del_ext), __FILE__, __LINE__);
 
                // Remove (maybe?) found tasks (main task and possible updates
-               SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE subject LIKE '[%s:] %' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
-                       array($name), __FILE__, __LINE__);
+               SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `subject`='[%s:]' AND (`task_type`='EXTENSION' OR `task_type`='EXTENSION_UPDATE')",
+                       array($del_ext), __FILE__, __LINE__);
        } // END - foreach
 
        // I think it's not neccessary to run the optimization function here