2 /************************************************************************
3 * MXChange v0.2.1 Start: 03/25/2004 *
4 * =============== Last change: 09/29/2004 *
6 * -------------------------------------------------------------------- *
7 * File : extensions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Extension management *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Erweiterungen-Management *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
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);
53 // Init EXT_UPDATE_DEPENDS
54 EXT_INIT_UPDATE_DEPENDS();
56 // Init current extension name list
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))) {
63 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
69 // Construct include filename and FQFN for extension file
70 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
71 $FQFN = constant('PATH') . $INC;
73 // Is the extension file NOT there?
74 if (!isIncludeReadable($INC)) {
76 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
82 // Load extension's own language file
83 loadLanguageFile($ext_name);
85 // Construct FQFN for functions file
86 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
88 // Is this include there?
89 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
91 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Functions loaded.");
92 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
93 loadIncludeOnce($funcsInclude);
94 } elseif ((isDebugModeEnabled()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
95 // No functions file is not so good...
96 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it.",
101 // Extensions are not deprecated by default
102 EXT_SET_DEPRECATED('N');
104 // Extensions are not always active by default
105 EXT_SET_ALWAYS_ACTIVE('N');
107 // Extension update notes
108 EXT_SET_UPDATE_NOTES('');
110 // Include the extension file
111 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Extension loaded.");
114 // Is this extension deprecated?
115 if (EXT_GET_DEPRECATED() == 'Y') {
116 // Deactivate the extension
117 DEACTIVATE_EXTENSION($ext_name);
123 // Mark it as loaded in normal mode
124 if (empty($EXT_LOAD_MODE)) {
126 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
133 // Registeres an extension and possible update depencies
134 function REGISTER_EXTENSION ($ext_name, $task_id, $dry_run = false, $logout = true) {
135 // Set current extension name
136 EXT_SET_CURR_NAME($ext_name);
138 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - ENTERED!<br />");
139 // This shall never do a non-admin user or if the extension is active (already installed)
140 if ((!IS_ADMIN()) || (EXT_IS_ACTIVE($ext_name))) {
144 // When this extension is already in install/update phase, all is fine
145 if (EXT_IS_REGISTER_RUNNING($ext_name)) {
146 // Then abort here which is fine
147 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - ALREADY!<br />");
151 // This registration is running
152 EXT_ADD_RUNNING_REGISTRATION($ext_name);
154 // Init EXT_UPDATE_DEPENDS
155 EXT_INIT_UPDATE_DEPENDS();
157 // Is the task id zero? Then we need to auto-fix it here
159 // Try to find the task
160 $task_id = DETERMINE_EXTENSION_TASK_ID(EXT_GET_CURR_NAME());
162 // Still zero and not in dry-run?
163 if (($task_id == 0) && (!$dry_run)) {
164 // Then request a bug report
165 debug_report_bug(sprintf("%s: task_id is still zero after DETERMINE_EXTENSION_TASK_ID(%s)",
172 // Init queries and notes
181 // By default we have no failures
182 EXT_SET_REPORTS_FAILURE(false);
184 // Does this extension exists?
185 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME()."");
186 if (LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'register', '', $dry_run)) {
187 // Set current extension name again
188 EXT_SET_CURR_NAME($ext_name);
190 // And run possible updates
191 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "".EXT_GET_CURR_NAME());
192 $history = EXT_GET_VER_HISTORY();
193 foreach ($history as $ver) {
194 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".EXT_GET_CURR_NAME().", ext_ver={$ver}");
195 // Load extension in update mode
196 LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'update', $ver, $dry_run);
198 // Add update notes to our output
199 ADD_EXTENSION_NOTES($ver);
202 // Does this extension depends on an outstanding update of another update?
203 for ($dmy = EXT_GET_UPDATE_ITERATOR(); EXT_GET_UPDATE_ITERATOR() < EXT_COUNT_UPDATE_DEPENDS();) {
205 $ext_update = EXT_GET_ITERATOR_UPDATE_DEPENDS();
207 // Increment here to avoid endless loop
208 EXT_INCREMENT_UPDATE_INTERATOR();
210 // Check for required file
211 if (LOAD_EXTENSION($ext_update, 'register', '', $dry_run)) {
212 // Set current extension name again
213 EXT_SET_CURR_NAME($ext_name);
215 // If versions mismatch update extension first
216 $ext_ver = GET_EXT_VERSION($ext_update);
218 // Extension version set? If empty the extension is not registered
219 if (empty($ext_ver)) {
220 // Extension not registered so far so first load task's ID...
221 $task = DETERMINE_EXTENSION_TASK_ID($ext_update);
225 // Try to register the extension
226 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().":ext_update=".$ext_update.",taskId=".$task."<br />");
227 $test = REGISTER_EXTENSION($ext_update, $task, $dry_run, false);
229 // Reset extension name
230 EXT_SET_CURR_NAME($ext_name);
231 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'); var_dump($test);
233 } elseif ($ext_ver != EXT_GET_VERSION()) {
234 // Ok, update this extension now
235 EXTENSION_UPDATE($ext_update, $ext_ver, $dry_run);
240 // Nothing to register / update before...
244 // Required file for update does not exists!
246 // But this is fine for the first time...
249 // Restore the current extension name
250 EXT_SET_CURR_NAME($ext_name);
253 // Is there no update?
254 if (EXT_COUNT_UPDATE_DEPENDS(EXT_GET_CURR_NAME()) == 0) {
255 // Then test is passed!
259 // Switch back to register mode
260 $EXT_LOAD_MODE = 'register';
262 // Remains true if extension registration reports no failures
263 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'); var_dump($test);
264 $test = (($test === true) && (EXT_GET_REPORTS_FAILURE() === false));
265 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'); var_dump($test);
267 // Does everthing before wents ok?
268 if ($test === true) {
269 // "Dry-run-mode" activated?
270 if ((!$dry_run) && (!EXT_IS_ON_REMOVAL_LIST())) {
271 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".EXT_GET_CURR_NAME());
272 // Init SQLs and transfer ext->generic
274 SET_SQLS(GET_EXT_SQLS());
276 // Run installation pre-installation filters
277 runFilterChain('pre_extension_installed', array('dry_run' => $dry_run));
279 // Register extension
280 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:insert=".EXT_GET_CURR_NAME().'/'.EXT_GET_VERSION()." - INSERT!<br />");
281 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_extensions` (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
282 array(EXT_GET_CURR_NAME(), EXT_GET_ALWAYS_ACTIVE(), EXT_GET_VERSION()), __FUNCTION__, __LINE__);
284 // Remove cache file(s) if extension is active
285 runFilterChain('post_extension_installed', array('ext_name' => EXT_GET_CURR_NAME(), 'task_id' => $task_id));
287 // Remove all SQL commands
290 // In normal mode return a true on success
292 } elseif ($dry_run) {
293 // Init SQLs and transfer ext->generic
295 SET_SQLS(GET_EXT_SQLS());
297 // Rewrite SQL command to keep { and } inside for dry-run
298 foreach (GET_SQLS() as $key => $sql) {
299 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
300 SET_SQL_KEY($key, $sql);
303 // In "dry-run" mode return array with all SQL commands
306 // Remove all SQL commands
309 // Extension has been removed for updates, so all is fine!
313 // No, an error occurs while registering extension :-(
314 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "".EXT_GET_CURR_NAME());
317 } elseif (($task_id > 0) && (EXT_GET_CURR_NAME() != '')) {
318 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()."<br />");
319 // Remove task from system when id and extension's name is valid
320 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
321 array(bigintval($task_id)), __FUNCTION__, __LINE__);
324 // Is this the sql_patches?
325 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ':'.EXT_GET_CURR_NAME()."/{$EXT_LOAD_MODE}");
326 if ((EXT_GET_CURR_NAME() == 'sql_patches') && (($EXT_LOAD_MODE == 'register') || ($EXT_LOAD_MODE == 'remove')) && (!$dry_run) && ($test)) {
327 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
328 if ($logout === true) {
329 // Then redirect to logout
330 redirectToUrl('modules.php?module=admin&logout=1&' . $EXT_LOAD_MODE . '=sql_patches');
332 // Add temporary filter
333 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
334 $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
338 // Return status code
339 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - LEFT!<br />");
340 //* DEBUG: */ var_dump($ret);
344 // Run SQL queries for given extension id
345 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
346 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
347 // This shall never do a non-admin user!
348 if (!IS_ADMIN()) return false;
350 // Get extension's name
351 $ext_name = GET_EXT_NAME($ext_id);
353 // If it is not set then maybe there is no extension for that ID number
354 if (empty($ext_name)) {
355 // We should fix these all!
356 debug_report_bug(__FUNCTION__ . ': ext_name is empty. ext_id=' . $ext_id);
359 // Set current SQL name
360 EXT_SET_CURR_NAME($ext_name);
362 // Init EXT_UPDATE_DEPENDS
363 EXT_INIT_UPDATE_DEPENDS();
368 // By default no SQL has been executed
371 // Load extension in detected mode
372 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]=".EXT_GET_CURR_NAME()."");
373 LOAD_EXTENSION(EXT_GET_CURR_NAME(), $load_mode, '', false);
377 SET_SQLS(GET_EXT_SQLS());
379 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".COUNT_SQLS()."");
380 if ((IS_SQLS_VALID() && (COUNT_SQLS() > 0))) {
381 // Run any filters depending on the action here
382 runFilterChain('extension_' . $load_mode, $ext_name);
384 // Run SQL commands...
385 runFilterChain('run_sqls');
388 if ($load_mode == 'remove') {
389 // Delete this extension (remember to remove it from your server *before* you click on welcome!
390 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
391 array(EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
395 // Remove cache file(s) if extension is active
396 if (((EXT_IS_ACTIVE('cache')) || (GET_EXT_VERSION('cache') != '')) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
398 runFilterChain('post_extension_run_sql', EXT_GET_CURR_NAME());
401 // Is this the sql_patches?
402 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": id=".$ext_id.",currName=".EXT_GET_CURR_NAME().",loadMode=".$load_mode);
403 if ((EXT_GET_CURR_NAME() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
404 // Then redirect to logout
405 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
406 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
410 // Check if given extension is active
411 function EXT_IS_ACTIVE ($ext_name) {
412 // Extensions are all inactive during installation
413 if ((!isInstalled()) || (isInstalling()) || (empty($ext_name))) return false;
415 // Not active is the default
419 if (isset($GLOBALS['cache_array']['extensions']['ext_active'][$ext_name])) {
421 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
422 $active = $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name];
425 incrementConfigEntry('cache_hits');
426 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
427 // @TODO Extension is loaded, what next?
428 app_die(__FUNCTION__, __LINE__, "LOADED:$ext_name");
429 } elseif (($ext_name == 'cache') || (GET_EXT_VERSION('cache') == '')) {
430 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
431 // Load from database
432 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
433 array($ext_name), __FUNCTION__, __LINE__);
436 if (SQL_NUMROWS($result) == 1) {
438 list($active) = SQL_FETCHROW($result);
442 SQL_FREERESULT($result);
445 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: {$active}");
446 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = $active;
448 // Extension not active!
449 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
450 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = 'N';
454 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name={$ext_name},active={$active}");
456 // Is this extension activated? (For admins we always have active extensions...)
457 return ($active == 'Y');
459 // Get version from extensions
460 function GET_EXT_VERSION ($ext_name) {
461 // By default no extension is found
464 // Empty extension name should be fixed!
465 if (empty($ext_name)) {
466 // Please report this bug!
467 debug_report_bug(__FUNCTION__ . ': ext_name is empty which is not allowed here.');
470 // Extensions are all inactive during installation
471 if ((!isInstalled()) || (isInstalling())) return "";
472 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
474 // Is the cache written?
475 if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
476 // Load data from cache
477 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
478 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
481 incrementConfigEntry('cache_hits');
482 } elseif (!isCacheInstanceValid()) {
483 // Load from database
484 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
485 array($ext_name), __FUNCTION__, __LINE__);
486 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
488 // Is the extension there?
489 if (SQL_NUMROWS($result) == 1) {
491 list($ext_ver) = SQL_FETCHROW($result);
492 } elseif (isDebugModeEnabled()) {
494 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
498 SQL_FREERESULT($result);
501 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
505 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
509 // Updates a given extension with current extension version to latest version
510 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
511 // Only admins are allowed to update extensions
512 if ((!IS_ADMIN()) || (empty($ext_name))) return false;
514 // Set current SQL name
515 EXT_SET_CURR_NAME($ext_name);
522 // Load extension in test mode
523 LOAD_EXTENSION($ext_name, 'test', $ext_ver, $dry_run);
525 // Save version history
526 $history = EXT_GET_VER_HISTORY();
528 // Remove old SQLs array to prevent possible bugs
531 // Check if version is updated
532 if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
533 // Search for starting point
534 $start = array_search($ext_ver, $history);
536 // And load SQL queries in order of version history
537 for ($idx = ($start + 1); $idx < count($history); $idx++) {
538 // Set current extension name
539 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__.'['.__LINE__.':] ext_name='.$ext_name."<br />");
540 EXT_SET_CURR_NAME($ext_name);
542 // Set extension version
543 $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()] = $history[$idx];
546 LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'update', $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], $dry_run);
549 $depencies = EXT_GET_UPDATE_DEPENDS();
552 if (count($depencies) > 0) {
553 // Apply all extension depencies
554 foreach ($depencies as $ext_depend) {
556 EXT_SET_CURR_NAME($ext_depend);
558 // Is the extension there?
559 if (GET_EXT_VERSION($ext_depend) != '') {
560 // Update another extension first!
561 $test = EXTENSION_UPDATE($ext_depend, GET_EXT_VERSION($ext_depend), $dry_run);
563 // Register new extension
564 $test = REGISTER_EXTENSION($ext_depend, 0, $dry_run, false);
569 EXT_SET_CURR_NAME($ext_name);
573 ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
576 // In real-mode execute any existing includes
578 $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
579 runFilterChain('load_includes');
580 SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
581 unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
586 SET_SQLS(GET_EXT_SQLS());
589 runFilterChain('run_sqls', array('dry_run' => $dry_run));
593 CREATE_EXTENSION_UPDATE_TASK(getCurrentAdminId(), EXT_GET_CURR_NAME(), $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], SQL_ESCAPE(EXT_GET_NOTES(EXT_GET_NOTES())));
595 // Update extension's version
596 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE `ext_name`='%s' LIMIT 1",
597 array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
601 unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
603 // Run filters on success extension update
604 runFilterChain('extension_update', EXT_GET_CURR_NAME());
609 // Output verbose SQL table for extension
610 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = '', $dashed = '', $switch = false, $width = '100%') {
613 // Then fix it to default
614 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
617 // Are there some queries in $queries?
618 if (count($queries) > 0) {
619 // Then use them instead!
627 // Do we have queries?
628 if ((IS_SQLS_VALID()) && (GET_EXT_VERSION('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
629 foreach (GET_SQLS() as $idx => $sql) {
633 // Output command if set
635 // Prepare output for template
643 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
645 // Switch color and count up
651 // Prepare content for template
659 // Load main template
660 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
661 } elseif ((GET_EXT_VERSION('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
662 // No addional SQL commands to run
663 $OUT = LOAD_TEMPLATE('admin_settings_saved', true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
670 // Get extension name from id
671 function GET_EXT_NAME ($ext_id) {
672 // Init extension name
676 if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
678 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
681 incrementConfigEntry('cache_hits');
682 } elseif (!EXT_IS_ACTIVE('cache')) {
683 // Load from database
684 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `id`=%s LIMIT 1",
685 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
686 list($ret) = SQL_FETCHROW($result);
687 SQL_FREERESULT($result);
690 // Return the extension name
694 // Get extension id from name
695 function GET_EXT_ID ($ext_name) {
698 if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
700 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
703 incrementConfigEntry('cache_hits');
704 } elseif (!EXT_IS_ACTIVE('cache')) {
705 // Load from database
706 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
707 array($ext_name), __FUNCTION__, __LINE__);
708 list($ret) = SQL_FETCHROW($result);
709 SQL_FREERESULT($result);
716 // Activate given extension
717 function ACTIVATE_EXTENSION ($ext_name) {
718 // Activate the extension
719 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
720 array($ext_name), __FUNCTION__, __LINE__);
722 // Extension has been activated?
723 if (SQL_AFFECTEDROWS() == 1) {
724 // Then run all queries
725 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), 'activate');
729 // Deactivate given extension
730 function DEACTIVATE_EXTENSION($ext_name) {
731 // Activate the extension
732 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
733 array($ext_name), __FUNCTION__, __LINE__);
735 // Extension has been activated?
736 if (SQL_AFFECTEDROWS() == 1) {
737 // Then run all queries
738 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), 'deactivate');
741 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
744 sendAdminNotification(
745 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
746 'admin_ext_deactivated',
747 array('ext_name' => $ext_name)
752 // Checks wether the extension is older than given
753 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
754 // Get current extension version
755 $currVersion = GET_EXT_VERSION($ext_name);
757 // Remove all dots from both versions
758 $currVersion = str_replace('.', '', $currVersion);
759 $ext_ver = str_replace('.', '', $ext_ver);
761 // Now compare both and return the result
762 return ($currVersion < $ext_ver);
765 // Creates a new task for updated extension
766 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
767 // Create subject line
768 $subject = '[UPDATE-'.$ext_name.'-'.$ext_ver.':] {--ADMIN_UPDATE_EXT_SUBJ--}';
770 // Is the extension there?
771 if (GET_EXT_VERSION($ext_name) != '') {
772 // Check if task is not there
773 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
774 // Task not created so it's a brand-new extension which we need to register and create a task for!
775 createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $admin_id);
780 // Creates a new task for newly installed extension
781 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
782 // Not installed and do we have created a task for the admin?
783 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == '')) {
784 // Set default message if ext-foo is missing
785 $message = sprintf(getMessage('ADMIN_EXT_TEXT_FILE_MISSING'), $ext);
788 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
794 // Load text for task if found
795 if (isFileReadable($tpl)) {
796 // Load extension's own text template (HTML!)
797 $message = LOAD_TEMPLATE('ext_' . $ext, true);
799 // Write this in debug.log as well
800 DEBUG_LOG(__FUNCTION__, __LINE__, $message);
803 // Task not created so it's a brand-new extension which we need to register and create a task for!
804 createNewTask($subject, $message, 'EXTENSION_UPDATE', 0, $admin_id, false);
808 // Creates a task for automatically deactivated (deprecated) extension
809 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
810 // Create subject line
811 $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
813 // Not installed and do we have created a task for the admin?
814 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != '')) {
815 // Task not created so add it
816 createNewTask($subject, SQL_ESCAPE(LOAD_TEMPLATE('task_ext_deactivated', true, $ext)), 'EXTENSION_DEACTIVATION');
820 // Checks if the module has a menu
821 function MODULE_HAS_MENU ($mod, $forceDb = false) {
822 // All is false by default
825 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "mod={$mod},cache=".GET_EXT_VERSION('cache'));
826 if (GET_EXT_VERSION('cache') >= '0.1.2') {
827 // Cache version is okay, so let's check the cache!
828 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
829 // Check module cache and count hit
830 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
831 incrementConfigEntry('cache_hits');
832 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
833 // Check cache and count hit
834 $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == 'Y');
835 incrementConfigEntry('cache_hits');
836 } elseif ((IS_ADMIN()) && ($mod == 'admin')) {
837 // Admin module has always a menu!
840 } elseif ((GET_EXT_VERSION('sql_patches') >= '0.3.6') && ((!EXT_IS_ACTIVE('cache')) || ($forceDb === true))) {
841 // Check database for entry
842 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE `module`='%s' LIMIT 1",
843 array($mod), __FUNCTION__, __LINE__);
846 if (SQL_NUMROWS($result) == 1) {
847 // Load "has_menu" column
848 list($has_menu) = SQL_FETCHROW($result);
851 $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
853 // Does it have a menu?
854 $ret = ($has_menu == 'Y');
858 SQL_FREERESULT($result);
859 } elseif (GET_EXT_VERSION('sql_patches') == '') {
860 // No sql_patches installed, so maybe in admin area or no admin registered?
861 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == 'admin')); // Then there is a menu!
865 //* DEBUG: */ OUTPUT_HTML(__FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'; var_dump($ret));
869 // Determines the task id for given extension
870 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
871 // Default is not found
874 // Search for extension task's id
875 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
876 array($ext_name), __FUNCTION__, __LINE__);
879 if (SQL_NUMROWS($result) == 1) {
880 // Task found so load task's ID and register extension...
881 list($task_id) = SQL_FETCHROW($result);
885 SQL_FREERESULT($result);
891 // Determines the task id for given subject
892 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
893 // Default is not found
896 // Search for task id
897 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
898 array($subject), __FUNCTION__, __LINE__);
901 if (SQL_NUMROWS($result) == 1) {
902 // Task found so load task's ID and register extension...
903 list($task_id) = SQL_FETCHROW($result);
907 SQL_FREERESULT($result);
913 // Add updates notes for given version
914 function ADD_EXTENSION_NOTES ($ver) {
915 // Init notes/content
916 $out = ''; $content = array();
918 // Is do we have verbose output enabled?
919 if ((getConfig('verbose_sql') == 'Y') || (!EXT_IS_ACTIVE('sql_patches'))) {
920 // Update notes found?
921 if (EXT_GET_UPDATE_NOTES() != '') {
922 // Update notes found
925 'notes' => EXT_GET_UPDATE_NOTES()
929 EXT_SET_UPDATE_NOTES('');
930 } elseif (($ver == '0.0') || ($ver == '0.0.0')) {
934 'notes' => getMessage('INITIAL_RELEASE')
937 // No update notes found!
940 'notes' => getMessage('NO_UPDATE_NOTES')
945 $out = LOAD_TEMPLATE('admin_ext_notes', true, $content);
949 EXT_APPEND_NOTES($out);
952 // Getter for CSS files array
953 function EXT_GET_CSS_FILES () {
954 // By default no additional CSS files are found
957 // Is the array there?
958 if (isset($GLOBALS['css_files'])) {
960 $cssFiles = $GLOBALS['css_files'];
967 // Init CSS files array
968 function EXT_INIT_CSS_FILES () {
970 $GLOBALS['css_files'] = array();
974 function EXT_ADD_CSS_FILE ($file) {
975 // Is the array there?
976 if (!isset($GLOBALS['css_files'])) {
977 // Then auto-init them
978 EXT_INIT_CSS_FILES();
982 $GLOBALS['css_files'][] = $file;
985 // Setter for EXT_ALWAYS_ACTIVE flag
986 function EXT_SET_ALWAYS_ACTIVE ($active) {
987 $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()] = (string) $active;
990 // Getter for EXT_ALWAYS_ACTIVE flag
991 function EXT_GET_ALWAYS_ACTIVE () {
992 return $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()];
995 // Setter for EXT_VERSION flag
996 function EXT_SET_VERSION ($version) {
997 $GLOBALS['ext_version'][EXT_GET_CURR_NAME()] = (string) $version;
1000 // Getter for EXT_VERSION flag
1001 function EXT_GET_VERSION () {
1002 return $GLOBALS['ext_version'][EXT_GET_CURR_NAME()];
1005 // Setter for EXT_DEPRECATED flag
1006 function EXT_SET_DEPRECATED ($deprecated) {
1007 $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()] = (string) $deprecated;
1010 // Getter for EXT_DEPRECATED flag
1011 function EXT_GET_DEPRECATED () {
1012 return $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()];
1015 // Setter for EXT_UPDATE_DEPENDS flag
1016 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
1017 // Is the update depency empty? (NEED TO BE FIXED!)
1018 if (empty($updateDepends)) {
1019 // Please report this bug!
1020 debug_report_bug("updateDepends is left empty!");
1023 // Is it not yet added?
1024 if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
1025 //* DEBUG */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME().'/'.$updateDepends);
1026 // Add it to the list of extension update depencies map
1027 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
1029 // Remember it in the list of running updates
1030 $GLOBALS['ext_running_updates'][] = $updateDepends;
1034 // Checks wether the given extension registration is in progress
1035 function EXT_IS_REGISTER_RUNNING ($ext_name) {
1036 return ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1039 // Init EXT_UPDATE_DEPENDS flag
1040 function EXT_INIT_UPDATE_DEPENDS () {
1041 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1043 // Init update depency map automatically if not found
1044 if (!EXT_IS_UPDATE_DEPENDS_INIT()) {
1045 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME()." - INIT!");
1046 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
1049 // Init running updates array
1050 EXT_INIT_RUNNING_UPDATES();
1053 // Adds an extension as "registration in progress"
1054 function EXT_ADD_RUNNING_REGISTRATION ($ext_name) {
1056 if (!EXT_IS_REGISTER_RUNNING($ext_name)) {
1058 $GLOBALS['ext_register_running'][] = $ext_name;
1062 // Checks wether EXT_UPDATE_DEPENDS is initialized
1063 function EXT_IS_UPDATE_DEPENDS_INIT () {
1064 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1065 return (isset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]));
1068 // Initializes the list of running updates
1069 function EXT_INIT_RUNNING_UPDATES () {
1070 // Auto-init ext_running_updates
1071 if (!isset($GLOBALS['ext_running_updates'])) {
1072 $GLOBALS['ext_running_updates'] = array();
1073 $GLOBALS['ext_register_running'] = array();
1077 // Getter for EXT_UPDATE_DEPENDS flag
1078 function EXT_GET_UPDATE_DEPENDS () {
1079 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1080 return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1083 // Getter for next iterator depency
1084 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1085 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1086 return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1089 // Counter for extension update depencies
1090 function EXT_COUNT_UPDATE_DEPENDS () {
1091 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1092 return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1095 // Removes given extension from update denpency list
1096 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1098 $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1101 if ($key !== false) {
1103 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1105 // And sort the array
1106 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1110 // Init iterator for update depencies
1111 function EXT_INIT_UPDATE_ITERATOR () {
1112 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1113 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1116 // Getter for depency iterator
1117 function EXT_GET_UPDATE_ITERATOR () {
1118 // Auto-init iterator
1119 if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1122 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME().'/'.$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]);
1123 return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1126 // Increments the update iterator
1127 function EXT_INCREMENT_UPDATE_INTERATOR () {
1128 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1129 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1132 // Setter for EXT_REPORTS_FAILURE flag
1133 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1134 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1137 // Getter for EXT_REPORTS_FAILURE flag
1138 function EXT_GET_REPORTS_FAILURE () {
1139 return $GLOBALS['ext_reports_failure'];
1142 // Setter for EXT_VER_HISTORY flag
1143 function EXT_SET_VER_HISTORY ($verHistory) {
1144 $GLOBALS['ext_ver_history'] = (array) $verHistory;
1147 // Getter for EXT_VER_HISTORY array
1148 function EXT_GET_VER_HISTORY () {
1149 return $GLOBALS['ext_ver_history'];
1152 // Setter for EXT_UPDATE_NOTES flag
1153 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1154 $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1157 // Getter for EXT_UPDATE_NOTES flag
1158 function EXT_GET_UPDATE_NOTES () {
1159 return $GLOBALS['ext_update_notes'];
1162 // Init extension notice
1163 function EXT_INIT_NOTES () {
1164 $GLOBALS['ext_notes'] = '';
1167 // Append extension notice
1168 function EXT_APPEND_NOTES ($notes) {
1169 $GLOBALS['ext_notes'] .= (string) $notes;
1172 // Getter for extension notes
1173 function EXT_GET_NOTES () {
1174 return $GLOBALS['ext_notes'];
1177 // Setter for current extension name
1178 function EXT_SET_CURR_NAME ($ext_name) {
1179 $GLOBALS['curr_ext_name'] = (string) $ext_name;
1182 // Getter for current extension name
1183 function EXT_GET_CURR_NAME () {
1184 if (isset($GLOBALS['curr_ext_name'])) {
1185 return $GLOBALS['curr_ext_name'];
1189 debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1192 // Init SQLs array for current extension
1193 function INIT_EXT_SQLS () {
1194 // Auto-init the array now...
1195 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1196 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1200 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1201 function ADD_EXT_SQL ($sql) {
1202 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1205 // Getter for SQLs array for current extension
1206 function GET_EXT_SQLS () {
1207 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1208 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1209 // Not found, should not happen
1210 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1216 return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1219 // Removes SQLs for current extension
1220 function UNSET_EXT_SQLS () {
1221 unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1224 // Auto-initializes the removal list
1225 function EXT_INIT_REMOVAL_LIST () {
1226 // Is the remove list there?
1227 if (!isset($GLOBALS['ext_update_remove'])) {
1229 $GLOBALS['ext_update_remove'] = array();
1233 // Checks wether the current extension is on the removal list
1234 function EXT_IS_ON_REMOVAL_LIST () {
1235 // Init removal list
1236 EXT_INIT_REMOVAL_LIST();
1239 return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1242 // Adds the current extension to the removal list
1243 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1245 $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1248 // Getter for removal list
1249 function EXT_GET_REMOVAL_LIST () {
1250 // Return the removal list
1251 return $GLOBALS['ext_update_remove'];
1254 // Redirects if the provided extension is not installed
1255 function redirectOnUninstalledExtension ($ext_name) {
1256 // So is the extension there?
1257 if ((!isExtensionInstalled($ext_name)) || (!EXT_IS_ACTIVE($ext_name))) {
1258 // Redirect to index
1259 redirectToUrl('modules.php?module=index&msg=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1263 // Determines wether the given extension is installed
1264 function isExtensionInstalled ($ext_name) {
1265 // Default is not installed
1266 $isInstalled = false;
1268 // Check cache ext_version
1269 $isInstalled = isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name]);
1272 return $isInstalled;