-
Notifications
You must be signed in to change notification settings - Fork 0
/
kandji_processor.php
75 lines (64 loc) · 2.32 KB
/
kandji_processor.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
<?php
/**
* Kandji processor class
*
* @package munkireport
* @author jc0b
**/
use CFPropertyList\CFPropertyList;
use munkireport\processors\Processor;
class Kandji_processor extends Processor
{
public function run($plist)
{
if ( ! $plist){
throw new Exception("Error Processing Request: No property list found", 1);
}
configAppendFile(__DIR__ . '/config.php');
$module_dir = dirname(__FILE__);
$parser = new CFPropertyList();
$parser->parse($plist, CFPropertyList::FORMAT_XML);
$mylist = $parser->toArray();
$mylist['serial_number'] = $this->serial_number;
// Retrieve Kandji MR record (if existing)
try {
$model = Kandji_model::select()
->where('serial_number', $this->serial_number)
->firstOrFail();
} catch (\Throwable $th) {
$model = new Kandji_model();
}
// Check if we should enable Kandji lookup
if (conf('kandji_enable')) {
// Load Kandji helper
require_once($module_dir.'/lib/kandji_helper.php');
$kandji_helper = new munkireport\module\kandji\kandji_helper;
$json = $kandji_helper->pull_kandji_data($this->serial_number);
// Transpose Kandji API output into Kandji model
// General section
$mylist['name'] = $json[0]->device_name;
$mylist['asset_tag'] = $json[0]->asset_tag;
$mylist['blueprint_id'] = $json[0]->blueprint_id;
$mylist['blueprint_name'] = $json[0]->blueprint_name;
$mylist['last_check_in'] = $this->convert_time_to_epoch($json[0]->last_check_in);
$mylist['last_enrollment'] = $this->convert_time_to_epoch($json[0]->last_enrollment);
$mylist['first_enrollment'] = $this->convert_time_to_epoch($json[0]->first_enrollment);
// Location section
$mylist['realname'] = $json[0]->user->name;
$mylist['email_address'] = $json[0]->user->email;
}
$model->fill($mylist)->save();
}
/**
* Convert Kandji timestamps to epochs
*
* @return Unix epoch
* @author jc0b
*
**/
private function convert_time_to_epoch($date)
{
$dt = new \DateTime($date);
return $dt->getTimestamp();
}
}