eZ Publish  [trunk]
ezcodepage.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZCodePage class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZCodePage ezcodepage.php
00013   \ingroup eZI18N
00014   \brief Handles codepage files for charset mapping
00015 
00016 */
00017 
00018 class eZCodePage
00019 {
00020     const CACHE_CODE_DATE = 1028204478;
00021 
00022     /*!
00023      Initializes the codepage with the charset code $charset_code, and then loads it.
00024     */
00025     function eZCodePage( $charset_code, $use_cache = true )
00026     {
00027         $this->RequestedCharsetCode = $charset_code;
00028         $this->CharsetCode = eZCharsetInfo::realCharsetCode( $charset_code );
00029         $this->CharsetEncodingScheme = eZCharsetInfo::characterEncodingScheme( $charset_code );
00030         $this->Valid = false;
00031         $this->SubstituteChar = 63; // the ? character
00032         $this->MinCharValue = 0;
00033         $this->MaxCharValue = 0;
00034 
00035         $this->load( $use_cache );
00036     }
00037 
00038     function convertString( $str )
00039     {
00040         $len = strlen( $str );
00041         $chars = '';
00042         $utf8_codec = eZUTF8Codec::instance();
00043         for ( $i = 0; $i < $len; )
00044         {
00045             $charLen = 1;
00046             $char = $this->charToUTF8( $str, $i, $charLen );
00047             if ( $char !== null )
00048                 $chars .= $char;
00049             else
00050                 $chars .= $utf8_codec->toUtf8( $this->SubstituteChar );
00051             $i += $charLen;
00052         }
00053         return $chars;
00054     }
00055 
00056     function convertStringToUnicode( $str )
00057     {
00058         $len = strlen( $str );
00059         $unicodeValues = array();
00060         for ( $i = 0; $i < $len; )
00061         {
00062             $charLen = 1;
00063             $unicodeValue = $this->charToUnicode( $str, $i, $charLen );
00064             if ( $unicodeValue !== null )
00065                 $unicodeValues[] = $unicodeValue;
00066             $i += $charLen;
00067         }
00068         return $unicodeValues;
00069     }
00070 
00071     function convertUnicodeToString( $unicodeValues )
00072     {
00073         if ( !is_array( $unicodeValues ) )
00074             return false;
00075         $text = '';
00076         foreach ( $unicodeValues as $unicodeValue )
00077         {
00078             $char = $this->unicodeToChar( $unicodeValue );
00079             $text .= $char;
00080         }
00081         return $text;
00082     }
00083 
00084     function convertStringFromUTF8( $multi_char )
00085     {
00086         $strlen = strlen( $multi_char );
00087         $text = '';
00088         $codeMap = $this->CodeMap;
00089         $subChar = $this->SubstituteChar;
00090         for ( $offs = 0; $offs < $strlen; )
00091         {
00092 //          The following code has been copied from lib/ezi18n/classes/ezutf8codec.php
00093 //          It has been optimized a bit from the original code due to inlining
00094 
00095             $char_code = false;
00096             if ( ( ord( $multi_char[$offs + 0] ) & 0x80 ) == 0x00 ) // 7 bit, 1 char
00097             {
00098                 $char_code = ord( $multi_char[$offs + 0] );
00099                 $offs += 1;
00100             }
00101             else if ( ( ord( $multi_char[$offs + 0] ) & 0xe0 ) == 0xc0 ) // 11 bit, 2 chars
00102             {
00103                 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 )
00104                 {
00105                     $offs += 2;
00106                     continue;
00107                 }
00108                 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x1f ) << 6) +
00109                                (( ord( $multi_char[$offs + 1] ) & 0x3f )) );
00110                 $offs += 2;
00111                 if ( $char_code < 128 ) // Illegal multibyte, should use less than 2 chars
00112                     continue;
00113             }
00114             else if ( ( ord( $multi_char[$offs + 0] ) & 0xf0 ) == 0xe0 ) // 16 bit, 3 chars
00115             {
00116                 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
00117                      ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 )
00118                 {
00119                     $offs += 3;
00120                     continue;
00121                 }
00122                 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x0f ) << 12) +
00123                                (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 6) +
00124                                (( ord( $multi_char[$offs + 2] ) & 0x3f )) );
00125                 $offs += 3;
00126                 if ( $char_code < 2048 ) // Illegal multibyte, should use less than 3 chars
00127                     continue;
00128             }
00129             else if ( ( ord( $multi_char[$offs + 0] ) & 0xf8 ) == 0xf0 ) // 21 bit, 4 chars
00130             {
00131                 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
00132                      ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
00133                      ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 )
00134                 {
00135                     $offs += 4;
00136                     continue;
00137                 }
00138                 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x07 ) << 18) +
00139                                (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 12) +
00140                                (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 6) +
00141                                (( ord( $multi_char[$offs + 3] ) & 0x3f )) );
00142                 $offs += 4;
00143                 if ( $char_code < 65536 ) // Illegal multibyte, should use less than 4 chars
00144                     continue;
00145             }
00146             else if ( ( ord( $multi_char[$offs + 0] ) & 0xfc ) == 0xf8 ) // 26 bit, 5 chars
00147             {
00148                 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
00149                      ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
00150                      ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
00151                      ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 )
00152                 {
00153                     $offs += 5;
00154                     continue;
00155                 }
00156                 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x03 ) << 24) +
00157                                (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 18) +
00158                                (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 12) +
00159                                (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 6) +
00160                                (( ord( $multi_char[$offs + 4] ) & 0x3f )) );
00161                 $offs += 5;
00162                 if ( $char_code < 2097152 ) // Illegal multibyte, should use less than 5 chars
00163                     continue;
00164             }
00165             else if ( ( ord( $multi_char[$offs + 0] ) & 0xfe ) == 0xfc ) // 31 bit, 6 chars
00166             {
00167                 if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
00168                      ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
00169                      ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
00170                      ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 or
00171                      ( ord( $multi_char[$offs + 5] ) & 0xc0 ) != 0x80 )
00172                 {
00173                     $offs += 6;
00174                     continue;
00175                 }
00176                 $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x01 ) << 30) +
00177                                (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 24) +
00178                                (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 18) +
00179                                (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 12) +
00180                                (( ord( $multi_char[$offs + 4] ) & 0x3f ) << 6) +
00181                                (( ord( $multi_char[$offs + 5] ) & 0x3f )) );
00182                 $offs += 6;
00183                 if ( $char_code < 67108864 ) // Illegal multibyte, should use less than 6 chars
00184                     continue;
00185             }
00186             else // Unknown state, just increase one to make sure we don't get stuck
00187             {
00188                 $offs += 1;
00189                 continue;
00190             }
00191 
00192 //          The following code has been copied from the member function unicodeToChar
00193             if ( isset( $codeMap[$char_code] ) )
00194             {
00195                 $code = $codeMap[$char_code];
00196                 if ( $code <= 0xff )
00197                     $text .= chr( $code );
00198                 else
00199                     $text .= chr( ( $code >> 8 ) & 0xff ) . chr( $code & 0xff );
00200             }
00201             else
00202                 $text .= chr( $subChar );
00203         }
00204         return $text;
00205     }
00206 
00207     function strlen( $str )
00208     {
00209         if ( $this->CharsetEncodingScheme == "doublebyte" )
00210         {
00211             $len = strlen( $str );
00212             $strlen = 0;
00213             for ( $i = 0; $i < $len; )
00214             {
00215                 $charLen = 1;
00216                 $code = ord( $str[$i] );
00217                 if ( isset( $this->ReadExtraMap[$code] ) )
00218                     $charLen = 2;
00219                 ++$strlen;
00220                 $i += $charLen;
00221             }
00222             return $strlen;
00223         }
00224         else
00225             return strlen( $str );
00226     }
00227 
00228     function strlenFromUTF8( $str )
00229     {
00230         return eZUTF8Codec::instance()->strlen( $str );
00231     }
00232 
00233     function charToUtf8( $str, $pos, &$charLen )
00234     {
00235         $code = ord( $str[$pos] );
00236         $charLen = 1;
00237         if ( isset( $this->ReadExtraMap[$code] ) )
00238         {
00239             $code = ( $code << 8 ) | ord( $str[$pos+1] );
00240             $charLen = 2;
00241         }
00242         if ( isset( $this->UTF8Map[$code] ) )
00243             return $this->UTF8Map[$code];
00244         return null;
00245     }
00246 
00247     function charToUnicode( $str, $pos, &$charLen )
00248     {
00249         $code = ord( $str[$pos] );
00250         $charLen = 1;
00251         if ( isset( $this->ReadExtraMap[$code] ) )
00252         {
00253             $code = ( $code << 8 ) | ord( $str[$pos+1] );
00254             $charLen = 2;
00255         }
00256         if ( isset( $this->UnicodeMap[$code] ) )
00257             return $this->UnicodeMap[$code];
00258         return null;
00259     }
00260 
00261     function codeToUtf8( $code )
00262     {
00263         return $this->UTF8Map[$code];
00264     }
00265 
00266     function codeToUnicode( $code )
00267     {
00268         if ( isset( $this->UnicodeMap[$code] ) )
00269         {
00270             return $this->UnicodeMap[$code];
00271         }
00272         return null;
00273     }
00274 
00275     function utf8ToChar( $ucode )
00276     {
00277         if ( isset( $this->UTF8CodeMap[$ucode] ) )
00278         {
00279             $code = $this->UTF8CodeMap[$ucode];
00280             if ( $code <= 0xff )
00281                 return chr( $code );
00282             else
00283                 return chr( ( $code >> 8 ) & 0xff ) . chr( $code & 0xff );
00284         }
00285         else
00286             return chr( $this->SubstituteChar );
00287     }
00288 
00289     function unicodeToChar( $ucode )
00290     {
00291         if ( isset( $this->CodeMap[$ucode] ) )
00292         {
00293             $code = $this->CodeMap[$ucode];
00294             if ( $code <= 0xff )
00295                 return chr( $code );
00296             else
00297                 return chr( ( $code >> 8 ) & 0xff ) . chr( $code & 0xff );
00298         }
00299         else
00300             return chr( $this->SubstituteChar );
00301     }
00302 
00303     function utf8ToCode( $ucode )
00304     {
00305         if ( isset( $this->UTF8CodeMap[$ucode] ) )
00306             return $this->UTF8CodeMap[$ucode];
00307         return null;
00308     }
00309 
00310     function unicodeToCode( $ucode )
00311     {
00312         if ( isset( $this->CodeMap[$ucode] ) )
00313             return $this->CodeMap[$ucode];
00314         return null;
00315     }
00316 
00317     function substituteChar()
00318     {
00319         return $this->SubstituteChar;
00320     }
00321 
00322     function setSubstituteChar( $char )
00323     {
00324         $this->SubstituteChar = $char;
00325     }
00326 
00327     /*!
00328      \static
00329      Returns true if the codepage $charset_code exists.
00330     */
00331     static function exists( $charset_code )
00332     {
00333         $file = eZCodePage::fileName( $charset_code );
00334         return file_exists( $file );
00335     }
00336 
00337     /*!
00338      \static
00339      Returns the filename of the charset code \a $charset_code.
00340     */
00341     static function fileName( $charset_code )
00342     {
00343         $charset_code = eZCharsetInfo::realCharsetCode( $charset_code );
00344         $file = "share/codepages/" . $charset_code;
00345         return $file;
00346     }
00347 
00348     function cacheFileName( $charset_code )
00349     {
00350         $permissionArray = eZCodePage::permissionSetting();
00351 
00352         if ( $permissionArray === false )
00353             return false;
00354         $charset_code = eZCharsetInfo::realCharsetCode( $charset_code );
00355         $cache_dir = $permissionArray['var_directory'] . "/codepages/";
00356         $cache_filename = md5( $charset_code );
00357         $cache = $cache_dir . $cache_filename . ".php";
00358         return $cache;
00359     }
00360 
00361     function fileModification( $charset_code )
00362     {
00363         $file = eZCodePage::fileName( $charset_code );
00364         if ( !file_exists( $file ) )
00365             return false;
00366         return filemtime( $file );
00367     }
00368 
00369     function codepageList()
00370     {
00371         $list = array();
00372         $dir = "share/codepages/";
00373         $dh = opendir( $dir );
00374         while ( ( $file = readdir( $dh ) ) !== false )
00375         {
00376             if ( $file == "." or
00377                  $file == ".." or
00378                  preg_match( "/^\./", $file ) or
00379                  preg_match( "/~$/", $file ) )
00380                 continue;
00381             $list[] = $file;
00382         }
00383         closedir( $dh );
00384         sort( $list );
00385         return $list;
00386     }
00387 
00388 
00389     /*!
00390     Stores the cache object.
00391     */
00392     function storeCacheObject( $filename, $permissionArray )
00393     {
00394         $dir = dirname( $filename );
00395         $file = basename( $filename );
00396         $php = new eZPHPCreator( $dir, $file );
00397 
00398         $php->addVariable( "umap", array() );
00399         $php->addVariable( "utf8map", array() );
00400         $php->addVariable( "cmap", array() );
00401         $php->addVariable( "utf8cmap", array() );
00402 
00403         reset( $this->UnicodeMap );
00404         while ( ( $key = key( $this->UnicodeMap ) ) !== null )
00405         {
00406             $item = $this->UnicodeMap[$key];
00407             $php->addVariable( "umap[$key]", $item );
00408             next( $this->UnicodeMap );
00409         }
00410 
00411         reset( $this->UTF8Map );
00412         while ( ( $key = key( $this->UTF8Map ) ) !== null )
00413         {
00414             $item = $this->UTF8Map[$key];
00415             if ( $item == 0 )
00416             {
00417                 $php->addCodePiece( "\$utf8map[0] = chr(0);\n" );
00418             }
00419             else
00420             {
00421                 $val = str_replace( array( "\\", "'" ),
00422                                     array( "\\\\", "\\'" ),
00423                                     $item );
00424                 $php->addVariable( "utf8map[$key]", $val );
00425             }
00426             next( $this->UTF8Map );
00427         }
00428 
00429         reset( $this->CodeMap );
00430         while ( ( $key = key( $this->CodeMap ) ) !== null )
00431         {
00432             $item = $this->CodeMap[$key];
00433             $php->addVariable( "cmap[$key]", $item );
00434             next( $this->CodeMap );
00435         }
00436 
00437         reset( $this->UTF8CodeMap );
00438         while ( ( $key = key( $this->UTF8CodeMap ) ) !== null )
00439         {
00440             $item = $this->UTF8CodeMap[$key];
00441             if ( $item == 0 )
00442             {
00443                 $php->addVariable( "utf8cmap[chr(0)]", 0 );
00444             }
00445             else
00446             {
00447                 $val = str_replace( array( "\\", "'" ),
00448                                     array( "\\\\", "\\'" ),
00449                                     $key );
00450                 $php->addVariable( "utf8cmap['$val']", $item );
00451             }
00452             next( $this->UTF8CodeMap );
00453         }
00454 
00455         reset( $this->ReadExtraMap );
00456         while ( ( $key = key( $this->ReadExtraMap ) ) !== null )
00457         {
00458             $item = $this->ReadExtraMap[$key];
00459             $php->addVariable( "read_extra[$key]", $item );
00460             next( $this->ReadExtraMap );
00461         }
00462 
00463         $php->addVariable( "eZCodePageCacheCodeDate", self::CACHE_CODE_DATE );
00464         $php->addVariable( "min_char", $this->MinCharValue );
00465         $php->addVariable( "max_char", $this->MaxCharValue );
00466         $php->store( true );
00467 
00468         if ( file_exists( $filename ) )
00469         {
00470             // Store the old umask and set a new one.
00471             $oldPermissionSetting = umask( 0 );
00472 
00473             // Change the permission setting.
00474             @chmod( $filename, $permissionArray['file_permission'] );
00475 
00476             // Restore the old umask.
00477             umask( $oldPermissionSetting );
00478         }
00479     }
00480 
00481 
00482     function cacheFilepath()
00483     {
00484         $permissionArray = eZCodePage::permissionSetting();
00485 
00486         if ( $permissionArray === false )
00487             return false;
00488         $cache_dir = $permissionArray['var_directory'] . "/codepages/";
00489         $cache_filename = md5( $this->CharsetCode );
00490         $cache = $cache_dir . $cache_filename . ".php";
00491 
00492         return $cache;
00493     }
00494 
00495 
00496 
00497     /*!
00498      Loads the codepage from disk.
00499      If $use_cache is true and a cached version is found it is used instead.
00500      If $use_cache is true and no cache was found a new cache is created.
00501     */
00502     function load( $use_cache = true )
00503     {
00504         // temporarely hide the cache display problem
00505         // http://ez.no/community/bugs/char_transform_cache_file_is_not_valid_php
00506         //$use_cache = false;
00507         $file = "share/codepages/" . $this->CharsetCode;
00508 //         eZDebug::writeDebug( "ezcodepage::load was called for $file..." );
00509 
00510         $permissionArray = self::permissionSetting();
00511         if ( $permissionArray !== false )
00512         {
00513             $cache_dir = $permissionArray['var_directory'] . "/codepages/";
00514             $cache_filename = md5( $this->CharsetCode );
00515             $cache = $cache_dir . $cache_filename . ".php";
00516         }
00517         else
00518         {
00519             $cache = false;
00520         }
00521 
00522         if ( !file_exists( $file ) )
00523         {
00524             eZDebug::writeWarning( "Couldn't load codepage file $file", "eZCodePage" );
00525             return;
00526         }
00527         $file_m = filemtime( $file );
00528         $this->Valid = false;
00529         if ( isset( $GLOBALS['eZSiteBasics'] ) )
00530         {
00531             $siteBasics = $GLOBALS['eZSiteBasics'];
00532             if ( isset( $siteBasics['no-cache-adviced'] ) and
00533                  $siteBasics['no-cache-adviced'] )
00534                 $use_cache = false;
00535         }
00536         if ( $cache && file_exists( $cache ) and $use_cache )
00537         {
00538             $cache_m = filemtime( $cache );
00539             if ( $file_m <= $cache_m )
00540             {
00541                 unset( $eZCodePageCacheCodeDate );
00542                 $umap = $utf8map = $cmap = $utf8cmap = $min_char = $max_char = $read_extra = null;
00543                 include( $cache );
00544                 $this->UnicodeMap = $umap;
00545                 $this->UTF8Map = $utf8map;
00546                 $this->CodeMap = $cmap;
00547                 $this->UTF8CodeMap = $utf8cmap;
00548                 $this->MinCharValue = $min_char;
00549                 $this->MaxCharValue = $max_char;
00550                 $this->ReadExtraMap = $read_extra;
00551 
00552                 if ( isset( $eZCodePageCacheCodeDate ) and
00553                      $eZCodePageCacheCodeDate == self::CACHE_CODE_DATE )
00554                 {
00555                     $this->Valid = true;
00556                     return;
00557                 }
00558             }
00559         }
00560 
00561         $utf8_codec = eZUTF8Codec::instance();
00562 
00563         $this->UnicodeMap = array();
00564         $this->UTF8Map = array();
00565         $this->CodeMap = array();
00566         $this->UTF8CodeMap = array();
00567         $this->ReadExtraMap = array();
00568         for ( $i = 0; $i < 32; ++$i )
00569         {
00570             $code = $i;
00571             $ucode = $i;
00572             $utf8_code = $utf8_codec->toUtf8( $ucode );
00573             $this->UnicodeMap[$code] = $ucode;
00574             $this->UTF8Map[$code] = $utf8_code;
00575             $this->CodeMap[$ucode] = $code;
00576             $this->UTF8CodeMap[$utf8_code] = $code;
00577         }
00578         $this->MinCharValue = 0;
00579         $this->MaxCharValue = 31;
00580 
00581         $lines = file( $file );
00582         reset( $lines );
00583         while ( ( $key = key( $lines ) ) !== null )
00584         {
00585             if ( preg_match( "/^#/", $lines[$key] ) )
00586             {
00587                 next( $lines );
00588                 continue;
00589             }
00590             $line = trim( $lines[$key] );
00591             $items = explode( "\t", $line );
00592             if ( count( $items ) == 3 )
00593             {
00594                 $code = false;
00595                 $ucode = false;
00596                 $desc = $items[2];
00597                 if ( preg_match( "/(=|0x)([0-9a-fA-F]{4})/", $items[0], $args ) )
00598                 {
00599                     $code = hexdec( $args[2] );
00600 //                    eZDebug::writeNotice( $args, "doublebyte" );
00601                 }
00602                 else if ( preg_match( "/(=|0x)([0-9a-fA-F]{2})/", $items[0], $args ) )
00603                 {
00604                     $code = hexdec( $args[2] );
00605 //                    eZDebug::writeNotice( $args, "singlebyte" );
00606                 }
00607                 if ( preg_match( "/(U\+|0x)([0-9a-fA-F]{4})/", $items[1], $args ) )
00608                 {
00609                     $ucode = hexdec( $args[2] );
00610                 }
00611 
00612                 if ( $code !== false and
00613                      $ucode !== false )
00614                 {
00615                     $utf8_code = $utf8_codec->toUtf8( $ucode );
00616                     $this->UnicodeMap[$code] = $ucode;
00617                     $this->UTF8Map[$code] = $utf8_code;
00618                     $this->CodeMap[$ucode] = $code;
00619                     $this->UTF8CodeMap[$utf8_code] = $code;
00620                     $this->MinCharValue = min( $this->MinCharValue, $code );
00621                     $this->MaxCharValue = max( $this->MaxCharValue, $code );
00622                 }
00623                 else if ( $code !== false )
00624                 {
00625                     $this->ReadExtraMap[$code] = true;
00626                 }
00627             }
00628             next( $lines );
00629         }
00630 
00631         $this->Valid = true;
00632         $this->MinCharValue = min( $this->MinCharValue, $code );
00633         $this->MaxCharValue = max( $this->MaxCharValue, $code );
00634 
00635         if ( $use_cache )
00636         {
00637             // If there is no setting; do nothing:
00638             if ( $permissionArray === false )
00639             {
00640                 if ( !isset ( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] ) )
00641                 {
00642                     $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] = array();
00643                 }
00644 
00645                 // The array already exists; we simply append to it.
00646                 $GLOBALS['EZCODEPAGECACHEOBJECTLIST'][] = $this;
00647             }
00648             // Else: a permission setting exists:
00649             else
00650             {
00651                 // Store the cache object with the correct permission setting.
00652                 $this->storeCacheObject( $cache, $permissionArray );
00653 
00654                 // Check if the global array for codepage cache objects exist:
00655             }
00656         }
00657     }
00658 
00659     /*!
00660      \return the charset code which is in use. This may not be the charset that was
00661      requested due to aliases.
00662      \sa requestedCharsetCode
00663     */
00664     function charsetCode()
00665     {
00666         return $this->CharsetCode;
00667     }
00668 
00669     /*!
00670      \return the charset code which was requested, may differ from charsetCode()
00671     */
00672     function requestedCharsetCode()
00673     {
00674         return $this->RequestedCharsetCode;
00675     }
00676 
00677     /*!
00678      \return the lowest character value used in the mapping table.
00679     */
00680     function minCharValue()
00681     {
00682         return $this->MinCharValue;
00683     }
00684 
00685     /*!
00686      \return the largest character value used in the mapping table.
00687     */
00688     function maxCharValue()
00689     {
00690         return $this->MaxCharValue;
00691     }
00692 
00693     /*!
00694      Returns true if the codepage is valid for use.
00695     */
00696     function isValid()
00697     {
00698         return $this->Valid;
00699     }
00700 
00701     /**
00702      * Returns a shared instance of the eZCodePage pr the
00703      * $charset_code param.
00704      *
00705      * @param string $charset_code
00706      * @param bool $use_cache
00707      * @return eZCodePage
00708      */
00709     static function instance( $charset_code, $use_cache = true )
00710     {
00711         if ( empty( $GLOBALS["eZCodePage-$charset_code"] ) )
00712         {
00713             $GLOBALS["eZCodePage-$charset_code"] = new eZCodePage( $charset_code, $use_cache );
00714         }
00715         return $GLOBALS["eZCodePage-$charset_code"];
00716     }
00717 
00718     /*!
00719      \private
00720 
00721      Gets the permission setting for codepage files & returns it.
00722      If the permission setting doesnt exists: returns false.
00723     */
00724     static function permissionSetting()
00725     {
00726 //         eZDebug::writeDebug( "permissionSetting was called..." );
00727 
00728         if ( isset( $GLOBALS['EZCODEPAGEPERMISSIONS'] ) )
00729         {
00730             return $GLOBALS['EZCODEPAGEPERMISSIONS'];
00731         }
00732         else
00733         {
00734             return false;
00735         }
00736     }
00737 
00738     /*!
00739      \private
00740 
00741      Sets the permission setting for codepagefiles.
00742     */
00743     static function setPermissionSetting( $permissionArray )
00744     {
00745 //         eZDebug::writeDebug( "setPermissionSetting was called..." );
00746 
00747         $GLOBALS['EZCODEPAGEPERMISSIONS'] = $permissionArray;
00748 
00749         if ( $permissionArray !== false )
00750         {
00751             eZCodePage::flushCacheObject();
00752         }
00753     }
00754 
00755     /*!
00756      \private
00757     */
00758     static function flushCacheObject()
00759     {
00760 //         eZDebug::writeDebug("flushCacheObject is called... ","");
00761         if ( !isset( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] ) )
00762         {
00763             return false;
00764         }
00765 
00766         // Grab the permission setting for codepage cache files.
00767         $permissionArray = self::permissionSetting();
00768 
00769         // If we were unable to extract the permission setting:
00770         if ( $permissionArray === false )
00771         {
00772 //             eZDebug::writeDebug( "permissionSetting: unable to grab permission setting from global array..." );
00773 
00774             // Bail with false.
00775             return false;
00776         }
00777         // Else: permission setting is available:
00778         else
00779         {
00780 //             eZDebug::writeDebug( "permissionSetting: grabbed permission setting from global array..." );
00781 
00782             // For all the cache objects:
00783             foreach( array_keys( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] ) as $codePageKey )
00784             {
00785                 $codePage = $GLOBALS['EZCODEPAGECACHEOBJECTLIST'][$codePageKey];
00786 
00787                 $filename = $codePage->cacheFilepath();
00788 
00789                 // Store __FIX_ME__
00790                 $codePage->storeCacheObject( $filename, $permissionArray );
00791             }
00792 
00793             //
00794             unset( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] );
00795         }
00796     }
00797 
00798     /// \privatesection
00799     /// The charset code which was requested, may differ from $CharsetCode
00800     public $RequestedCharsetCode;
00801     /// The read charset code, may differ from $RequestedCharsetCode
00802     public $CharsetCode;
00803     /// Encoding scheme for current charset, for instance utf-8, singlebyte, multibyte
00804     public $CharsetEncodingScheme;
00805     /// Maps normal codes to unicode
00806     public $UnicodeMap;
00807     /// Maps normal codes to utf8
00808     public $UTF8Map;
00809     /// Maps unicode to normal codes
00810     public $CodeMap;
00811     /// Maps utf8 to normal codes
00812     public $UTF8CodeMap;
00813     /// The minimum key value for the mapping tables
00814     public $MinCharValue;
00815     /// The maximum key value for the mapping tables
00816     public $MaxCharValue;
00817     /// Whether the codepage is valid or not
00818     public $Valid;
00819     /// The character to use when an alternative doesn't exist
00820     public $SubstituteChar;
00821 }
00822 
00823 // Checks if index.php or any other script has set any codepage permissions
00824 if ( isset( $GLOBALS['EZCODEPAGEPERMISSIONS'] ) and
00825      $GLOBALS['EZCODEPAGEPERMISSIONS'] !== false )
00826 {
00827     eZCodePage::flushCacheObject();
00828 }
00829 
00830 ?>