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 * -------------------------------------------------------------------- *
13 * Copyright (c) 2003 - 2009 by Roland Haeder *
14 * Copyright (c) 2009 - 2015 by Mailer Developer Team *
15 * For more information visit: http://mxchange.org *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
31 ************************************************************************/
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
38 // "Getter" for module title
39 function getModuleTitle ($module) {
41 if (!isset($GLOBALS[__FUNCTION__][$module])) {
46 // Is the script installed?
47 if ((isInstalled()) && ($module != 'error')) {
48 // Check if cache is valid
49 if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['modules']['module'])) && (in_array($module, $GLOBALS['cache_array']['modules']['module']))) {
51 $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module];
54 incrementStatsEntry('cache_hits');
55 } elseif (!isExtensionActive('cache')) {
57 $result = sqlQueryEscaped("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
58 array($module), __FUNCTION__, __LINE__);
60 // Is the entry there?
61 if (sqlNumRows($result) == 1) {
62 // Get the title from database
63 $data = sqlFetchArray($result);
67 sqlFreeResult($result);
72 $data['title'] = trim($data['title']);
74 // Still no luck or empty title?
75 if (empty($data['title'])) {
77 if ($module == 'error') {
78 // Error (real module was not found)
79 $data['title'] = '{--MODULE_ERROR_404_TITLE--}';
82 $data['title'] = '{%message,UNKNOWN_MODULE_DETECTED_TITLE=' . $module . '%}';
83 if ((is_resource($result)) && (ifSqlHasZeroNums($result))) {
84 // Add module to database and ignore return value
85 checkModulePermissions($module);
91 $GLOBALS[__FUNCTION__][$module] = $data['title'];
95 return $GLOBALS[__FUNCTION__][$module];
98 // Checks if module_status entry is there
99 function isModuleStatusSet ($module) {
101 return (isset($GLOBALS['module_status'][$module]));
104 // Setter module status
105 function setModuleStatus ($module, $status) {
106 $GLOBALS['module_status'][$module] = $status;
109 // Getter for module status
110 function getModuleStatus ($module) {
111 // Is the module_status entry there?
112 if (!isModuleStatusSet($module)) {
114 reportBug(__FUNCTION__, __LINE__, 'Module status not set. module=' . $module);
118 return $GLOBALS['module_status'][$module];
121 // Checks whether the given module is registered
122 function isModuleRegistered ($module) {
123 // By default nothing is found
126 // Check if cache is latest version
127 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
128 // Is the cache there?
129 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using cache.');
130 if (isset($GLOBALS['cache_array']['modules']['locked'][$module])) {
132 incrementStatsEntry('cache_hits');
137 // No, then we have to update it!
138 setModuleStatus($module, 'cache_miss');
140 } elseif (!isExtensionActive('cache')) {
141 // Check for module in database
142 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
143 $result = sqlQueryEscaped("SELECT
148 `{?_MYSQL_PREFIX?}_mod_reg`
152 array($module), __FUNCTION__, __LINE__);
153 if (sqlNumRows($result) == 1) {
155 $data = sqlFetchArray($result);
158 foreach ($data as $key => $value) {
159 $GLOBALS['cache_array']['modules'][$key][$module] = $value;
164 } elseif (isDebugModeEnabled()) {
165 // Debug message only in debug-mode...
166 logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module . ' not found.');
170 sqlFreeResult($result);
177 // Checks whether the given module is locked by just checking the cache
178 function isModuleLocked ($module) {
179 // Determine if there a cache entry and is it set
180 $return = ((isset($GLOBALS['cache_array']['modules']['locked'][$module])) && ($GLOBALS['cache_array']['modules']['locked'][$module] == 'Y'));
182 // Return determined value
186 // Checks whether the given module is mem_only by just checking the cache
187 function isModuleMemberOnly ($module) {
188 // Determine if there a cache entry and is it set
189 $return = ((isset($GLOBALS['cache_array']['modules']['mem_only'][$module])) && ($GLOBALS['cache_array']['modules']['mem_only'][$module] == 'Y'));
191 // Return determined value
195 // Checks whether the given module is admin_only by just checking the cache
196 function isModuleAdminOnly ($module) {
197 // Determine if there a cache entry and is it set
198 $return = ((isset($GLOBALS['cache_array']['modules']['admin_only'][$module])) && ($GLOBALS['cache_array']['modules']['admin_only'][$module] == 'Y'));
200 // Return determined value
204 // Check validity of a given module name (no file extension)
205 function checkModulePermissions ($module = '') {
206 // Is it empty (default), then take the current one
207 if (empty($module)) {
208 // Use current module
209 $module = getModule();
213 if (isModuleStatusSet($module)) {
215 return getModuleStatus($module);
218 // Filter module name (names with low chars and underlines are fine!)
219 $module = preg_replace('/[^a-z_]/', '', $module);
221 // Check for prefix is a extension...
222 $modSplit = explode('_', $module);
223 $extension = ''; $module_chk = $module;
224 //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
225 if (count($modSplit) == 2) {
226 // Okay, there is a separator (_) in the name so is the first part a module?
227 //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
228 if (isExtensionActive($modSplit[0])) {
229 // The prefix is an extension's name, so let's set it
230 $extension = $modSplit[0]; $module = $modSplit[1];
234 // Major error in module registry is the default
235 setModuleStatus($module_chk, 'major');
237 // Check if script is installed if not return a 'done' to prevent some errors
238 if ((isInstaller()) || (!isAdminRegistered())) {
239 // Not installed or no admin registered or in installation phase
240 setModuleStatus($module_chk, 'done');
246 // Check if the module is registered
247 $found = isModuleRegistered($module_chk);
249 // Is the module found?
250 if ($found === TRUE) {
251 // Check returned values against current access permissions
253 // Admin access ----- Guest access ----- --- Guest or member? ---
254 if ((isAdmin()) || ((!isModuleLocked($module_chk)) && (!isModuleAdminOnly($module_chk)) && ((!isModuleMemberOnly($module_chk)) || (isMember())))) {
255 // If you are admin you are welcome for everything!
256 setModuleStatus($module_chk, 'done');
257 } elseif (isModuleLocked($module_chk)) {
259 setModuleStatus($module_chk, 'locked');
260 } elseif ((isModuleMemberOnly($module_chk)) && (!isMember())) {
261 // You have to login first!
262 setModuleStatus($module_chk, 'mem_only');
263 } elseif ((isModuleAdminOnly($module_chk)) && (!isAdmin())) {
264 // Only the Admin is allowed to enter this module!
265 setModuleStatus($module_chk, 'admin_only');
267 // @TODO Nothing helped???
268 logDebugMessage(__FUNCTION__, __LINE__, sprintf('ret=%s,locked=%d,mem=%d,admin=%d',
269 getModuleStatus($module_chk),
270 intval(isModuleLocked($module_chk)),
271 intval(isModuleMemberOnly($module_chk)),
272 intval(isModuleAdminOnly($module_chk))
277 // Still no luck or not found?
278 if (($found === FALSE) && (!isExtensionActive('cache')) && (getModuleStatus($module_chk) != 'done')) {
279 // ----- Default module ----- ---- Module in base folder ---- --- Module with extension's name ---
280 if ((isIncludeReadable(sprintf('inc/modules/%s.php', $module))) || (isIncludeReadable($module . '.php')) || (isIncludeReadable(sprintf('%s/%s.php', $extension, $module)))) {
281 // Data is missing so we add it
282 if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
284 * Since 0.3.6 there is a has_menu column, this took me a half
285 * hour to find a loop here... *sigh*
287 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
288 (`module`, `locked`, `mem_only`, `admin_only`, `has_menu`)
290 ('%s','Y'','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
292 // Wrong/missing sql_patches!
293 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
294 (`module`, `locked`, `mem_only`, `admin_only`)
296 ('%s','Y','N','N')", array($module_chk), __FUNCTION__, __LINE__);
299 // Everthing is fine?
300 if (ifSqlHasZeroAffectedRows()) {
301 // Something bad happend!
302 setModuleStatus($module_chk, 'major');
306 // Destroy cache here
307 // @TODO Rewrite this to a filter
308 if ((isHtmlOutputMode()) || (isRawOutputMode())) {
309 rebuildCache('modules', 'modules');
313 unset($GLOBALS['module_status'][$module_chk]);
314 return checkModulePermissions($module_chk);
316 // Module not found we don't add it to the database
317 setModuleStatus($module_chk, '404');
319 } elseif ((getModuleStatus($module_chk) == 'cache_miss') && (isHtmlOutputMode())) {
320 // Rebuild the cache files
321 rebuildCache('modules', 'modules');
322 } elseif ($found === FALSE) {
323 // Problem with module detected
324 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Problem in module %s detected. getModuleStatus()=%s,isModuleLocked()=%d,mem=%d,admin=%d,output_mode=%s',
326 getModuleStatus($module_chk),
327 intval(isModuleLocked($module_chk)),
328 intval(isModuleMemberOnly($module_chk)),
329 intval(isModuleAdminOnly($module_chk)),
330 getScriptOutputMode()
335 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf('module=%s, status=%s', $module_chk, getModuleStatus($module_chk)));
338 return getModuleStatus($module_chk);
341 // Checks if the module has a menu
342 function ifModuleHasMenu ($module, $forceDb = FALSE) {
343 // All is false by default
346 // Extension installed and newer than or has version 0.1.2?
347 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
348 // Cache version is okay, so let's check the cache!
349 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$module])) {
350 // Check module cache and count hit
351 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$module] == 'Y');
352 incrementStatsEntry('cache_hits');
353 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$module])) {
354 // Check cache and count hit
355 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$module] == 'Y');
356 incrementStatsEntry('cache_hits');
358 // Admin/guest/member/sponsor modules have always a menu!
359 $ret = in_array($module, array('admin', 'index', 'login', 'sponsor'));
361 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === TRUE))) {
362 // Check database for entry
363 $result = sqlQueryEscaped("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
364 array($module), __FUNCTION__, __LINE__);
367 if (sqlNumRows($result) == 1) {
368 // Load "has_menu" column
369 $data = sqlFetchArray($result);
372 $GLOBALS['cache_array']['extension']['ext_menu'][$module] = $data['has_menu'];
374 // Does it have a menu?
375 $ret = ($data['has_menu'] == 'Y');
379 sqlFreeResult($result);
380 } elseif (!isExtensionInstalled('sql_patches')) {
381 // No ext-sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
382 $ret = in_array($module, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
383 } elseif (!isInstaller()) {
384 // Unsupported state, but ignored in installation phase
385 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached, module[' . gettype($module) . ']=' . $module . ',forceDb=' . intval($forceDb));
392 // Adds a SQL for given module
393 function addModuleSql ($module, $title, $locked, $hidden, $adminOnly, $memOnly) {
394 // Is the module already registered?
395 if (!isModuleRegistered($module)) {
397 addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`, `title`, `locked`, `admin_only`, `mem_only`) VALUES ('" . $module . "', '" . $title . "', '" . $locked . "', '" . $adminOnly . "', '" . $memOnly . "')");
399 // Already registered
400 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Already registered: module=%s,locked=%s,admin=%s,mem=%s',
409 // Load the currently set module
410 function loadModule () {
411 // By default all modules are invalid
412 $isModuleValid = FALSE;
414 // Construct module name
415 $GLOBALS['module_inc'] = sprintf('inc/modules/%s.php', getModule());
417 // Check module permission (again)
418 $moduleState = checkModulePermissions();
420 // Which permission/error state do we have?
421 switch ($moduleState) {
422 case 'cache_miss': // The cache is gone
423 case 'admin_only': // Admin-only access
424 case 'mem_only': // Member-only access
425 case 'done': // All fine!
426 // Does the module exists on local file system?
427 if ((isIncludeReadable($GLOBALS['module_inc'])) && (!ifFatalErrorsDetected())) {
428 // Module is valid, active and located on the local disk...
429 $isModuleValid = TRUE;
430 } elseif (!ifFatalErrorsDetected()) {
432 setHttpStatus('404');
435 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_404--}');
437 // Set module to error module (non-existent!)
444 setHttpStatus('404');
447 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_404--}');
452 setHttpStatus('403 Forbidden');
454 if (!isIncludeReadable($GLOBALS['module_inc'])) {
455 // Set HTTP status again
456 setHttpStatus('404 Not Found');
458 // Module does addionally not exists
459 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_LOCKED_404--}');
463 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_IS_LOCKED--}');
467 // Unknown module status
468 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Unknown status %s return from module check. Module=%s', $moduleState, getModule()));
469 addFatalMessage(__FUNCTION__, __LINE__, '{%message,UNKNOWN_MODULE_STATUS=' . $moduleState . '%}');
474 return $isModuleValid;
478 function doIncludeModule () {
480 setContentType('text/html');
485 // Modules are by default not valid!
486 $isModuleValid = FALSE;
488 // By default NULL is used
489 $GLOBALS['module_inc'] = NULL;
491 // Is the maintenance mode active or goes all well?
492 if ((isExtensionActive('maintenance')) && (isMaintenanceEnabled()) && (!isAdmin()) && (getModule() != 'admin')) {
493 // Maintain mode is active and you are no admin
494 addFatalMessage(__FUNCTION__, __LINE__, '{--MAILER_DOWN_FOR_MAINTENANCE--}');
495 } elseif ((isSqlLinkUp()) && (!ifFatalErrorsDetected())) {
496 // Do the small "load module" call
497 $isModuleValid = loadModule();
498 } elseif (!ifFatalErrorsDetected()) {
499 // SQL problems detected
500 addFatalMessage(__FUNCTION__, __LINE__, '{--MYSQL_ERRORS--}');
503 // Is the module valid?
504 if (($isModuleValid === TRUE) && (!is_null($GLOBALS['module_inc']))) {
506 runFilterChain('pre_module_load');
508 // Everything is okay so we can load the module
509 loadIncludeOnce($GLOBALS['module_inc']);
512 runFilterChain('post_module_load');
515 // Add the footer (this will call doShutdown())
519 // "Getter" for menu mode from given module
520 function getMenuModeFromModule () {
522 if (!isset($GLOBALS[__FUNCTION__])) {
523 // Default is 'noindex' which is invalid for SQL tables but okay for meta data template
524 $GLOBALS[__FUNCTION__] = 'noindex';
526 // Determine it hard-coded
527 if (getModule() == 'login') {
529 $GLOBALS[__FUNCTION__] = 'member';
530 } elseif (getModule() == 'index') {
532 $GLOBALS[__FUNCTION__] = 'guest';
533 } elseif (getModule() == 'admin') {
535 $GLOBALS[__FUNCTION__] = 'admin';
536 } elseif (isInstaller()) {
537 // Is installation phase
538 $GLOBALS[__FUNCTION__] = 'install';
540 // Get it from filter
541 $GLOBALS[__FUNCTION__] = runFilterChain('determine_menu_mode');
546 return $GLOBALS[__FUNCTION__];