]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/HTTP/Util.php
Initial Release of the calendar plugin
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / HTTP / Util.php
1 <?php
2
3 /**
4  * HTTP utility methods
5  *
6  * @package Sabre
7  * @subpackage HTTP
8  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
9  * @author Evert Pot (http://www.rooftopsolutions.nl/)
10  * @author Paul Voegler
11  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
12  */
13 class Sabre_HTTP_Util {
14
15     /**
16      * Parses a RFC2616-compatible date string
17      *
18      * This method returns false if the date is invalid
19      *
20      * @param string $dateHeader
21      * @return bool|DateTime
22      */
23     static function parseHTTPDate($dateHeader) {
24
25         //RFC 2616 section 3.3.1 Full Date
26         //Only the format is checked, valid ranges are checked by strtotime below
27         $month = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
28         $weekday = '(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)';
29         $wkday = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun)';
30         $time = '[0-2]\d(\:[0-5]\d){2}';
31         $date3 = $month . ' ([1-3]\d| \d)';
32         $date2 = '[0-3]\d\-' . $month . '\-\d\d';
33         //4-digit year cannot begin with 0 - unix timestamp begins in 1970
34         $date1 = '[0-3]\d ' . $month . ' [1-9]\d{3}';
35
36         //ANSI C's asctime() format
37         //4-digit year cannot begin with 0 - unix timestamp begins in 1970
38         $asctime_date = $wkday . ' ' . $date3 . ' ' . $time . ' [1-9]\d{3}';
39         //RFC 850, obsoleted by RFC 1036
40         $rfc850_date = $weekday . ', ' . $date2 . ' ' . $time . ' GMT';
41         //RFC 822, updated by RFC 1123
42         $rfc1123_date = $wkday . ', ' . $date1 . ' ' . $time . ' GMT';
43         //allowed date formats by RFC 2616
44         $HTTP_date = "($rfc1123_date|$rfc850_date|$asctime_date)";
45
46         //allow for space around the string and strip it
47         $dateHeader = trim($dateHeader, ' ');
48         if (!preg_match('/^' . $HTTP_date . '$/', $dateHeader))
49             return false;
50
51         //append implicit GMT timezone to ANSI C time format
52         if (strpos($dateHeader, ' GMT') === false)
53             $dateHeader .= ' GMT';
54
55
56         $realDate = strtotime($dateHeader);
57         //strtotime can return -1 or false in case of error
58         if ($realDate !== false && $realDate >= 0)
59             return new DateTime('@' . $realDate, new DateTimeZone('UTC'));
60
61     }
62
63     /**
64      * Transforms a DateTime object to HTTP's most common date format.
65      *
66      * We're serializing it as the RFC 1123 date, which, for HTTP must be
67      * specified as GMT.
68      *
69      * @param DateTime $dateTime
70      * @return string
71      */
72     static function toHTTPDate(DateTime $dateTime) {
73
74         // We need to clone it, as we don't want to affect the existing
75         // DateTime.
76         $dateTime = clone $dateTime;
77         $dateTime->setTimeZone(new DateTimeZone('GMT'));
78         return $dateTime->format('D, d M Y H:i:s \G\M\T');
79
80     }
81
82 }