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