Compare commits

..

2 Commits

Author SHA1 Message Date
Clemens Schwaighofer
f5daaca598 Fixes for Create\Email docstrings 2022-07-08 17:25:45 +09:00
Clemens Schwaighofer
6b4f310cd2 Email\Sending add dedicated kv folding flag
kv folding is now done via flag and only if an encoded is detected that
is japanese or UTF-8
2022-07-04 15:45:51 +09:00
5 changed files with 210 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
-- 2022/6/17 update edit_user with login uid -- 2022/6/17 update edit_user with login uid
-- !!! COPY TABLE ARRAY FOLDER !!!
-- the login uid, at least 32 chars -- the login uid, at least 32 chars
ALTER TABLE edit_user ADD login_user_id VARCHAR UNIQUE; ALTER TABLE edit_user ADD login_user_id VARCHAR UNIQUE;
-- CREATE UNIQUE INDEX edit_user_login_user_id_key ON edit_user (login_user_id) WHERE login_user_id IS NOT NULL; -- CREATE UNIQUE INDEX edit_user_login_user_id_key ON edit_user (login_user_id) WHERE login_user_id IS NOT NULL;

View File

@@ -41,36 +41,56 @@ final class CoreLibsCreateEmailTest extends TestCase
// 0: email // 0: email
// 1: name // 1: name
// 2: encoding // 2: encoding
// 3: expected // 3: kv_folding
// 4: expected
return [ return [
'all empty' => [ 'all empty' => [
'', '',
null, null,
null, null,
null,
'' ''
], ],
'email only' => [ 'email only' => [
'test@test.com', 'test@test.com',
null, null,
null, null,
null,
'test@test.com' 'test@test.com'
], ],
'email and name' => [ 'email and name' => [
'test@test.com', 'test@test.com',
'Test Name', 'Test Name',
null, null,
null,
'"Test Name" <test@test.com>' '"Test Name" <test@test.com>'
], ],
'name in mime encoded, default UTF-8' => [ 'name in mime encoded, default UTF-8' => [
'test@test.com', 'test@test.com',
'日本語', '日本語',
null, null,
null,
'"=?UTF-8?B?5pel5pys6Kqe?=" <test@test.com>' '"=?UTF-8?B?5pel5pys6Kqe?=" <test@test.com>'
], ],
'name in mime encoded with half width Katakana, default UTF-8' => [
'test@test.com',
'日本語カタカナパ',
null,
null,
'"=?UTF-8?B?5pel5pys6Kqe7722776A7722776F776K776f?=" <test@test.com>'
],
'name in mime encoded with half width Katakana, folding on, default UTF-8' => [
'test@test.com',
'日本語カタカナパ',
'UTF-8',
true,
'"=?UTF-8?B?5pel5pys6Kqe44Kr44K/44Kr44OK44OR?=" <test@test.com>'
],
'name in mime encoded, UTF-8 parameter' => [ 'name in mime encoded, UTF-8 parameter' => [
'test@test.com', 'test@test.com',
'日本語', '日本語',
'UTF-8', 'UTF-8',
null,
'"=?UTF-8?B?5pel5pys6Kqe?=" <test@test.com>' '"=?UTF-8?B?5pel5pys6Kqe?=" <test@test.com>'
], ],
// does internal UTF-8 to ISO-2022-JP convert // does internal UTF-8 to ISO-2022-JP convert
@@ -78,7 +98,22 @@ final class CoreLibsCreateEmailTest extends TestCase
'test@test.com', 'test@test.com',
'日本語', '日本語',
'ISO-2022-JP', 'ISO-2022-JP',
'"=?ISO-2022-JP?B?GyRCRnxLXA==?=" <test@test.com>' null,
'"=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=" <test@test.com>'
],
'encoding with half width Katakana in ISO-2022-JP' => [
'test@test.com',
'日本語カタカナパ',
'ISO-2022-JP',
null,
'"=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCPz8/Pz8/?=" <test@test.com>'
],
'encoding with half width Katakana, folding on in ISO-2022-JP' => [
'test@test.com',
'日本語カタカナパ',
'ISO-2022-JP',
true,
'"=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCPz8/Pz8=?=" <test@test.com>'
] ]
]; ];
} }
@@ -95,14 +130,17 @@ final class CoreLibsCreateEmailTest extends TestCase
string $email, string $email,
?string $name, ?string $name,
?string $encoding, ?string $encoding,
?bool $kv_folding,
string $expected string $expected
): void { ): void {
if ($name === null && $encoding === null) { if ($name === null && $encoding === null && $kv_folding === null) {
$encoded_email = \CoreLibs\Create\Email::encodeEmailName($email); $encoded_email = \CoreLibs\Create\Email::encodeEmailName($email);
} elseif ($encoding === null) { } elseif ($encoding === null && $kv_folding === null) {
$encoded_email = \CoreLibs\Create\Email::encodeEmailName($email, $name); $encoded_email = \CoreLibs\Create\Email::encodeEmailName($email, $name);
} else { } elseif ($kv_folding === null) {
$encoded_email = \CoreLibs\Create\Email::encodeEmailName($email, $name, $encoding); $encoded_email = \CoreLibs\Create\Email::encodeEmailName($email, $name, $encoding);
} else {
$encoded_email = \CoreLibs\Create\Email::encodeEmailName($email, $name, $encoding, $kv_folding);
} }
$this->assertEquals( $this->assertEquals(
$expected, $expected,
@@ -119,6 +157,7 @@ final class CoreLibsCreateEmailTest extends TestCase
// 4: array for to email // 4: array for to email
// 5: replace content ([]/null) // 5: replace content ([]/null)
// 6: encoding (UTF-8/null) // 6: encoding (UTF-8/null)
// 7: kv_folding
// 8: return status // 8: return status
// 9: expected content // 9: expected content
return [ return [
@@ -130,6 +169,7 @@ final class CoreLibsCreateEmailTest extends TestCase
'to_email' => [], 'to_email' => [],
'replace' => null, 'replace' => null,
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => -1, 'expected_status' => -1,
'expected_content' => [], 'expected_content' => [],
], ],
@@ -141,9 +181,22 @@ final class CoreLibsCreateEmailTest extends TestCase
'to_email' => [], 'to_email' => [],
'replace' => null, 'replace' => null,
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => -2, 'expected_status' => -2,
'expected_content' => [], 'expected_content' => [],
], ],
'bad encoding, fail -3' => [
'subject' => 'SUBJECT',
'body' => 'BODY',
'from_email' => 'test@test.com',
'from_name' => '',
'to_email' => ['to@test.com'],
'replace' => null,
'encoding' => 'IDONTEXISTENCODING',
'kv_folding' => null,
'expected_status' => -3,
'expected_content' => [],
],
'sending email 1' => [ 'sending email 1' => [
'subject' => 'SUBJECT', 'subject' => 'SUBJECT',
'body' => 'BODY', 'body' => 'BODY',
@@ -154,6 +207,7 @@ final class CoreLibsCreateEmailTest extends TestCase
], ],
'replace' => null, 'replace' => null,
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -176,6 +230,7 @@ final class CoreLibsCreateEmailTest extends TestCase
], ],
'replace' => null, 'replace' => null,
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -188,6 +243,52 @@ final class CoreLibsCreateEmailTest extends TestCase
] ]
], ],
], ],
'sending email 1, encoded, with half width katakanata' => [
'subject' => 'SUBJECT 日本語カタカナパ',
'body' => 'BODY 日本語',
'from_email' => 'test@test.com',
'from_name' => '',
'to_email' => [
'test@test.com'
],
'replace' => null,
'encoding' => 'UTF-8',
'kv_folding' => null,
'expected_status' => 2,
'expected_content' => [
[
'header' => [
'From' => 'test@test.com'
],
'to' => 'test@test.com',
'subject' => 'SUBJECT =?UTF-8?B?5pel5pys6Kqe7722776A7722776F776K776f?=',
'body' => 'BODY 日本語',
]
],
],
'sending email 1, encoded, with half width katakanata, folding on' => [
'subject' => 'SUBJECT 日本語カタカナパ',
'body' => 'BODY 日本語',
'from_email' => 'test@test.com',
'from_name' => '',
'to_email' => [
'test@test.com'
],
'replace' => null,
'encoding' => 'UTF-8',
'kv_folding' => true,
'expected_status' => 2,
'expected_content' => [
[
'header' => [
'From' => 'test@test.com'
],
'to' => 'test@test.com',
'subject' => 'SUBJECT =?UTF-8?B?5pel5pys6Kqe44Kr44K/44Kr44OK44OR?=',
'body' => 'BODY 日本語',
]
],
],
'sending email 1, encoded subject ISO-2022-JP' => [ 'sending email 1, encoded subject ISO-2022-JP' => [
'subject' => 'SUBJECT 日本語', 'subject' => 'SUBJECT 日本語',
'body' => 'BODY 日本語', 'body' => 'BODY 日本語',
@@ -198,6 +299,7 @@ final class CoreLibsCreateEmailTest extends TestCase
], ],
'replace' => null, 'replace' => null,
'encoding' => 'ISO-2022-JP', 'encoding' => 'ISO-2022-JP',
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -222,6 +324,7 @@ final class CoreLibsCreateEmailTest extends TestCase
], ],
'replace' => null, 'replace' => null,
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -255,6 +358,7 @@ final class CoreLibsCreateEmailTest extends TestCase
'VAR' => 'bar', 'VAR' => 'bar',
], ],
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -280,6 +384,7 @@ final class CoreLibsCreateEmailTest extends TestCase
'VAR' => 'bar', 'VAR' => 'bar',
], ],
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -310,6 +415,7 @@ final class CoreLibsCreateEmailTest extends TestCase
'VAR' => 'bar', 'VAR' => 'bar',
], ],
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -340,6 +446,7 @@ final class CoreLibsCreateEmailTest extends TestCase
'VAR' => 'bar', 'VAR' => 'bar',
], ],
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -379,6 +486,7 @@ final class CoreLibsCreateEmailTest extends TestCase
'VAR' => 'bar', 'VAR' => 'bar',
], ],
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -434,6 +542,7 @@ final class CoreLibsCreateEmailTest extends TestCase
'VAR' => 'bar', 'VAR' => 'bar',
], ],
'encoding' => null, 'encoding' => null,
'kv_folding' => null,
'expected_status' => 2, 'expected_status' => 2,
'expected_content' => [ 'expected_content' => [
[ [
@@ -478,6 +587,7 @@ final class CoreLibsCreateEmailTest extends TestCase
* @param array $to_email * @param array $to_email
* @param array|null $replace * @param array|null $replace
* @param string|null $encoding * @param string|null $encoding
* @param bool|null $kv_folding
* @param int $expected_status * @param int $expected_status
* @param array $expected_content * @param array $expected_content
* @return void * @return void
@@ -490,6 +600,7 @@ final class CoreLibsCreateEmailTest extends TestCase
array $to_email, array $to_email,
?array $replace, ?array $replace,
?string $encoding, ?string $encoding,
?bool $kv_folding,
int $expected_status, int $expected_status,
array $expected_content array $expected_content
): void { ): void {
@@ -499,6 +610,9 @@ final class CoreLibsCreateEmailTest extends TestCase
if ($encoding === null) { if ($encoding === null) {
$encoding = 'UTF-8'; $encoding = 'UTF-8';
} }
if ($kv_folding === null) {
$kv_folding = false;
}
// force new set for each run // force new set for each run
self::$log->setLogUniqueId(true); self::$log->setLogUniqueId(true);
// set on of unique log id // set on of unique log id
@@ -512,6 +626,7 @@ final class CoreLibsCreateEmailTest extends TestCase
$to_email, $to_email,
$replace, $replace,
$encoding, $encoding,
$kv_folding,
true, true,
self::$log self::$log
); );

View File

@@ -70,10 +70,11 @@ $status = Email::sendEmail(
], ],
[], [],
'UTF-8', 'UTF-8',
false,
true, true,
$log $log
); );
print "SENDING: " . $status . "<br>"; print "SENDING A: " . $status . "<br>";
$status = Email::sendEmail( $status = Email::sendEmail(
'TEST {REPLACE}', 'TEST {REPLACE}',
'BODY {OTHER}', 'BODY {OTHER}',
@@ -97,10 +98,11 @@ $status = Email::sendEmail(
'OTHER' => '**other**' 'OTHER' => '**other**'
], ],
'UTF-8', 'UTF-8',
false,
true, true,
$log $log
); );
print "SENDING: " . $status . "<br>"; print "SENDING B: " . $status . "<br>";
$status = Email::sendEmail( $status = Email::sendEmail(
'TEST', 'TEST',
@@ -110,10 +112,29 @@ $status = Email::sendEmail(
['a@a.com', 'b@b.com'], ['a@a.com', 'b@b.com'],
[], [],
'UTF-8', 'UTF-8',
false,
true, true,
$log $log
); );
print "SENDING: " . $status . "<br>"; print "SENDING C: " . $status . "<br>";
// SUBJECT 日本語カタカナパ
$status = Email::sendEmail(
'TEST 日本語カタカナパカタカナバ',
'BODY 日本語カタカナパカタカナバ',
'test@test.com',
'Test Name 日本語カタカナパ',
[
['email' => 'a@a.com', 'name' => 'a 日本語カタカナパカタカナバ'],
['email' => 'b@b.com', 'name' => 'b 日本語プブガバケブプガバケ'],
],
[],
'UTF-8',
false,
true,
$log
);
print "SENDING D: " . $status . "<br>";
// error message // error message
print $log->printErrorMsg(); print $log->printErrorMsg();

View File

@@ -14,9 +14,9 @@ if (!DEBUG) {
}); });
}*/ }*/
// open overlay boxes counter // open overlay boxes counter for z-index
var GL_OB_S = 30; var GL_OB_S = 100;
var GL_OB_BASE = 30; var GL_OB_BASE = 100;
/** /**
* opens a popup window with winName and given features (string) * opens a popup window with winName and given features (string)

View File

@@ -13,6 +13,20 @@ namespace CoreLibs\Create;
*/ */
class Email class Email
{ {
/** @var array<string> allowed list for encodings that can do KV folding */
private static $encoding_kv_allowed = [
'UTF-8',
'EUC-JP',
'SJIS',
'SJIS-win',
'ISO-2022-JP',
'ISO-2022-JP-MS',
'JIS',
'JIS-ms',
];
/** @var string normaly this does not need to be changed */
private static $mb_convert_kana_mode = 'KV';
/** /**
* create mime encoded email part for to/from emails. * create mime encoded email part for to/from emails.
* If encoding is not UTF-8 it will convert the email name to target encoding * If encoding is not UTF-8 it will convert the email name to target encoding
@@ -22,12 +36,14 @@ class Email
* @param string $email E-Mail address * @param string $email E-Mail address
* @param string $email_name Name for the email address, in UTF-8, if not set, empty * @param string $email_name Name for the email address, in UTF-8, if not set, empty
* @param string $encoding Encoding, if not set UTF-8 * @param string $encoding Encoding, if not set UTF-8
* @param bool $kv_folding If set to true and a valid encoding, do KV folding
* @return string Correctly encoded and build email string * @return string Correctly encoded and build email string
*/ */
public static function encodeEmailName( public static function encodeEmailName(
string $email, string $email,
string $email_name = '', string $email_name = '',
string $encoding = 'UTF-8' string $encoding = 'UTF-8',
bool $kv_folding = false
): string { ): string {
if (!empty($email_name)) { if (!empty($email_name)) {
// if encoding is not UTF-8 then we convert // if encoding is not UTF-8 then we convert
@@ -36,11 +52,13 @@ class Email
} }
$email_name = $email_name =
mb_encode_mimeheader( mb_encode_mimeheader(
mb_convert_kana( in_array($encoding, self::$encoding_kv_allowed) && $kv_folding ?
mb_convert_kana(
$email_name,
self::$mb_convert_kana_mode,
$encoding
) :
$email_name, $email_name,
'KV',
$encoding
),
$encoding $encoding
); );
return '"' . $email_name . '" ' return '"' . $email_name . '" '
@@ -53,17 +71,20 @@ class Email
/** /**
* Subject/Body replace sub function * Subject/Body replace sub function
* *
* @param string $subject Subject string, in UTF-8 * @param string $subject Subject string, in UTF-8
* @param string $body Body string, in UTF-8 * @param string $body Body string, in UTF-8
* @param array<string,string> $replace Replace the array as key -> value, in UTF-8 * @param array<string,string> $replace Replace the array as key -> value, in UTF-8
* @param string $encoding Encoding for subject encode mime header * @param string $encoding Encoding for subject encode mime header
* @return array<string> Pos 0: Subject, Pos 1: Body * @param bool $kv_folding If set to true and a valid encoding,
* do KV folding
* @return array<string> Pos 0: Subject, Pos 1: Body
*/ */
private static function replaceContent( private static function replaceContent(
string $subject, string $subject,
string $body, string $body,
array $replace, array $replace,
string $encoding string $encoding,
bool $kv_folding
): array { ): array {
foreach (['subject', 'body'] as $element) { foreach (['subject', 'body'] as $element) {
$$element = str_replace( $$element = str_replace(
@@ -83,7 +104,17 @@ class Email
$body = mb_convert_encoding($body, $encoding, 'UTF-8'); $body = mb_convert_encoding($body, $encoding, 'UTF-8');
} }
// we need to encodde the subject // we need to encodde the subject
$subject = mb_encode_mimeheader($subject, $encoding); $subject = mb_encode_mimeheader(
in_array($encoding, self::$encoding_kv_allowed) && $kv_folding ?
// for any non UTF-8 encoding convert kana
mb_convert_kana(
$subject,
self::$mb_convert_kana_mode,
$encoding
) :
$subject,
$encoding
);
return [$subject, $body]; return [$subject, $body];
} }
@@ -104,6 +135,8 @@ class Email
* @param array<string,string> $replace_content Subject/Body replace as * @param array<string,string> $replace_content Subject/Body replace as
* search -> replace, in UTF-8 * search -> replace, in UTF-8
* @param string $encoding E-Mail encoding, default UTF-8 * @param string $encoding E-Mail encoding, default UTF-8
* @param bool $kv_folding If set to true and a valid encoding,
* do KV folding
* @param bool $test test flag, default off * @param bool $test test flag, default off
* @param \CoreLibs\Debug\Logging|null $log Logging class, * @param \CoreLibs\Debug\Logging|null $log Logging class,
* only used if test flag is true * only used if test flag is true
@@ -112,6 +145,7 @@ class Email
* 0 for send not ok * 0 for send not ok
* -1 for nothing set (emails, subject, body) * -1 for nothing set (emails, subject, body)
* -2 for empty to list * -2 for empty to list
* -3 encoding target not valid or not installed
*/ */
public static function sendEmail( public static function sendEmail(
string $subject, string $subject,
@@ -121,6 +155,7 @@ class Email
array $send_to_emails, array $send_to_emails,
array $replace_content = [], array $replace_content = [],
string $encoding = 'UTF-8', string $encoding = 'UTF-8',
bool $kv_folding = false,
bool $test = false, bool $test = false,
?\CoreLibs\Debug\Logging $log = null ?\CoreLibs\Debug\Logging $log = null
): int { ): int {
@@ -136,6 +171,12 @@ class Email
if (empty($subject) || empty($body) || empty($from_email)) { if (empty($subject) || empty($body) || empty($from_email)) {
return -1; return -1;
} }
if (
$encoding != 'UTF-8' &&
!in_array($encoding, mb_list_encodings())
) {
return -3;
}
// if not one valid to, abort // if not one valid to, abort
foreach ($send_to_emails as $to_email) { foreach ($send_to_emails as $to_email) {
// to_email can be string, then only to email // to_email can be string, then only to email
@@ -147,7 +188,8 @@ class Email
$_to_email = self::encodeEmailName( $_to_email = self::encodeEmailName(
$to_email['email'], $to_email['email'],
$to_email['name'] ?? '', $to_email['name'] ?? '',
$encoding $encoding,
$kv_folding
); );
$to_emails[] = $_to_email; $to_emails[] = $_to_email;
// if we have to replacement, this override replace content // if we have to replacement, this override replace content
@@ -183,7 +225,8 @@ class Email
$subject, $subject,
$body, $body,
$replace_content, $replace_content,
$encoding $encoding,
$kv_folding
); );
} }
@@ -205,7 +248,8 @@ class Email
$subject, $subject,
$body, $body,
$_replace, $_replace,
$encoding $encoding,
$kv_folding
); );
} }
} }
@@ -220,6 +264,7 @@ class Email
// build debug strings: convert to UTF-8 if not utf-8 // build debug strings: convert to UTF-8 if not utf-8
$log->debug('SEND EMAIL', 'HEADERS: ' . $log->prAr($headers) . ', ' $log->debug('SEND EMAIL', 'HEADERS: ' . $log->prAr($headers) . ', '
. 'ENCODING: ' . $encoding . ', ' . 'ENCODING: ' . $encoding . ', '
. 'KV FOLDING: ' . $log->prBl($kv_folding) . ', '
. 'TO: ' . $to_email . ', ' . 'TO: ' . $to_email . ', '
. 'SUBJECT: ' . $out_subject . ', ' . 'SUBJECT: ' . $out_subject . ', '
. 'BODY: ' . ($encoding == 'UTF-8' ? . 'BODY: ' . ($encoding == 'UTF-8' ?
@@ -227,6 +272,7 @@ class Email
mb_convert_encoding($out_body, 'UTF-8', $encoding))); mb_convert_encoding($out_body, 'UTF-8', $encoding)));
$log->debug('SEND EMAIL JSON', json_encode([ $log->debug('SEND EMAIL JSON', json_encode([
'encoding' => $encoding, 'encoding' => $encoding,
'kv_folding' => $kv_folding,
'header' => $headers, 'header' => $headers,
'to' => $to_email, 'to' => $to_email,
'subject' => $out_subject, 'subject' => $out_subject,