diff --git a/_tests/unit/ConfigurationTest.php b/_tests/unit/ConfigurationTest.php index 2af504b..585fa5e 100644 --- a/_tests/unit/ConfigurationTest.php +++ b/_tests/unit/ConfigurationTest.php @@ -13,14 +13,19 @@ public function testConfigFileStructure() require('include/config.inc.php'); // verify we have the correct number of config array elements - $this->assertEquals(19, count($config), 'Incorrect number of settings in $config array.'); + $this->assertEquals(23, count($config), 'Incorrect number of settings in $config array.'); // verify config array keys are named as expected $this->assertTrue(array_key_exists('site_version', $config), '$config element version missing.'); $this->assertTrue(array_key_exists('site_domain', $config), '$config element site_domain missing.'); $this->assertTrue(array_key_exists('site_root', $config), '$config element site_root missing.'); $this->assertTrue(array_key_exists('compress', $config), '$config element compress missing.'); + $this->assertTrue(array_key_exists('contact_method', $config), '$config element contact_method missing.'); $this->assertTrue(array_key_exists('send_to_address', $config), '$config element send_to_address missing.'); + + $this->assertTrue(array_key_exists('slack_channel', $config), '$config element slack_channel missing.'); + $this->assertTrue(array_key_exists('slack_icon', $config), '$config element slack_icon missing.'); + $this->assertTrue(array_key_exists('slack_url', $config), '$config element slack_url missing.'); $this->assertTrue(array_key_exists('optimizely_id', $config), '$config element optimizely_id missing.'); $this->assertTrue(array_key_exists('a_b_testing', $config), '$config element a_b_testing missing.'); $this->assertTrue(array_key_exists('google_analytics_id', $config), '$config element google_analytics_id missing.'); diff --git a/ajax.php b/ajax.php index 1470204..0343ec2 100644 --- a/ajax.php +++ b/ajax.php @@ -30,9 +30,9 @@ // package form data up form submission data $form = package_form_submission($_POST['data']); - // send email with cleaned form data - send_contact_email($form, $response); - + // send message with cleaned form data + send_contact_message($form, $response); + break; } } diff --git a/contact.php b/contact.php index b063166..ad3b8bf 100644 --- a/contact.php +++ b/contact.php @@ -42,9 +42,10 @@ if ($config['recaptcha_active'] && $config['recaptcha_site_key'] && $config['recaptcha_secret_key']) { $captcha_challenge = '
'; + $captcha_scripts = ''; + } - $captcha_scripts = << + $contact_javascript = << (function($){ /* @@ -71,7 +72,10 @@ .done(function(data) { // handle success response, reset form and recaptcha $('#contact_christopherl').trigger('reset'); - grecaptcha.reset(); + + if (typeof grecaptcha !== 'undefined') { + grecaptcha.reset(); + } // update feedback $('#feedback').show(); @@ -100,11 +104,10 @@ }(jQuery)); HTML; - } $smarty->assign('head_extras', ''); $smarty->assign('body_header_extras', ''); - $smarty->assign('body_footer_extras', smarty_content($captcha_scripts)); + $smarty->assign('body_footer_extras', smarty_content($captcha_scripts . $contact_javascript)); $footer_cta = newsletter_subscribe(); diff --git a/include/config.inc.dist.php b/include/config.inc.dist.php index f848318..606418e 100644 --- a/include/config.inc.dist.php +++ b/include/config.inc.dist.php @@ -33,8 +33,19 @@ 'compress' => 0, - // Email address that should recieve contact form submissions - 'send_to_address' => '', + // 0 = old timey email + // 1 = slack webhook + 'contact_method' => '0', + + + // Email address that should recieve contact form submissions, can be left blank if using slack + 'send_to_address' => 'chris@christopherl.com', + + + // slack webhook settings + 'slack_channel' => '#', + 'slack_icon' => ':memo:', + 'slack_url' => '', // Optimizely Project ID, if blank JavaScript test code will not be enabled. diff --git a/include/email.inc.php b/include/email.inc.php index 4ff0c4d..b570ad2 100644 --- a/include/email.inc.php +++ b/include/email.inc.php @@ -14,7 +14,7 @@ * @param array $form submitted form data ($key = form input id, $value = submitted value) * @param $response reference to ajax.php $response array */ -function send_contact_email($form=array(), &$response) { +function send_contact_message($form=array(), &$response) { global $config; if (!$config['send_to_address']) { @@ -52,12 +52,37 @@ function send_contact_email($form=array(), &$response) { else if (!$form['message']) { set_response_error("How can we reply if you don't say anything?", 'message', $response); } - // looks good, send that email + // looks good, send message else { - $send_result = mail("{$config['send_to_address']}", - "Greetings from ChristopherL", - "{$form['message']}\n\n--------------\n{$form['fullname']}\n{$form['email']}", - "From: {$form['email']}"); + if (intval($config['contact_method']) == 0) { + $send_result = mail("{$config['send_to_address']}", + "Greetings from ChristopherL", + "{$form['message']}\n\n--------------\n{$form['fullname']}\n{$form['email']}", + "From: {$form['email']}"); + } + else { + $data = "payload=" . json_encode(array( + "channel" => "{$config['slack_channel']}", + "text" => sprintf("%s Contact Message:\n*Name:* %s\n*Email:* %s\n> %s", + $config['site_domain'], + $form['fullname'], + $form['email'], + $form['message'] + ), + "icon_emoji" => $config['slack_icon'] + )); + + $ch = curl_init($config['slack_url']); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + if (curl_exec($ch) == 'ok') { + $send_result = true; + } + + curl_close($ch); + } // didn't work, have them try again if (!$send_result) {