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