Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.75 KB | None | 0 0
  1. <?php
  2. /**
  3. ************************************
  4. * MTA PHP SDK
  5. ************************************
  6. *
  7. * @copyright    Copyright (C) 2010, Multi Theft Auto
  8. * @author       JackC, eAi, Sebas
  9. * @link         http://www.mtasa.com
  10. * @version      0.4
  11. */
  12.  
  13. class mta
  14. {
  15.     private $useCurl = false;
  16.     private $sockTimeout = 6; // seconds
  17.    
  18.     public $http_username = '';
  19.     public $http_password = '';
  20.    
  21.     public $host = '';
  22.     public $port = '';
  23.    
  24.     private $resources = array();
  25.    
  26.     public function __construct( $host, $port, $username = "", $pass = "" )
  27.     {
  28.         $this->host = $host;
  29.         $this->port = $port;
  30.         $this->http_username = $username;
  31.         $this->http_password = $pass;
  32.     }
  33.    
  34.     public function getResource ( $resourceName )
  35.     {
  36.         foreach ( $this->resources as $resource )
  37.         {
  38.             if ( $resource->getName == $resourceName )
  39.                 return $resource;
  40.         }
  41.        
  42.         $res = new ResourceClass ( $resourceName, $this );
  43.         $this->resources[] = $res;
  44.         return $res;
  45.     }
  46.    
  47.     public static function getInput()
  48.     {
  49.         $out = mta::convertToObjects( json_decode( file_get_contents('php://input'), true ) );
  50.         return (is_array($out)) ? $out : false;
  51.     }
  52.    
  53.     public static function doReturn()
  54.     {
  55.         $val = array();
  56.        
  57.         for ( $i = 0; $i < func_num_args(); $i++ )
  58.         {
  59.             $val[$i] = func_get_arg($i);
  60.         }
  61.        
  62.         $val = mta::convertFromObjects($val);
  63.         $json_output = json_encode($val);
  64.         echo $json_output;
  65.     }
  66.    
  67.     public function callFunction( $resourceName, $function, $args )
  68.     {
  69.         if ( $args != null )
  70.         {
  71.             $args = mta::convertFromObjects($args);
  72.             $json_output = json_encode($args);
  73.         }
  74.         else
  75.         {
  76.             $json_output = "";
  77.         }
  78.         $path = "/" . $resourceName . "/call/" . $function;
  79.         $result = $this->do_post_request( $this->host, $this->port, $path, $json_output );
  80.         echo $json_output;
  81.         $out = mta::convertToObjects( json_decode( $result, true ) );
  82.        
  83.         return (is_array($out)) ? $out : false;
  84.     }
  85.    
  86.     public static function convertToObjects( $item )
  87.     {
  88.         if ( is_array($item) )
  89.         {
  90.             foreach ( $item as &$value )
  91.             {
  92.                 $value = mta::convertToObjects( $value );
  93.             }
  94.         }
  95.         else if ( is_string($item) )
  96.         {  
  97.             if ( substr( $item, 0, 3 ) == "^E^" )
  98.             {
  99.                 $item = new ElementClass( substr( $item, 3 ) );
  100.             }
  101.             elseif ( substr( $item, 0, 3 ) == "^R^" )
  102.             {
  103.                 $item = $this->getResource( substr( $item, 3 ) );
  104.             }
  105.         }
  106.        
  107.         return $item;
  108.     }
  109.    
  110.     public static function convertFromObjects( $item )
  111.     {
  112.         if ( is_array($item) )
  113.         {
  114.             foreach ( $item as &$value )
  115.             {
  116.                 $value = mta::convertFromObjects($value);
  117.             }
  118.         }
  119.         elseif ( is_object($item) )
  120.         {  
  121.             if ( get_class($item) == "Element" || get_class($item) == "Resource" )
  122.             {
  123.                 $item = $item->toString();
  124.             }
  125.         }
  126.        
  127.         return $item;
  128.     }
  129.    
  130.     function do_post_request( $host, $port, $path, $json_data )
  131.     {
  132.         if ( $this->useCurl )
  133.         {
  134.             $ch = curl_init();  
  135.             curl_setopt( $ch, CURLOPT_URL, "http://{$host}:{$port}{$path}" );
  136.             curl_setopt( $ch, CURLOPT_POST, 1 );
  137.             curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data );
  138.             $result = curl_exec($ch);    
  139.             curl_close($ch);
  140.             return $result;
  141.         }
  142.         else
  143.         {
  144.             if ( !$fp = @fsockopen( $host, $port, $errno, $errstr, $this->sockTimeout ) )
  145.             {
  146.                 throw new Exception( "Could not connect to {$host}:{$port}" );
  147.             }
  148.  
  149.             $out = "POST {$path} HTTP/1.0\r\n";
  150.             $out .= "Host: {$host}:{$port}\r\n";
  151.            
  152.             if ( $this->http_username && $this->http_password )
  153.             {
  154.                 $out .= "Authorization: Basic " . base64_encode( "{$this->http_username}:{$this->http_password}" ) . "\r\n";
  155.             }
  156.            
  157.             $out .= "Content-Length: " . strlen($json_data) . "\r\n";
  158.             $out .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
  159.             //$out .= "Connection: close\r\n\r\n";
  160.             $out .= $json_data . "\r\n\r\n";
  161.            
  162.             if ( !fputs( $fp, $out ) )
  163.             {
  164.                 throw new Exception( "Unable to send request to {$host}:{$port}" );
  165.             }
  166.            
  167.             @stream_set_timeout( $fp, $this->sockTimeout );
  168.             $status = @socket_get_status($fp);
  169.            
  170.             $response = '';
  171.            
  172.             while ( !feof($fp) && !$status['timed_out'] )
  173.             {
  174.                 $response .= fgets( $fp, 128 );
  175.                 $status = socket_get_status($fp);
  176.             }
  177.            
  178.             fclose( $fp );
  179.            
  180.             $tmp = explode( "\r\n\r\n", $response, 2 );
  181.             $headers = $tmp[0];
  182.             $response = trim($tmp[1]);
  183.            
  184.             preg_match( "/HTTP\/1.(?:0|1)\s*([0-9]{3})/", $headers, $matches );
  185.             $statusCode = intval($matches[1]);
  186.            
  187.             if ( $statusCode != 200 )
  188.             {
  189.                 switch( $statusCode )
  190.                 {
  191.                     case 401:
  192.                         throw new Exception( "Access Denied. This server requires authentication. Please ensure that a valid username and password combination is provided." );
  193.                     break;
  194.                    
  195.                     case 404:
  196.                         throw new Exception( "There was a problem with the request. Ensure that the resource exists and that the name is spelled correctly." );
  197.                     break;
  198.                 }
  199.             }
  200.            
  201.             if ( preg_match( "/^error/i", $response ) )
  202.             {
  203.                 throw new Exception( ucwords( preg_replace("/^error:?\s*/i", "", $response ) ) );
  204.             }
  205.            
  206.             return $response;
  207.         }
  208.     }
  209. }
  210.  
  211. class ElementClass
  212. {
  213.     var $id;
  214.  
  215.     function Element($id)
  216.     {
  217.         $this->id = $id;
  218.     }
  219.  
  220.     function toString()
  221.     {
  222.         return "^E^" . $this->id;
  223.     }
  224. }
  225.  
  226.  
  227. class ResourceClass
  228. {
  229.     var $name;
  230.     private $server;
  231.  
  232.     function Resource($name, $server)
  233.     {
  234.         $this->name = $name;
  235.         $this->server = $server;
  236.     }
  237.  
  238.     function toString()
  239.     {
  240.         return "^R^" . $this->name;
  241.     }
  242.    
  243.     public function getName()
  244.     {
  245.         return $this->name;
  246.     }
  247.    
  248.     function call ( $function )
  249.     {
  250.        
  251.         $val = array();
  252.        
  253.         for ( $i = 1; $i < func_num_args(); $i++ )
  254.         {
  255.             $val[$i-1] = func_get_arg($i);
  256.         }
  257.         return $this->server->callFunction ( $this->name, $function, $val );
  258.     }
  259. }
  260. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement