@@ -119,31 +119,23 @@ public static function get_all_information_of_language($parent_id)
119
119
* Get all information of chamilo file.
120
120
*
121
121
* @param string $system_path_file The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php)
122
- * @param bool $get_as_string_index Whether we want to remove the '$' prefix in the results or not
123
122
*
124
123
* @return array Contains all information of chamilo file
125
124
*/
126
- public static function get_all_language_variable_in_file ($ system_path_file , $ get_as_string_index = false )
127
- {
128
- $ res_list = [];
129
- if (!is_readable ($ system_path_file )) {
130
- return $ res_list ;
131
- }
132
- $ info_file = file ($ system_path_file );
133
- foreach ($ info_file as $ line ) {
134
- if (substr ($ line , 0 , 1 ) != '$ ' ) {
135
- continue ;
136
- }
137
- list ($ var , $ val ) = explode ('= ' , $ line , 2 );
138
- $ var = trim ($ var );
139
- $ val = trim ($ val );
140
- if ($ get_as_string_index ) { //remove the prefix $
141
- $ var = substr ($ var , 1 );
142
- }
143
- $ res_list [$ var ] = $ val ;
144
- }
125
+ public static function get_all_language_variable_in_file (string $ system_path_file ): array {
126
+ ob_start ();
127
+
128
+ include $ system_path_file ;
145
129
146
- return $ res_list ;
130
+ ob_end_clean ();
131
+
132
+ $ variables = get_defined_vars ();
133
+
134
+ unset($ variables ['system_path_file ' ]);
135
+ unset($ variables ['get_as_string_index ' ]);
136
+ unset($ variables ['php_errormsg ' ]);
137
+
138
+ return $ variables ;
147
139
}
148
140
149
141
/**
@@ -171,16 +163,16 @@ public static function add_file_in_language_directory($system_path_file)
171
163
*/
172
164
public static function write_data_in_file ($ path_file , $ new_term , $ new_variable )
173
165
{
166
+ // Replace double quotes to avoid parse errors
167
+ $ new_term = addcslashes ($ new_term , "\$\"\\" );
168
+ // Replace new line signs to avoid parse errors - see #6773
169
+ $ new_term = str_replace ("\n" , "\\n " , $ new_term );
170
+
174
171
$ return_value = false ;
175
- $ new_data = $ new_variable .'= ' .$ new_term ;
172
+ $ new_data = ' $ ' . $ new_variable .'=" ' .$ new_term. ' "; ' . PHP_EOL ;
176
173
$ resource = @fopen ($ path_file , "a " );
177
174
if (file_exists ($ path_file ) && $ resource ) {
178
- if (fwrite ($ resource , $ new_data .PHP_EOL ) === false ) {
179
- //not allow to write
180
- $ return_value = false ;
181
- } else {
182
- $ return_value = true ;
183
- }
175
+ $ return_value = !(fwrite ($ resource , $ new_data ) === false );
184
176
fclose ($ resource );
185
177
}
186
178
0 commit comments