<?php
// Hardcode reroute for EU traffic 12/15/16 until we have a better solution in place
if ($_SERVER['REGION'] == "eu") {
    include "{$_SERVER['LIB_ROOT']}/actions/api/resumereroute.php";
    exit;
}

// The entire application runs through this one page.
//
// Common themes:
//
// - "controllers" are in lib/actions/api
// - "models" are in lib/models
// - "views" don't really exist since this is an API -- data is bundled up and spit out as JSON
//
// - non-static classes are initiated with "new Class();"
// - logging to /logs/errors/ is done verbosely with logIt()
// - $page is a class that wraps around Smarty and uses that syntax to handle variables
//    e.g. $page->assign('variable', 'var value');  -> retrieved with {$variable} in template
//    e.g. $page->assign(array('var1'=>'value 1','var2'=>array('key'=>'also value') ));  -> retrieved with {$var1}, {$var2.key} in template
//
// Application themes:
//
// - Partners / internal users access the API with a public and secret key.
//   The application *used to* do OAuth2 with both of theses, but too many partners didn't have the ability to code quickly for that.
//   Also, we didn't have the ability to write OAuth2 client examples for a bunch of languages at the time.
// - Documentation for the API is mostly in the https://affiliates.talentinc.com interface.
// - There are a lot of internal endpoints that aren't advertised in the application.
//

define('APPLICATION', 'products');

// Connect to DB, configure
require_once($_SERVER['LIB_ROOT']."/Common.php");
logIt("HTTP_HOST=".$_SERVER['HTTP_HOST'].", SERVER_NAME=".$_SERVER['SERVER_NAME'].", partner_key=".$_REQUEST['partner_key']);

logIt("REQUEST = ".print_r($_REQUEST,true));

// Set up API response
APIResponse::init();

// Pull client privileges
PartnerAuth::init($_REQUEST['partner_key'], $_REQUEST['secret_key']);

logIt("SSL/TLS Debug: partner=" . PartnerAuth::$row['partner'] . ", partner_key=" . PartnerAuth::$row['partner_key'] . ", protocol={$_SERVER['SSL_PROTOCOL']}");

Processor::init();
Mail::init();

if (!preg_match("/Talent Inc. Internal/", PartnerAuth::$row['partner'])) {
    Mail::assign_brand_id(PartnerAuth::$row['default_brand_id']);
    Events::$experienceSiteId = PartnerAuth::$row['site_id'];
} else {
    Events::$experienceSiteId = null;
}

// _REQUEST 'route' is coming through eg '//v1/emaillookup' from the default.conf routing
// let's clean this up to get the right route 'emaillookup'
$_REQUEST['route'] = str_replace('/', '', $_REQUEST['route']);
$_REQUEST['route'] = str_replace('v1', '', $_REQUEST['route']);

// TO DO: make this dynamic if we add more endpoints
if ($_REQUEST['route'] == "resume") {
    $route['action'] = "resume.POST";
} elseif($_REQUEST['route'] == "reportusers"){
    $route['action'] = "reportusers.GET";
} elseif(!empty($_REQUEST['route'])) {
    $route['action'] = $_REQUEST['route'] . ".POST";
}

// Process code for specific endpoint
if (!empty($route['action'])) {
    $endpoint = "{$_SERVER['LIB_ROOT']}/actions/api/{$route['action']}.php";
    if (file_exists($endpoint)) {
        include($endpoint);
    } else {
        APIResponse::error("400", "Invalid endpoint {$route['action']} (make sure to remove all trailing slashes and newlines)");
    }
    if (!empty($mailtemplate['template'])) {
        $mres = Mail::send($_REQUEST['email'], $mailtemplate['template'], $variables, $options, $sendtime);
    }
    APIResponse::display();

}
?>
