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()), __FILE__, __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)), __FILE__, __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()), __FILE__, __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 mxchange_die("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), __FILE__, __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 // Extensions are all inactive during installation
438 if ((!isInstalled()) || (isInstalling())) return "";
439 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
441 // Is the cache written?
442 if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
443 // Load data from cache
444 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
445 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
448 incrementConfigEntry('cache_hits');
449 } elseif (!isCacheInstanceValid()) {
450 // Load from database
451 $result = SQL_QUERY_ESC("SELECT ext_version FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
452 array($ext_name), __FILE__, __LINE__);
453 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
455 // Is the extension there?
456 if (SQL_NUMROWS($result) == 1) {
458 list($ext_ver) = SQL_FETCHROW($result);
462 SQL_FREERESULT($result);
465 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
469 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
473 // Updates a given extension with current extension version to latest version
474 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
475 // Only admins are allowed to update extensions
476 if ((!IS_ADMIN()) || (empty($ext_name))) return false;
478 // Set current SQL name
479 EXT_SET_CURR_NAME($ext_name);
486 // Load extension in test mode
487 LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run);
489 // Save version history
490 $history = EXT_GET_VER_HISTORY();
492 // Remove old SQLs array to prevent possible bugs
495 // Check if version is updated
496 if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
497 // Search for starting point
498 $start = array_search($ext_ver, $history);
500 // And load SQL queries in order of version history
501 for ($idx = ($start + 1); $idx < count($history); $idx++) {
502 // Set extension version
503 $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()] = $history[$idx];
506 LOAD_EXTENSION(EXT_GET_CURR_NAME(), "update", $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], $dry_run);
508 if (EXT_GET_UPDATE_DEPENDS() != "") {
509 // Is the extension there?
510 if (GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()) != "") {
511 // Update another extension first!
512 $test = EXTENSION_UPDATE(EXT_GET_UPDATE_DEPENDS(), GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()), $dry_run);
514 // Register new extension
515 $test = REGISTER_EXTENSION(EXT_GET_UPDATE_DEPENDS(), 0, $dry_run, false);
520 ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
523 // In real-mode execute any existing includes
525 $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
526 runFilterChain('load_includes');
527 SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
528 unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
533 SET_SQLS(GET_EXT_SQLS());
536 runFilterChain('run_sqls', array('dry_run' => $dry_run));
540 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())));
542 // Update extension's version
543 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
544 array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FILE__, __LINE__);
548 unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
550 // Run filters on success extension update
551 runFilterChain('extension_update', EXT_GET_CURR_NAME());
556 // Output verbose SQL table for extension
557 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = "", $dashed = "", $switch = false, $width = "100%") {
560 // Then fix it to default
561 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
564 // Are there some queries in $queries?
565 if (count($queries) > 0) {
566 // Then use them instead!
574 // Do we have queries?
575 if ((IS_SQLS_VALID()) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
576 foreach (GET_SQLS() as $idx => $sql) {
580 // Output command if set
582 // Prepare output for template
590 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
592 // Switch color and count up
598 // Prepare content for template
606 // Load main template
607 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
608 } elseif ((GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
609 // No addional SQL commands to run
610 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
617 // Get extension name from id
618 function GET_EXT_NAME ($ext_id) {
619 // Init extension name
623 if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
625 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
628 incrementConfigEntry('cache_hits');
629 } elseif (!EXT_IS_ACTIVE("cache")) {
630 // Load from database
631 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE id=%s LIMIT 1",
632 array(bigintval($ext_id)), __FILE__, __LINE__);
633 list($ret) = SQL_FETCHROW($result);
634 SQL_FREERESULT($result);
639 // Get extension id from name
640 function GET_EXT_ID ($ext_name) {
643 if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
645 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
648 incrementConfigEntry('cache_hits');
649 } elseif (!EXT_IS_ACTIVE("cache")) {
650 // Load from database
651 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
652 array($ext_name), __FILE__, __LINE__);
653 list($ret) = SQL_FETCHROW($result);
654 SQL_FREERESULT($result);
661 // Activate given extension
662 function ACTIVATE_EXTENSION ($ext_name) {
663 // Activate the extension
664 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
665 array($ext_name), __FILE__, __LINE__);
667 // Extension has been activated?
668 if (SQL_AFFECTEDROWS() == 1) {
669 // Then run all queries
670 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
674 // Deactivate given extension
675 function DEACTIVATE_EXTENSION($ext_name) {
676 // Activate the extension
677 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
678 array($ext_name), __FILE__, __LINE__);
680 // Extension has been activated?
681 if (SQL_AFFECTEDROWS() == 1) {
682 // Then run all queries
683 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
686 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
689 SEND_ADMIN_NOTIFICATION(
690 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
691 "admin_ext_deactivated",
692 array('ext_name' => $ext_name)
697 // Checks wether the extension is older than given
698 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
699 // Get current extension version
700 $currVersion = GET_EXT_VERSION($ext_name);
702 // Remove all dots from both versions
703 $currVersion = str_replace(".", "", $currVersion);
704 $ext_ver = str_replace(".", "", $ext_ver);
706 // Now compare both and return the result
707 return ($currVersion < $ext_ver);
710 // Creates a new task for updated extension
711 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
712 // Create subject line
713 $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] {--ADMIN_UPDATE_EXT_SUBJ--}";
715 // Is the extension there?
716 if (GET_EXT_VERSION($ext_name) != "") {
717 // Check if task is not there
718 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
719 // Task not created so it's a brand-new extension which we need to register and create a task for!
720 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())",
721 array($admin_id, $subject, $notes), __FILE__, __LINE__);
726 // Creates a new task for newly installed extension
727 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
728 // Not installed and do we have created a task for the admin?
729 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
731 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
737 // Load text for task
738 if (FILE_READABLE($tpl)) {
739 // Load extension's own text template (HTML!)
740 $msg = LOAD_TEMPLATE("ext_".$ext, true);
742 // Load default message
743 $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
746 // Task not created so it's a brand-new extension which we need to register and create a task for!
747 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
748 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
753 ), __FILE__, __LINE__, true, false
758 // Creates a task for automatically deactivated (deprecated) extension
759 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
760 // Create subject line
761 $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
763 // Not installed and do we have created a task for the admin?
764 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
765 // Task not created so add it
766 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
767 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
770 SQL_ESCAPE(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
771 ), __FILE__, __LINE__, true, false
776 // Checks if the module has a menu
777 function MODULE_HAS_MENU ($mod, $forceDb = false) {
778 // All is false by default
781 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
782 if (GET_EXT_VERSION("cache") >= "0.1.2") {
783 // Cache version is okay, so let's check the cache!
784 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
785 // Check module cache and count hit
786 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == "Y");
787 incrementConfigEntry('cache_hits');
788 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
789 // Check cache and count hit
790 $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == "Y");
791 incrementConfigEntry('cache_hits');
792 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
793 // Admin module has always a menu!
796 } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
797 // Check database for entry
798 $result = SQL_QUERY_ESC("SELECT has_menu FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE module='%s' LIMIT 1",
799 array($mod), __FILE__, __LINE__);
802 if (SQL_NUMROWS($result) == 1) {
803 // Load "has_menu" column
804 list($has_menu) = SQL_FETCHROW($result);
807 $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
809 // Does it have a menu?
810 $ret = ($has_menu == "Y");
814 SQL_FREERESULT($result);
815 } elseif (GET_EXT_VERSION("sql_patches") == "") {
816 // No sql_patches installed, so maybe in admin area or no admin registered?
817 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == "admin")); // Then there is a menu!
821 //* DEBUG: */ var_dump($ret);
825 // Determines the task id for given extension
826 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
827 // Default is not found
830 // Search for extension task's id
831 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
832 array($ext_name), __FILE__, __LINE__);
835 if (SQL_NUMROWS($result) == 1) {
836 // Task found so load task's ID and register extension...
837 list($task_id) = SQL_FETCHROW($result);
841 SQL_FREERESULT($result);
847 // Determines the task id for given subject
848 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
849 // Default is not found
852 // Search for task id
853 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject LIKE '%s%%' LIMIT 1",
854 array($subject), __FILE__, __LINE__);
857 if (SQL_NUMROWS($result) == 1) {
858 // Task found so load task's ID and register extension...
859 list($task_id) = SQL_FETCHROW($result);
863 SQL_FREERESULT($result);
869 // Add updates notes for given version
870 function ADD_EXTENSION_NOTES ($ver) {
871 // Init notes/content
872 $out = ""; $content = array();
874 // Is do we have verbose output enabled?
875 if ((getConfig('verbose_sql') == "Y") || (!EXT_IS_ACTIVE("sql_patches"))) {
877 // Update notes found?
878 if (EXT_GET_UPDATE_NOTES() != "") {
879 // Update notes found
882 'notes' => EXT_GET_UPDATE_NOTES()
886 EXT_SET_UPDATE_NOTES("");
887 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
891 'notes' => getMessage('INITIAL_RELEASE')
894 // No update notes found!
897 'notes' => getMessage('NO_UPDATE_NOTES')
902 $out = LOAD_TEMPLATE("admin_ext_notes", true, $content);
906 EXT_APPEND_NOTES($out);
909 // Getter for CSS files array
910 function EXT_GET_CSS_FILES () {
911 // By default no additional CSS files are found
914 // Is the array there?
915 if (isset($GLOBALS['css_files'])) {
917 $cssFiles = $GLOBALS['css_files'];
924 // Init CSS files array
925 function EXT_INIT_CSS_FILES () {
927 $GLOBALS['css_files'] = array();
931 function EXT_ADD_CSS_FILE ($file) {
932 // Is the array there?
933 if (!isset($GLOBALS['css_files'])) {
934 // Then auto-init them
935 EXT_INIT_CSS_FILES();
939 $GLOBALS['css_files'][] = $file;
942 // Setter for EXT_ALWAYS_ACTIVE flag
943 function EXT_SET_ALWAYS_ACTIVE ($active) {
944 $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()] = (string) $active;
947 // Getter for EXT_ALWAYS_ACTIVE flag
948 function EXT_GET_ALWAYS_ACTIVE () {
949 return $GLOBALS['ext_always_active'][EXT_GET_CURR_NAME()];
952 // Setter for EXT_VERSION flag
953 function EXT_SET_VERSION ($version) {
954 $GLOBALS['ext_version'][EXT_GET_CURR_NAME()] = (float) $version;
957 // Getter for EXT_VERSION flag
958 function EXT_GET_VERSION () {
959 return $GLOBALS['ext_version'][EXT_GET_CURR_NAME()];
962 // Setter for EXT_DEPRECATED flag
963 function EXT_SET_DEPRECATED ($deprecated) {
964 $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()] = (string) $deprecated;
967 // Getter for EXT_DEPRECATED flag
968 function EXT_GET_DEPRECATED () {
969 return $GLOBALS['ext_deprecated'][EXT_GET_CURR_NAME()];
972 // Setter for EXT_UPDATE_DEPENDS flag
973 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
974 // Is the update depency empty? (NEED TO BE FIXED!)
975 if (empty($updateDepends)) {
976 // Please report this bug!
977 debug_report_bug("updateDepends is left empty!");
980 // Is it not yet added?
981 if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
982 //* DEBUG */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$updateDepends."<br />\n";
983 // Add it to the list of extension update depencies map
984 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
986 // Remember it in the list of running updates
987 $GLOBALS['ext_running_updates'][] = $updateDepends;
991 // Init EXT_UPDATE_DEPENDS flag
992 function EXT_INIT_UPDATE_DEPENDS () {
993 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
995 // Init update depency map
996 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
998 // Init running updates array
999 EXT_INIT_RUNNING_UPDATES();
1002 // Initializes the list of running updates
1003 function EXT_INIT_RUNNING_UPDATES () {
1004 // Auto-init ext_running_updates
1005 if (!isset($GLOBALS['ext_running_updates'])) {
1006 $GLOBALS['ext_running_updates'] = array();
1010 // Getter for EXT_UPDATE_DEPENDS flag
1011 function EXT_GET_UPDATE_DEPENDS () {
1012 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1013 return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1016 // Getter for next iterator depency
1017 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1018 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1019 return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1022 // Counter for extension update depencies
1023 function EXT_COUNT_UPDATE_DEPENDS () {
1024 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1025 return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1028 // Removes given extension from update denpency list
1029 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1031 $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1034 if ($key !== false) {
1036 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1038 // And sort the array
1039 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1043 // Init iterator for update depencies
1044 function EXT_INIT_UPDATE_ITERATOR () {
1045 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1046 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1049 // Getter for depency iterator
1050 function EXT_GET_UPDATE_ITERATOR () {
1051 // Auto-init iterator
1052 if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1055 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]."<br />\n";
1056 return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1059 // Increments the update iterator
1060 function EXT_INCREMENT_UPDATE_INTERATOR () {
1061 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1062 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1065 // Setter for EXT_REPORTS_FAILURE flag
1066 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1067 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1070 // Getter for EXT_REPORTS_FAILURE flag
1071 function EXT_GET_REPORTS_FAILURE () {
1072 return $GLOBALS['ext_reports_failure'];
1075 // Setter for EXT_VER_HISTORY flag
1076 function EXT_SET_VER_HISTORY ($verHistory) {
1077 $GLOBALS['ext_ver_history'] = (array) $verHistory;
1080 // Getter for EXT_VER_HISTORY array
1081 function EXT_GET_VER_HISTORY () {
1082 return $GLOBALS['ext_ver_history'];
1085 // Setter for EXT_UPDATE_NOTES flag
1086 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1087 $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1090 // Getter for EXT_UPDATE_NOTES flag
1091 function EXT_GET_UPDATE_NOTES () {
1092 return $GLOBALS['ext_update_notes'];
1095 // Init extension notice
1096 function EXT_INIT_NOTES () {
1097 $GLOBALS['ext_notes'] = "";
1100 // Append extension notice
1101 function EXT_APPEND_NOTES ($notes) {
1102 $GLOBALS['ext_notes'] .= (string) $notes;
1105 // Getter for extension notes
1106 function EXT_GET_NOTES () {
1107 return $GLOBALS['ext_notes'];
1110 // Setter for current extension name
1111 function EXT_SET_CURR_NAME ($ext_name) {
1112 $GLOBALS['curr_ext_name'] = (string) $ext_name;
1115 // Getter for current extension name
1116 function EXT_GET_CURR_NAME () {
1117 if (isset($GLOBALS['curr_ext_name'])) {
1118 return $GLOBALS['curr_ext_name'];
1122 debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1125 // Init SQLs array for current extension
1126 function INIT_EXT_SQLS () {
1127 // Auto-init the array now...
1128 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1129 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1133 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1134 function ADD_EXT_SQL ($sql) {
1135 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1138 // Getter for SQLs array for current extension
1139 function GET_EXT_SQLS () {
1140 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1141 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1142 // Not found, should not happen
1143 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1149 return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1152 // Removes SQLs for current extension
1153 function UNSET_EXT_SQLS () {
1154 unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1157 // Auto-initializes the removal list
1158 function EXT_INIT_REMOVAL_LIST () {
1159 // Is the remove list there?
1160 if (!isset($GLOBALS['ext_update_remove'])) {
1162 $GLOBALS['ext_update_remove'] = array();
1166 // Checks wether the current extension is on the removal list
1167 function EXT_IS_ON_REMOVAL_LIST () {
1168 // Init removal list
1169 EXT_INIT_REMOVAL_LIST();
1172 return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1175 // Adds the current extension to the removal list
1176 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1178 $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1181 // Getter for removal list
1182 function EXT_GET_REMOVAL_LIST () {
1183 // Return the removal list
1184 return $GLOBALS['ext_update_remove'];