BunnyCDN.php
1700 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Libraries; |
| 4 | |
| 5 | class BunnyCDN |
| 6 | { |
| 7 | private $api_key_account; |
| 8 | private $api_key_storage; |
| 9 | |
| 10 | protected $api_url = array( |
| 11 | "zone" => "https://bunnycdn.com/api", |
| 12 | 'storage' => 'https://storage.bunnycdn.com' |
| 13 | ); |
| 14 | |
| 15 | |
| 16 | //--->account > start |
| 17 | |
| 18 | public function Account($api_key_account = '') |
| 19 | { |
| 20 | if (!$api_key_account) { |
| 21 | return array('status' => 'error', 'code' => 'missing_api_key_account', 'msg' => 'missing api key account'); |
| 22 | die(); |
| 23 | } |
| 24 | $this->api_key_account = $api_key_account; |
| 25 | return $this; |
| 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 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 36 | die(); |
| 37 | } |
| 38 | |
| 39 | $key = $this->api_key_account; |
| 40 | $api_url = $this->api_url['zone'] . '/pullzone'; |
| 41 | |
| 42 | $get_header = $this->create_header($key); |
| 43 | |
| 44 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header)); |
| 45 | |
| 46 | if ($api_call['http_code'] != 200) { |
| 47 | //error message |
| 48 | $request_array = json_decode(json_encode($api_call['data'])); |
| 49 | $result = array( |
| 50 | "status" => 'error', |
| 51 | "http_code" => $api_call['http_code'], |
| 52 | "msg" => json_decode($request_array)->Message, |
| 53 | ); |
| 54 | return $result; |
| 55 | die(); |
| 56 | } |
| 57 | |
| 58 | $zone_data = json_decode($api_call['data']); |
| 59 | |
| 60 | $a1 = array(); |
| 61 | |
| 62 | foreach ($zone_data as $k1 => $v1) { |
| 63 | $arr_hostnames = array(); |
| 64 | |
| 65 | //--->get all the hostnames > start |
| 66 | if ($v1->Hostnames) { |
| 67 | foreach ($v1->Hostnames as $key => $v2) { |
| 68 | array_push($arr_hostnames, $v2->Value); |
| 69 | } |
| 70 | } |
| 71 | //--->get all the hostnames > end |
| 72 | |
| 73 | $d = array( |
| 74 | "zone_id" => $v1->Id, |
| 75 | "zone_name" => $v1->Name, |
| 76 | "monthly_bandwidth_used" => $this->format_bytes($v1->MonthlyBandwidthUsed), |
| 77 | "host_names" => $arr_hostnames, |
| 78 | 'security_key' => $v1->ZoneSecurityKey |
| 79 | ); |
| 80 | array_push($a1, $d); |
| 81 | } |
| 82 | |
| 83 | return array('status' => 'success', 'zone_smry' => $a1, "zone_details" => $zone_data); |
| 84 | } |
| 85 | |
| 86 | public function GetZone($zone_id = '') |
| 87 | { |
| 88 | /* |
| 89 | will get a user zone for the account |
| 90 | */ |
| 91 | if (!$this->api_key_account) { |
| 92 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 93 | die(); |
| 94 | } |
| 95 | |
| 96 | if (!$zone_id) { |
| 97 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 98 | die(); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | $key = $this->api_key_account; |
| 103 | $api_url = $this->api_url['zone'] . '/pullzone/' . $zone_id; |
| 104 | |
| 105 | $get_header = $this->create_header($key); |
| 106 | $post_data_array = array('id' => $zone_id); |
| 107 | |
| 108 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header, 'post_data_array' => $post_data_array)); |
| 109 | |
| 110 | |
| 111 | if ($api_call['http_code'] != 200) { |
| 112 | //error message |
| 113 | $request_array = json_decode(json_encode($api_call['data'])); |
| 114 | $result = array( |
| 115 | "status" => 'error', |
| 116 | "http_code" => $api_call['http_code'], |
| 117 | "msg" => json_decode($request_array), |
| 118 | ); |
| 119 | return $result; |
| 120 | die(); |
| 121 | } |
| 122 | |
| 123 | $zone_data = json_decode($api_call['data']); |
| 124 | |
| 125 | $a1 = array(); |
| 126 | $arr_hostnames = array(); |
| 127 | |
| 128 | //--->get all the hostnames > start |
| 129 | if ($zone_data->Hostnames) { |
| 130 | foreach ($zone_data->Hostnames as $key => $v1) { |
| 131 | array_push($arr_hostnames, $v1->Value); |
| 132 | } |
| 133 | } |
| 134 | //--->get all the hostnames > end |
| 135 | |
| 136 | $d = array( |
| 137 | "zone_id" => $zone_data->Id, |
| 138 | "zone_name" => $zone_data->Name, |
| 139 | "monthly_bandwidth_used" => $this->format_bytes($zone_data->MonthlyBandwidthUsed), |
| 140 | "host_names" => $arr_hostnames, |
| 141 | ); |
| 142 | array_push($a1, $d); |
| 143 | |
| 144 | return array('status' => 'success', 'zone_smry' => $a1, "zone_details" => $zone_data); |
| 145 | die(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * We'll default to high volume pricing |
| 150 | */ |
| 151 | public function CreateNewZone($zone_name = '', $zone_url = '', $type = 1) |
| 152 | { |
| 153 | /* |
| 154 | will create a new zone for the account |
| 155 | */ |
| 156 | |
| 157 | if (!$this->api_key_account) { |
| 158 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 159 | die(); |
| 160 | } |
| 161 | |
| 162 | if (!$zone_name) { |
| 163 | return array('status' => 'error', 'code' => 'zone_name', 'msg' => 'missing zone name'); |
| 164 | die(); |
| 165 | } |
| 166 | |
| 167 | if (!$zone_url) { |
| 168 | return array('status' => 'error', 'code' => 'zone_url', 'msg' => 'missing zone url'); |
| 169 | die(); |
| 170 | } |
| 171 | |
| 172 | $key = $this->api_key_account; |
| 173 | $api_url = $this->api_url['zone'] . '/pullzone'; |
| 174 | |
| 175 | $get_header = $this->create_header($key); |
| 176 | |
| 177 | $post_data_array = array('Name' => $zone_name, 'OriginUrl' => $zone_url, 'Type' => $type); |
| 178 | |
| 179 | $api_call = $this->run(array('call_method' => 'POST', 'api_url' => $api_url, 'header' => $get_header, 'post_data_array' => $post_data_array)); |
| 180 | |
| 181 | if ($api_call['http_code'] != 201) { |
| 182 | //error message |
| 183 | $request_array = json_decode(json_encode($api_call['data'])); |
| 184 | $result = array( |
| 185 | "status" => 'error', |
| 186 | "http_code" => $api_call['http_code'], |
| 187 | "msg" => json_decode($request_array), |
| 188 | ); |
| 189 | return $result; |
| 190 | die(); |
| 191 | } |
| 192 | |
| 193 | //convert to php array for data parsing |
| 194 | $zone_data = json_decode($api_call['data']); |
| 195 | |
| 196 | //--->get all the hostnames > start |
| 197 | $cdnurl = ''; |
| 198 | if ($zone_data->Hostnames) { |
| 199 | foreach ($zone_data->Hostnames as $key => $v1) { |
| 200 | $cdnurl = $v1->Value; |
| 201 | } |
| 202 | } |
| 203 | //--->get all the hostnames > end |
| 204 | |
| 205 | |
| 206 | |
| 207 | return array( |
| 208 | 'status' => 'success', |
| 209 | "zone_id" => $zone_data->Id, |
| 210 | "zone_name" => $zone_data->Name, |
| 211 | "origin_url" => $zone_data->OriginUrl, |
| 212 | "cdn_url" => $cdnurl, |
| 213 | "zone_details" => $zone_data |
| 214 | ); |
| 215 | die(); |
| 216 | } |
| 217 | |
| 218 | public function UpdateZone($zone_id = '', $data) |
| 219 | { |
| 220 | if (!$this->api_key_account) { |
| 221 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 222 | die(); |
| 223 | } |
| 224 | |
| 225 | if (!$zone_id) { |
| 226 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 227 | die(); |
| 228 | } |
| 229 | |
| 230 | $key = $this->api_key_account; |
| 231 | $api_url = $this->api_url['zone'] . '/pullzone/' . $zone_id; |
| 232 | |
| 233 | $get_header = $this->create_header($key); |
| 234 | |
| 235 | $api_call = $this->run(array('call_method' => 'POST', 'api_url' => $api_url, 'header' => $get_header, 'post_data_array' => (array) $data)); |
| 236 | |
| 237 | if ($api_call['http_code'] != 200) { |
| 238 | //error message |
| 239 | $request_array = json_decode(json_encode($api_call['data'])); |
| 240 | $result = array( |
| 241 | "status" => 'error', |
| 242 | "http_code" => $api_call['http_code'], |
| 243 | "msg" => json_decode($request_array), |
| 244 | ); |
| 245 | return $result; |
| 246 | die(); |
| 247 | } |
| 248 | |
| 249 | //convert to php array for data parsing |
| 250 | $zone_data = json_decode($api_call['data']); |
| 251 | |
| 252 | //--->get all the hostnames > start |
| 253 | $cdnurl = ''; |
| 254 | if ($zone_data->Hostnames) { |
| 255 | foreach ($zone_data->Hostnames as $key => $v1) { |
| 256 | $cdnurl = $v1->Value; |
| 257 | } |
| 258 | } |
| 259 | //--->get all the hostnames > end |
| 260 | return array( |
| 261 | 'status' => 'success', |
| 262 | "zone_id" => $zone_data->Id, |
| 263 | "zone_name" => $zone_data->Name, |
| 264 | "origin_url" => $zone_data->OriginUrl, |
| 265 | "cdn_url" => $cdnurl, |
| 266 | "zone_details" => $zone_data |
| 267 | ); |
| 268 | die(); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | public function DeleteZone($zone_id = '') |
| 273 | { |
| 274 | /* |
| 275 | will delete a zone for the account |
| 276 | */ |
| 277 | |
| 278 | if (!$this->api_key_account) { |
| 279 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 280 | die(); |
| 281 | } |
| 282 | |
| 283 | if (!$zone_id) { |
| 284 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 285 | die(); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | $key = $this->api_key_account; |
| 290 | $api_url = $this->api_url['zone'] . '/pullzone/' . $zone_id; |
| 291 | |
| 292 | $get_header = $this->create_header($key); |
| 293 | |
| 294 | $api_call = $this->run(array('call_method' => 'DELETE', 'api_url' => $api_url, 'header' => $get_header,)); |
| 295 | |
| 296 | |
| 297 | if ($api_call['http_code'] != 200 && $api_call['http_code'] != 302) { |
| 298 | //error message |
| 299 | $request_array = json_decode(json_encode($api_call['data'])); |
| 300 | $result = array( |
| 301 | "status" => 'error', |
| 302 | "http_code" => $api_call['http_code'], |
| 303 | "msg" => json_decode($request_array), |
| 304 | ); |
| 305 | return $result; |
| 306 | die(); |
| 307 | } |
| 308 | |
| 309 | return array( |
| 310 | 'status' => 'success', |
| 311 | "msg" => $api_call, |
| 312 | |
| 313 | ); |
| 314 | //return $api_call; |
| 315 | die(); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | |
| 320 | public function PurgeZoneCache($zone_id = '') |
| 321 | { |
| 322 | /* |
| 323 | will purge cache for the whole zone |
| 324 | */ |
| 325 | |
| 326 | if (!$this->api_key_account) { |
| 327 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 328 | die(); |
| 329 | } |
| 330 | |
| 331 | if (!$zone_id) { |
| 332 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 333 | die(); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | $key = $this->api_key_account; |
| 338 | $api_url = $this->api_url['zone'] . '/pullzone/' . $zone_id . '/purgeCache'; |
| 339 | |
| 340 | $get_header = $this->create_header($key); |
| 341 | |
| 342 | |
| 343 | $api_call = $this->run(array('call_method' => 'POST', 'api_url' => $api_url, 'header' => $get_header,)); |
| 344 | |
| 345 | |
| 346 | if ($api_call['http_code'] != 200) { |
| 347 | //error message |
| 348 | $request_array = json_decode(json_encode($api_call['data'])); |
| 349 | $result = array( |
| 350 | "status" => 'error', |
| 351 | "http_code" => $api_call['http_code'], |
| 352 | "msg" => json_decode($request_array), |
| 353 | ); |
| 354 | return $result; |
| 355 | die(); |
| 356 | } |
| 357 | |
| 358 | return array( |
| 359 | 'status' => 'success', |
| 360 | "msg" => $api_call, |
| 361 | ); |
| 362 | die(); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | public function AddHostName($zone_id = '', $host_name_url = '') |
| 367 | { |
| 368 | /* |
| 369 | will add a host name for the zone |
| 370 | */ |
| 371 | |
| 372 | if (!$this->api_key_account) { |
| 373 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 374 | die(); |
| 375 | } |
| 376 | |
| 377 | if (!$zone_id) { |
| 378 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 379 | die(); |
| 380 | } |
| 381 | |
| 382 | if (!$host_name_url) { |
| 383 | return array('status' => 'error', 'code' => 'host_name_url', 'msg' => 'missing host name url'); |
| 384 | die(); |
| 385 | } |
| 386 | |
| 387 | $key = $this->api_key_account; |
| 388 | $api_url = $this->api_url['zone'] . '/pullzone/addHostname'; |
| 389 | |
| 390 | $get_header = $this->create_header($key); |
| 391 | |
| 392 | |
| 393 | $post_data_array = array('PullZoneId' => $zone_id, 'Hostname' => $host_name_url); |
| 394 | |
| 395 | $api_call = $this->run(array('call_method' => 'POST', 'api_url' => $api_url, 'header' => $get_header, 'post_data_array' => $post_data_array)); |
| 396 | |
| 397 | if ($api_call['http_code'] != 200) { |
| 398 | //error message |
| 399 | $request_array = json_decode(json_encode($api_call['data'])); |
| 400 | $result = array( |
| 401 | "status" => 'error', |
| 402 | "http_code" => $api_call['http_code'], |
| 403 | "msg" => json_decode($request_array), |
| 404 | ); |
| 405 | return $result; |
| 406 | die(); |
| 407 | } |
| 408 | |
| 409 | return array( |
| 410 | 'status' => 'success', |
| 411 | "msg" => $api_call, |
| 412 | ); |
| 413 | die(); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | public function DeleteHostName($zone_id = '', $host_name_url = '') |
| 418 | { |
| 419 | /* |
| 420 | will delete a host name for the zone |
| 421 | */ |
| 422 | |
| 423 | if (!$this->api_key_account) { |
| 424 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 425 | die(); |
| 426 | } |
| 427 | |
| 428 | if (!$zone_id) { |
| 429 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 430 | die(); |
| 431 | } |
| 432 | |
| 433 | if (!$host_name_url) { |
| 434 | return array('status' => 'error', 'code' => 'host_name_url', 'msg' => 'missing host name url'); |
| 435 | die(); |
| 436 | } |
| 437 | |
| 438 | $key = $this->api_key_account; |
| 439 | $api_url = $this->api_url['zone'] . '/pullzone/deleteHostname?id=' . $zone_id . '&hostname=' . $host_name_url; |
| 440 | |
| 441 | $get_header = $this->create_header($key); |
| 442 | |
| 443 | |
| 444 | $api_call = $this->run(array('call_method' => 'DELETE', 'api_url' => $api_url, 'header' => $get_header,)); |
| 445 | |
| 446 | if ($api_call['http_code'] != 200) { |
| 447 | //error message |
| 448 | $request_array = json_decode(json_encode($api_call['data'])); |
| 449 | $result = array( |
| 450 | "status" => 'error', |
| 451 | "http_code" => $api_call['http_code'], |
| 452 | "msg" => json_decode($request_array), |
| 453 | ); |
| 454 | return $result; |
| 455 | die(); |
| 456 | } |
| 457 | |
| 458 | return array( |
| 459 | 'status' => 'success', |
| 460 | "msg" => $api_call, |
| 461 | ); |
| 462 | die(); |
| 463 | } |
| 464 | |
| 465 | public function AddBlockedIP($zone_id = '', $blocked_ip = '') |
| 466 | { |
| 467 | /* |
| 468 | will add a blocked ip for the zone |
| 469 | */ |
| 470 | |
| 471 | if (!$this->api_key_account) { |
| 472 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 473 | die(); |
| 474 | } |
| 475 | |
| 476 | if (!$zone_id) { |
| 477 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 478 | die(); |
| 479 | } |
| 480 | |
| 481 | if (!$blocked_ip) { |
| 482 | return array('status' => 'error', 'code' => 'blocked_ip', 'msg' => 'missing blocked ip'); |
| 483 | die(); |
| 484 | } |
| 485 | |
| 486 | $key = $this->api_key_account; |
| 487 | $api_url = $this->api_url['zone'] . '/pullzone/addBlockedIp'; |
| 488 | |
| 489 | $get_header = $this->create_header($key); |
| 490 | |
| 491 | |
| 492 | $post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip); |
| 493 | |
| 494 | $api_call = $this->run(array('call_method' => 'POST', 'api_url' => $api_url, 'header' => $get_header, 'post_data_array' => $post_data_array)); |
| 495 | |
| 496 | if ($api_call['http_code'] != 200) { |
| 497 | //error message |
| 498 | $request_array = json_decode(json_encode($api_call['data'])); |
| 499 | $result = array( |
| 500 | "status" => 'error', |
| 501 | "http_code" => $api_call['http_code'], |
| 502 | "msg" => json_decode($request_array), |
| 503 | ); |
| 504 | return $result; |
| 505 | die(); |
| 506 | } |
| 507 | |
| 508 | return array( |
| 509 | 'status' => 'success', |
| 510 | "msg" => $api_call, |
| 511 | ); |
| 512 | die(); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | public function RemoveBlockedIP($zone_id = '', $blocked_ip = '') |
| 517 | { |
| 518 | /* |
| 519 | will remove a blocked ip for the zone |
| 520 | */ |
| 521 | |
| 522 | if (!$this->api_key_account) { |
| 523 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 524 | die(); |
| 525 | } |
| 526 | |
| 527 | if (!$zone_id) { |
| 528 | return array('status' => 'error', 'code' => 'zone_id', 'msg' => 'missing zone id'); |
| 529 | die(); |
| 530 | } |
| 531 | |
| 532 | if (!$blocked_ip) { |
| 533 | return array('status' => 'error', 'code' => 'blocked_ip', 'msg' => 'missing blocked ip'); |
| 534 | die(); |
| 535 | } |
| 536 | |
| 537 | $key = $this->api_key_account; |
| 538 | $api_url = $this->api_url['zone'] . '/pullzone/removeBlockedIp'; |
| 539 | |
| 540 | $get_header = $this->create_header($key); |
| 541 | |
| 542 | |
| 543 | $post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip); |
| 544 | |
| 545 | $api_call = $this->run(array('call_method' => 'POST', 'api_url' => $api_url, 'header' => $get_header, 'post_data_array' => $post_data_array)); |
| 546 | |
| 547 | if ($api_call['http_code'] != 200) { |
| 548 | //error message |
| 549 | $request_array = json_decode(json_encode($api_call['data'])); |
| 550 | $result = array( |
| 551 | "status" => 'error', |
| 552 | "http_code" => $api_call['http_code'], |
| 553 | "msg" => json_decode($request_array), |
| 554 | ); |
| 555 | return $result; |
| 556 | die(); |
| 557 | } |
| 558 | |
| 559 | return array( |
| 560 | 'status' => 'success', |
| 561 | "msg" => $api_call, |
| 562 | ); |
| 563 | die(); |
| 564 | } |
| 565 | |
| 566 | public function PurgeURL($url = '') |
| 567 | { |
| 568 | /* |
| 569 | will purge a url for the account |
| 570 | */ |
| 571 | |
| 572 | if (!$this->api_key_account) { |
| 573 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 574 | die(); |
| 575 | } |
| 576 | |
| 577 | if (!$url) { |
| 578 | return array('status' => 'error', 'code' => 'url', 'msg' => 'missing url'); |
| 579 | die(); |
| 580 | } |
| 581 | |
| 582 | $key = $this->api_key_account; |
| 583 | $api_url = $this->api_url['zone'] . '/purge?url=' . $url; |
| 584 | |
| 585 | $get_header = $this->create_header($key); |
| 586 | |
| 587 | |
| 588 | //$post_data_array = array('PullZoneId' => $zone_id, 'BlockedIp' => $blocked_ip); |
| 589 | |
| 590 | $api_call = $this->run(array('call_method' => 'POST', 'api_url' => $api_url, 'header' => $get_header,)); |
| 591 | |
| 592 | if ($api_call['http_code'] != 200) { |
| 593 | //error message |
| 594 | $request_array = json_decode(json_encode($api_call['data'])); |
| 595 | $result = array( |
| 596 | "status" => 'error', |
| 597 | "http_code" => $api_call['http_code'], |
| 598 | "msg" => json_decode($request_array), |
| 599 | ); |
| 600 | return $result; |
| 601 | die(); |
| 602 | } |
| 603 | |
| 604 | return array( |
| 605 | 'status' => 'success', |
| 606 | "msg" => $api_call, |
| 607 | ); |
| 608 | die(); |
| 609 | } |
| 610 | |
| 611 | public function Stats() |
| 612 | { |
| 613 | /* |
| 614 | will get all the statistics for the account |
| 615 | */ |
| 616 | |
| 617 | if (!$this->api_key_account) { |
| 618 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 619 | die(); |
| 620 | } |
| 621 | |
| 622 | $key = $this->api_key_account; |
| 623 | $api_url = $this->api_url['zone'] . '/statistics'; |
| 624 | |
| 625 | $get_header = $this->create_header($key); |
| 626 | |
| 627 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header,)); |
| 628 | |
| 629 | if ($api_call['http_code'] != 200) { |
| 630 | //error message |
| 631 | $request_array = json_decode(json_encode($api_call['data'])); |
| 632 | $result = array( |
| 633 | "status" => 'error', |
| 634 | "http_code" => $api_call['http_code'], |
| 635 | "msg" => json_decode($request_array), |
| 636 | ); |
| 637 | return $result; |
| 638 | die(); |
| 639 | } |
| 640 | |
| 641 | return array( |
| 642 | 'status' => 'success', |
| 643 | "msg" => json_decode(($api_call['data'])), |
| 644 | ); |
| 645 | die(); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | public function Billing() |
| 652 | { |
| 653 | /* |
| 654 | will get the billing information for the account |
| 655 | */ |
| 656 | |
| 657 | if (!$this->api_key_account) { |
| 658 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 659 | die(); |
| 660 | } |
| 661 | |
| 662 | $key = $this->api_key_account; |
| 663 | $api_url = $this->api_url['zone'] . '/billing'; |
| 664 | |
| 665 | $get_header = $this->create_header($key); |
| 666 | |
| 667 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header,)); |
| 668 | |
| 669 | if ($api_call['http_code'] != 200) { |
| 670 | //error message |
| 671 | $request_array = json_decode(json_encode($api_call['data'])); |
| 672 | $result = array( |
| 673 | "status" => 'error', |
| 674 | "http_code" => $api_call['http_code'], |
| 675 | "msg" => json_decode($request_array), |
| 676 | ); |
| 677 | return $result; |
| 678 | die(); |
| 679 | } |
| 680 | |
| 681 | return array( |
| 682 | 'status' => 'success', |
| 683 | "msg" => json_decode(($api_call['data'])), |
| 684 | ); |
| 685 | die(); |
| 686 | } |
| 687 | |
| 688 | |
| 689 | public function ApplyCode($apply_code = '') |
| 690 | { |
| 691 | /* |
| 692 | will apply a promo code to account to save money |
| 693 | */ |
| 694 | |
| 695 | if (!$this->api_key_account) { |
| 696 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 697 | die(); |
| 698 | } |
| 699 | |
| 700 | if (!$apply_code) { |
| 701 | return array('status' => 'error', 'code' => 'apply_code', 'msg' => 'missing apply code'); |
| 702 | die(); |
| 703 | } |
| 704 | |
| 705 | $key = $this->api_key_account; |
| 706 | $api_url = $this->api_url['zone'] . '/billing/applycode?couponCode=' . $apply_code; |
| 707 | |
| 708 | $get_header = $this->create_header($key); |
| 709 | |
| 710 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header,)); |
| 711 | |
| 712 | if ($api_call['http_code'] != 200) { |
| 713 | //error message |
| 714 | $request_array = json_decode(json_encode($api_call['data'])); |
| 715 | $result = array( |
| 716 | "status" => 'error', |
| 717 | "http_code" => $api_call['http_code'], |
| 718 | "msg" => json_decode($request_array), |
| 719 | ); |
| 720 | return $result; |
| 721 | die(); |
| 722 | } |
| 723 | |
| 724 | return array( |
| 725 | 'status' => 'success', |
| 726 | "msg" => $api_call, |
| 727 | ); |
| 728 | die(); |
| 729 | } |
| 730 | |
| 731 | //--->account > end |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | //--->storage > start |
| 737 | |
| 738 | public function Storage($api_key_storage = '') |
| 739 | { |
| 740 | if (!$api_key_storage) { |
| 741 | return array('status' => 'error', 'code' => 'api_key_storage', 'msg' => 'missing storage api key'); |
| 742 | die(); |
| 743 | } |
| 744 | |
| 745 | $this->api_key_storage = $api_key_storage; |
| 746 | return $this; |
| 747 | } |
| 748 | |
| 749 | public function GetStorageZone($storage_path = '') |
| 750 | { |
| 751 | /* |
| 752 | will get all of the files and subfolders for storage zone |
| 753 | */ |
| 754 | |
| 755 | if (!$this->api_key_storage) { |
| 756 | return array('status' => 'error', 'code' => 'api_key_storage', 'msg' => 'missing storage api key'); |
| 757 | die(); |
| 758 | } |
| 759 | if (!$storage_path) { |
| 760 | return array('status' => 'error', 'code' => 'missing_zone_id', 'msg' => 'missing zone id'); |
| 761 | die(); |
| 762 | } |
| 763 | |
| 764 | $key = $this->api_key_storage; |
| 765 | $api_url = $this->fix_url($this->api_url['storage'] . $storage_path); |
| 766 | |
| 767 | $get_header = $this->create_header($key); |
| 768 | |
| 769 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header,)); |
| 770 | |
| 771 | if ($api_call['http_code'] != 200) { |
| 772 | //error message |
| 773 | $request_array = json_decode(json_encode($api_call['data'])); |
| 774 | $result = array( |
| 775 | "status" => 'error', |
| 776 | "http_code" => $api_call['http_code'], |
| 777 | "msg" => json_decode($request_array), |
| 778 | ); |
| 779 | return $result; |
| 780 | die(); |
| 781 | } |
| 782 | $request_array = json_decode(json_encode($api_call['data'])); |
| 783 | |
| 784 | //convert to php array for data parsing |
| 785 | $zone_data = json_decode(($api_call['data'])); |
| 786 | |
| 787 | //--->get all the hostnames > start |
| 788 | $files = array(); |
| 789 | $folders = array(); |
| 790 | //--->get all the hostnames > start |
| 791 | if ($zone_data) { |
| 792 | foreach ($zone_data as $key => $v1) { |
| 793 | $folder_path = str_replace('/' . $v1->StorageZoneName . '/', "/", $v1->Path); |
| 794 | if (!$v1->IsDirectory) { |
| 795 | //files only |
| 796 | $d = array( |
| 797 | "storage_zone_name" => $v1->StorageZoneName, |
| 798 | "folder_path" => $folder_path, |
| 799 | "file_name" => $v1->ObjectName, |
| 800 | "file_zone_path" => $v1->Path . $v1->ObjectName, |
| 801 | "file_dl_path" => $folder_path . $v1->ObjectName, |
| 802 | ); |
| 803 | array_push($files, $d); |
| 804 | } else if ($v1->IsDirectory) { |
| 805 | //folders only |
| 806 | $d = array( |
| 807 | "storage_zone_name" => $v1->StorageZoneName, |
| 808 | "main_folder" => $v1->Path, |
| 809 | "sub_folder" => $v1->ObjectName, |
| 810 | "folder_path" => $v1->Path . $v1->ObjectName, |
| 811 | ); |
| 812 | array_push($folders, $d); |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | //--->get all the hostnames > end |
| 817 | |
| 818 | return array( |
| 819 | 'status' => 'success', |
| 820 | 'zone_smry' => array('folders' => $folders, 'files' => $files,), |
| 821 | "zone_details" => json_decode($request_array), |
| 822 | ); |
| 823 | die(); |
| 824 | } |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | public function PutFile($local_upload_file_path = '', $storage_zone_path = '', $storage_zone_file_path = '') |
| 830 | { |
| 831 | /* |
| 832 | will upload a file to storage zone |
| 833 | */ |
| 834 | |
| 835 | if (!$this->api_key_storage) { |
| 836 | return array('status' => 'error', 'code' => 'api_key_storage', 'msg' => 'missing storage api key'); |
| 837 | die(); |
| 838 | } |
| 839 | if (!$local_upload_file_path) { |
| 840 | return array('status' => 'error', 'code' => 'local_upload_file_path', 'msg' => 'missing file path'); |
| 841 | die(); |
| 842 | } |
| 843 | |
| 844 | if (!$storage_zone_file_path) { |
| 845 | return array('status' => 'error', 'code' => 'storage_zone_file_path', 'msg' => 'missing storage zone file path'); |
| 846 | die(); |
| 847 | } |
| 848 | |
| 849 | |
| 850 | |
| 851 | //file variables |
| 852 | |
| 853 | //make folder and file name seo friendly to ensure no problem happen |
| 854 | $cdn_file_path = $this->seo_file_name($storage_zone_file_path); |
| 855 | |
| 856 | $path_info = pathinfo($cdn_file_path); |
| 857 | |
| 858 | //will get folders path |
| 859 | $info_dir_name = strtolower($path_info['dirname']); |
| 860 | |
| 861 | //will get file name with ext |
| 862 | $info_file_name = $path_info['basename']; |
| 863 | |
| 864 | //$info_file_name = $path_info['filename']; |
| 865 | $info_file_ext = $path_info['extension']; |
| 866 | |
| 867 | |
| 868 | $storage_file_path = $storage_zone_path . $cdn_file_path; |
| 869 | |
| 870 | |
| 871 | |
| 872 | $key = $this->api_key_storage; |
| 873 | $api_url = $this->fix_url($this->api_url['storage'] . $storage_file_path); |
| 874 | |
| 875 | $get_header = $this->create_header($key); |
| 876 | |
| 877 | |
| 878 | // Open the file |
| 879 | $file = $local_upload_file_path; |
| 880 | $fileStream = fopen($file, "r") or die("Unable to open file!"); |
| 881 | $dataLength = filesize($file); |
| 882 | |
| 883 | |
| 884 | // Initialize and configure curl |
| 885 | $curl = curl_init(); |
| 886 | curl_setopt_array( |
| 887 | $curl, |
| 888 | array( |
| 889 | CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_URL => $api_url, CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed |
| 890 | , CURLOPT_TIMEOUT => 60000 // in case you are uploading a really BIG file!! |
| 891 | , CURLOPT_FOLLOWLOCATION => 0 // don't follow any Location headers, use only the CURLOPT_URL, this is for security |
| 892 | , CURLOPT_FAILONERROR => 0 // do not fail verbosely fi the http_code is an error, this is for security |
| 893 | , CURLOPT_SSL_VERIFYPEER => 1 // do verify the SSL of CURLOPT_URL, this is for security |
| 894 | , CURLOPT_VERBOSE => 0 // don't output verbosely to stderr, this is for security |
| 895 | , CURLOPT_INFILE => $fileStream, CURLOPT_INFILESIZE => $dataLength, CURLOPT_UPLOAD => 1, CURLOPT_HTTPHEADER => array( |
| 896 | 'AccessKey: ' . $key |
| 897 | ) |
| 898 | ) |
| 899 | ); |
| 900 | |
| 901 | // Send the request |
| 902 | $response = curl_exec($curl); |
| 903 | $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 904 | |
| 905 | // Cleanup |
| 906 | curl_close($curl); |
| 907 | fclose($fileStream); |
| 908 | |
| 909 | if ($http_code != 201) { |
| 910 | //error message |
| 911 | $request_array = json_decode(json_encode($response)); |
| 912 | $result = array( |
| 913 | "status" => 'error', |
| 914 | "http_code" => $http_code, |
| 915 | "msg" => json_decode($request_array), |
| 916 | ); |
| 917 | return $result; |
| 918 | die(); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | return array( |
| 923 | 'status' => 'success', |
| 924 | 'file_name' => $info_file_name, |
| 925 | 'storage_file_path' => $storage_file_path, |
| 926 | 'cdn_file_path' => $cdn_file_path, |
| 927 | 'msg' => $response, |
| 928 | ); |
| 929 | die(); |
| 930 | } |
| 931 | |
| 932 | public function GetFile($storage_path = '') |
| 933 | { |
| 934 | /* |
| 935 | will get a file from the storage zone |
| 936 | */ |
| 937 | |
| 938 | if (!$storage_path || !$this->api_key_storage) { |
| 939 | return array('status' => 'error', 'code' => 'missing_api_key_storage', 'msg' => 'missing storage missing api'); |
| 940 | die(); |
| 941 | } |
| 942 | |
| 943 | $key = $this->api_key_storage; |
| 944 | $api_url = $this->fix_url($this->api_url['storage'] . $storage_path); |
| 945 | |
| 946 | $accessKey = $this->api_key_storage; |
| 947 | |
| 948 | $get_header = $this->create_header($key); |
| 949 | |
| 950 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header)); |
| 951 | |
| 952 | if ($api_call['http_code'] != 200) { |
| 953 | //error message |
| 954 | $request_array = json_decode(json_encode($api_call['data'])); |
| 955 | $result = array( |
| 956 | "status" => 'error', |
| 957 | "http_code" => $api_call['http_code'], |
| 958 | "msg" => json_decode($request_array), |
| 959 | ); |
| 960 | return $result; |
| 961 | die(); |
| 962 | } |
| 963 | |
| 964 | $path_info = pathinfo($storage_path); |
| 965 | $file_name = $path_info['basename']; |
| 966 | |
| 967 | |
| 968 | $file = $api_call['data']; |
| 969 | |
| 970 | |
| 971 | header("Content-type: application/octet-stream"); |
| 972 | header("Content-Disposition: attachment; filename=$file_name"); |
| 973 | //will force to download... |
| 974 | echo $file; |
| 975 | } |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | public function DeleteFile($storage_path = '') |
| 981 | { |
| 982 | /* |
| 983 | will delete a file from the storage zone |
| 984 | */ |
| 985 | |
| 986 | if (!$storage_path || !$this->api_key_storage) { |
| 987 | return array('status' => 'error', 'code' => 'missing_api_key_storage', 'msg' => 'missing storage missing api'); |
| 988 | die(); |
| 989 | } |
| 990 | |
| 991 | $key = $this->api_key_storage; |
| 992 | $api_url = $this->fix_url($this->api_url['storage'] . $storage_path); |
| 993 | |
| 994 | $accessKey = $this->api_key_storage; |
| 995 | |
| 996 | $get_header = $this->create_header($key); |
| 997 | |
| 998 | $api_call = $this->run(array('call_method' => 'DELETE', 'api_url' => $api_url, 'header' => $get_header)); |
| 999 | |
| 1000 | if ($api_call['http_code'] != 200) { |
| 1001 | //error message |
| 1002 | $request_array = json_decode(json_encode($api_call['data'])); |
| 1003 | $result = array( |
| 1004 | "status" => 'error', |
| 1005 | "http_code" => $api_call['http_code'], |
| 1006 | "msg" => json_decode($request_array), |
| 1007 | ); |
| 1008 | return $result; |
| 1009 | die(); |
| 1010 | } |
| 1011 | |
| 1012 | return array( |
| 1013 | 'status' => 'success', |
| 1014 | 'msg' => $api_call, |
| 1015 | ); |
| 1016 | die(); |
| 1017 | } |
| 1018 | |
| 1019 | public function SecureLink($host_name = '', $security_key = '', $file_path = '', $expiry_hr = 24) |
| 1020 | { |
| 1021 | $securityKey = $security_key; |
| 1022 | $path = $file_path; |
| 1023 | |
| 1024 | // Set the time of expiry to one hour from now |
| 1025 | $expires = (time() + 3600) * $expiry_hr; |
| 1026 | |
| 1027 | // Generate the token |
| 1028 | $hashableBase = $securityKey . $path . $expires; |
| 1029 | |
| 1030 | // If using IP validation |
| 1031 | // $hashableBase .= "146.14.19.7"; |
| 1032 | |
| 1033 | $token = md5($hashableBase, true); |
| 1034 | $token = base64_encode($token); |
| 1035 | $token = strtr($token, '+/', '-_'); |
| 1036 | $token = str_replace('=', '', $token); |
| 1037 | |
| 1038 | // Generate the URL |
| 1039 | $url = "$host_name$file_path?token={$token}&expires={$expires}"; |
| 1040 | |
| 1041 | return $url; |
| 1042 | } |
| 1043 | //--->storage > end |
| 1044 | |
| 1045 | public function DownloadFile($file_url = '', $oupt_file_name = '') |
| 1046 | { |
| 1047 | //this is a fast way to download a file |
| 1048 | //remove any query string data |
| 1049 | if (isset($oupt_file_name)) { |
| 1050 | $file_name = $oupt_file_name; |
| 1051 | } |
| 1052 | if (empty($oupt_file_name)) { |
| 1053 | $file_name = preg_replace('/\?.*/', '', basename($file_url)); |
| 1054 | } |
| 1055 | |
| 1056 | header("Content-Type: application/octet-stream"); |
| 1057 | header("Content-Transfer-Encoding: Binary"); |
| 1058 | header("Content-disposition: attachment; filename=$file_name"); |
| 1059 | readfile($file_url); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | public function DownloadFile1($file_url) |
| 1064 | { |
| 1065 | /* |
| 1066 | this is a slow way to download a file |
| 1067 | will allow you to download a remote file from any server that is accessible |
| 1068 | */ |
| 1069 | |
| 1070 | $filename = $file_url; |
| 1071 | $filedata = @file_get_contents($filename); |
| 1072 | |
| 1073 | // SUCCESS |
| 1074 | if ($filedata) { |
| 1075 | // GET A NAME FOR THE FILE |
| 1076 | //remove any query string data |
| 1077 | $basename = preg_replace('/\?.*/', '', basename($file_url)); |
| 1078 | //$basename = basename($filename); |
| 1079 | |
| 1080 | // THESE HEADERS ARE USED ON ALL BROWSERS |
| 1081 | header("Content-Type: application-x/force-download"); |
| 1082 | header("Content-Disposition: attachment; filename=$basename"); |
| 1083 | header("Content-length: " . (string)(strlen($filedata))); |
| 1084 | header("Expires: " . gmdate("D, d M Y H:i:s", mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"))) . " GMT"); |
| 1085 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
| 1086 | |
| 1087 | // THIS HEADER MUST BE OMITTED FOR IE 6+ |
| 1088 | if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE ')) { |
| 1089 | header("Cache-Control: no-cache, must-revalidate"); |
| 1090 | } |
| 1091 | |
| 1092 | // THIS IS THE LAST HEADER |
| 1093 | header("Pragma: no-cache"); |
| 1094 | |
| 1095 | // FLUSH THE HEADERS TO THE BROWSER |
| 1096 | flush(); |
| 1097 | |
| 1098 | // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END |
| 1099 | ob_start(); |
| 1100 | echo $filedata; |
| 1101 | } |
| 1102 | |
| 1103 | // FAILURE |
| 1104 | else { |
| 1105 | die("ERROR: UNABLE TO OPEN $filename"); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | public function Logs($zone_id = '', $log_date = '') |
| 1110 | { |
| 1111 | /* |
| 1112 | will get log for the zone |
| 1113 | */ |
| 1114 | |
| 1115 | if (!$this->api_key_account) { |
| 1116 | return array('status' => 'error', 'code' => 'api_key_account', 'msg' => 'missing acount api key'); |
| 1117 | die(); |
| 1118 | } |
| 1119 | |
| 1120 | if (!$log_date) { |
| 1121 | $date = new DateTime(); |
| 1122 | |
| 1123 | //today minus 1 day... if today(2019-03-29), then date is: 2019-03-28 |
| 1124 | $date = $date->modify("-1 day"); |
| 1125 | $date = $date->format('m-d-y'); |
| 1126 | $log_dt = $date; |
| 1127 | } else if ($log_date) { |
| 1128 | $date = new DateTime($log_date); |
| 1129 | $date = $date->format('m-d-y'); |
| 1130 | $log_dt = $date; |
| 1131 | } |
| 1132 | |
| 1133 | $key = $this->api_key_account; |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | //$api_url = 'https://logging.bunnycdn.com/{mm}-{dd}-{yy}/{pull_zone_id}.log'; |
| 1138 | $api_url = 'https://logging.bunnycdn.com/' . $log_dt . '/' . $zone_id . '.log'; |
| 1139 | |
| 1140 | $get_header = $this->create_header($key); |
| 1141 | |
| 1142 | $api_call = $this->run(array('call_method' => 'GET', 'api_url' => $api_url, 'header' => $get_header,)); |
| 1143 | |
| 1144 | if ($api_call['http_code'] != 200) { |
| 1145 | //error message |
| 1146 | $request_array = json_decode(json_encode($api_call['data'])); |
| 1147 | $result = array( |
| 1148 | "status" => 'error', |
| 1149 | "http_code" => $api_call['http_code'], |
| 1150 | "msg" => ($request_array), |
| 1151 | ); |
| 1152 | return $result; |
| 1153 | //die(); |
| 1154 | } else if (strlen($api_call['data']) < 1) { |
| 1155 | $result = array( |
| 1156 | "status" => 'error', |
| 1157 | "http_code" => 800, |
| 1158 | 'msg' => 'Ran successfully but no log data returned for the current selection.', |
| 1159 | ); |
| 1160 | return $result; |
| 1161 | //die(); |
| 1162 | } else if ($api_call['http_code'] == 200) { |
| 1163 | |
| 1164 | //convert/parse it to line break |
| 1165 | $t1 = explode("\n", $api_call['data']); |
| 1166 | |
| 1167 | $a1 = array(); |
| 1168 | |
| 1169 | foreach ($t1 as $v1) { |
| 1170 | if (isset($v1) && strlen($v1) > 0) { |
| 1171 | //parse "|" |
| 1172 | $t2 = explode("|", $v1); |
| 1173 | |
| 1174 | //divide it by 1000 to convert it to php unix time |
| 1175 | $time = round($t2[2] / 1000, 0); |
| 1176 | |
| 1177 | $a2 = array( |
| 1178 | 'cache_hit' => $t2[0], |
| 1179 | |
| 1180 | 'status' => $t2[1], |
| 1181 | 'status_code' => $this->get_http_status_code($t2[1]), |
| 1182 | |
| 1183 | 'time_js' => $t2[2] * 1, |
| 1184 | |
| 1185 | 'time_unix' => $time, |
| 1186 | 'time_dttm' => date('Y-m-d H:i:s', $time), |
| 1187 | 'time_dt' => date('Y-m-d', $time), |
| 1188 | |
| 1189 | 'bytes' => $t2[3], |
| 1190 | 'bytes_format' => $this->format_bytes($t2[3]), |
| 1191 | |
| 1192 | 'zone_id' => $t2[4], |
| 1193 | 'remote_ip' => $t2[5], |
| 1194 | |
| 1195 | 'referer_url' => strlen($t2[6]) > 1 ? ($t2[6]) : 'direct', |
| 1196 | 'referer_url_raw' => $t2[6], |
| 1197 | |
| 1198 | |
| 1199 | 'file_url' => $t2[7], |
| 1200 | |
| 1201 | 'cdn_datacenter_loc' => $t2[8], |
| 1202 | |
| 1203 | 'user_agent' => $t2[9], |
| 1204 | 'request_id' => $t2[10], |
| 1205 | |
| 1206 | 'country' => $t2[11], |
| 1207 | 'country_name' => $this->get_country_name($t2[11]), |
| 1208 | ); |
| 1209 | array_push($a1, $a2); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | $get_stats = $this->Account($key)->GetZone($zone_id)['zone_smry'][0]; |
| 1214 | |
| 1215 | return array( |
| 1216 | 'status' => 'success', |
| 1217 | "log" => $a1, |
| 1218 | 'zone_current_monthly_bandwidth_used' => $get_stats['monthly_bandwidth_used'], |
| 1219 | 'zone_name' => $get_stats['zone_name'], |
| 1220 | ); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | //--->process functions > start |
| 1225 | |
| 1226 | private function create_header($api_key) |
| 1227 | { |
| 1228 | $header = array('Content-Type:application/json', 'accesskey:' . $api_key . ''); |
| 1229 | return $header; |
| 1230 | } |
| 1231 | |
| 1232 | private function run($call_arr = array('call_method' => 'GET', 'api_url' => 'api_url', 'header' => array(), 'post_data_array' => array(),)) |
| 1233 | { |
| 1234 | $call_method = isset($call_arr['call_method']) ? $call_arr['call_method'] : 'GET'; |
| 1235 | $api_url = isset($call_arr['api_url']) ? $call_arr['api_url'] : 'api_url'; |
| 1236 | $header = isset($call_arr['header']) ? $call_arr['header'] : ''; |
| 1237 | $post_data_array = isset($call_arr['post_data_array']) ? $call_arr['post_data_array'] : ''; |
| 1238 | |
| 1239 | |
| 1240 | $post_data = json_encode($post_data_array); |
| 1241 | |
| 1242 | $curl = curl_init($api_url); |
| 1243 | |
| 1244 | curl_setopt($curl, CURLOPT_HTTPHEADER, $header); |
| 1245 | |
| 1246 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $call_method); |
| 1247 | |
| 1248 | curl_setopt($curl, CURLOPT_URL, $api_url); |
| 1249 | |
| 1250 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
| 1251 | |
| 1252 | curl_setopt($curl, CURLOPT_POST, 1); |
| 1253 | curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); |
| 1254 | |
| 1255 | $result = curl_exec($curl); |
| 1256 | $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 1257 | |
| 1258 | curl_close($curl); |
| 1259 | |
| 1260 | |
| 1261 | //For error checking |
| 1262 | if ($result === false) { |
| 1263 | return array('status' => 'error', 'code' => 'curl_error', 'result' => curl_error($curl),); |
| 1264 | die(); |
| 1265 | } |
| 1266 | |
| 1267 | return array('http_code' => $http_code, 'data' => $result,); |
| 1268 | } |
| 1269 | //--->process functions > end |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | //--->private functions > start |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | private function fix_url($url = '') |
| 1278 | { |
| 1279 | return str_replace("\\", "/", $url); |
| 1280 | } |
| 1281 | |
| 1282 | private function format_bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE) |
| 1283 | { |
| 1284 | // Format string |
| 1285 | $format = ($format === NULL) ? '%01.2f %s' : (string) $format; |
| 1286 | |
| 1287 | // IEC prefixes (binary) |
| 1288 | if ($si == FALSE or strpos($force_unit, 'i') !== FALSE) { |
| 1289 | $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); |
| 1290 | $mod = 1024; |
| 1291 | } |
| 1292 | // SI prefixes (decimal) |
| 1293 | else { |
| 1294 | $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB'); |
| 1295 | $mod = 1000; |
| 1296 | } |
| 1297 | // Determine unit to use |
| 1298 | if (($power = array_search((string) $force_unit, $units)) === FALSE) { |
| 1299 | $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0; |
| 1300 | } |
| 1301 | return sprintf($format, $bytes / pow($mod, $power), $units[$power]); |
| 1302 | } |
| 1303 | |
| 1304 | private function seo_file_name($file_name) |
| 1305 | { |
| 1306 | /* |
| 1307 | will convert file name into seo url file name |
| 1308 | |
| 1309 | i.e. |
| 1310 | $file_name = 'code with mark !@#$%^*()_+~ $$%& _03e05 122-9****.mp4'; |
| 1311 | |
| 1312 | //output will be |
| 1313 | code-with-mark-03e05-122-9.mp4 |
| 1314 | |
| 1315 | Note only use this for file names and not for folder names!!! |
| 1316 | |
| 1317 | */ |
| 1318 | |
| 1319 | $path_info = pathinfo($file_name); |
| 1320 | $info_dir_name = preg_replace("/[\s]/", "-", strtolower($path_info['dirname'])); |
| 1321 | |
| 1322 | |
| 1323 | $info_file_name = $path_info['filename']; |
| 1324 | $info_file_ext = $path_info['extension']; |
| 1325 | |
| 1326 | $string = $info_file_name; |
| 1327 | |
| 1328 | $src = 'àáâãäçèéêëìíîïñòóôõöøùúûüýÿßÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ'; |
| 1329 | $rep = 'aaaaaceeeeiiiinoooooouuuuyysAAAAACEEEEIIIINOOOOOOUUUUY'; |
| 1330 | // strip off accents (assuming utf8 PHP - note strtr() requires single-byte) |
| 1331 | $string = strtr(utf8_decode($string), utf8_decode($src), $rep); |
| 1332 | // convert to lower case |
| 1333 | $string = strtolower($string); |
| 1334 | // strip all but alphanumeric, whitespace, dot, underscore, hyphen |
| 1335 | $string = preg_replace("/[^a-z0-9\s._-]/", "", $string); |
| 1336 | // merge multiple consecutive whitespaces, dots, underscores, hyphens |
| 1337 | $string = preg_replace("/[\s._-]+/", " ", $string); |
| 1338 | // convert whitespaces to hyphens |
| 1339 | $string = preg_replace("/[\s]/", "-", $string); |
| 1340 | |
| 1341 | |
| 1342 | if (substr($info_dir_name, 1)) { |
| 1343 | $file_path = $info_dir_name . "/" . $string . '.' . $info_file_ext; |
| 1344 | } else { |
| 1345 | $file_path = "/" . $string . '.' . $info_file_ext; |
| 1346 | } |
| 1347 | |
| 1348 | return $file_path; |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | private function get_country_name($country_code) |
| 1353 | { |
| 1354 | $country_name = array( |
| 1355 | "A1" => "Anonymous Proxy", |
| 1356 | "A2" => "Satellite Provider", |
| 1357 | "O1" => "Other Country", |
| 1358 | "AD" => "Andorra", |
| 1359 | "AE" => "United Arab Emirates", |
| 1360 | "AF" => "Afghanistan", |
| 1361 | "AG" => "Antigua and Barbuda", |
| 1362 | "AI" => "Anguilla", |
| 1363 | "AL" => "Albania", |
| 1364 | "AM" => "Armenia", |
| 1365 | "AO" => "Angola", |
| 1366 | "AP" => "Asia/Pacific Region", |
| 1367 | "AQ" => "Antarctica", |
| 1368 | "AR" => "Argentina", |
| 1369 | "AS" => "American Samoa", |
| 1370 | "AT" => "Austria", |
| 1371 | "AU" => "Australia", |
| 1372 | "AW" => "Aruba", |
| 1373 | "AX" => "Aland Islands", |
| 1374 | "AZ" => "Azerbaijan", |
| 1375 | "BA" => "Bosnia and Herzegovina", |
| 1376 | "BB" => "Barbados", |
| 1377 | "BD" => "Bangladesh", |
| 1378 | "BE" => "Belgium", |
| 1379 | "BF" => "Burkina Faso", |
| 1380 | "BG" => "Bulgaria", |
| 1381 | "BH" => "Bahrain", |
| 1382 | "BI" => "Burundi", |
| 1383 | "BJ" => "Benin", |
| 1384 | "BL" => "Saint Bartelemey", |
| 1385 | "BM" => "Bermuda", |
| 1386 | "BN" => "Brunei Darussalam", |
| 1387 | "BO" => "Bolivia", |
| 1388 | "BQ" => "Bonaire, Saint Eustatius and Saba", |
| 1389 | "BR" => "Brazil", |
| 1390 | "BS" => "Bahamas", |
| 1391 | "BT" => "Bhutan", |
| 1392 | "BV" => "Bouvet Island", |
| 1393 | "BW" => "Botswana", |
| 1394 | "BY" => "Belarus", |
| 1395 | "BZ" => "Belize", |
| 1396 | "CA" => "Canada", |
| 1397 | "CC" => "Cocos (Keeling) Islands", |
| 1398 | "CD" => "Congo, The Democratic Republic of the", |
| 1399 | "CF" => "Central African Republic", |
| 1400 | "CG" => "Congo", |
| 1401 | "CH" => "Switzerland", |
| 1402 | "CI" => "Cote d'Ivoire", |
| 1403 | "CK" => "Cook Islands", |
| 1404 | "CL" => "Chile", |
| 1405 | "CM" => "Cameroon", |
| 1406 | "CN" => "China", |
| 1407 | "CO" => "Colombia", |
| 1408 | "CR" => "Costa Rica", |
| 1409 | "CU" => "Cuba", |
| 1410 | "CV" => "Cape Verde", |
| 1411 | "CW" => "Curacao", |
| 1412 | "CX" => "Christmas Island", |
| 1413 | "CY" => "Cyprus", |
| 1414 | "CZ" => "Czech Republic", |
| 1415 | "DE" => "Germany", |
| 1416 | "DJ" => "Djibouti", |
| 1417 | "DK" => "Denmark", |
| 1418 | "DM" => "Dominica", |
| 1419 | "DO" => "Dominican Republic", |
| 1420 | "DZ" => "Algeria", |
| 1421 | "EC" => "Ecuador", |
| 1422 | "EE" => "Estonia", |
| 1423 | "EG" => "Egypt", |
| 1424 | "EH" => "Western Sahara", |
| 1425 | "ER" => "Eritrea", |
| 1426 | "ES" => "Spain", |
| 1427 | "ET" => "Ethiopia", |
| 1428 | "EU" => "Europe", |
| 1429 | "FI" => "Finland", |
| 1430 | "FJ" => "Fiji", |
| 1431 | "FK" => "Falkland Islands (Malvinas)", |
| 1432 | "FM" => "Micronesia, Federated States of", |
| 1433 | "FO" => "Faroe Islands", |
| 1434 | "FR" => "France", |
| 1435 | "GA" => "Gabon", |
| 1436 | "GB" => "United Kingdom", |
| 1437 | "GD" => "Grenada", |
| 1438 | "GE" => "Georgia", |
| 1439 | "GF" => "French Guiana", |
| 1440 | "GG" => "Guernsey", |
| 1441 | "GH" => "Ghana", |
| 1442 | "GI" => "Gibraltar", |
| 1443 | "GL" => "Greenland", |
| 1444 | "GM" => "Gambia", |
| 1445 | "GN" => "Guinea", |
| 1446 | "GP" => "Guadeloupe", |
| 1447 | "GQ" => "Equatorial Guinea", |
| 1448 | "GR" => "Greece", |
| 1449 | "GS" => "South Georgia and the South Sandwich Islands", |
| 1450 | "GT" => "Guatemala", |
| 1451 | "GU" => "Guam", |
| 1452 | "GW" => "Guinea-Bissau", |
| 1453 | "GY" => "Guyana", |
| 1454 | "HK" => "Hong Kong", |
| 1455 | "HM" => "Heard Island and McDonald Islands", |
| 1456 | "HN" => "Honduras", |
| 1457 | "HR" => "Croatia", |
| 1458 | "HT" => "Haiti", |
| 1459 | "HU" => "Hungary", |
| 1460 | "ID" => "Indonesia", |
| 1461 | "IE" => "Ireland", |
| 1462 | "IL" => "Israel", |
| 1463 | "IM" => "Isle of Man", |
| 1464 | "IN" => "India", |
| 1465 | "IO" => "British Indian Ocean Territory", |
| 1466 | "IQ" => "Iraq", |
| 1467 | "IR" => "Iran, Islamic Republic of", |
| 1468 | "IS" => "Iceland", |
| 1469 | "IT" => "Italy", |
| 1470 | "JE" => "Jersey", |
| 1471 | "JM" => "Jamaica", |
| 1472 | "JO" => "Jordan", |
| 1473 | "JP" => "Japan", |
| 1474 | "KE" => "Kenya", |
| 1475 | "KG" => "Kyrgyzstan", |
| 1476 | "KH" => "Cambodia", |
| 1477 | "KI" => "Kiribati", |
| 1478 | "KM" => "Comoros", |
| 1479 | "KN" => "Saint Kitts and Nevis", |
| 1480 | "KP" => "Korea, Democratic People's Republic of", |
| 1481 | "KR" => "Korea, Republic of", |
| 1482 | "KW" => "Kuwait", |
| 1483 | "KY" => "Cayman Islands", |
| 1484 | "KZ" => "Kazakhstan", |
| 1485 | "LA" => "Lao People's Democratic Republic", |
| 1486 | "LB" => "Lebanon", |
| 1487 | "LC" => "Saint Lucia", |
| 1488 | "LI" => "Liechtenstein", |
| 1489 | "LK" => "Sri Lanka", |
| 1490 | "LR" => "Liberia", |
| 1491 | "LS" => "Lesotho", |
| 1492 | "LT" => "Lithuania", |
| 1493 | "LU" => "Luxembourg", |
| 1494 | "LV" => "Latvia", |
| 1495 | "LY" => "Libyan Arab Jamahiriya", |
| 1496 | "MA" => "Morocco", |
| 1497 | "MC" => "Monaco", |
| 1498 | "MD" => "Moldova, Republic of", |
| 1499 | "ME" => "Montenegro", |
| 1500 | "MF" => "Saint Martin", |
| 1501 | "MG" => "Madagascar", |
| 1502 | "MH" => "Marshall Islands", |
| 1503 | "MK" => "Macedonia", |
| 1504 | "ML" => "Mali", |
| 1505 | "MM" => "Myanmar", |
| 1506 | "MN" => "Mongolia", |
| 1507 | "MO" => "Macao", |
| 1508 | "MP" => "Northern Mariana Islands", |
| 1509 | "MQ" => "Martinique", |
| 1510 | "MR" => "Mauritania", |
| 1511 | "MS" => "Montserrat", |
| 1512 | "MT" => "Malta", |
| 1513 | "MU" => "Mauritius", |
| 1514 | "MV" => "Maldives", |
| 1515 | "MW" => "Malawi", |
| 1516 | "MX" => "Mexico", |
| 1517 | "MY" => "Malaysia", |
| 1518 | "MZ" => "Mozambique", |
| 1519 | "NA" => "Namibia", |
| 1520 | "NC" => "New Caledonia", |
| 1521 | "NE" => "Niger", |
| 1522 | "NF" => "Norfolk Island", |
| 1523 | "NG" => "Nigeria", |
| 1524 | "NI" => "Nicaragua", |
| 1525 | "NL" => "Netherlands", |
| 1526 | "NO" => "Norway", |
| 1527 | "NP" => "Nepal", |
| 1528 | "NR" => "Nauru", |
| 1529 | "NU" => "Niue", |
| 1530 | "NZ" => "New Zealand", |
| 1531 | "OM" => "Oman", |
| 1532 | "PA" => "Panama", |
| 1533 | "PE" => "Peru", |
| 1534 | "PF" => "French Polynesia", |
| 1535 | "PG" => "Papua New Guinea", |
| 1536 | "PH" => "Philippines", |
| 1537 | "PK" => "Pakistan", |
| 1538 | "PL" => "Poland", |
| 1539 | "PM" => "Saint Pierre and Miquelon", |
| 1540 | "PN" => "Pitcairn", |
| 1541 | "PR" => "Puerto Rico", |
| 1542 | "PS" => "Palestinian Territory", |
| 1543 | "PT" => "Portugal", |
| 1544 | "PW" => "Palau", |
| 1545 | "PY" => "Paraguay", |
| 1546 | "QA" => "Qatar", |
| 1547 | "RE" => "Reunion", |
| 1548 | "RO" => "Romania", |
| 1549 | "RS" => "Serbia", |
| 1550 | "RU" => "Russian Federation", |
| 1551 | "RW" => "Rwanda", |
| 1552 | "SA" => "Saudi Arabia", |
| 1553 | "SB" => "Solomon Islands", |
| 1554 | "SC" => "Seychelles", |
| 1555 | "SD" => "Sudan", |
| 1556 | "SE" => "Sweden", |
| 1557 | "SG" => "Singapore", |
| 1558 | "SH" => "Saint Helena", |
| 1559 | "SI" => "Slovenia", |
| 1560 | "SJ" => "Svalbard and Jan Mayen", |
| 1561 | "SK" => "Slovakia", |
| 1562 | "SL" => "Sierra Leone", |
| 1563 | "SM" => "San Marino", |
| 1564 | "SN" => "Senegal", |
| 1565 | "SO" => "Somalia", |
| 1566 | "SR" => "Suriname", |
| 1567 | "SS" => "South Sudan", |
| 1568 | "ST" => "Sao Tome and Principe", |
| 1569 | "SV" => "El Salvador", |
| 1570 | "SX" => "Sint Maarten", |
| 1571 | "SY" => "Syrian Arab Republic", |
| 1572 | "SZ" => "Swaziland", |
| 1573 | "TC" => "Turks and Caicos Islands", |
| 1574 | "TD" => "Chad", |
| 1575 | "TF" => "French Southern Territories", |
| 1576 | "TG" => "Togo", |
| 1577 | "TH" => "Thailand", |
| 1578 | "TJ" => "Tajikistan", |
| 1579 | "TK" => "Tokelau", |
| 1580 | "TL" => "Timor-Leste", |
| 1581 | "TM" => "Turkmenistan", |
| 1582 | "TN" => "Tunisia", |
| 1583 | "TO" => "Tonga", |
| 1584 | "TR" => "Turkey", |
| 1585 | "TT" => "Trinidad and Tobago", |
| 1586 | "TV" => "Tuvalu", |
| 1587 | "TW" => "Taiwan", |
| 1588 | "TZ" => "Tanzania, United Republic of", |
| 1589 | "UA" => "Ukraine", |
| 1590 | "UG" => "Uganda", |
| 1591 | "UM" => "United States Minor Outlying Islands", |
| 1592 | "US" => "United States", |
| 1593 | "UY" => "Uruguay", |
| 1594 | "UZ" => "Uzbekistan", |
| 1595 | "VA" => "Holy See (Vatican City State)", |
| 1596 | "VC" => "Saint Vincent and the Grenadines", |
| 1597 | "VE" => "Venezuela", |
| 1598 | "VG" => "Virgin Islands, British", |
| 1599 | "VI" => "Virgin Islands, U.S.", |
| 1600 | "VN" => "Vietnam", |
| 1601 | "VU" => "Vanuatu", |
| 1602 | "WF" => "Wallis and Futuna", |
| 1603 | "WS" => "Samoa", |
| 1604 | "YE" => "Yemen", |
| 1605 | "YT" => "Mayotte", |
| 1606 | "ZA" => "South Africa", |
| 1607 | "ZM" => "Zambia", |
| 1608 | "ZW" => "Zimbabwe", |
| 1609 | ); |
| 1610 | //return $country_name[$country_code]; |
| 1611 | foreach ($country_name as $k1 => $v1) { |
| 1612 | if ($k1 == $country_code) { |
| 1613 | return $v1; |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | private function get_http_status_code($code) |
| 1619 | { |
| 1620 | $status_code = array( |
| 1621 | // 1XX |
| 1622 | "100" => "Continue", |
| 1623 | "101" => "Switching Protocols", |
| 1624 | "102" => "Processing", |
| 1625 | "103" => "Early Hints", |
| 1626 | // 2XX |
| 1627 | "200" => "OK", |
| 1628 | "201" => "Created", |
| 1629 | "202" => "Accepted", |
| 1630 | "203" => "Non-Authoritative Information", |
| 1631 | "204" => "No Content", |
| 1632 | "206" => "Partial Content", |
| 1633 | "207" => "Multi-Status", |
| 1634 | "208" => "Already Reported", |
| 1635 | "226" => "IM Used", |
| 1636 | // 3XX |
| 1637 | "300" => "Multiple Choices", |
| 1638 | "301" => "Moved Permanently", |
| 1639 | "302" => "Found", |
| 1640 | "303" => "See Other", |
| 1641 | "304" => "Not Modified", |
| 1642 | "305" => "Use Proxy", |
| 1643 | "306" => "Switch Proxy", |
| 1644 | "307" => "Temporary Redirect", |
| 1645 | "308" => "Permanent Redirect", |
| 1646 | // 4XX |
| 1647 | "400" => "Bad Request", |
| 1648 | "401" => "Unauthorized", |
| 1649 | "402" => "Payment Required", |
| 1650 | "403" => "Forbidden", |
| 1651 | "404" => "Not Found", |
| 1652 | "405" => "Method Not Allowed", |
| 1653 | "406" => "Not Acceptable", |
| 1654 | "407" => "Proxy Authentication Required", |
| 1655 | "408" => "Request Timeout", |
| 1656 | "409" => "Conflict", |
| 1657 | "410" => "Gone", |
| 1658 | "411" => "Length Required", |
| 1659 | "412" => "Precondition Failed", |
| 1660 | "413" => "Payload Too Large", |
| 1661 | "414" => "URI Too Long", |
| 1662 | "415" => "Unsupported Media Type", |
| 1663 | "416" => "Range Not Satisfiable", |
| 1664 | "417" => "Expectation Failed", |
| 1665 | "418" => "I'm a teapot", |
| 1666 | "421" => "Misdirected Request", |
| 1667 | "422" => "Unprocessable Entity", |
| 1668 | "423" => "Locked", |
| 1669 | "424" => "Failed Dependency", |
| 1670 | "426" => "Upgrade Required", |
| 1671 | "428" => "Precondition Required", |
| 1672 | "429" => "Too Many Requests", |
| 1673 | "431" => "Request Header Fields Too Large", |
| 1674 | "451" => "Unavailable For Legal Reasons", |
| 1675 | |
| 1676 | // 5XX |
| 1677 | "500" => "Internal Server Error", |
| 1678 | "501" => "Not Implemented", |
| 1679 | "502" => "Bad Gateway", |
| 1680 | "503" => "Service Unavailable", |
| 1681 | "504" => "Gateway Timeout", |
| 1682 | "505" => "HTTP Version Not Supported", |
| 1683 | "506" => "Variant Also Negotiates", |
| 1684 | "507" => "Insufficient Storage", |
| 1685 | "508" => "Loop Detected", |
| 1686 | "510" => "Not Extended", |
| 1687 | "511" => "Network Authentication Required", |
| 1688 | ); |
| 1689 | |
| 1690 | foreach ($status_code as $k1 => $v1) { |
| 1691 | if ($k1 == $code) { |
| 1692 | return $v1; |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | |
| 1698 | //--->private functions > end |
| 1699 | } |
| 1700 |