From 72b262c7060f91897d5f58b4f689e05bba1c59ad Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 5 Sep 2018 22:42:08 +0200 Subject: [PATCH] Added the option to specify the request method (#631) --- CHANGELOG.rst | 1 + src/includes/functions.inc.php | 7 +- src/lang/en_US.lang.php | 4 ++ .../Controller/AbstractServerController.php | 1 + .../Server/Controller/ServerController.php | 8 ++- src/psm/Util/Install/Installer.php | 5 +- src/psm/Util/Server/Updater/StatusUpdater.php | 5 +- .../module/server/server/update.tpl.html | 26 +++++++ static/js/scripts.js | 67 ++++++++++++------- 9 files changed, 93 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 272f724f..64692cb5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,7 @@ Changelog Not yet released ---------------- +* #631: Added option to specify the request method. * #628: Added the option to mark specific HTTP status codes as online. * #640: Removed () after last offline value when the last offline duration is unknown. * #637: Added php extensions to composer required list, spelling fixes in changelog and composer update. diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index 0b2ceba2..a0e3b32d 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -343,9 +343,10 @@ 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|bool $request_method Request method like GET, POST etc. * @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) { $timeout = $timeout == null ? PSM_CURL_TIMEOUT : intval($timeout); $ch = curl_init(); @@ -358,6 +359,10 @@ 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 ($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 12bdc42e..f9f76f83 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,9 @@ $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', '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 07a74a9e..0e6b8fe8 100644 --- a/src/psm/Module/Server/Controller/AbstractServerController.php +++ b/src/psm/Module/Server/Controller/AbstractServerController.php @@ -61,6 +61,7 @@ abstract class AbstractServerController extends AbstractController { `s`.`server_id`, `s`.`ip`, `s`.`port`, + `s`.`request_method`, `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 5998e7a6..193ba20c 100644 --- a/src/psm/Module/Server/Controller/ServerController.php +++ b/src/psm/Module/Server/Controller/ServerController.php @@ -196,6 +196,7 @@ 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_timeout' => $edit_server['timeout'], 'default_value_timeout' => PSM_CURL_TIMEOUT, 'edit_value_pattern' => $edit_server['pattern'], @@ -265,6 +266,7 @@ 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'), 'type' => psm_POST('type', ''), 'pattern' => psm_POST('pattern', ''), 'pattern_online' => in_array($_POST['pattern_online'], array('yes', 'no')) ? $_POST['pattern_online'] : 'yes', @@ -465,8 +467,12 @@ 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_please_select' => psm_get_lang('servers', 'please_select'), '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_none' => psm_get_lang('system', 'none'), + 'label_please_select' => psm_get_lang('servers', 'please_select'), 'label_type' => psm_get_lang('servers', 'type'), 'label_website' => psm_get_lang('servers', 'type_website'), 'label_service' => psm_get_lang('servers', 'type_service'), diff --git a/src/psm/Util/Install/Installer.php b/src/psm/Util/Install/Installer.php index dd81afde..5cfe126a 100644 --- a/src/psm/Util/Install/Installer.php +++ b/src/psm/Util/Install/Installer.php @@ -225,6 +225,7 @@ class Installer { `server_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ip` varchar(500) NOT NULL, `port` int(5) unsigned NOT NULL, + `request_method` varchar(50) unsigned NULL, `label` varchar(255) NOT NULL, `type` enum('ping','service','website') NOT NULL default 'service', `pattern` varchar(255) NOT NULL default '', @@ -550,8 +551,8 @@ class Installer { */ protected function upgrade340() { $queries = array(); - $queries[] = "ALTER TABLE `".PSM_DB_PREFIX."servers` ADD `allow_http_status` VARCHAR(255) NOT NULL DEFAULT '' AFTER j`pattern_online`;"; + $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 `allow_http_status` VARCHAR(255) NOT NULL DEFAULT '' AFTER `pattern_online`;"; $this->execSQL($queries); - } } diff --git a/src/psm/Util/Server/Updater/StatusUpdater.php b/src/psm/Util/Server/Updater/StatusUpdater.php index 045aeed3..8f9dcbb8 100644 --- a/src/psm/Util/Server/Updater/StatusUpdater.php +++ b/src/psm/Util/Server/Updater/StatusUpdater.php @@ -82,7 +82,7 @@ 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', 'allow_http_status', 'header_name', 'header_value', 'status', 'active', 'warning_threshold', + 'server_id', 'ip', 'port', 'request_method', 'label', 'type', 'pattern', 'pattern_online', 'allow_http_status', 'header_name', 'header_value', 'status', 'active', 'warning_threshold', 'warning_threshold_counter', 'timeout', 'website_username', 'website_password', 'last_offline' )); if (empty($this->server)) { @@ -229,7 +229,8 @@ 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->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 55b42ae9..6b4f0a6c 100644 --- a/src/templates/default/module/server/server/update.tpl.html +++ b/src/templates/default/module/server/server/update.tpl.html @@ -59,6 +59,32 @@ +
+ +
+ +
+
+
+ +
+ +
+
diff --git a/static/js/scripts.js b/static/js/scripts.js index 46669303..bcebcabd 100755 --- a/static/js/scripts.js +++ b/static/js/scripts.js @@ -6,11 +6,11 @@ $().ready(function() { } var $modal_id = $this.attr('data-modal-id') || 'main'; var $modal = $('#' + $modal_id + 'Modal'); - if($modal.length) { + if ($modal.length) { $modal.find('.modalOKButton').data('modal-origin', $this); var param = $this.attr('data-modal-param'); - if(param) { + if (param) { var ary = param.split(','); for (var index = 0; index < ary.length && index < 9; ++index) { var value = ary[index]; @@ -56,25 +56,42 @@ $().ready(function() { if (portInput != '') { var findPopularPorts = $('#popularPorts').find('option[value=' + portInput + ']'); - if(findPopularPorts.length) { + if (findPopularPorts.length) { $(findPopularPorts).attr("selected", "selected"); } else { $('#popularPorts').find('option[value=custom]').attr("selected", "selected"); $('.portGroup').slideDown(); } - } + } $('#popularPorts').change(function () { - changePopularPorts($(this).val(), false, $('#type').val()); + changePopular($(this).val(), $('#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()); }); }); @@ -83,34 +100,34 @@ function changeTypeSwitch(typeInput) { case 'service': $('.types').slideUp(); $('.typeService').slideDown(); + changePopular($('#popularPorts').val(), typeInput, true); break; case 'website': $('.types').slideUp(); $('.typeWebsite').slideDown(); + changePopular($('#popularRequestMethods').val(), typeInput, true); break; default: - $('.types').hide(); + $('.types').slideUp(); } } -function changePopularPorts(popularPorts, changeType, typeInput) { - if (changeType === true) { - if (typeInput == 'service') { - if (popularPorts == 'custom') { - $('.portGroup').slideDown(); - } else { - $('.portGroup').hide(); - } - } +function changePopular(inputValue, typeInput, changedType = false) { + if (typeInput == 'website') { + htmlClass = '.requestMethodGroup'; + htmlID = '#requestMethod'; + } else if (typeInput == 'service') { + htmlClass = '.portGroup'; + htmlID = '#port'; + } + + if (inputValue == 'custom') { + $(htmlClass).slideDown(); } else { - if (popularPorts == 'custom') { - $('.portGroup').slideDown(); - } else { - $('#port').val(popularPorts); - $('.portGroup').slideUp(); - } + changedType ? $(htmlClass).hide() : $(htmlClass).slideUp(); + $(htmlID).val(inputValue); } } @@ -173,14 +190,14 @@ function rtrim(str) { function psm_flash_message(message) { var flashmessage = $('#flashmessage'); - if(flashmessage.length){ - if(typeof message != 'undefined') { + if (flashmessage.length) { + if (typeof message != 'undefined') { flashmessage.html(message); } var t = flashmessage.html(); var c = trim(t); var t = c.replace(' ', ''); - if(t){ + if (t) { flashmessage.slideDown(); } }