Reverted of changes in 1704, see ticket #160
[mailer.git] / inc / inc-functions.php
index a2a63b59298d2fc8443b330fa35f2fdd0789e743..c31c7eb78bf1167c12487e2b1074d17693d2c880 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 03/10/2009 *
- * ===============                              Last change: 03/10/2009 *
+ * Mailer v0.2.1-FINAL                                Start: 03/10/2009 *
+ * ===================                          Last change: 03/10/2009 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : inc-functions.php                                *
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Spezielle Funktionen fuer Include-Dateien        *
  * -------------------------------------------------------------------- *
- * $Revision:: 999                                                    $ *
- * $Date:: 2009-03-10 17:24:54 +0100 (Tue, 10 Mar 2009)               $ *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author:: quix0r                                                   $ *
+ * $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                           *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
  * For more information visit: http://www.mxchange.org                  *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
-       require($INC);
-}
-
-// Check if our config file is writeable or not
-function isIncludeWriteable ($inc) {
-       // Generate FQFN
-       $FQFN = sprintf("%sinc/%s.php", constant('PATH'), $inc);
-
-       // Abort by simple test
-       if ((isFileReadable($FQFN)) && (!is_writeable($FQFN))) {
-               return false;
-       } // END - if
-
-       // Test write-access on directory
-       return is_writeable(dirname($FQFN));
-}
-
-// Reads a directory with PHP files in and gets only files back
-function getArrayFromDirectory ($baseDir, $prefix, $includeDirs = false, $addBaseDir = true, $excludePattern = '@(\.|\.\.)$@') {
-       //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "baseDir={$baseDir},prefix={$prefix} - Entered!");
-       // Init includes
-       $INCs = array();
-
-       // Open directory
-       $dirPointer = opendir(constant('PATH') . $baseDir) or app_die(__FUNCTION__, __LINE__, 'Cannot read '.basename($baseDir).' path!');
-
-       // Read all entries
-       while ($baseFile = readdir($dirPointer)) {
-               // Steps over this returned $baseFile-Name, when it matches the $excludePattern
-               if (preg_match($excludePattern, $baseFile, $match)) {
-                       // These Lines are only for debugging!!
-                   //$INC = $baseDir . '/' . $baseFile;
-                       //$FQFN = constant('PATH') . $INC;
-                       //echo '<pre>$baseDir:'.print_r($baseDir, true).'</pre>';
-                       //echo '<pre>$baseDir:'.print_r(constant('PATH') . $baseDir, true).'</pre>';
-                       //echo '<pre>constant(\'PATH\'):'.print_r(constant('PATH'), true).'</pre>';
-                       //echo '<pre>$FQFN:'.print_r($FQFN, true).'</pre>';
-                       continue;
-               } // END - if
-
-               // Construct include filename and FQFN
-               $INC = $baseDir . '/' . $baseFile;
-               $FQFN = constant('PATH') . $INC;
-
-               // repalecment of // to / is needed, whenn $baseDir is an emty String
-               $FQFN = str_replace('//', '/', $FQFN);
-
-               // Is this a valid reset file?
-               //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "baseDir={$baseDir},prefix={$prefix},baseFile={$baseFile}");
-               if (((isFileReadable($FQFN)) && (substr($baseFile, 0, strlen($prefix)) == $prefix) && (substr($baseFile, -4, 4) == '.php')) || (($includeDirs) && (isDirectory($FQFN)))) {
-                       // Remove both for extension name
-                       $extName = substr($baseFile, strlen($prefix), -4);
-
-                       // Try to find it
-                       $extId = GET_EXT_ID($extName);
-
-                       // Is the extension valid and active?
-                       if (($extId > 0) && (EXT_IS_ACTIVE($extName))) {
-                               // Then add this file
-                               //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " Extension entry ".$baseFile." added.");
-                               $INCs[] = $INC;
-                       } elseif ($extId == 0) {
-                               // Add non-extension files as well
-                               //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " Regular entry ".$baseFile." added.");
-                               if ($addBaseDir) {
-                                       $INCs[] = $INC;
-                               } else {
-                                       $INCs[] = $baseFile;
-                               }
-                       }
-               } // END - if
-       } // END - while
-
-       // Close directory
-       closedir($dirPointer);
-
-       // Sort array
-       asort($INCs);
-
-       // Return array with include files
-       //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " - Left!");
-       return $INCs;
-}
+       die();
+} // END - if
 
 // Init INC_POOL
-function INIT_INC_POOL () {
-       $GLOBALS['inc_pool'] = array();
+function initIncludePool ($pool) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.'<br />';
+       $GLOBALS['inc_pool'][$pool] = array();
 }
 
 // Setter for INC_POOL
-function SET_INC_POOL ($includePool) {
-       $GLOBALS['inc_pool'] = (array) $includePool;
+function setIncludePool ($pool, $includePool) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.'<br />';
+       $GLOBALS['inc_pool'][$pool] = (array) $includePool;
 }
 
 // Getter for INC_POOL
-function GET_INC_POOL () {
-       return $GLOBALS['inc_pool'];
+function getIncludePool ($pool) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.'<br />';
+       return $GLOBALS['inc_pool'][$pool];
 }
 
 // Count INC_POOL
-function COUNT_INC_POOL () {
-       return count($GLOBALS['inc_pool']);
+function countIncludePool ($pool) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.'<br />';
+       return count($GLOBALS['inc_pool'][$pool]);
 }
 
 // Merge INC_POOL into given
-function MERGE_INC_POOL ($includePool) {
-       SET_INC_POOL(merge_array(GET_INC_POOL(), $includePool));
+function mergeIncludePool ($pool, $includePool) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.'<br />';
+       setIncludePool($pool, merge_array(getIncludePool($pool), $includePool));
 }
 
 // Add single include file to INC_POOL
-function ADD_INC_TO_POOL ($INC) {
-       $GLOBALS['inc_pool'][] = (string) $INC;
+function addIncludeToPool ($pool, $inc) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.'<br />';
+       $GLOBALS['inc_pool'][$pool][] = (string) $inc;
 }
 
 // Remove an include file from INC_POOL
-function REMOVE_INC_FROM_POOL ($INC) {
+function removeIncludeFromPool ($pool, $inc) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.'<br />';
        // First look it up
-       $key = array_search($INC, GET_INC_POOL());
+       $key = array_search($inc, getIncludePool($pool));
 
        // Is it valid?
        if ($key !== false) {
                // Then remove it
-               unset($GLOBALS['inc_pool'][$key]);
+               unset($GLOBALS['inc_pool'][$pool][$key]);
 
                // And sort the list
-               asort($GLOBALS['inc_pool']);
+               asort($GLOBALS['inc_pool'][$pool]);
+       } // END - if
+}
+
+// Load the whole include pool
+function loadIncludePool ($pool) {
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.' - START<br />';
+       foreach (getIncludePool($pool) as $inc) {
+               //* DEBUG: */ print __FUNCTION__.':inc='.$inc.'<br />';
+               loadIncludeOnce($inc);
+       } // END - foreach
+       //* DEBUG: */ print __FUNCTION__.':pool='.$pool.' - END<br />';
+
+       // Remove it
+       initIncludePool($pool);
+}
+
+// Loads an include file and logs any missing files for debug purposes
+function loadInclude ($inc) {
+       // Do we have cache?
+       if (!isset($GLOBALS['inc_loaded'][$inc])) {
+               // Add the path. This is why we need a trailing slash in config.php
+               $GLOBALS['inc_loaded'][$inc] = getConfig('PATH') . $inc;
+
+               // Is the include file there?
+               if (!isIncludeReadable($inc)) {
+                       // Not there so log it
+                       debug_report_bug(sprintf("Include file %s not found.", $inc));
+               } // END - if
+       } // END - if
+
+       // Try to load it
+       include($GLOBALS['inc_loaded'][$inc]);
+}
+
+// Loads an include file once
+function loadIncludeOnce ($inc) {
+       // Remove double path
+       $inc = str_replace(getConfig('PATH'), '', $inc);
+
+       // Is it not loaded?
+       if (!isset($GLOBALS['load_once'][$inc])) {
+               // Mark it as loaded
+               $GLOBALS['load_once'][$inc] = 'loaded';
+
+               // Then try to load it
+               loadInclude($inc);
+       } // END - if
+}
+
+// Checks wether an include file (non-FQFN better) is readable
+function isIncludeReadable ($inc) {
+       // Do we have cache?
+       if (!isset($GLOBALS['inc_readable'][$inc])) {
+               // Remove double path
+               $inc = str_replace(getConfig('PATH'), '', $inc);
+
+               // Construct FQFN
+               $FQFN = getConfig('PATH') . $inc;
+
+               // Is it readable?
+               $GLOBALS['inc_readable'][$inc] = isFileReadable($FQFN);
        } // END - if
+
+       // Return result
+       return $GLOBALS['inc_readable'][$inc];
 }
 
 // [EOF]