diff --git a/bootstrap/web/middleware.php b/bootstrap/web/middleware.php deleted file mode 100644 index 411c15b..0000000 --- a/bootstrap/web/middleware.php +++ /dev/null @@ -1,3 +0,0 @@ -add(new ProVM\KI\Common\Middleware\Visits($app->getContainer()->get('file.visits'), $app->getContainer()->get('visits.time'))); diff --git a/bootstrap/web/setup.php b/bootstrap/web/setup.php index 4ea82b9..1c43856 100644 --- a/bootstrap/web/setup.php +++ b/bootstrap/web/setup.php @@ -60,8 +60,24 @@ return [ $segmentos = json_decode(trim(file_get_contents($filename))); return $segmentos; }, - Slim\Views\Blade::class => function(Container $c) { - return new Slim\Views\Blade( + 'email' => function(Container $c) { + $filename = implode(DIRECTORY_SEPARATOR, [ + $c->get('folders.data'), + 'emails.json' + ]); + $emails = json_decode(trim(file_get_contents($filename))); + return $emails->settings; + }, + 'emails' => function(Container $c) { + $filename = implode(DIRECTORY_SEPARATOR, [ + $c->get('folders.data'), + 'emails.json' + ]); + $emails = json_decode(trim(file_get_contents($filename))); + return $emails->emails; + }, + ProVM\KI\Common\Alias\View::class => function(Container $c) { + return new ProVM\KI\Common\Implementation\View( $c->get('folders.templates'), $c->get('folders.cache'), null, @@ -91,5 +107,15 @@ return [ $c->get('folders.data'), $c->get('folders.public') ); + }, + ProVM\KI\Common\Alias\Message::class => function(Container $c) { + return new ProVM\KI\Common\Implementation\Message(); + }, + ProVM\KI\Common\Alias\Mailer::class => function(Container $c) { + return new ProVM\KI\Common\Implementation\Mailer([ + 'host' => $c->get('email')->host->name, + 'username' => $c->get('email')->user->name, + 'password' => $c->get('email')->user->password + ]); } ]; diff --git a/common/Alias/Mailer.php b/common/Alias/Mailer.php new file mode 100644 index 0000000..1c6563d --- /dev/null +++ b/common/Alias/Mailer.php @@ -0,0 +1,6 @@ +render($response, 'contacto'); } + public function enviar(Request $request, Response $response, Container $container, Mailer $mailer): Response { + $post = $request->getParsedBody(); + $body = []; + $filename = implode(DIRECTORY_SEPARATOR, [ + $container->get('folders.templates'), + 'contacto', + 'email.php' + ]); + $html = file_get_contents($filename); + foreach ($post as $key => $val) { + $body []= ucwords($key) . ': ' . $val; + $html = str_replace('{{$' . $key . '}}', $val, $html); + } + $body = implode(PHP_EOL, $body); + $subject = 'Contacto Web - ' . $post['nombre']; + + $message = $container->make(Message::class) + ->setFrom($container->get('emails')[0]) + ->addTo($container->get('emails')[0]) + ->addCc($container->get('emails')[1]) + ->setSubject($subject) + ->setBody($body) + ->setHTMLBody($html); + $status = $mailer->send($message); + $message = $container->make(Message::class) + ->setFrom($container->get('emails')[0]) + ->addTo($post['mail'], $post['nombre']) + ->setSubject($subject) + ->setBody('Su correo a CapitalInvestments ha sido recibido.'); + $mailer->send($message); + + $output = [ + 'informacion' => $post, + 'estado' => $status, + 'mail' => [ + 'from' => $post['mail'], + 'to' => $container->get('emails')[0], + 'cc' => $container->get('emails')[1], + 'asunto' => $subject, + 'mensaje' => $body, + 'html' => $html + ] + ]; + $response->getBody()->write(json_encode($output)); + return $response + ->withHeader('Content-Type', 'application/json') + ->withStatus(201); + } } diff --git a/common/Controller/Web/Faq.php b/common/Controller/Web/Faq.php index 7867771..5b95e90 100644 --- a/common/Controller/Web/Faq.php +++ b/common/Controller/Web/Faq.php @@ -4,7 +4,7 @@ namespace ProVM\KI\Common\Controller\Web; use Psr\Container\ContainerInterface as Container; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; -use Slim\Views\Blade as View; +use ProVM\KI\Common\Alias\View; class Faq { public function __invoke(Request $request, Response $response, View $view, Container $container) { diff --git a/common/Controller/Web/Nosotros.php b/common/Controller/Web/Nosotros.php index af9ea8b..5ae767a 100644 --- a/common/Controller/Web/Nosotros.php +++ b/common/Controller/Web/Nosotros.php @@ -4,7 +4,7 @@ namespace ProVM\KI\Common\Controller\Web; use Psr\Container\ContainerInterface as Container; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; -use Slim\Views\Blade as View; +use ProVM\KI\Common\Alias\View; class Nosotros { public function __invoke(Request $request, Response $response, View $view, Container $container) { diff --git a/common/Controller/Web/Productos.php b/common/Controller/Web/Productos.php index 0669da6..873595b 100644 --- a/common/Controller/Web/Productos.php +++ b/common/Controller/Web/Productos.php @@ -4,7 +4,7 @@ namespace ProVM\KI\Common\Controller\Web; use Psr\Container\ContainerInterface as Container; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; -use Slim\Views\Blade as View; +use ProVM\KI\Common\Alias\View; use ProVM\Common\Factory\Model as ModelFactory; use ProVM\KI\Producto; diff --git a/common/Implementation/Mailer.php b/common/Implementation/Mailer.php new file mode 100644 index 0000000..5b8fa1c --- /dev/null +++ b/common/Implementation/Mailer.php @@ -0,0 +1,102 @@ +host = $settings['host']; + if (!isset($settings['username'])) { + throw new \Exception('User name is missing in ' . get_called_class()); + } + $this->user = (object) ['name' => null, 'password' => null]; + $this->user->name = $settings['username']; + if (!isset($settings['password'])) { + throw new \Exception('User password is missing in ' . get_called_class()); + } + $this->user->password = $settings['password']; + $this->port = 25; + if (isset($settings['secure']) and $settings['secure'] == 'ssl') { + $this->is_secure = true; + $this->port = 465; + } + if (isset($settings['port'])) { + $this->port = $settings['port']; + } + } + public function isSecure(): bool { + return $this->is_secure; + } + public function send(Message $message): bool { + $mailer = new BaseMailer(); + $mailer->isSMTP(); + //$mailer->SMTPDebug = SMTP::DEBUG_SERVER; + $mailer->Host = $this->host; + $mailer->SMTPAuth = true; + $mailer->Username = $this->user->name; + $mailer->Password = $this->user->password; + $mailer->Port = $this->port; + if ($this->isSecure()) { + $mailer->SMTPSecure = BaseMailer::ENCRYPTION_STARTTLS; + } + + try { + $from = $message->getFrom(); + if ($from->name !== null) { + $mailer->setFrom($from->email, $from->name); + } else { + $mailer->setFrom($from->email); + } + foreach ($message->getTo() as $to) { + if ($to->name !== null) { + $mailer->addAddress($to->email, $to->name); + } else { + $mailer->addAddress($to->email); + } + } + $ccs = $message->getCc(); + if (!empty($ccs)) { + foreach ($ccs as $cc) { + if ($cc->name !== null) { + $mailer->addCC($cc->email, $cc->name); + } else { + $mailer->addCC($cc->email); + } + } + } + $bccs = $message->getBcc(); + if (!empty($bccs)) { + foreach ($bccs as $bcc) { + if ($bcc->name !== null) { + $mailer->addBCC($bcc->email, $bcc->name); + } else { + $mailer->addBCC($bcc->email); + } + } + } + $mailer->Subject = $message->getSubject(); + if ($message->isHtml()) { + $mailer->Body = $message->getHtmlBody(); + $mailer->isHTML(); + $mailer->AltBody = $message->getBody(); + } else { + $mailer->Body = $message->getBody(); + } + + $mailer->send(); + return true; + } catch (\Exception $e) { + return false; + } + } +} diff --git a/common/Implementation/Message.php b/common/Implementation/Message.php new file mode 100644 index 0000000..85b2644 --- /dev/null +++ b/common/Implementation/Message.php @@ -0,0 +1,100 @@ +from = $this->formatEmail($email, $name); + return $this; + } + public function getFrom() { + return $this->from; + } + protected $reply_to; + public function addReplyTo(string $email, string $name = null): Message { + if ($this->reply_to === null) { + $this->reply_to = []; + } + $this->reply_to []= $this->formatEmail($email, $name); + return $this; + } + public function getReplyTo() { + return $this->reply_to; + } + protected $subject; + public function setSubject(string $subject): Message { + $this->subject = $subject; + return $this; + } + public function getSubject(): string { + return $this->subject; + } + protected $to; + public function addTo(string $email, string $name = null): Message { + if ($this->to === null) { + $this->to = []; + } + $this->to []= $this->formatEmail($email, $name); + return $this; + } + public function getTo(): array { + return $this->to; + } + protected $cc; + public function addCc(string $email, string $name = null): Message { + if ($this->cc === null) { + $this->cc = []; + } + $this->cc []= $this->formatEmail($email, $name); + return $this; + } + public function getCc() { + return $this->cc; + } + protected $bcc; + public function addBcc(string $email, string $name = null): Message { + if ($this->bcc === null) { + $this->bcc = []; + } + $this->bcc []= $this->formatEmail($email, $name); + return $this; + } + public function getBcc() { + return $this->bcc; + } + protected $body; + public function setBody(string $body): Message { + $this->body = $body; + return $this; + } + public function getBody(): string { + return $this->body; + } + protected $html; + protected $is_html = false; + public function setHtmlBody(string $body): Message { + $this->html = $body; + $this->is_html = true; + return $this; + } + public function getHtmlBody(): string { + return $this->html; + } + public function isHtml(): bool { + return $this->is_html; + } + + protected function formatEmail(string $email, string $name = null) { + if (!$name && preg_match('#^(.+) +<(.*)>$#D', $email, $matches)) { + [, $name, $email] = $matches; + $name = stripslashes($name); + $tmp = substr($name, 1, -1); + if ($name === '"' . $tmp . '"') { + $name = $tmp; + } + } + return (object) ['name' => $name, 'email' => $email]; + } +} diff --git a/common/Implementation/View.php b/common/Implementation/View.php new file mode 100644 index 0000000..0dc939f --- /dev/null +++ b/common/Implementation/View.php @@ -0,0 +1,7 @@ +get('/contacto', Contacto::class); +$app->post('/contacto', [Contacto::class, 'enviar']); diff --git a/resources/views/contacto/email.php b/resources/views/contacto/email.php new file mode 100644 index 0000000..078244f --- /dev/null +++ b/resources/views/contacto/email.php @@ -0,0 +1,39 @@ + +
+ ++ Nombre + | ++ {{$nombre}} + | +|||||
+ Email + | ++ {{$mail}} + | +|||||
+ Mensaje + | ++ {{$mensaje}} + | +