PageRenderTime 63ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/source/core/oxstrmb.php

https://github.com/GM-Alex/oxideshop_ce
PHP | 405 lines | 142 code | 37 blank | 226 comment | 12 complexity | 624dad0af285936fe90592af2abfcfe9 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-3.0
  1. <?php
  2. /**
  3. * This file is part of OXID eShop Community Edition.
  4. *
  5. * OXID eShop Community Edition is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * OXID eShop Community Edition is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with OXID eShop Community Edition. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @link http://www.oxid-esales.com
  19. * @copyright (C) OXID eSales AG 2003-2014
  20. * @version OXID eShop CE
  21. */
  22. /**
  23. * Class dealing with multibyte strings
  24. */
  25. class oxStrMb
  26. {
  27. /**
  28. * The character encoding.
  29. *
  30. * @var string
  31. */
  32. protected $_sEncoding = 'UTF-8';
  33. /**
  34. * Language specific characters (currently german; storen in octal form)
  35. *
  36. * @var array
  37. */
  38. protected $_aUmls = array("\xc3\xa4", "\xc3\xb6", "\xc3\xbc", "\xC3\x84", "\xC3\x96", "\xC3\x9C", "\xC3\x9F");
  39. /**
  40. * oxUtilsString::$_aUmls equivalent in entities form
  41. *
  42. * @var array
  43. */
  44. protected $_aUmlEntities = array('&auml;', '&ouml;', '&uuml;', '&Auml;', '&Ouml;', '&Uuml;', '&szlig;');
  45. /**
  46. * Class constructor. The constructor is defined in order to be possible to call parent::__construct() in modules.
  47. */
  48. public function __construct()
  49. {
  50. }
  51. /**
  52. * PHP multi byte compliant strlen() function wrapper
  53. *
  54. * @param string $sStr string to measure its length
  55. *
  56. * @return int
  57. */
  58. public function strlen($sStr)
  59. {
  60. return mb_strlen($sStr, $this->_sEncoding);
  61. }
  62. /**
  63. * PHP multi byte compliant substr() function wrapper
  64. *
  65. * @param string $sStr value to truncate
  66. * @param int $iStart start position
  67. * @param int $iLength length
  68. *
  69. * @return string
  70. */
  71. public function substr($sStr, $iStart, $iLength = null)
  72. {
  73. $iLength = is_null($iLength) ? $this->strlen($sStr) : $iLength;
  74. return mb_substr($sStr, $iStart, $iLength, $this->_sEncoding);
  75. }
  76. /**
  77. * PHP multi byte compliant strpos() function wrapper
  78. *
  79. * @param string $sHaystack value to search in
  80. * @param string $sNeedle value to search for
  81. * @param int $iOffset initial search position
  82. *
  83. * @return string
  84. */
  85. public function strpos($sHaystack, $sNeedle, $iOffset = null)
  86. {
  87. $iPos = false;
  88. if ($sHaystack && $sNeedle) {
  89. $iOffset = is_null($iOffset) ? 0 : $iOffset;
  90. $iPos = mb_strpos($sHaystack, $sNeedle, $iOffset, $this->_sEncoding);
  91. }
  92. return $iPos;
  93. }
  94. /**
  95. * PHP multi byte compliant strstr() function wrapper
  96. *
  97. * @param string $sHaystack value to search in
  98. * @param string $sNeedle value to search for
  99. *
  100. * @return string
  101. */
  102. public function strstr($sHaystack, $sNeedle)
  103. {
  104. // additional check according to bug in PHP 5.2.0 version
  105. if (!$sHaystack) {
  106. return false;
  107. }
  108. return mb_strstr($sHaystack, $sNeedle, false, $this->_sEncoding);
  109. }
  110. /**
  111. * PHP multi byte compliant strtolower() function wrapper
  112. *
  113. * @param string $sString string being lower cased
  114. *
  115. * @return string
  116. */
  117. public function strtolower($sString)
  118. {
  119. return mb_strtolower($sString, $this->_sEncoding);
  120. }
  121. /**
  122. * PHP multi byte compliant strtoupper() function wrapper
  123. *
  124. * @param string $sString string being lower cased
  125. *
  126. * @return string
  127. */
  128. public function strtoupper($sString)
  129. {
  130. return mb_strtoupper($sString, $this->_sEncoding);
  131. }
  132. /**
  133. * PHP htmlspecialchars() function wrapper
  134. *
  135. * @param string $sString string being converted
  136. * @param int $iQuotStyle quoting rule
  137. *
  138. * @return string
  139. */
  140. public function htmlspecialchars($sString, $iQuotStyle = ENT_QUOTES)
  141. {
  142. return htmlspecialchars($sString, $iQuotStyle, $this->_sEncoding);
  143. }
  144. /**
  145. * PHP htmlentities() function wrapper
  146. *
  147. * @param string $sString string being converted
  148. * @param int $iQuotStyle quoting rule
  149. *
  150. * @return string
  151. */
  152. public function htmlentities($sString, $iQuotStyle = ENT_QUOTES)
  153. {
  154. return htmlentities($sString, $iQuotStyle, $this->_sEncoding);
  155. }
  156. /**
  157. * PHP html_entity_decode() function wrapper
  158. *
  159. * @param string $sString string being converted
  160. * @param int $iQuotStyle quoting rule
  161. *
  162. * @return string
  163. */
  164. public function html_entity_decode($sString, $iQuotStyle = ENT_QUOTES)
  165. {
  166. return html_entity_decode($sString, $iQuotStyle, $this->_sEncoding);
  167. }
  168. /**
  169. * PHP preg_split() function wrapper
  170. *
  171. * @param string $sPattern pattern to search for, as a string
  172. * @param string $sString input string
  173. * @param int $iLimit (optional) only sub strings up to limit are returned
  174. * @param int $iFlag flags
  175. *
  176. * @return string
  177. */
  178. public function preg_split($sPattern, $sString, $iLimit = -1, $iFlag = 0)
  179. {
  180. return preg_split($sPattern . 'u', $sString, $iLimit, $iFlag);
  181. }
  182. /**
  183. * PHP preg_replace() function wrapper
  184. *
  185. * @param mixed $aPattern pattern to search for, as a string
  186. * @param mixed $sString string to replace
  187. * @param string $sSubject strings to search and replace
  188. * @param int $iLimit maximum possible replacements
  189. * @param int $iCount number of replacements done
  190. *
  191. * @return string
  192. */
  193. public function preg_replace($aPattern, $sString, $sSubject, $iLimit = -1, $iCount = null)
  194. {
  195. if (is_array($aPattern)) {
  196. foreach ($aPattern as &$sPattern) {
  197. $sPattern = $sPattern . 'u';
  198. }
  199. } else {
  200. $aPattern = $aPattern . 'u';
  201. }
  202. return preg_replace($aPattern, $sString, $sSubject, $iLimit, $iCount);
  203. }
  204. /**
  205. * PHP preg_match() function wrapper
  206. *
  207. * @param string $sPattern pattern to search for, as a string
  208. * @param string $sSubject input string
  209. * @param array &$aMatches is filled with the results of search
  210. * @param int $iFlags flags
  211. * @param int $iOffset place from which to start the search
  212. *
  213. * @return string
  214. */
  215. public function preg_match($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
  216. {
  217. return preg_match($sPattern . 'u', $sSubject, $aMatches, $iFlags, $iOffset);
  218. }
  219. /**
  220. * PHP preg_match_all() function wrapper
  221. *
  222. * @param string $sPattern pattern to search for, as a string
  223. * @param string $sSubject input string
  224. * @param array &$aMatches is filled with the results of search
  225. * @param int $iFlags flags
  226. * @param int $iOffset place from which to start the search
  227. *
  228. * @return string
  229. */
  230. public function preg_match_all($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
  231. {
  232. return preg_match_all($sPattern . 'u', $sSubject, $aMatches, $iFlags, $iOffset);
  233. }
  234. /**
  235. * PHP ucfirst() function wrapper
  236. *
  237. * @param string $sSubject input string
  238. *
  239. * @return string
  240. */
  241. public function ucfirst($sSubject)
  242. {
  243. $sString = $this->strtoupper($this->substr($sSubject, 0, 1));
  244. return $sString . $this->substr($sSubject, 1);
  245. }
  246. /**
  247. * PHP wordwrap() function wrapper
  248. *
  249. * @param string $sString input string
  250. * @param int $iLength column width
  251. * @param string $sBreak line is broken using the optional break parameter
  252. * @param bool $blCut string is always wrapped at the specified width
  253. *
  254. * @return string
  255. */
  256. public function wordwrap($sString, $iLength = 75, $sBreak = "\n", $blCut = null)
  257. {
  258. if (!$blCut) {
  259. $sRegexp = "/^(.{1,{$iLength}}\r?(\s|$|\n)|.{1,{$iLength}}[^\r\s\n]*\r?(\n|\s|$))/u";
  260. } else {
  261. $sRegexp = "/^([^\s]{{$iLength}}|.{1,{$iLength}}\s)/u";
  262. }
  263. $iStrLen = mb_strlen($sString, $this->_sEncoding);
  264. $iWraps = floor($iStrLen / $iLength);
  265. $i = $iWraps;
  266. $sReturn = '';
  267. $aMatches = array();
  268. while ($i > 0) {
  269. $iWraps = floor(mb_strlen($sString, $this->_sEncoding) / $iLength);
  270. $i = $iWraps;
  271. if (preg_match($sRegexp, $sString, $aMatches)) {
  272. $sStr = $aMatches[0];
  273. $sReturn .= preg_replace('/\s$/s', '', $sStr) . $sBreak;
  274. $sString = $this->substr($sString, mb_strlen($sStr, $this->_sEncoding));
  275. } else {
  276. break;
  277. }
  278. $i--;
  279. }
  280. $sReturn = preg_replace("/$sBreak$/", '', $sReturn);
  281. if ($sString) {
  282. $sReturn .= $sBreak . $sString;
  283. }
  284. return $sReturn;
  285. }
  286. /**
  287. * Recodes and returns passed input:
  288. * if $blToHtmlEntities == true ä -> &auml;
  289. * if $blToHtmlEntities == false &auml; -> ä
  290. *
  291. * @param string $sInput text to recode
  292. * @param bool $blToHtmlEntities recode direction
  293. * @param array $aUmls language specific characters
  294. * @param array $aUmlEntities language specific characters equivalents in entities form
  295. *
  296. * @return string
  297. */
  298. public function recodeEntities($sInput, $blToHtmlEntities = false, $aUmls = array(), $aUmlEntities = array())
  299. {
  300. $aUmls = (count($aUmls) > 0) ? array_merge($this->_aUmls, $aUmls) : $this->_aUmls;
  301. $aUmlEntities = (count($aUmlEntities) > 0) ? array_merge($this->_aUmlEntities, $aUmlEntities) : $this->_aUmlEntities;
  302. return $blToHtmlEntities ? str_replace($aUmls, $aUmlEntities, $sInput) : str_replace($aUmlEntities, $aUmls, $sInput);
  303. }
  304. /**
  305. * Checks if string has special chars
  306. *
  307. * @param string $sStr string to search in
  308. *
  309. * @return bool
  310. */
  311. public function hasSpecialChars($sStr)
  312. {
  313. return $this->preg_match("/(" . implode("|", $this->_aUmls) . "|(&amp;))/", $sStr);
  314. }
  315. /**
  316. * Replaces special characters with passed char.
  317. * Special chars are: \n \r \t \xc2\x95 \xc2\xa0 ;
  318. *
  319. * @param string $sStr string to cleanup
  320. * @param string $sCleanChr which character should be used as a replacement (default is empty space)
  321. *
  322. * @return string
  323. */
  324. public function cleanStr($sStr, $sCleanChr = ' ')
  325. {
  326. return $this->preg_replace("/\n|\r|\t|\xc2\x95|\xc2\xa0|;/", $sCleanChr, $sStr);
  327. }
  328. /**
  329. * wrapper for json encode, which does not work with non utf8 characters
  330. *
  331. * @param mixed $data data to encode
  332. *
  333. * @return string
  334. */
  335. public function jsonEncode($data)
  336. {
  337. return json_encode($data);
  338. }
  339. /**
  340. * PHP strip_tags() function wrapper.
  341. *
  342. * @param string $sString the input string
  343. * @param string $sAllowableTags an optional parameter to specify tags which should not be stripped
  344. *
  345. * @return string
  346. */
  347. public function strip_tags($sString, $sAllowableTags = '')
  348. {
  349. if (stripos($sAllowableTags, '<style>') === false) {
  350. // strip style tags with definitions within
  351. $sString = $this->preg_replace("'<style[^>]*>.*</style>'siU", '', $sString);
  352. }
  353. return strip_tags($sString, $sAllowableTags);
  354. }
  355. /**
  356. * Compares two strings. Case sensitive.
  357. * For use in sorting with reverse order
  358. *
  359. * @param string $sStr1 String to compare
  360. * @param string $sStr2 String to compare
  361. *
  362. * @return int > 0 if str1 is less than str2; < 0 if str1 is greater than str2, and 0 if they are equal.
  363. */
  364. public function strrcmp($sStr1, $sStr2)
  365. {
  366. return -strcmp($sStr1, $sStr2);
  367. }
  368. }