-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWPSImport.php
executable file
·210 lines (172 loc) · 7 KB
/
CWPSImport.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/* CWPSImport.php
* -
* Written by damian <[email protected]>
* -
* A method of importing clients from ConnectWise into PasswordState automatically
* Run this script once a day (or even more often..) to automatically generate new Folders in PasswordState for new clients
* -
* Note that this initial script doesn't care about what your current setup is, it will create a folder with ASIT default
*/
define('CONFIG_FILE', '/var/www/config/cwpsimport.config.ini');
// require the ini.php
require_once('ini.php');
class CWPSImport {
private $SET;
private $CW;
private $PS;
public function __construct() {
// import the configuration file
$this->SET = ini2array(CONFIG_FILE);
// get a list of ConnectWise customers
$this->CW = $this->collect_connectwise();
// get a list of PasswordState customer folders
$this->PS = $this->collect_passwordstate();
// add new clients to PasswordState
$NEW = $this->check_and_add_ps_clients();
}
function collect_connectwise() {
// set up http options
$HTTP_OPTS = Array(
'http' => Array(
'method' => "GET",
'header' => "Accept: text/html\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36\r\n".
"Authorization: Basic ". base64_encode($this->SET['ConnectWise']['Company'] ."+". $this->SET['ConnectWise']['PublicKey'] .":". $this->SET['ConnectWise']['PrivateKey']) ."\r\n"
)
);
$HTTP_CONTEXT = stream_context_create($HTTP_OPTS);
// set up the URL
$URL = $this->SET['ConnectWise']['URL'] . $this->SET['ConnectWise']['Branch'] ."/apis/3.0/company/companies?pagesize=1000";
// grab the JSON output
$JSON = file_get_contents($URL, false, $HTTP_CONTEXT);
// return the JSON as an Array
return json_decode($JSON, true);
}
function collect_passwordstate() {
// set up http options
$HTTP_OPTS = Array(
'http' => Array(
'method' => "GET",
'header' => "Accept: text/html\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36\r\n"
),
'ssl' => Array( // maybe shouldn't do this, but it's internal, so who cares!
'verify_peer' => false,
'verify_peer_name' => false,
),
);
$HTTP_CONTEXT = stream_context_create($HTTP_OPTS);
// set up the URL
$URL = $this->SET['PasswordState']['URL'] ."api/folders?apikey=". $this->SET['PasswordState']['API'];
// grab the JSON output
$JSON = file_get_contents($URL, false, $HTTP_CONTEXT);
// convert to an Array
$CLIENT_LIST = json_decode($JSON, true);
// go through the list and get a list of clients (makes it easier to search later)
$CLIENTS = Array();
foreach ($CLIENT_LIST as $CLIENT) {
$CLIENTS[strtolower($CLIENT['FolderName'])] = $CLIENT['FolderName'];
}
// return
return $CLIENTS;
}
function check_and_add_ps_clients() {
foreach ($this->CW as $CLIENT) {
if ($CLIENT['status']['id'] != 1) {
// if the status isn't Active..
continue;
} elseif ($this->find_ps_client($CLIENT['name'])) {
// if the client already exists
continue;
} else {
// add this guy
$this->add_ps_client($CLIENT['name']);
}
}
}
function find_ps_client($CLIENT) {
if (isset($this->PS[strtolower($CLIENT)])) {
return true;
} else {
return false;
}
}
function post_content($URL, $DATA) {
// set up http options
$HTTP_OPTS = Array(
'http' => Array(
'method' => "POST",
'header' => "Content-Type: application/json\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36\r\n",
'content' => $DATA,
),
'ssl' => Array( // maybe shouldn't do this, but it's internal, so who cares!
'verify_peer' => false,
'verify_peer_name' => false,
),
);
$HTTP_CONTEXT = stream_context_create($HTTP_OPTS);
// post the data, get the output
$OUTPUT = file_get_contents($URL, false, $HTTP_CONTEXT);
// return the output
return $OUTPUT;
}
function get_content() {
}
function add_ps_client($CLIENT) {
echo '& Adding new client "'. $CLIENT .'" ... ';
// set up the folder configuration
$FOLDER = Array(
'FolderName' => $CLIENT,
'Description' => '',
'CopyPermissionsFromTemplateID' => '7',
'NestUnderFolderID' => '0',
'PropagatePermissions' => 'true',
'APIKey' => $this->SET['PasswordState']['API'],
);
// create the folder
$URL = $this->SET['PasswordState']['URL'] .'api/folders/';
$FOLDERID = json_decode($this->post_content($URL, json_encode($FOLDER)), true)['FolderID'];
// set up the default PasswordList configuration
$LIST = Array(
'Description' => '',
'LinkToTemplate' => false,
'NestUnderFolderID' => $FOLDERID,
'APIKey' => $this->SET['PasswordState']['API'],
);
// create Network Peripherals
$LIST['PasswordList'] = 'Network Peripherals';
$LIST['CopySettingsFromTemplateID'] = '1';
$URL = $this->SET['PasswordState']['URL'] .'api/passwordLists/';
$OUTPUT = json_decode($this->post_content($URL, json_encode($LIST)), true);
// create Network Devices
$LIST['PasswordList'] = 'Network Devices';
$LIST['CopySettingsFromTemplateID'] = '2';
$URL = $this->SET['PasswordState']['URL'] .'api/passwordLists/';
$OUTPUT = json_decode($this->post_content($URL, json_encode($LIST)), true);
// create Servers
$LIST['PasswordList'] = 'Servers';
$LIST['CopySettingsFromTemplateID'] = '3';
$URL = $this->SET['PasswordState']['URL'] .'api/passwordLists/';
$OUTPUT = json_decode($this->post_content($URL, json_encode($LIST)), true);
// create Software
$LIST['PasswordList'] = 'Software';
$LIST['CopySettingsFromTemplateID'] = '4';
$URL = $this->SET['PasswordState']['URL'] .'api/passwordLists/';
$OUTPUT = json_decode($this->post_content($URL, json_encode($LIST)), true);
// create Supplier Websites
$LIST['PasswordList'] = 'Supplier Websites';
$LIST['CopySettingsFromTemplateID'] = '5';
$URL = $this->SET['PasswordState']['URL'] .'api/passwordLists/';
$OUTPUT = json_decode($this->post_content($URL, json_encode($LIST)), true);
// create User Accounts
$LIST['PasswordList'] = 'User Accounts';
$LIST['CopySettingsFromTemplateID'] = '6';
$URL = $this->SET['PasswordState']['URL'] .'api/passwordLists/';
$OUTPUT = json_decode($this->post_content($URL, json_encode($LIST)), true);
echo 'done'.PHP_EOL;
}
}
$Import = new CWPSImport();
?>