Skip to content

Commit 3d93174

Browse files
author
eAi
committed
Release v0.3
This release improves syntax. Supports functions in callRemote. This was originally published at 18:21, 18 November 2007. Unfortunately this version could not be recovered. An additional fix was published at 16:32, 17 April 2008. This was released as "0.3_fix".
1 parent 7a834ca commit 3d93174

File tree

1 file changed

+82
-15
lines changed

1 file changed

+82
-15
lines changed

mta_sdk.php

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @copyright Copyright (C) 2007, Multi Theft Auto
88
* @author JackC, eAi
99
* @link http://www.mtasa.com
10-
* @version 0.2
10+
* @version 0.3
1111
*/
1212

1313
class mta
@@ -18,31 +18,78 @@ class mta
1818
public $http_username = '';
1919
public $http_password = '';
2020

21-
public function callFunction( $host, $port, $resource, $function )
21+
public $host = '';
22+
public $port = '';
23+
24+
private $resources = array();
25+
26+
public function mta( $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 Resource ( $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()
2254
{
2355
$val = array();
2456

25-
for ( $i = 4; $i < func_num_args(); $i++ )
57+
for ( $i = 0; $i < func_num_args(); $i++ )
2658
{
27-
$val[$i-4] = func_get_arg($i);
59+
$val[$i] = func_get_arg($i);
2860
}
29-
30-
$val = $this->convertFromObjects($val);
61+
62+
$val = mta::convertFromObjects($val);
3163
$json_output = json_encode($val);
32-
$path = "/" . $resource . "/call/" . $function;
33-
$result = $this->do_post_request( $host, $port, $path, $json_output );
34-
$out = $this->convertToObjects( json_decode( $result, true ) );
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 ) );
3582

3683
return (is_array($out)) ? $out : false;
3784
}
3885

39-
public function convertToObjects( $item )
86+
public static function convertToObjects( $item )
4087
{
4188
if ( is_array($item) )
4289
{
4390
foreach ( $item as &$value )
4491
{
45-
$value = $this->convertToObjects( $value );
92+
$value = mta::convertToObjects( $value );
4693
}
4794
}
4895
else if ( is_string($item) )
@@ -53,20 +100,20 @@ public function convertToObjects( $item )
53100
}
54101
elseif ( substr( $item, 0, 3 ) == "^R^" )
55102
{
56-
$item = new Resource( substr( $item, 3 ) );
103+
$item = $this->getResource( substr( $item, 3 ) );
57104
}
58105
}
59106

60107
return $item;
61108
}
62109

63-
public function convertFromObjects( $item )
110+
public static function convertFromObjects( $item )
64111
{
65112
if ( is_array($item) )
66113
{
67114
foreach ( $item as &$value )
68115
{
69-
$value = $this->convertFromObjects($value);
116+
$value = mta::convertFromObjects($value);
70117
}
71118
}
72119
elseif ( is_object($item) )
@@ -176,18 +223,38 @@ function toString()
176223
}
177224
}
178225

226+
179227
class Resource
180228
{
181229
var $name;
230+
private $server;
182231

183-
function Resource($name)
232+
function Resource($name, $server)
184233
{
185234
$this->name = $name;
235+
$this->server = $server;
186236
}
187237

188238
function toString()
189239
{
190240
return "^R^" . $this->name;
191241
}
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+
}
192259
}
193260
?>

0 commit comments

Comments
 (0)