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