2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/25/2009 *
4 * =================== Last change: 10/25/2009 *
6 * -------------------------------------------------------------------- *
7 * File : extensions-functions.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 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Load the extension and maybe found language and function files.
46 function loadExtension ($ext_name, $ext_mode = 'init', $ext_ver = '', $dry_run = false) {
47 // If this happens twice, we need the bug report from you, except for updates/tests
48 if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
49 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: ext_name=' . $ext_name . ', ext_mode='. $ext_mode . ',ext_sqls=' . print_r(getExtensionSqls(), true) . ', ext_register_running=' . print_r($GLOBALS['ext_register_running'], true) . ', ext_running_updates=' . print_r($GLOBALS['ext_running_updates'], true));
52 // Make sure this situation can only happen once
53 $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = true;
56 setExtensionMode($ext_mode);
58 // Set current extension name
59 setCurrentExtensionName($ext_name);
61 if (!empty($ext_ver)) {
62 // Set current extension version
63 setCurrentExtensionVersion($ext_ver);
66 setCurrentExtensionVersion('0.0');
68 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
69 if ($ext_mode != 'test') {
70 // Log empty versions not in test-mode, but maybe it is fine...
71 logDebugMessage(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
76 enableExtensionDryRun($dry_run);
79 initIncludePool('extension');
81 // Init EXT_UPDATE_DEPENDS if not yet done
82 if (!isExtensionUpdateDependenciesInitialized()) {
84 initExtensionUpdateDependencies();
87 // Init current extension name list
90 // Is the extension already loaded?
91 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', mode=' . getExtensionMode() . ', ver=' . getCurrentExtensionVersion());
92 if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (getExtensionMode() == 'init')) {
94 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
100 // Is the extension file NOT there?
101 if (!isExtensionNameValid($ext_name)) {
103 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
109 // Load extension's own language file if not in test mode
110 if ((getExtensionMode() != 'test') && (ifExtensionHasLanguageFile($ext_name))) {
112 loadLanguageFile($ext_name);
116 if (isExtensionFunctionFileReadable($ext_name)) {
118 if ((($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y') || (!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name]))) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
119 // Construct IFN for functions file
120 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
123 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
125 // Download functions file
126 loadIncludeOnce($funcsInclude);
128 } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (getScriptOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
129 // No functions file is not so good...
130 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
136 // Extensions are not deprecated by default
137 setExtensionDeprecated('N');
139 // Extensions are not always active by default
140 setExtensionAlwaysActive('N');
142 // Extension update notes
143 // @TODO Do we still need this? setExtensionUpdateNotes('');
145 // Include the extension file
146 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension loaded.');
147 loadExtensionInclude();
149 // Is this extension deprecated?
150 if (isExtensionDeprecated()) {
151 // Deactivate the extension
152 doDeactivateExtension($ext_name);
158 // Mark it as loaded in normal mode
159 if (getExtensionMode() == '') {
161 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
168 // Registeres an extension and possible update depencies
169 function registerExtension ($ext_name, $taskId, $dry_run = false, $logout = true) {
170 // Set current extension name
171 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',task_id=' . $taskId . ',dry_run=' . intval($dry_run) . ',logout=' . intval($logout) . ' - ENTERED!');
172 setCurrentExtensionName($ext_name);
175 enableExtensionDryRun($dry_run);
177 // By default all extensions are in productive phase
178 enableExtensionProductive();
180 // This shall never do a non-admin user or if the extension is active (already installed)
181 if ((!isAdmin()) || (isExtensionInstalled($ext_name))) {
182 // Abort here with 'false'
186 // When this extension is already in registration/update phase, all is fine
187 if ((isExtensionRegistrationRunning($ext_name)) || (isExtensionUpdateRunning($ext_name))) {
188 // Then abort here with 'true' becaus it is fine
189 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ALREADY!');
193 // This registration is running
194 addExtensionRunningRegistration($ext_name);
196 // Init EXT_UPDATE_DEPENDS if not yet done
197 if (!isExtensionUpdateDependenciesInitialized()) {
199 initExtensionUpdateDependencies();
202 // Is the task id zero? Then we need to auto-fix it here
203 if ($taskId == '0') {
204 // Try to find the task
205 $taskId = determineExtensionTaskId(getCurrentExtensionName());
207 // Still zero and not in dry-run?
208 if (($taskId == '0') && (!isExtensionDryRun())) {
209 // Now try to create a new task
210 $taskId = createNewExtensionTask(getCurrentExtensionName());
213 if ($taskId == '0') {
214 // Then request a bug report
215 debug_report_bug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
217 getCurrentExtensionName()
223 // Init queries and notes
225 initExtensionNotes();
230 initIncludePool('extension');
232 // By default we have no failures
233 setExtensionReportsFailure(false);
235 // Does this extension exists?
236 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
237 if (loadExtension(getCurrentExtensionName(), 'register', '', isExtensionDryRun())) {
238 // Set current extension name again
239 setCurrentExtensionName($ext_name);
241 // And run possible updates
242 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
243 $history = getExtensionVersionHistory();
244 foreach ($history as $ext_ver) {
245 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ext_ver);
246 // Load extension in update mode
247 loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun());
249 // Add update notes to our output
250 addExtensionNotes($ext_ver);
253 // Does this extension depends on an outstanding update of another update?
254 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
256 $ext_update = getExtensionUpdateDependenciesIterator();
258 // Increment here to avoid endless loop
259 incrementExtensionUpdateIterator();
261 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
262 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
263 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun()))) {
264 // Set current extension name again
265 setCurrentExtensionName($ext_name);
267 // If versions mismatch update extension first
269 if (isExtensionInstalled($ext_update)) {
270 // Get version only if installed
271 $ext_ver = getExtensionVersion($ext_update);
274 // Extension version set? If empty the extension is not registered
275 if (empty($ext_ver)) {
276 // Extension not registered so far so first load task's id...
277 $task = determineExtensionTaskId($ext_update);
281 // Try to register the extension
282 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ':ext_update=' . $ext_update . ',taskId=' . $task);
283 $test = registerExtension($ext_update, $task, isExtensionDryRun(), false);
285 // Reset extension name
286 setCurrentExtensionName($ext_name);
287 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
289 } elseif ($ext_ver != getCurrentExtensionVersion()) {
290 // Ok, update this extension now
291 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
292 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
293 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
294 updateExtension($ext_update, $ext_ver, isExtensionDryRun());
295 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
296 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
297 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
298 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
299 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
304 // Nothing to register / update before...
308 // Required file for update does not exists!
310 // But this is fine for the first time...
313 // Restore the current extension name
314 setCurrentExtensionName($ext_name);
317 // Is there no update?
318 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
319 // Then test is passed!
323 // Switch back to register mode
324 setExtensionMode('register');
326 // Remains true if extension registration reports no failures
327 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
328 $test = (($test === true) && (getExtensionReportsFailure() === false));
329 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',test=' . intval($test));
331 // Does everthing before wents ok?
332 if ($test === true) {
333 // "Dry-run-mode" activated?
334 if ((isExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
335 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
336 // Init SQLs and transfer ext->generic
338 setSqlsArray(getExtensionSqls());
340 // Run installation pre-installation filters
341 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun()));
343 // Register extension
344 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
345 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
346 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
348 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
350 getCurrentExtensionName(),
351 getExtensionAlwaysActive(),
352 getCurrentExtensionVersion(),
354 ), __FUNCTION__, __LINE__);
356 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
358 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
360 getCurrentExtensionName(),
361 getExtensionAlwaysActive(),
362 getCurrentExtensionVersion()
363 ), __FUNCTION__, __LINE__);
366 // Use the insert id as extension id and cache it for early usage
367 $GLOBALS['cache_array']['extension']['ext_id'][getCurrentExtensionName()] = SQL_INSERTID();
369 // Remove cache file(s) if extension is active
370 runFilterChain('post_extension_installed', array(
371 'pool' => 'extension',
372 'ext_name' => getCurrentExtensionName(),
376 // Re-init queries and notes
377 initExtensionSqls(true);
378 initExtensionNotes(true);
380 // Mark it as installed
381 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
383 // In normal mode return a true on success
385 } elseif (isExtensionDryRun() === true) {
386 // In "dry-run" mode do always return a true
389 // Extension has been removed for updates, so all is fine!
393 // No, an error occurs while registering extension :-(
394 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
397 } elseif (($taskId > 0) && (getCurrentExtensionName() != '')) {
398 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
399 // Remove task from system when id and extension's name is valid
400 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
401 array(bigintval($taskId)), __FUNCTION__, __LINE__);
404 // Is this the sql_patches?
405 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
406 if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!isExtensionDryRun()) && ($test)) {
407 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
408 if ($logout === true) {
409 // Then redirect to logout
410 redirectToUrl('modules.php?module=admin&logout=1&' . getExtensionMode() . '=sql_patches');
412 // Add temporary filter
413 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
414 $GLOBALS['ext_load_mode'] = getExtensionMode();
418 // Return status code
419 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',test=' . intval($test) . ' - EXIT!');
423 // Run SQL queries for given extension id
424 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
425 function doExtensionSqls ($ext_id, $load_mode) {
426 // This shall never do a non-admin user!
427 if (!isAdmin()) return false;
429 // Get extension's name
430 $ext_name = getExtensionName($ext_id);
432 // Set current SQL name
433 setCurrentExtensionName($ext_name);
435 // Init EXT_UPDATE_DEPENDS
436 if (!isExtensionUpdateDependenciesInitialized()) {
438 initExtensionUpdateDependencies();
444 // By default no SQL has been executed
447 // Load extension in detected mode
448 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName());
449 loadExtension(getCurrentExtensionName(), $load_mode, '', false);
453 setSqlsArray(getExtensionSqls());
455 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
457 // Run SQL commands...
458 runFilterChain('run_sqls');
461 // Run any filters depending on the action here
462 runFilterChain('extension_' . $load_mode);
464 // Remove cache file(s) if extension is active
465 if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
467 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
470 // Is this the sql_patches?
471 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $ext_id . ',currName=' . getCurrentExtensionName() . ',loadMode=' . $load_mode);
472 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
473 // Then redirect to logout
474 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ': LOAD!');
475 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
479 // Check wether the given extension is installed
480 function isExtensionInstalled ($ext_name) {
481 // We don't like empty extension names here
482 if (empty($ext_name)) {
483 // Please fix them all
484 debug_report_bug(__FUNCTION__.': ext_name is empty.');
487 // By default non is installed
488 $isInstalled = false;
490 // Check if there is a cache entry
491 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
492 // Use cache built from below queries
493 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
494 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
499 incrementStatsEntry('cache_hits');
500 } elseif ((isInstallationPhase())) {
501 // Extensions are all inactive/not installed during installation
504 $ext_id = getExtensionId($ext_name);
506 // Do we have a record?
507 $isInstalled = ($ext_id > 0);
509 // Is it installed, then cache the entry
510 if ($isInstalled === true) {
511 // Dummy call (get is okay here)
512 getExtensionId($ext_name, true);
515 // Remember the status
516 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
523 // Check if given extension is active
524 function isExtensionActive ($ext_name) {
525 // Extensions are all inactive during installation
526 if ((isInstallationPhase()) || (empty($ext_name))) return false;
528 // Not active is the default
529 $data['ext_active'] = 'N';
532 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
534 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
535 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
538 incrementStatsEntry('cache_hits');
539 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
540 // @TODO Extension is loaded, what next?
541 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
542 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
543 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DB! ext_name=' . $ext_name);
544 // Load from database
545 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
546 array($ext_name), __FUNCTION__, __LINE__);
549 if (SQL_NUMROWS($result) == 1) {
551 $data = SQL_FETCHARRAY($result);
555 SQL_FREERESULT($result);
558 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . '[DB]: ' . $data['ext_active']);
559 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
561 // Extension not active!
562 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': Not active!');
563 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
567 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
569 // Is this extension activated? (For admins we always have active extensions...)
570 return ($data['ext_active'] == 'Y');
573 // Get version from extensions
574 function getExtensionVersion ($ext_name, $force = false) {
575 // By default no extension is found
576 $data['ext_version'] = 'false';
578 // Empty extension name should be fixed!
579 if (empty($ext_name)) {
580 // Please report this bug!
581 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
584 // Extensions are all inactive during installation
585 if (isInstallationPhase()) return '';
586 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
588 // Is the cache written?
589 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
590 // Load data from cache
591 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
592 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
595 incrementStatsEntry('cache_hits');
596 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getScriptOutputMode() != 0)) {
597 // Load from database
598 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
599 array($ext_name), __FUNCTION__, __LINE__);
600 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
602 // Is the extension there?
603 if (SQL_NUMROWS($result) == 1) {
605 $data = SQL_FETCHARRAY($result);
606 } elseif (isDebugModeEnabled()) {
607 // Not found, please report all
608 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
612 SQL_FREERESULT($result);
615 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
618 // Extension version should not be invalid
619 if (($data['ext_version'] == 'false') && ($force === false)) {
620 // Please report this trouble
621 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
625 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
626 return $data['ext_version'];
629 // Updates a given extension with current extension version to latest version
630 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
631 // Only admins are allowed to update extensions
632 if ((!isAdmin()) || (empty($ext_name))) return false;
634 // Set current SQL name
635 setCurrentExtensionName($ext_name);
639 initExtensionNotes();
640 initIncludePool('extension');
642 // Load extension in test mode
643 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
645 // Save version history
646 $history = getExtensionVersionHistory();
648 // Remove old SQLs array to prevent possible bugs
651 // Check if version is updated
652 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
653 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
654 // Search for starting point
655 $start = array_search($ext_ver, $history);
657 // And load SQL queries in order of version history
658 for ($idx = ($start + 1); $idx < count($history); $idx++) {
659 // Set extension version
660 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
663 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
666 $depencies = getExtensionUpdateDependencies();
669 if (count($depencies) > 0) {
670 // Apply all extension depencies
671 foreach ($depencies as $ext_depend) {
672 // Did we already update/register this?
673 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
675 setCurrentExtensionName($ext_depend);
677 // Mark it as already updated before we update it
678 $GLOBALS['ext_updated'][$ext_depend] = true;
680 // Is the extension there?
681 if (isExtensionInstalled($ext_depend)) {
682 // Update another extension first!
683 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
685 // Register new extension
686 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
691 // Set extension version here
692 setCurrentExtensionVersion($ext_ver);
695 setCurrentExtensionName($ext_name);
699 addExtensionNotes($history[$idx]);
702 // In real-mode execute any existing includes
703 if (isExtensionDryRun() === false) {
704 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
705 runFilterChain('load_includes', 'extension');
706 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
707 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
712 setSqlsArray(getExtensionSqls());
715 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun()));
717 if (isExtensionDryRun() === false) {
718 // Run filters on success extension update
719 runFilterChain('extension_update', getCurrentExtensionName());
724 // Output verbose SQL table for extension
725 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
728 // Then fix it to default
729 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
735 // Do we have queries?
736 if ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
737 // Do we have entries?
738 if (countExtensionSqls() > 0) {
742 foreach (getExtensionSqls() as $sqls) {
743 // New array format is recursive
744 foreach ($sqls as $sql) {
748 // Output command if set
750 // Prepare output for template
757 $OUT .= loadTemplate('admin_extension_sql_row', true, $content);
765 // Prepare content for template
773 // Load main template
774 $OUT = loadTemplate('admin_extension_sql_table', true, $content);
776 // No addional SQL commands to run
777 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
785 // Get extension name from id
786 function getExtensionName ($ext_id) {
787 // Init extension name
788 $data['ext_name'] = '';
791 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
793 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
796 incrementStatsEntry('cache_hits');
797 } elseif (!isExtensionActive('cache')) {
798 // Load from database
799 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
800 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
802 // Is the entry there?
803 if (SQL_NUMROWS($result) == 1) {
804 // Get the extension's name from database
805 $data = SQL_FETCHARRAY($result);
809 SQL_FREERESULT($result);
812 // Did we find some extension?
813 if (empty($data['ext_name'])) {
814 // We should fix these all!
815 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
818 // Return the extension name
819 return $data['ext_name'];
822 // Get extension id from name
823 function getExtensionId ($ext_name) {
825 $data['ext_id'] = '0';
828 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
830 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
833 incrementStatsEntry('cache_hits');
835 // Load from database
836 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
837 array($ext_name), __FUNCTION__, __LINE__);
839 // Is the entry there?
840 if (SQL_NUMROWS($result) == 1) {
841 // Get the extension's id from database
842 $data = SQL_FETCHARRAY($result);
846 SQL_FREERESULT($result);
849 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
853 return $data['ext_id'];
856 // Determines wether the given extension name is valid
857 function isExtensionNameValid ($ext_name) {
859 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
860 // Generate include file name
861 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
863 // Is there a file in inc/extensions/ ?
864 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
868 return $GLOBALS['ext_name_valid'][$ext_name];
871 // Determines wether the given extension id is valid
872 function isExtensionIdValid ($ext_id) {
873 // Default is nothing valid
876 // Check in cache then in database
877 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
882 incrementStatsEntry('cache_hits');
885 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
892 // Activate given extension
893 function doActivateExtension ($ext_name) {
894 // Activate the extension
895 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
896 array($ext_name), __FUNCTION__, __LINE__);
898 // Extension has been activated?
899 if (SQL_AFFECTEDROWS() == 1) {
900 // Then run all queries
901 doExtensionSqls(getExtensionId($ext_name), 'activate');
905 // Deactivate given extension
906 function doDeactivateExtension($ext_name) {
907 // Activate the extension
908 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
909 array($ext_name), __FUNCTION__, __LINE__);
911 // Extension has been activated?
912 if (SQL_AFFECTEDROWS() == 1) {
913 // Then run all queries
914 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
916 // Create new task (we ignore the task id here)
917 createExtensionDeactivationTask($ext_name);
920 sendAdminNotification(
921 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
922 'admin_extension_deactivated',
923 array('ext_name' => $ext_name)
928 // Checks wether the extension is older than given
929 function isExtensionOlder ($ext_name, $ext_ver) {
930 // Get current extension version
931 $currVersion = getExtensionVersion($ext_name);
933 // Remove all dots from both versions
934 $currVersion = str_replace('.', '', $currVersion);
935 $ext_ver = str_replace('.', '', $ext_ver);
937 // Now compare both and return the result
938 return ($currVersion < $ext_ver);
941 // Creates a new task for updated extension
942 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
943 // Create subject line
944 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
947 $taskId = determineTaskIdBySubject($subject);
949 // Is the extension there?
950 if (isExtensionInstalled($ext_name)) {
951 // Check if task is not there
952 if ($taskId == '0') {
953 // Create extension update-task
954 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
957 // Extension not there! :-(
958 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
965 // Creates a new task for newly installed extension
966 function createNewExtensionTask ($ext_name) {
967 // Generate subject line
968 $subject = sprintf("[%s:]", $ext_name);
971 $taskId = determineTaskIdBySubject($subject);
973 // Not installed and do we have created a task for the admin?
974 if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
975 // Set default message if ext-foo is missing
976 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
979 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
985 // Load text for task if found
986 if (isFileReadable($tpl)) {
987 // Load extension's own text template (HTML!)
988 $message = loadTemplate('ext_' . $ext_name, true);
990 // Write this in debug.log as well
991 logDebugMessage(__FUNCTION__, __LINE__, $message);
994 // Task not created so it's a brand-new extension which we need to register and create a task for!
995 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
1002 // Creates a task for automatically deactivated (deprecated) extension
1003 function createExtensionDeactivationTask ($ext_name) {
1004 // Create subject line
1005 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1008 $taskId = determineTaskIdBySubject($subject);
1010 // Not installed and do we have created a task for the admin?
1011 if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1012 // Task not created so add it
1013 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1020 // Determines the task id for given extension
1021 function determineExtensionTaskId ($ext_name) {
1022 // Default is not found
1023 $data['task_id'] = '0';
1025 // Search for extension task's id
1026 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1027 array($ext_name), __FUNCTION__, __LINE__);
1030 if (SQL_NUMROWS($result) == 1) {
1031 // Task found so load task's id and register extension...
1032 $data = SQL_FETCHARRAY($result);
1036 SQL_FREERESULT($result);
1039 return $data['task_id'];
1042 // Determines the task id for given subject
1043 function determineTaskIdBySubject ($subject) {
1044 // Default is not found
1045 $data['task_id'] = '0';
1047 // Search for task id
1048 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1049 array($subject), __FUNCTION__, __LINE__);
1052 if (SQL_NUMROWS($result) == 1) {
1053 // Task found so load task's id and register extension...
1054 $data = SQL_FETCHARRAY($result);
1058 SQL_FREERESULT($result);
1061 return $data['task_id'];
1064 // Add updates notes for given version
1065 function addExtensionNotes ($ext_ver) {
1066 // Init notes/content
1070 // Is do we have verbose output enabled?
1071 if ((!isExtensionActive('sql_patches')) || (getConfig('verbose_sql') == 'Y')) {
1072 // Update notes found?
1073 if (isExtensionUpdateNoteSet($ext_ver)) {
1074 // Update notes found
1077 'notes' => getExtensionUpdateNotes($ext_ver)
1081 setExtensionUpdateNotes('', $ext_ver);
1082 } elseif (in_array($ext_ver, array('0.0', '0.0.0'))) {
1086 'notes' => '{--INITIAL_RELEASE--}'
1089 // No update notes found!
1092 'notes' => '{--NO_UPDATE_NOTES--}'
1097 $out = loadTemplate('admin_extension_notes', true, $content);
1101 appendExtensionNotes($out);
1104 // Getter for CSS files array
1105 function getExtensionCssFiles () {
1106 // By default no additional CSS files are found
1107 $cssFiles = array();
1109 // Is the array there?
1110 if (isset($GLOBALS['css_files'])) {
1112 $cssFiles = $GLOBALS['css_files'];
1119 // Init CSS files array
1120 function initExtensionCssFiles () {
1122 $GLOBALS['css_files'] = array();
1126 function addExtensionCssFile ($file) {
1127 // Is the array there?
1128 if (!isset($GLOBALS['css_files'])) {
1129 // Then auto-init them
1130 initExtensionCssFiles();
1134 $GLOBALS['css_files'][] = $file;
1137 // Setter for EXT_ALWAYS_ACTIVE flag
1138 function setExtensionAlwaysActive ($active) {
1139 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1142 // Getter for EXT_ALWAYS_ACTIVE flag
1143 function getExtensionAlwaysActive () {
1144 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1147 // Checks wether the current extension is always active
1148 function isExtensionAlwaysActive () {
1149 return (getExtensionAlwaysActive() == 'Y');
1152 // Setter for EXT_VERSION flag
1153 function setThisExtensionVersion ($ext_version) {
1154 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1157 // Getter for EXT_VERSION flag
1158 function getThisExtensionVersion () {
1159 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1162 // Setter for EXT_DEPRECATED flag
1163 function setExtensionDeprecated ($deprecated) {
1164 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1167 // Getter for EXT_DEPRECATED flag
1168 function isExtensionDeprecated () {
1169 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1172 // Setter for EXT_UPDATE_DEPENDS flag
1173 function addExtensionUpdateDependency ($updateDepends) {
1174 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1175 // Is the update depency empty? (NEED TO BE FIXED!)
1176 if (empty($updateDepends)) {
1177 // Please report this bug!
1178 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1181 // Is it not yet added?
1182 /* Only for debugging!
1183 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1184 // Double-adding isn't fine, too
1185 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1189 // Add it to the list of extension update depencies map
1190 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1192 // Remember it in the list of running updates
1193 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1194 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1197 // Getter for running updates
1198 function getExtensionUpdatesRunning () {
1199 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1202 // Checks wether the given extension registration is in progress
1203 function isExtensionRegistrationRunning ($ext_name) {
1205 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1208 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1212 // Init EXT_UPDATE_DEPENDS flag
1213 function initExtensionUpdateDependencies () {
1214 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1216 // Init update depency map automatically if not found
1217 if (isExtensionUpdateDependenciesInitialized()) {
1218 // We need these bug reports as well...
1219 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1222 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1224 // Init running updates array
1225 initExtensionRuningUpdates();
1227 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1230 // Adds an extension as "registration in progress"
1231 function addExtensionRunningRegistration ($ext_name) {
1233 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1234 if (isExtensionRegistrationRunning($ext_name)) {
1235 // This is really bad and should not be quietly ignored
1236 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1240 $GLOBALS['ext_register_running'][] = $ext_name;
1241 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1244 // Checks wether EXT_UPDATE_DEPENDS is initialized
1245 function isExtensionUpdateDependenciesInitialized () {
1246 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1247 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1250 // Checks wether an update is already running for given extension
1251 function isExtensionUpdateRunning ($ext_name) {
1252 // Current and given extensions means whole array
1253 if ($ext_name == getCurrentExtensionName()) {
1254 // Default is not found
1257 // Walk through whole array
1258 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1260 if (($ext1 == $ext_name) || ($isRunning === true)) {
1262 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1269 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1274 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1277 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1281 // Initializes the list of running updates
1282 function initExtensionRuningUpdates () {
1283 // Auto-init ext_running_updates
1284 if (!isset($GLOBALS['ext_running_updates'])) {
1285 $GLOBALS['ext_running_updates'] = array();
1286 $GLOBALS['ext_register_running'] = array();
1290 // Getter for EXT_UPDATE_DEPENDS flag
1291 function getExtensionUpdateDependencies () {
1292 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1293 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1296 // Getter for next iterator depency
1297 function getExtensionUpdateDependenciesIterator () {
1298 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1299 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1302 // Counter for extension update depencies
1303 function countExtensionUpdateDependencies () {
1304 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1305 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1308 // Removes given extension from update denpency list
1309 function removeExtensionUpdateDependency ($ext_name) {
1311 $key = array_search($ext_name, getExtensionUpdateDependencies());
1314 if ($key !== false) {
1316 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1318 // And sort the array
1319 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1323 // Init iterator for update depencies
1324 function initExtensionUpdateIterator () {
1325 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1326 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1329 // Getter for depency iterator
1330 function getExtensionUpdateIterator () {
1331 // Auto-init iterator
1332 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1335 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1336 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1339 // Increments the update iterator
1340 function incrementExtensionUpdateIterator () {
1341 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1342 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1345 // Setter for EXT_REPORTS_FAILURE flag
1346 function setExtensionReportsFailure ($reportsFailure) {
1347 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1350 // Getter for EXT_REPORTS_FAILURE flag
1351 function getExtensionReportsFailure () {
1352 return $GLOBALS['ext_reports_failure'];
1355 // Setter for EXT_VER_HISTORY flag
1356 function setExtensionVersionHistory ($versionHistory) {
1357 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1360 // Getter for EXT_VER_HISTORY array
1361 function getExtensionVersionHistory () {
1362 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1365 // Setter for EXT_UPDATE_NOTES
1366 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1367 // . '/' . getCurrentExtensionVersion()
1368 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . ',updateNotes()=' . strlen($updateNotes));
1369 if (empty($ext_ver)) {
1370 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1372 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1376 // Getter for EXT_UPDATE_NOTES
1377 function getExtensionUpdateNotes ($ext_ver) {
1378 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1381 // Checks if ext_update_notes is set
1382 function isExtensionUpdateNoteSet ($ext_ver) {
1383 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1386 // Init extension notice
1387 function initExtensionNotes ($force = false) {
1388 // Is it already initialized?
1389 if (($force === false) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1390 // This is mostly not wanted, so please report it
1391 debug_report_bug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1394 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1395 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1398 // Append extension notice
1399 function appendExtensionNotes ($notes) {
1400 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes()=' . strlen($notes));
1401 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1404 // Getter for extension notes
1405 function getExtensionNotes () {
1406 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1409 // Setter for current extension name
1410 function setCurrentExtensionName ($ext_name) {
1411 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1414 // Getter for current extension name
1415 function getCurrentExtensionName () {
1416 if (!isset($GLOBALS['curr_extension_name'])) {
1418 debug_report_bug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1422 return $GLOBALS['curr_extension_name'];
1425 // Init SQLs array for current extension
1426 function initExtensionSqls ($force = false) {
1427 // Auto-init the array or if forced
1428 if (($force === true) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1430 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1432 // Initialize the generic array
1437 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1438 function addExtensionSql ($sql) {
1440 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1443 // Getter for SQLs array for current extension
1444 function getExtensionSqls () {
1445 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1446 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1447 // Not found, should not happen
1448 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1449 getCurrentExtensionName()
1454 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1457 // Count SQLs for current extension
1458 function countExtensionSqls () {
1459 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1460 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1461 // Not found, should not happen
1462 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1463 getCurrentExtensionName()
1468 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1471 // Removes SQLs for current extension
1472 function unsetExtensionSqls () {
1473 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1476 // Auto-initializes the removal list
1477 function initExtensionRemovalList () {
1478 // Is the remove list there?
1479 if (!isset($GLOBALS['ext_update_remove'])) {
1481 $GLOBALS['ext_update_remove'] = array();
1485 // Checks wether the current extension is on the removal list
1486 function isExtensionOnRemovalList () {
1487 // Init removal list
1488 initExtensionRemovalList();
1491 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1494 // Adds the current extension to the removal list
1495 function addCurrentExtensionToRemovalList () {
1497 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1500 // Getter for removal list
1501 function getExtensionRemovalList () {
1502 // Return the removal list
1503 return $GLOBALS['ext_update_remove'];
1506 // Redirects if the provided extension is not installed
1507 function redirectOnUninstalledExtension ($ext_name) {
1508 // So is the extension there?
1509 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1510 // Redirect to index
1511 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1515 // Filter for initialization of all extensions by loading them in 'init' mode
1516 function FILTER_INIT_EXTENSIONS () {
1517 // Do we have some entries?
1518 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY!');
1519 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1520 // Load all found extensions if found
1521 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1522 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1524 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - START');
1525 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1526 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name='.$ext_name.' - END');
1528 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1530 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1533 // Setter for extension mode
1534 function setExtensionMode ($ext_mode) {
1535 $GLOBALS['ext_mode'] = (string) $ext_mode;
1538 // Getter for extension mode
1539 function getExtensionMode () {
1540 return $GLOBALS['ext_mode'];
1543 // Setter for dry-run
1544 function enableExtensionDryRun ($dry_run = true) {
1545 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',dry='.intval($dry_run));
1546 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1549 // Getter for dry-run
1550 function isExtensionDryRun () {
1551 return $GLOBALS['ext_dry_run'];
1554 // Setter for current extension version
1555 function setCurrentExtensionVersion ($ext_ver) {
1556 // ext_ver should never be empty in other modes than 'test'
1557 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1558 // Please report all these messages
1559 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1563 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1564 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1567 // Getter for current extension version
1568 function getCurrentExtensionVersion () {
1569 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1570 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1573 // Remove the extension from cache array
1574 function removeExtensionFromArray () {
1575 // "Cache" this name
1576 $ext_name = getCurrentExtensionName();
1578 // Now loop through the whole cache
1579 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1580 // Is it an element?
1581 if (isset($cacheArray[$ext_name])) {
1583 unset($cacheArray[$ext_name]);
1584 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1587 $key = array_search($ext_name, $cacheArray);
1590 if ($key !== false) {
1591 // Found, so remove it
1592 unset($cacheArray[$key]);
1593 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1599 // "Getter" for 'extension has a CSS file' (with same name, of course)
1600 function getExtensionHasCss () {
1601 // Default is no CSS
1604 // Construct FQFN for check
1605 $FQFN = sprintf("%stheme/%s/css/%s.css",
1608 getCurrentExtensionName()
1612 if (isFileReadable($FQFN)) {
1613 // Readable, so it is there...
1621 // Checks wether the given extension has a language file
1622 function ifExtensionHasLanguageFile ($ext_name) {
1623 // Default is no language file
1624 $hasLanguage = false;
1626 // Do we have cache?
1627 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1629 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1632 incrementStatsEntry('cache_hits');
1634 // Not readable is default
1637 // Is the language file readable for this extension?
1638 if (isLanguageIncludeReadable($ext_name)) {
1644 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
1648 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1651 // Load an extension's include file
1652 function loadExtensionInclude () {
1654 if (!isExtensionIncludeReadable()) {
1656 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1659 // Generate INC name
1660 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1666 // Checks wether an extension is readable
1667 function isExtensionIncludeReadable ($ext_name = '') {
1668 // If empty, use current
1669 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1672 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1673 // Generate INC name
1674 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1677 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1681 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1682 return $GLOBALS['ext_inc_readable'][$ext_name];
1685 // Checks if an extension's function file is readable
1686 function isExtensionFunctionFileReadable ($ext_name) {
1688 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1689 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1690 // Just count cache hits
1691 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1692 incrementStatsEntry('cache_hits');
1694 // Construct IFN for functions file
1695 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1697 // Is this include there?
1698 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1700 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=Y - FOUND!');
1701 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1704 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=N - NOT FOUND!');
1705 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1710 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1711 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1714 // Adds an admin menu to the SQL queue of the menu entry is not found
1715 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1716 // Now check if this menu is there
1717 if (!isMenuActionValid('admin', $action, $what)) {
1719 if (is_null($what)) {
1721 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1729 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1738 // Add it to the queue
1739 addExtensionSql($sql);
1740 } elseif (isDebugModeEnabled()) {
1741 // Double menus should be located and fixed!
1742 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1746 // Adds a guest menu to the SQL queue if the menu entry is not found
1747 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1748 // Now check if this menu is there
1749 if (!isMenuActionValid('guest', $action, $what)) {
1751 if (is_null($what)) {
1753 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1762 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1772 // Add it to the queue
1773 addExtensionSql($sql);
1774 } elseif (isDebugModeEnabled()) {
1775 // Double menus should be located and fixed!
1776 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1780 // Adds a member menu to the SQL queue if the menu entry is not found
1781 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1782 // Now check if this menu is there
1783 if (!isMenuActionValid('member', $action, $what)) {
1785 if (is_null($what)) {
1787 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1796 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1806 // Add it to the queue
1807 addExtensionSql($sql);
1808 } elseif (isDebugModeEnabled()) {
1809 // Double menus should be located and fixed!
1810 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1814 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1815 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1816 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1817 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1819 if (is_null($what)) {
1821 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1829 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1838 // Add it to the queue
1839 addExtensionSql($sql);
1840 } elseif (isDebugModeEnabled()) {
1841 // Double menus should be located and fixed!
1842 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1846 // Enables/disables productive mode for current extension (used only while
1848 function enableExtensionProductive ($isProductive = true) {
1849 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1852 // Checks wether the extension is in productive phase. If not set, development
1853 // phase (=false) is assumed.
1854 function isExtensionProductive () {
1855 return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));