API functions renamed:
[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:: 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                  *
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()) {
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                 // No name found
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);
86                 } // END - if
87         } // END - if
88
89         // Return name
90         return $data['title'];
91 }
92
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();
97
98         // Do we have cache?
99         if (isset($GLOBALS['module_status'][$module])) {
100                 // Then use it
101                 return $GLOBALS['module_status'][$module];
102         } // END - if
103
104         // Filter module name (names with low chars and underlines are fine!)
105         $module = preg_replace('/[^a-z_]/', '', $module);
106
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];
117                 } // END - if
118         } // END - if
119
120         // Major error in module registry is the default
121         $ret = 'major';
122
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
126                 return 'done';
127         } // END - if
128
129         // Init data array
130         $data = array(
131                 'locked'     => 'Y',
132                 'hidden'     => 'N',
133                 'admin_only' => 'N',
134                 'mem_only'   => 'N'
135         );
136
137         // By default nothing is found
138         $found  = false;
139
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])) {
145                         // Check cache
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];
151
152                         // Update cache hits
153                         incrementStatsEntry('cache_hits');
154                         $found = true;
155                 } else {
156                         // No, then we have to update it!
157                         $ret = 'cache_miss';
158                 }
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) {
165                         // Read data
166                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Entry found.');
167                         $data = SQL_FETCHARRAY($result);
168                         $found = true;
169                 } elseif (isDebugModeEnabled()) {
170                         // Debug message only in debug-mode...
171                         logDebugMessage(__FUNCTION__, __LINE__, 'Module ' . $module_chk . ' not found!');
172                 }
173
174                 // Free result
175                 SQL_FREERESULT($result);
176         }
177         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
178
179         // Is the module found?
180         if ($found === true) {
181                 // Check returned values against current access permissions
182                 //
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!
186                         $ret = 'done';
187                 } elseif ($data['locked'] == 'Y') {
188                         // Module is locked
189                         $ret = 'locked';
190                 } elseif (($data['mem_only'] == 'Y') && (!isMember())) {
191                         // You have to login first!
192                         $ret = 'mem_only';
193                 } elseif (($data['admin_only'] == 'Y') && (!isAdmin())) {
194                         // Only the Admin is allowed to enter this module!
195                         $ret = 'admin_only';
196                 } else {
197                         // @TODO Nothing helped???
198                         logDebugMessage(__FUNCTION__, __LINE__, sprintf("ret=%s,locked=%s,admin=%s,mem=%s",
199                                 $ret,
200                                 $data['locked'],
201                                 $data['admin_only'],
202                                 $data['mem_only']
203                         ));
204                 }
205         } // END - if
206
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__);
218                         } else {
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__);
223                         }
224
225                         // Everthing is fine?
226                         if (SQL_AFFECTEDROWS() < 1) {
227                                 // Something bad happend!
228                                 return 'major';
229                         } // END - if
230
231                         // Destroy cache here
232                         // @TODO Rewrite this to a filter
233                         if ((getScriptOutputMode() == '0') || (getScriptOutputMode() == -1)) rebuildCache('modules', 'modules');
234
235                         // And reload data
236                         unset($GLOBALS['module_status'][$module]);
237                         $ret = checkModulePermissions($module_chk);
238                 } else {
239                         // Module not found we don't add it to the database
240                         $ret = '404';
241                 }
242         } elseif (($ret == 'cache_miss') && (getScriptOutputMode() == '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",
248                         $module,
249                         $ret,
250                         $data['locked'],
251                         $data['hidden'],
252                         $data['mem_only'],
253                         $data['admin_only'],
254                         getScriptOutputMode()
255                 ));
256         }
257
258         // Return the value
259         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ret=' . $ret);
260         $GLOBALS['module_status'][$module] = $ret;
261         return $ret;
262 }
263
264 // Checks if the module has a menu
265 function ifModuleHasMenu ($mod, $forceDb = false) {
266         // All is false by default
267         $ret = false;
268
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');
280                 } else {
281                         // Admin/guest/member/sponsor modules have always a menu!
282                         $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
283                 }
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__);
288
289                 // Entry found?
290                 if (SQL_NUMROWS($result) == 1) {
291                         // Load "has_menu" column
292                         $data = SQL_FETCHARRAY($result);
293
294                         // Fake cache... ;-)
295                         $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
296
297                         // Does it have a menu?
298                         $ret = ($data['has_menu'] == 'Y');
299                 } // END  - if
300
301                 // Free memory
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!
306         } else {
307                 // Unsupported state!
308                 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
309         }
310
311         // Return status
312         return $ret;
313 }
314
315 // [EOF]
316 ?>