/lib/ezi18n/classes/ezcodepage.php

https://github.com/GunioRobot/ezpublish · PHP · 830 lines · 735 code · 51 blank · 44 comment · 86 complexity · 09c4c1e67a11bac4637b315ef5a76040 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZCodePage class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZCodePage ezcodepage.php
  12. \ingroup eZI18N
  13. \brief Handles codepage files for charset mapping
  14. */
  15. class eZCodePage
  16. {
  17. const CACHE_CODE_DATE = 1028204478;
  18. /*!
  19. Initializes the codepage with the charset code $charset_code, and then loads it.
  20. */
  21. function eZCodePage( $charset_code, $use_cache = true )
  22. {
  23. $this->RequestedCharsetCode = $charset_code;
  24. $this->CharsetCode = eZCharsetInfo::realCharsetCode( $charset_code );
  25. $this->CharsetEncodingScheme = eZCharsetInfo::characterEncodingScheme( $charset_code );
  26. $this->Valid = false;
  27. $this->SubstituteChar = 63; // the ? character
  28. $this->MinCharValue = 0;
  29. $this->MaxCharValue = 0;
  30. $this->load( $use_cache );
  31. }
  32. function convertString( $str )
  33. {
  34. $len = strlen( $str );
  35. $chars = '';
  36. $utf8_codec = eZUTF8Codec::instance();
  37. for ( $i = 0; $i < $len; )
  38. {
  39. $charLen = 1;
  40. $char = $this->charToUTF8( $str, $i, $charLen );
  41. if ( $char !== null )
  42. $chars .= $char;
  43. else
  44. $chars .= $utf8_codec->toUtf8( $this->SubstituteChar );
  45. $i += $charLen;
  46. }
  47. return $chars;
  48. }
  49. function convertStringToUnicode( $str )
  50. {
  51. $len = strlen( $str );
  52. $unicodeValues = array();
  53. for ( $i = 0; $i < $len; )
  54. {
  55. $charLen = 1;
  56. $unicodeValue = $this->charToUnicode( $str, $i, $charLen );
  57. if ( $unicodeValue !== null )
  58. $unicodeValues[] = $unicodeValue;
  59. $i += $charLen;
  60. }
  61. return $unicodeValues;
  62. }
  63. function convertUnicodeToString( $unicodeValues )
  64. {
  65. if ( !is_array( $unicodeValues ) )
  66. return false;
  67. $text = '';
  68. foreach ( $unicodeValues as $unicodeValue )
  69. {
  70. $char = $this->unicodeToChar( $unicodeValue );
  71. $text .= $char;
  72. }
  73. return $text;
  74. }
  75. function convertStringFromUTF8( $multi_char )
  76. {
  77. $strlen = strlen( $multi_char );
  78. $text = '';
  79. $codeMap = $this->CodeMap;
  80. $subChar = $this->SubstituteChar;
  81. for ( $offs = 0; $offs < $strlen; )
  82. {
  83. // The following code has been copied from lib/ezi18n/classes/ezutf8codec.php
  84. // It has been optimized a bit from the original code due to inlining
  85. $char_code = false;
  86. if ( ( ord( $multi_char[$offs + 0] ) & 0x80 ) == 0x00 ) // 7 bit, 1 char
  87. {
  88. $char_code = ord( $multi_char[$offs + 0] );
  89. $offs += 1;
  90. }
  91. else if ( ( ord( $multi_char[$offs + 0] ) & 0xe0 ) == 0xc0 ) // 11 bit, 2 chars
  92. {
  93. if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 )
  94. {
  95. $offs += 2;
  96. continue;
  97. }
  98. $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x1f ) << 6) +
  99. (( ord( $multi_char[$offs + 1] ) & 0x3f )) );
  100. $offs += 2;
  101. if ( $char_code < 128 ) // Illegal multibyte, should use less than 2 chars
  102. continue;
  103. }
  104. else if ( ( ord( $multi_char[$offs + 0] ) & 0xf0 ) == 0xe0 ) // 16 bit, 3 chars
  105. {
  106. if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
  107. ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 )
  108. {
  109. $offs += 3;
  110. continue;
  111. }
  112. $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x0f ) << 12) +
  113. (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 6) +
  114. (( ord( $multi_char[$offs + 2] ) & 0x3f )) );
  115. $offs += 3;
  116. if ( $char_code < 2048 ) // Illegal multibyte, should use less than 3 chars
  117. continue;
  118. }
  119. else if ( ( ord( $multi_char[$offs + 0] ) & 0xf8 ) == 0xf0 ) // 21 bit, 4 chars
  120. {
  121. if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
  122. ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
  123. ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 )
  124. {
  125. $offs += 4;
  126. continue;
  127. }
  128. $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x07 ) << 18) +
  129. (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 12) +
  130. (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 6) +
  131. (( ord( $multi_char[$offs + 3] ) & 0x3f )) );
  132. $offs += 4;
  133. if ( $char_code < 65536 ) // Illegal multibyte, should use less than 4 chars
  134. continue;
  135. }
  136. else if ( ( ord( $multi_char[$offs + 0] ) & 0xfc ) == 0xf8 ) // 26 bit, 5 chars
  137. {
  138. if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
  139. ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
  140. ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
  141. ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 )
  142. {
  143. $offs += 5;
  144. continue;
  145. }
  146. $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x03 ) << 24) +
  147. (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 18) +
  148. (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 12) +
  149. (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 6) +
  150. (( ord( $multi_char[$offs + 4] ) & 0x3f )) );
  151. $offs += 5;
  152. if ( $char_code < 2097152 ) // Illegal multibyte, should use less than 5 chars
  153. continue;
  154. }
  155. else if ( ( ord( $multi_char[$offs + 0] ) & 0xfe ) == 0xfc ) // 31 bit, 6 chars
  156. {
  157. if ( ( ord( $multi_char[$offs + 1] ) & 0xc0 ) != 0x80 or
  158. ( ord( $multi_char[$offs + 2] ) & 0xc0 ) != 0x80 or
  159. ( ord( $multi_char[$offs + 3] ) & 0xc0 ) != 0x80 or
  160. ( ord( $multi_char[$offs + 4] ) & 0xc0 ) != 0x80 or
  161. ( ord( $multi_char[$offs + 5] ) & 0xc0 ) != 0x80 )
  162. {
  163. $offs += 6;
  164. continue;
  165. }
  166. $char_code = ( (( ord( $multi_char[$offs + 0] ) & 0x01 ) << 30) +
  167. (( ord( $multi_char[$offs + 1] ) & 0x3f ) << 24) +
  168. (( ord( $multi_char[$offs + 2] ) & 0x3f ) << 18) +
  169. (( ord( $multi_char[$offs + 3] ) & 0x3f ) << 12) +
  170. (( ord( $multi_char[$offs + 4] ) & 0x3f ) << 6) +
  171. (( ord( $multi_char[$offs + 5] ) & 0x3f )) );
  172. $offs += 6;
  173. if ( $char_code < 67108864 ) // Illegal multibyte, should use less than 6 chars
  174. continue;
  175. }
  176. else // Unknown state, just increase one to make sure we don't get stuck
  177. {
  178. $offs += 1;
  179. continue;
  180. }
  181. // The following code has been copied from the member function unicodeToChar
  182. if ( isset( $codeMap[$char_code] ) )
  183. {
  184. $code = $codeMap[$char_code];
  185. if ( $code <= 0xff )
  186. $text .= chr( $code );
  187. else
  188. $text .= chr( ( $code >> 8 ) & 0xff ) . chr( $code & 0xff );
  189. }
  190. else
  191. $text .= chr( $subChar );
  192. }
  193. return $text;
  194. }
  195. function strlen( $str )
  196. {
  197. if ( $this->CharsetEncodingScheme == "doublebyte" )
  198. {
  199. $len = strlen( $str );
  200. $strlen = 0;
  201. for ( $i = 0; $i < $len; )
  202. {
  203. $charLen = 1;
  204. $code = ord( $str[$i] );
  205. if ( isset( $this->ReadExtraMap[$code] ) )
  206. $charLen = 2;
  207. ++$strlen;
  208. $i += $charLen;
  209. }
  210. return $strlen;
  211. }
  212. else
  213. return strlen( $str );
  214. }
  215. function strlenFromUTF8( $str )
  216. {
  217. return eZUTF8Codec::instance()->strlen( $str );
  218. }
  219. function charToUtf8( $str, $pos, &$charLen )
  220. {
  221. $code = ord( $str[$pos] );
  222. $charLen = 1;
  223. if ( isset( $this->ReadExtraMap[$code] ) )
  224. {
  225. $code = ( $code << 8 ) | ord( $str[$pos+1] );
  226. $charLen = 2;
  227. }
  228. if ( isset( $this->UTF8Map[$code] ) )
  229. return $this->UTF8Map[$code];
  230. return null;
  231. }
  232. function charToUnicode( $str, $pos, &$charLen )
  233. {
  234. $code = ord( $str[$pos] );
  235. $charLen = 1;
  236. if ( isset( $this->ReadExtraMap[$code] ) )
  237. {
  238. $code = ( $code << 8 ) | ord( $str[$pos+1] );
  239. $charLen = 2;
  240. }
  241. if ( isset( $this->UnicodeMap[$code] ) )
  242. return $this->UnicodeMap[$code];
  243. return null;
  244. }
  245. function codeToUtf8( $code )
  246. {
  247. return $this->UTF8Map[$code];
  248. }
  249. function codeToUnicode( $code )
  250. {
  251. if ( isset( $this->UnicodeMap[$code] ) )
  252. {
  253. return $this->UnicodeMap[$code];
  254. }
  255. return null;
  256. }
  257. function utf8ToChar( $ucode )
  258. {
  259. if ( isset( $this->UTF8CodeMap[$ucode] ) )
  260. {
  261. $code = $this->UTF8CodeMap[$ucode];
  262. if ( $code <= 0xff )
  263. return chr( $code );
  264. else
  265. return chr( ( $code >> 8 ) & 0xff ) . chr( $code & 0xff );
  266. }
  267. else
  268. return chr( $this->SubstituteChar );
  269. }
  270. function unicodeToChar( $ucode )
  271. {
  272. if ( isset( $this->CodeMap[$ucode] ) )
  273. {
  274. $code = $this->CodeMap[$ucode];
  275. if ( $code <= 0xff )
  276. return chr( $code );
  277. else
  278. return chr( ( $code >> 8 ) & 0xff ) . chr( $code & 0xff );
  279. }
  280. else
  281. return chr( $this->SubstituteChar );
  282. }
  283. function utf8ToCode( $ucode )
  284. {
  285. if ( isset( $this->UTF8CodeMap[$ucode] ) )
  286. return $this->UTF8CodeMap[$ucode];
  287. return null;
  288. }
  289. function unicodeToCode( $ucode )
  290. {
  291. if ( isset( $this->CodeMap[$ucode] ) )
  292. return $this->CodeMap[$ucode];
  293. return null;
  294. }
  295. function substituteChar()
  296. {
  297. return $this->SubstituteChar;
  298. }
  299. function setSubstituteChar( $char )
  300. {
  301. $this->SubstituteChar = $char;
  302. }
  303. /*!
  304. \static
  305. Returns true if the codepage $charset_code exists.
  306. */
  307. static function exists( $charset_code )
  308. {
  309. $file = eZCodePage::fileName( $charset_code );
  310. return file_exists( $file );
  311. }
  312. /*!
  313. \static
  314. Returns the filename of the charset code \a $charset_code.
  315. */
  316. static function fileName( $charset_code )
  317. {
  318. $charset_code = eZCharsetInfo::realCharsetCode( $charset_code );
  319. $file = "share/codepages/" . $charset_code;
  320. return $file;
  321. }
  322. function cacheFileName( $charset_code )
  323. {
  324. $permissionArray = eZCodePage::permissionSetting();
  325. if ( $permissionArray === false )
  326. return false;
  327. $charset_code = eZCharsetInfo::realCharsetCode( $charset_code );
  328. $cache_dir = $permissionArray['var_directory'] . "/codepages/";
  329. $cache_filename = md5( $charset_code );
  330. $cache = $cache_dir . $cache_filename . ".php";
  331. return $cache;
  332. }
  333. function fileModification( $charset_code )
  334. {
  335. $file = eZCodePage::fileName( $charset_code );
  336. if ( !file_exists( $file ) )
  337. return false;
  338. return filemtime( $file );
  339. }
  340. function codepageList()
  341. {
  342. $list = array();
  343. $dir = "share/codepages/";
  344. $dh = opendir( $dir );
  345. while ( ( $file = readdir( $dh ) ) !== false )
  346. {
  347. if ( $file == "." or
  348. $file == ".." or
  349. preg_match( "/^\./", $file ) or
  350. preg_match( "/~$/", $file ) )
  351. continue;
  352. $list[] = $file;
  353. }
  354. closedir( $dh );
  355. sort( $list );
  356. return $list;
  357. }
  358. /*!
  359. Stores the cache object.
  360. */
  361. function storeCacheObject( $filename, $permissionArray )
  362. {
  363. $dir = dirname( $filename );
  364. $file = basename( $filename );
  365. $php = new eZPHPCreator( $dir, $file );
  366. $php->addVariable( "umap", array() );
  367. $php->addVariable( "utf8map", array() );
  368. $php->addVariable( "cmap", array() );
  369. $php->addVariable( "utf8cmap", array() );
  370. reset( $this->UnicodeMap );
  371. while ( ( $key = key( $this->UnicodeMap ) ) !== null )
  372. {
  373. $item = $this->UnicodeMap[$key];
  374. $php->addVariable( "umap[$key]", $item );
  375. next( $this->UnicodeMap );
  376. }
  377. reset( $this->UTF8Map );
  378. while ( ( $key = key( $this->UTF8Map ) ) !== null )
  379. {
  380. $item = $this->UTF8Map[$key];
  381. if ( $item == 0 )
  382. {
  383. $php->addCodePiece( "\$utf8map[0] = chr(0);\n" );
  384. }
  385. else
  386. {
  387. $val = str_replace( array( "\\", "'" ),
  388. array( "\\\\", "\\'" ),
  389. $item );
  390. $php->addVariable( "utf8map[$key]", $val );
  391. }
  392. next( $this->UTF8Map );
  393. }
  394. reset( $this->CodeMap );
  395. while ( ( $key = key( $this->CodeMap ) ) !== null )
  396. {
  397. $item = $this->CodeMap[$key];
  398. $php->addVariable( "cmap[$key]", $item );
  399. next( $this->CodeMap );
  400. }
  401. reset( $this->UTF8CodeMap );
  402. while ( ( $key = key( $this->UTF8CodeMap ) ) !== null )
  403. {
  404. $item = $this->UTF8CodeMap[$key];
  405. if ( $item == 0 )
  406. {
  407. $php->addVariable( "utf8cmap[chr(0)]", 0 );
  408. }
  409. else
  410. {
  411. $val = str_replace( array( "\\", "'" ),
  412. array( "\\\\", "\\'" ),
  413. $key );
  414. $php->addVariable( "utf8cmap['$val']", $item );
  415. }
  416. next( $this->UTF8CodeMap );
  417. }
  418. reset( $this->ReadExtraMap );
  419. while ( ( $key = key( $this->ReadExtraMap ) ) !== null )
  420. {
  421. $item = $this->ReadExtraMap[$key];
  422. $php->addVariable( "read_extra[$key]", $item );
  423. next( $this->ReadExtraMap );
  424. }
  425. $php->addVariable( "eZCodePageCacheCodeDate", self::CACHE_CODE_DATE );
  426. $php->addVariable( "min_char", $this->MinCharValue );
  427. $php->addVariable( "max_char", $this->MaxCharValue );
  428. $php->store( true );
  429. if ( file_exists( $filename ) )
  430. {
  431. // Store the old umask and set a new one.
  432. $oldPermissionSetting = umask( 0 );
  433. // Change the permission setting.
  434. @chmod( $filename, $permissionArray['file_permission'] );
  435. // Restore the old umask.
  436. umask( $oldPermissionSetting );
  437. }
  438. }
  439. function cacheFilepath()
  440. {
  441. $permissionArray = eZCodePage::permissionSetting();
  442. if ( $permissionArray === false )
  443. return false;
  444. $cache_dir = $permissionArray['var_directory'] . "/codepages/";
  445. $cache_filename = md5( $this->CharsetCode );
  446. $cache = $cache_dir . $cache_filename . ".php";
  447. return $cache;
  448. }
  449. /*!
  450. Loads the codepage from disk.
  451. If $use_cache is true and a cached version is found it is used instead.
  452. If $use_cache is true and no cache was found a new cache is created.
  453. */
  454. function load( $use_cache = true )
  455. {
  456. // temporarely hide the cache display problem
  457. // http://ez.no/community/bugs/char_transform_cache_file_is_not_valid_php
  458. //$use_cache = false;
  459. $file = "share/codepages/" . $this->CharsetCode;
  460. // eZDebug::writeDebug( "ezcodepage::load was called for $file..." );
  461. $permissionArray = self::permissionSetting();
  462. if ( $permissionArray !== false )
  463. {
  464. $cache_dir = $permissionArray['var_directory'] . "/codepages/";
  465. $cache_filename = md5( $this->CharsetCode );
  466. $cache = $cache_dir . $cache_filename . ".php";
  467. }
  468. else
  469. {
  470. $cache = false;
  471. }
  472. if ( !file_exists( $file ) )
  473. {
  474. eZDebug::writeWarning( "Couldn't load codepage file $file", "eZCodePage" );
  475. return;
  476. }
  477. $file_m = filemtime( $file );
  478. $this->Valid = false;
  479. if ( isset( $GLOBALS['eZSiteBasics'] ) )
  480. {
  481. $siteBasics = $GLOBALS['eZSiteBasics'];
  482. if ( isset( $siteBasics['no-cache-adviced'] ) and
  483. $siteBasics['no-cache-adviced'] )
  484. $use_cache = false;
  485. }
  486. if ( $cache && file_exists( $cache ) and $use_cache )
  487. {
  488. $cache_m = filemtime( $cache );
  489. if ( $file_m <= $cache_m )
  490. {
  491. unset( $eZCodePageCacheCodeDate );
  492. $umap = $utf8map = $cmap = $utf8cmap = $min_char = $max_char = $read_extra = null;
  493. include( $cache );
  494. $this->UnicodeMap = $umap;
  495. $this->UTF8Map = $utf8map;
  496. $this->CodeMap = $cmap;
  497. $this->UTF8CodeMap = $utf8cmap;
  498. $this->MinCharValue = $min_char;
  499. $this->MaxCharValue = $max_char;
  500. $this->ReadExtraMap = $read_extra;
  501. if ( isset( $eZCodePageCacheCodeDate ) and
  502. $eZCodePageCacheCodeDate == self::CACHE_CODE_DATE )
  503. {
  504. $this->Valid = true;
  505. return;
  506. }
  507. }
  508. }
  509. $utf8_codec = eZUTF8Codec::instance();
  510. $this->UnicodeMap = array();
  511. $this->UTF8Map = array();
  512. $this->CodeMap = array();
  513. $this->UTF8CodeMap = array();
  514. $this->ReadExtraMap = array();
  515. for ( $i = 0; $i < 32; ++$i )
  516. {
  517. $code = $i;
  518. $ucode = $i;
  519. $utf8_code = $utf8_codec->toUtf8( $ucode );
  520. $this->UnicodeMap[$code] = $ucode;
  521. $this->UTF8Map[$code] = $utf8_code;
  522. $this->CodeMap[$ucode] = $code;
  523. $this->UTF8CodeMap[$utf8_code] = $code;
  524. }
  525. $this->MinCharValue = 0;
  526. $this->MaxCharValue = 31;
  527. $lines = file( $file );
  528. reset( $lines );
  529. while ( ( $key = key( $lines ) ) !== null )
  530. {
  531. if ( preg_match( "/^#/", $lines[$key] ) )
  532. {
  533. next( $lines );
  534. continue;
  535. }
  536. $line = trim( $lines[$key] );
  537. $items = explode( "\t", $line );
  538. if ( count( $items ) == 3 )
  539. {
  540. $code = false;
  541. $ucode = false;
  542. $desc = $items[2];
  543. if ( preg_match( "/(=|0x)([0-9a-fA-F]{4})/", $items[0], $args ) )
  544. {
  545. $code = hexdec( $args[2] );
  546. // eZDebug::writeNotice( $args, "doublebyte" );
  547. }
  548. else if ( preg_match( "/(=|0x)([0-9a-fA-F]{2})/", $items[0], $args ) )
  549. {
  550. $code = hexdec( $args[2] );
  551. // eZDebug::writeNotice( $args, "singlebyte" );
  552. }
  553. if ( preg_match( "/(U\+|0x)([0-9a-fA-F]{4})/", $items[1], $args ) )
  554. {
  555. $ucode = hexdec( $args[2] );
  556. }
  557. if ( $code !== false and
  558. $ucode !== false )
  559. {
  560. $utf8_code = $utf8_codec->toUtf8( $ucode );
  561. $this->UnicodeMap[$code] = $ucode;
  562. $this->UTF8Map[$code] = $utf8_code;
  563. $this->CodeMap[$ucode] = $code;
  564. $this->UTF8CodeMap[$utf8_code] = $code;
  565. $this->MinCharValue = min( $this->MinCharValue, $code );
  566. $this->MaxCharValue = max( $this->MaxCharValue, $code );
  567. }
  568. else if ( $code !== false )
  569. {
  570. $this->ReadExtraMap[$code] = true;
  571. }
  572. }
  573. next( $lines );
  574. }
  575. $this->Valid = true;
  576. $this->MinCharValue = min( $this->MinCharValue, $code );
  577. $this->MaxCharValue = max( $this->MaxCharValue, $code );
  578. if ( $use_cache )
  579. {
  580. // If there is no setting; do nothing:
  581. if ( $permissionArray === false )
  582. {
  583. if ( !isset ( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] ) )
  584. {
  585. $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] = array();
  586. }
  587. // The array already exists; we simply append to it.
  588. $GLOBALS['EZCODEPAGECACHEOBJECTLIST'][] = $this;
  589. }
  590. // Else: a permission setting exists:
  591. else
  592. {
  593. // Store the cache object with the correct permission setting.
  594. $this->storeCacheObject( $cache, $permissionArray );
  595. // Check if the global array for codepage cache objects exist:
  596. }
  597. }
  598. }
  599. /*!
  600. \return the charset code which is in use. This may not be the charset that was
  601. requested due to aliases.
  602. \sa requestedCharsetCode
  603. */
  604. function charsetCode()
  605. {
  606. return $this->CharsetCode;
  607. }
  608. /*!
  609. \return the charset code which was requested, may differ from charsetCode()
  610. */
  611. function requestedCharsetCode()
  612. {
  613. return $this->RequestedCharsetCode;
  614. }
  615. /*!
  616. \return the lowest character value used in the mapping table.
  617. */
  618. function minCharValue()
  619. {
  620. return $this->MinCharValue;
  621. }
  622. /*!
  623. \return the largest character value used in the mapping table.
  624. */
  625. function maxCharValue()
  626. {
  627. return $this->MaxCharValue;
  628. }
  629. /*!
  630. Returns true if the codepage is valid for use.
  631. */
  632. function isValid()
  633. {
  634. return $this->Valid;
  635. }
  636. /**
  637. * Returns a shared instance of the eZCodePage pr the
  638. * $charset_code param.
  639. *
  640. * @param string $charset_code
  641. * @param bool $use_cache
  642. * @return eZCodePage
  643. */
  644. static function instance( $charset_code, $use_cache = true )
  645. {
  646. if ( empty( $GLOBALS["eZCodePage-$charset_code"] ) )
  647. {
  648. $GLOBALS["eZCodePage-$charset_code"] = new eZCodePage( $charset_code, $use_cache );
  649. }
  650. return $GLOBALS["eZCodePage-$charset_code"];
  651. }
  652. /*!
  653. \private
  654. Gets the permission setting for codepage files & returns it.
  655. If the permission setting doesnt exists: returns false.
  656. */
  657. static function permissionSetting()
  658. {
  659. // eZDebug::writeDebug( "permissionSetting was called..." );
  660. if ( isset( $GLOBALS['EZCODEPAGEPERMISSIONS'] ) )
  661. {
  662. return $GLOBALS['EZCODEPAGEPERMISSIONS'];
  663. }
  664. else
  665. {
  666. return false;
  667. }
  668. }
  669. /*!
  670. \private
  671. Sets the permission setting for codepagefiles.
  672. */
  673. static function setPermissionSetting( $permissionArray )
  674. {
  675. // eZDebug::writeDebug( "setPermissionSetting was called..." );
  676. $GLOBALS['EZCODEPAGEPERMISSIONS'] = $permissionArray;
  677. if ( $permissionArray !== false )
  678. {
  679. eZCodePage::flushCacheObject();
  680. }
  681. }
  682. /*!
  683. \private
  684. */
  685. static function flushCacheObject()
  686. {
  687. // eZDebug::writeDebug("flushCacheObject is called... ","");
  688. if ( !isset( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] ) )
  689. {
  690. return false;
  691. }
  692. // Grab the permission setting for codepage cache files.
  693. $permissionArray = self::permissionSetting();
  694. // If we were unable to extract the permission setting:
  695. if ( $permissionArray === false )
  696. {
  697. // eZDebug::writeDebug( "permissionSetting: unable to grab permission setting from global array..." );
  698. // Bail with false.
  699. return false;
  700. }
  701. // Else: permission setting is available:
  702. else
  703. {
  704. // eZDebug::writeDebug( "permissionSetting: grabbed permission setting from global array..." );
  705. // For all the cache objects:
  706. foreach( array_keys( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] ) as $codePageKey )
  707. {
  708. $codePage = $GLOBALS['EZCODEPAGECACHEOBJECTLIST'][$codePageKey];
  709. $filename = $codePage->cacheFilepath();
  710. // Store __FIX_ME__
  711. $codePage->storeCacheObject( $filename, $permissionArray );
  712. }
  713. //
  714. unset( $GLOBALS['EZCODEPAGECACHEOBJECTLIST'] );
  715. }
  716. }
  717. /// \privatesection
  718. /// The charset code which was requested, may differ from $CharsetCode
  719. public $RequestedCharsetCode;
  720. /// The read charset code, may differ from $RequestedCharsetCode
  721. public $CharsetCode;
  722. /// Encoding scheme for current charset, for instance utf-8, singlebyte, multibyte
  723. public $CharsetEncodingScheme;
  724. /// Maps normal codes to unicode
  725. public $UnicodeMap;
  726. /// Maps normal codes to utf8
  727. public $UTF8Map;
  728. /// Maps unicode to normal codes
  729. public $CodeMap;
  730. /// Maps utf8 to normal codes
  731. public $UTF8CodeMap;
  732. /// The minimum key value for the mapping tables
  733. public $MinCharValue;
  734. /// The maximum key value for the mapping tables
  735. public $MaxCharValue;
  736. /// Whether the codepage is valid or not
  737. public $Valid;
  738. /// The character to use when an alternative doesn't exist
  739. public $SubstituteChar;
  740. }
  741. // Checks if index.php or any other script has set any codepage permissions
  742. if ( isset( $GLOBALS['EZCODEPAGEPERMISSIONS'] ) and
  743. $GLOBALS['EZCODEPAGEPERMISSIONS'] !== false )
  744. {
  745. eZCodePage::flushCacheObject();
  746. }
  747. ?>