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