@@ -120,8 +120,13 @@ class S3
120
120
*/
121
121
public static $ useExceptions = false ;
122
122
123
- // SSL CURL SSL options - only needed if you are experiencing problems with your OpenSSL configuration
124
-
123
+ /**
124
+ * Time offset applied to time()
125
+ * @access private
126
+ * @static
127
+ */
128
+ private static $ __timeOffset = 0 ;
129
+
125
130
/**
126
131
* SSL client key
127
132
*
@@ -278,6 +283,30 @@ public static function setExceptions($enabled = true)
278
283
}
279
284
280
285
286
+ /**
287
+ * Set AWS time correction offset (use carefully)
288
+ *
289
+ * This can be used when an inaccurate system time is generating
290
+ * invalid request signatures. It should only be used as a last
291
+ * resort when the system time cannot be changed.
292
+ *
293
+ * @param string $offset Time offset (set to zero to use AWS server time)
294
+ * @return void
295
+ */
296
+ public static function setTimeCorrectionOffset ($ offset = 0 )
297
+ {
298
+ if ($ offset == 0 )
299
+ {
300
+ $ rest = new S3Request ('HEAD ' );
301
+ $ rest = $ rest ->getResponse ();
302
+ $ awstime = $ rest ->headers ['date ' ];
303
+ $ systime = time ();
304
+ $ offset = $ systime > $ awstime ? -($ systime - $ awstime ) : ($ awstime - $ systime );
305
+ }
306
+ self ::$ __timeOffset = $ offset ;
307
+ }
308
+
309
+
281
310
/**
282
311
* Set signing key
283
312
*
@@ -1125,7 +1154,7 @@ public static function deleteObject($bucket, $uri)
1125
1154
*/
1126
1155
public static function getAuthenticatedURL ($ bucket , $ uri , $ lifetime , $ hostBucket = false , $ https = false )
1127
1156
{
1128
- $ expires = time () + $ lifetime ;
1157
+ $ expires = self :: __getTime () + $ lifetime ;
1129
1158
$ uri = str_replace (array ('%2F ' , '%2B ' ), array ('/ ' , '+ ' ), rawurlencode ($ uri ));
1130
1159
return sprintf (($ https ? 'https ' : 'http ' ).'://%s/%s?AWSAccessKeyId=%s&Expires=%u&Signature=%s ' ,
1131
1160
// $hostBucket ? $bucket : $bucket.'.s3.amazonaws.com', $uri, self::$__accessKey, $expires,
@@ -1168,7 +1197,7 @@ public static function getSignedCannedURL($url, $lifetime)
1168
1197
return self ::getSignedPolicyURL (array (
1169
1198
'Statement ' => array (
1170
1199
array ('Resource ' => $ url , 'Condition ' => array (
1171
- 'DateLessThan ' => array ('AWS:EpochTime ' => time () + $ lifetime )
1200
+ 'DateLessThan ' => array ('AWS:EpochTime ' => self :: __getTime () + $ lifetime )
1172
1201
))
1173
1202
)
1174
1203
));
@@ -1194,7 +1223,7 @@ public static function getHttpUploadPostParams($bucket, $uriPrefix = '', $acl =
1194
1223
{
1195
1224
// Create policy object
1196
1225
$ policy = new stdClass ;
1197
- $ policy ->expiration = gmdate ('Y-m-d\TH:i:s\Z ' , (time () + $ lifetime ));
1226
+ $ policy ->expiration = gmdate ('Y-m-d\TH:i:s\Z ' , (self :: __getTime () + $ lifetime ));
1198
1227
$ policy ->conditions = array ();
1199
1228
$ obj = new stdClass ; $ obj ->bucket = $ bucket ; array_push ($ policy ->conditions , $ obj );
1200
1229
$ obj = new stdClass ; $ obj ->acl = $ acl ; array_push ($ policy ->conditions , $ obj );
@@ -1810,6 +1839,18 @@ private static function __getMIMEType(&$file)
1810
1839
}
1811
1840
1812
1841
1842
+ /**
1843
+ * Get the current time
1844
+ *
1845
+ * @internal Used to apply offsets to sytem time
1846
+ * @return integer
1847
+ */
1848
+ public static function __getTime ()
1849
+ {
1850
+ return time () + self ::$ __timeOffset ;
1851
+ }
1852
+
1853
+
1813
1854
/**
1814
1855
* Generate the auth string: "AWS AccessKey:Signature"
1815
1856
*
@@ -2278,6 +2319,8 @@ private function __responseHeaderCallback(&$curl, &$data)
2278
2319
list ($ header , $ value ) = explode (': ' , $ data , 2 );
2279
2320
if ($ header == 'Last-Modified ' )
2280
2321
$ this ->response ->headers ['time ' ] = strtotime ($ value );
2322
+ elseif ($ header == 'Date ' )
2323
+ $ this ->response ->headers ['date ' ] = strtotime ($ value );
2281
2324
elseif ($ header == 'Content-Length ' )
2282
2325
$ this ->response ->headers ['size ' ] = (int )$ value ;
2283
2326
elseif ($ header == 'Content-Type ' )
0 commit comments