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 3, 2024
1 parent 997330a commit 3b0631b
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 342 deletions.
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;support_jmap;jmap_server;username;password;imap_port;imap_tls;imap_server;smtp_server;smtp_port;smtp_tls;sieve_host;sieve_port
Exemple;FALSE;;[email protected];secret;993;TRUE;imap.exemple.org;smtp.exemple.org;465;TRUE;tls://imap.exemple.org;4190
2 changes: 0 additions & 2 deletions modules/nux/assets/data/server_accounts_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Test1:
tls: true
imap_server: imap.example.org
smtp:
support_smtp: true
smtp_server: smtp.example.org
smtp_port: 465
smtp_tls: true
Expand All @@ -27,7 +26,6 @@ Test2:
tls: true
imap_server: mail.example.org
smtp:
support_smtp: true
smtp_server: mail.example.org
smtp_port: 465
smtp_tls: true
Expand Down
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 3b0631b

Please sign in to comment.