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 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // "Getter" for module title
46 function getModuleTitle ($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)) {
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'] = getMessage('MODULE_ERROR_404');
87 $data['title'] = getMaskedMessage('UNKNOWN_MODULE_DETECTED', $module);
88 if ((is_resource($result)) && (SQL_HASZERONUMS($result))) {
89 // Add module to database
90 $dummy = checkModulePermissions($module);
96 return $data['title'];
99 // Check validity of a given module name (no file extension)
100 function checkModulePermissions ($module = '') {
101 // Is it empty (default), then take the current one
102 if (empty($module)) $module = getModule();
105 if (isset($GLOBALS['module_status'][$module])) {
107 return $GLOBALS['module_status'][$module];
110 // Filter module name (names with low chars and underlines are fine!)
111 $module = preg_replace('/[^a-z_]/', '', $module);
113 // Check for prefix is a extension...
114 $modSplit = explode('_', $module);
115 $extension = ''; $module_chk = $module;
116 //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
117 if (count($modSplit) == 2) {
118 // Okay, there is a seperator (_) in the name so is the first part a module?
119 //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
120 if (isExtensionActive($modSplit[0])) {
121 // The prefix is an extension's name, so let's set it
122 $extension = $modSplit[0]; $module = $modSplit[1];
126 // Major error in module registry is the default
129 // Check if script is installed if not return a 'done' to prevent some errors
130 if ((isInstallationPhase()) || (!isAdminRegistered())) {
131 // Not installed or no admin registered or in installation phase
143 // By default nothing is found
146 // Check if cache is latest version
147 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
148 // Is the cache there?
149 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using cache.');
150 if (isset($GLOBALS['cache_array']['modules']['locked'][$module_chk])) {
152 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache found.');
153 $data['locked'] = $GLOBALS['cache_array']['modules']['locked'][$module_chk];
154 $data['hidden'] = $GLOBALS['cache_array']['modules']['hidden'][$module_chk];
155 $data['admin_only'] = $GLOBALS['cache_array']['modules']['admin_only'][$module_chk];
156 $data['mem_only'] = $GLOBALS['cache_array']['modules']['mem_only'][$module_chk];
159 incrementStatsEntry('cache_hits');
162 // No, then we have to update it!
165 } elseif (!isExtensionActive('cache')) {
166 // Check for module in database
167 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
168 $result = SQL_QUERY_ESC("SELECT `locked`, `hidden`, `admin_only`, `mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
169 array($module_chk), __FUNCTION__, __LINE__);
170 if (SQL_NUMROWS($result) == 1) {
172 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Entry found.');
173 $data = SQL_FETCHARRAY($result);
175 } elseif (isDebugModeEnabled()) {
176 // Debug message only in debug-mode...
177 logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module_chk . ' not found!');
181 SQL_FREERESULT($result);
183 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
185 // Is the module found?
186 if ($found === true) {
187 // Check returned values against current access permissions
189 // Admin access ----- Guest access ----- --- Guest or member? ---
190 if ((isAdmin()) || (($data['locked'] != 'Y') && ($data['admin_only'] != 'Y') && (($data['mem_only'] != 'Y') || (isMember())))) {
191 // If you are admin you are welcome for everything!
193 } elseif ($data['locked'] == 'Y') {
196 } elseif (($data['mem_only'] == 'Y') && (!isMember())) {
197 // You have to login first!
199 } elseif (($data['admin_only'] == 'Y') && (!isAdmin())) {
200 // Only the Admin is allowed to enter this module!
203 // @TODO Nothing helped???
204 logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,admin=%s,mem=%s",
213 // Still no luck or not found?
214 if (($found === false) && (!isExtensionActive('cache')) && ($ret != 'done')) {
215 // ----- Legacy module ----- ---- Module in base folder ---- --- Module with extension's name ---
216 if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) {
217 // Data is missing so we add it
218 if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
219 // Since 0.3.6 we have a has_menu column, this took me a half hour
220 // to find a loop here... *sigh*
221 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
222 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`) VALUES
223 ('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
225 // Wrong/missing sql_patches!
226 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
227 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`) VALUES
228 ('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
231 // Everthing is fine?
232 if (SQL_AFFECTEDROWS() < 1) {
233 // Something bad happend!
237 // Destroy cache here
238 // @TODO Rewrite this to a filter
239 if ((isHtmlOutputMode()) || (isRawOutputMode())) rebuildCache('modules', 'modules');
242 unset($GLOBALS['module_status'][$module]);
243 $ret = checkModulePermissions($module_chk);
245 // Module not found we don't add it to the database
248 } elseif (($ret == 'cache_miss') && (isHtmlOutputMode())) {
249 // Rebuild the cache files
250 rebuildCache('modules', 'modules');
251 } elseif ($found === false) {
252 // Problem with module detected
253 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. ret=%s, locked=%s, hidden=%s, mem=%s, admin=%s, output_mode=%s",
260 getScriptOutputMode()
265 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
266 $GLOBALS['module_status'][$module] = $ret;
270 // Checks if the module has a menu
271 function ifModuleHasMenu ($mod, $forceDb = false) {
272 // All is false by default
275 // Extension installed and newer than or has version 0.1.2?
276 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
277 // Cache version is okay, so let's check the cache!
278 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
279 // Check module cache and count hit
280 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
281 incrementStatsEntry('cache_hits');
282 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
283 // Check cache and count hit
284 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
285 incrementStatsEntry('cache_hits');
287 // Admin/guest/member/sponsor modules have always a menu!
288 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
290 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
291 // Check database for entry
292 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
293 array($mod), __FUNCTION__, __LINE__);
296 if (SQL_NUMROWS($result) == 1) {
297 // Load "has_menu" column
298 $data = SQL_FETCHARRAY($result);
301 $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
303 // Does it have a menu?
304 $ret = ($data['has_menu'] == 'Y');
308 SQL_FREERESULT($result);
309 } elseif (!isExtensionInstalled('sql_patches')) {
310 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
311 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
313 // Unsupported state!
314 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');