From f8a6a47e31c8321835dab8a8aa0e6b6e4fa2a26b Mon Sep 17 00:00:00 2001 From: Johannes Feichtner Date: Sun, 19 Jun 2022 15:17:04 +0200 Subject: [PATCH] code refactoring, no functional changes --- ispconfig3_account/ispconfig3_account.php | 2 +- .../ispconfig3_autoselect.php | 9 +- ispconfig3_filter/ispconfig3_filter.php | 6 +- ispconfig3_spam/ispconfig3_spam.php | 134 +++++++++--------- 4 files changed, 75 insertions(+), 76 deletions(-) diff --git a/ispconfig3_account/ispconfig3_account.php b/ispconfig3_account/ispconfig3_account.php index 84f70d9..252bf36 100644 --- a/ispconfig3_account/ispconfig3_account.php +++ b/ispconfig3_account/ispconfig3_account.php @@ -105,7 +105,7 @@ function template_object_identityform($args) $email_pattern = '//U'; preg_match($email_pattern, $args['content'], $test); - $email = isset($test[1]) ? $test[1] : ''; + $email = $test[1] ?? ''; $args['content'] = preg_replace($email_pattern, $emails->show($email), $args['content']); return $args; diff --git a/ispconfig3_autoselect/ispconfig3_autoselect.php b/ispconfig3_autoselect/ispconfig3_autoselect.php index de4bc9b..a382e96 100644 --- a/ispconfig3_autoselect/ispconfig3_autoselect.php +++ b/ispconfig3_autoselect/ispconfig3_autoselect.php @@ -35,14 +35,16 @@ function load_con_config() { $config = $this->api->dir . 'ispconfig3_account/config/config.inc.php'; if (file_exists($config)) { - if (!$this->rcmail->config->load_from_file($config)) + if (!$this->rcmail->config->load_from_file($config)) { rcube::raise_error(['code' => 527, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Failed to load config from $config"], true, false); + } } else if (file_exists($config . ".dist")) { - if (!$this->rcmail->config->load_from_file($config . '.dist')) + if (!$this->rcmail->config->load_from_file($config . '.dist')) { rcube::raise_error(['code' => 527, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Failed to load config from $config"], true, false); + } } } @@ -55,8 +57,9 @@ function template_object_loginform($args) function authenticate($args) { - if (isset($_POST['_user'], $_POST['_pass'])) + if (isset($_POST['_user'], $_POST['_pass'])) { $args['host'] = $this->getHost(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST)); + } return $args; } diff --git a/ispconfig3_filter/ispconfig3_filter.php b/ispconfig3_filter/ispconfig3_filter.php index a4313a7..510c3e4 100644 --- a/ispconfig3_filter/ispconfig3_filter.php +++ b/ispconfig3_filter/ispconfig3_filter.php @@ -314,7 +314,7 @@ function gen_table($attrib) } $rule_table->set_row_attribs($row_attribs); - $this->_rule_row($rule_table, $filter_value['rulename'], $filter_value['active'], $filter_value['filter_id'], $attrib); + $this->_rule_row($rule_table, $filter_value['rulename'], $filter_value['active'], $filter_value['filter_id']); } if (empty($filter)) { @@ -334,7 +334,7 @@ function gen_table($attrib) return $out; } - private function _rule_row($rule_table, $name, $active, $id, $attrib) + private function _rule_row($rule_table, $name, $active, $id) { $rule_table->add(['class' => 'rule', 'onclick' => 'filter_edit(' . $id . ');'], $name); @@ -359,7 +359,5 @@ private function _rule_row($rule_table, $name, $active, $id, $attrib) 'title' => 'delete' ]); $rule_table->add(['class' => 'control'], $del_button); - - return $rule_table; } } diff --git a/ispconfig3_spam/ispconfig3_spam.php b/ispconfig3_spam/ispconfig3_spam.php index 5385eed..1fcd8ea 100644 --- a/ispconfig3_spam/ispconfig3_spam.php +++ b/ispconfig3_spam/ispconfig3_spam.php @@ -1,4 +1,5 @@ [ 'policy_tag' => 'spam_tag_level', 'policy_tag2' => 'spam_tag2_level', @@ -26,29 +27,25 @@ function init() $this->add_texts('localization/'); $this->require_plugin('ispconfig3_account'); - $this->register_action('plugin.ispconfig3_spam', array($this, 'init_html')); - $this->register_action('plugin.ispconfig3_spam.save', array($this, 'save')); + $this->register_action('plugin.ispconfig3_spam', [$this, 'init_html']); + $this->register_action('plugin.ispconfig3_spam.save', [$this, 'save']); if (strpos($this->rcmail->action, 'plugin.ispconfig3_spam') === 0) { - $this->api->output->add_handler('spam_form', array($this, 'gen_form')); - $this->api->output->add_handler('sectionname_spam', array($this, 'prefs_section_name')); - $this->api->output->add_handler('spam_table', array($this, 'gen_table')); + $this->api->output->add_handler('spam_form', [$this, 'gen_form']); + $this->api->output->add_handler('sectionname_spam', [$this, 'prefs_section_name']); + $this->api->output->add_handler('spam_table', [$this, 'gen_table']); $this->include_script('spam.js'); $this->include_stylesheet($this->local_skin_path() . '/spam.css'); - $this->soap = new SoapClient(null, array( + $this->soap = new SoapClient(null, [ 'location' => $this->rcmail->config->get('soap_url') . 'index.php', 'uri' => $this->rcmail->config->get('soap_url'), $this->rcmail->config->get('soap_validate_cert') ?: - 'stream_context' => stream_context_create( - array('ssl' => array( - 'verify_peer' => false, - 'verify_peer_name' => false, - 'allow_self_signed' => true - ) - )) - )); + 'stream_context' => stream_context_create(['ssl' => [ + 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true + ]]) + ]); } } @@ -67,38 +64,36 @@ function save() { $policy_id = rcube_utils::get_input_value('_spampolicy_name', rcube_utils::INPUT_POST); $move_junk = rcube_utils::get_input_value('_spammove', rcube_utils::INPUT_POST); - - if (!$move_junk) - $move_junk = 'n'; - else - $move_junk = 'y'; + $move_junk = (!$move_junk) ? 'n': 'y'; try { $session_id = $this->soap->login($this->rcmail->config->get('remote_soap_user'), $this->rcmail->config->get('remote_soap_pass')); - $mail_user = $this->soap->mail_user_get($session_id, array('login' => $this->rcmail->user->data['username'])); + $mail_user = $this->soap->mail_user_get($session_id, ['login' => $this->rcmail->user->data['username']]); // Alternatively also search the email field, this can differ from the login field for legacy reasons. if (empty($mail_user)) { - $mail_user = $this->soap->mail_user_get($session_id, array('email' => $this->rcmail->user->data['username'])); + $mail_user = $this->soap->mail_user_get($session_id, ['email' => $this->rcmail->user->data['username']]); } - $spam_user = $this->soap->mail_spamfilter_user_get($session_id, array('email' => $mail_user[0]['email'])); + $spam_user = $this->soap->mail_spamfilter_user_get($session_id, ['email' => $mail_user[0]['email']]); $uid = $this->soap->client_get_id($session_id, $mail_user[0]['sys_userid']); if ($spam_user[0]['id'] == '') { - $params = array('server_id' => $mail_user[0]['server_id'], - 'priority' => '5', - 'policy_id' => $policy_id, - 'email' => $mail_user[0]['email'], - 'fullname' => $mail_user[0]['email'], - 'local' => 'Y'); - - $add = $this->soap->mail_spamfilter_user_add($session_id, $uid, $params); + $params = [ + 'server_id' => $mail_user[0]['server_id'], + 'priority' => '5', + 'policy_id' => $policy_id, + 'email' => $mail_user[0]['email'], + 'fullname' => $mail_user[0]['email'], + 'local' => 'Y' + ]; + + $this->soap->mail_spamfilter_user_add($session_id, $uid, $params); } else { $params = $spam_user[0]; $params['policy_id'] = $policy_id; - $update = $this->soap->mail_spamfilter_user_update($session_id, $uid, $spam_user[0]['id'], $params); + $this->soap->mail_spamfilter_user_update($session_id, $uid, $spam_user[0]['id'], $params); } $params = $mail_user[0]; @@ -106,17 +101,21 @@ function save() $ispconfig_version = $this->soap->server_get_app_version($session_id); if (version_compare($ispconfig_version['ispc_app_version'], '3.1dev', '<')) { - $startdate = array('year' => substr($params['autoresponder_start_date'], 0, 4), + $startdate = [ + 'year' => substr($params['autoresponder_start_date'], 0, 4), 'month' => substr($params['autoresponder_start_date'], 5, 2), 'day' => substr($params['autoresponder_start_date'], 8, 2), 'hour' => substr($params['autoresponder_start_date'], 11, 2), - 'minute' => substr($params['autoresponder_start_date'], 14, 2)); + 'minute' => substr($params['autoresponder_start_date'], 14, 2) + ]; - $enddate = array('year' => substr($params['autoresponder_end_date'], 0, 4), + $enddate = [ + 'year' => substr($params['autoresponder_end_date'], 0, 4), 'month' => substr($params['autoresponder_end_date'], 5, 2), 'day' => substr($params['autoresponder_end_date'], 8, 2), 'hour' => substr($params['autoresponder_end_date'], 11, 2), - 'minute' => substr($params['autoresponder_end_date'], 14, 2)); + 'minute' => substr($params['autoresponder_end_date'], 14, 2) + ]; $params['autoresponder_end_date'] = $enddate; $params['autoresponder_start_date'] = $startdate; @@ -124,7 +123,7 @@ function save() $params['move_junk'] = $move_junk; - $update = $this->soap->mail_user_update($session_id, $uid, $mail_user[0]['mailuser_id'], $params); + $this->soap->mail_user_update($session_id, $uid, $mail_user[0]['mailuser_id'], $params); $this->soap->logout($session_id); $this->rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation'); @@ -139,59 +138,60 @@ function save() function gen_form($attrib) { - $policy_name = array(); - $policy_id = array(); + $policy_name = []; + $policy_id = []; $enabled = 0; $form_id = $attrib['id'] ?: 'form'; - $out = $this->rcmail->output->request_form(array( + $out = $this->rcmail->output->request_form([ 'id' => $form_id, 'name' => $form_id, 'method' => 'post', 'task' => 'settings', 'action' => 'plugin.ispconfig3_spam.save', 'noclose' => true - ) + $attrib); + ] + $attrib); $out .= '
' . $this->gettext('acc_spam') . '' . "\n"; try { $session_id = $this->soap->login($this->rcmail->config->get('remote_soap_user'), $this->rcmail->config->get('remote_soap_pass')); - $mail_user = $this->soap->mail_user_get($session_id, array('login' => $this->rcmail->user->data['username'])); + $mail_user = $this->soap->mail_user_get($session_id, ['login' => $this->rcmail->user->data['username']]); // Alternatively also search the email field, this can differ from the login field for legacy reasons. if (empty($mail_user)) { - $mail_user = $this->soap->mail_user_get($session_id, array('email' => $this->rcmail->user->data['username'])); + $mail_user = $this->soap->mail_user_get($session_id, ['email' => $this->rcmail->user->data['username']]); } - $spam_user = $this->soap->mail_spamfilter_user_get($session_id, array('email' => $mail_user[0]['email'])); - $policies = $this->soap->mail_policy_get($session_id, array()); - $policy_sel = $this->soap->mail_policy_get($session_id, array('id' => $spam_user[0]['policy_id'])); + $spam_user = $this->soap->mail_spamfilter_user_get($session_id, ['email' => $mail_user[0]['email']]); + $policies = $this->soap->mail_policy_get($session_id, []); + $policy_sel = $this->soap->mail_policy_get($session_id, ['id' => $spam_user[0]['policy_id']]); $this->soap->logout($session_id); - foreach ($policies as $policy) { + foreach ((array) $policies as $policy) { $policy_name[] = $policy['policy_name']; $policy_id[] = $policy['id']; } $enabled = $mail_user[0]['move_junk']; - if ($enabled == 'y') + if ($enabled == 'y') { $enabled = 1; + } } catch (SoapFault $e) { $error = $this->rc->text_exists($e->getMessage(), $this->ID) ? $this->gettext($e->getMessage()) : $e->getMessage(); $this->rcmail->output->command('display_message', 'Soap Error: ' . $error, 'error'); } - $table = new html_table(array('cols' => 2, 'class' => 'propform')); + $table = new html_table(['cols' => 2, 'class' => 'propform']); $field_id = 'spampolicy_name'; - $input_spampolicy_name = new html_select(array('name' => '_' . $field_id, 'id' => $field_id)); + $input_spampolicy_name = new html_select(['name' => '_' . $field_id, 'id' => $field_id]); $input_spampolicy_name->add($policy_name, $policy_id); $table->add('title', html::label($field_id, rcube::Q($this->gettext('policy_name')))); - $table->add('', $input_spampolicy_name->show($policy_sel[0]['policy_name'])); + $table->add('', $input_spampolicy_name->show($policy_sel[0]['policy_name'] ?? '')); $field_id = 'spammove'; - $input_spammove = new html_checkbox(array('name' => '_' . $field_id, 'id' => $field_id, 'value' => '1')); + $input_spammove = new html_checkbox(['name' => '_' . $field_id, 'id' => $field_id, 'value' => '1']); $table->add('title', html::label($field_id, rcube::Q($this->gettext('spammove')))); $table->add('', $input_spammove->show($enabled)); $out .= $table->show(); @@ -206,18 +206,18 @@ function gen_table($attrib) $out = '
' . $this->gettext('policy_entries') . '' . "\n"; $spam_table = new html_table([ 'id' => 'spam-table', 'class' => 'records-table', 'cellspacing' => '0', 'cols' => 4 ]); - $spam_table->add_header(array('width' => '220px'), $this->gettext('policy_name')); + $spam_table->add_header(['width' => '220px'], $this->gettext('policy_name')); try { $session_id = $this->soap->login($this->rcmail->config->get('remote_soap_user'), $this->rcmail->config->get('remote_soap_pass')); - $mail_user = $this->soap->mail_user_get($session_id, array('login' => $this->rcmail->user->data['username'])); + $mail_user = $this->soap->mail_user_get($session_id, ['login' => $this->rcmail->user->data['username']]); // Alternatively also search the email field, this can differ from the login field for legacy reasons. if (empty($mail_user)) { - $mail_user = $this->soap->mail_user_get($session_id, array('email' => $this->rcmail->user->data['username'])); + $mail_user = $this->soap->mail_user_get($session_id, ['email' => $this->rcmail->user->data['username']]); } - $spam_user = $this->soap->mail_spamfilter_user_get($session_id, array('email' => $mail_user[0]['email'])); - $policies = $this->soap->mail_policy_get($session_id, array()); + $spam_user = $this->soap->mail_spamfilter_user_get($session_id, ['email' => $mail_user[0]['email']]); + $policies = $this->soap->mail_policy_get($session_id, []); $mail_server_config = $this->soap->server_get($session_id, $mail_user[0]['server_id'], 'mail'); $this->soap->logout($session_id); @@ -226,20 +226,20 @@ function gen_table($attrib) $filter = 'rspamd'; } - $policy_titles = array_keys(self::content_filters[$filter]); - $policy_fields = array_values(self::content_filters[$filter]); + $policy_titles = array_keys(self::CONTENT_FILTERS[$filter]); + $policy_fields = array_values(self::CONTENT_FILTERS[$filter]); $spam_table->add_header([ 'class' => 'value', 'width' => '150px' ], $this->gettext($policy_titles[0])); $spam_table->add_header([ 'class' => 'value', 'width' => '150px' ], $this->gettext($policy_titles[1])); $spam_table->add_header([ 'class' => 'value', 'width' => '130px' ], $this->gettext($policy_titles[2])); - foreach ($policies as $policy) { + foreach ((array) $policies as $policy) { if ($policy['id'] == $spam_user[0]['policy_id']) { $spam_table->set_row_attribs([ 'class' => 'selected' ]); } $this->_spam_row($spam_table, $policy['policy_name'], $policy[$policy_fields[0]], - $policy[$policy_fields[1]], $policy[$policy_fields[2]], $attrib); + $policy[$policy_fields[1]], $policy[$policy_fields[2]]); } if (empty($policies)) { @@ -259,13 +259,11 @@ function gen_table($attrib) return $out; } - private function _spam_row($spam_table, $name, $tag, $tag2, $kill, $attrib) + private function _spam_row($spam_table, $name, $tag, $tag2, $kill) { - $spam_table->add(array('class' => 'policy'), $name); - $spam_table->add(array('class' => 'value'), ' ' . $tag); - $spam_table->add(array('class' => 'value'), ' ' . $tag2); - $spam_table->add(array('class' => 'value'), $kill); - - return $spam_table; + $spam_table->add(['class' => 'policy'], $name); + $spam_table->add(['class' => 'value'], ' ' . $tag); + $spam_table->add(['class' => 'value'], ' ' . $tag2); + $spam_table->add(['class' => 'value'], $kill); } }