Skip to content

Commit

Permalink
Add symfony/yaml to format accounts_source file && complete integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed May 16, 2024
1 parent ee745a8 commit 6c46733
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 104 deletions.
1 change: 1 addition & 0 deletions language/az.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/et.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/fa.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,4 +679,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/hu.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/id.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/ja.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/nl.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/pt-BR.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/ro.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,5 @@
'IMAP and JMAP Servers' => false,
'Junk' => false,
'Trash' => false,
'Yaml File' => false,
);
1 change: 1 addition & 0 deletions language/zh-Hans.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,4 +648,5 @@
'IMAP and JMAP Servers' => 'IMAP 服务器及 JMAP 服务器',
'Junk' => '垃圾',
'Trash' => '已删除',
'Yaml File' => false,
);
4 changes: 2 additions & 2 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1457,9 +1457,9 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima
$context->user_config->get('enable_sieve_filter_setting', true)) {
try {

include APP_PATH.'modules/sievefilters/hm-sieve.php';
include_once APP_PATH.'modules/sievefilters/hm-sieve.php';
$sieveClientFactory = new Hm_Sieve_Client_Factory();
$client = $sieveClientFactory::init(null, $server);
$client = $sieveClientFactory->init(null, $server);

if (!$client) {
Hm_Msgs::add("ERRFailed to authenticate to the Sieve host");
Expand Down
2 changes: 0 additions & 2 deletions modules/nux/assets/data/server_accounts_sample.csv

This file was deleted.

12 changes: 6 additions & 6 deletions modules/nux/assets/data/server_accounts_sample.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Migadu.com:
jmap:
jmap_server:
jmap_server:
user:
username: test@enterprisedepartment.com
password: 8xiK&z5TsQaJ!t
username: test@email.com
password: secret
imap:
port: 993
tls: true
imap_server: imap.migadu.com
smtp:
support_smtp: true
smtp_server: smtp.migadu.com
smtp_port: 587
smtp_port: 465
smtp_tls: true
sieve:
sieve_host: tls://imap.migadu.com
Expand All @@ -20,8 +20,8 @@ Postale.io:
jmap:
jmap_server:
user:
username: test@notrewiki.com
password: FuMxdfhCLD3vEuP5
username: test@email.com
password: secret
imap:
port: 993
tls: true
Expand Down
180 changes: 90 additions & 90 deletions modules/nux/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,91 +293,87 @@ public function process()
list($success, $form) = $this->process_form(array('accounts_source'));

if ($success) {
// if($form['accounts_source'] == 'yaml') {
// $file = $this->request->files['accounts_csv'];
$file = WEB_ROOT . 'modules/nux/assets/data/server_accounts_sample.yaml';
//
// if(!is_null($file) && $file['type'] == 'text/yaml') {
try {
$servers = Yaml::parseFile($file); //$file['tmp_name']

foreach ($servers as $key => $value) {
if (empty($value['user']['username']) or empty($value['user']['password'])) {
Hm_Msgs::add('ERRUsername and password are required for: ' . $key . ' server');
return;
if ($form['accounts_source'] == 'yaml') {
$file = $this->request->files['accounts_sample'];
if (!is_null($file) && $file['type'] == 'application/x-yaml') {
try {
$servers = Yaml::parseFile($file['tmp_name']);
} catch (\Throwable $th) {
Hm_Msgs::add('ERR' . $th->getMessage());
$this->save_hm_msgs();
Hm_Dispatch::page_redirect('?page=servers');
}
if ($value['jmap'] && !empty($value['jmap']['jmap_server'])) {
if (!$this->module_is_supported('jmap')) {
Hm_Msgs::add("ERRJMAP module is not enabled");
foreach ($servers as $key => $value) {
if (empty($value['user']['username']) or empty($value['user']['password'])) {
Hm_Msgs::add('ERRUsername and password are required for: ' . $key . ' server');
return;
}
connect_to_imap_server(
$value['imap']['imap_server'],
$key,
null,
$value['user']['username'],
$value['user']['password'],
false,
null,
false,
'jmap',
$this,
false
);
Hm_Msgs::add("JMAP Server " . $key . " saved");
}
if ($value['smtp'] && !empty($value['smtp']['smtp_server'])) {
// Check if module is supported
if (!$this->module_is_supported('smtp')) {
Hm_Msgs::add("ERRSMTP module is not enabled");
} else {
//TO DO: check first if the smtp_server server is already configured before connecting. can use the in_server_list function or create a new one specifically for my case
$smtp_server_id = connect_to_smtp_server(
$value['smtp']['smtp_server'],
if ($value['jmap'] && !empty($value['jmap']['jmap_server'])) {
if (!$this->module_is_supported('jmap')) {
Hm_Msgs::add("ERRJMAP module is not enabled");
return;
}
connect_to_imap_server(
$value['imap']['imap_server'],
$key,
$value['smtp']['smtp_port'],
null,
$value['user']['username'],
$value['user']['password'],
$value['smtp']['smtp_tls'],
$this
false,
null,
false,
'jmap',
$this,
false
);
var_dump('smtp_server_id', $smtp_server_id);
die();
Hm_Msgs::add("SMTP Server " . $key . " saved");
Hm_Msgs::add("JMAP Server " . $key . " saved");
}
if ($value['smtp'] && !empty($value['smtp']['smtp_server'])) {
// Check if module is supported
if (!$this->module_is_supported('smtp')) {
Hm_Msgs::add("ERRSMTP module is not enabled");
} else {
//TO DO: check first if the smtp_server server is already configured before connecting. can use the in_server_list function or create a new one specifically for my case
$smtp_server_id = connect_to_smtp_server(
$value['smtp']['smtp_server'],
$key,
$value['smtp']['smtp_port'],
$value['user']['username'],
$value['user']['password'],
$value['smtp']['smtp_tls'],
$this
);
Hm_Msgs::add("SMTP Server " . $key . " saved");
}
}
}

if ($value['imap'] && !empty($value['imap']['imap_server'])) {
// Check if module is supported
if (!$this->module_is_supported('imap')) {
Hm_Msgs::add("ERRIMAP module is not enabled");
return;
if ($value['imap'] && !empty($value['imap']['imap_server'])) {
// Check if module is supported
if (!$this->module_is_supported('imap')) {
Hm_Msgs::add("ERRIMAP module is not enabled");
return;
}
//TO DO: check first if the imap_server server is already configured before connecting can use the in_server_list function or create a new one specifically for my case
$imap_server_id = connect_to_imap_server(
$value['imap']['imap_server'],
$key,
$value['imap']['port'],
$value['user']['username'],
$value['user']['password'],
$value['imap']['tls'],
$value['sieve']['sieve_host'],
$value['sieve']['sieve_port'],
'imap',
$this
);
Hm_Msgs::add("Server " . $key . " saved");
}
//TO DO: check first if the imap_server server is already configured before connecting can use the in_server_list function or create a new one specifically for my case
connect_to_imap_server(
$value['imap']['imap_server'],
$key,
$value['imap']['port'],
$value['user']['username'],
$value['user']['password'],
$value['imap']['tls'],
$value['sieve']['sieve_host'],
$value['sieve']['sieve_port'],
'imap',
$this
);
Hm_Msgs::add("Server " . $key . " saved");
}

$this->save_hm_msgs();
Hm_Dispatch::page_redirect('?page=servers');
}
} catch (\Throwable $th) {
Hm_Msgs::add('ERR' . $th->getMessage());
exit(var_dump($th->getMessage()));
Hm_Dispatch::page_redirect('?page=servers');
}
// }
// }
} else {
// TODO: Add error message and redirect back
}
}
}
Expand Down Expand Up @@ -425,20 +421,20 @@ protected function output()
if ($this->get('single_server_mode')) {
return '';
}
$title = $this->trans('Import from CSV file');
$notice = 'Please ensure your CSV header file follows the format: server_name, username, password, imap_server, imap_port, imap_tls, smtp_server, smtp_port, smtp_tls, sieve_host, sieve_port.';
$csv_sample_path = WEB_ROOT . 'modules/nux/assets/data/server_accounts_sample.csv';
$title = $this->trans('Import from YAML file');
$notice = 'Please ensure your YAML file follows the correct format';
$file_sample_path = WEB_ROOT . 'modules/nux/assets/data/server_accounts_sample.yaml';

return '<div class="quick_add_multiple_section">' .
'<div class="row"><div class="col col-lg-4"><div class="form-floating mb-3">' .
'<form class="quick_add_multiple_server_form" action="?page=add_multiple_servers" method="POST" enctype="multipart/form-data">' .
'<button class="browser_acc_csv mt-2 btn btn-light" title="' . $notice . '"><i class="bi bi-filetype-csv me-2"></i>' . $title . '</button>' .
'<form class="quick_add_multiple_server_form" action="?page=servers" method="POST" enctype="multipart/form-data">' .
'<button class="browser_acc_file mt-2 btn btn-light" title="' . $notice . '"><i class="bi bi-filetype-yml me-2"></i>' . $title . '</button>' .
'<div class="server_form">' .
'<div><a href="' . $csv_sample_path . '">' . $this->trans('download a sample csv file') . '</a></div><br />' .
'<div><a href="' . $file_sample_path . '">' . $this->trans('download a sample yaml file') . '</a></div><br />' .
'<input type="hidden" name="hm_page_key" value="' . $this->html_safe(Hm_Request_Key::generate()) . '" />' .
'<input type="hidden" name="accounts_source" value="yaml" />' .
'<label class="screen_reader" for="accounts_csv">' . $this->trans('Csv File') . '</label>' .
'<input class="form-control" id="accounts_csv" type="file" name="accounts_csv" accept=".csv,.yaml"/> <br />' .
'<label class="screen_reader" for="accounts_sample">' . $this->trans('Yaml File') . '</label>' .
'<input class="form-control" id="accounts_sample" type="file" name="accounts_sample" accept=".yaml"/> <br />' .
'<input class="btn btn-primary add_multiple_server_submit" type="submit" name="import_contact" id="import_contact" value="' . $this->trans('Add') . '" /> <input type="button" class="btn btn-secondary reset_add_multiple_server" value="' .
$this->trans('Cancel') . '" /></div></form></div></div></div></div></div>';
}
Expand Down Expand Up @@ -477,9 +473,11 @@ protected function output()
/**
* @subpackage nux/output
*/
class Hm_Output_nux_dev_news extends Hm_Output_Module {
protected function output() {
$res = '<div class="nux_dev_news mt-3 col-12"><div class="card"><div class="card-body"><div class="card_title"><h4>'.$this->trans('Development Updates').'</h4></div><table>';
class Hm_Output_nux_dev_news extends Hm_Output_Module
{
protected function output()
{
$res = '<div class="nux_dev_news mt-3 col-12"><div class="card"><div class="card-body"><div class="card_title"><h4>' . $this->trans('Development Updates') . '</h4></div><table>';
foreach ($this->get('nux_dev_news', array()) as $vals) {
$res .= sprintf(
'<tr><td><a href="https:/cypht-org/cypht/commit/%s" target="_blank" rel="noopener">%s</a>' .
Expand All @@ -499,11 +497,13 @@ protected function output() {
/**
* @subpackage nux/output
*/
class Hm_Output_nux_help extends Hm_Output_Module {
protected function output() {
return '<div class="nux_help mt-3 col-lg-6 col-md-12 col-sm-12"><div class="card"><div class="card-body"><div class="card_title"><h4>'.$this->trans('Help').'</h4></div>'.
$this->trans('Cypht is a webmail program. You can use it to access your E-mail accounts from any service that offers IMAP, or SMTP access - which most do.').' '.
'</div></div></div>';
class Hm_Output_nux_help extends Hm_Output_Module
{
protected function output()
{
return '<div class="nux_help mt-3 col-lg-6 col-md-12 col-sm-12"><div class="card"><div class="card-body"><div class="card_title"><h4>' . $this->trans('Help') . '</h4></div>' .
$this->trans('Cypht is a webmail program. You can use it to access your E-mail accounts from any service that offers IMAP, or SMTP access - which most do.') . ' ' .
'</div></div></div>';
}
}

Expand Down Expand Up @@ -615,8 +615,8 @@ protected function output()
return '';
}
return '<div class="nux_add_multiple_account"><div data-target=".quick_add_multiple_section" class="server_section border-bottom cursor-pointer px-1 py-3 pe-auto"><a href="#" class="pe-auto">' .
'<i class="bi bi-filetype-csv me-3"></i>' .
'<b>' . $this->trans('Bulk-import accounts using csv template') . '</b></a></div>';
'<i class="bi bi-filetype-yml me-3"></i>' .
'<b>' . $this->trans('Bulk-import accounts using yaml template') . '</b></a></div>';
}
}

Expand Down
4 changes: 1 addition & 3 deletions modules/nux/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@

add_output('message_list', 'nux_message_list_notice', true, 'nux', 'message_list_start', 'before');

setup_base_page('add_multiple_servers', 'core');
add_handler('add_multiple_servers', 'process_import_accouts_servers', true, 'nux','load_user_data', 'after');
add_handler('servers', 'process_import_accouts_servers', true, 'nux','load_smtp_servers_from_config', 'after');
add_output('servers', 'quick_add_multiple_section', true, 'nux', 'server_config_stepper_accordion_end_part', 'after');
add_output('servers', 'quick_add_multiple_dialog', true, 'nux', 'quick_add_multiple_section', 'after');

Expand All @@ -60,7 +59,6 @@
'ajax_nux_service_select',
'ajax_get_nux_service_details',
'ajax_nux_add_service',
'add_multiple_servers',
),
'allowed_get' => array(
'code' => FILTER_DEFAULT,
Expand Down
Loading

0 comments on commit 6c46733

Please sign in to comment.