Advertisement
Guest User

Untitled

a guest
Jun 1st, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. <?php
  2. //This class connects PHP with SA:MP Servers (v0.1)
  3. //Original file: http://team.sa-mp.com/sampqry.php.txt
  4. class SAMP_Socket{
  5. //By jose.rob.jr
  6. //Set the address to connect, can be IP or Host
  7. var $addr="";
  8. //Set The port to connect
  9. var $port=0;
  10. //Generate or not XML Document
  11. var $xml=false;
  12. //Give the last result
  13. var $last=array();
  14. //Try to get the IP/Host (may be slow)
  15. var $getHost=false;
  16. //Private variables
  17. private $give=1;
  18. private $fp;
  19. private $ini="";
  20. private $Txml=false;
  21. //Read an address, you can call without parameters if you have set $addr has the exemple in the bottom
  22. function read($addr=null,$port=null,$xml=null){
  23. //This function sets a time limit to complete the page
  24. //the connection will never ends if the server is down without this.
  25.  
  26. //This function changes the Content-Type header to application/xml if xml is set to true
  27. //If you don't want it, you can print anything (a blank space for exemple) before calling read()
  28. //or setting returnXml instead and reading the array from $last
  29.  
  30. //This function won't show PHP Erros when xml is set to true
  31. set_time_limit(20);
  32. $last=&$this->last;
  33. $fp=&$this->fp;
  34. $this->Txml=&$xml;
  35. if(!$addr&&!$this->addr) $this->erro("Called ".__CLASS__."::open() without setting an address.",true);
  36. if(!$addr&&$this->addr) $addr=$this->addr;
  37. elseif($addr&&!$this->addr) $this->addr=$addr;
  38. if(preg_match('/^.*\:[0-9]+$/',$addr)){
  39. $port=substr(strrchr($addr,":"),1);
  40. $this->port=$port;
  41. $addr=preg_replace('/^(.*):[0-9]+$/','\1',$addr);
  42. $this->addr=$addr;
  43. }
  44. elseif(!$port&&$this->port) $port=$this->port;
  45. elseif($port&&!$this->port) $this->port=$port;
  46. else{
  47. $port=7777;
  48. $this->port=$port;
  49. }
  50. if(preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',$addr)){
  51. if($this->getHost){
  52. $host=getHostByAddr($addr);
  53. if($host==getHostByName($host)) $host=$addr;
  54. }
  55. else{
  56. $host="ignored";
  57. }
  58. $ip=$addr;
  59. }
  60. else{
  61. if($this->getHost) $ip=getHostByName($addr);
  62. else $ip=$addr;
  63. $host=$addr;
  64. }
  65. if($xml===null) $xml=$this->xml;
  66. if($xml){
  67. @header("Content-Type: application/xml");
  68. $this->ini=@ini_get('display_errors');
  69. @ini_set('display_errors','0');
  70. }
  71.  
  72. if($this->give==2){
  73. ob_start();
  74. if($xml===true) $xml=2;
  75. else $xml=1;
  76. }
  77.  
  78. if($xml) echo "<"."?xml version='1.0' encoding='iso-8859-1'?"."><server>";
  79. $fp = @fsockopen('udp://' . $ip, $port, $errno, $errstr,3) or $this->erro($errstr,__LINE__,__FILE__,$errno);
  80. if($fp){
  81. $packet = 'SAMP';
  82. $packet .= chr(strtok($ip, '.'));
  83. $packet .= chr(strtok('.'));
  84. $packet .= chr(strtok('.'));
  85. $packet .= chr(strtok('.'));
  86. $packet .= chr($port & 0xFF);
  87. $packet .= chr($port >> 8 & 0xFF);
  88.  
  89. fwrite($fp, $packet.'i');
  90. stream_set_timeout($fp,10);
  91. fread($fp, 11);
  92. $conInfo=stream_get_meta_data($fp);
  93. if($conInfo['timed_out']){
  94. $this->erro("Connection timed out!");
  95. }
  96. else{
  97. $is_passworded = ord(fread($fp, 1));
  98. $plr_count = ord(fread($fp, 2));
  99. $max_plrs = ord(fread($fp, 2));
  100. $strlen = ord(fread($fp, 4));
  101. $hostname = fread($fp, $strlen);
  102. $strlen = ord(fread($fp, 4));
  103. $gamemode = fread($fp, $strlen);
  104. $strlen = ord(fread($fp, 4));
  105. $mapname = fread($fp, $strlen);
  106.  
  107. $last['server']['ip']=$ip;
  108. $last['server']['host']=$host;
  109. $last['server']['hostname']=$hostname;
  110. $last['server']['mode']=$gamemode;
  111. $last['server']['mapa']=$mapname;
  112. $last['server']['passworded']=(bool)$is_passworded;
  113. $last['server']['players']['atual']=$plr_count;
  114. $last['server']['players']['maximo']=$max_plrs;
  115.  
  116. if(!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',$ip)) $ip="ignored";
  117.  
  118. if($xml){
  119. echo '<hostname>' . htmlentities($hostname) . '</hostname>';
  120. echo '<ip>' . $ip.($ip!='ignored'?":$port":'') . "</ip>";
  121. echo '<host>'. $host.($host!='ignored'?":$port":'') . '</host>';
  122. echo '<mode>' . htmlentities($gamemode) . '</mode>';
  123. echo '<mapa>' . htmlentities($mapname) . '</mapa>';
  124. echo '<passworded>';
  125. if ($is_passworded){
  126. echo 'yes';
  127. } else {
  128. echo 'no';
  129. }
  130. echo '</passworded>';
  131. echo '<players><atual>' . htmlentities($plr_count) . '</atual><max>' . htmlentities($max_plrs) . '</max>';
  132. }
  133.  
  134. fwrite($fp, $packet.'c');
  135. fread($fp, 11);
  136. $plr_count = ord(fread($fp, 2));
  137. if($xml) echo '<lista>';
  138. if ($plr_count > 0)
  139. {
  140. for ($i=0; $i<$plr_count; $i++)
  141. {
  142. $strlen = ord(fread($fp, 1));
  143. $plrname = fread($fp, $strlen);
  144. $score = $this->samp_getLong(fread($fp, 4));
  145.  
  146. $last['server']['players']['lista'][$i]['nick']=$plrname;
  147. $last['server']['players']['lista'][$i]['score']=$score;
  148. if($xml) echo '<player><nick>' . htmlentities($plrname) . '</nick><score>' . htmlentities($score) . '</score></player>';
  149. }
  150. }
  151. if($xml) echo '</lista></players>';
  152. }
  153. fclose($fp);
  154. }
  155. if($xml) echo "</server>";
  156.  
  157. if($xml){
  158. @ini_set("display_errors",$this->ini);
  159. $this->ini="";
  160. }
  161. if($xml===1){
  162. $ret=ob_get_contents();
  163. ob_end_clean();
  164. $this->Txml=false;
  165. return $ret;
  166. }
  167. elseif($xml===2){
  168. $ret=ob_get_contents();
  169. ob_end_flush();
  170. $this->Txml=false;
  171. return $ret;
  172. }
  173. $this->Txml=false;
  174. return $last;
  175. }
  176. //This function does the read function print an XML File when called
  177. function xml(){
  178. $this->xml=true;
  179. }
  180. //This does the inverse of xml()
  181. function notXml(){
  182. $this->xml=false;
  183. }
  184. //This function does the read function return an array
  185. function returnArray(){
  186. $this->give=1;
  187. }
  188. //This function does the read function return an XML File
  189. function returnXml(){
  190. $this->give=2;
  191. }
  192. //This function does the read function trys to get IP/Host address (may be slow)
  193. function getHost(){
  194. $this->getHost=true;
  195. }
  196. //This function does the read function just get the information without trying to get the IP/Host Address
  197. function notGetHost(){
  198. $this->getHost=false;
  199. }
  200. //Private functions
  201. private function erro($string,$line=null,$file=null,$number=null,$die=false){
  202. if($line===true){
  203. $line=null;
  204. $die=true;
  205. }
  206. if($this->Txml) echo "<erros><fatal>Socket Error: ".htmlentities($string).($number!==null?" (#".htmlentities($number).")":'').($file?" in $file":'').($line?" on line $line":'')."</fatal></erros>";
  207. else echo "<br/>\r\n<b>Socket Error:</b> ".htmlentities($string).($number!==null?" (#".htmlentities($number).")":'').($file?" in <b>$file</b>":'').($line?" on line <b>$line</b>":'')."<br/>\r\n";
  208. if($die) die();
  209. }
  210. private function samp_getLong($dat) {
  211. $num=0;
  212. if((ord(substr($dat,3,1)) & 128) > 0) {
  213. for ($i=0; $i<strlen($dat); $i++) {
  214. $num-=((255-ord(substr($dat,$i,1))) << 8*$i);
  215. }
  216. $num--;
  217. }
  218. else {
  219. for ($i=0; $i<strlen($dat); $i++) {
  220. $num+=(ord(substr($dat,$i,1)) << 8*$i);
  221. }
  222. }
  223. return $num;
  224. }
  225. }
  226. //Exemples:
  227. /*
  228. $samp=new SAMP_Socket;
  229. $info=$samp->read("66.197.126.19:7777");
  230. @header("Content-Type: text/plain");
  231. print_r($info);
  232. */ /*
  233. $samp=new SAMP_Socket;
  234. $info=$samp->read("66.197.126.19",7777,true);
  235. */ /*
  236. $samp=new SAMP_Socket;
  237. $samp->xml();
  238. $samp->read("66.197.126.19",7777);
  239. */ /*
  240. $samp=new SAMP_Socket;
  241. $samp->xml();
  242. $samp->getHost();
  243. $samp->read("66.197.126.19");
  244. */ /*
  245. $samp=new SAMP_Socket;
  246. $samp->addr="66.197.126.19";
  247. $samp->port=7777;
  248. $samp->xml();
  249. $samp->read();
  250. */ /*
  251. $samp=new SAMP_Socket;
  252. $samp->returnXml();
  253. $xml=$samp->read("66.197.126.19:7777");
  254. echo
  255. "<html><head><title>Exemple -- SAMP Socket</title></head>
  256. <body style='text-align: center;'><form><textarea rows='10' cols='100'>$xml</textarea>
  257. </form></body></html>"
  258. */
  259. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement