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 // Checks if module_status entry is there
98 function isModuleStatusSet ($module) {
100 return (isset($GLOBALS['module_status'][$module]));
103 // Setter module status
104 function setModuleStatus ($module, $status) {
105 $GLOBALS['module_status'][$module] = $status;
108 // Getter for module status
109 function getModuleStatus ($module) {
110 // Is the module_status entry there?
111 if (!isModuleStatusSet($module)) {
113 debug_report_bug('Module status not set. module=' . $module);
117 return $GLOBALS['module_status'][$module];
120 // Checks wether the given module is registered
121 function isModuleRegistered ($module) {
122 // By default nothing is found
125 // Check if cache is latest version
126 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
127 // Is the cache there?
128 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using cache.');
129 if (isset($GLOBALS['cache_array']['modules']['locked'][$module])) {
131 incrementStatsEntry('cache_hits');
136 // No, then we have to update it!
137 setModuleStatus($module, 'cache_miss');
139 } elseif (!isExtensionActive('cache')) {
140 // Check for module in database
141 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
142 $result = SQL_QUERY_ESC("SELECT `locked`, `hidden`, `admin_only`, `mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
143 array($module), __FUNCTION__, __LINE__);
144 if (SQL_NUMROWS($result) == 1) {
146 $data = SQL_FETCHARRAY($result);
149 foreach ($data as $key=>$value) {
150 $GLOBALS['cache_array']['modules'][$key][$module] = $value;
155 } elseif (isDebugModeEnabled()) {
156 // Debug message only in debug-mode...
157 logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module . ' not found!');
161 SQL_FREERESULT($result);
168 // Check validity of a given module name (no file extension)
169 function checkModulePermissions ($module = '') {
170 // Is it empty (default), then take the current one
171 if (empty($module)) {
172 // Use current module
173 $module = getModule();
177 if (isModuleStatusSet($module)) {
179 return getModuleStatus($module);
182 // Filter module name (names with low chars and underlines are fine!)
183 $module = preg_replace('/[^a-z_]/', '', $module);
185 // Check for prefix is a extension...
186 $modSplit = explode('_', $module);
187 $extension = ''; $module_chk = $module;
188 //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
189 if (count($modSplit) == 2) {
190 // Okay, there is a seperator (_) in the name so is the first part a module?
191 //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
192 if (isExtensionActive($modSplit[0])) {
193 // The prefix is an extension's name, so let's set it
194 $extension = $modSplit[0]; $module = $modSplit[1];
198 // Major error in module registry is the default
199 setModuleStatus($module_chk, 'major');
201 // Check if script is installed if not return a 'done' to prevent some errors
202 if ((isInstallationPhase()) || (!isAdminRegistered())) {
203 // Not installed or no admin registered or in installation phase
204 setModuleStatus($module_chk, 'done');
210 // Check if the module is registered
211 $found = isModuleRegistered($module_chk);
213 // Is the module found?
214 if ($found === true) {
215 // Check returned values against current access permissions
217 // Admin access ----- Guest access ----- --- Guest or member? ---
218 if ((isAdmin()) || (($GLOBALS['cache_array']['modules']['locked'][$module_chk] != 'Y') && ($GLOBALS['cache_array']['modules']['admin_only'][$module_chk] != 'Y') && (($GLOBALS['cache_array']['modules']['mem_only'][$module_chk] != 'Y') || (isMember())))) {
219 // If you are admin you are welcome for everything!
220 setModuleStatus($module_chk, 'done');
221 } elseif ($GLOBALS['cache_array']['modules']['locked'][$module_chk] == 'Y') {
223 setModuleStatus($module_chk, 'locked');
224 } elseif (($GLOBALS['cache_array']['modules']['mem_only'][$module_chk] == 'Y') && (!isMember())) {
225 // You have to login first!
226 setModuleStatus($module_chk, 'mem_only');
227 } elseif (($GLOBALS['cache_array']['modules']['admin_only'][$module_chk] == 'Y') && (!isAdmin())) {
228 // Only the Admin is allowed to enter this module!
229 setModuleStatus($module_chk, 'admin_only');
231 // @TODO Nothing helped???
232 logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,hidden=%s,mem=%s,admin=%s",
233 getModuleStatus($module_chk),
234 $GLOBALS['cache_array']['modules']['locked'][$module_chk],
235 $GLOBALS['cache_array']['modules']['hidden'][$module_chk],
236 $GLOBALS['cache_array']['modules']['mem_only'][$module_chk],
237 $GLOBALS['cache_array']['modules']['admin_only'][$module_chk]
242 // Still no luck or not found?
243 if (($found === false) && (!isExtensionActive('cache')) && (getModuleStatus($module_chk) != 'done')) {
244 // ----- Default module ----- ---- Module in base folder ---- --- Module with extension's name ---
245 if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) {
246 // Data is missing so we add it
247 if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
249 * Since 0.3.6 we have a has_menu column, this took me a half
250 * hour to find a loop here... *sigh*
252 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
253 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`)
255 ('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
257 // Wrong/missing sql_patches!
258 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
259 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`)
261 ('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
264 // Everthing is fine?
265 if (SQL_AFFECTEDROWS() < 1) {
266 // Something bad happend!
267 setModuleStatus($module_chk, 'major');
271 // Destroy cache here
272 // @TODO Rewrite this to a filter
273 if ((isHtmlOutputMode()) || (isRawOutputMode())) {
274 rebuildCache('modules', 'modules');
278 unset($GLOBALS['module_status'][$module_chk]);
279 return checkModulePermissions($module_chk);
281 // Module not found we don't add it to the database
282 setModuleStatus($module_chk, '404');
284 } elseif ((getModuleStatus($module_chk) == 'cache_miss') && (isHtmlOutputMode())) {
285 // Rebuild the cache files
286 rebuildCache('modules', 'modules');
287 } elseif ($found === false) {
288 // Problem with module detected
289 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. ret=%s, locked=%s, hidden=%s, mem=%s, admin=%s, output_mode=%s",
291 getModuleStatus($module_chk),
292 $GLOBALS['cache_array']['modules']['locked'][$module_chk],
293 $GLOBALS['cache_array']['modules']['hidden'][$module_chk],
294 $GLOBALS['cache_array']['modules']['mem_only'][$module_chk],
295 $GLOBALS['cache_array']['modules']['admin_only'][$module_chk],
296 getScriptOutputMode()
301 logDebugMessage(__FUNCTION__, __LINE__, sprintf("module=%s, status=%s", $module_chk, getModuleStatus($module_chk)));
304 return getModuleStatus($module_chk);
307 // Checks if the module has a menu
308 function ifModuleHasMenu ($module, $forceDb = false) {
309 // All is false by default
312 // Extension installed and newer than or has version 0.1.2?
313 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
314 // Cache version is okay, so let's check the cache!
315 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$module])) {
316 // Check module cache and count hit
317 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$module] == 'Y');
318 incrementStatsEntry('cache_hits');
319 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$module])) {
320 // Check cache and count hit
321 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$module] == 'Y');
322 incrementStatsEntry('cache_hits');
324 // Admin/guest/member/sponsor modules have always a menu!
325 $ret = in_array($module, array('admin', 'index', 'login', 'sponsor'));
327 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
328 // Check database for entry
329 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
330 array($module), __FUNCTION__, __LINE__);
333 if (SQL_NUMROWS($result) == 1) {
334 // Load "has_menu" column
335 $data = SQL_FETCHARRAY($result);
338 $GLOBALS['cache_array']['extension']['ext_menu'][$module] = $data['has_menu'];
340 // Does it have a menu?
341 $ret = ($data['has_menu'] == 'Y');
345 SQL_FREERESULT($result);
346 } elseif (!isExtensionInstalled('sql_patches')) {
347 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
348 $ret = in_array($module, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
350 // Unsupported state!
351 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
358 // Adds a SQL for given module
359 function addModuleSql ($module, $locked, $hidden, $adminOnly, $memOnly) {
360 // Is the module already registered?
361 if (!isModuleRegistered($module)) {
363 addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`,`locked`,`hidden`,`admin_only`,`mem_only`) VALUES('" . $module . "','" . $locked . "','" . $hidden . "','" . $adminOnly . "','" . $memOnly . "')");
365 // Already registered
366 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Already registered: module=%s,locked=%s,hidden=%s,admin=%s,mem=%s",