1- <?php
2-
3- /*
4- Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5- All rights reserved.
6-
7- Redistribution and use in source and binary forms, with or without modification,
8- are permitted provided that the following conditions are met:
9-
10- * Redistributions of source code must retain the above copyright notice,
11- this list of conditions and the following disclaimer.
12- * Redistributions in binary form must reproduce the above copyright notice,
13- this list of conditions and the following disclaimer in the documentation
14- and/or other materials provided with the distribution.
15- * Neither the name of ISPConfig nor the names of its contributors
16- may be used to endorse or promote products derived from this software without
17- specific prior written permission.
18-
19- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22- IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26- OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29- */
30-
31- class network_settings_plugin {
32-
33- var $ plugin_name = 'network_settings_plugin ' ;
34- var $ class_name = 'network_settings_plugin ' ;
35-
36-
37- //* This function is called during ispconfig installation to determine
38- // if a symlink shall be created for this plugin.
39- function onInstall () {
40- global $ conf ;
41-
42- return true ;
43-
44- }
45-
46- /*
47- This function is called when the plugin is loaded
48- */
49-
50- function onLoad () {
51- global $ app ;
52-
53- /*
54- Register for the events
55- */
56-
57- $ app ->plugins ->registerEvent ('server_insert ' ,'network_settings_plugin ' ,'insert ' );
58- $ app ->plugins ->registerEvent ('server_update ' ,'network_settings_plugin ' ,'update ' );
59-
60- $ app ->plugins ->registerEvent ('server_ip_insert ' ,'network_settings_plugin ' ,'insert ' );
61- $ app ->plugins ->registerEvent ('server_ip_update ' ,'network_settings_plugin ' ,'update ' );
62-
63-
64-
65- }
66-
67- function insert ($ event_name ,$ data ) {
68- global $ app , $ conf ;
69-
70- $ this ->update ($ event_name ,$ data );
71-
72- }
73-
74- // The purpose of this plugin is to rewrite the main.cf file
75- function update ($ event_name ,$ data ) {
76- global $ app , $ conf ;
77-
78- // get the config
79- $ app ->uses ("getconf " );
80- $ server_config = $ app ->getconf ->get_server_config ($ conf ["server_id " ], 'server ' );
81-
82- // Configure the debian network card settings
83- if (is_file ('/etc/debian_version ' ) && $ server_config ['auto_network_configuration ' ] == 'y ' ) {
84- copy ('/etc/network/interfaces ' ,'/etc/network/interfaces~ ' );
85-
86- $ app ->load ('tpl ' );
87-
88- $ network_tpl = new tpl ();
89- $ network_tpl ->newTemplate ("debian_network_interfaces.master " );
90-
91- $ network_tpl ->setVar ('ip_address ' ,$ server_config ["ip_address " ]);
92- $ network_tpl ->setVar ('netmask ' ,$ server_config ["netmask " ]);
93- $ network_tpl ->setVar ('gateway ' ,$ server_config ["gateway " ]);
94- $ network_tpl ->setVar ('broadcast ' ,$ this ->broadcast ($ server_config ["ip_address " ],$ server_config ["netmask " ]));
95- $ network_tpl ->setVar ('network ' ,$ this ->network ($ server_config ["ip_address " ],$ server_config ["netmask " ]));
96-
97- $ records = $ app ->db ->queryAllRecords ("SELECT ip_address FROM server_ip WHERE server_id = " .intval ($ conf ["server_id " ]));
98- $ ip_records = array ();
99- $ additionl_ip_records = 0 ;
100- $ n = 0 ;
101- if (is_array ($ records )) {
102- foreach ($ records as $ rec ) {
103- $ ip_records [] = array (
104- 'id ' => $ n ,
105- 'ip_address ' => $ rec ['ip_address ' ],
106- 'netmask ' => $ server_config ["netmask " ],
107- 'gateway ' => $ server_config ["gateway " ],
108- 'broadcast ' => $ this ->broadcast ($ rec ['ip_address ' ],$ server_config ["netmask " ]),
109- 'network ' => $ this ->network ($ rec ['ip_address ' ],$ server_config ["netmask " ])
110- );
111- $ additionl_ip_records = 1 ;
112- $ n ++;
113- }
114- }
115-
116- $ network_tpl ->setVar ('additionl_ip_records ' ,$ additionl_ip_records );
117- $ network_tpl ->setLoop ('interfaces ' ,$ ip_records );
118-
119- file_put_contents ('/etc/network/interfaces ' ,$ network_tpl ->grab ());
120- unset($ network_tpl );
121-
122- $ app ->log ("Changed Network settings " ,LOGLEVEL_DEBUG );
123-
124- exec ('/etc/init.d/networking force-reload ' );
125- } else {
126- if (is_file ('/etc/debian_version ' )) {
127- $ app ->log ("Network configuration disabled in server settings. " ,LOGLEVEL_WARN );
128- } else {
129- $ app ->log ("Network configuration not available for this linux distribution. " ,LOGLEVEL_DEBUG );
130- }
131- }
132-
133- }
134-
135- function network ($ ip , $ netmask ){
136- $ netmask = $ this ->netmask ($ netmask );
137- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ netmask );
138- $ netmask_bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
139- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ ip );
140- $ ip_bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
141- for ($ i =0 ;$ i <32 ;$ i ++){
142- $ network_bin .= substr ($ netmask_bin ,$ i ,1 ) * substr ($ ip_bin ,$ i ,1 );
143- }
144- $ network_bin = wordwrap ($ network_bin , 8 , ". " , 1 );
145- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ network_bin ));
146- return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
147- }
148-
149- function broadcast ($ ip , $ netmask ){
150- $ netmask = $ this ->netmask ($ netmask );
151- $ binary_netmask = $ this ->binary_netmask ($ netmask );
152- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ ip );
153- $ ip_bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
154- $ broadcast_bin = str_pad (substr ($ ip_bin , 0 , $ binary_netmask ),32 ,"1 " ,STR_PAD_RIGHT );
155- $ broadcast_bin = wordwrap ($ broadcast_bin , 8 , ". " , 1 );
156- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ broadcast_bin ));
157- return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
158- }
159-
160- function netmask ($ netmask ){
161- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ netmask ));
162- $ bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
163- $ parts = explode ("0 " , $ bin );
164- $ bin = str_pad ($ parts [0 ], 32 , "0 " , STR_PAD_RIGHT );
165- $ bin = wordwrap ($ bin , 8 , ". " , 1 );
166- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ bin ));
167- return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
168- }
169-
170- function binary_netmask ($ netmask ){
171- list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ netmask ));
172- $ bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
173- $ parts = explode ("0 " , $ bin );
174- return substr_count ($ parts [0 ], "1 " );
175- }
176-
177- } // end class
178-
179-
180-
1+ <?php
2+
3+ /*
4+ Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5+ All rights reserved.
6+
7+ Redistribution and use in source and binary forms, with or without modification,
8+ are permitted provided that the following conditions are met:
9+
10+ * Redistributions of source code must retain the above copyright notice,
11+ this list of conditions and the following disclaimer.
12+ * Redistributions in binary form must reproduce the above copyright notice,
13+ this list of conditions and the following disclaimer in the documentation
14+ and/or other materials provided with the distribution.
15+ * Neither the name of ISPConfig nor the names of its contributors
16+ may be used to endorse or promote products derived from this software without
17+ specific prior written permission.
18+
19+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+ IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+ */
30+
31+ class network_settings_plugin {
32+
33+ var $ plugin_name = 'network_settings_plugin ' ;
34+ var $ class_name = 'network_settings_plugin ' ;
35+
36+
37+ //* This function is called during ispconfig installation to determine
38+ // if a symlink shall be created for this plugin.
39+ function onInstall () {
40+ global $ conf ;
41+
42+ return true ;
43+
44+ }
45+
46+ /*
47+ This function is called when the plugin is loaded
48+ */
49+
50+ function onLoad () {
51+ global $ app ;
52+
53+ /*
54+ Register for the events
55+ */
56+
57+ $ app ->plugins ->registerEvent ('server_insert ' ,'network_settings_plugin ' ,'insert ' );
58+ $ app ->plugins ->registerEvent ('server_update ' ,'network_settings_plugin ' ,'update ' );
59+
60+ $ app ->plugins ->registerEvent ('server_ip_insert ' ,'network_settings_plugin ' ,'insert ' );
61+ $ app ->plugins ->registerEvent ('server_ip_update ' ,'network_settings_plugin ' ,'update ' );
62+
63+
64+
65+ }
66+
67+ function insert ($ event_name ,$ data ) {
68+ global $ app , $ conf ;
69+
70+ $ this ->update ($ event_name ,$ data );
71+
72+ }
73+
74+ // The purpose of this plugin is to rewrite the main.cf file
75+ function update ($ event_name ,$ data ) {
76+ global $ app , $ conf ;
77+
78+ // get the config
79+ $ app ->uses ("getconf " );
80+ $ server_config = $ app ->getconf ->get_server_config ($ conf ["server_id " ], 'server ' );
81+
82+ // Configure the debian network card settings
83+ if (is_file ('/etc/debian_version ' ) && $ server_config ['auto_network_configuration ' ] == 'y ' ) {
84+ copy ('/etc/network/interfaces ' ,'/etc/network/interfaces~ ' );
85+
86+ $ app ->load ('tpl ' );
87+
88+ $ network_tpl = new tpl ();
89+ $ network_tpl ->newTemplate ("debian_network_interfaces.master " );
90+
91+ $ network_tpl ->setVar ('ip_address ' ,$ server_config ["ip_address " ]);
92+ $ network_tpl ->setVar ('netmask ' ,$ server_config ["netmask " ]);
93+ $ network_tpl ->setVar ('gateway ' ,$ server_config ["gateway " ]);
94+ $ network_tpl ->setVar ('broadcast ' ,$ this ->broadcast ($ server_config ["ip_address " ],$ server_config ["netmask " ]));
95+ $ network_tpl ->setVar ('network ' ,$ this ->network ($ server_config ["ip_address " ],$ server_config ["netmask " ]));
96+
97+ $ records = $ app ->db ->queryAllRecords ("SELECT ip_address FROM server_ip WHERE server_id = " .intval ($ conf ["server_id " ]) . " order by ip_address " );
98+ $ ip_records = array ();
99+ $ additionl_ip_records = 0 ;
100+ $ n = 0 ;
101+ if (is_array ($ records )) {
102+ foreach ($ records as $ rec ) {
103+ /*
104+ * don't insert the main-ip again!
105+ */
106+ if ($ rec ['ip_address ' ] != $ server_config ["ip_address " ])
107+ {
108+ $ ip_records [$ n ] = array (
109+ 'id ' => $ n ,
110+ 'ip_address ' => $ rec ['ip_address ' ],
111+ 'netmask ' => $ server_config ["netmask " ],
112+ 'gateway ' => $ server_config ["gateway " ],
113+ 'broadcast ' => $ this ->broadcast ($ rec ['ip_address ' ],$ server_config ["netmask " ]),
114+ 'network ' => $ this ->network ($ rec ['ip_address ' ],$ server_config ["netmask " ])
115+ );
116+ $ additionl_ip_records = 1 ;
117+ $ n ++;
118+ }
119+ }
120+ }
121+
122+ /*
123+ * If we have more than 1 IP we have to add the main-ip at the end
124+ * of the network-ip-list. If we don't do so, there may be problems
125+ * in multi-server-settings (with the acces from other server to the
126+ * main-server) because the LAST IP in the list is the IP mysql uses
127+ * to determine the host, the user is logging in from.
128+ */
129+ if ($ additionl_ip_records != 0 )
130+ {
131+ $ swap ["ip_address " ] = $ ip_records [$ n -1 ]["ip_address " ];
132+ $ swap ["netmask " ] = $ ip_records [$ n -1 ]["netmask " ];
133+ $ swap ["gateway " ] = $ ip_records [$ n -1 ]["gateway " ];
134+
135+ $ ip_records [$ n -1 ] = array (
136+ 'id ' => $ n -1 ,
137+ 'ip_address ' => $ server_config ['ip_address ' ],
138+ 'netmask ' => $ server_config ["netmask " ],
139+ 'gateway ' => $ server_config ["gateway " ],
140+ 'broadcast ' => $ this ->broadcast ($ server_config ['ip_address ' ],$ server_config ["netmask " ]),
141+ 'network ' => $ this ->network ($ server_config ['ip_address ' ],$ server_config ["netmask " ])
142+ );
143+ $ network_tpl ->setVar ('ip_address ' ,$ swap ["ip_address " ]);
144+ $ network_tpl ->setVar ('netmask ' ,$ swap ["netmask " ]);
145+ $ network_tpl ->setVar ('gateway ' ,$ swap ["gateway " ]);
146+ $ network_tpl ->setVar ('broadcast ' ,$ this ->broadcast ($ swap ["ip_address " ],$ swap ["netmask " ]));
147+ $ network_tpl ->setVar ('network ' ,$ this ->network ($ swap ["ip_address " ],$ swap ["netmask " ]));
148+ }
149+
150+ $ network_tpl ->setVar ('additionl_ip_records ' ,$ additionl_ip_records );
151+ $ network_tpl ->setLoop ('interfaces ' ,$ ip_records );
152+ file_put_contents ('/etc/network/interfaces ' ,$ network_tpl ->grab ());
153+ unset($ network_tpl );
154+
155+ $ app ->log ("Changed Network settings " ,LOGLEVEL_DEBUG );
156+ exec ('/etc/init.d/networking force-reload ' );
157+ } else {
158+ if (is_file ('/etc/debian_version ' )) {
159+ $ app ->log ("Network configuration disabled in server settings. " ,LOGLEVEL_WARN );
160+ } else {
161+ $ app ->log ("Network configuration not available for this linux distribution. " ,LOGLEVEL_DEBUG );
162+ }
163+ }
164+
165+ }
166+
167+ function network ($ ip , $ netmask ){
168+ $ netmask = $ this ->netmask ($ netmask );
169+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ netmask );
170+ $ netmask_bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
171+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ ip );
172+ $ ip_bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
173+ for ($ i =0 ;$ i <32 ;$ i ++){
174+ $ network_bin .= substr ($ netmask_bin ,$ i ,1 ) * substr ($ ip_bin ,$ i ,1 );
175+ }
176+ $ network_bin = wordwrap ($ network_bin , 8 , ". " , 1 );
177+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ network_bin ));
178+ return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
179+ }
180+
181+ function broadcast ($ ip , $ netmask ){
182+ $ netmask = $ this ->netmask ($ netmask );
183+ $ binary_netmask = $ this ->binary_netmask ($ netmask );
184+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ ip );
185+ $ ip_bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
186+ $ broadcast_bin = str_pad (substr ($ ip_bin , 0 , $ binary_netmask ),32 ,"1 " ,STR_PAD_RIGHT );
187+ $ broadcast_bin = wordwrap ($ broadcast_bin , 8 , ". " , 1 );
188+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ broadcast_bin ));
189+ return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
190+ }
191+
192+ function netmask ($ netmask ){
193+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ netmask ));
194+ $ bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
195+ $ parts = explode ("0 " , $ bin );
196+ $ bin = str_pad ($ parts [0 ], 32 , "0 " , STR_PAD_RIGHT );
197+ $ bin = wordwrap ($ bin , 8 , ". " , 1 );
198+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ bin ));
199+ return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
200+ }
201+
202+ function binary_netmask ($ netmask ){
203+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ netmask ));
204+ $ bin = str_pad (decbin ($ f1 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f2 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f3 ),8 ,"0 " ,STR_PAD_LEFT ).str_pad (decbin ($ f4 ),8 ,"0 " ,STR_PAD_LEFT );
205+ $ parts = explode ("0 " , $ bin );
206+ return substr_count ($ parts [0 ], "1 " );
207+ }
208+
209+ } // end class
210+
211+
212+
181213?>
0 commit comments