<?php

include_once 'lib/Smarty-3.1.4/libs/Smarty.class.php';
include_once 'lib/config.lib.php';
include_once 'lib/session.lib.php';
include_once 'lib/ohShopify/shopify.php';
// if the code param has been sent to this page... we are in Step 2
if (isset($_GET['code'])) {
    // Step 2: do a form POST to get the access token
    $shopifyClient = new ShopifyClient($_GET['shop'], "", SHOPIFY_API_KEY, SHOPIFY_SECRET);
    session_unset();
    $_SESSION['token'] = $shopifyClient->getAccessToken($_GET['code']);
    if ($_SESSION['token'] != '') {
        $_SESSION['shop'] = $_GET['shop'];
    }
    header("Location: index.php");
    exit;
} else {
    if (isset($_POST['shop']) || isset($_GET['shop'])) {
        // Step 1: get the shopname from the user and redirect the user to the
        // shopify authorization page where they can choose to authorize this app
        $shop = isset($_POST['shop']) ? $_POST['shop'] : $_GET['shop'];
        $shopifyClient = new ShopifyClient($shop, "", SHOPIFY_API_KEY, SHOPIFY_SECRET);
        // get the URL to the current page
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {
            $pageURL .= "s";
        }
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
 $res = mysql_query($select_sql);
 if (mysql_num_rows($res) > 0) {
     header("Location: admin/add_image.php?shop=" . $shop);
     exit;
 }
 if (isset($_GET['code'])) {
     $select_sql = "SELECT `id`, `payment_status` FROM `app` WHERE `shop` = '" . $_GET['shop'] . "' ORDER BY `id` DESC LIMIT 1";
     $res = mysql_query($select_sql);
     if (mysql_num_rows($res) > 0) {
         $result = mysql_fetch_assoc($res);
         /*if ($result['payment_status'] == 'accepted' || $result['payment_status'] == 'free') {
         			header("Location: widget.php?shop=".$_GET['shop']);
         			exit;
         		}*/
         $shopifyClient = new ShopifyClient($_GET['shop'], "", SHOPIFY_API_KEY, SHOPIFY_SECRET);
         $access_token = $shopifyClient->getAccessToken($_GET['code']);
         session_unset();
         $_SESSION['token'] = $access_token;
         if ($_SESSION['token'] != '') {
             $_SESSION['shop'] = $_GET['shop'];
             $update_sql = "UPDATE `app` SET `code` = '" . $_GET['code'] . "', `token` = '" . $_SESSION['token'] . "', `payment_status` = 'pending', created_date = '" . date('Y-m-d H:i:s') . "' WHERE `id` = '" . $result['id'] . "'";
             mysql_query($update_sql);
             $delete_all_other_entries = "DELETE FROM `app` WHERE `id` != '" . $result['id'] . "' AND `shop` = '" . $_GET['shop'] . "'";
             mysql_query($delete_all_other_entries);
         }
     } else {
         $error_message = "Something went wrong, Please try after sometime.";
     }
     header("Location: index.php");
     exit;
 } elseif (isset($_POST['shop']) || isset($_GET['shop']) && !isset($_GET['c_id'])) {
// shoify config.
define("SHOPIFY_API_KEY", "0324a720b5a2c69b7cfe8256adbd77ed");
define("SHOPIFY_SECRET", "737d6460a53c8c166e45d9c078bce686");
define("REDIRECT_URI", "http://localhost/shopify_app_phong");
define("SHOPIFY_SCOPE", "read_products,write_products");
session_start();
if (isset($_GET['code'])) {
    // if the code param has been sent to this page... we are in Step 2
    // Step 2: do a form POST to get the access token
    $shopifyClient = new ShopifyClient($_GET['shop'], "", SHOPIFY_API_KEY, SHOPIFY_SECRET);
    session_unset();
    // if(!$shopifyClient->validateSignature($_GET)) die('Error: invalid signature.');
    // var_dump($_GET);
    // die( "signature: " . $_GET['signature']);
    // Now, request the token and store it in your session.
    $token = $shopifyClient->getAccessToken($_GET['code'], REDIRECT_URI);
    $_SESSION['token'] = $token;
    if ($_SESSION['token'] != '') {
        $_SESSION['shop'] = $_GET['shop'];
    }
    echo $token;
    header("Location: shop.php");
    exit;
} else {
    if (isset($_POST['shop']) || isset($_GET['shop'])) {
        // Step 1: get the shopname from the user and redirect the user to the
        // shopify authorization page where they can choose to authorize this app
        $shop = isset($_POST['shop']) ? $_POST['shop'] : $_GET['shop'];
        $shopifyClient = new ShopifyClient($shop, "", SHOPIFY_API_KEY, SHOPIFY_SECRET);
        // if(!$shopifyClient->validateSignature($_GET)) die('Error: invalid signature.');
        // redirect to authorize url