functions.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. function add_maintainer( $api ) {
  3. /**
  4. * @param string $api Provided Torn API string
  5. * @return array XID and name of maintainer
  6. */
  7. $maintainer = array(0, '?');
  8. $url = "https://api.torn.com/user/?key=";
  9. $url .= $api;
  10. $response = json_decode( file_get_contents( $url ) );
  11. if ( property_exists( $response, "error" ) ) {
  12. print "ERROR " . $response->error->code . ": " . $response->error->error . "\n";
  13. $maintainer[0] = $response->error->code;
  14. $maintainer[1] = $response->error->error;
  15. } else {
  16. $pdo = null;
  17. try {
  18. $pdo = new PDO("sqlite:" . './slork.sqlite');
  19. } catch (PDOException $e) {
  20. // handle the exception here
  21. }
  22. $query = "INSERT OR REPLACE INTO maintainer (maintainer, api) VALUES (?, ?);";
  23. $stmt = $pdo->prepare( $query );
  24. $stmt->execute( [$response->player_id, $api] );
  25. $query = "INSERT OR REPLACE INTO player (maintainer, player, property, value) VALUES (?, ?, ?, ?);";
  26. $stmt = $pdo->prepare( $query );
  27. $stmt->execute( [$response->player_id, $response->player_id,
  28. 'name', $response->name] );
  29. // $pdo->commit();
  30. $maintainer[0] = $response->player_id;
  31. $maintainer[1] = $response->name;
  32. }
  33. return $maintainer;
  34. }
  35. function get_maintainer( $id ) {
  36. /**
  37. * @param string $id Maintainer's XID
  38. * @return array XID and name of maintainer
  39. */
  40. $pdo = null;
  41. try {
  42. $pdo = new PDO("sqlite:" . './slork.sqlite');
  43. } catch (PDOException $e) {
  44. // handle the exception here
  45. }
  46. $query = "SELECT value FROM player WHERE maintainer = ? AND player = ? AND property = 'name';";
  47. $stmt = $pdo->prepare( $query );
  48. $stmt->execute( [$id, $id] );
  49. $maintainer = [0, '?'];
  50. $row = $stmt->fetchAll();
  51. $maintainer[0] = $id;
  52. $maintainer[1] = $row['value'];
  53. return $maintainer;
  54. }