PageRenderTime 206ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/source/core/oxstrregular.php

https://github.com/GM-Alex/oxideshop_ce
PHP | 393 lines | 156 code | 28 blank | 209 comment | 14 complexity | e2da7af0ceeda7359d156f91c3ed5f93 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 regular string handling
  24. */
  25. class oxStrRegular
  26. {
  27. /**
  28. * The character encoding.
  29. *
  30. * @var string
  31. */
  32. protected $_sEncoding = 'ISO8859-15';
  33. /**
  34. * Language specific characters (currently german; storen in octal form)
  35. *
  36. * @var array
  37. */
  38. protected $_aUmls = array("\344", "\366", "\374", "\304", "\326", "\334", "\337");
  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. * @return null;
  49. */
  50. public function __construct()
  51. {
  52. }
  53. /**
  54. * PHP strlen() function wrapper
  55. *
  56. * @param string $sStr string to measure its length
  57. *
  58. * @return int
  59. */
  60. public function strlen($sStr)
  61. {
  62. return strlen($sStr);
  63. }
  64. /**
  65. * PHP substr() function wrapper
  66. *
  67. * @param string $sStr value to truncate
  68. * @param int $iStart start position
  69. * @param int $iLength length
  70. *
  71. * @return string
  72. */
  73. public function substr($sStr, $iStart, $iLength = null)
  74. {
  75. if (is_null($iLength)) {
  76. return substr($sStr, $iStart);
  77. } else {
  78. return substr($sStr, $iStart, $iLength);
  79. }
  80. }
  81. /**
  82. * PHP strpos() function wrapper
  83. *
  84. * @param string $sHaystack value to search in
  85. * @param string $sNeedle value to search for
  86. * @param int $iOffset initial search position
  87. *
  88. * @return string
  89. */
  90. public function strpos($sHaystack, $sNeedle, $iOffset = null)
  91. {
  92. $iPos = false;
  93. if ($sHaystack && $sNeedle) {
  94. if (is_null($iOffset)) {
  95. $iPos = strpos($sHaystack, $sNeedle);
  96. } else {
  97. $iPos = strpos($sHaystack, $sNeedle, $iOffset);
  98. }
  99. }
  100. return $iPos;
  101. }
  102. /**
  103. * PHP strstr() function wrapper
  104. *
  105. * @param string $sHaystack string searching in
  106. * @param string $sNeedle string to search
  107. *
  108. * @return mixed
  109. */
  110. public function strstr($sHaystack, $sNeedle)
  111. {
  112. return strstr($sHaystack, $sNeedle);
  113. }
  114. /**
  115. * PHP multi byte compliant strtolower() function wrapper
  116. *
  117. * @param string $sString string being lower cased
  118. *
  119. * @return string
  120. */
  121. public function strtolower($sString)
  122. {
  123. return strtolower($sString);
  124. }
  125. /**
  126. * PHP strtolower() function wrapper
  127. *
  128. * @param string $sString string being lower cased
  129. *
  130. * @return string
  131. */
  132. public function strtoupper($sString)
  133. {
  134. return strtoupper($sString);
  135. }
  136. /**
  137. * PHP htmlspecialchars() function wrapper
  138. *
  139. * @param string $sString string being converted
  140. * @param int $iQuotStyle quoting rule
  141. *
  142. * @return string
  143. */
  144. public function htmlspecialchars($sString, $iQuotStyle = ENT_QUOTES)
  145. {
  146. return htmlspecialchars($sString, $iQuotStyle, $this->_sEncoding);
  147. }
  148. /**
  149. * PHP htmlentities() function wrapper
  150. *
  151. * @param string $sString string being converted
  152. * @param int $iQuotStyle quoting rule
  153. *
  154. * @return string
  155. */
  156. public function htmlentities($sString, $iQuotStyle = ENT_QUOTES)
  157. {
  158. return htmlentities($sString, $iQuotStyle, $this->_sEncoding);
  159. }
  160. /**
  161. * PHP html_entity_decode() function wrapper
  162. *
  163. * @param string $sString string being converted
  164. * @param int $iQuotStyle quoting rule
  165. *
  166. * @return string
  167. */
  168. public function html_entity_decode($sString, $iQuotStyle = ENT_QUOTES)
  169. {
  170. return html_entity_decode($sString, $iQuotStyle, $this->_sEncoding);
  171. }
  172. /**
  173. * PHP preg_split() function wrapper
  174. *
  175. * @param string $sPattern pattern to search for, as a string
  176. * @param string $sString input string
  177. * @param int $iLimit (optional) only substrings up to limit are returned
  178. * @param int $iFlag flags
  179. *
  180. * @return string
  181. */
  182. public function preg_split($sPattern, $sString, $iLimit = -1, $iFlag = 0)
  183. {
  184. return preg_split($sPattern, $sString, $iLimit, $iFlag);
  185. }
  186. /**
  187. * PHP preg_replace() function wrapper
  188. *
  189. * @param mixed $sPattern pattern to search for, as a string
  190. * @param mixed $sString string to replace
  191. * @param string $sSubject strings to search and replace
  192. * @param int $iLimit maximum possible replacements
  193. * @param int $iCount number of replacements done
  194. *
  195. * @return string
  196. */
  197. public function preg_replace($sPattern, $sString, $sSubject, $iLimit = -1, $iCount = null)
  198. {
  199. return preg_replace($sPattern, $sString, $sSubject, $iLimit, $iCount);
  200. }
  201. /**
  202. * PHP preg_match() function wrapper
  203. *
  204. * @param string $sPattern pattern to search for, as a string
  205. * @param string $sSubject input string
  206. * @param array &$aMatches is filled with the results of search
  207. * @param int $iFlags flags
  208. * @param int $iOffset place from which to start the search
  209. *
  210. * @return string
  211. */
  212. public function preg_match($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
  213. {
  214. return preg_match($sPattern, $sSubject, $aMatches, $iFlags, $iOffset);
  215. }
  216. /**
  217. * PHP preg_match_all() function wrapper
  218. *
  219. * @param string $sPattern pattern to search for, as a string
  220. * @param string $sSubject input string
  221. * @param array &$aMatches is filled with the results of search
  222. * @param int $iFlags flags
  223. * @param int $iOffset place from which to start the search
  224. *
  225. * @return string
  226. */
  227. public function preg_match_all($sPattern, $sSubject, &$aMatches = null, $iFlags = null, $iOffset = null)
  228. {
  229. return preg_match_all($sPattern, $sSubject, $aMatches, $iFlags, $iOffset);
  230. }
  231. /**
  232. * PHP ucfirst() function wrapper
  233. *
  234. * @param string $sSubject input string
  235. *
  236. * @return string
  237. */
  238. public function ucfirst($sSubject)
  239. {
  240. $sString = $this->strtoupper($this->substr($sSubject, 0, 1));
  241. return $sString . $this->substr($sSubject, 1);
  242. }
  243. /**
  244. * PHP wordwrap() function wrapper
  245. *
  246. * @param string $sString input string
  247. * @param int $iLength column width
  248. * @param string $sBreak line is broken using the optional break parameter
  249. * @param bool $blCut string is always wrapped at the specified width
  250. *
  251. * @return string
  252. */
  253. public function wordwrap($sString, $iLength = 75, $sBreak = "\n", $blCut = null)
  254. {
  255. return wordwrap($sString, $iLength, $sBreak, $blCut);
  256. }
  257. /**
  258. * Recodes and returns passed input:
  259. * if $blToHtmlEntities == true ä -> &auml;
  260. * if $blToHtmlEntities == false &auml; -> ä
  261. *
  262. * @param string $sInput text to recode
  263. * @param bool $blToHtmlEntities recode direction
  264. * @param array $aUmls language specific characters
  265. * @param array $aUmlEntities language specific characters equivalents in entities form
  266. *
  267. * @return string
  268. */
  269. public function recodeEntities($sInput, $blToHtmlEntities = false, $aUmls = array(), $aUmlEntities = array())
  270. {
  271. $aUmls = (count($aUmls) > 0) ? array_merge($this->_aUmls, $aUmls) : $this->_aUmls;
  272. $aUmlEntities = (count($aUmlEntities) > 0) ? array_merge($this->_aUmlEntities, $aUmlEntities) : $this->_aUmlEntities;
  273. return $blToHtmlEntities ? str_replace($aUmls, $aUmlEntities, $sInput) : str_replace($aUmlEntities, $aUmls, $sInput);
  274. }
  275. /**
  276. * Checks if string has special chars
  277. *
  278. * @param string $sStr string to search in
  279. *
  280. * @return bool
  281. */
  282. public function hasSpecialChars($sStr)
  283. {
  284. return $this->preg_match("/(" . implode("|", $this->_aUmls) . "|(&amp;))/", $sStr);
  285. }
  286. /**
  287. * Replaces special characters with passed char.
  288. * Special chars are: \n \r \t x95 xa0 ;
  289. *
  290. * @param string $sStr string to cleanup
  291. * @param object $sCleanChr which character should be used as a replacement (default is empty space)
  292. *
  293. * @return string
  294. */
  295. public function cleanStr($sStr, $sCleanChr = ' ')
  296. {
  297. return $this->preg_replace("/\n|\r|\t|\x95|\xa0|;/", $sCleanChr, $sStr);
  298. }
  299. /**
  300. * wrapper for json encode, which does not work with non utf8 characters
  301. *
  302. * @param mixed $data data to encode
  303. *
  304. * @return string
  305. */
  306. public function jsonEncode($data)
  307. {
  308. if (is_array($data)) {
  309. $ret = "";
  310. $blWasOne = false;
  311. $blNumerical = true;
  312. reset($data);
  313. while ($blNumerical && (list($key) = each($data))) {
  314. $blNumerical = !is_string($key);
  315. }
  316. if ($blNumerical) {
  317. return '[' . implode(',', array_map(array($this, 'jsonEncode'), $data)) . ']';
  318. } else {
  319. foreach ($data as $key => $val) {
  320. if ($blWasOne) {
  321. $ret .= ',';
  322. } else {
  323. $blWasOne = true;
  324. }
  325. $ret .= '"' . addslashes($key) . '":' . $this->jsonEncode($val);
  326. }
  327. return "{" . $ret . "}";
  328. }
  329. } else {
  330. return '"' . addcslashes((string) $data, "\r\n\t\"\\") . '"';
  331. }
  332. }
  333. /**
  334. * PHP strip_tags() function wrapper.
  335. *
  336. * @param string $sString the input string
  337. * @param string $sAllowableTags an optional parameter to specify tags which should not be stripped
  338. *
  339. * @return string
  340. */
  341. public function strip_tags($sString, $sAllowableTags = '')
  342. {
  343. if (stripos($sAllowableTags, '<style>') === false) {
  344. // strip style tags with definitions within
  345. $sString = $this->preg_replace("'<style[^>]*>.*</style>'siU", '', $sString);
  346. }
  347. return strip_tags($sString, $sAllowableTags);
  348. }
  349. /**
  350. * Compares two strings. Case sensitive.
  351. * For use in sorting with reverse order
  352. *
  353. * @param string $sStr1 String to compare
  354. * @param string $sStr2 String to compare
  355. *
  356. * @return int > 0 if str1 is less than str2; < 0 if str1 is greater than str2, and 0 if they are equal.
  357. */
  358. public function strrcmp($sStr1, $sStr2)
  359. {
  360. return -strcmp($sStr1, $sStr2);
  361. }
  362. }