-
Notifications
You must be signed in to change notification settings - Fork 54
/
testwebservice.php
126 lines (107 loc) · 4.43 KB
/
testwebservice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* -------------------------------------------------------------------------
* DataInjection plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of DataInjection.
*
* DataInjection is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* DataInjection is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DataInjection. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2007-2023 by DataInjection plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/datainjection
* -------------------------------------------------------------------------
*/
if (!extension_loaded("xmlrpc")) {
die("Extension xmlrpc not loaded\n");
}
chdir(dirname($_SERVER["SCRIPT_FILENAME"]));
chdir("../../..");
$url = "/" . basename(getcwd()) . Plugin::getWebDir('webservices', false) . "/xmlrpc.php";
$args = [];
if ($_SERVER['argc'] > 1) {
for ($i = 1; $i < count($_SERVER['argv']); $i++) {
$it = explode("=", $argv[$i], 2);
$it[0] = preg_replace('/^--/', '', $it[0]);
$args[$it[0]] = (isset($it[1]) ? $it[1] : true);
}
}
if (isset($args['help']) && !isset($args['method'])) {
echo "\nusage : " . $_SERVER["SCRIPT_FILENAME"] . " [ options] \n\n";
echo "\thelp : display this screen\n";
echo "\thost : server name or IP, default : localhost\n";
echo "\turl : XML-RPC plugin URL, default : $url\n";
echo "\tusername : User name for security check (optionnal, default : glpi)\n";
echo "\tpassword : User password (optionnal, default : glpi)\n";
echo "\turi : Absolute path to the csv file to inject\n";
echo "\additional[xx]=yy : Additional info to import (xx = field name, yy=value)\n";
echo "\tmodels_id : ID of the model to use\n";
echo "\tentities_id : ID of the entity in whic data should be injected\n";
die("\nOther options are used for XML-RPC call.\n\n");
}
print_r($args);
//First of all, user login
$attrs['login_name'] = (isset($args['username']) ? $args['username'] : 'glpi');
$attrs['login_password'] = (isset($args['password']) ? $args['password'] : 'glpi');
$attrs['host'] = (isset($args['host']) ? $args['host'] : 'localhost');
$attrs['url'] = (isset($args['url']) ? $args['url']
: 'glpi080/plugins/webservices/xmlrpc.php');
$attrs['additional'] = (isset($args['additional']) ? $args['additional'] : []);
$response = call('glpi.doLogin', $attrs);
if ($response) {
$attrs['session'] = $response['session'];
echo "User logged in with session=" . $response['session'] . "\n";
} else {
exit(0);
}
//Set parameters for csv injection
$attrs['models_id'] = $args['models_id'];
$attrs['entities_id'] = $args['entities_id'];
$attrs['uri'] = $args['uri'];
//Inject file
$response = call('datainjection.inject', $attrs);
print_r($response);
print_r(call('glpi.doLogout', $attrs));
function call($method, $params)
{
$header = "Content-Type: text/xml";
echo "+ Calling '$method' on http://" . $params['host'] . "/" . $params['url'] . "\n";
$request = xmlrpc_encode_request($method, $params);
$context = stream_context_create(
[
'http' => [
'method' => "POST",
'header' => $header,
'content' => $request
]
]
);
$file = file_get_contents("http://" . $params['host'] . "/" . $params['url'], false, $context);
if (!$file) {
die("+ No response\n");
}
$response = xmlrpc_decode($file);
if (!is_array($response)) {
echo $file;
die("+ Bad response\n");
}
if (xmlrpc_is_fault($response)) {
echo("xmlrpc error(" . $response['faultCode'] . "): " . $response['faultString'] . "\n");
return false;
}
return $response;
}