Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new recipient choice syntax #5

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,37 +142,20 @@ In this case **one** drop-down will be present above the submit button. All addr

```php
'choices' => [
'mysite.org' => [
'me',
'awesome-you',
'info'
],
'another-person.org' => [
'hello',
'name',
'info'
]
]
```

In this case, just like the one above, **one** drop-down will be present above the submit button. All addresses will be available as separate entries. It happens when domain-keys are given, but the values inside differ.

```php
'choices' => [
'mysite.org' => [
'me',
'domains' => [
'mysite.org',
'awesome-you',
'info'
'ask.me'
],
'another-person.org' => [
'users' => [
'me',
'awesome-you',
'info'
]
]
```

In this case, just like the one above, **two** drop-down will be present above the submit button. One for the user (the part before the @-sign) and one for the domain. It happens when domain-keys are given, and the values are equal across all domains.
In this case **two** drop-downs will be present above the submit button. One for the user (the part before the @-sign) and one for the domain. It happens when domain-keys are given, and the values are equal across all domains.

### message

Expand Down
117 changes: 61 additions & 56 deletions config.default.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,70 @@
<?php

return [
'page' => [
'language' => 'en', // two digits as of ISO 639-1
'title' => 'Ask me something random!',
'description' => 'Feel free to ask me anything. Please just be reasonably polite.',
'debug' => false,
],
'owner' => [
'name' => '',
'e-mail' => '[email protected]',
'recipient-choices' => [
'enabled' => false,
'choices' => [
//'domain1' => [
// 'ask', // => ask@domain1
// 'awesome', // => awesome@domain1
//]
]
],
'allow-bad-recipient' => false, // Set to true if you want to skip PHPMailers E-Mail validation (e.g. name only recipients for local test sendmail setup)
'page' => [
'language' => 'en', // two digits as of ISO 639-1
'title' => 'Ask me something random!',
'description' => 'Feel free to ask me anything. Please just be reasonably polite.',
'debug' => false,
],
'owner' => [
'name' => '',
'e-mail' => '[email protected]',
'recipient-choices' => [
'enabled' => false,
'choices' => [
//'domain' => [
// 'myname.info',
// 'my.org',
//],
//'user' => [
// 'hello',
// 'ask',
// 'awesome'
//]
]
],
'allow-bad-recipient' => false, // Set to true if you want to skip PHPMailers E-Mail validation (e.g. name only recipients for local test sendmail setup)
],
'message' => [
'subject' => 'New message for you',
'sender' => '[email protected]',
'force-sender' => false, // Always set it to sender even if a senders E-Mail is given
],
'form' => [
'message' => [
'subject' => 'New message for you',
'sender' => '[email protected]',
'force-sender' => false, // Always set it to sender even if a senders E-Mail is given
'label' => 'Your Message to me',
'placeholder' => 'I\'ve always wanted to tell you...',
],
'submit' => [
'label' => 'Send it!',
],
'form' => [
'message' => [
'label' => 'Your Message to me',
'placeholder' => 'I\'ve always wanted to tell you...',
],
'submit' => [
'label' => 'Send it!',
],
'sender' => [
'label' => 'This is optional.',
'email' => [
'label' => 'Your E-Mail (optional)',
'palceholder' => '[email protected]',
],
'name' => [
'label' => 'Your Name (optional)',
'placeholder' => 'Lucky Luke',
]
],
'recipient' => [
'label' => 'Whom to send the message to?'
]
'sender' => [
'label' => 'This is optional.',
'email' => [
'label' => 'Your E-Mail (optional)',
'palceholder' => '[email protected]',
],
'name' => [
'label' => 'Your Name (optional)',
'placeholder' => 'Lucky Luke',
]
],
'email' => [
'provider' => 'sendmail',
'smtp' => [
'host' => '',
'port' => 465,
'crypt' => 'smtps',
'user' => '',
'pass' => ''
],
'recipient' => [
'label' => 'Whom to send the message to?'
]
],
'email' => [
'provider' => 'sendmail',
'smtp' => [
'host' => '',
'port' => 465,
'crypt' => 'smtps',
'user' => '',
'pass' => ''
],
'modules' => [ // Different modules that are included at different parts of the process. Must be present in modules/<name>/ and provide a main.php
'headers' => [] // Must return an array with all custom headers to be added
]
],
'modules' => [ // Different modules that are included at different parts of the process. Must be present in modules/<name>/ and provide a main.php
'headers' => [] // Must return an array with all custom headers to be added
]
];
90 changes: 51 additions & 39 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,72 @@
* Source: https://stackoverflow.com/a/263621
*/
function array_depth(array $array) :int {
$max_indentation = 1;
$max_indentation = 1;

$array_str = print_r($array, true);
$lines = explode("\n", $array_str);
$array_str = print_r($array, true);
$lines = explode("\n", $array_str);

foreach ($lines as $line) {
$indentation = (strlen($line) - strlen(ltrim($line))) / 4;
foreach ($lines as $line) {
$indentation = (strlen($line) - strlen(ltrim($line))) / 4;

if ($indentation > $max_indentation) {
$max_indentation = $indentation;
}
if ($indentation > $max_indentation) {
$max_indentation = $indentation;
}
}

return ceil(($max_indentation - 1) / 2) + 1;
return ceil(($max_indentation - 1) / 2) + 1;
}

function processRecipientChoices(array $recipientChoices) :array {
$depth = array_depth($recipientChoices);
$depth = array_depth($recipientChoices);

$choices = [
'addresses' => [],
'users' => [],
'domains' => []
];
$choices = [
'addresses' => [],
'users' => [],
'domains' => []
];

$usersEqual = true;
if ($depth == 1) {
$choices['addresses'] = $recipientChoices;
} elseif ($depth == 2) {
$choices['domains'] = array_keys($recipientChoices);
foreach ($choices['domains'] as $domain) {
foreach ($choices['domains'] as $domain2) {
if ($recipientChoices[$domain] != $recipientChoices[$domain2]) {
$usersEqual = false;
$choices['users'] = [];
break 2;
} else {
$choices['users'] = $recipientChoices[$domain];
}
}
$usersEqual = true;
if ($depth == 1) {
$choices['addresses'] = $recipientChoices;
} elseif ($depth == 2) {
if (array_key_exists('domains', $recipientChoices) && array_key_exists('users', $recipientChoices)) {
$choices['domains'] = $recipientChoices['domains'];
$choices['users'] = $recipientChoices['users'];
foreach ($choices['domains'] as $domain) {
foreach ($choices['users'] as $user) {
$choices['addresses'][] = $user . '@' . $domain;
}
}
} else {
$choices['domains'] = array_keys($recipientChoices);
foreach ($choices['domains'] as $domain) {
foreach ($choices['domains'] as $domain2) {
if ($recipientChoices[$domain] != $recipientChoices[$domain2]) {
$usersEqual = false;
$choices['users'] = [];
break 2;
} else {
$choices['users'] = $recipientChoices[$domain];
}
}

foreach ($choices['domains'] as $domain) {
foreach($recipientChoices[$domain] as $user) {
if (!empty($domain)) {
$choices['addresses'][] = $user . '@' . $domain;
} else {
$choices['addresses'][] = $user;
}
foreach($recipientChoices[$domain] as $user) {
if (!empty($domain)) {
$choices['addresses'][] = $user . '@' . $domain;
} else {
$choices['addresses'][] = $user;
}
}
}
} else {
throw new Exception("There are to many levels for custom recipient choices available. Maximum is 2", 1);
}
}

return $choices;
} else {
throw new Exception("There are to many levels for custom recipient choices available. Maximum is 2", 1);
}


return $choices;
}
17 changes: 12 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,19 @@
}
echo "</select>";
} else {
echo "<select name=\"recipient-user\" id=\"recipient-user\" required>\n";
foreach ($recipientChoices['users'] as $user) {
printf("<option value=\"%1\$s\">%1\$s</option>\n", $user);
if (count($recipientChoices['users']) > 1) {
echo "<select name=\"recipient-user\" id=\"recipient-user\" required>\n";
foreach ($recipientChoices['users'] as $user) {
printf("<option value=\"%1\$s\">%1\$s</option>\n", $user);
}
echo "</select>";
} else {
printf('
<span>%1$s</span><input type="hidden" value="%1$s" name="recipient-user">',
$recipientChoices['users'][0]
);
}
echo "</select>
<span class=\"at-connector\">@</span>";
echo "<span class=\"at-connector\">@</span>";
if (count($recipientChoices['domains']) > 1) {
echo "<select name=\"recipient-domain\" id=\"recipient-domain\" required>\n";
foreach ($recipientChoices['domains'] as $domain) {
Expand Down