| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg\Plugins; |
| 4 | |
| 5 | /** |
| 6 | * Example of the `host` group plugin. |
| 7 | * |
| 8 | * To activate the plugin, rename this file so that the file name (without extension) |
| 9 | * matches the name of the parent directory. |
| 10 | * |
| 11 | * The plugin must implement the `PluginInterface` interface. |
| 12 | * Currently `hostInformationStart`, `hostInformationFinish` events are processed. |
| 13 | * In this example, the `removeFields` method handles the `hostInformationStart` event, |
| 14 | * and the `modifyData` method handles the `hostInformationFinish` event. |
| 15 | * Note that in the both methods, the data array is passed by reference. |
| 16 | * |
| 17 | * The `removeFields` method can be used to filter the data being processed and displayed. |
| 18 | * Simply delete the fields you do not need in order to exclude them from processing. |
| 19 | * |
| 20 | * The `modifyData` method can be used to add, modify, reorder, and rename data items. |
| 21 | * If data elements are being replaced, such elements should be removed in the first step |
| 22 | * to eliminate unnecessary work. |
| 23 | * |
| 24 | * The plugin replaces two items and add new one in the Main section. |
| 25 | * Renames one item in the Statistics section, and adds a new section. |
| 26 | * |
| 27 | * ---------- Before ----------- |
| 28 | * |
| 29 | * HOST INFORMATION |
| 30 | * |
| 31 | * IP address: 1.2.3.4 |
| 32 | * rDNS name: host.domain.ltd |
| 33 | * Reverse IP: match |
| 34 | * |
| 35 | * STATISTICS |
| 36 | * |
| 37 | * Total reports: 5 |
| 38 | * Total messages: 6 |
| 39 | * Last report: Jan 1 2001 |
| 40 | * |
| 41 | * |
| 42 | * ---------- After ------------ |
| 43 | * |
| 44 | * HOST INFORMATION |
| 45 | * |
| 46 | * IP address: 1.2.3.4 |
| 47 | * rDNS name: host.example.com |
| 48 | * Reverse IP: match |
| 49 | * ISP: ABC Telecom |
| 50 | * |
| 51 | * STATISTICS |
| 52 | * |
| 53 | * Total reports: 5 |
| 54 | * Total messages: 6 |
| 55 | * Last report date: Jan 1 2001 |
| 56 | * |
| 57 | * PLUGIN INFORMATION |
| 58 | * |
| 59 | * Name: ExampleHost |
| 60 | * Version: 1.0 |
| 61 | */ |
| 62 | class ExampleHostPlugin implements PluginInterface |
| 63 | { |
| 64 | /** |
| 65 | * Returns the name of the plugin. Used when displaying errors |
| 66 | * |
| 67 | * @return string Any string that identifies the plugin |
| 68 | */ |
| 69 | public function name(): string |
| 70 | { |
| 71 | return 'ExampleHostPlugin'; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Associates events with handler method names and returns them as an array |
| 76 | * |
| 77 | * Available events are: `hostInformationStart`, `hostInformationFinish` |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public function subscribedEvents(): array |
| 82 | { |
| 83 | return [ |
| 84 | 'hostInformationStart' => 'removeFields', |
| 85 | 'hostInformationFinish' => 'modifyData' |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Handles the `hostInformationStart` event (see the subscribedEvents method) |
| 91 | * |
| 92 | * To prevent the data item from being processed and displayed on the client, |
| 93 | * just remove the corresponding field from the array by the `fields` key. |
| 94 | * |
| 95 | * @param array &$data Array with the following keys: |
| 96 | * `ip` - Host IP address |
| 97 | * `event` - Event name |
| 98 | * `fields` - Array of strings with fields to get information for. Can be the following: |
| 99 | * 'main.rdns' - Reverse DNS name |
| 100 | * 'main.rip' - Reverse IP |
| 101 | * 'stats.reports' - Total reports |
| 102 | * 'stats.messages' - Total messages |
| 103 | * 'stats.last_report' - Last report date |
| 104 | * Note that the list of fields may vary depending on the client's needs. |
| 105 | * |
| 106 | * @return void |
| 107 | */ |
| 108 | public function removeFields(array &$data): void |
| 109 | { |
| 110 | // Note that if you remove the main.rdns item, the main.rip item will not be defined correctly |
| 111 | // because it relies on the value of main.rdns. So if you override the first one, |
| 112 | // you should override the second one as well. |
| 113 | $old_count = count($data['fields']); |
| 114 | $data['fields'] = array_filter($data['fields'], function ($fld) { |
| 115 | return $fld !== 'main.rdns' && $fld != 'main.rip'; |
| 116 | }); |
| 117 | $data['dns_removed'] = ($old_count > count($data['fields'])); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Handles the `hostInformationFinish` event (see the subscribedEvents method) |
| 122 | * |
| 123 | * @param array &$data Array with the following keys: |
| 124 | * `ip` - Same as in the `removeFields` method |
| 125 | * `event` - Same as in the `removeFields` method |
| 126 | * `fields` - Same as in the `removeFields` method |
| 127 | * `result` - Array of [ 'field' => 'value' ] items |
| 128 | * The data from this array will be displayed |
| 129 | * in the same order as here, grouped by section name. |
| 130 | * The main section is displayed always displayed first. |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public function modifyData(array &$data): void |
| 135 | { |
| 136 | // The host name, reverse IP and ISP name. |
| 137 | // The main section is always displayed first, |
| 138 | // so you can add items to the end of the array. |
| 139 | if ($data['dns_removed']) { |
| 140 | $data['result'][] = [ 'main.rdns', 'host.example.com' ]; |
| 141 | $data['result'][] = [ 'main.rip', true ]; |
| 142 | $data['result'][] = [ 'main.ISP', 'ABC Telecom' ]; |
| 143 | } |
| 144 | |
| 145 | // Plugin section |
| 146 | $data['result'][] = [ 'plugin.Name', 'ExampleHost' ]; |
| 147 | $data['result'][] = [ 'plugin.Version', '1.0' ]; |
| 148 | |
| 149 | // Renaming a section |
| 150 | $data['dictionary']['plugin'] = 'Plugin information'; |
| 151 | |
| 152 | // Renaming an item |
| 153 | $data['dictionary']['stats.last_report'] = 'Last report date'; |
| 154 | } |
| 155 | } |