Advertisement
ZELDAX

mta_sdk.php

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