/ojs/ojs-2.0.1/classes/core/String.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite · PHP · 212 lines · 138 code · 32 blank · 42 comment · 34 complexity · 8f33532cad95eba9efda455b3686535e MD5 · raw file

  1. <?php
  2. /**
  3. * String.inc.php
  4. *
  5. * Copyright (c) 2003-2004 The Public Knowledge Project
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package core
  9. *
  10. * String manipulation wrapper class.
  11. *
  12. * $Id: String.inc.php,v 1.3 2005/06/16 20:28:05 alec Exp $
  13. */
  14. class String {
  15. /**
  16. * Perform initialization required for the string wrapper library.
  17. */
  18. function init() {
  19. $clientCharset = strtolower(Config::getVar('i18n', 'client_charset'));
  20. // FIXME Should non-UTF-8 encodings be supported with mbstring?
  21. $PCRE_UTF8 = '';
  22. if ($clientCharset == 'utf-8' && String::hasPCREUTF8()) {
  23. $PCRE_UTF8 = 'u';
  24. }
  25. // Check if mbstring is installed
  26. // NOTE: Requires PHP >= 4.3.0
  27. if (String::hasMBString()) {
  28. // mbstring routines are available
  29. define('ENABLE_MBSTRING', 1);
  30. // Set up required ini settings for mbstring
  31. ini_set('mbstring.internal_encoding', $clientCharset);
  32. if ($clientCharset == 'utf-8') {
  33. ini_set('mbstring.substitute_character', '12307');
  34. }
  35. // FIXME Do any other mbstring settings need to be set?
  36. }
  37. // Define modifier to be used in regexp_* routines
  38. define('PCRE_UTF8', $PCRE_UTF8);
  39. }
  40. /**
  41. * Check if server has the mbstring library.
  42. * Currently requires PHP >= 4.3.0 (for mb_strtolower, mb_strtoupper, and mb_substr_count)
  43. */
  44. function hasMBString() {
  45. return (function_exists('mb_strlen')
  46. && function_exists('mb_strpos')
  47. && function_exists('mb_strrpos')
  48. && function_exists('mb_substr')
  49. && function_exists('mb_strtolower')
  50. && function_exists('mb_strtoupper')
  51. && function_exists('mb_substr_count')
  52. && function_exists('mb_send_mail'));
  53. }
  54. /**
  55. * Check if server supports the PCRE_UTF8 modifier.
  56. */
  57. function hasPCREUTF8() {
  58. // The PCRE_UTF8 modifier is only supported on PHP >= 4.1.0 (*nix) or PHP >= 4.2.3 (win32)
  59. // Evil check to see if PCRE_UTF8 is supported
  60. if (@preg_match('//u', '')) {
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. //
  67. // Wrappers for basic string manipulation routines.
  68. // See the php.net documentation for usage.
  69. //
  70. function strlen($string) {
  71. if (defined('ENABLE_MBSTRING')) {
  72. return mb_strlen($string);
  73. } else {
  74. return strlen($string);
  75. }
  76. }
  77. function strpos($haystack, $needle, $offset = 0) {
  78. if (defined('ENABLE_MBSTRING')) {
  79. return mb_strpos($haystack, $needle, $offset);
  80. } else {
  81. return strpos($haystack, $needle, $offset);
  82. }
  83. }
  84. function strrpos($haystack, $needle) {
  85. if (defined('ENABLE_MBSTRING')) {
  86. return mb_strrpos($haystack, $needle);
  87. } else {
  88. return strrpos($haystack, $needle);
  89. }
  90. }
  91. function substr($string, $start, $length = null) {
  92. if (defined('ENABLE_MBSTRING')) {
  93. $substr = 'mb_substr';
  94. } else {
  95. $substr = 'substr';
  96. }
  97. if (isset($length)) {
  98. return $substr($string, $start, $length);
  99. } else {
  100. return $substr($string, $start);
  101. }
  102. }
  103. function strtolower($string) {
  104. if (defined('ENABLE_MBSTRING')) {
  105. return mb_strtolower($string); // Requires PHP >= 4.3.0
  106. } else {
  107. return strtolower($string);
  108. }
  109. }
  110. function strtoupper($string) {
  111. if (defined('ENABLE_MBSTRING')) {
  112. return mb_strtoupper($string); // Requires PHP >= 4.3.0
  113. } else {
  114. return strtolower($string);
  115. }
  116. }
  117. function substr_count($haystack, $needle) {
  118. if (defined('ENABLE_MBSTRING')) {
  119. return mb_substr_count($haystack, $needle); // Requires PHP >= 4.3.0
  120. } else {
  121. return substr_count($haystack, $needle);
  122. }
  123. }
  124. function encode_mime_header($string) {
  125. if (defined('ENABLE_MBSTRING')) {
  126. return mb_encode_mimeheader($string);
  127. } else {
  128. return $string;
  129. }
  130. }
  131. function mail($to, $subject, $message, $additional_headers = '', $additional_parameters = '') {
  132. // Cannot use mb_send_mail as it base64 encodes the whole body of the email,
  133. // making it useless for multipart emails
  134. return mail($to, $subject, $message, $additional_headers, $additional_parameters);
  135. }
  136. //
  137. // Wrappers for PCRE-compatible regular expression routines.
  138. // See the php.net documentation for usage.
  139. //
  140. function regexp_quote($string, $delimiter = '/') {
  141. return preg_quote($string, $delimiter);
  142. }
  143. function regexp_grep($pattern, $input) {
  144. $pattern .= PCRE_UTF8;
  145. return preg_grep($pattern, $input);
  146. }
  147. function regexp_match($pattern, $subject) {
  148. $pattern .= PCRE_UTF8;
  149. return preg_match($pattern, $subject);
  150. }
  151. function regexp_match_get($pattern, $subject, &$matches) {
  152. // NOTE: This function was created since PHP < 5.x does not support optional reference parameters
  153. $pattern .= PCRE_UTF8;
  154. return preg_match($pattern, $subject, $matches);
  155. }
  156. function regexp_match_all($pattern, $subject, &$matches) {
  157. $pattern .= PCRE_UTF8;
  158. return preg_match_all($pattern, $subject, $matches);
  159. }
  160. function regexp_replace($pattern, $replacement, $subject, $limit = -1) {
  161. $pattern .= PCRE_UTF8;
  162. return preg_replace($pattern, $replacement, $subject, $limit);
  163. }
  164. function regexp_replace_callback($pattern, $callback, $subject, $limit = -1) {
  165. $pattern .= PCRE_UTF8;
  166. return preg_replace_callback($pattern, $callback, $subject, $limit);
  167. }
  168. function regexp_split($pattern, $subject, $limit = -1) {
  169. $pattern .= PCRE_UTF8;
  170. return preg_split($pattern, $subject, $limit);
  171. }
  172. function mime_content_type($filename) {
  173. if (!function_exists('mime_content_type')) {
  174. $f = escapeshellarg($filename);
  175. return trim(`file -bi $f`);
  176. }
  177. return mime_content_type($filename);
  178. }
  179. }
  180. ?>