src/Controller/ApiPluginController.php line 105

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AuctionAccount;
  4. use App\Entity\AuctionBid;
  5. use App\Entity\PluginEventLog;
  6. use App\Entity\PluginLotData;
  7. use App\Entity\PluginPing;
  8. use App\Entity\PluginToken;
  9. use App\Entity\PluginUser;
  10. use App\Entity\PluginUserAuctionAccountAssigment;
  11. use App\Entity\ProxyAccount;
  12. use App\Exception\Plugin\BadRequestException;
  13. use App\Exception\Plugin\InvalidTokenException;
  14. use App\Repository\AccountCodeRepository;
  15. use App\Form\PluginUserType;
  16. use App\Repository\PluginTokenRepository;
  17. use App\Repository\PluginUserRepository;
  18. use App\Service\Utils\CommonUtils;
  19. use App\Service\Utils\FileUploader;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\HttpFoundation\File\UploadedFile;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. /**
  30.  * @Route("/api_plugin", name="api_plugin_")
  31.  */
  32. class ApiPluginController extends AbstractController
  33. {
  34.     const STATIC_VPN_CONFIG 'CmNsaWVudApub2JpbmQKZGV2IHR1bgpyZW1vdGUtY2VydC10bHMgc2VydmVyCgpyZW1vdGUgNTEuODMuMjQ5LjU4IDExOTQgdWRwCgo8a2V5PgotLS0tLUJFR0lOIFBSSVZBVEUgS0VZLS0tLS0KTUlJRXZ3SUJBREFOQmdrcWhraUc5dzBCQVFFRkFBU0NCS2t3Z2dTbEFnRUFBb0lCQVFERFo1TmpLTWhrSkNsRgo0QThudmYwNHJNWHNVNkN3VEJiQnF4dnZmSVVqekF5VkQzWWE5UE8zWllMTkFOR3F5anI1OWFaZDR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.         'https://copart.com',
  38.         'https://www.copart.com',
  39.         ];
  40.     /**
  41.      * @var FileUploader
  42.      */
  43.     private $fileUploader;
  44.     /**
  45.      * @var TranslatorInterface
  46.      */
  47.     private $translator;
  48.     public function __construct(TranslatorInterface $translatorFileUploader $fileUploader)
  49.     {
  50.         $this->translator $translator;
  51.         $this->fileUploader $fileUploader;
  52.     }
  53.     /**
  54.      * @Route("/headers_sh", name="headers_show", methods={"GET, POST"})
  55.      * @param Request $request
  56.      * @return Response
  57.      */
  58.     public function headers_show(Request $request): Response
  59.     {
  60.         //$headers = $request->headers->all();
  61.         //dd($headers);
  62.         return new JsonResponse([], 200);
  63.     }
  64.     /**
  65.      * @param PluginToken $token
  66.      * @return void
  67.      */
  68.     private function disableToken(PluginToken $token)
  69.     {
  70.         $token
  71.             ->setStatus('off')
  72.             ->setDisabledAt(new \DateTime('now'));
  73.         $em $this->getDoctrine()->getManager();
  74.         $em->persist($token);
  75.         $em->flush();
  76.     }
  77.     /**
  78.      * @Route("/user_auth", name="user_auth", methods={"POST"})
  79.      * @return Response
  80.      */
  81.     public function pluginUserAuth(PluginTokenRepository $pluginTokenRepositoryPluginUserRepository $pluginUserRepositoryRequest $request): Response
  82.     {
  83.         $proxy = [];
  84.         $parameters json_decode($request->getContent(), true);
  85.         if (empty($parameters) || empty($parameters['login']) || empty($parameters['password'])) {
  86.             header(self::HEADERS);
  87.             return new JsonResponse('Bad Request'400);
  88.         }
  89.         if (empty($parameters['pv'])) {
  90.             header(self::HEADERS);
  91.             return new JsonResponse('Unknown Application'400);
  92.         }
  93.         $pluginUser $pluginUserRepository->findOneBy(['login' => $parameters['login'],'deleted' => 0]);
  94.         $entityManager $this->getDoctrine()->getManager();
  95.         if (!$pluginUser) {
  96.             header(self::HEADERS);
  97.             return new JsonResponse('Invalid login'401);
  98.         } elseif ($parameters['password'] != $pluginUser->getPassword()) {
  99.             header(self::HEADERS);
  100.             return new JsonResponse('Invalid password'401);
  101.         }
  102.         if (!$pluginUser->getOwner()->getEnablePlugin()){
  103.             header(self::HEADERS);
  104.             return new JsonResponse('Plugin disabled'401);
  105.         }
  106.         if(!in_array($pluginUser->getOwner()->getId(), [15410] )) { //developer, Katerina, Nikita
  107.             if (!preg_match('/^.*((app)|(android)|(ios)|(windows)|(mac)).*/i'$parameters['pv'], $matches) || !$matches) {
  108.                 header(self::HEADERS);
  109.                 return new JsonResponse('Access Denied'403);
  110.             }
  111.         }
  112. //        $onPluginTokens = $pluginTokenRepository->findBy(['plugin_user' => $pluginUser, 'status' => 'on']);
  113. //        if ($onPluginTokens) {
  114. //            foreach ($onPluginTokens as $onPluginToken) {
  115. //                $onPluginToken
  116. //                    ->setStatus('off')
  117. //                    ->setDisabledAt(new \DateTime('now'));
  118. //                $entityManager->persist($onPluginToken);
  119. //                $entityManager->flush();
  120. //            }
  121. //        }
  122.         $is_token true;
  123.         while ($is_token) {
  124.             $token substr(hash('sha256'mt_rand()), 040);
  125.             $is_token $pluginTokenRepository->findOneBy(['token' => $token]);
  126.         }
  127.         $pluginToken = new PluginToken();
  128.         $pluginToken
  129.             ->setToken($token)
  130.             ->setPluginUser($pluginUser);
  131.         $entityManager->persist($pluginToken);
  132.         $entityManager->flush();
  133.         $customer $pluginUser->getCustomer();
  134.         $owner $pluginUser->getOwner();
  135.         if ($customer) {
  136.             $id $customer->getId();
  137.             $name $customer->getUsername();
  138.         } else {
  139.             $id $owner->getId();
  140.             $name $owner->getUsername();
  141.         }
  142.         if(isset($parameters['auction']) && $parameters['auction'] == 'manheim') {
  143.             $AuctionAccounts $pluginToken->getPluginUser()->getOnlyManheimAccounts();
  144.             $count count($AuctionAccounts);
  145.             if ($count 1) {
  146.                 header(self::HEADERS);
  147.                 return new JsonResponse('Invalid Manheim accounts'401);
  148.             }
  149.             $em $this->getDoctrine()->getManager();
  150.             $proxyAccount $em->getRepository(ProxyAccount::class)->findLastUsedManheimProxy();
  151.             // $key = random_int(0, $count - 1);
  152.             // $auctionAccount = $AuctionAccounts[$key];
  153. //        $login = $auctionAccount->getLogin();
  154. //        $password = $auctionAccount->getPassword();
  155.             // $proxyAccount = $auctionAccount->getProxyAccount();
  156.             if ($proxyAccount) {
  157.                 $proxy = [
  158.                     'proxy' => [
  159.                         'host' => $proxyAccount->getProxyHost() ?? '',
  160.                         'port' => $proxyAccount->getProxyPort() ?? '',
  161.                         'username' => $proxyAccount->getProxyUsername() ?? '',
  162.                         'password' => $proxyAccount->getProxyPassword() ?? '',
  163.                     ]
  164.                 ];
  165.                 $proxyAccount->setLastUsedAt(new \DateTime('now'));
  166.                 $em->persist($proxyAccount);
  167.                 $em->flush();
  168.             }
  169.         }
  170.         $data = [
  171.             'id' => $pluginUser->getId(),
  172.             'name' => $name,
  173.             'token' => $token,
  174.             'allowLogging' => true,
  175.         ];
  176.         if(!empty($proxy)) {
  177.             $data array_merge($data$proxy);
  178.         }
  179.         header(self::HEADERS);
  180.         return new JsonResponse($data200);
  181.     }
  182.     /**
  183.      * @param EntityManagerInterface $em
  184.      * @param Request $request
  185.      * @return PluginToken
  186.      * @throws BadRequestException
  187.      * @throws InvalidTokenException
  188.      */
  189.     private function _authUserByToken(EntityManagerInterface $emRequest $request): PluginToken
  190.     {
  191.         $pluginTokenRepository $em->getRepository(PluginToken::class);
  192.         $parameters json_decode($request->getContent(), true);
  193.         if(!empty($parameters['user'])) { // if request is from universal APP
  194.             $parameters $parameters['user'];
  195.         }
  196.         if (empty($parameters) || empty($parameters['id']) || empty($parameters['name']) || empty($parameters['token']) || empty($parameters['pv'])) {
  197.             throw new BadRequestException();
  198.         }
  199.         $pluginToken $pluginTokenRepository->findOneBy(['token' => $parameters['token'], 'status' => 'on']);
  200.         if (!$pluginToken instanceof PluginToken) {
  201.             throw new InvalidTokenException();
  202.         }
  203.         if(!$pluginToken->getPluginUser()->getOwner()->getEnablePlugin()){
  204.             $this->disableToken($pluginToken);
  205.             throw new InvalidTokenException();
  206.         }
  207.         if($pluginToken->getPluginUser()->getDeleted()){
  208.             $this->disableToken($pluginToken);
  209.             throw new InvalidTokenException();
  210.         }
  211.         if(!in_array($pluginToken->getPluginUser()->getOwner()->getId(), [15410] )) { //developer, Katerina, Nikita
  212.             if (!preg_match('/^.*((app)|(android)|(ios)|(windows)|(mac)).*/i'$parameters['pv'], $matches) || !$matches) {
  213.                 $this->disableToken($pluginToken);
  214.                 throw new InvalidTokenException();
  215.             }
  216.         }
  217.         return $pluginToken;
  218.     }
  219.     /**
  220.      * @Route("/get_copart_account", name="get_copart_account", methods={"POST"})
  221.      * @param Request $request
  222.      * @param EntityManagerInterface $em
  223.      * @return Response
  224.      */
  225.     public function getCopartAccount(Request $requestEntityManagerInterface $em): Response
  226.     {
  227.         try {
  228.             $pluginToken $this->_authUserByToken($em$request);
  229.         } catch (BadRequestException $e) {
  230.             header(self::HEADERS);
  231.             return new JsonResponse('Bad Request'400);
  232.         } catch (InvalidTokenException $e) {
  233.             header(self::HEADERS);
  234.             return new JsonResponse('Invalid token'401);
  235.         }
  236.         $parameters json_decode($request->getContent(), true);
  237.         $plugin_version $parameters['pv'] ?? '';
  238.         $allValue $parameters['all'] ?? false;
  239.         $all is_bool($allValue)
  240.             ? $allValue
  241.             : (is_int($allValue)
  242.                 ? $allValue === 1
  243.                 : (is_string($allValue) && in_array(strtolower(trim($allValue)), ['1''true''yes'], true)));
  244.         if (empty($plugin_version)) {
  245.             header(self::HEADERS);
  246.             return new JsonResponse('Invalid pv'400);
  247.         }
  248.         $ip CommonUtils::getClientIP($request);
  249.         $pluginUser $pluginToken->getPluginUser();
  250.         $AuctionAccounts $pluginUser->getOnlyCopartAccounts();
  251.         if (count($AuctionAccounts) < 1) {
  252.             header(self::HEADERS);
  253.             return new JsonResponse('Invalid Copart accounts'401);
  254.         }
  255.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  256.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  257.         if ($all) {
  258.             $allCopartAccounts = [];
  259.             foreach ($AuctionAccounts as $auctionAccount) {
  260.                 $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  261.                 $accountData = [
  262.                     'id' => $auctionAccount->getId(),
  263.                     'auction_account_id' => $auctionAccount->getId(),
  264.                     'account_name' => $auctionAccount->getCommonName() ?? '',
  265.                     'login' => $auctionAccount->getLogin() ?? '',
  266.                     'password' => $auctionAccount->getPassword() ?? '',
  267.                     'can_manage_payments' => $canManagePayments,
  268.                 ];
  269.                 $proxyAccount $auctionAccount->getProxyAccount();
  270.                 $proxy = [];
  271.                 if ($proxyAccount) {
  272.                     $proxy = [
  273.                         'proxy' => [
  274.                             'host' => $proxyAccount->getProxyHost() ?? '',
  275.                             'port' => $proxyAccount->getProxyPort() ?? '',
  276.                             'username' => $proxyAccount->getProxyUsername() ?? '',
  277.                             'password' => $proxyAccount->getProxyPassword() ?? '',
  278.                         ]
  279.                     ];
  280.                 }
  281.                 if (!empty($proxy)) {
  282.                     $accountData['proxy'] = $proxy['proxy'];
  283.                 }
  284.                 $allCopartAccounts[] = $accountData;
  285.             }
  286.             header(self::HEADERS);
  287.             return new JsonResponse(['copart_accounts' => $allCopartAccounts], 200);
  288.         }
  289.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  290.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  291.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  292.         $proxyAccount $auctionAccount->getProxyAccount();
  293.         $proxy = [];
  294.         if ($proxyAccount) {
  295.             $proxy = [
  296.                 'proxy' => [
  297.                     'host' => $proxyAccount->getProxyHost() ?? '',
  298.                     'port' => $proxyAccount->getProxyPort() ?? '',
  299.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  300.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  301.                 ]
  302.             ];
  303.         }
  304.         $data = [
  305.             'copart' => [
  306.                 'id' => $pluginAuctionAssignment->getAuctionAccount()->getId(),
  307.                 'account_name' => $pluginAuctionAssignment->getAuctionAccount()->getCommonName() ?? '',
  308.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  309.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  310.             ],
  311.             'assigment_id' => $pluginAuctionAssignment->getId(),
  312.             'can_manage_payments' => $canManagePayments,
  313.         ];
  314.         header(self::HEADERS);
  315.         return new JsonResponse(array_merge($data$proxy), 200);
  316.     }
  317.     /**
  318.      * @Route("/bid_clicked", name="bid_clicked", methods={"POST"})
  319.      * @param Request $request
  320.      * @param EntityManagerInterface $em
  321.      * @return Response
  322.      */
  323.     public function bidClicked(Request $requestEntityManagerInterface $em): Response
  324.     {
  325.         try {
  326.             $pluginToken $this->_authUserByToken($em$request);
  327.         } catch (BadRequestException $e) {
  328.             header(self::HEADERS);
  329.             return new JsonResponse('Bad Request'400);
  330.         } catch (InvalidTokenException $e) {
  331.             header(self::HEADERS);
  332.             return new JsonResponse('Invalid token'401);
  333.         }
  334.         $parameters json_decode($request->getContent(), true);
  335.         $lot $parameters['lot'] ?? '';
  336.         $bid $parameters['bid'] ?? false;
  337.         $auctionLocation mb_substr(trim($parameters['auction_location'] ?? ''), 060);
  338.         $bid_type strtolower($parameters['bid_type']) ?? '';
  339.         $session_url $parameters['session_url'] ?? '';
  340.         $window_host $parameters['window_host'] ?? '';
  341.         $loggedCopart =
  342.             $parameters['loggedCopart'] ??
  343.             $parameters['loggedIaai'] ??
  344.             $parameters['loggedManheim'] ??
  345.             $parameters['loggedBringatrailer'] ??
  346.             $parameters['loggedCars_and_bids'] ??
  347.             $parameters['loggedCars'] ??
  348.             $parameters['loggedCargurus'] ?? false;
  349.         if (empty($lot)) {
  350.             header(self::HEADERS);
  351.             return new JsonResponse('Invalid lot'400);
  352.         }
  353.         if ($bid === false) {
  354.             header(self::HEADERS);
  355.             return new JsonResponse('Invalid bid'400);
  356.         }
  357.         if(empty($bid_type) || !in_array($bid_typeAuctionBid::TYPES)){
  358.             header(self::HEADERS);
  359.             return new JsonResponse('Invalid bid_type'400);
  360.         }
  361.         if (empty($parameters['assigment_id'])) {
  362.             header(self::HEADERS);
  363.             return new JsonResponse('Invalid assigment'406);
  364.         }
  365.         $assignedAuctionAccount $em->getRepository(PluginUserAuctionAccountAssigment::class)->find((int)$parameters['assigment_id']);
  366.         if (!$assignedAuctionAccount instanceof PluginUserAuctionAccountAssigment) {
  367.             header(self::HEADERS);
  368.             return new JsonResponse('Invalid assigment'406);
  369.         }
  370.         /******* check iaai country *********/
  371.         /*$auctionAccount = $assignedAuctionAccount->getAuctionAccount();
  372.         if (strtolower($auctionAccount->getAuctionName()) === 'iaai') {
  373.             if (!empty($window_host)) {
  374.                 if (($window_host === 'www.iaai.com') && ($auctionAccount->getCountry() === 'usa')) {
  375.                     true;
  376.                 } elseif (($window_host === 'ca.iaai.com') && ($auctionAccount->getCountry() === 'canada')) {
  377.                     true;
  378.                 } else {
  379.                     header(self::HEADERS);
  380.                     return new JsonResponse('Invalid country in window_host', 406);
  381.                 }
  382.             } else {
  383.                 header(self::HEADERS);
  384.                 return new JsonResponse('Invalid window_host', 406);
  385.             }
  386.         }*/
  387.         /******* end *********/
  388.         $auctionBid = (new AuctionBid())
  389.             ->setCreatedAt(new \DateTime('now'))
  390.             ->setAssignation($assignedAuctionAccount)
  391.             ->setBid((float)$bid)
  392.             ->setLot($lot)
  393.             ->setLoggedCopart((bool)$loggedCopart)
  394.             ->setType($bid_type)
  395.             ->setSessionUrl($session_url)
  396.             ->setAuctionLocation($auctionLocation);
  397.         $em->persist($auctionBid);
  398.         $em->flush();
  399.         header(self::HEADERS);
  400.         return new JsonResponse(['history_id' => $auctionBid->getId()], 200);
  401.     }
  402.     /**
  403.      * @Route("/get_assigment_finances_access", name="assigment_finances_access", methods={"POST"})
  404.      * @param Request $request
  405.      * @param EntityManagerInterface $em
  406.      * @return Response
  407.      */
  408.     public function getAssigmentFinancesAccess(Request $requestEntityManagerInterface $em): Response
  409.     {
  410.         try {
  411.             $pluginToken $this->_authUserByToken($em$request);
  412.         } catch (BadRequestException $e) {
  413.             header(self::HEADERS);
  414.             return new JsonResponse('Bad Request'400);
  415.         } catch (InvalidTokenException $e) {
  416.             header(self::HEADERS);
  417.             return new JsonResponse('Invalid token'401);
  418.         }
  419.         $parameters json_decode($request->getContent(), true);
  420.         $assigmentId $parameters['assigment_id'] ?? null;
  421.         if (empty($assigmentId)) {
  422.             header(self::HEADERS);
  423.             return new JsonResponse('Invalid assigment'406);
  424.         }
  425.         $pluginUser $pluginToken->getPluginUser();
  426.         $assignedAuctionAccount $em->getRepository(PluginUserAuctionAccountAssigment::class)->find((int)$parameters['assigment_id']);
  427.         if (!$assignedAuctionAccount instanceof PluginUserAuctionAccountAssigment
  428.             || $assignedAuctionAccount->getPluginUser()->getId() !== $pluginUser->getId()
  429.         ) {
  430.             header(self::HEADERS);
  431.             return new JsonResponse('Invalid assigment'406);
  432.         }
  433.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  434.         $canManageFinances $accountFinancesAccesses[$assignedAuctionAccount->getAuctionAccount()->getId()] ?? false;
  435.         header(self::HEADERS);
  436.         return new JsonResponse(['can_manage_finances' => $canManageFinances], 200);
  437.     }
  438.     /**
  439.      * @Route("/ping", name="ping", methods={"POST"})
  440.      * @param Request $request
  441.      * @param EntityManagerInterface $em
  442.      * @return Response
  443.      */
  444.     public function ping(Request $requestEntityManagerInterface $em): Response
  445.     {
  446.         try {
  447.             $pluginToken $this->_authUserByToken($em$request);
  448.         } catch (BadRequestException $e) {
  449.             header(self::HEADERS);
  450.             return new JsonResponse('Bad Request'400);
  451.         } catch (InvalidTokenException $e) {
  452.             header(self::HEADERS);
  453.             return new JsonResponse('Invalid token'401);
  454.         }
  455.         $parameters json_decode($request->getContent(), true);
  456.         $assigment_id $parameters['assigment_id'] ?? '';
  457.         $token $parameters['token'] ?? '';
  458.         $loggedCopart =
  459.             $parameters['loggedCopart'] ??
  460.             $parameters['loggedIaai'] ??
  461.             $parameters['loggedManheim'] ??
  462.             $parameters['loggedBringatrailer'] ??
  463.             $parameters['loggedCars_and_bids'] ??
  464.             $parameters['loggedCars'] ??
  465.             $parameters['loggedCargurus'] ?? false;
  466.         $loggedPlugin $parameters['loggedPlugin'] ?? '';
  467. //        $date = $parameters['date'] ?? '';
  468.         if (empty($assigment_id)) {
  469.             header(self::HEADERS);
  470.             return new JsonResponse('Empty assigment_id'400);
  471.         }
  472.         if (empty($token)) {
  473.             header(self::HEADERS);
  474.             return new JsonResponse('Empty token'406);
  475.         }
  476. //        if (empty($loggedCopart)) {
  477. //            return new JsonResponse('Empty loggedCopart', 406);
  478. //        }
  479. //        if (empty($loggedPlugin)) {
  480. //            return new JsonResponse('Empty loggedPlugin', 406);
  481. //        }
  482. //        if (empty($date)) {
  483. //            return new JsonResponse('Empty date', 406);
  484. //        }
  485.         $assignedAuctionAccount $em->getRepository(PluginUserAuctionAccountAssigment::class)->find((int)$assigment_id);
  486.         if (!$assignedAuctionAccount instanceof PluginUserAuctionAccountAssigment) {
  487.             header(self::HEADERS);
  488.             return new JsonResponse('Invalid assigment'406);
  489.         }
  490.         if($pluginToken->getToken() != $token){
  491.             header(self::HEADERS);
  492.             return new JsonResponse('Invalid token'406);
  493.         }
  494.         $pluginPing = (new PluginPing())
  495.             ->setAssigment($assignedAuctionAccount)
  496.             ->setToken($pluginToken)
  497.             ->setLoggedCopart((bool)$loggedCopart)
  498.             ->setLoggedPlugin((bool)$loggedPlugin)
  499.             ->setDate( new \DateTime('now'));
  500.         $em->persist($pluginPing);
  501.         $em->flush();
  502.         header(self::HEADERS);
  503.         return new JsonResponse(['status' => 'Ok'], 200);
  504.     }
  505.     /**
  506.      * @Route("/add_event_log", name="add_event_log", methods={"POST"})
  507.      * @param Request $request
  508.      * @param EntityManagerInterface $em
  509.      * @return Response
  510.      */
  511.     public function addEventLog(Request $requestEntityManagerInterface $em): Response
  512.     {
  513.         $parameters json_decode($request->getContent(), true);
  514.         if (empty($parameters['event'])) {
  515.             header(self::HEADERS);
  516.             return new JsonResponse('Empty event'400);
  517.         }
  518.         if (empty($parameters['eventData']) || !is_array($parameters['eventData'])) {
  519.             header(self::HEADERS);
  520.             return new JsonResponse('Bad data'400);
  521.         }
  522.         $ip CommonUtils::getClientIP($request);
  523.         $pluginEventLog = (new PluginEventLog())
  524.             ->setIp($ip)
  525.             ->setCreatedAt(new \DateTime('now'))
  526.             ->setName($parameters['event'])
  527.             ->setData($parameters['eventData']);
  528.         if( !empty($parameters['eventData']['origin']) && in_array($parameters['eventData']['origin'], PluginEventLog::ORIGINS) ){
  529.             $pluginEventLog->setOrigin($parameters['eventData']['origin']);
  530.         }
  531.         $em->persist($pluginEventLog);
  532.         $em->flush();
  533.         header(self::HEADERS);
  534.         return new JsonResponse(['status' => 'Ok'], 200);
  535.     }
  536.     /**
  537.      * @Route("/add_lot_data", name="add_lot_data", methods={"POST"})
  538.      * @param Request $request
  539.      * @param EntityManagerInterface $em
  540.      * @return Response
  541.      */
  542.     public function addLotData(Request $requestEntityManagerInterface $em): Response
  543.     {
  544.         $parameters json_decode($request->getContent(), true);
  545.         if (empty($parameters['auction'])) {
  546.             header(self::HEADERS);
  547.             return new JsonResponse('Empty auction'400);
  548.         }
  549.         if (empty($parameters['lot'])) {
  550.             header(self::HEADERS);
  551.             return new JsonResponse('Empty lot'400);
  552.         }
  553.         if (empty($parameters['vin']) || str_contains('stars_'$parameters['vin'])) {
  554.             header(self::HEADERS);
  555.             return new JsonResponse('Wrong vin'400);
  556.         }
  557.         if (!isset($parameters['data']) || empty($parameters['data']) || !is_array($parameters['data']) || !isset($parameters['data']['lotDetails'])) {
  558.             header(self::HEADERS);
  559.             return new JsonResponse('Bad data'400);
  560.         }
  561.         $ip CommonUtils::getClientIP($request);
  562.         $pluginEventLog = (new PluginLotData())
  563.             ->setIp($ip)
  564.             ->setCreatedAt(new \DateTimeImmutable('now'))
  565.             ->setLot($parameters['lot'])
  566.             ->setVin($parameters['vin'])
  567.             ->setPv($parameters['pv'])
  568.             ->setUserData($parameters['user_data'])
  569.             ->setData($parameters['data']);
  570.         if( !empty($parameters['auction']) && in_array($parameters['auction'], PluginLotData::AUCTIONS) ){
  571.             $pluginEventLog->setAuction($parameters['auction']);
  572.         }
  573.         $em->persist($pluginEventLog);
  574.         $em->flush();
  575.         header(self::HEADERS);
  576.         return new JsonResponse(['status' => 'Ok'], 200);
  577.     }
  578.     /**
  579.      * @Route("/get_auction", name="get_auction", methods={"POST"})
  580.      * @param Request $request
  581.      * @param EntityManagerInterface $em
  582.      * @return Response
  583.      */
  584.     public function get_auction(Request $requestEntityManagerInterface $em): Response
  585.     {
  586.         $request_body $request->toArray();
  587.         $plugin_version $request_body['pv'] ?? '';
  588.         $auction_name strtolower(trim($request_body['auction_name'])) ?? '';
  589.         $ip CommonUtils::getClientIP($request);
  590.         if(empty($auction_name)){
  591.             header(self::HEADERS);
  592.             return new JsonResponse('Invalid Request'400);
  593.         }
  594.         if (empty($plugin_version)) {
  595.             header(self::HEADERS);
  596.             return new JsonResponse('Invalid pv'400);
  597.         }
  598.         try {
  599.             $pluginToken $this->_authUserByToken($em$request);
  600.         } catch (BadRequestException $e) {
  601.             header(self::HEADERS);
  602.             return new JsonResponse('Bad Request'400);
  603.         } catch (InvalidTokenException $e) {
  604.             header(self::HEADERS);
  605.         }
  606.         $available_accounts $pluginToken->getPluginUser()->getCopartAccounts();
  607.         $AuctionAccounts = [];
  608.         foreach($available_accounts as $account) {
  609.             if (strtolower($account->getAuctionNameWithFullCountry()) === $auction_name) {
  610.                 $AuctionAccounts[] = $account;
  611.             }
  612.         }
  613.         $count count($AuctionAccounts);
  614.         if ($count 1) {
  615.             header(self::HEADERS);
  616.             return new JsonResponse('Invalid Auction accounts'401);
  617.         }
  618.         $key random_int(0$count 1);
  619.         $auctionAccount $AuctionAccounts[$key];
  620.         $proxyAccount $auctionAccount->getProxyAccount();
  621.         $proxy = [];
  622.         if ($proxyAccount) {
  623.             $proxy = [
  624.                     'host' => $proxyAccount->getProxyHost() ?? '',
  625.                     'port' => $proxyAccount->getProxyPort() ?? '',
  626.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  627.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  628.             ];
  629.         }
  630.         $vpn = [];
  631.         $vpn_required false;
  632.         if($auction_name == 'cars-usa') { //TODO check if auction has vpn config.
  633.             $vpn = [
  634.                 'vpn_type' => 'openvpn',
  635.                 'vpn_config' => self::STATIC_VPN_CONFIG,
  636.             ];
  637.             $vpn_required true;
  638.         }
  639.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  640.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  641.         $data = [
  642.             'auction_credentials' => [
  643.                 'auction_name' => $auction_name,
  644.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  645.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  646.             ],
  647.             'assigment_id' => $pluginAuctionAssignment->getId(),
  648.             'proxy' => $proxy,
  649.             'vpn_options' => $vpn,
  650.             'vpn_required' => $vpn_required,
  651.         ];
  652.         header(self::HEADERS);
  653.         return new JsonResponse($data200);
  654.     }
  655.     /**
  656.      * @Route("/get_available_auctions", name="get_available_auctions", methods={"POST"})
  657.      * @param Request $request
  658.      * @param EntityManagerInterface $em
  659.      * @return Response
  660.      */
  661.     public function getAvailableAuctionAccounts(Request $requestEntityManagerInterface $em): Response
  662.     {
  663.         $data = [];
  664.         $_avail_auction_names = [];
  665.         $parameters json_decode($request->getContent(), true) ?: [];
  666.         $allValue $parameters['all'] ?? false;
  667.         $all is_bool($allValue)
  668.             ? $allValue
  669.             : (is_int($allValue)
  670.                 ? $allValue === 1
  671.                 : (is_string($allValue) && in_array(strtolower(trim($allValue)), ['1''true''yes'], true)));
  672.         $auctionNameFilter strtolower(trim((string)($parameters['auction_name'] ?? '')));
  673.         try {
  674.             $pluginToken $this->_authUserByToken($em$request);
  675.         } catch (BadRequestException $e) {
  676.             header(self::HEADERS);
  677.             return new JsonResponse('Bad Request'400);
  678.         } catch (InvalidTokenException $e) {
  679.             header(self::HEADERS);
  680.             return new JsonResponse('Invalid token'401);
  681.         }
  682.         $available_accounts $pluginToken->getPluginUser()->getCopartAccounts();
  683.         foreach($available_accounts as $account) {
  684.             $fullAuctionName strtolower((string)$account->getAuctionName() . '-' . (string)$account->getCountry());
  685.             $shortAuctionName strtolower((string)$account->getAuctionName());
  686.             if (!empty($auctionNameFilter) && $auctionNameFilter !== $shortAuctionName && $auctionNameFilter !== $fullAuctionName) {
  687.                 continue;
  688.             }
  689.             if (!$all && in_array($account->getAuctionName().'-'.$account->getCountry(), $_avail_auction_namestrue)) {
  690.                 continue;
  691.             }
  692.             $_avail_auction_names[] = $account->getAuctionName().'-'.$account->getCountry();
  693.             $id $account->getId();
  694.             if (strtolower($account->getAuctionName()) === 'copart'){
  695.                 $id 2;
  696.             } elseif (strtolower($account->getAuctionName()) === 'manheim'){
  697.                 $id 11;
  698.             } elseif (strtolower($account->getAuctionName()) === 'iaai' && !is_null($account->getCountry())) {
  699.                 if (strtolower($account->getCountry()) === 'usa') {
  700.                     $id 1;
  701.                 } elseif (strtolower($account->getCountry()) === 'canada') {
  702.                     $id 12;
  703.                 }
  704.             } elseif (strtolower($account->getAuctionName()) === 'bringatrailer'){
  705.                 $id 17;
  706.             } elseif (strtolower($account->getAuctionName()) === 'cars_and_bids'){
  707.                 $id 20;
  708.             } elseif (strtolower($account->getAuctionName()) === 'cars'){
  709.                 $id 23;
  710.             } elseif (strtolower($account->getAuctionName()) === 'cargurus'){
  711.                 $id 26;
  712.             }
  713.             $data[] = [
  714.                 'id' => $id,
  715.                 'auction_account_id' => $account->getId(),
  716.                 'account_name' => $account->getCommonName() ?? '',
  717.                 'auction_name' => $account->getAuctionName(),
  718.                 'auction_country' => $account->getCountry(),
  719.                 'display_name' => ( is_null($account->getCountry()) || empty($account->getCountry()) ) ? strtoupper($account->getAuctionName()) : strtoupper($account->getAuctionName().'-'.$account->getCountry()),
  720.             ];
  721.         }
  722.         if (count($data) < 1) {
  723.             header(self::HEADERS);
  724.             return new JsonResponse('Invalid Auction accounts'401);
  725.         }
  726.         header(self::HEADERS);
  727.         return new JsonResponse($data200);
  728.     }
  729.     /**
  730.      * @Route("/get_iaai_account", name="get_iaai_account", methods={"POST"})
  731.      * @param Request $request
  732.      * @param EntityManagerInterface $em
  733.      * @return Response
  734.      */
  735.     public function getIaaiAccount(Request $requestEntityManagerInterface $em): Response
  736.     {
  737.         try {
  738.             $pluginToken $this->_authUserByToken($em$request);
  739.         } catch (BadRequestException $e) {
  740.             header(self::HEADERS);
  741.             return new JsonResponse('Bad Request'400);
  742.         } catch (InvalidTokenException $e) {
  743.             header(self::HEADERS);
  744.             return new JsonResponse('Invalid token'401);
  745.         }
  746.         $parameters json_decode($request->getContent(), true);
  747.         $plugin_version $parameters['pv'] ?? '';
  748.         $allValue $parameters['all'] ?? false;
  749.         $all is_bool($allValue)
  750.             ? $allValue
  751.             : (is_int($allValue)
  752.                 ? $allValue === 1
  753.                 : (is_string($allValue) && in_array(strtolower(trim($allValue)), ['1''true''yes'], true)));
  754.         if (empty($plugin_version)) {
  755.             header(self::HEADERS);
  756.             return new JsonResponse('Invalid pv'400);
  757.         }
  758.         $ip CommonUtils::getClientIP($request);
  759.         /******* parse iaai redirect country *********/
  760.         $redirect_uri $parameters['url'] ?? 'usa';
  761.         $country 'usa';
  762.         if (!empty($redirect_uri)) {
  763.             parse_str(parse_url(urldecode($redirect_uri), PHP_URL_QUERY), $parsed_redirect_url);
  764.             if (!empty($parsed_redirect_url['redirect_uri'])) {
  765.                 if (strpos($parsed_redirect_url['redirect_uri'], 'ca.iaai.com') !== false) {
  766.                     $country 'canada';
  767.                 } elseif (strpos($parsed_redirect_url['redirect_uri'], 'www.iaai.com') !== false) {
  768.                     $country 'usa';
  769.                 }
  770.             }
  771.         }
  772.         /******* end *********/
  773.         $pluginUser $pluginToken->getPluginUser();
  774.         $AuctionAccounts = [];
  775.         foreach ($pluginUser->getOnlyIaaiAccounts() as $aa) {
  776.             if ($aa->getCountry() === $country) {
  777.                 $AuctionAccounts[] = $aa;
  778.             }
  779.         }
  780.         if (count($AuctionAccounts) < 1) {
  781.             header(self::HEADERS);
  782.             return new JsonResponse('Invalid Iaai accounts'401);
  783.         }
  784.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  785.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  786.         if ($all) {
  787.             $allIaaiAccounts = [];
  788.             foreach ($AuctionAccounts as $auctionAccount) {
  789.                 $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  790.                 $accountData = [
  791.                     'id' => $auctionAccount->getId(),
  792.                     'auction_account_id' => $auctionAccount->getId(),
  793.                     'account_name' => $auctionAccount->getCommonName() ?? '',
  794.                     'login' => $auctionAccount->getLogin() ?? '',
  795.                     'password' => $auctionAccount->getPassword() ?? '',
  796.                     'can_manage_payments' => $canManagePayments,
  797.                     'country' => $auctionAccount->getCountry(),
  798.                 ];
  799.                 $proxyAccount $auctionAccount->getProxyAccount();
  800.                 $proxy = [];
  801.                 if ($proxyAccount) {
  802.                     $proxy = [
  803.                         'proxy' => [
  804.                             'host' => $proxyAccount->getProxyHost() ?? '',
  805.                             'port' => $proxyAccount->getProxyPort() ?? '',
  806.                             'username' => $proxyAccount->getProxyUsername() ?? '',
  807.                             'password' => $proxyAccount->getProxyPassword() ?? '',
  808.                         ]
  809.                     ];
  810.                 }
  811.                 if (!empty($proxy)) {
  812.                     $accountData['proxy'] = $proxy['proxy'];
  813.                 }
  814.                 $allIaaiAccounts[] = $accountData;
  815.             }
  816.             header(self::HEADERS);
  817.             return new JsonResponse(['iaai_accounts' => $allIaaiAccounts], 200);
  818.         }
  819.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  820.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  821.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  822.         $proxyAccount $auctionAccount->getProxyAccount();
  823.         $proxy = [];
  824.         if ($proxyAccount) {
  825.             $proxy = [
  826.                 'proxy' => [
  827.                     'host' => $proxyAccount->getProxyHost() ?? '',
  828.                     'port' => $proxyAccount->getProxyPort() ?? '',
  829.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  830.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  831.                 ]
  832.             ];
  833.         }
  834.         $data = [
  835.             'iaai' => [
  836.                 'id' => $pluginAuctionAssignment->getAuctionAccount()->getId(),
  837.                 'account_name' => $pluginAuctionAssignment->getAuctionAccount()->getCommonName() ?? '',
  838.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  839.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  840.             ],
  841.             'assigment_id' => $pluginAuctionAssignment->getId(),
  842.             'can_manage_payments' => $canManagePayments,
  843.         ];
  844.         header(self::HEADERS);
  845.         return new JsonResponse(array_merge($data$proxy), 200);
  846.     }
  847.     /**
  848.      * @Route("/get_manheim_account", name="get_manheim_account", methods={"POST"})
  849.      * @param Request $request
  850.      * @param EntityManagerInterface $em
  851.      * @return Response
  852.      */
  853.     public function getManheimAccount(Request $requestEntityManagerInterface $em): Response
  854.     {
  855.         try {
  856.             $pluginToken $this->_authUserByToken($em$request);
  857.         } catch (BadRequestException $e) {
  858.             header(self::HEADERS);
  859.             return new JsonResponse('Bad Request'400);
  860.         } catch (InvalidTokenException $e) {
  861.             header(self::HEADERS);
  862.             return new JsonResponse('Invalid token'401);
  863.         }
  864.         $parameters json_decode($request->getContent(), true);
  865.         $plugin_version $parameters['pv'] ?? '';
  866.         $allValue $parameters['all'] ?? false;
  867.         $all is_bool($allValue)
  868.             ? $allValue
  869.             : (is_int($allValue)
  870.                 ? $allValue === 1
  871.                 : (is_string($allValue) && in_array(strtolower(trim($allValue)), ['1''true''yes'], true)));
  872.         if (empty($plugin_version)) {
  873.             header(self::HEADERS);
  874.             return new JsonResponse('Invalid pv'400);
  875.         }
  876.         $ip CommonUtils::getClientIP($request);
  877.         $pluginUser $pluginToken->getPluginUser();
  878.         $AuctionAccounts $pluginUser->getOnlyManheimAccounts();
  879.         if (count($AuctionAccounts) < 1) {
  880.             header(self::HEADERS);
  881.             return new JsonResponse('Invalid Manheim accounts'401);
  882.         }
  883.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  884.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  885.         if ($all) {
  886.             $allManheimAccounts = [];
  887.             foreach ($AuctionAccounts as $auctionAccount) {
  888.                 $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  889.                 $accountData = [
  890.                     'id' => $auctionAccount->getId(),
  891.                     'auction_account_id' => $auctionAccount->getId(),
  892.                     'account_name' => $auctionAccount->getCommonName() ?? '',
  893.                     'login' => $auctionAccount->getLogin() ?? '',
  894.                     'password' => $auctionAccount->getPassword() ?? '',
  895.                     'can_manage_payments' => $canManagePayments,
  896.                 ];
  897.                 $proxyAccount $auctionAccount->getProxyAccount();
  898.                 $proxy = [];
  899.                 if ($proxyAccount) {
  900.                     $proxy = [
  901.                         'proxy' => [
  902.                             'host' => $proxyAccount->getProxyHost() ?? '',
  903.                             'port' => $proxyAccount->getProxyPort() ?? '',
  904.                             'username' => $proxyAccount->getProxyUsername() ?? '',
  905.                             'password' => $proxyAccount->getProxyPassword() ?? '',
  906.                         ]
  907.                     ];
  908.                 }
  909.                 if (!empty($proxy)) {
  910.                     $accountData['proxy'] = $proxy['proxy'];
  911.                 }
  912.                 $allManheimAccounts[] = $accountData;
  913.             }
  914.             header(self::HEADERS);
  915.             return new JsonResponse(['manheim_accounts' => $allManheimAccounts], 200);
  916.         }
  917.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  918.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  919.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  920. //        $login = $auctionAccount->getLogin();
  921. //        $password = $auctionAccount->getPassword();
  922.         // $proxyAccount = $auctionAccount->getProxyAccount();
  923.         $em $this->getDoctrine()->getManager();
  924.         $proxyAccount $em->getRepository(ProxyAccount::class)->findLastUsedManheimProxy();
  925.         $proxy = [];
  926.         if ($proxyAccount) {
  927.             $proxy = [
  928.                 'proxy' => [
  929.                     'host' => $proxyAccount->getProxyHost() ?? '',
  930.                     'port' => $proxyAccount->getProxyPort() ?? '',
  931.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  932.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  933.                 ]
  934.             ];
  935.             $proxyAccount->setLastUsedAt(new \DateTime('now'));
  936.             $em->persist($proxyAccount);
  937.         }
  938.         $data = [
  939.             'manheim' => [
  940.                 'id' => $pluginAuctionAssignment->getAuctionAccount()->getId(),
  941.                 'account_name' => $pluginAuctionAssignment->getAuctionAccount()->getCommonName() ?? '',
  942.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  943.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  944.             ],
  945.             'assigment_id' => $pluginAuctionAssignment->getId(),
  946.             'can_manage_payments' => $canManagePayments,
  947.         ];
  948.         header(self::HEADERS);
  949.         return new JsonResponse(array_merge($data$proxy), 200);
  950.     }
  951.     /**
  952.      * @Route("/create_assigment_log", name="create_assigment_log", methods={"POST"})
  953.      * @return Response
  954.      */
  955.     public function createAssigmentLog(Request $requestEntityManagerInterface $em): Response
  956.     {
  957.         $parameters json_decode($request->getContent(), true);
  958.         $auctionAccountId $parameters['id'] ?? null;
  959.         $token $parameters['token'] ?? '';
  960.         $pluginVersion $parameters['pv'] ?? '';
  961.         if (empty($auctionAccountId) || empty($token)) {
  962.             header(self::HEADERS);
  963.             return new JsonResponse('Bad Request'400);
  964.         }
  965.         $pluginToken $em->getRepository(PluginToken::class)->findOneBy(['token' => $token'status' => 'on']);
  966.         if (!$pluginToken instanceof PluginToken) {
  967.             header(self::HEADERS);
  968.             return new JsonResponse('Invalid token'401);
  969.         }
  970.         $pluginUser $pluginToken->getPluginUser();
  971.         if (!$pluginUser->getOwner()->getEnablePlugin() || $pluginUser->getDeleted()) {
  972.             header(self::HEADERS);
  973.             return new JsonResponse('Invalid token'401);
  974.         }
  975.         $auctionAccount $em->getRepository(AuctionAccount::class)->find((int)$auctionAccountId);
  976.         if (!$auctionAccount instanceof AuctionAccount) {
  977.             header(self::HEADERS);
  978.             return new JsonResponse('Invalid account id'406);
  979.         }
  980.         $allowed false;
  981.         foreach ($pluginUser->getCopartAccounts() as $userAuctionAccount) {
  982.             if ($userAuctionAccount->getId() === $auctionAccount->getId()) {
  983.                 $allowed true;
  984.                 break;
  985.             }
  986.         }
  987.         if (!$allowed) {
  988.             header(self::HEADERS);
  989.             return new JsonResponse('Invalid account id'406);
  990.         }
  991.         $ip CommonUtils::getClientIP($request);
  992.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  993.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment(
  994.             $auctionAccount,
  995.             $pluginToken,
  996.             (string)$pluginVersion,
  997.             $ip
  998.         );
  999.         header(self::HEADERS);
  1000.         return new JsonResponse([
  1001.             'status' => 'Ok',
  1002.             'assigment_id' => $pluginAuctionAssignment->getId(),
  1003.         ], 200);
  1004.     }
  1005.     /**
  1006.      * @Route("/manheim_2fa_codes", name="manheim_2fa_code", methods={"POST"})
  1007.      * @param AccountCodeRepository $accountCodeRepository
  1008.      * @param EntityManagerInterface $em
  1009.      * @param Request $request
  1010.      * @return Response
  1011.      */
  1012.     public function getManheim2FACodes(AccountCodeRepository $accountCodeRepositoryEntityManagerInterface $emRequest $request): Response
  1013.     {
  1014.         try {
  1015.             $pluginToken $this->_authUserByToken($em$request);
  1016.         } catch (BadRequestException $e) {
  1017.             header(self::HEADERS);
  1018.             return new JsonResponse('Bad Request'400);
  1019.         } catch (InvalidTokenException $e) {
  1020.             header(self::HEADERS);
  1021.             return new JsonResponse('Invalid token'401);
  1022.         }
  1023.         $auctionAccounts $pluginToken->getPluginUser()->getOnlyManheimAccounts();
  1024.         $count count($auctionAccounts);
  1025.         if ($count 1) {
  1026.             header(self::HEADERS);
  1027.             return new JsonResponse('Invalid Manheim accounts'401);
  1028.         }
  1029.         $manheimCodes $accountCodeRepository->getLatestCodesByAccountType('manheim');
  1030.         $resultCodes = [];
  1031.         foreach ($manheimCodes as $code) {
  1032.             $resultCodes[] = [
  1033.                 '2fa_code' => $code->getCode(),
  1034.                 'received_at' => $code->getReceivedAt()->format('c'), // ISO 8601 format,
  1035.             ];
  1036.         }
  1037.         $data = [
  1038.             'manheim_codes' => $resultCodes
  1039.         ];
  1040.         header(self::HEADERS);
  1041.         return new JsonResponse($data200);
  1042.     }
  1043.     /**
  1044.      * @Route("/get_bringatrailer_account", name="get_bringatrailer_account", methods={"POST"})
  1045.      * @param Request $request
  1046.      * @param EntityManagerInterface $em
  1047.      * @return Response
  1048.      */
  1049.     public function getBringatrailerAccount(Request $requestEntityManagerInterface $em): Response
  1050.     {
  1051.         try {
  1052.             $pluginToken $this->_authUserByToken($em$request);
  1053.         } catch (BadRequestException $e) {
  1054.             header(self::HEADERS);
  1055.             return new JsonResponse('Bad Request'400);
  1056.         } catch (InvalidTokenException $e) {
  1057.             header(self::HEADERS);
  1058.             return new JsonResponse('Invalid token'401);
  1059.         }
  1060.         $parameters json_decode($request->getContent(), true);
  1061.         $plugin_version $parameters['pv'] ?? '';
  1062.         if (empty($plugin_version)) {
  1063.             header(self::HEADERS);
  1064.             return new JsonResponse('Invalid pv'400);
  1065.         }
  1066.         $ip CommonUtils::getClientIP($request);
  1067.         $pluginUser $pluginToken->getPluginUser();
  1068.         $AuctionAccounts $pluginUser->getOnlyBringatrailerAccounts();
  1069.         if (count($AuctionAccounts) < 1) {
  1070.             header(self::HEADERS);
  1071.             return new JsonResponse('Invalid Bringatrailer accounts'401);
  1072.         }
  1073.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  1074.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  1075.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  1076.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  1077.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  1078. //        $login = $auctionAccount->getLogin();
  1079. //        $password = $auctionAccount->getPassword();
  1080.         $proxyAccount $auctionAccount->getProxyAccount();
  1081.         $proxy = [];
  1082.         if ($proxyAccount) {
  1083.             $proxy = [
  1084.                 'proxy' => [
  1085.                     'host' => $proxyAccount->getProxyHost() ?? '',
  1086.                     'port' => $proxyAccount->getProxyPort() ?? '',
  1087.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  1088.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  1089.                 ]
  1090.             ];
  1091.         }
  1092.         $data = [
  1093.             'bringatrailer' => [
  1094.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  1095.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  1096.             ],
  1097.             'assigment_id' => $pluginAuctionAssignment->getId(),
  1098.             'can_manage_payments' => $canManagePayments,
  1099.         ];
  1100.         header(self::HEADERS);
  1101.         return new JsonResponse(array_merge($data$proxy), 200);
  1102.     }
  1103.     /**
  1104.      * @Route("/get_cars_and_bids_account", name="get_cars_and_bids_account", methods={"POST"})
  1105.      * @param Request $request
  1106.      * @param EntityManagerInterface $em
  1107.      * @return Response
  1108.      */
  1109.     public function getCarsAndBidsAccount(Request $requestEntityManagerInterface $em): Response
  1110.     {
  1111.         try {
  1112.             $pluginToken $this->_authUserByToken($em$request);
  1113.         } catch (BadRequestException $e) {
  1114.             header(self::HEADERS);
  1115.             return new JsonResponse('Bad Request'400);
  1116.         } catch (InvalidTokenException $e) {
  1117.             header(self::HEADERS);
  1118.             return new JsonResponse('Invalid token'401);
  1119.         }
  1120.         $parameters json_decode($request->getContent(), true);
  1121.         $plugin_version $parameters['pv'] ?? '';
  1122.         if (empty($plugin_version)) {
  1123.             header(self::HEADERS);
  1124.             return new JsonResponse('Invalid pv'400);
  1125.         }
  1126.         $ip CommonUtils::getClientIP($request);
  1127.         $pluginUser $pluginToken->getPluginUser();
  1128.         $AuctionAccounts $pluginUser->getOnlyCarsAndBidsAccounts();
  1129.         if (count($AuctionAccounts) < 1) {
  1130.             header(self::HEADERS);
  1131.             return new JsonResponse('Invalid Cars_and_bids accounts'401);
  1132.         }
  1133.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  1134.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  1135.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  1136.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  1137.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  1138. //        $login = $auctionAccount->getLogin();
  1139. //        $password = $auctionAccount->getPassword();
  1140.         $proxyAccount $auctionAccount->getProxyAccount();
  1141.         $proxy = [];
  1142.         if ($proxyAccount) {
  1143.             $proxy = [
  1144.                 'proxy' => [
  1145.                     'host' => $proxyAccount->getProxyHost() ?? '',
  1146.                     'port' => $proxyAccount->getProxyPort() ?? '',
  1147.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  1148.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  1149.                 ]
  1150.             ];
  1151.         }
  1152.         $data = [
  1153.             'cars_and_bids' => [
  1154.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  1155.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  1156.             ],
  1157.             'assigment_id' => $pluginAuctionAssignment->getId(),
  1158.             'can_manage_payments' => $canManagePayments,
  1159.         ];
  1160.         header(self::HEADERS);
  1161.         return new JsonResponse(array_merge($data$proxy), 200);
  1162.     }
  1163.     /**
  1164.      * @Route("/get_cars_account", name="get_cars_account", methods={"POST"})
  1165.      * @param Request $request
  1166.      * @param EntityManagerInterface $em
  1167.      * @return Response
  1168.      */
  1169.     public function getCarsAccount(Request $requestEntityManagerInterface $em): Response
  1170.     {
  1171.         try {
  1172.             $pluginToken $this->_authUserByToken($em$request);
  1173.         } catch (BadRequestException $e) {
  1174.             header(self::HEADERS);
  1175.             return new JsonResponse('Bad Request'400);
  1176.         } catch (InvalidTokenException $e) {
  1177.             header(self::HEADERS);
  1178.             return new JsonResponse('Invalid token'401);
  1179.         }
  1180.         $parameters json_decode($request->getContent(), true);
  1181.         $plugin_version $parameters['pv'] ?? '';
  1182.         if (empty($plugin_version)) {
  1183.             header(self::HEADERS);
  1184.             return new JsonResponse('Invalid pv'400);
  1185.         }
  1186.         $ip CommonUtils::getClientIP($request);
  1187.         $pluginUser $pluginToken->getPluginUser();
  1188.         $AuctionAccounts $pluginUser->getOnlyCarsAccounts();
  1189.         if (count($AuctionAccounts) < 1) {
  1190.             header(self::HEADERS);
  1191.             return new JsonResponse('Invalid Cars accounts'401);
  1192.         }
  1193.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  1194.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  1195.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  1196.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  1197.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  1198.         $proxyAccount $auctionAccount->getProxyAccount();
  1199.         $proxy = [];
  1200.         if ($proxyAccount) {
  1201.             $proxy = [
  1202.                 'proxy' => [
  1203.                     'host' => $proxyAccount->getProxyHost() ?? '',
  1204.                     'port' => $proxyAccount->getProxyPort() ?? '',
  1205.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  1206.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  1207.                 ]
  1208.             ];
  1209.         }
  1210.         $data = [
  1211.             'cars' => [
  1212.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  1213.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  1214.             ],
  1215.             'assigment_id' => $pluginAuctionAssignment->getId(),
  1216.             'can_manage_payments' => $canManagePayments,
  1217.         ];
  1218.         header(self::HEADERS);
  1219.         return new JsonResponse(array_merge($data$proxy), 200);
  1220.     }
  1221.     /**
  1222.      * @Route("/get_cargurus_account", name="get_cargurus_account", methods={"POST"})
  1223.      * @param Request $request
  1224.      * @param EntityManagerInterface $em
  1225.      * @return Response
  1226.      */
  1227.     public function getCargurusAccount(Request $requestEntityManagerInterface $em): Response
  1228.     {
  1229.         try {
  1230.             $pluginToken $this->_authUserByToken($em$request);
  1231.         } catch (BadRequestException $e) {
  1232.             header(self::HEADERS);
  1233.             return new JsonResponse('Bad Request'400);
  1234.         } catch (InvalidTokenException $e) {
  1235.             header(self::HEADERS);
  1236.             return new JsonResponse('Invalid token'401);
  1237.         }
  1238.         $parameters json_decode($request->getContent(), true);
  1239.         $plugin_version $parameters['pv'] ?? '';
  1240.         if (empty($plugin_version)) {
  1241.             header(self::HEADERS);
  1242.             return new JsonResponse('Invalid pv'400);
  1243.         }
  1244.         $ip CommonUtils::getClientIP($request);
  1245.         $pluginUser $pluginToken->getPluginUser();
  1246.         $AuctionAccounts $pluginUser->getOnlyCargurusAccounts();
  1247.         if (count($AuctionAccounts) < 1) {
  1248.             header(self::HEADERS);
  1249.             return new JsonResponse('Invalid Cargurus accounts'401);
  1250.         }
  1251.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  1252.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  1253.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  1254.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  1255.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  1256.         $proxyAccount $auctionAccount->getProxyAccount();
  1257.         $proxy = [];
  1258.         if ($proxyAccount) {
  1259.             $proxy = [
  1260.                 'proxy' => [
  1261.                     'host' => $proxyAccount->getProxyHost() ?? '',
  1262.                     'port' => $proxyAccount->getProxyPort() ?? '',
  1263.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  1264.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  1265.                 ]
  1266.             ];
  1267.         }
  1268.         $data = [
  1269.             'cargurus' => [
  1270.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  1271.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  1272.             ],
  1273.             'assigment_id' => $pluginAuctionAssignment->getId(),
  1274.             'can_manage_payments' => $canManagePayments,
  1275.         ];
  1276.         header(self::HEADERS);
  1277.         return new JsonResponse(array_merge($data$proxy), 200);
  1278.     }
  1279.     /**
  1280.      * @Route("/send_app_debug", name="send_app_debug", methods={"POST"})
  1281.      * @param Request $request
  1282.      * @param EntityManagerInterface $em
  1283.      * @return Response
  1284.      */
  1285.     public function sendAppDebug(Request $requestEntityManagerInterface $em): Response
  1286.     {
  1287.         $plugin_login $request->get('plugin_login''');
  1288.         $plugin_user $em->getRepository(PluginUser::class)->findOneBy(['login' => $plugin_login]);
  1289.         //TODO: add checking if is set debug flag for plugin user
  1290.         if (!$plugin_user instanceof PluginUser) {
  1291.             return new JsonResponse('Invalid user'401);
  1292.         }
  1293.         $date = (new \DateTime('now'))->format('Y-m-d');
  1294.         $files $request->files->all();
  1295.         if(!array_key_exists('files'$files)) {
  1296.             return new JsonResponse('File error'Response::HTTP_BAD_REQUEST, []);
  1297.         }
  1298.         $files $files['files'];
  1299.         $files_uploaded $this->_uploadedFiles$files,"fk_o1z_app_logs/{$plugin_login}/{$date});
  1300.         $result = [
  1301.             'status'=>'Success',
  1302.             'message' => 'Logs are successfully saved',
  1303.             'files_url' => $files_uploaded//TODO: Remove after debug!
  1304.         ];
  1305.         return new JsonResponse($result200, []);
  1306.     }
  1307.     private function _uploadedFiles($added_files_set$store_path){
  1308.         $_files = [];
  1309.         if (!empty($added_files_set)) {
  1310.             foreach ($added_files_set as $_newFile) {
  1311.                 if($_newFile instanceof UploadedFile) {
  1312.                     $new_file_path $this->fileUploader->upload($store_path$_newFile'');
  1313.                     if ($new_file_path) {
  1314.                         $_files[] = $this->fileUploader->getAbsoluteURLToFile($new_file_path);
  1315.                     }
  1316.                 }
  1317.             }
  1318.         }
  1319.         return $_files;
  1320.     }
  1321.     /**
  1322.      * @Route("/check_app_updates", name="check_app_updates", methods={"POST"})
  1323.      * @param Request $request
  1324.      * @param EntityManagerInterface $em
  1325.      * @return Response
  1326.      */
  1327.     public function checkAppUpdates(Request $requestEntityManagerInterface $em): Response
  1328.     {
  1329.         try {
  1330.             $pluginToken $this->_authUserByToken($em$request);
  1331.         } catch (BadRequestException $e) {
  1332.             header(self::HEADERS);
  1333.             return new JsonResponse('Bad Request'400);
  1334.         } catch (InvalidTokenException $e) {
  1335.             header(self::HEADERS);
  1336.             return new JsonResponse('Invalid token'401);
  1337.         }
  1338.         $parameters json_decode($request->getContent(), true);
  1339.         //TODO: This is only for test. Make Production checks !!!
  1340.         $data = [
  1341.           'update_available' => false,
  1342.           'update_required' => false,
  1343.         ];
  1344.         $platform $parameters['platform'] ?? '';
  1345.         $version $parameters['version'] ?? '';
  1346.         if($platform == 'windows' && $version == '0.3.33') {
  1347.             $data['update_available'] = true;
  1348.             $data['update_required'] = false;
  1349.             $data['update_link'] =  'http://my.zvigerauto.com/Setup_ZvigerautoAuction_v0334.exe';
  1350.             $data['update_message'] = 'New Version is available';
  1351.         }
  1352.         if($platform == 'windows' && $version == '0.3.32') {
  1353.             $data['update_available'] = true;
  1354.             $data['update_required'] = true;
  1355.             $data['update_link'] =  'http://my.zvigerauto.com/Setup_ZvigerautoAuction_v0334.exe';
  1356.             $data['update_message'] = 'New Version is available';
  1357.         }
  1358.         return new JsonResponse(['data'=>$data], Response::HTTP_OK, []);
  1359.     }
  1360. }