PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezi18n/classes/ezcodepage.php

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