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