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