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, $task_id, $dry_run = false, $logout = true) {
170 // Set current extension name
171 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',task_id=' . $task_id . ',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 ($task_id == '0') {
204 // Try to find the task
205 $task_id = determineExtensionTaskId(getCurrentExtensionName());
207 // Still zero and not in dry-run?
208 if (($task_id == '0') && (!getExtensionDryRun())) {
209 // Then request a bug report
210 debug_report_bug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
212 getCurrentExtensionName()
217 // Init queries and notes
219 initExtensionNotes();
224 initIncludePool('extension');
226 // By default we have no failures
227 setExtensionReportsFailure(false);
229 // Does this extension exists?
230 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
231 if (loadExtension(getCurrentExtensionName(), 'register', '', getExtensionDryRun())) {
232 // Set current extension name again
233 setCurrentExtensionName($ext_name);
235 // And run possible updates
236 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
237 $history = getExtensionVersionHistory();
238 foreach ($history as $ver) {
239 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ver);
240 // Load extension in update mode
241 loadExtension(getCurrentExtensionName(), 'update', $ver, getExtensionDryRun());
243 // Add update notes to our output
244 addExtensionNotes($ver);
247 // Does this extension depends on an outstanding update of another update?
248 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
250 $ext_update = getExtensionUpdateDependenciesIterator();
252 // Increment here to avoid endless loop
253 incrementExtensionUpdateIterator();
255 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
256 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
257 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', getExtensionDryRun()))) {
258 // Set current extension name again
259 setCurrentExtensionName($ext_name);
261 // If versions mismatch update extension first
263 if (isExtensionInstalled($ext_update)) {
264 // Get version only if installed
265 $ext_ver = getExtensionVersion($ext_update);
268 // Extension version set? If empty the extension is not registered
269 if (empty($ext_ver)) {
270 // Extension not registered so far so first load task's id...
271 $task = determineExtensionTaskId($ext_update);
275 // Try to register the extension
276 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ':ext_update=' . $ext_update . ',taskId=' . $task);
277 $test = registerExtension($ext_update, $task, getExtensionDryRun(), false);
279 // Reset extension name
280 setCurrentExtensionName($ext_name);
281 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
282 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
284 } elseif ($ext_ver != getCurrentExtensionVersion()) {
285 // Ok, update this extension now
286 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
287 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
288 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
289 updateExtension($ext_update, $ext_ver, getExtensionDryRun());
290 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
291 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
292 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
293 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
294 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
299 // Nothing to register / update before...
303 // Required file for update does not exists!
305 // But this is fine for the first time...
308 // Restore the current extension name
309 setCurrentExtensionName($ext_name);
312 // Is there no update?
313 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
314 // Then test is passed!
318 // Switch back to register mode
319 setExtensionMode('register');
321 // Remains true if extension registration reports no failures
322 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
323 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
324 $test = (($test === true) && (getExtensionReportsFailure() === false));
325 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
326 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>');
328 // Does everthing before wents ok?
329 if ($test === true) {
330 // "Dry-run-mode" activated?
331 if ((getExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
332 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
333 // Init SQLs and transfer ext->generic
335 setSqlsArray(getExtensionSqls());
337 // Run installation pre-installation filters
338 runFilterChain('pre_extension_installed', array('dry_run' => getExtensionDryRun()));
340 // Register extension
341 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
342 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
343 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
345 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
347 getCurrentExtensionName(),
348 getExtensionAlwaysActive(),
349 getCurrentExtensionVersion(),
351 ), __FUNCTION__, __LINE__);
353 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
355 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
357 getCurrentExtensionName(),
358 getExtensionAlwaysActive(),
359 getCurrentExtensionVersion()
360 ), __FUNCTION__, __LINE__);
363 // Remove cache file(s) if extension is active
364 runFilterChain('post_extension_installed', array(
365 'pool' => 'extension',
366 'ext_name' => getCurrentExtensionName(),
367 'task_id' => $task_id
370 // Re-init queries and notes
371 initExtensionSqls(true);
372 initExtensionNotes();
374 // Mark it as installed
375 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
377 // In normal mode return a true on success
379 } elseif (getExtensionDryRun() === true) {
380 // In "dry-run" mode do always return a true
383 // Re-init queries and notes
384 initExtensionNotes();
386 // Extension has been removed for updates, so all is fine!
390 // No, an error occurs while registering extension :-(
391 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
394 } elseif (($task_id > 0) && (getCurrentExtensionName() != '')) {
395 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
396 // Remove task from system when id and extension's name is valid
397 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
398 array(bigintval($task_id)), __FUNCTION__, __LINE__);
401 // Is this the sql_patches?
402 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
403 if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!getExtensionDryRun()) && ($test)) {
404 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
405 if ($logout === true) {
406 // Then redirect to logout
407 redirectToUrl('modules.php?module=admin&logout=1&' . getExtensionMode() . '=sql_patches');
409 // Add temporary filter
410 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
411 $GLOBALS['ext_load_mode'] = getExtensionMode();
415 // Return status code
416 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ' - EXIT!');
417 //* DEBUG: */ debugOutput(getCurrentExtensionName().':<pre>'.print_r($ret, true).'</pre>');
421 // Run SQL queries for given extension id
422 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
423 function doExtensionSqls ($ext_id, $load_mode) {
424 // This shall never do a non-admin user!
425 if (!isAdmin()) return false;
427 // Get extension's name
428 $ext_name = getExtensionName($ext_id);
430 // Set current SQL name
431 setCurrentExtensionName($ext_name);
433 // Init EXT_UPDATE_DEPENDS
434 if (!isExtensionUpdateDependenciesInitialized()) {
436 initExtensionUpdateDependencies();
442 // By default no SQL has been executed
445 // Load extension in detected mode
446 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]=".getCurrentExtensionName()."");
447 loadExtension(getCurrentExtensionName(), $load_mode, '', false);
451 setSqlsArray(getExtensionSqls());
453 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":SQLs::count=".countSqls()."");
455 // Run SQL commands...
456 runFilterChain('run_sqls');
459 // Run any filters depending on the action here
460 runFilterChain('extension_' . $load_mode);
462 // Remove cache file(s) if extension is active
463 if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
465 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
468 // Is this the sql_patches?
469 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": id=".$ext_id.",currName=".getCurrentExtensionName().",loadMode=".$load_mode);
470 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
471 // Then redirect to logout
472 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": LOAD!");
473 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
477 // Check wether the given extension is installed
478 function isExtensionInstalled ($ext_name) {
479 // We don't like empty extension names here
480 if (empty($ext_name)) {
481 // Please fix them all
482 debug_report_bug(__FUNCTION__.': ext_name is empty.');
485 // By default non is installed
486 $isInstalled = false;
488 // Check if there is a cache entry
489 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
490 // Use cache built from below queries
491 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
492 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
497 incrementStatsEntry('cache_hits');
498 } elseif ((isInstallationPhase())) {
499 // Extensions are all inactive/not installed during installation
502 $ext_id = getExtensionId($ext_name);
504 // Do we have a record?
505 $isInstalled = ($ext_id > 0);
507 // Is it installed, then cache the entry
508 if ($isInstalled === true) {
509 // Dummy call (get is okay here)
510 getExtensionId($ext_name, true);
513 // Remember the status
514 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
521 // Check if given extension is active
522 function isExtensionActive ($ext_name) {
523 // Extensions are all inactive during installation
524 if ((isInstallationPhase()) || (empty($ext_name))) return false;
526 // Not active is the default
527 $data['ext_active'] = 'N';
530 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
532 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
533 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
536 incrementStatsEntry('cache_hits');
537 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
538 // @TODO Extension is loaded, what next?
539 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
540 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
541 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
542 // Load from database
543 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
544 array($ext_name), __FUNCTION__, __LINE__);
547 if (SQL_NUMROWS($result) == 1) {
549 $data = SQL_FETCHARRAY($result);
553 SQL_FREERESULT($result);
556 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: ".$data['ext_active']."");
557 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
559 // Extension not active!
560 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
561 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
565 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.",active=".$data['ext_active']."");
567 // Is this extension activated? (For admins we always have active extensions...)
568 return ($data['ext_active'] == 'Y');
571 // Get version from extensions
572 function getExtensionVersion ($ext_name, $force = false) {
573 // By default no extension is found
574 $data['ext_version'] = 'false';
576 // Empty extension name should be fixed!
577 if (empty($ext_name)) {
578 // Please report this bug!
579 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
582 // Extensions are all inactive during installation
583 if (isInstallationPhase()) return '';
584 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ext_name=".$ext_name."");
586 // Is the cache written?
587 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
588 // Load data from cache
589 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
590 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
593 incrementStatsEntry('cache_hits');
594 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getOutputMode() != 0)) {
595 // Load from database
596 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
597 array($ext_name), __FUNCTION__, __LINE__);
598 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
600 // Is the extension there?
601 if (SQL_NUMROWS($result) == 1) {
603 $data = SQL_FETCHARRAY($result);
604 } elseif (isDebugModeEnabled()) {
605 // Not found, please report all
606 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
610 SQL_FREERESULT($result);
613 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
616 // Extension version should not be invalid
617 if (($data['ext_version'] == 'false') && ($force === false)) {
618 // Please report this trouble
619 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
623 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ret={$data['ext_version']}");
624 return $data['ext_version'];
627 // Updates a given extension with current extension version to latest version
628 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
629 // Only admins are allowed to update extensions
630 if ((!isAdmin()) || (empty($ext_name))) return false;
632 // Set current SQL name
633 setCurrentExtensionName($ext_name);
637 initExtensionNotes();
638 initIncludePool('extension');
640 // Load extension in test mode
641 loadExtension($ext_name, 'test', $ext_ver, getExtensionDryRun());
643 // Save version history
644 $history = getExtensionVersionHistory();
646 // Remove old SQLs array to prevent possible bugs
649 // Check if version is updated
650 //* DEBUG: */ debugOutput(getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)));
651 if (((getThisExtensionVersion() != $ext_ver) || (getExtensionDryRun())) && (is_array($history))) {
652 // Search for starting point
653 $start = array_search($ext_ver, $history);
655 // And load SQL queries in order of version history
656 for ($idx = ($start + 1); $idx < count($history); $idx++) {
657 // Set extension version
658 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
661 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], getExtensionDryRun());
664 $depencies = getExtensionUpdateDependencies();
667 if (count($depencies) > 0) {
668 // Apply all extension depencies
669 foreach ($depencies as $ext_depend) {
670 // Did we already update/register this?
671 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
673 setCurrentExtensionName($ext_depend);
675 // Mark it as already updated before we update it
676 $GLOBALS['ext_updated'][$ext_depend] = true;
678 // Is the extension there?
679 if (isExtensionInstalled($ext_depend)) {
680 // Update another extension first!
681 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), getExtensionDryRun());
683 // Register new extension
684 $test = registerExtension($ext_depend, 0, getExtensionDryRun(), false);
689 // Set extension version here
690 setCurrentExtensionVersion($ext_ver);
693 setCurrentExtensionName($ext_name);
697 addExtensionNotes($history[$idx]);
700 // In real-mode execute any existing includes
701 if (getExtensionDryRun() === false) {
702 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
703 runFilterChain('load_includes', 'extension');
704 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
705 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
710 setSqlsArray(getExtensionSqls());
713 runFilterChain('run_sqls', array('dry_run' => getExtensionDryRun()));
715 if (getExtensionDryRun() === false) {
716 // Run filters on success extension update
717 runFilterChain('extension_update', getCurrentExtensionName());
722 // Output verbose SQL table for extension
723 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
726 // Then fix it to default
727 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
733 // Do we have queries?
734 if ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
739 foreach (getExtensionSqls() as $sqls) {
740 // New array format is recursive
741 foreach ($sqls as $idx => $sql) {
745 // Output command if set
747 // Prepare output for template
755 $OUT .= loadTemplate('admin_ext_sql_row', true, $content);
763 // Prepare content for template
771 // Load main template
772 $OUT = loadTemplate('admin_ext_sql_table', true, $content);
773 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
774 // No addional SQL commands to run
775 $OUT = loadTemplate('admin_settings_saved', true, '{--ADMIN_NO_ADDITIONAL_SQLS--}');
782 // Get extension name from id
783 function getExtensionName ($ext_id) {
784 // Init extension name
785 $data['ext_name'] = '';
788 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
790 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
793 incrementStatsEntry('cache_hits');
794 } elseif (!isExtensionActive('cache')) {
795 // Load from database
796 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
797 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
799 // Is the entry there?
800 if (SQL_NUMROWS($result) == 1) {
801 // Get the extension's name from database
802 $data = SQL_FETCHARRAY($result);
806 SQL_FREERESULT($result);
809 // Did we find some extension?
810 if (empty($data['ext_name'])) {
811 // We should fix these all!
812 debug_report_bug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
815 // Return the extension name
816 return $data['ext_name'];
819 // Get extension id from name
820 function getExtensionId ($ext_name) {
822 $data['ext_id'] = '0';
825 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
827 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
830 incrementStatsEntry('cache_hits');
832 // Load from database
833 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
834 array($ext_name), __FUNCTION__, __LINE__);
836 // Is the entry there?
837 if (SQL_NUMROWS($result) == 1) {
838 // Get the extension's id from database
839 $data = SQL_FETCHARRAY($result);
843 SQL_FREERESULT($result);
846 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
850 return $data['ext_id'];
853 // Determines wether the given extension name is valid
854 function isExtensionNameValid ($ext_name) {
856 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
857 // Generate include file name
858 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
860 // Is there a file in inc/extensions/ ?
861 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
865 return $GLOBALS['ext_name_valid'][$ext_name];
868 // Determines wether the given extension id is valid
869 function isExtensionIdValid ($ext_id) {
870 // Default is nothing valid
873 // Check in cache then in database
874 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
879 incrementStatsEntry('cache_hits');
882 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
889 // Activate given extension
890 function doActivateExtension ($ext_name) {
891 // Activate the extension
892 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
893 array($ext_name), __FUNCTION__, __LINE__);
895 // Extension has been activated?
896 if (SQL_AFFECTEDROWS() == 1) {
897 // Then run all queries
898 doExtensionSqls(getExtensionId($ext_name), 'activate');
902 // Deactivate given extension
903 function doDeactivateExtension($ext_name) {
904 // Activate the extension
905 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
906 array($ext_name), __FUNCTION__, __LINE__);
908 // Extension has been activated?
909 if (SQL_AFFECTEDROWS() == 1) {
910 // Then run all queries
911 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
914 createExtensionDeactivationTask($ext_name);
917 sendAdminNotification(
918 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
919 'admin_ext_deactivated',
920 array('ext_name' => $ext_name)
925 // Checks wether the extension is older than given
926 function isExtensionOlder ($ext_name, $ext_ver) {
927 // Get current extension version
928 $currVersion = getExtensionVersion($ext_name);
930 // Remove all dots from both versions
931 $currVersion = str_replace('.', '', $currVersion);
932 $ext_ver = str_replace('.', '', $ext_ver);
934 // Now compare both and return the result
935 return ($currVersion < $ext_ver);
938 // Creates a new task for updated extension
939 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
940 // Create subject line
941 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJ--}';
943 // Is the extension there?
944 if (isExtensionInstalled($ext_name)) {
945 // Check if task is not there
946 if (determineTaskIdBySubject($subject) == '0') {
947 // Create extension update-task
948 createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
951 // Extension not there! :-(
952 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
956 // Creates a new task for newly installed extension
957 function createNewExtensionTask ($adminId, $subject, $ext) {
958 // Not installed and do we have created a task for the admin?
959 if ((determineTaskIdBySubject($subject) == '0') && (!isExtensionInstalled($ext))) {
960 // Set default message if ext-foo is missing
961 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext);
964 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
970 // Load text for task if found
971 if (isFileReadable($tpl)) {
972 // Load extension's own text template (HTML!)
973 $message = loadTemplate('ext_' . $ext, true);
975 // Write this in debug.log as well
976 logDebugMessage(__FUNCTION__, __LINE__, $message);
979 // Task not created so it's a brand-new extension which we need to register and create a task for!
980 createNewTask($subject, $message, 'EXTENSION', 0, $adminId, false);
984 // Creates a task for automatically deactivated (deprecated) extension
985 function createExtensionDeactivationTask ($ext) {
986 // Create subject line
987 $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
989 // Not installed and do we have created a task for the admin?
990 if ((determineTaskIdBySubject($subject) == '0') && (isExtensionInstalled($ext))) {
991 // Task not created so add it
992 createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext)), 'EXTENSION_DEACTIVATION');
996 // Checks if the module has a menu
997 function ifModuleHasMenu ($mod, $forceDb = false) {
998 // All is false by default
1001 // Extension installed and newer than or has version 0.1.2?
1002 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
1003 // Cache version is okay, so let's check the cache!
1004 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
1005 // Check module cache and count hit
1006 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
1007 incrementStatsEntry('cache_hits');
1008 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
1009 // Check cache and count hit
1010 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
1011 incrementStatsEntry('cache_hits');
1013 // Admin/guest/member/sponsor modules have always a menu!
1014 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
1016 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
1017 // Check database for entry
1018 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
1019 array($mod), __FUNCTION__, __LINE__);
1022 if (SQL_NUMROWS($result) == 1) {
1023 // Load "has_menu" column
1024 $data = SQL_FETCHARRAY($result);
1026 // Fake cache... ;-)
1027 $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
1029 // Does it have a menu?
1030 $ret = ($data['has_menu'] == 'Y');
1034 SQL_FREERESULT($result);
1035 } elseif (!isExtensionInstalled('sql_patches')) {
1036 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
1037 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
1039 // Unsupported state!
1040 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
1047 // Determines the task id for given extension
1048 function determineExtensionTaskId ($ext_name) {
1049 // Default is not found
1050 $data['task_id'] = '0';
1052 // Search for extension task's id
1053 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1054 array($ext_name), __FUNCTION__, __LINE__);
1057 if (SQL_NUMROWS($result) == 1) {
1058 // Task found so load task's id and register extension...
1059 $data = SQL_FETCHARRAY($result);
1063 SQL_FREERESULT($result);
1066 return $data['task_id'];
1069 // Determines the task id for given subject
1070 function determineTaskIdBySubject ($subject) {
1071 // Default is not found
1072 $data['task_id'] = '0';
1074 // Search for task id
1075 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1076 array($subject), __FUNCTION__, __LINE__);
1079 if (SQL_NUMROWS($result) == 1) {
1080 // Task found so load task's id and register extension...
1081 $data = SQL_FETCHARRAY($result);
1085 SQL_FREERESULT($result);
1088 return $data['task_id'];
1091 // Add updates notes for given version
1092 function addExtensionNotes ($ver) {
1093 // Init notes/content
1094 $out = ''; $content = array();
1096 // Is do we have verbose output enabled?
1097 if ((!isExtensionActive('sql_patches')) || (getConfig('verbose_sql') == 'Y')) {
1098 // Update notes found?
1099 if (isExtensionUpdateNoteSet($ver)) {
1100 // Update notes found
1103 'notes' => getExtensionUpdateNotes($ver)
1107 setExtensionUpdateNotes('', $ver);
1108 } elseif (($ver == '0.0') || ($ver == '0.0.0')) {
1112 'notes' => getMessage('INITIAL_RELEASE')
1115 // No update notes found!
1118 'notes' => getMessage('NO_UPDATE_NOTES')
1123 $out = loadTemplate('admin_EXTENSION_notes', true, $content);
1127 appendExtensionNotes($out);
1130 // Getter for CSS files array
1131 function getExtensionCssFiles () {
1132 // By default no additional CSS files are found
1133 $cssFiles = array();
1135 // Is the array there?
1136 if (isset($GLOBALS['css_files'])) {
1138 $cssFiles = $GLOBALS['css_files'];
1145 // Init CSS files array
1146 function initExtensionCssFiles () {
1148 $GLOBALS['css_files'] = array();
1152 function addExtensionCssFile ($file) {
1153 // Is the array there?
1154 if (!isset($GLOBALS['css_files'])) {
1155 // Then auto-init them
1156 initExtensionCssFiles();
1160 $GLOBALS['css_files'][] = $file;
1163 // Setter for EXT_ALWAYS_ACTIVE flag
1164 function setExtensionAlwaysActive ($active) {
1165 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1168 // Getter for EXT_ALWAYS_ACTIVE flag
1169 function getExtensionAlwaysActive () {
1170 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1173 // Checks wether the current extension is always active
1174 function isExtensionAlwaysActive () {
1175 return (getExtensionAlwaysActive() == 'Y');
1178 // Setter for EXT_VERSION flag
1179 function setThisExtensionVersion ($version) {
1180 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $version;
1183 // Getter for EXT_VERSION flag
1184 function getThisExtensionVersion () {
1185 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1188 // Setter for EXT_DEPRECATED flag
1189 function setExtensionDeprecated ($deprecated) {
1190 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1193 // Getter for EXT_DEPRECATED flag
1194 function isExtensionDeprecated () {
1195 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1198 // Setter for EXT_UPDATE_DEPENDS flag
1199 function addExtensionUpdateDependency ($updateDepends) {
1200 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1201 // Is the update depency empty? (NEED TO BE FIXED!)
1202 if (empty($updateDepends)) {
1203 // Please report this bug!
1204 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1207 // Is it not yet added?
1209 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1210 // Double-adding isn't fine, too
1211 debug_report_bug(__FUNCTION__, __LINE__, '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1215 // Add it to the list of extension update depencies map
1216 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1218 // Remember it in the list of running updates
1219 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1220 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1223 // Getter for running updates
1224 function getExtensionUpdatesRunning () {
1225 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1228 // Checks wether the given extension registration is in progress
1229 function isExtensionRegistrationRunning ($ext_name) {
1231 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1234 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1238 // Init EXT_UPDATE_DEPENDS flag
1239 function initExtensionUpdateDependencies () {
1240 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1242 // Init update depency map automatically if not found
1243 if (isExtensionUpdateDependenciesInitialized()) {
1244 // We need these bug reports as well...
1245 debug_report_bug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1248 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1250 // Init running updates array
1251 initExtensionRuningUpdates();
1253 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1256 // Adds an extension as "registration in progress"
1257 function addExtensionRunningRegistration ($ext_name) {
1259 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1260 if (isExtensionRegistrationRunning($ext_name)) {
1261 // This is really bad and should not be quietly ignored
1262 debug_report_bug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1266 $GLOBALS['ext_register_running'][] = $ext_name;
1267 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1270 // Checks wether EXT_UPDATE_DEPENDS is initialized
1271 function isExtensionUpdateDependenciesInitialized () {
1272 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1273 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1276 // Checks wether an update is already running for given extension
1277 function isExtensionUpdateRunning ($ext_name) {
1278 // Current and given extensions means whole array
1279 if ($ext_name == getCurrentExtensionName()) {
1280 // Default is not found
1283 // Walk through whole array
1284 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1286 if (($ext1 == $ext_name) || ($isRunning === true)) {
1288 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1295 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1300 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1303 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1307 // Initializes the list of running updates
1308 function initExtensionRuningUpdates () {
1309 // Auto-init ext_running_updates
1310 if (!isset($GLOBALS['ext_running_updates'])) {
1311 $GLOBALS['ext_running_updates'] = array();
1312 $GLOBALS['ext_register_running'] = array();
1316 // Getter for EXT_UPDATE_DEPENDS flag
1317 function getExtensionUpdateDependencies () {
1318 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1319 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1322 // Getter for next iterator depency
1323 function getExtensionUpdateDependenciesIterator () {
1324 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1325 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1328 // Counter for extension update depencies
1329 function countExtensionUpdateDependencies () {
1330 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1331 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1334 // Removes given extension from update denpency list
1335 function removeExtensionUpdateDependency ($ext_name) {
1337 $key = array_search($ext_name, getExtensionUpdateDependencies());
1340 if ($key !== false) {
1342 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1344 // And sort the array
1345 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1349 // Init iterator for update depencies
1350 function initExtensionUpdateIterator () {
1351 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1352 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1355 // Getter for depency iterator
1356 function getExtensionUpdateIterator () {
1357 // Auto-init iterator
1358 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1361 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1362 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1365 // Increments the update iterator
1366 function incrementExtensionUpdateIterator () {
1367 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1368 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1371 // Setter for EXT_REPORTS_FAILURE flag
1372 function setExtensionReportsFailure ($reportsFailure) {
1373 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1376 // Getter for EXT_REPORTS_FAILURE flag
1377 function getExtensionReportsFailure () {
1378 return $GLOBALS['ext_reports_failure'];
1381 // Setter for EXT_VER_HISTORY flag
1382 function setExtensionVersionHistory ($verHistory) {
1383 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $verHistory;
1386 // Getter for EXT_VER_HISTORY array
1387 function getExtensionVersionHistory () {
1388 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1391 // Setter for EXT_UPDATE_NOTES
1392 function setExtensionUpdateNotes ($updateNotes, $ver='') {
1393 // . '/' . getCurrentExtensionVersion()
1394 //* DEBUG: */ debug_report_bug(__FUNCTION__.':' . getCurrentExtensionName() . '/' . getExtensionMode() . '/' . $ver . '=' . $updateNotes);
1396 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1398 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver] = (string) $updateNotes;
1402 // Getter for EXT_UPDATE_NOTES
1403 function getExtensionUpdateNotes ($ver) {
1404 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver];
1407 // Checks if ext_update_notes is set
1408 function isExtensionUpdateNoteSet ($ver) {
1409 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver]);
1412 // Init extension notice
1413 function initExtensionNotes () {
1414 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1417 // Append extension notice
1418 function appendExtensionNotes ($notes) {
1419 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1422 // Getter for extension notes
1423 function getExtensionNotes () {
1424 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1427 // Setter for current extension name
1428 function setCurrentExtensionName ($ext_name) {
1429 $GLOBALS['curr_EXTENSION_name'] = (string) trim($ext_name);
1432 // Getter for current extension name
1433 function getCurrentExtensionName () {
1434 if (isset($GLOBALS['curr_EXTENSION_name'])) {
1435 return $GLOBALS['curr_EXTENSION_name'];
1439 debug_report_bug(__FUNCTION__.": curr_EXTENSION_name not initialized. Please execute initExtensionSqls() before calling this function.");
1442 // Init SQLs array for current extension
1443 function initExtensionSqls ($force = false) {
1444 // Auto-init the array or if forced
1445 if ((!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) || ($force === true)) {
1447 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1449 // Initialize the generic array
1454 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1455 function addExtensionSql ($sql) {
1457 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1460 // Getter for SQLs array for current extension
1461 function getExtensionSqls () {
1462 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1463 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1464 // Not found, should not happen
1465 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1466 getCurrentExtensionName()
1471 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1474 // Removes SQLs for current extension
1475 function unsetExtensionSqls () {
1476 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1479 // Auto-initializes the removal list
1480 function initExtensionRemovalList () {
1481 // Is the remove list there?
1482 if (!isset($GLOBALS['ext_update_remove'])) {
1484 $GLOBALS['ext_update_remove'] = array();
1488 // Checks wether the current extension is on the removal list
1489 function isExtensionOnRemovalList () {
1490 // Init removal list
1491 initExtensionRemovalList();
1494 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1497 // Adds the current extension to the removal list
1498 function addCurrentExtensionToRemovalList () {
1500 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1503 // Getter for removal list
1504 function getExtensionRemovalList () {
1505 // Return the removal list
1506 return $GLOBALS['ext_update_remove'];
1509 // Redirects if the provided extension is not installed
1510 function redirectOnUninstalledExtension ($ext_name) {
1511 // So is the extension there?
1512 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1513 // Redirect to index
1514 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1518 // Filter for initialization of all extensions by loading them in 'init' mode
1519 function FILTER_INIT_EXTENSIONS () {
1520 // Do we have some entries?
1521 //* DEBUG */ print __FUNCTION__.': ENTRY!<br />';
1522 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1523 // Load all found extensions if found
1524 //* DEBUG */ print __FUNCTION__.': CACHE - START!<br />';
1525 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1527 //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - START<br />';
1528 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1529 //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - END<br />';
1531 //* DEBUG */ print __FUNCTION__.': CACHE - END!<br />';
1533 //* DEBUG */ print __FUNCTION__.': EXIT!<br />';
1536 // Setter for extension mode
1537 function setExtensionMode ($ext_mode) {
1538 $GLOBALS['ext_mode'] = (string) $ext_mode;
1541 // Getter for extension mode
1542 function getExtensionMode () {
1543 return $GLOBALS['ext_mode'];
1546 // Setter for dry-run
1547 function enableExtensionDryRun ($dry_run = true) {
1548 //* DEBUG: */ debugOutput(__FUNCTION__.': '.getCurrentExtensionName().'='.intval($dry_run));
1549 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1552 // Getter for dry-run
1553 function getExtensionDryRun () {
1554 return $GLOBALS['ext_dry_run'];
1557 // Setter for current extension version
1558 function setCurrentExtensionVersion ($ext_ver) {
1559 // ext_ver should never be empty in other modes than 'test'
1560 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1561 // Please report all these messages
1562 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1566 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1567 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1570 // Getter for current extension version
1571 function getCurrentExtensionVersion () {
1572 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1573 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1576 // Remove the extension from cache array
1577 function removeExtensionFromArray () {
1578 // "Cache" this name
1579 $ext_name = getCurrentExtensionName();
1581 // Now loop through the whole cache
1582 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1583 // Is it an element?
1584 if (isset($cacheArray[$ext_name])) {
1586 unset($cacheArray[$ext_name]);
1587 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1590 $key = array_search($ext_name, $cacheArray);
1593 if ($key !== false) {
1594 // Found, so remove it
1595 unset($cacheArray[$key]);
1596 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1602 // "Getter" for 'extension has a CSS file' (with same name, of course)
1603 function getExtensionHasCss () {
1604 // Default is no CSS
1607 // Construct FQFN for check
1608 $FQFN = sprintf("%stheme/%s/css/%s.css",
1611 getCurrentExtensionName()
1615 if (isFileReadable($FQFN)) {
1616 // Readable, so it is there...
1624 // Checks wether the given extension has a language file
1625 function ifExtensionHasLanguageFile ($ext_name) {
1626 // Default is no language file
1627 $hasLanguage = false;
1629 // Do we have cache?
1630 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1632 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1635 incrementStatsEntry('cache_hits');
1637 // Not readable is default
1640 // Is the language file readable for this extension?
1641 if (isLanguageIncludeReadable($ext_name)) {
1647 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
1651 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1654 // Load an extension's include file
1655 function loadExtensionInclude () {
1657 if (!isExtensionIncludeReadable()) {
1659 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1662 // Generate INC name
1663 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1669 // Checks wether an extension is readable
1670 function isExtensionIncludeReadable ($ext_name = '') {
1671 // If empty, use current
1672 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1675 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1676 // Generate INC name
1677 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1680 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1684 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1685 return $GLOBALS['ext_inc_readable'][$ext_name];
1688 // Checks if an extension's function file is readable
1689 function isExtensionFunctionFileReadable ($ext_name) {
1690 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1691 // Just count cache hits
1692 incrementStatsEntry('cache_hits');
1694 // Construct FQFN 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 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1703 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1708 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1711 // Adds an admin menu to the SQL queue of the menu entry is not found
1712 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1713 // Now check if this menu is there
1714 if (!isMenuActionValid('admin', $action, $what)) {
1716 if (is_null($what)) {
1718 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1726 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1735 // Add it to the queue
1736 addExtensionSql($sql);
1737 } elseif (isDebugModeEnabled()) {
1738 // Double menus should be located and fixed!
1739 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1743 // Adds a guest menu to the SQL queue if the menu entry is not found
1744 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1745 // Now check if this menu is there
1746 if (!isMenuActionValid('guest', $action, $what)) {
1748 if (is_null($what)) {
1750 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1759 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1769 // Add it to the queue
1770 addExtensionSql($sql);
1771 } elseif (isDebugModeEnabled()) {
1772 // Double menus should be located and fixed!
1773 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1777 // Adds a member menu to the SQL queue if the menu entry is not found
1778 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1779 // Now check if this menu is there
1780 if (!isMenuActionValid('member', $action, $what)) {
1782 if (is_null($what)) {
1784 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1793 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1803 // Add it to the queue
1804 addExtensionSql($sql);
1805 } elseif (isDebugModeEnabled()) {
1806 // Double menus should be located and fixed!
1807 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1811 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1812 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1813 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1814 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1816 if (is_null($what)) {
1818 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1826 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1835 // Add it to the queue
1836 addExtensionSql($sql);
1837 } elseif (isDebugModeEnabled()) {
1838 // Double menus should be located and fixed!
1839 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1843 // Enables/disables productive mode for current extension (used only while
1845 function enableExtensionProductive ($isProductive = true) {
1846 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1849 // Checks wether the extension is in productive phase. If not set, development
1850 // phase (=false) is assumed.
1851 function isExtensionProductive () {
1852 return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));