Missing variable fixed, opps
[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 // Checks if module_status entry is there
98 function isModuleStatusSet ($module) {
99         // Check it
100         return (isset($GLOBALS['module_status'][$module]));
101 }
102
103 // Setter module status
104 function setModuleStatus ($module, $status) {
105         $GLOBALS['module_status'][$module] = $status;
106 }
107
108 // Getter for module status
109 function getModuleStatus ($module) {
110         // Is the module_status entry there?
111         if (!isModuleStatusSet($module)) {
112                 // Abort
113                 debug_report_bug('Module status not set. module=' . $module);
114         } // END - if
115
116         // Return it
117         return $GLOBALS['module_status'][$module];
118 }
119
120 // Checks wether the given module is registered
121 function isModuleRegistered ($module) {
122         // By default nothing is found
123         $found  = false;
124
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])) {
130                         // Update cache hits
131                         incrementStatsEntry('cache_hits');
132
133                         // Is found
134                         $found = true;
135                 } else {
136                         // No, then we have to update it!
137                         setModuleStatus($module, 'cache_miss');
138                 }
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) {
145                         // Read data
146                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Entry found.');
147                         $GLOBALS['cache_array']['modules'][$module] = SQL_FETCHARRAY($result);
148                         $found = true;
149                 } elseif (isDebugModeEnabled()) {
150                         // Debug message only in debug-mode...
151                         logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module . ' not found!');
152                 }
153
154                 // Free result
155                 SQL_FREERESULT($result);
156         }
157
158         // Return status
159         return $found;
160 }
161
162 // Check validity of a given module name (no file extension)
163 function checkModulePermissions ($module = '') {
164         // Is it empty (default), then take the current one
165         if (empty($module)) $module = getModule();
166
167         // Do we have cache?
168         if (isModuleStatusSet($module)) {
169                 // Then use it
170                 return getModuleStatus($module);
171         } // END - if
172
173         // Filter module name (names with low chars and underlines are fine!)
174         $module = preg_replace('/[^a-z_]/', '', $module);
175
176         // Check for prefix is a extension...
177         $modSplit = explode('_', $module);
178         $extension = ''; $module_chk = $module;
179         //* DEBUG: */ debugOutput(__LINE__.'*'.count($modSplit).'/'.$module.'*');
180         if (count($modSplit) == 2) {
181                 // Okay, there is a seperator (_) in the name so is the first part a module?
182                 //* DEBUG: */ debugOutput(__LINE__.'*'.$modSplit[0].'*');
183                 if (isExtensionActive($modSplit[0])) {
184                         // The prefix is an extension's name, so let's set it
185                         $extension = $modSplit[0]; $module = $modSplit[1];
186                 } // END - if
187         } // END - if
188
189         // Major error in module registry is the default
190         setModuleStatus($module_chk, 'major');
191
192         // Check if script is installed if not return a 'done' to prevent some errors
193         if ((isInstallationPhase()) || (!isAdminRegistered())) {
194                 // Not installed or no admin registered or in installation phase
195                 // Set status
196                 setModuleStatus($module_chk, 'done');
197
198                 // Return status
199                 return 'done';
200         } // END - if
201
202         // Check if the module is registered
203         $found = isModuleRegistered($module_chk);
204
205         // Is the module found?
206         if ($found === true) {
207                 // Check returned values against current access permissions
208                 //
209                 // Admin access                                                              ----- Guest access -----                                                                                                                         --- Guest   or   member? ---
210                 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())))) {
211                         // If you are admin you are welcome for everything!
212                         setModuleStatus($module_chk, 'done');
213                 } elseif ($GLOBALS['cache_array']['modules']['locked'][$module_chk] == 'Y') {
214                         // Module is locked
215                         setModuleStatus($module_chk, 'locked');
216                 } elseif (($GLOBALS['cache_array']['modules']['mem_only'][$module_chk] == 'Y') && (!isMember())) {
217                         // You have to login first!
218                         setModuleStatus($module_chk, 'mem_only');
219                 } elseif (($GLOBALS['cache_array']['modules']['admin_only'][$module_chk] == 'Y') && (!isAdmin())) {
220                         // Only the Admin is allowed to enter this module!
221                         setModuleStatus($module_chk, 'admin_only');
222                 } else {
223                         // @TODO Nothing helped???
224                         logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,admin=%s,mem=%s",
225                                 getModuleStatus($module_chk),
226                                 $GLOBALS['cache_array']['modules']['locked'][$module_chk],
227                                 $GLOBALS['cache_array']['modules']['admin_only'][$module_chk],
228                                 $GLOBALS['cache_array']['modules']['mem_only'][$module_chk]
229                         ));
230                 }
231         } // END - if
232
233         // Still no luck or not found?
234         if (($found === false) && (!isExtensionActive('cache')) && (getModuleStatus($module_chk) != 'done'))  {
235                 //              ----- Legacy module -----                                  ---- Module in base folder  ----                       --- Module with extension's name ---
236                 if ((isIncludeReadable(sprintf("inc/modules/%s.php", $module))) || (isIncludeReadable(sprintf("%s.php", $module))) || (isIncludeReadable(sprintf("%s/%s.php", $extension, $module)))) {
237                         // Data is missing so we add it
238                         if (isExtensionInstalledAndNewer('sql_patches', '0.3.6')) {
239                                 // Since 0.3.6 we have a has_menu column, this took me a half hour
240                                 // to find a loop here... *sigh*
241                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
242 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`, `has_menu`) VALUES
243 ('%s','Y','N','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
244                         } else {
245                                 // Wrong/missing sql_patches!
246                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg`
247 (`module`, `locked`, `hidden`, `mem_only`, `admin_only`) VALUES
248 ('%s','Y','N','N','N')", array($module_chk), __FUNCTION__, __LINE__);
249                         }
250
251                         // Everthing is fine?
252                         if (SQL_AFFECTEDROWS() < 1) {
253                                 // Something bad happend!
254                                 setModuleStatus($module_chk, 'major');
255                                 return 'major';
256                         } // END - if
257
258                         // Destroy cache here
259                         // @TODO Rewrite this to a filter
260                         if ((isHtmlOutputMode()) || (isRawOutputMode())) rebuildCache('modules', 'modules');
261
262                         // And reload data
263                         unset($GLOBALS['module_status'][$module_chk]);
264                         return checkModulePermissions($module_chk);
265                 } else {
266                         // Module not found we don't add it to the database
267                         setModuleStatus($module_chk, '404');
268                 }
269         } elseif ((getModuleStatus($module_chk) == 'cache_miss') && (isHtmlOutputMode())) {
270                 // Rebuild the cache files
271                 rebuildCache('modules', 'modules');
272         } elseif ($found === false) {
273                 // Problem with module detected
274                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Problem in module %s detected. ret=%s, locked=%s, hidden=%s, mem=%s, admin=%s, output_mode=%s",
275                         $module,
276                         getModuleStatus($module),
277                         $GLOBALS['cache_array']['modules']['locked'][$module_chk],
278                         $GLOBALS['cache_array']['modules']['hidden'][$module_chk],
279                         $GLOBALS['cache_array']['modules']['mem_only'][$module_chk],
280                         $GLOBALS['cache_array']['modules']['admin_only'][$module_chk],
281                         getScriptOutputMode()
282                 ));
283         }
284
285         // Debug log
286         logDebugMessage(__FUNCTION__, __LINE__, sprintf("module=%s, statyus=%s", $module_chk, getModuleStatus($module_chk)));
287
288         // Return the value
289         return getModuleStatus($module_chk);
290 }
291
292 // Checks if the module has a menu
293 function ifModuleHasMenu ($mod, $forceDb = false) {
294         // All is false by default
295         $ret = false;
296
297         // Extension installed and newer than or has version 0.1.2?
298         if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
299                 // Cache version is okay, so let's check the cache!
300                 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
301                         // Check module cache and count hit
302                         $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
303                         incrementStatsEntry('cache_hits');
304                 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
305                         // Check cache and count hit
306                         $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
307                         incrementStatsEntry('cache_hits');
308                 } else {
309                         // Admin/guest/member/sponsor modules have always a menu!
310                         $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
311                 }
312         } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
313                 // Check database for entry
314                 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
315                         array($mod), __FUNCTION__, __LINE__);
316
317                 // Entry found?
318                 if (SQL_NUMROWS($result) == 1) {
319                         // Load "has_menu" column
320                         $data = SQL_FETCHARRAY($result);
321
322                         // Fake cache... ;-)
323                         $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
324
325                         // Does it have a menu?
326                         $ret = ($data['has_menu'] == 'Y');
327                 } // END  - if
328
329                 // Free memory
330                 SQL_FREERESULT($result);
331         } elseif (!isExtensionInstalled('sql_patches')) {
332                 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
333                 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
334         } else {
335                 // Unsupported state!
336                 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
337         }
338
339         // Return status
340         return $ret;
341 }
342
343 // Adds a SQL for given module
344 function addModuleSql ($module, $locked, $hidden, $adminOnly, $memOnly) {
345         // Is the module already registered?
346         if (!isModuleRegistered($module)) {
347                 // Add it
348                 addExtensionSql("INSERT INTO `{?_MYSQL_PREFIX?}_mod_reg` (`module`,`locked`,`hidden`,`admin_only`,`mem_only`) VALUES('" . $module . "','" . $locked . "','" . $hidden . "','" . $adminOnly . "','" . $memOnly . "')");
349         } else {
350                 // Already registered
351                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Already registered: module=%s,locked=%s,hidden=%s,admin=%s,mem=%s",
352                         $module,
353                         $locked,
354                         $hidden,
355                         $adminOnly,
356                         $memOnly
357                 ));
358         }
359 }
360
361 // [EOF]
362 ?>