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 // Construct FQFN for language file
83 $langInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
85 // Is this include there?
86 if ((isFileReadable($langInclude)) && (!isset($GLOBALS['ext_loaded']['lang'][$ext_name]))) {
88 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Language loaded.");
89 $GLOBALS['ext_loaded']['lang'][$ext_name] = true;
90 loadIncludeOnce($langInclude);
91 } elseif (($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
92 // No language file is not so good...
93 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("WARNING: Extension %s has no language file or we cannot read from it. lang=%s",
94 $ext_name, getLanguage()
98 // Construct FQFN for functions file
99 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
101 // Is this include there?
102 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
104 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Functions loaded.");
105 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
106 loadIncludeOnce($funcsInclude);
107 } elseif (($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
108 // No functions file is not so good...
109 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("WARNING: Extension %s has no own functions file or we cannot read from it.",
114 // Extensions are not deprecated by default
115 EXT_SET_DEPRECATED('N');
117 // Extensions are not always active by default
118 EXT_SET_ALWAYS_ACTIVE('N');
120 // Extension update notes
121 EXT_SET_UPDATE_NOTES('');
123 // Include the extension file
124 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Extension loaded.");
127 // Is this extension deprecated?
128 if (EXT_GET_DEPRECATED() == 'Y') {
129 // Deactivate the extension
130 DEACTIVATE_EXTENSION($ext_name);
136 // Mark it as loaded in normal mode
137 if (empty($EXT_LOAD_MODE)) {
139 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
146 // Registeres an extension and possible update depencies
147 function REGISTER_EXTENSION ($ext_name, $task_id, $dry_run = false, $logout = true) {
148 // Set current extension name
149 EXT_SET_CURR_NAME($ext_name);
151 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - ENTERED!<br />\n";
152 // This shall never do a non-admin user or if the extension is active (already installed)
153 if ((!IS_ADMIN()) || (EXT_IS_ACTIVE($ext_name))) {
157 // When this extension is already in install/update phase, all is fine
158 if (EXT_IS_REGISTER_RUNNING($ext_name)) {
159 // Then abort here which is fine
160 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - ALREADY!<br />\n";
164 // This registration is running
165 EXT_ADD_RUNNING_REGISTRATION($ext_name);
167 // Init EXT_UPDATE_DEPENDS
168 EXT_INIT_UPDATE_DEPENDS();
170 // Is the task id zero? Then we need to auto-fix it here
172 // Try to find the task
173 $task_id = DETERMINE_EXTENSION_TASK_ID(EXT_GET_CURR_NAME());
175 // Still zero and not in dry-run?
176 if (($task_id == 0) && (!$dry_run)) {
177 // Then request a bug report
178 debug_report_bug(sprintf("%s: task_id is still zero after DETERMINE_EXTENSION_TASK_ID(%s)",
185 // Init queries and notes
194 // By default we have no failures
195 EXT_SET_REPORTS_FAILURE(false);
197 // Does this extension exists?
198 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME()."");
199 if (LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'register', '', $dry_run)) {
200 // Set current extension name again
201 EXT_SET_CURR_NAME($ext_name);
203 // And run possible updates
204 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "".EXT_GET_CURR_NAME());
205 $history = EXT_GET_VER_HISTORY();
206 foreach ($history as $ver) {
207 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".EXT_GET_CURR_NAME().", ext_ver={$ver}");
208 // Load extension in update mode
209 LOAD_EXTENSION(EXT_GET_CURR_NAME(), 'update', $ver, $dry_run);
211 // Add update notes to our output
212 ADD_EXTENSION_NOTES($ver);
215 // Does this extension depends on an outstanding update of another update?
216 for ($dmy = EXT_GET_UPDATE_ITERATOR(); EXT_GET_UPDATE_ITERATOR() < EXT_COUNT_UPDATE_DEPENDS();) {
218 $ext_update = EXT_GET_ITERATOR_UPDATE_DEPENDS();
220 // Increment here to avoid endless loop
221 EXT_INCREMENT_UPDATE_INTERATOR();
223 // Check for required file
224 if (LOAD_EXTENSION($ext_update, 'register', '', $dry_run)) {
225 // Set current extension name again
226 EXT_SET_CURR_NAME($ext_name);
228 // If versions mismatch update extension first
229 $ext_ver = GET_EXT_VERSION($ext_update);
231 // Extension version set? If empty the extension is not registered
232 if (empty($ext_ver)) {
233 // Extension not registered so far so first load task's ID...
234 $task = DETERMINE_EXTENSION_TASK_ID($ext_update);
238 // Try to register the extension
239 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().":ext_update=".$ext_update.",taskId=".$task."<br />\n";
240 $test = REGISTER_EXTENSION($ext_update, $task, $dry_run, false);
241 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'; var_dump($test);
243 } elseif ($ext_ver != EXT_GET_VERSION()) {
244 // Ok, update this extension now
245 EXTENSION_UPDATE($ext_update, $ext_ver, $dry_run);
250 // Nothing to register / update before...
254 // Required file for update does not exists!
256 // But this is fine for the first time...
259 // Restore the current extension name
260 EXT_SET_CURR_NAME($ext_name);
263 // Is there no update?
264 if (EXT_COUNT_UPDATE_DEPENDS(EXT_GET_CURR_NAME()) == 0) {
265 // Then test is passed!
269 // Switch back to register mode
270 $EXT_LOAD_MODE = 'register';
272 // Remains true if extension registration reports no failures
273 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'; var_dump($test);
274 $test = (($test === true) && (EXT_GET_REPORTS_FAILURE() === false));
275 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'; var_dump($test);
277 // Does everthing before wents ok?
278 if ($test === true) {
279 // "Dry-run-mode" activated?
280 if ((!$dry_run) && (!EXT_IS_ON_REMOVAL_LIST())) {
281 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name=".EXT_GET_CURR_NAME());
282 // Init SQLs and transfer ext->generic
284 SET_SQLS(GET_EXT_SQLS());
286 // Run installation pre-installation filters
287 runFilterChain('pre_extension_installed', array('dry_run' => $dry_run));
289 // Register extension
290 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:insert=".EXT_GET_CURR_NAME().'/'.EXT_GET_VERSION()." - INSERT!<br />\n";
291 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_extensions` (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
292 array(EXT_GET_CURR_NAME(), EXT_GET_ALWAYS_ACTIVE(), EXT_GET_VERSION()), __FUNCTION__, __LINE__);
294 // Remove cache file(s) if extension is active
295 runFilterChain('post_extension_installed', array('ext_name' => EXT_GET_CURR_NAME(), 'task_id' => $task_id));
297 // Remove all SQL commands
300 // In normal mode return a true on success
302 } elseif ($dry_run) {
303 // Init SQLs and transfer ext->generic
305 SET_SQLS(GET_EXT_SQLS());
307 // Rewrite SQL command to keep { and } inside for dry-run
308 foreach (GET_SQLS() as $key => $sql) {
309 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
310 SET_SQL_KEY($key, $sql);
313 // In "dry-run" mode return array with all SQL commands
316 // Remove all SQL commands
319 // Extension has been removed for updates, so all is fine!
323 // No, an error occurs while registering extension :-(
324 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "".EXT_GET_CURR_NAME());
327 } elseif (($task_id > 0) && (EXT_GET_CURR_NAME() != '')) {
328 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()."<br />\n";
329 // Remove task from system when id and extension's name is valid
330 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
331 array(bigintval($task_id)), __FUNCTION__, __LINE__);
334 // Is this the sql_patches?
335 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ':'.EXT_GET_CURR_NAME()."/{$EXT_LOAD_MODE}");
336 if ((EXT_GET_CURR_NAME() == 'sql_patches') && (($EXT_LOAD_MODE == 'register') || ($EXT_LOAD_MODE == 'remove')) && (!$dry_run) && ($test)) {
337 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
338 if ($logout === true) {
339 // Then redirect to logout
340 redirectToUrl('modules.php?module=admin&logout=1&' . $EXT_LOAD_MODE . '=sql_patches');
342 // Add temporary filter
343 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
344 $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
348 // Return status code
349 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME()." - LEFT!<br />\n";
350 //* DEBUG: */ var_dump($ret);
354 // Run SQL queries for given extension id
355 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
356 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
357 // This shall never do a non-admin user!
358 if (!IS_ADMIN()) return false;
360 // Get extension's name
361 $ext_name = GET_EXT_NAME($ext_id);
363 // If it is not set then maybe there is no extension for that ID number
364 if ($ext_name == '') return false;
366 // Set current SQL name
367 EXT_SET_CURR_NAME($ext_name);
369 // Init EXT_UPDATE_DEPENDS
370 EXT_INIT_UPDATE_DEPENDS();
375 // By default no SQL has been executed
378 // Load extension in detected mode
379 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]=".EXT_GET_CURR_NAME()."");
380 LOAD_EXTENSION(EXT_GET_CURR_NAME(), $load_mode, '', false);
384 SET_SQLS(GET_EXT_SQLS());
386 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".COUNT_SQLS()."");
387 if ((IS_SQLS_VALID() && (COUNT_SQLS() > 0))) {
388 // Run SQL commands...
389 runFilterChain('run_sqls');
392 if ($load_mode == 'remove') {
393 // Delete this extension (remember to remove it from your server *before* you click on welcome!
394 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
395 array(EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
399 // Remove cache file(s) if extension is active
400 if (((EXT_IS_ACTIVE('cache')) || (GET_EXT_VERSION('cache') != '')) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
402 runFilterChain('post_extension_run_sql', EXT_GET_CURR_NAME());
405 // Is this the sql_patches?
406 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": id=".$ext_id.",currName=".EXT_GET_CURR_NAME().",loadMode=".$load_mode);
407 if ((EXT_GET_CURR_NAME() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
408 // Then redirect to logout
409 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
410 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
414 // Check if given extension is active
415 function EXT_IS_ACTIVE ($ext_name) {
416 // Extensions are all inactive during installation
417 if ((!isInstalled()) || (isInstalling()) || (empty($ext_name))) return false;
419 // Not active is the default
423 if (isset($GLOBALS['cache_array']['extensions']['ext_active'][$ext_name])) {
425 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
426 $active = $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name];
429 incrementConfigEntry('cache_hits');
430 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
431 // @TODO Extension is loaded, what next?
432 app_die(__FUNCTION__, __LINE__, "LOADED:$ext_name");
433 } elseif (($ext_name == 'cache') || (GET_EXT_VERSION('cache') == '')) {
434 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
435 // Load from database
436 $result = SQL_QUERY_ESC("SELECT ext_active FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
437 array($ext_name), __FUNCTION__, __LINE__);
440 if (SQL_NUMROWS($result) == 1) {
442 list($active) = SQL_FETCHROW($result);
446 SQL_FREERESULT($result);
449 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: {$active}");
450 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = $active;
452 // Extension not active!
453 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
454 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = 'N';
458 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
460 // Is this extension activated? (For admins we always have active extensions...)
461 return ($active == 'Y');
463 // Get version from extensions
464 function GET_EXT_VERSION ($ext_name) {
465 // By default no extension is found
468 // Empty extension name should be fixed!
469 if (empty($ext_name)) {
470 // Please report this bug!
471 debug_report_bug(__FUNCTION__.": ext_name is empty which is not allowed here.");
474 // Extensions are all inactive during installation
475 if ((!isInstalled()) || (isInstalling())) return "";
476 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
478 // Is the cache written?
479 if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
480 // Load data from cache
481 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
482 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
485 incrementConfigEntry('cache_hits');
486 } elseif (!isCacheInstanceValid()) {
487 // Load from database
488 $result = SQL_QUERY_ESC("SELECT ext_version FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
489 array($ext_name), __FUNCTION__, __LINE__);
490 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
492 // Is the extension there?
493 if (SQL_NUMROWS($result) == 1) {
495 list($ext_ver) = SQL_FETCHROW($result);
496 } elseif (isDebugModeEnabled()) {
498 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
502 SQL_FREERESULT($result);
505 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
509 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
513 // Updates a given extension with current extension version to latest version
514 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
515 // Only admins are allowed to update extensions
516 if ((!IS_ADMIN()) || (empty($ext_name))) return false;
518 // Set current SQL name
519 EXT_SET_CURR_NAME($ext_name);
526 // Load extension in test mode
527 LOAD_EXTENSION($ext_name, 'test', $ext_ver, $dry_run);
529 // Save version history
530 $history = EXT_GET_VER_HISTORY();
532 // Remove old SQLs array to prevent possible bugs
535 // Check if version is updated
536 if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
537 // Search for starting point
538 $start = array_search($ext_ver, $history);
540 // And load SQL queries in order of version history
541 for ($idx = ($start + 1); $idx < count($history); $idx++) {
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);
548 if (EXT_GET_UPDATE_DEPENDS() != '') {
549 // Is the extension there?
550 if (GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()) != '') {
551 // Update another extension first!
552 $test = EXTENSION_UPDATE(EXT_GET_UPDATE_DEPENDS(), GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()), $dry_run);
554 // Register new extension
555 $test = REGISTER_EXTENSION(EXT_GET_UPDATE_DEPENDS(), 0, $dry_run, false);
560 ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
563 // In real-mode execute any existing includes
565 $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
566 runFilterChain('load_includes');
567 SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
568 unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
573 SET_SQLS(GET_EXT_SQLS());
576 runFilterChain('run_sqls', array('dry_run' => $dry_run));
580 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())));
582 // Update extension's version
583 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE `ext_name`='%s' LIMIT 1",
584 array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
588 unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
590 // Run filters on success extension update
591 runFilterChain('extension_update', EXT_GET_CURR_NAME());
596 // Output verbose SQL table for extension
597 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = '', $dashed = '', $switch = false, $width = '100%') {
600 // Then fix it to default
601 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
604 // Are there some queries in $queries?
605 if (count($queries) > 0) {
606 // Then use them instead!
614 // Do we have queries?
615 if ((IS_SQLS_VALID()) && (GET_EXT_VERSION('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
616 foreach (GET_SQLS() as $idx => $sql) {
620 // Output command if set
622 // Prepare output for template
630 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
632 // Switch color and count up
638 // Prepare content for template
646 // Load main template
647 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
648 } elseif ((GET_EXT_VERSION('sql_patches') >= '0.0.7') && (getConfig('verbose_sql') == 'Y')) {
649 // No addional SQL commands to run
650 $OUT = LOAD_TEMPLATE('admin_settings_saved', true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
657 // Get extension name from id
658 function GET_EXT_NAME ($ext_id) {
659 // Init extension name
663 if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
665 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
668 incrementConfigEntry('cache_hits');
669 } elseif (!EXT_IS_ACTIVE('cache')) {
670 // Load from database
671 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `id`=%s LIMIT 1",
672 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
673 list($ret) = SQL_FETCHROW($result);
674 SQL_FREERESULT($result);
679 // Get extension id from name
680 function GET_EXT_ID ($ext_name) {
683 if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
685 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
688 incrementConfigEntry('cache_hits');
689 } elseif (!EXT_IS_ACTIVE('cache')) {
690 // Load from database
691 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_extensions` WHERE `ext_name`='%s' LIMIT 1",
692 array($ext_name), __FUNCTION__, __LINE__);
693 list($ret) = SQL_FETCHROW($result);
694 SQL_FREERESULT($result);
701 // Activate given extension
702 function ACTIVATE_EXTENSION ($ext_name) {
703 // Activate the extension
704 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
705 array($ext_name), __FUNCTION__, __LINE__);
707 // Extension has been activated?
708 if (SQL_AFFECTEDROWS() == 1) {
709 // Then run all queries
710 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), 'activate');
714 // Deactivate given extension
715 function DEACTIVATE_EXTENSION($ext_name) {
716 // Activate the extension
717 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
718 array($ext_name), __FUNCTION__, __LINE__);
720 // Extension has been activated?
721 if (SQL_AFFECTEDROWS() == 1) {
722 // Then run all queries
723 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), 'deactivate');
726 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
729 sendAdminNotification(
730 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
731 'admin_ext_deactivated',
732 array('ext_name' => $ext_name)
737 // Checks wether the extension is older than given
738 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
739 // Get current extension version
740 $currVersion = GET_EXT_VERSION($ext_name);
742 // Remove all dots from both versions
743 $currVersion = str_replace('.', '', $currVersion);
744 $ext_ver = str_replace('.', '', $ext_ver);
746 // Now compare both and return the result
747 return ($currVersion < $ext_ver);
750 // Creates a new task for updated extension
751 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
752 // Create subject line
753 $subject = '[UPDATE-'.$ext_name.'-'.$ext_ver.':] {--ADMIN_UPDATE_EXT_SUBJ--}';
755 // Is the extension there?
756 if (GET_EXT_VERSION($ext_name) != '') {
757 // Check if task is not there
758 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
759 // Task not created so it's a brand-new extension which we need to register and create a task for!
760 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())",
761 array($admin_id, $subject, $notes), __FUNCTION__, __LINE__);
766 // Creates a new task for newly installed extension
767 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
768 // Not installed and do we have created a task for the admin?
769 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == '')) {
771 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
777 // Set default message if ext-foo is missing
778 $msg = sprintf(getMessage('ADMIN_EXT_TEXT_FILE_MISSING'), $ext);
780 // Load text for task if found
781 if (isFileReadable($tpl)) {
782 // Load extension's own text template (HTML!)
783 $msg = LOAD_TEMPLATE('ext_' . $ext, true);
785 // Write this in debug.log as well
786 DEBUG_LOG(__FUNCTION__, __LINE__, $msg);
789 // Task not created so it's a brand-new extension which we need to register and create a task for!
790 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
791 VALUES (%s, 0, 'NEW', 'EXTENSION', '%s', '%s', UNIX_TIMESTAMP())",
795 smartAddSlashes($msg),
796 ), __FUNCTION__, __LINE__, true, false, false
801 // Creates a task for automatically deactivated (deprecated) extension
802 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
803 // Create subject line
804 $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
806 // Not installed and do we have created a task for the admin?
807 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != '')) {
808 // Task not created so add it
809 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
810 VALUES (0, 0, 'NEW', 'EXTENSION_DEACTIVATION', '%s', '%s', UNIX_TIMESTAMP())",
813 SQL_ESCAPE(LOAD_TEMPLATE('task_ext_deactivated', true, $ext)),
814 ), __FUNCTION__, __LINE__, true, false
819 // Checks if the module has a menu
820 function MODULE_HAS_MENU ($mod, $forceDb = false) {
821 // All is false by default
824 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "mod={$mod},cache=".GET_EXT_VERSION('cache'));
825 if (GET_EXT_VERSION('cache') >= '0.1.2') {
826 // Cache version is okay, so let's check the cache!
827 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
828 // Check module cache and count hit
829 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
830 incrementConfigEntry('cache_hits');
831 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
832 // Check cache and count hit
833 $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == 'Y');
834 incrementConfigEntry('cache_hits');
835 } elseif ((IS_ADMIN()) && ($mod == 'admin')) {
836 // Admin module has always a menu!
839 } elseif ((GET_EXT_VERSION('sql_patches') >= '0.3.6') && ((!EXT_IS_ACTIVE('cache')) || ($forceDb === true))) {
840 // Check database for entry
841 $result = SQL_QUERY_ESC("SELECT has_menu FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE `module`='%s' LIMIT 1",
842 array($mod), __FUNCTION__, __LINE__);
845 if (SQL_NUMROWS($result) == 1) {
846 // Load "has_menu" column
847 list($has_menu) = SQL_FETCHROW($result);
850 $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
852 // Does it have a menu?
853 $ret = ($has_menu == 'Y');
857 SQL_FREERESULT($result);
858 } elseif (GET_EXT_VERSION('sql_patches') == '') {
859 // No sql_patches installed, so maybe in admin area or no admin registered?
860 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == 'admin')); // Then there is a menu!
864 //* DEBUG: */ print __FUNCTION__."[".__LINE__."]:currName=".EXT_GET_CURR_NAME().':'; var_dump($ret);
868 // Determines the task id for given extension
869 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
870 // Default is not found
873 // Search for extension task's id
874 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_task_system` WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
875 array($ext_name), __FUNCTION__, __LINE__);
878 if (SQL_NUMROWS($result) == 1) {
879 // Task found so load task's ID and register extension...
880 list($task_id) = SQL_FETCHROW($result);
884 SQL_FREERESULT($result);
890 // Determines the task id for given subject
891 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
892 // Default is not found
895 // Search for task id
896 $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject LIKE '%s%%' LIMIT 1",
897 array($subject), __FUNCTION__, __LINE__);
900 if (SQL_NUMROWS($result) == 1) {
901 // Task found so load task's ID and register extension...
902 list($task_id) = SQL_FETCHROW($result);
906 SQL_FREERESULT($result);
912 // Add updates notes for given version
913 function ADD_EXTENSION_NOTES ($ver) {
914 // Init notes/content
915 $out = ''; $content = array();
917 // Is do we have verbose output enabled?
918 if ((getConfig('verbose_sql') == 'Y') || (!EXT_IS_ACTIVE('sql_patches'))) {
919 // Update notes found?
920 if (EXT_GET_UPDATE_NOTES() != '') {
921 // Update notes found
924 'notes' => EXT_GET_UPDATE_NOTES()
928 EXT_SET_UPDATE_NOTES('');
929 } elseif (($ver == '0.0') || ($ver == '0.0.0')) {
933 'notes' => getMessage('INITIAL_RELEASE')
936 // No update notes found!
939 'notes' => getMessage('NO_UPDATE_NOTES')
944 $out = LOAD_TEMPLATE('admin_ext_notes', true, $content);
948 EXT_APPEND_NOTES($out);
951 // Getter for CSS files array
952 function EXT_GET_CSS_FILES () {
953 // By default no additional CSS files are found
956 // Is the array there?
957 if (isset($GLOBALS['css_files'])) {
959 $cssFiles = $GLOBALS['css_files'];
966 // Init CSS files array
967 function EXT_INIT_CSS_FILES () {
969 $GLOBALS['css_files'] = array();
973 function EXT_ADD_CSS_FILE ($file) {
974 // Is the array there?
975 if (!isset($GLOBALS['css_files'])) {
976 // Then auto-init them
977 EXT_INIT_CSS_FILES();
981 $GLOBALS['css_files'][] = $file;
984 // Setter for EXT_ALWAYS_ACTIVE flag
985 function EXT_SET_ALWAYS_ACTIVE ($active) {
986 $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()] = (string) $active;
989 // Getter for EXT_ALWAYS_ACTIVE flag
990 function EXT_GET_ALWAYS_ACTIVE () {
991 return $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()];
994 // Setter for EXT_VERSION flag
995 function EXT_SET_VERSION ($version) {
996 $GLOBALS['ext_version'][EXT_GET_CURR_NAME()] = (string) $version;
999 // Getter for EXT_VERSION flag
1000 function EXT_GET_VERSION () {
1001 return $GLOBALS['ext_version'][EXT_GET_CURR_NAME()];
1004 // Setter for EXT_DEPRECATED flag
1005 function EXT_SET_DEPRECATED ($deprecated) {
1006 $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()] = (string) $deprecated;
1009 // Getter for EXT_DEPRECATED flag
1010 function EXT_GET_DEPRECATED () {
1011 return $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()];
1014 // Setter for EXT_UPDATE_DEPENDS flag
1015 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
1016 // Is the update depency empty? (NEED TO BE FIXED!)
1017 if (empty($updateDepends)) {
1018 // Please report this bug!
1019 debug_report_bug("updateDepends is left empty!");
1022 // Is it not yet added?
1023 if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
1024 //* DEBUG */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME().'/'.$updateDepends);
1025 // Add it to the list of extension update depencies map
1026 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
1028 // Remember it in the list of running updates
1029 $GLOBALS['ext_running_updates'][] = $updateDepends;
1033 // Checks wether the given extension registration is in progress
1034 function EXT_IS_REGISTER_RUNNING ($ext_name) {
1035 return ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1038 // Init EXT_UPDATE_DEPENDS flag
1039 function EXT_INIT_UPDATE_DEPENDS () {
1040 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1042 // Init update depency map automatically if not found
1043 if (!EXT_IS_UPDATE_DEPENDS_INIT()) {
1044 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME()." - INIT!");
1045 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
1048 // Init running updates array
1049 EXT_INIT_RUNNING_UPDATES();
1052 // Adds an extension as "registration in progress"
1053 function EXT_ADD_RUNNING_REGISTRATION ($ext_name) {
1055 if (!EXT_IS_REGISTER_RUNNING($ext_name)) {
1057 $GLOBALS['ext_register_running'][] = $ext_name;
1061 // Checks wether EXT_UPDATE_DEPENDS is initialized
1062 function EXT_IS_UPDATE_DEPENDS_INIT () {
1063 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1064 return (isset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]));
1067 // Initializes the list of running updates
1068 function EXT_INIT_RUNNING_UPDATES () {
1069 // Auto-init ext_running_updates
1070 if (!isset($GLOBALS['ext_running_updates'])) {
1071 $GLOBALS['ext_running_updates'] = array();
1072 $GLOBALS['ext_register_running'] = array();
1076 // Getter for EXT_UPDATE_DEPENDS flag
1077 function EXT_GET_UPDATE_DEPENDS () {
1078 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1079 return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1082 // Getter for next iterator depency
1083 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1084 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1085 return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1088 // Counter for extension update depencies
1089 function EXT_COUNT_UPDATE_DEPENDS () {
1090 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1091 return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1094 // Removes given extension from update denpency list
1095 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1097 $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1100 if ($key !== false) {
1102 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1104 // And sort the array
1105 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1109 // Init iterator for update depencies
1110 function EXT_INIT_UPDATE_ITERATOR () {
1111 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1112 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1115 // Getter for depency iterator
1116 function EXT_GET_UPDATE_ITERATOR () {
1117 // Auto-init iterator
1118 if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1121 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME().'/'.$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]);
1122 return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1125 // Increments the update iterator
1126 function EXT_INCREMENT_UPDATE_INTERATOR () {
1127 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "currName=".EXT_GET_CURR_NAME());
1128 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1131 // Setter for EXT_REPORTS_FAILURE flag
1132 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1133 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1136 // Getter for EXT_REPORTS_FAILURE flag
1137 function EXT_GET_REPORTS_FAILURE () {
1138 return $GLOBALS['ext_reports_failure'];
1141 // Setter for EXT_VER_HISTORY flag
1142 function EXT_SET_VER_HISTORY ($verHistory) {
1143 $GLOBALS['ext_ver_history'] = (array) $verHistory;
1146 // Getter for EXT_VER_HISTORY array
1147 function EXT_GET_VER_HISTORY () {
1148 return $GLOBALS['ext_ver_history'];
1151 // Setter for EXT_UPDATE_NOTES flag
1152 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1153 $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1156 // Getter for EXT_UPDATE_NOTES flag
1157 function EXT_GET_UPDATE_NOTES () {
1158 return $GLOBALS['ext_update_notes'];
1161 // Init extension notice
1162 function EXT_INIT_NOTES () {
1163 $GLOBALS['ext_notes'] = '';
1166 // Append extension notice
1167 function EXT_APPEND_NOTES ($notes) {
1168 $GLOBALS['ext_notes'] .= (string) $notes;
1171 // Getter for extension notes
1172 function EXT_GET_NOTES () {
1173 return $GLOBALS['ext_notes'];
1176 // Setter for current extension name
1177 function EXT_SET_CURR_NAME ($ext_name) {
1178 $GLOBALS['curr_ext_name'] = (string) $ext_name;
1181 // Getter for current extension name
1182 function EXT_GET_CURR_NAME () {
1183 if (isset($GLOBALS['curr_ext_name'])) {
1184 return $GLOBALS['curr_ext_name'];
1188 debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1191 // Init SQLs array for current extension
1192 function INIT_EXT_SQLS () {
1193 // Auto-init the array now...
1194 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1195 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1199 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1200 function ADD_EXT_SQL ($sql) {
1201 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1204 // Getter for SQLs array for current extension
1205 function GET_EXT_SQLS () {
1206 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1207 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1208 // Not found, should not happen
1209 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1215 return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1218 // Removes SQLs for current extension
1219 function UNSET_EXT_SQLS () {
1220 unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1223 // Auto-initializes the removal list
1224 function EXT_INIT_REMOVAL_LIST () {
1225 // Is the remove list there?
1226 if (!isset($GLOBALS['ext_update_remove'])) {
1228 $GLOBALS['ext_update_remove'] = array();
1232 // Checks wether the current extension is on the removal list
1233 function EXT_IS_ON_REMOVAL_LIST () {
1234 // Init removal list
1235 EXT_INIT_REMOVAL_LIST();
1238 return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1241 // Adds the current extension to the removal list
1242 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1244 $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1247 // Getter for removal list
1248 function EXT_GET_REMOVAL_LIST () {
1249 // Return the removal list
1250 return $GLOBALS['ext_update_remove'];
1253 // Redirects if the provided extension is not installed
1254 function redirectOnUninstalledExtension ($ext_name) {
1255 // So is the extension there?
1256 if ((!isExtensionInstalled($ext_name)) || (!EXT_IS_ACTIVE($ext_name))) {
1257 // Redirect to index
1258 redirectToUrl('modules.php?module=index&msg=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1262 // Determines wether the given extension is installed
1263 function isExtensionInstalled ($ext_name) {
1264 // Default is not installed
1265 $isInstalled = false;
1267 // Check cache ext_version
1268 $isInstalled = isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name]);
1271 return $isInstalled;