diff --git a/app/App.php b/app/App.php index 8f4dede..4f376b3 100644 --- a/app/App.php +++ b/app/App.php @@ -117,6 +117,7 @@ class App extends Application public function registerExtenders() { if (!$this['debug']) { + $this->log('In Error handler.'); $this->error(function (\Exception $e, \Symfony\Component\HttpFoundation\Request $request, $code) { switch ($code) { case 405: @@ -129,10 +130,13 @@ class App extends Application $matches = 'Available methods are unknown.'; } $message = json_encode(['status' => 'error', 'message' => 'Method not allowed', 'allowedMethods' => $matches, 'requestedMethod' => $request->getMethod(), 'code' => $code]); - //$message = 'Sorry bout that.
' . $e->getMessage(); + break; + case 500: + $message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]); break; default: $message = json_encode(['status' => 'error', 'message' => $e->getMessage(), 'code' => $code]); + break; } return new \Symfony\Component\HttpFoundation\Response($message, $code); }); diff --git a/app/config/config.yml b/app/config/config.yml index fc41bc1..0b6d523 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -10,3 +10,7 @@ app: captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q- captcha_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo theme: default + smtp_host: smtp.gmail.com + smtp_port: 465 + smtp_user: eric@rewiv.com + smtp_password: 'P*8ic32!100023&p' diff --git a/app/config/config.yml.dist b/app/config/config.yml.dist index 648a3c3..25f75b5 100644 --- a/app/config/config.yml.dist +++ b/app/config/config.yml.dist @@ -9,7 +9,8 @@ app: captcha_sitekey: 6LcvmSQTAAAAAMmf9w6mhCbpdLvknuD9SGVHT0q- captcha_secret: 6LcvmSQTAAAAAITkvYJjgLar1LqGGLz-ic0ZMiXo theme: default - #twig_paths: - # - views - #twig_template: uikit.html.twig + smtp_host: localhost + smtp_port: 465 + smtp_user: root + smtp_password: 123 diff --git a/app/providers.php b/app/providers.php index 61314dc..c7c8a76 100644 --- a/app/providers.php +++ b/app/providers.php @@ -29,6 +29,8 @@ use Silex\Provider\{ VarDumperServiceProvider, WebProfilerServiceProvider }; +use Symfony\Component\Debug\ErrorHandler; +use Symfony\Component\Debug\ExceptionHandler; use Symfony\Bridge\Monolog\Logger; use WhoopsPimple\WhoopsServiceProvider; @@ -36,6 +38,9 @@ $app->register(new ConfigServiceProvider(), [ 'config.path' => $app->getConfDirectory() . '/config.yml', ]); +ErrorHandler::register(); +ExceptionHandler::register(false); + $app->setDebug(); @@ -60,21 +65,32 @@ $app 'monolog.level' => $app->getDebug() ? Logger::DEBUG : Logger::INFO, 'monolog.handler' => new \Monolog\Handler\StreamHandler('php://stderr'), ]) - ->register(new \Silex\Provider\SwiftmailerServiceProvider()); + ->register(new \Silex\Provider\SwiftmailerServiceProvider()) + ->register(new ConsoleServiceProvider(), [ + 'console.name' => 'Resume.PHP', + 'console.version' => '0.0.1', + 'console.project_directory' => $app->getAppDirectory(), + ]); - if(false === getenv('SPARKPOST_API_KEY')) { - $app['swiftmailer.transport'] = new Swift_SendmailTransport(); - $app->log('We are local'); - } else { + if(false === getenv('SPARKPOST_API_KEY') && null !== $app->config('app.smtp_host')) { + $app['swiftmailer.options'] = [ + 'host' => $app->config('app.smtp_host'), + 'port' => $app->config('app.smtp_post'), + 'username' => $app->config('app.smtp_user'), + 'password' => $app->config('app.smtp_password'), + ]; + $app->log('Setting up local email.'); + } elseif (false !== getenv('SPARKPOST_API_KEY')) { $app['swiftmailer.options'] = [ 'host' => getenv('SPARKPOST_SMTP_HOST'), 'port' => getenv('SPARKPOST_SMTP_PORT'), 'username' => getenv('SPARKPOST_SMTP_USERNAME'), 'password' => getenv('SPARKPOST_SMTP_PASSWORD'), 'encryption' => 'tls', - 'auth_mode' => 'plain', ]; - $app->log('We are on heroku.'); + $app->log('Setting up sparkpost email.'); + } else { + $app['swiftmailer.transport'] = new Swift_SendmailTransport(); } $app->register(new RoutingServiceProvider()) @@ -86,12 +102,7 @@ if ($app['debug'] || 0 === strcasecmp($app['env'], 'dev')) { 'profiler.cache_dir' => $app->getDataDirectory() . '/cache/profiler', ]) ->register(new WhoopsServiceProvider()) - ->register(new VarDumperServiceProvider()) - ->register(new ConsoleServiceProvider(), [ - 'console.name' => 'Resume.PHP', - 'console.version' => '0.0.1', - 'console.project_directory' => $app->getAppDirectory(), - ]); + ->register(new VarDumperServiceProvider()); } if (null === $app->config('app.schema')) { $app->config('app.schema', 'https://raw.githubusercontent.com/jsonresume/resume-schema/v1.0.0/schema.json'); diff --git a/app/themes/default/base.html.twig b/app/themes/default/base.html.twig index 295ff0a..f5c3359 100644 --- a/app/themes/default/base.html.twig +++ b/app/themes/default/base.html.twig @@ -19,6 +19,7 @@ + {% block javascripts_head %}{% endblock %} {% block inline_js_head %}{% endblock %} diff --git a/app/themes/default/index.html.twig b/app/themes/default/index.html.twig index 84201f1..3ce5102 100644 --- a/app/themes/default/index.html.twig +++ b/app/themes/default/index.html.twig @@ -257,6 +257,13 @@ +
+
+

Sending message ... + +

+
+
{% endblock %} {% block javascripts_foot %} diff --git a/src/Sikofitt/Controller/ApiControllerProvider.php b/src/Sikofitt/Controller/ApiControllerProvider.php index bb47b20..e544c33 100644 --- a/src/Sikofitt/Controller/ApiControllerProvider.php +++ b/src/Sikofitt/Controller/ApiControllerProvider.php @@ -75,9 +75,9 @@ class ApiControllerProvider implements ControllerProviderInterface { // Set some validation constraints $constraints = [ 'contact' => new Collection([ - 'name' => [ + 'name' => [ new Length([ - 'min' => 4, + 'min' => 4, 'minMessage' => 'Name must be at least 4 characters.', ] ), @@ -86,7 +86,7 @@ class ApiControllerProvider implements ControllerProviderInterface { ] ), ], - 'email' => [ + 'email' => [ new Email([ 'message' => 'Invalid email', ]), @@ -96,16 +96,16 @@ class ApiControllerProvider implements ControllerProviderInterface { ], 'message' => [ new Length([ - 'min' => 20, + 'min' => 20, 'minMessage' => 'Message must be at least 20 characters.', ]), new NotBlank([ 'message' => 'Message must not be blank', ]), ], - '_token' => [ + '_token' => [ new EqualTo(['value' => $csrf, 'message' => 'Invalid token.']), - ] + ], ] ), ]; @@ -114,53 +114,64 @@ class ApiControllerProvider implements ControllerProviderInterface { $valid = $app['validator']->validate($contactFormData, new Collection($constraints)); - if(count($valid) > 0) { - $sanitizeProperty = function() use ($valid) { - return str_replace(['][', '[', ']'], ['_','',''], $valid[0]->getPropertyPath()); + if (count($valid) > 0) { + $sanitizeProperty = function () use ($valid) { + return str_replace(['][', '[', ']'], [ + '_', + '', + '', + ], $valid[0]->getPropertyPath()); }; return new JsonResponse([ - 'status' => 'error', + 'status' => 'error', 'message' => $valid[0]->getMessage(), - 'id' => $sanitizeProperty(), - 'const' => $valid[0]->getCode(), - 'code' => 256 + 'id' => $sanitizeProperty(), + 'const' => $valid[0]->getCode(), + 'code' => 256, ], 256); - } else { + } + else { $contactFormName = $contactFormData['contact']['name']; $contactFormEmail = $contactFormData['contact']['email']; $contactFormMessage = $contactFormData['contact']['message']; $request->getSession()->remove('_csrf/contact'); + $failures = ''; - try { - $app->mail(\Swift_Message::newInstance() + $sent = $app['mailer']->send(\Swift_Message::newInstance() ->setSubject('[Resume] Message') ->setFrom([$contactFormEmail => $contactFormName]) ->setTo($app->config('app.email')) ->setBody($contactFormMessage) - ); - $returnData = [ - 'status' => 'success', - 'message' => 'Message successfully sent.', - 'code' => 201, - 'data' => $contactFormData, - ]; - } catch(\Exception $e) { - $returnData = [ - 'status' => 'error', - 'message' => 'Could not send message.', - 'code' => 256, - 'data' => $e->getMessage(), - ]; + , $failures); + if($sent > 0) { + return new JsonResponse([ + 'status' => 'success', + 'message' => 'Message successfully sent.', + 'code' => 201, + 'data' => $contactFormData, + 'failed' => $failures, + 'sent' => $sent, + ], 200); + + } else { + return new JsonResponse([ + 'status' => 'error', + 'message' => 'There was an error sending the message.', + 'code' => 255, + 'data' => $contactFormData, + 'failed' => $failures, + 'sent' => $sent, + ], 255); + } } - return new JsonResponse($returnData, 201); - } })->method('GET|POST')->bind('api_message'); - $controllers->get('/v1/emailTest', function(Request $request) use ($app) { + + $controllers->get('/v1/emailTest', function (Request $request) use ($app) { try { $app->mail(\Swift_Message::newInstance() ->setSubject('[Resume] Message') @@ -168,10 +179,10 @@ class ApiControllerProvider implements ControllerProviderInterface { ->setTo('eric@ericwheeler.net') ->setBody('Testing message.') ); - } - catch(\Exception $e) { + } catch (\Exception $e) { dump($e->getMessage()); } + return new Response('Hello'); }); diff --git a/src/Sikofitt/js/resume.js b/src/Sikofitt/js/resume.js index 0c57109..ed08c2a 100644 --- a/src/Sikofitt/js/resume.js +++ b/src/Sikofitt/js/resume.js @@ -34,6 +34,8 @@ jq(document).ready(function (jq) { }); }); // Phone form jq('form[name=contact]').on('submit', function(event) { + contactModal = UIkit.modal('#spinner-modal', { modal:false, center:true, bgclose:false }); + contactModal.show(); jq.post(jq(this).attr('action'), jq(this).serialize(), function(response) { if(response.status !== 'success') { jq('#' + response.id).addClass('uk-form-danger'); @@ -54,6 +56,14 @@ jq(document).ready(function (jq) { $wrapper.empty().append($thankYouText).append($button); jq('a[href="#contact-form-wrapper"]').replaceWith('eric@ericwheeler.net'); } + + }).fail(function(response, code, resp) { + console.log(response); + console.log(code); + console.log(resp); + contactModal.hide(); + }).done(function () { + contactModal.hide(); }); }); diff --git a/web/index.php b/web/index.php index 8d1abd2..5e66ef7 100644 --- a/web/index.php +++ b/web/index.php @@ -9,12 +9,15 @@ * file that was distributed with this source code. */ + require_once __DIR__ . '/../vendor/autoload.php'; + $app = new App(); require_once $app->getAppDirectory() . '/providers.php'; $app->mount('/', new \Sikofitt\Controller\ResumeControllerProvider()); $app->mount('/api', new \Sikofitt\Controller\ApiControllerProvider()); + $app->run();