aaaaaa123456789 icon

PR2 user details lookup, PHP generator code

aaaaaa123456789 | PRO | 02/06/13 12:19:07 AM UTC | 0 ⭐ | 430 👁️ | Never ⏰ | []
PHP |

5.41 KB

|

None

|

0 👍

/

0 👎

<?php
 
require_once "ptempl.php";
 
function get_URI ($username) {
  $query = http_build_query(array("target_name" => (string) $username));
  $baseURI = "http://pr2hub.com/get_player_info.php";
  return $baseURI . "?" . $query;
}
 
function read_page ($URI) {
  $file = @fopen($URI, 'r');
  if ($file === false) return null;
  $resource_data = stream_get_meta_data($file);
  if ($resource_data['wrapper_type'] != 'http') {
    fclose($file);
    return null;
  }
  $data = "";
  while (!feof($file))
    $data .= fread($file, 1000);
  fclose($file);
  return $data;
}
 
function parse_page_data ($pagedata) {
  $data = null;
  parse_str($pagedata, $data);
  return $data;
}
 
function get_page_data ($URI) {
  $data = read_page($URI);
  if ($data === null) return null;
  return parse_page_data ($data);
}
 
function get_user_data ($username) {
  $URI = get_URI($username);
  return get_page_data($URI);
}
 
function get_color ($usergroup) {
  $usergroup = (int) $usergroup;
  switch ($usergroup) {
    case 3: return "#870a6f";
    case 2: return "#1c369f";
    case 1: return "#047b7b";
    default: return "#7e7f7f";
  }
}
 
function parse_status ($status) {
  if (strpos($status, "Platform Racing 2") === false) return $status;
  $lastspace = strrpos($status, " ");
  return substr($status, $lastspace + 1);
}
 
function process_user ($username) {
  $username = (string) $username;
  $data = get_user_data($username);
  if ($data === null) return "could not reach pr2hub.com";
  if (@$data['error'] != '') return "user does not exist";
  return array(
    'username' => htmlspecialchars($username),
    'color' => get_color($data['group']),
    'rank' => ($data['rank'] == '') ? '--' : (string) $data['rank'],
    'hats' => (string) $data['hats'],
    'registration' => ($data['registerDate'] == '1st of January 1970') ? 'Age of Heroes' : $data['registerDate'],
    'login' => ($data['loginDate'] == '1st of January 1970') ? 'Unknown' : $data['loginDate'],
    'server' => parse_status($data['status'])
  );
}
 
class UserInformation {
 
  private $usernames = null;
  private $userdata = null;
  private $userPosition = 0;
  private $errorPosition = 0;
  
  public function build ($users) {
    $this -> userdata = array();
    $this -> usernames = array();
    $pos = 0;
    foreach ($users as $user)
      if ($user != "") {
        $this -> usernames[$pos] = (string) $user;
        $this -> userdata[$pos ++] = process_user($user);
      }
    $this -> reset();
  }
  
  public function reset () {
    $this -> position = 0;
  }
  
  public function nextUser () {
    if ($this -> userdata === null) return false;
    $data = @$this -> userdata[$this -> userPosition ++];
    if ($this -> userPosition > $this -> userCount()) return false;
    if (!is_array($data)) return false;
    return $data;
  }
  
  public function nextError () {
    if ($this -> userdata === null) return false;
    $name = @$this -> usernames[$this -> errorPosition];
    $data = @$this -> userdata[$this -> errorPosition ++];
    if ($this -> errorPosition > $this -> userCount()) return false;
    if (is_array($data)) return false;
    return array(
      'username' => $name,
      'error' => $data
    );
  }
  
  public function userCount () {
    return ($this -> userdata === null) ? 0 : count($this -> userdata);
  }
 
}
 
function hide () {
  return false;
}
 
function generate_page($template, $users) {
  $template_data = file_get_contents($template);
  if ($template_data === false) die("Could not read page data");
  $user_data = new UserInformation();
  $user_data -> build($users);
  $callbacks = array(
    'single_user' => (($user_data -> userCount()) == 1) ? array($user_data, 'nextUser') : 'hide',
    'single_error' => (($user_data -> userCount()) == 1) ? array($user_data, 'nextError') : 'hide',
    'multiple_users' => function ($users) {return (bool) (($users -> userCount()) > 1);},
    'user' => array($user_data, 'nextUser'),
    'error' => array($user_data, 'nextError'),
    'row' => function ($users) {return $users -> userCount();},
    'form' => function () {return array(1 => (string) $_SERVER['PHP_SELF']);}
  );
  return parse_template($template_data, $callbacks, $user_data);
}
 
function generate_page_no_users($template) {
  $template_data = file_get_contents($template);
  if ($template_data === false) die("Could not read page data");
  $callback = function ($tag) {
    if ($tag == 'form')
      return array(1 => (string) $_SERVER['PHP_SELF']);
    else
      return false;
  };
  return parse_template($template_data, $callback);
}
 
function parse_user_data($user_string) {
  $user_string = (string) $user_string;
  if (strpos($user_string, "\r\n") !== false)
    $sep = "\r\n";
  else if (strpos($user_string, "\n") !== false)
    $sep = "\n";
  else if (strpos($user_string, "\r") !== false)
    $sep = "\r";
  else
    $sep = null;
  if ($sep === null)
    if ($user_string == "")
      return null;
    else
      return array(0 => $user_string);
  return explode($sep, $user_string);
}
 
function get_page ($template) {
  if (isset($_REQUEST['users'])) {
    $userdata = $_REQUEST['users'];
    if ($userdata === null) return generate_page_no_users($template);
    $users = parse_user_data($userdata);
    return generate_page($template, $users);
  }
  return generate_page_no_users($template);
}
 
echo get_page("usertmpl.htm");
 
?>

Comments