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) {
50 // Set current extension name
51 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));
67 // Construct include filename and FQFN for extension file
68 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
69 $FQFN = constant('PATH') . $INC;
71 // Is the extension file NOT there?
72 if (!INCLUDE_READABLE($INC)) {
74 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s not found.", $ext_name));
80 // Construct FQFN for language file
81 $langInclude = sprintf("inc/language/%s_%s.php", $ext_name, GET_LANGUAGE());
83 // Is this include there?
84 if ((FILE_READABLE($langInclude)) && (!isset($GLOBALS['ext_loaded']['lang'][$ext_name]))) {
86 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Language loaded.");
87 $GLOBALS['ext_loaded']['lang'][$ext_name] = true;
88 LOAD_INC_ONCE($langInclude);
91 // Construct FQFN for functions file
92 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
94 // Is this include there?
95 if ((FILE_READABLE($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
97 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Functions loaded.");
98 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
99 LOAD_INC_ONCE($funcsInclude);
102 // Extensions are not deprecated by default
103 EXT_SET_DEPRECATED("N");
105 // Extensions are not always active by default
106 EXT_SET_ALWAYS_ACTIVE("N");
108 // Extension update notes
109 EXT_SET_UPDATE_NOTES("");
111 // Include the extension file
112 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Extension loaded.");
115 // Is this extension deprecated?
116 if (EXT_GET_DEPRECATED() == "Y") {
117 // Deactivate the extension
118 DEACTIVATE_EXTENSION($ext_name);
124 // Mark it as loaded in normal mode
125 if (empty($EXT_LOAD_MODE)) {
127 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
134 // Registeres an extension and possible update depencies
135 function REGISTER_EXTENSION ($ext_name, $task_id, $dry_run = false, $logout = true) {
136 // This shall never do a non-admin user!
137 if (!IS_ADMIN()) return false;
139 // Is this extension already installed?
140 if (EXT_IS_ACTIVE($ext_name)) return false;
142 // Set current extension name
143 EXT_SET_CURR_NAME($ext_name);
145 // Init EXT_UPDATE_DEPENDS
146 EXT_INIT_UPDATE_DEPENDS();
148 // Is the task id zero? Then we need to auto-fix it here
150 // Try to find the task
151 $task_id = DETERMINE_EXTENSION_TASK_ID(EXT_GET_CURR_NAME());
153 // Still zero and not in dry-run?
154 if (($task_id == 0) && (!$dry_run)) {
155 // Then request a bug report
156 debug_report_bug(sprintf("%s: task_id is still zero after DETERMINE_EXTENSION_TASK_ID(%s)",
163 // Init queries and notes
172 // By default we have no failures
173 EXT_SET_REPORTS_FAILURE(false);
175 // Does this extension exists?
176 //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
177 if (LOAD_EXTENSION(EXT_GET_CURR_NAME(), "register", "", $dry_run)) {
178 // And run possible updates
179 //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
180 $history = EXT_GET_VER_HISTORY();
181 foreach ($history as $ver) {
182 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name={EXT_GET_CURR_NAME()}, ext_ver={$ver}");
183 // Load extension in update mode
184 LOAD_EXTENSION(EXT_GET_CURR_NAME(), "update", $ver, $dry_run);
186 // Add update notes to our output
187 ADD_EXTENSION_NOTES($ver);
190 // Does this extension depends on an outstanding update of another update?
191 for ($dmy = EXT_GET_UPDATE_ITERATOR(); EXT_GET_UPDATE_ITERATOR() < EXT_COUNT_UPDATE_DEPENDS();) {
193 $ext_update = EXT_GET_ITERATOR_UPDATE_DEPENDS();
195 // Increment here to avoid endless loop
196 EXT_INCREMENT_UPDATE_INTERATOR();
198 // Check for required file
199 if (LOAD_EXTENSION($ext_update, "register", "", $dry_run)) {
200 // If versions mismatch update extension first
201 $ext_ver = GET_EXT_VERSION($ext_update);
203 // Extension version set? If empty the extension is not registered
204 if (empty($ext_ver)) {
205 // Extension not registered so far so first load task's ID...
206 $task = DETERMINE_EXTENSION_TASK_ID($ext_update);
210 // Try to register the extension
211 //* DEBUG: */ print __LINE__.sprintf(":ext_update=%s,task=%s<br />\n", $ext_update, $task);
212 $test = REGISTER_EXTENSION($ext_update, $task, $dry_run, false);
213 //* DEBUG: */ var_dump($test);
215 } elseif ($ext_ver != EXT_GET_VERSION()) {
216 // Ok, update this extension now
217 EXTENSION_UPDATE($ext_update, $ext_ver, $dry_run);
222 // Nothing to register / update before...
226 // Required file for update does not exists!
228 // But this is fine for the first time...
231 // Restore the current extension name
232 EXT_SET_CURR_NAME($ext_name);
235 // Is there no update?
236 if (EXT_COUNT_UPDATE_DEPENDS(EXT_GET_CURR_NAME()) == 0) {
237 // Then test is passed!
241 // Switch back to register mode
242 $EXT_LOAD_MODE = "register";
244 // Remains true if extension registration reports no failures
245 //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME();
246 //* DEBUG: */ var_dump($test);
247 $test = (($test === true) && (EXT_GET_REPORTS_FAILURE() === false));
248 //* DEBUG: */ var_dump($test);
250 // Does everthing before wents ok?
251 if ($test === true) {
252 // "Dry-run-mode" activated?
253 if ((!$dry_run) && (!EXT_IS_ON_REMOVAL_LIST())) {
254 //* DEBUG: */ print __LINE__.": ext_name=".EXT_GET_CURR_NAME()."<br />\n";
255 // Init SQLs and transfer ext->generic
257 SET_SQLS(GET_EXT_SQLS());
259 // Run installation pre-installation filters
260 runFilterChain('pre_extension_installed', array('dry_run' => $dry_run));
262 // Register extension
263 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_extensions` (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
264 array(EXT_GET_CURR_NAME(), EXT_GET_ALWAYS_ACTIVE(), EXT_GET_VERSION()), __FUNCTION__, __LINE__);
266 // Remove cache file(s) if extension is active
267 runFilterChain('post_extension_installed', array('ext_name' => EXT_GET_CURR_NAME(), 'task_id' => $task_id));
269 // Remove all SQL commands
272 // In normal mode return a true on success
274 } elseif ($dry_run) {
275 // Init SQLs and transfer ext->generic
277 SET_SQLS(GET_EXT_SQLS());
279 // Rewrite SQL command to keep { and } inside for dry-run
280 foreach (GET_SQLS() as $key => $sql) {
281 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
282 SET_SQL_KEY($key, $sql);
285 // In "dry-run" mode return array with all SQL commands
288 // Remove all SQL commands
291 // Extension has been removed for updates, so all is fine!
295 // No, an error occurs while registering extension :-(
296 //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
299 } elseif (($task_id > 0) && (EXT_GET_CURR_NAME() != "")) {
300 // Remove task from system when id and extension's name is valid
301 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND `status`='NEW' LIMIT 1",
302 array(bigintval($task_id)), __FUNCTION__, __LINE__);
305 // Is this the sql_patches?
306 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":{EXT_GET_CURR_NAME()}/{$EXT_LOAD_MODE}");
307 if ((EXT_GET_CURR_NAME() == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
308 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
309 if ($logout === true) {
310 // Then redirect to logout
311 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
313 // Add temporary filter
314 REGISTER_FILTER('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
315 $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
319 // Return status code
323 // Run SQL queries for given extension id
324 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
325 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
326 // This shall never do a non-admin user!
327 if (!IS_ADMIN()) return false;
329 // Get extension's name
330 $ext_name = GET_EXT_NAME($ext_id);
332 // If it is not set then maybe there is no extension for that ID number
333 if ($ext_name == "") return false;
335 // Set current SQL name
336 EXT_SET_CURR_NAME($ext_name);
338 // Init EXT_UPDATE_DEPENDS
339 EXT_INIT_UPDATE_DEPENDS();
344 // By default no SQL has been executed
347 // Load extension in detected mode
348 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]={EXT_GET_CURR_NAME()}");
349 LOAD_EXTENSION(EXT_GET_CURR_NAME(), $load_mode, "", false);
353 SET_SQLS(GET_EXT_SQLS());
355 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".COUNT_SQLS()."");
356 if ((IS_SQLS_VALID() && (COUNT_SQLS() > 0))) {
357 // Run SQL commands...
358 runFilterChain('run_sqls');
361 if ($load_mode == "remove") {
362 // Delete this extension (remember to remove it from your server *before* you click on welcome!
363 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
364 array(EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
368 // Remove cache file(s) if extension is active
369 if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == "activate") || ($load_mode == "deactivate"))) {
371 runFilterChain('post_extension_run_sql', EXT_GET_CURR_NAME());
374 // Is this the sql_patches?
375 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": {$ext_id}/{EXT_GET_CURR_NAME()}/{$load_mode}");
376 if ((EXT_GET_CURR_NAME() == "sql_patches") && (($load_mode == "register") || ($load_mode == "remove"))) {
377 // Then redirect to logout
378 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
379 LOAD_URL("modules.php?module=admin&logout=1&".$load_mode."=sql_patches");
383 // Check if given extension is active
384 function EXT_IS_ACTIVE ($ext_name) {
385 // Extensions are all inactive during installation
386 if ((!isInstalled()) || (isInstalling()) || (empty($ext_name))) return false;
388 // Not active is the default
392 if (isset($GLOBALS['cache_array']['extensions']['ext_active'][$ext_name])) {
394 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
395 $active = $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name];
398 incrementConfigEntry('cache_hits');
399 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
400 // @TODO Extension is loaded, what next?
401 app_die(__FUNCTION__, __LINE__, "LOADED:$ext_name");
402 } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
403 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
404 // Load from database
405 $result = SQL_QUERY_ESC("SELECT ext_active FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
406 array($ext_name), __FUNCTION__, __LINE__);
409 if (SQL_NUMROWS($result) == 1) {
411 list($active) = SQL_FETCHROW($result);
415 SQL_FREERESULT($result);
418 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
419 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = $active;
421 // Extension not active!
422 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
423 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = "N";
427 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
429 // Is this extension activated? (For admins we always have active extensions...)
430 return ($active == "Y");
432 // Get version from extensions
433 function GET_EXT_VERSION ($ext_name) {
434 // By default no extension is found
437 // Empty extension name should be fixed!
438 if (empty($ext_name)) {
439 // Please report this bug!
440 debug_report_bug(__FUNCTION__.": ext_name is empty which is not allowed here.");
443 // Extensions are all inactive during installation
444 if ((!isInstalled()) || (isInstalling())) return "";
445 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
447 // Is the cache written?
448 if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
449 // Load data from cache
450 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
451 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
454 incrementConfigEntry('cache_hits');
455 } elseif (!isCacheInstanceValid()) {
456 // Load from database
457 $result = SQL_QUERY_ESC("SELECT ext_version FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
458 array($ext_name), __FUNCTION__, __LINE__);
459 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
461 // Is the extension there?
462 if (SQL_NUMROWS($result) == 1) {
464 list($ext_ver) = SQL_FETCHROW($result);
468 SQL_FREERESULT($result);
471 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
475 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
479 // Updates a given extension with current extension version to latest version
480 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
481 // Only admins are allowed to update extensions
482 if ((!IS_ADMIN()) || (empty($ext_name))) return false;
484 // Set current SQL name
485 EXT_SET_CURR_NAME($ext_name);
492 // Load extension in test mode
493 LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run);
495 // Save version history
496 $history = EXT_GET_VER_HISTORY();
498 // Remove old SQLs array to prevent possible bugs
501 // Check if version is updated
502 if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
503 // Search for starting point
504 $start = array_search($ext_ver, $history);
506 // And load SQL queries in order of version history
507 for ($idx = ($start + 1); $idx < count($history); $idx++) {
508 // Set extension version
509 $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()] = $history[$idx];
512 LOAD_EXTENSION(EXT_GET_CURR_NAME(), "update", $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], $dry_run);
514 if (EXT_GET_UPDATE_DEPENDS() != "") {
515 // Is the extension there?
516 if (GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()) != "") {
517 // Update another extension first!
518 $test = EXTENSION_UPDATE(EXT_GET_UPDATE_DEPENDS(), GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()), $dry_run);
520 // Register new extension
521 $test = REGISTER_EXTENSION(EXT_GET_UPDATE_DEPENDS(), 0, $dry_run, false);
526 ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
529 // In real-mode execute any existing includes
531 $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
532 runFilterChain('load_includes');
533 SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
534 unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
539 SET_SQLS(GET_EXT_SQLS());
542 runFilterChain('run_sqls', array('dry_run' => $dry_run));
546 CREATE_EXTENSION_UPDATE_TASK(GET_CURRENT_ADMIN_ID(), EXT_GET_CURR_NAME(), $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], SQL_ESCAPE(EXT_GET_NOTES(EXT_GET_NOTES())));
548 // Update extension's version
549 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
550 array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FUNCTION__, __LINE__);
554 unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
556 // Run filters on success extension update
557 runFilterChain('extension_update', EXT_GET_CURR_NAME());
562 // Output verbose SQL table for extension
563 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = "", $dashed = "", $switch = false, $width = "100%") {
566 // Then fix it to default
567 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
570 // Are there some queries in $queries?
571 if (count($queries) > 0) {
572 // Then use them instead!
580 // Do we have queries?
581 if ((IS_SQLS_VALID()) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
582 foreach (GET_SQLS() as $idx => $sql) {
586 // Output command if set
588 // Prepare output for template
596 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
598 // Switch color and count up
604 // Prepare content for template
612 // Load main template
613 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
614 } elseif ((GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
615 // No addional SQL commands to run
616 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
623 // Get extension name from id
624 function GET_EXT_NAME ($ext_id) {
625 // Init extension name
629 if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
631 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
634 incrementConfigEntry('cache_hits');
635 } elseif (!EXT_IS_ACTIVE("cache")) {
636 // Load from database
637 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE id=%s LIMIT 1",
638 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
639 list($ret) = SQL_FETCHROW($result);
640 SQL_FREERESULT($result);
645 // Get extension id from name
646 function GET_EXT_ID ($ext_name) {
649 if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
651 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
654 incrementConfigEntry('cache_hits');
655 } elseif (!EXT_IS_ACTIVE("cache")) {
656 // Load from database
657 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
658 array($ext_name), __FUNCTION__, __LINE__);
659 list($ret) = SQL_FETCHROW($result);
660 SQL_FREERESULT($result);
667 // Activate given extension
668 function ACTIVATE_EXTENSION ($ext_name) {
669 // Activate the extension
670 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
671 array($ext_name), __FUNCTION__, __LINE__);
673 // Extension has been activated?
674 if (SQL_AFFECTEDROWS() == 1) {
675 // Then run all queries
676 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
680 // Deactivate given extension
681 function DEACTIVATE_EXTENSION($ext_name) {
682 // Activate the extension
683 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
684 array($ext_name), __FUNCTION__, __LINE__);
686 // Extension has been activated?
687 if (SQL_AFFECTEDROWS() == 1) {
688 // Then run all queries
689 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
692 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
695 SEND_ADMIN_NOTIFICATION(
696 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
697 "admin_ext_deactivated",
698 array('ext_name' => $ext_name)
703 // Checks wether the extension is older than given
704 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
705 // Get current extension version
706 $currVersion = GET_EXT_VERSION($ext_name);
708 // Remove all dots from both versions
709 $currVersion = str_replace(".", "", $currVersion);
710 $ext_ver = str_replace(".", "", $ext_ver);
712 // Now compare both and return the result
713 return ($currVersion < $ext_ver);
716 // Creates a new task for updated extension
717 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
718 // Create subject line
719 $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] {--ADMIN_UPDATE_EXT_SUBJ--}";
721 // Is the extension there?
722 if (GET_EXT_VERSION($ext_name) != "") {
723 // Check if task is not there
724 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
725 // Task not created so it's a brand-new extension which we need to register and create a task for!
726 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())",
727 array($admin_id, $subject, $notes), __FUNCTION__, __LINE__);
732 // Creates a new task for newly installed extension
733 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
734 // Not installed and do we have created a task for the admin?
735 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
737 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
743 // Set default message if ext-foo is missing
744 $msg = sprintf(getMessage('ADMIN_EXT_TEXT_FILE_MISSING'), $ext);
746 // Load text for task if found
747 if (FILE_READABLE($tpl)) {
748 // Load extension's own text template (HTML!)
749 $msg = LOAD_TEMPLATE("ext_".$ext, true);
751 // Write this in debug.log as well
752 DEBUG_LOG(__FUNCTION__, __LINE__, $msg);
755 // Task not created so it's a brand-new extension which we need to register and create a task for!
756 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
757 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
762 ), __FUNCTION__, __LINE__, true, false
767 // Creates a task for automatically deactivated (deprecated) extension
768 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
769 // Create subject line
770 $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
772 // Not installed and do we have created a task for the admin?
773 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
774 // Task not created so add it
775 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
776 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
779 SQL_ESCAPE(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
780 ), __FUNCTION__, __LINE__, true, false
785 // Checks if the module has a menu
786 function MODULE_HAS_MENU ($mod, $forceDb = false) {
787 // All is false by default
790 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
791 if (GET_EXT_VERSION("cache") >= "0.1.2") {
792 // Cache version is okay, so let's check the cache!
793 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
794 // Check module cache and count hit
795 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == "Y");
796 incrementConfigEntry('cache_hits');
797 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
798 // Check cache and count hit
799 $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == "Y");
800 incrementConfigEntry('cache_hits');
801 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
802 // Admin module has always a menu!
805 } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
806 // Check database for entry
807 $result = SQL_QUERY_ESC("SELECT has_menu FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE `module`='%s' LIMIT 1",
808 array($mod), __FUNCTION__, __LINE__);
811 if (SQL_NUMROWS($result) == 1) {
812 // Load "has_menu" column
813 list($has_menu) = SQL_FETCHROW($result);
816 $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
818 // Does it have a menu?
819 $ret = ($has_menu == "Y");
823 SQL_FREERESULT($result);
824 } elseif (GET_EXT_VERSION("sql_patches") == "") {
825 // No sql_patches installed, so maybe in admin area or no admin registered?
826 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == "admin")); // Then there is a menu!
830 //* DEBUG: */ var_dump($ret);
834 // Determines the task id for given extension
835 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
836 // Default is not found
839 // Search for extension task's id
840 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
841 array($ext_name), __FUNCTION__, __LINE__);
844 if (SQL_NUMROWS($result) == 1) {
845 // Task found so load task's ID and register extension...
846 list($task_id) = SQL_FETCHROW($result);
850 SQL_FREERESULT($result);
856 // Determines the task id for given subject
857 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
858 // Default is not found
861 // Search for task id
862 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject LIKE '%s%%' LIMIT 1",
863 array($subject), __FUNCTION__, __LINE__);
866 if (SQL_NUMROWS($result) == 1) {
867 // Task found so load task's ID and register extension...
868 list($task_id) = SQL_FETCHROW($result);
872 SQL_FREERESULT($result);
878 // Add updates notes for given version
879 function ADD_EXTENSION_NOTES ($ver) {
880 // Init notes/content
881 $out = ""; $content = array();
883 // Is do we have verbose output enabled?
884 if ((getConfig('verbose_sql') == "Y") || (!EXT_IS_ACTIVE("sql_patches"))) {
886 // Update notes found?
887 if (EXT_GET_UPDATE_NOTES() != "") {
888 // Update notes found
891 'notes' => EXT_GET_UPDATE_NOTES()
895 EXT_SET_UPDATE_NOTES("");
896 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
900 'notes' => getMessage('INITIAL_RELEASE')
903 // No update notes found!
906 'notes' => getMessage('NO_UPDATE_NOTES')
911 $out = LOAD_TEMPLATE("admin_ext_notes", true, $content);
915 EXT_APPEND_NOTES($out);
918 // Getter for CSS files array
919 function EXT_GET_CSS_FILES () {
920 // By default no additional CSS files are found
923 // Is the array there?
924 if (isset($GLOBALS['css_files'])) {
926 $cssFiles = $GLOBALS['css_files'];
933 // Init CSS files array
934 function EXT_INIT_CSS_FILES () {
936 $GLOBALS['css_files'] = array();
940 function EXT_ADD_CSS_FILE ($file) {
941 // Is the array there?
942 if (!isset($GLOBALS['css_files'])) {
943 // Then auto-init them
944 EXT_INIT_CSS_FILES();
948 $GLOBALS['css_files'][] = $file;
951 // Setter for EXT_ALWAYS_ACTIVE flag
952 function EXT_SET_ALWAYS_ACTIVE ($active) {
953 $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()] = (string) $active;
956 // Getter for EXT_ALWAYS_ACTIVE flag
957 function EXT_GET_ALWAYS_ACTIVE () {
958 return $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()];
961 // Setter for EXT_VERSION flag
962 function EXT_SET_VERSION ($version) {
963 $GLOBALS['ext_version'][EXT_GET_CURR_NAME()] = (float) $version;
966 // Getter for EXT_VERSION flag
967 function EXT_GET_VERSION () {
968 return $GLOBALS['ext_version'][EXT_GET_CURR_NAME()];
971 // Setter for EXT_DEPRECATED flag
972 function EXT_SET_DEPRECATED ($deprecated) {
973 $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()] = (string) $deprecated;
976 // Getter for EXT_DEPRECATED flag
977 function EXT_GET_DEPRECATED () {
978 return $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()];
981 // Setter for EXT_UPDATE_DEPENDS flag
982 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
983 // Is the update depency empty? (NEED TO BE FIXED!)
984 if (empty($updateDepends)) {
985 // Please report this bug!
986 debug_report_bug("updateDepends is left empty!");
989 // Is it not yet added?
990 if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
991 //* DEBUG */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$updateDepends."<br />\n";
992 // Add it to the list of extension update depencies map
993 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
995 // Remember it in the list of running updates
996 $GLOBALS['ext_running_updates'][] = $updateDepends;
1000 // Init EXT_UPDATE_DEPENDS flag
1001 function EXT_INIT_UPDATE_DEPENDS () {
1002 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1004 // Init update depency map
1005 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
1007 // Init running updates array
1008 EXT_INIT_RUNNING_UPDATES();
1011 // Initializes the list of running updates
1012 function EXT_INIT_RUNNING_UPDATES () {
1013 // Auto-init ext_running_updates
1014 if (!isset($GLOBALS['ext_running_updates'])) {
1015 $GLOBALS['ext_running_updates'] = array();
1019 // Getter for EXT_UPDATE_DEPENDS flag
1020 function EXT_GET_UPDATE_DEPENDS () {
1021 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1022 return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1025 // Getter for next iterator depency
1026 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1027 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1028 return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1031 // Counter for extension update depencies
1032 function EXT_COUNT_UPDATE_DEPENDS () {
1033 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1034 return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1037 // Removes given extension from update denpency list
1038 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1040 $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1043 if ($key !== false) {
1045 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1047 // And sort the array
1048 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1052 // Init iterator for update depencies
1053 function EXT_INIT_UPDATE_ITERATOR () {
1054 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1055 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1058 // Getter for depency iterator
1059 function EXT_GET_UPDATE_ITERATOR () {
1060 // Auto-init iterator
1061 if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1064 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]."<br />\n";
1065 return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1068 // Increments the update iterator
1069 function EXT_INCREMENT_UPDATE_INTERATOR () {
1070 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1071 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1074 // Setter for EXT_REPORTS_FAILURE flag
1075 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1076 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1079 // Getter for EXT_REPORTS_FAILURE flag
1080 function EXT_GET_REPORTS_FAILURE () {
1081 return $GLOBALS['ext_reports_failure'];
1084 // Setter for EXT_VER_HISTORY flag
1085 function EXT_SET_VER_HISTORY ($verHistory) {
1086 $GLOBALS['ext_ver_history'] = (array) $verHistory;
1089 // Getter for EXT_VER_HISTORY array
1090 function EXT_GET_VER_HISTORY () {
1091 return $GLOBALS['ext_ver_history'];
1094 // Setter for EXT_UPDATE_NOTES flag
1095 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1096 $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1099 // Getter for EXT_UPDATE_NOTES flag
1100 function EXT_GET_UPDATE_NOTES () {
1101 return $GLOBALS['ext_update_notes'];
1104 // Init extension notice
1105 function EXT_INIT_NOTES () {
1106 $GLOBALS['ext_notes'] = "";
1109 // Append extension notice
1110 function EXT_APPEND_NOTES ($notes) {
1111 $GLOBALS['ext_notes'] .= (string) $notes;
1114 // Getter for extension notes
1115 function EXT_GET_NOTES () {
1116 return $GLOBALS['ext_notes'];
1119 // Setter for current extension name
1120 function EXT_SET_CURR_NAME ($ext_name) {
1121 $GLOBALS['curr_ext_name'] = (string) $ext_name;
1124 // Getter for current extension name
1125 function EXT_GET_CURR_NAME () {
1126 if (isset($GLOBALS['curr_ext_name'])) {
1127 return $GLOBALS['curr_ext_name'];
1131 debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1134 // Init SQLs array for current extension
1135 function INIT_EXT_SQLS () {
1136 // Auto-init the array now...
1137 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1138 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1142 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1143 function ADD_EXT_SQL ($sql) {
1144 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1147 // Getter for SQLs array for current extension
1148 function GET_EXT_SQLS () {
1149 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1150 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1151 // Not found, should not happen
1152 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1158 return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1161 // Removes SQLs for current extension
1162 function UNSET_EXT_SQLS () {
1163 unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1166 // Auto-initializes the removal list
1167 function EXT_INIT_REMOVAL_LIST () {
1168 // Is the remove list there?
1169 if (!isset($GLOBALS['ext_update_remove'])) {
1171 $GLOBALS['ext_update_remove'] = array();
1175 // Checks wether the current extension is on the removal list
1176 function EXT_IS_ON_REMOVAL_LIST () {
1177 // Init removal list
1178 EXT_INIT_REMOVAL_LIST();
1181 return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1184 // Adds the current extension to the removal list
1185 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1187 $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1190 // Getter for removal list
1191 function EXT_GET_REMOVAL_LIST () {
1192 // Return the removal list
1193 return $GLOBALS['ext_update_remove'];