There is a simple (main) script index.php :

 <?php require 'cfg.php'; //файл с настройками require 'langs/' . $CFG['lang'] . '.php'; // языковой файл, прописаный в файле с настройками. Языковой файл содержит фразы в виде массива //echo 'P1>>>>>'; //var_dump($FO_LANG); //echo '<br>'; if(file_exists($CFG['auth'])) { //если есть файл с настройками авторизации //echo 'P2>>>>>'; //var_dump($FO_LANG); //echo '<br>'; include $CFG['auth']; //добавить его в скрипт //echo 'P3>>>>>'; //var_dump($FO_LANG); if ($fo_userid > 0) { //если юзерID > ноля echo $fo_userid; //вывести его ID } else { //если юзерID НЕ больше ноля var_dump ($FO_LANG['0002_fo_not_authorized']); // вывести второй элемент массива из языкового файла } } else { //если нет файла с настройками авторизации var_dump ($FO_LANG['0001_fo_cfg_file_not_exist']); //вывести первый элемент массива из языкового файла } ?> 

In the additional file langs/ru.php as you know, phrases are saved, in the form of an array:

 <?php $FO_LANG = array ( '0001_fo_cfg_file_not_exist' => 'Не верная конфигурация.', '0002_fo_not_authorized' => 'Вы не авторизованы!', ); ?> 

The authorization file joomla_cfg.php , a standard script for retrieving the data of the current Joomla user.

 define( '_JEXEC', 1 ); define('JPATH_BASE', dirname('../.') ); define( 'DS', DIRECTORY_SEPARATOR ); require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); $mainframe =& JFactory::getApplication('site'); $mainframe->initialise(); $mainframe->route(); $user =& JFactory::getUser(); $fo_userid = $user->get('id'); 

The problem is that if the file_exists($CFG['auth']) condition file_exists($CFG['auth']) turns out to be false, then the phrase is correctly read from the array in the language file. And if it turns out to be true, and Joomla user data is "connected", then the array is not visible and var_dump NULL .

It is clear that you can once again include necessary files, but how to avoid this and correctly add an array, so that it would be visible in all cases within the main script?

cfg.php configuration cfg.php :

 <?php $FO_CFG = array ( 'auth' => 'joomla_cfg.php', 'lang' => 'ru',, ); 

    1 answer 1

    After the investigation, it was found that the string is harmful.

     include $CFG['auth']; 

    And specifically call JRequest::clean(); , in which the following occurs literally:

     foreach ($GLOBALS as $key => $value){ if ( $key != 'GLOBALS' ) { unset ( $GLOBALS [ $key ] ); } } 

    There are several methods of struggle:

    1. Incorporate, inclusive, and again incorporate, as bequeathed to bad practics.
    2. Understand how to work with Joomla. Something tells me that there are regular means of ensuring multilingualism, sharpened for it
    3. Slightly change the application logic and render include $CFG['auth']; to the top.
    4. To use for localization not an array in the global namespace, but a class. Something like this:

    langs / ru.php:

     namespace langs; class ru{ const fo_cfg_file_not_exist = 'Не верная конфигурация.'; const fo_not_authorized = 'Вы не авторизованы!'; } 

    index.php

     $lang = 'langs\\'.$CFG['lang']; spl_autoload_register(function ($class) { include_once str_replace('\\', '/', $class).'.php'; }); echo $lang::fo_not_authorized; 
    • Is there certainty that in both test cases $ CFG ['lang'] === 'ru' comes here? Your question is not completely clear. Rather, there is confidence that with a successful connection to Joomla, this element of the array definitely does not come))) However, BEFORE connecting, it definitely is. And exactly is equal to 'ru'. In $ CFG ['auth'] there are only three elements. There is nothing that affects my problem. The array, however, does not contain the element 0002_fo_not_authorized Uniquely contains. For if the connection to Joomla does not occur, then this element of the array is output without problems. and see what the same NULL will show (( - user3543081
    • @ user3543081 Look here. Immediately after require 'langs/' . $CFG['lang'] . '.php'; require 'langs/' . $CFG['lang'] . '.php'; there is an array. After include $CFG['auth']; he disappears. Hence the conclusion that somewhere in there it is redefined. Wanghui require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); . In general, do a search for the word $FO_LANG across all files and see where it can be redefined. - rjhdby
    • @ user3543081 in general, such jambs are looked for by a reliable antiquated method. var_dump($FO_LANG) every second line. And when you find the point where it is lost - fall further. - rjhdby
    • The fact is that the $FO_LANG created by me (for a separate page) and, accordingly, it cannot be redefined anywhere. I will give the names of all the files in question and add the fourth file there. And it will be all my files with the full, at the moment, their content (I specifically cleared them to leave only the relevant content of the problem). In addition, the project is only Joomla. And its code somewhere "eats" my array. However, assuming that the problem is typical for Joomla, he hoped that they would be prompted by Joomlava ... - user3543081
    • @ user3543081 Well, you understand that miracles do not happen and by itself Joomla will not destroy your arrays? Do not attribute to her the devil's wiles. You only need to write in three places var_dump($FO_LANG); in order to localize the problem in index.php . Just 3 lines, and what a saving of time and nerves. I will even now add these lines to the question in the form of comments. - rjhdby