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 FQFN 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()) && (getOutputMode() == '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 $ver) {
245 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ver);
246 // Load extension in update mode
247 loadExtension(getCurrentExtensionName(), 'update', $ver, isExtensionDryRun());
249 // Add update notes to our output
250 addExtensionNotes($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());
288 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
290 } elseif ($ext_ver != getCurrentExtensionVersion()) {
291 // Ok, update this extension now
292 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
293 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
294 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
295 updateExtension($ext_update, $ext_ver, isExtensionDryRun());
296 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
297 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
298 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
299 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
300 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
305 // Nothing to register / update before...
309 // Required file for update does not exists!
311 // But this is fine for the first time...
314 // Restore the current extension name
315 setCurrentExtensionName($ext_name);
318 // Is there no update?
319 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
320 // Then test is passed!
324 // Switch back to register mode
325 setExtensionMode('register');
327 // Remains true if extension registration reports no failures
328 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
329 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
330 $test = (($test === true) && (getExtensionReportsFailure() === false));
331 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
332 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
334 // Does everthing before wents ok?
335 if ($test === true) {
336 // "Dry-run-mode" activated?
337 if ((isExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
338 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
339 // Init SQLs and transfer ext->generic
341 setSqlsArray(getExtensionSqls());
343 // Run installation pre-installation filters
344 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun()));
346 // Register extension
347 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
348 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
349 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
351 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
353 getCurrentExtensionName(),
354 getExtensionAlwaysActive(),
355 getCurrentExtensionVersion(),
357 ), __FUNCTION__, __LINE__);
359 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
361 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
363 getCurrentExtensionName(),
364 getExtensionAlwaysActive(),
365 getCurrentExtensionVersion()
366 ), __FUNCTION__, __LINE__);
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();
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 // Re-init queries and notes
390 initExtensionNotes();
392 // Extension has been removed for updates, so all is fine!
396 // No, an error occurs while registering extension :-(
397 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
400 } elseif (($taskId > 0) && (getCurrentExtensionName() != '')) {
401 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
402 // Remove task from system when id and extension's name is valid
403 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
404 array(bigintval($taskId)), __FUNCTION__, __LINE__);
407 // Is this the sql_patches?
408 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
409 if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!isExtensionDryRun()) && ($test)) {
410 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
411 if ($logout === true) {
412 // Then redirect to logout
413 redirectToUrl('modules.php?module=admin&logout=1&' . getExtensionMode() . '=sql_patches');
415 // Add temporary filter
416 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
417 $GLOBALS['ext_load_mode'] = getExtensionMode();
421 // Return status code
422 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ' - EXIT!');
423 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>'.print_r($ret, true).'</pre>');
427 // Run SQL queries for given extension id
428 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
429 function doExtensionSqls ($ext_id, $load_mode) {
430 // This shall never do a non-admin user!
431 if (!isAdmin()) return false;
433 // Get extension's name
434 $ext_name = getExtensionName($ext_id);
436 // Set current SQL name
437 setCurrentExtensionName($ext_name);
439 // Init EXT_UPDATE_DEPENDS
440 if (!isExtensionUpdateDependenciesInitialized()) {
442 initExtensionUpdateDependencies();
448 // By default no SQL has been executed
451 // Load extension in detected mode
452 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]=".getCurrentExtensionName()."");
453 loadExtension(getCurrentExtensionName(), $load_mode, '', false);
457 setSqlsArray(getExtensionSqls());
459 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":SQLs::count=".countSqls()."");
461 // Run SQL commands...
462 runFilterChain('run_sqls');
465 // Run any filters depending on the action here
466 runFilterChain('extension_' . $load_mode);
468 // Remove cache file(s) if extension is active
469 if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
471 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
474 // Is this the sql_patches?
475 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": id=".$ext_id.",currName=".getCurrentExtensionName().",loadMode=".$load_mode);
476 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
477 // Then redirect to logout
478 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": LOAD!");
479 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
483 // Check wether the given extension is installed
484 function isExtensionInstalled ($ext_name) {
485 // We don't like empty extension names here
486 if (empty($ext_name)) {
487 // Please fix them all
488 debug_report_bug(__FUNCTION__.': ext_name is empty.');
491 // By default non is installed
492 $isInstalled = false;
494 // Check if there is a cache entry
495 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
496 // Use cache built from below queries
497 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
498 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
503 incrementStatsEntry('cache_hits');
504 } elseif ((isInstallationPhase())) {
505 // Extensions are all inactive/not installed during installation
508 $ext_id = getExtensionId($ext_name);
510 // Do we have a record?
511 $isInstalled = ($ext_id > 0);
513 // Is it installed, then cache the entry
514 if ($isInstalled === true) {
515 // Dummy call (get is okay here)
516 getExtensionId($ext_name, true);
519 // Remember the status
520 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
527 // Check if given extension is active
528 function isExtensionActive ($ext_name) {
529 // Extensions are all inactive during installation
530 if ((isInstallationPhase()) || (empty($ext_name))) return false;
532 // Not active is the default
533 $data['ext_active'] = 'N';
536 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
538 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
539 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
542 incrementStatsEntry('cache_hits');
543 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
544 // @TODO Extension is loaded, what next?
545 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
546 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
547 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
548 // Load from database
549 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
550 array($ext_name), __FUNCTION__, __LINE__);
553 if (SQL_NUMROWS($result) == 1) {
555 $data = SQL_FETCHARRAY($result);
559 SQL_FREERESULT($result);
562 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: ".$data['ext_active']."");
563 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
565 // Extension not active!
566 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
567 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
571 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.",active=".$data['ext_active']."");
573 // Is this extension activated? (For admins we always have active extensions...)
574 return ($data['ext_active'] == 'Y');
577 // Get version from extensions
578 function getExtensionVersion ($ext_name, $force = false) {
579 // By default no extension is found
580 $data['ext_version'] = 'false';
582 // Empty extension name should be fixed!
583 if (empty($ext_name)) {
584 // Please report this bug!
585 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
588 // Extensions are all inactive during installation
589 if (isInstallationPhase()) return '';
590 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ext_name=".$ext_name."");
592 // Is the cache written?
593 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
594 // Load data from cache
595 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
596 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
599 incrementStatsEntry('cache_hits');
600 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getOutputMode() != 0)) {
601 // Load from database
602 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
603 array($ext_name), __FUNCTION__, __LINE__);
604 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
606 // Is the extension there?
607 if (SQL_NUMROWS($result) == 1) {
609 $data = SQL_FETCHARRAY($result);
610 } elseif (isDebugModeEnabled()) {
611 // Not found, please report all
612 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
616 SQL_FREERESULT($result);
619 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
622 // Extension version should not be invalid
623 if (($data['ext_version'] == 'false') && ($force === false)) {
624 // Please report this trouble
625 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
629 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ret={$data['ext_version']}");
630 return $data['ext_version'];
633 // Updates a given extension with current extension version to latest version
634 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
635 // Only admins are allowed to update extensions
636 if ((!isAdmin()) || (empty($ext_name))) return false;
638 // Set current SQL name
639 setCurrentExtensionName($ext_name);
643 initExtensionNotes();
644 initIncludePool('extension');
646 // Load extension in test mode
647 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
649 // Save version history
650 $history = getExtensionVersionHistory();
652 // Remove old SQLs array to prevent possible bugs
655 // Check if version is updated
656 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
657 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
658 // Search for starting point
659 $start = array_search($ext_ver, $history);
661 // And load SQL queries in order of version history
662 for ($idx = ($start + 1); $idx < count($history); $idx++) {
663 // Set extension version
664 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
667 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
670 $depencies = getExtensionUpdateDependencies();
673 if (count($depencies) > 0) {
674 // Apply all extension depencies
675 foreach ($depencies as $ext_depend) {
676 // Did we already update/register this?
677 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
679 setCurrentExtensionName($ext_depend);
681 // Mark it as already updated before we update it
682 $GLOBALS['ext_updated'][$ext_depend] = true;
684 // Is the extension there?
685 if (isExtensionInstalled($ext_depend)) {
686 // Update another extension first!
687 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun());
689 // Register new extension
690 $test = registerExtension($ext_depend, 0, isExtensionDryRun(), false);
695 // Set extension version here
696 setCurrentExtensionVersion($ext_ver);
699 setCurrentExtensionName($ext_name);
703 addExtensionNotes($history[$idx]);
706 // In real-mode execute any existing includes
707 if (isExtensionDryRun() === false) {
708 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
709 runFilterChain('load_includes', 'extension');
710 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
711 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
716 setSqlsArray(getExtensionSqls());
719 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun()));
721 if (isExtensionDryRun() === false) {
722 // Run filters on success extension update
723 runFilterChain('extension_update', getCurrentExtensionName());
728 // Output verbose SQL table for extension
729 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
732 // Then fix it to default
733 $title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}';
739 // Do we have queries?
740 if ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
742 foreach (getExtensionSqls() as $sqls) {
743 // New array format is recursive
744 foreach ($sqls as $idx => $sql) {
748 // Output command if set
750 // Prepare output for template
757 $OUT .= loadTemplate('admin_ext_sql_row', true, $content);
762 // Prepare content for template
770 // Load main template
771 $OUT = loadTemplate('admin_ext_sql_table', true, $content);
772 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
773 // No addional SQL commands to run
774 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
781 // Get extension name from id
782 function getExtensionName ($ext_id) {
783 // Init extension name
784 $data['ext_name'] = '';
787 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
789 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
792 incrementStatsEntry('cache_hits');
793 } elseif (!isExtensionActive('cache')) {
794 // Load from database
795 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
796 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
798 // Is the entry there?
799 if (SQL_NUMROWS($result) == 1) {
800 // Get the extension's name from database
801 $data = SQL_FETCHARRAY($result);
805 SQL_FREERESULT($result);
808 // Did we find some extension?
809 if (empty($data['ext_name'])) {
810 // We should fix these all!
811 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
814 // Return the extension name
815 return $data['ext_name'];
818 // Get extension id from name
819 function getExtensionId ($ext_name) {
821 $data['ext_id'] = '0';
824 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
826 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
829 incrementStatsEntry('cache_hits');
831 // Load from database
832 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
833 array($ext_name), __FUNCTION__, __LINE__);
835 // Is the entry there?
836 if (SQL_NUMROWS($result) == 1) {
837 // Get the extension's id from database
838 $data = SQL_FETCHARRAY($result);
842 SQL_FREERESULT($result);
845 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
849 return $data['ext_id'];
852 // Determines wether the given extension name is valid
853 function isExtensionNameValid ($ext_name) {
855 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
856 // Generate include file name
857 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
859 // Is there a file in inc/extensions/ ?
860 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
864 return $GLOBALS['ext_name_valid'][$ext_name];
867 // Determines wether the given extension id is valid
868 function isExtensionIdValid ($ext_id) {
869 // Default is nothing valid
872 // Check in cache then in database
873 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
878 incrementStatsEntry('cache_hits');
881 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
888 // Activate given extension
889 function doActivateExtension ($ext_name) {
890 // Activate the extension
891 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
892 array($ext_name), __FUNCTION__, __LINE__);
894 // Extension has been activated?
895 if (SQL_AFFECTEDROWS() == 1) {
896 // Then run all queries
897 doExtensionSqls(getExtensionId($ext_name), 'activate');
901 // Deactivate given extension
902 function doDeactivateExtension($ext_name) {
903 // Activate the extension
904 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
905 array($ext_name), __FUNCTION__, __LINE__);
907 // Extension has been activated?
908 if (SQL_AFFECTEDROWS() == 1) {
909 // Then run all queries
910 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
912 // Create new task (we ignore the task id here)
913 createExtensionDeactivationTask($ext_name);
916 sendAdminNotification(
917 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
918 'admin_ext_deactivated',
919 array('ext_name' => $ext_name)
924 // Checks wether the extension is older than given
925 function isExtensionOlder ($ext_name, $ext_ver) {
926 // Get current extension version
927 $currVersion = getExtensionVersion($ext_name);
929 // Remove all dots from both versions
930 $currVersion = str_replace('.', '', $currVersion);
931 $ext_ver = str_replace('.', '', $ext_ver);
933 // Now compare both and return the result
934 return ($currVersion < $ext_ver);
937 // Creates a new task for updated extension
938 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
939 // Create subject line
940 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
943 $taskId = determineTaskIdBySubject($subject);
945 // Is the extension there?
946 if (isExtensionInstalled($ext_name)) {
947 // Check if task is not there
948 if ($taskId == '0') {
949 // Create extension update-task
950 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
953 // Extension not there! :-(
954 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
961 // Creates a new task for newly installed extension
962 function createNewExtensionTask ($ext_name) {
963 // Generate subject line
964 $subject = sprintf("[%s:]", $ext_name);
967 $taskId = determineTaskIdBySubject($subject);
969 // Not installed and do we have created a task for the admin?
970 if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
971 // Set default message if ext-foo is missing
972 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext_name);
975 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
981 // Load text for task if found
982 if (isFileReadable($tpl)) {
983 // Load extension's own text template (HTML!)
984 $message = loadTemplate('ext_' . $ext_name, true);
986 // Write this in debug.log as well
987 logDebugMessage(__FUNCTION__, __LINE__, $message);
990 // Task not created so it's a brand-new extension which we need to register and create a task for!
991 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), false);
998 // Creates a task for automatically deactivated (deprecated) extension
999 function createExtensionDeactivationTask ($ext_name) {
1000 // Create subject line
1001 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1004 $taskId = determineTaskIdBySubject($subject);
1006 // Not installed and do we have created a task for the admin?
1007 if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
1008 // Task not created so add it
1009 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
1016 // Checks if the module has a menu
1017 function ifModuleHasMenu ($mod, $forceDb = false) {
1018 // All is false by default
1021 // Extension installed and newer than or has version 0.1.2?
1022 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
1023 // Cache version is okay, so let's check the cache!
1024 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
1025 // Check module cache and count hit
1026 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
1027 incrementStatsEntry('cache_hits');
1028 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
1029 // Check cache and count hit
1030 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
1031 incrementStatsEntry('cache_hits');
1033 // Admin/guest/member/sponsor modules have always a menu!
1034 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
1036 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
1037 // Check database for entry
1038 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
1039 array($mod), __FUNCTION__, __LINE__);
1042 if (SQL_NUMROWS($result) == 1) {
1043 // Load "has_menu" column
1044 $data = SQL_FETCHARRAY($result);
1046 // Fake cache... ;-)
1047 $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
1049 // Does it have a menu?
1050 $ret = ($data['has_menu'] == 'Y');
1054 SQL_FREERESULT($result);
1055 } elseif (!isExtensionInstalled('sql_patches')) {
1056 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
1057 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
1059 // Unsupported state!
1060 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
1067 // Determines the task id for given extension
1068 function determineExtensionTaskId ($ext_name) {
1069 // Default is not found
1070 $data['task_id'] = '0';
1072 // Search for extension task's id
1073 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1074 array($ext_name), __FUNCTION__, __LINE__);
1077 if (SQL_NUMROWS($result) == 1) {
1078 // Task found so load task's id and register extension...
1079 $data = SQL_FETCHARRAY($result);
1083 SQL_FREERESULT($result);
1086 return $data['task_id'];
1089 // Determines the task id for given subject
1090 function determineTaskIdBySubject ($subject) {
1091 // Default is not found
1092 $data['task_id'] = '0';
1094 // Search for task id
1095 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1096 array($subject), __FUNCTION__, __LINE__);
1099 if (SQL_NUMROWS($result) == 1) {
1100 // Task found so load task's id and register extension...
1101 $data = SQL_FETCHARRAY($result);
1105 SQL_FREERESULT($result);
1108 return $data['task_id'];
1111 // Add updates notes for given version
1112 function addExtensionNotes ($ver) {
1113 // Init notes/content
1114 $out = ''; $content = array();
1116 // Is do we have verbose output enabled?
1117 if ((!isExtensionActive('sql_patches')) || (getConfig('verbose_sql') == 'Y')) {
1118 // Update notes found?
1119 if (isExtensionUpdateNoteSet($ver)) {
1120 // Update notes found
1123 'notes' => getExtensionUpdateNotes($ver)
1127 setExtensionUpdateNotes('', $ver);
1128 } elseif (($ver == '0.0') || ($ver == '0.0.0')) {
1132 'notes' => '{--INITIAL_RELEASE--}'
1135 // No update notes found!
1138 'notes' => '{--NO_UPDATE_NOTES--}'
1143 $out = loadTemplate('admin_EXTENSION_notes', true, $content);
1147 appendExtensionNotes($out);
1150 // Getter for CSS files array
1151 function getExtensionCssFiles () {
1152 // By default no additional CSS files are found
1153 $cssFiles = array();
1155 // Is the array there?
1156 if (isset($GLOBALS['css_files'])) {
1158 $cssFiles = $GLOBALS['css_files'];
1165 // Init CSS files array
1166 function initExtensionCssFiles () {
1168 $GLOBALS['css_files'] = array();
1172 function addExtensionCssFile ($file) {
1173 // Is the array there?
1174 if (!isset($GLOBALS['css_files'])) {
1175 // Then auto-init them
1176 initExtensionCssFiles();
1180 $GLOBALS['css_files'][] = $file;
1183 // Setter for EXT_ALWAYS_ACTIVE flag
1184 function setExtensionAlwaysActive ($active) {
1185 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1188 // Getter for EXT_ALWAYS_ACTIVE flag
1189 function getExtensionAlwaysActive () {
1190 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1193 // Checks wether the current extension is always active
1194 function isExtensionAlwaysActive () {
1195 return (getExtensionAlwaysActive() == 'Y');
1198 // Setter for EXT_VERSION flag
1199 function setThisExtensionVersion ($version) {
1200 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $version;
1203 // Getter for EXT_VERSION flag
1204 function getThisExtensionVersion () {
1205 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1208 // Setter for EXT_DEPRECATED flag
1209 function setExtensionDeprecated ($deprecated) {
1210 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1213 // Getter for EXT_DEPRECATED flag
1214 function isExtensionDeprecated () {
1215 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1218 // Setter for EXT_UPDATE_DEPENDS flag
1219 function addExtensionUpdateDependency ($updateDepends) {
1220 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1221 // Is the update depency empty? (NEED TO BE FIXED!)
1222 if (empty($updateDepends)) {
1223 // Please report this bug!
1224 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1227 // Is it not yet added?
1229 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1230 // Double-adding isn't fine, too
1231 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1235 // Add it to the list of extension update depencies map
1236 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1238 // Remember it in the list of running updates
1239 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1240 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1243 // Getter for running updates
1244 function getExtensionUpdatesRunning () {
1245 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1248 // Checks wether the given extension registration is in progress
1249 function isExtensionRegistrationRunning ($ext_name) {
1251 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1254 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1258 // Init EXT_UPDATE_DEPENDS flag
1259 function initExtensionUpdateDependencies () {
1260 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1262 // Init update depency map automatically if not found
1263 if (isExtensionUpdateDependenciesInitialized()) {
1264 // We need these bug reports as well...
1265 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1268 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1270 // Init running updates array
1271 initExtensionRuningUpdates();
1273 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1276 // Adds an extension as "registration in progress"
1277 function addExtensionRunningRegistration ($ext_name) {
1279 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1280 if (isExtensionRegistrationRunning($ext_name)) {
1281 // This is really bad and should not be quietly ignored
1282 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1286 $GLOBALS['ext_register_running'][] = $ext_name;
1287 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1290 // Checks wether EXT_UPDATE_DEPENDS is initialized
1291 function isExtensionUpdateDependenciesInitialized () {
1292 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1293 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1296 // Checks wether an update is already running for given extension
1297 function isExtensionUpdateRunning ($ext_name) {
1298 // Current and given extensions means whole array
1299 if ($ext_name == getCurrentExtensionName()) {
1300 // Default is not found
1303 // Walk through whole array
1304 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1306 if (($ext1 == $ext_name) || ($isRunning === true)) {
1308 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1315 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1320 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1323 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1327 // Initializes the list of running updates
1328 function initExtensionRuningUpdates () {
1329 // Auto-init ext_running_updates
1330 if (!isset($GLOBALS['ext_running_updates'])) {
1331 $GLOBALS['ext_running_updates'] = array();
1332 $GLOBALS['ext_register_running'] = array();
1336 // Getter for EXT_UPDATE_DEPENDS flag
1337 function getExtensionUpdateDependencies () {
1338 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1339 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1342 // Getter for next iterator depency
1343 function getExtensionUpdateDependenciesIterator () {
1344 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1345 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1348 // Counter for extension update depencies
1349 function countExtensionUpdateDependencies () {
1350 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1351 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1354 // Removes given extension from update denpency list
1355 function removeExtensionUpdateDependency ($ext_name) {
1357 $key = array_search($ext_name, getExtensionUpdateDependencies());
1360 if ($key !== false) {
1362 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1364 // And sort the array
1365 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1369 // Init iterator for update depencies
1370 function initExtensionUpdateIterator () {
1371 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1372 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1375 // Getter for depency iterator
1376 function getExtensionUpdateIterator () {
1377 // Auto-init iterator
1378 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1381 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1382 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1385 // Increments the update iterator
1386 function incrementExtensionUpdateIterator () {
1387 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1388 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1391 // Setter for EXT_REPORTS_FAILURE flag
1392 function setExtensionReportsFailure ($reportsFailure) {
1393 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1396 // Getter for EXT_REPORTS_FAILURE flag
1397 function getExtensionReportsFailure () {
1398 return $GLOBALS['ext_reports_failure'];
1401 // Setter for EXT_VER_HISTORY flag
1402 function setExtensionVersionHistory ($verHistory) {
1403 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $verHistory;
1406 // Getter for EXT_VER_HISTORY array
1407 function getExtensionVersionHistory () {
1408 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1411 // Setter for EXT_UPDATE_NOTES
1412 function setExtensionUpdateNotes ($updateNotes, $ver='') {
1413 // . '/' . getCurrentExtensionVersion()
1414 //* DEBUG: */ debug_report_bug(__FUNCTION__.':' . getCurrentExtensionName() . '/' . getExtensionMode() . '/' . $ver . '=' . $updateNotes);
1416 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1418 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver] = (string) $updateNotes;
1422 // Getter for EXT_UPDATE_NOTES
1423 function getExtensionUpdateNotes ($ver) {
1424 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver];
1427 // Checks if ext_update_notes is set
1428 function isExtensionUpdateNoteSet ($ver) {
1429 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver]);
1432 // Init extension notice
1433 function initExtensionNotes () {
1434 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1437 // Append extension notice
1438 function appendExtensionNotes ($notes) {
1439 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1442 // Getter for extension notes
1443 function getExtensionNotes () {
1444 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1447 // Setter for current extension name
1448 function setCurrentExtensionName ($ext_name) {
1449 $GLOBALS['curr_EXTENSION_name'] = (string) trim($ext_name);
1452 // Getter for current extension name
1453 function getCurrentExtensionName () {
1454 if (isset($GLOBALS['curr_EXTENSION_name'])) {
1455 return $GLOBALS['curr_EXTENSION_name'];
1459 debug_report_bug(__FUNCTION__.": curr_EXTENSION_name not initialized. Please execute initExtensionSqls() before calling this function.");
1462 // Init SQLs array for current extension
1463 function initExtensionSqls ($force = false) {
1464 // Auto-init the array or if forced
1465 if ((!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) || ($force === true)) {
1467 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1469 // Initialize the generic array
1474 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1475 function addExtensionSql ($sql) {
1477 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1480 // Getter for SQLs array for current extension
1481 function getExtensionSqls () {
1482 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1483 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1484 // Not found, should not happen
1485 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1486 getCurrentExtensionName()
1491 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1494 // Removes SQLs for current extension
1495 function unsetExtensionSqls () {
1496 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1499 // Auto-initializes the removal list
1500 function initExtensionRemovalList () {
1501 // Is the remove list there?
1502 if (!isset($GLOBALS['ext_update_remove'])) {
1504 $GLOBALS['ext_update_remove'] = array();
1508 // Checks wether the current extension is on the removal list
1509 function isExtensionOnRemovalList () {
1510 // Init removal list
1511 initExtensionRemovalList();
1514 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1517 // Adds the current extension to the removal list
1518 function addCurrentExtensionToRemovalList () {
1520 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1523 // Getter for removal list
1524 function getExtensionRemovalList () {
1525 // Return the removal list
1526 return $GLOBALS['ext_update_remove'];
1529 // Redirects if the provided extension is not installed
1530 function redirectOnUninstalledExtension ($ext_name) {
1531 // So is the extension there?
1532 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1533 // Redirect to index
1534 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1538 // Filter for initialization of all extensions by loading them in 'init' mode
1539 function FILTER_INIT_EXTENSIONS () {
1540 // Do we have some entries?
1541 //* DEBUG */ print __FUNCTION__.': ENTRY!<br />';
1542 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1543 // Load all found extensions if found
1544 //* DEBUG */ print __FUNCTION__.': CACHE - START!<br />';
1545 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1547 //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - START<br />';
1548 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1549 //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - END<br />';
1551 //* DEBUG */ print __FUNCTION__.': CACHE - END!<br />';
1553 //* DEBUG */ print __FUNCTION__.': EXIT!<br />';
1556 // Setter for extension mode
1557 function setExtensionMode ($ext_mode) {
1558 $GLOBALS['ext_mode'] = (string) $ext_mode;
1561 // Getter for extension mode
1562 function getExtensionMode () {
1563 return $GLOBALS['ext_mode'];
1566 // Setter for dry-run
1567 function enableExtensionDryRun ($dry_run = true) {
1568 //* DEBUG: */ debugOutput(__FUNCTION__.': '.getCurrentExtensionName().'='.intval($dry_run));
1569 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1572 // Getter for dry-run
1573 function isExtensionDryRun () {
1574 return $GLOBALS['ext_dry_run'];
1577 // Setter for current extension version
1578 function setCurrentExtensionVersion ($ext_ver) {
1579 // ext_ver should never be empty in other modes than 'test'
1580 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1581 // Please report all these messages
1582 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1586 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1587 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1590 // Getter for current extension version
1591 function getCurrentExtensionVersion () {
1592 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1593 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1596 // Remove the extension from cache array
1597 function removeExtensionFromArray () {
1598 // "Cache" this name
1599 $ext_name = getCurrentExtensionName();
1601 // Now loop through the whole cache
1602 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1603 // Is it an element?
1604 if (isset($cacheArray[$ext_name])) {
1606 unset($cacheArray[$ext_name]);
1607 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1610 $key = array_search($ext_name, $cacheArray);
1613 if ($key !== false) {
1614 // Found, so remove it
1615 unset($cacheArray[$key]);
1616 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1622 // "Getter" for 'extension has a CSS file' (with same name, of course)
1623 function getExtensionHasCss () {
1624 // Default is no CSS
1627 // Construct FQFN for check
1628 $FQFN = sprintf("%stheme/%s/css/%s.css",
1631 getCurrentExtensionName()
1635 if (isFileReadable($FQFN)) {
1636 // Readable, so it is there...
1644 // Checks wether the given extension has a language file
1645 function ifExtensionHasLanguageFile ($ext_name) {
1646 // Default is no language file
1647 $hasLanguage = false;
1649 // Do we have cache?
1650 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1652 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1655 incrementStatsEntry('cache_hits');
1657 // Not readable is default
1660 // Is the language file readable for this extension?
1661 if (isLanguageIncludeReadable($ext_name)) {
1667 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
1671 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1674 // Load an extension's include file
1675 function loadExtensionInclude () {
1677 if (!isExtensionIncludeReadable()) {
1679 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1682 // Generate INC name
1683 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1689 // Checks wether an extension is readable
1690 function isExtensionIncludeReadable ($ext_name = '') {
1691 // If empty, use current
1692 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1695 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1696 // Generate INC name
1697 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1700 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1704 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1705 return $GLOBALS['ext_inc_readable'][$ext_name];
1708 // Checks if an extension's function file is readable
1709 function isExtensionFunctionFileReadable ($ext_name) {
1710 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1711 // Just count cache hits
1712 incrementStatsEntry('cache_hits');
1714 // Construct FQFN for functions file
1715 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1717 // Is this include there?
1718 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1720 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1723 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1728 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1731 // Adds an admin menu to the SQL queue of the menu entry is not found
1732 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1733 // Now check if this menu is there
1734 if (!isMenuActionValid('admin', $action, $what)) {
1736 if (is_null($what)) {
1738 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1746 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1755 // Add it to the queue
1756 addExtensionSql($sql);
1757 } elseif (isDebugModeEnabled()) {
1758 // Double menus should be located and fixed!
1759 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1763 // Adds a guest menu to the SQL queue if the menu entry is not found
1764 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1765 // Now check if this menu is there
1766 if (!isMenuActionValid('guest', $action, $what)) {
1768 if (is_null($what)) {
1770 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1779 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1789 // Add it to the queue
1790 addExtensionSql($sql);
1791 } elseif (isDebugModeEnabled()) {
1792 // Double menus should be located and fixed!
1793 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1797 // Adds a member menu to the SQL queue if the menu entry is not found
1798 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1799 // Now check if this menu is there
1800 if (!isMenuActionValid('member', $action, $what)) {
1802 if (is_null($what)) {
1804 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1813 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1823 // Add it to the queue
1824 addExtensionSql($sql);
1825 } elseif (isDebugModeEnabled()) {
1826 // Double menus should be located and fixed!
1827 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1831 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1832 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1833 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1834 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1836 if (is_null($what)) {
1838 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1846 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1855 // Add it to the queue
1856 addExtensionSql($sql);
1857 } elseif (isDebugModeEnabled()) {
1858 // Double menus should be located and fixed!
1859 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1863 // Enables/disables productive mode for current extension (used only while
1865 function enableExtensionProductive ($isProductive = true) {
1866 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1869 // Checks wether the extension is in productive phase. If not set, development
1870 // phase (=false) is assumed.
1871 function isExtensionProductive () {
1872 return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));