From 29157685184b9aa2ddbf42e9d2e49141af95f7e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 23 Jul 2010 02:20:26 +0000 Subject: [PATCH] Fixes for auto-activation of extensions while registration phase --- DOCS/TODOs.txt | 8 +-- inc/extensions-functions.php | 72 +++++++++++++++++---------- inc/filters.php | 17 ++++--- inc/modules/admin/what-extensions.php | 61 ++++++++++++----------- inc/sql-functions.php | 1 + 5 files changed, 93 insertions(+), 66 deletions(-) diff --git a/DOCS/TODOs.txt b/DOCS/TODOs.txt index 279a03b186..cc6ff6d87e 100644 --- a/DOCS/TODOs.txt +++ b/DOCS/TODOs.txt @@ -37,9 +37,9 @@ ./inc/extensions/ext-rallye.php:94: // @TODO Fix config_rallye_prices to list_rallye_prices ./inc/extensions/ext-yoomedia.php:123: // @TODO Can this be moved into a database table? ./inc/extensions/ext-yoomedia.php:56:// @TODO Only deprecated when 'ext-network' is ready! setExtensionDeprecated('Y'); -./inc/extensions-functions.php:146: // @TODO Do we still need this? setExtensionUpdateNotes(''); -./inc/extensions-functions.php:427:// @TODO Change from ext_id to ext_name (not just even the variable! ;-) ) -./inc/extensions-functions.php:551: // @TODO Extension is loaded, what next? +./inc/extensions-functions.php:152: // @TODO Do we still need this? setExtensionUpdateNotes(''); +./inc/extensions-functions.php:434:// @TODO Change from ext_id to ext_name (not just even the variable! ;-) ) +./inc/extensions-functions.php:564: // @TODO Extension is loaded, what next? ./inc/functions.php:115: // @TODO Extension 'msg' does not exist ./inc/functions.php:1486: // @TODO Move this SQL code into a function, let's say 'getTimestampFromPoolId($id) ? ./inc/functions.php:1509: // @TODO Rewrite this old lost code to a template @@ -117,7 +117,7 @@ ./inc/modules/admin/what-del_email.php:61: // @TODO Unused: cat_id, payment_id ./inc/modules/admin/what-edit_user.php:20: * @TODO Add support for ext-country * ./inc/modules/admin/what-email_stats.php:48:// @TODO Unused at the moment -./inc/modules/admin/what-extensions.php:368: // @TODO Rewrite this to a filter +./inc/modules/admin/what-extensions.php:369: // @TODO Rewrite this to a filter ./inc/modules/admin/what-guest_add.php:131: // @TODO This can be somehow rewritten to a function ./inc/modules/admin/what-guest_add.php:69: // @TODO Cant this be rewritten? ./inc/modules/admin/what-guest_add.php:82: // @TODO This can be somehow rewritten to a function diff --git a/inc/extensions-functions.php b/inc/extensions-functions.php index 9abe929fef..e63855da65 100644 --- a/inc/extensions-functions.php +++ b/inc/extensions-functions.php @@ -65,11 +65,17 @@ function loadExtension ($ext_name, $ext_mode, $ext_ver = '', $dry_run = false) { // Set current extension version setCurrentExtensionVersion($ext_ver); } else { - // Set it to 0.0 + // Set it to 0.0 by default setCurrentExtensionVersion('0.0'); + // Is the extension installed? + if ((isExtensionInstalled($ext_name)) && ($ext_mode != 'register')) { + // Get extension's version + setCurrentExtensionVersion(getExtensionVersion($ext_name)); + } // END - if + // In all but test-mode we need these messages to debug! Please report all (together, e.g.) - if ($ext_mode != 'test') { + if (($ext_mode != 'test') && (getCurrentExtensionVersion() == '0.0')) { // Log empty versions not in test-mode, but maybe it is fine... logDebugMessage(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run)); } // END - if @@ -368,6 +374,10 @@ function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true // Use the insert id as extension id and cache it for early usage $GLOBALS['cache_array']['extension']['ext_id'][getCurrentExtensionName()] = SQL_INSERTID(); + $GLOBALS['cache_array']['extension']['ext_name'][SQL_INSERTID()] = getCurrentExtensionName(); + + // Mark it as installed + $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true; // Remove cache file(s) if extension is active runFilterChain('post_extension_installed', array( @@ -380,9 +390,6 @@ function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true initExtensionSqls(true); initExtensionNotes(true); - // Mark it as installed - $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true; - // In normal mode return a true on success $ret = true; } elseif (isExtensionDryRun() === true) { @@ -442,20 +449,23 @@ function doExtensionSqls ($ext_id, $load_mode) { } // END - if // Init array - initExtensionSqls(); + initExtensionSqls(true); // By default no SQL has been executed $sqlRan = false; // Load extension in detected mode - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName()); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName() . ',load_mode=' . $load_mode); loadExtension(getCurrentExtensionName(), $load_mode, '', false); // Init these SQLs initSqls(); setSqlsArray(getExtensionSqls()); + // Debug message //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls()); + + // Do we have entries? if (isSqlsValid()) { // Run SQL commands... runFilterChain('run_sqls'); @@ -506,6 +516,9 @@ function isExtensionInstalled ($ext_name) { // Look in database $ext_id = getExtensionId($ext_name); + // Log debug message + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension ' . $ext_name . ' has ext_id=' . $ext_id); + // Do we have a record? $isInstalled = ($ext_id > 0); @@ -902,38 +915,44 @@ function isExtensionIdValid ($ext_id) { // Activate given extension function doActivateExtension ($ext_name) { + // Is the extension installed? + if (!isExtensionInstalled($ext_name)) { + // Non-installed extensions cannot be activated + debug_report_bug(__FUNCTION__, __LINE__, 'Tried to activate non-installed extension ' . $ext_name); + } // END - if + // Activate the extension SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1", array($ext_name), __FUNCTION__, __LINE__); - // Extension has been activated? - if (SQL_AFFECTEDROWS() == 1) { - // Then run all queries - doExtensionSqls(getExtensionId($ext_name), 'activate'); - } // END - if + // Then run all queries + doExtensionSqls(getExtensionId($ext_name), 'activate'); } // Deactivate given extension -function doDeactivateExtension($ext_name) { +function doDeactivateExtension ($ext_name) { + // Is the extension installed? + if (!isExtensionInstalled($ext_name)) { + // Non-installed extensions cannot be activated + debug_report_bug(__FUNCTION__, __LINE__, 'Tried to deactivate non-installed extension ' . $ext_name); + } // END - if + // Activate the extension SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1", array($ext_name), __FUNCTION__, __LINE__); - // Extension has been activated? - if (SQL_AFFECTEDROWS() == 1) { - // Then run all queries - doExtensionSqls(getExtensionId($ext_name), 'deactivate'); + // Then run all queries + doExtensionSqls(getExtensionId($ext_name), 'deactivate'); - // Create new task (we ignore the task id here) - createExtensionDeactivationTask($ext_name); + // Create new task (we ignore the task id here) + createExtensionDeactivationTask($ext_name); - // Notify the admin - sendAdminNotification( - '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}', - 'admin_extension_deactivated', - array('ext_name' => $ext_name) - ); - } // END - if + // Notify the admin + sendAdminNotification( + '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}', + 'admin_extension_deactivated', + array('ext_name' => $ext_name) + ); } // Checks wether the extension is older than given @@ -1448,6 +1467,7 @@ function initExtensionSqls ($force = false) { // Adds SQLs to the SQLs array but "assigns" it with current extension name function addExtensionSql ($sql) { // Add it + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',ext_version=' . getCurrentExtensionVersion() . ',sql=' . $sql); $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql; } diff --git a/inc/filters.php b/inc/filters.php index 254630b39e..04ee9a451e 100644 --- a/inc/filters.php +++ b/inc/filters.php @@ -165,10 +165,12 @@ function FILTER_REDIRECT_TO_LOGOUT_SQL_PATCHES () { // Filter for auto-activation of a extension function FILTER_AUTO_ACTIVATE_EXTENSION ($data) { + // Debug message + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $data['ext_name'] . ',isExtensionAlwaysActive()=' . intval(isExtensionAlwaysActive())); + // Is this extension always activated? if (isExtensionAlwaysActive()) { // Then activate the extension - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name={$data['ext_name']}
"); doActivateExtension($data['ext_name']); } // END - if @@ -242,13 +244,16 @@ function FILTER_LOAD_INCLUDES ($pool) { // Filter for running SQL commands function FILTER_RUN_SQLS ($data) { // Debug message - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "- Entered!"); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '- Entered!'); // Is the array there? if ((isSqlsValid()) && ((!isset($data['dry_run'])) || ($data['dry_run'] == false))) { // Run SQL commands - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "- Found ".countSqls()." queries to run."); - foreach (getSqls() as $sqls) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '- Found ' . countSqls() . ' queries to run.'); + foreach (getSqls() as $mode=>$sqls) { + // Debug message + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'mode=' . $mode . ',count()=' . count($sqls)); + // New cache format... foreach ($sqls as $sql) { // Trim spaces away @@ -266,7 +271,7 @@ function FILTER_RUN_SQLS ($data) { SQL_ALTER_TABLE($sql, __FUNCTION__, __LINE__, $data['enable_codes']); } else { // Run regular SQL command - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running regular query: ' . $sql . ',enable_codes=' . intval($data['enable_codes']); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running regular query: ' . $sql . ',enable_codes=' . intval($data['enable_codes'])); SQL_QUERY($sql, __FUNCTION__, __LINE__, $data['enable_codes']); } } // END - if @@ -275,7 +280,7 @@ function FILTER_RUN_SQLS ($data) { } // END - if // Debug message - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "- Left!"); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '- Left!'); } // Filter for updating/validating login data diff --git a/inc/modules/admin/what-extensions.php b/inc/modules/admin/what-extensions.php index 3188fea46e..0e130785ef 100644 --- a/inc/modules/admin/what-extensions.php +++ b/inc/modules/admin/what-extensions.php @@ -56,11 +56,11 @@ if (isGetRequestParameterSet('reg_ext')) { // The id comes from task management and it is - of course - *not* the extension's name! } elseif ((isFormSent('change')) && (ifPostContainsSelections()) && (!isDemoModeActive())) { // De-/activate extensions - foreach (postRequestParameter('sel') as $taskId => $ext_active) { + foreach (postRequestParameter('sel') as $ext_id => $ext_active) { // Shall we keep the extension always active? - if ((isset($GLOBALS['cache_array']['always_active'][getExtensionName($taskId)])) && ($GLOBALS['cache_array']['always_active'][getExtensionName($taskId)] == 'Y') && ($ext_active == 'Y')) { + if ((isset($GLOBALS['cache_array']['always_active'][getExtensionName($ext_id)])) && ($GLOBALS['cache_array']['always_active'][getExtensionName($ext_id)] == 'Y') && ($ext_active == 'Y')) { // Keep this extension active! - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_ALWAYS_ACTIVE', getExtensionName($taskId))); + loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_EXTENSION_ALWAYS_ACTIVE', getExtensionName($ext_id))); } else { // Deactivation is default $active = 'N'; @@ -75,10 +75,10 @@ if (isGetRequestParameterSet('reg_ext')) { // Update database SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='%s' WHERE `id`=%s AND `ext_active`='%s' LIMIT 1", - array($active, bigintval($taskId), $ext_active), __FILE__, __LINE__); + array($active, bigintval($ext_id), $ext_active), __FILE__, __LINE__); // Run embeded SQL commands - doExtensionSqls($taskId, getExtensionMode()); + doExtensionSqls($ext_id, getExtensionMode()); } } // END - foreach @@ -89,9 +89,9 @@ if (isGetRequestParameterSet('reg_ext')) { if (isPostRequestParameterSet('modify')) { // Change entries $cache_update = '0'; - foreach (postRequestParameter('sel') as $taskId => $sel) { + foreach (postRequestParameter('sel') as $ext_id => $sel) { // Secure id - $taskId = bigintval($taskId); + $ext_id = bigintval($ext_id); // Change this extension? if ($sel == 1) { @@ -99,17 +99,17 @@ if (isGetRequestParameterSet('reg_ext')) { setExtensionMode('modify'); // Get entry for 'active' - $active = postRequestParameter('active', $taskId); + $active = postRequestParameter('active', $ext_id); // Update extension's record if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) { // Update also CSS column when extensions sql_patches is newer or exact v0.0.6 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_has_css`='%s', `ext_active`='%s' WHERE `id`=%s LIMIT 1", - array(postRequestParameter('css', $taskId), $active, $taskId), __FILE__, __LINE__); + array(postRequestParameter('css', $ext_id), $active, $ext_id), __FILE__, __LINE__); } else { // When extension is older than v0.0.6 there is no column for the CSS information SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='%s' WHERE `id`=%s LIMIT 1", - array($active, $taskId), __FILE__, __LINE__); + array($active, $ext_id), __FILE__, __LINE__); } // Run SQLs on activation / deactivation @@ -119,16 +119,16 @@ if (isGetRequestParameterSet('reg_ext')) { } // END - if // Run embeded SQL commands - doExtensionSqls($taskId, getExtensionMode()); - } - } + doExtensionSqls($ext_id, getExtensionMode()); + } // END - if + } // END - foreach // Redirect to prevent missing cache in js.php redirectToUrl('modules.php?module=admin&what=extensions&edited=' . countPostSelection()); } else { // Edit selected entries $OUT = ''; - foreach (postRequestParameter('sel') as $taskId => $sel) { + foreach (postRequestParameter('sel') as $ext_id => $sel) { // Edit this extension? if (($sel == 'Y') || ($sel != 'Y')) { // Default is no CSS for non-updated mailers @@ -137,12 +137,12 @@ if (isGetRequestParameterSet('reg_ext')) { // Load required data if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) { $result = SQL_QUERY_ESC("SELECT ext_has_css, ext_active FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1", - array(bigintval($taskId)), __FILE__, __LINE__); + array(bigintval($ext_id)), __FILE__, __LINE__); list($css, $active) = SQL_FETCHROW($result); SQL_FREERESULT($result); } else { $result = SQL_QUERY_ESC("SELECT ext_active FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1", - array(bigintval($taskId)), __FILE__, __LINE__); + array(bigintval($ext_id)), __FILE__, __LINE__); list($active) = SQL_FETCHROW($result); SQL_FREERESULT($result); $css = 'X'; @@ -151,14 +151,14 @@ if (isGetRequestParameterSet('reg_ext')) { // Prepare CSS column $cssSelection = '---'; if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) { - $cssSelection = addSelectionBox('yn', $css, 'css', $taskId); + $cssSelection = addSelectionBox('yn', $css, 'css', $ext_id); } // END - if // Prepare data for the row template $content = array( - 'id' => $taskId, - 'name' => getExtensionName($taskId), - 'active' => addSelectionBox('yn', $active, 'active', $taskId), + 'id' => $ext_id, + 'name' => getExtensionName($ext_id), + 'active' => addSelectionBox('yn', $active, 'active', $ext_id), 'css' => $cssSelection, ); @@ -174,19 +174,20 @@ if (isGetRequestParameterSet('reg_ext')) { } elseif ((isPostRequestParameterSet('delete')) && (ifPostContainsSelections()) && (!isDemoModeActive())) { // List extensions and when verbose is enabled SQL statements which will be executed $OUT = ''; - foreach (postRequestParameter('sel') as $taskId => $sel) { + foreach (postRequestParameter('sel') as $ext_id => $sel) { // Init variables $VERBOSE_OUT = ''; initSqls(); // Secure id number - $taskId = bigintval($taskId); + $ext_id = bigintval($ext_id); // Is the id valid? - if (isExtensionIdValid($taskId)) { + if (isExtensionIdValid($ext_id)) { // Get extension name - $ext_name = getExtensionName($taskId); + $ext_name = getExtensionName($ext_id); + // Listing of SQLs enabled? if (isVerboseSqlEnabled()) { // Load SQL commands in remove mode if (loadExtension($ext_name, 'remove', '', true)) { @@ -197,7 +198,7 @@ if (isGetRequestParameterSet('reg_ext')) { // Prepare data for the row template $content = array( - 'id' => $taskId, + 'id' => $ext_id, 'ext_name' => $ext_name, 'ext_ver' => getExtensionVersion($ext_name), 'verbose' => $VERBOSE_OUT @@ -208,7 +209,7 @@ if (isGetRequestParameterSet('reg_ext')) { } else { // Prepare data for the row template $content = array( - 'id' => $taskId + 'id' => $ext_id ); // Not valid! @@ -222,14 +223,14 @@ if (isGetRequestParameterSet('reg_ext')) { } elseif ((isFormSent('remove')) && (ifPostContainsSelections()) && (!isDemoModeActive())) { // Remove extensions from DB (you have to delete all files manually!) $cache_update = '0'; - foreach (postRequestParameter('sel') as $taskId => $active) { + foreach (postRequestParameter('sel') as $ext_id => $active) { // Secure id number - $taskId = bigintval($taskId); + $ext_id = bigintval($ext_id); // Is this extension selected and id valid? - if (($active == 1) && (isExtensionIdValid($taskId))) { + if (($active == 1) && (isExtensionIdValid($ext_id))) { // Run embeded SQL commands - doExtensionSqls($taskId, 'remove'); + doExtensionSqls($ext_id, 'remove'); } // END - if } // END - foreach diff --git a/inc/sql-functions.php b/inc/sql-functions.php index 712bc1967a..11d737489e 100644 --- a/inc/sql-functions.php +++ b/inc/sql-functions.php @@ -54,6 +54,7 @@ function isSqlsInitialized () { // Setter for SQLs array function setSqlsArray ($SQLs) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count()='.count($SQLs)); $GLOBALS['sqls'] = (array) $SQLs; } -- 2.39.5