Activer le Safe Mode pour phpBB

Description: Charger phpBB sans extensions et avec le style prosilver

Catégories: Serveur, PHP et MySQL

Lien vers cet article: Tout sélectionner

[url=https://forums.caforum.fr/kb/viewarticle?a=38]Base de connaissance - Activer le Safe Mode pour phpBB[/url]

Les pages blanches et autres erreurs suite à une installation d'extension non compatible avec votre version de phpBB peuvent rendre le forum inaccessible ou empêcher l'accès au PCA.
Le mode sans échec (Safe Mode) permet de retrouver un forum phpBB avec comme seul style prosilver et sans aucune extensions chargés, le forum sera également en mode maintenance.

Sur le serveur faire une sauvegarde du fichier common.php original et remplacer l'intégralité du fichier par ceci :

Code: Tout sélectionner

<?php
/**
 * Starts phpBB without extensions, aka "safe mode", if the PHPBB_SAFEMODE
 * constant is defined in the config.php file.
 *
 * Safe mode should only be used for emergencies!
 *
 * @copyright (c) 2024 Dion Designs
 */

/**
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

/**
* Minimum Requirement: PHP 7.2.0
*/

if (!defined('IN_PHPBB'))
{
	exit;
}

require($phpbb_root_path . 'includes/startup.' . $phpEx);
require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);

$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();

$phpbb_config_php_file = new \phpbb\config_php_file($phpbb_root_path, $phpEx);
extract($phpbb_config_php_file->get_all());

// This file is only for phpBB 3.3.x, so include constants file early and then
// verify the phpBB version.
require($phpbb_root_path . 'includes/constants.' . $phpEx);
if (version_compare(PHPBB_VERSION, '3.2.99', '<') || version_compare(PHPBB_VERSION, '3.3.99', '>'))
{
	die('Safe Mode support requires phpBB 3.3');
}

if (!defined('PHPBB_ENVIRONMENT'))
{
	@define('PHPBB_ENVIRONMENT', 'production');
}

if (!defined('PHPBB_INSTALLED'))
{
	// Redirect the user to the installer
	require($phpbb_root_path . 'includes/functions.' . $phpEx);

	// We have to generate a full HTTP/1.1 header here since we can't guarantee to have any of the information
	// available as used by the redirect function
	$server_name = (!empty($_SERVER['HTTP_HOST'])) ? strtolower($_SERVER['HTTP_HOST']) : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME'));
	$server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT');
	$secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 1 : 0;

	if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
	{
		$secure = 1;
		$server_port = 443;
	}

	$script_path = phpbb_get_install_redirect($phpbb_root_path, $phpEx);

	// Eliminate . and .. from the path
	require($phpbb_root_path . 'phpbb/filesystem/filesystem.' . $phpEx);
	$phpbb_filesystem = new phpbb\filesystem\filesystem();
	$script_path = $phpbb_filesystem->clean_path($script_path);

	$url = (($secure) ? 'https://' : 'http://') . $server_name;

	if ($server_port && (($secure && $server_port <> 443) || (!$secure && $server_port <> 80)))
	{
		// HTTP HOST can carry a port number...
		if (strpos($server_name, ':') === false)
		{
			$url .= ':' . $server_port;
		}
	}

	$url .= $script_path;
	header('Location: ' . $url);
	exit;
}

// In case $phpbb_adm_relative_path is not set (in case of an update), use the default.
$phpbb_adm_relative_path = (isset($phpbb_adm_relative_path)) ? $phpbb_adm_relative_path : 'adm/';
$phpbb_admin_path = (defined('PHPBB_ADMIN_PATH')) ? PHPBB_ADMIN_PATH : $phpbb_root_path . $phpbb_adm_relative_path;

// Include files
require($phpbb_root_path . 'includes/functions.' . $phpEx);
require($phpbb_root_path . 'includes/functions_content.' . $phpEx);
include($phpbb_root_path . 'includes/functions_compatibility.' . $phpEx);
require($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);

// Determine whether safe mode is defined
$phpbb_safe_mode = defined('PHPBB_SAFEMODE') && !empty(PHPBB_SAFEMODE);

// Registered before building the container so the development environment stay capable of intercepting
// the container builder exceptions.
if (PHPBB_ENVIRONMENT === 'development')
{
	\phpbb\debug\debug::enable();
}
else
{
	set_error_handler(defined('PHPBB_MSG_HANDLER') ? PHPBB_MSG_HANDLER : 'msg_handler');
}

// Don't load extension classes if in safe mode
if (!$phpbb_safe_mode)
{
	$phpbb_class_loader_ext = new \phpbb\class_loader('\\', "{$phpbb_root_path}ext/", $phpEx);
	$phpbb_class_loader_ext->register();
}

// Set up container
try
{
	$phpbb_container_builder = new \phpbb\di\container_builder($phpbb_root_path, $phpEx);

	// Check that cache directory is writable before trying to build container
	$cache_dir = $phpbb_container_builder->get_cache_dir();
	if (file_exists($cache_dir) && !is_writable($phpbb_container_builder->get_cache_dir()))
	{
		die('Unable to write to the cache directory path "' . $cache_dir . '". Ensure that the web server user can write to the cache folder.');
	}

	if ($phpbb_safe_mode)
	{
		// Build container without extensions if in safe mode
		$phpbb_container = $phpbb_container_builder->with_config($phpbb_config_php_file)->without_extensions()->get_container();
	}
	else
	{
		$phpbb_container = $phpbb_container_builder->with_config($phpbb_config_php_file)->get_container();
	}
}
catch (InvalidArgumentException $e)
{
	if (PHPBB_ENVIRONMENT !== 'development')
	{
		trigger_error(
			'The requested environment ' . PHPBB_ENVIRONMENT . ' is not available.',
			E_USER_ERROR
		);
	}
	else
	{
		throw $e;
	}
}

if ($phpbb_container->getParameter('debug.error_handler'))
{
	\phpbb\debug\debug::enable();
}

$phpbb_class_loader->set_cache($phpbb_container->get('cache.driver'));

// Selected debug info is displayed in "safe mode", and we need DB load time
if ($phpbb_safe_mode)
{
	$phpbb_container->get('dbal.conn')->set_debug_load_time(true);
}
else
{
	$phpbb_class_loader_ext->set_cache($phpbb_container->get('cache.driver'));
	$phpbb_container->get('dbal.conn')->set_debug_sql_explain($phpbb_container->getParameter('debug.sql_explain'));
	$phpbb_container->get('dbal.conn')->set_debug_load_time($phpbb_container->getParameter('debug.load_time'));
}

require($phpbb_root_path . 'includes/compatibility_globals.' . $phpEx);
register_compatibility_globals();

// Fix a few things to insure "safe mode" works correctly
if ($phpbb_safe_mode)
{
	// Given the issues below, disable board while in "safe mode"
	$config->offsetSet('board_disable', 1);

	// Force board style to Prosilver because some custom styles require
	// extensions to work correctly. Server performance will not be impacted.
	$result = $db->sql_query('SELECT style_id FROM ' . STYLES_TABLE . " WHERE LOWER(style_name) = 'prosilver'");
	$style = $db->sql_fetchfield('style_id');
	$db->sql_freeresult($result);

	if ($style)
	{
		$config->offsetSet('default_style', $style);
		$config->offsetSet('override_user_style', 1);
	}
	else
	{
		// Abort if moron admin manually uninstalled Prosilver
		die('Safe Mode requires the Prosilver theme');
	}

	// Generate some debug output to assist with potential issues. The output
	// can also be used to determine the server impact of extensions.
	//
	// This block of code should ideally be integrated into functions.php
	$phpbb_dispatcher->addListener('core.phpbb_generate_debug_output', function($event) use ($db, $request)
	{
		$debug_info = [];

		// REQUEST_TIME_FLOAT has been available since PHP 5.4.
		$totaltime = microtime(true) - $request->server('REQUEST_TIME_FLOAT');
		$debug_info[] = sprintf('<span title="SQL time: %.3fs / PHP time: %.3fs">Time: %.3fs</span>', $db->get_sql_time(), ($totaltime - $db->get_sql_time()), $totaltime);

		$memory_usage = memory_get_peak_usage();
		if ($memory_usage)
		{
			$debug_info[] = 'Peak Memory Usage: ' . get_formatted_filesize($memory_usage);
		}

		$debug_info[] = sprintf('<span title="Cached: %d">Queries: %d</span>', $db->sql_num_queries(true), $db->sql_num_queries());

		$event['debug_info'] = $debug_info;
	});

	// In ACP extension module, disallow the enable option, and load ext.php
	// when the disable and delete data options are selected.
	//
	// This block of code should ideally be integrated into acp_extensions.php
	$phpbb_dispatcher->addListener('core.acp_extensions_run_action_before', function($event) use ($language, $phpbb_root_path)
	{
		if (in_array($event['action'], ['enable_pre','enable']))
		{
			trigger_error($language->lang('FEATURE_NOT_AVAILABLE') . adm_back_link($event['u_action']), E_USER_WARNING);
		}
		else if (in_array($event['action'], ['disable','delete_data']) && file_exists("{$phpbb_root_path}ext/{$event['ext_name']}/ext.php"))
		{
			include_once("{$phpbb_root_path}ext/{$event['ext_name']}/ext.php");
		}
	});

	// Unfortunately, some features must be disabled while in "safe mode".
	//
	// Why, you may ask? Well...it's because those features throw exceptions
	// when they should instead be using a try/catch block. As a result, the
	// following features must be disabled to avoid phpBB throwing exceptions
	// if an active extension created a service to extend the feature.

	// The worst offender! Must be completely disabled. UGH!!!
	$config->offsetSet('load_notifications', '');

	// Disable Registrations and CAPTCHA unless Q&A is selected
	// Should also do something about limiting login attempts
	if ($config->offsetGet('captcha_plugin') != 'core.captcha.plugins.qa')
	{
		$config->offsetSet('enable_confirm', 0);
		$config->offsetSet('max_login_attempts', 0);
		$config->offsetSet('require_activation', 3);

		// This is needed if board is not disabled! (And if guest posting is
		// enabled, disable it via the core.modify_posting_auth event.)
		//$config->offsetSet('enable_post_confirm', 0);
	}

	// Disable search if the default engine was added by an extension. Not
	// sure extensions have this capability, but better safe than sorry!
	if (strpos($config->offsetGet('search_type'), '\\phpbb\\search') !== 0)
	{
		$config->offsetSet('load_search', 0);
	}

	// The code in phpbb/avatar/manager.php seems to correctly deal with
	// avatars defined by extensions when "safe mode" is enabled. More
	// research is required, but for now, avatars can remain enabled.
	//
	// NOTE: if a user has selected an avatar defined by an extension, their
	// avatar will not be displayed when "safe mode" is enabled.
	//$config->offsetSet('allow_avatar', '');
}

// Add own hook handler
require($phpbb_root_path . 'includes/hooks/index.' . $phpEx);
$phpbb_hook = new phpbb_hook(array('exit_handler', 'phpbb_user_session_handler', 'append_sid', array('template', 'display')));

/* @var $phpbb_hook_finder \phpbb\hook\finder */
$phpbb_hook_finder = $phpbb_container->get('hook_finder');

foreach ($phpbb_hook_finder->find() as $hook)
{
	@include($phpbb_root_path . 'includes/hooks/' . $hook . '.' . $phpEx);
}

/**
* Main event which is triggered on every page
*
* You can use this event to load function files and initiate objects
*
* NOTE:	At this point the global session ($user) and permissions ($auth)
*		do NOT exist yet. If you need to use the user object
*		(f.e. to include language files) or need to check permissions,
*		please use the core.user_setup event instead!
*
* @event core.common
* @since 3.1.0-a1
*/
$phpbb_dispatcher->dispatch('core.common');

Vider le cache.


Vous pouvez ensuite activer le mode sans échec à tout moment en ajoutant la ligne suivante à votre fichier config.php :

Code: Tout sélectionner

@define('PHPBB_SAFEMODE', true);
Cela ne fonctionne qu'avec phpBB 3.3.11 et supérieure.

Pour désactiver le safemode il suffit de passer la ligne en false

Code: Tout sélectionner

@define('PHPBB_SAFEMODE', false);