Changeset 2949
- Timestamp:
- 05/31/10 05:16:02 (21 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
CHANGELOG (modified) (1 diff)
-
gui/admin/settings_ports.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/CHANGELOG
r2948 r2949 6 6 * Added toArray() method in IspCP_ConfigHandler class to be able to 7 7 get an associative array that contain all configuration parameters 8 * Complete rewrite of the admin/settings_ports.php script 8 9 9 10 2010-05-30 Laurent Declercq -
trunk/gui/admin/settings_ports.php
r2941 r2949 29 29 */ 30 30 31 // Include all needed libraries 31 32 require '../include/ispcp-lib.php'; 32 33 34 // Check for login 33 35 check_login(__FILE__); 34 36 35 $cfg = IspCP_Registry::get('Config'); 36 37 $tpl = new pTemplate(); 38 $tpl->define_dynamic('page', $cfg->ADMIN_TEMPLATE_PATH . '/settings_ports.tpl'); 39 $tpl->define_dynamic('service_ports', 'page'); 40 $tpl->define_dynamic('port_delete_link', 'service_ports'); 41 $tpl->define_dynamic('port_delete_show', 'service_ports'); 42 43 $tpl->assign( 44 array( 45 'TR_ADMIN_SETTINGS_PAGE_TITLE' => tr('ispCP - Admin/Settings'), 46 'THEME_COLOR_PATH' => "../themes/{$cfg->USER_INITIAL_THEME}", 47 'THEME_CHARSET' => tr('encoding'), 48 'ISP_LOGO' => get_logo(get_session('user_id')) 49 ) 50 ); 51 52 function update_services(&$sql) { 37 /******************************************************************************* 38 * Functions 39 */ 40 41 /** 42 * Gets and prepares the template part for services ports 43 * 44 * @param pTemplate &$tpl Reference to a pTemplate instance 45 * @return void; 46 */ 47 function show_services(&$tpl) { 53 48 54 49 $cfg = IspCP_Registry::get('Config'); 55 50 $db_cfg = IspCP_Registry::get('Db_Config'); 56 51 57 if (isset($_POST['uaction']) && $_POST['uaction'] == "apply") { 58 $count = count(get_post('name')); 59 $break = false; 60 $service_name = get_post('name'); 61 $var_name = get_post('var_name'); 62 $ip = get_post('ip'); 63 $port = get_post('port'); 64 $protocol = get_post('port_type'); 65 $status = get_post('show_val'); 66 $custom = get_post('custom'); 67 68 for ($j = 0; $j < $count; $j++) { 69 if (!is_number($port[$j]) OR $port[$j] <= 0) { 70 set_page_message(tr('ERROR: Only positive numbers are allowed !')); 71 $break = true; 72 break; 52 $filter = create_function('$v', 'if(substr($v,0,5) == "PORT_") return $v;'); 53 $services = array_filter(array_keys($db_cfg->toArray()), $filter); 54 55 if(empty($services)) { 56 $tpl->assign('SERVICE_PORTS', ''); 57 58 set_page_message(tr('You have no custom service ports defined.')); 59 } else { 60 sort($services); 61 62 foreach($services as $i => $service) { 63 64 $tpl->assign('CLASS', ($i % 2 == 0) ? 'content' : 'content2'); 65 66 $v = (count(explode(';', $db_cfg->$service)) < 6) 67 ? $db_cfg->$service . ';' : $db_cfg->$service; 68 69 list($port, $proto, $name, $status, $custom, $ip) = explode(';', $v); 70 71 $selected_udp = $proto == 'udp' ? $cfg->HTML_SELECTED : ''; 72 $selected_tcp = $proto == 'udp' ? '' : $cfg->HTML_SELECTED; 73 74 $selected_on = $status == '1' ? $cfg->HTML_SELECTED : ''; 75 $selected_off = $status == '1' ? '' : $cfg->HTML_SELECTED; 76 77 if ($custom == 0) { 78 $tpl->assign( 79 array( 80 'SERVICE' => tohtml($name) . 81 '<input name="name[]" type="hidden" id="name' . 82 $i . '" value="' . tohtml($name) . '" />', 83 84 'PORT_READONLY' => $cfg->HTML_READONLY, 85 'PROTOCOL_READONLY' => $cfg->HTML_DISABLED, 86 'TR_DELETE' => '-', 87 'PORT_DELETE_LINK' => '', 88 'NUM' => $i 89 ) 90 ); 91 92 $tpl->parse('PORT_DELETE_SHOW', ''); 93 } else { 94 95 $tpl->assign( 96 array( 97 'SERVICE' => 98 '<input name="name[]" type="text" id="name' . 99 $i . '" value="' . tohtml($name) . 100 '" class="textinput" maxlength="25" />', 101 102 'NAME' => tohtml($name), 103 'PORT_READONLY' => '', 104 'PROTOCOL_READONLY' => '', 105 'TR_DELETE' => tr('Delete'), 106 'URL_DELETE' => "?delete=$service", 107 'PORT_DELETE_SHOW' => '', 108 'NUM' => $i 109 ) 110 ); 111 112 $tpl->parse('PORT_DELETE_LINK', 'port_delete_link'); 113 } 114 115 $tpl->assign( 116 array( 117 'CUSTOM' => tohtml($custom), 118 'VAR_NAME' => tohtml($service), 119 'IP' => (($ip == '127.0.0.1') 120 ? 'localhost' 121 : (empty($ip) ? $cfg->BASE_SERVER_IP : tohtml($ip))), 122 'PORT' => $port, 123 'SELECTED_UDP' => $selected_udp, 124 'SELECTED_TCP' => $selected_tcp, 125 'SELECTED_ON' => $selected_on, 126 'SELECTED_OFF' => $selected_off 127 ) 128 ); 129 130 $tpl->parse('SERVICE_PORTS', '.service_ports'); 131 } 132 } 133 } // end show_services() 134 135 136 /** 137 * Validates a service port 138 * 139 * @since 1.0.6 140 * @author Laurent declercq (nuxwin) <laurent.declercq@ispcp.net> 141 * @param string $name Service port name 142 * @param string $ip Ip address 143 * @param int $port Service port 144 * @param string $proto Service port protocol 145 * @param int $show 146 * @param boolean $on_updt True: validates for update 147 */ 148 function validates_service($name, $ip, $port, $proto, $show, $on_updt = false) { 149 150 $db_cfg = IspCP_Registry::get('Db_Config'); 151 152 $db_sname = "PORT_$name"; 153 $ip = ($ip == 'localhost') ? '127.0.0.1' : $ip; 154 155 if (!is_basicString($name)) { 156 $e = tr('ERROR: Only letters, numbers, dash and underscore are allowed!'); 157 } elseif(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { 158 $e = tr('ERROR: Wrong Ip number!'); 159 } elseif(!is_number($port) || $port <= 0) { 160 $e = tr('ERROR: Only positive numbers are allowed!'); 161 } elseif(isset($db_cfg->$db_sname) && !$on_updt) { 162 $e = tr('ERROR: Service port already exists!'); 163 } elseif($proto != 'tcp' && $proto != 'udp') { 164 $e = tr('ERROR: Unallowed protocol!'); 165 } elseif($show != '0' && $show != '1') { 166 $e = tr('ERROR: Bad value for show entry!'); 167 } else { 168 return true; 169 } 170 171 set_page_message($e); 172 173 return false; 174 } 175 176 /** 177 * Adds or updates a service port 178 * 179 * @return void 180 */ 181 function add_update_services() { 182 183 $cfg = IspCP_Registry::get('Config'); 184 $db_cfg = IspCP_Registry::get('Db_Config'); 185 186 // Adds a service port 187 if(isset($_POST['name_new']) && !empty($_POST['name_new'])) { 188 189 $name = strtoupper($_POST['name_new']); 190 $ip = $_POST['ip_new']; 191 $port = $_POST['port_new']; 192 $proto = $_POST['port_type_new']; 193 $show = $_POST['show_val_new']; 194 195 if(validates_service($name, $ip, $port, $proto, $show)) { 196 $db_sname = "PORT_$name"; 197 198 // Add the service port in the database 199 // See the {@link IspCP_ConfigHandler_Db} adapter class to learn 200 // how it work 201 $db_cfg->$db_sname = "$port;$proto;$name;$show;1;$ip"; 202 203 write_log( 204 get_session('user_logged') . 205 ": Added service port $name ($port)!" 206 ); 207 208 set_page_message(tr('Service port was added!')); 209 } else { 210 return; 211 } 212 213 // Updates one or more services ports 214 } elseif(isset($_POST['name']) && !empty($_POST['name'])) { 215 foreach($_POST['name'] as $index => $name) { 216 217 $ip = $_POST['ip'][$index]; 218 $port = $_POST['port'][$index]; 219 $proto = $_POST['port_type'][$index]; 220 $show = $_POST['show_val'][$index]; 221 222 if(validates_service($name, $ip, $port, $proto, $show, true)) { 223 $db_sname = "PORT_$name"; 224 225 // Update the service port in the database 226 // See the {@link IspCP_ConfigHandler_Db} adapter class to learn 227 // how it work 228 $db_cfg->$db_sname = "$port;$proto;$name;$show;1;$ip"; 229 } else { 230 return; 73 231 } 74 232 } 75 233 76 if (!$break) { 77 // Adding new Ports! 78 if (isset($_POST['name_new']) && !empty($_POST['name_new'])) { 79 $ip = get_post('ip_new'); 80 $port = get_post('port_new'); 81 $name = strtoupper(get_post('name_new')); 82 $protocol = get_post('port_type_new'); 83 $status = get_post('show_val_new'); 84 85 if (!is_number($port) OR $port <= 0) { 86 set_page_message(tr('ERROR: Only positive numbers are allowed !')); 87 return; 88 } elseif (!is_basicString($name)) { 89 set_page_message(tr('ERROR: Only Letters, Numbers, Dash and Underscore are allowed!')); 90 return; 91 } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { 92 set_page_message(tr('Wrong IP number!')); 93 return; 94 } else { 95 // Check if PORT exists 96 $query = " 97 SELECT 98 `name` 99 FROM 100 `config` 101 WHERE 102 `name` = ? 103 "; 104 $var = "PORT_" . $name; 105 $rs = exec_query($sql, $query, array($var)); 106 107 if ($rs->RecordCount() == 0) { 108 $value = implode(";", array($port, $protocol, $name, $status, 1, $ip)); 109 $db_cfg->$var = $value; 110 //setConfig_Value($var, $value); 111 write_log(get_session('user_logged') . ": add service port $name ({$port})!"); 112 } else { 113 set_page_message(tr('ERROR: Port already exists!')); 114 return; 115 } 116 } 117 } else { 118 for ($j = 0; $j < $count; $j++) { 119 $var = $var_name[$j]; 120 $name = strtoupper(strip_tags($service_name[$j])); 121 $value = implode(";", array($port[$j], $protocol[$j], $name, $status[$j], $custom[$j], $ip[$j])); 122 123 $db_cfg->$var = $value; 124 //setConfig_Value($var, $value); 125 } 126 } 127 set_page_message(tr('Settings saved !')); 128 } 129 130 $cfg->replace_with($db_cfg); 131 } 132 } 133 234 set_page_message(tr('Service(s) port was updated !')); 235 } 236 } // end add_update_services() 237 238 /** 239 * Remove a service port from the database 240 * 241 * @param string service name 242 * return void 243 */ 134 244 function delete_service($port_name) { 135 245 136 $sql = Database::getInstance(); 137 138 if (!is_basicString($port_name)) { 139 set_page_message(tr('ERROR: Only Letters, Numbers, Dash and Underscore are allowed!')); 246 $db_cfg = IspCP_Registry::get('Db_Config'); 247 248 if (!isset($db_cfg->$port_name)) { 249 set_page_message(tr('ERROR: Unknown service port name!')); 250 140 251 return; 141 252 } 142 253 143 $query = " 144 SELECT 145 * 146 FROM 147 `config` 148 WHERE 149 `name` = ? 150 "; 151 152 $rs = exec_query($sql, $query, array($port_name)); 153 154 $value = (count(explode(";", $rs->fields['value'])) < 6) 155 ? $rs->fields['value'].';' 156 : $rs->fields['value']; 157 list($port, $protocol, $name, $status, $custom, $ip) = explode(";", $value); 158 159 if ($custom == 1) { 160 $query = " 161 DELETE FROM 162 `config` 163 WHERE 164 `name` = ? 165 "; 166 167 $rs = exec_query($sql, $query, array($port_name)); 168 write_log(get_session('user_logged') . ": remove service port $port_name!"); 254 $values = (count(explode(';', $db_cfg->$port_name)) < 6) 255 ? $db_cfg->$port_name . ';' : $db_cfg->$port_name; 256 257 list(,,,,$custom,) = explode(';', $values); 258 259 if($custom == 1) { 260 // Remove the service from the database 261 // see the {@link IspCP_ConfigHandler_Db} adapter class to learn how 262 // it work 263 unset($db_cfg->$port_name); 264 265 write_log( 266 get_session('user_logged') . ": Removed service port $port_name!" 267 ); 169 268 170 269 set_page_message('Service port was removed!'); 171 270 } else { 172 set_page_message('ERROR: You are not allowed to remove this port entry!'); 173 } 174 271 set_page_message( 272 'ERROR: You are not allowed to remove this port entry!' 273 ); 274 } 275 } 276 277 278 /******************************************************************************* 279 * Main program 280 */ 281 282 /** 283 * Dispatches the request 284 */ 285 286 // Adds a service port or updates one or more services ports 287 if (isset($_POST['uaction']) && $_POST['uaction'] == 'apply') { 288 add_update_services(); 289 290 echo '<pre>'; 175 291 user_goto('settings_ports.php'); 292 293 // Deletes a service port 294 } elseif(isset($_GET['delete'])) { 295 296 delete_service($_GET['delete']); 297 user_goto('settings_ports.php'); 298 299 // Show all services ports 300 } else { 301 302 $cfg = IspCP_Registry::get('Config'); 303 304 $tpl = new pTemplate(); 305 $tpl->define_dynamic( 306 'page', $cfg->ADMIN_TEMPLATE_PATH . '/settings_ports.tpl' 307 ); 308 $tpl->define_dynamic('service_ports', 'page'); 309 $tpl->define_dynamic('port_delete_link', 'service_ports'); 310 $tpl->define_dynamic('port_delete_show', 'service_ports'); 311 312 $tpl->assign( 313 array( 314 'TR_ADMIN_SETTINGS_PAGE_TITLE' => tr('ispCP - Admin/Settings'), 315 'THEME_COLOR_PATH' => "../themes/{$cfg->USER_INITIAL_THEME}", 316 'THEME_CHARSET' => tr('encoding'), 317 'ISP_LOGO' => get_logo(get_session('user_id')) 318 ) 319 ); 320 321 gen_admin_mainmenu( 322 $tpl, $cfg->ADMIN_TEMPLATE_PATH . '/main_menu_settings.tpl' 323 ); 324 gen_admin_menu($tpl, $cfg->ADMIN_TEMPLATE_PATH . '/menu_settings.tpl'); 325 326 show_services($tpl); 327 328 $tpl->assign( 329 array( 330 'TR_ACTION' => tr('Action'), 331 'TR_UDP' => tr('udp'), 332 'TR_TCP' => tr('tcp'), 333 'TR_ENABLED' => tr('Yes'), 334 'TR_DISABLED' => tr('No'), 335 'TR_APPLY_CHANGES' => tr('Apply changes'), 336 'TR_SERVERPORTS' => tr('Server ports'), 337 'TR_SERVICES' => tr('Services'), 338 'TR_SERVICE' => tr('Service'), 339 'TR_IP' => tr('IP'), 340 'TR_PORT' => tr('Port'), 341 'TR_PROTOCOL' => tr('Protocol'), 342 'TR_SHOW' => tr('Show'), 343 'TR_ACTION' => tr('Action'), 344 'TR_DELETE' => tr('Delete'), 345 'TR_ADD' => tr('Add'), 346 'TR_MESSAGE_DELETE' => 347 tr('Are you sure you want to delete %s?', true, '%s') 348 ) 349 ); 350 351 gen_page_message($tpl); 352 353 $tpl->parse('PAGE', 'page'); 354 $tpl->prnt(); 176 355 } 177 178 function show_services(&$tpl, &$sql) {179 180 $cfg = IspCP_Registry::get('Config');181 182 $query = "183 SELECT184 *185 FROM186 `config`187 WHERE188 `name` LIKE 'PORT_%'189 ORDER BY190 `name` ASC191 ";192 193 $rs = exec_query($sql, $query, array());194 195 $row = 1;196 197 if ($rs->RecordCount() == 0) {198 $tpl->assign('SERVICE_PORTS', '');199 200 set_page_message(tr('You have no custom service ports defined.'));201 } else {202 while (!$rs->EOF) {203 $tpl->assign('CLASS', ($row++ % 2 == 0) ? 'content' : 'content2');204 205 $value = (count(explode(";", $rs->fields['value'])) < 6)206 ? $rs->fields['value'].';'207 : $rs->fields['value'];208 list($port, $protocol, $name, $status, $custom, $ip) = explode(";", $value);209 210 $selected_udp = $protocol == 'udp' ? $cfg->HTML_SELECTED : '';211 $selected_tcp = $protocol == 'udp' ? '' : $cfg->HTML_SELECTED;212 213 $selected_on = $status == '1' ? $cfg->HTML_SELECTED : '';214 $selected_off = $status == '1' ? '' : $cfg->HTML_SELECTED;215 216 if ($custom == 0) {217 $tpl->assign(array('SERVICE' => tohtml($name) . '<input name="name[]" type="hidden" id="name' . $row . '" value="' . tohtml($name) . '" />'));218 $tpl->assign(219 array(220 'PORT_READONLY' => $cfg->HTML_READONLY,221 'PROTOCOL_READONLY' => $cfg->HTML_DISABLED,222 'TR_DELETE' => '-',223 'PORT_DELETE_LINK' => '',224 'NUM' => $row225 )226 );227 $tpl->parse('PORT_DELETE_SHOW', '');228 } else {229 $tpl->assign(array('SERVICE' => '<input name="name[]" type="text" id="name' . $row . '" value="' . tohtml($name) . '" class="textinput" maxlength="25" />'));230 $tpl->assign(231 array(232 'NAME' => tohtml($name),233 'PORT_READONLY' => '',234 'PROTOCOL_READONLY' => '',235 'TR_DELETE' => tr('Delete'),236 'URL_DELETE' => 'settings_ports.php?delete=' . $rs->fields['name'],237 'PORT_DELETE_SHOW' => '',238 'NUM' => $row239 )240 );241 $tpl->parse('PORT_DELETE_LINK', 'port_delete_link');242 }243 244 $tpl->assign(245 array(246 'CUSTOM' => tohtml($custom),247 'VAR_NAME' => tohtml($rs->fields['name']),248 'IP' => (($ip == '127.0.0.1') ? 'localhost' : (empty($ip) ? $cfg->BASE_SERVER_IP : tohtml($ip))),249 'PORT' => $port,250 'SELECTED_UDP' => $selected_udp,251 'SELECTED_TCP' => $selected_tcp,252 'SELECTED_ON' => $selected_on,253 'SELECTED_OFF' => $selected_off,254 )255 );256 257 $tpl->parse('SERVICE_PORTS', '.service_ports');258 259 $rs->MoveNext();260 } // end while261 } // end else262 }263 // Fetch delete request264 if (isset($_GET['delete'])) {265 delete_service($_GET['delete']);266 }267 268 /**269 * static page messages.270 */271 272 update_services($sql);273 274 gen_admin_mainmenu($tpl, $cfg->ADMIN_TEMPLATE_PATH . '/main_menu_settings.tpl');275 gen_admin_menu($tpl, $cfg->ADMIN_TEMPLATE_PATH . '/menu_settings.tpl');276 277 show_services($tpl, $sql);278 279 $tpl->assign(280 array(281 'TR_ACTION' => tr('Action'),282 'TR_UDP' => tr('udp'),283 'TR_TCP' => tr('tcp'),284 'TR_ENABLED' => tr('Yes'),285 'TR_DISABLED' => tr('No'),286 'TR_APPLY_CHANGES' => tr('Apply changes'),287 'TR_SERVERPORTS' => tr('Server ports'),288 'TR_SERVICES' => tr('Services'),289 'TR_SERVICE' => tr('Service'),290 'TR_IP' => tr('IP'),291 'TR_PORT' => tr('Port'),292 'TR_PROTOCOL' => tr('Protocol'),293 'TR_SHOW' => tr('Show'),294 'TR_ACTION' => tr('Action'),295 'TR_DELETE' => tr('Delete'),296 'TR_ADD' => tr('Add'),297 'TR_MESSAGE_DELETE' => tr('Are you sure you want to delete %s?', true, '%s')298 )299 );300 301 gen_page_message($tpl);302 303 $tpl->parse('PAGE', 'page');304 $tpl->prnt();305 356 306 357 if ($cfg->DUMP_GUI_DEBUG) {
Note: See TracChangeset
for help on using the changeset viewer.
