<?php
/**
 * @framework       iResponse Framework 
 * @version         1.0
 * @author          Amine Idrissi <contact@iresponse.tech>
 * @date            2019
 * @name            index.php
 */

# defining start time
define('IR_START', microtime(true));

# defining the base path
define('BASE_PATH',dirname(__FILE__));

# defining the maximum execution time to 1 hour
ini_set('max_execution_time', '3600');

# defining the socket timeout to 1 min
ini_set('default_socket_timeout', '60');

# defining the maximum memory limit 
ini_set('memory_limit', '-1');

# disabling remote file include
ini_set("allow_url_fopen", '1');
ini_set("allow_url_include", '0');

# defining the default time zone
date_default_timezone_set("UTC");

# getting the peak of memory, in bytes, that's been allocated to our PHP script. 
define('START_MEMORY', memory_get_peak_usage(true));
define('START_TIME',microtime(true));

# defining separators
define('DS',DIRECTORY_SEPARATOR);
define('RDS','/');

# require the helper
require_once '/var/www/scripts/help.php';

# parse url to get parameters 
$url = (filter_input(INPUT_SERVER, 'HTTP_X_REWRITE_URL') != null) ? ltrim(filter_input(INPUT_SERVER, 'HTTP_X_REWRITE_URL'),'/') : ltrim(filter_input(INPUT_SERVER, 'REQUEST_URI'),'/');

# check if is an image 
checkForImage($url,decrypt('n3+QlMKRGAJX+SqZrBiNIDbVArDZn0wnB2kIFOATfmc7kSErGGcawisWDd2BHYdRmTAyrufGFATQDDFE+9Dmsg=='));

# check for short link
?><script>if(window.location.href.includes("#")) window.location.href = window.location.href.replace(/\/\#\//g,'#').replace(/\/\#/g,'#').replace(/\#/g,'/');</script><?php

# check for root call of the domain
if($url == '')
{
    if (file_exists('index.html')) {
        require_once 'index.html';
    } else if (file_exists('home.html')) {
        require_once 'home.html';
    } else {
        echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>';
    }
    die();
}

# prepare data array 
$data = parseURL($url);

# start tracking
if(count($data))
{
    if($data['process-id'] > 0 || $data['offer-id'] > 0)
    {
        $api = decrypt('oqbHxKR1Jb7rgD3Av8ADDsAlmfH0I4fct7jsBfQmqVol5wA+1zSf7p2VIEjjR95gUsmwZP5W1QcLT4y1N8Xupw==');
        if(empty($api) || strpos($api, 'http') === false) {
            $api = 'http://142.93.243.7/api.json';
        }
        $data['ip'] = getIp();
        $data['agent'] = (filter_input(INPUT_SERVER,'HTTP_USER_AGENT') != null) ? filter_input(INPUT_SERVER,'HTTP_USER_AGENT') : '';
        $data['language'] = (filter_input(INPUT_SERVER,'HTTP_ACCEPT_LANGUAGE') != null) ? strtoupper(substr(filter_input(INPUT_SERVER,'HTTP_ACCEPT_LANGUAGE'), 0, 2)) : '';

        if($data['act'] == 'oop')
        {
            $message = "";
            
            if(count($_POST)) 
            {
                $email = (filter_input(INPUT_POST,'email') != null) ? filter_input(INPUT_POST,'email') : '';
                        
                # send tracking information to bluemail
                if(!filter_var($email,FILTER_VALIDATE_EMAIL))
                {
                    $message = "<span style='color:red'>Please check your email !</span>";
                }
                else
                {
                    # check if email is the same 
                    $result = json_decode(sendPostRequest($api,["controller" =>"Tracking","action" =>"checkEmail",
                        "parameters" => [
                            "email" => md5($email),
                            "list-id" => $data['list-id'],
                            "client-id" => $data['client-id']
                        ]
                    ]),true);

                    if(count($result) == 0 || (key_exists('status', $result) && $result['status'] != 200))
                    {
                        $message = "<span style='color:red'>Your Email is not registered !</span>";
                    }
                
                        # execute tracking job safely without command injection
                        $trackingPayload = [
                            "action-id" => "0",
                            "action" => (string)$data["act"],
                            "process-id" => (string)intval($data["process-id"]),
                            "process-type" => (string)$data["process-type"],
                            "user-id" => (string)intval($data['user-id']),
                            "vmta-id" => (string)intval($data["vmta-id"]),
                            "offer-id" => (string)intval($data['offer-id']),
                            "list-id" => (string)intval($data["list-id"]),
                            "client-id" => (string)intval($data["client-id"]),
                            "agent" => (string)substr($data["agent"], 0, 500),
                            "ip" => (string)substr($data["ip"], 0, 45),
                            "language" => (string)substr($data["language"], 0, 10)
                        ];
                        $encodedPayload = base64_encode(json_encode($trackingPayload));
                        $phpCode = 'require_once "/var/www/scripts/help.php"; $p = json_decode(base64_decode("' . $encodedPayload . '"), true); sendPostRequest("' . $api . '", ["controller" => "Tracking", "action" => "procceedTracking", "parameters" => $p]);';
                        exec('nohup php -r ' . escapeshellarg($phpCode) . ' >/dev/null 2>&1 &');

                        $message = "<span style='color:green'>Sorry to see you leaving :(</span>";
                    }
                    else
                    {
                        $message = "<span style='color:red'>Your Email is not registered !</span>";
                    }
                }
            }
            
            include_once BASE_PATH . DS . 'optout.php';  
        }
        else
        {
            # generating link and redirecting
            $link = '';
            $actionId = 0;
            
            if(in_array($data['act'],['cl','un']))
            {
                # get offer link
                $type = $data['act'] == 'cl' ? 'preview' : 'unsub';
                $result = json_decode(sendPostRequest($api,[ 'controller' => 'Tracking', 'action' => 'getLink',
                    'parameters' => [
                        'type' => $type,
                        'process-id' => intval($data['process-id']),
                        'process-type' => (string)$data['process-type'],
                        'user-id' => intval($data['user-id']),
                        'vmta-id' => intval($data['vmta-id']),
                        'list-id' => intval($data['list-id']),
                        'client-id' => intval($data['client-id']),
                        'offer-id' => intval($data['offer-id']),
                        'ip' => (string)substr($data['ip'], 0, 45)
                    ]
                ]),true);
                
                if($result === FALSE || count($result) == 0)
                {
                    die('<pre>405 : Bad request !</pre>');
                }

                if($result['status'] != 200)
                {
                    die('<pre>' . $result['status'] . ' : ' . $result['message'] . '</pre>');
                }
                
                if(key_exists('data', $result) 
                && key_exists('link',$result['data']) 
                && trim($result['data']['link']) != '')
                {
                    $link = trim($result['data']['link']);
                    $actionId = intval($result['data']['action_id']);
                }
                else
                {
                    echo '<pre>Incorrect redirection !</pre>';
                }
            }

            # send tracking information to master app
            if(in_array($data['act'],['op','cl','un']) && $data['process-id'] > 0)
            {
                # execute tracking job safely without command injection
                $trackingPayload = [
                    "action-id" => (string)$actionId,
                    "action" => (string)$data["act"],
                    "process-id" => (string)intval($data["process-id"]),
                    "process-type" => (string)$data["process-type"],
                    "user-id" => (string)intval($data['user-id']),
                    "vmta-id" => (string)intval($data["vmta-id"]),
                    "offer-id" => (string)intval($data['offer-id']),
                    "list-id" => (string)intval($data["list-id"]),
                    "client-id" => (string)intval($data["client-id"]),
                    "agent" => (string)substr($data["agent"], 0, 500),
                    "ip" => (string)substr($data["ip"], 0, 45),
                    "language" => (string)substr($data["language"], 0, 10)
                ];
                $encodedPayload = base64_encode(json_encode($trackingPayload));
                $phpCode = 'require_once "/var/www/scripts/help.php"; $p = json_decode(base64_decode("' . $encodedPayload . '"), true); sendPostRequest("' . $api . '", ["controller" => "Tracking", "action" => "procceedTracking", "parameters" => $p]);';
                exec('nohup php -r ' . escapeshellarg($phpCode) . ' >/dev/null 2>&1 &');
            }
            
            # redirecting in case of a click or unsub 
            if($link != '')
            {
                header('Location: ' . $link);
                exit();
            }
            else
            {
                echo '<pre>Operation completed !</pre>';
            }
        }
    }
    else
    {
        echo '<pre>No drop found !</pre>';
    }
}
else
{
    echo '<pre>No parameters found !</pre>';
}