Function loadLanguageFile() does now support extensions
[mailer.git] / inc / extensions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 03/25/2004 *
4  * ===============                              Last change: 09/29/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : extensions.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Extension management                             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Erweiterungen-Management                         *
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 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 // Load the extension and maybe found language and function files.
46 function LOAD_EXTENSION ($ext_name, $EXT_LOAD_MODE = '', $EXT_VER = '', $dry_run = false) {
47         // Set current extension name
48         EXT_SET_CURR_NAME($ext_name);
49
50         // Init array
51         INIT_INC_POOL();
52
53         // Init EXT_UPDATE_DEPENDS
54         EXT_INIT_UPDATE_DEPENDS();
55
56         // Init current extension name list
57         INIT_EXT_SQLS();
58
59         // Is the extension already loaded?
60         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Loading extension {$ext_name}, mode={$EXT_LOAD_MODE}, ver={$EXT_VER}.");
61         if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (empty($EXT_LOAD_MODE))) {
62                 // Debug message
63                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
64
65                 // Abort here
66                 return false;
67         } // END - if
68
69         // Construct include filename and FQFN for extension file
70         $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
71         $FQFN = constant('PATH') . $INC;
72
73         // Is the extension file NOT there?
74         if (!isIncludeReadable($INC)) {
75                 // Debug message
76                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
77
78                 // Abort here
79                 return false;
80         } // END - if
81
82         // Load extension's own language file
83         loadLanguageFile($ext_name);
84
85         // Construct FQFN for functions file
86         $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
87
88         // Is this include there?
89         if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
90                 // Then load it
91                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Functions loaded.");
92                 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
93                 loadIncludeOnce($funcsInclude);
94         } elseif ((isDebugModeEnabled()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
95                 // No functions file is not so good...
96                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("WARNING: Extension %s has no own functions file or we cannot read from it.",
97                         $ext_name
98                 ));
99         } // END - if
100
101         // Extensions are not deprecated by default
102         EXT_SET_DEPRECATED('N');
103
104         // Extensions are not always active by default
105         EXT_SET_ALWAYS_ACTIVE('N');
106
107         // Extension update notes
108         EXT_SET_UPDATE_NOTES('');
109
110         // Include the extension file
111         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Extension loaded.");
112         require($FQFN);
113
114         // Is this extension deprecated?
115         if (EXT_GET_DEPRECATED() == 'Y') {
116                 // Deactivate the extension
117                 DEACTIVATE_EXTENSION($ext_name);
118
119                 // Abort here
120                 return false;
121         } // END - if
122
123         // Mark it as loaded in normal mode
124         if (empty($EXT_LOAD_MODE)) {
125                 // Mark it now...
126                 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
127         } // END - if
128
129         // All fine!
130         return true;
131 }
132
133 // Registeres an extension and possible update depencies
134 function REGISTER_EXTENSION ($ext_name, $task_id, $dry_run = false, $logout = true) {
135         // Set current extension name
136         EXT_SET_CURR_NAME($ext_name);
137
138         //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - ENTERED!<br />");
139         // This shall never do a non-admin user or if the extension is active (already installed)
140         if ((!IS_ADMIN()) || (EXT_IS_ACTIVE($ext_name))) {
141                 return false;
142         } // END - if
143
144         // When this extension is already in install/update phase, all is fine
145         if (EXT_IS_REGISTER_RUNNING($ext_name)) {
146                 // Then abort here which is fine
147                 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - ALREADY!<br />");
148                 return true;
149         } // END - if
150
151         // This registration is running
152         EXT_ADD_RUNNING_REGISTRATION($ext_name);
153
154         // Init EXT_UPDATE_DEPENDS
155         EXT_INIT_UPDATE_DEPENDS();
156
157         // Is the task id zero? Then we need to auto-fix it here
158         if ($task_id == 0) {
159                 // Try to find the task
160                 $task_id = DETERMINE_EXTENSION_TASK_ID(EXT_GET_CURR_NAME());
161
162                 // Still zero and not in dry-run?
163                 if (($task_id == 0) && (!$dry_run)) {
164                         // Then request a bug report
165                         debug_report_bug(sprintf("%s: task_id is still zero after DETERMINE_EXTENSION_TASK_ID(%s)",
166                         __FUNCTION__,
167                         EXT_GET_CURR_NAME()
168                         ));
169                 } // END - if
170         } // END - if
171
172         // Init queries and notes
173         INIT_EXT_SQLS();
174         EXT_INIT_NOTES();
175
176         // Init variables
177         $ret = false;
178         $test = false;
179         INIT_INC_POOL();
180
181         // By default we have no failures
182         EXT_SET_REPORTS_FAILURE(false);
183
184         // Does this extension exists?
185         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME()."");
186         if (LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'register', '', $dry_run)) {
187                 // Set current extension name again
188                 EXT_SET_CURR_NAME($ext_name);
189
190                 // And run possible updates
191                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "".EXT_GET_CURR_NAME());
192                 $history = EXT_GET_VER_HISTORY();
193                 foreach ($history as $ver) {
194                         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".EXT_GET_CURR_NAME().", ext_ver={$ver}");
195                         // Load extension in update mode
196                         LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'update', $ver, $dry_run);
197
198                         // Add update notes to our output
199                         ADD_EXTENSION_NOTES($ver);
200                 } // END - foreach
201
202                 // Does this extension depends on an outstanding update of another update?
203                 for ($dmy = EXT_GET_UPDATE_ITERATOR(); EXT_GET_UPDATE_ITERATOR() < EXT_COUNT_UPDATE_DEPENDS();) {
204                         // Get next update
205                         $ext_update = EXT_GET_ITERATOR_UPDATE_DEPENDS();
206
207                         // Increment here to avoid endless loop
208                         EXT_INCREMENT_UPDATE_INTERATOR();
209
210                         // Check for required file
211                         if (LOAD_EXTENSION($ext_update, 'register', '', $dry_run)) {
212                                 // Set current extension name again
213                                 EXT_SET_CURR_NAME($ext_name);
214
215                                 // If versions mismatch update extension first
216                                 $ext_ver = GET_EXT_VERSION($ext_update);
217
218                                 // Extension version set? If empty the extension is not registered
219                                 if (empty($ext_ver)) {
220                                         // Extension not registered so far so first load task's ID...
221                                         $task = DETERMINE_EXTENSION_TASK_ID($ext_update);
222
223                                         // Entry found?
224                                         if ($task > 0) {
225                                                 // Try to register the extension
226                                                 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().":ext_update=".$ext_update.",taskId=".$task."<br />");
227                                                 $test = REGISTER_EXTENSION($ext_update, $task, $dry_run, false);
228
229                                                 // Reset extension name
230                                                 EXT_SET_CURR_NAME($ext_name);
231                                                 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'); var_dump($test);
232                                         } // END - if
233                                 } elseif ($ext_ver != EXT_GET_VERSION()) {
234                                         // Ok, update this extension now
235                                         EXTENSION_UPDATE($ext_update, $ext_ver, $dry_run);
236
237                                         // All okay!
238                                         $test = true;
239                                 } else {
240                                         // Nothing to register / update before...
241                                         $test = true;
242                                 }
243                         } else {
244                                 // Required file for update does not exists!
245                                 $test = true;
246                                 // But this is fine for the first time...
247                         }
248
249                         // Restore the current extension name
250                         EXT_SET_CURR_NAME($ext_name);
251                 } // END - for
252
253                 // Is there no update?
254                 if (EXT_COUNT_UPDATE_DEPENDS(EXT_GET_CURR_NAME()) == 0) {
255                         // Then test is passed!
256                         $test = true;
257                 } // END - if
258
259                 // Switch back to register mode
260                 $EXT_LOAD_MODE = 'register';
261
262                 // Remains true if extension registration reports no failures
263                 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'); var_dump($test);
264                 $test = (($test === true) && (EXT_GET_REPORTS_FAILURE() === false));
265                 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'); var_dump($test);
266
267                 // Does everthing before wents ok?
268                 if ($test === true) {
269                         // "Dry-run-mode" activated?
270                         if ((!$dry_run) && (!EXT_IS_ON_REMOVAL_LIST())) {
271                                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".EXT_GET_CURR_NAME());
272                                 // Init SQLs and transfer ext->generic
273                                 INIT_SQLS();
274                                 SET_SQLS(GET_EXT_SQLS());
275
276                                 // Run installation pre-installation filters
277                                 runFilterChain('pre_extension_installed', array('dry_run' => $dry_run));
278
279                                 // Register extension
280                                 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:insert=".EXT_GET_CURR_NAME().'/'.EXT_GET_VERSION()." - INSERT!<br />");
281                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_extensions` (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
282                                         array(EXT_GET_CURR_NAME(), EXT_GET_ALWAYS_ACTIVE(), EXT_GET_VERSION()), __FUNCTION__, __LINE__);
283
284                                 // Remove cache file(s) if extension is active
285                                 runFilterChain('post_extension_installed', array('ext_name' => EXT_GET_CURR_NAME(), 'task_id' => $task_id));
286
287                                 // Remove all SQL commands
288                                 UNSET_SQLS();
289
290                                 // In normal mode return a true on success
291                                 $ret = true;
292                         } elseif ($dry_run) {
293                                 // Init SQLs and transfer ext->generic
294                                 INIT_SQLS();
295                                 SET_SQLS(GET_EXT_SQLS());
296
297                                 // Rewrite SQL command to keep { and } inside for dry-run
298                                 foreach (GET_SQLS() as $key => $sql) {
299                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
300                                         SET_SQL_KEY($key, $sql);
301                                 } // END - foreach
302
303                                 // In  "dry-run" mode return array with all SQL commands
304                                 $ret = GET_SQLS();
305
306                                 // Remove all SQL commands
307                                 UNSET_SQLS();
308                         } else {
309                                 // Extension has been removed for updates, so all is fine!
310                                 $ret = true;
311                         }
312                 } else {
313                         // No, an error occurs while registering extension :-(
314                         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "".EXT_GET_CURR_NAME());
315                         $ret = false;
316                 }
317         } elseif (($task_id > 0) && (EXT_GET_CURR_NAME() != '')) {
318                 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()."<br />");
319                 // Remove task from system when id and extension's name is valid
320                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
321                         array(bigintval($task_id)), __FUNCTION__, __LINE__);
322         }
323
324         // Is this the sql_patches?
325         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ':'.EXT_GET_CURR_NAME()."/{$EXT_LOAD_MODE}");
326         if ((EXT_GET_CURR_NAME() == 'sql_patches') && (($EXT_LOAD_MODE == 'register') || ($EXT_LOAD_MODE == 'remove')) && (!$dry_run) && ($test)) {
327                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
328                 if ($logout === true) {
329                         // Then redirect to logout
330                         redirectToUrl('modules.php?module=admin&amp;logout=1&amp;' . $EXT_LOAD_MODE . '=sql_patches');
331                 } else {
332                         // Add temporary filter
333                         registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
334                         $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
335                 }
336         } // END - if
337
338         // Return status code
339         //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - LEFT!<br />");
340         //* DEBUG: */ var_dump($ret);
341         return $ret;
342 }
343
344 // Run SQL queries for given extension id
345 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
346 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
347         // This shall never do a non-admin user!
348         if (!IS_ADMIN()) return false;
349
350         // Get extension's name
351         $ext_name = GET_EXT_NAME($ext_id);
352
353         // If it is not set then maybe there is no extension for that ID number
354         if (empty($ext_name)) {
355                 // We should fix these all!
356                 debug_report_bug(__FUNCTION__ . ': ext_name is empty. ext_id=' . $ext_id);
357         } // END - if
358
359         // Set current SQL name
360         EXT_SET_CURR_NAME($ext_name);
361
362         // Init EXT_UPDATE_DEPENDS
363         EXT_INIT_UPDATE_DEPENDS();
364
365         // Init array
366         INIT_EXT_SQLS();
367
368         // By default no SQL has been executed
369         $sqlRan = false;
370
371         // Load extension in detected mode
372         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]=".EXT_GET_CURR_NAME()."");
373         LOAD_EXTENSION(EXT_GET_CURR_NAME(), $load_mode, '', false);
374
375         // Init these SQLs
376         INIT_SQLS();
377         SET_SQLS(GET_EXT_SQLS());
378
379         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".COUNT_SQLS()."");
380         if ((IS_SQLS_VALID() && (COUNT_SQLS() > 0))) {
381                 // Run any filters depending on the action here
382                 runFilterChain('extension_' . $load_mode, $ext_name);
383
384                 // Run SQL commands...
385                 runFilterChain('run_sqls');
386
387                 // Removal mode?
388                 if ($load_mode == 'remove') {
389                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
390                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
391                                 array(EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
392                 } // END - if
393         } // END - if
394
395         // Remove cache file(s) if extension is active
396         if (((EXT_IS_ACTIVE('cache')) || (GET_EXT_VERSION('cache') != '')) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
397                 // Run filters
398                 runFilterChain('post_extension_run_sql', EXT_GET_CURR_NAME());
399         } // END - if
400
401         // Is this the sql_patches?
402         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": id=".$ext_id.",currName=".EXT_GET_CURR_NAME().",loadMode=".$load_mode);
403         if ((EXT_GET_CURR_NAME() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
404                 // Then redirect to logout
405                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
406                 redirectToUrl('modules.php?module=admin&amp;logout=1&amp;' . $load_mode . '=sql_patches');
407         } // END - if
408 }
409
410 // Check if given extension is active
411 function EXT_IS_ACTIVE ($ext_name) {
412         // Extensions are all inactive during installation
413         if ((!isInstalled()) || (isInstalling()) || (empty($ext_name))) return false;
414
415         // Not active is the default
416         $active = 'N';
417
418         // Check cache
419         if (isset($GLOBALS['cache_array']['extensions']['ext_active'][$ext_name])) {
420                 // Load from cache
421                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
422                 $active = $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name];
423
424                 // Count cache hits
425                 incrementConfigEntry('cache_hits');
426         } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
427                 // @TODO Extension is loaded, what next?
428                 app_die(__FUNCTION__, __LINE__, "LOADED:$ext_name");
429         } elseif (($ext_name == 'cache') || (GET_EXT_VERSION('cache') == '')) {
430                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
431                 // Load from database
432                 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
433                         array($ext_name), __FUNCTION__, __LINE__);
434
435                 // Entry found?
436                 if (SQL_NUMROWS($result) == 1) {
437                         // Load entry
438                         list($active) = SQL_FETCHROW($result);
439                 } // END - if
440
441                 // Free result
442                 SQL_FREERESULT($result);
443
444                 // Write cache array
445                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: {$active}");
446                 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = $active;
447         } else {
448                 // Extension not active!
449                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
450                 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = 'N';
451         }
452
453         // Debug message
454         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name={$ext_name},active={$active}");
455
456         // Is this extension activated? (For admins we always have active extensions...)
457         return ($active == 'Y');
458 }
459 // Get version from extensions
460 function GET_EXT_VERSION ($ext_name) {
461         // By default no extension is found
462         $ext_ver = false;
463
464         // Empty extension name should be fixed!
465         if (empty($ext_name)) {
466                 // Please report this bug!
467                 debug_report_bug(__FUNCTION__ . ': ext_name is empty which is not allowed here.');
468         } // END - if
469
470         // Extensions are all inactive during installation
471         if ((!isInstalled()) || (isInstalling())) return "";
472         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
473
474         // Is the cache written?
475         if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
476                 // Load data from cache
477                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
478                 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
479
480                 // Count cache hits
481                 incrementConfigEntry('cache_hits');
482         } elseif (!isCacheInstanceValid()) {
483                 // Load from database
484                 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
485                         array($ext_name), __FUNCTION__, __LINE__);
486                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
487
488                 // Is the extension there?
489                 if (SQL_NUMROWS($result) == 1) {
490                         // Load entry
491                         list($ext_ver) = SQL_FETCHROW($result);
492                 } elseif (isDebugModeEnabled()) {
493                         // Not found!
494                         DEBUG_LOG(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
495                 }
496
497                 // Free result
498                 SQL_FREERESULT($result);
499
500                 // Set cache
501                 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
502         }
503
504         // Return result
505         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
506         return $ext_ver;
507 }
508
509 // Updates a given extension with current extension version to latest version
510 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
511         // Only admins are allowed to update extensions
512         if ((!IS_ADMIN()) || (empty($ext_name))) return false;
513
514         // Set current SQL name
515         EXT_SET_CURR_NAME($ext_name);
516
517         // Init arrays
518         INIT_EXT_SQLS();
519         EXT_INIT_NOTES();
520         INIT_INC_POOL();
521
522         // Load extension in test mode
523         LOAD_EXTENSION($ext_name, 'test', $ext_ver, $dry_run);
524
525         // Save version history
526         $history = EXT_GET_VER_HISTORY();
527
528         // Remove old SQLs array to prevent possible bugs
529         INIT_EXT_SQLS();
530
531         // Check if version is updated
532         if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
533                 // Search for starting point
534                 $start = array_search($ext_ver, $history);
535
536                 // And load SQL queries in order of version history
537                 for ($idx = ($start + 1); $idx < count($history); $idx++) {
538                         // Set current extension name
539                         //* DEBUG: */ OUTPUT_HTML(__FUNCTION__.'['.__LINE__.':] ext_name='.$ext_name."<br />");
540                         EXT_SET_CURR_NAME($ext_name);
541
542                         // Set extension version
543                         $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()] = $history[$idx];
544
545                         // Load again...
546                         LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'update', $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], $dry_run);
547
548                         // Get all depencies
549                         $depencies = EXT_GET_UPDATE_DEPENDS();
550
551                         // Nothing to apply?
552                         if (count($depencies) > 0) {
553                                 // Apply all extension depencies
554                                 foreach ($depencies as $ext_depend) {
555                                         // Set it as current
556                                         EXT_SET_CURR_NAME($ext_depend);
557
558                                         // Is the extension there?
559                                         if (GET_EXT_VERSION($ext_depend) != '') {
560                                                 // Update another extension first!
561                                                 $test = EXTENSION_UPDATE($ext_depend, GET_EXT_VERSION($ext_depend), $dry_run);
562                                         } else {
563                                                 // Register new extension
564                                                 $test = REGISTER_EXTENSION($ext_depend, 0, $dry_run, false);
565                                         }
566                                 } // END - foreach
567
568                                 // Set name back
569                                 EXT_SET_CURR_NAME($ext_name);
570                         } // END - if
571
572                         // Add notes
573                         ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
574                 } // END - for
575
576                 // In real-mode execute any existing includes
577                 if (!$dry_run) {
578                         $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
579                         runFilterChain('load_includes');
580                         SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
581                         unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
582                 } // END - if
583
584                 // Init these SQLs
585                 INIT_SQLS();
586                 SET_SQLS(GET_EXT_SQLS());
587
588                 // Run SQLs
589                 runFilterChain('run_sqls', array('dry_run' => $dry_run));
590
591                 if (!$dry_run) {
592                         // Create task
593                         CREATE_EXTENSION_UPDATE_TASK(getCurrentAdminId(), EXT_GET_CURR_NAME(), $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], SQL_ESCAPE(EXT_GET_NOTES(EXT_GET_NOTES())));
594
595                         // Update extension's version
596                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE `ext_name`='%s' LIMIT 1",
597                         array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
598
599                         // Remove arrays
600                         UNSET_SQLS();
601                         unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
602
603                         // Run filters on success extension update
604                         runFilterChain('extension_update', EXT_GET_CURR_NAME());
605                 } // END - if
606         } // END - if
607 }
608
609 // Output verbose SQL table for extension
610 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = '', $dashed = '', $switch = false, $width = '100%') {
611         // Empty title?
612         if (empty($title)) {
613                 // Then fix it to default
614                 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
615         } // END - if
616
617         // Are there some queries in $queries?
618         if (count($queries) > 0) {
619                 // Then use them instead!
620                 SET_SQLS($queries);
621         } // END - if
622
623         // Init variables
624         $SW = 2; $i = 1;
625         $OUT = '';
626
627         // Do we have queries?
628         if ((IS_SQLS_VALID()) && (GET_EXT_VERSION('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
629                 foreach (GET_SQLS() as $idx => $sql) {
630                         // Trim out spaces
631                         $sql = trim($sql);
632
633                         // Output command if set
634                         if (!empty($sql)) {
635                                 // Prepare output for template
636                                 $content = array(
637                                         'sw'  => $SW,
638                                         'i'   => $i,
639                                         'sql' => $sql
640                                 );
641
642                                 // Load row template
643                                 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
644
645                                 // Switch color and count up
646                                 $SW = 3 - $SW;
647                                 $i++;
648                         } // END - if
649                 } // END - foreach
650
651                 // Prepare content for template
652                 $content = array(
653                         'width'  => $width,
654                         'dashed' => $dashed,
655                         'title'  => $title,
656                         'out'    => $OUT
657                 );
658
659                 // Load main template
660                 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
661         } elseif ((GET_EXT_VERSION('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
662                 // No addional SQL commands to run
663                 $OUT = LOAD_TEMPLATE('admin_settings_saved', true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
664         } // END - if
665
666         // Return output
667         return $OUT;
668 }
669
670 // Get extension name from id
671 function GET_EXT_NAME ($ext_id) {
672         // Init extension name
673         $ret = '';
674
675         // Is cache there?
676         if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
677                 // Load from cache
678                 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
679
680                 // Count cache hits
681                 incrementConfigEntry('cache_hits');
682         } elseif (!EXT_IS_ACTIVE('cache')) {
683                 // Load from database
684                 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `id`=%s LIMIT 1",
685                         array(bigintval($ext_id)), __FUNCTION__, __LINE__);
686                 list($ret) = SQL_FETCHROW($result);
687                 SQL_FREERESULT($result);
688         }
689
690         // Return the extension name
691         return $ret;
692 }
693
694 // Get extension id from name
695 function GET_EXT_ID ($ext_name) {
696         // Init ID number
697         $ret = 0;
698         if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
699                 // Load from cache
700                 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
701
702                 // Count cache hits
703                 incrementConfigEntry('cache_hits');
704         } elseif (!EXT_IS_ACTIVE('cache')) {
705                 // Load from database
706                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
707                         array($ext_name), __FUNCTION__, __LINE__);
708                 list($ret) = SQL_FETCHROW($result);
709                 SQL_FREERESULT($result);
710         }
711
712         // Return value
713         return $ret;
714 }
715
716 // Activate given extension
717 function ACTIVATE_EXTENSION ($ext_name) {
718         // Activate the extension
719         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
720         array($ext_name), __FUNCTION__, __LINE__);
721
722         // Extension has been activated?
723         if (SQL_AFFECTEDROWS() == 1) {
724                 // Then run all queries
725                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), 'activate');
726         } // END - if
727 }
728
729 // Deactivate given extension
730 function DEACTIVATE_EXTENSION($ext_name) {
731         // Activate the extension
732         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
733         array($ext_name), __FUNCTION__, __LINE__);
734
735         // Extension has been activated?
736         if (SQL_AFFECTEDROWS() == 1) {
737                 // Then run all queries
738                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), 'deactivate');
739
740                 // Create new task
741                 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
742
743                 // Notify the admin
744                 sendAdminNotification(
745                 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
746                         'admin_ext_deactivated',
747                 array('ext_name' => $ext_name)
748                 );
749         } // END - if
750 }
751
752 // Checks wether the extension is older than given
753 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
754         // Get current extension version
755         $currVersion = GET_EXT_VERSION($ext_name);
756
757         // Remove all dots from both versions
758         $currVersion = str_replace('.', '', $currVersion);
759         $ext_ver = str_replace('.', '', $ext_ver);
760
761         // Now compare both and return the result
762         return ($currVersion < $ext_ver);
763 }
764
765 // Creates a new task for updated extension
766 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
767         // Create subject line
768         $subject = '[UPDATE-'.$ext_name.'-'.$ext_ver.':] {--ADMIN_UPDATE_EXT_SUBJ--}';
769
770         // Is the extension there?
771         if (GET_EXT_VERSION($ext_name) != '') {
772                 // Check if task is not there
773                 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
774                         // Task not created so it's a brand-new extension which we need to register and create a task for!
775                         createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $admin_id);
776                 } // END - if
777         } // END - if
778 }
779
780 // Creates a new task for newly installed extension
781 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
782         // Not installed and do we have created a task for the admin?
783         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == '')) {
784                 // Set default message if ext-foo is missing
785                 $message = sprintf(getMessage('ADMIN_EXT_TEXT_FILE_MISSING'), $ext);
786
787                 // Template file
788                 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
789                         constant('PATH'),
790                         getLanguage(),
791                         $ext
792                 );
793
794                 // Load text for task if found
795                 if (isFileReadable($tpl)) {
796                         // Load extension's own text template (HTML!)
797                         $message = LOAD_TEMPLATE('ext_' . $ext, true);
798                 } else {
799                         // Write this in debug.log as well
800                         DEBUG_LOG(__FUNCTION__, __LINE__, $message);
801                 }
802
803                 // Task not created so it's a brand-new extension which we need to register and create a task for!
804                 createNewTask($subject, $message, 'EXTENSION_UPDATE', 0, $admin_id, false);
805         } // END - if
806 }
807
808 // Creates a task for automatically deactivated (deprecated) extension
809 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
810         // Create subject line
811         $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
812
813         // Not installed and do we have created a task for the admin?
814         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != '')) {
815                 // Task not created so add it
816                 createNewTask($subject, SQL_ESCAPE(LOAD_TEMPLATE('task_ext_deactivated', true, $ext)), 'EXTENSION_DEACTIVATION');
817         } // END - if
818 }
819
820 // Checks if the module has a menu
821 function MODULE_HAS_MENU ($mod, $forceDb = false) {
822         // All is false by default
823         $ret = false;
824
825         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "mod={$mod},cache=".GET_EXT_VERSION('cache'));
826         if (GET_EXT_VERSION('cache') >= '0.1.2') {
827                 // Cache version is okay, so let's check the cache!
828                 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
829                         // Check module cache and count hit
830                         $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
831                         incrementConfigEntry('cache_hits');
832                 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
833                         // Check cache and count hit
834                         $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == 'Y');
835                         incrementConfigEntry('cache_hits');
836                 } elseif ((IS_ADMIN()) && ($mod == 'admin')) {
837                         // Admin module has always a menu!
838                         $ret = true;
839                 }
840         } elseif ((GET_EXT_VERSION('sql_patches') >= '0.3.6') && ((!EXT_IS_ACTIVE('cache')) || ($forceDb === true))) {
841                 // Check database for entry
842                 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE `module`='%s' LIMIT 1",
843                         array($mod), __FUNCTION__, __LINE__);
844
845                 // Entry found?
846                 if (SQL_NUMROWS($result) == 1) {
847                         // Load "has_menu" column
848                         list($has_menu) = SQL_FETCHROW($result);
849
850                         // Fake cache... ;-)
851                         $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
852
853                         // Does it have a menu?
854                         $ret = ($has_menu == 'Y');
855                 } // END  - if
856
857                 // Free memory
858                 SQL_FREERESULT($result);
859         } elseif (GET_EXT_VERSION('sql_patches') == '') {
860                 // No sql_patches installed, so maybe in admin area or no admin registered?
861                 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == 'admin')); // Then there is a menu!
862         }
863
864         // Return status
865         //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'; var_dump($ret));
866         return $ret;
867 }
868
869 // Determines the task id for given extension
870 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
871         // Default is not found
872         $task_id = 0;
873
874         // Search for extension task's id
875         $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
876                 array($ext_name), __FUNCTION__, __LINE__);
877
878         // Entry found?
879         if (SQL_NUMROWS($result) == 1) {
880                 // Task found so load task's ID and register extension...
881                 list($task_id) = SQL_FETCHROW($result);
882         } // END - if
883
884         // Free result
885         SQL_FREERESULT($result);
886
887         // Return it
888         return $task_id;
889 }
890
891 // Determines the task id for given subject
892 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
893         // Default is not found
894         $task_id = 0;
895
896         // Search for task id
897         $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
898                 array($subject), __FUNCTION__, __LINE__);
899
900         // Entry found?
901         if (SQL_NUMROWS($result) == 1) {
902                 // Task found so load task's ID and register extension...
903                 list($task_id) = SQL_FETCHROW($result);
904         } // END - if
905
906         // Free result
907         SQL_FREERESULT($result);
908
909         // Return it
910         return $task_id;
911 }
912
913 // Add updates notes for given version
914 function ADD_EXTENSION_NOTES ($ver) {
915         // Init notes/content
916         $out = ''; $content = array();
917
918         // Is do we have verbose output enabled?
919         if ((getConfig('verbose_sql') == 'Y') || (!EXT_IS_ACTIVE('sql_patches'))) {
920                 // Update notes found?
921                 if (EXT_GET_UPDATE_NOTES() != '') {
922                         // Update notes found
923                         $content = array(
924                                 'ver'   => $ver,
925                                 'notes' => EXT_GET_UPDATE_NOTES()
926                         );
927
928                         // Reset them
929                         EXT_SET_UPDATE_NOTES('');
930                 } elseif (($ver == '0.0') || ($ver == '0.0.0')) {
931                         // Initial release
932                         $content = array(
933                                 'ver'   => $ver,
934                                 'notes' => getMessage('INITIAL_RELEASE')
935                         );
936                 } else {
937                         // No update notes found!
938                         $content = array(
939                                 'ver'   => $ver,
940                                 'notes' => getMessage('NO_UPDATE_NOTES')
941                         );
942                 }
943
944                 // Load template
945                 $out = LOAD_TEMPLATE('admin_ext_notes', true, $content);
946         } // END - if
947
948         // Add the notes
949         EXT_APPEND_NOTES($out);
950 }
951
952 // Getter for CSS files array
953 function EXT_GET_CSS_FILES () {
954         // By default no additional CSS files are found
955         $cssFiles = array();
956
957         // Is the array there?
958         if (isset($GLOBALS['css_files'])) {
959                 // Then use it
960                 $cssFiles = $GLOBALS['css_files'];
961         } // END - if
962
963         // Return array
964         return $cssFiles;
965 }
966
967 // Init CSS files array
968 function EXT_INIT_CSS_FILES () {
969         // Simply init it
970         $GLOBALS['css_files'] = array();
971 }
972
973 // Add new entry
974 function EXT_ADD_CSS_FILE ($file) {
975         // Is the array there?
976         if (!isset($GLOBALS['css_files'])) {
977                 // Then auto-init them
978                 EXT_INIT_CSS_FILES();
979         } // END - if
980
981         // Add the entry
982         $GLOBALS['css_files'][] = $file;
983 }
984
985 // Setter for EXT_ALWAYS_ACTIVE flag
986 function EXT_SET_ALWAYS_ACTIVE ($active) {
987         $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()] = (string) $active;
988 }
989
990 // Getter for EXT_ALWAYS_ACTIVE flag
991 function EXT_GET_ALWAYS_ACTIVE () {
992         return $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()];
993 }
994
995 // Setter for EXT_VERSION flag
996 function EXT_SET_VERSION ($version) {
997         $GLOBALS['ext_version'][EXT_GET_CURR_NAME()] = (string) $version;
998 }
999
1000 // Getter for EXT_VERSION flag
1001 function EXT_GET_VERSION () {
1002         return $GLOBALS['ext_version'][EXT_GET_CURR_NAME()];
1003 }
1004
1005 // Setter for EXT_DEPRECATED flag
1006 function EXT_SET_DEPRECATED ($deprecated) {
1007         $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()] = (string) $deprecated;
1008 }
1009
1010 // Getter for EXT_DEPRECATED flag
1011 function EXT_GET_DEPRECATED () {
1012         return $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()];
1013 }
1014
1015 // Setter for EXT_UPDATE_DEPENDS flag
1016 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
1017         // Is the update depency empty? (NEED TO BE FIXED!)
1018         if (empty($updateDepends)) {
1019                 // Please report this bug!
1020                 debug_report_bug("updateDepends is left empty!");
1021         } // END - if
1022
1023         // Is it not yet added?
1024         if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
1025                 //* DEBUG */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME().'/'.$updateDepends);
1026                 // Add it to the list of extension update depencies map
1027                 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
1028
1029                 // Remember it in the list of running updates
1030                 $GLOBALS['ext_running_updates'][] = $updateDepends;
1031         } // END - if
1032 }
1033
1034 // Checks wether the given extension registration is in progress
1035 function EXT_IS_REGISTER_RUNNING ($ext_name) {
1036         return ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1037 }
1038
1039 // Init EXT_UPDATE_DEPENDS flag
1040 function EXT_INIT_UPDATE_DEPENDS () {
1041         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1042
1043         // Init update depency map automatically if not found
1044         if (!EXT_IS_UPDATE_DEPENDS_INIT()) {
1045                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME()." - INIT!");
1046                 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
1047         } // END - if
1048
1049         // Init running updates array
1050         EXT_INIT_RUNNING_UPDATES();
1051 }
1052
1053 // Adds an extension as "registration in progress"
1054 function EXT_ADD_RUNNING_REGISTRATION ($ext_name) {
1055         // Is it running?
1056         if (!EXT_IS_REGISTER_RUNNING($ext_name)) {
1057                 // Then add it!
1058                 $GLOBALS['ext_register_running'][] = $ext_name;
1059         } // END - if
1060 }
1061
1062 // Checks wether EXT_UPDATE_DEPENDS is initialized
1063 function EXT_IS_UPDATE_DEPENDS_INIT () {
1064         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1065         return (isset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]));
1066 }
1067
1068 // Initializes the list of running updates
1069 function EXT_INIT_RUNNING_UPDATES () {
1070         // Auto-init ext_running_updates
1071         if (!isset($GLOBALS['ext_running_updates'])) {
1072                 $GLOBALS['ext_running_updates'] = array();
1073                 $GLOBALS['ext_register_running'] = array();
1074         } // END - if
1075 }
1076
1077 // Getter for EXT_UPDATE_DEPENDS flag
1078 function EXT_GET_UPDATE_DEPENDS () {
1079         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1080         return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1081 }
1082
1083 // Getter for next iterator depency
1084 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1085         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1086         return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1087 }
1088
1089 // Counter for extension update depencies
1090 function EXT_COUNT_UPDATE_DEPENDS () {
1091         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1092         return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1093 }
1094
1095 // Removes given extension from update denpency list
1096 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1097         // Look it up
1098         $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1099
1100         // Is it valid?
1101         if ($key !== false) {
1102                 // Then remove it
1103                 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1104
1105                 // And sort the array
1106                 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1107         } // END - if
1108 }
1109
1110 // Init iterator for update depencies
1111 function EXT_INIT_UPDATE_ITERATOR () {
1112         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1113         $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1114 }
1115
1116 // Getter for depency iterator
1117 function EXT_GET_UPDATE_ITERATOR () {
1118         // Auto-init iterator
1119         if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1120
1121         // Return it
1122         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME().'/'.$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]);
1123         return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1124 }
1125
1126 // Increments the update iterator
1127 function EXT_INCREMENT_UPDATE_INTERATOR () {
1128         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1129         $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1130 }
1131
1132 // Setter for EXT_REPORTS_FAILURE flag
1133 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1134         $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1135 }
1136
1137 // Getter for EXT_REPORTS_FAILURE flag
1138 function EXT_GET_REPORTS_FAILURE () {
1139         return $GLOBALS['ext_reports_failure'];
1140 }
1141
1142 // Setter for EXT_VER_HISTORY flag
1143 function EXT_SET_VER_HISTORY ($verHistory) {
1144         $GLOBALS['ext_ver_history'] = (array) $verHistory;
1145 }
1146
1147 // Getter for EXT_VER_HISTORY array
1148 function EXT_GET_VER_HISTORY () {
1149         return $GLOBALS['ext_ver_history'];
1150 }
1151
1152 // Setter for EXT_UPDATE_NOTES flag
1153 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1154         $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1155 }
1156
1157 // Getter for EXT_UPDATE_NOTES flag
1158 function EXT_GET_UPDATE_NOTES () {
1159         return $GLOBALS['ext_update_notes'];
1160 }
1161
1162 // Init extension notice
1163 function EXT_INIT_NOTES () {
1164         $GLOBALS['ext_notes'] = '';
1165 }
1166
1167 // Append extension notice
1168 function EXT_APPEND_NOTES ($notes) {
1169         $GLOBALS['ext_notes'] .= (string) $notes;
1170 }
1171
1172 // Getter for extension notes
1173 function EXT_GET_NOTES () {
1174         return $GLOBALS['ext_notes'];
1175 }
1176
1177 // Setter for current extension name
1178 function EXT_SET_CURR_NAME ($ext_name) {
1179         $GLOBALS['curr_ext_name'] = (string) $ext_name;
1180 }
1181
1182 // Getter for current extension name
1183 function EXT_GET_CURR_NAME () {
1184         if (isset($GLOBALS['curr_ext_name'])) {
1185                 return $GLOBALS['curr_ext_name'];
1186         } // END - if
1187
1188         // Not set!
1189         debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1190 }
1191
1192 // Init SQLs array for current extension
1193 function INIT_EXT_SQLS () {
1194         // Auto-init the array now...
1195         if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1196                 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1197         } // END - if
1198 }
1199
1200 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1201 function ADD_EXT_SQL ($sql) {
1202         $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1203 }
1204
1205 // Getter for SQLs array for current extension
1206 function GET_EXT_SQLS () {
1207         // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1208         if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1209                 // Not found, should not happen
1210                 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1211                 EXT_GET_CURR_NAME()
1212                 ));
1213         } // END - if
1214
1215         // Return the array
1216         return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1217 }
1218
1219 // Removes SQLs for current extension
1220 function UNSET_EXT_SQLS () {
1221         unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1222 }
1223
1224 // Auto-initializes the removal list
1225 function EXT_INIT_REMOVAL_LIST () {
1226         // Is the remove list there?
1227         if (!isset($GLOBALS['ext_update_remove'])) {
1228                 // Then create it
1229                 $GLOBALS['ext_update_remove'] = array();
1230         } // END - if
1231 }
1232
1233 // Checks wether the current extension is on the removal list
1234 function EXT_IS_ON_REMOVAL_LIST () {
1235         // Init removal list
1236         EXT_INIT_REMOVAL_LIST();
1237
1238         // Is it there?
1239         return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1240 }
1241
1242 // Adds the current extension to the removal list
1243 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1244         // Simply add it
1245         $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1246 }
1247
1248 // Getter for removal list
1249 function EXT_GET_REMOVAL_LIST () {
1250         // Return the removal list
1251         return $GLOBALS['ext_update_remove'];
1252 }
1253
1254 // Redirects if the provided extension is not installed
1255 function redirectOnUninstalledExtension ($ext_name) {
1256         // So is the extension there?
1257         if ((!isExtensionInstalled($ext_name)) || (!EXT_IS_ACTIVE($ext_name))) {
1258                 // Redirect to index
1259                 redirectToUrl('modules.php?module=index&amp;msg=' . getCode('EXTENSION_PROBLEM') . '&amp;ext=' . $ext_name);
1260         } // END - if
1261 }
1262
1263 // Determines wether the given extension is installed
1264 function isExtensionInstalled ($ext_name) {
1265         // Default is not installed
1266         $isInstalled = false;
1267
1268         // Check cache ext_version
1269         $isInstalled = isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name]);
1270
1271         // Return status
1272         return $isInstalled;
1273 }
1274
1275 //
1276 ?>