diff --git a/README.md b/README.md index e678951..333e9c3 100644 --- a/README.md +++ b/README.md @@ -142,29 +142,12 @@ 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' @@ -172,7 +155,7 @@ In this case, just like the one above, **one** drop-down will be present above t ] ``` -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 diff --git a/config.default.php b/config.default.php index 85a5668..4e3ec28 100644 --- a/config.default.php +++ b/config.default.php @@ -1,65 +1,70 @@ [ - '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' => 'owner@ask.example.com', - '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' => 'owner@ask.example.com', + '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' => 'ask@example.com', + 'force-sender' => false, // Always set it to sender even if a senders E-Mail is given + ], + 'form' => [ 'message' => [ - 'subject' => 'New message for you', - 'sender' => 'ask@example.com', - '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' => 'person@example.com', - ], - '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' => 'person@example.com', + ], + '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// 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// and provide a main.php + 'headers' => [] // Must return an array with all custom headers to be added + ] ]; diff --git a/functions.php b/functions.php index ab83ec9..ddfc5c4 100644 --- a/functions.php +++ b/functions.php @@ -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; } diff --git a/index.php b/index.php index f98dd1d..75226a6 100644 --- a/index.php +++ b/index.php @@ -255,12 +255,19 @@ } echo ""; } else { - echo "\n"; + foreach ($recipientChoices['users'] as $user) { + printf("\n", $user); + } + echo ""; + } else { + printf(' + %1$s', + $recipientChoices['users'][0] + ); } - echo " - @"; + echo "@"; if (count($recipientChoices['domains']) > 1) { echo "