Pourquoi la plupart des fichiers PHP de Joomla! commence par ant par defined('_JEXEC') ?

From Joomla! Documentation

This page is a translated version of the page Why do most of the Joomla! PHP files start with defined(' JEXEC')? and the translation is 0% complete.
Outdated translations are marked like this.
Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎العربية • ‎中文(台灣)‎

La plupart des fichiers PHP de Joomla! commencent avec la déclaration suivante :

Cette déclaration permet de vérifier si le fichier est appelé à partir d'une session Joomla. Cela protège votre site en compliquant la possibilité pour un pirate/hacker d'endommager votre site.

Why do most of the Joomla! PHP files start with defined(' JEXEC')?

La déclaration agit principalement à deux niveaux :

  • Cela permet d'éviter des erreurs d'exécution d'un fichier PHP qui doit être exécuté à dans Jooma! Bootstrap et il empêche la divulgation des vulnérabilités découlant de l'affichage d'erreurs fatales PHP qui seraient générées.
defined('_JEXEC') or die('Restricted access');
  • Cela empêche l'injection accidentelle de variables par le biais d'une attaque globale faisant croire au fichier PHP qu'il est traité à l'intérieur de l'application, alors qu'il ne le serait pas.

Régler les rapports d'erreurs sur un niveau moins élevé pourrait avoir des effets similaires mais il existe des des cas où les changements de la configuration des paramètres PHP .INI ne sont pas autorisés. La vérification JEXEC fonctionne indépendamment si la modification de la configuration est possible ou non (par exemple si vous êtes en mode débogage diminuer le niveau de déclaration des erreurs serait gênant pour pouvoir définir l'indicateur de débogage).


Notez cependant que cette ligne ne devrait PAS figurer dans votre fichier index.php principal puisque c'est le programme qui démarre la session Joomla.

When?

The check should be added to files that when accessed directly cause a path exposure. For example, the following error occurs when the Backlink System Plugin (/plugins/system/backlink.php file) has had the _JEXEC check disabled:

Fatal error: Call to undefined function jimport() in /Users/pasamio/Sites/workspace/joomla_15/plugins/system/backlink.php on line 18

As is evidenced, the 'jimport' function doesn't exist when the file is directly called so PHP raises an error and exposes the path to the file. Adding the defined or die check to this file will cause a "Restricted access" message to be displayed when the file is accessed.

So the general rule for the JEXEC check is if the PHP file depends on another file to operate properly. Typically if you access a file directly without the JEXEC check and a PHP error is raised (presuming your PHP error reporting is set to show errors by default) about a missing variable, function, object or similar then the file needs to be protected.

Some files don't need to be protected from this check. They might be files with no external dependencies (e.g. a simple class or bit of code) or they might be external files that can operate without being within Joomla!. Examples of this include TinyMCE's GZip'd Javascript generator which is entirely self contained.