<?php
namespace Customize\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Repository\PluginRepository;
use Eccube\Service\Composer\ComposerServiceInterface;
use Eccube\Util\CacheUtil;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Eccube\Service\EntityProxyService;
use Eccube\Service\SchemaService;
use Eccube\Service\PluginApiService;
use Eccube\Service\SystemService;
use Eccube\Service\PluginContext;
use Eccube\Util\StringUtil;
//use Symfony\Bridge\Doctrine\RegistryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Eccube\Entity\Master\CustomerStatus;
use Eccube\Repository\Master\CustomerStatusRepository;
use Eccube\Repository\CustomerRepository;
class UpdateCustomerController
{
/**
* @var EccubeConfig
*/
protected $eccubeConfig;
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var PluginRepository
*/
protected $pluginRepository;
/**
* @var EntityProxyService
*/
protected $entityProxyService;
/**
* @var SchemaService
*/
protected $schemaService;
/**
* @var ComposerServiceInterface
*/
protected $composerService;
const VENDOR_NAME = 'ec-cube';
/**
* Plugin type/library of ec-cube
*/
const ECCUBE_LIBRARY = 1;
/**
* Plugin type/library of other (except ec-cube)
*/
const OTHER_LIBRARY = 2;
/**
* @var string %kernel.project_dir%
*/
private $projectRoot;
/**
* @var string %kernel.environment%
*/
private $environment;
/**
* @var ContainerInterface
*/
protected $container;
/** @var CacheUtil */
protected $cacheUtil;
/**
* @var PluginApiService
*/
private $pluginApiService;
/**
* @var SystemService
*/
private $systemService;
/**
* @var PluginContext
*/
private $pluginContext;
/**
* @var CustomerRepository
*/
protected $customerRepository;
/**
* PluginService constructor.
*
* @param EntityManagerInterface $entityManager
* @param PluginRepository $pluginRepository
* @param EntityProxyService $entityProxyService
* @param SchemaService $schemaService
* @param EccubeConfig $eccubeConfig
* @param ContainerInterface $container
* @param CacheUtil $cacheUtil
* @param ComposerServiceInterface $composerService
* @param PluginApiService $pluginApiService
* @param SystemService $systemService
* @param PluginContext $pluginContext
* @param RegistryInterface $registry
*
*/
public function __construct(
EntityManagerInterface $entityManager,
PluginRepository $pluginRepository,
EntityProxyService $entityProxyService,
SchemaService $schemaService,
EccubeConfig $eccubeConfig,
ContainerInterface $container,
CacheUtil $cacheUtil,
ComposerServiceInterface $composerService,
PluginApiService $pluginApiService,
SystemService $systemService,
PluginContext $pluginContext,
CustomerRepository $customerRepository
//RegistryInterface $registry
// ManagerRegistry $registry
) {
// parent::__construct($registry, Customer::class);
$this->entityManager = $entityManager;
$this->pluginRepository = $pluginRepository;
$this->entityProxyService = $entityProxyService;
$this->schemaService = $schemaService;
$this->eccubeConfig = $eccubeConfig;
$this->projectRoot = $eccubeConfig->get('kernel.project_dir');
$this->environment = $eccubeConfig->get('kernel.environment');
$this->container = $container;
$this->cacheUtil = $cacheUtil;
$this->composerService = $composerService;
$this->pluginApiService = $pluginApiService;
$this->systemService = $systemService;
$this->pluginContext = $pluginContext;
$this->customerRepository = $customerRepository;
}
/**
* @Route("/api/update")
* @Method("POST")
*/
public function getAction(Request $request)
{
$ip = getenv('API_KV_IP');
// $ip = "3.114.86.82";
// $clientip = $request->getClientIp();
// $ips = explode (".",$ip);
// $clientips = explode (".",$clientip);
// log_info('会員編集API ', ['HTTP_X_FORWARDED_FOR' => $_SERVER['HTTP_X_FORWARDED_FOR']]);
// log_info('会員編集API ', ['IP' => $ips,'IP2' => $clientips]);
// log_info('会員編集API ', ['IP' => $ips[0],'IP2' => $clientips[0]]);
// log_info('会員編集API ', ['is not *' => ($ips[0] !== '*')]);
// if($ips[0] !== "*"){
// if($ips[0] != $clientips[0]){
// log_info('会員編集API IPエラー1', ['IP' => $request->getClientIp()]);
// $return_code['status'] = false;
// $return_code['error_message'] = 'IPエラー1';
// return new Response(json_encode($return_code));
// }
// }
// if($ips[1] !== "*"){
// if($ips[1] != $clientips[1]){
// log_info('会員編集API IPエラー2', ['IP' => $request->getClientIp()]);
// $return_code['status'] = false;
// $return_code['error_message'] = 'IPエラー2';
// return new Response(json_encode($return_code));
// }
// }
// if($ips[2] !== "*"){
// if($ips[2] != $clientips[2]){
// log_info('会員編集API IPエラー3', ['IP' => $request->getClientIp()]);
// $return_code['status'] = false;
// $return_code['error_message'] = 'IPエラー3';
// return new Response(json_encode($return_code));
// }
// }
// if($ips[3] !== "*"){
// if($ips[3] != $clientips[3]){
// log_info('会員編集API IPエラー4', ['IP' => $request->getClientIp()]);
// $return_code['status'] = false;
// $return_code['error_message'] = 'IPエラー4';
// return new Response(json_encode($return_code));
// }
// }
// if ($_SERVER['HTTP_X_FORWARDED_FOR'] != $ip) {
if ($_SERVER['HTTP_X_FORWARDED_FOR'] != $ip) {
log_info('会員編集API IPエラー', ['IP' => $_SERVER['HTTP_X_FORWARDED_FOR']]);
$return_code['status'] = false;
$return_code['error_message'] = 'IPエラー';
return new Response(json_encode($return_code));
}
$content = $request->getContent();
$param = json_decode($content, true);
$em = $this->entityManager;
try{
$aes_key = getenv('API_AES_KEY');
log_info('会員編集API ', ['id' => $param["user_id"],'mail' => $param['mail_address'],'pass' => $param['password'],'salt' => $param['salt']]);
if(!empty($param['mail_address']) && !empty($param['user_id']) && !empty($param['password']) && !empty($param['salt'])){
$mail_address = openssl_decrypt($param["mail_address"],'aes-256-ecb',$aes_key);
$user_id = openssl_decrypt($param["user_id"],'aes-256-ecb',$aes_key);
$password = $param["password"];
$salt = $param["salt"];
}else{
$return_code['status'] = false;
$return_code['user_id'] = '';
$return_code['error_no'] = 4;
$return_code['message'] = '会員編集エラー()';
return new Response(json_encode($return_code));
}
log_info('会員編集API ', ['id' => $user_id,'mail' => $mail_address,'pass' => $password,'salt' => $salt]);
$em->getConnection()->beginTransaction();
$secret_Key = $this->customerRepository->getUniqueSecretKey();
// $secretkey = $this->getUniqueSecretKey();
$conn = $em->getConnection();
// 存在チェック
// $stmt = $conn->prepare('SELECT id FROM dtb_customer WHERE id = :id;');
// $stmt->execute([':id' => $user_id]);
// $row = $stmt->fetch();
$row = $this->customerRepository->findOneBy([
'id' => $user_id,
]);
if (!$row) {
$return_code['status'] = false;
$return_code['user_id'] = $param["user_id"];
$return_code['error_message'] = '会員は登録されていません。';
return new Response(json_encode($return_code));
}
$sth = $conn->prepare('UPDATE dtb_customer set email = :email, password = :password, salt = :salt, update_date = current_timestamp WHERE id = :id;');
$sth->execute([
':email' => $mail_address,
':password' => $password,
':salt' => $salt,
':id' => $user_id
]);
$em->getConnection()->commit();
$return_code['status'] = true;
$return_code['user_id'] = $param["user_id"];
$return_code['error_message'] = '';
} catch (\Exception $e) {
$return_code['status'] = false;
$return_code['user_id'] = '';
$return_code['error_message'] = '会員編集エラー('.$e.')';
$conn->rollback();
}
return new Response(json_encode($return_code));
}
/**
* ユニークなシークレットキーを返す.
*
* @return string
*/
// public function getUniqueSecretKey()
// {
// $em2 = $this->entityManager;
// $conn2 = $em2->getConnection();
// do {
// $key = StringUtil::random(32);
// $stmt2 = $conn2->prepare('SELECT id FROM dtb_customer WHERE secret_key = :secretkey;');
// $stmt2->execute([':secretkey' => $key]);
// $Customer = $stmt2->fetch();
// } while ($Customer);
// return $key;
// }
}