You are here

poptin.module in Website Popups, Email Popup, Exit-Intent Popup, and Contact Forms – Poptin 7

Same filename and directory in other branches
  1. 8 poptin.module

Module for adding popups on website.

File

poptin.module
View source
<?php

/**
 * @file
 * Module for adding popups on website.
 */

/**
 * Implements poptin_help().
 */
function poptin_help($path, $arg) {
  switch ($path) {
    case 'admin/help#poptin':
      $filepath = dirname(__FILE__) . '/README.md';
      if (file_exists($filepath)) {
        $readme = file_get_contents($filepath);
      }
      else {
        $filepath = dirname(__FILE__) . '/README.txt';
        if (file_exists($filepath)) {
          $readme = file_get_contents($filepath);
        }
      }
      if (!isset($readme)) {
        return NULL;
      }
      if (module_exists('markdown')) {
        $filters = module_invoke('markdown', 'filter_info');
        $info = $filters['filter_markdown'];
        if (function_exists($info['process callback'])) {
          $output = $info['process callback']($readme, NULL);
        }
        else {
          $output = '<pre>' . $readme . '</pre>';
        }
      }
      else {
        $output = '<pre>' . $readme . '</pre>';
      }
      return $output;
  }
}

/**
 * Implements poptin_menu().
 */
function poptin_menu() {
  $items['admin/config/system/poptin'] = array(
    'title' => 'Poptin Configuration',
    'description' => 'Poptin Popups & Forms',
    'page callback' => 'poptin_config',
    'file' => 'includes/poptin.inc',
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  $items['admin/poptin/config'] = array(
    'title' => 'Poptin Configuration',
    'page callback' => 'poptin_config',
    'access arguments' => array(
      'poptin config',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['poptinregister'] = array(
    'title' => 'Poptin Configuration',
    'page callback' => 'poptin_signup',
    'access arguments' => array(
      'administer users',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/poptin.inc',
  );
  $items['poptinlogin'] = array(
    'title' => 'Poptin Configuration',
    'page callback' => 'poptin_login',
    'access arguments' => array(
      'poptin login',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/poptin.inc',
  );
  $items['poptindeactivate'] = array(
    'title' => 'Poptin Configuration',
    'page callback' => 'poptin_deactivate',
    'access arguments' => array(
      'poptin deactive',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/poptin.inc',
  );
  $items['generateloginlink'] = array(
    'title' => 'Deactiate Poptin',
    'page callback' => 'poptin_generatepoptinlink',
    'access arguments' => array(
      'poptin linkgenerate',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/poptin.inc',
  );
  return $items;
}

/**
 * Implements hook_page_build().
 */
function poptin_page_build(&$page) {
  if (!path_is_admin(current_path())) {
    $poptin_db = poptin_fetch_row();
    if (is_array($poptin_db) && isset($poptin_db['client_id']) && $poptin_db['client_id'] != '') {
      $client_id = filter_var($poptin_db['client_id'], FILTER_SANITIZE_STRING);
      $front_url = "https://cdn.popt.in/pixel.js?id=" . check_plain($client_id);
      $page['page_bottom']['poptin'] = array(
        '#weight' => 25,
        '#markup' => '<script id="pixel-script-poptin" src="' . $front_url . '" async="true"></script>',
      );
    }
  }
}

/**
 * Implements poptin_signup().
 */
function poptin_signup() {
  if (!poptin_check_csrf_token()) {
    $return_mssg = array(
      'mssg' => t("Invalid CSRF TOKEN"),
      'status' => 0,
      'cccToken' => drupal_get_token(),
    );
    drupal_json_output($return_mssg);
    drupal_exit();
  }
  $query_params_post = drupal_get_query_parameters($_POST);
  $email_id = $query_params_post['email'];
  $api_url = "https://app.popt.in/api/marketplace/";
  $url = $api_url . "register";
  $dataurl = "email=" . $email_id . "&marketplace=drupal7";
  watchdog("API URL", $dataurl);
  $curl = curl_init();
  curl_setopt_array($curl, array(
    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 => array(
      "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 {
    $result = json_decode($response);
    if (!empty($result) && $result->success == 1) {
      $tmp_arr = array(
        'user_id' => $result->user_id,
        'client_id' => $result->client_id,
        'token' => $result->token,
      );
      db_insert('poptin')
        ->fields($tmp_arr)
        ->execute();
      $return_mssg = array(
        'mssg' => t("User Registered successfully"),
        'status' => 1,
        'cccToken' => drupal_get_token(),
      );
    }
    else {
      $return_mssg = array(
        'mssg' => $result->message,
        'status' => 0,
        'cccToken' => drupal_get_token(),
      );
    }
  }
  drupal_json_output($return_mssg);
  drupal_exit();
}

/**
 * Implements poptin_login().
 */
function poptin_login() {
  if (!poptin_check_csrf_token()) {
    $return_mssg = array(
      'mssg' => t("Invalid CSRF TOKEN"),
      'status' => 0,
      'cccToken' => drupal_get_token(),
    );
    drupal_json_output($return_mssg);
    drupal_exit();
  }
  $query_params_post = drupal_get_query_parameters($_POST);
  $userid = $query_params_post['userid'];
  db_insert('poptin')
    ->fields(array(
    'client_id' => $userid,
  ))
    ->execute();
  $return_mssg = array(
    'mssg' => t("User id Registered successfully"),
    'status' => 1,
    'cccToken' => drupal_get_token(),
  );
  drupal_json_output($return_mssg);
  drupal_exit();
}

/**
 * Implements poptin_deactivate().
 */
function poptin_deactivate() {
  if (!poptin_check_csrf_token()) {
    $return_mssg = array(
      'mssg' => t("Invalid CSRF TOKEN"),
      'status' => 0,
      'cccToken' => drupal_get_token(),
    );
    drupal_json_output($return_mssg);
    drupal_exit();
  }
  db_truncate('poptin')
    ->execute();
  $return_mssg = array(
    'mssg' => t("done"),
    'status' => 1,
    'cccToken' => drupal_get_token(),
  );
  drupal_json_output($return_mssg);
  drupal_exit();
}

/**
 * Implements poptin_config().
 */
function poptin_config() {
  $custom_js_path = drupal_get_path('module', 'poptin') . '/js/poptincustom.js';
  $css_path = drupal_get_path('module', 'poptin') . '/css/poptin.css';
  drupal_add_js($custom_js_path);
  drupal_add_css($css_path);
  module_load_include('inc', 'poptin', 'includes/poptin');
  $html = poptin_render_html();
  return $html;
}

/**
 * Implements poptin_generatepoptinlink().
 */
function poptin_generatepoptinlink() {
  if (!poptin_check_csrf_token()) {
    $return_mssg = array(
      'mssg' => t("Invalid CSRF TOKEN"),
      'status' => 0,
      'cccToken' => drupal_get_token(),
    );
    drupal_json_output($return_mssg);
    drupal_exit();
  }
  $api_url = "https://app.popt.in/api/marketplace/";
  $row_details = poptin_fetch_row();
  if (!is_array($row_details)) {
    exit(FALSE);
  }
  else {
    if (isset($row_details['token']) && $row_details['token'] == '') {
      drupal_goto("https://app.popt.in/login");
      drupal_exit();
    }
  }
  $url = $api_url . "auth";
  $dataurl = "token=" . $row_details['token'] . "&user_id=" . $row_details['userid'];
  watchdog("API URL", $dataurl);
  $curl = curl_init();
  curl_setopt_array($curl, array(
    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 => array(
      "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);
    $query_params_get = drupal_get_query_parameters($_GET);
    if (isset($query_params_get['utm_source'])) {
      header("location:" . $res->login_url . "&utm_source=drupal7");
    }
    else {
      header("location:" . $res->login_url);
    }
    drupal_exit();
  }
}

/**
 * Implements poptin_fetch_row().
 */
function poptin_fetch_row() {
  $results = db_select('poptin', 'p')
    ->fields('p')
    ->execute();
  $num_of_results = $results
    ->rowCount();
  if ($num_of_results > 0) {
    foreach ($results as $result) {
      return array(
        'userid' => $result->user_id,
        'client_id' => $result->client_id,
        'token' => $result->token,
      );
    }
  }
  else {
    return FALSE;
  }
}

/**
 * Implements poptin_check_csrf_token().
 */
function poptin_check_csrf_token() {
  if (isset($_REQUEST['token'])) {
    $testtoken = drupal_valid_token($_REQUEST['token']);
    if ($testtoken) {
      return TRUE;
    }
    return FALSE;
  }
  return FALSE;
}