Advertisement
djazz

SGIP - list.php - Getting list of servers as JSON

Mar 13th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. // For SGIP:
  3. // http://djazz.mine.nu/apps/sgip/
  4.  
  5. header("content-type: text/plain");
  6. header("Access-Control-Allow-Origin: *");
  7. set_time_limit(5);
  8.  
  9. $list = array();
  10.  
  11. if(isset($_GET['listserver']) && strlen($_GET['listserver']) > 0) {
  12.     $listserver = $_GET['listserver'];
  13. }
  14. else {
  15.     $listserver = "list2.digiex.net";
  16. }
  17.  
  18. $f = @fsockopen($listserver, 10057, $errno, $errstr, 5);
  19.  
  20. if(!$f) {
  21.     echo "$errstr ($errno)\n".json_encode($list);
  22.     exit;
  23. }
  24. stream_set_blocking($f, 0);
  25. $raw = "";
  26. while ($f && !feof($f) && connection_status()===0) {
  27.     $raw .= @fgets($f, 1024);
  28. }
  29. fclose($f);
  30.  
  31. $raw = explode("\n", $raw);
  32. $parts = array();
  33.  
  34. for($i=0; $i < count($raw)-1; $i++) {
  35.     $parts = array(); // Clean the array
  36.     $parts[0] = explode(" ", $raw[$i], 5); // Split up the first parts
  37.     $parts[1][0] = substr($parts[0][4], 2, 4); // Version
  38.     $parts[2] = explode(" ", substr($parts[0][4], 7), 3); // Split up the last parts
  39.     array_pop($parts[0]); // Remove the extra part from the first parts
  40.     $parts = array_merge($parts[0], $parts[1], $parts[2]); // Merge the arrays
  41.     $parts[0] = explode(":", $parts[0]); // IP:port
  42.     $parts[0][1] = +$parts[0][1]; // Force number
  43.     $parts[5] = +$parts[5]; // ...
  44.     $parts[6] = explode("/", substr($parts[6], 1, -1)); // Capacity
  45.     $parts[6][0] = +$parts[6][0]; // Force number
  46.     $parts[6][1] = +$parts[6][1]; // ...
  47.     $parts[7] = utf8_encode(htmlentities(str_replace("\r", "", $parts[7]))); // Remove "\r" in name if any and replace html
  48.     $list[$i] = $parts; // Store in final array
  49. }
  50.  
  51. echo "\n".json_encode($list); // Make it JSON encoded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement