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