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 * -------------------------------------------------------------------- *
13 * $Revision:: 1910 $ *
14 * $Date:: 2010-06-29 06:00:23 +0200 (Tue, 29 Jun 2010) $ *
15 * $Tag:: 0.2.1-FINAL $ *
16 * $Author:: quix0r $ *
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?
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 $data['title'] = getMaskedMessage('UNKNOWN_MODULE_DETECTED', $module);
83 if ((is_resource($result)) && (SQL_HASZERONUMS($result))) {
84 // Add module to database
85 $dummy = checkModulePermissions($module);
90 return $data['title'];
93 // Check validity of a given module name (no file extension)
94 function checkModulePermissions ($module = '') {
95 // Is it empty (default), then take the current one
96 if (empty($module)) $module = getModule();
99 if (isset($GLOBALS['module_status'][$module])) {
101 return $GLOBALS['module_status'][$module];
104 // Filter module name (names with low chars and underlines are fine!)
105 $module = preg_replace('/[^a-z_]/', '', $module);
107 // Check for prefix is a extension...
108 $modSplit = explode('_', $module);
109 $extension = ''; $module_chk = $module;
110 //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
111 if (count($modSplit) == 2) {
112 // Okay, there is a seperator (_) in the name so is the first part a module?
113 //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
114 if (isExtensionActive($modSplit[0])) {
115 // The prefix is an extension's name, so let's set it
116 $extension = $modSplit[0]; $module = $modSplit[1];
120 // Major error in module registry is the default
123 // Check if script is installed if not return a 'done' to prevent some errors
124 if ((isInstallationPhase()) || (!isAdminRegistered())) {
125 // Not installed or no admin registered or in installation phase
137 // By default nothing is found
140 // Check if cache is latest version
141 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
142 // Is the cache there?
143 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using cache.');
144 if (isset($GLOBALS['cache_array']['modules']['locked'][$module_chk])) {
146 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache found.');
147 $data['locked'] = $GLOBALS['cache_array']['modules']['locked'][$module_chk];
148 $data['hidden'] = $GLOBALS['cache_array']['modules']['hidden'][$module_chk];
149 $data['admin_only'] = $GLOBALS['cache_array']['modules']['admin_only'][$module_chk];
150 $data['mem_only'] = $GLOBALS['cache_array']['modules']['mem_only'][$module_chk];
153 incrementStatsEntry('cache_hits');
156 // No, then we have to update it!
159 } elseif (!isExtensionActive('cache')) {
160 // Check for module in database
161 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Using database.');
162 $result = SQL_QUERY_ESC("SELECT `locked`, `hidden`, `admin_only`, `mem_only` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
163 array($module_chk), __FUNCTION__, __LINE__);
164 if (SQL_NUMROWS($result) == 1) {
166 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Entry found.');
167 $data = SQL_FETCHARRAY($result);
169 } elseif (isDebugModeEnabled()) {
170 // Debug message only in debug-mode...
171 logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module_chk . ' not found!');
175 SQL_FREERESULT($result);
177 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
179 // Is the module found?
180 if ($found === true) {
181 // Check returned values against current access permissions
183 // Admin access ----- Guest access ----- --- Guest or member? ---
184 if ((isAdmin()) || (($data['locked'] != 'Y') && ($data['admin_only'] != 'Y') && (($data['mem_only'] != 'Y') || (isMember())))) {
185 // If you are admin you are welcome for everything!
187 } elseif ($data['locked'] == 'Y') {
190 } elseif (($data['mem_only'] == 'Y') && (!isMember())) {
191 // You have to login first!
193 } elseif (($data['admin_only'] == 'Y') && (!isAdmin())) {
194 // Only the Admin is allowed to enter this module!
197 // @TODO Nothing helped???
198 logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,admin=%s,mem=%s",
207 // Still no luck or not found?
208 if (($found === false) && (!isExtensionActive('cache')) && ($ret != 'done')) {
209 // ----- Legacy module ----- ---- Module in base folder ---- --- Module with extension's name ---
210 if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) {
211 // Data is missing so we add it
212 if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
213 // Since 0.3.6 we have a has_menu column, this took me a half hour
214 // to find a loop here... *sigh*
215 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
216 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`) VALUES
217 ('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
219 // Wrong/missing sql_patches!
220 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
221 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`) VALUES
222 ('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
225 // Everthing is fine?
226 if (SQL_AFFECTEDROWS() < 1) {
227 // Something bad happend!
231 // Destroy cache here
232 // @TODO Rewrite this to a filter
233 if ((getOutputMode() == '0') || (getOutputMode() == -1)) rebuildCache('modules', 'modules');
236 unset($GLOBALS['module_status'][$module]);
237 $ret = checkModulePermissions($module_chk);
239 // Module not found we don't add it to the database
242 } elseif (($ret == 'cache_miss') && (getOutputMode() == '0')) {
243 // Rebuild the cache files
244 rebuildCache('modules', 'modules');
245 } elseif ($found === false) {
246 // Problem with module detected
247 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. ret=%s, locked=%s, hidden=%s, mem=%s, admin=%s, output_mode=%s",
259 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
260 $GLOBALS['module_status'][$module] = $ret;
264 // Checks if the module has a menu
265 function ifModuleHasMenu ($mod, $forceDb = false) {
266 // All is false by default
269 // Extension installed and newer than or has version 0.1.2?
270 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
271 // Cache version is okay, so let's check the cache!
272 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
273 // Check module cache and count hit
274 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
275 incrementStatsEntry('cache_hits');
276 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
277 // Check cache and count hit
278 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
279 incrementStatsEntry('cache_hits');
281 // Admin/guest/member/sponsor modules have always a menu!
282 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
284 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
285 // Check database for entry
286 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
287 array($mod), __FUNCTION__, __LINE__);
290 if (SQL_NUMROWS($result) == 1) {
291 // Load "has_menu" column
292 $data = SQL_FETCHARRAY($result);
295 $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
297 // Does it have a menu?
298 $ret = ($data['has_menu'] == 'Y');
302 SQL_FREERESULT($result);
303 } elseif (!isExtensionInstalled('sql_patches')) {
304 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
305 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
307 // Unsupported state!
308 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');