From 4b2f07a3bedbbbff0605380254d559f1103e59ea Mon Sep 17 00:00:00 2001 From: TimZ99 Date: Fri, 7 Sep 2018 01:27:09 +0200 Subject: [PATCH] Added post field --- src/includes/functions.inc.php | 12 +++- src/lang/en_US.lang.php | 6 ++ .../Controller/AbstractServerController.php | 2 + .../Server/Controller/ServerController.php | 15 +++++ src/psm/Util/Install/Installer.php | 40 ++++++++++-- src/psm/Util/Server/Updater/StatusUpdater.php | 10 ++- .../module/server/server/update.tpl.html | 32 +++++++++ static/js/scripts.js | 65 +++++++++++++------ 8 files changed, 153 insertions(+), 29 deletions(-) diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index 0b2ceba2..fe155ceb 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -343,9 +343,11 @@ function psm_parse_msg($status, $type, $vars) { * @param boolean $add_agent add user agent? * @param string|bool $website_username Username website * @param string|bool $website_password Password website + * @param string|null $request_method Request method like GET, POST etc. + * @param string|null $post_field POST data * @return string cURL result */ -function psm_curl_get($href, $header = false, $body = true, $timeout = null, $add_agent = true, $website_username = false, $website_password = false) { +function psm_curl_get($href, $header = false, $body = true, $timeout = null, $add_agent = true, $website_username = false, $website_password = false, $request_method = null, $post_field = null) { $timeout = $timeout == null ? PSM_CURL_TIMEOUT : intval($timeout); $ch = curl_init(); @@ -358,6 +360,14 @@ function psm_curl_get($href, $header = false, $body = true, $timeout = null, $ad curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_ENCODING, ''); + + if (!empty($request_method)) { + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method); + } + + if (!empty($post_field)) { + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field); + } if ($website_username !== false && $website_password !== false && !empty($website_username) && !empty($website_password)) { curl_setopt($ch, CURLOPT_USERPWD, $website_username.":".$website_password); diff --git a/src/lang/en_US.lang.php b/src/lang/en_US.lang.php index 57eb41b9..ca63b1c2 100644 --- a/src/lang/en_US.lang.php +++ b/src/lang/en_US.lang.php @@ -48,6 +48,7 @@ $sm_lang = array( 'go_back' => 'Go back', 'ok' => 'OK', 'cancel' => 'Cancel', + 'none' => 'None', 'activate' => 'Activate', // date/time format according the strftime php function format parameter http://php.net/manual/function.strftime.php 'short_day_format' => '%B %e', @@ -160,6 +161,11 @@ $sm_lang = array( 'port' => 'Port', 'custom_port' => 'Custom Port', 'popular_ports' => 'Popular Ports', + 'request_method' => 'Request method', + 'custom_request_method' => 'Custom request method', + 'popular_request_methods' => 'Popular request methods', + 'post_field' => 'Post field', + 'post_field_description' => 'The data that will be send using the request method above. Example: param1=val1&param2=val2&...', 'please_select' => 'Please select', 'type' => 'Type', 'type_website' => 'Website', diff --git a/src/psm/Module/Server/Controller/AbstractServerController.php b/src/psm/Module/Server/Controller/AbstractServerController.php index 53145b66..48c8edd4 100644 --- a/src/psm/Module/Server/Controller/AbstractServerController.php +++ b/src/psm/Module/Server/Controller/AbstractServerController.php @@ -61,6 +61,8 @@ abstract class AbstractServerController extends AbstractController { `s`.`server_id`, `s`.`ip`, `s`.`port`, + `s`.`request_method`, + `s`.`post_field`, `s`.`type`, `s`.`label`, `s`.`pattern`, diff --git a/src/psm/Module/Server/Controller/ServerController.php b/src/psm/Module/Server/Controller/ServerController.php index fdeed658..154e2988 100644 --- a/src/psm/Module/Server/Controller/ServerController.php +++ b/src/psm/Module/Server/Controller/ServerController.php @@ -196,6 +196,8 @@ class ServerController extends AbstractServerController { 'edit_value_label' => $edit_server['label'], 'edit_value_ip' => $edit_server['ip'], 'edit_value_port' => $edit_server['port'], + 'edit_value_request_method' => $edit_server['request_method'], + 'edit_value_post_field' => $edit_server['post_field'], 'edit_value_timeout' => $edit_server['timeout'], 'default_value_timeout' => PSM_CURL_TIMEOUT, 'edit_value_pattern' => $edit_server['pattern'], @@ -264,6 +266,8 @@ class ServerController extends AbstractServerController { 'website_username' => psm_POST('website_username'), 'website_password' => $encrypted_password, 'port' => intval(psm_POST('port', 0)), + 'request_method' => empty(psm_POST('request_method')) ? null : psm_POST('request_method'), + 'post_field' => empty(psm_POST('post_field')) ? null : psm_POST('post_field'), 'type' => psm_POST('type', ''), 'pattern' => psm_POST('pattern', ''), 'pattern_online' => in_array($_POST['pattern_online'], array('yes', 'no')) ? $_POST['pattern_online'] : 'yes', @@ -282,6 +286,10 @@ class ServerController extends AbstractServerController { $clean['ip'] = 'http://'.$clean['ip']; } + if($clean['request_method'] == null) { + $clean['post_field'] = null; + } + // validate the lot $server_validator = new \psm\Util\Server\ServerValidator($this->db); @@ -463,6 +471,13 @@ class ServerController extends AbstractServerController { 'label_fieldset_permissions' => psm_get_lang('servers', 'fieldset_permissions'), 'label_port' => psm_get_lang('servers', 'port'), 'label_custom_port' => psm_get_lang('servers', 'custom_port'), + 'label_popular_ports' => psm_get_lang('servers', 'popular_ports'), + 'label_request_method' => psm_get_lang('servers', 'request_method'), + 'label_custom_request_method' => psm_get_lang('servers', 'custom_request_method'), + 'label_popular_request_methods' => psm_get_lang('servers', 'popular_request_methods'), + 'label_post_field' => psm_get_lang('servers', 'post_field'), + 'label_post_field_description' => psm_get_lang('servers', 'post_field_description'), + 'label_none' => psm_get_lang('system', 'none'), 'label_please_select' => psm_get_lang('servers', 'please_select'), 'label_popular_ports' => psm_get_lang('servers', 'popular_ports'), 'label_type' => psm_get_lang('servers', 'type'), diff --git a/src/psm/Util/Install/Installer.php b/src/psm/Util/Install/Installer.php index cfc328dc..84fe4ca7 100644 --- a/src/psm/Util/Install/Installer.php +++ b/src/psm/Util/Install/Installer.php @@ -196,7 +196,7 @@ class Installer { `email` varchar(255) NOT NULL, PRIMARY KEY (`user_id`), UNIQUE KEY `unique_username` (`user_name`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX.'users_preferences' => "CREATE TABLE IF NOT EXISTS `".PSM_DB_PREFIX."users_preferences` ( `user_id` int(11) unsigned NOT NULL, `key` varchar(255) NOT NULL, @@ -207,7 +207,7 @@ class Installer { `user_id` INT( 11 ) UNSIGNED NOT NULL , `server_id` INT( 11 ) UNSIGNED NOT NULL , PRIMARY KEY ( `user_id` , `server_id` ) - ) ENGINE = MYISAM ;", + ) ENGINE = MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX.'log' => "CREATE TABLE `".PSM_DB_PREFIX."log` ( `log_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `server_id` int(11) unsigned NOT NULL, @@ -215,7 +215,7 @@ class Installer { `message` varchar(255) NOT NULL, `datetime` timestamp NOT NULL default CURRENT_TIMESTAMP, PRIMARY KEY (`log_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX.'log_users' => "CREATE TABLE `".PSM_DB_PREFIX."log_users` ( `log_id` int(11) UNSIGNED NOT NULL , `user_id` int(11) UNSIGNED NOT NULL , @@ -224,11 +224,15 @@ class Installer { PSM_DB_PREFIX.'servers' => "CREATE TABLE `".PSM_DB_PREFIX."servers` ( `server_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ip` varchar(500) NOT NULL, - `port` int(5) unsigned NOT NULL, + `port` int(5) NOT NULL, + `request_method` varchar(50) NULL, `label` varchar(255) NOT NULL, `type` enum('ping','service','website') NOT NULL default 'service', `pattern` varchar(255) NOT NULL, `pattern_online` enum('yes','no') NOT NULL default 'yes', + `post_field` varchar(255) NOT NULL default '', + `redirect_check` enum('ok','bad') NOT NULL default 'bad', + `allow_http_status` varchar(255) NOT NULL default '', `header_name` varchar(255) NOT NULL default '', `header_value` varchar(255) NOT NULL default '', `status` enum('on','off') NOT NULL default 'on', @@ -249,7 +253,7 @@ class Installer { `website_username` varchar(255) DEFAULT NULL, `website_password` varchar(255) DEFAULT NULL, PRIMARY KEY (`server_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX.'servers_uptime' => "CREATE TABLE IF NOT EXISTS `".PSM_DB_PREFIX."servers_uptime` ( `servers_uptime_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `server_id` int(11) unsigned NOT NULL, @@ -258,7 +262,7 @@ class Installer { `latency` float(9,7) DEFAULT NULL, PRIMARY KEY (`servers_uptime_id`), KEY `server_id` (`server_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX.'servers_history' => "CREATE TABLE IF NOT EXISTS `".PSM_DB_PREFIX."servers_history` ( `servers_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `server_id` int(11) unsigned NOT NULL, @@ -539,6 +543,28 @@ class Installer { if (psm_get_conf('sms_gateway') == 'mollie') { psm_update_conf('sms_gateway', 'messagebird'); } - + } + + /** + * Upgrade for v3.4.0 release + */ + protected function upgrade340() { + $queries = array(); + /** + * Redirect_check is first set to default ok. + * If you have a lot of server that are redirecting, + * this will make sure you're servers stay online. + */ + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD COLUMN `allow_http_status` VARCHAR(255) NOT NULL DEFAULT '' AFTER `pattern_online`;"; + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD `redirect_check` ENUM( 'ok','bad' ) NOT NULL DEFAULT 'ok' AFTER `allow_http_status`;"; + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` CHANGE `redirect_check` `redirect_check` ENUM('ok','bad') NOT NULL DEFAULT 'bad';"; + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD COLUMN `last_error` VARCHAR(255) NULL AFTER `website_password`;"; + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD COLUMN `last_error_output` TEXT NULL AFTER `last_error`;"; + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD COLUMN `last_output` TEXT NULL AFTER `last_error_output`;"; + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD COLUMN `request_method` varchar(50) NULL AFTER `port`;"; + $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD COLUMN `post_field` varchar(255) NOT NULL DEFAULT '' AFTER `pattern_online`;"; + $queries[] = "INSERT INTO `".PSM_DB_PREFIX."config` (`key`, `value`) VALUES ('combine_notifications', '1');"; + $this->execSQL($queries); + $this->log('Combined notifications enabled. Check out the config page for more info.'); } } diff --git a/src/psm/Util/Server/Updater/StatusUpdater.php b/src/psm/Util/Server/Updater/StatusUpdater.php index 3879c57a..95b4a6ac 100644 --- a/src/psm/Util/Server/Updater/StatusUpdater.php +++ b/src/psm/Util/Server/Updater/StatusUpdater.php @@ -69,6 +69,9 @@ class StatusUpdater { * * Please note: if the server is down but has not met the warning threshold, this will return true * to avoid any "we are down" events. + * + * @todo Get last_output when there is a HTTP 50x error. + * * @param int $server_id * @param int $max_runs how many times should the script recheck the server if unavailable. default is 2 * @return boolean TRUE if server is up, FALSE otherwise @@ -82,7 +85,8 @@ class StatusUpdater { $this->server = $this->db->selectRow(PSM_DB_PREFIX.'servers', array( 'server_id' => $server_id, ), array( - 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'pattern_online', 'header_name', 'header_value', 'status', 'active', 'warning_threshold', + 'server_id', 'ip', 'port', 'request_method', 'label', 'type', 'pattern', 'pattern_online', 'post_field', + 'allow_http_status', 'redirect_check', 'header_name', 'header_value', 'status', 'active', 'warning_threshold', 'warning_threshold_counter', 'timeout', 'website_username', 'website_password', 'last_offline' )); if (empty($this->server)) { @@ -229,7 +233,9 @@ class StatusUpdater { $this->server['timeout'], true, $this->server['website_username'], - psm_password_decrypt($this->server['server_id'].psm_get_conf('password_encrypt_key'), $this->server['website_password']) + psm_password_decrypt($this->server['server_id'].psm_get_conf('password_encrypt_key'), $this->server['website_password']), + $this->server['request_method'], + $this->server['post_field'] ); $this->rtime = (microtime(true) - $starttime); diff --git a/src/templates/default/module/server/server/update.tpl.html b/src/templates/default/module/server/server/update.tpl.html index dee8558d..0dd41e11 100644 --- a/src/templates/default/module/server/server/update.tpl.html +++ b/src/templates/default/module/server/server/update.tpl.html @@ -59,6 +59,38 @@ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
diff --git a/static/js/scripts.js b/static/js/scripts.js index 46669303..7c976e5f 100755 --- a/static/js/scripts.js +++ b/static/js/scripts.js @@ -68,13 +68,32 @@ $().ready(function() { changePopularPorts($(this).val(), false, $('#type').val()); }); + // popularRequestMethods + // initial + $('.requestMethodGroup').hide(); + var requestMethodInput = $('#requestMethod').val(); + + if (requestMethodInput != '') { + var findPopularRequestMethods = $('#popularRequestMethods').find('option[value=' + requestMethodInput + ']'); + if (findPopularRequestMethods.length) { + $(findPopularRequestMethods).attr("selected", "selected"); + } else { + $('#popularRequestMethods').find('option[value=custom]').attr("selected", "selected"); + $('.requestMethodGroup').slideDown(); + } + } + + $('#popularRequestMethods').change(function () { + changePopular($(this).val(), $('#type').val()); + }); + // server type $('.types').hide(); changeTypeSwitch($('#type').val()); $('#type').change(function () { changeTypeSwitch($('#type').val()); - changePopularPorts($('#popularPorts').val(), true, $('#type').val()); + changePopular($('#popularPorts').val(), true, $('#type').val()); }); }); @@ -95,27 +114,35 @@ function changeTypeSwitch(typeInput) { } } -function changePopularPorts(popularPorts, changeType, typeInput) { - if (changeType === true) { - if (typeInput == 'service') { - if (popularPorts == 'custom') { - $('.portGroup').slideDown(); - } else { - $('.portGroup').hide(); - } - } - } else { - if (popularPorts == 'custom') { - $('.portGroup').slideDown(); - } else { - $('#port').val(popularPorts); - $('.portGroup').slideUp(); - } +function changePopular(inputValue, typeInput, changedType = false) { + if (typeInput === 'website') { + htmlClass = '.requestMethodGroup'; + htmlID = '#requestMethod'; + postClass = '.postGroup'; + } else if (typeInput === 'service') { + htmlClass = '.portGroup'; + htmlID = '#port'; + } + + if (typeInput === 'website' && inputValue === '') { + changedType ? $(postClass).hide() : $(postClass).slideUp(); + } else { + $(postClass).slideDown(); + } + + if (inputValue === 'custom') { + $(htmlClass).slideDown(); + return; } + + changedType ? $(htmlClass).hide() : $(htmlClass).slideUp(); + $(htmlID).val(inputValue); + + } function psm_xhr(mod, params, method, on_complete, options) { - method = (typeof method == 'undefined') ? 'GET' : method; + method = (typeof method === 'undefined') ? 'GET' : method; var xhr_options = { data: params, @@ -174,7 +201,7 @@ function rtrim(str) { function psm_flash_message(message) { var flashmessage = $('#flashmessage'); if(flashmessage.length){ - if(typeof message != 'undefined') { + if(typeof message !== 'undefined') { flashmessage.html(message); } var t = flashmessage.html();