Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.14 KB | None | 0 0
  1. <?php
  2. define('COOKIE_FILE', 'cookie.txt');
  3. define('BUNGIE_URL', 'https://www.bungie.net');
  4. define('API_KEY', '{myapigoeshere}');
  5. define('USER_AGENT', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
  6.  
  7. define('SETTING_FILE', 'settings.json');
  8.  
  9. $default_options = array(
  10.     CURLOPT_USERAGENT => USER_AGENT,
  11.     CURLOPT_COOKIEJAR => COOKIE_FILE,
  12.     CURLOPT_COOKIEFILE => COOKIE_FILE,
  13.     CURLOPT_RETURNTRANSFER => true,
  14.     CURLOPT_FOLLOWLOCATION => true,
  15.     CURLOPT_SSL_VERIFYHOST => 2,
  16. );
  17.  
  18. function loadSettings() {
  19.     if (!file_exists(SETTING_FILE)) return new stdClass();
  20.     return json_decode(file_get_contents(SETTING_FILE));
  21. }
  22. function setSetting($name, $value) {
  23.     $settings = loadSettings();
  24.     $settings->{$name} = $value;
  25.     file_put_contents(SETTING_FILE, json_encode($settings));
  26. }
  27. function getSetting($name) {
  28.     $settings = loadSettings();
  29.     if (isset($settings->{$name})) return $settings->{$name};
  30.     return '';
  31. }
  32.  
  33. function parseCookieFile($file) {
  34.     $cookies = array();
  35.     if (file_exists($_SERVER['DOCUMENT_ROOT'].'/'.$file)) {
  36.         $lines = file($file);
  37.         foreach($lines as $line) {
  38.             if (substr_count($line, "\t") == 6) {
  39.                 $tokens = explode("\t", $line);
  40.                 $tokens = array_map('trim', $tokens);
  41.  
  42.                 $domain = preg_replace('/#[^_]+_/i', '', $tokens[0]);
  43.                 $flag = $tokens[1] == 'TRUE';
  44.                 $path = $tokens[2];
  45.                 $secure = $tokens[3] == 'TRUE';
  46.                 $expiration = $tokens[4];
  47.                 $name = $tokens[5];
  48.                 $value = $tokens[6];
  49.                 if (!isset($cookies[$domain])) $cookies[$domain] = array();
  50.                 $cookies[$domain][$name] = array(
  51.                     'flag' => $flag,
  52.                     'path' => $path,
  53.                     'secure' => $secure,
  54.                     'expiration' => $expiration,
  55.                     'value' => $value
  56.                 );
  57.             }
  58.         }
  59.     }
  60.     return $cookies;
  61. }
  62.  
  63. function doRequest($path) {
  64.     global $default_options;
  65.  
  66.     $cookies = parseCookieFile(COOKIE_FILE);
  67.     $bungieCookies = isset($cookies['www.bungie.net']) ? $cookies['www.bungie.net'] : array();
  68.  
  69.     $ch = curl_init(BUNGIE_URL.$path);
  70.     curl_setopt_array($ch, $default_options);
  71.     curl_setopt_array($ch, array(
  72.         CURLOPT_HTTPHEADER => array(
  73.             'x-api-key: '.API_KEY,
  74.             'x-csrf: '.(isset($bungieCookies['bungled']) ? $bungieCookies['bungled']['value'] : '')
  75.         )
  76.     ));
  77.     $response = curl_exec($ch);
  78.     curl_close($ch);
  79.  
  80.     return json_decode($response);
  81. }
  82.  
  83. function updateManifest($url) {
  84.     $ch = curl_init(BUNGIE_URL.$url);
  85.     curl_setopt_array($ch, array(
  86.         CURLOPT_RETURNTRANSFER => true
  87.     ));
  88.     $data = curl_exec($ch);
  89.     curl_close($ch);
  90.  
  91.     $cacheFilePath = 'cache/'.pathinfo($url, PATHINFO_BASENAME);
  92.     if (!file_exists(dirname($cacheFilePath))) mkdir(dirname($cacheFilePath), 0777, true);
  93.     file_put_contents($cacheFilePath.'.zip', $data);
  94.  
  95.     $zip = new ZipArchive();
  96.     if ($zip->open($cacheFilePath.'.zip') === TRUE) {
  97.         $zip->extractTo('cache');
  98.         $zip->close();
  99.     }
  100.  
  101.     $tables = array();
  102.     if ($db = new SQLite3($cacheFilePath)) {
  103.         $result = $db->query("SELECT name FROM sqlite_master WHERE type='table'");
  104.         while($row = $result->fetchArray()) {
  105.             $table = array();
  106.             $result2 = $db->query("PRAGMA table_info(".$row['name'].")");
  107.             while($row2 = $result2->fetchArray()) {
  108.                 $table[] = $row2[1];
  109.             }
  110.             $tables[$row['name']] = $table;
  111.         }
  112.     }
  113.  
  114.     return $tables;
  115. }
  116.  
  117. function checkManifest() {
  118.     // Checking for Manifest changes.
  119.     $result = doRequest('/Platform/Destiny/Manifest/');
  120.  
  121.     // Grab the path of the language you want
  122.     $database = $result->Response->mobileWorldContentPaths->en;
  123.  
  124.     // Check to see if had been changed
  125.     if ($database != getSetting('database')) {
  126.         // New database found.
  127.         $tables = updateManifest($database);
  128.         setSetting('database', $database);
  129.         setSetting('tables', $tables);
  130.     }
  131. }
  132.  
  133. function queryManifest($query) {
  134.     $database = getSetting('database');
  135.     $cacheFilePath = 'cache/'.pathinfo($database, PATHINFO_BASENAME);
  136.  
  137.     $results = array();
  138.     if ($db = new SQLite3($cacheFilePath)) {
  139.         $result = $db->query($query);
  140.         while($row = $result->fetchArray()) {
  141.             $key = is_numeric($row[0]) ? sprintf('%u', $row[0] & 0xFFFFFFFF) : $row[0];
  142.             $results[$key] = json_decode($row[1]);
  143.         }
  144.     }
  145.     return $results;
  146. }
  147.  
  148. function getDefinition($tableName) {
  149.     return queryManifest('SELECT * FROM '.$tableName);
  150. }
  151.  
  152. function getSingleDefinition($tableName, $id) {
  153.     $tables = getSetting('tables');
  154.  
  155.     $key = $tables->{$tableName}[0];
  156.     $where = ' WHERE '.(!is_numeric($id) ? $key.'='.$id.' OR '.$key.'='.($id-4294967296) : $key.'="'.$id.'"');
  157.     $results = queryManifest('SELECT * FROM '.$tableName.$where);
  158.  
  159.     return isset($results[$id]) ? $results[$id] : false;
  160. }
  161.  
  162. checkManifest();
  163.  
  164.  
  165. // Get the InventoryItemDefinition for Gjallarhorn
  166. $item = getSingleDefinition('DestinyInventoryItemDefinition', 1274330687);
  167.  
  168. // Some examples of referencing attributes of the definition object
  169. $icon = 'https://bungie.net' . $item->icon;
  170. $name = $item->itemName;
  171. $description = $item->itemDescription;
  172.  
  173. // Iterating through item stats
  174. foreach($item->stats as $statRef) {
  175. // We need to get the stat definition too
  176. $stat = getSingleDefinition('DestinyStatDefinition', $statRef->statHash);
  177. $statName = $stat->statName;
  178. $statValue = $statRef->value;
  179. }
  180.  
  181. //Gjalla icon thanks to lowlines, thanks bro!
  182. echo '<img src="'.$icon.'" alt="Cover">';
  183.  
  184. //this is what i'm trying to do to output what xur is selling failing
  185. echo '<pre>DestinyInventoryBucketDefinition: '.json_encode(getDefinition('GetPublicXurVendor'), JSON_PRETTY_PRINT).'</pre>';
  186.  
  187. //what should I try to write to get all the icons of what xur is selling like we did before with the Gjalla?
  188.  
  189. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement