<?php
namespace App\Controller;
use Pimcore;
use Pimcore\Model\DataObject\Customer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route("/account")
*/
class AccountController extends \Sintra\CMSCoreBundle\Controller\AccountController
{
/** @var TranslatorInterface */
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @Route("/password-change", methods={"GET"})
*
* @param Request $request
* @return void
*/
public function passwordChangeAction(Request $request)
{
$user = $this->getUser();
if ($user && ($user instanceof Customer)) {
$guid = uuid_create();
$user->setPasswordRecoveryToken($guid);
$user->save();
$language = $user->getCustomerLanguage();
if (empty($language) || !in_array($language, array("en", "it"))) {
$language = "en";
}
//generare email
$emailUrl = \Pimcore\Tool::getHostUrl("https") . '/password-restore' . '/' . $guid;
$mail = new \Pimcore\Mail();
$mail->to($user->getEmail());
$mail->subject("Kemon - ".$this->translator->trans("password_recovery", [], 'messages', $language));
$mail->html("<p>".$this->translator->trans("greetings", [], 'messages', $language)." " . $user->getFirstname() . ",</p>
<br>
<p>".$this->translator->trans("password_recovery_request", [], 'messages', $language)."</p>
<p>".$this->translator->trans("click_the_following_link", [], 'messages', $language)."</p>
<br><a href='$emailUrl'>$emailUrl</a><br>
<p>".$this->translator->trans("have_a_nice_day", [], 'messages', $language).",</p><br>
<p>".$this->translator->trans("kemon_team", [], 'messages', $language)."</p>");
$mail->send();
return $this->redirectToRoute('home', array('success' => "password_recovery_success"));
} else {
return $this->redirectToRoute('home', array('error' => "password_recovery_error"));
}
}
/**
* @Route("/password-modify", methods={"GET"})
*
* @param Request $request
* @return void
*/
public function passwordModifyAction(Request $request)
{
$user = $this->getUser();
if ($user && ($user instanceof Customer)) {
$language = "en";
return $this->render('account/passwordModify.html.twig', array(
'locale' => $language,
'languages' => $user->getLanguages(),
'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages', $language),
'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages', $language),
'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages', $language),
'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages', $language),
'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages', $language)
));
} else {
return $this->redirectToRoute('home');
}
}
/**
* @Route("/password-modify", methods={"POST"})
*/
public function passwordAccountModifyAction(Request $request)
{
$old_password = $request->get('old_password');
$new_password = $request->get('new_password');
$repeat_new_password = $request->get('repeat_new_password');
$user = $this->getUser();
if ($user && ($user instanceof Customer)) {
$errors = [];
$language = "en";
$error_old_password = "";
$errors_different_password = "";
if(!password_verify($old_password,$user->getPassword())){
$error_old_password = $this->translator->trans("error_old_password", [], 'messages', $language);
}
if ($new_password == $repeat_new_password) {
if (preg_match('/[A-Z]/', $new_password) == false) {
$errors[] = "upper_error";
}
if (preg_match('/[a-z]/', $new_password) == false) {
$errors[] = "lower_error";
}
if (preg_match('/[\'^£$%&*()}{@#~?><>,!|=_+¬-]/', $new_password) == false) {
$errors[] = "special_error";
}
if (preg_match('/[0-9]/', $new_password) == false) {
$errors[] = "number_error";
}
if (strlen($new_password) < 6) {
$errors[] = "six_error";
}
} else {
$errors_different_password = $this->translator->trans("error_password_different", [], 'messages', $language);
}
if (count($errors) > 0 || (empty($error_old_password) == false) || (empty($errors_different_password) == false)) {
return $this->render('account/passwordModify.html.twig', array(
'errors' => $errors,
'error_old_password' => $error_old_password,
'errors_different_password' => $errors_different_password,
'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages', $language),
'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages', $language),
'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages', $language),
'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages', $language),
'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages', $language),
'locale' => $language,
'languages' => $user->getLanguages()
));
} else {
$user->setPassword(strval($new_password));
$user->setPasswordRecoveryToken(null);
$user->save();
return $this->redirectToRoute('home');
}
} else {
return $this->redirectToRoute('home');
}
}
/**
* @Route("/password-restore", methods={"POST"}, name="password-restore-process")
*/
public function passwordRestoreAction(Request $request)
{
$guid = $request->get('guid');
$title = $request->get('title');
$new_password = $request->get('new_password');
$repeat_new_password = $request->get('repeat_new_password');
if (!(empty($guid))) {
$customer = Customer::getByPasswordRecoveryToken($guid, 1);
$language = "en";
$errors = [];
$errors_different_password = "";
if ($new_password == $repeat_new_password) {
if (preg_match('/[A-Z]/', $new_password) == false) {
$errors[] = "upper_error";
}
if (preg_match('/[a-z]/', $new_password) == false) {
$errors[] = "lower_error";
}
if (preg_match('/[\'^£$%&*()}{@#~?><>,!|=_+¬-]/', $new_password) == false) {
$errors[] = "special_error";
}
if (preg_match('/[0-9]/', $new_password) == false) {
$errors[] = "number_error";
}
if (strlen($new_password) < 6) {
$errors[] = "six_error";
}
} else {
$errors_different_password = $this->translator->trans("error_password_different", [], 'messages', $language);
}
if (count($errors) > 0 || (empty($errors_different_password) == false)) {
return $this->render('account/passwordRestore.html.twig', array(
'errors' => $errors,
'errors_different_password' => $errors_different_password,
'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages', $language),
'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages', $language),
'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages', $language),
'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages', $language),
'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages', $language),
'title' => $title,
'guid' => $guid,
'locale' => $language
));
} else {
$customer->setPassword(strval($new_password));
$customer->setPasswordRecoveryToken(null);
$customer->save();
return $this->render('account/login.html.twig', array(
'success' => $this->translator->trans("password_restored_msg", [], 'messages', $language),
'error' => "",
'locale' => $language
));
}
}
}
/**
* @Route("/password-recovery", methods={"POST"})
*
* @param Request $request
* @return void
*/
public function passwordAccountRecoveryAction(Request $request)
{
$email = $request->get('email');
$customerByEmail = Customer::getByEmail($email, 1);
if ($email && ($customerByEmail instanceof Customer)) {
$guid = uuid_create();
$customerByEmail->setPasswordRecoveryToken($guid);
$customerByEmail->save();
$language = $customerByEmail->getCustomerLanguage();
if (empty($language) || !in_array($language, array("en", "it"))) {
$language = "en";
}
//generare email
$emailUrl = \Pimcore\Tool::getHostUrl("https") . '/password-restore' . '/' . $guid;
$mail = new \Pimcore\Mail();
$mail->to($email);
$mail->subject("Kemon - ".$this->translator->trans("password_recovery", [], 'messages', $language));
$mail->html("<p>".$this->translator->trans("greetings", [], 'messages', $language)." " . $customerByEmail->getFirstname() . ",</p>
<br>
<p>".$this->translator->trans("password_recovery_request", [], 'messages', $language)."</p>
<p>".$this->translator->trans("click_the_following_link", [], 'messages', $language)."</p>
<br><a href='$emailUrl'>$emailUrl</a><br>
<p>".$this->translator->trans("have_a_nice_day", [], 'messages', $language).",</p><br>
<p>".$this->translator->trans("kemon_team", [], 'messages', $language)."</p>");
$mail->send();
return $this->render('account/passwordRecovery.html.twig', array(
'success' => $this->translator->trans("email_recovery_sent", [], 'messages', "en"),"locale"=>"en"));
} else {
return $this->render('account/passwordRecovery.html.twig', array(
'error' => $this->translator->trans("email_recovery_error", [], 'messages', "en"),"locale"=>"en"));
}
}
}