|
|
@@ -0,0 +1,59 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+function add_maintainer( $api ) {
|
|
|
+ /**
|
|
|
+ * @param string $api Provided Torn API string
|
|
|
+ * @return array XID and name of maintainer
|
|
|
+ */
|
|
|
+ $maintainer = array(0, '?');
|
|
|
+ $url = "https://api.torn.com/user/?key=";
|
|
|
+ $url .= $api;
|
|
|
+
|
|
|
+ $response = json_decode( file_get_contents( $url ) );
|
|
|
+ if ( property_exists( $response, "error" ) ) {
|
|
|
+ print "ERROR " . $response->error->code . ": " . $response->error->error . "\n";
|
|
|
+ $maintainer[0] = $response->error->code;
|
|
|
+ $maintainer[1] = $response->error->error;
|
|
|
+ } else {
|
|
|
+ $pdo = null;
|
|
|
+ try {
|
|
|
+ $pdo = new PDO("sqlite:" . './slork.sqlite');
|
|
|
+ } catch (PDOException $e) {
|
|
|
+ // handle the exception here
|
|
|
+ }
|
|
|
+ $query = "INSERT OR REPLACE INTO maintainer (maintainer, api) VALUES (?, ?);";
|
|
|
+ $stmt = $pdo->prepare( $query );
|
|
|
+ $stmt->execute( [$response->player_id, $api] );
|
|
|
+ $query = "INSERT OR REPLACE INTO player (maintainer, player, property, value) VALUES (?, ?, ?, ?);";
|
|
|
+ $stmt = $pdo->prepare( $query );
|
|
|
+ $stmt->execute( [$response->player_id, $response->player_id,
|
|
|
+ 'name', $response->name] );
|
|
|
+ // $pdo->commit();
|
|
|
+ $maintainer[0] = $response->player_id;
|
|
|
+ $maintainer[1] = $response->name;
|
|
|
+ }
|
|
|
+ return $maintainer;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function get_maintainer( $id ) {
|
|
|
+ /**
|
|
|
+ * @param string $id Maintainer's XID
|
|
|
+ * @return array XID and name of maintainer
|
|
|
+ */
|
|
|
+ $pdo = null;
|
|
|
+ try {
|
|
|
+ $pdo = new PDO("sqlite:" . './slork.sqlite');
|
|
|
+ } catch (PDOException $e) {
|
|
|
+ // handle the exception here
|
|
|
+ }
|
|
|
+ $query = "SELECT value FROM player WHERE maintainer = ? AND player = ? AND property = 'name';";
|
|
|
+ $stmt = $pdo->prepare( $query );
|
|
|
+ $stmt->execute( [$id, $id] );
|
|
|
+ $maintainer = [0, '?'];
|
|
|
+ $row = $stmt->fetchAll();
|
|
|
+ $maintainer[0] = $id;
|
|
|
+ $maintainer[1] = $row['value'];
|
|
|
+
|
|
|
+ return $maintainer;
|
|
|
+}
|