A lot CSS classes rewritten, please update all your themes.
[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 : All MySQL-related functions                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle MySQL-Relevanten Funktionen                 *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
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                  *
23  *                                                                      *
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.                                  *
28  *                                                                      *
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.                         *
33  *                                                                      *
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,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // "Getter" for module title
46 function getModuleTitle ($module) {
47         // Init variables
48         $data['title'] = '';
49         $result = false;
50
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']))) {
55                         // Load from cache
56                         $data['title'] = $GLOBALS['cache_array']['modules']['title'][$module];
57
58                         // Update cache hits
59                         incrementStatsEntry('cache_hits');
60                 } elseif (!isExtensionActive('cache')) {
61                         // Load from database
62                         $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
63                                 array($module), __FUNCTION__, __LINE__);
64
65                         // Is the entry there?
66                         if (SQL_NUMROWS($result)) {
67                                 // Get the title from database
68                                 $data = SQL_FETCHARRAY($result);
69                         } // END - if
70
71                         // Free the result
72                         SQL_FREERESULT($result);
73                 }
74         } // END - if
75
76         // Trim name
77         $data['title'] = trim($data['title']);
78
79         // Still no luck or empty title?
80         if (empty($data['title'])) {
81                 // Is it 'error'?
82                 if ($module == 'error') {
83                         // Error (real module was not found)
84                         $data['title'] = getMessage('MODULE_ERROR_404');
85                 }  else {
86                         // No name found
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);
91                         } // END - if
92                 }
93         } // END - if
94
95         // Return name
96         return $data['title'];
97 }
98
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();
103
104         // Do we have cache?
105         if (isset($GLOBALS['module_status'][$module])) {
106                 // Then use it
107                 return $GLOBALS['module_status'][$module];
108         } // END - if
109
110         // Filter module name (names with low chars and underlines are fine!)
111         $module = preg_replace('/[^a-z_]/', '', $module);
112
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];
123                 } // END - if
124         } // END - if
125
126         // Major error in module registry is the default
127         $ret = 'major';
128
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
132                 return 'done';
133         } // END - if
134
135         // Init data array
136         $data = array(
137                 'locked'     => 'Y',
138                 'hidden'     => 'N',
139                 'admin_only' => 'N',
140                 'mem_only'   => 'N'
141         );
142
143         // By default nothing is found
144         $found  = false;
145
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])) {
151                         // Check cache
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];
157
158                         // Update cache hits
159                         incrementStatsEntry('cache_hits');
160                         $found = true;
161                 } else {
162                         // No, then we have to update it!
163                         $ret = 'cache_miss';
164                 }
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) {
171                         // Read data
172                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Entry found.');
173                         $data = SQL_FETCHARRAY($result);
174                         $found = true;
175                 } elseif (isDebugModeEnabled()) {
176                         // Debug message only in debug-mode...
177                         logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module_chk . ' not found!');
178                 }
179
180                 // Free result
181                 SQL_FREERESULT($result);
182         }
183         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
184
185         // Is the module found?
186         if ($found === true) {
187                 // Check returned values against current access permissions
188                 //
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!
192                         $ret = 'done';
193                 } elseif ($data['locked'] == 'Y') {
194                         // Module is locked
195                         $ret = 'locked';
196                 } elseif (($data['mem_only'] == 'Y') && (!isMember())) {
197                         // You have to login first!
198                         $ret = 'mem_only';
199                 } elseif (($data['admin_only'] == 'Y') && (!isAdmin())) {
200                         // Only the Admin is allowed to enter this module!
201                         $ret = 'admin_only';
202                 } else {
203                         // @TODO Nothing helped???
204                         logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,admin=%s,mem=%s",
205                                 $ret,
206                                 $data['locked'],
207                                 $data['admin_only'],
208                                 $data['mem_only']
209                         ));
210                 }
211         } // END - if
212
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__);
224                         } else {
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__);
229                         }
230
231                         // Everthing is fine?
232                         if (SQL_AFFECTEDROWS() < 1) {
233                                 // Something bad happend!
234                                 return 'major';
235                         } // END - if
236
237                         // Destroy cache here
238                         // @TODO Rewrite this to a filter
239                         if ((isHtmlOutputMode()) || (isRawOutputMode())) rebuildCache('modules', 'modules');
240
241                         // And reload data
242                         unset($GLOBALS['module_status'][$module]);
243                         $ret = checkModulePermissions($module_chk);
244                 } else {
245                         // Module not found we don't add it to the database
246                         $ret = '404';
247                 }
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",
254                         $module,
255                         $ret,
256                         $data['locked'],
257                         $data['hidden'],
258                         $data['mem_only'],
259                         $data['admin_only'],
260                         getScriptOutputMode()
261                 ));
262         }
263
264         // Return the value
265         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
266         $GLOBALS['module_status'][$module] = $ret;
267         return $ret;
268 }
269
270 // Checks if the module has a menu
271 function ifModuleHasMenu ($mod, $forceDb = false) {
272         // All is false by default
273         $ret = false;
274
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');
286                 } else {
287                         // Admin/guest/member/sponsor modules have always a menu!
288                         $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
289                 }
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__);
294
295                 // Entry found?
296                 if (SQL_NUMROWS($result) == 1) {
297                         // Load "has_menu" column
298                         $data = SQL_FETCHARRAY($result);
299
300                         // Fake cache... ;-)
301                         $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
302
303                         // Does it have a menu?
304                         $ret = ($data['has_menu'] == 'Y');
305                 } // END  - if
306
307                 // Free memory
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!
312         } else {
313                 // Unsupported state!
314                 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
315         }
316
317         // Return status
318         return $ret;
319 }
320
321 // [EOF]
322 ?>