Code Duplication    Length = 58-59 lines in 2 locations

includes/libraries/protect/AntiXSS/UTF8.php 2 locations

@@ 5213-5271 (lines=59) @@
5210
   *
5211
   * @return false|string A sub-string,<br />or <strong>false</strong> if needle is not found.
5212
   */
5213
  public static function stristr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false)
5214
  {
5215
    $haystack = (string)$haystack;
5216
    $needle = (string)$needle;
5217
    $before_needle = (bool)$before_needle;
5218
5219
    if (!isset($haystack[0], $needle[0])) {
5220
      return false;
5221
    }
5222
5223
    if ($encoding !== 'UTF-8') {
5224
      $encoding = self::normalize_encoding($encoding, 'UTF-8');
5225
    }
5226
5227
    if ($cleanUtf8 === true) {
5228
      // "\mb_strpos" and "\iconv_strpos" returns wrong position,
5229
      // if invalid characters are found in $haystack before $needle
5230
      $needle = self::clean($needle);
5231
      $haystack = self::clean($haystack);
5232
    }
5233
5234
    if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) {
5235
      self::checkForSupport();
5236
    }
5237
5238
    if (
5239
        $encoding !== 'UTF-8'
5240
        &&
5241
        self::$SUPPORT['mbstring'] === false
5242
    ) {
5243
      trigger_error('UTF8::stristr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING);
5244
    }
5245
5246
    if (self::$SUPPORT['mbstring'] === true) {
5247
      return \mb_stristr($haystack, $needle, $before_needle, $encoding);
5248
    }
5249
5250
    if (
5251
        $encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings
5252
        &&
5253
        self::$SUPPORT['intl'] === true
5254
        &&
5255
        Bootup::is_php('5.4') === true
5256
    ) {
5257
      return \grapheme_stristr($haystack, $needle, $before_needle);
5258
    }
5259
5260
    preg_match('/^(.*?)' . preg_quote($needle, '/') . '/usi', $haystack, $match);
5261
5262
    if (!isset($match[1])) {
5263
      return false;
5264
    }
5265
5266
    if ($before_needle) {
5267
      return $match[1];
5268
    }
5269
5270
    return self::substr($haystack, self::strlen($match[1]));
5271
  }
5272
5273
  /**
5274
   * Get the string length, not the byte-length!
@@ 5938-5995 (lines=58) @@
5935
   *
5936
   * @return string|false A sub-string,<br />or <strong>false</strong> if needle is not found.
5937
   */
5938
  public static function strstr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false)
5939
  {
5940
    $haystack = (string)$haystack;
5941
    $needle = (string)$needle;
5942
5943
    if (!isset($haystack[0], $needle[0])) {
5944
      return false;
5945
    }
5946
5947
    if ($cleanUtf8 === true) {
5948
      // "\mb_strpos" and "\iconv_strpos" returns wrong position,
5949
      // if invalid characters are found in $haystack before $needle
5950
      $needle = self::clean($needle);
5951
      $haystack = self::clean($haystack);
5952
    }
5953
5954
    if ($encoding !== 'UTF-8') {
5955
      $encoding = self::normalize_encoding($encoding, 'UTF-8');
5956
    }
5957
5958
    if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) {
5959
      self::checkForSupport();
5960
    }
5961
5962
    if (
5963
        $encoding !== 'UTF-8'
5964
        &&
5965
        self::$SUPPORT['mbstring'] === false
5966
    ) {
5967
      trigger_error('UTF8::strstr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING);
5968
    }
5969
5970
    if (self::$SUPPORT['mbstring'] === true) {
5971
      return \mb_strstr($haystack, $needle, $before_needle, $encoding);
5972
    }
5973
5974
    if (
5975
        $encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings
5976
        &&
5977
        self::$SUPPORT['intl'] === true
5978
        &&
5979
        Bootup::is_php('5.4') === true
5980
    ) {
5981
      return \grapheme_strstr($haystack, $needle, $before_needle);
5982
    }
5983
5984
    preg_match('/^(.*?)' . preg_quote($needle, '/') . '/us', $haystack, $match);
5985
5986
    if (!isset($match[1])) {
5987
      return false;
5988
    }
5989
5990
    if ($before_needle) {
5991
      return $match[1];
5992
    }
5993
5994
    return self::substr($haystack, self::strlen($match[1]));
5995
  }
5996
5997
  /**
5998
   * Unicode transformation for case-less matching.