HtmGem is Php program whose goal is to make Gemini files reachable through HTTP. It can be used on a shared host. https://gmi.sbgodin.fr/htmgem
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1001 B

  1. <?php declare(strict_types=1);
  2. namespace htmgem\io;
  3. define("_BOMS", array( // Byte Order Mark
  4. // https://www.unicode.org/faq/utf_bom.html
  5. "UTF-32LE" => "\xFF\xFE\x00\x00",
  6. "UTF-16LE" => "\xFF\xFE",
  7. "UTF-16BE" => "\xFE\xFF",
  8. "UTF-8" => "\xEF\xBB\xBF",
  9. "UTF-32BE" => "\x00\x00\xFE\xFF"
  10. ));
  11. /**
  12. * Returns the encoding among Unicode ones, using the BOM
  13. * @param txt $text
  14. * @returns the encoding, or UTF-8 if no BOM read
  15. */
  16. function _detectUnicodeEncoding(&$text) {
  17. /* The PHP built-in function mb-detect-encoding()
  18. * doesn't detect UTF-16.
  19. */
  20. foreach (_BOMS as $bomName => $bomBytes)
  21. if (strpos($text, $bomBytes) === 0) return $bomName;
  22. return "UTF-8";
  23. }
  24. /** Converts to UTF8 an Unicode text and removes the BOM
  25. */
  26. function convertToUTF8(&$text) {
  27. $encoding = _detectUnicodeEncoding($text);
  28. $text = mb_convert_encoding($text, "UTF-8", $encoding);
  29. $text = preg_replace("/^"._BOMS['UTF-8']."/", "", $text);
  30. return $encoding;
  31. }