You are here

public function PoptinController::poptinGeneratePoptinLink in Website Popups, Email Popup, Exit-Intent Popup, and Contact Forms – Poptin 8

Implements poptinGeneratePoptinLink().

1 string reference to 'PoptinController::poptinGeneratePoptinLink'
poptin.routing.yml in ./poptin.routing.yml
poptin.routing.yml

File

src/Controller/PoptinController.php, line 104

Class

PoptinController
Provides route responses for the Poptin module.

Namespace

Drupal\poptin\Controller

Code

public function poptinGeneratePoptinLink(Request $request) {
  $token = $request->query
    ->get('csrftoken');
  if (!$this
    ->poptinCheckToken($token)) {
    $tmp_arr = [
      'mssg' => "Invalid csrf token",
      'status' => 0,
      'token' => $this
        ->poptinGetToken(),
    ];
    die(Json::encode($tmp_arr));
  }
  $api_url = "https://app.popt.in/api/marketplace/";
  $row_details = $this
    ->poptinFetchRow();
  if (!is_array($row_details)) {
    exit(FALSE);
  }
  else {
    if (isset($row_details['token']) && $row_details['token'] == '') {
      return new TrustedRedirectResponse("https://app.popt.in/login");
    }
  }
  $url = $api_url . "auth";
  $dataurl = "token=" . $row_details['token'] . "&user_id=" . $row_details['userid'];
  $curl = curl_init();
  curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $dataurl,
    CURLOPT_HTTPHEADER => [
      "cache-control: no-cache",
      "content-type: application/x-www-form-urlencoded",
      "postman-token: 16ba048a-499c-06c8-517c-cea2abb11945",
    ],
  ]);
  $response = curl_exec($curl);
  $err = curl_error($curl);
  if ($err) {
    echo "cURL Error #:" . $err;
  }
  else {
    $res = json_decode($response);
    if (!isset($res->login_url)) {
      return new TrustedRedirectResponse("https://app.popt.in/login");
    }
    if ($request->query
      ->get('utm_source')) {
      return new TrustedRedirectResponse($res->login_url . "&utm_source=drupal8");
    }
    else {
      return new TrustedRedirectResponse($res->login_url);
    }
  }
}