Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

eZPBINIFile.php
Go to the documentation of this file.
1 XSS<?php
3 //
4 // $Id: eZPBINIFile.php 9625 2002-06-11 08:36:33Z jhe $
5 //
6 // Implements a simple INI-file parser
7 //
8 // Based upon class.INIfile.php by Mircho Mirev <mircho@macropoint.com>
9 //
10 // Created on: <09-Jun-2001 07:18:20 bf>
11 //
12 // This source file is part of Exponential Basic, publishing software.
13 //
14 // Copyright (C) 1999-2001 eZ Systems. All rights reserved.
15 //
16 // This program is free software; you can redistribute it and/or
17 // modify it under the terms of the GNU General Public License
18 // as published by the Free Software Foundation; either version 2
19 // of the License, or (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program; if not, write to the Free Software
28 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
29 //
30
33
55 class eZPBINIFile
56 {
57
61 function __construct( $inifilename = "", $write = false )
62 {
63 //include_once( "kernel/classes/ezfile.php" );
64
65 $cachedFile = "kernel/classes/cache/" . md5( eZPBFile::realpath( $inifilename ) ) . ".php";
66
67 // check for modifications
68 $cacheTime = eZPBFile::filemtime( $cachedFile );
69 $origTime = eZPBFile::filemtime( $inifilename );
70 $overrideTime = eZPBFile::filemtime( "settings/override/" . basename($inifilename) );
71 $appendTime = eZPBFile::filemtime( "settings/override/" . basename($inifilename) . ".append" );
72
73 $loadCache = false;
74 if ( file_exists( $cachedFile ) )
75 {
76 $loadCache = true;
77 if ( $cacheTime < $origTime )
78 $loadCache = false;
79 if ( file_exists( "settings/override/" . basename($inifilename) ) and $cacheTime < $overrideTime )
80 $loadCache = false;
81 if ( file_exists( "settings/override/" . basename($inifilename) . ".append" ) and $cacheTime < $appendTime )
82 $loadCache = false;
83 }
84
85 if ( $loadCache )
86 {
87 include( $cachedFile );
88 }
89 else
90 {
91 $this->load_data( $inifilename, $write );
92 // save the data to a cached file
93 $buffer = "";
94 $i = 0;
95 reset( $this->GROUPS );
96
97 foreach( $this->GROUPS as $groupKey => $groupVal )
98 {
99 reset( $groupVal );
100
101 foreach( $groupVal as $key => $val )
102 {
103 $tmpVal = str_replace( "\"", "\\\"", $val );
104
105 $buffer .= "\$Array_" . $i . "[\"$key\"] = \"$tmpVal\";\n";
106 }
107
108 $buffer .= "\$this->GROUPS[\"$groupKey\"] =& \$Array_" . $i . ";\n";
109 $i++;
110 }
111 $buffer = "<?php\n" . $buffer . "\n?>";
112
113 $fp = eZPBFile::fopen( $cachedFile, "w+" );
114 fwrite ( $fp, $buffer );
115 fclose( $fp );
116 }
117
118 }
119
120 function load_data( $inifilename = "", $write = true, $useoverride = true )
121 {
122 $this->WRITE_ACCESS = $write;
123 if ( !empty( $inifilename ) )
124 {
125 if ( !file_exists( $inifilename ) )
126 {
127 if ( file_exists( $inifilename . ".php") )
128 {
129 $this->parse( $inifilename . ".php" );
130 }
131 else
132 {
133 $this->error( "The file \"" . $inifilename . "\" or \"" . $inifilename . ".php\" does not exist!" );
134 }
135 }
136 else
137 {
138 if ( $inifilename == "settings/site.ini" )
139 {
140 $filesiteini = "site.ini";
141
142 if ( file_exists( "settings/override/$filesiteini" ) )
143 {
144 if ( file_exists( "settings/override/$filesiteini" . ".php") )
145 {
146 $this->parse( "settings/override/$filesiteini" . ".php" );
147 }
148 else
149 {
150 $this->parse( "settings/override/$filesiteini" );
151 $this->error( "The file \"" . "settings/override/$filesiteini" . "\" or \"" . "settings/override/$filesiteini" . ".php\" does not exist!" );
152 }
153 }
154 else
155 {
156 if ( file_exists( "settings/$filesiteini" ) )
157 {
158 if ( file_exists( "settings/$filesiteini" . ".php") )
159 {
160 $this->parse( "settings/$filesiteini" . ".php" );
161 }
162 else
163 {
164 $this->parse( "settings/$filesiteini" );
165 $this->error( "The file \"" . "settings/$filesiteini" . "\" or \"" . "settings/$filesiteini" . ".php\" does not exist!" );
166 }
167 }
168 }
169 }
170 else
171 {
172 $this->parse( $inifilename );
173 }
174 }
175 }
176 if ( $useoverride )
177 {
178 $this->load_override_data( "settings/override/" . basename($inifilename) );
179 }
180 }
181
182 function load_override_data( $inifilename = "" )
183 {
184 $appendfilename = $inifilename . ".append";
185 if ( !empty( $inifilename ) and file_exists( $inifilename ) )
186 {
187 $this->parse( $inifilename, false );
188 }
189 else if ( !empty( $appendfilename ) and file_exists( $appendfilename ) )
190 {
191 $this->parse( $appendfilename, true );
192 }
193 }
194
195 static public function file_exists( $inifilename )
196 {
197 return ( file_exists( "override/" . $inifilename . ".append" ) or
198 file_exists( "override/" . $inifilename ) or
199 file_exists( $inifilename ) );
200 }
201
205 function parse( $inifilename, $append = false )
206 {
207 $this->INI_FILE_NAME = $inifilename;
208
209 $fp = eZPBFile::fopen( $inifilename, $this->WRITE_ACCESS ? "r+" : "r" );
210
211 if ( !isset( $this->CURRENT_GROUP ) or !$append )
212 $this->CURRENT_GROUP = false;
213 if ( !isset( $this->GROUPS ) or !$append )
214 $this->GROUPS = array();
215
216 $contents = fread( $fp, eZPBFile::filesize( $inifilename ) );
217
218 // Remove trailing empty lines in the file
219 // This should be the code used, but for some reason it will not work
220 //$contents = eregi_replace( "\n*$", "", $contents );
221
222 // So instead we'll do a hack until the reason for the error is found
223 $contents .= "\n";
224
225 $ini_data = preg_split( "/\n/",$contents );
226
227 foreach( $ini_data as $key => $data )
228 {
229 // Remove MS-DOS Carriage return from end of line
230 $data = preg_replace( "/\r*$/", "", $data );
231 $this->parse_data( $data );
232 }
233
234 fclose( $fp );
235 }
236
240 function parse_data( $data )
241 {
242 // Remove comments from line
243 if ( preg_match( "/([^#]*)#.*/", $data, $m ) )
244 {
245 $data = $m[1];
246 }
247
248 if( preg_match( "/^\[([[:alnum:]]+)\]/", $data, $out ) )
249 {
250 $this->CURRENT_GROUP = strtolower( $out[1] );
251 }
252 else
253 {
254 $split_data = preg_split( "/=/", $data );
255
256 if ( !isset( $split_data[1] ) )
257 $split_data[1] = "";
258 $this->GROUPS[ $this->CURRENT_GROUP ][ $split_data[0] ] = $split_data[1];
259 }
260 }
261
265 function save_data()
266 {
267 $fp = eZPBFile::fopen( $this->INI_FILE_NAME, "w" );
268
269 if ( empty( $fp ) )
270 {
271 $this->Error( "Cannot create file " . $this->INI_FILE_NAME );
272 return false;
273 }
274
275 $groups = $this->read_groups();
276 $group_cnt = count( $groups );
277
278 for( $i = 0; $i < $group_cnt; $i++ )
279 {
280 $group_name = $groups[ $i ];
281 if ( $i == 0 )
282 {
283 $res = sprintf( "[%s]\n", $group_name );
284 }
285 else
286 {
287 $res = sprintf( "\n[%s]\n",$group_name );
288 }
289 fwrite( $fp, $res );
290
291 $group = $this->read_group( $group_name );
292
293 foreach( $group as $key => $data )
294 {
295 $res = "$key=$data\n";
296 fwrite( $fp, $res );
297 }
298 }
299
300 fclose( $fp );
301 }
302
306 function get_group_count()
307 {
308 return count($this->GROUPS);
309 }
310
314 function read_groups()
315 {
316 $groups = array();
317 for ( reset( $this->GROUPS ); $key = key( $this->GROUPS ); next( $this->GROUPS ) )
318 $groups[] = $key;
319 return $groups;
320 }
321
325 function group_exists( $group_name )
326 {
327 $group_name = strtolower( $group_name );
328 $group =& $this->GROUPS[ $group_name ];
329 if ( empty( $group ) ) return false;
330 else return true;
331 }
332
336 function read_group( $group_name )
337 {
338 $group_name = strtolower( $group_name );
339 $group_array =& $this->GROUPS[ $group_name ];
340 if(! empty( $group_array ) )
341 return $group_array;
342 else
343 {
344 $this->Error( "Group " . $group_name . " does not exist" );
345 return false;
346 }
347 }
348
352 function add_group( $group_name )
353 {
354 $group_name = strtolower( $group_name );
355 $new_group = $this->GROUPS[ $group_name ];
356 if ( empty( $new_group ) )
357 {
358 $this->GROUPS[ $group_name ] = array();
359 }
360 else
361 {
362 $this->Error( "Group " . $group_name . " exists" );
363 }
364 }
365
369 function empty_group( $group_name )
370 {
371 $group_name = strtolower( $group_name );
372 unset( $this->GROUPS[ $group_name ] );
373 $this->GROUPS[ $group_name ] = array();
374 }
375
379 function has_var( $group_name, $var_name )
380 {
381 $group_name = strtolower( $group_name );
382 return isset( $this->GROUPS[ $group_name ] ) and isset( $this->GROUPS[ $group_name ][ $var_name ] );
383 }
384
388 function read_var( $group_name, $var_name )
389 {
390 $group_name = strtolower( $group_name );
391
392 // EP: multilingual interface in administrator ---------------------------------------------------
393
394 // if ( $var_name == "Language" and $GLOBALS["SCRIPT_NAME"] == "/index_admin.php" )
395 // {
396 // global $Language;
397 //
398 // include_once( "kernel/ezsession/classes/ezsession.php" );
399 // $session =& eZSession::globalSession();
400 // $session->fetch();
401 //
402 // if ( isset ( $Language ))
403 // {
404 // $session->setVariable( "AdminSiteLanguage", $Language );
405 // }
406 //
407 // $Language =& $session->variable( "AdminSiteLanguage" );
408 // }
409 //
410 // if ( $Language <> "" )
411 // {
412 // return $Language;
413 // }
414
415 // EP --------------------------------------------------------------------------------------------
416
417 if ( !isset( $this->GROUPS[ $group_name ] ) or !isset( $this->GROUPS[ $group_name ][ $var_name ] ) )
418 {
419 $this->Error( $var_name . " does not exist in " . $group_name );
420 return false;
421 }
422 return $this->GROUPS[ $group_name ][ $var_name ];
423 }
424
431 function read_array( $group_name, $var_name )
432 {
433 if ( $this->hasVariable( $group_name, $var_name ) )
434 {
435 $var_value =& $this->variable( $group_name, $var_name );
436 if ( $var_value != "" )
437 {
438 $var_array = explode( ";", $var_value );
439 }
440 else
441 {
442 $var_array = array();
443 }
444 return $var_array;
445 }
446 else
447 {
448 $this->Error( "array " . $var_name . " does not exist in " . $group_name );
449 return false;
450 }
451 }
452
456 function set_var( $group_name, $var_name, $var_value )
457 {
458 $group_name = strtolower( $group_name );
459 $this->GROUPS[ $group_name ][ $var_name ] = $var_value;
460 }
461
465 function error( $errmsg )
466 {
467 $this->ERROR = $errmsg;
468 $ini = $this->globalINI();
469 if ( $ini->variable( "site", "DebugOutput" ) == "enabled" )
470 {
471 print( "Error:" . $this->ERROR . "<br>\n" );
472 }
473 return;
474 }
475
482 static public function &globalINI( $type = "SiteIni", $file = "settings/site.ini" )
483 {
484 $ini =& $GLOBALS["INI_$type"];
485
486 if ( !is_a( $ini, "eZPBINIFile" ) )
487 {
488 $ini = new eZINI( $file );
489 }
490
491 return $ini;
492 }
493
494 var $INI_FILE_NAME = "";
495 var $ERROR = "";
496 var $GROUPS = array();
497 var $CURRENT_GROUP = "";
498 var $WRITE_ACCESS = "";
499 var $Index = "";
500 var $WWWDir = "";
501 var $SiteDir = "";
502 }
503
504 ?>
$i
$i
Definition: admin/adlist.php:91
$group
$group
Definition: admin/articleedit.php:799
eZINI
Reads and writes .ini style configuration files.
eZPBFile\fopen
static fopen( $filename, $options)
Definition: ezpbfile.php:326
eZPBFile\realpath
static realpath( $filename)
Definition: ezpbfile.php:415
eZPBFile\filemtime
static filemtime( $filename)
Definition: ezpbfile.php:306
eZPBFile\filesize
static filesize( $filename)
Definition: ezpbfile.php:339
eZPBINIFile
Definition: eZPBINIFile.php:56
eZPBINIFile\load_data
load_data( $inifilename="", $write=true, $useoverride=true)
Definition: eZPBINIFile.php:120
eZPBINIFile\__construct
__construct( $inifilename="", $write=false)
Definition: eZPBINIFile.php:61
$ini
$ini
Definition: classes/admin/linklist.php:64
$type
$type
Definition: classes/admin/linkselect.php:220
read_group
read_group( $group_name)
Definition: eZPBINIFile.php:336
read_var
read_var( $group_name, $var_name)
Definition: eZPBINIFile.php:388
load_override_data
if( $useoverride) load_override_data( $inifilename="")
Definition: eZPBINIFile.php:182
$WRITE_ACCESS
$WRITE_ACCESS
Definition: eZPBINIFile.php:498
$SiteDir
$SiteDir
Definition: eZPBINIFile.php:501
$ERROR
$ERROR
Definition: eZPBINIFile.php:495
read_array
read_array( $group_name, $var_name)
Definition: eZPBINIFile.php:431
$Index
$Index
Definition: eZPBINIFile.php:499
parse
parse( $inifilename, $append=false)
Definition: eZPBINIFile.php:205
get_group_count
get_group_count()
Definition: eZPBINIFile.php:306
parse_data
parse_data( $data)
Definition: eZPBINIFile.php:240
has_var
has_var( $group_name, $var_name)
Definition: eZPBINIFile.php:379
$INI_FILE_NAME
$INI_FILE_NAME
Definition: eZPBINIFile.php:494
read_groups
read_groups()
Definition: eZPBINIFile.php:314
group_exists
group_exists( $group_name)
Definition: eZPBINIFile.php:325
set_var
set_var( $group_name, $var_name, $var_value)
Definition: eZPBINIFile.php:456
empty_group
empty_group( $group_name)
Definition: eZPBINIFile.php:369
add_group
add_group( $group_name)
Definition: eZPBINIFile.php:352
file_exists
static file_exists( $inifilename)
Definition: eZPBINIFile.php:195
error
error( $errmsg)
Definition: eZPBINIFile.php:465
globalINI
static & globalINI( $type="SiteIni", $file="settings/site.ini")
Definition: eZPBINIFile.php:482
$CURRENT_GROUP
$CURRENT_GROUP
Definition: eZPBINIFile.php:497
$GROUPS
$GROUPS
Definition: eZPBINIFile.php:496
save_data
save_data()
Definition: eZPBINIFile.php:265
$WWWDir
$WWWDir
Definition: eZPBINIFile.php:500
$fp
$fp
Definition: export.php:97
$contents
$contents
Definition: ezarticle/user/extendedsearch.php:52
$groups
if( $CategoryID !=0) $groups
Definition: ezbulkmail/admin/categoryedit.php:108
$file
$file
Definition: ezfilemanager/user/filedownload.php:35
$group_array
$group_array
Definition: ezgroupeventcalendar/user/dayview.php:836
$cachedFile
$cachedFile
Definition: eznewsfeed/user/headlines.php:109
$res
$res
Definition: ezstats/admin/cron.php:45
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.