2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 07/01/2010 *
4 * =================== Last change: 07/01/2010 *
6 * -------------------------------------------------------------------- *
7 * File : module-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Module functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Modulfunktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // "Getter" for module title
44 function getModuleTitle ($module) {
46 if (!isset($GLOBALS[__FUNCTION__][$module])) {
51 // Is the script installed?
52 if ((isInstalled()) && ($module != 'error')) {
53 // Check if cache is valid
54 if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['modules']['module'])) && (in_array($module, $GLOBALS['cache_array']['modules']['module']))) {
56 $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module];
59 incrementStatsEntry('cache_hits');
60 } elseif (!isExtensionActive('cache')) {
62 $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
63 array($module), __FUNCTION__, __LINE__);
65 // Is the entry there?
66 if (SQL_NUMROWS($result) == 1) {
67 // Get the title from database
68 $data = SQL_FETCHARRAY($result);
72 SQL_FREERESULT($result);
77 $data['title'] = trim($data['title']);
79 // Still no luck or empty title?
80 if (empty($data['title'])) {
82 if ($module == 'error') {
83 // Error (real module was not found)
84 $data['title'] = '{--MODULE_ERROR_404_TITLE--}';
87 $data['title'] = '{%message,UNKNOWN_MODULE_DETECTED_TITLE=' . $module . '%}';
88 if ((is_resource($result)) && (SQL_HASZERONUMS($result))) {
89 // Add module to database and ignore return value
90 checkModulePermissions($module);
96 $GLOBALS[__FUNCTION__][$module] = $data['title'];
100 return $GLOBALS[__FUNCTION__][$module];
103 // Checks if module_status entry is there
104 function isModuleStatusSet ($module) {
106 return (isset($GLOBALS['module_status'][$module]));
109 // Setter module status
110 function setModuleStatus ($module, $status) {
111 $GLOBALS['module_status'][$module] = $status;
114 // Getter for module status
115 function getModuleStatus ($module) {
116 // Is the module_status entry there?
117 if (!isModuleStatusSet($module)) {
119 reportBug('Module status not set. module=' . $module);
123 return $GLOBALS['module_status'][$module];
126 // Checks whether the given module is registered
127 function isModuleRegistered ($module) {
128 // By default nothing is found
131 // Check if cache is latest version
132 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
133 // Is the cache there?
134 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using cache.');
135 if (isset($GLOBALS['cache_array']['modules']['locked'][$module])) {
137 incrementStatsEntry('cache_hits');
142 // No, then we have to update it!
143 setModuleStatus($module, 'cache_miss');
145 } elseif (!isExtensionActive('cache')) {
146 // Check for module in database
147 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
148 $result = SQL_QUERY_ESC("SELECT `locked`, `hidden`, `admin_only`, `mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
149 array($module), __FUNCTION__, __LINE__);
150 if (SQL_NUMROWS($result) == 1) {
152 $data = SQL_FETCHARRAY($result);
155 foreach ($data as $key => $value) {
156 $GLOBALS['cache_array']['modules'][$key][$module] = $value;
161 } elseif (isDebugModeEnabled()) {
162 // Debug message only in debug-mode...
163 logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module . ' not found.');
167 SQL_FREERESULT($result);
174 // Checks whether the given module is locked by just checking the cache
175 function isModuleLocked ($module) {
176 // Determine if there a cache entry and is it set
177 $return = ((isset($GLOBALS['cache_array']['modules']['locked'][$module])) && ($GLOBALS['cache_array']['modules']['locked'][$module] == 'Y'));
179 // Return determined value
183 // Checks whether the given module is hidden by just checking the cache
184 function isModuleHidden ($module) {
185 // Determine if there a cache entry and is it set
186 $return = ((isset($GLOBALS['cache_array']['modules']['hidden'][$module])) && ($GLOBALS['cache_array']['modules']['hidden'][$module] == 'Y'));
188 // Return determined value
192 // Checks whether the given module is mem_only by just checking the cache
193 function isModuleMemberOnly ($module) {
194 // Determine if there a cache entry and is it set
195 $return = ((isset($GLOBALS['cache_array']['modules']['mem_only'][$module])) && ($GLOBALS['cache_array']['modules']['mem_only'][$module] == 'Y'));
197 // Return determined value
201 // Checks whether the given module is admin_only by just checking the cache
202 function isModuleAdminOnly ($module) {
203 // Determine if there a cache entry and is it set
204 $return = ((isset($GLOBALS['cache_array']['modules']['admin_only'][$module])) && ($GLOBALS['cache_array']['modules']['admin_only'][$module] == 'Y'));
206 // Return determined value
210 // Check validity of a given module name (no file extension)
211 function checkModulePermissions ($module = '') {
212 // Is it empty (default), then take the current one
213 if (empty($module)) {
214 // Use current module
215 $module = getModule();
219 if (isModuleStatusSet($module)) {
221 return getModuleStatus($module);
224 // Filter module name (names with low chars and underlines are fine!)
225 $module = preg_replace('/[^a-z_]/', '', $module);
227 // Check for prefix is a extension...
228 $modSplit = explode('_', $module);
229 $extension = ''; $module_chk = $module;
230 //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
231 if (count($modSplit) == 2) {
232 // Okay, there is a separator (_) in the name so is the first part a module?
233 //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
234 if (isExtensionActive($modSplit[0])) {
235 // The prefix is an extension's name, so let's set it
236 $extension = $modSplit[0]; $module = $modSplit[1];
240 // Major error in module registry is the default
241 setModuleStatus($module_chk, 'major');
243 // Check if script is installed if not return a 'done' to prevent some errors
244 if ((isInstallationPhase()) || (!isAdminRegistered())) {
245 // Not installed or no admin registered or in installation phase
246 setModuleStatus($module_chk, 'done');
252 // Check if the module is registered
253 $found = isModuleRegistered($module_chk);
255 // Is the module found?
256 if ($found === TRUE) {
257 // Check returned values against current access permissions
259 // Admin access ----- Guest access ----- --- Guest or member? ---
260 if ((isAdmin()) || ((!isModuleLocked($module_chk)) && (!isModuleAdminOnly($module_chk)) && ((!isModuleMemberOnly($module_chk)) || (isMember())))) {
261 // If you are admin you are welcome for everything!
262 setModuleStatus($module_chk, 'done');
263 } elseif (isModuleLocked($module_chk)) {
265 setModuleStatus($module_chk, 'locked');
266 } elseif ((isModuleMemberOnly($module_chk)) && (!isMember())) {
267 // You have to login first!
268 setModuleStatus($module_chk, 'mem_only');
269 } elseif ((isModuleAdminOnly($module_chk)) && (!isAdmin())) {
270 // Only the Admin is allowed to enter this module!
271 setModuleStatus($module_chk, 'admin_only');
273 // @TODO Nothing helped???
274 logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%d,hidden=%d,mem=%d,admin=%d",
275 getModuleStatus($module_chk),
276 intval(isModuleLocked($module_chk)),
277 intval(isModuleHidden($module_chk)),
278 intval(isModuleMemberOnly($module_chk)),
279 intval(isModuleAdminOnly($module_chk))
284 // Still no luck or not found?
285 if (($found === FALSE) && (!isExtensionActive('cache')) && (getModuleStatus($module_chk) != 'done')) {
286 // ----- Default module ----- ---- Module in base folder ---- --- Module with extension's name ---
287 if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) {
288 // Data is missing so we add it
289 if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
291 * Since 0.3.6 we have a has_menu column, this took me a half
292 * hour to find a loop here... *sigh*
294 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
295 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`)
297 ('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
299 // Wrong/missing sql_patches!
300 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
301 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`)
303 ('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
306 // Everthing is fine?
307 if (SQL_HASZEROAFFECTED()) {
308 // Something bad happend!
309 setModuleStatus($module_chk, 'major');
313 // Destroy cache here
314 // @TODO Rewrite this to a filter
315 if ((isHtmlOutputMode()) || (isRawOutputMode())) {
316 rebuildCache('modules', 'modules');
320 unset($GLOBALS['module_status'][$module_chk]);
321 return checkModulePermissions($module_chk);
323 // Module not found we don't add it to the database
324 setModuleStatus($module_chk, '404');
326 } elseif ((getModuleStatus($module_chk) == 'cache_miss') && (isHtmlOutputMode())) {
327 // Rebuild the cache files
328 rebuildCache('modules', 'modules');
329 } elseif ($found === FALSE) {
330 // Problem with module detected
331 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. getModuleStatus()=%s,locked=%d,hidden=%d,mem=%d,admin=%d,output_mode=%s",
333 getModuleStatus($module_chk),
334 intval(isModuleLocked($module_chk)),
335 intval(isModuleHidden($module_chk)),
336 intval(isModuleMemberOnly($module_chk)),
337 intval(isModuleAdminOnly($module_chk)),
338 getScriptOutputMode()
343 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("module=%s, status=%s", $module_chk, getModuleStatus($module_chk)));
346 return getModuleStatus($module_chk);
349 // Checks if the module has a menu
350 function ifModuleHasMenu ($module, $forceDb = FALSE) {
351 // All is false by default
354 // Extension installed and newer than or has version 0.1.2?
355 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
356 // Cache version is okay, so let's check the cache!
357 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$module])) {
358 // Check module cache and count hit
359 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$module] == 'Y');
360 incrementStatsEntry('cache_hits');
361 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$module])) {
362 // Check cache and count hit
363 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$module] == 'Y');
364 incrementStatsEntry('cache_hits');
366 // Admin/guest/member/sponsor modules have always a menu!
367 $ret = in_array($module, array('admin', 'index', 'login', 'sponsor'));
369 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === TRUE))) {
370 // Check database for entry
371 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
372 array($module), __FUNCTION__, __LINE__);
375 if (SQL_NUMROWS($result) == 1) {
376 // Load "has_menu" column
377 $data = SQL_FETCHARRAY($result);
380 $GLOBALS['cache_array']['extension']['ext_menu'][$module] = $data['has_menu'];
382 // Does it have a menu?
383 $ret = ($data['has_menu'] == 'Y');
387 SQL_FREERESULT($result);
388 } elseif (!isExtensionInstalled('sql_patches')) {
389 // No ext-sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
390 $ret = in_array($module, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
392 // Unsupported state!
393 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
400 // Adds a SQL for given module
401 function addModuleSql ($module, $title, $locked, $hidden, $adminOnly, $memOnly) {
402 // Is the module already registered?
403 if (!isModuleRegistered($module)) {
405 addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`, `title`, `locked`, `hidden`, `admin_only`, `mem_only`) VALUES ('" . $module . "','" . $title . "','" . $locked . "','" . $hidden . "','" . $adminOnly . "','" . $memOnly . "')");
407 // Already registered
408 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Already registered: module=%s,locked=%s,hidden=%s,admin=%s,mem=%s",
418 // Load the currently set module
419 function loadModule () {
420 // By default all modules are invalid
421 $isModuleValid = FALSE;
423 // Construct module name
424 $GLOBALS['module_inc'] = sprintf("inc/modules/%s.php", getModule());
426 // Check module permission (again)
427 $moduleState = checkModulePermissions();
429 // Which permission/error state do we have?
430 switch ($moduleState) {
431 case 'cache_miss': // The cache is gone
432 case 'admin_only': // Admin-only access
433 case 'mem_only': // Member-only access
434 case 'done': // All fine!
435 // Does the module exists on local file system?
436 if ((isIncludeReadable($GLOBALS['module_inc'])) && (!ifFatalErrorsDetected())) {
437 // Module is valid, active and located on the local disk...
438 $isModuleValid = TRUE;
439 } elseif (!ifFatalErrorsDetected()) {
441 setHttpStatus('404');
444 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_404--}');
446 // Set module to error module (non-existent!)
453 setHttpStatus('404');
456 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_404--}');
461 setHttpStatus('403 Forbidden');
463 if (!isIncludeReadable($GLOBALS['module_inc'])) {
464 // Set HTTP status again
465 setHttpStatus('404 Not Found');
467 // Module does addionally not exists
468 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_LOCKED_404--}');
472 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_IS_LOCKED--}');
476 // Unknown module status
477 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unknown status %s return from module check. Module=%s", $moduleState, getModule()));
478 addFatalMessage(__FUNCTION__, __LINE__, '{%message,UNKNOWN_MODULE_STATUS=' . $moduleState . '%}');
483 return $isModuleValid;
487 function doIncludeModule () {
489 setContentType('text/html');
492 loadIncludeOnce('inc/header.php');
494 // Modules are by default not valid!
495 $isModuleValid = FALSE;
497 // By default NULL is used
498 $GLOBALS['module_inc'] = NULL;
500 // Is the maintenance mode active or goes all well?
501 if ((isExtensionActive('maintenance')) && (isMaintenanceEnabled()) && (!isAdmin()) && (getModule() != 'admin')) {
502 // Maintain mode is active and you are no admin
503 addFatalMessage(__FUNCTION__, __LINE__, '{--MAILER_DOWN_FOR_MAINTENANCE--}');
504 } elseif ((SQL_IS_LINK_UP()) && (!ifFatalErrorsDetected())) {
505 // Do the small "load module" call
506 $isModuleValid = loadModule();
507 } elseif (!ifFatalErrorsDetected()) {
508 // SQL problems detected
509 addFatalMessage(__FUNCTION__, __LINE__, '{--MYSQL_ERRORS--}');
512 // Is the module valid?
513 if (($isModuleValid === TRUE) && (!is_null($GLOBALS['module_inc']))) {
515 runFilterChain('pre_module_load');
517 // Everything is okay so we can load the module
518 loadIncludeOnce($GLOBALS['module_inc']);
521 runFilterChain('post_module_load');
524 // Add the footer (this will call doShutdown())
525 loadIncludeOnce('inc/footer.php');