functions.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }
  55. function delete_player( $player ) {
  56. $pdo = null;
  57. try {
  58. $pdo = new PDO("sqlite:" . './slork.sqlite');
  59. } catch (PDOException $e) {
  60. // handle the exception here
  61. }
  62. $maintainer_id = $_REQUEST['maintainer'];
  63. $query = "DELETE FROM player WHERE player = ? AND maintainer = ?;";
  64. $stmt = $pdo->prepare( $query );
  65. $stmt->execute( [$player, $maintainer_id] );
  66. $maintainer = get_maintainer( $maintainer_id );
  67. if ( $player == $maintainer_id ) {
  68. // Poof
  69. $query = "DELETE FROM maintainer WHERE maintainer = ?";
  70. $stmt = $pdo->prepare( $query );
  71. $stmt->execute( [$maintainer] );
  72. unset( $_REQUEST['action'] );
  73. unset( $_REQUEST['maintainer'] );
  74. unset( $_REQUEST['player'] );
  75. $maintainer = [0, '?'];
  76. }
  77. return $maintainer;
  78. }