Compare commits

..

No commits in common. "87e58e64252998c98ee7996e2972a1146afb65fa" and "dc8e08b5d11d895d8ca7305ca2d19cf18fc5fba2" have entirely different histories.

6 changed files with 27 additions and 403 deletions

View file

@ -21,8 +21,4 @@
******************************************************************************/
// PhpLdapAdmin View Object URL format
define(
'LS_PHPLDAPADMIN_VIEW_OBJECT_URL_FORMAT',
'//'.(isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:gethostname()).
'/phpldapadmin/cmd.php?cmd=template_engine&server_id=0&dn=%{dn}'
);
define('LS_PHPLDAPADMIN_VIEW_OBJECT_URL_FORMAT','//'.$_SERVER['SERVER_NAME'].'/phpldapadmin/cmd.php?cmd=template_engine&server_id=0&dn=%{dn}');

View file

@ -1,7 +0,0 @@
{literal}<div id='logo'><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAAuCAMAAABam6YzAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAAlwSFlzAAAZ5QAAGeUBblTa3gAAAwBQTFRFR3BMAAAAAAAAAAAAAHK4AAAAAAAAAHK5AHO5AHK4AHK4AAAAAHS3AHC3AHG5AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/AAAAAAAAAAAAAAAAAAAAAAAAAHG4AAAAAHq6AHW3AAAAAG+4AAAAAAAAAHK4AAAAAAAAAAAAAHK4AAAAAG/BAAAAAHK4AAAAAAAAAHK4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK4AAAAAAAAAAAAAHK4AHK5AAAAAHK4AHK3AHK4AAAAAHK4AAAAAICAAHK3AAAAAAAAAAAAAAAAAHK4AHK4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHS5AHS5AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK4AAAAAAAAAAAAAAAAAHG4AHO5AA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ie"/></div>{/literal}
<h2>Welcome {$name}!</h2>
<p>This email is just an example of an email template.</p>
<p>Regards,</p>

View file

@ -1 +0,0 @@
Welcome {$name}!

View file

@ -69,23 +69,11 @@ function LSaddon_mail_support() {
'test_send_mail',
'cli_test_send_mail',
'Send a test email',
"[-s subject] [-b body] [-H] [recipient1] [...]",
"[-s subject] [-b body] [recipient]",
array (
" -s/--subject The test email subject (optional)",
" -b/--body The test email body (optional)",
" -H/--HTML Enable HTML email body mode (optional)",
" --header Email header using format:",
" header_name=header_value",
" Multiple headers could be specified by using this",
" optional argument multiple time.",
" -a|--attachment Email attachment using format:",
" /path/to/attachment.file[:filename]",
" The filename is optional (default: using source filename).",
" Multiple attachments could be specified by using this",
" optional argument multiple time.",
" --bcc Add Blind Carbon Copy (BCC) recipient(s)",
" --cc Add Carbon Copy (CC) recipient(s)",
" recipients The test email recipient(s) (required).",
" -s/--subject The test email subject (optional)",
" -b/--body The test email body (optional)",
" recipient The test email recipient (required)",
),
false, // This command does not need LDAP connection
'cli_test_send_mail_autocompleter'
@ -154,7 +142,9 @@ function sendMail($to, $subject, $msg, $headers=null, $attachments=null,
$headers["To"] = $to;
$to = ensureIsArray($to);
$to = array (
'To' => $to
);
foreach(array_keys($headers) as $header) {
if(in_array(strtoupper($header), array('BCC', 'CC'))) {
@ -171,7 +161,7 @@ function sendMail($to, $subject, $msg, $headers=null, $attachments=null,
unset($headers[$header]);
continue;
}
$to = array_merge($to, $headers[$header]);
$to[strtoupper($header)] = $headers[$header];
}
}
@ -225,12 +215,9 @@ if (php_sapi_name() != 'cli')
* @return bool
*/
function cli_test_send_mail($command_args) {
$recipients = array();
$recipient = null;
$subject = "Test email";
$body = "This is a test message.";
$html = false;
$headers = array();
$attachments = array();
for ($i=0; $i < count($command_args); $i++) {
switch ($command_args[$i]) {
case '--subject':
@ -253,68 +240,23 @@ function cli_test_send_mail($command_args) {
LScli :: usage("Invalid body provided.");
break;
case '--html':
case '-H':
$html = true;
break;
case '--header':
$i++;
LScli :: unquote_word($command_args[$i]);
$parts = explode('=', $command_args[$i]);
if (count($parts) != 2)
LScli :: usage('Invalid header string ('.$command_args[$i].').');
if (array_key_exists($parts[0], $headers))
LScli :: usage('Header "'.$parts[0].'" already specified.');
$headers[$parts[0]] = $parts[1];
break;
case '-a':
case '--attachment':
$i++;
LScli :: unquote_word($command_args[$i]);
$parts = explode(':', $command_args[$i]);
$path = $parts[0];
if (!is_file($path))
LScli :: usage('Invalid attachment "'.$command_args[$i].'": file not found.');
$attachments[$path] = count($parts) >= 2?$parts[1]:basename($path);
break;
case '--bcc':
$i++;
LScli :: unquote_word($command_args[$i]);
if (!checkEmail($command_args[$i]))
LScli :: usage('Invalid BCC recipient "'.$command_args[$i].'".');
$headers['BCC'] = isset($headers['BCC'])?ensureIsArray($headers['BCC']):[];
$headers['BCC'][] = $command_args[$i];
break;
case '--cc':
$i++;
LScli :: unquote_word($command_args[$i]);
if (!checkEmail($command_args[$i]))
LScli :: usage('Invalid CC recipient "'.$command_args[$i].'".');
$headers['CC'] = isset($headers['CC'])?ensureIsArray($headers['CC']):[];
$headers['CC'][] = $command_args[$i];
break;
default:
if (checkEmail($command_args[$i]))
$recipients[] = $command_args[$i];
if (!$recipient && checkEmail($command_args[$i]))
$recipient = $command_args[$i];
else
LScli :: usage("Invalid parameter '".$command_args[$i]."'.");
}
}
if (!$recipients)
LScli :: usage("You must provide as least one email recipient as positional parameter");
if (!$recipient)
LScli :: usage("You must provide test email recipient as first positional parameter");
$logger = LSlog :: get_logger('LSaddon_mail');
if (!sendMail($recipients, $subject, $body, $headers, $attachments, null, null, $html)) {
$logger -> error("Fail to send test email sent to '".implode(', ', $recipients)."'.");
if (!sendMail($recipient, $subject, $body)) {
$logger -> error("Fail to send test email sent to '$recipient'.");
return false;
}
$logger -> info("Test email sent to '".implode(', ', $recipients)."'.");
$logger -> info("Test email sent to '$recipient'.");
return true;
}
@ -329,29 +271,19 @@ function cli_test_send_mail($command_args) {
* @return array<string> List of available options for the word to autocomplete
**/
function cli_test_send_mail_autocompleter($comp_words, $comp_word_num, $comp_word, $opts) {
if (isset($comp_words[$comp_word_num-1]))
switch ($comp_words[$comp_word_num-1]) {
case '-s':
case '--subject':
case '-b':
case '--body':
case '--header':
case '-a':
case '--attachment':
case '--bcc':
case '--cc':
return array();
break;
}
switch ($comp_words[$comp_word_num-1]) {
case '-s':
case '--subject':
case '-b':
case '--body':
return array();
break;
}
$opts = array_merge(
$opts,
array (
'-s', '--subject',
'-b', '--body',
'-H', '--html',
'--header',
'-a', '--attachment',
'--bcc', '--cc',
)
);
return LScli :: autocomplete_opts($opts, $comp_word);

View file

@ -819,7 +819,7 @@ class LScli extends LSlog_staticLoggerClass {
* @return string The quote character or an empty string if word if not quoted
*/
public static function unquote_word(&$word) {
if ($word && in_array($word[0], array('"', "'"))) {
if (in_array($word[0], array('"', "'"))) {
$quote_char = $word[0];
$word = substr($word, 1);
if ($word[strlen($word)-1] == $quote_char)

View file

@ -20,20 +20,7 @@
******************************************************************************/
class LSmail extends LSlog_staticLoggerClass {
/**
* Array of templates directories where file have to be search
* @var array
*/
private static $template_directories = array('local', './');
/**
* Cache of loaded templates
* @see list_templates()
* @var array<string,array<string,string|null>>
*/
private static $_templates = null;
class LSmail {
/*
* Méthode chargeant les dépendances d'affichage
@ -94,287 +81,4 @@ class LSmail extends LSlog_staticLoggerClass {
}
}
/**
* List exiting email templates
* @return array<string,array<string,string|null>>
* [
* '[name]' => [
* 'subject' => '/path/to/name.subject' or null,
* 'html' => '/path/to/name.html' or null,
* 'txt' => '/path/to/name.txt' or null,
* ],
* [...]
* ]
*/
public static function list_templates() {
if (self :: $_templates)
return self :: $_templates;
self :: $_templates = [];
$expected_extensions = ['subject', 'html', 'txt'];
foreach(self :: $template_directories as $dir) {
$dir_path = realpath(LS_ROOT_DIR."/".$dir."/email_templates");
if ($dir_path === false)
// Directory not found or not accessible
continue;
foreach (new DirectoryIterator($dir_path) as $fileInfo) {
if(
$fileInfo->isDot()
|| !$fileInfo->isFile()
|| !$fileInfo->isReadable()
|| !in_array($fileInfo->getExtension(), $expected_extensions)
)
continue;
$name = $fileInfo->getBasename(".".$fileInfo->getExtension());
if (!array_key_exists($name, self :: $_templates)) {
self :: $_templates[$name] = [];
foreach($expected_extensions as $ext) self :: $_templates[$name][$ext] = null;
}
if (!self :: $_templates[$name][$fileInfo->getExtension()])
self :: $_templates[$name][$fileInfo->getExtension()] = $fileInfo->getRealPath();
}
}
return self :: $_templates;
}
/**
* Send email from template
* @param string $tplname The email template name
* @param string $to The email recipient
* @param array<string,mixed> $variables Variables to use to compute the template
* @param array<string,string>|null $headers Email headers
* @param array<string,string>|null $attachments Email attachments as an array with
* filepath as key and filename as value
* @return boolean True if the email was sent, false otherwise
*/
public static function send_mail_from_template(
$tplname, $to, $variables=null, $headers=null, $attachments=null
) {
$templates = self :: list_templates();
if (!array_key_exists($tplname, $templates)) {
LSerror :: addErrorCode('LSmail_01', $tplname);
return False;
}
$tpl = $templates[$tplname];
if (!$tpl['subject'] || !($tpl['txt'] || $tpl['html'])) {
LSerror :: addErrorCode('LSmail_02', $tplname);
return False;
}
$smarty = new Smarty();
if (is_array($variables))
array_map([$smarty, "assign"], array_keys($variables), array_values($variables));
try {
$subject = $smarty -> fetch("file:{$tpl['subject']}");
// Multiple line from subject cause problem, trim it and only the first line
$subject = explode("\n", trim($subject))[0];
self :: log_debug(
"send_mail_from_template($tplname, ".implode("|", $to)."): ".
"subject compute from '{$tpl['subject']}'."
);
if ($tpl['html']) {
$body = $smarty -> fetch("file:{$tpl['html']}");
$html = true;
self :: log_debug(
"send_mail_from_template($tplname, ".implode("|", $to)."): ".
"HTML body compute from '{$tpl['html']}'."
);
}
else {
$body = $smarty -> fetch("file:{$tpl['txt']}");
$html = false;
self :: log_debug(
"send_mail_from_template($tplname, ".implode("|", $to)."): ".
"text body compute from '{$tpl['txt']}'."
);
}
}
catch (Exception $e) {
self :: log_exception(
$e, getFData(
_("LSmail - An exception occured forging message from email template '%{template}'"),
$tplname
),
false
);
return false;
}
return sendMail($to, $subject, $body, $headers, $attachments, "\n", "utf8", $html);
}
/**
* CLI test_send_mail_template command
*
* @param array $command_args Command arguments :
* - Positional arguments :
* - template name
* - recipient
* - Optional arguments :
* - -V|--variable: template variable (format: variable=value)
* - -H|--header: header (format: header=value)
* - -a|--attachent: (format: /path/to/file.ext:filename or just /path/to/file.ext)
* - -bcc: BCC recipient(s)
* - -cc: CC recipient(s)
*
* @return boolean True on success, false otherwise
**/
public static function cli_test_send_mail_template($command_args) {
$template = null;
$recipients = array();
$variables = array();
$headers = array();
$attachments = array();
for ($i=0; $i < count($command_args); $i++) {
LScli :: unquote_word($command_args[$i]);
if (in_array($command_args[$i], array('-V', '--variable'))) {
$i++;
LScli :: unquote_word($command_args[$i]);
$parts = explode('=', $command_args[$i]);
if (count($parts) != 2)
LScli :: usage('Invalid variable string ('.$command_args[$i].').');
if (array_key_exists($parts[0], $variables))
LScli :: usage('Variable "'.$parts[0].'" already specified.');
$variables[$parts[0]] = $parts[1];
}
elseif (in_array($command_args[$i], array('-H', '--header'))) {
$i++;
LScli :: unquote_word($command_args[$i]);
$parts = explode('=', $command_args[$i]);
if (count($parts) != 2)
LScli :: usage('Invalid header string ('.$command_args[$i].').');
if (array_key_exists($parts[0], $headers))
LScli :: usage('Header "'.$parts[0].'" already specified.');
$headers[$parts[0]] = $parts[1];
}
elseif (in_array($command_args[$i], array('-a', '--attachent'))) {
$i++;
LScli :: unquote_word($command_args[$i]);
$parts = explode(':', $command_args[$i]);
$path = $parts[0];
if (!is_file($path))
LScli :: usage('Invalid attachment "'.$command_args[$i].'": file not found.');
$attachments[$path] = count($parts) >= 2?$parts[1]:basename($path);
}
elseif ($command_args[$i] == '--bcc') {
$i++;
LScli :: unquote_word($command_args[$i]);
if (!checkEmail($command_args[$i]))
LScli :: usage('Invalid BCC recipient "'.$command_args[$i].'".');
$headers['BCC'] = isset($headers['BCC'])?ensureIsArray($headers['BCC']):[];
$headers['BCC'][] = $command_args[$i];
}
elseif ($command_args[$i] == '--cc') {
$i++;
LScli :: unquote_word($command_args[$i]);
if (!checkEmail($command_args[$i]))
LScli :: usage('Invalid CC recipient "'.$command_args[$i].'".');
$headers['CC'] = isset($headers['CC'])?ensureIsArray($headers['CC']):[];
$headers['CC'][] = $command_args[$i];
}
else if (is_null($template)) {
$template = $command_args[$i];
}
else if (checkEmail($command_args[$i])) {
$recipients[] = $command_args[$i];
}
else
LScli :: usage('Invalid recipient "'.$command_args[$i].'".');
}
if (is_null($template) || empty($recipients))
LScli :: usage('You must provide email template name and at least one recipient.');
return self :: send_mail_from_template(
$template,
$recipients,
$variables,
$headers,
$attachments
);
}
/**
* Args autocompleter for CLI test_send_mail_from_template command
*
* @param array<string> $comp_words List of already typed words of the command
* @param int $comp_word_num The command word number to autocomplete
* @param string $comp_word The command word to autocomplete
* @param array<string> $opts List of global available options
*
* @return array<string> List of available options for the word to autocomplete
**/
public static function cli_test_send_mail_from_template_autocompleter(
$comp_words, $comp_word_num, $comp_word, $opts
) {
if (isset($comp_words[$comp_word_num-1]))
switch ($comp_words[$comp_word_num-1]) {
case '-v':
case '--variable':
case '-H':
case '--header':
case '-a':
case '--attachment':
case '--bcc':
case '--cc':
return array();
break;
}
$opts = array_merge(
$opts,
array (
'-v', '--variable',
'-H', '--header',
'-a', '--attachment',
'--bcc', '--cc',
)
);
return LScli :: autocomplete_opts($opts, $comp_word);
}
}
/**
* Error Codes
*/
LSerror :: defineError('LSmail_01',
___("LSmail: Unknown template %{name}.")
);
LSerror :: defineError('LSmail_02',
___("LSmail: Template %{name} is incomplete.")
);
// LScli
LScli :: add_command(
'test_send_mail_template',
array('LSmail', 'cli_test_send_mail_template'),
'Test to send an email template',
'[template] [-V var1=value] [recipient1] [recipient2]',
array(
' - Positional arguments :',
' - email template name',
' - email recipient(s)',
'',
' - Optional arguments :',
' -V|--variable Template variable using format:',
' variable_name=variable_value',
' Multiple variables could be specified by using this',
' optional argument multiple time.',
' -H|--header Email header using format:',
' header_name=header_value',
' Multiple headers could be specified by using this',
' optional argument multiple time.',
' -a|--attachment Email attachment using format:',
' /path/to/attachment.file[:filename]',
' The filename is optional (default: using source filename).',
' Multiple attachments could be specified by using this',
' optional argument multiple time.',
' --bcc Add Blind Carbon Copy (BCC) recipient(s)',
' --cc Add Carbon Copy (CC) recipient(s)',
),
false, // This command does not need LDAP connection
array('LSmail', 'cli_test_send_mail_from_template_autocompleter')
);