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 // In normal mode return a true on success
271 } elseif ($dry_run) {
272 // Rewrite SQL command to keep { and } inside for dry-run
273 foreach (GET_SQLS() as $key => $sql) {
274 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
275 SET_SQL_KEY($key, $sql);
278 // In "dry-run" mode return array with all SQL commands
281 // Remove all SQL commands
284 // Extension has been removed for updates, so all is fine!
288 // No, an error occurs while registering extension :-(
289 //* DEBUG: */ print __LINE__.":".EXT_GET_CURR_NAME()."<br />\n";
292 } elseif (($task_id > 0) && (EXT_GET_CURR_NAME() != "")) {
293 // Remove task from system when id and extension's name is valid
294 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE id=%s AND `status`='NEW' LIMIT 1",
295 array(bigintval($task_id)), __FILE__, __LINE__);
298 // Is this the sql_patches?
299 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":{EXT_GET_CURR_NAME()}/{$EXT_LOAD_MODE}");
300 if ((EXT_GET_CURR_NAME() == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
301 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
302 if ($logout === true) {
303 // Then redirect to logout
304 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
306 // Add temporary filter
307 REGISTER_FILTER('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
308 $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
312 // Return status code
316 // Run SQL queries for given extension id
317 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
318 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
319 // This shall never do a non-admin user!
320 if (!IS_ADMIN()) return false;
322 // Get extension's name
323 $ext_name = GET_EXT_NAME($ext_id);
325 // If it is not set then maybe there is no extension for that ID number
326 if ($ext_name == "") return false;
328 // Set current SQL name
329 EXT_SET_CURR_NAME($ext_name);
334 // By default no SQL has been executed
337 // Load extension in detected mode
338 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]={EXT_GET_CURR_NAME()}");
339 LOAD_EXTENSION(EXT_GET_CURR_NAME(), $load_mode, "", false);
341 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".COUNT_SQLS()."");
342 if ((IS_SQLS_VALID() && (COUNT_SQLS() > 0))) {
343 // Run SQL commands...
344 runFilterChain('run_sqls');
347 if ($load_mode == "remove") {
348 // Delete this extension (remember to remove it from your server *before* you click on welcome!
349 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
350 array(EXT_GET_CURR_NAME()), __FILE__, __LINE__);
354 // Remove cache file(s) if extension is active
355 if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == "activate") || ($load_mode == "deactivate"))) {
357 runFilterChain('post_extension_run_sql', EXT_GET_CURR_NAME());
360 // Is this the sql_patches?
361 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": {$ext_id}/{EXT_GET_CURR_NAME()}/{$load_mode}");
362 if ((EXT_GET_CURR_NAME() == "sql_patches") && (($load_mode == "register") || ($load_mode == "remove"))) {
363 // Then redirect to logout
364 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
365 LOAD_URL("modules.php?module=admin&logout=1&".$load_mode."=sql_patches");
369 // Check if given extension is active
370 function EXT_IS_ACTIVE ($ext_name) {
371 // Extensions are all inactive during installation
372 if ((!isInstalled()) || (isInstalling()) || (empty($ext_name))) return false;
374 // Not active is the default
378 if (isset($GLOBALS['cache_array']['extensions']['ext_active'][$ext_name])) {
380 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
381 $active = $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name];
384 incrementConfigEntry('cache_hits');
385 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
386 // @TODO Extension is loaded, what next?
387 mxchange_die("LOADED:$ext_name");
388 } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
389 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
390 // Load from database
391 $result = SQL_QUERY_ESC("SELECT ext_active FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
392 array($ext_name), __FILE__, __LINE__);
395 if (SQL_NUMROWS($result) == 1) {
397 list($active) = SQL_FETCHROW($result);
401 SQL_FREERESULT($result);
404 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
405 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = $active;
407 // Extension not active!
408 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
409 $GLOBALS['cache_array']['extensions']['ext_active'][$ext_name] = "N";
413 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
415 // Is this extension activated? (For admins we always have active extensions...)
416 return ($active == "Y");
418 // Get version from extensions
419 function GET_EXT_VERSION ($ext_name) {
420 // By default no extension is found
423 // Extensions are all inactive during installation
424 if ((!isInstalled()) || (isInstalling())) return "";
425 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
427 // Is the cache written?
428 if (isset($GLOBALS['cache_array']['extensions']['ext_version'][$ext_name])) {
429 // Load data from cache
430 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
431 $ext_ver = $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name];
434 incrementConfigEntry('cache_hits');
435 } elseif (!isCacheInstanceValid()) {
436 // Load from database
437 $result = SQL_QUERY_ESC("SELECT ext_version FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
438 array($ext_name), __FILE__, __LINE__);
439 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": DB - ".SQL_NUMROWS($result)."");
441 // Is the extension there?
442 if (SQL_NUMROWS($result) == 1) {
444 list($ext_ver) = SQL_FETCHROW($result);
448 SQL_FREERESULT($result);
451 $GLOBALS['cache_array']['extensions']['ext_version'][$ext_name] = $ext_ver;
455 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
459 // Updates a given extension with current extension version to latest version
460 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
461 // Only admins are allowed to update extensions
462 if ((!IS_ADMIN()) || (empty($ext_name))) return false;
464 // Set current SQL name
465 EXT_SET_CURR_NAME($ext_name);
472 // Load extension in test mode
473 LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run);
475 // Save version history
476 $history = EXT_GET_VER_HISTORY();
478 // Remove old SQLs array to prevent possible bugs
481 // Check if version is updated
482 if (((EXT_GET_VERSION() != $ext_ver) || ($dry_run)) && (is_array($history))) {
483 // Search for starting point
484 $start = array_search($ext_ver, $history);
486 // And load SQL queries in order of version history
487 for ($idx = ($start + 1); $idx < count($history); $idx++) {
488 // Set extension version
489 $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()] = $history[$idx];
492 LOAD_EXTENSION(EXT_GET_CURR_NAME(), "update", $GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], $dry_run);
494 if (EXT_GET_UPDATE_DEPENDS() != "") {
495 // Backup current SQL queries
496 $GLOBALS['cache_array']['update_sqls'][EXT_GET_CURR_NAME()] = GET_SQLS();
498 // Is the extension there?
499 if (GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()) != "") {
500 // Update another extension first!
501 $test = EXTENSION_UPDATE(EXT_GET_UPDATE_DEPENDS(), GET_EXT_VERSION(EXT_GET_UPDATE_DEPENDS()), $dry_run);
503 // Register new extension
504 $test = REGISTER_EXTENSION(EXT_GET_UPDATE_DEPENDS(), 0, $dry_run, false);
507 // Restore previous SQL queries
508 SET_SQLS($GLOBALS['cache_array']['update_sqls'][EXT_GET_CURR_NAME()]);
509 unset($GLOBALS['cache_array']['update_sqls'][EXT_GET_CURR_NAME()]);
513 ADD_EXTENSION_NOTES($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
516 // In real-mode execute any existing includes
518 $GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()] = GET_INC_POOL();
519 runFilterChain('load_includes');
520 SET_INC_POOL($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
521 unset($GLOBALS['cache_array']['inc_pool'][EXT_GET_CURR_NAME()]);
525 runFilterChain('run_sqls', array('dry_run' => $dry_run));
529 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())));
531 // Update extension's version
532 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
533 array($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()], EXT_GET_CURR_NAME()), __FILE__, __LINE__);
537 unset($GLOBALS['cache_array']['update_ver'][EXT_GET_CURR_NAME()]);
539 // Run filters on success extension update
540 runFilterChain('extension_update', EXT_GET_CURR_NAME());
545 // Output verbose SQL table for extension
546 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = "", $dashed = "", $switch = false, $width = "100%") {
549 // Then fix it to default
550 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
553 // Are there some queries in $queries?
554 if (count($queries) > 0) {
555 // Then use them instead!
563 // Do we have queries?
564 if ((IS_SQLS_VALID()) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
565 foreach (GET_SQLS() as $idx => $sql) {
569 // Output command if set
571 // Prepare output for template
579 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
581 // Switch color and count up
587 // Prepare content for template
595 // Load main template
596 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
597 } elseif ((GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
598 // No addional SQL commands to run
599 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
606 // Get extension name from id
607 function GET_EXT_NAME ($ext_id) {
608 // Init extension name
612 if (isset($GLOBALS['cache_array']['extensions']['ext_name'][$ext_id])) {
614 $ret = $GLOBALS['cache_array']['extensions']['ext_name'][$ext_id];
617 incrementConfigEntry('cache_hits');
618 } elseif (!EXT_IS_ACTIVE("cache")) {
619 // Load from database
620 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE id=%s LIMIT 1",
621 array(bigintval($ext_id)), __FILE__, __LINE__);
622 list($ret) = SQL_FETCHROW($result);
623 SQL_FREERESULT($result);
628 // Get extension id from name
629 function GET_EXT_ID ($ext_name) {
632 if (isset($GLOBALS['cache_array']['extensions']['ext_id'][$ext_name])) {
634 $ret = $GLOBALS['cache_array']['extensions']['ext_id'][$ext_name];
637 incrementConfigEntry('cache_hits');
638 } elseif (!EXT_IS_ACTIVE("cache")) {
639 // Load from database
640 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
641 array($ext_name), __FILE__, __LINE__);
642 list($ret) = SQL_FETCHROW($result);
643 SQL_FREERESULT($result);
650 // Activate given extension
651 function ACTIVATE_EXTENSION ($ext_name) {
652 // Activate the extension
653 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
654 array($ext_name), __FILE__, __LINE__);
656 // Extension has been activated?
657 if (SQL_AFFECTEDROWS() == 1) {
658 // Then run all queries
659 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
663 // Deactivate given extension
664 function DEACTIVATE_EXTENSION($ext_name) {
665 // Activate the extension
666 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
667 array($ext_name), __FILE__, __LINE__);
669 // Extension has been activated?
670 if (SQL_AFFECTEDROWS() == 1) {
671 // Then run all queries
672 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
675 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
678 SEND_ADMIN_NOTIFICATION(
679 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
680 "admin_ext_deactivated",
681 array('ext_name' => $ext_name)
686 // Checks wether the extension is older than given
687 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
688 // Get current extension version
689 $currVersion = GET_EXT_VERSION($ext_name);
691 // Remove all dots from both versions
692 $currVersion = str_replace(".", "", $currVersion);
693 $ext_ver = str_replace(".", "", $ext_ver);
695 // Now compare both and return the result
696 return ($currVersion < $ext_ver);
699 // Creates a new task for updated extension
700 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
701 // Create subject line
702 $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] {--ADMIN_UPDATE_EXT_SUBJ--}";
704 // Is the extension there?
705 if (GET_EXT_VERSION($ext_name) != "") {
706 // Check if task is not there
707 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
708 // Task not created so it's a brand-new extension which we need to register and create a task for!
709 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())",
710 array($admin_id, $subject, $notes), __FILE__, __LINE__);
715 // Creates a new task for newly installed extension
716 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
717 // Not installed and do we have created a task for the admin?
718 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
720 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
726 // Load text for task
727 if (FILE_READABLE($tpl)) {
728 // Load extension's own text template (HTML!)
729 $msg = LOAD_TEMPLATE("ext_".$ext, true);
731 // Load default message
732 $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
735 // Task not created so it's a brand-new extension which we need to register and create a task for!
736 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
737 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
742 ), __FILE__, __LINE__, true, false
747 // Creates a task for automatically deactivated (deprecated) extension
748 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
749 // Create subject line
750 $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
752 // Not installed and do we have created a task for the admin?
753 if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
754 // Task not created so add it
755 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
756 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
759 SQL_ESCAPE(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
760 ), __FILE__, __LINE__, true, false
765 // Checks if the module has a menu
766 function MODULE_HAS_MENU ($mod, $forceDb = false) {
767 // All is false by default
770 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
771 if (GET_EXT_VERSION("cache") >= "0.1.2") {
772 // Cache version is okay, so let's check the cache!
773 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
774 // Check module cache and count hit
775 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == "Y");
776 incrementConfigEntry('cache_hits');
777 } elseif (isset($GLOBALS['cache_array']['extensions']['ext_menu'][$mod])) {
778 // Check cache and count hit
779 $ret = ($GLOBALS['cache_array']['extensions']['ext_menu'][$mod] == "Y");
780 incrementConfigEntry('cache_hits');
781 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
782 // Admin module has always a menu!
785 } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
786 // Check database for entry
787 $result = SQL_QUERY_ESC("SELECT has_menu FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE module='%s' LIMIT 1",
788 array($mod), __FILE__, __LINE__);
791 if (SQL_NUMROWS($result) == 1) {
792 // Load "has_menu" column
793 list($has_menu) = SQL_FETCHROW($result);
796 $GLOBALS['cache_array']['extensions']['ext_menu'][$mod] = $has_menu;
798 // Does it have a menu?
799 $ret = ($has_menu == "Y");
803 SQL_FREERESULT($result);
804 } elseif (GET_EXT_VERSION("sql_patches") == "") {
805 // No sql_patches installed, so maybe in admin area or no admin registered?
806 $ret = (((IS_ADMIN()) || (!isAdminRegistered())) && ($mod == "admin")); // Then there is a menu!
810 //* DEBUG: */ var_dump($ret);
814 // Determines the task id for given extension
815 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
816 // Default is not found
819 // Search for extension task's id
820 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
821 array($ext_name), __FILE__, __LINE__);
824 if (SQL_NUMROWS($result) == 1) {
825 // Task found so load task's ID and register extension...
826 list($task_id) = SQL_FETCHROW($result);
830 SQL_FREERESULT($result);
836 // Determines the task id for given subject
837 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
838 // Default is not found
841 // Search for task id
842 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject LIKE '%s%%' LIMIT 1",
843 array($subject), __FILE__, __LINE__);
846 if (SQL_NUMROWS($result) == 1) {
847 // Task found so load task's ID and register extension...
848 list($task_id) = SQL_FETCHROW($result);
852 SQL_FREERESULT($result);
858 // Add updates notes for given version
859 function ADD_EXTENSION_NOTES ($ver) {
860 // Init notes/content
861 $out = ""; $content = array();
863 // Is do we have verbose output enabled?
864 if ((getConfig('verbose_sql') == "Y") || (!EXT_IS_ACTIVE("sql_patches"))) {
866 // Update notes found?
867 if (EXT_GET_UPDATE_NOTES() != "") {
868 // Update notes found
871 'notes' => EXT_GET_UPDATE_NOTES()
875 EXT_SET_UPDATE_NOTES("");
876 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
880 'notes' => getMessage('INITIAL_RELEASE')
883 // No update notes found!
886 'notes' => getMessage('NO_UPDATE_NOTES')
891 $out = LOAD_TEMPLATE("admin_ext_notes", true, $content);
895 EXT_APPEND_NOTES($out);
898 // Getter for CSS files array
899 function EXT_GET_CSS_FILES () {
900 // By default no additional CSS files are found
903 // Is the array there?
904 if (isset($GLOBALS['css_files'])) {
906 $cssFiles = $GLOBALS['css_files'];
913 // Init CSS files array
914 function EXT_INIT_CSS_FILES () {
916 $GLOBALS['css_files'] = array();
920 function EXT_ADD_CSS_FILE ($file) {
921 // Is the array there?
922 if (!isset($GLOBALS['css_files'])) {
923 // Then auto-init them
924 EXT_INIT_CSS_FILES();
928 $GLOBALS['css_files'][] = $file;
931 // Setter for EXT_ALWAYS_ACTIVE flag
932 function EXT_SET_ALWAYS_ACTIVE ($active) {
933 $GLOBALS['ext_always_active'] = (string) $active;
936 // Getter for EXT_ALWAYS_ACTIVE flag
937 function EXT_GET_ALWAYS_ACTIVE () {
938 return $GLOBALS['ext_always_active'];
941 // Setter for EXT_VERSION flag
942 function EXT_SET_VERSION ($version) {
943 $GLOBALS['ext_version'] = (float) $version;
946 // Getter for EXT_VERSION flag
947 function EXT_GET_VERSION () {
948 return $GLOBALS['ext_version'];
951 // Setter for EXT_DEPRECATED flag
952 function EXT_SET_DEPRECATED ($deprecated) {
953 $GLOBALS['ext_deprecated'] = (string) $deprecated;
956 // Getter for EXT_DEPRECATED flag
957 function EXT_GET_DEPRECATED () {
958 return $GLOBALS['ext_deprecated'];
961 // Setter for EXT_UPDATE_DEPENDS flag
962 function EXT_ADD_UPDATE_DEPENDS ($updateDepends) {
963 // Is the update depency empty? (NEED TO BE FIXED!)
964 if (empty($updateDepends)) {
965 // Please report this bug!
966 debug_report_bug("updateDepends is left empty!");
969 // Is it not yet added?
970 if (!in_array($updateDepends, $GLOBALS['ext_running_updates'])) {
971 //* DEBUG */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$updateDepends."<br />\n";
972 // Add it to the list of extension update depencies map
973 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][] = (string) $updateDepends;
975 // Remember it in the list of running updates
976 $GLOBALS['ext_running_updates'][] = $updateDepends;
980 // Init EXT_UPDATE_DEPENDS flag
981 function EXT_INIT_UPDATE_DEPENDS () {
982 //* DEBUG: */ print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
984 // Init update depency map
985 $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()] = array();
987 // Init running updates array
988 EXT_INIT_RUNNING_UPDATES();
991 // Initializes the list of running updates
992 function EXT_INIT_RUNNING_UPDATES () {
993 // Auto-init ext_running_updates
994 if (!isset($GLOBALS['ext_running_updates'])) {
995 $GLOBALS['ext_running_updates'] = array();
999 // Getter for EXT_UPDATE_DEPENDS flag
1000 function EXT_GET_UPDATE_DEPENDS () {
1001 print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1002 return $GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()];
1005 // Getter for next iterator depency
1006 function EXT_GET_ITERATOR_UPDATE_DEPENDS () {
1007 print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1008 return ($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][EXT_GET_UPDATE_ITERATOR()]);
1011 // Counter for extension update depencies
1012 function EXT_COUNT_UPDATE_DEPENDS () {
1013 print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1014 return count($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1017 // Removes given extension from update denpency list
1018 function EXT_REMOVE_UPDATE_DEPENDS ($ext_name) {
1020 $key = array_search($ext_name, EXT_GET_UPDATE_DEPENDS());
1023 if ($key !== false) {
1025 unset($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()][$key]);
1027 // And sort the array
1028 ksort($GLOBALS['ext_update_depends'][EXT_GET_CURR_NAME()]);
1032 // Init iterator for update depencies
1033 function EXT_INIT_UPDATE_ITERATOR () {
1034 print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1035 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()] = 0;
1038 // Getter for depency iterator
1039 function EXT_GET_UPDATE_ITERATOR () {
1040 // Auto-init iterator
1041 if (!isset($GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()])) EXT_INIT_UPDATE_ITERATOR();
1044 print __FUNCTION__.":".EXT_GET_CURR_NAME()."/".$GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]."<br />\n";
1045 return $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()];
1048 // Increments the update iterator
1049 function EXT_INCREMENT_UPDATE_INTERATOR () {
1050 print __FUNCTION__.":".EXT_GET_CURR_NAME()."<br />\n";
1051 $GLOBALS['ext_depend_iterator'][EXT_GET_CURR_NAME()]++;
1054 // Setter for EXT_REPORTS_FAILURE flag
1055 function EXT_SET_REPORTS_FAILURE ($reportsFailure) {
1056 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1059 // Getter for EXT_REPORTS_FAILURE flag
1060 function EXT_GET_REPORTS_FAILURE () {
1061 return $GLOBALS['ext_reports_failure'];
1064 // Setter for EXT_VER_HISTORY flag
1065 function EXT_SET_VER_HISTORY ($verHistory) {
1066 $GLOBALS['ext_ver_history'] = (array) $verHistory;
1069 // Getter for EXT_VER_HISTORY array
1070 function EXT_GET_VER_HISTORY () {
1071 return $GLOBALS['ext_ver_history'];
1074 // Setter for EXT_UPDATE_NOTES flag
1075 function EXT_SET_UPDATE_NOTES ($updateNotes) {
1076 $GLOBALS['ext_update_notes'] = (string) $updateNotes;
1079 // Getter for EXT_UPDATE_NOTES flag
1080 function EXT_GET_UPDATE_NOTES () {
1081 return $GLOBALS['ext_update_notes'];
1084 // Init extension notice
1085 function EXT_INIT_NOTES () {
1086 $GLOBALS['ext_notes'] = "";
1089 // Append extension notice
1090 function EXT_APPEND_NOTES ($notes) {
1091 $GLOBALS['ext_notes'] .= (string) $notes;
1094 // Getter for extension notes
1095 function EXT_GET_NOTES () {
1096 return $GLOBALS['ext_notes'];
1099 // Setter for current extension name
1100 function EXT_SET_CURR_NAME ($ext_name) {
1101 $GLOBALS['curr_ext_name'] = (string) $ext_name;
1104 // Getter for current extension name
1105 function EXT_GET_CURR_NAME () {
1106 if (isset($GLOBALS['curr_ext_name'])) {
1107 return $GLOBALS['curr_ext_name'];
1111 debug_report_bug(__FUNCTION__.": curr_ext_name not initialized. Please execute INIT_EXT_SQLS() before calling this function.");
1114 // Init SQLs array for current extension
1115 function INIT_EXT_SQLS () {
1116 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()] = array();
1119 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1120 function ADD_EXT_SQL ($sql) {
1121 $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()][] = $sql;
1124 // Getter for SQLs array for current extension
1125 function GET_EXT_SQLS () {
1126 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1127 if (!isset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()])) {
1128 // Not found, should not happen
1129 debug_report_bug(sprintf("ext_sqls is empty, current extension: %s",
1135 return $GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()];
1138 // Removes SQLs for current extension
1139 function UNSET_EXT_SQLS () {
1140 unset($GLOBALS['ext_sqls'][EXT_GET_CURR_NAME()]);
1143 // Auto-initializes the removal list
1144 function EXT_INIT_REMOVAL_LIST () {
1145 // Is the remove list there?
1146 if (!isset($GLOBALS['ext_update_remove'])) {
1148 $GLOBALS['ext_update_remove'] = array();
1152 // Checks wether the current extension is on the removal list
1153 function EXT_IS_ON_REMOVAL_LIST () {
1154 // Init removal list
1155 EXT_INIT_REMOVAL_LIST();
1158 return (in_array(EXT_GET_CURR_NAME(), $GLOBALS['ext_update_remove']));
1161 // Adds the current extension to the removal list
1162 function EXT_ADD_CURRENT_TO_REMOVAL_LIST () {
1164 $GLOBALS['ext_update_remove'][] = EXT_GET_CURR_NAME();
1167 // Getter for removal list
1168 function EXT_GET_REMOVAL_LIST () {
1169 // Return the removal list
1170 return $GLOBALS['ext_update_remove'];