PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/core/Transcoder.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 104 lines | 45 code | 18 blank | 41 comment | 17 complexity | 5bfa123dad544444079fb1705691ca75 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/core/Transcoder.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class Transcoder
  9. * @ingroup db
  10. *
  11. * @brief Multi-class transcoder; uses mbstring and iconv if available, otherwise falls back to built-in classes
  12. */
  13. // $Id$
  14. class Transcoder {
  15. /** @var $fromEncoding string Name of source encoding */
  16. var $fromEncoding;
  17. /** @var $toEncoding string Name of target encoding */
  18. var $toEncoding;
  19. /** @var $translit boolean Whether or not to transliterate while transcoding */
  20. var $translit;
  21. /**
  22. * Constructor
  23. * @param $fromEncoding string Name of source encoding
  24. * @param $toEncoding string Name of target encoding
  25. * @param $translit boolean Whether or not to transliterate while transcoding
  26. */
  27. function Transcoder($fromEncoding, $toEncoding, $translit = false) {
  28. $this->fromEncoding = $fromEncoding;
  29. $this->toEncoding = $toEncoding;
  30. $this->translit = $translit;
  31. }
  32. /**
  33. * Transcode a string
  34. * @param $string string String to transcode
  35. * @return string Result of transcoding
  36. */
  37. function trans($string) {
  38. // detect existence of encoding conversion libraries
  39. $mbstring = function_exists('mb_convert_encoding');
  40. $iconv = function_exists('iconv');
  41. // don't do work unless we have to
  42. if (strtolower($this->fromEncoding) == strtolower($this->toEncoding)) {
  43. return $string;
  44. }
  45. // 'HTML-ENTITIES' is not a valid encoding for iconv, so transcode manually
  46. if ($this->toEncoding == 'HTML-ENTITIES' && !$mbstring) {
  47. // NB: old PHP versions may have issues with htmlentities()
  48. if (checkPhpVersion('5.2.3')) {
  49. // don't double encode added in PHP 5.2.3
  50. return htmlentities($string, ENT_COMPAT, $this->fromEncoding, false);
  51. } else {
  52. return htmlentities($string, ENT_COMPAT, $this->fromEncoding);
  53. }
  54. } elseif ($this->fromEncoding == 'HTML-ENTITIES' && !$mbstring) {
  55. // NB: old PHP versions may have issues with html_entity_decode()
  56. if (checkPhpVersion('4.3.0')) {
  57. // multibyte character handling added in PHP 5.0.0
  58. return html_entity_decode($string, ENT_COMPAT, $this->toEncoding);
  59. } else {
  60. // use built-in transcoding to UTF8
  61. $string = String::html2utf($string);
  62. // make another pass to target encoding
  63. $this->fromEncoding = 'UTF-8';
  64. return $this->trans($string);
  65. }
  66. // Special cases for transliteration ("down-sampling")
  67. } elseif ($this->translit && $iconv) {
  68. // use the iconv library to transliterate
  69. return iconv($this->fromEncoding, $this->toEncoding . '//TRANSLIT', $string);
  70. } elseif ($this->translit && $this->fromEncoding == "UTF-8" && $this->toEncoding == "ASCII") {
  71. // use the utf2ascii library
  72. require_once 'utf8_to_ascii.php';
  73. return utf8_to_ascii($string);
  74. } elseif ($mbstring) {
  75. // use the mbstring library to transcode
  76. return mb_convert_encoding($string, $this->toEncoding, $this->fromEncoding);
  77. } elseif ($iconv) {
  78. // use the iconv library to transcode
  79. return iconv($this->fromEncoding, $this->toEncoding . '//IGNORE', $string);
  80. } else {
  81. // fail gracefully by returning the original string unchanged
  82. return $string;
  83. }
  84. }
  85. }
  86. ?>