2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/25/2009 *
4 * =================== Last change: 10/25/2009 *
6 * -------------------------------------------------------------------- *
7 * File : extensions-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Extension management *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Erweiterungen-Management *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Load the extension and maybe found language and function files.
46 function loadExtension ($ext_name, $ext_mode = 'init', $ext_ver = '', $dry_run = false) {
47 // If this happens twice, we need the bug report from you, except for updates/tests
48 if ((isset($GLOBALS['loaded_extension'][$ext_name][$ext_mode])) && (!in_array($ext_mode, array('update', 'test')))) {
49 debug_report_bug(__FUNCTION__ . '() is called twice: ext_name=' . $ext_name . ', ext_mode='. $ext_mode . ',ext_sqls=' . print_r(getExtensionSqls(), true) . ', ext_register_running=' . print_r($GLOBALS['ext_register_running'], true) . ', ext_running_updates=' . print_r($GLOBALS['ext_running_updates'], true));
52 // Make sure this situation can only happen once
53 $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = true;
56 setExtensionMode($ext_mode);
58 // Set current extension name
59 setCurrentExtensionName($ext_name);
61 if (!empty($ext_ver)) {
62 // Set current extension version
63 setCurrentExtensionVersion($ext_ver);
66 setCurrentExtensionVersion('0.0');
68 // In all but test-mode we need these messages to debug! Please report all (together, e.g.)
69 if ($ext_mode != 'test') {
70 // Log empty versions not in test-mode, but maybe it is fine...
71 logDebugMessage(__FUNCTION__, __LINE__, 'Extension version is empty, setting to 0.0. ext_name=' . $ext_name . ', ext_mode=' . $ext_mode . ', dry_run=' . intval($dry_run));
76 enableExtensionDryRun($dry_run);
79 initIncludePool('extension');
81 // Init EXT_UPDATE_DEPENDS if not yet done
82 if (!isExtensionUpdateDependenciesInitialized()) {
84 initExtensionUpdateDependencies();
87 // Init current extension name list
90 // Is the extension already loaded?
91 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Loading extension ' . $ext_name . ', mode=' . getExtensionMode() . ', ver=' . getCurrentExtensionVersion());
92 if ((isset($GLOBALS['ext_loaded']['ext'][$ext_name])) && (getExtensionMode() == 'init')) {
94 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
100 // Is the extension file NOT there?
101 if (!isExtensionNameValid($ext_name)) {
103 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Extension %s not found or not readable.", $ext_name));
109 // Load extension's own language file if not in test mode
110 if ((getExtensionMode() != 'test') && (ifExtensionHasLanguageFile($ext_name))) {
112 loadLanguageFile($ext_name);
116 if (isExtensionFunctionFileReadable($ext_name)) {
118 if ((($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y') || (!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name]))) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name]))) {
119 // Construct FQFN for functions file
120 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
123 $GLOBALS['ext_loaded']['funcs'][$ext_name] = true;
125 // Download functions file
126 loadIncludeOnce($funcsInclude);
128 } elseif ((!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) && (isDebugModeEnabled()) && (getOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme') && (getExtensionMode() == 'test')) {
129 // No functions file is not so good...
130 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no own functions file or we cannot read from it. mode=%s",
136 // Extensions are not deprecated by default
137 setExtensionDeprecated('N');
139 // Extensions are not always active by default
140 setExtensionAlwaysActive('N');
142 // Extension update notes
143 // @TODO Do we still need this? setExtensionUpdateNotes('');
145 // Include the extension file
146 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Extension loaded.');
147 loadExtensionInclude();
149 // Is this extension deprecated?
150 if (isExtensionDeprecated()) {
151 // Deactivate the extension
152 doDeactivateExtension($ext_name);
158 // Mark it as loaded in normal mode
159 if (getExtensionMode() == '') {
161 $GLOBALS['ext_loaded']['ext'][$ext_name] = true;
168 // Registeres an extension and possible update depencies
169 function registerExtension ($ext_name, $task_id, $dry_run = false, $logout = true) {
170 // Set current extension name
171 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',task_id=' . $task_id . ',dry_run=' . intval($dry_run) . ',logout=' . intval($logout) . ' - ENTERED!');
172 setCurrentExtensionName($ext_name);
175 enableExtensionDryRun($dry_run);
177 // By default all extensions are in productive phase
178 enableExtensionProductive();
180 // This shall never do a non-admin user or if the extension is active (already installed)
181 if ((!isAdmin()) || (isExtensionInstalled($ext_name))) {
182 // Abort here with 'false'
186 // When this extension is already in registration/update phase, all is fine
187 if ((isExtensionRegistrationRunning($ext_name)) || (isExtensionUpdateRunning($ext_name))) {
188 // Then abort here with 'true' becaus it is fine
189 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ALREADY!');
193 // This registration is running
194 addExtensionRunningRegistration($ext_name);
196 // Init EXT_UPDATE_DEPENDS if not yet done
197 if (!isExtensionUpdateDependenciesInitialized()) {
199 initExtensionUpdateDependencies();
202 // Is the task id zero? Then we need to auto-fix it here
203 if ($task_id == '0') {
204 // Try to find the task
205 $task_id = determineExtensionTaskId(getCurrentExtensionName());
207 // Still zero and not in dry-run?
208 if (($task_id == '0') && (!getExtensionDryRun())) {
209 // Then request a bug report
210 debug_report_bug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
212 getCurrentExtensionName()
217 // Init queries and notes
219 initExtensionNotes();
224 initIncludePool('extension');
226 // By default we have no failures
227 setExtensionReportsFailure(false);
229 // Does this extension exists?
230 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
231 if (loadExtension(getCurrentExtensionName(), 'register', '', getExtensionDryRun())) {
232 // Set current extension name again
233 setCurrentExtensionName($ext_name);
235 // And run possible updates
236 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
237 $history = getExtensionVersionHistory();
238 foreach ($history as $ver) {
239 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ver);
240 // Load extension in update mode
241 loadExtension(getCurrentExtensionName(), 'update', $ver, getExtensionDryRun());
243 // Add update notes to our output
244 addExtensionNotes($ver);
247 // Does this extension depends on an outstanding update of another update?
248 for ($dmy = getExtensionUpdateIterator(); getExtensionUpdateIterator() < countExtensionUpdateDependencies();) {
250 $ext_update = getExtensionUpdateDependenciesIterator();
252 // Increment here to avoid endless loop
253 incrementExtensionUpdateIterator();
255 // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
256 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
257 if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', getExtensionDryRun()))) {
258 // Set current extension name again
259 setCurrentExtensionName($ext_name);
261 // If versions mismatch update extension first
263 if (isExtensionInstalled($ext_update)) {
264 // Get version only if installed
265 $ext_ver = getExtensionVersion($ext_update);
268 // Extension version set? If empty the extension is not registered
269 if (empty($ext_ver)) {
270 // Extension not registered so far so first load task's id...
271 $task = determineExtensionTaskId($ext_update);
275 // Try to register the extension
276 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ':ext_update=' . $ext_update . ',taskId=' . $task);
277 $test = registerExtension($ext_update, $task, getExtensionDryRun(), false);
279 // Reset extension name
280 setCurrentExtensionName($ext_name);
281 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
282 //* DEBUG: */ print getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>';
284 } elseif ($ext_ver != getCurrentExtensionVersion()) {
285 // Ok, update this extension now
286 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
287 $GLOBALS['ext_backup_name'][$ext_update][$ext_ver] = getCurrentExtensionName();
288 $GLOBALS['ext_backup_ver'][$ext_update][$ext_ver] = getCurrentExtensionVersion();
289 updateExtension($ext_update, $ext_ver, getExtensionDryRun());
290 setCurrentExtensionName($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
291 setCurrentExtensionVersion($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
292 unset($GLOBALS['ext_backup_name'][$ext_update][$ext_ver]);
293 unset($GLOBALS['ext_backup_ver'][$ext_update][$ext_ver]);
294 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',currVer=' . getCurrentExtensionVersion());
299 // Nothing to register / update before...
303 // Required file for update does not exists!
305 // But this is fine for the first time...
308 // Restore the current extension name
309 setCurrentExtensionName($ext_name);
312 // Is there no update?
313 if (countExtensionUpdateDependencies(getCurrentExtensionName()) == 0) {
314 // Then test is passed!
318 // Switch back to register mode
319 setExtensionMode('register');
321 // Remains true if extension registration reports no failures
322 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
323 //* DEBUG: */ print getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>';
324 $test = (($test === true) && (getExtensionReportsFailure() === false));
325 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
326 //* DEBUG: */ print getCurrentExtensionName().':<pre>' . print_r($test, true) . '</pre>';
328 // Does everthing before wents ok?
329 if ($test === true) {
330 // "Dry-run-mode" activated?
331 if ((getExtensionDryRun() === false) && (!isExtensionOnRemovalList())) {
332 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName());
333 // Init SQLs and transfer ext->generic
335 setSqlsArray(getExtensionSqls());
337 // Run installation pre-installation filters
338 runFilterChain('pre_extension_installed', array('dry_run' => getExtensionDryRun()));
340 // Register extension
341 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'insert=' . getCurrentExtensionName() . '/' . getCurrentExtensionVersion() . ' - INSERT!');
342 if (isExtensionInstalledAndNewer('sql_patches', '0.0.6')) {
343 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion() . 'ext_css=' . getExtensionHasCss());
345 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`,`ext_has_css`) VALUES ('%s','%s','%s','%s')",
347 getCurrentExtensionName(),
348 getExtensionAlwaysActive(),
349 getCurrentExtensionVersion(),
351 ), __FUNCTION__, __LINE__);
353 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',always_active=' . getExtensionAlwaysActive() . ', ext_ver=' . getCurrentExtensionVersion());
355 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_extensions` (`ext_name`, `ext_active`, `ext_version`) VALUES ('%s','%s','%s')",
357 getCurrentExtensionName(),
358 getExtensionAlwaysActive(),
359 getCurrentExtensionVersion()
360 ), __FUNCTION__, __LINE__);
363 // Remove cache file(s) if extension is active
364 runFilterChain('post_extension_installed', array(
365 'pool' => 'extension',
366 'ext_name' => getCurrentExtensionName(),
367 'task_id' => $task_id
370 // Re-init queries and notes
371 initExtensionSqls(true);
372 initExtensionNotes();
374 // Mark it as installed
375 $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true;
377 // In normal mode return a true on success
379 } elseif (getExtensionDryRun() === true) {
380 // In "dry-run" mode return array with all SQL commands
381 $ret = getExtensionSqls();
383 // Re-init queries and notes
384 initExtensionSqls(true);
385 initExtensionNotes();
387 // Extension has been removed for updates, so all is fine!
391 // No, an error occurs while registering extension :-(
392 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName());
395 } elseif (($task_id > 0) && (getCurrentExtensionName() != '')) {
396 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
397 // Remove task from system when id and extension's name is valid
398 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `id`=%s AND `status`='NEW' LIMIT 1",
399 array(bigintval($task_id)), __FUNCTION__, __LINE__);
402 // Is this the sql_patches?
403 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . '/' . getExtensionMode());
404 if ((getCurrentExtensionName() == 'sql_patches') && ((getExtensionMode() == 'register') || (getExtensionMode() == 'remove')) && (!getExtensionDryRun()) && ($test)) {
405 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ': LOAD!');
406 if ($logout === true) {
407 // Then redirect to logout
408 redirectToUrl('modules.php?module=admin&logout=1&' . getExtensionMode() . '=sql_patches');
410 // Add temporary filter
411 registerFilter('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
412 $GLOBALS['ext_load_mode'] = getExtensionMode();
416 // Return status code
417 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ' - EXIT!');
418 //* DEBUG: */ print getCurrentExtensionName().':<pre>'.print_r($ret, true).'</pre>';
422 // Run SQL queries for given extension id
423 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
424 function doExtensionSqls ($ext_id, $load_mode) {
425 // This shall never do a non-admin user!
426 if (!isAdmin()) return false;
428 // Get extension's name
429 $ext_name = getExtensionName($ext_id);
431 // Set current SQL name
432 setCurrentExtensionName($ext_name);
434 // Init EXT_UPDATE_DEPENDS
435 if (!isExtensionUpdateDependenciesInitialized()) {
437 initExtensionUpdateDependencies();
443 // By default no SQL has been executed
446 // Load extension in detected mode
447 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]=".getCurrentExtensionName()."");
448 loadExtension(getCurrentExtensionName(), $load_mode, '', false);
452 setSqlsArray(getExtensionSqls());
454 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ":SQLs::count=".countSqls()."");
456 // Run SQL commands...
457 runFilterChain('run_sqls');
460 // Run any filters depending on the action here
461 runFilterChain('extension_' . $load_mode);
463 // Remove cache file(s) if extension is active
464 if (((isExtensionActive('cache')) && ((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == 'activate') || ($load_mode == 'deactivate'))) {
466 runFilterChain('post_extension_run_sql', getCurrentExtensionName());
469 // Is this the sql_patches?
470 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": id=".$ext_id.",currName=".getCurrentExtensionName().",loadMode=".$load_mode);
471 if ((getCurrentExtensionName() == 'sql_patches') && (($load_mode == 'register') || ($load_mode == 'remove'))) {
472 // Then redirect to logout
473 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": LOAD!");
474 redirectToUrl('modules.php?module=admin&logout=1&' . $load_mode . '=sql_patches');
478 // Check wether the given extension is installed
479 function isExtensionInstalled ($ext_name) {
480 // We don't like empty extension names here
481 if (empty($ext_name)) {
482 // Please fix them all
483 debug_report_bug(__FUNCTION__.': ext_name is empty.');
486 // By default non is installed
487 $isInstalled = false;
489 // Check if there is a cache entry
490 if (isset($GLOBALS['ext_is_installed'][$ext_name])) {
491 // Use cache built from below queries
492 $isInstalled = $GLOBALS['ext_is_installed'][$ext_name];
493 } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
498 incrementStatsEntry('cache_hits');
499 } elseif ((isInstallationPhase())) {
500 // Extensions are all inactive/not installed during installation
503 $ext_id = getExtensionId($ext_name);
505 // Do we have a record?
506 $isInstalled = ($ext_id > 0);
508 // Is it installed, then cache the entry
509 if ($isInstalled === true) {
510 // Dummy call (get is okay here)
511 getExtensionId($ext_name, true);
514 // Remember the status
515 $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled;
522 // Check if given extension is active
523 function isExtensionActive ($ext_name) {
524 // Extensions are all inactive during installation
525 if ((isInstallationPhase()) || (empty($ext_name))) return false;
527 // Not active is the default
528 $data['ext_active'] = 'N';
531 if (isset($GLOBALS['cache_array']['extension']['ext_active'][$ext_name])) {
533 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
534 $data['ext_active'] = $GLOBALS['cache_array']['extension']['ext_active'][$ext_name];
537 incrementStatsEntry('cache_hits');
538 } elseif (isset($GLOBALS['ext_loaded'][$ext_name])) {
539 // @TODO Extension is loaded, what next?
540 debug_report_bug(__FUNCTION__, __LINE__, 'LOADED:' . $ext_name);
541 } elseif (($ext_name == 'cache') || (!isExtensionInstalled('cache'))) {
542 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
543 // Load from database
544 $result = SQL_QUERY_ESC("SELECT `ext_active` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
545 array($ext_name), __FUNCTION__, __LINE__);
548 if (SQL_NUMROWS($result) == 1) {
550 $data = SQL_FETCHARRAY($result);
554 SQL_FREERESULT($result);
557 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name."[DB]: ".$data['ext_active']."");
558 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = $data['ext_active'];
560 // Extension not active!
561 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.": Not active!");
562 $GLOBALS['cache_array']['extension']['ext_active'][$ext_name] = 'N';
566 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "ext_name=".$ext_name.",active=".$data['ext_active']."");
568 // Is this extension activated? (For admins we always have active extensions...)
569 return ($data['ext_active'] == 'Y');
572 // Get version from extensions
573 function getExtensionVersion ($ext_name, $force = false) {
574 // By default no extension is found
575 $data['ext_version'] = 'false';
577 // Empty extension name should be fixed!
578 if (empty($ext_name)) {
579 // Please report this bug!
580 debug_report_bug(__FUNCTION__ . ': ext_name is empty which is not allowed here.');
583 // Extensions are all inactive during installation
584 if (isInstallationPhase()) return '';
585 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ext_name=".$ext_name."");
587 // Is the cache written?
588 if (isset($GLOBALS['cache_array']['extension']['ext_version'][$ext_name])) {
589 // Load data from cache
590 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': CACHE!');
591 $data['ext_version'] = $GLOBALS['cache_array']['extension']['ext_version'][$ext_name];
594 incrementStatsEntry('cache_hits');
595 } elseif ((!isCacheInstanceValid()) || (isset($GLOBALS['cache_array']['extension'])) || (getOutputMode() != 0)) {
596 // Load from database
597 $result = SQL_QUERY_ESC("SELECT `ext_version` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
598 array($ext_name), __FUNCTION__, __LINE__);
599 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $ext_name . ': DB - '.SQL_NUMROWS($result).'');
601 // Is the extension there?
602 if (SQL_NUMROWS($result) == 1) {
604 $data = SQL_FETCHARRAY($result);
605 } elseif (isDebugModeEnabled()) {
606 // Not found, please report all
607 debug_report_bug(__FUNCTION__, __LINE__, sprintf(": Cannot find extension %s in database!", $ext_name));
611 SQL_FREERESULT($result);
614 $GLOBALS['cache_array']['extension']['ext_version'][$ext_name] = $data['ext_version'];
617 // Extension version should not be invalid
618 if (($data['ext_version'] == 'false') && ($force === false)) {
619 // Please report this trouble
620 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> has empty version!", $ext_name));
624 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ": ret={$data['ext_version']}");
625 return $data['ext_version'];
628 // Updates a given extension with current extension version to latest version
629 function updateExtension ($ext_name, $ext_ver, $dry_run = false) {
630 // Only admins are allowed to update extensions
631 if ((!isAdmin()) || (empty($ext_name))) return false;
633 // Set current SQL name
634 setCurrentExtensionName($ext_name);
638 initExtensionNotes();
639 initIncludePool('extension');
641 // Load extension in test mode
642 loadExtension($ext_name, 'test', $ext_ver, getExtensionDryRun());
644 // Save version history
645 $history = getExtensionVersionHistory();
647 // Remove old SQLs array to prevent possible bugs
650 // Check if version is updated
651 //* DEBUG: */ print getCurrentExtensionName().'/'.$ext_name.':'.getThisExtensionVersion().'/'.$ext_ver.'/'.intval(is_array($history)).'<br />';
652 if (((getThisExtensionVersion() != $ext_ver) || (getExtensionDryRun())) && (is_array($history))) {
653 // Search for starting point
654 $start = array_search($ext_ver, $history);
656 // And load SQL queries in order of version history
657 for ($idx = ($start + 1); $idx < count($history); $idx++) {
658 // Set extension version
659 $GLOBALS['update_ver'][getCurrentExtensionName()] = $history[$idx];
662 loadExtension(getCurrentExtensionName(), 'update', $GLOBALS['update_ver'][getCurrentExtensionName()], getExtensionDryRun());
665 $depencies = getExtensionUpdateDependencies();
668 if (count($depencies) > 0) {
669 // Apply all extension depencies
670 foreach ($depencies as $ext_depend) {
671 // Did we already update/register this?
672 if (!isset($GLOBALS['ext_updated'][$ext_depend])) {
674 setCurrentExtensionName($ext_depend);
676 // Mark it as already updated before we update it
677 $GLOBALS['ext_updated'][$ext_depend] = true;
679 // Is the extension there?
680 if (isExtensionInstalled($ext_depend)) {
681 // Update another extension first!
682 $test = updateExtension($ext_depend, getExtensionVersion($ext_depend), getExtensionDryRun());
684 // Register new extension
685 $test = registerExtension($ext_depend, 0, getExtensionDryRun(), false);
690 // Set extension version here
691 setCurrentExtensionVersion($ext_ver);
694 setCurrentExtensionName($ext_name);
698 addExtensionNotes($history[$idx]);
701 // In real-mode execute any existing includes
702 if (getExtensionDryRun() === false) {
703 $GLOBALS['ext_inc_pool'][getCurrentExtensionName()] = getIncludePool('extension');
704 runFilterChain('load_includes', 'extension');
705 setIncludePool('extension', $GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
706 unset($GLOBALS['ext_inc_pool'][getCurrentExtensionName()]);
711 setSqlsArray(getExtensionSqls());
714 runFilterChain('run_sqls', array('dry_run' => getExtensionDryRun()));
716 if (getExtensionDryRun() === false) {
717 // Run filters on success extension update
718 runFilterChain('extension_update', getCurrentExtensionName());
723 // Output verbose SQL table for extension
724 function addExtensionVerboseSqlTable ($title = '', $dashed = '', $switch = false, $width = '100%') {
727 // Then fix it to default
728 $title = getMessage('ADMIN_SQLS_EXECUTED_ON_REMOVAL');
734 // Do we have queries?
735 if ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
740 foreach (getExtensionSqls() as $sqls) {
741 // New array format is recursive
742 foreach ($sqls as $idx => $sql) {
746 // Output command if set
748 // Prepare output for template
756 $OUT .= loadTemplate('admin_ext_sql_row', true, $content);
764 // Prepare content for template
772 // Load main template
773 $OUT = loadTemplate('admin_ext_sql_table', true, $content);
774 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.0.7')) && (getConfig('verbose_sql') == 'Y')) {
775 // No addional SQL commands to run
776 $OUT = loadTemplate('admin_settings_saved', true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
783 // Get extension name from id
784 function getExtensionName ($ext_id) {
785 // Init extension name
786 $data['ext_name'] = '';
789 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
791 $data['ext_name'] = $GLOBALS['cache_array']['extension']['ext_name'][$ext_id];
794 incrementStatsEntry('cache_hits');
795 } elseif (!isExtensionActive('cache')) {
796 // Load from database
797 $result = SQL_QUERY_ESC("SELECT `ext_name` FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `id`=%s LIMIT 1",
798 array(bigintval($ext_id)), __FUNCTION__, __LINE__);
800 // Is the entry there?
801 if (SQL_NUMROWS($result) == 1) {
802 // Get the extension's name from database
803 $data = SQL_FETCHARRAY($result);
807 SQL_FREERESULT($result);
810 // Did we find some extension?
811 if (empty($data['ext_name'])) {
812 // We should fix these all!
813 debug_report_bug(__FUNCTION__ . ': ext_name is empty. ext_id=' . $ext_id);
816 // Return the extension name
817 return $data['ext_name'];
820 // Get extension id from name
821 function getExtensionId ($ext_name) {
823 $data['ext_id'] = '0';
826 if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
828 $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
831 incrementStatsEntry('cache_hits');
833 // Load from database
834 $result = SQL_QUERY_ESC("SELECT `id` AS ext_id FROM `{?_MYSQL_PREFIX?}_extensions` WHERE `ext_name`='%s' LIMIT 1",
835 array($ext_name), __FUNCTION__, __LINE__);
837 // Is the entry there?
838 if (SQL_NUMROWS($result) == 1) {
839 // Get the extension's id from database
840 $data = SQL_FETCHARRAY($result);
844 SQL_FREERESULT($result);
847 $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $data['ext_id'];
851 return $data['ext_id'];
854 // Determines wether the given extension name is valid
855 function isExtensionNameValid ($ext_name) {
857 if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
858 // Generate include file name
859 $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
861 // Is there a file in inc/extensions/ ?
862 $GLOBALS['ext_name_valid'][$ext_name] = isIncludeReadable($INC);
866 return $GLOBALS['ext_name_valid'][$ext_name];
869 // Determines wether the given extension id is valid
870 function isExtensionIdValid ($ext_id) {
871 // Default is nothing valid
874 // Check in cache then in database
875 if (isset($GLOBALS['cache_array']['extension']['ext_name'][$ext_id])) {
880 incrementStatsEntry('cache_hits');
883 $isValid = (countSumTotalData($ext_id, 'extensions', 'id', 'id', true) == 1);
890 // Activate given extension
891 function doActivateExtension ($ext_name) {
892 // Activate the extension
893 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='Y' WHERE `ext_name`='%s' LIMIT 1",
894 array($ext_name), __FUNCTION__, __LINE__);
896 // Extension has been activated?
897 if (SQL_AFFECTEDROWS() == 1) {
898 // Then run all queries
899 doExtensionSqls(getExtensionId($ext_name), 'activate');
903 // Deactivate given extension
904 function doDeactivateExtension($ext_name) {
905 // Activate the extension
906 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_extensions` SET `ext_active`='N' WHERE `ext_name`='%s' LIMIT 1",
907 array($ext_name), __FUNCTION__, __LINE__);
909 // Extension has been activated?
910 if (SQL_AFFECTEDROWS() == 1) {
911 // Then run all queries
912 doExtensionSqls(getExtensionId($ext_name), 'deactivate');
915 createExtensionDeactivationTask($ext_name);
918 sendAdminNotification(
919 getMessage('ADMIN_SUBJECT_EXTENSION_DEACTIVATED'),
920 'admin_ext_deactivated',
921 array('ext_name' => $ext_name)
926 // Checks wether the extension is older than given
927 function isExtensionOlder ($ext_name, $ext_ver) {
928 // Get current extension version
929 $currVersion = getExtensionVersion($ext_name);
931 // Remove all dots from both versions
932 $currVersion = str_replace('.', '', $currVersion);
933 $ext_ver = str_replace('.', '', $ext_ver);
935 // Now compare both and return the result
936 return ($currVersion < $ext_ver);
939 // Creates a new task for updated extension
940 function createExtensionUpdateTask ($adminId, $ext_name, $ext_ver, $notes) {
941 // Create subject line
942 $subject = '[UPDATE-' . $ext_name . '-' . $ext_ver . ':] {--ADMIN_UPDATE_EXTENSION_SUBJ--}';
944 // Is the extension there?
945 if (isExtensionInstalled($ext_name)) {
946 // Check if task is not there
947 if (determineTaskIdBySubject($subject) == '0') {
948 // Create extension update-task
949 createNewTask($subject, $notes, 'EXTENSION_UPDATE', 0, $adminId);
952 // Extension not there! :-(
953 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Extension <span class=\"data\">%s</span> not found but should be updated?", $ext_name));
957 // Creates a new task for newly installed extension
958 function createNewExtensionTask ($adminId, $subject, $ext) {
959 // Not installed and do we have created a task for the admin?
960 if ((determineTaskIdBySubject($subject) == '0') && (!isExtensionInstalled($ext))) {
961 // Set default message if ext-foo is missing
962 $message = getMaskedMessage('ADMIN_EXTENSION_TEXT_FILE_MISSING', $ext);
965 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
971 // Load text for task if found
972 if (isFileReadable($tpl)) {
973 // Load extension's own text template (HTML!)
974 $message = loadTemplate('ext_' . $ext, true);
976 // Write this in debug.log as well
977 logDebugMessage(__FUNCTION__, __LINE__, $message);
980 // Task not created so it's a brand-new extension which we need to register and create a task for!
981 createNewTask($subject, $message, 'EXTENSION', 0, $adminId, false);
985 // Creates a task for automatically deactivated (deprecated) extension
986 function createExtensionDeactivationTask ($ext) {
987 // Create subject line
988 $subject = sprintf("[%s:] %s", $ext, getMessage('TASK_SUBJ_EXTENSION_DEACTIVATED'));
990 // Not installed and do we have created a task for the admin?
991 if ((determineTaskIdBySubject($subject) == '0') && (isExtensionInstalled($ext))) {
992 // Task not created so add it
993 createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext)), 'EXTENSION_DEACTIVATION');
997 // Checks if the module has a menu
998 function ifModuleHasMenu ($mod, $forceDb = false) {
999 // All is false by default
1002 // Extension installed and newer than or has version 0.1.2?
1003 if (isExtensionInstalledAndNewer('cache', '0.1.2')) {
1004 // Cache version is okay, so let's check the cache!
1005 if (isset($GLOBALS['cache_array']['modules']['has_menu'][$mod])) {
1006 // Check module cache and count hit
1007 $ret = ($GLOBALS['cache_array']['modules']['has_menu'][$mod] == 'Y');
1008 incrementStatsEntry('cache_hits');
1009 } elseif (isset($GLOBALS['cache_array']['extension']['ext_menu'][$mod])) {
1010 // Check cache and count hit
1011 $ret = ($GLOBALS['cache_array']['extension']['ext_menu'][$mod] == 'Y');
1012 incrementStatsEntry('cache_hits');
1014 // Admin/guest/member/sponsor modules have always a menu!
1015 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor'));
1017 } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.3.6')) && ((!isExtensionActive('cache')) || ($forceDb === true))) {
1018 // Check database for entry
1019 $result = SQL_QUERY_ESC("SELECT `has_menu` FROM `{?_MYSQL_PREFIX?}_mod_reg` WHERE `module`='%s' LIMIT 1",
1020 array($mod), __FUNCTION__, __LINE__);
1023 if (SQL_NUMROWS($result) == 1) {
1024 // Load "has_menu" column
1025 $data = SQL_FETCHARRAY($result);
1027 // Fake cache... ;-)
1028 $GLOBALS['cache_array']['extension']['ext_menu'][$mod] = $data['has_menu'];
1030 // Does it have a menu?
1031 $ret = ($data['has_menu'] == 'Y');
1035 SQL_FREERESULT($result);
1036 } elseif (!isExtensionInstalled('sql_patches')) {
1037 // No sql_patches installed, so maybe in admin/guest/member/sponsor area or no admin registered?
1038 $ret = in_array($mod, array('admin', 'index', 'login', 'sponsor')); // Then there is a menu!
1040 // Unsupported state!
1041 logDebugMessage(__FUNCTION__, __LINE__, 'This should never be reached.');
1048 // Determines the task id for given extension
1049 function determineExtensionTaskId ($ext_name) {
1050 // Default is not found
1051 $data['task_id'] = '0';
1053 // Search for extension task's id
1054 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `task_type`='EXTENSION' AND `subject`='[%s:]' LIMIT 1",
1055 array($ext_name), __FUNCTION__, __LINE__);
1058 if (SQL_NUMROWS($result) == 1) {
1059 // Task found so load task's id and register extension...
1060 $data = SQL_FETCHARRAY($result);
1064 SQL_FREERESULT($result);
1067 return $data['task_id'];
1070 // Determines the task id for given subject
1071 function determineTaskIdBySubject ($subject) {
1072 // Default is not found
1073 $data['task_id'] = '0';
1075 // Search for task id
1076 $result = SQL_QUERY_ESC("SELECT `id` AS task_id FROM `{?_MYSQL_PREFIX?}_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1077 array($subject), __FUNCTION__, __LINE__);
1080 if (SQL_NUMROWS($result) == 1) {
1081 // Task found so load task's id and register extension...
1082 $data = SQL_FETCHARRAY($result);
1086 SQL_FREERESULT($result);
1089 return $data['task_id'];
1092 // Add updates notes for given version
1093 function addExtensionNotes ($ver) {
1094 // Init notes/content
1095 $out = ''; $content = array();
1097 // Is do we have verbose output enabled?
1098 if ((!isExtensionActive('sql_patches')) || (getConfig('verbose_sql') == 'Y')) {
1099 // Update notes found?
1100 if (isExtensionUpdateNoteSet($ver)) {
1101 // Update notes found
1104 'notes' => getExtensionUpdateNotes($ver)
1108 setExtensionUpdateNotes('', $ver);
1109 } elseif (($ver == '0.0') || ($ver == '0.0.0')) {
1113 'notes' => getMessage('INITIAL_RELEASE')
1116 // No update notes found!
1119 'notes' => getMessage('NO_UPDATE_NOTES')
1124 $out = loadTemplate('admin_EXTENSION_notes', true, $content);
1128 appendExtensionNotes($out);
1131 // Getter for CSS files array
1132 function getExtensionCssFiles () {
1133 // By default no additional CSS files are found
1134 $cssFiles = array();
1136 // Is the array there?
1137 if (isset($GLOBALS['css_files'])) {
1139 $cssFiles = $GLOBALS['css_files'];
1146 // Init CSS files array
1147 function initExtensionCssFiles () {
1149 $GLOBALS['css_files'] = array();
1153 function addExtensionCssFile ($file) {
1154 // Is the array there?
1155 if (!isset($GLOBALS['css_files'])) {
1156 // Then auto-init them
1157 initExtensionCssFiles();
1161 $GLOBALS['css_files'][] = $file;
1164 // Setter for EXT_ALWAYS_ACTIVE flag
1165 function setExtensionAlwaysActive ($active) {
1166 $GLOBALS['ext_always_active'][getCurrentExtensionName()] = (string) $active;
1169 // Getter for EXT_ALWAYS_ACTIVE flag
1170 function getExtensionAlwaysActive () {
1171 return $GLOBALS['ext_always_active'][getCurrentExtensionName()];
1174 // Checks wether the current extension is always active
1175 function isExtensionAlwaysActive () {
1176 return (getExtensionAlwaysActive() == 'Y');
1179 // Setter for EXT_VERSION flag
1180 function setThisExtensionVersion ($version) {
1181 $GLOBALS['ext_version'][getCurrentExtensionName()] = (string) $version;
1184 // Getter for EXT_VERSION flag
1185 function getThisExtensionVersion () {
1186 return $GLOBALS['ext_version'][getCurrentExtensionName()];
1189 // Setter for EXT_DEPRECATED flag
1190 function setExtensionDeprecated ($deprecated) {
1191 $GLOBALS['ext_deprecated'][getCurrentExtensionName()] = (string) $deprecated;
1194 // Getter for EXT_DEPRECATED flag
1195 function isExtensionDeprecated () {
1196 return ($GLOBALS['ext_deprecated'][getCurrentExtensionName()] == 'Y');
1199 // Setter for EXT_UPDATE_DEPENDS flag
1200 function addExtensionUpdateDependency ($updateDepends) {
1201 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - ENTERED!');
1202 // Is the update depency empty? (NEED TO BE FIXED!)
1203 if (empty($updateDepends)) {
1204 // Please report this bug!
1205 debug_report_bug(__FUNCTION__, __LINE__, 'updateDepends is empty: currentExtension=' . getCurrentExtensionName());
1208 // Is it not yet added?
1210 if ((isset($updateDepends, $GLOBALS['ext_running_updates'][getCurrentExtensionName()])) && (in_array($updateDepends, getExtensionUpdatesRunning()))) {
1211 // Double-adding isn't fine, too
1212 debug_report_bug(__FUNCTION__ . '() called twice: updateDepends=' . $updateDepends . ',currentExtension=' . getCurrentExtensionName());
1216 // Add it to the list of extension update depencies map
1217 $GLOBALS['ext_update_depends'][getCurrentExtensionName()][] = (string) $updateDepends;
1219 // Remember it in the list of running updates
1220 $GLOBALS['ext_running_updates'][getCurrentExtensionName()][] = $updateDepends;
1221 //* DEBUG */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . '/' . $updateDepends . ' - EXIT!');
1224 // Getter for running updates
1225 function getExtensionUpdatesRunning () {
1226 return $GLOBALS['ext_running_updates'][getCurrentExtensionName()];
1229 // Checks wether the given extension registration is in progress
1230 function isExtensionRegistrationRunning ($ext_name) {
1232 $isRunning = ((isset($GLOBALS['ext_register_running'])) && (in_array($ext_name, $GLOBALS['ext_register_running'])));
1235 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1239 // Init EXT_UPDATE_DEPENDS flag
1240 function initExtensionUpdateDependencies () {
1241 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - ENTERED!');
1243 // Init update depency map automatically if not found
1244 if (isExtensionUpdateDependenciesInitialized()) {
1245 // We need these bug reports as well...
1246 debug_report_bug(__FUNCTION__ . '() is called twice: currName=' . getCurrentExtensionName());
1249 $GLOBALS['ext_update_depends'][getCurrentExtensionName()] = array();
1251 // Init running updates array
1252 initExtensionRuningUpdates();
1254 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - EXIT!');
1257 // Adds an extension as "registration in progress"
1258 function addExtensionRunningRegistration ($ext_name) {
1260 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - ENTERED!');
1261 if (isExtensionRegistrationRunning($ext_name)) {
1262 // This is really bad and should not be quietly ignored
1263 debug_report_bug(__FUNCTION__ . '() already called! ext_name=' . $ext_name);
1267 $GLOBALS['ext_register_running'][] = $ext_name;
1268 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Registration in progress: ext_name=' . $ext_name . ' - EXIT!');
1271 // Checks wether EXT_UPDATE_DEPENDS is initialized
1272 function isExtensionUpdateDependenciesInitialized () {
1273 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName());
1274 return (isset($GLOBALS['ext_update_depends'][getCurrentExtensionName()]));
1277 // Checks wether an update is already running for given extension
1278 function isExtensionUpdateRunning ($ext_name) {
1279 // Current and given extensions means whole array
1280 if ($ext_name == getCurrentExtensionName()) {
1281 // Default is not found
1284 // Walk through whole array
1285 foreach ($GLOBALS['ext_running_updates'] as $ext1=>$depends) {
1287 if (($ext1 == $ext_name) || ($isRunning === true)) {
1289 logDebugMessage(__FUNCTION__, __LINE__, 'ext1=' . $ext1 . ',ext_name=' . $ext_name . ',isRunning=' . intval($isRunning));
1296 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1301 $isRunning = ((isExtensionUpdateDependenciesInitialized()) && (in_array($ext_name, getExtensionRunningUpdates())));
1304 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentExtension=' . getCurrentExtensionName() . ',ext_name=' . $ext_name . ', isRunning=' . intval($isRunning));
1308 // Initializes the list of running updates
1309 function initExtensionRuningUpdates () {
1310 // Auto-init ext_running_updates
1311 if (!isset($GLOBALS['ext_running_updates'])) {
1312 $GLOBALS['ext_running_updates'] = array();
1313 $GLOBALS['ext_register_running'] = array();
1317 // Getter for EXT_UPDATE_DEPENDS flag
1318 function getExtensionUpdateDependencies () {
1319 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1320 return $GLOBALS['ext_update_depends'][getCurrentExtensionName()];
1323 // Getter for next iterator depency
1324 function getExtensionUpdateDependenciesIterator () {
1325 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1326 return ($GLOBALS['ext_update_depends'][getCurrentExtensionName()][getExtensionUpdateIterator()]);
1329 // Counter for extension update depencies
1330 function countExtensionUpdateDependencies () {
1331 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1332 return count($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1335 // Removes given extension from update denpency list
1336 function removeExtensionUpdateDependency ($ext_name) {
1338 $key = array_search($ext_name, getExtensionUpdateDependencies());
1341 if ($key !== false) {
1343 unset($GLOBALS['ext_update_depends'][getCurrentExtensionName()][$key]);
1345 // And sort the array
1346 ksort($GLOBALS['ext_update_depends'][getCurrentExtensionName()]);
1350 // Init iterator for update depencies
1351 function initExtensionUpdateIterator () {
1352 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1353 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()] = '0';
1356 // Getter for depency iterator
1357 function getExtensionUpdateIterator () {
1358 // Auto-init iterator
1359 if (!isset($GLOBALS['ext_depend_iterator'][getCurrentExtensionName()])) initExtensionUpdateIterator();
1362 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName().'/'.$GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]);
1363 return $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()];
1366 // Increments the update iterator
1367 function incrementExtensionUpdateIterator () {
1368 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "currName=".getCurrentExtensionName());
1369 $GLOBALS['ext_depend_iterator'][getCurrentExtensionName()]++;
1372 // Setter for EXT_REPORTS_FAILURE flag
1373 function setExtensionReportsFailure ($reportsFailure) {
1374 $GLOBALS['ext_reports_failure'] = (bool) $reportsFailure;
1377 // Getter for EXT_REPORTS_FAILURE flag
1378 function getExtensionReportsFailure () {
1379 return $GLOBALS['ext_reports_failure'];
1382 // Setter for EXT_VER_HISTORY flag
1383 function setExtensionVersionHistory ($verHistory) {
1384 $GLOBALS['ext_ver_history'][getCurrentExtensionName()] = (array) $verHistory;
1387 // Getter for EXT_VER_HISTORY array
1388 function getExtensionVersionHistory () {
1389 return $GLOBALS['ext_ver_history'][getCurrentExtensionName()];
1392 // Setter for EXT_UPDATE_NOTES
1393 function setExtensionUpdateNotes ($updateNotes, $ver='') {
1394 // . '/' . getCurrentExtensionVersion()
1395 //* DEBUG: */ debug_report_bug(__FUNCTION__.':' . getCurrentExtensionName() . '/' . getExtensionMode() . '/' . $ver . '=' . $updateNotes);
1397 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][getCurrentExtensionVersion()] = (string) $updateNotes;
1399 $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver] = (string) $updateNotes;
1403 // Getter for EXT_UPDATE_NOTES
1404 function getExtensionUpdateNotes ($ver) {
1405 return $GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver];
1408 // Checks if ext_update_notes is set
1409 function isExtensionUpdateNoteSet ($ver) {
1410 return isset($GLOBALS['ext_update_notes'][getCurrentExtensionName()][$ver]);
1413 // Init extension notice
1414 function initExtensionNotes () {
1415 $GLOBALS['ext_notes'][getCurrentExtensionName()] = '';
1418 // Append extension notice
1419 function appendExtensionNotes ($notes) {
1420 $GLOBALS['ext_notes'][getCurrentExtensionName()] .= (string) trim($notes);
1423 // Getter for extension notes
1424 function getExtensionNotes () {
1425 return $GLOBALS['ext_notes'][getCurrentExtensionName()];
1428 // Setter for current extension name
1429 function setCurrentExtensionName ($ext_name) {
1430 $GLOBALS['curr_EXTENSION_name'] = (string) trim($ext_name);
1433 // Getter for current extension name
1434 function getCurrentExtensionName () {
1435 if (isset($GLOBALS['curr_EXTENSION_name'])) {
1436 return $GLOBALS['curr_EXTENSION_name'];
1440 debug_report_bug(__FUNCTION__.": curr_EXTENSION_name not initialized. Please execute initExtensionSqls() before calling this function.");
1443 // Init SQLs array for current extension
1444 function initExtensionSqls ($force = false) {
1445 // Auto-init the array or if forced
1446 if ((!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) || ($force === true)) {
1448 $GLOBALS['ext_sqls'][getCurrentExtensionName()] = array();
1450 // Initialize the generic array
1455 // Adds SQLs to the SQLs array but "assigns" it with current extension name
1456 function addExtensionSql ($sql) {
1458 $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()][] = $sql;
1461 // Getter for SQLs array for current extension
1462 function getExtensionSqls () {
1463 // Output debug backtrace if not found (SHOULD NOT HAPPEN!)
1464 if (!isset($GLOBALS['ext_sqls'][getCurrentExtensionName()])) {
1465 // Not found, should not happen
1466 debug_report_bug(__FUNCTION__, __LINE__, sprintf("ext_sqls is empty, current extension: %s",
1467 getCurrentExtensionName()
1472 return $GLOBALS['ext_sqls'][getCurrentExtensionName()];
1475 // Removes SQLs for current extension
1476 function unsetExtensionSqls () {
1477 unset($GLOBALS['ext_sqls'][getCurrentExtensionName()]);
1480 // Auto-initializes the removal list
1481 function initExtensionRemovalList () {
1482 // Is the remove list there?
1483 if (!isset($GLOBALS['ext_update_remove'])) {
1485 $GLOBALS['ext_update_remove'] = array();
1489 // Checks wether the current extension is on the removal list
1490 function isExtensionOnRemovalList () {
1491 // Init removal list
1492 initExtensionRemovalList();
1495 return (in_array(getCurrentExtensionName(), $GLOBALS['ext_update_remove']));
1498 // Adds the current extension to the removal list
1499 function addCurrentExtensionToRemovalList () {
1501 $GLOBALS['ext_update_remove'][] = getCurrentExtensionName();
1504 // Getter for removal list
1505 function getExtensionRemovalList () {
1506 // Return the removal list
1507 return $GLOBALS['ext_update_remove'];
1510 // Redirects if the provided extension is not installed
1511 function redirectOnUninstalledExtension ($ext_name) {
1512 // So is the extension there?
1513 if ((!isExtensionInstalled($ext_name)) || (!isExtensionActive($ext_name))) {
1514 // Redirect to index
1515 redirectToUrl('modules.php?module=index&code=' . getCode('EXTENSION_PROBLEM') . '&ext=' . $ext_name);
1519 // Filter for initialization of all extensions by loading them in 'init' mode
1520 function FILTER_INIT_EXTENSIONS () {
1521 // Do we have some entries?
1522 //* DEBUG */ print __FUNCTION__.': ENTRY!<br />';
1523 if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
1524 // Load all found extensions if found
1525 //* DEBUG */ print __FUNCTION__.': CACHE - START!<br />';
1526 foreach ($GLOBALS['cache_array']['extension']['ext_name'] as $key => $ext_name) {
1528 //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - START<br />';
1529 loadExtension($ext_name, 'init', getExtensionVersion($ext_name));
1530 //* DEBUG */ print __FUNCTION__.': '.$ext_name.' - END<br />';
1532 //* DEBUG */ print __FUNCTION__.': CACHE - END!<br />';
1534 //* DEBUG */ print __FUNCTION__.': EXIT!<br />';
1537 // Setter for extension mode
1538 function setExtensionMode ($ext_mode) {
1539 $GLOBALS['ext_mode'] = (string) $ext_mode;
1542 // Getter for extension mode
1543 function getExtensionMode () {
1544 return $GLOBALS['ext_mode'];
1547 // Setter for dry-run
1548 function enableExtensionDryRun ($dry_run = true) {
1549 //* DEBUG: */ print __FUNCTION__.': '.getCurrentExtensionName().'='.intval($dry_run).'<br />';
1550 $GLOBALS['ext_dry_run'] = (bool) $dry_run;
1553 // Getter for dry-run
1554 function getExtensionDryRun () {
1555 return $GLOBALS['ext_dry_run'];
1558 // Setter for current extension version
1559 function setCurrentExtensionVersion ($ext_ver) {
1560 // ext_ver should never be empty in other modes than 'test'
1561 if ((empty($ext_ver)) && (getExtensionMode() != 'test')) {
1562 // Please report all these messages
1563 debug_report_bug(__FUNCTION__, __LINE__, 'ext_ver is empty. Current extension name: ' . getCurrentExtensionName() . ', mode=' . getExtensionMode());
1567 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver[' . gettype($ext_ver) . ']=' . $ext_ver);
1568 $GLOBALS['ext_current_version'][getCurrentExtensionName()] = (string) $ext_ver;
1571 // Getter for current extension version
1572 function getCurrentExtensionVersion () {
1573 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $GLOBALS['ext_current_version'][getCurrentExtensionName()]);
1574 return $GLOBALS['ext_current_version'][getCurrentExtensionName()];
1577 // Remove the extension from cache array
1578 function removeExtensionFromArray () {
1579 // "Cache" this name
1580 $ext_name = getCurrentExtensionName();
1582 // Now loop through the whole cache
1583 foreach ($GLOBALS['cache_array']['extension'] as $cacheName => $cacheArray) {
1584 // Is it an element?
1585 if (isset($cacheArray[$ext_name])) {
1587 unset($cacheArray[$ext_name]);
1588 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1591 $key = array_search($ext_name, $cacheArray);
1594 if ($key !== false) {
1595 // Found, so remove it
1596 unset($cacheArray[$key]);
1597 $GLOBALS['cache_array']['extension'][$cacheName] = $cacheArray;
1603 // "Getter" for 'extension has a CSS file' (with same name, of course)
1604 function getExtensionHasCss () {
1605 // Default is no CSS
1608 // Construct FQFN for check
1609 $FQFN = sprintf("%stheme/%s/css/%s.css",
1612 getCurrentExtensionName()
1616 if (isFileReadable($FQFN)) {
1617 // Readable, so it is there...
1625 // Checks wether the given extension has a language file
1626 function ifExtensionHasLanguageFile ($ext_name) {
1627 // Default is no language file
1628 $hasLanguage = false;
1630 // Do we have cache?
1631 if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
1633 $hasLanguage = ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1636 incrementStatsEntry('cache_hits');
1638 // Not readable is default
1641 // Is the language file readable for this extension?
1642 if (isLanguageIncludeReadable($ext_name)) {
1648 $GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] = 'Y';
1652 return ($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name] == 'Y');
1655 // Load an extension's include file
1656 function loadExtensionInclude () {
1658 if (!isExtensionIncludeReadable()) {
1660 debug_report_bug(__FUNCTION__, __LINE__, 'Extension ' . getCurrentExtensionName() . ' should be loaded, but is not readable.');
1663 // Generate INC name
1664 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1670 // Checks wether an extension is readable
1671 function isExtensionIncludeReadable ($ext_name = '') {
1672 // If empty, use current
1673 if (empty($ext_name)) $ext_name = getCurrentExtensionName();
1676 if (!isset($GLOBALS['ext_inc_readable'][$ext_name])) {
1677 // Generate INC name
1678 $INC = sprintf("inc/extensions/ext-%s.php", getCurrentExtensionName());
1681 $GLOBALS['ext_inc_readable'][$ext_name] = isIncludeReadable($INC);
1685 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',realable='.intval($GLOBALS['ext_inc_readable'][$ext_name]));
1686 return $GLOBALS['ext_inc_readable'][$ext_name];
1689 // Checks if an extension's function file is readable
1690 function isExtensionFunctionFileReadable ($ext_name) {
1691 if (isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name])) {
1692 // Just count cache hits
1693 incrementStatsEntry('cache_hits');
1695 // Construct FQFN for functions file
1696 $funcsInclude = sprintf("inc/libs/%s_functions.php", $ext_name);
1698 // Is this include there?
1699 if ((isFileReadable($funcsInclude)) && (!isset($GLOBALS['ext_loaded']['funcs'][$ext_name])) && (getExtensionMode() == 'test')) {
1701 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'Y';
1704 $GLOBALS['cache_array']['extension']['ext_func'][$ext_name] = 'N';
1709 return ($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y');
1712 // Adds an admin menu to the SQL queue of the menu entry is not found
1713 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
1714 // Now check if this menu is there
1715 if (!isMenuActionValid('admin', $action, $what)) {
1717 if (is_null($what)) {
1719 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1727 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1736 // Add it to the queue
1737 addExtensionSql($sql);
1738 } elseif (isDebugModeEnabled()) {
1739 // Double menus should be located and fixed!
1740 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double admin menu action=%s, what=%s detected.", $action, $what));
1744 // Adds a guest menu to the SQL queue if the menu entry is not found
1745 function addGuestMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1746 // Now check if this menu is there
1747 if (!isMenuActionValid('guest', $action, $what)) {
1749 if (is_null($what)) {
1751 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1760 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_guest_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1770 // Add it to the queue
1771 addExtensionSql($sql);
1772 } elseif (isDebugModeEnabled()) {
1773 // Double menus should be located and fixed!
1774 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double guest menu action=%s, what=%s detected.", $action, $what));
1778 // Adds a member menu to the SQL queue if the menu entry is not found
1779 function addMemberMenuSql ($action, $what, $title, $visible, $locked, $sort) {
1780 // Now check if this menu is there
1781 if (!isMenuActionValid('member', $action, $what)) {
1783 if (is_null($what)) {
1785 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s',NULL,'%s','%s','%s',%s)",
1794 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_member_menu` (`action`,`what`,`title`,`visible`,`locked`,`sort`) VALUES('%s','%s','%s','%s','%s',%s)",
1804 // Add it to the queue
1805 addExtensionSql($sql);
1806 } elseif (isDebugModeEnabled()) {
1807 // Double menus should be located and fixed!
1808 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double member menu action=%s, what=%s detected.", $action, $what));
1812 // Adds a sponsor menu to the SQL queue if the menu entry is not found
1813 function addSponsorMenuSql ($action, $what, $title, $active, $sort) {
1814 // Now check if this menu is there, if no ext-sponsor is installed all is not yet added
1815 if ((!isExtensionInstalled('sponsor')) || (!isMenuActionValid('sponsor', $action, $what))) {
1817 if (is_null($what)) {
1819 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s',NULL,'%s','%s',%s)",
1827 $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_sponsor_menu` (`action`,`what`,`title`,`active`,`sort`) VALUES('%s','%s','%s','%s',%s)",
1836 // Add it to the queue
1837 addExtensionSql($sql);
1838 } elseif (isDebugModeEnabled()) {
1839 // Double menus should be located and fixed!
1840 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Double sponsor menu action=%s, what=%s detected.", $action, $what));
1844 // Enables/disables productive mode for current extension (used only while
1846 function enableExtensionProductive ($isProductive = true) {
1847 $GLOBALS['ext_productive'][getCurrentExtensionName()] = (bool) $isProductive;
1850 // Checks wether the extension is in productive phase. If not set, development
1851 // phase (=false) is assumed.
1852 function isExtensionProductive () {
1853 return ((isset($GLOBALS['ext_productive'][getCurrentExtensionName()])) && ($GLOBALS['ext_productive'][getCurrentExtensionName()] === true));