Blame view

sources/3rdparty/sabre/dav/lib/Sabre/DAV/StringUtil.php 2.65 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
  namespace Sabre\DAV;
03e52840d   Kload   Init
3
4
5
6
7
  /**
   * String utility
   *
   * This class is mainly used to implement the 'text-match' filter, used by both
   * the CalDAV calendar-query REPORT, and CardDAV addressbook-query REPORT.
6d9380f96   Cédric Dupont   Update sources OC...
8
   * Because they both need it, it was decided to put it in Sabre\DAV instead.
03e52840d   Kload   Init
9
   *
6d9380f96   Cédric Dupont   Update sources OC...
10
11
12
   * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
   * @author Evert Pot (http://evertpot.com/)
   * @license http://sabre.io/license/ Modified BSD License
03e52840d   Kload   Init
13
   */
6d9380f96   Cédric Dupont   Update sources OC...
14
  class StringUtil {
03e52840d   Kload   Init
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
      /**
       * Checks if a needle occurs in a haystack ;)
       *
       * @param string $haystack
       * @param string $needle
       * @param string $collation
       * @param string $matchType
       * @return bool
       */
      static public function textMatch($haystack, $needle, $collation, $matchType = 'contains') {
  
          switch($collation) {
  
              case 'i;ascii-casemap' :
                  // default strtolower takes locale into consideration
                  // we don't want this.
                  $haystack = str_replace(range('a','z'), range('A','Z'), $haystack);
                  $needle = str_replace(range('a','z'), range('A','Z'), $needle);
                  break;
  
              case 'i;octet' :
                  // Do nothing
                  break;
  
              case 'i;unicode-casemap' :
                  $haystack = mb_strtoupper($haystack, 'UTF-8');
                  $needle = mb_strtoupper($needle, 'UTF-8');
                  break;
  
              default :
6d9380f96   Cédric Dupont   Update sources OC...
46
                  throw new Exception\BadRequest('Collation type: ' . $collation . ' is not supported');
03e52840d   Kload   Init
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  
          }
  
          switch($matchType) {
  
              case 'contains' :
                  return strpos($haystack, $needle)!==false;
              case 'equals' :
                  return $haystack === $needle;
              case 'starts-with' :
                  return strpos($haystack, $needle)===0;
              case 'ends-with' :
                  return strrpos($haystack, $needle)===strlen($haystack)-strlen($needle);
              default :
6d9380f96   Cédric Dupont   Update sources OC...
61
                  throw new Exception\BadRequest('Match-type: ' . $matchType . ' is not supported');
03e52840d   Kload   Init
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  
          }
  
      }
  
      /**
       * This method takes an input string, checks if it's not valid UTF-8 and
       * attempts to convert it to UTF-8 if it's not.
       *
       * Note that currently this can only convert ISO-8559-1 to UTF-8 (latin-1),
       * anything else will likely fail.
       *
       * @param string $input
       * @return string
       */
      static public function ensureUTF8($input) {
  
          $encoding = mb_detect_encoding($input , array('UTF-8','ISO-8859-1'), true);
  
          if ($encoding === 'ISO-8859-1') {
              return utf8_encode($input);
          } else {
              return $input;
          }
  
      }
  
  }