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 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2013 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Load the extension and maybe found language and function files.
44 function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0.0', $isDryRun = FALSE, $previousExtension = NULL) {
45 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_mode=' . $ext_mode . ',ext_ver=' . $ext_ver . ',isDryRun=' . intval($isDryRun) . ' - ENTERED!');
46 // Loading an extension in same mode, but not test/update, twice is not
47 // good, so is the extension $ext_name already loaded in mode $ext_mode?
48 if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
49 // If this happens twice, we need the bug report from you, except for updates/tests
50 reportBug(__FUNCTION__, __LINE__, __FUNCTION__ . '() 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));
53 // Make sure this situation can only happen once
54 $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = TRUE;
56 // Remember previous extension here
57 $GLOBALS['previous_extension'][$ext_name] = $previousExtension;
60 setExtensionMode($ext_mode);
62 // Set current extension name
63 setCurrentExtensionName($ext_name);
65 // By default all extensions are in productive phase
66 enableExtensionProductive();
68 if (!empty($ext_ver)) {
69 // Set current extension version
70 setCurrentExtensionVersion($ext_ver);
72 // Set it to 0.0 by default
73 setCurrentExtensionVersion('0.0.0');
75 // Is the extension installed?
76 if ((isExtensionInstalled($ext_name)) && ($ext_mode != 'setup')) {
77 // Get extension's version
78 setCurrentExtensionVersion(getExtensionVersion($ext_name));
81 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
82 if (($ext_mode != 'test') && (getCurrentExtensionVersion() == '0.0.0')) {
83 // Abort here, this must now always be set!
84 reportBug(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0.0 ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', isDryRun=' . intval($isDryRun));
89 enableExtensionDryRun($isDryRun);
92 initIncludePool('extension');
94 // Init EXT_UPDATE_DEPENDS if not yet done
95 if (!isExtensionUpdateDependenciesInitialized()) {
97 initExtensionUpdateDependencies();
100 // Init current extension name list
103 // Is the extension already loaded?
104 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', getExtensionMode()=' . getExtensionMode() . ', getCurrentExtensionVersion()=' . getCurrentExtensionVersion());
105 if ((isExtensionLoaded($ext_name)) && (getExtensionMode() == 'init')) {
107 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
113 // Is the extension file NOT there?
114 if (!isExtensionNameValid($ext_name)) {
116 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable or the PHP script is deprecated.", $ext_name));
122 // Load extension's own language file if not in test mode
123 if ((getExtensionMode() != 'test') && (isExtensionLanguageFileReadable($ext_name))) {
125 loadLanguageFile($ext_name);
129 if (isExtensionFunctionFileReadable($ext_name)) {
131 if ((($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y') || (!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name]))) && (!isExtensionLibraryLoaded($ext_name))) {
132 // Construct IFN for functions file
133 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
136 markExtensionLibraryAsLoaded($ext_name);
138 // Download functions file
139 loadIncludeOnce($funcsInclude);
141 } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
142 // No functions file is not so good...
143 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
149 // Load extension's filter library if present
150 loadExtensionFilters($ext_name);
152 // Extensions are not deprecated by default
153 setExtensionDeprecated('N');
155 // Extensions are not always active by default
156 setExtensionAlwaysActive('N');
158 // Include the extension file
159 loadCurrentExtensionInclude();
161 // Load extra mode files
162 loadCurrentExtensionModeInclude();
164 // Is this extension deprecated?
165 if ((isExtensionDeprecated()) && (!in_array(getExtensionMode(), array('test', 'update', 'deactivate'))) && (isExtensionActive($ext_name))) {
166 // Deactivate the extension
167 doDeactivateExtension($ext_name);
170 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension load aborted, ext_name=' . $ext_name . ' - Extension is deprecated.');
174 // Mark it as loaded in normal mode
175 if (getExtensionMode() == '') {
177 markExtensionAsLoaded($ext_name);
181 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension successfully loaded, ext_name=' . $ext_name);
185 // Registers an extension and possible update dependencies
186 function registerExtension ($ext_name, $taskId, $isDryRun = FALSE, $ignoreUpdates = FALSE, $previousExtension = NULL) {
187 // Set current extension name
188 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId=' . intval($taskId) . ',isDryRun=' . intval($isDryRun) . ',ignoreUpdates=' . intval($ignoreUpdates) . ' - ENTERED!');
189 setCurrentExtensionName($ext_name);
192 enableExtensionDryRun($isDryRun);
194 // By default all extensions are in productive phase
195 enableExtensionProductive();
197 // This shall never do a non-admin user or if the extension is active (already installed)
198 if (((!isAdmin()) && (!isInstallationPhase())) || (isExtensionInstalled($ext_name))) {
199 // Abort here with 'false'
200 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ABORTED: isAdmin()=' . intval(isAdmin()) . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ',isExtensionInstalled()=' . intval(isExtensionInstalled($ext_name)));
204 // When this extension is already in registration/update phase, all is fine
205 if ((isExtensionRegistrationRunning($ext_name)) || ((isExtensionUpdateRunning($ext_name)) && ($ignoreUpdates === FALSE))) {
206 // Then abort here with 'true' because it is fine
207 /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - already in registration/update phase, all fine,isDryRun=' . intval($isDryRun) . ',ignoreUpdates=' . intval($ignoreUpdates));
208 //* BUG: */ reportBug(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isDryRun=' . intval($isDryRun) . ',ignoreUpdates=' . intval($ignoreUpdates) . ' - Please investigate!');
212 // This registration is running
213 addExtensionRunningRegistration($ext_name);
215 // Init EXT_UPDATE_DEPENDS if not yet done
216 if (!isExtensionUpdateDependenciesInitialized()) {
218 initExtensionUpdateDependencies();
221 // Is the task id zero? Then we need to auto-fix it here
222 if ((!isValidId($taskId)) && (!isInstallationPhase())) {
223 // Try to find the task
224 $taskId = determineExtensionTaskId(getCurrentExtensionName());
226 // Still zero and not in dry-run?
227 if ((!isValidId($taskId)) && (!isExtensionDryRun())) {
228 // Now try to create a new task
229 $taskId = createNewExtensionTask(getCurrentExtensionName());
232 if (!isValidId($taskId)) {
233 // Then request a bug report
234 reportBug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId/createNewExtensionTask(%s)",
236 getCurrentExtensionName()
242 // Init queries and notes
244 initExtensionNotes();
248 $processResult = FALSE;
249 initIncludePool('extension');
251 // By default there is no failure
252 enableExtensionReportingFailure();
254 // Does this extension exists?
255 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - CALLING loadExtension() ...');
256 if (loadExtension(getCurrentExtensionName(), 'setup', '0.0.0', isExtensionDryRun(), $previousExtension)) {
257 // Set current extension name again
258 setCurrentExtensionName($ext_name);
260 // And run possible updates
261 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
262 $history = getExtensionVersionHistory();
263 foreach ($history as $ext_ver) {
264 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ext_ver);
265 // Load extension in update mode
266 loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun(), $previousExtension);
268 // Add update notes to our output
269 addExtensionNotes($ext_ver);
272 // Does this extension depends on an outstanding update of another update?
273 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
275 $ext_update = getExtensionUpdateDependenciesIterator();
277 // Increment here to avoid endless loop
278 incrementExtensionUpdateIterator();
280 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
281 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
282 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun(), $previousExtension))) {
283 // Set current extension name again
284 setCurrentExtensionName($ext_name);
286 // If versions mismatch update extension first
288 if (isExtensionInstalled($ext_update)) {
289 // Get version only if installed
290 $ext_ver = getExtensionVersion($ext_update);
293 // Extension version set? If empty the extension is not registered
294 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ',currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ' - EMPTY?');
295 if (empty($ext_ver)) {
296 // Extension not registered so far so first load task's id...
297 $updateTaskId = determineExtensionTaskId($ext_update);
300 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'updateTaskId=' . $updateTaskId . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ',currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ' - CHECKING!');
301 if ((isValidId($updateTaskId)) || (isInstallationPhase())) {
302 // Try to register the extension
303 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ',updateTaskId=' . $updateTaskId . ',isExtensionDryRun()=' . intval(isExtensionDryRun()));
304 $processResult = registerExtension($ext_update, $updateTaskId, isExtensionDryRun(), TRUE, $ext_name);
306 // Reset extension name
307 setCurrentExtensionName($ext_name);
308 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ',processResult=' . intval($processResult));
310 } elseif ($ext_ver != getCurrentExtensionVersion()) {
311 // Ok, update this extension now
312 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
313 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
314 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
315 updateExtension($ext_update, $ext_ver, isExtensionDryRun());
316 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
317 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
318 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
319 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
320 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
323 $processResult = TRUE;
325 // Nothing to register / update before...
326 $processResult = TRUE;
329 // Required file for update does not exists!
330 $processResult = TRUE;
331 // But this is fine for the first time...
334 // Restore the current extension name
335 setCurrentExtensionName($ext_name);
338 // Is there no update?
339 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
340 // Then test is passed!
341 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',processResult=true,countExtensionUpdateDependencies()=0 - Test passed!');
342 $processResult = TRUE;
345 // Switch back to register mode
346 setExtensionMode('setup');
348 // Remains true if extension registration reports no failures
349 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',processResult=' . intval($processResult));
350 $processResult = (($processResult === TRUE) && (isExtensionReportingFailure() === FALSE));
351 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',processResult=' . intval($processResult));
353 // Does everthing before wents ok?
354 if ($processResult === TRUE) {
355 // "Dry-run-mode" activated?
356 if ((isExtensionDryRun() === FALSE) && (!isExtensionOnRemovalList())) {
357 // Init SQLs and transfer ext->generic
358 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
360 setSqlsArray(getExtensionSqls());
362 // Copy current name to save calls
363 $currentName = getCurrentExtensionName();
365 // Mark it as NOT installed
366 $GLOBALS['ext_is_installed'][$currentName] = FALSE;
368 // Run installation pre-installation filters
369 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun(), 'ext_installing' => TRUE, 'enable_codes' => FALSE));
371 // Register extension
372 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . $currentName . '/' . getCurrentExtensionVersion() . ' - INSERT!');
373 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
375 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $currentName . ',always_active=' . getThisExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
376 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`, `ext_has_css`) VALUES ('%s','%s','%s','%s')",
379 getThisExtensionAlwaysActive(),
380 getCurrentExtensionVersion(),
382 ), __FUNCTION__, __LINE__);
385 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $currentName . ',always_active=' . getThisExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
386 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
389 getThisExtensionAlwaysActive(),
390 getCurrentExtensionVersion()
391 ), __FUNCTION__, __LINE__);
395 * Use the insert id as extension id and "cache" all data for
396 * this extension for early usage.
398 copyExtensionDataToCacheArray($currentName, getSqlInsertId());
400 // Mark it as installed
401 $GLOBALS['ext_is_installed'][$currentName] = TRUE;
404 * Run filter chain after extension has been "installed" which
405 * means that the extension has been registered in 'extensions'
406 * table and marked as "installed".
408 runFilterChain('post_extension_installed', array(
409 'pool' => 'extension',
410 'ext_name' => $currentName,
414 // Re-init queries and notes
415 initExtensionSqls(TRUE);
416 initExtensionNotes(TRUE);
418 // In normal mode return a true on success
420 } elseif (isExtensionDryRun() === TRUE) {
421 // In "dry-run" mode do always return a true
424 // Extension has been removed for updates, so all is fine!
428 // No, an error occurs while registering extension :-(
429 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
432 } elseif ((isValidId($taskId)) && (getCurrentExtensionName() != '')) {
433 // Remove task from system when id and extension's name is valid
434 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
435 sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
436 array(bigintval($taskId)), __FUNCTION__, __LINE__);
439 // @TODO This redirect is still needed to register sql_patches! Please try to avoid it
440 if (($ret === TRUE) && ($isDryRun === FALSE) && ($ext_name == 'sql_patches') && (!isInstallationPhase())) {
442 * This is a really dirty hack to prevent an error about a missing
443 * configuration entry which should be there after registration of
446 redirectToRequestUri();
449 // Return status code
450 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',currName=' . getCurrentExtensionName() . ',processResult=' . intval($processResult) . ',ret=' . intval($ret) . ' - EXIT!');
454 // Run SQL queries for given extension id
455 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
456 function doExtensionSqls ($ext_id, $load_mode) {
457 // This shall never do a non-admin user but installation phase is okay
458 if ((!isAdmin()) && (!isInstallationPhase())) {
459 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_id=' . $ext_id. ',load_mode=' . $load_mode . ',isAdmin()=' . intval(isAdmin()) . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ' - ABORT!');
463 // Get extension's name
464 $ext_name = getExtensionName($ext_id);
466 // Set current SQL name
467 setCurrentExtensionName($ext_name);
469 // Init EXT_UPDATE_DEPENDS
470 if (!isExtensionUpdateDependenciesInitialized()) {
472 initExtensionUpdateDependencies();
476 initExtensionSqls(TRUE);
478 // By default no SQL has been executed
481 // Load extension in detected mode
482 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName() . ',load_mode=' . $load_mode);
483 loadExtension(getCurrentExtensionName(), $load_mode, '0.0.0', FALSE);
487 setSqlsArray(getExtensionSqls());
490 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
492 // Are there entries?
494 // Run SQL commands...
495 runFilterChain('run_sqls');
498 // Run any filters depending on the action here
499 runFilterChain('extension_' . $load_mode);
501 // Remove cache file(s) if extension is active
502 if (((isExtensionActive('cache')) && ((!ifSqlHasZeroAffectedRows()) || ($sqlRan === TRUE) || ($load_mode == 'activate') || ($load_mode == 'deactivate')))) {
504 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
507 // Is this the sql_patches?
508 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $ext_id . ',currName=' . getCurrentExtensionName() . ',loadMode=' . $load_mode);
509 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'setup') || ($load_mode == 'remove'))) {
510 // Then redirect to logout
511 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ': LOAD!');
512 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
516 // Check whether the given extension is installed
517 function isExtensionInstalled ($ext_name) {
518 // We don't like empty extension names here
519 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
520 if (empty($ext_name)) {
521 // Please fix them all
522 reportBug(__FUNCTION__, __LINE__, 'ext_name is empty.');
525 // By default non is installed
526 $isInstalled = FALSE;
528 // Check if there is a cache entry
529 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
530 // Use cache built from below queries
531 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - CACHE!');
532 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
533 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
535 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - FOUND!');
539 incrementStatsEntry('cache_hits');
540 } elseif (isInstallationPhase()) {
541 // Extensions are all inactive/not installed during installation
542 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - installation phase detected.');
545 $ext_id = getExtensionId($ext_name);
547 // Is there a record?
548 $isInstalled = isValidId($ext_id);
551 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_id=' . $ext_id . ',isInstalled=' . intval($isInstalled));
553 // Is it installed, then cache the entry
554 if ($isInstalled === TRUE) {
555 // Dummy call (get is okay here)
556 getExtensionId($ext_name, TRUE);
559 // Remember the status
560 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
564 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isInstalled=' . intval($isInstalled) . ' - EXIT!');
568 // Check if given extension is active
569 function isExtensionActive ($ext_name) {
570 if (isInstallationPhase()) {
571 // Extensions are all inactive during installation
572 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extensions are always inactive while installation phase. ext_name=' . $ext_name);
574 } elseif (empty($ext_name)) {
575 // Empty extension names must befixed
576 reportBug(__FUNCTION__, __LINE__, 'Empty extension name provided.');
577 } elseif (!isExtensionInstalled($ext_name)) {
578 // Not installed extensions are always inactive
579 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not installed extensions are always inactive. ext_name=' . $ext_name);
583 // Not active is the default
584 $data['ext_active'] = 'N';
587 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
589 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
590 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
593 incrementStatsEntry('cache_hits');
594 } elseif (isExtensionLoaded($ext_name)) {
595 // @TODO Extension is loaded, what next?
596 reportBug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
597 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
598 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DB! ext_name=' . $ext_name);
599 // Load from database
600 $result = sqlQueryEscaped("SELECT * FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
601 array($ext_name), __FUNCTION__, __LINE__);
604 if (sqlNumRows($result) == 1) {
606 $data = sqlFetchArray($result);
609 setExtensionCacheRow($ext_name, $data);
613 sqlFreeResult($result);
615 // Extension not active!
616 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': Not active!');
617 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
621 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
623 // Is this extension activated? (For admins we always have active extensions...)
624 return ($data['ext_active'] == 'Y');
627 // Sets extension cache row
628 function setExtensionCacheRow ($ext_name, &$data) {
630 foreach ($data as $key => $value) {
633 $data['ext_id'] = $value;
637 // Is key=ext_has_css?
638 if ($key == 'ext_has_css') {
639 $data['ext_css'] = $value;
644 if ($key == 'ext_name') {
645 $GLOBALS['cache_array']['extension']['ext_name'][$data['id']] = $value;
647 // Add ordinary entry
648 $GLOBALS['cache_array']['extension'][$key][$ext_name] = $value;
653 // Get version from extensions
654 function getExtensionVersion ($ext_name, $force = FALSE) {
655 // By default no extension is found
656 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
657 $data['ext_version'] = 'false';
659 // Empty extension name should be fixed!
660 if (empty($ext_name)) {
661 // Please report this bug!
662 reportBug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
665 // Extensions are all inactive during installation
666 if (isInstallationPhase()) {
667 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',force=' . intval($force) . ' - Installation phase detected, returning empty version.');
671 // Is the cache written?
672 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - Checking cache ...');
673 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
674 // Load data from cache
675 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - CACHE!');
676 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
679 incrementStatsEntry('cache_hits');
680 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (!isHtmlOutputMode())) {
681 // Load from database
682 $result = sqlQueryEscaped("SELECT * FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
683 array($ext_name), __FUNCTION__, __LINE__);
684 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': DB - '.sqlNumRows($result).'');
686 // Is the extension there?
687 if (sqlNumRows($result) == 1) {
689 $data = sqlFetchArray($result);
692 setExtensionCacheRow($ext_name, $data);
693 } elseif (isDebugModeEnabled()) {
694 // Not found, may happen while an extension is uninstalled
695 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot find extension %s in database!", $ext_name));
699 sqlFreeResult($result);
702 // Extension version should not be invalid
703 if (($data['ext_version'] == 'false') && ($force === FALSE)) {
704 // Please report this trouble
705 reportBug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
709 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
710 return $data['ext_version'];
713 // Updates a given extension with current extension version to latest version
714 function updateExtension ($ext_name, $ext_ver, $isDryRun = FALSE, $ignoreDependencies = FALSE) {
715 // Only admins are allowed to update extensions
716 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_ver=' . $ext_ver . ',isDryRun=' . intval($isDryRun) . ',ignoreDependencies=' . intval($ignoreDependencies) . ' - ENTERED!');
717 if ((!isAdmin()) || (empty($ext_name))) {
718 // Called as non-admin or empty extension
719 reportBug(__FUNCTION__, __LINE__, 'Called as non-admin (isAdmin()=' . intval(isAdmin()) . '), or empty extension name. ext_name=' . $ext_name);
722 // Set current SQL name
723 setCurrentExtensionName($ext_name);
725 // Is this extension update already running?
726 if ((isExtensionUpdateRunning($ext_name, $ignoreDependencies)) && ($isDryRun === FALSE)) {
727 // This is fine but needs logging ATM
728 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - already in update phase, all fine.');
729 //* BUG: */ reportBug(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - already in update phase, please investigate!');
735 initExtensionNotes();
736 initIncludePool('extension');
738 // Load extension in test mode
739 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
741 // Save version history
742 $history = getExtensionVersionHistory();
744 // Remove old SQLs array to prevent possible bugs
747 // Check if version is updated
748 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, getCurrentExtensionName() . '/' . $ext_name . ':' . getThisExtensionVersion() . '/' . $ext_ver . '/' . intval(is_array($history)));
749 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
750 // Search for starting point (-1 for making 0.0 -> 0.0.0 switch work)
752 if ($ext_ver != '0.0') {
753 $start = array_search($ext_ver, $history);
756 // And load SQL queries in order of version history
757 for ($idx = ($start + 1); $idx < count($history); $idx++) {
758 // Set extension version
759 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
762 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
765 $depencies = getExtensionUpdateDependencies();
768 if (count($depencies) > 0) {
769 // Apply all extension depencies
770 foreach ($depencies as $ext_depend) {
771 // Did we already update/register this?
772 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
774 setCurrentExtensionName($ext_depend);
776 // Mark it as already updated before we update it
777 $GLOBALS['ext_updated'][$ext_depend] = TRUE;
779 // Is the extension there?
780 if (isExtensionInstalled($ext_depend)) {
781 // Update another extension first!
782 $processResult = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun(), TRUE);
784 // Register new extension
785 $processResult = registerExtension($ext_depend, NULL, isExtensionDryRun());
791 setCurrentExtensionName($ext_name);
793 // Set extension version here
794 setCurrentExtensionVersion($ext_ver);
798 addExtensionNotes($history[$idx]);
801 // In real-mode execute any existing includes
802 if (isExtensionDryRun() === FALSE) {
803 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
804 runFilterChain('load_includes', 'extension');
805 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
806 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
811 setSqlsArray(getExtensionSqls());
814 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun(), 'ext_installing' => TRUE, 'enable_codes' => FALSE));
816 if (isExtensionDryRun() === FALSE) {
817 // Run filters on success extension update
818 runFilterChain('extension_update', getCurrentExtensionName());
822 //* DEBUG: */logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_ver=' . $ext_ver . ',isDryRun=' . intval($isDryRun) . ',ignoreDependencies=' . intval($ignoreDependencies) . ' - EXIT!');
825 // Output verbose SQL table for extension
826 function addExtensionVerboseSqlTable ($title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}') {
830 reportBug(__FUNCTION__, __LINE__, 'title is empty.');
837 if (isVerboseSqlEnabled()) {
838 // Are there entries?
839 if (countExtensionSqls() > 0) {
843 foreach (getExtensionSqls() as $sqls) {
844 // New array format is recursive
845 foreach ($sqls as $sql) {
849 // Output command if set
851 // Prepare output for template
854 'sql' => str_replace(array('{', '}'), array('{', '}'), encodeEntities($sql))
858 $OUT .= loadTemplate('admin_extension_sql_row', TRUE, $content);
866 // Prepare content for template
872 // Load main template
873 $OUT = loadTemplate('admin_extension_sql_table', TRUE, $content);
875 // No addional SQL commands to run
876 $OUT = displayMessage('{--ADMIN_EXTENSION_VERBOSE_SQLS_404--}', TRUE);
884 // Get extension name from id
885 function getExtensionName ($ext_id) {
886 // Init extension name
887 $data['ext_name'] = '';
890 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
892 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
895 incrementStatsEntry('cache_hits');
896 } elseif (!isExtensionActive('cache')) {
897 // Load from database
898 $result = sqlQueryEscaped("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
899 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
901 // Is the entry there?
902 if (sqlNumRows($result) == 1) {
903 // Get the extension's name from database
904 $data = sqlFetchArray($result);
908 sqlFreeResult($result);
911 // Did we find some extension?
912 if (empty($data['ext_name'])) {
913 // We should fix these all!
914 reportBug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
917 // Return the extension name
918 return $data['ext_name'];
921 // Get extension id from name
922 function getExtensionId ($ext_name) {
924 $data['ext_id'] = '0';
927 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
929 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
932 incrementStatsEntry('cache_hits');
934 // Load from database
935 $result = sqlQueryEscaped("SELECT * FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
936 array($ext_name), __FUNCTION__, __LINE__);
938 // Is the entry there?
939 if (sqlNumRows($result) == 1) {
940 // Get the extension's id from database
941 $data = sqlFetchArray($result);
944 setExtensionCacheRow($ext_name, $data);
948 sqlFreeResult($result);
952 return $data['ext_id'];
955 // Determines whether the given extension name is valid
956 function isExtensionNameValid ($ext_name) {
958 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
959 // Generate include file name
960 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
962 // Is there a file in inc/extensions/ ?
963 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
967 return $GLOBALS['ext_name_valid'][$ext_name];
970 // Determines whether the given extension id is valid
971 function isExtensionIdValid ($ext_id) {
972 // Default is nothing valid
975 // Check in cache then in database
976 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
981 incrementStatsEntry('cache_hits');
984 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', TRUE) == 1);
991 // Activate given extension
992 function doActivateExtension ($ext_name) {
993 // Is the extension installed?
994 if (!isExtensionInstalled($ext_name)) {
995 // Non-installed extensions cannot be activated
996 reportBug(__FUNCTION__, __LINE__, 'Tried to activate non-installed extension ' . $ext_name);
999 // Activate the extension
1000 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
1001 array($ext_name), __FUNCTION__, __LINE__);
1003 // Then run all queries
1004 doExtensionSqls(getExtensionId($ext_name), 'activate');
1007 // Deactivate given extension
1008 function doDeactivateExtension ($ext_name, $inRebuild = FALSE) {
1009 // Is the extension installed?
1010 if (!isExtensionInstalled($ext_name)) {
1011 // Non-installed extensions cannot be activated
1012 reportBug(__FUNCTION__, __LINE__, 'Tried to deactivate non-installed extension ' . $ext_name . ',getExtensionMode()=' . getExtensionMode());
1015 // Activate the extension
1016 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
1017 array($ext_name), __FUNCTION__, __LINE__);
1019 // Then run all queries
1020 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
1022 // Create new task (we ignore the task id here)
1023 createExtensionDeactivationTask($ext_name);
1025 // Do not rebuild cache if it is already been rebuild
1026 if ($inRebuild === FALSE) {
1028 rebuildCache('extension', 'extension');
1032 sendAdminNotification(
1033 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
1034 'admin_extension_deactivated',
1035 array('ext_name' => $ext_name)
1039 // Creates a new task for updated extension
1040 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
1041 // Create subject line
1042 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
1045 $taskId = determineTaskIdBySubject($subject);
1047 // Is the extension there?
1048 if (isExtensionInstalled($ext_name)) {
1049 // Check if task is not there
1050 if (!isValidId($taskId)) {
1051 // Create extension update-task
1052 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
1055 // Extension not there! :-(
1056 reportBug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
1063 // Creates a new task for newly installed extension
1064 function createNewExtensionTask ($ext_name) {
1065 // Generate subject line
1066 $subject = sprintf("[%s:]", $ext_name);
1069 $taskId = determineTaskIdBySubject($subject);
1071 // Not installed and do we have created a task for the admin?
1072 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId[' . gettype($taskId) . ']=' . $taskId);
1073 if ((!isValidId($taskId)) && (!isExtensionInstalled($ext_name))) {
1074 // Set default message if ext-foo is missing
1075 $message = '{%message,ADMIN_EXTENSION_TEXT_FILE_MISSING=' . $ext_name . '%}';
1078 $FQFN = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
1084 // Load text for task if found
1085 if (isFileReadable($FQFN)) {
1086 // Load extension's description template (but do not compile the code)
1087 $message = '{OPEN_TEMPLATE}template,LoadTemplate=ext_' . $ext_name . '{CLOSE_TEMPLATE}';
1089 // Write this in debug.log as well
1090 logDebugMessage(__FUNCTION__, __LINE__, $message);
1093 // Task not created so it's a brand-new extension which we need to register and create a task for!
1094 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), FALSE);
1101 // Creates a task for automatically deactivated (deprecated) extension
1102 function createExtensionDeactivationTask ($ext_name) {
1103 // Create subject line
1104 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1107 $taskId = determineTaskIdBySubject($subject);
1109 // Not installed and do we have created a task for the admin?
1110 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId[' . gettype($taskId) . ']=' . $taskId);
1111 if ((!isValidId($taskId)) && (isExtensionInstalled($ext_name))) {
1112 // Task not created so add it
1113 $taskId = createNewTask($subject, sqlEscapeString(loadTemplate('task_EXTENSION_deactivated', TRUE, $ext_name)), 'EXTENSION_DEACTIVATION');
1120 // Determines the task id for given extension
1121 function determineExtensionTaskId ($ext_name) {
1122 // Is it installation phase and table task_system is not found?
1123 if ((isInstallationPhase()) && (!ifSqlTableExists('task_system'))) {
1124 // Then return NULL (not found)
1128 // Default is not found
1129 $data['task_id'] = NULL;
1131 // Search for extension task's id
1132 $result = sqlQueryEscaped("SELECT `id` AS `task_id` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1133 array($ext_name), __FUNCTION__, __LINE__);
1136 if (sqlNumRows($result) == 1) {
1137 // Task found so load task's id and register extension...
1138 $data = sqlFetchArray($result);
1142 sqlFreeResult($result);
1145 return $data['task_id'];
1148 // Determines the task id for given subject
1149 function determineTaskIdBySubject ($subject) {
1150 // Default is not found
1151 $data['task_id'] = '0';
1153 // Search for task id
1154 $result = sqlQueryEscaped("SELECT `id` AS `task_id` FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1155 array($subject), __FUNCTION__, __LINE__);
1158 if (sqlNumRows($result) == 1) {
1159 // Task found so load task's id and register extension...
1160 $data = sqlFetchArray($result);
1164 sqlFreeResult($result);
1167 return $data['task_id'];
1170 // Add updates notes for given version
1171 function addExtensionNotes ($ext_ver) {
1172 // Init notes/content
1176 // Is do we have verbose output enabled?
1177 if ((!isExtensionActive('sql_patches')) || (isVerboseSqlEnabled())) {
1178 // Update notes found?
1179 if ((isExtensionUpdateNoteSet($ext_ver)) && ($ext_ver != '0.0.0')) {
1180 // Update notes found
1183 'notes' => getExtensionUpdateNotes($ext_ver)
1187 setExtensionUpdateNotes('', $ext_ver);
1188 } elseif ($ext_ver == '0.0.0') {
1189 // Is the extension productive?
1190 if (isExtensionProductive(getCurrentExtensionName())) {
1194 'notes' => '{--ADMIN_EXTENSION_INITIAL_RELEASE--}'
1200 'notes' => '{--ADMIN_EXTENSION_DEVELOPER_RELEASE--}'
1204 // No update notes found
1207 'notes' => '{--ADMIN_EXTENSION_UPDATE_NOTICES_404--}'
1212 $out = loadTemplate('admin_extension_notes', TRUE, $content);
1216 appendExtensionNotes($out);
1219 // Getter for CSS files array
1220 function getExtensionCssFiles () {
1221 // By default no additional CSS files are found
1222 $cssFiles = array();
1224 // Is the array there?
1225 if (isset($GLOBALS['css_files'])) {
1227 $cssFiles = $GLOBALS['css_files'];
1234 // Init CSS files array
1235 function initExtensionCssFiles () {
1237 $GLOBALS['css_files'] = array();
1241 function addExtensionCssFile ($file) {
1242 // Is the array there?
1243 if (!isset($GLOBALS['css_files'])) {
1244 // Then auto-init them
1245 initExtensionCssFiles();
1249 array_push($GLOBALS['css_files'], $file);
1252 // Setter for EXT_ALWAYS_ACTIVE flag
1253 function setExtensionAlwaysActive ($active) {
1254 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1257 // Getter for EXT_ALWAYS_ACTIVE flag
1258 function getThisExtensionAlwaysActive () {
1259 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1262 // Checks whether the current extension is always active
1263 function isThisExtensionAlwaysActive () {
1264 return (getThisExtensionAlwaysActive() == 'Y');
1267 // Setter for EXT_VERSION flag
1268 function setThisExtensionVersion ($ext_version) {
1269 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1272 // Getter for EXT_VERSION flag
1273 function getThisExtensionVersion () {
1274 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1277 // Setter for EXT_DEPRECATED flag
1278 function setExtensionDeprecated ($deprecated) {
1279 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1282 // Getter for EXT_DEPRECATED flag
1283 function isExtensionDeprecated ($ext_name = NULL) {
1284 // Default is from current (NULL) extension
1285 $isDeprecated = ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1288 if (!is_null($ext_name)) {
1289 // Then use it instead
1290 $isDeprecated = ((isset($GLOBALS['ext_deprecated'][$ext_name])) && ($GLOBALS['ext_deprecated'][$ext_name] == 'Y'));
1294 return $isDeprecated;
1297 // Setter for EXT_UPDATE_DEPENDS flag
1298 function addExtensionDependency ($updateDepends) {
1299 // Is the update depency empty? (NEED TO BE FIXED!)
1300 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ',extensionMode=' . getExtensionMode() . ' - ENTERED!');
1301 if (empty($updateDepends)) {
1302 // Please report this bug!
1303 reportBug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1306 // Is it not yet added?
1307 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1309 * Double-adding happens when the extension and an update of the same
1310 * extension requires the same other extension again.
1312 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'updateDepends=' . $updateDepends . ',extensionMode=' . getExtensionMode() . ',currentExtension=' . getCurrentExtensionName() . ' - called twice.');
1316 // Add it to the list of extension update depencies map
1317 array_push($GLOBALS['ext_update_depends'][getCurrentExtensionName()], $updateDepends);
1320 if ((!isset($GLOBALS['ext_running_updates'][getCurrentExtensionName()])) || (!is_array($GLOBALS['ext_running_updates'][getCurrentExtensionName()]))) {
1321 $GLOBALS['ext_running_updates'][getCurrentExtensionName()] = array();
1324 // Remember it in the list of running updates
1325 array_push($GLOBALS['ext_running_updates'][getCurrentExtensionName()], $updateDepends);
1326 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ',extensionMode=' . getExtensionMode() . ' - EXIT!');
1329 // Getter for running updates
1330 function getExtensionUpdatesRunning () {
1331 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1334 // Checks whether the given extension registration is in progress
1335 function isExtensionRegistrationRunning ($ext_name) {
1337 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1340 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1344 // Init EXT_UPDATE_DEPENDS flag
1345 function initExtensionUpdateDependencies () {
1346 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1348 // Init update depency map automatically if not found
1349 if (isExtensionUpdateDependenciesInitialized()) {
1350 // We need these bug reports as well...
1351 reportBug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1354 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1356 // Init running updates array
1357 initExtensionRuningUpdates();
1359 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1362 // Adds an extension as "registration in progress"
1363 function addExtensionRunningRegistration ($ext_name) {
1365 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1366 if (isExtensionRegistrationRunning($ext_name)) {
1367 // This is really bad and should not be quietly ignored
1368 reportBug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1372 array_push($GLOBALS['ext_register_running'], $ext_name);
1373 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1376 // Checks whether EXT_UPDATE_DEPENDS is initialized
1377 function isExtensionUpdateDependenciesInitialized () {
1378 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1379 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1382 // Checks whether an update is already running for given extension
1383 function isExtensionUpdateRunning ($ext_name, $ignoreDependencies = FALSE) {
1384 // 'ext_running_updates' must be there
1385 if ((!isset($GLOBALS['ext_running_updates'])) || (!is_array($GLOBALS['ext_running_updates']))) {
1387 reportBug(__FUNCTION__, __LINE__, 'Required array ext_running_updates not found. ext_name=' . $ext_name . ',ignoreDependencies=' . intval($ignoreDependencies));
1390 // Current and given extensions means whole array
1391 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',ignoreDependencies=' . intval($ignoreDependencies) . ' - ENTERED!');
1392 if ($ext_name == getCurrentExtensionName()) {
1393 // Default is not found
1396 // Walk through whole array
1397 foreach ($GLOBALS['ext_running_updates'] as $ext1 => $depends) {
1399 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',depends=' . print_r($depends, TRUE));
1400 if (($ext1 == $ext_name) || ((in_array($ext_name, $depends)) && ($ignoreDependencies === FALSE))) {
1403 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=true - FOUND!');
1409 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',ignoreDependencies=' . intval($ignoreDependencies) . ', isRunning=' . intval($isRunning) . ' - ALT-EXIT!');
1414 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1417 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',ignoreDependencies=' . intval($ignoreDependencies) . ', isRunning=' . intval($isRunning) . ' - EXIT!');
1421 // Initializes the list of running updates
1422 function initExtensionRuningUpdates () {
1423 // Auto-init ext_running_updates
1424 if (!isset($GLOBALS['ext_running_updates'])) {
1425 $GLOBALS['ext_running_updates'] = array();
1426 $GLOBALS['ext_register_running'] = array();
1430 // Getter for EXT_UPDATE_DEPENDS flag
1431 function getExtensionUpdateDependencies () {
1432 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1433 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1436 // Getter for next iterator depency
1437 function getExtensionUpdateDependenciesIterator () {
1438 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1439 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1442 // Counter for extension update depencies
1443 function countExtensionUpdateDependencies () {
1444 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '=' . count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1445 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1448 // Removes given extension from update denpency list
1449 function removeExtensionDependency ($ext_name) {
1451 $key = array_search($ext_name, getExtensionUpdateDependencies());
1454 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',key[' . gettype($key) . ']=' . $key);
1457 if ($key !== FALSE) {
1459 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1461 // And sort the array
1462 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1466 // Init iterator for update depencies
1467 function initExtensionUpdateIterator () {
1468 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1469 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1472 // Getter for depency iterator
1473 function getExtensionUpdateIterator () {
1474 // Auto-init iterator
1475 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) {
1476 // Initialize update iterator
1477 initExtensionUpdateIterator();
1481 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '=' . $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1482 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1485 // Increments the update iterator
1486 function incrementExtensionUpdateIterator () {
1487 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1488 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1491 // Setter for EXT_REPORTS_FAILURE flag
1492 function enableExtensionReportingFailure ($reportsFailure = FALSE) {
1493 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1496 // Getter for EXT_REPORTS_FAILURE flag
1497 function isExtensionReportingFailure () {
1498 return $GLOBALS['ext_reports_failure'];
1501 // Setter for EXT_VER_HISTORY flag
1502 function setExtensionVersionHistory ($versionHistory) {
1503 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1506 // Getter for EXT_VER_HISTORY array
1507 function getExtensionVersionHistory () {
1509 if (!isset($GLOBALS['ext_ver_history'][getCurrentExtensionName()])) {
1510 // Then abort here to a avoid an ugly "Undefined index" error
1511 reportBug(__FUNCTION__, __LINE__, 'Extension ext-' . getCurrentExtensionName() . ' has no history set! Is this extension empty?');
1514 // Return the history
1515 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1518 // Setter for EXT_UPDATE_NOTICES
1519 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1521 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . '/' . getCurrentExtensionVersion() . ',updateNotes(length)=' . strlen($updateNotes));
1522 if (empty($ext_ver)) {
1523 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1525 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1529 // Getter for EXT_UPDATE_NOTICES
1530 function getExtensionUpdateNotes ($ext_ver) {
1531 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1534 // Checks if ext_update_notes is set
1535 function isExtensionUpdateNoteSet ($ext_ver) {
1536 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1539 // Init extension notice
1540 function initExtensionNotes ($force = FALSE) {
1541 // Is it already initialized?
1542 if (($force === FALSE) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1543 // This is mostly not wanted, so please report it
1544 reportBug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1547 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1548 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1551 // Append extension notice
1552 function appendExtensionNotes ($notes) {
1553 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes(length)=' . strlen($notes));
1554 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1557 // Getter for extension notes
1558 function getExtensionNotes () {
1559 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1562 // Setter for current extension name
1563 function setCurrentExtensionName ($ext_name) {
1564 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1567 // Getter for current extension name
1568 function getCurrentExtensionName () {
1569 if (!isset($GLOBALS['curr_extension_name'])) {
1571 reportBug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1575 return $GLOBALS['curr_extension_name'];
1578 // Init SQLs array for current extension
1579 function initExtensionSqls ($force = FALSE) {
1580 // Auto-init the array or if forced
1581 if (($force === TRUE) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1583 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1585 // Initialize the generic array
1590 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1591 function addExtensionSql ($sql) {
1592 // Copy current name/version to local variable
1593 $currentName = getCurrentExtensionName();
1594 $currentVersion = getCurrentExtensionVersion();
1596 // Is is the array there?
1597 if ((!isset($GLOBALS['ext_sqls'][$currentName][$currentVersion])) || (!is_array($GLOBALS['ext_sqls'][$currentName][$currentVersion]))) {
1599 $GLOBALS['ext_sqls'][$currentName][$currentVersion] = array();
1602 // Is the SQL statement empty?
1605 * Abort here as this may happen if getExtensionMode() is 'activate' or
1606 * 'deactivate'. This means that for 'mode' are no SQL statements
1613 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $currentName . ',ext_version=' . $currentVersion . ',sql=' . $sql);
1614 array_push($GLOBALS['ext_sqls'][$currentName][$currentVersion], $sql);
1617 // Getter for SQLs array for current extension
1618 function getExtensionSqls () {
1619 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1620 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1621 // Not found, should not happen
1622 reportBug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1623 getCurrentExtensionName()
1628 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1631 // Count SQLs for current extension
1632 function countExtensionSqls () {
1633 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1634 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1635 // Not found, should not happen
1636 reportBug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1637 getCurrentExtensionName()
1642 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1645 // Removes SQLs for current extension
1646 function unsetExtensionSqls () {
1647 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1650 // Auto-initializes the removal list
1651 function initExtensionRemovalList () {
1652 // Is the remove list there?
1653 if (!isset($GLOBALS['ext_update_remove'])) {
1655 $GLOBALS['ext_update_remove'] = array();
1659 // Checks whether the current extension is on the removal list
1660 function isExtensionOnRemovalList () {
1661 // Init removal list
1662 initExtensionRemovalList();
1665 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1668 // Adds the current extension to the removal list
1669 function addCurrentExtensionToRemovalList () {
1671 array_push($GLOBALS['ext_update_remove'], getCurrentExtensionName());
1674 // Getter for removal list
1675 function getExtensionRemovalList () {
1676 // Return the removal list
1677 return $GLOBALS['ext_update_remove'];
1680 // Redirects if the provided extension is not installed
1681 function redirectOnUninstalledExtension ($ext_name) {
1682 // So is the extension there?
1683 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1684 // Redirect to index
1685 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1689 // Filter for initialization of all extensions by loading them in 'init' mode
1690 function FILTER_INIT_EXTENSIONS () {
1691 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTERED!');
1692 // Init notification pool
1693 initIncludePool('notify');
1695 // Are there some entries?
1696 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1697 // Load all found extensions if found
1698 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1699 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1701 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - START');
1702 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1703 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - END');
1705 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1708 // Run any notifications
1709 runFilterChain('load_includes', 'notify');
1711 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1714 // Setter for extension mode
1715 function setExtensionMode ($ext_mode) {
1716 $GLOBALS['ext_mode'] = (string) $ext_mode;
1719 // Getter for extension mode
1720 function getExtensionMode () {
1721 return $GLOBALS['ext_mode'];
1724 // Setter for dry-run
1725 function enableExtensionDryRun ($isDryRun = TRUE) {
1726 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',isDryRun='.intval($isDryRun));
1727 $GLOBALS['ext_isDryRun'] = (bool) $isDryRun;
1730 // Getter for dry-run
1731 function isExtensionDryRun () {
1732 return $GLOBALS['ext_isDryRun'];
1735 // Setter for current extension version
1736 function setCurrentExtensionVersion ($ext_ver) {
1737 // ext_ver should never be empty in other modes than 'test'
1738 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1739 // Please report all these messages
1740 reportBug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1744 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1745 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1748 // Getter for current extension version
1749 function getCurrentExtensionVersion () {
1750 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1751 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1754 // Remove the extension from cache array
1755 function removeExtensionFromArray () {
1756 // "Cache" this name
1757 $ext_name = getCurrentExtensionName();
1759 // Now loop through the whole cache
1760 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1761 // Is it an element?
1762 if (isset($cacheArray[$ext_name])) {
1764 unset($cacheArray[$ext_name]);
1765 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1768 $key = array_search($ext_name, $cacheArray);
1771 if ($key !== FALSE) {
1772 // Found, so remove it
1773 unset($cacheArray[$key]);
1774 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1779 // Remove from other caches as well
1780 unset($GLOBALS['ext_is_installed'][$ext_name]);
1781 unset($GLOBALS['loaded_extension'][$ext_name]);
1784 // "Getter" for 'extension has a CSS file' (with same name, of course)
1785 function getExtensionHasCss () {
1787 if (!isset($GLOBALS[__FUNCTION__][getCurrentExtensionName()][getCurrentTheme()])) {
1788 // Construct FQFN for check
1789 $FQFN = sprintf("%stheme/%s/css/%s.css",
1792 getCurrentExtensionName()
1796 $GLOBALS[__FUNCTION__][getCurrentExtensionName()][getCurrentTheme()] = convertBooleanToYesNo(isFileReadable($FQFN));
1800 return $GLOBALS[__FUNCTION__][getCurrentExtensionName()][getCurrentTheme()];
1803 // Checks whether the given extension's language file is readable
1804 function isExtensionLanguageFileReadable ($ext_name) {
1806 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1808 incrementStatsEntry('cache_hits');
1810 // Determine it and put it in cache
1811 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = convertBooleanToYesNo(isLanguageIncludeReadable($ext_name));
1815 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1818 // Load current extension's include file
1819 function loadCurrentExtensionInclude () {
1821 if (!isExtensionIncludeReadable()) {
1823 reportBug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1826 // Generate INC name
1827 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1833 // Load current extension's mode include file
1834 function loadCurrentExtensionModeInclude () {
1835 // Generate INC name
1836 $INC = sprintf("inc/extensions/%s/mode-%s.php", getCurrentExtensionName(), getExtensionMode());
1838 // Is the include readable?
1839 if (isIncludeReadable($INC)) {
1840 // Load it as it is optional
1845 // Checks whether an extension is readable
1846 function isExtensionIncludeReadable ($ext_name = '') {
1847 // If empty, use current
1848 if (empty($ext_name)) {
1849 $ext_name = getCurrentExtensionName();
1853 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1854 // Generate INC name
1855 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1858 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1862 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1863 return $GLOBALS['ext_inc_readable'][$ext_name];
1866 // Checks if an extension's function file is readable
1867 function isExtensionFunctionFileReadable ($ext_name) {
1869 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1870 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1871 // Just count cache hits
1872 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1873 incrementStatsEntry('cache_hits');
1875 // Construct IFN for functions file
1876 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1878 // Is this include there?
1879 $isIncludeFound = ((isFileReadable($funcsInclude)) && (!isExtensionLibraryLoaded($ext_name)) && (getExtensionMode() == 'test'));
1881 // And put in cache, converted
1882 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = convertBooleanToYesNo($isIncludeFound);
1886 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1887 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1890 // Adds a CREATE TABLE statement if the requested table is not there
1891 function addCreateTableSql ($tableName, $sql, $comment) {
1892 // Is the table not there?
1893 if (!ifSqlTableExists($tableName)) {
1894 // Is not found, so add it
1895 addExtensionSql('CREATE TABLE
1896 `{?_MYSQL_PREFIX?}_' . $tableName . '` (' . $sql . ')
1897 ENGINE = {?_TABLE_TYPE?}
1899 COLLATE utf8_general_ci
1900 COMMENT ' . chr(39) . $comment . chr(39));
1903 logDebugMessage(__FUNCTION__, __LINE__, 'The table ' . $tableName . ' is already created.');
1907 // Adds a DROP TABLE statement if the requested tabled is there
1908 function addDropTableSql ($tableName) {
1909 // Is the table there?
1910 if (ifSqlTableExists($tableName)) {
1912 * Then add it, non-existing tables can be ignored because it will
1913 * happen with every newly installed extension.
1915 addExtensionSql('DROP TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '`');
1918 $GLOBALS['ifSqlTableExists'][$tableName] = FALSE;
1922 // Adds a RENAME TABLE stament if 'from' table exist and 'to' table not
1923 function addRenameTableSql ($fromTable, $toTable) {
1924 // Make sure both are not the same
1925 assert($fromTable != $toTable);
1927 // Is renaming required?
1928 if ((ifSqlTableExists($fromTable)) && (!ifSqlTableExists($toTable))) {
1930 addExtensionSql('RENAME TABLE `{?_MYSQL_PREFIX?}_' . $fromTable . '` TO `{?_MYSQL_PREFIX?}_' . $toTable . '`');
1933 $GLOBALS['ifSqlTableExists'][$fromTable] = FALSE;
1934 $GLOBALS['ifSqlTableExists'][$toTable] = TRUE;
1938 // Adds an admin menu to the SQL queue of the menu entry is not found
1939 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1940 // Now check if this menu is there
1941 if (!isMenuActionValid('admin', $action, $what)) {
1943 if (is_null($what)) {
1945 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`, `what`, `title`, `descr`, `sort`) VALUES ('%s',NULL,'%s','%s',%s)",
1953 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`, `what`, `title`, `descr`, `sort`) VALUES ('%s','%s','%s','%s',%s)",
1962 // Add it to the queue
1963 addExtensionSql($sql);
1964 } elseif (isDebugModeEnabled()) {
1965 // Double menus should be located and fixed!
1966 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
1970 // Adds a guest menu to the SQL queue if the menu entry is not found
1971 function addGuestMenuSql ($action, $what, $title, $sort) {
1972 // Now check if this menu is there
1973 if (!isMenuActionValid('guest', $action, $what)) {
1975 if (is_null($what)) {
1977 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s',NULL,'%s','N','Y',%s)",
1984 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s','%s','%s','N','Y',%s)",
1992 // Add it to the queue
1993 addExtensionSql($sql);
1994 } elseif (isDebugModeEnabled()) {
1995 // Double menus should be located and fixed!
1996 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
2000 // Adds a member menu to the SQL queue if the menu entry is not found
2001 function addMemberMenuSql ($action, $what, $title, $sort) {
2002 // Now check if this menu is there
2003 if (!isMenuActionValid('member', $action, $what)) {
2005 if (is_null($what)) {
2007 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s',NULL,'%s','N','Y',%s)",
2014 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s','%s','%s','N','Y',%s)",
2022 // Add it to the queue
2023 addExtensionSql($sql);
2024 } elseif (isDebugModeEnabled()) {
2025 // Double menus should be located and fixed!
2026 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
2030 // Adds a sponsor menu to the SQL queue if the menu entry is not found
2031 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
2032 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
2033 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
2035 if (is_null($what)) {
2037 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`, `what`, `title`, `active`, `sort`) VALUES ('%s',NULL,'%s','%s',%s)",
2045 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`, `what`, `title`, `active`, `sort`) VALUES ('%s','%s','%s','%s',%s)",
2054 // Add it to the queue
2055 addExtensionSql($sql);
2056 } elseif (isDebugModeEnabled()) {
2057 // Double menus should be located and fixed!
2058 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s,what=%s,title=%s,active=%s detected.", $action, $what, $title, $active));
2062 // Add ALTER TABLE `foo` ADD sql if not found
2063 function addExtensionAddTableColumnSql ($tableName, $columnName, $columnSql) {
2064 // Is the column there?
2065 if (!ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $columnName, isInstallationPhase())) {
2067 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD `' . $columnName . '` ' . $columnSql);
2068 } elseif (isDebugModeEnabled()) {
2070 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',columnSql=' . $columnSql . ': does already exist.');
2074 // Add ALTER TABLE `foo` ADD INDEX sql if not found
2075 function addExtensionAddTableIndexSql ($tableName, $indexName, $columnSql) {
2076 // Is the column there?
2077 if (!ifSqlTableIndexExist('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase())) {
2079 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD INDEX `' . $indexName . '` ' . $columnSql);
2080 } elseif (isDebugModeEnabled()) {
2082 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ',columnSql=' . $columnSql . ': does already exist.');
2086 // Add ALTER TABLE `foo` ADD UNIQUE INDEX sql if not found
2087 function addExtensionAddTableUniqueSql ($tableName, $indexName, $columnSql) {
2088 // Is the column there?
2089 if (!ifSqlTableIndexExist('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase())) {
2091 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD UNIQUE INDEX `' . $indexName . '` ' . $columnSql);
2092 } elseif (isDebugModeEnabled()) {
2094 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ',columnSql=' . $columnSql . ': does already exist.');
2098 // Add ALTER TABLE `foo` ADD FULLTEXT sql if not found
2099 function addExtensionAddTableFulltextSql ($tableName, $indexName, $columnSql) {
2100 // Is the column there and MyISAM engine? (InnoDB doesn't support FULLTEXT)
2101 if ((getTableType() == 'MyISAM') && (!ifSqlTableIndexExist('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase()))) {
2103 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD FULLTEXT `' . $indexName . '` ' . $columnSql);
2104 } elseif (isDebugModeEnabled()) {
2106 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ',columnSql=' . $columnSql . ': does already exist.');
2110 // Add ALTER TABLE `foo` CHANGE sql if not found
2111 function addExtensionChangeTableColumnSql ($tableName, $fromColumnName, $toColumnName, $columnSql) {
2112 // Is the column there?
2113 if ((ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $fromColumnName)) && (($fromColumnName == $toColumnName) || (!ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $toColumnName, isInstallationPhase())))) {
2115 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` CHANGE `' . $fromColumnName . '` `' . $toColumnName . '` ' . $columnSql);
2116 } elseif (isDebugModeEnabled()) {
2118 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',fromColumnName=' . $fromColumnName . ',toColumnName=' . $toColumnName . ',columnSql=' . $columnSql . ': Cannot be changed.');
2122 // Add ALTER TABLE `foo` DROP sql if not found
2123 function addExtensionDropTableColumnSql ($tableName, $columnName) {
2124 // Is the column there?
2125 if (ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $columnName, isInstallationPhase())) {
2127 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` DROP `' . $columnName . '`');
2128 } elseif (isDebugModeEnabled()) {
2130 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ': does not exist.');
2134 // Add ALTER TABLE `foo` DROP INDEX sql if not found
2135 function addExtensionDropTableIndexSql ($tableName, $indexName) {
2136 // Is the column there?
2137 if (ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase())) {
2139 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` DROP INDEX `' . $indexName . '`');
2140 } elseif (isDebugModeEnabled()) {
2142 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ': does not exist.');
2146 // Add configuration entry if not found for actual extension
2147 function addConfigAddSql ($columnName, $columnSql) {
2148 // Call inner function
2149 addExtensionAddTableColumnSql('config', $columnName, $columnSql);
2152 // Drop configuration entry if found for actual extension
2153 function addConfigDropSql ($columnName) {
2154 // Call inner function
2155 addExtensionDropTableColumnSql('config', $columnName);
2158 // Change configuration entry for actual extension
2159 function addConfigChangeSql ($oldColumnName, $newColumnName, $columnSql) {
2160 // Add the SQL statement
2161 addExtensionChangeTableColumnSql('config', $oldColumnName, $newColumnName, $columnSql);
2165 * Checks if given subject is found and if not, adds an SQL query to the
2166 * extension registration queue.
2168 function registerExtensionPointsData ($subject, $columnName, $lockedMode, $paymentMethod) {
2169 // Default is old extension version
2172 // Is the extension equal or newer 0.8.9?
2173 if (((isInstallationPhase()) && ((getExtensionMode() == 'setup') || (getExtensionMode() == 'update'))) || (isExtensionInstalledAndNewer('sql_patches', '0.8.9'))) {
2174 // Then add provider
2175 $add = " AND `account_provider`='EXTENSION'";
2178 // Is the 'subject' there?
2179 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ifSqlTableExists(points_data)=' . ifSqlTableExists('points_data') . ',getExtensionMode()=' . getExtensionMode() . ',add=' . $add);
2180 if (((!ifSqlTableExists('points_data')) && ((getExtensionMode() == 'setup') || (getExtensionMode() == 'update'))) || (countSumTotalData($subject, 'points_data', 'id', 'subject', TRUE, $add) == 0)) {
2182 if ((!isInstallationPhase()) && (isset($GLOBALS['previous_extension'][getCurrentExtensionName()])) && (!ifSqlTableExists('points_data'))) {
2183 // This may happen (but when?)
2184 // @TODO Is this really neccessary?
2185 $dummy = $GLOBALS['previous_extension'][getCurrentExtensionName()];
2186 reportBug(__FUNCTION__, __LINE__, 'previous_extension[' . gettype($dummy) . ']=' . $dummy . ',getCurrentExtensionName()=' . getCurrentExtensionName() . ' - Under development, please report this!');
2189 // With or without account_provider?
2190 if (((isInstallationPhase()) && ((getExtensionMode() == 'setup') || (getExtensionMode() == 'update'))) || (isExtensionInstalledAndNewer('sql_patches', '0.8.9'))) {
2191 // Add account_provider
2192 addExtensionSql(sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`, `account_provider`) VALUES ('%s','%s','%s','%s','EXTENSION')",
2199 // ... add an SQL query
2200 addExtensionSql(sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`) VALUES ('%s','%s','%s','%s')",
2211 * Checks if given subject is found and if so, adds an SQL query to the
2212 * extension unregistration queue.
2214 function unregisterExtensionPointsData ($subject) {
2215 // Default is old extension version
2218 // Is the extension equal or newer 0.8.9?
2219 if (isExtensionInstalledAndNewer('sql_patches', '0.8.9')) {
2220 // Then add provider
2221 $add = " AND `account_provider`='EXTENSION'";
2224 // Is the 'subject' there?
2225 if (countSumTotalData($subject, 'points_data', 'id', 'subject', TRUE, $add) == 1) {
2226 // Found one or more, so add an SQL query
2227 addExtensionSql(sprintf("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s'" . $add . " LIMIT 1",
2233 // Enables/disables productive mode for current extension (used only while
2235 // @TODO This should be rewrittten to allow, more development states, e.g. 'planing','alpha','beta','beta2','stable'
2236 function enableExtensionProductive ($isProductive = TRUE) {
2237 // Log debug message
2238 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',isProductive=', intval($isProductive));
2241 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
2244 // Checks whether the extension is in productive phase. If not set, development
2245 // phase (=false) is assumed.
2246 function isExtensionProductive ($ext_name = '') {
2247 // Is the extension name empty? Then use current
2248 if (empty($ext_name)) {
2249 // Get current extension name
2250 $ext_name = getCurrentExtensionName();
2252 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
2255 if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
2256 // Load extension only if not yet loaded
2257 if (!isset($GLOBALS['ext_productive'][$ext_name])) {
2258 // Load extension in test mode
2259 loadExtension($ext_name, 'test');
2263 $GLOBALS[__FUNCTION__][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === TRUE));
2267 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isProductive=', intval($GLOBALS[__FUNCTION__][$ext_name]) . ' - EXIT!');
2268 return $GLOBALS[__FUNCTION__][$ext_name];
2271 // Mark extension file as loaded
2272 function markExtensionAsLoaded ($ext_name) {
2273 // Is it already loaded?
2274 if (isExtensionLoaded($ext_name)) {
2276 reportBug(__FUNCTION__, __LINE__, 'Extension ' . $ext_name . ' is already marked as loaded!');
2280 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_loaded=true');
2281 $GLOBALS['ext_loaded']['ext_name'][$ext_name] = TRUE;
2284 // Determine whether the given extension is already loaded
2285 function isExtensionLoaded ($ext_name) {
2287 return ((isset($GLOBALS['ext_loaded']['ext_name'][$ext_name])) && ($GLOBALS['ext_loaded']['ext_name'][$ext_name] === TRUE));
2290 // Mark extension's library file as loaded
2291 function markExtensionLibraryAsLoaded ($ext_name) {
2292 // Is it already loaded?
2293 if (isExtensionLibraryLoaded($ext_name)) {
2295 reportBug(__FUNCTION__, __LINE__, 'Extension library ' . $ext_name . ' is already marked as loaded!');
2299 $GLOBALS['ext_loaded']['library'][$ext_name] = TRUE;
2302 // Determine whether the given extension's library is already loaded
2303 function isExtensionLibraryLoaded ($ext_name) {
2305 return ((isset($GLOBALS['ext_loaded']['library'][$ext_name])) && ($GLOBALS['ext_loaded']['library'][$ext_name] === TRUE));
2308 // Copies the given extension's data to cache_array (USE THIS ONLY IN REGISTRATION PHASE!)
2309 function copyExtensionDataToCacheArray ($ext_name, $ext_id) {
2311 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $ext_id;
2312 $GLOBALS['cache_array']['extension']['ext_name'][$ext_id] = $ext_name;
2313 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = getCurrentExtensionVersion();
2314 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = getThisExtensionAlwaysActive();
2315 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = convertBooleanToYesNo(isExtensionLanguageFileReadable($ext_name));
2316 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = convertBooleanToYesNo(isExtensionFunctionFileReadable($ext_name));
2317 $GLOBALS['cache_array']['extension']['ext_menu'][$ext_name] = convertBooleanToYesNo(ifModuleHasMenu($ext_name, isInstallationPhase()));
2318 $GLOBALS['cache_array']['extension']['ext_css'][$ext_name] = convertBooleanToYesNo(getExtensionHasCss());
2319 $GLOBALS['cache_array']['extension']['ext_deprecated'][$ext_name] = 'N';
2322 // "Getter" for "checked="checked" from given session data
2323 function getExtensionSelectedFromSession ($ext_name, $sessionName, $separator = ':') {
2324 // Get the data and explode it
2325 $selections = explode($separator, getSession($sessionName));
2327 // Default is not checked
2331 if (in_array($ext_name, $selections)) {
2332 // It is, so mark it
2333 $checked = ' checked="checked"';