Blame view

sources/3rdparty/sabre/dav/lib/Sabre/HTTP/Util.php 2.72 KB
03e52840d   Kload   Init
1
  <?php
6d9380f96   Cédric Dupont   Update sources OC...
2
  namespace Sabre\HTTP;
03e52840d   Kload   Init
3
4
5
  /**
   * HTTP utility methods
   *
6d9380f96   Cédric Dupont   Update sources OC...
6
7
   * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
   * @author Evert Pot (http://evertpot.com/)
03e52840d   Kload   Init
8
   * @author Paul Voegler
6d9380f96   Cédric Dupont   Update sources OC...
9
   * @license http://sabre.io/license/ Modified BSD License
03e52840d   Kload   Init
10
   */
6d9380f96   Cédric Dupont   Update sources OC...
11
  class Util {
03e52840d   Kload   Init
12
13
14
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
46
47
48
49
50
51
52
53
54
55
56
  
      /**
       * Parses a RFC2616-compatible date string
       *
       * This method returns false if the date is invalid
       *
       * @param string $dateHeader
       * @return bool|DateTime
       */
      static function parseHTTPDate($dateHeader) {
  
          //RFC 2616 section 3.3.1 Full Date
          //Only the format is checked, valid ranges are checked by strtotime below
          $month = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
          $weekday = '(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)';
          $wkday = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun)';
          $time = '[0-2]\d(\:[0-5]\d){2}';
          $date3 = $month . ' ([1-3]\d| \d)';
          $date2 = '[0-3]\d\-' . $month . '\-\d\d';
          //4-digit year cannot begin with 0 - unix timestamp begins in 1970
          $date1 = '[0-3]\d ' . $month . ' [1-9]\d{3}';
  
          //ANSI C's asctime() format
          //4-digit year cannot begin with 0 - unix timestamp begins in 1970
          $asctime_date = $wkday . ' ' . $date3 . ' ' . $time . ' [1-9]\d{3}';
          //RFC 850, obsoleted by RFC 1036
          $rfc850_date = $weekday . ', ' . $date2 . ' ' . $time . ' GMT';
          //RFC 822, updated by RFC 1123
          $rfc1123_date = $wkday . ', ' . $date1 . ' ' . $time . ' GMT';
          //allowed date formats by RFC 2616
          $HTTP_date = "($rfc1123_date|$rfc850_date|$asctime_date)";
  
          //allow for space around the string and strip it
          $dateHeader = trim($dateHeader, ' ');
          if (!preg_match('/^' . $HTTP_date . '$/', $dateHeader))
              return false;
  
          //append implicit GMT timezone to ANSI C time format
          if (strpos($dateHeader, ' GMT') === false)
              $dateHeader .= ' GMT';
  
  
          $realDate = strtotime($dateHeader);
          //strtotime can return -1 or false in case of error
          if ($realDate !== false && $realDate >= 0)
6d9380f96   Cédric Dupont   Update sources OC...
57
              return new \DateTime('@' . $realDate, new \DateTimeZone('UTC'));
03e52840d   Kload   Init
58
59
60
61
62
63
64
65
66
  
      }
  
      /**
       * Transforms a DateTime object to HTTP's most common date format.
       *
       * We're serializing it as the RFC 1123 date, which, for HTTP must be
       * specified as GMT.
       *
6d9380f96   Cédric Dupont   Update sources OC...
67
       * @param \DateTime $dateTime
03e52840d   Kload   Init
68
69
       * @return string
       */
6d9380f96   Cédric Dupont   Update sources OC...
70
      static function toHTTPDate(\DateTime $dateTime) {
03e52840d   Kload   Init
71
72
73
74
  
          // We need to clone it, as we don't want to affect the existing
          // DateTime.
          $dateTime = clone $dateTime;
6d9380f96   Cédric Dupont   Update sources OC...
75
          $dateTime->setTimeZone(new \DateTimeZone('GMT'));
03e52840d   Kload   Init
76
77
78
79
80
          return $dateTime->format('D, d M Y H:i:s \G\M\T');
  
      }
  
  }