Skip to content

Commit

Permalink
Merge pull request #855 from henochit/refactor/stmp_imap_server_confi…
Browse files Browse the repository at this point in the history
…guration

Refactor/stmp imap server configuration
  • Loading branch information
kroky authored Mar 22, 2024
2 parents 9594cd2 + a4ab292 commit eb72761
Show file tree
Hide file tree
Showing 26 changed files with 1,613 additions and 602 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ scripts/test.php
*.sublime-workspace
composer.phar
lib/hm3/users/
.env
3 changes: 0 additions & 3 deletions .travis/.env
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,3 @@ WORDPRESS_CLIENT_URI=

RECAPTCHA_SECRET=
RECAPTCHA_SITE_KEY=

#LoginPage
FANCY_LOGIN=false
20 changes: 19 additions & 1 deletion language/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,24 @@
'Warn for unsaved changes' => false,
'We couldn\'t find the attachment you referred to. Please confirm if you attached it or provide the details again.' => false,
'attachment,file,attach,attached,attaching,enclosed,CV,cover letter' => false,
'Connection To Sieve Server Failed' => false,
'Step 1' => false,
'Step 2' => false,
'Authentication' => false,
'Profile name' => false,
'Email' => false,
'Password' => false,
'Mail server configuration' => false,
'Sender account' => false,
'Receiver account' => false,
'Address' => false,
'Enable Sieve' => false,
'Create Profile' => false,
'Sieve server' => false,
'Reply to' => false,
'Set this profile default' => false,
'Finish' => false,
'Add a new server' => false,
'Automatically add outgoing email addresses' => false,
'Trusted Senders' => false,
'Collected Recipients' => false,
Expand Down Expand Up @@ -624,6 +642,6 @@
'Send anyway' => false,
'Send anyway and don\'t warn in the future' => false,
'Are you sure you want to send this message?' => false,
);
);

?>
146 changes: 146 additions & 0 deletions modules/core/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,149 @@ function warn_for_unsaved_changes_callback($val) {
process_site_setting('warn_for_unsaved_changes', $this, 'warn_for_unsaved_changes_callback');
}
}

/**
* @subpackage core/handler
*/
class Hm_Handler_quick_servers_setup extends Hm_Handler_Module {
public $smtp_server_id = null;
public $imap_server_id = null;
public $jmap_server_id = null;

public function process() {
list($success, $form) = $this->process_form(array(
'srv_setup_stepper_profile_name',
'srv_setup_stepper_email',
'srv_setup_stepper_password',
'srv_setup_stepper_provider',
'srv_setup_stepper_is_sender',
'srv_setup_stepper_is_receiver',
'srv_setup_stepper_smtp_address',
'srv_setup_stepper_smtp_port',
'srv_setup_stepper_smtp_tls',
'srv_setup_stepper_imap_address',
'srv_setup_stepper_imap_port',
'srv_setup_stepper_imap_tls',
'srv_setup_stepper_enable_sieve',
'srv_setup_stepper_create_profile',
'srv_setup_stepper_profile_is_default',
'srv_setup_stepper_profile_signature',
'srv_setup_stepper_profile_reply_to',
'srv_setup_stepper_imap_sieve_host',
'srv_setup_stepper_only_jmap',
'srv_setup_stepper_jmap_hide_from_c_page',
'srv_setup_stepper_jmap_address',
));

if ($success) {
// Destructure form array into variables
[
'srv_setup_stepper_profile_name' => $profileName,
'srv_setup_stepper_email' => $email,
'srv_setup_stepper_password' => $password,
'srv_setup_stepper_provider' => $provider,
'srv_setup_stepper_is_sender' => $isSender,
'srv_setup_stepper_is_receiver' => $isReceiver,
'srv_setup_stepper_smtp_address' => $smtpAddress,
'srv_setup_stepper_smtp_port' => $smtpPort,
'srv_setup_stepper_smtp_tls' => $smtpTls,
'srv_setup_stepper_imap_address' => $imapAddress,
'srv_setup_stepper_imap_port' => $imapPort,
'srv_setup_stepper_imap_tls' => $imapTls,
'srv_setup_stepper_enable_sieve' => $enableSieve,
'srv_setup_stepper_create_profile' => $createProfile,
'srv_setup_stepper_profile_is_default' => $profileIsDefault,
'srv_setup_stepper_profile_signature' => $profileSignature,
'srv_setup_stepper_profile_reply_to' => $profileReplyTo,
'srv_setup_stepper_imap_sieve_host' => $imapSieveHost,
'srv_setup_stepper_only_jmap' => $onlyJmap,
'srv_setup_stepper_jmap_hide_from_c_page' => $jmapHideFromCPage,
'srv_setup_stepper_jmap_address' => $jmapAddress,
] = $form;

/*
* When JMAP selected only configure JMAP
*/
if(isset($onlyJmap) && $onlyJmap) {
if (!$this->module_is_supported('jmap')) {
Hm_Msgs::add("ERRJMAP module is not enabled");
return;
}

$this->jmap_server_id = connect_to_imap_server(
$jmapAddress,
$profileName,
$imapPort,
$email,
$password,
$imapTls,
null,
false,
'imap',
$this,
$jmapHideFromCPage
);

Hm_Msgs::add("JMAP Server saved");
$this->out('just_saved_credentials', true);
} else {
/*
* Connect to SMTP server if user wants to send emails
*/
if($isSender){
if (!$this->module_is_supported('smtp')) {
Hm_Msgs::add("ERRSMTP module is not enabled");
return;
}

$this->smtp_server_id = connect_to_smtp_server($smtpAddress, $profileName, $smtpPort, $email, $password, $smtpTls, $this);
if(!isset($this->smtp_server_id)){
return;
}
}

/*
* Connect to IMAP server if user wants to receive emails
*/
if($isReceiver){
if (!$this->module_is_supported('imap')) {
Hm_Msgs::add("ERRIMAP module is not enabled");
return;
}

$this->imap_server_id = connect_to_imap_server(
$imapAddress,
$profileName,
$imapPort,
$email,
$password,
$imapTls,
$imapSieveHost,
$enableSieve,
'imap',
$this
);

if(!isset($this->imap_server_id)) {
if($isSender && isset($this->smtp_server_id)){
delete_smtp_server($this->smtp_server_id, $this);
}
return;
};
}

if($isSender && $isReceiver && $createProfile && isset($this->imap_server_id) && isset($this->smtp_server_id)) {
if (!$this->module_is_supported('profiles')) {
Hm_Msgs::add("ERRProfiles module is not enabled");
return;
}

add_profile($profileName, $profileSignature, $profileReplyTo, $profileIsDefault, $email, $imapAddress, $this->smtp_server_id, $this->imap_server_id, $this);
}

Hm_Msgs::add("Server saved");
$this->out('just_saved_credentials', true);
}
}
}
}
189 changes: 189 additions & 0 deletions modules/core/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2072,3 +2072,192 @@ protected function output() {
'<td><input type="checkbox" '.$checked.' id="warn_for_unsaved_changes" name="warn_for_unsaved_changes" class="form-check-input" value="1" />'.$reset.'</td></tr>';
}
}

class Hm_Output_server_config_stepper extends Hm_Output_Module {
protected function output() {
$accordionTitle = '';
$configuredText = $this->trans('Configured') .' ';
$hasEssentialModuleActivated = false;

$hasImapActivated = in_array('imap', $this->get('router_module_list'), true);
$hasSmtpActivated = in_array('smtp', $this->get('router_module_list'), true);
$hasJmapActivated = in_array('jmap', $this->get('router_module_list'), true);

if($hasImapActivated){
$imap_servers_count = count(array_filter($this->get('imap_servers', array()), function($v) { return !array_key_exists('type', $v) || $v['type'] != 'jmap'; }));
$accordionTitle .= 'IMAP';
$configuredText .= $imap_servers_count .' IMAP';
$hasEssentialModuleActivated = true;
}

if($hasJmapActivated){
$jmap_servers_count = count(array_filter($this->get('imap_servers', array()), function($v) { return array_key_exists('type', $v) && $v['type'] == 'jmap'; }));
if($accordionTitle != ''){
$accordionTitle .= ' - ';
$configuredText .= ' / ';
}
$accordionTitle .= 'JMAP';
$configuredText .= $jmap_servers_count .' JMAP';
$hasEssentialModuleActivated = true;
}

if($hasSmtpActivated){
$smtp_servers_count = count($this->get('smtp_servers', array()));
if($accordionTitle != ''){
$accordionTitle .= ' - ';
$configuredText .= ' / ';
}
$accordionTitle .= 'SMTP';
$configuredText .= $smtp_servers_count .' SMTP';
$hasEssentialModuleActivated = true;
}

$accordionTitle .= ' Servers';

// When essential module is not activated, we don't display the accordion
if(!$hasEssentialModuleActivated) return '';

$serverList = null;

if(class_exists('Nux_Quick_Services')){
$serverList = Nux_Quick_Services::option_list(false, $this);
}

$hideClass = 'd-none';

// Don't hide this section if at least one of the essential module is activated
if($hasImapActivated || $hasSmtpActivated || $hasJmapActivated){
$hideClass = '';
}

$res = '<div class="smtp_imap_server_setup '. $hideClass .'">
<div data-target=".server_config_section" class="server_section border-bottom cursor-pointer px-1 py-3 pe-auto">
<a href="#" class="pe-auto">
<i class="bi bi-envelope-fill me-3"></i>
<b> '.$accordionTitle.'</b>
</a>
<div class="server_count">'.$configuredText.'</div>
</div>
<div class="server_config_section">
<div class="stepper" id="srv_setup_stepper_stepper">
<div class="step-container">
<div id="step_config_1" class="step step_config">
<div class="step_config-title">
<h2>'.$this->trans('Step 1').'</h2>
<span>('.$this->trans('Authentication').')</span>
</div>
<div>
<form class=" me-0" method="POST">
<input type="hidden" name="hm_page_key" value="'.$this->html_safe(Hm_Request_Key::generate()).'" />
<div class="form-floating mb-3">
<input required type="text" id="srv_setup_stepper_profile_name" name="srv_setup_stepper_profile_name" class="txt_fld form-control" value="" placeholder="'.$this->trans('Name').'">
<label class="" for="srv_setup_stepper_profile_name">'.$this->trans('Name').'</label>
<span id="srv_setup_stepper_profile_name-error" class="error-message"></span>
</div>
<div class="form-floating mb-3">
<input required type="text" id="srv_setup_stepper_email" name="srv_setup_stepper_email" class="txt_fld form-control" value="" placeholder="'.$this->trans('Email or Username').'">
<label class="" for="srv_setup_stepper_email">'.$this->trans('Email or Username').'</label>
<span id="srv_setup_stepper_email-error" class="error-message"></span>
</div>
<div class="form-floating mb-3">
<input required type="password" id="srv_setup_stepper_password" name="srv_setup_stepper_password" class="txt_fld form-control" value="" placeholder="'.$this->trans('Password').'">
<label class="" for="srv_setup_stepper_password">'.$this->trans('Password').'</label>
<span id="srv_setup_stepper_password-error" class="error-message"></span>
</div>
</form>
</div>
<div class="step_config-actions">
<button class="btn btn-success px-5" onclick="display_config_step(0)">'.$this->trans('Cancel').'</button>
<button class="btn btn-success px-5" onclick="display_config_step(2)">'.$this->trans('Next').'</button>
</div>
</div>
<div id="step_config_2" class="step step_config">
<div class="step_config-title">
<h2>'.$this->trans('Step 2').'</h2>
<span>('.$this->trans('Mail server configuration').')</span>
</div>
<div>
<form>
<div class="form-floating mb-3">
<select class="form-select" id="srv_setup_stepper_provider" onchange="handleProviderChange(this) label="'.$this->trans('Provider').'">
<option value="">'.$this->trans('Other').'</option>'.$serverList.'
</select>
<label for="srv_setup_stepper_provider">'.$this->trans('Provider').'</label>
</div>';

if($hasSmtpActivated && $hasImapActivated) {
$res .= '
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" onchange="handleSmtpImapCheckboxChange(this)" id="srv_setup_stepper_is_sender" checked>
<label class="form-check-label" for="srv_setup_stepper_is_sender">'.$this->trans('Sender account').'</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" onchange="handleSmtpImapCheckboxChange(this)" id="srv_setup_stepper_is_receiver" checked>
<label class="form-check-label" for="srv_setup_stepper_is_receiver">'.$this->trans('Receiver account').'</label>
</div>
<span id="srv_setup_stepper_serve_type-error" class="error-message"></span>
';
}

$res .= '<div class="step_config-smtp_imap_bloc">';

return $res;
}
}

class Hm_Output_server_config_stepper_end_part extends Hm_Output_Module {
protected function output() {
$res = '</div>';

if(in_array('profiles', $this->get('router_module_list'), true)) {
$res .= '
<div class="form-check form-switch mt-3" id="srv_setup_stepper_profile_checkbox_bloc">
<input class="form-check-input" type="checkbox" role="switch" onchange="handleCreateProfileCheckboxChange(this)" id="srv_setup_stepper_create_profile" checked>
<label class="form-check-label" for="srv_setup_stepper_create_profile">'.$this->trans('Create Profile').'</label>
</div>
<div class="ms-3" id="srv_setup_stepper_profile_bloc">
<div class="form-floating mb-2">
<input required type="text" id="srv_setup_stepper_profile_reply_to" name="srv_setup_stepper_profile_reply_to" class="txt_fld form-control" value="" placeholder="'.$this->trans('Reply to').'">
<label class="" for="srv_setup_stepper_profile_reply_to">'.$this->trans('Reply to').'</label>
</div>
<div class="form-floating mb-2">
<input required type="text" id="srv_setup_stepper_profile_signature" name="srv_setup_stepper_profile_signature" class="txt_fld form-control" value="" placeholder="'.$this->trans('Signature').'">
<label class="" for="srv_setup_stepper_profile_signature">'.$this->trans('Signature').'</label>
</div>
<div class="form-check" id="srv_setup_stepper_profile_checkbox_bloc">
<input class="form-check-input" type="checkbox" role="switch" id="srv_setup_stepper_profile_is_default" checked>
<label class="form-check-label" for="srv_setup_stepper_profile_is_default">'.$this->trans('Set this profile default').'</label>
</div>
</div>
';
}

$res .= '</form>
</div>
<div class="srv_setup_stepper_form_loader hide" id="srv_setup_stepper_form_loader">
<div class="spinner-border text-dark" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
<div class="step_config-actions">
<button class="btn btn-danger px-3" onclick="display_config_step(0)">'.$this->trans('Cancel').'</button>
<button class="btn btn-success px-3" onclick="display_config_step(1)">'.$this->trans('Previous').'</button>
<button class="btn btn-success px-3" onclick="display_config_step(3)">'.$this->trans('Finish').'</button>
</div>
</div>
<div id="step_config_0" class="step_config current_config_step">
<button class="btn btn-success px-5" onclick="display_config_step(1)">+ '.$this->trans('Add a new server').'</button>
</div>
</div>
</div>
<div class="px-5">';

return $res;
}
}

class Hm_Output_server_config_stepper_accordion_end_part extends Hm_Output_Module {
protected function output() {
return '</div></div></div>';
}
}
Loading

0 comments on commit eb72761

Please sign in to comment.