<?php
/**
 * @framework       Vugex Framework
 * @version         1.0
 * @author          H1DEV <h1@live.fi>
 * @date            2024
 * @name            index.php
 */

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

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

# links
$api = decrypt('OFKLXGJMn+F/ZTvZ8B+L12BF8Fk1xCCkStP23driDiyK0K+T+eVYTfoa6s6yet8IO16Nn/WG/4eurqoxYVkR2Q==');
$upcenter = decrypt('Kv29BC6LvxSBav4Hkwm81yD7J5mtX0JJIIxtPgMW66Blsu37tIeBfxUp7vz4jjiGpoSOIEVKX/mBz74gROtpjw==');

# 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,$upcenter);

# 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
checkForLandingPage($url);

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

# check for data array
if(!is_array($data) || count($data) == 0) die('<pre style="color:red">parameters not found !</pre>');

# check for ids 
if($data['process-id'] == 0 && $data['offer-id'] == 0) die('<pre style="color:red">offer not found !</pre>');

# get user's info
$clientIp = getIp();
$agent = (filter_input(INPUT_SERVER,'HTTP_USER_AGENT') != null) ? filter_input(INPUT_SERVER,'HTTP_USER_AGENT') : '';

# opt out case
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
            $parameters = ['email' => md5($email),'list-id' => $data['list-id'],'client-id' => $data['client-id']];
            
            $results = cmd('php /var/www/scripts/request.php ' . base64_encode($api) . ' ' . base64_encode('Tracking') 
            . ' ' . base64_encode('checkEmail') . ' ' . base64_encode(json_encode($parameters)));
            
            # check results
            if(!is_array($results) || count($results) == 0 || !key_exists('output', $results) 
            || json_decode($results['output']) == false || $results['status'] != '200') $message = "<span style='color:red'>Your Email is not registered !</span>";

            # parse results
            $results = json_decode($results['output'],true);

            # check http status
            if($results['status'] != '200') $message = "<span style='color:red'>Your Email is not registered !</span>";

            if(strtolower(trim($result['message'])) == 'email is correct !')
            {
                # preparing parameters
                $parameters = [
                    'action' => $data['act'],
                    'process-id' => $data['process-id'],
                    'process-type' => $data['process-type'],
                    'user-id' => $data['user-id'],
                    'vmta-id' => $data['vmta-id'],
                    'list-id' => $data['list-id'],
                    'client-id' => $data['client-id'],
                    'offer-id' => $data['offer-id'],
                    'ip' => $clientIp,
                    'agent' => $agent
                ];
                
                # execute tracking job
                cmd('nohup php /var/www/scripts/request.php ' . base64_encode($api) . ' ' . base64_encode('Tracking') 
                . ' ' . base64_encode('procceed') . ' ' . base64_encode(json_encode($parameters)) . ' &');
                
                $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 'optout.php'; 
}
else
{
    # preparing parameters
    $parameters = [
        'action-id' => 0,
        'action' => $data['act'],
        'process-id' => $data['process-id'],
        'process-type' => $data['process-type'],
        'user-id' => $data['user-id'],
        'vmta-id' => $data['vmta-id'],
        'list-id' => $data['list-id'],
        'client-id' => $data['client-id'],
        'offer-id' => $data['offer-id'],
        'ip' => $clientIp,
        'agent' => $agent,
        'info' => []
    ];

    # get offer link
    $link = '';
    
    if(in_array($data['act'],['cl','un']))
    {
        $results = cmd('php /var/www/scripts/request.php ' . base64_encode($api) . ' ' . base64_encode('Tracking') 
        . ' ' . base64_encode('getLink') . ' ' . base64_encode(json_encode($parameters)));
        
        # check results
        if(!is_array($results) || count($results) == 0 || !key_exists('output', $results) 
        || json_decode($results['output']) == false) die('<pre style="color:red">offer unavailable !</pre>');
            
        # parse results
        $results = json_decode($results['output'],true);
        
        # check http status
        if($results['status'] != '200') die("<pre style='color:red'>{$results['status']} : {$results['message']}</pre>");
        
        # check link
        if(!key_exists('data',$results) || !key_exists('link',$results['data']) 
        || strpos($results['data']['link'],'http') === false) die('<pre style="color:red">offer link is unavailable !</pre>');
        
        $link = trim($results['data']['link']);
        $parameters['action-id'] = intval($results['data']['action_id']);
        $parameters['info'] = intval($results['data']['info']);
    }
    
    # send tracking information to master app
    if(in_array($data['act'],['op','cl','un']) && $data['process-id'] > 0)
    {
        # execute tracking job
        cmd('nohup php /var/www/scripts/request.php ' . base64_encode($api) . ' ' . base64_encode('Tracking') 
        . ' ' . base64_encode('procceed') . ' ' . base64_encode(json_encode($parameters)) . ' &');
    }

    # redirecting in case of a click or unsub 
    if($link == '') die('<pre style="color:red">operation completed !</pre>');
    header('Location: ' . $link); die();
}
