· 5 years ago · Dec 23, 2020, 11:12 PM
1<?PHP
2//Set our script to JSON
3header( 'Content-Type: application/json' );
4
5//Web Login Page
6$ServerURL = "PUT YOUR URL HERE WITHOUT PROTOTCAL"; //e.g. "company.3cx.co.uk"
7
8//Web URL Credentials
9$LoginCreds = new stdClass( );
10$LoginCreds->username = "WEB USERNAME"; //admin
11$LoginCreds->password = "WEB PASSWORD"; //Password
12
13//Function to post data to website and read returned cookie
14function Get3CXCookie( )
15{
16 //Define our variables are globals
17 global $ServerURL, $LoginCreds;
18
19 //Encode Logon into JSON Data for body
20 $UserDetails = json_encode( $LoginCreds );
21
22 //Create our POST login with headers, ensure close!
23 $PostData = file_get_contents( "https://". $ServerURL ."/api/login", null, stream_context_create( array(
24 'http' => array(
25 'protocol_version' => '1.1',
26 'user_agent' => 'PHP',
27 'method' => 'POST',
28 'header' => 'Content-type: application/json\r\n'.
29 'User-Agent: PHP\r\n'.
30 'Connection: close\r\n' .
31 'Content-length: ' . strlen( $UserDetails ) . '',
32 'content' => $UserDetails,
33 ),
34 ) ) );
35
36 //Take response header 9 and break it into an array using explode from "; " which separates each variable
37 $TempCookie = explode( "; ", $http_response_header[9] );
38
39 //Build our required cookie
40 $FinalCookie = substr( $TempCookie[0], 12 );
41
42 //Return the cookie Data if auth succeeded
43 if( $PostData == "AuthSuccess" )
44 return $FinalCookie;
45
46 //Return null/blank if auth failed
47 return null;
48}
49
50//Function to GET data from API and read returned data
51function GetAPIData( $API, $AuthCookie )
52{
53 //Define our variable is a global
54 global $ServerURL;
55
56 //Create our GET with headers
57 $GetData = file_get_contents( "https://". $ServerURL ."/api/" . $API, null, stream_context_create( array(
58 'http' => array(
59 'protocol_version' => '1.1',
60 'user-agent' => 'PHP',
61 'method' => 'GET',
62 'header' => 'Cookie: '. $AuthCookie .''
63 ),
64 ) ) );
65
66 //Return the API Data
67 return $GetData;
68}
69
70//Grab our auth cookie
71$Auth3CX = Get3CXCookie( );
72
73//If we got something back, Auth succeeded, so move on else login failed!
74if( strlen( $Auth3CX ) != 0 )
75{
76 /*======================================================================================================================================*/
77 //SystemStatus
78 //$DisplaySystemStatusAPI = GetAPIData( "SystemStatus", $Auth3CX );
79
80 //Output to screen pretty
81 //echo json_encode( json_decode( $DisplaySystemStatusAPI ), JSON_PRETTY_PRINT );
82
83 //Decode JSON ready for grabbign VARS
84 //$SystemStatus = json_decode( $DisplaySystemStatusAPI );
85
86 //Grab Values
87 /*
88 echo "FQDN: " . $SystemStatus->FQDN . "\n";
89 echo "Webmeeting FQDN: " . $SystemStatus->WebMeetingFQDN . "\n";
90 echo "Webmeeting Status: " . $SystemStatus->WebMeetingStatus . "\n";
91 echo "Version: " . $SystemStatus->Version . "\n";
92 echo "Recoding State: " . $SystemStatus->RecordingState . "\n";
93 echo "Activated: " . $SystemStatus->Activated . "\n";
94 echo "Max Sim Calls: " . $SystemStatus->MaxSimCalls . "\n";
95 echo "Max Sim Meeting: " . $SystemStatus->MaxSimMeetingParticipants . "\n";
96 echo "Call History: " . $SystemStatus->CallHistoryCount . "\n";
97 echo "Chat Message Count: " . $SystemStatus->ChatMessagesCount . "\n";
98 echo "Extensions Registered: " . $SystemStatus->ExtensionsRegistered . "\n";
99 echo "Own Push Certificate: " . $SystemStatus->OwnPush . "\n";
100 echo "Public IP: " . $SystemStatus->Ip . "\n";
101 echo "Local IP Valid: " . $SystemStatus->LocalIpValid . "\n";
102 echo "Local IP: " . $SystemStatus->CurrentLocalIp . "\n";
103 echo "All Local IP's: " . $SystemStatus->AvailableLocalIps . "\n";
104 echo "Total Extensions: " . $SystemStatus->ExtensionsTotal . "\n";
105 echo "Has Unregistered Extensions: " . $SystemStatus->HasUnregisteredSystemExtensions . "\n";
106 echo "Services Not Running: " . $SystemStatus->HasNotRunningServices . "\n";
107 echo "Trunks Registered: " . $SystemStatus->TrunksRegistered . "\n";
108 echo "Trunks Total: " . $SystemStatus->TrunksTotal . "\n";
109 echo "FQDN: " . $SystemStatus->CallsActive . "\n";
110 echo "Total Blacklisted IP's: " . $SystemStatus->BlacklistedIpCount . "\n";
111 echo "Mem Usage %: " . $SystemStatus->MemoryUsage . "\n";
112 echo "Free Physical Mem: " . $SystemStatus->PhysicalMemoryUsage . "\n";
113 echo "Free Virtual Memory: " . $SystemStatus->FreeFirtualMemory . "\n";
114 echo "Total Virtual Memory: " . $SystemStatus->TotalVirtualMemory . "\n";
115 echo "Free Physical Memory: " . $SystemStatus->FreePhysicalMemory . "\n";
116 echo "Total Physical Memory: " . $SystemStatus->TotalPhysicalMemory . "\n";
117 echo "Disk Usage %: " . $SystemStatus->DiskUsage . "\n";
118 echo "Free Disk Space: " . $SystemStatus->FreeDiskSpace . "\n";
119 echo "Total Disk Space: " . $SystemStatus->TotalDiskSpace . "\n";
120 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[0][0] . ": " . $SystemStatus->CpuUsageHistory[0][1] . "\n";
121 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[1][0] . ": " . $SystemStatus->CpuUsageHistory[1][1] . "\n";
122 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[2][0] . ": " . $SystemStatus->CpuUsageHistory[2][1] . "\n";
123 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[3][0] . ": " . $SystemStatus->CpuUsageHistory[3][1] . "\n";
124 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[4][0] . ": " . $SystemStatus->CpuUsageHistory[4][1] . "\n";
125 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[5][0] . ": " . $SystemStatus->CpuUsageHistory[5][1] . "\n";
126 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[6][0] . ": " . $SystemStatus->CpuUsageHistory[6][1] . "\n";
127 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[7][0] . ": " . $SystemStatus->CpuUsageHistory[7][1] . "\n";
128 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[8][0] . ": " . $SystemStatus->CpuUsageHistory[8][1] . "\n";
129 echo "CPU Valie At " . $SystemStatus->CpuUsageHistory[9][0] . ": " . $SystemStatus->CpuUsageHistory[9][1] . "\n";
130 echo "Maintenance Expires: " . $SystemStatus->MaintenanceExpiresAt . "\n";
131 echo "Support: " . $SystemStatus->Support . "\n";
132 echo "License Active: " . $SystemStatus->LicenseActive . "\n";
133 echo "Expiration Date: " . $SystemStatus->ExpirationDate . "\n";
134 echo "Outbound Rules: " . $SystemStatus->OutboundRules . "\n";
135 echo "Backup Scheduled: " . $SystemStatus->BackupScheduled . "\n";
136 echo "Last Backup Date: " . $SystemStatus->LastBackupDateTime . "\n";
137 echo "Reseller: " . $SystemStatus->ResellerName . "\n";
138 echo "License Key: " . $SystemStatus->LicenseKey . "\n";
139 echo "Product Key: " . $SystemStatus->ProductCode . "\n";
140 echo "Is SPLA: " . $SystemStatus->IsSpla . "\n\n\n";
141 */
142 /*======================================================================================================================================*/
143 //Active Calls
144 //$DisplayActiveCallsAPI = GetAPIData( "ActiveCalls", $Auth3CX );
145
146 //Output to screen pretty
147 //echo json_encode( json_decode( $DisplayActiveCallsAPI ), JSON_PRETTY_PRINT );
148 /*
149 {
150 "Now": "2020-12-23T16:16:47.4072367+00:00",
151 "list": [
152 {
153 "Id": 9,
154 "Caller": "101 Steve Jenner Home",
155 "Callee": "Voicemail Menu",
156 "Status": "Talking",
157 "LastChangeStatus": "2020-12-23T16:16:44+00:00",
158 "EstablishedAt": "2020-12-23T16:16:44+00:00"
159 }
160 ],
161 "licenseRestricted": false
162 }
163 */
164
165 //Decode JSON ready for grabbign VARS
166 //$ActiveCalls = json_decode( $DisplayActiveCallsAPI );
167
168 //Grab Values
169 //echo "System Time Now: " . $ActiveCalls->Now . "\n";
170 //foreach( $ActiveCalls->list as $Call )
171 //{
172 // echo $Call->Id. " => " .$Call->Caller. " => " .$Call->Callee. " => " .$Call->Status. " => " .$Call->LastChangeStatus. " => " .$Call->EstablishedAt. "\n";
173 //}
174 //$Restricted = "No";
175 //echo "License Restricted?: " . $Restricted ? "Yes" : "No" ."\n";
176
177 /*======================================================================================================================================*/
178 //System Services
179 //$DisplayServiceListAPI = GetAPIData( "ServiceList", $Auth3CX );
180
181 //Output to screen pretty
182 //echo json_encode( json_decode( $DisplayServiceListAPI ), JSON_PRETTY_PRINT );
183 /*
184 {
185 "Name": "3CXCfgServ01",
186 "DisplayName": "3CX PhoneSystem 01 Configuration Server",
187 "Status": 4,
188 "MemoryUsed": 3719168,
189 "CpuUsage": 0,
190 "ThreadCount": 19,
191 "HandleCount": 16,
192 "startStopEnabled": false,
193 "restartEnabled": true
194 },
195 */
196
197 //Decode JSON ready for grabbign VARS
198 //$Services = json_decode( $DisplayServiceListAPI );
199
200 //Grab Values
201 /*
202 foreach( $Services as $Service )
203 {
204 echo "Name: ". $Service->Name ."\n";
205 echo "Display Name: ". $Service->DisplayName ."\n";
206 switch( $Service->Status )
207 {
208 case 4: { echo "Running\n"; break; }
209 case 3: { echo "Stop Pending\n"; break; }
210 case 2: { echo "Start Pending\n"; break; }
211 case 1: { echo "Stopped\n"; break; }
212 default: { echo "Unknown Status\n"; break; }
213 }
214 echo "Memory Used: ". $Service->MemoryUsed / 1024 . " KB\n";
215 echo "CPU Usage: ". $Service->CpuUsage . "\n";
216 echo "Thread Count: ". $Service->ThreadCount . "\n";
217 echo "Handle Count: ". $Service->HandleCount . "\n";
218 echo $Service->startStopEnabled ? "Start Stop Enabled: Yes\n" : "Start Stop Enabled: No\n";
219 echo $Service->restartEnabled ? "Restart Enabled: Yes\n\n" : "Restart Enabled: No\n\n";
220 }
221 */
222 /*======================================================================================================================================*/
223 //Trunk Information
224 //$DisplayTrunkListAPI = GetAPIData( "TrunkList", $Auth3CX );
225
226 //Output to screen pretty
227 //echo json_encode( json_decode( $DisplayTrunkListAPI )->list, JSON_PRETTY_PRINT ). "\n\n";
228 /*
229 {
230 "Id": "56",
231 "Number": "10001",
232 "Name": "Steptech Solutions",
233 "Host": "sip.voip-unlimited.net",
234 "Type": "Provider",
235 "SimCalls": "10",
236 "ExternalNumber": "01892277030",
237 "IsRegistered": true,
238 "RegisterOkTime": "2020-12-23T01:04:54.0000000Z",
239 "RegisterSentTime": "2020-12-23T01:04:54.0000000Z",
240 "RegisterFailedTime": "",
241 "CanBeDeleted": true,
242 "IsExpiredProviderRootCertificate": false,
243 "ExpiredProviderRootCertificateDate": ""
244 },
245 */
246
247 //Decode JSON ready for grabbign VARS
248 //$Trunks = json_decode( $DisplayTrunkListAPI )->list;
249
250 //Get Data
251 /*
252 foreach( $Trunks as $Trunk )
253 {
254 echo "Trunk ID: ". $Trunk->Id ."\n";
255 echo "Trunk Number: ". $Trunk->Number ."\n";
256 echo "Trunk Name: ". $Trunk->Name ."\n";
257 echo "Trunk Host: ". $Trunk->Host ."\n";
258 echo "Trunk Type: ". $Trunk->Type ."\n";
259 echo "Trunk SimCalls: ". $Trunk->SimCalls ."\n";
260 echo "Trunk ExternalNumber: ". $Trunk->ExternalNumber ."\n";
261 echo $Trunk->IsRegistered ? "Trunk Is Registered: Yes\n" : "Trunk Is Registered: No\n";
262 echo "Trunk Register Ok Time: ". $Trunk->RegisterOkTime ."\n";
263 echo "Trunk Register Sent Time: ". $Trunk->RegisterSentTime ."\n";
264 echo "Trunk Register Failed Time: ". $Trunk->RegisterFailedTime ."\n";
265 echo $Trunk->CanBeDeleted ? "Trunk Can Be Deleted: Yes\n" : "Trunk Can Be Deleted: No\n";
266 echo $Trunk->IsExpiredProviderRootCertificate ? "Trunk Is Expired Provider Root Certificate: Yes\n" : "Trunk Is Expired Provider Root Certificate: No\n";
267 echo $Trunk->ExpiredProviderRootCertificateDate ? "Trunk Expired Provider Root Certificate Date: Yes\n\n\n" : "Trunk Expired Provider Root Certificate Date: No\n\n\n";
268 }
269 */
270 /*======================================================================================================================================*/
271 //BlackList
272 //$DisplayBlackListAPI = GetAPIData( "ipBlackList", $Auth3CX );
273
274 //Output to screen pretty
275 //echo json_encode( json_decode( $DisplayBlackListAPI )->list, JSON_PRETTY_PRINT );
276 /*
277 {
278 "Id": 21,
279 "IpAddress": "88.83.103.225",
280 "SubnetMask": "",
281 "Action": "enum.BlacklistBlockType.Allowed",
282 "ExpirationDate": "2039-09-30T17:15:27.0000000Z",
283 "IpAddressRange": "88.83.103.225 - 88.83.103.225",
284 "CanBeDeleted": true,
285 "Description": "Home"
286 },
287 */
288
289 //Decode JSON ready for grabbign VARS
290 //Blacklist = json_decode( $DisplayBlackListAPI )->list;
291
292 //Get Data
293 /*
294 foreach( $Blacklist as $IP )
295 {
296 echo "ID: ". $IP->Id ."\n";
297 echo "IP Address: ". $IP->IpAddress ."\n";
298 echo "Subnet Mask: ". $IP->SubnetMask ."\n";
299 $Action = "Denied";
300 switch( $IP->Action )
301 {
302 case "enum.BlacklistBlockType.Allowed": { $Action = "Allowed"; break; }
303 case "enum.BlacklistBlockType.Denied": { $Action = "Denied"; break; }
304 default: $Action = "Unknown"; break;
305 }
306 echo "Action: ". $Action ."\n";
307 echo "Expiration Date: ". $IP->ExpirationDate ."\n";
308 echo "IP Address Range: ". $IP->IpAddressRange ."\n";
309 echo $IP->CanBeDeleted ? "Can Be Deleted: Yes\n" : "Can Be Deleted: No\n";
310 echo "Description". $IP->Description ."\n\n\n";
311 }
312 */
313 /*======================================================================================================================================*/
314 //User Info
315 $DisplayUserInfoAPI = GetAPIData( "CurrentUser", $Auth3CX );
316
317 //Output to screen pretty
318 //echo json_encode( json_decode( $DisplayUserInfoAPI ), JSON_PRETTY_PRINT );
319
320 /*
321 "name": "jenners",
322 "initials": "A",
323 "version": "16.0.7.1078",
324 "roles": [
325 "GlobalExtensionManager",
326 "GlobalAdmin",
327 "Admin",
328 "Trunks",
329 "Recordings",
330 "Reports"
331 ],
332 "email": "support@steptechsolutions.co.uk",
333 "registrationEmail": "stephen.jenner@netvector.co.uk",
334 "consoleEnabled": true,
335 "isProfessional": true,
336 "IsHostingMode": false,
337 "isChatLogEnabled": false,
338 "isInstanceManagerEnabled": true,
339 "isLabFeaturesEnabled": false,
340 "exchangeServicesEnabled": false,
341 "callReportsEnabled": true,
342 "acquaintance": null
343 */
344
345 //Decode JSON ready for grabbign VARS
346 //$UserInfo = json_decode( $DisplayUserInfoAPI );
347
348 //echo "Name: ". $UserInfo->name ."\n";
349 //echo "Initials: ". $UserInfo->initials ."\n";
350 //echo "Version: ". $UserInfo->version ."\n";
351 /*
352 $MyRoles = "";
353 foreach( $UserInfo->roles as $role )
354 {
355 switch( $role )
356 {
357 case "GlobalExtensionManager": { $MyRoles .= "Global Extension Manager "; break; }
358 case "GlobalAdmin": { $MyRoles .= "Global Admin "; break; }
359 case "Admin": { $MyRoles .= "Admin "; break; }
360 case "Trunks": { $MyRoles .= "Trunk Admin "; break; }
361 case "Recordings": { $MyRoles .= "Recording Admin "; break; }
362 case "Reports": { $MyRoles .= "Report Admin "; break; }
363 }
364 }
365 */
366 //echo "My Roles: ". $MyRoles ."\n";
367 //echo "Admin Email: ". $UserInfo->email ."\n";
368 //echo "License Email: ". $UserInfo->registrationEmail ."\n";
369 //echo $UserInfo->consoleEnabled ? "Console Enabled: Yes\n" : "Console Enabled: No\n";
370 //echo $UserInfo->isProfessional ? "Is Professional: Yes\n" : "Is Professional: No\n";
371 //echo $UserInfo->IsHostingMode ? "Is Hosting Mode: Yes\n" : "Is Hosting Mode: No\n";
372 //echo $UserInfo->isChatLogEnabled ? "Chat Log: Yes\n" : "Chat Log: No\n";
373 //echo $UserInfo->isInstanceManagerEnabled ? "Is Instance Manager: Yes\n" : "Is Instance Manager: No\n";
374 //echo $UserInfo->isLabFeaturesEnabled ? "Is Lab Features: Yes\n" : "Is Lab Features: No\n";
375 //echo $UserInfo->exchangeServicesEnabled ? "Exchange Services: Yes\n" : "Exchange Services: No\n";
376 //echo $UserInfo->callReportsEnabled ? "Call Reports: Yes\n" : "Call Reports: No\n";
377 //echo "Acquaintance: ". $UserInfo->acquaintance ."\n\n\n";
378
379 /*======================================================================================================================================*/
380 //Call Log
381 //$DisplayCallLogAPI = GetAPIData( "CallLog", $Auth3CX );
382
383 //Output to screen pretty
384 //echo json_encode( json_decode( $DisplayCallLogAPI ), JSON_PRETTY_PRINT );
385
386 /*
387 {
388 "CallLogRows": [],
389 "HasMoreRows": true,
390 "IsExportAllowed": true
391 }
392 */
393
394 //Decode JSON ready for grabbign VARS
395 //$CallLog = json_decode( $DisplayCallLogAPI );
396 /*======================================================================================================================================*/
397 //Backup Info
398 //$DisplayBackupRestoreAPI = GetAPIData( "BackupAndRestoreList", $Auth3CX );
399
400 //Output to screen pretty
401 //echo json_encode( json_decode( $DisplayBackupRestoreAPI ), JSON_PRETTY_PRINT );
402
403 /*
404 {
405 "Id": -112157339,
406 "FileName": "3CXScheduledBackup.1.zip",
407 "CreationTime": "2020-12-22T03:00:00.1729722+00:00",
408 "Version": "16.0.7.1078",
409 "Size": 98256027,
410 "IsEncrypted": false,
411 "Footprint": 0
412 },
413 */
414
415 //Decode JSON ready for grabbign VARS
416 //$BackupList = json_decode( $DisplayBackupRestoreAPI );
417
418 //Get Data
419 /*
420 foreach( $BackupList->list as $Backup )
421 {
422 echo "Id: ". $Backup->Id ."\n";
423 echo "FileName: ". $Backup->FileName ."\n";
424 echo "Created: ". $Backup->CreationTime ."\n";
425 echo "Version: ". $Backup->Version ."\n";
426 echo "Size: ". $Backup->Size / 1024 ." KB\n";
427
428 $Encryption = "No";
429 switch( $Backup->IsEncrypted )
430 {
431 case true: { $Encryption = "Yes"; break; }
432 case false: { $Encryption = "No"; break; }
433 default: { $Encryption = "Unknown"; break; }
434 }
435
436 echo "Footprint: ". $Backup->Footprint ."\n\n";
437 }
438 */
439 /*======================================================================================================================================*/
440}
441
442//Blank return, auth failed!
443else
444{
445 die( "Authentication failed" );
446}
447
448?>