source: trunk/Modules/NetworkConfig/NetworkConfig.php

Last change on this file was 899, checked in by chronos, 3 years ago
File size: 4.4 KB
Line 
1<?php
2
3class ModuleNetworkConfig extends Module
4{
5 public array $ConfigItems;
6
7 function __construct(System $System)
8 {
9 parent::__construct($System);
10 $this->Name = 'NetworkConfig';
11 $this->Version = '1.0';
12 $this->Creator = 'Chronos';
13 $this->License = 'GNU/GPLv3';
14 $this->Description = 'Network device remote configuration';
15 $this->Dependencies = array(ModuleNetwork::GetName());
16 $this->Models = array(NetworkConfigurationLog::GetClassName(), NetworkConfiguration::GetClassName());
17
18 $this->ConfigItems = array();
19 }
20
21 function DoStart(): void
22 {
23 $this->System->FormManager->RegisterClass('NetworkConfiguration', array(
24 'Title' => 'Restart síťových služeb',
25 'Table' => 'NetworkConfiguration',
26 'Items' => array(
27 'Caption' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
28 'SysName' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
29 'Changed' => array('Type' => 'TNetworkConfigurationState', 'Caption' => 'Stav', 'Default' => 0),
30 'LastTime' => array('Type' => 'DateTime', 'Caption' => 'Naposledy spuštěno', 'ReadOnly' => true),
31 'ExecutionTime' => array('Type' => 'Integer', 'Caption' => 'Doba běhu', 'Default' => '0', 'Suffix' => 'sekund', 'ReadOnly' => true),
32 'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '0'),
33 'Period' => array('Type' => 'Integer', 'Caption' => 'Min. perioda', 'Default' => '60', 'Suffix' => 'sekund'),
34 ),
35 'ItemActions' => array(
36 array('Caption' => 'Záznam', 'URL' => '/is/?a=view&t=NetworkConfigurationLog&i=#RowId'),
37 ),
38 ));
39 $this->System->FormManager->RegisterClass('NetworkConfigurationLog', array(
40 'Title' => 'Záznam restartu síťových služeb',
41 'Table' => 'NetworkConfiguration',
42 'Items' => array(
43 'Caption' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
44 'Log' => array('Type' => 'Text', 'Caption' => 'Záznam', 'Default' => '', 'ReadOnly' => true),
45 ),
46 ));
47 $this->System->FormManager->RegisterFormType('TNetworkConfigurationState', array(
48 'Type' => 'Enumeration',
49 'States' => array('Neplánováno', 'V plánu', 'Provádí se'),
50 ));
51
52 $this->System->RegisterCommandLine('config', 'Configures network services.', array($this, 'Config'));
53 $this->System->Models['NetworkDevice']->RegisterOnChange('NetworkConfig', array($this, 'DoNetworkChange'));
54 $this->System->Models['NetworkInterface']->RegisterOnChange('NetworkConfig', array($this, 'DoNetworkChange'));
55 }
56
57 function DoNetworkChange(): void
58 {
59 $this->Database->query('UPDATE `NetworkConfiguration` SET `Changed`=1 WHERE '.
60 '(`Id`=1) OR (`Id`=7) OR (`Id`=8) OR (`Id`=9) OR (`Id`=10) OR (`Id`=11) OR (`Id`=12) OR (`Id`=13)');
61 }
62
63 function RegisterConfigItem(string $Name, string $ClassName): void
64 {
65 $this->ConfigItems[$Name] = $ClassName;
66 }
67
68 function UnregisterConfigItem(string $Name): void
69 {
70 unset($this->ConfigItems[$Name]);
71 }
72
73 function Config(array $Parameters): void
74 {
75 $Output = '';
76 if ($Parameters >= 3)
77 {
78 $ConfigItemName = $Parameters[2];
79 if (array_key_exists($ConfigItemName, $this->ConfigItems))
80 {
81 $ClassName = $this->ConfigItems[$ConfigItemName];
82 $ConfigItem = new $ClassName($this->System);
83 $ConfigItem->Run();
84 } else $Output = 'Config item '.$ConfigItemName.' not found';
85 } else $Output = 'Not enough parameters';
86 echo($Output);
87 }
88
89 static function Cast(Module $Module): ModuleNetworkConfig
90 {
91 if ($Module instanceof ModuleNetworkConfig)
92 {
93 return $Module;
94 }
95 throw new Exception('Expected ModuleNetworkConfig type but got '.gettype($Module));
96 }
97}
98
99class NetworkConfigItem extends Model
100{
101 function Run(): void
102 {
103 }
104}
105
106class NetworkConfiguration extends Model
107{
108 static function GetModelDesc(): ModelDesc
109 {
110 $Desc = new ModelDesc(self::GetClassName());
111 $Desc->AddString('Caption');
112 $Desc->AddString('SysName');
113 $Desc->AddBoolean('Changed');
114 $Desc->AddDateTime('LastTime');
115 $Desc->AddInteger('ExecutionTime');
116 $Desc->AddBoolean('Enabled');
117 $Desc->AddInteger('Period');
118 return $Desc;
119 }
120}
121
122class NetworkConfigurationLog extends Model
123{
124 static function GetModelDesc(): ModelDesc
125 {
126 $Desc = new ModelDesc(self::GetClassName());
127 $Desc->AddString('Caption');
128 $Desc->AddText('Log');
129 return $Desc;
130 }
131}
Note: See TracBrowser for help on using the repository browser.