Advertisement
Guest User

SA:MP RCON API

a guest
Jan 19th, 2012
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.79 KB | None | 0 0
  1. <?php
  2. /**
  3.  *  This API also connects directly to the server, but instead of using the
  4.  *  query system, depends on the RCON system.
  5.  * 
  6.  *  This system, unlike the query system (in my opinion) is not able to
  7.  *  handle as many calls, so please use this wisely.
  8.  *
  9.  *  @package sampAPI
  10.  *  @version 1.2
  11.  *  @author David Weston <westie@typefish.co.uk>
  12.  *  @copyright 2010; http://www.typefish.co.uk/licences/
  13.  */
  14.  
  15.  
  16. class SampRconAPI
  17. {
  18.     /**
  19.      *  @ignore
  20.      */
  21.     private $rSocket = false;
  22.    
  23.    
  24.     /**
  25.      *  @ignore
  26.      */
  27.     private $aServer = array();
  28.    
  29.    
  30.     /**
  31.      *  Creation of the RCON class.
  32.      *
  33.      */
  34.     public function __construct($sServer, $iPort, $sPassword)
  35.     {
  36.         /* Fill some arrays. */
  37.         $this->aServer[0] = $sServer;
  38.         $this->aServer[1] = $iPort;
  39.         $this->aServer[2] = $sPassword;
  40.        
  41.         /* Start the connection. */
  42.         $this->rSocket = fsockopen('udp://'.$this->aServer[0], $this->aServer[1], $iError, $sError, 2);
  43.        
  44.         if(!$this->rSocket)
  45.         {
  46.             $this->aServer[4] = false;
  47.             return;
  48.         }
  49.        
  50.         socket_set_timeout($this->rSocket, 2);
  51.        
  52.         $sPacket = 'SAMP';
  53.         $sPacket .= chr(strtok($this->aServer[0], '.'));
  54.         $sPacket .= chr(strtok('.'));
  55.         $sPacket .= chr(strtok('.'));
  56.         $sPacket .= chr(strtok('.'));
  57.         $sPacket .= chr($this->aServer[1] & 0xFF);
  58.         $sPacket .= chr($this->aServer[1] >> 8 & 0xFF);
  59.         $sPacket .= 'p4150';
  60.        
  61.         fwrite($this->rSocket, $sPacket);
  62.        
  63.         if(fread($this->rSocket, 10))
  64.         {
  65.             if(fread($this->rSocket, 5) == 'p4150')
  66.             {
  67.                 $this->aServer[4] = true;
  68.                 return;
  69.             }
  70.         }
  71.        
  72.         $this->aServer[4] = false;
  73.     }
  74.    
  75.    
  76.     /**
  77.      *  @ignore
  78.      */
  79.     public function __destruct()
  80.     {
  81.         @fclose($this->rSocket);
  82.     }
  83.    
  84.    
  85.     /**
  86.      *  Used to tell if the server is ready to accept queries.
  87.      *
  88.      *  If false is returned, then it is suggested that you remove the
  89.      *  class from active use, so that you can reload the class if needs
  90.      *  be.
  91.      *
  92.      *  @return bool true if success, false if failure.
  93.      */
  94.     public function isOnline()
  95.     {
  96.         return $this->aServer[4];
  97.     }
  98.    
  99.    
  100.     /**
  101.      *  Retrieves the command list.
  102.      *
  103.      *  @return array Array of available RCON commands.
  104.      */
  105.     public function getCommandList()
  106.     {
  107.         $aCommands = $this->packetSend('cmdlist');
  108.         unset($aCommands[0]);
  109.        
  110.         foreach($aCommands as &$sCommand)
  111.         {
  112.             $sCommand = trim($sCommand);
  113.         }
  114.        
  115.         return $aCommands;
  116.     }
  117.    
  118.    
  119.     /**
  120.      *  Retrieves and parses the server variables.
  121.      *
  122.      *  @return array Array of current server variables.
  123.      */
  124.     public function getServerVariables()
  125.     {
  126.         $aVariables = $this->packetSend('varlist');
  127.         unset($aVariables[0]);
  128.         $aReturn = array();
  129.        
  130.         foreach($aVariables as $sString)
  131.         {
  132.             preg_match('/(.*)=[\s]+(.*)/', $sString, $aMatches);
  133.            
  134.             if($aMatches[2][0] == '"')
  135.             {
  136.                 preg_match('/\"(.*)\"[\s]+\(/', $aMatches[2], $aTemp);
  137.                 $aReturn[trim($aMatches[1])] = $aTemp[1];
  138.             }
  139.             else
  140.             {
  141.                 preg_match('/(.*?)\s+\(/', $aMatches[2], $aTemp);
  142.                 $aReturn[trim($aMatches[1])] = $aTemp[1];
  143.             }
  144.         }
  145.        
  146.         return $aReturn;
  147.     }
  148.    
  149.    
  150.     /**
  151.      *  Sets the server's weather to the one specified.
  152.      *
  153.      *  @param integer $iWeatherID Weather ID
  154.      */
  155.     public function setWeather($iWeatherID)
  156.     {
  157.         $this->packetSend('weather '.$iWeatherID, false);
  158.     }
  159.    
  160.    
  161.     /**
  162.      *  Sets the server's gravity to the one specified.
  163.      *
  164.      *  @param float $fGravity Gravity amount (0.008 is default)
  165.      */
  166.     public function setGravity($fGravity)
  167.     {
  168.         $this->packetSend('gravity '.$fGravity, false);
  169.     }
  170.    
  171.    
  172.     /**
  173.      *  Bans a player ID from the server.
  174.      *
  175.      *  @param integer $iPlayerID Player ID
  176.      *  @return array Output from ban.
  177.      */
  178.     public function playerBan($iPlayerID)
  179.     {
  180.         return $this->packetSend('ban '.$iPlayerID);
  181.     }
  182.    
  183.    
  184.     /**
  185.      *  Kicks a player ID from the server.
  186.      *
  187.      *  @param integer $iPlayerID Player ID
  188.      *  @return array Output from kick.
  189.      */
  190.     public function playerKick($iPlayerID)
  191.     {
  192.         return $this->packetSend('kick '.$iPlayerID);
  193.     }
  194.    
  195.    
  196.     /**
  197.      *  Bans an IP address from the server.
  198.      *
  199.      *  @param string $sIPAddress IP Address
  200.      *  @return array Output from ban.
  201.      */
  202.     public function addressBan($sIPAddress)
  203.     {
  204.         return $this->packetSend('banip '.$sIPAddress);
  205.     }
  206.    
  207.    
  208.     /**
  209.      *  Unbans an IP address from the server.
  210.      *
  211.      *  @param string $sIPAddress IP Address
  212.      */
  213.     public function addressUnban($sIPAddress)
  214.     {
  215.         return $this->packetSend('unbanip '.$sIPAddress);
  216.     }
  217.    
  218.    
  219.     /**
  220.      *  Reloads the log on a server - useful when the log doesn't exist
  221.      *  any more.
  222.      */
  223.     public function reloadLogs()
  224.     {
  225.         return $this->packetSend('reloadlog');
  226.     }
  227.    
  228.    
  229.     /**
  230.      *  Reloads the ban file on a server.
  231.      *
  232.      */
  233.     public function reloadBans()
  234.     {
  235.         return $this->packetSend('reloadbans');
  236.     }
  237.    
  238.    
  239.     /**
  240.      *  Send a message as an admin to the players of the server.
  241.      *
  242.      *  @param string $sMessage Message
  243.      */
  244.     public function adminSay($sMessage)
  245.     {
  246.         $this->packetSend('say '.$sMessage, false);
  247.     }
  248.    
  249.    
  250.     /**
  251.      *  Change the gamemode in the server.
  252.      *
  253.      *  @param string $sGamemode Gamemode
  254.      */
  255.     public function gameChangeMode($sGamemode)
  256.     {
  257.         $this->packetSend('changemode '.$sGamemode, false);
  258.     }
  259.    
  260.    
  261.     /**
  262.      *  Sends a call to GMX.
  263.      */
  264.     public function gameNextMode()
  265.     {
  266.         $this->packetSend('gmx', false);
  267.     }
  268.    
  269.    
  270.     /**
  271.      *  Executes a file that contains server configuration.
  272.      *
  273.      *  @param string $sConfig Server config name/location.
  274.      */
  275.     public function gameExec($sConfig)
  276.     {
  277.         return $this->packetSend('exec '.$sConfig);
  278.     }
  279.    
  280.    
  281.     /**
  282.      *  Loads a filterscript.
  283.      *
  284.      *  @param string $sFilterscript Filterscript name/location.
  285.      */
  286.     public function gameLoadFilterscript($sFilterscript)
  287.     {
  288.         return $this->packetSend('loadfs '.$sFilterscript);
  289.     }
  290.    
  291.    
  292.     /**
  293.      *  Unloads a filterscript.
  294.      *
  295.      *  @param string $sFilterscript Filterscript name/location.
  296.      */
  297.     public function gameUnloadFilterscript($sFilterscript)
  298.     {
  299.         return $this->packetSend('unloadfs '.$sFilterscript);
  300.     }
  301.    
  302.    
  303.     /**
  304.      *  Reloads a filterscript.
  305.      *
  306.      *  @param string $sFilterscript Filterscript name/location.
  307.      */
  308.     public function gameReloadFilterscript($sFilterscript)
  309.     {
  310.         return $this->packetSend('reloadfs '.$sFilterscript);
  311.     }
  312.    
  313.    
  314.     /**
  315.      *  Shuts down the server, without any verification.
  316.      */
  317.     public function gameExit()
  318.     {
  319.         $this->packetSend('exit', false);
  320.     }
  321.    
  322.    
  323.     /**
  324.      *  Send an RCON command.
  325.      *
  326.      *  @param string $sCommand Command to send to the server.
  327.      *  @param float $fDelay Seconds to capture data, or false to retrieve no data.
  328.      *  @return array Array of output, in order of receipt.
  329.      */
  330.     public function Call($sCommand, $fDelay = 1.0)
  331.     {
  332.         return $this->packetSend($sCommand, $fDelay);
  333.     }
  334.    
  335.    
  336.     /**
  337.      *  Send an RCON command.
  338.      *
  339.      *  @ignore
  340.      *  @see SampRconApi::Call()
  341.      */
  342.     public function packetSend($sCommand, $fDelay = 1.0)
  343.     {
  344.         fwrite($this->rSocket, $this->packetCreate($sCommand));
  345.        
  346.         if($fDelay === false)
  347.         {
  348.             return;
  349.         }
  350.        
  351.         $aReturn = array();
  352.         $iMicrotime = microtime(true) + $fDelay;
  353.        
  354.         while(microtime(true) < $iMicrotime)
  355.         {
  356.             $sTemp = substr(fread($this->rSocket, 128), 13);
  357.            
  358.             if(strlen($sTemp))
  359.             {
  360.                 $aReturn[] = $sTemp;
  361.             }
  362.             else
  363.             {
  364.                 break;
  365.             }
  366.         }
  367.        
  368.         return $aReturn;
  369.     }
  370.    
  371.    
  372.     /**
  373.      *  @ignore
  374.      */
  375.     private function packetCreate($sCommand)
  376.     {
  377.         $sPacket = 'SAMP';
  378.         $sPacket .= chr(strtok($this->aServer[0], '.'));
  379.         $sPacket .= chr(strtok('.'));
  380.         $sPacket .= chr(strtok('.'));
  381.         $sPacket .= chr(strtok('.'));
  382.         $sPacket .= chr($this->aServer[1] & 0xFF);
  383.         $sPacket .= chr($this->aServer[1] >> 8 & 0xFF);
  384.         $sPacket .= 'x';
  385.        
  386.         $sPacket .= chr(strlen($this->aServer[2]) & 0xFF);
  387.         $sPacket .= chr(strlen($this->aServer[2]) >> 8 & 0xFF);
  388.         $sPacket .= $this->aServer[2];
  389.         $sPacket .= chr(strlen($sCommand) & 0xFF);
  390.         $sPacket .= chr(strlen($sCommand) >> 8 & 0xFF);
  391.         $sPacket .= $sCommand;
  392.        
  393.         return $sPacket;
  394.     }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement