<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\HttpFoundation\Request;
//require 'vendor/autoload.php';
use GuzzleHttp\Client;
use Eccube\Repository\CustomerRepository;
use Eccube\Repository\Master\CustomerStatusRepository;
use Eccube\Service\MailService;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Eccube\Service\CartService;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\HttpFoundation\Cookie;
class CustomTopController extends AbstractController
{
private $client;
/**
* @var CustomerStatusRepository
*/
protected $customerStatusRepository;
/**
* @var ValidatorInterface
*/
protected $recursiveValidator;
/**
* @var MailService
*/
protected $mailService;
/**
* @var CustomerRepository
*/
protected $customerRepository;
/**
* @var EncoderFactoryInterface
*/
protected $encoderFactory;
/**
* @var TokenStorageInterface
*/
protected $tokenStorage;
/**
* @var \Eccube\Service\CartService
*/
protected $cartService;
private $authenticationManager;
/**
* EntryController constructor.
*
* @param CartService $cartService
* @param CustomerStatusRepository $customerStatusRepository
* @param MailService $mailService
* @param CustomerRepository $customerRepository
* @param EncoderFactoryInterface $encoderFactory
* @param ValidatorInterface $validatorInterface
* @param TokenStorageInterface $tokenStorage
* @param AuthenticationManagerInterface $authenticationManager An AuthenticationManager instance
*/
public function __construct(
CartService $cartService,
CustomerStatusRepository $customerStatusRepository,
MailService $mailService,
CustomerRepository $customerRepository,
EncoderFactoryInterface $encoderFactory,
ValidatorInterface $validatorInterface,
TokenStorageInterface $tokenStorage,
AuthenticationManagerInterface $authenticationManager
) {
$this->customerStatusRepository = $customerStatusRepository;
$this->mailService = $mailService;
$this->customerRepository = $customerRepository;
$this->encoderFactory = $encoderFactory;
$this->recursiveValidator = $validatorInterface;
$this->tokenStorage = $tokenStorage;
$this->cartService = $cartService;
$this->authenticationManager = $authenticationManager;
}
// public function __construct(HttpClientInterface $client)
// {
// $this->client = $client;
// }
/**
* @Route("/", name="homepage")
* @Template("index.twig")
* Unirest\Request::get($url, $headers = array(), $parameters = null)
* Unirest\Request::post($url, $headers = array(), $body = null)
* Unirest\Request::put($url, $headers = array(), $body = null)
* Unirest\Request::patch($url, $headers = array(), $body = null)
* Unirest\Request::delete($url, $headers = array(), $body = null)
*/
public function index(Request $request)
{
// $Customer = $this->getUser();
$token = $this->tokenStorage->getToken();
log_info('ログイン確認 ',["token" => $token]);
if(is_null($token)){
$Customer = $this->getUser();
}else{
$Customer = $token->getUser();
}
log_info('ログイン確認 ',["id" => $Customer]);
if(!$Customer){
if(isset($_COOKIE["nooVAJhmuvazsDiDliP"])){
$user_id = $_COOKIE["nooVAJhmuvazsDiDliP"];
$client = new Client();
$options = [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
"site_id" => 1,
"user_id" => $user_id
]
];
$url = getenv('API_KV_REGIST');
$response = $client->request('POST', $url, $options);
$res = json_decode($response->getBody());
if($res->status){
$options = [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
"site_id" => 1,
"user_id" => $user_id,
"password" => $res->password
]
];
$url = getenv('API_KV_LOGIN');
$response = $client->request('POST', $url, $options);
$res = json_decode($response->getBody());
if($res->status){
log_info('ログイン後処理1');
$Carts = $this->cartService->getCarts();
log_info('ログイン後処理2');
$qtyInCart = 0;
log_info('ログイン後処理3');
foreach ($Carts as $Cart) {
$qtyInCart += $Cart->getTotalQuantity();
}
log_info('ログイン後処理4');
//
$em = $this->entityManager;
$conn = $em->getConnection();
log_info('ログイン後処理5');
// 可逆変換
$aes_key = getenv("API_AES_KEY");
$user_id = openssl_decrypt($user_id,'aes-256-ecb',$aes_key);
$stmt = $conn->prepare('SELECT * FROM dtb_customer WHERE id = :id;');
$result = $stmt->execute([':id' => $user_id]);
log_info('ログイン後処理6');
$row = $result->fetch();
if ($row) {
log_info('ログイン後処理7',["id" => $row["id"]]);
$Customer = $this->customerRepository->find($row["id"]);
// $Customer
// ->setSalt($row["salt"])
// ->setPassword($row["password"])
// ->setSecretKey($row["secret_key"])
// ->setPoint($row["point"]);
log_info('ログイン後処理8',["Customer" => $Customer]);
$this->entityManager->persist($Customer);
$this->entityManager->flush();
$token = new UsernamePasswordToken($Customer, null, 'customer', ['ROLE_USER']);
$this->tokenStorage->setToken($token);
$request->getSession()->migrate(true);
log_info('ログイン後処理9',["token" => $token]);
if ($qtyInCart) {
$this->cartService->save();
}
log_info('ログイン済に変更', [$this->getUser()->getId()]);
}
}
}
}
}
return [];
}
}