|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class SandCage { |
|
4
|
|
|
// Your SandCage API Key |
|
5
|
|
|
// This can be retrieved from https://www.sandcage.com/panel/api_key |
|
6
|
|
|
protected $sandcage_api_key = '[YOUR SANDCAGE API KEY]'; |
|
7
|
|
|
|
|
8
|
|
|
// SandCage API version |
|
9
|
|
|
protected $sandcage_api_version = '0.2'; |
|
10
|
|
|
|
|
11
|
|
|
// SandCage API endpoint base |
|
12
|
|
|
protected $sandcage_api_endpoint_base; |
|
13
|
|
|
|
|
14
|
|
|
protected $user_agent; |
|
15
|
|
|
protected $follow_location = true; |
|
16
|
|
|
protected $timeout = 30; |
|
17
|
|
|
protected $post; |
|
18
|
|
|
protected $post_fields; |
|
19
|
|
|
protected $status; |
|
20
|
|
|
protected $response; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($sandcage_api_key=null) { |
|
23
|
|
|
|
|
24
|
|
|
if (!is_null($sandcage_api_key)) { |
|
25
|
|
|
$this->sandcage_api_key = $sandcage_api_key; |
|
26
|
|
|
} |
|
27
|
|
|
$this->sandcage_api_endpoint_base = 'https://api.sandcage.com/' . $this->sandcage_api_version . '/'; |
|
28
|
|
|
$this->user_agent = 'SandCage - ' . $this->sandcage_api_version; |
|
29
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The "schedule-tasks" service |
|
34
|
|
|
* @param array $payload values to send |
|
35
|
|
|
* @param string $callback_endpoint to send the callback to |
|
36
|
|
|
*/ |
|
37
|
|
View Code Duplication |
public function scheduleFiles($payload, $callback_endpoint='') { |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->post = true; |
|
40
|
|
|
$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload; |
|
41
|
|
|
|
|
42
|
|
|
if ($callback_endpoint != '') { |
|
43
|
|
|
$this->post_fields['callback_url'] = $callback_endpoint; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->call($this->sandcage_api_endpoint_base . 'schedule-tasks'); |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The "destroy-files" service |
|
52
|
|
|
* @param array $payload values to send |
|
53
|
|
|
* @param string $callback_endpoint to send the callback to |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function destroyFiles($payload, $callback_endpoint='') { |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->post = true; |
|
58
|
|
|
$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload; |
|
59
|
|
|
|
|
60
|
|
|
if ($callback_endpoint != '') { |
|
61
|
|
|
$this->post_fields['callback_url'] = $callback_endpoint; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->call($this->sandcage_api_endpoint_base . 'destroy-files'); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The "list-files" service |
|
70
|
|
|
* @param array $payload values to send |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function listFiles($payload) { |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$this->post = true; |
|
75
|
|
|
$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload; |
|
76
|
|
|
|
|
77
|
|
|
$this->call($this->sandcage_api_endpoint_base . 'list-files'); |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* The "get-info" service |
|
83
|
|
|
* @param array $payload values to send |
|
84
|
|
|
*/ |
|
85
|
|
View Code Duplication |
public function getInfo($payload) { |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$this->post = true; |
|
88
|
|
|
$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload; |
|
89
|
|
|
|
|
90
|
|
|
$this->call($this->sandcage_api_endpoint_base . 'get-info'); |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Send a requst using cURL |
|
96
|
|
|
* @param string $service_endpoint to request |
|
97
|
|
|
*/ |
|
98
|
|
|
public function call($service_endpoint) { |
|
99
|
|
|
|
|
100
|
|
|
// Initialize the cURL session |
|
101
|
|
|
$ch = curl_init($service_endpoint); |
|
102
|
|
|
|
|
103
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); |
|
104
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location); |
|
105
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
|
106
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
|
107
|
|
|
|
|
108
|
|
|
if ($this->post) { |
|
109
|
|
|
curl_setopt($ch, CURLOPT_POST, TRUE); |
|
110
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->post_fields)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Execute the cURL session |
|
114
|
|
|
$this->response = curl_exec($ch); |
|
115
|
|
|
|
|
116
|
|
|
// Retry if certificates are missing. |
|
117
|
|
|
if (curl_errno($ch) == CURLE_SSL_CACERT) { |
|
118
|
|
|
|
|
119
|
|
|
// Set the pem file holding the CA Root Certificates to verify the peer with. |
|
120
|
|
|
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); |
|
121
|
|
|
|
|
122
|
|
|
// Retry execution after setting CURLOPT_CAINFO |
|
123
|
|
|
$this->response = curl_exec($ch); |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Get information regarding the transfer |
|
128
|
|
|
$this->status = curl_getinfo($ch); |
|
129
|
|
|
|
|
130
|
|
|
// Close the cURL session |
|
131
|
|
|
curl_close($ch); |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Return the HTTP status of the call |
|
137
|
|
|
* @return array or FALSE on failure |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getHttpStatus() { |
|
140
|
|
|
|
|
141
|
|
|
return $this->status; |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Return the HTTP status of the call |
|
147
|
|
|
* @return array or FALSE on failure |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getResponse() { |
|
150
|
|
|
|
|
151
|
|
|
return $this->response; |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
?> |
|
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.