Skip to content

Commit

Permalink
parse_csv_with_headers added to support any csv
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Aug 9, 2024
1 parent 997330a commit 78a6079
Show file tree
Hide file tree
Showing 22 changed files with 405 additions and 367 deletions.
1 change: 1 addition & 0 deletions language/az.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,4 +649,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/et.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/fa.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/hu.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/id.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/ja.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/nl.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/pt-BR.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/ro.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
1 change: 1 addition & 0 deletions language/zh-Hans.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,5 @@
'Pasted text has leading or trailing spaces' => false,
'No tags available yet.' => false,
'Yaml File' => false,
'Please ensure your YAML or CSV file follows the correct format' => false,
);
4 changes: 2 additions & 2 deletions modules/core/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2177,12 +2177,12 @@ protected function output() {
<span id="srv_setup_stepper_profile_name-error" class="invalid-feedback"></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 warn_on_paste" autocomplete="username" value="" placeholder="'.$this->trans('Email or Username').'">
<input required type="text" id="srv_setup_stepper_email" name="srv_setup_stepper_email" class="txt_fld form-control warn_on_paste" 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="invalid-feedback"></span>
</div>
<div class="form-floating mb-3">
<input required type="password" id="srv_setup_stepper_password" name="srv_setup_stepper_password" autocomplete="current-password" class="txt_fld form-control warn_on_paste" value="" placeholder="'.$this->trans('Password').'">
<input required type="password" id="srv_setup_stepper_password" name="srv_setup_stepper_password" class="txt_fld form-control warn_on_paste" value="" placeholder="'.$this->trans('Password').'">
<label class="" for="srv_setup_stepper_password">'.$this->trans('Password').'</label>
<span id="srv_setup_stepper_password-error" class="invalid-feedback"></span>
</div>
Expand Down
4 changes: 2 additions & 2 deletions modules/nux/assets/data/server_accounts_sample.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
server_name;support_jmap;jmap_server;username;password;imap_port;imap_tls;imap_server;support_smtp;smtp_server;smtp_port;smtp_tls;sieve_host;sieve_port
Exemple;FALSE;;[email protected];secret;993;TRUE;imap.exemple.org;TRUE;smtp.exemple.org;465;TRUE;tls://imap.exemple.org;4190
server_name;jmap_server;jmap_hide_from_combined_view;username;password;imap_port;imap_tls;imap_hide_from_combined_view;imap_server;smtp_server;smtp_port;smtp_tls;sieve_host;sieve_port
Exemple;;FALSE;[email protected];secret;993;TRUE;FALSE;imap.exemple.org;smtp.exemple.org;465;TRUE;tls://imap.exemple.org;4190
32 changes: 16 additions & 16 deletions modules/nux/assets/data/server_accounts_sample.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
Test1:
jmap:
jmap_server:
server:
hide_from_combined_view: false
user:
username: [email protected]
password: secret
imap:
port: 993
tls: true
imap_server: imap.example.org
hide_from_combined_view: false
server: imap.example.org
smtp:
support_smtp: true
smtp_server: smtp.example.org
smtp_port: 465
smtp_tls: true
server: smtp.example.org
port: 465
tls: true
sieve:
sieve_host: tls://imap.example.org
sieve_port: 4190
host: tls://imap.example.org
port: 4190
Test2:
jmap:
jmap_server:
server:
user:
username: [email protected]
password: secret
imap:
port: 993
tls: true
imap_server: mail.example.org
server: mail.example.org
smtp:
support_smtp: true
smtp_server: mail.example.org
smtp_port: 465
smtp_tls: true
server: mail.example.org
port: 465
tls: true
sieve:
sieve_host: tls://mail.example.org
sieve_port: 4190
host: tls://mail.example.org
port: 4190
120 changes: 120 additions & 0 deletions modules/nux/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
use SplFileObject;

/**
* NUX modules
* @package modules
* @subpackage nux
*/

if (!defined('DEBUG_MODE')) { die(); }

/**
* Build a source list for sent folders
* @subpackage nux/functions
* @param string $file_path file path
* @param string $delimiter csv delimiter with default ;
* @return array
*/
if (!hm_exists('parse_csv_with_headers')) {
function parse_csv_with_headers($file_path, $delimiter = ';') {
// Open the file
$file = new SplFileObject($file_path);
// Set the file to read as CSV
$file->setFlags(SplFileObject::DROP_NEW_LINE);

// Initialize an array to hold the rows
$rows = [];
$is_first_line = true;
$header = [];
// Loop through each line in the CSV file
while (!$file->eof()) {
// Get the line as a string
$line = $file->fgets();

// Skip empty lines (e.g., due to trailing newlines)
if ($line === [null] || $line === false) {
continue;
}

// Split the line into an array using the specified delimiter
$fields = explode($delimiter, $line);
// Process the header line
if ($is_first_line) {
$header = $fields;
$is_first_line = false;
} else {
// Ensure that the number of fields matches the header count
if (count($fields) === count($header)) {
// Combine the header and fields into an associative array
$rows[] = array_combine($header, $fields);
}
}
}
return $rows;
}}

/**
* @subpackage nux/functions
*/
if (!hm_exists('oauth2_form')) {
function oauth2_form($details, $mod)
{
$oauth2 = new Hm_Oauth2($details['client_id'], $details['client_secret'], $details['redirect_uri']);
$url = $oauth2->request_authorization_url($details['auth_uri'], $details['scope'], 'nux_authorization', $details['email']);
$res = '<input type="hidden" name="nux_service" value="' . $mod->html_safe($details['id']) . '" />';
$res .= '<div class="nux_step_two_title fw-bold">' . $mod->html_safe($details['name']) . '</div><div class="mb-3">';
$res .= $mod->trans('This provider supports Oauth2 access to your account.');
$res .= $mod->trans(' This is the most secure way to access your E-mail. Click "Enable" to be redirected to the provider site to allow access.');
$res .= '</div><div class="mb-3"><a class="enable_auth2 btn btn-sm btn-success me-2" href="' . $url . '">' . $mod->trans('Enable') . '</a>';
$res .= '<a href="" class="reset_nux_form btn btn-sm btn-secondary">Reset</a></div>';
return $res;
}
}

/**
* @subpackage nux/functions
*/
if (!hm_exists('credentials_form')) {
function credentials_form($details, $mod)
{
$res = '<input type="hidden" id="nux_service" name="nux_service" value="' . $mod->html_safe($details['id']) . '" />';
$res .= '<input type="hidden" name="nux_name" class="nux_name" value="' . $mod->html_safe($details['name']) . '" />';
$res .= '<div class="nux_step_two_title"><b>' . $mod->html_safe($details['name']) . '</b></div>';
$res .= $mod->trans('Enter your password for this E-mail provider to complete the connection process');

$res .= '<div class="row"><div class="col col-lg-4">';
// E-mail Address Field
$res .= '<div class="form-floating mb-3 mt-3">';
$res .= '<input type="email" class="form-control" id="nux_email" name="nux_email" placeholder="' . $mod->trans('E-mail Address') . '" value="' . $mod->html_safe($details['email']) . '">';
$res .= '<label for="nux_email">' . $mod->trans('E-mail Address') . '</label></div>';

// E-mail Password Field
$res .= '<div class="form-floating mb-3">';
$res .= '<input type="password" class="form-control nux_password" id="nux_password" name="nux_password" placeholder="' . $mod->trans('E-Mail Password') . '">';
$res .= '<label for="nux_password">' . $mod->trans('E-mail Password') . '</label></div>';

// Connect Button
$res .= '<input type="button" class="nux_submit px-5 btn btn-primary me-3" value="' . $mod->trans('Connect') . '">';

// Reset Link
$res .= '<a href="" class="reset_nux_form px-5 btn btn-secondary">Reset</a>';

$res .= '</div></div>';

return $res;
}
}

/**
* @subpackage nux/functions
*/
if (!hm_exists('data_source_available')) {
function data_source_available($mods, $types)
{
if (!is_array($types)) {
$types = array($types);
}
return count(array_intersect($types, $mods)) == count($types);
}
}
Loading

0 comments on commit 78a6079

Please sign in to comment.