286 lines
10 KiB
PHP
Executable file
286 lines
10 KiB
PHP
Executable file
<?php
|
|
/* Copyright (C) 2026 Eduard Wisch <data@data-it-solution.de>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*/
|
|
|
|
/**
|
|
* \file importzugferd/admin/setup.php
|
|
* \ingroup importzugferd
|
|
* \brief ImportZugferd setup page.
|
|
*/
|
|
|
|
// Load Dolibarr environment
|
|
$res = 0;
|
|
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
|
|
$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
|
|
}
|
|
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
|
|
$tmp2 = realpath(__FILE__);
|
|
$i = strlen($tmp) - 1;
|
|
$j = strlen($tmp2) - 1;
|
|
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
|
|
$i--;
|
|
$j--;
|
|
}
|
|
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
|
|
$res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
|
|
}
|
|
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
|
|
$res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
|
|
}
|
|
if (!$res && file_exists("../../main.inc.php")) {
|
|
$res = @include "../../main.inc.php";
|
|
}
|
|
if (!$res && file_exists("../../../main.inc.php")) {
|
|
$res = @include "../../../main.inc.php";
|
|
}
|
|
if (!$res) {
|
|
die("Include of main fails");
|
|
}
|
|
|
|
// Libraries
|
|
require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
|
|
require_once '../lib/importzugferd.lib.php';
|
|
|
|
// Translations
|
|
$langs->loadLangs(array("admin", "importzugferd@importzugferd"));
|
|
|
|
// Parameters
|
|
$action = GETPOST('action', 'aZ09');
|
|
$backtopage = GETPOST('backtopage', 'alpha');
|
|
|
|
// Access control
|
|
if (!$user->admin) {
|
|
accessforbidden();
|
|
}
|
|
|
|
// Form setup using FormSetup class
|
|
if (!class_exists('FormSetup')) {
|
|
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
|
|
}
|
|
$formSetup = new FormSetup($db);
|
|
|
|
/*
|
|
* Setup configuration items
|
|
*/
|
|
|
|
// IMAP Settings Section
|
|
$formSetup->newItem('IMAPSettings')->setAsTitle();
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_HOST');
|
|
$item->defaultFieldValue = '';
|
|
$item->cssClass = 'minwidth300';
|
|
$item->fieldAttr['placeholder'] = 'imap.example.com';
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_PORT');
|
|
$item->defaultFieldValue = '993';
|
|
$item->cssClass = 'width100';
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_USER');
|
|
$item->defaultFieldValue = '';
|
|
$item->cssClass = 'minwidth300';
|
|
$item->fieldAttr['placeholder'] = 'invoices@example.com';
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_PASSWORD');
|
|
$item->cssClass = 'minwidth300';
|
|
$item->fieldAttr['type'] = 'password';
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_FOLDER');
|
|
$item->defaultFieldValue = 'INBOX';
|
|
$item->cssClass = 'minwidth200';
|
|
|
|
$formSetup->newItem('IMPORTZUGFERD_IMAP_SSL')->setAsYesNo();
|
|
|
|
// Import Settings Section
|
|
$formSetup->newItem('ImportSettings')->setAsTitle();
|
|
|
|
$formSetup->newItem('IMPORTZUGFERD_AUTO_CREATE_INVOICE')->setAsYesNo();
|
|
|
|
// Folder Import Settings Section
|
|
$formSetup->newItem('FolderImportSettings')->setAsTitle();
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_WATCH_FOLDER');
|
|
$item->defaultFieldValue = '';
|
|
$item->cssClass = 'minwidth400';
|
|
$item->fieldAttr['placeholder'] = '/path/to/invoices';
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_ARCHIVE_FOLDER');
|
|
$item->defaultFieldValue = '';
|
|
$item->cssClass = 'minwidth400';
|
|
$item->fieldAttr['placeholder'] = '/path/to/archive';
|
|
|
|
$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_ARCHIVE_FOLDER');
|
|
$item->defaultFieldValue = 'Archive';
|
|
$item->cssClass = 'minwidth200';
|
|
|
|
/*
|
|
* Actions
|
|
*/
|
|
|
|
if (versioncompare(explode('.', DOL_VERSION), array(15)) < 0 && $action == 'update' && !empty($user->admin)) {
|
|
$formSetup->saveConfFromPost();
|
|
}
|
|
|
|
include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
|
|
|
|
/*
|
|
* View
|
|
*/
|
|
|
|
$form = new Form($db);
|
|
|
|
$title = "ImportZugferdSetup";
|
|
llxHeader('', $langs->trans($title), '', '', 0, 0, '', '', '', 'mod-importzugferd page-admin');
|
|
|
|
// Subheader
|
|
$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';
|
|
|
|
print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
|
|
|
|
// Configuration header
|
|
$head = importzugferdAdminPrepareHead();
|
|
print dol_get_fiche_head($head, 'settings', $langs->trans($title), -1, "importzugferd@importzugferd");
|
|
|
|
// Setup page description
|
|
print '<span class="opacitymedium">'.$langs->trans("ImportZugferdSetupPage").'</span><br><br>';
|
|
|
|
// Display the form
|
|
print $formSetup->generateOutput(true);
|
|
|
|
// Test IMAP connection button and folder selection
|
|
if (getDolGlobalString('IMPORTZUGFERD_IMAP_HOST')) {
|
|
print '<br>';
|
|
print '<div class="div-table-responsive-no-min">';
|
|
print '<table class="noborder centpercent">';
|
|
print '<tr class="liste_titre">';
|
|
print '<td colspan="3">'.$langs->trans('TestConnection').'</td>';
|
|
print '</tr>';
|
|
|
|
// Check if IMAP extension is available
|
|
$imap_available = function_exists('imap_open');
|
|
|
|
// Test connection action
|
|
$imap_folders = array();
|
|
$connection_ok = false;
|
|
|
|
if (!$imap_available) {
|
|
print '<tr class="oddeven">';
|
|
print '<td colspan="3">';
|
|
print '<span class="error"><i class="fas fa-exclamation-triangle paddingright"></i>';
|
|
print $langs->trans('IMAPExtensionNotInstalled');
|
|
print '</span><br>';
|
|
print '<span class="opacitymedium">'.$langs->trans('IMAPExtensionHelp').'</span>';
|
|
print '</td>';
|
|
print '</tr>';
|
|
} elseif ($action == 'test_imap' || $action == 'select_folder') {
|
|
$host = getDolGlobalString('IMPORTZUGFERD_IMAP_HOST');
|
|
$port = getDolGlobalString('IMPORTZUGFERD_IMAP_PORT', '993');
|
|
$imap_user = getDolGlobalString('IMPORTZUGFERD_IMAP_USER');
|
|
$password = getDolGlobalString('IMPORTZUGFERD_IMAP_PASSWORD');
|
|
$ssl = getDolGlobalString('IMPORTZUGFERD_IMAP_SSL');
|
|
|
|
$mailbox_base = '{' . $host . ':' . $port . '/imap' . ($ssl ? '/ssl' : '') . '/novalidate-cert}';
|
|
$mailbox = $mailbox_base . 'INBOX';
|
|
|
|
$connection = @imap_open($mailbox, $imap_user, $password);
|
|
|
|
if ($connection) {
|
|
$connection_ok = true;
|
|
setEventMessages($langs->trans('ConnectionSuccessful'), null, 'mesgs');
|
|
|
|
// Get list of folders
|
|
$folders_raw = imap_list($connection, $mailbox_base, '*');
|
|
if ($folders_raw) {
|
|
foreach ($folders_raw as $folder) {
|
|
// Remove the mailbox base from folder name
|
|
$folder_name = str_replace($mailbox_base, '', $folder);
|
|
// Decode folder name (IMAP uses modified UTF-7)
|
|
$folder_name_decoded = imap_utf7_decode($folder_name);
|
|
$imap_folders[$folder_name] = $folder_name_decoded;
|
|
}
|
|
}
|
|
|
|
imap_close($connection);
|
|
} else {
|
|
setEventMessages($langs->trans('ConnectionFailed') . ': ' . imap_last_error(), null, 'errors');
|
|
}
|
|
}
|
|
|
|
// Save selected folder
|
|
if ($action == 'select_folder' && GETPOST('imap_folder', 'alpha')) {
|
|
$selected_folder = GETPOST('imap_folder', 'alpha');
|
|
dolibarr_set_const($db, 'IMPORTZUGFERD_IMAP_FOLDER', $selected_folder, 'chaine', 0, '', $conf->entity);
|
|
setEventMessages($langs->trans('FolderSelected').': '.$selected_folder, null, 'mesgs');
|
|
}
|
|
|
|
// Only show status and folder selection if IMAP is available
|
|
if ($imap_available) {
|
|
print '<tr class="oddeven">';
|
|
print '<td class="titlefield">'.$langs->trans('Status').'</td>';
|
|
print '<td colspan="2">';
|
|
if ($action == 'test_imap' || $action == 'select_folder') {
|
|
if ($connection_ok) {
|
|
print '<span class="ok"><i class="fas fa-check paddingright"></i>'.$langs->trans('ConnectionSuccessful').'</span>';
|
|
} else {
|
|
print '<span class="error"><i class="fas fa-times paddingright"></i>'.$langs->trans('ConnectionFailed').'</span>';
|
|
}
|
|
} else {
|
|
print '<span class="opacitymedium">'.$langs->trans('ClickTestToCheck').'</span>';
|
|
}
|
|
print '</td>';
|
|
print '</tr>';
|
|
}
|
|
|
|
// Show folder selection if connection was successful
|
|
if ($imap_available && $connection_ok && !empty($imap_folders)) {
|
|
print '<tr class="oddeven">';
|
|
print '<td>'.$langs->trans('SelectFolder').'</td>';
|
|
print '<td>';
|
|
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" class="inline-block">';
|
|
print '<input type="hidden" name="token" value="'.newToken().'">';
|
|
print '<input type="hidden" name="action" value="select_folder">';
|
|
|
|
$current_folder = getDolGlobalString('IMPORTZUGFERD_IMAP_FOLDER', 'INBOX');
|
|
print '<select name="imap_folder" class="flat minwidth200">';
|
|
foreach ($imap_folders as $folder_raw => $folder_decoded) {
|
|
$selected = ($folder_raw == $current_folder) ? ' selected' : '';
|
|
print '<option value="'.dol_escape_htmltag($folder_raw).'"'.$selected.'>';
|
|
print dol_escape_htmltag($folder_decoded);
|
|
print '</option>';
|
|
}
|
|
print '</select>';
|
|
print ' <input type="submit" class="button" value="'.$langs->trans('Save').'">';
|
|
print '</form>';
|
|
print '</td>';
|
|
print '<td>';
|
|
print '<span class="opacitymedium">'.$langs->trans('FoundFolders').': '.count($imap_folders).'</span>';
|
|
print '</td>';
|
|
print '</tr>';
|
|
}
|
|
|
|
// Only show test button if IMAP extension is available
|
|
if ($imap_available) {
|
|
print '<tr class="oddeven">';
|
|
print '<td colspan="3" class="center">';
|
|
print '<a class="button" href="'.$_SERVER['PHP_SELF'].'?action=test_imap&token='.newToken().'">';
|
|
print '<i class="fas fa-plug paddingright"></i>'.$langs->trans('TestConnection');
|
|
print '</a>';
|
|
print '</td>';
|
|
print '</tr>';
|
|
}
|
|
|
|
print '</table>';
|
|
print '</div>';
|
|
}
|
|
|
|
print '<br>';
|
|
|
|
// Page end
|
|
print dol_get_fiche_end();
|
|
|
|
llxFooter();
|
|
$db->close();
|