Cdn_RackSpace_Api_CloudFiles.php
3.86 KB
1
2
3
4
5
6
7
8
9
10
11
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
57
58
59
60
61
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace W3TC;
class Cdn_RackSpace_Api_CloudFiles {
private $_access_token;
private $_access_region_descriptor;
private $_new_access_required;
public function __construct( $config = array() ) {
$this->_access_token = $config['access_token'];
$this->_access_region_descriptor = $config['access_region_descriptor'];
$this->_new_access_required = $config['new_access_required'];
}
public function container_create( $container ) {
return $this->_wp_remote_put( '/' . $container );
}
/**
* data is:
* name
* content_type
* content
*/
public function object_create( $data ) {
$headers = array(
'ETag' => md5( $data['content'] )
);
if ( isset( $data['content_type'] ) )
$headers['Content-Type'] = $data['content_type'];
return $this->_wp_remote_put( '/' . $data['container'] . '/' .
ltrim( $data['name'], '/' ),
$data['content'],
$headers );
}
public function object_get_meta_or_null( $container, $name ) {
return $this->_wp_remote_head( '/' . $container . '/' .
ltrim( $name, '/' ) );
}
public function object_delete( $container, $name ) {
return $this->_wp_remote_delete( '/' . $container . '/' .
ltrim( $name, '/' ) );
}
private function _wp_remote_put( $uri, $body = array(), $headers = array() ) {
if ( !empty( $this->_access_region_descriptor['object-store.publicURL'] ) ) {
$url_base = $this->_access_region_descriptor['object-store.publicURL'];
$headers['X-Auth-Token'] = $this->_access_token;
$headers['Accept'] = 'application/json';
$result = wp_remote_post( $url_base . $uri . '?format=json', array(
'headers' => $headers,
'body' => $body,
'sslcertificates' => dirname( __FILE__ ) .
'/Cdn_RackSpace_Api_CaCert.pem',
'timeout' => 120,
'method' => 'PUT'
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return;
}
$new_object = call_user_func( $this->_new_access_required );
return $new_object->_wp_remote_put( $uri, $body, $headers );
}
private function _wp_remote_head( $uri ) {
if ( !empty( $this->_access_region_descriptor['object-store.publicURL'] ) ) {
$url_base = $this->_access_region_descriptor['object-store.publicURL'];
$result = wp_remote_get( $url_base . $uri . '?format=json', array(
'headers' => array( 'X-Auth-Token' => $this->_access_token ),
'sslcertificates' => dirname( __FILE__ ) .
'/Cdn_RackSpace_Api_CaCert.pem',
'method' => 'HEAD'
) );
if ( $result['response']['code'] == '404' )
return null;
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return $result['headers'];
}
$new_object = call_user_func( $this->_new_access_required );
return $new_object->_wp_remote_head( $uri );
}
private function _wp_remote_delete( $uri ) {
if ( !empty( $this->_access_region_descriptor['object-store.publicURL'] ) ) {
$url_base = $this->_access_region_descriptor['object-store.publicURL'];
$result = wp_remote_post( $url_base . $uri . '?format=json', array(
'headers' => array( 'X-Auth-Token' => $this->_access_token ),
'sslcertificates' => dirname( __FILE__ ) .
'/Cdn_RackSpace_Api_CaCert.pem',
'method' => 'DELETE'
) );
$r = self::_decode_response( $result );
if ( !$r['auth_required'] )
return;
}
$new_object = call_user_func( $this->_new_access_required );
return $new_object->_wp_remote_delete( $uri );
}
static private function _decode_response( $result ) {
if ( is_wp_error( $result ) )
throw new \Exception( 'Failed to reach API endpoint' );
if ( $result['response']['code'] != '200' &&
$result['response']['code'] != '201' &&
$result['response']['code'] != '202' &&
$result['response']['code'] != '204' )
throw new \Exception(
'Failed to reach API endpoint, got unexpected response ' .
$result['response']['message'] );
return array( 'auth_required' => false );
}
}