Advertisement
aaaaaa123456789

PR2 user details lookup, PHP generator code

Feb 5th, 2013
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. require_once "ptempl.php";
  4.  
  5. function get_URI ($username) {
  6.   $query = http_build_query(array("target_name" => (string) $username));
  7.   $baseURI = "http://pr2hub.com/get_player_info.php";
  8.   return $baseURI . "?" . $query;
  9. }
  10.  
  11. function read_page ($URI) {
  12.   $file = @fopen($URI, 'r');
  13.   if ($file === false) return null;
  14.   $resource_data = stream_get_meta_data($file);
  15.   if ($resource_data['wrapper_type'] != 'http') {
  16.     fclose($file);
  17.     return null;
  18.   }
  19.   $data = "";
  20.   while (!feof($file))
  21.     $data .= fread($file, 1000);
  22.   fclose($file);
  23.   return $data;
  24. }
  25.  
  26. function parse_page_data ($pagedata) {
  27.   $data = null;
  28.   parse_str($pagedata, $data);
  29.   return $data;
  30. }
  31.  
  32. function get_page_data ($URI) {
  33.   $data = read_page($URI);
  34.   if ($data === null) return null;
  35.   return parse_page_data ($data);
  36. }
  37.  
  38. function get_user_data ($username) {
  39.   $URI = get_URI($username);
  40.   return get_page_data($URI);
  41. }
  42.  
  43. function get_color ($usergroup) {
  44.   $usergroup = (int) $usergroup;
  45.   switch ($usergroup) {
  46.     case 3: return "#870a6f";
  47.     case 2: return "#1c369f";
  48.     case 1: return "#047b7b";
  49.     default: return "#7e7f7f";
  50.   }
  51. }
  52.  
  53. function parse_status ($status) {
  54.   if (strpos($status, "Platform Racing 2") === false) return $status;
  55.   $lastspace = strrpos($status, " ");
  56.   return substr($status, $lastspace + 1);
  57. }
  58.  
  59. function process_user ($username) {
  60.   $username = (string) $username;
  61.   $data = get_user_data($username);
  62.   if ($data === null) return "could not reach pr2hub.com";
  63.   if (@$data['error'] != '') return "user does not exist";
  64.   return array(
  65.     'username' => htmlspecialchars($username),
  66.     'color' => get_color($data['group']),
  67.     'rank' => ($data['rank'] == '') ? '--' : (string) $data['rank'],
  68.     'hats' => (string) $data['hats'],
  69.     'registration' => ($data['registerDate'] == '1st of January 1970') ? 'Age of Heroes' : $data['registerDate'],
  70.     'login' => ($data['loginDate'] == '1st of January 1970') ? 'Unknown' : $data['loginDate'],
  71.     'server' => parse_status($data['status'])
  72.   );
  73. }
  74.  
  75. class UserInformation {
  76.  
  77.   private $usernames = null;
  78.   private $userdata = null;
  79.   private $userPosition = 0;
  80.   private $errorPosition = 0;
  81.  
  82.   public function build ($users) {
  83.     $this -> userdata = array();
  84.     $this -> usernames = array();
  85.     $pos = 0;
  86.     foreach ($users as $user)
  87.       if ($user != "") {
  88.         $this -> usernames[$pos] = (string) $user;
  89.         $this -> userdata[$pos ++] = process_user($user);
  90.       }
  91.     $this -> reset();
  92.   }
  93.  
  94.   public function reset () {
  95.     $this -> position = 0;
  96.   }
  97.  
  98.   public function nextUser () {
  99.     if ($this -> userdata === null) return false;
  100.     $data = @$this -> userdata[$this -> userPosition ++];
  101.     if ($this -> userPosition > $this -> userCount()) return false;
  102.     if (!is_array($data)) return false;
  103.     return $data;
  104.   }
  105.  
  106.   public function nextError () {
  107.     if ($this -> userdata === null) return false;
  108.     $name = @$this -> usernames[$this -> errorPosition];
  109.     $data = @$this -> userdata[$this -> errorPosition ++];
  110.     if ($this -> errorPosition > $this -> userCount()) return false;
  111.     if (is_array($data)) return false;
  112.     return array(
  113.       'username' => $name,
  114.       'error' => $data
  115.     );
  116.   }
  117.  
  118.   public function userCount () {
  119.     return ($this -> userdata === null) ? 0 : count($this -> userdata);
  120.   }
  121.  
  122. }
  123.  
  124. function hide () {
  125.   return false;
  126. }
  127.  
  128. function generate_page($template, $users) {
  129.   $template_data = file_get_contents($template);
  130.   if ($template_data === false) die("Could not read page data");
  131.   $user_data = new UserInformation();
  132.   $user_data -> build($users);
  133.   $callbacks = array(
  134.     'single_user' => (($user_data -> userCount()) == 1) ? array($user_data, 'nextUser') : 'hide',
  135.     'single_error' => (($user_data -> userCount()) == 1) ? array($user_data, 'nextError') : 'hide',
  136.     'multiple_users' => function ($users) {return (bool) (($users -> userCount()) > 1);},
  137.     'user' => array($user_data, 'nextUser'),
  138.     'error' => array($user_data, 'nextError'),
  139.     'row' => function ($users) {return $users -> userCount();},
  140.     'form' => function () {return array(1 => (string) $_SERVER['PHP_SELF']);}
  141.   );
  142.   return parse_template($template_data, $callbacks, $user_data);
  143. }
  144.  
  145. function generate_page_no_users($template) {
  146.   $template_data = file_get_contents($template);
  147.   if ($template_data === false) die("Could not read page data");
  148.   $callback = function ($tag) {
  149.     if ($tag == 'form')
  150.       return array(1 => (string) $_SERVER['PHP_SELF']);
  151.     else
  152.       return false;
  153.   };
  154.   return parse_template($template_data, $callback);
  155. }
  156.  
  157. function parse_user_data($user_string) {
  158.   $user_string = (string) $user_string;
  159.   if (strpos($user_string, "\r\n") !== false)
  160.     $sep = "\r\n";
  161.   else if (strpos($user_string, "\n") !== false)
  162.     $sep = "\n";
  163.   else if (strpos($user_string, "\r") !== false)
  164.     $sep = "\r";
  165.   else
  166.     $sep = null;
  167.   if ($sep === null)
  168.     if ($user_string == "")
  169.       return null;
  170.     else
  171.       return array(0 => $user_string);
  172.   return explode($sep, $user_string);
  173. }
  174.  
  175. function get_page ($template) {
  176.   if (isset($_REQUEST['users'])) {
  177.     $userdata = $_REQUEST['users'];
  178.     if ($userdata === null) return generate_page_no_users($template);
  179.     $users = parse_user_data($userdata);
  180.     return generate_page($template, $users);
  181.   }
  182.   return generate_page_no_users($template);
  183. }
  184.  
  185. echo get_page("usertmpl.htm");
  186.  
  187. ?>
Advertisement
RAW Paste Data Copied
Advertisement