PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/develop/generate_utf_casefold.php

https://github.com/naderman/phpbb-orchestra
PHP | 156 lines | 113 code | 17 blank | 26 comment | 13 complexity | bfa00417a508390df59a88b2745739a5 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (php_sapi_name() != 'cli')
  11. {
  12. die("This program must be run from the command line.\n");
  13. }
  14. //
  15. // Security message:
  16. //
  17. // This script is potentially dangerous.
  18. // Remove or comment the next line (die(".... ) to enable this script.
  19. // Do NOT FORGET to either remove this script or disable it after you have used it.
  20. //
  21. die("Please read the first lines of this script for instructions on how to enable it");
  22. set_time_limit(0);
  23. define('IN_PHPBB', true);
  24. $phpbb_root_path = '../';
  25. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  26. echo "Checking for required files\n";
  27. download('http://unicode.org/Public/UNIDATA/CaseFolding.txt');
  28. echo "\n";
  29. /**
  30. * Load the CaseFolding table
  31. */
  32. echo "Loading CaseFolding\n";
  33. $unidata = file_get_contents('CaseFolding.txt');
  34. function utf8_chr($cp)
  35. {
  36. if ($cp > 0xFFFF)
  37. {
  38. return chr(0xF0 | ($cp >> 18)) . chr(0x80 | (($cp >> 12) & 0x3F)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F));
  39. }
  40. else if ($cp > 0x7FF)
  41. {
  42. return chr(0xE0 | ($cp >> 12)) . chr(0x80 | (($cp >> 6) & 0x3F)) . chr(0x80 | ($cp & 0x3F));
  43. }
  44. else if ($cp > 0x7F)
  45. {
  46. return chr(0xC0 | ($cp >> 6)) . chr(0x80 | ($cp & 0x3F));
  47. }
  48. else
  49. {
  50. return chr($cp);
  51. }
  52. }
  53. preg_match_all('/^([0-9A-F]+); ([CFS]); ([0-9A-F]+(?: [0-9A-F]+)*);/im', $unidata, $array, PREG_SET_ORDER);
  54. $uniarray = array();
  55. foreach ($array as $value)
  56. {
  57. $uniarray[$value[2]][utf8_chr(hexdec((string)$value[1]))] = implode(array_map('utf8_chr', array_map('hexdec', explode(' ', $value[3]))));
  58. }
  59. foreach ($uniarray as $idx => $contents)
  60. {
  61. echo "Writing to case_fold_$idx.$phpEx\n";
  62. $fp = fopen($phpbb_root_path . 'includes/utf/data/case_fold_' . strtolower($idx) . '.' . $phpEx, 'wb');
  63. fwrite($fp, '<?php return ' . my_var_export($contents) . ';');
  64. fclose($fp);
  65. }
  66. /**
  67. * Return a parsable string representation of a variable
  68. *
  69. * This is function is limited to array/strings/integers
  70. *
  71. * @param mixed $var Variable
  72. * @return string PHP code representing the variable
  73. */
  74. function my_var_export($var)
  75. {
  76. if (is_array($var))
  77. {
  78. $lines = array();
  79. foreach ($var as $k => $v)
  80. {
  81. $lines[] = my_var_export($k) . '=>' . my_var_export($v);
  82. }
  83. return 'array(' . implode(',', $lines) . ')';
  84. }
  85. else if (is_string($var))
  86. {
  87. return "'" . str_replace(array('\\', "'"), array('\\\\', "\\'"), $var) . "'";
  88. }
  89. else
  90. {
  91. return $var;
  92. }
  93. }
  94. /**
  95. * Download a file to the develop/ dir
  96. *
  97. * @param string $url URL of the file to download
  98. * @return void
  99. */
  100. function download($url)
  101. {
  102. global $phpbb_root_path;
  103. if (file_exists($phpbb_root_path . 'develop/' . basename($url)))
  104. {
  105. return;
  106. }
  107. echo 'Downloading from ', $url, ' ';
  108. if (!$fpr = fopen($url, 'rb'))
  109. {
  110. die("Can't download from $url\nPlease download it yourself and put it in the develop/ dir, kthxbai");
  111. }
  112. if (!$fpw = fopen($phpbb_root_path . 'develop/' . basename($url), 'wb'))
  113. {
  114. die("Can't open develop/" . basename($url) . " for output... please check your permissions or something");
  115. }
  116. $i = 0;
  117. $chunk = 32768;
  118. $done = '';
  119. while (!feof($fpr))
  120. {
  121. $i += fwrite($fpw, fread($fpr, $chunk));
  122. echo str_repeat("\x08", strlen($done));
  123. $done = ($i >> 10) . ' KiB';
  124. echo $done;
  125. }
  126. fclose($fpr);
  127. fclose($fpw);
  128. echo "\n";
  129. }
  130. ?>