Added update_year.sh (still not fully flexible) and updated all years with it.
[mailer.git] / inc / module-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 07/01/2010 *
4  * ===================                          Last change: 07/01/2010 *
5  *                                                                      *
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                      *
16  *                                                                      *
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.                                  *
21  *                                                                      *
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.                         *
26  *                                                                      *
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,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
38 // "Getter" for module title
39 function getModuleTitle ($module) {
40         // Is there cache?
41         if (!isset($GLOBALS[__FUNCTION__][$module])) {
42                 // Init variables
43                 $data['title'] = '';
44                 $result = FALSE;
45
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']))) {
50                                 // Load from cache
51                                 $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module];
52
53                                 // Update cache hits
54                                 incrementStatsEntry('cache_hits');
55                         } elseif (!isExtensionActive('cache')) {
56                                 // Load from database
57                                 $result = sqlQueryEscaped("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
58                                         array($module), __FUNCTION__, __LINE__);
59
60                                 // Is the entry there?
61                                 if (sqlNumRows($result) == 1) {
62                                         // Get the title from database
63                                         $data = sqlFetchArray($result);
64                                 } // END - if
65
66                                 // Free the result
67                                 sqlFreeResult($result);
68                         }
69                 } // END - if
70
71                 // Trim name
72                 $data['title'] = trim($data['title']);
73
74                 // Still no luck or empty title?
75                 if (empty($data['title'])) {
76                         // Is it 'error'?
77                         if ($module == 'error') {
78                                 // Error (real module was not found)
79                                 $data['title'] = '{--MODULE_ERROR_404_TITLE--}';
80                         }  else {
81                                 // No name found
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);
86                                 } // END - if
87                         }
88                 } // END - if
89
90                 // Store in cache
91                 $GLOBALS[__FUNCTION__][$module] = $data['title'];
92         } // END - if
93
94         // Return it
95         return $GLOBALS[__FUNCTION__][$module];
96 }
97
98 // Checks if module_status entry is there
99 function isModuleStatusSet ($module) {
100         // Check it
101         return (isset($GLOBALS['module_status'][$module]));
102 }
103
104 // Setter module status
105 function setModuleStatus ($module, $status) {
106         $GLOBALS['module_status'][$module] = $status;
107 }
108
109 // Getter for module status
110 function getModuleStatus ($module) {
111         // Is the module_status entry there?
112         if (!isModuleStatusSet($module)) {
113                 // Abort
114                 reportBug(__FUNCTION__, __LINE__, 'Module status not set. module=' . $module);
115         } // END - if
116
117         // Return it
118         return $GLOBALS['module_status'][$module];
119 }
120
121 // Checks whether the given module is registered
122 function isModuleRegistered ($module) {
123         // By default nothing is found
124         $found  = FALSE;
125
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])) {
131                         // Update cache hits
132                         incrementStatsEntry('cache_hits');
133
134                         // Is found
135                         $found = TRUE;
136                 } else {
137                         // No, then we have to update it!
138                         setModuleStatus($module, 'cache_miss');
139                 }
140         } elseif (!isExtensionActive('cache')) {
141                 // Check for module in database
142                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
143                 $result = sqlQueryEscaped("SELECT
144         `locked`,
145         `admin_only`,
146         `mem_only`
147 FROM
148         `{?_MYSQL_PREFIX?}_mod_reg`
149 WHERE
150         `module`='%s'
151 LIMIT 1",
152                         array($module), __FUNCTION__, __LINE__);
153                 if (sqlNumRows($result) == 1) {
154                         // Read data
155                         $data = sqlFetchArray($result);
156
157                         // Set all entries
158                         foreach ($data as $key => $value) {
159                                 $GLOBALS['cache_array']['modules'][$key][$module] = $value;
160                         } // END - foreach
161
162                         // Mark as found
163                         $found = TRUE;
164                 } elseif (isDebugModeEnabled()) {
165                         // Debug message only in debug-mode...
166                         logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module . ' not found.');
167                 }
168
169                 // Free result
170                 sqlFreeResult($result);
171         }
172
173         // Return status
174         return $found;
175 }
176
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'));
181
182         // Return determined value
183         return $return;
184 }
185
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'));
190
191         // Return determined value
192         return $return;
193 }
194
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'));
199
200         // Return determined value
201         return $return;
202 }
203
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();
210         } // END - if
211
212         // Is there cache?
213         if (isModuleStatusSet($module)) {
214                 // Then use it
215                 return getModuleStatus($module);
216         } // END - if
217
218         // Filter module name (names with low chars and underlines are fine!)
219         $module = preg_replace('/[^a-z_]/', '', $module);
220
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];
231                 } // END - if
232         } // END - if
233
234         // Major error in module registry is the default
235         setModuleStatus($module_chk, 'major');
236
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');
241
242                 // Return status
243                 return 'done';
244         } // END - if
245
246         // Check if the module is registered
247         $found = isModuleRegistered($module_chk);
248
249         // Is the module found?
250         if ($found === TRUE) {
251                 // Check returned values against current access permissions
252                 //
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)) {
258                         // Module is locked
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');
266                 } else {
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))
273                         ));
274                 }
275         } // END - if
276
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')) {
283                                 /*
284                                  * Since 0.3.6 there is a has_menu column, this took me a half
285                                  * hour to find a loop here... *sigh*
286                                  */
287                                 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
288 (`module`, `locked`, `mem_only`, `admin_only`, `has_menu`)
289 VALUES
290 ('%s','Y'','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
291                         } else {
292                                 // Wrong/missing sql_patches!
293                                 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
294 (`module`, `locked`, `mem_only`, `admin_only`)
295 VALUES
296 ('%s','Y','N','N')", array($module_chk), __FUNCTION__, __LINE__);
297                         }
298
299                         // Everthing is fine?
300                         if (ifSqlHasZeroAffectedRows()) {
301                                 // Something bad happend!
302                                 setModuleStatus($module_chk, 'major');
303                                 return 'major';
304                         } // END - if
305
306                         // Destroy cache here
307                         // @TODO Rewrite this to a filter
308                         if ((isHtmlOutputMode()) || (isRawOutputMode())) {
309                                 rebuildCache('modules', 'modules');
310                         } // END - if
311
312                         // And reload data
313                         unset($GLOBALS['module_status'][$module_chk]);
314                         return checkModulePermissions($module_chk);
315                 } else {
316                         // Module not found we don't add it to the database
317                         setModuleStatus($module_chk, '404');
318                 }
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',
325                         $module_chk,
326                         getModuleStatus($module_chk),
327                         intval(isModuleLocked($module_chk)),
328                         intval(isModuleMemberOnly($module_chk)),
329                         intval(isModuleAdminOnly($module_chk)),
330                         getScriptOutputMode()
331                 ));
332         }
333
334         // Debug log
335         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf('module=%s, status=%s', $module_chk, getModuleStatus($module_chk)));
336
337         // Return the value
338         return getModuleStatus($module_chk);
339 }
340
341 // Checks if the module has a menu
342 function ifModuleHasMenu ($module, $forceDb = FALSE) {
343         // All is false by default
344         $ret = FALSE;
345
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');
357                 } else {
358                         // Admin/guest/member/sponsor modules have always a menu!
359                         $ret = in_array($module, array('admin', 'index', 'login', 'sponsor'));
360                 }
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__);
365
366                 // Entry found?
367                 if (sqlNumRows($result) == 1) {
368                         // Load "has_menu" column
369                         $data = sqlFetchArray($result);
370
371                         // Fake cache... ;-)
372                         $GLOBALS['cache_array']['extension']['ext_menu'][$module] = $data['has_menu'];
373
374                         // Does it have a menu?
375                         $ret = ($data['has_menu'] == 'Y');
376                 } // END  - if
377
378                 // Free memory
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));
386         }
387
388         // Return status
389         return $ret;
390 }
391
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)) {
396                 // Add it
397                 addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`, `title`, `locked`, `admin_only`, `mem_only`) VALUES ('" . $module . "', '" . $title . "', '" . $locked . "', '" . $adminOnly . "', '" . $memOnly . "')");
398         } else {
399                 // Already registered
400                 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Already registered: module=%s,locked=%s,admin=%s,mem=%s',
401                         $module,
402                         $locked,
403                         $adminOnly,
404                         $memOnly
405                 ));
406         }
407 }
408
409 // Load the currently set module
410 function loadModule () {
411         // By default all modules are invalid
412         $isModuleValid = FALSE;
413
414         // Construct module name
415         $GLOBALS['module_inc'] = sprintf('inc/modules/%s.php', getModule());
416
417         // Check module permission (again)
418         $moduleState = checkModulePermissions();
419
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()) {
431                                 // Set HTTP status
432                                 setHttpStatus('404');
433
434                                 // Module not found
435                                 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_404--}');
436
437                                 // Set module to error module (non-existent!)
438                                 setModule('error');
439                         }
440                         break;
441
442                 case '404':
443                         // Set HTTP status
444                         setHttpStatus('404');
445
446                         // Add fatal message
447                         addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_404--}');
448                         break;
449
450                 case 'locked':
451                         // Set HTTP status
452                         setHttpStatus('403 Forbidden');
453
454                         if (!isIncludeReadable($GLOBALS['module_inc'])) {
455                                 // Set HTTP status again
456                                 setHttpStatus('404 Not Found');
457
458                                 // Module does addionally not exists
459                                 addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_LOCKED_404--}');
460                         } // END - if
461
462                         // Add fatal message
463                         addFatalMessage(__FUNCTION__, __LINE__, '{--MODULE_REGISTRY_IS_LOCKED--}');
464                         break;
465
466                 default:
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 . '%}');
470                         break;
471         } // END - switch
472
473         // Return status
474         return $isModuleValid;
475 }
476
477 // Include module
478 function doIncludeModule () {
479         // Set content type
480         setContentType('text/html');
481
482         // Load page header
483         loadPageHeader();
484
485         // Modules are by default not valid!
486         $isModuleValid = FALSE;
487
488         // By default NULL is used
489         $GLOBALS['module_inc'] = NULL;
490
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--}');
501         }
502
503         // Is the module valid?
504         if (($isModuleValid === TRUE) && (!is_null($GLOBALS['module_inc']))) {
505                 // Run pre-filter
506                 runFilterChain('pre_module_load');
507
508                 // Everything is okay so we can load the module
509                 loadIncludeOnce($GLOBALS['module_inc']);
510
511                 // Run post-filter
512                 runFilterChain('post_module_load');
513         } // END - if
514
515         // Add the footer (this will call doShutdown())
516         loadPageFooter();
517 }
518
519 // "Getter" for menu mode from given module
520 function getMenuModeFromModule () {
521         // Is cache set?
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';
525
526                 // Determine it hard-coded
527                 if (getModule() == 'login') {
528                         // Is member area
529                         $GLOBALS[__FUNCTION__] = 'member';
530                 } elseif (getModule() == 'index') {
531                         // Is guest area
532                         $GLOBALS[__FUNCTION__] = 'guest';
533                 } elseif (getModule() == 'admin') {
534                         // Is admin area
535                         $GLOBALS[__FUNCTION__] = 'admin';
536                 } elseif (isInstaller()) {
537                         // Is installation phase
538                         $GLOBALS[__FUNCTION__] = 'install';
539                 } else {
540                         // Get it from filter
541                         $GLOBALS[__FUNCTION__] = runFilterChain('determine_menu_mode');
542                 }
543         } // END - if
544
545         // Return it
546         return $GLOBALS[__FUNCTION__];
547 }
548
549 // [EOF]
550 ?>