1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class phpBuycraftAPI {
29: private $baseUrl = "https://api.buycraft.net/v3?";
30: private $secret = "unspecified";
31: private $secretBad = NULL;
32: private $dataStore;
33:
34: 35: 36: 37: 38: 39:
40: public function __construct($secret=NULL) {
41: if ($secret === NULL) {
42: throw new \RuntimeException("Error: No Secret key input.\n");
43: } else {
44: $this->secret = $secret;
45: }
46: $this->dataStore = new phpBuycraftData();
47: }
48:
49: 50: 51: 52: 53: 54: 55:
56: public function fetch($indata) {
57: $data = array_merge(array("secret"=>$this->secret), $indata);
58: $raw = file_get_contents($this->baseUrl.http_build_query($data));
59: $json = json_decode($raw);
60: $code = $json->{"code"};
61: if ($code === phpBuycraftStatus::$OK[0]) {
62: return $json;
63: }
64: ob_start();
65: if ($code === phpBuycraftStatus::$NEED_MORE_INFO[0]) {
66: echo "Error: ".phpBuycraftStatus::$NEED_MORE_INFO[1]."\n";
67: } else if ($code === phpBuycraftStatus::$SECRET_NOT_FOUND[0]) {
68: $this->secretBad = TRUE;
69: echo "Error: ".phpBuycraftStatus::$SECRET_NOT_FOUND[1]."\n";
70: } else if ($code === phpBuycraftStatus::$UNKNOWN_ACTION[0]) {
71: echo "Error: ".phpBuycraftStatus::$UNKNOWN_ACTION[1]."\n";
72: } else {
73: echo "Error: The API returned an unknown code: ".$code.".\n";
74: }
75: var_dump($indata);
76: var_dump($json);
77: throw new \RuntimeException(ob_get_clean());
78: return NULL;
79: }
80:
81: 82: 83: 84: 85: 86:
87: public function refreshInfo() {
88: $data = array("action"=>"info");
89: $return = $this->fetch($data);
90: if ($return === NULL) { return FALSE; }
91: $payload = $return->{"payload"};
92: $this->dataStore->info = $payload;
93: return TRUE;
94: }
95: 96: 97: 98: 99: 100:
101: public function refreshPackages() {
102: $data = array("action"=>"packages");
103: $return = $this->fetch($data);
104: if ($return === NULL) { return FALSE; }
105: $payload = $return->{"payload"};
106: $this->dataStore->packages = $payload;
107: return TRUE;
108: }
109: 110: 111: 112: 113: 114:
115: public function refreshPayments() {
116: $data = array("action"=>"payments");
117: $return = $this->fetch($data);
118: if ($return === NULL) { return FALSE; }
119: $payload = $return->{"payload"};
120: $this->dataStore->payments = $payload;
121: return TRUE;
122: }
123: 124: 125: 126: 127: 128:
129: public function refreshCommands() {
130: $data = array("action"=>"commands", "do"=>"lookup");
131: $return = $this->fetch($data);
132: if ($return === NULL) { return FALSE; }
133: $payload = $return->{"payload"};
134: $this->dataStore->commands = $payload->{"commands"};
135: return TRUE;
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function refreshChecker() {
145: $data = array("action"=>"checker", "do"=>"lookup");
146: $return = $this->fetch($data);
147: if ($return === NULL) { return FALSE; }
148: $payload = $return->{"payload"};
149: $this->dataStore->checker = $payload;
150: return TRUE;
151: }
152:
153:
154: 155: 156: 157: 158: 159:
160: public function ensureInfo() {
161: if ($this->secretBad === TRUE) { return FALSE; }
162: if ($this->dataStore->info == NULL) { return $this->refreshInfo(); }
163: return TRUE;
164: }
165: 166: 167: 168: 169: 170:
171: public function ensurePackages() {
172: if ($this->secretBad === TRUE) { return FALSE; }
173: if ($this->dataStore->packages == NULL) { return $this->refreshPackages(); }
174: return TRUE;
175: }
176: 177: 178: 179: 180: 181:
182: public function ensurePayments() {
183: if ($this->secretBad === TRUE) { return FALSE; }
184: if ($this->dataStore->payments == NULL) { return $this->refreshPayments(); }
185: return TRUE;
186: }
187: 188: 189: 190: 191: 192:
193: public function ensureCommands() {
194: if ($this->secretBad === TRUE) { return FALSE; }
195: if ($this->dataStore->commands == NULL) { return $this->refreshCommands(); }
196: return TRUE;
197: }
198: 199: 200: 201: 202: 203:
204: public function ensureChecker() {
205: if ($this->secretBad === TRUE) { return FALSE; }
206: if ($this->dataStore->checker == NULL) { return $this->refreshChecker(); }
207: return TRUE;
208: }
209:
210: 211: 212: 213:
214: public function rawInfo() {
215: if ($this->ensureInfo() === FALSE) { return NULL; }
216: return $this->dataStore->info;
217: }
218: 219: 220: 221:
222: public function rawPackages() {
223: if ($this->ensurePackages() === FALSE) { return NULL; }
224: return $this->dataStore->packages;
225: }
226: 227: 228: 229:
230: public function rawPayments() {
231: if ($this->ensurePayments() === FALSE) { return NULL; }
232: return $this->dataStore->payments;
233: }
234: 235: 236: 237:
238: public function rawCommands() {
239: if ($this->ensureCommands() === FALSE) { return NULL; }
240: return $this->dataStore->commands;
241: }
242: 243: 244: 245:
246: public function rawChecker() {
247: if ($this->ensureChecker() === FALSE) { return NULL; }
248: return $this->dataStore->checker;
249: }
250:
251: 252: 253: 254: 255:
256: public function getLatestVersion() {
257: if ($this->ensureInfo() === FALSE) { return NULL; }
258: return $this->dataStore->info->{"latestVersion"};
259: }
260: 261: 262: 263: 264:
265: public function getLatestDownload() {
266: if ($this->ensureInfo() === FALSE) { return NULL; }
267: return $this->dataStore->info->{"latestDownload"};
268: }
269: 270: 271: 272: 273:
274: public function getServerId() {
275: if ($this->ensureInfo() === FALSE) { return NULL; }
276: return $this->dataStore->info->{"serverId"};
277: }
278: 279: 280: 281: 282:
283: public function getServerCurrency() {
284: if ($this->ensureInfo() === FALSE) { return NULL; }
285: return $this->dataStore->info->{"serverCurrency"};
286: }
287: 288: 289: 290: 291:
292: public function getServerName() {
293: if ($this->ensureInfo() === FALSE) { return NULL; }
294: return $this->dataStore->info->{"serverName"};
295: }
296: 297: 298: 299: 300:
301: public function getServerStore() {
302: if ($this->ensureInfo() === FALSE) { return NULL; }
303: return $this->dataStore->info->{"serverStore"};
304: }
305:
306: 307: 308: 309: 310:
311: public function getPackageCount() {
312: if ($this->ensurePackages() === FALSE) { return NULL; }
313: return count($this->dataStore->packages);
314: }
315: 316: 317: 318: 319: 320:
321: public function getPackageId($num=-1) {
322: if ($num <= -1 || $this->ensurePackages() === FALSE || $num >= count($this->dataStore->packages)) { return NULL; }
323: return $this->dataStore->packages[$num]->{"id"};
324: }
325: 326: 327: 328: 329: 330:
331: public function getPackageOrder($num=-1) {
332: if ($num <= -1 || $this->ensurePackages() === FALSE || $num >= count($this->dataStore->packages)) { return NULL; }
333: return $this->dataStore->packages[$num]->{"order"};
334: }
335: 336: 337: 338: 339: 340:
341: public function getPackageName($num=-1) {
342: if ($num <= -1 || $this->ensurePackages() === FALSE || $num >= count($this->dataStore->packages)) { return NULL; }
343: return $this->dataStore->packages[$num]->{"name"};
344: }
345: 346: 347: 348: 349: 350:
351: public function getPackageDescription($num=-1) {
352: if ($num <= -1 || $this->ensurePackages() === FALSE || $num >= count($this->dataStore->packages)) { return NULL; }
353: return $this->dataStore->packages[$num]->{"description"};
354: }
355: 356: 357: 358: 359: 360:
361: public function getPackagePrice($num=-1) {
362: if ($num <= -1 || $this->ensurePackages() === FALSE || $num >= count($this->dataStore->packages)) { return NULL; }
363: return $this->dataStore->packages[$num]->{"price"};
364: }
365:
366: 367: 368: 369: 370:
371: public function getPaymentCount() {
372: if ($this->ensurePayments() === FALSE) { return NULL; }
373: return count($this->dataStore->payments);
374: }
375: 376: 377: 378: 379: 380:
381: public function getPaymentTime($num=-1) {
382: if ($num <= -1 || $this->ensurePayments() === FALSE || $num >= count($this->dataStore->payments)) { return NULL; }
383: return $this->dataStore->payments[$num]->{"time"};
384: }
385: 386: 387: 388: 389: 390:
391: public function getPaymentPackages($num=-1) {
392: if ($num <= -1 || $this->ensurePayments() === FALSE || $num >= count($this->dataStore->payments)) { return NULL; }
393: return $this->dataStore->payments[$num]->{"packages"};
394: }
395: 396: 397: 398: 399: 400:
401: public function getPaymentPlayerName($num=-1) {
402: if ($num <= -1 || $this->ensurePayments() === FALSE || $num >= count($this->dataStore->payments)) { return NULL; }
403: return $this->dataStore->payments[$num]->{"ign"};
404: }
405: 406: 407: 408: 409: 410:
411: public function getPaymentPrice($num=-1) {
412: if ($num <= -1 || $this->ensurePayments() === FALSE || $num >= count($this->dataStore->payments)) { return NULL; }
413: return $this->dataStore->payments[$num]->{"price"};
414: }
415: 416: 417: 418: 419: 420:
421: public function getPaymentCurrency($num=-1) {
422: if ($num <= -1 || $this->ensurePayments() === FALSE || $num >= count($this->dataStore->payments)) { return NULL; }
423: return $this->dataStore->payments[$num]->{"currency"};
424: }
425:
426: 427: 428: 429: 430:
431: public function getCommandCount() {
432: if ($this->ensureCommands() === FALSE) { return NULL; }
433: return count($this->dataStore->commands);
434: }
435: 436: 437: 438: 439: 440:
441: public function getCommandPlayerName($num=-1) {
442: if ($num <= -1 || $this->ensureCommands() === FALSE || $num >= count($this->dataStore->commands)) { return NULL; }
443: return $this->dataStore->commands[$num]->{"ign"};
444: }
445: 446: 447: 448: 449: 450:
451: public function getCommandList($num=-1) {
452: if ($num <= -1 || $this->ensureCommands() === FALSE || $num >= count($this->dataStore->commands)) { return NULL; }
453: return $this->dataStore->commands[$num]->{"commands"};
454: }
455: 456: 457: 458: 459: 460:
461: public function getCommandRequireOnline($num=-1) {
462: if ($num <= -1 || $this->ensureCommands() === FALSE || $num >= count($this->dataStore->commands)) { return NULL; }
463: return $this->dataStore->commands[$num]->{"requireOnline"};
464: }
465:
466: 467: 468: 469: 470:
471: public function getClaimableCount() {
472: if ($this->ensureChecker() === FALSE) { return NULL; }
473: return count($this->dataStore->checker->{"claimables"});
474: }
475: 476: 477: 478: 479: 480:
481: public function getClaimablePlayerName($num=-1) {
482: if ($num <= -1 || $this->ensureChecker() === FALSE || $num >= count($this->dataStore->checker->{"claimables"})) { return NULL; }
483: return $this->dataStore->checker->{"claimables"}[$num]->{"ign"};
484: }
485: 486: 487: 488: 489: 490:
491: public function getClaimableCommandList($num=-1) {
492: if ($num <= -1 || $this->ensureChecker() === FALSE || $num >= count($this->dataStore->checker->{"claimables"})) { return NULL; }
493: return $this->dataStore->checker->{"claimables"}[$num]->{"commands"};
494: }
495: 496: 497: 498: 499: 500:
501: public function getClaimableRequireOnline($num=-1) {
502: if ($num <= -1 || $this->ensureChecker() === FALSE || $num >= count($this->dataStore->checker->{"claimables"})) { return NULL; }
503: return $this->dataStore->checker->{"claimables"}[$num]->{"requireOnline"};
504: }
505:
506: 507: 508: 509: 510:
511: public function getExpiryCount() {
512: if ($this->ensureChecker() === FALSE) { return NULL; }
513: return count($this->dataStore->checker->{"expirys"});
514: }
515: 516: 517: 518: 519: 520:
521: public function getExpiryPlayerName($num=-1) {
522: if ($num <= -1 || $this->ensureChecker() === FALSE || $num >= count($this->dataStore->checker->{"expirys"})) { return NULL; }
523: return $this->dataStore->checker->{"expirys"}[$num]->{"ign"};
524: }
525: 526: 527: 528: 529: 530:
531: public function getExpiryCommandList($num=-1) {
532: if ($num <= -1 || $this->ensureChecker() === FALSE || $num >= count($this->dataStore->checker->{"expirys"})) { return NULL; }
533: return $this->dataStore->checker->{"expirys"}[$num]->{"commands"};
534: }
535: 536: 537: 538: 539: 540:
541: public function getExpiryRequireOnline($num=-1) {
542: if ($num <= -1 || $this->ensureChecker() === FALSE || $num >= count($this->dataStore->checker->{"expirys"})) { return NULL; }
543: return $this->dataStore->checker->{"expirys"}[$num]->{"requireOnline"};
544: }
545:
546: 547: 548: 549: 550: 551:
552: public function getPackagesById($id=-1) {
553: if ($id <= -1 || $this->ensurePackages() === FALSE) { return NULL; }
554: $output = array();
555: $max = count($this->dataStore->packages);
556: for($i = 0; $i < $max; $i++) {
557: if ($this->dataStore->packages[$i]->{"id"} === $id) {
558: $output[] = $i;
559: }
560: }
561: return $output;
562: }
563: 564: 565: 566: 567: 568:
569: public function getPaymentsSince($time=-1) {
570: if ($time <= -1 || $this->ensurePayments() === FALSE) { return NULL; }
571: $output = array();
572: $max = count($this->dataStore->payments);
573: for($i = 0; $i < $max; $i++) {
574: if ($this->dataStore->payments[$i]->{"time"} >= $time) {
575: $output[] = $i;
576: } else {
577: break;
578: }
579: }
580: return $output;
581: }
582: 583: 584: 585: 586: 587:
588: public function getRecentPayments($amount=-1) {
589: if ($amount <= 0 || $this->ensurePayments() === FALSE) { return NULL; }
590: $output = array();
591: $max = count($this->dataStore->payments);
592: for($i = 0; $i < $max && $i < $amount; $i++) {
593: $output[] = $i;
594: }
595: return $output;
596: }
597: 598: 599: 600: 601: 602:
603: public function getPaymentsByPlayerName($playerName=NULL) {
604: if ($num === NULL || $this->ensurePayments() === FALSE) { return NULL; }
605: $output = array();
606: $max = count($this->dataStore->payments);
607: for($i = 0; $i < $max; $i++) {
608: if (strcasecmp($this->dataStore->payments[$i]->{"ign"}, $playerName) === 0) {
609: $output[] = $i;
610: }
611: }
612: return $output;
613: }
614: 615: 616: 617: 618: 619:
620: public function getPaymentsByPackageId($id=-1) {
621: if ($id <= -1 || $this->ensurePayments() === FALSE) { return NULL; }
622: $output = array();
623: $max = count($this->dataStore->payments);
624: for($i = 0; $i < $max; $i++) {
625: $max2 = count($this->dataStore->payments[$i]->{"packages"});
626: for ($j = 0; $j < $max2; $j++) {
627: if ($this->dataStore->payments[$i]->{"packages"}[$j] === $id) {
628: $output[] = $i;
629: }
630: }
631: }
632: return $output;
633: }
634:
635: 636: 637: 638: 639: 640: 641:
642: public function getBuyLink($id=-1, $name=NULL) {
643: $store = $this->getServerStore();
644: if ($id <= -1 || $store === NULL) { return NULL; }
645: $data = array("action"=>"add", "package"=>$id);
646: if ($name !== NULL) { $data["ign"] = $name; }
647: return $store."/checkout/packages?".http_build_query($data);
648: }
649: 650: 651: 652: 653: 654: 655: 656:
657: public function getBuyLinkDirect($id=-1, $name=NULL, $gateway=NULL) {
658: $store = $this->getServerStore();
659: if ($id <= -1 || $name === NULL || $store === NULL || $gateway === NULL) { return NULL; }
660: $data = array("direct"=>"true", "package"=>$id, "agreement"=>"true", "gateway"=>$gateway, "ign"=>$name);
661: return $store."/checkout/pay?".http_build_query($data);
662: }
663: }
664:
665: 666: 667: 668: 669:
670: class phpBuycraftStatus {
671: public static $OK = array(0, "Authenticated with the specified Secret key.");
672: public static $NEED_MORE_INFO = array(100, "The specified action requires more information then was provided.");
673: public static $SECRET_NOT_FOUND = array(101, "The specified Secret key could not be found.");
674: public static $UNKNOWN_ACTION = array(102, "The specified action does not exist.");
675: }
676:
677: 678: 679: 680: 681:
682: class phpBuycraftData {
683: public $info = NULL;
684: public $packages = NULL;
685: public $payments = NULL;
686: public $commands = NULL;
687: public $checker = NULL;
688: }
689: ?>
690: