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 - 2012 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 != 'register')) {
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') && (ifExtensionHasLanguageFile($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 // Is this extension deprecated?
162 if ((isExtensionDeprecated()) && (!in_array(getExtensionMode(), array('test', 'update', 'deactivate'))) && (isExtensionActive($ext_name))) {
163 // Deactivate the extension
164 doDeactivateExtension($ext_name);
167 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension load aborted, ext_name=' . $ext_name . ' - Extension is deprecated.');
171 // Mark it as loaded in normal mode
172 if (getExtensionMode() == '') {
174 markExtensionAsLoaded($ext_name);
178 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension successfully loaded, ext_name=' . $ext_name);
182 // Registers an extension and possible update dependencies
183 function registerExtension ($ext_name, $taskId, $isDryRun = FALSE, $ignoreUpdates = FALSE, $previousExtension = NULL) {
184 // Set current extension name
185 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId=' . intval($taskId) . ',isDryRun=' . intval($isDryRun) . ',ignoreUpdates=' . intval($ignoreUpdates) . ' - ENTERED!');
186 setCurrentExtensionName($ext_name);
189 enableExtensionDryRun($isDryRun);
191 // By default all extensions are in productive phase
192 enableExtensionProductive();
194 // This shall never do a non-admin user or if the extension is active (already installed)
195 if (((!isAdmin()) && (!isInstallationPhase())) || (isExtensionInstalled($ext_name))) {
196 // Abort here with 'false'
197 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ABORTED: isAdmin()=' . intval(isAdmin()) . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ',isExtensionInstalled()=' . intval(isExtensionInstalled($ext_name)));
201 // When this extension is already in registration/update phase, all is fine
202 if ((isExtensionRegistrationRunning($ext_name)) || ((isExtensionUpdateRunning($ext_name)) && ($ignoreUpdates === FALSE))) {
203 // Then abort here with 'true' becaus it is fine
204 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - already in registration/update phase, all fine,taskId=' . $taskId . ',isDryRun=' . intval($isDryRun) . ',ignoreUpdates=' . intval($ignoreUpdates));
205 //* BUG: */ reportBug(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId=' . $taskId . ',isDryRun=' . intval($isDryRun) . ',ignoreUpdates=' . intval($ignoreUpdates) . ' - Please investigate!');
209 // This registration is running
210 addExtensionRunningRegistration($ext_name);
212 // Init EXT_UPDATE_DEPENDS if not yet done
213 if (!isExtensionUpdateDependenciesInitialized()) {
215 initExtensionUpdateDependencies();
218 // Is the task id zero? Then we need to auto-fix it here
219 if (((is_null($taskId)) || ($taskId == '0')) && (!isInstallationPhase())) {
220 // Try to find the task
221 $taskId = determineExtensionTaskId(getCurrentExtensionName());
223 // Still zero and not in dry-run?
224 if (((is_null($taskId)) || ($taskId == '0')) && (!isExtensionDryRun())) {
225 // Now try to create a new task
226 $taskId = createNewExtensionTask(getCurrentExtensionName());
229 if ((is_null($taskId)) || ($taskId == '0')) {
230 // Then request a bug report
231 reportBug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId/createNewExtensionTask(%s)",
233 getCurrentExtensionName()
239 // Init queries and notes
241 initExtensionNotes();
245 $processResult = FALSE;
246 initIncludePool('extension');
248 // By default we have no failures
249 enableExtensionReportingFailure();
251 // Does this extension exists?
252 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - CALLING loadExtension() ...');
253 if (loadExtension(getCurrentExtensionName(), 'register', '0.0.0', isExtensionDryRun(), $previousExtension)) {
254 // Set current extension name again
255 setCurrentExtensionName($ext_name);
257 // And run possible updates
258 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
259 $history = getExtensionVersionHistory();
260 foreach ($history as $ext_ver) {
261 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ext_ver);
262 // Load extension in update mode
263 loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun(), $previousExtension);
265 // Add update notes to our output
266 addExtensionNotes($ext_ver);
269 // Does this extension depends on an outstanding update of another update?
270 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
272 $ext_update = getExtensionUpdateDependenciesIterator();
274 // Increment here to avoid endless loop
275 incrementExtensionUpdateIterator();
277 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
278 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
279 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun(), $previousExtension))) {
280 // Set current extension name again
281 setCurrentExtensionName($ext_name);
283 // If versions mismatch update extension first
285 if (isExtensionInstalled($ext_update)) {
286 // Get version only if installed
287 $ext_ver = getExtensionVersion($ext_update);
290 // Extension version set? If empty the extension is not registered
291 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ',currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ' - EMPTY?');
292 if (empty($ext_ver)) {
293 // Extension not registered so far so first load task's id...
294 $updateTaskId = determineExtensionTaskId($ext_update);
297 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'updateTaskId=' . $updateTaskId . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ',currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ' - CHECKING!');
298 if (($updateTaskId > 0) || (isInstallationPhase())) {
299 // Try to register the extension
300 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ',updateTaskId=' . $updateTaskId . ',isExtensionDryRun()=' . intval(isExtensionDryRun()));
301 $processResult = registerExtension($ext_update, $updateTaskId, isExtensionDryRun(), TRUE, $ext_name);
303 // Reset extension name
304 setCurrentExtensionName($ext_name);
305 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ',processResult=' . intval($processResult));
307 } elseif ($ext_ver != getCurrentExtensionVersion()) {
308 // Ok, update this extension now
309 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
310 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
311 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
312 updateExtension($ext_update, $ext_ver, isExtensionDryRun());
313 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
314 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
315 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
316 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
317 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
320 $processResult = TRUE;
322 // Nothing to register / update before...
323 $processResult = TRUE;
326 // Required file for update does not exists!
327 $processResult = TRUE;
328 // But this is fine for the first time...
331 // Restore the current extension name
332 setCurrentExtensionName($ext_name);
335 // Is there no update?
336 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
337 // Then test is passed!
338 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',processResult=true,countExtensionUpdateDependencies()=0 - Test passed!');
339 $processResult = TRUE;
342 // Switch back to register mode
343 setExtensionMode('register');
345 // Remains true if extension registration reports no failures
346 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',processResult=' . intval($processResult));
347 $processResult = (($processResult === TRUE) && (isExtensionReportingFailure() === FALSE));
348 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',processResult=' . intval($processResult));
350 // Does everthing before wents ok?
351 if ($processResult === TRUE) {
352 // "Dry-run-mode" activated?
353 if ((isExtensionDryRun() === FALSE) && (!isExtensionOnRemovalList())) {
354 // Init SQLs and transfer ext->generic
355 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
357 setSqlsArray(getExtensionSqls());
359 // Run installation pre-installation filters
360 runFilterChain('pre_extension_installed', array('dry_run' => isExtensionDryRun(), 'ext_installing' => TRUE, 'enable_codes' => FALSE));
362 // Register extension
363 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
364 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
366 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getThisExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
367 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`, `ext_has_css`) VALUES ('%s','%s','%s','%s')",
369 getCurrentExtensionName(),
370 getThisExtensionAlwaysActive(),
371 getCurrentExtensionVersion(),
373 ), __FUNCTION__, __LINE__);
376 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getThisExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
377 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
379 getCurrentExtensionName(),
380 getThisExtensionAlwaysActive(),
381 getCurrentExtensionVersion()
382 ), __FUNCTION__, __LINE__);
385 // Use the insert id as extension id and cache it for early usage
386 $GLOBALS['cache_array']['extension']['ext_id'][getCurrentExtensionName()] = SQL_INSERTID();
387 $GLOBALS['cache_array']['extension']['ext_name'][SQL_INSERTID()] = getCurrentExtensionName();
389 // Mark it as installed
390 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = TRUE;
392 // Remove cache file(s) if extension is active
393 runFilterChain('post_extension_installed', array(
394 'pool' => 'extension',
395 'ext_name' => getCurrentExtensionName(),
399 // Re-init queries and notes
400 initExtensionSqls(TRUE);
401 initExtensionNotes(TRUE);
403 // In normal mode return a true on success
405 } elseif (isExtensionDryRun() === TRUE) {
406 // In "dry-run" mode do always return a true
409 // Extension has been removed for updates, so all is fine!
413 // No, an error occurs while registering extension :-(
414 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
417 } elseif (($taskId > 0) && (getCurrentExtensionName() != '')) {
418 // Remove task from system when id and extension's name is valid
419 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
420 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
421 array(bigintval($taskId)), __FUNCTION__, __LINE__);
424 // @TODO This redirect is still needed to register sql_patches! Please try to avoid it
425 if (($ret === TRUE) && ($isDryRun === FALSE) && ($ext_name == 'sql_patches') && (!isInstallationPhase())) {
427 * This is a really dirty hack to prevent an error about a missing
428 * configuration entry which should be there after registration of
431 redirectToRequestUri();
434 // Return status code
435 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',currName=' . getCurrentExtensionName() . ',processResult=' . intval($processResult) . ',ret=' . intval($ret) . ' - EXIT!');
439 // Run SQL queries for given extension id
440 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
441 function doExtensionSqls ($ext_id, $load_mode) {
442 // This shall never do a non-admin user but installation phase is okay
443 if ((!isAdmin()) && (!isInstallationPhase())) {
444 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_id=' . $ext_id. ',load_mode=' . $load_mode . ',isAdmin()=' . intval(isAdmin()) . ',isInstallationPhase()=' . intval(isInstallationPhase()) . ' - ABORT!');
448 // Get extension's name
449 $ext_name = getExtensionName($ext_id);
451 // Set current SQL name
452 setCurrentExtensionName($ext_name);
454 // Init EXT_UPDATE_DEPENDS
455 if (!isExtensionUpdateDependenciesInitialized()) {
457 initExtensionUpdateDependencies();
461 initExtensionSqls(TRUE);
463 // By default no SQL has been executed
466 // Load extension in detected mode
467 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name[' . $ext_id . ']=' . getCurrentExtensionName() . ',load_mode=' . $load_mode);
468 loadExtension(getCurrentExtensionName(), $load_mode, '0.0.0', FALSE);
472 setSqlsArray(getExtensionSqls());
475 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
477 // Are there entries?
479 // Run SQL commands...
480 runFilterChain('run_sqls');
483 // Run any filters depending on the action here
484 runFilterChain('extension_' . $load_mode);
486 // Remove cache file(s) if extension is active
487 if (((isExtensionActive('cache')) && ((!SQL_HASZEROAFFECTED()) || ($sqlRan === TRUE) || ($load_mode == 'activate') || ($load_mode == 'deactivate')))) {
489 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
492 // Is this the sql_patches?
493 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $ext_id . ',currName=' . getCurrentExtensionName() . ',loadMode=' . $load_mode);
494 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
495 // Then redirect to logout
496 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ': LOAD!');
497 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
501 // Check whether the given extension is installed
502 function isExtensionInstalled ($ext_name) {
503 // We don't like empty extension names here
504 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
505 if (empty($ext_name)) {
506 // Please fix them all
507 reportBug(__FUNCTION__, __LINE__, 'ext_name is empty.');
510 // By default non is installed
511 $isInstalled = FALSE;
513 // Check if there is a cache entry
514 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
515 // Use cache built from below queries
516 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - CACHE!');
517 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
518 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
520 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - FOUND!');
524 incrementStatsEntry('cache_hits');
525 } elseif (isInstallationPhase()) {
526 // Extensions are all inactive/not installed during installation
527 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - installation phase detected.');
530 $ext_id = getExtensionId($ext_name);
532 // Is there a record?
533 $isInstalled = ($ext_id > 0);
536 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_id=' . $ext_id . ',isInstalled=' . intval($isInstalled));
538 // Is it installed, then cache the entry
539 if ($isInstalled === TRUE) {
540 // Dummy call (get is okay here)
541 getExtensionId($ext_name, TRUE);
544 // Remember the status
545 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
549 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isInstalled=' . intval($isInstalled) . ' - EXIT!');
553 // Check if given extension is active
554 function isExtensionActive ($ext_name) {
555 if (isInstallationPhase()) {
556 // Extensions are all inactive during installation
557 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extensions are always inactive while installation phase. ext_name=' . $ext_name);
559 } elseif (empty($ext_name)) {
560 // Empty extension names must befixed
561 reportBug(__FUNCTION__, __LINE__, 'Empty extension name provided.');
562 } elseif (!isExtensionInstalled($ext_name)) {
563 // Not installed extensions are always inactive
564 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not installed extensions are always inactive. ext_name=' . $ext_name);
568 // Not active is the default
569 $data['ext_active'] = 'N';
572 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
574 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE! ext_name=' . $ext_name);
575 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
578 incrementStatsEntry('cache_hits');
579 } elseif (isExtensionLoaded($ext_name)) {
580 // @TODO Extension is loaded, what next?
581 reportBug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
582 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
583 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DB! ext_name=' . $ext_name);
584 // Load from database
585 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
586 array($ext_name), __FUNCTION__, __LINE__);
589 if (SQL_NUMROWS($result) == 1) {
591 $data = SQL_FETCHARRAY($result);
595 SQL_FREERESULT($result);
598 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . '[DB]: ' . $data['ext_active']);
599 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
601 // Extension not active!
602 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': Not active!');
603 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
607 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',active=' . $data['ext_active']);
609 // Is this extension activated? (For admins we always have active extensions...)
610 return ($data['ext_active'] == 'Y');
613 // Get version from extensions
614 function getExtensionVersion ($ext_name, $force = FALSE) {
615 // By default no extension is found
616 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
617 $data['ext_version'] = 'false';
619 // Empty extension name should be fixed!
620 if (empty($ext_name)) {
621 // Please report this bug!
622 reportBug(__FUNCTION__, __LINE__, 'ext_name is empty which is not allowed here.');
625 // Extensions are all inactive during installation
626 if (isInstallationPhase()) {
627 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',force=' . intval($force) . ' - Installation phase detected, returning empty version.');
631 // Is the cache written?
632 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - Checking cache ...');
633 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
634 // Load data from cache
635 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - CACHE!');
636 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
639 incrementStatsEntry('cache_hits');
640 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (!isHtmlOutputMode())) {
641 // Load from database
642 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
643 array($ext_name), __FUNCTION__, __LINE__);
644 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ': DB - '.SQL_NUMROWS($result).'');
646 // Is the extension there?
647 if (SQL_NUMROWS($result) == 1) {
649 $data = SQL_FETCHARRAY($result);
652 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
653 } elseif (isDebugModeEnabled()) {
654 // Not found, may happen while an extension is uninstalled
655 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot find extension %s in database!", $ext_name));
659 SQL_FREERESULT($result);
662 // Extension version should not be invalid
663 if (($data['ext_version'] == 'false') && ($force === FALSE)) {
664 // Please report this trouble
665 reportBug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
669 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_version=' . $data['ext_version']);
670 return $data['ext_version'];
673 // Updates a given extension with current extension version to latest version
674 function updateExtension ($ext_name, $ext_ver, $isDryRun = FALSE, $ignoreDependencies = FALSE) {
675 // Only admins are allowed to update extensions
676 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_ver=' . $ext_ver . ',isDryRun=' . intval($isDryRun) . ',ignoreDependencies=' . intval($ignoreDependencies) . ' - ENTERED!');
677 if ((!isAdmin()) || (empty($ext_name))) {
678 // Called as non-admin or empty extension
679 reportBug(__FUNCTION__, __LINE__, 'Called as non-admin (isAdmin()=' . intval(isAdmin()) . '), or empty extension name. ext_name=' . $ext_name);
682 // Set current SQL name
683 setCurrentExtensionName($ext_name);
685 // Is this extension update already running?
686 if ((isExtensionUpdateRunning($ext_name, $ignoreDependencies)) && ($isDryRun === FALSE)) {
687 // This is fine but needs logging ATM
688 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - already in update phase, all fine.');
689 //* BUG: */ reportBug(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - already in update phase, please investigate!');
695 initExtensionNotes();
696 initIncludePool('extension');
698 // Load extension in test mode
699 loadExtension($ext_name, 'test', $ext_ver, isExtensionDryRun());
701 // Save version history
702 $history = getExtensionVersionHistory();
704 // Remove old SQLs array to prevent possible bugs
707 // Check if version is updated
708 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, getCurrentExtensionName() . '/' . $ext_name . ':' . getThisExtensionVersion() . '/' . $ext_ver . '/' . intval(is_array($history)));
709 if (((getThisExtensionVersion() != $ext_ver) || (isExtensionDryRun())) && (is_array($history))) {
710 // Search for starting point (-1 for making 0.0 -> 0.0.0 switch work)
712 if ($ext_ver != '0.0') {
713 $start = array_search($ext_ver, $history);
716 // And load SQL queries in order of version history
717 for ($idx = ($start + 1); $idx < count($history); $idx++) {
718 // Set extension version
719 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
722 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], isExtensionDryRun());
725 $depencies = getExtensionUpdateDependencies();
728 if (count($depencies) > 0) {
729 // Apply all extension depencies
730 foreach ($depencies as $ext_depend) {
731 // Did we already update/register this?
732 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
734 setCurrentExtensionName($ext_depend);
736 // Mark it as already updated before we update it
737 $GLOBALS['ext_updated'][$ext_depend] = TRUE;
739 // Is the extension there?
740 if (isExtensionInstalled($ext_depend)) {
741 // Update another extension first!
742 $processResult = updateExtension($ext_depend, getExtensionVersion($ext_depend), isExtensionDryRun(), TRUE);
744 // Register new extension
745 $processResult = registerExtension($ext_depend, NULL, isExtensionDryRun());
751 setCurrentExtensionName($ext_name);
753 // Set extension version here
754 setCurrentExtensionVersion($ext_ver);
758 addExtensionNotes($history[$idx]);
761 // In real-mode execute any existing includes
762 if (isExtensionDryRun() === FALSE) {
763 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
764 runFilterChain('load_includes', 'extension');
765 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
766 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
771 setSqlsArray(getExtensionSqls());
774 runFilterChain('run_sqls', array('dry_run' => isExtensionDryRun(), 'ext_installing' => TRUE, 'enable_codes' => FALSE));
776 if (isExtensionDryRun() === FALSE) {
777 // Run filters on success extension update
778 runFilterChain('extension_update', getCurrentExtensionName());
782 //* DEBUG: */logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_ver=' . $ext_ver . ',isDryRun=' . intval($isDryRun) . ',ignoreDependencies=' . intval($ignoreDependencies) . ' - EXIT!');
785 // Output verbose SQL table for extension
786 function addExtensionVerboseSqlTable ($title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVAL--}') {
790 reportBug(__FUNCTION__, __LINE__, 'title is empty.');
797 if (isVerboseSqlEnabled()) {
798 // Are there entries?
799 if (countExtensionSqls() > 0) {
803 foreach (getExtensionSqls() as $sqls) {
804 // New array format is recursive
805 foreach ($sqls as $sql) {
809 // Output command if set
811 // Prepare output for template
814 'sql' => str_replace(array('{', '}'), array('{', '}'), encodeEntities($sql))
818 $OUT .= loadTemplate('admin_extension_sql_row', TRUE, $content);
826 // Prepare content for template
832 // Load main template
833 $OUT = loadTemplate('admin_extension_sql_table', TRUE, $content);
835 // No addional SQL commands to run
836 $OUT = displayMessage('{--ADMIN_EXTENSION_VERBOSE_SQLS_404--}', TRUE);
844 // Get extension name from id
845 function getExtensionName ($ext_id) {
846 // Init extension name
847 $data['ext_name'] = '';
850 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
852 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
855 incrementStatsEntry('cache_hits');
856 } elseif (!isExtensionActive('cache')) {
857 // Load from database
858 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
859 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
861 // Is the entry there?
862 if (SQL_NUMROWS($result) == 1) {
863 // Get the extension's name from database
864 $data = SQL_FETCHARRAY($result);
868 SQL_FREERESULT($result);
871 // Did we find some extension?
872 if (empty($data['ext_name'])) {
873 // We should fix these all!
874 reportBug(__FUNCTION__, __LINE__, 'ext_name is empty. ext_id=' . $ext_id);
877 // Return the extension name
878 return $data['ext_name'];
881 // Get extension id from name
882 function getExtensionId ($ext_name) {
884 $data['ext_id'] = '0';
887 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
889 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
892 incrementStatsEntry('cache_hits');
894 // Load from database
895 $result = SQL_QUERY_ESC("SELECT `id` AS `ext_id` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
896 array($ext_name), __FUNCTION__, __LINE__);
898 // Is the entry there?
899 if (SQL_NUMROWS($result) == 1) {
900 // Get the extension's id from database
901 $data = SQL_FETCHARRAY($result);
905 SQL_FREERESULT($result);
908 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
912 return $data['ext_id'];
915 // Determines whether the given extension name is valid
916 function isExtensionNameValid ($ext_name) {
918 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
919 // Generate include file name
920 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
922 // Is there a file in inc/extensions/ ?
923 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
927 return $GLOBALS['ext_name_valid'][$ext_name];
930 // Determines whether the given extension id is valid
931 function isExtensionIdValid ($ext_id) {
932 // Default is nothing valid
935 // Check in cache then in database
936 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
941 incrementStatsEntry('cache_hits');
944 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', TRUE) == 1);
951 // Activate given extension
952 function doActivateExtension ($ext_name) {
953 // Is the extension installed?
954 if (!isExtensionInstalled($ext_name)) {
955 // Non-installed extensions cannot be activated
956 reportBug(__FUNCTION__, __LINE__, 'Tried to activate non-installed extension ' . $ext_name);
959 // Activate the extension
960 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
961 array($ext_name), __FUNCTION__, __LINE__);
963 // Then run all queries
964 doExtensionSqls(getExtensionId($ext_name), 'activate');
967 // Deactivate given extension
968 function doDeactivateExtension ($ext_name, $inRebuild = FALSE) {
969 // Is the extension installed?
970 if (!isExtensionInstalled($ext_name)) {
971 // Non-installed extensions cannot be activated
972 reportBug(__FUNCTION__, __LINE__, 'Tried to deactivate non-installed extension ' . $ext_name . ',getExtensionMode()=' . getExtensionMode());
975 // Activate the extension
976 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
977 array($ext_name), __FUNCTION__, __LINE__);
979 // Then run all queries
980 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
982 // Create new task (we ignore the task id here)
983 createExtensionDeactivationTask($ext_name);
985 // Do not rebuild cache if it is already been rebuild
986 if ($inRebuild === FALSE) {
988 rebuildCache('extension', 'extension');
992 sendAdminNotification(
993 '{--ADMIN_EXTENSION_DEACTIVATED_SUBJECT--}',
994 'admin_extension_deactivated',
995 array('ext_name' => $ext_name)
999 // Creates a new task for updated extension
1000 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
1001 // Create subject line
1002 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJECT--}';
1005 $taskId = determineTaskIdBySubject($subject);
1007 // Is the extension there?
1008 if (isExtensionInstalled($ext_name)) {
1009 // Check if task is not there
1010 if ($taskId == '0') {
1011 // Create extension update-task
1012 $taskId = createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
1015 // Extension not there! :-(
1016 reportBug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
1023 // Creates a new task for newly installed extension
1024 function createNewExtensionTask ($ext_name) {
1025 // Generate subject line
1026 $subject = sprintf("[%s:]", $ext_name);
1029 $taskId = determineTaskIdBySubject($subject);
1031 // Not installed and do we have created a task for the admin?
1032 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId[' . gettype($taskId) . ']=' . $taskId);
1033 if (((is_null($taskId)) || ($taskId == '0')) && (!isExtensionInstalled($ext_name))) {
1034 // Set default message if ext-foo is missing
1035 $message = '{%message,ADMIN_EXTENSION_TEXT_FILE_MISSING=' . $ext_name . '%}';
1038 $FQFN = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
1044 // Load text for task if found
1045 if (isFileReadable($FQFN)) {
1046 // Load extension's description template (but do not compile the code)
1047 $message = loadTemplate('ext_' . $ext_name, TRUE, array(), FALSE);
1049 // Write this in debug.log as well
1050 logDebugMessage(__FUNCTION__, __LINE__, $message);
1053 // Task not created so it's a brand-new extension which we need to register and create a task for!
1054 $taskId = createNewTask($subject, $message, 'EXTENSION', 0, getCurrentAdminId(), FALSE);
1061 // Creates a task for automatically deactivated (deprecated) extension
1062 function createExtensionDeactivationTask ($ext_name) {
1063 // Create subject line
1064 $subject = sprintf("[%s:] %s", $ext_name, '{--ADMIN_TASK_EXTENSION_DEACTIVATED_SUBJECT--}');
1067 $taskId = determineTaskIdBySubject($subject);
1069 // Not installed and do we have created a task for the admin?
1070 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId[' . gettype($taskId) . ']=' . $taskId);
1071 if (((is_null($taskId)) || ($taskId == '0')) && (isExtensionInstalled($ext_name))) {
1072 // Task not created so add it
1073 $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', TRUE, $ext_name)), 'EXTENSION_DEACTIVATION');
1080 // Determines the task id for given extension
1081 function determineExtensionTaskId ($ext_name) {
1082 // Is it installation phase and table task_system is not found?
1083 if ((isInstallationPhase()) && (!ifSqlTableExists('task_system'))) {
1084 // Then return NULL (not found)
1088 // Default is not found
1089 $data['task_id'] = NULL;
1091 // Search for extension task's id
1092 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1093 array($ext_name), __FUNCTION__, __LINE__);
1096 if (SQL_NUMROWS($result) == 1) {
1097 // Task found so load task's id and register extension...
1098 $data = SQL_FETCHARRAY($result);
1102 SQL_FREERESULT($result);
1105 return $data['task_id'];
1108 // Determines the task id for given subject
1109 function determineTaskIdBySubject ($subject) {
1110 // Default is not found
1111 $data['task_id'] = '0';
1113 // Search for task id
1114 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1115 array($subject), __FUNCTION__, __LINE__);
1118 if (SQL_NUMROWS($result) == 1) {
1119 // Task found so load task's id and register extension...
1120 $data = SQL_FETCHARRAY($result);
1124 SQL_FREERESULT($result);
1127 return $data['task_id'];
1130 // Add updates notes for given version
1131 function addExtensionNotes ($ext_ver) {
1132 // Init notes/content
1136 // Is do we have verbose output enabled?
1137 if ((!isExtensionActive('sql_patches')) || (isVerboseSqlEnabled())) {
1138 // Update notes found?
1139 if ((isExtensionUpdateNoteSet($ext_ver)) && ($ext_ver != '0.0.0')) {
1140 // Update notes found
1143 'notes' => getExtensionUpdateNotes($ext_ver)
1147 setExtensionUpdateNotes('', $ext_ver);
1148 } elseif ($ext_ver == '0.0.0') {
1149 // Is the extension productive?
1150 if (isExtensionProductive(getCurrentExtensionName())) {
1154 'notes' => '{--ADMIN_EXTENSION_INITIAL_RELEASE--}'
1160 'notes' => '{--ADMIN_EXTENSION_DEVELOPER_RELEASE--}'
1164 // No update notes found
1167 'notes' => '{--ADMIN_EXTENSION_UPDATE_NOTICES_404--}'
1172 $out = loadTemplate('admin_extension_notes', TRUE, $content);
1176 appendExtensionNotes($out);
1179 // Getter for CSS files array
1180 function getExtensionCssFiles () {
1181 // By default no additional CSS files are found
1182 $cssFiles = array();
1184 // Is the array there?
1185 if (isset($GLOBALS['css_files'])) {
1187 $cssFiles = $GLOBALS['css_files'];
1194 // Init CSS files array
1195 function initExtensionCssFiles () {
1197 $GLOBALS['css_files'] = array();
1201 function addExtensionCssFile ($file) {
1202 // Is the array there?
1203 if (!isset($GLOBALS['css_files'])) {
1204 // Then auto-init them
1205 initExtensionCssFiles();
1209 array_push($GLOBALS['css_files'], $file);
1212 // Setter for EXT_ALWAYS_ACTIVE flag
1213 function setExtensionAlwaysActive ($active) {
1214 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1217 // Getter for EXT_ALWAYS_ACTIVE flag
1218 function getThisExtensionAlwaysActive () {
1219 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1222 // Checks whether the current extension is always active
1223 function isThisExtensionAlwaysActive () {
1224 return (getThisExtensionAlwaysActive() == 'Y');
1227 // Setter for EXT_VERSION flag
1228 function setThisExtensionVersion ($ext_version) {
1229 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $ext_version;
1232 // Getter for EXT_VERSION flag
1233 function getThisExtensionVersion () {
1234 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1237 // Setter for EXT_DEPRECATED flag
1238 function setExtensionDeprecated ($deprecated) {
1239 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1242 // Getter for EXT_DEPRECATED flag
1243 function isExtensionDeprecated ($ext_name = NULL) {
1244 // Default is from current (NULL) extension
1245 $isDeprecated = ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1248 if (!is_null($ext_name)) {
1249 // Then use it instead
1250 $isDeprecated = ((isset($GLOBALS['ext_deprecated'][$ext_name])) && ($GLOBALS['ext_deprecated'][$ext_name] == 'Y'));
1254 return $isDeprecated;
1257 // Setter for EXT_UPDATE_DEPENDS flag
1258 function addExtensionDependency ($updateDepends) {
1259 // Is the update depency empty? (NEED TO BE FIXED!)
1260 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ',extensionMode=' . getExtensionMode() . ' - ENTERED!');
1261 if (empty($updateDepends)) {
1262 // Please report this bug!
1263 reportBug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1266 // Is it not yet added?
1267 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1269 * Double-adding happens when the extension and an update of the same
1270 * extension requires the same other extension again.
1272 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'updateDepends=' . $updateDepends . ',extensionMode=' . getExtensionMode() . ',currentExtension=' . getCurrentExtensionName() . ' - called twice.');
1276 // Add it to the list of extension update depencies map
1277 array_push($GLOBALS['ext_update_depends'][getCurrentExtensionName()], $updateDepends);
1280 if ((!isset($GLOBALS['ext_running_updates'][getCurrentExtensionName()])) || (!is_array($GLOBALS['ext_running_updates'][getCurrentExtensionName()]))) {
1281 $GLOBALS['ext_running_updates'][getCurrentExtensionName()] = array();
1284 // Remember it in the list of running updates
1285 array_push($GLOBALS['ext_running_updates'][getCurrentExtensionName()], $updateDepends);
1286 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ',extensionMode=' . getExtensionMode() . ' - EXIT!');
1289 // Getter for running updates
1290 function getExtensionUpdatesRunning () {
1291 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1294 // Checks whether the given extension registration is in progress
1295 function isExtensionRegistrationRunning ($ext_name) {
1297 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1300 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1304 // Init EXT_UPDATE_DEPENDS flag
1305 function initExtensionUpdateDependencies () {
1306 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1308 // Init update depency map automatically if not found
1309 if (isExtensionUpdateDependenciesInitialized()) {
1310 // We need these bug reports as well...
1311 reportBug(__FUNCTION__, __LINE__, '() is called twice: currName=' . getCurrentExtensionName());
1314 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1316 // Init running updates array
1317 initExtensionRuningUpdates();
1319 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1322 // Adds an extension as "registration in progress"
1323 function addExtensionRunningRegistration ($ext_name) {
1325 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1326 if (isExtensionRegistrationRunning($ext_name)) {
1327 // This is really bad and should not be quietly ignored
1328 reportBug(__FUNCTION__, __LINE__, '() already called! ext_name=' . $ext_name);
1332 array_push($GLOBALS['ext_register_running'], $ext_name);
1333 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1336 // Checks whether EXT_UPDATE_DEPENDS is initialized
1337 function isExtensionUpdateDependenciesInitialized () {
1338 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1339 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1342 // Checks whether an update is already running for given extension
1343 function isExtensionUpdateRunning ($ext_name, $ignoreDependencies = FALSE) {
1344 // Current and given extensions means whole array
1345 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',ignoreDependencies=' . intval($ignoreDependencies) . ' - ENTERED!');
1346 if ($ext_name == getCurrentExtensionName()) {
1347 // Default is not found
1350 // Walk through whole array
1351 foreach ($GLOBALS['ext_running_updates'] as $ext1 => $depends) {
1353 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',depends=' . print_r($depends, TRUE));
1354 if (($ext1 == $ext_name) || ((in_array($ext_name, $depends)) && ($ignoreDependencies === FALSE))) {
1357 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=true - FOUND!');
1363 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',ignoreDependencies=' . intval($ignoreDependencies) . ', isRunning=' . intval($isRunning) . ' - ALT-EXIT!');
1368 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1371 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ',ignoreDependencies=' . intval($ignoreDependencies) . ', isRunning=' . intval($isRunning) . ' - EXIT!');
1375 // Initializes the list of running updates
1376 function initExtensionRuningUpdates () {
1377 // Auto-init ext_running_updates
1378 if (!isset($GLOBALS['ext_running_updates'])) {
1379 $GLOBALS['ext_running_updates'] = array();
1380 $GLOBALS['ext_register_running'] = array();
1384 // Getter for EXT_UPDATE_DEPENDS flag
1385 function getExtensionUpdateDependencies () {
1386 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1387 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1390 // Getter for next iterator depency
1391 function getExtensionUpdateDependenciesIterator () {
1392 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1393 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1396 // Counter for extension update depencies
1397 function countExtensionUpdateDependencies () {
1398 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '=' . count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1399 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1402 // Removes given extension from update denpency list
1403 function removeExtensionDependency ($ext_name) {
1405 $key = array_search($ext_name, getExtensionUpdateDependencies());
1408 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',key[' . gettype($key) . ']=' . $key);
1411 if ($key !== FALSE) {
1413 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1415 // And sort the array
1416 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1420 // Init iterator for update depencies
1421 function initExtensionUpdateIterator () {
1422 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1423 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1426 // Getter for depency iterator
1427 function getExtensionUpdateIterator () {
1428 // Auto-init iterator
1429 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) {
1430 // Initialize update iterator
1431 initExtensionUpdateIterator();
1435 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '=' . $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1436 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1439 // Increments the update iterator
1440 function incrementExtensionUpdateIterator () {
1441 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1442 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1445 // Setter for EXT_REPORTS_FAILURE flag
1446 function enableExtensionReportingFailure ($reportsFailure = FALSE) {
1447 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1450 // Getter for EXT_REPORTS_FAILURE flag
1451 function isExtensionReportingFailure () {
1452 return $GLOBALS['ext_reports_failure'];
1455 // Setter for EXT_VER_HISTORY flag
1456 function setExtensionVersionHistory ($versionHistory) {
1457 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $versionHistory;
1460 // Getter for EXT_VER_HISTORY array
1461 function getExtensionVersionHistory () {
1463 if (!isset($GLOBALS['ext_ver_history'][getCurrentExtensionName()])) {
1464 // Then abort here to a avoid an ugly "Undefined index" error
1465 reportBug(__FUNCTION__, __LINE__, 'Extension ext-' . getCurrentExtensionName() . ' has no history set! Is this extension empty?');
1468 // Return the history
1469 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1472 // Setter for EXT_UPDATE_NOTICES
1473 function setExtensionUpdateNotes ($updateNotes, $ext_ver = '') {
1475 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ',getExtensionMode()=' . getExtensionMode() . ',ext_ver=' . $ext_ver . '/' . getCurrentExtensionVersion() . ',updateNotes(length)=' . strlen($updateNotes));
1476 if (empty($ext_ver)) {
1477 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1479 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver] = (string) $updateNotes;
1483 // Getter for EXT_UPDATE_NOTICES
1484 function getExtensionUpdateNotes ($ext_ver) {
1485 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver];
1488 // Checks if ext_update_notes is set
1489 function isExtensionUpdateNoteSet ($ext_ver) {
1490 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ext_ver]);
1493 // Init extension notice
1494 function initExtensionNotes ($force = FALSE) {
1495 // Is it already initialized?
1496 if (($force === FALSE) && (isset($GLOBALS['ext_notes'][getCurrentExtensionName()]))) {
1497 // This is mostly not wanted, so please report it
1498 reportBug(__FUNCTION__, __LINE__, 'ext_notes already set for extension ' . getCurrentExtensionName());
1501 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName());
1502 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1505 // Append extension notice
1506 function appendExtensionNotes ($notes) {
1507 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()=' . getCurrentExtensionName() . ', notes(length)=' . strlen($notes));
1508 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1511 // Getter for extension notes
1512 function getExtensionNotes () {
1513 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1516 // Setter for current extension name
1517 function setCurrentExtensionName ($ext_name) {
1518 $GLOBALS['curr_extension_name'] = (string) trim($ext_name);
1521 // Getter for current extension name
1522 function getCurrentExtensionName () {
1523 if (!isset($GLOBALS['curr_extension_name'])) {
1525 reportBug(__FUNCTION__, __LINE__, 'curr_extension_name not initialized. Please execute initExtensionSqls() before calling this function.');
1529 return $GLOBALS['curr_extension_name'];
1532 // Init SQLs array for current extension
1533 function initExtensionSqls ($force = FALSE) {
1534 // Auto-init the array or if forced
1535 if (($force === TRUE) || (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()]))) {
1537 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1539 // Initialize the generic array
1544 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1545 function addExtensionSql ($sql) {
1546 // Copy current name/version to local variable
1547 $currentName = getCurrentExtensionName();
1548 $currentVersion = getCurrentExtensionVersion();
1550 // Is is the array there?
1551 if ((!isset($GLOBALS['ext_sqls'][$currentName][$currentVersion])) || (!is_array($GLOBALS['ext_sqls'][$currentName][$currentVersion]))) {
1553 $GLOBALS['ext_sqls'][$currentName][$currentVersion] = array();
1556 // Is the SQL statement empty?
1559 * Abort here as this may happen if getExtensionMode() is 'activate' or
1560 * 'deactivate'. This means that for 'mode' are no SQL statements
1567 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $currentName . ',ext_version=' . $currentVersion . ',sql=' . $sql);
1568 array_push($GLOBALS['ext_sqls'][$currentName][$currentVersion], $sql);
1571 // Getter for SQLs array for current extension
1572 function getExtensionSqls () {
1573 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1574 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1575 // Not found, should not happen
1576 reportBug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1577 getCurrentExtensionName()
1582 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1585 // Count SQLs for current extension
1586 function countExtensionSqls () {
1587 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1588 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1589 // Not found, should not happen
1590 reportBug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1591 getCurrentExtensionName()
1596 return count($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1599 // Removes SQLs for current extension
1600 function unsetExtensionSqls () {
1601 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1604 // Auto-initializes the removal list
1605 function initExtensionRemovalList () {
1606 // Is the remove list there?
1607 if (!isset($GLOBALS['ext_update_remove'])) {
1609 $GLOBALS['ext_update_remove'] = array();
1613 // Checks whether the current extension is on the removal list
1614 function isExtensionOnRemovalList () {
1615 // Init removal list
1616 initExtensionRemovalList();
1619 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1622 // Adds the current extension to the removal list
1623 function addCurrentExtensionToRemovalList () {
1625 array_push($GLOBALS['ext_update_remove'], getCurrentExtensionName());
1628 // Getter for removal list
1629 function getExtensionRemovalList () {
1630 // Return the removal list
1631 return $GLOBALS['ext_update_remove'];
1634 // Redirects if the provided extension is not installed
1635 function redirectOnUninstalledExtension ($ext_name) {
1636 // So is the extension there?
1637 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1638 // Redirect to index
1639 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1643 // Filter for initialization of all extensions by loading them in 'init' mode
1644 function FILTER_INIT_EXTENSIONS () {
1645 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTERED!');
1646 // Init notification pool
1647 initIncludePool('notify');
1649 // Are there some entries?
1650 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1651 // Load all found extensions if found
1652 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
1653 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1655 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - START');
1656 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1657 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - END');
1659 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - END!');
1662 // Run any notifications
1663 runFilterChain('load_includes', 'notify');
1665 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'EXIT!');
1668 // Setter for extension mode
1669 function setExtensionMode ($ext_mode) {
1670 $GLOBALS['ext_mode'] = (string) $ext_mode;
1673 // Getter for extension mode
1674 function getExtensionMode () {
1675 return $GLOBALS['ext_mode'];
1678 // Setter for dry-run
1679 function enableExtensionDryRun ($isDryRun = TRUE) {
1680 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getCurrentExtensionName()='.getCurrentExtensionName().',isDryRun='.intval($isDryRun));
1681 $GLOBALS['ext_isDryRun'] = (bool) $isDryRun;
1684 // Getter for dry-run
1685 function isExtensionDryRun () {
1686 return $GLOBALS['ext_isDryRun'];
1689 // Setter for current extension version
1690 function setCurrentExtensionVersion ($ext_ver) {
1691 // ext_ver should never be empty in other modes than 'test'
1692 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1693 // Please report all these messages
1694 reportBug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1698 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1699 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1702 // Getter for current extension version
1703 function getCurrentExtensionVersion () {
1704 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1705 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1708 // Remove the extension from cache array
1709 function removeExtensionFromArray () {
1710 // "Cache" this name
1711 $ext_name = getCurrentExtensionName();
1713 // Now loop through the whole cache
1714 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1715 // Is it an element?
1716 if (isset($cacheArray[$ext_name])) {
1718 unset($cacheArray[$ext_name]);
1719 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1722 $key = array_search($ext_name, $cacheArray);
1725 if ($key !== FALSE) {
1726 // Found, so remove it
1727 unset($cacheArray[$key]);
1728 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1733 // Remove from other caches as well
1734 unset($GLOBALS['ext_is_installed'][$ext_name]);
1735 unset($GLOBALS['loaded_extension'][$ext_name]);
1738 // "Getter" for 'extension has a CSS file' (with same name, of course)
1739 function getExtensionHasCss () {
1741 if (!isset($GLOBALS[__FUNCTION__][getCurrentExtensionName()][getCurrentTheme()])) {
1742 // Construct FQFN for check
1743 $FQFN = sprintf("%stheme/%s/css/%s.css",
1746 getCurrentExtensionName()
1750 $GLOBALS[__FUNCTION__][getCurrentExtensionName()][getCurrentTheme()] = convertBooleanToYesNo(isFileReadable($FQFN));
1754 return $GLOBALS[__FUNCTION__][getCurrentExtensionName()][getCurrentTheme()];
1757 // Checks whether the given extension has a language file
1758 function ifExtensionHasLanguageFile ($ext_name) {
1760 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1762 incrementStatsEntry('cache_hits');
1764 // Determine it and put it in cache
1765 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = convertBooleanToYesNo(isLanguageIncludeReadable($ext_name));
1769 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1772 // Load current extension's include file
1773 function loadCurrentExtensionInclude () {
1775 if (!isExtensionIncludeReadable()) {
1777 reportBug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1780 // Generate INC name
1781 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1787 // Checks whether an extension is readable
1788 function isExtensionIncludeReadable ($ext_name = '') {
1789 // If empty, use current
1790 if (empty($ext_name)) {
1791 $ext_name = getCurrentExtensionName();
1795 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1796 // Generate INC name
1797 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1800 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1804 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1805 return $GLOBALS['ext_inc_readable'][$ext_name];
1808 // Checks if an extension's function file is readable
1809 function isExtensionFunctionFileReadable ($ext_name) {
1811 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name);
1812 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1813 // Just count cache hits
1814 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] .' - CACHE!');
1815 incrementStatsEntry('cache_hits');
1817 // Construct IFN for functions file
1818 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1820 // Is this include there?
1821 $isIncludeFound = ((isFileReadable($funcsInclude)) && (!isExtensionLibraryLoaded($ext_name)) && (getExtensionMode() == 'test'));
1823 // And put in cache, converted
1824 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = convertBooleanToYesNo($isIncludeFound);
1828 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_func=' . $GLOBALS['cache_array']['extension']['ext_func'][$ext_name]);
1829 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1832 // Adds a CREATE TABLE statement if the requested table is not there
1833 function addCreateTableSql ($tableName, $sql, $comment) {
1834 // Is the table not there?
1835 if (!ifSqlTableExists($tableName)) {
1836 // Is not found, so add it
1837 addExtensionSql('CREATE TABLE
1838 `{?_MYSQL_PREFIX?}_' . $tableName . '` (' . $sql . ')
1839 ENGINE = {?_TABLE_TYPE?}
1841 COLLATE utf8_general_ci
1842 COMMENT ' . chr(39) . $comment . chr(39));
1845 logDebugMessage(__FUNCTION__, __LINE__, 'The table ' . $tableName . ' is already created.');
1849 // Adds a DROP TABLE statement if the requested tabled is there
1850 function addDropTableSql ($tableName) {
1851 // Is the table there?
1852 if (ifSqlTableExists($tableName)) {
1854 * Then add it, non-existing tables can be ignored because it will
1855 * happen with every newly installed extension.
1857 addExtensionSql('DROP TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '`');
1860 $GLOBALS['ifSqlTableExists'][$tableName] = FALSE;
1864 // Adds a RENAME TABLE stament if 'from' table exist and 'to' table not
1865 function addRenameTableSql ($fromTable, $toTable) {
1866 // Make sure both are not the same
1867 assert($fromTable != $toTable);
1869 // Is renaming required?
1870 if ((ifSqlTableExists($fromTable)) && (!ifSqlTableExists($toTable))) {
1872 addExtensionSql('RENAME TABLE `{?_MYSQL_PREFIX?}_' . $fromTable . '` TO `{?_MYSQL_PREFIX?}_' . $toTable . '`');
1875 $GLOBALS['ifSqlTableExists'][$fromTable] = FALSE;
1876 $GLOBALS['ifSqlTableExists'][$toTable] = TRUE;
1880 // Adds an admin menu to the SQL queue of the menu entry is not found
1881 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1882 // Now check if this menu is there
1883 if (!isMenuActionValid('admin', $action, $what)) {
1885 if (is_null($what)) {
1887 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`, `what`, `title`, `descr`, `sort`) VALUES ('%s',NULL,'%s','%s',%s)",
1895 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`, `what`, `title`, `descr`, `sort`) VALUES ('%s','%s','%s','%s',%s)",
1904 // Add it to the queue
1905 addExtensionSql($sql);
1906 } elseif (isDebugModeEnabled()) {
1907 // Double menus should be located and fixed!
1908 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
1912 // Adds a guest menu to the SQL queue if the menu entry is not found
1913 function addGuestMenuSql ($action, $what, $title, $sort) {
1914 // Now check if this menu is there
1915 if (!isMenuActionValid('guest', $action, $what)) {
1917 if (is_null($what)) {
1919 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s',NULL,'%s','N','Y',%s)",
1926 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s','%s','%s','N','Y',%s)",
1934 // Add it to the queue
1935 addExtensionSql($sql);
1936 } elseif (isDebugModeEnabled()) {
1937 // Double menus should be located and fixed!
1938 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
1942 // Adds a member menu to the SQL queue if the menu entry is not found
1943 function addMemberMenuSql ($action, $what, $title, $sort) {
1944 // Now check if this menu is there
1945 if (!isMenuActionValid('member', $action, $what)) {
1947 if (is_null($what)) {
1949 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s',NULL,'%s','N','Y',%s)",
1956 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`, `what`, `title`, `visible`, `locked`, `sort`) VALUES ('%s','%s','%s','N','Y',%s)",
1964 // Add it to the queue
1965 addExtensionSql($sql);
1966 } elseif (isDebugModeEnabled()) {
1967 // Double menus should be located and fixed!
1968 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s,what=%s,title=%s detected.", $action, $what, $title));
1972 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1973 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1974 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1975 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1977 if (is_null($what)) {
1979 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`, `what`, `title`, `active`, `sort`) VALUES ('%s',NULL,'%s','%s',%s)",
1987 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`, `what`, `title`, `active`, `sort`) VALUES ('%s','%s','%s','%s',%s)",
1996 // Add it to the queue
1997 addExtensionSql($sql);
1998 } elseif (isDebugModeEnabled()) {
1999 // Double menus should be located and fixed!
2000 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s,what=%s,title=%s,active=%s detected.", $action, $what, $title, $active));
2004 // Add ALTER TABLE `foo` ADD sql if not found
2005 function addExtensionAddTableColumnSql ($tableName, $columnName, $columnSql) {
2006 // Is the column there?
2007 if (!ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $columnName, isInstallationPhase())) {
2009 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD `' . $columnName . '` ' . $columnSql);
2010 } elseif (isDebugModeEnabled()) {
2012 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',columnSql=' . $columnSql . ': does already exist.');
2016 // Add ALTER TABLE `foo` ADD INDEX sql if not found
2017 function addExtensionAddTableIndexSql ($tableName, $indexName, $columnSql) {
2018 // Is the column there?
2019 if (!ifSqlTableIndexExist('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase())) {
2021 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD INDEX `' . $indexName . '` ' . $columnSql);
2022 } elseif (isDebugModeEnabled()) {
2024 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ',columnSql=' . $columnSql . ': does already exist.');
2028 // Add ALTER TABLE `foo` ADD UNIQUE INDEX sql if not found
2029 function addExtensionAddTableUniqueSql ($tableName, $indexName, $columnSql) {
2030 // Is the column there?
2031 if (!ifSqlTableIndexExist('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase())) {
2033 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD UNIQUE INDEX `' . $indexName . '` ' . $columnSql);
2034 } elseif (isDebugModeEnabled()) {
2036 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ',columnSql=' . $columnSql . ': does already exist.');
2040 // Add ALTER TABLE `foo` ADD FULLTEXT sql if not found
2041 function addExtensionAddTableFulltextSql ($tableName, $indexName, $columnSql) {
2042 // Is the column there and MyISAM engine? (InnoDB doesn't support FULLTEXT)
2043 if ((getTableType() == 'MyISAM') && (!ifSqlTableIndexExist('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase()))) {
2045 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` ADD FULLTEXT `' . $indexName . '` ' . $columnSql);
2046 } elseif (isDebugModeEnabled()) {
2048 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ',columnSql=' . $columnSql . ': does already exist.');
2052 // Add ALTER TABLE `foo` CHANGE sql if not found
2053 function addExtensionChangeTableColumnSql ($tableName, $fromColumnName, $toColumnName, $columnSql) {
2054 // Is the column there?
2055 if ((ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $fromColumnName)) && (($fromColumnName == $toColumnName) || (!ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $toColumnName, isInstallationPhase())))) {
2057 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` CHANGE `' . $fromColumnName . '` `' . $toColumnName . '` ' . $columnSql);
2058 } elseif (isDebugModeEnabled()) {
2060 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',fromColumnName=' . $fromColumnName . ',toColumnName=' . $toColumnName . ',columnSql=' . $columnSql . ': Cannot be changed.');
2064 // Add ALTER TABLE `foo` DROP sql if not found
2065 function addExtensionDropTableColumnSql ($tableName, $columnName) {
2066 // Is the column there?
2067 if (ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $columnName, isInstallationPhase())) {
2069 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` DROP `' . $columnName . '`');
2070 } elseif (isDebugModeEnabled()) {
2072 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ': does not exist.');
2076 // Add ALTER TABLE `foo` DROP INDEX sql if not found
2077 function addExtensionDropTableIndexSql ($tableName, $indexName) {
2078 // Is the column there?
2079 if (ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_' . $tableName, $indexName, isInstallationPhase())) {
2081 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_' . $tableName . '` DROP INDEX `' . $indexName . '`');
2082 } elseif (isDebugModeEnabled()) {
2084 logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',indexName=' . $indexName . ': does not exist.');
2088 // Add configuration entry if not found for actual extension
2089 function addConfigAddSql ($columnName, $columnSql) {
2090 // Is the column there?
2091 if (!ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_config', $columnName)) {
2092 // Not found, so add it
2093 addExtensionAddTableColumnSql('config', $columnName, $columnSql);
2094 } elseif (isDebugModeEnabled()) {
2096 logDebugMessage(__FUNCTION__, __LINE__, 'Configuration entry ' . $columnName . ' already created. columnSql=' . $columnSql);
2100 // Drop configuration entry if found for actual extension
2101 function addConfigDropSql ($columnName) {
2102 // Is the column there?
2103 if (ifSqlTableColumnExists('{?_MYSQL_PREFIX?}_config', $columnName)) {
2105 addExtensionDropTableColumnSql('config', $columnName);
2106 } elseif (isDebugModeEnabled()) {
2107 // Add debug line, reportBug() would cause some extenion updates fail
2108 logDebugMessage(__FUNCTION__, __LINE__, 'Configuration entry ' . $columnName . ' not found.');
2112 // Change configuration entry for actual extension
2113 function addConfigChangeSql ($oldColumnName, $newColumnName, $columnSql) {
2114 // Add the SQL statement
2115 addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_config` CHANGE `' . $oldColumnName . '` `' . $newColumnName . '` ' . $columnSql);
2119 * Checks if given subject is found and if not, adds an SQL query to the
2120 * extension registration queue.
2122 function registerExtensionPointsData ($subject, $columnName, $lockedMode, $paymentMethod) {
2123 // Default is old extension version
2126 // Is the extension equal or newer 0.8.9?
2127 if (((isInstallationPhase()) && ((getExtensionMode() == 'register') || (getExtensionMode() == 'update'))) || (isExtensionInstalledAndNewer('sql_patches', '0.8.9'))) {
2128 // Then add provider
2129 $add = " AND `account_provider`='EXTENSION'";
2132 // Is the 'subject' there?
2133 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ifSqlTableExists(points_data)=' . ifSqlTableExists('points_data') . ',getExtensionMode()=' . getExtensionMode() . ',add=' . $add);
2134 if (((!ifSqlTableExists('points_data')) && ((getExtensionMode() == 'register') || (getExtensionMode() == 'update'))) || (countSumTotalData($subject, 'points_data', 'id', 'subject', TRUE, $add) == 0)) {
2136 if ((isset($GLOBALS['previous_extension'][getCurrentExtensionName()])) && (!ifSqlTableExists('points_data'))) {
2137 $dummy = $GLOBALS['previous_extension'][getCurrentExtensionName()];
2138 reportBug(__FUNCTION__, __LINE__, 'previous_extension[' . gettype($dummy) . ']=' . $dummy . ',getCurrentExtensionName()=' . getCurrentExtensionName() . ' - Under development, please report this!');
2141 // ... add an SQL query
2142 addExtensionSql(sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`) VALUES ('%s','%s','%s','%s')",
2152 * Checks if given subject is found and if so, adds an SQL query to the
2153 * extension unregistration queue.
2155 function unregisterExtensionPointsData ($subject) {
2156 // Default is old extension version
2159 // Is the extension equal or newer 0.8.9?
2160 if (isExtensionInstalledAndNewer('sql_patches', '0.8.9')) {
2161 // Then add provider
2162 $add = " AND `account_provider`='EXTENSION'";
2165 // Is the 'subject' there?
2166 if (countSumTotalData($subject, 'points_data', 'id', 'subject', TRUE, $add) == 1) {
2167 // Found one or more, so add an SQL query
2168 addExtensionSql(sprintf("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s'" . $add . " LIMIT 1",
2174 // Enables/disables productive mode for current extension (used only while
2176 // @TODO This should be rewrittten to allow, more development states, e.g. 'planing','alpha','beta','beta2','stable'
2177 function enableExtensionProductive ($isProductive = TRUE) {
2178 // Log debug message
2179 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',isProductive=', intval($isProductive));
2182 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
2185 // Checks whether the extension is in productive phase. If not set, development
2186 // phase (=false) is assumed.
2187 function isExtensionProductive ($ext_name = '') {
2188 // Is the extension name empty? Then use current
2189 if (empty($ext_name)) {
2190 // Get current extension name
2191 $ext_name = getCurrentExtensionName();
2193 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
2196 if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
2197 // Load extension only if not yet loaded
2198 if (!isset($GLOBALS['ext_productive'][$ext_name])) {
2199 // Load extension in test mode
2200 loadExtension($ext_name, 'test');
2204 $GLOBALS[__FUNCTION__][$ext_name] = ((isset($GLOBALS['ext_productive'][$ext_name])) && ($GLOBALS['ext_productive'][$ext_name] === TRUE));
2208 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isProductive=', intval($GLOBALS[__FUNCTION__][$ext_name]) . ' - EXIT!');
2209 return $GLOBALS[__FUNCTION__][$ext_name];
2212 // Mark extension file as loaded
2213 function markExtensionAsLoaded ($ext_name) {
2214 // Is it already loaded?
2215 if (isExtensionLoaded($ext_name)) {
2217 reportBug(__FUNCTION__, __LINE__, 'Extension ' . $ext_name . ' is already marked as loaded!');
2221 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_loaded=true');
2222 $GLOBALS['ext_loaded']['ext_name'][$ext_name] = TRUE;
2225 // Determine whether the given extension is already loaded
2226 function isExtensionLoaded ($ext_name) {
2228 return ((isset($GLOBALS['ext_loaded']['ext_name'][$ext_name])) && ($GLOBALS['ext_loaded']['ext_name'][$ext_name] === TRUE));
2231 // Mark extension's library file as loaded
2232 function markExtensionLibraryAsLoaded ($ext_name) {
2233 // Is it already loaded?
2234 if (isExtensionLibraryLoaded($ext_name)) {
2236 reportBug(__FUNCTION__, __LINE__, 'Extension library ' . $ext_name . ' is already marked as loaded!');
2240 $GLOBALS['ext_loaded']['library'][$ext_name] = TRUE;
2243 // Determine whether the given extension's library is already loaded
2244 function isExtensionLibraryLoaded ($ext_name) {
2246 return ((isset($GLOBALS['ext_loaded']['library'][$ext_name])) && ($GLOBALS['ext_loaded']['library'][$ext_name] === TRUE));