1+ <?php
2+
3+ /*
4+ Copyright (c) 2008, 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+ /*
38+ This function is called when the plugin is loaded
39+ */
40+
41+ function onLoad () {
42+ global $ app ;
43+
44+ /*
45+ Register for the events
46+ */
47+
48+ $ app ->plugins ->registerEvent ('server_insert ' ,'network_settings_plugin ' ,'insert ' );
49+ $ app ->plugins ->registerEvent ('server_update ' ,'network_settings_plugin ' ,'update ' );
50+
51+ $ app ->plugins ->registerEvent ('server_ip_insert ' ,'network_settings_plugin ' ,'insert ' );
52+ $ app ->plugins ->registerEvent ('server_ip_update ' ,'network_settings_plugin ' ,'update ' );
53+
54+
55+
56+ }
57+
58+ function insert ($ event_name ,$ data ) {
59+ global $ app , $ conf ;
60+
61+ $ this ->update ($ event_name ,$ data );
62+
63+ }
64+
65+ // The purpose of this plugin is to rewrite the main.cf file
66+ function update ($ event_name ,$ data ) {
67+ global $ app , $ conf ;
68+
69+ // get the config
70+ $ app ->uses ("getconf " );
71+ $ server_config = $ app ->getconf ->get_server_config ($ conf ["server_id " ], 'server ' );
72+
73+ // Configure the debian network card settings
74+ if (is_file ('/etc/debian_version ' ) && $ server_config ['auto_network_configuration ' ] == 'y ' ) {
75+ copy ('/etc/network/interfaces ' ,'/etc/network/interfaces~ ' );
76+
77+ $ app ->load ('tpl ' );
78+
79+ $ network_tpl = new tpl ();
80+ $ network_tpl ->newTemplate ("debian_network_interfaces.master " );
81+
82+ $ network_tpl ->setVar ('ip_address ' ,$ server_config ["ip_address " ]);
83+ $ network_tpl ->setVar ('netmask ' ,$ server_config ["netmask " ]);
84+ $ network_tpl ->setVar ('gateway ' ,$ server_config ["gateway " ]);
85+ $ network_tpl ->setVar ('broadcast ' ,$ this ->broadcast ($ server_config ["ip_address " ],$ server_config ["netmask " ]));
86+ $ network_tpl ->setVar ('network ' ,$ this ->network ($ server_config ["ip_address " ],$ server_config ["netmask " ]));
87+
88+ $ records = $ app ->db ->queryAllRecords ("SELECT ip_address FROM server_ip WHERE server_id = " .intval ($ conf ["server_id " ]));
89+ $ ip_records = array ();
90+ $ additionl_ip_records = 0 ;
91+ $ n = 0 ;
92+ if (is_array ($ records )) {
93+ foreach ($ records as $ rec ) {
94+ $ ip_records [] = array (
95+ 'id ' => $ n ,
96+ 'ip_address ' => $ rec ['ip_address ' ],
97+ 'netmask ' => $ server_config ["netmask " ],
98+ 'gateway ' => $ server_config ["gateway " ],
99+ 'broadcast ' => $ this ->broadcast ($ rec ['ip_address ' ],$ server_config ["netmask " ]),
100+ 'network ' => $ this ->network ($ rec ['ip_address ' ],$ server_config ["netmask " ])
101+ );
102+ $ additionl_ip_records = 1 ;
103+ $ n ++;
104+ }
105+ }
106+
107+ $ network_tpl ->setVar ('additionl_ip_records ' ,$ additionl_ip_records );
108+ $ network_tpl ->setLoop ('interfaces ' ,$ ip_records );
109+
110+ file_put_contents ('/etc/network/interfaces ' ,$ network_tpl ->grab ());
111+ unset($ network_tpl );
112+
113+ $ app ->log ("Changed Network settings " ,LOGLEVEL_DEBUG );
114+
115+ exec ('/etc/init.d/networking force-reload ' );
116+ }
117+
118+ }
119+
120+ function network ($ ip , $ netmask ){
121+ $ netmask = $ this ->netmask ($ netmask );
122+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ netmask );
123+ $ 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 );
124+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ ip );
125+ $ 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 );
126+ for ($ i =0 ;$ i <32 ;$ i ++){
127+ $ network_bin .= substr ($ netmask_bin ,$ i ,1 ) * substr ($ ip_bin ,$ i ,1 );
128+ }
129+ $ network_bin = wordwrap ($ network_bin , 8 , ". " , 1 );
130+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ network_bin ));
131+ return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
132+ }
133+
134+ function broadcast ($ ip , $ netmask ){
135+ $ netmask = $ this ->netmask ($ netmask );
136+ $ binary_netmask = $ this ->binary_netmask ($ netmask );
137+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , $ ip );
138+ $ 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 );
139+ $ broadcast_bin = str_pad (substr ($ ip_bin , 0 , $ binary_netmask ),32 ,"1 " ,STR_PAD_RIGHT );
140+ $ broadcast_bin = wordwrap ($ broadcast_bin , 8 , ". " , 1 );
141+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ broadcast_bin ));
142+ return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
143+ }
144+
145+ function netmask ($ netmask ){
146+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ netmask ));
147+ $ 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 );
148+ $ parts = explode ("0 " , $ bin );
149+ $ bin = str_pad ($ parts [0 ], 32 , "0 " , STR_PAD_RIGHT );
150+ $ bin = wordwrap ($ bin , 8 , ". " , 1 );
151+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ bin ));
152+ return bindec ($ f1 ).". " .bindec ($ f2 ).". " .bindec ($ f3 ).". " .bindec ($ f4 );
153+ }
154+
155+ function binary_netmask ($ netmask ){
156+ list ($ f1 ,$ f2 ,$ f3 ,$ f4 ) = explode (". " , trim ($ netmask ));
157+ $ 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 );
158+ $ parts = explode ("0 " , $ bin );
159+ return substr_count ($ parts [0 ], "1 " );
160+ }
161+
162+ } // end class
163+
164+
165+
166+ ?>
0 commit comments