├── templates
├── error.tpl
└── overview.tpl
├── README.md
├── bunnycdn.php
└── api.php
/templates/error.tpl:
--------------------------------------------------------------------------------
1 |
Oops! Something went wrong.
2 |
3 |
6 |
7 | Please go back and try again.
8 |
9 | If the problem persists, please contact support.
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BunnyCDN WHMCS Module
2 | This provisioning module for WHMCS can be used to create new pull zones inside bunnycdn through WHMCS. However, this module currently does not support features like Purge specific URL, changing other settings etc.
3 |
4 | Only Create/Suspend/Unsuspend/Terminate/Purge All Cache actions are available. But if you are interested in contributing, please open a PR.
5 |
6 |
7 | ## Installation
8 | Goto modules/servers and create a new directory named 'bunnycdn' and copy the contents of this repo there.
9 |
10 | Or
11 |
12 | Goto modules/servers and run below command if available
13 | ``
14 | git clone https://github.com/jetchirag/bunnycdn-whmcs/ bunnycdn
15 | ``
16 |
17 | ### Create new server
18 | Add a new server and select BunnyCDN in Module. Use your API key as server's password.
19 |
20 | ## While creating a product, it is important to provide a custom field option which looks like this:
21 |
22 | 
23 |
24 | API Wrapper Used: https://github.com/codewithmark/bunnycdn/
25 |
--------------------------------------------------------------------------------
/templates/overview.tpl:
--------------------------------------------------------------------------------
1 | Overview
2 |
3 |
4 |
5 |
6 | Monthly Bandwidth limit
7 |
8 |
9 | {$MonthlyBandwidthLimit}
10 |
11 |
12 |
13 |
14 |
15 | Bandwidth used
16 |
17 |
18 | {$MonthlyBandwidthUsed}
19 |
20 |
21 |
22 |
23 |
24 | Origin URL
25 |
26 |
27 | {$OriginUrl}
28 |
29 |
30 |
31 |
32 |
33 | CDN URL
34 |
35 |
36 | {foreach from=$Hostnames item=hostname}
37 | {if $hostname->IsSystemHostname eq 1}
38 | {$hostname->Value}
39 | {/if}
40 | {/foreach}
41 |
42 |
43 |
44 |
45 |
46 | Hostnames
47 |
48 |
49 | {foreach from=$Hostnames item=hostname}
50 | {if $hostname->IsSystemHostname neq 1}
51 | {$hostname->Value}
52 | {/if}
53 | {/foreach}
54 |
55 |
56 |
57 |
58 | {$LANG.clientareaproductdetails}
59 |
60 |
61 |
62 |
63 |
64 | {$LANG.clientareahostingregdate}
65 |
66 |
67 | {$regdate}
68 |
69 |
70 |
71 |
72 |
73 | {$LANG.orderproduct}
74 |
75 |
76 | {$groupname} - {$product}
77 |
78 |
79 |
80 | {foreach from=$configurableoptions item=configoption}
81 |
82 |
83 | {$configoption.optionname}
84 |
85 |
86 | {if $configoption.optiontype eq 3}
87 | {if $configoption.selectedqty}
88 | {$LANG.yes}
89 | {else}
90 | {$LANG.no}
91 | {/if}
92 | {elseif $configoption.optiontype eq 4}
93 | {$configoption.selectedqty} x {$configoption.selectedoption}
94 | {else}
95 | {$configoption.selectedoption}
96 | {/if}
97 |
98 |
99 | {/foreach}
100 |
101 | {foreach from=$productcustomfields item=customfield}
102 |
103 |
104 | {$customfield.name}
105 |
106 |
107 | {$customfield.value}
108 |
109 |
110 | {/foreach}
111 |
112 | {if $lastupdate}
113 |
114 |
115 | {$LANG.clientareadiskusage}
116 |
117 |
118 | {$diskusage}MB / {$disklimit}MB ({$diskpercent})
119 |
120 |
121 |
122 |
123 | {$LANG.clientareabwusage}
124 |
125 |
126 | {$bwusage}MB / {$bwlimit}MB ({$bwpercent})
127 |
128 |
129 | {/if}
130 |
131 |
132 |
133 | {$LANG.orderpaymentmethod}
134 |
135 |
136 | {$paymentmethod}
137 |
138 |
139 |
140 |
141 |
142 | {$LANG.firstpaymentamount}
143 |
144 |
145 | {$firstpaymentamount}
146 |
147 |
148 |
149 |
150 |
151 | {$LANG.recurringamount}
152 |
153 |
154 | {$recurringamount}
155 |
156 |
157 |
158 |
159 |
160 | {$LANG.clientareahostingnextduedate}
161 |
162 |
163 | {$nextduedate}
164 |
165 |
166 |
167 |
168 |
169 | {$LANG.orderbillingcycle}
170 |
171 |
172 | {$billingcycle}
173 |
174 |
175 |
176 |
177 |
178 | {$LANG.clientareastatus}
179 |
180 |
181 | {$status}
182 |
183 |
184 |
185 | {if $suspendreason}
186 |
187 |
188 | {$LANG.suspendreason}
189 |
190 |
191 | {$suspendreason}
192 |
193 |
194 | {/if}
195 |
196 |
197 |
198 |
199 | {if $packagesupgrade}
200 |
205 | {/if}
206 |
207 |
216 |
217 |
--------------------------------------------------------------------------------
/bunnycdn.php:
--------------------------------------------------------------------------------
1 | 'BunnyCDN',
20 | 'APIVersion' => '1.0',
21 | 'RequiresServer' => true
22 | );
23 | }
24 |
25 | function bunnycdn_ConfigOptions()
26 | {
27 | return array(
28 | 'bandwidth' => array(
29 | 'FriendlyName' => 'Bandwidth limit',
30 | 'Type' => 'text',
31 | 'Default' => '10737418240',
32 | 'Description' => 'Enter in bytes',
33 | ),
34 | );
35 | }
36 |
37 | function bunnycdn_CreateAccount(array $params){
38 | try {
39 |
40 | $protocol = $params['customfields']['protocol'];
41 |
42 | if ($protocol != 'http' && $protocol != 'https') {
43 | return "Invalid Protocol given '" . $protocol . "'";
44 | }
45 |
46 | $bunny = new BunnyCDN($params['serverpassword']);
47 |
48 | $response = $bunny->CreateNewZone($params['username'], $protocol . "://" . $params['domain']);
49 |
50 | if ($response['status'] != 'success') {
51 | return json_encode($response['msg']);
52 | }
53 |
54 | $updateFields = [
55 | 'MonthlyBandwidthLimit' => $params['configoption1']
56 | ];
57 |
58 | $params['model']->serviceProperties->save(['zoneid' => $response['zone_id']]);
59 |
60 | $response = $bunny->UpdateZone($response['zone_id'], $updateFields);
61 |
62 | if ($response['status'] != 'success') {
63 | return json_encode($response['msg']);
64 | }
65 |
66 |
67 | } catch (Exception $e) {
68 | logModuleCall(
69 | 'bunnycdn',
70 | __FUNCTION__,
71 | $params,
72 | $e->getMessage(),
73 | $e->getTraceAsString()
74 | );
75 |
76 | return $e->getMessage();
77 | }
78 |
79 | return 'success';
80 | }
81 |
82 | function bunnycdn_SuspendAccount(array $params)
83 | {
84 | try {
85 | $bunny = new BunnyCDN($params['serverpassword']);
86 |
87 | $zoneid = $params['model']->serviceProperties->get('zoneid');
88 |
89 | $updateFields = [
90 | 'MonthlyBandwidthLimit' => 1
91 | ];
92 |
93 | $response = $bunny->UpdateZone($zoneid, $updateFields);
94 |
95 | if ($response['status'] != 'success') {
96 | return json_encode($response['msg']);
97 | }
98 | } catch (Exception $e) {
99 | logModuleCall(
100 | 'bunnycdn',
101 | __FUNCTION__,
102 | $params,
103 | $e->getMessage(),
104 | $e->getTraceAsString()
105 | );
106 |
107 | return $e->getMessage();
108 | }
109 |
110 | return 'success';
111 | }
112 |
113 | function bunnycdn_UnsuspendAccount(array $params)
114 | {
115 | try {
116 | $bunny = new BunnyCDN($params['serverpassword']);
117 |
118 | $zoneid = $params['model']->serviceProperties->get('zoneid');
119 |
120 | $updateFields = [
121 | 'MonthlyBandwidthLimit' => $params['configoption1']
122 | ];
123 |
124 | $response = $bunny->UpdateZone($zoneid, $updateFields);
125 |
126 | if ($response['status'] != 'success') {
127 | return json_encode($response['msg']);
128 | }
129 | } catch (Exception $e) {
130 | logModuleCall(
131 | 'bunnycdn',
132 | __FUNCTION__,
133 | $params,
134 | $e->getMessage(),
135 | $e->getTraceAsString()
136 | );
137 |
138 | return $e->getMessage();
139 | }
140 |
141 | return 'success';
142 | }
143 |
144 | function bunnycdn_TerminateAccount(array $params)
145 | {
146 | try {
147 | $bunny = new BunnyCDN($params['serverpassword']);
148 |
149 | $zoneid = $params['model']->serviceProperties->get('zoneid');
150 |
151 | $response = $bunny->DeleteZone($zoneid);
152 |
153 | if ($response['status'] != 'success') {
154 | return json_encode($response['msg']);
155 | }
156 | } catch (Exception $e) {
157 | logModuleCall(
158 | 'bunnycdn',
159 | __FUNCTION__,
160 | $params,
161 | $e->getMessage(),
162 | $e->getTraceAsString()
163 | );
164 |
165 | return $e->getMessage();
166 | }
167 |
168 | return 'success';
169 | }
170 |
171 | function bunnycdn_TestConnection(array $params)
172 | {
173 | try {
174 | $bunny = new BunnyCDN($params['serverpassword']);
175 | $response = $bunny->GetZoneList();
176 |
177 | if ($response['status'] != 'success') {
178 | $success = false;
179 | $errorMsg = $response['msg'];
180 | }
181 | else {
182 | $success = true;
183 | $errorMsg = '';
184 | }
185 |
186 | } catch (Exception $e) {
187 | logModuleCall(
188 | 'bunnycdn',
189 | __FUNCTION__,
190 | $params,
191 | $e->getMessage(),
192 | $e->getTraceAsString()
193 | );
194 |
195 | $success = false;
196 | $errorMsg = $e->getMessage();
197 | }
198 |
199 | return array(
200 | 'success' => $success,
201 | 'error' => $errorMsg,
202 | );
203 | }
204 |
205 | function bunnycdn_AdminCustomButtonArray()
206 | {
207 | return array(
208 | "Purge Cache" => "purgeCache",
209 | );
210 | }
211 |
212 | function bunnycdn_ClientAreaCustomButtonArray()
213 | {
214 | return array(
215 | "Purge Cache" => "purgeCache",
216 | );
217 | }
218 |
219 | function bunnycdn_purgeCache(array $params)
220 | {
221 | try {
222 | $bunny = new BunnyCDN($params['serverpassword']);
223 |
224 | $zoneid = $params['model']->serviceProperties->get('zoneid');
225 |
226 | $response = $bunny->PurgeZoneCache($zoneid);
227 |
228 | if ($response['status'] != 'success') {
229 | return "An error occured. Please contact Administrator";
230 | }
231 | } catch (Exception $e) {
232 | logModuleCall(
233 | 'bunnycdn',
234 | __FUNCTION__,
235 | $params,
236 | $e->getMessage(),
237 | $e->getTraceAsString()
238 | );
239 |
240 | return $e->getMessage();
241 | }
242 |
243 | return 'success';
244 | }
245 |
246 | function bunnycdn_AdminServicesTabFields(array $params)
247 | {
248 | try {
249 | $bunny = new BunnyCDN($params['serverpassword']);
250 |
251 | $zoneid = $params['model']->serviceProperties->get('zoneid');
252 |
253 | $response = $bunny->GetZone($zoneid);
254 |
255 | if ($response['status'] != 'success') {
256 | return [];
257 | }
258 |
259 | $zoneDetails = [];
260 | // foreach ((array)$response['zone_details'] as $key => $value) {
261 | // if (!is_array($value)){
262 | // $zoneDetails[$key] = $value;
263 | // }
264 | // }
265 | return $zoneDetails;
266 | } catch (Exception $e) {
267 | logModuleCall(
268 | 'bunnycdn',
269 | __FUNCTION__,
270 | $params,
271 | $e->getMessage(),
272 | $e->getTraceAsString()
273 | );
274 | }
275 |
276 | return array();
277 | }
278 |
279 | function bunnycdn_ClientArea(array $params){
280 | try {
281 | $bunny = new BunnyCDN($params['serverpassword']);
282 |
283 | $zoneid = $params['model']->serviceProperties->get('zoneid');
284 | $cdnURL = $params['model']->serviceProperties->get('CDNURL');
285 |
286 |
287 | $response = $bunny->GetZone($zoneid);
288 |
289 | if ($response['status'] != 'success') {
290 | return array(
291 | 'tabOverviewReplacementTemplate' => 'templates/error.tpl',
292 | 'templateVariables' => array(
293 | 'errMsg' => 'Unknown error'
294 | ),
295 | );
296 | }
297 |
298 | $requestedAction = isset($_REQUEST['customAction']) ? $_REQUEST['customAction'] : '';
299 |
300 | if ($requestedAction == 'purge') {
301 | } else {
302 | $templateFile = 'templates/overview.tpl';
303 | return array(
304 | 'tabOverviewReplacementTemplate' => 'templates/overview.tpl',
305 | 'templateVariables' => array(
306 | 'MonthlyBandwidthLimit' => $bunny->format_bytes($response['zone_details']->MonthlyBandwidthLimit),
307 | 'MonthlyBandwidthUsed' => $bunny->format_bytes($response['zone_details']->MonthlyBandwidthUsed),
308 | 'OriginUrl' => $response['zone_details']->OriginUrl,
309 | 'Hostnames' => (array) $response['zone_details']->Hostnames,
310 | ),
311 | );
312 |
313 | }
314 | } catch (Exception $e) {
315 | logModuleCall(
316 | 'bunnycdn',
317 | __FUNCTION__,
318 | $params,
319 | $e->getMessage(),
320 | $e->getTraceAsString()
321 | );
322 |
323 | return array(
324 | 'tabOverviewReplacementTemplate' => 'error.tpl',
325 | 'templateVariables' => array(
326 | 'usefulErrorHelper' => $e->getMessage(),
327 | ),
328 | );
329 | }
330 | }
331 |
--------------------------------------------------------------------------------
/api.php:
--------------------------------------------------------------------------------
1 | api_key_account = $api_key_account;
19 | return $this;
20 | }
21 |
22 | protected $api_url = array
23 | (
24 | "zone" => "https://bunnycdn.com/api",
25 | 'storage' => 'https://storage.bunnycdn.com'
26 | );
27 |
28 | public function GetZoneList()
29 | {
30 | /*
31 | will get all of the zones for the account
32 | */
33 |
34 | if( !$this->api_key_account)
35 | {
36 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
37 | die();
38 | }
39 |
40 | $key = $this->api_key_account;
41 | $api_url = $this->api_url['zone'].'/pullzone';
42 |
43 | $get_header = $this->create_header($key);
44 |
45 | $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header , ) );
46 |
47 | if($api_call['http_code'] !=200)
48 | {
49 | //error message
50 | $request_array = json_decode(json_encode($api_call['data']));
51 | $result = array
52 | (
53 | "status" => 'error',
54 | "http_code"=>$api_call['http_code'],
55 | "msg" => json_decode($request_array)->Message ,
56 | );
57 | return $result;
58 | die();
59 | }
60 |
61 | $zone_data = json_decode($api_call['data']);
62 |
63 | $a1 = array();
64 |
65 | foreach ($zone_data as $k1 => $v1)
66 | {
67 | $arr_hostnames = array();
68 |
69 | //--->get all the hostnames > start
70 | if($v1->Hostnames)
71 | {
72 | foreach ($v1->Hostnames as $key => $v2)
73 | {
74 | array_push($arr_hostnames, $v2->Value);
75 | }
76 | }
77 | //--->get all the hostnames > end
78 |
79 | $d = array
80 | (
81 | "zone_id" => $v1->Id,
82 | "zone_name"=>$v1->Name,
83 | "monthly_bandwidth_used" =>$this->format_bytes($v1->MonthlyBandwidthUsed),
84 | "host_names" =>$arr_hostnames,
85 | );
86 | array_push($a1,$d);
87 | }
88 |
89 | return array('status' => 'success', 'zone_smry'=>$a1,"zone_details" => $zone_data);
90 |
91 | }
92 | public function GetZone($zone_id = '')
93 | {
94 | /*
95 | will get a user zone for the account
96 | */
97 | if( !$this->api_key_account)
98 | {
99 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
100 | die();
101 | }
102 |
103 | if(!$zone_id)
104 | {
105 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
106 | die();
107 | }
108 |
109 |
110 | $key = $this->api_key_account;
111 | $api_url = $this->api_url['zone'].'/pullzone/'.$zone_id;
112 |
113 | $get_header = $this->create_header($key);
114 | $post_data_array = array('id'=>$zone_id);
115 |
116 | $api_call = $this->run( array('call_method' => 'GET', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) );
117 |
118 |
119 | if($api_call['http_code'] !=200)
120 | {
121 | //error message
122 | $request_array = json_decode(json_encode($api_call['data']));
123 | $result = array
124 | (
125 | "status" => 'error',
126 | "http_code"=>$api_call['http_code'],
127 | "msg" => json_decode($request_array) ,
128 | );
129 | return $result;
130 | die();
131 | }
132 |
133 | $zone_data = json_decode($api_call['data']);
134 |
135 | $a1 = array();
136 | $arr_hostnames = array();
137 |
138 | //--->get all the hostnames > start
139 | if($zone_data->Hostnames)
140 | {
141 | foreach ($zone_data->Hostnames as $key => $v1)
142 | {
143 | array_push($arr_hostnames, $v1->Value);
144 | }
145 | }
146 | //--->get all the hostnames > end
147 |
148 | $d = array
149 | (
150 | "zone_id" => $zone_data->Id,
151 | "zone_name"=>$zone_data->Name,
152 | "monthly_bandwidth_used" =>$this->format_bytes($zone_data->MonthlyBandwidthUsed),
153 | "host_names" =>$arr_hostnames,
154 | );
155 | array_push($a1,$d);
156 |
157 | return array('status' => 'success', 'zone_smry'=>$a1,"zone_details" => $zone_data);
158 | die();
159 | }
160 |
161 |
162 | public function CreateNewZone($zone_name = '', $zone_url = '')
163 | {
164 | /*
165 | will create a new zone for the account
166 | */
167 |
168 | if( !$this->api_key_account)
169 | {
170 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
171 | die();
172 | }
173 |
174 | if(!$zone_name)
175 | {
176 | return array('status' =>'error' ,'code' =>'zone_name' ,'msg'=> 'missing zone name');
177 | die();
178 | }
179 |
180 | if(!$zone_url)
181 | {
182 | return array('status' =>'error' ,'code' =>'zone_url' ,'msg'=> 'missing zone url');
183 | die();
184 | }
185 |
186 | $key = $this->api_key_account;
187 | $api_url = $this->api_url['zone'].'/pullzone';
188 |
189 | $get_header = $this->create_header($key);
190 |
191 |
192 | $post_data_array = array('Name' => $zone_name, 'OriginUrl' => $zone_url);
193 |
194 | $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) );
195 |
196 | if($api_call['http_code'] !=201)
197 | {
198 | //error message
199 | $request_array = json_decode(json_encode($api_call['data']));
200 | $result = array
201 | (
202 | "status" => 'error',
203 | "http_code"=>$api_call['http_code'],
204 | "msg" => json_decode($request_array) ,
205 | );
206 | return $result;
207 | die();
208 | }
209 |
210 | //convert to php array for data parsing
211 | $zone_data = json_decode($api_call['data']);
212 |
213 | //--->get all the hostnames > start
214 | $cdnurl = '';
215 | if($zone_data->Hostnames)
216 | {
217 | foreach ($zone_data->Hostnames as $key => $v1)
218 | {
219 | $cdnurl = $v1->Value;
220 | }
221 | }
222 | //--->get all the hostnames > end
223 |
224 |
225 |
226 | return array
227 | (
228 | 'status' => 'success',
229 | "zone_id" => $zone_data->Id,
230 | "zone_name"=>$zone_data->Name,
231 | "origin_url"=>$zone_data->OriginUrl,
232 | "cdn_url"=>$cdnurl,
233 | "zone_details" => $zone_data
234 | );
235 | die();
236 | }
237 |
238 |
239 | public function DeleteZone($zone_id= '')
240 | {
241 | /*
242 | will delete a zone for the account
243 | */
244 |
245 | if( !$this->api_key_account)
246 | {
247 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
248 | die();
249 | }
250 |
251 | if(!$zone_id)
252 | {
253 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
254 | die();
255 | }
256 |
257 |
258 | $key = $this->api_key_account;
259 | $api_url = $this->api_url['zone'].'/pullzone/'. $zone_id;
260 |
261 | $get_header = $this->create_header($key);
262 |
263 | $api_call = $this->run( array('call_method' => 'DELETE', 'api_url' => $api_url,'header' => $get_header , ) );
264 |
265 |
266 | if($api_call['http_code'] !=200 && $api_call['http_code'] !=302)
267 | {
268 | //error message
269 | $request_array = json_decode(json_encode($api_call['data']));
270 | $result = array
271 | (
272 | "status" => 'error',
273 | "http_code"=>$api_call['http_code'],
274 | "msg" => json_decode($request_array) ,
275 | );
276 | return $result;
277 | die();
278 | }
279 |
280 | return array(
281 | 'status' => 'success',
282 | "msg" => $api_call,
283 |
284 | );
285 | //return $api_call;
286 | die();
287 | }
288 |
289 | // Custom function added by @jetchirag
290 |
291 | public function UpdateZone($zone_id='', $updateFields){
292 | if( !$this->api_key_account)
293 | {
294 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
295 | die();
296 | }
297 |
298 | if(!$zone_id)
299 | {
300 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
301 | die();
302 | }
303 |
304 | $zoneDetails = $this->GetZone($zone_id);
305 |
306 | if ($zoneDetails['status'] != 'success') {
307 | return array('status' =>'error' ,'msg'=> 'Unable to get data for zone');
308 | die();
309 | }
310 |
311 | $zoneDetails = $zoneDetails['zone_details'];
312 |
313 | $post_data_array = array_merge((array)$zoneDetails, (array)$updateFields);
314 |
315 | $key = $this->api_key_account;
316 | $api_url = $this->api_url['zone'].'/pullzone/'. $zone_id;
317 |
318 | $get_header = $this->create_header($key);
319 |
320 | $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) );
321 |
322 |
323 | if($api_call['http_code'] !=200 && $api_call['http_code'] !=302)
324 | {
325 | //error message
326 | $request_array = json_decode(json_encode($api_call['data']));
327 | $result = array
328 | (
329 | "status" => 'error',
330 | "http_code"=>$api_call['http_code'],
331 | "msg" => json_decode($request_array) ,
332 | );
333 | return $result;
334 | die();
335 | }
336 |
337 | return array(
338 | 'status' => 'success',
339 | "msg" => $api_call,
340 |
341 | );
342 | //return $api_call;
343 | die();
344 | }
345 |
346 | public function PurgeZoneCache($zone_id= '')
347 | {
348 | /*
349 | will purge cache for the whole zone
350 | */
351 |
352 | if( !$this->api_key_account)
353 | {
354 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
355 | die();
356 | }
357 |
358 | if(!$zone_id)
359 | {
360 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
361 | die();
362 | }
363 |
364 |
365 | $key = $this->api_key_account;
366 | $api_url = $this->api_url['zone'].'/pullzone/'. $zone_id.'/purgeCache';
367 |
368 | $get_header = $this->create_header($key);
369 |
370 |
371 | $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , ) );
372 |
373 |
374 | if($api_call['http_code'] !=200 )
375 | {
376 | //error message
377 | $request_array = json_decode(json_encode($api_call['data']));
378 | $result = array
379 | (
380 | "status" => 'error',
381 | "http_code"=>$api_call['http_code'],
382 | "msg" => json_decode($request_array) ,
383 | );
384 | return $result;
385 | die();
386 | }
387 |
388 | return array(
389 | 'status' => 'success',
390 | "msg" => $api_call,
391 | );
392 | die();
393 | }
394 |
395 |
396 | public function AddHostName($zone_id = '', $host_name_url = '')
397 | {
398 | /*
399 | will add a host name for the zone
400 | */
401 |
402 | if( !$this->api_key_account)
403 | {
404 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
405 | die();
406 | }
407 |
408 | if(!$zone_id)
409 | {
410 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
411 | die();
412 | }
413 |
414 | if(!$host_name_url)
415 | {
416 | return array('status' =>'error' ,'code' =>'host_name_url' ,'msg'=> 'missing host name url');
417 | die();
418 | }
419 |
420 | $key = $this->api_key_account;
421 | $api_url = $this->api_url['zone'].'/pullzone/addHostname';
422 |
423 | $get_header = $this->create_header($key);
424 |
425 |
426 | $post_data_array = array('PullZoneId' => $zone_id, 'Hostname' => $host_name_url);
427 |
428 | $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) );
429 |
430 | if($api_call['http_code'] !=200 )
431 | {
432 | //error message
433 | $request_array = json_decode(json_encode($api_call['data']));
434 | $result = array
435 | (
436 | "status" => 'error',
437 | "http_code"=>$api_call['http_code'],
438 | "msg" => json_decode($request_array) ,
439 | );
440 | return $result;
441 | die();
442 | }
443 |
444 | return array(
445 | 'status' => 'success',
446 | "msg" => $api_call,
447 | );
448 | die();
449 | }
450 |
451 |
452 | public function DeleteHostName($zone_id = '', $host_name_url = '')
453 | {
454 | /*
455 | will delete a host name for the zone
456 | */
457 |
458 | if( !$this->api_key_account)
459 | {
460 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
461 | die();
462 | }
463 |
464 | if(!$zone_id)
465 | {
466 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
467 | die();
468 | }
469 |
470 | if(!$host_name_url)
471 | {
472 | return array('status' =>'error' ,'code' =>'host_name_url' ,'msg'=> 'missing host name url');
473 | die();
474 | }
475 |
476 | $key = $this->api_key_account;
477 | $api_url = $this->api_url['zone'].'/pullzone/deleteHostname?id='.$zone_id.'&hostname='.$host_name_url ;
478 |
479 | $get_header = $this->create_header($key);
480 |
481 |
482 | $api_call = $this->run( array('call_method' => 'DELETE', 'api_url' => $api_url,'header' => $get_header , ) );
483 |
484 | if($api_call['http_code'] !=200 )
485 | {
486 | //error message
487 | $request_array = json_decode(json_encode($api_call['data']));
488 | $result = array
489 | (
490 | "status" => 'error',
491 | "http_code"=>$api_call['http_code'],
492 | "msg" => json_decode($request_array) ,
493 | );
494 | return $result;
495 | die();
496 | }
497 |
498 | return array(
499 | 'status' => 'success',
500 | "msg" => $api_call,
501 | );
502 | die();
503 | }
504 |
505 | public function AddBlockedIP($zone_id = '', $blocked_ip = '')
506 | {
507 | /*
508 | will add a blocked ip for the zone
509 | */
510 |
511 | if( !$this->api_key_account)
512 | {
513 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
514 | die();
515 | }
516 |
517 | if(!$zone_id)
518 | {
519 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
520 | die();
521 | }
522 |
523 | if(!$blocked_ip)
524 | {
525 | return array('status' =>'error' ,'code' =>'blocked_ip' ,'msg'=> 'missing blocked ip');
526 | die();
527 | }
528 |
529 | $key = $this->api_key_account;
530 | $api_url = $this->api_url['zone'].'/pullzone/addBlockedIp' ;
531 |
532 | $get_header = $this->create_header($key);
533 |
534 |
535 | $post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip);
536 |
537 | $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) );
538 |
539 | if($api_call['http_code'] !=200 )
540 | {
541 | //error message
542 | $request_array = json_decode(json_encode($api_call['data']));
543 | $result = array
544 | (
545 | "status" => 'error',
546 | "http_code"=>$api_call['http_code'],
547 | "msg" => json_decode($request_array) ,
548 | );
549 | return $result;
550 | die();
551 | }
552 |
553 | return array(
554 | 'status' => 'success',
555 | "msg" => $api_call,
556 | );
557 | die();
558 | }
559 |
560 |
561 | public function RemoveBlockedIP($zone_id = '', $blocked_ip = '')
562 | {
563 | /*
564 | will remove a blocked ip for the zone
565 | */
566 |
567 | if( !$this->api_key_account)
568 | {
569 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
570 | die();
571 | }
572 |
573 | if(!$zone_id)
574 | {
575 | return array('status' =>'error' ,'code' =>'zone_id' ,'msg'=> 'missing zone id');
576 | die();
577 | }
578 |
579 | if(!$blocked_ip)
580 | {
581 | return array('status' =>'error' ,'code' =>'blocked_ip' ,'msg'=> 'missing blocked ip');
582 | die();
583 | }
584 |
585 | $key = $this->api_key_account;
586 | $api_url = $this->api_url['zone'].'/pullzone/removeBlockedIp' ;
587 |
588 | $get_header = $this->create_header($key);
589 |
590 |
591 | $post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip);
592 |
593 | $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , 'post_data_array'=>$post_data_array) );
594 |
595 | if($api_call['http_code'] !=200 )
596 | {
597 | //error message
598 | $request_array = json_decode(json_encode($api_call['data']));
599 | $result = array
600 | (
601 | "status" => 'error',
602 | "http_code"=>$api_call['http_code'],
603 | "msg" => json_decode($request_array) ,
604 | );
605 | return $result;
606 | die();
607 | }
608 |
609 | return array(
610 | 'status' => 'success',
611 | "msg" => $api_call,
612 | );
613 | die();
614 | }
615 |
616 | public function PurgeURL($url = '')
617 | {
618 | /*
619 | will purge a url for the account
620 | */
621 |
622 | if( !$this->api_key_account)
623 | {
624 | return array('status' =>'error' ,'code' =>'api_key_account' ,'msg'=> 'missing acount api key');
625 | die();
626 | }
627 |
628 | if(!$url)
629 | {
630 | return array('status' =>'error' ,'code' =>'url' ,'msg'=> 'missing url');
631 | die();
632 | }
633 |
634 | $key = $this->api_key_account;
635 | $api_url = $this->api_url['zone'].'/purge?url='.$url ;
636 |
637 | $get_header = $this->create_header($key);
638 |
639 |
640 | //$post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip);
641 |
642 | $api_call = $this->run( array('call_method' => 'POST', 'api_url' => $api_url,'header' => $get_header , ));
643 |
644 | if($api_call['http_code'] !=200 )
645 | {
646 | //error message
647 | $request_array = json_decode(json_encode($api_call['data']));
648 | $result = array
649 | (
650 | "status" => 'error',
651 | "http_code"=>$api_call['http_code'],
652 | "msg" => json_decode($request_array) ,
653 | );
654 | return $result;
655 | die();
656 | }
657 |
658 | return array(
659 | 'status' => 'success',
660 | "msg" => $api_call,
661 | );
662 | die();
663 | }
664 |
665 | //--->process functions > start
666 |
667 | private function create_header($api_key)
668 | {
669 | $header = array('Content-Type:application/json','accesskey:'.$api_key.'' );
670 | return $header;
671 | }
672 |
673 | private function run($call_arr = array('call_method' => 'GET', 'api_url' => 'api_url','header' => array(),'post_data_array' => array() , ) )
674 | {
675 | $call_method = isset($call_arr['call_method']) ? $call_arr['call_method'] : 'GET' ;
676 | $api_url = isset($call_arr['api_url']) ? $call_arr['api_url'] : 'api_url' ;
677 | $header = isset($call_arr['header']) ? $call_arr['header'] : '' ;
678 | $post_data_array = isset($call_arr['post_data_array']) ? $call_arr['post_data_array'] : '' ;
679 |
680 |
681 | $post_data = json_encode($post_data_array);
682 |
683 | $curl = curl_init($api_url);
684 |
685 | curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
686 |
687 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $call_method);
688 |
689 | curl_setopt($curl, CURLOPT_URL, $api_url);
690 |
691 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
692 |
693 | curl_setopt($curl, CURLOPT_POST, 1);
694 | curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
695 |
696 | $result = curl_exec($curl);
697 | $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
698 |
699 | curl_close($curl);
700 |
701 |
702 | //For error checking
703 | if ( $result === false )
704 | {
705 | return array('status' =>'error' ,'code'=> 'curl_error', 'result' => curl_error($curl) ,);
706 | die();
707 | }
708 |
709 | return array('http_code'=> $http_code, 'data' => $result,);
710 | }
711 | //--->process functions > end
712 |
713 | //--->private functions > start
714 |
715 | public function format_bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE)
716 | {
717 | // Format string
718 | $format = ($format === NULL) ? '%01.2f %s' : (string) $format;
719 |
720 | // IEC prefixes (binary)
721 | if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE)
722 | {
723 | $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
724 | $mod = 1024;
725 | }
726 | // SI prefixes (decimal)
727 | else
728 | {
729 | $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
730 | $mod = 1000;
731 | }
732 | // Determine unit to use
733 | if (($power = array_search((string) $force_unit, $units)) === FALSE)
734 | {
735 | $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
736 | }
737 | return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
738 | }
739 |
740 | private function fix_url($url ='')
741 | {
742 | return str_replace("\\", "/", $url );
743 | }
744 |
745 | private function seo_file_name($file_name)
746 | {
747 | /*
748 | will convert file name into seo url file name
749 |
750 | i.e.
751 | $file_name = 'code with mark !@#$%^*()_+~ $$%& _03e05 122-9****.mp4';
752 |
753 | //output will be
754 | code-with-mark-03e05-122-9.mp4
755 |
756 | Note only use this for file names and not for folder names!!!
757 |
758 | */
759 |
760 | $path_info = pathinfo($file_name);
761 | $info_dir_name = preg_replace("/[\s]/", "-", strtolower($path_info['dirname']) );
762 |
763 |
764 | $info_file_name = $path_info['filename'];
765 | $info_file_ext = $path_info['extension'];
766 |
767 | $string = $info_file_name ;
768 |
769 | $src = 'àáâãäçèéêëìíîïñòóôõöøùúûüýÿßÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ';
770 | $rep = 'aaaaaceeeeiiiinoooooouuuuyysAAAAACEEEEIIIINOOOOOOUUUUY';
771 | // strip off accents (assuming utf8 PHP - note strtr() requires single-byte)
772 | $string = strtr(utf8_decode($string), utf8_decode($src), $rep);
773 | // convert to lower case
774 | $string = strtolower($string);
775 | // strip all but alphanumeric, whitespace, dot, underscore, hyphen
776 | $string = preg_replace("/[^a-z0-9\s._-]/", "", $string);
777 | // merge multiple consecutive whitespaces, dots, underscores, hyphens
778 | $string = preg_replace("/[\s._-]+/", " ", $string);
779 | // convert whitespaces to hyphens
780 | $string = preg_replace("/[\s]/", "-", $string);
781 |
782 |
783 | if(substr($info_dir_name,1))
784 | {
785 | $file_path = $info_dir_name."/".$string.'.'.$info_file_ext;
786 | }
787 | else
788 | {
789 | $file_path = "/". $string.'.'.$info_file_ext;
790 | }
791 |
792 | return $file_path;
793 | }
794 |
795 |
796 | //--->private functions > end
797 | }
798 |
--------------------------------------------------------------------------------