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 : All MySQL-related functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Alle MySQL-Relevanten Funktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009, 2010 by Mailer Developer Team *
20 * For more information visit: http://www.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) {
49 // Is the script installed?
50 if ((isInstalled()) && ($module != 'error')) {
51 // Check if cache is valid
52 if ((isExtensionInstalledAndNewer('cache', '0.1.2')) && (isset($GLOBALS['cache_array']['modules']['module'])) && (in_array($module, $GLOBALS['cache_array']['modules']['module']))) {
54 $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module];
57 incrementStatsEntry('cache_hits');
58 } elseif (!isExtensionActive('cache')) {
60 $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
61 array($module), __FUNCTION__, __LINE__);
63 // Is the entry there?
64 if (SQL_NUMROWS($result)) {
65 // Get the title from database
66 $data = SQL_FETCHARRAY($result);
70 SQL_FREERESULT($result);
75 $data['title'] = trim($data['title']);
77 // Still no luck or empty title?
78 if (empty($data['title'])) {
80 if ($module == 'error') {
81 // Error (real module was not found)
82 $data['title'] = getMessage('MODULE_ERROR_404');
85 $data['title'] = getMaskedMessage('UNKNOWN_MODULE_DETECTED', $module);
86 if ((is_resource($result)) && (SQL_HASZERONUMS($result))) {
87 // Add module to database
88 $dummy = checkModulePermissions($module);
94 return $data['title'];
97 // Check validity of a given module name (no file extension)
98 function checkModulePermissions ($module = '') {
99 // Is it empty (default), then take the current one
100 if (empty($module)) $module = getModule();
103 if (isset($GLOBALS['module_status'][$module])) {
105 return $GLOBALS['module_status'][$module];
108 // Filter module name (names with low chars and underlines are fine!)
109 $module = preg_replace('/[^a-z_]/', '', $module);
111 // Check for prefix is a extension...
112 $modSplit = explode('_', $module);
113 $extension = ''; $module_chk = $module;
114 //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
115 if (count($modSplit) == 2) {
116 // Okay, there is a seperator (_) in the name so is the first part a module?
117 //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
118 if (isExtensionActive($modSplit[0])) {
119 // The prefix is an extension's name, so let's set it
120 $extension = $modSplit[0]; $module = $modSplit[1];
124 // Major error in module registry is the default
127 // Check if script is installed if not return a 'done' to prevent some errors
128 if ((isInstallationPhase()) || (!isAdminRegistered())) {
129 // Not installed or no admin registered or in installation phase
141 // By default nothing is found
144 // Check if cache is latest version
145 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
146 // Is the cache there?
147 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using cache.');
148 if (isset($GLOBALS['cache_array']['modules']['locked'][$module_chk])) {
150 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache found.');
151 $data['locked'] = $GLOBALS['cache_array']['modules']['locked'][$module_chk];
152 $data['hidden'] = $GLOBALS['cache_array']['modules']['hidden'][$module_chk];
153 $data['admin_only'] = $GLOBALS['cache_array']['modules']['admin_only'][$module_chk];
154 $data['mem_only'] = $GLOBALS['cache_array']['modules']['mem_only'][$module_chk];
157 incrementStatsEntry('cache_hits');
160 // No, then we have to update it!
163 } elseif (!isExtensionActive('cache')) {
164 // Check for module in database
165 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
166 $result = SQL_QUERY_ESC("SELECT `locked`, `hidden`, `admin_only`, `mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
167 array($module_chk), __FUNCTION__, __LINE__);
168 if (SQL_NUMROWS($result) == 1) {
170 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Entry found.');
171 $data = SQL_FETCHARRAY($result);
173 } elseif (isDebugModeEnabled()) {
174 // Debug message only in debug-mode...
175 logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module_chk . ' not found!');
179 SQL_FREERESULT($result);
181 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
183 // Is the module found?
184 if ($found === true) {
185 // Check returned values against current access permissions
187 // Admin access ----- Guest access ----- --- Guest or member? ---
188 if ((isAdmin()) || (($data['locked'] != 'Y') && ($data['admin_only'] != 'Y') && (($data['mem_only'] != 'Y') || (isMember())))) {
189 // If you are admin you are welcome for everything!
191 } elseif ($data['locked'] == 'Y') {
194 } elseif (($data['mem_only'] == 'Y') && (!isMember())) {
195 // You have to login first!
197 } elseif (($data['admin_only'] == 'Y') && (!isAdmin())) {
198 // Only the Admin is allowed to enter this module!
201 // @TODO Nothing helped???
202 logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,admin=%s,mem=%s",
211 // Still no luck or not found?
212 if (($found === false) && (!isExtensionActive('cache')) && ($ret != 'done')) {
213 // ----- Legacy module ----- ---- Module in base folder ---- --- Module with extension's name ---
214 if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) {
215 // Data is missing so we add it
216 if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
217 // Since 0.3.6 we have a has_menu column, this took me a half hour
218 // to find a loop here... *sigh*
219 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
220 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`) VALUES
221 ('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
223 // Wrong/missing sql_patches!
224 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
225 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`) VALUES
226 ('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
229 // Everthing is fine?
230 if (SQL_AFFECTEDROWS() < 1) {
231 // Something bad happend!
235 // Destroy cache here
236 // @TODO Rewrite this to a filter
237 if ((isHtmlOutputMode()) || (isRawOutputMode())) rebuildCache('modules', 'modules');
240 unset($GLOBALS['module_status'][$module]);
241 $ret = checkModulePermissions($module_chk);
243 // Module not found we don't add it to the database
246 } elseif (($ret == 'cache_miss') && (isHtmlOutputMode())) {
247 // Rebuild the cache files
248 rebuildCache('modules', 'modules');
249 } elseif ($found === false) {
250 // Problem with module detected
251 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. ret=%s, locked=%s, hidden=%s, mem=%s, admin=%s, output_mode=%s",
258 getScriptOutputMode()
263 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
264 $GLOBALS['module_status'][$module] = $ret;
268 // Checks if the module has a menu
269 function ifModuleHasMenu ($mod, $forceDb = false) {
270 // All is false by default
273 // Extension installed and newer than or has version 0.1.2?
274 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
275 // Cache version is okay, so let's check the cache!
276 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
277 // Check module cache and count hit
278 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
279 incrementStatsEntry('cache_hits');
280 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
281 // Check cache and count hit
282 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
283 incrementStatsEntry('cache_hits');
285 // Admin/guest/member/sponsor modules have always a menu!
286 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
288 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
289 // Check database for entry
290 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
291 array($mod), __FUNCTION__, __LINE__);
294 if (SQL_NUMROWS($result) == 1) {
295 // Load "has_menu" column
296 $data = SQL_FETCHARRAY($result);
299 $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
301 // Does it have a menu?
302 $ret = ($data['has_menu'] == 'Y');
306 SQL_FREERESULT($result);
307 } elseif (!isExtensionInstalled('sql_patches')) {
308 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
309 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
311 // Unsupported state!
312 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');