00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 include_once 'lib/ezutils/classes/ezdebug.php';
00070 include_once( 'lib/ezfile/classes/ezdir.php' );
00071
00072
00073
00074
00075
00076
00077 define( "EZ_INI_CACHE_CODE_DATE", 1043407542 );
00078 define( "EZ_INI_DEBUG_INTERNALS", false );
00079
00080 class eZINI
00081 {
00082
00083
00084
00085 function eZINI( $fileName, $rootDir = "", $useTextCodec = null, $useCache = null, $useLocalOverrides = null, $directAccess = false, $addArrayDefinition = false )
00086 {
00087 $this->Charset = "utf8";
00088 if ( $fileName == "" )
00089 $fileName = "site.ini";
00090 if ( $rootDir !== false && $rootDir == "" )
00091 $rootDir = "settings";
00092 if ( $useCache === null )
00093 $useCache = eZINI::isCacheEnabled();
00094 if ( eZINI::isNoCacheAdviced() )
00095 {
00096 $useCache = false;
00097 }
00098 if ( $useTextCodec === null )
00099 $useTextCodec = eZINI::isTextCodecEnabled();
00100
00101 $this->UseTextCodec = $useTextCodec;
00102 $this->Codec = null;
00103 $this->FileName = $fileName;
00104 $this->RootDir = $rootDir;
00105 $this->UseCache = $useCache;
00106 $this->DirectAccess = $directAccess;
00107 $this->UseLocalOverrides = $useLocalOverrides;
00108 $this->AddArrayDefinition = $addArrayDefinition;
00109
00110 if ( $this->UseLocalOverrides == true )
00111 {
00112 $this->LocalOverrideDirArray = $GLOBALS["eZINIOverrideDirList"];
00113 }
00114
00115 $this->load();
00116 }
00117
00118
00119
00120
00121 function filename()
00122 {
00123 return $this->FileName;
00124 }
00125
00126
00127
00128
00129
00130
00131 function isCacheEnabled()
00132 {
00133 if ( !isset( $GLOBALS['eZINICacheEnabled'] ) )
00134 $GLOBALS['eZINICacheEnabled'] = true;
00135 return $GLOBALS['eZINICacheEnabled'];
00136 }
00137
00138
00139
00140
00141
00142 function isNoCacheAdviced()
00143 {
00144 if ( !isset( $GLOBALS['eZSiteBasics'] ) )
00145 return false;
00146 $siteBasics = $GLOBALS['eZSiteBasics'];
00147 if ( !isset( $siteBasics['no-cache-adviced'] ) )
00148 return false;
00149 return $siteBasics['no-cache-adviced'];
00150 }
00151
00152
00153
00154
00155
00156
00157 function setIsCacheEnabled( $cache )
00158 {
00159 $GLOBALS['eZINICacheEnabled'] = $cache;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 function isDebugEnabled()
00169 {
00170 if ( !isset( $GLOBALS['eZINIDebugInternalsEnabled'] ) )
00171 $GLOBALS['eZINIDebugInternalsEnabled'] = EZ_INI_DEBUG_INTERNALS;
00172 return $GLOBALS['eZINIDebugInternalsEnabled'];
00173 }
00174
00175
00176
00177
00178
00179 function setIsDebugEnabled( $debug )
00180 {
00181 $GLOBALS['eZINIDebugInternalsEnabled'] = $debug;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190 function isTextCodecEnabled()
00191 {
00192 if ( !isset( $GLOBALS['eZINITextCodecEnabled'] ) )
00193 $GLOBALS['eZINITextCodecEnabled'] = true;
00194 return $GLOBALS['eZINITextCodecEnabled'];
00195 }
00196
00197
00198
00199
00200
00201 function setIsTextCodecEnabled( $codec )
00202 {
00203 $GLOBALS['eZINITextCodecEnabled'] = $codec;
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 function parameterSet( $fileName = 'site.ini', $rootDir = 'settings', &$section, &$parameter )
00216 {
00217 if ( !eZINI::exists( $fileName, $rootDir ) )
00218 return false;
00219
00220 $iniInstance =& eZINI::instance( $fileName, $rootDir, null, null, null, true );
00221 return $iniInstance->hasVariable( $section, $parameter );
00222 }
00223
00224
00225
00226
00227
00228
00229 function exists( $fileName = "site.ini", $rootDir = "settings" )
00230 {
00231 if ( $fileName == "" )
00232 $fileName = "site.ini";
00233 if ( $rootDir == "" )
00234 $rootDir = "settings";
00235 if ( file_exists( $rootDir . '/' . $fileName ) )
00236 return true;
00237 else if ( file_exists( $rootDir . '/' . $fileName . '.append' ) )
00238 return true;
00239 else if ( file_exists( $rootDir . '/' . $fileName . '.append.php' ) )
00240 return true;
00241 return false;
00242 }
00243
00244
00245
00246
00247
00248
00249 function load( $reset = true )
00250 {
00251 if ( $this->UseCache )
00252 {
00253 $this->loadCache( $reset );
00254 }
00255 else
00256 {
00257 $this->parse( false, false, $reset );
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266 function loadPlacement( $reset = true )
00267 {
00268 if ( $this->UseCache )
00269 {
00270 $this->loadCache( $reset, true );
00271 }
00272 else
00273 {
00274 $this->parse( false, false, $reset, true );
00275 }
00276 }
00277
00278
00279
00280
00281
00282
00283
00284 function findInputFiles( &$inputFiles, &$iniFile )
00285 {
00286 if ( $this->RootDir !== false )
00287 $iniFile = eZDir::path( array( $this->RootDir, $this->FileName ) );
00288 else
00289 $iniFile = eZDir::path( array( $this->FileName ) );
00290
00291 $inputFiles = array();
00292
00293 if ( $this->FileName == 'override.ini' )
00294 {
00295 eZExtension::prependExtensionSiteAccesses( false, $this, true, false, false );
00296 }
00297
00298 if ( file_exists( $iniFile ) )
00299 $inputFiles[] = $iniFile;
00300
00301
00302 if ( preg_match('/^(.+.append).php$/i', $iniFile, $matches ) && file_exists( $matches[1] ) )
00303 $inputFiles[] = $matches[1];
00304
00305 if ( file_exists ( $iniFile . '.php' ) )
00306 $inputFiles[] = $iniFile . '.php';
00307
00308 if ( $this->DirectAccess )
00309 {
00310 if ( file_exists ( $iniFile . '.append' ) )
00311 $inputFiles[] = $iniFile . '.append';
00312
00313 if ( file_exists ( $iniFile . '.append.php' ) )
00314 $inputFiles[] = $iniFile . '.append.php';
00315 }
00316 else
00317 {
00318 $overrideDirs = $this->overrideDirs();
00319 foreach ( $overrideDirs as $overrideDirItem )
00320 {
00321 $overrideDir = $overrideDirItem[0];
00322 $isGlobal = $overrideDirItem[1];
00323 if ( $isGlobal )
00324 $overrideFile = eZDir::path( array( $overrideDir, $this->FileName ) );
00325 else
00326 $overrideFile = eZDir::path( array( $this->RootDir, $overrideDir, $this->FileName ) );
00327 if ( file_exists( $overrideFile . '.php' ) )
00328 {
00329 $inputFiles[] = $overrideFile . '.php';
00330 }
00331 if ( file_exists( $overrideFile ) )
00332 $inputFiles[] = $overrideFile;
00333
00334 if ( $isGlobal )
00335 $overrideFile = eZDir::path( array( $overrideDir, $this->FileName . '.append' ) );
00336 else
00337 $overrideFile = eZDir::path( array( $this->RootDir, $overrideDir, $this->FileName . '.append' ) );
00338 if ( file_exists( $overrideFile . '.php' ) )
00339 {
00340 $inputFiles[] = $overrideFile . '.php';
00341 }
00342 if ( file_exists( $overrideFile ) )
00343 $inputFiles[] = $overrideFile;
00344 }
00345 }
00346 }
00347
00348
00349
00350
00351
00352
00353 function loadCache( $reset = true, $placement = false )
00354 {
00355 eZDebug::accumulatorStart( 'ini', 'ini_load', 'Load cache' );
00356 if ( $reset )
00357 $this->reset();
00358 $cachedDir = "var/cache/ini/";
00359
00360 eZDebug::accumulatorStart( 'ini_find_files', 'ini_load', 'FindInputFiles' );
00361 $this->findInputFiles( $inputFiles, $iniFile );
00362 eZDebug::accumulatorStop( 'ini_find_files' );
00363 if ( count( $inputFiles ) == 0 )
00364 {
00365 eZDebug::accumulatorStop( 'ini' );
00366 return false;
00367 }
00368
00369
00370
00371
00372
00373
00374 $md5_input = '';
00375 foreach ( $inputFiles as $inputFile )
00376 {
00377 $md5_input .= $inputFile. "\n";
00378 }
00379 if ( $this->UseTextCodec )
00380 {
00381 include_once( "lib/ezi18n/classes/eztextcodec.php" );
00382 $md5_input .= '-' . eZTextCodec::internalCharset();
00383 }
00384 if ( $placement )
00385 {
00386 $md5_input .= '-placement';
00387 }
00388 $fileName = md5( $md5_input ) . ".php";
00389 $cachedFile = $cachedDir . $fileName;
00390 if ( $placement )
00391 {
00392 $this->PlacementCacheFile = $cachedFile;
00393 }
00394 else
00395 {
00396 $this->CacheFile = $cachedFile;
00397 }
00398
00399 $inputTime = false;
00400
00401 foreach ( $inputFiles as $inputFile )
00402 {
00403 $fileTime = filemtime( $inputFile );
00404 if ( $inputTime === false or
00405 $fileTime > $inputTime )
00406 $inputTime = $fileTime;
00407 }
00408
00409 $loadCache = false;
00410 $cacheTime = false;
00411 if ( file_exists( $cachedFile ) )
00412 {
00413 $fileInfo = @stat( $cachedFile );
00414 if ( $fileInfo )
00415 {
00416 $cacheTime = $fileInfo['mtime'];
00417 $loadCache = true;
00418 if ( $cacheTime < $inputTime )
00419 {
00420 $loadCache = false;
00421 }
00422 }
00423 }
00424
00425 $useCache = false;
00426 if ( $loadCache )
00427 {
00428 $useCache = true;
00429 if ( eZINI::isDebugEnabled() )
00430 eZDebug::writeNotice( "Loading cache '$cachedFile' for file '" . $this->FileName . "'", "eZINI" );
00431 $charset = null;
00432 $blockValues = array();
00433 include( $cachedFile );
00434 if ( !isset( $val ) or
00435 !isset( $eZIniCacheCodeDate ) or
00436 $eZIniCacheCodeDate != EZ_INI_CACHE_CODE_DATE )
00437 {
00438 if ( eZINI::isDebugEnabled() )
00439 eZDebug::writeNotice( "Old structure in cache file used, recreating '$cachedFile' to new structure", "eZINI" );
00440 $this->reset();
00441 $useCache = false;
00442 }
00443 else
00444 {
00445 $this->Charset = $charset;
00446 $this->ModifiedBlockValues = array();
00447 if ( $placement )
00448 {
00449 $this->BlockValuesPlacement = $val;
00450 }
00451 else
00452 {
00453 $this->BlockValues = $val;
00454 }
00455 unset( $val );
00456 }
00457 }
00458 if ( !$useCache )
00459 {
00460 eZDebug::accumulatorStart( 'ini_files_1', 'ini_load', 'Parse' );
00461 $this->parse( $inputFiles, $iniFile, false, $placement );
00462 eZDebug::accumulatorStop( 'ini_files_1' );
00463 eZDebug::accumulatorStart( 'ini_files_2', 'ini_load', 'Save Cache' );
00464 $cacheSaved = $this->saveCache( $cachedDir, $cachedFile, $placement ? $this->BlockValuesPlacement : $this->BlockValues );
00465 eZDebug::accumulatorStop( 'ini_files_2' );
00466
00467 if ( $cacheSaved )
00468 {
00469
00470 include_once( 'lib/ezutils/classes/ezlog.php' );
00471 eZLog::writeStorageLog( $fileName, $cachedDir );
00472 }
00473 }
00474
00475 eZDebug::accumulatorStop( 'ini' );
00476 }
00477
00478
00479
00480
00481
00482 function saveCache( $cachedDir, $cachedFile, $data )
00483 {
00484 if ( !file_exists( $cachedDir ) )
00485 {
00486 include_once( 'lib/ezfile/classes/ezdir.php' );
00487 if ( !eZDir::mkdir( $cachedDir, 0777, true ) )
00488 {
00489 eZDebug::writeError( "Couldn't create cache directory $cachedDir, perhaps wrong permissions", "eZINI" );
00490 return false;
00491 }
00492 }
00493 $tmpCacheFile = $cachedFile . '_' . substr( md5( mt_rand() ), 0, 8 );
00494
00495 $fp = @fopen( $tmpCacheFile, "w" );
00496 if ( $fp === false )
00497 {
00498 eZDebug::writeError( "Couldn't create cache file '$cachedFile', perhaps wrong permissions", "eZINI" );
00499 return false;
00500 }
00501 fwrite( $fp, "<?php\n\$eZIniCacheCodeDate = " . EZ_INI_CACHE_CODE_DATE . ";\n" );
00502
00503 if ( $this->Codec )
00504 fwrite( $fp, "\$charset = \"".$this->Codec->RequestedOutputCharsetCode."\";\n" );
00505 else
00506 fwrite( $fp, "\$charset = \"$this->Charset\";\n" );
00507
00508 fwrite( $fp, "\$val = " . preg_replace( "@\n[\s]+@", '', var_export( $data, true ) ) . ";" );
00509 fwrite( $fp, "\n?>" );
00510 fclose( $fp );
00511 include_once( 'lib/ezfile/classes/ezfile.php' );
00512 eZFile::rename( $tmpCacheFile, $cachedFile );
00513
00514 if ( eZINI::isDebugEnabled() )
00515 eZDebug::writeNotice( "Wrote cache file '$cachedFile'", "eZINI" );
00516
00517 return true;
00518 }
00519
00520
00521
00522
00523
00524
00525 function parse( $inputFiles = false, $iniFile = false, $reset = true, $placement = false )
00526 {
00527 if ( $reset )
00528 $this->reset();
00529 if ( $inputFiles === false or
00530 $iniFile === false )
00531 $this->findInputFiles( $inputFiles, $iniFile );
00532
00533 foreach ( $inputFiles as $inputFile )
00534 {
00535 if ( file_exists( $inputFile ) )
00536 {
00537 $this->parseFile( $inputFile, $placement );
00538 }
00539 }
00540 }
00541
00542
00543
00544
00545
00546 function parseFile( $file, $placement = false )
00547 {
00548 if ( eZINI::isDebugEnabled() )
00549 eZDebug::writeNotice( "Parsing file '$file'", 'eZINI' );
00550
00551 include_once( "lib/ezfile/classes/ezfile.php" );
00552 $contents = eZFile::getContents( $file );
00553 if ( $contents === false )
00554 {
00555 eZDebug::writeError( "Failed opening file '$file' for reading", "eZINI" );
00556 return false;
00557 }
00558
00559 $contents = str_replace( "\r", '', $contents );
00560 $endOfLine = strpos( $contents, "\n" );
00561 $line = substr( $contents, 0, $endOfLine );
00562
00563 $currentBlock = "";
00564 if ( $line )
00565 {
00566
00567 if ( preg_match( "/#\?ini(.+)\?/", $line, $ini_arr ) )
00568 {
00569 $args = explode( " ", trim( $ini_arr[1] ) );
00570 foreach ( $args as $arg )
00571 {
00572 $vars = explode( '=', trim( $arg ) );
00573 if ( $vars[0] == "charset" )
00574 {
00575 $val = $vars[1];
00576 if ( $val[0] == '"' and
00577 strlen( $val ) > 0 and
00578 $val[strlen($val)-1] == '"' )
00579 $val = substr( $val, 1, strlen($val) - 2 );
00580 $this->Charset = $val;
00581 }
00582 }
00583 }
00584 }
00585
00586 unset( $this->Codec );
00587 if ( $this->UseTextCodec )
00588 {
00589 include_once( "lib/ezi18n/classes/eztextcodec.php" );
00590 $this->Codec =& eZTextCodec::instance( $this->Charset, false, false );
00591
00592 if ( $this->Codec )
00593 {
00594 eZDebug::accumulatorStart( 'ini_conversion', false, 'INI string conversion' );
00595 $contents = $this->Codec->convertString( $contents );
00596 eZDebug::accumulatorStop( 'ini_conversion', false, 'INI string conversion' );
00597 }
00598 }
00599 else
00600 $this->Codec = null;
00601
00602 foreach ( explode( "\n", $contents ) as $line )
00603 {
00604 if ( $line == '' or $line[0] == '#' )
00605 continue;
00606 if ( preg_match( "/^(.+)##.*/", $line, $regs ) )
00607 $line = $regs[1];
00608 if ( trim( $line ) == '' )
00609 continue;
00610
00611 if ( preg_match("#^\[(.+)\]\s*$#", $line, $newBlockNameArray ) )
00612 {
00613 $newBlockName = trim( $newBlockNameArray[1] );
00614 $currentBlock = $newBlockName;
00615 continue;
00616 }
00617
00618
00619 if ( preg_match("#^([\w_*@-]+)\\[\\]$#", $line, $valueArray ) )
00620 {
00621 $varName = trim( $valueArray[1] );
00622
00623 if ( $placement )
00624 {
00625 if ( isset( $this->BlockValuesPlacement[$currentBlock][$varName] ) &&
00626 !is_array( $this->BlockValuesPlacement[$currentBlock][$varName] ) )
00627 {
00628 eZDebug::writeError( "Wrong operation on the ini setting array '$varName'", 'eZINI' );
00629 continue;
00630 }
00631
00632 $this->BlockValuesPlacement[$currentBlock][$varName][] = $file;
00633 }
00634 else
00635 {
00636 $this->BlockValues[$currentBlock][$varName] = array();
00637
00638
00639
00640
00641 if ( $this->AddArrayDefinition )
00642 {
00643 $this->BlockValues[$currentBlock][$varName][] = "";
00644 }
00645 }
00646 }
00647 else if ( preg_match("#^([\w_*@-]+)(\\[([^\\]]*)\\])?=(.*)$#", $line, $valueArray ) )
00648 {
00649 $varName = trim( $valueArray[1] );
00650 $varValue = $valueArray[4];
00651
00652 if ( $valueArray[2] )
00653 {
00654 if ( $valueArray[3] )
00655 {
00656 $keyName = $valueArray[3];
00657 if ( $placement )
00658 {
00659 $this->BlockValuesPlacement[$currentBlock][$varName][$keyName] = $file;
00660 }
00661 else
00662 {
00663 $this->BlockValues[$currentBlock][$varName][$keyName] = $varValue;
00664 }
00665 }
00666 else
00667 {
00668 if ( $placement )
00669 {
00670 $this->BlockValuesPlacement[$currentBlock][$varName][] = $file;
00671 }
00672 else
00673 {
00674 $this->BlockValues[$currentBlock][$varName][] = $varValue;
00675 }
00676 }
00677 }
00678 else
00679 {
00680 if ( $placement )
00681 {
00682 $this->BlockValuesPlacement[$currentBlock][$varName] = $file;
00683 }
00684 else
00685 {
00686 $this->BlockValues[$currentBlock][$varName] = $varValue;
00687 }
00688 }
00689 }
00690 }
00691 }
00692
00693
00694
00695
00696 function resetCache()
00697 {
00698 if ( file_exists( $this->CacheFile ) )
00699 unlink( $this->CacheFile );
00700 if ( file_exists( $this->PlacementCacheFile ) )
00701 unlink( $this->PlacementCacheFile );
00702 }
00703
00704
00705
00706
00707
00708
00709
00710
00711 function save( $fileName = false, $suffix = false, $useOverride = false,
00712 $onlyModified = false, $useRootDir = true, $resetArrays = false,
00713 $encapsulateInPHP = true )
00714 {
00715 include_once( 'lib/ezfile/classes/ezdir.php' );
00716 $lineSeparator = eZSys::lineSeparator();
00717 $pathArray = array();
00718 $dirArray = array();
00719 if ( $fileName === false )
00720 $fileName = $this->FileName;
00721 if ( $useRootDir === true )
00722 {
00723 $pathArray[] = $this->RootDir;
00724 $dirArray[] = $this->RootDir;
00725 }
00726 else if ( is_string( $useRootDir ) )
00727 {
00728 $pathArray[] = $useRootDir;
00729 $dirArray[] = $useRootDir;
00730 }
00731 if ( $useOverride )
00732 {
00733 $pathArray[] = 'override';
00734 $dirArray[] = 'override';
00735 }
00736 if ( is_string( $useOverride ) and
00737 $useOverride == "append" )
00738 $fileName .= ".append";
00739 if ( $suffix !== false )
00740 $fileName .= $suffix;
00741
00742
00743
00744
00745
00746 if( strstr( $fileName, '.append' ) )
00747 {
00748 $fnAppend = ereg_replace( '\.php$', '', $fileName );
00749 $fnAppendPhp = $fnAppend.'.php';
00750 $fpAppend = eZDir::path( array_merge( $pathArray, $fnAppend ) );
00751 $fpAppendPhp = eZDir::path( array_merge( $pathArray, $fnAppendPhp ) );
00752 $fileName = ( file_exists( $fpAppend ) && !file_exists( $fpAppendPhp ) )
00753 ? $fnAppend : $fnAppendPhp;
00754 }
00755
00756 $originalFileName = $fileName;
00757 $backupFileName = $originalFileName . eZSys::backupFilename();
00758 $fileName .= '.tmp';
00759
00760 $dirPath = eZDir::path( $dirArray );
00761 if ( !file_exists( $dirPath ) )
00762 eZDir::mkdir( $dirPath, octdec( '777' ), true );
00763
00764 include_once( 'lib/ezfile/classes/ezdir.php' );
00765 $filePath = eZDir::path( array_merge( $pathArray, $fileName ) );
00766 $originalFilePath = eZDir::path( array_merge( $pathArray, $originalFileName ) );
00767 $backupFilePath = eZDir::path( array_merge( $pathArray, $backupFileName ) );
00768
00769 $fp = @fopen( $filePath, "w+");
00770 if ( !$fp )
00771 {
00772 eZDebug::writeError( "Failed opening file '$filePath' for writing", "eZINI" );
00773 return false;
00774 }
00775 $writeOK = true;
00776 $written = 0;
00777
00778 $charset = $this->Codec ? $this->Codec->RequestedOutputCharsetCode : $this->Charset;
00779 if ( $encapsulateInPHP )
00780 {
00781 $written = fwrite( $fp, "<?php /* #?ini charset=\"$charset\"?$lineSeparator$lineSeparator" );
00782 }
00783 else
00784 {
00785 $written = fwrite( $fp, "#?ini charset=\"$charset\"?$lineSeparator$lineSeparator" );
00786 }
00787
00788 if ( $written === false )
00789 $writeOK = false;
00790 $i = 0;
00791 if ( $writeOK )
00792 {
00793 foreach( array_keys( $this->BlockValues ) as $blockName )
00794 {
00795 if ( $onlyModified )
00796 {
00797 $groupHasModified = false;
00798 if ( isset( $this->ModifiedBlockValues[$blockName] ) )
00799 {
00800 foreach ( $this->ModifiedBlockValues[$blockName] as $modifiedValue )
00801 {
00802 if ( $modifiedValue )
00803 $groupHasModified = true;
00804 }
00805 }
00806 if ( !$groupHasModified )
00807 continue;
00808 }
00809 $written = 0;
00810 if ( $i > 0 )
00811 $written = fwrite( $fp, "$lineSeparator" );
00812 if ( $written === false )
00813 {
00814 $writeOK = false;
00815 break;
00816 }
00817 $written = fwrite( $fp, "[$blockName]$lineSeparator" );
00818 if ( $written === false )
00819 {
00820 $writeOK = false;
00821 break;
00822 }
00823 foreach( array_keys( $this->BlockValues[$blockName] ) as $blockVariable )
00824 {
00825 if ( $onlyModified )
00826 {
00827 if ( !isset( $this->ModifiedBlockValues[$blockName][$blockVariable] ) or
00828 !$this->ModifiedBlockValues[$blockName][$blockVariable] )
00829 continue;
00830 }
00831 $varKey = $blockVariable;
00832 $varValue = $this->BlockValues[$blockName][$blockVariable];
00833 if ( is_array( $varValue ) )
00834 {
00835 if ( count( $varValue ) > 0 )
00836 {
00837 $customResetArray = ( isset( $this->BlockValues[$blockName]['ResetArrays'] ) and
00838 $this->BlockValues[$blockName]['ResetArrays'] == 'false' )
00839 ? true
00840 : false;
00841 if ( $resetArrays and !$customResetArray )
00842 $written = fwrite( $fp, "$varKey" . "[]$lineSeparator" );
00843 foreach ( $varValue as $varArrayKey => $varArrayValue )
00844 {
00845 if ( is_string( $varArrayKey ) )
00846 $written = fwrite( $fp, "$varKey" . "[$varArrayKey]=$varArrayValue$lineSeparator" );
00847 else
00848 {
00849 if ( $varArrayValue == NULL )
00850 $written = fwrite( $fp, "$varKey" . "[]$lineSeparator" );
00851 else
00852 $written = fwrite( $fp, "$varKey" . "[]=$varArrayValue$lineSeparator" );
00853 }
00854 if ( $written === false )
00855 break;
00856 }
00857 }
00858 else
00859 $written = fwrite( $fp, "$varKey" . "[]$lineSeparator" );
00860 }
00861 else
00862 {
00863 $written = fwrite( $fp, "$varKey=$varValue$lineSeparator" );
00864 }
00865 if ( $written === false )
00866 {
00867 $writeOK = false;
00868 break;
00869 }
00870 }
00871 if ( !$writeOK )
00872 break;
00873 ++$i;
00874 }
00875 }
00876 if ( $writeOK )
00877 {
00878 if ( $encapsulateInPHP )
00879 {
00880 $written = fwrite( $fp, "*/ ?>" );
00881
00882 if ( $written === false )
00883 $writeOK = false;
00884 }
00885 }
00886 @fclose( $fp );
00887 if ( !$writeOK )
00888 {
00889 unlink( $filePath );
00890 return false;
00891 }
00892
00893 $siteConfig =& eZINI::instance( 'site.ini' );
00894 $filePermissions = $siteConfig->variable( 'FileSettings', 'StorageFilePermissions');
00895 @chmod( $filePath, octdec( $filePermissions ) );
00896
00897 if ( file_exists( $backupFilePath ) )
00898 unlink( $backupFilePath );
00899 if ( file_exists( $originalFilePath ) )
00900 {
00901 if ( !rename( $originalFilePath, $backupFilePath ) )
00902 return false;
00903 }
00904 if ( !rename( $filePath, $originalFilePath ) )
00905 {
00906 rename( $backupFilePath, $originalFilePath );
00907 return false;
00908 }
00909
00910 return true;
00911 }
00912
00913
00914
00915
00916 function reset()
00917 {
00918 $this->BlockValues = array();
00919 $this->ModifiedBlockValues = array();
00920 }
00921
00922
00923
00924
00925
00926
00927 function rootDir()
00928 {
00929 return $this->RootDir;
00930 }
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943 function overrideDirs()
00944 {
00945 if ( $this->UseLocalOverrides == true )
00946 $dirs =& $this->LocalOverrideDirArray;
00947 else
00948 $dirs =& $GLOBALS["eZINIOverrideDirList"];
00949
00950 if ( !isset( $dirs ) or !is_array( $dirs ) )
00951 $dirs = array( array( "override", false, false ) );
00952 return $dirs;
00953 }
00954
00955
00956
00957
00958
00959 function prependOverrideDir( $dir, $globalDir = false, $identifier = false )
00960 {
00961 if ( eZINI::isDebugEnabled() )
00962 eZDebug::writeNotice( "Changing override dir to '$dir'", "eZINI" );
00963
00964 if ( $this->UseLocalOverrides == true )
00965 $dirs =& $this->LocalOverrideDirArray;
00966 else
00967 $dirs =& $GLOBALS["eZINIOverrideDirList"];
00968
00969 if ( !isset( $dirs ) or !is_array( $dirs ) )
00970 $dirs = array( array( 'override', false, false ) );
00971
00972
00973 $overrideOverwritten = false;
00974 if ( $identifier !== false )
00975 {
00976 foreach ( array_keys( $dirs ) as $dirKey )
00977 {
00978 if ( $dirs[$dirKey][2] == $identifier )
00979 {
00980 $dirs[$dirKey][0] = $dir;
00981 $dirs[$dirKey][1] = $globalDir;
00982 $overrideOverwritten = true;
00983 }
00984 }
00985 }
00986
00987 if ( $overrideOverwritten == false )
00988 $dirs = array_merge( array( array( $dir, $globalDir, $identifier ) ), $dirs );
00989
00990 $this->CacheFile = false;
00991 }
00992
00993
00994
00995
00996 function appendOverrideDir( $dir, $globalDir = false, $identifier = false )
00997 {
00998 if ( eZINI::isDebugEnabled() )
00999 eZDebug::writeNotice( "Changing override dir to '$dir'", "eZINI" );
01000
01001 if ( $this->UseLocalOverrides == true )
01002 $dirs =& $this->LocalOverrideDirArray;
01003 else
01004 $dirs =& $GLOBALS["eZINIOverrideDirList"];
01005
01006 if ( !isset( $dirs ) or !is_array( $dirs ) )
01007 $dirs = array( 'override', false, false );
01008
01009
01010 $overrideOverwritten = false;
01011 if ( $identifier !== false )
01012 {
01013 foreach ( array_keys( $dirs ) as $dirKey )
01014 {
01015 if ( $dirs[$dirKey][2] == $identifier )
01016 {
01017 $dirs[$dirKey][0] = $dir;
01018 $dirs[$dirKey][1] = $globalDir;
01019 $overrideOverwritten = true;
01020 }
01021 }
01022 }
01023
01024 if ( $overrideOverwritten == false )
01025 $dirs[] = array( $dir, $globalDir, $identifier = false );
01026 $this->CacheFile = false;
01027 }
01028
01029
01030
01031
01032
01033 function assign( $blockName, $varName, &$variable )
01034 {
01035 if ( $this->hasVariable( $blockName, $varName ) )
01036 $variable = $this->variable( $blockName, $varName );
01037 else
01038 return false;
01039 return true;
01040 }
01041
01042
01043
01044
01045
01046 function variable( $blockName, $varName )
01047 {
01048 $ret = false;
01049 if ( !isset( $this->BlockValues[$blockName] ) )
01050 eZDebug::writeError( "Undefined group: '$blockName' in " . $this->FileName, "eZINI" );
01051 else if ( isset( $this->BlockValues[$blockName][$varName] ) )
01052 $ret = $this->BlockValues[$blockName][$varName];
01053 else
01054 eZDebug::writeError( "Undefined variable: '$varName' in group '$blockName' in " . $this->FileName, "eZINI" );
01055
01056 return $ret;
01057 }
01058
01059
01060
01061
01062
01063 function variableMulti( $blockName, $varNames, $signatures = array() )
01064 {
01065 $ret = array();
01066
01067 if ( !isset( $this->BlockValues[$blockName] ) )
01068 {
01069 eZDebug::writeError( "Undefined group: '$blockName' in " . $this->FileName, "eZINI" );
01070 return false;
01071 }
01072 foreach ( $varNames as $key => $varName )
01073 {
01074 if ( isset( $this->BlockValues[$blockName][$varName] ) )
01075 {
01076 $ret[$key] = $this->BlockValues[$blockName][$varName];
01077
01078 if ( isset( $signatures[$key] ) )
01079 {
01080 switch ( $signatures[$key] )
01081 {
01082 case 'enabled':
01083 $ret[$key] = $this->BlockValues[$blockName][$varName] == 'enabled';
01084 break;
01085 }
01086 }
01087 }
01088 else
01089 {
01090 $ret[] = null;
01091 }
01092 }
01093
01094 return $ret;
01095 }
01096
01097
01098
01099
01100 function hasVariable( $blockName, $varName )
01101 {
01102 return isset( $this->BlockValues[$blockName][$varName] );
01103 }
01104
01105
01106
01107
01108 function hasSection( $sectionName )
01109 {
01110 return isset( $this->BlockValues[$sectionName] );
01111 }
01112
01113
01114
01115
01116 function isVariableModified( $blockName, $varName )
01117 {
01118 return ( isset( $this->ModifiedBlockValues[$blockName][$varName] ) and
01119 $this->ModifiedBlockValues[$blockName][$varName] );
01120 }
01121
01122
01123
01124
01125
01126 function &variableArray( $blockName, $varName )
01127 {
01128 $ret = $this->variable( $blockName, $varName );
01129 if ( is_array( $ret ) )
01130 {
01131 $arr = array();
01132 foreach ( $ret as $key => $retItem )
01133 {
01134 $arr[$key] = explode( ";", $retItem );
01135 }
01136 $ret = $arr;
01137 }
01138 else if ( $ret !== false )
01139 {
01140 $ret = trim( $ret ) === '' ? array() : explode( ";", $ret );
01141 }
01142
01143 return $ret;
01144 }
01145
01146
01147
01148
01149 function hasGroup( $blockName )
01150 {
01151 return isSet( $this->BlockValues[$blockName] );
01152 }
01153
01154
01155
01156
01157 function &group( $blockName )
01158 {
01159 if ( !isset( $this->BlockValues[$blockName] ) )
01160 {
01161 eZDebug::writeError( "Unknown group: '$blockName'", "eZINI" );
01162 $ret = null;
01163 return $ret;
01164 }
01165 $ret = $this->BlockValues[$blockName];
01166
01167 return $ret;
01168 }
01169
01170 function isSettingReadOnly( $fileName = false, $blockName = false, $settingName = false )
01171 {
01172 if ( !$this->readOnlySettingsCheck() )
01173 return true;
01174
01175 $ini =& eZINI::instance();
01176 if ( !$ini->hasVariable( 'eZINISettings', 'ReadonlySettingList' ) )
01177 return true;
01178
01179 $fileName = $fileName === false ? $ini->FileName : $fileName;
01180 $fileNameExploded = explode( '.', $fileName );
01181 $realFileName = $fileNameExploded[0] . '.' . $fileNameExploded[1];
01182 $blockName = $blockName === false ? '*' : $blockName;
01183 $settingName = $settingName === false ? '*' : $settingName;
01184 $currentSetting = $realFileName . '/' . $blockName . '/' . $settingName;
01185
01186 $settingList = $ini->variable( 'eZINISettings', 'ReadonlySettingList' );
01187 $settingList[] = 'site.ini/eZINISettings/*';
01188
01189 $result = !( in_array( $realFileName . '/*' , $settingList ) or
01190 in_array( $realFileName . '/' . $blockName . '/*' , $settingList ) or
01191 in_array( $realFileName . '/' . $blockName . '/' . $settingName , $settingList ) );
01192
01193 return $result;
01194 }
01195
01196
01197
01198 function removeGroup( $blockName )
01199 {
01200 unset( $this->BlockValues[$blockName] );
01201 unset( $this->BlockValuesPlacement[$blockName] );
01202 }
01203
01204 function removeSetting( $blockName, $settingName )
01205 {
01206 unset( $this->BlockValues[$blockName][$settingName] );
01207 unset( $this->BlockValuesPlacement[$blockName][$settingName] );
01208 if ( $this->BlockValues[$blockName] == null )
01209 $this->removeGroup( $blockName );
01210 }
01211
01212
01213
01214
01215 function &groups()
01216 {
01217 return $this->BlockValues;
01218 }
01219
01220
01221
01222
01223 function &groupPlacements()
01224 {
01225 if ( !$this->BlockValuesPlacement )
01226 {
01227 $this->loadPlacement();
01228 }
01229 return $this->BlockValuesPlacement;
01230 }
01231
01232 function &findSettingPlacement( $path )
01233 {
01234 if ( is_array( $path ) && count( $path ) )
01235 $path = $path[0];
01236 $exploded = explode( '/', $path );
01237 $directoryCount = count( $exploded );
01238 switch ( $directoryCount )
01239 {
01240 case 2:
01241 $placement = 'default';
01242 break;
01243 case 3:
01244 $placement = 'override';
01245 break;
01246 case 4:
01247 {
01248 $placement = 'siteaccess';
01249 if ( $exploded[0] == 'extension' )
01250 $placement = 'extension:' . $exploded[1];
01251 }
01252 break;
01253 case 6:
01254 {
01255 $placement = 'ext-siteaccess:' . $exploded[4];
01256 }
01257 break;
01258 default:
01259 $placement = 'undefined';
01260 break;
01261 }
01262 return $placement;
01263 }
01264
01265 function settingType( $settingValue )
01266 {
01267 if ( is_array( $settingValue ) )
01268 return 'array';
01269
01270 if ( is_numeric( $settingValue ) )
01271 return 'numeric';
01272
01273 if ( $settingValue == 'true' or $settingValue == 'false' )
01274 {
01275 return 'true/false';
01276 }
01277 if ( $settingValue == 'enabled' or $settingValue == 'disabled' )
01278 {
01279 return 'enable/disable';
01280 }
01281
01282 return 'string';
01283 }
01284
01285
01286
01287
01288 function setGroups( $groupArray )
01289 {
01290 $resultArray = array();
01291
01292 foreach ( $groupArray as $blockName => $blockVariables )
01293 {
01294 foreach ( $blockVariables as $variableName => $variableValue )
01295 {
01296 if ( !$this->isSettingReadOnly( $this->FileName, $blockName, $variableName ) )
01297 continue;
01298 $resultArray[$blockName][$variableName] = $variableValue;
01299 }
01300 }
01301 $this->BlockValues = $resultArray;
01302 }
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314 function setVariables( $variables )
01315 {
01316 foreach ( $variables as $blockName => $blockVariables )
01317 {
01318 foreach ( $blockVariables as $variableName => $variableValue )
01319 {
01320 $this->setVariable( $blockName, $variableName, $variableValue );
01321 }
01322 }
01323 }
01324
01325
01326
01327
01328
01329
01330
01331
01332 function setVariable( $blockName, $variableName, $variableValue )
01333 {
01334 if ( !$this->isSettingReadOnly( $this->filename(), $blockName, $variableName ) )
01335 return false;
01336
01337 $this->BlockValues[$blockName][$variableName] = $variableValue;
01338 $this->ModifiedBlockValues[$blockName][$variableName] = true;
01339 }
01340
01341
01342
01343
01344 function getNamedArray()
01345 {
01346 return $this->BlockValues;
01347 }
01348
01349
01350
01351
01352
01353 function isLoaded( $fileName = "site.ini", $rootDir = "settings", $useLocalOverrides = null )
01354 {
01355 $isLoaded =& $GLOBALS["eZINIGlobalIsLoaded-$rootDir-$fileName-$useLocalOverrides"];
01356 if ( !isset( $isLoaded ) )
01357 return false;
01358 return $isLoaded;
01359 }
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369 function &instance( $fileName = "site.ini", $rootDir = "settings", $useTextCodec = null, $useCache = null, $useLocalOverrides = null, $directAccess = false, $addArrayDefinition = false )
01370 {
01371 $impl =& $GLOBALS["eZINIGlobalInstance-$rootDir-$fileName-$useLocalOverrides"];
01372 $isLoaded =& $GLOBALS["eZINIGlobalIsLoaded-$rootDir-$fileName-$useLocalOverrides"];
01373
01374 $class = get_class( $impl );
01375 if ( $class != "ezini" )
01376 {
01377 $isLoaded = false;
01378
01379 $impl = new eZINI( $fileName, $rootDir, $useTextCodec, $useCache, $useLocalOverrides, $directAccess, $addArrayDefinition );
01380
01381 $isLoaded = true;
01382 }
01383 return $impl;
01384 }
01385
01386
01387
01388
01389
01390 function &fetchFromFile( $fileName, $useTextCodec = null )
01391 {
01392 $impl = new eZINI( $fileName, false, $useTextCodec, false, false, true );
01393 return $impl;
01394 }
01395
01396
01397
01398
01399
01400 function &create( $fileName = "site.ini", $rootDir = "settings", $useTextCodec = null, $useCache = null, $useLocalOverrides = null )
01401 {
01402 $impl = new eZINI( $fileName, $rootDir, $useTextCodec, $useCache, $useLocalOverrides );
01403 return $impl;
01404 }
01405
01406
01407
01408
01409 function setReadOnlySettingsCheck( $readOnly = true )
01410 {
01411 $this->ReadOnlySettingsCheck = $readOnly;
01412 }
01413
01414
01415
01416
01417 function readOnlySettingsCheck()
01418 {
01419 return $this->ReadOnlySettingsCheck;
01420 }
01421
01422
01423
01424
01425 function resetGlobals( $fileName = "site.ini", $rootDir = "settings", $useLocalOverrides = null )
01426 {
01427 unset( $GLOBALS["eZINIGlobalInstance-$rootDir-$fileName-$useLocalOverrides"] );
01428 unset( $GLOBALS["eZINIGlobalIsLoaded-$rootDir-$fileName-$useLocalOverrides"] );
01429 }
01430
01431
01432
01433 var $Charset;
01434
01435
01436 var $Codec;
01437
01438
01439 var $BlockValues;
01440
01441
01442 var $BlockValuesPlacement;
01443
01444
01445 var $ModifiedBlockValues;
01446
01447
01448 var $FileName;
01449
01450
01451 var $RootDir;
01452
01453
01454 var $UseTextCodec;
01455
01456
01457 var $CacheFile;
01458
01459
01460 var $PlacementCacheFile;
01461
01462
01463 var $UseCache;
01464
01465
01466 var $UseLocalOverrides;
01467
01468
01469 var $LocalOverrideDirArray;
01470
01471
01472 var $DirectAccess;
01473
01474
01475 var $AddArrayDefinition;
01476
01477
01478 var $ReadOnlySettingsCheck = true;
01479
01480 }
01481
01482 ?>