-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanagedinstalls_processor.php
122 lines (103 loc) · 3.44 KB
/
managedinstalls_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
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
<?php
use CFPropertyList\CFPropertyList;
use munkireport\processors\Processor;
class Managedinstalls_processor extends Processor
{
public function run($plist)
{
$this->timestamp = date('Y-m-d H:i:s');
if (! $plist) {
throw new Exception(
"Error Processing Request: No property list found", 1
);
}
$parser = new CFPropertyList();
$parser->parse($plist, CFPropertyList::FORMAT_XML);
if( ! $mylist = $parser->toArray()){
return;
}
// Delete old entries
Managedinstalls_model::where('serial_number', $this->serial_number)->delete();
// List with fillable entries
$fillable = [
'serial_number' => $this->serial_number,
'name' => '',
'display_name' => '',
'version' => '',
'size' => 0,
'installed' => 0,
'status' => '',
'type' => '',
];
$new_installs = [];
$uninstalls = [];
$save_array = [];
# Loop through list
foreach ($mylist as $name => $props) {
// Get an instance of the fillable array
$temp = $fillable;
// Add name to temp
$temp['name'] = $name;
// Copy values and correct type
foreach ($temp as $key => $value) {
if (array_key_exists($key, $props)) {
$temp[$key] = $props[$key];
settype($temp[$key], gettype($value));
}
}
// Set version
if (isset($props['installed_version'])) {
$temp['version'] = $props['installed_version'];
} elseif (isset($props['version_to_install'])) {
$temp['version'] = $props['version_to_install'];
}
// Set installed size
if (isset($props['installed_size'])) {
$temp['size'] = $props['installed_size'];
}
$save_array[] = $temp;
// Store new installs
if ($temp['status'] == 'install_succeeded') {
$new_installs[] = $temp;
}
// Store uninstalls
if ($temp['status'] == 'uninstalled') {
$uninstalls[] = $temp;
}
}
$model = Managedinstalls_model::insertChunked(
$save_array
);
$this->_storeEvents($new_installs, $uninstalls);
return $this;
}
private function _storeEvents($new_installs, $uninstalls)
{
if ($new_installs) {
$count = count($new_installs);
$msg = array('count' => $count);
if ($count == 1) {
$pkg = array_pop($new_installs);
$msg['pkg'] = $pkg['display_name'] . ' ' . $pkg['version'];
}
$this->store_event(
'success',
'munki.package_installed',
json_encode($msg)
);
}
elseif ($uninstalls) {
$count = count($uninstalls);
$msg = array('count' => $count);
if ($count == 1) {
$pkg = array_pop($uninstalls);
$msg['pkg'] = $pkg['display_name'] . ' ' . $pkg['version'];
}
$this->store_event(
'success',
'munki.package_uninstalled',
json_encode($msg)
);
}
}
}