1<?php
2/**
3 * Affiliate Cookie Tracking + Redirection Handler
4 *
5 * @package WHMCS
6 * @author WHMCS Limited <development@whmcs.com>
7 * @copyright Copyright (c) WHMCS Limited 2005-2018
8 * @license https://www.whmcs.com/license/ WHMCS Eula
9 * @version $Id$
10 * @link https://www.whmcs.com/
11 */
12
13use WHMCS\Affiliate\Referrer;
14use WHMCS\Carbon;
15use WHMCS\Cookie;
16
17define("CLIENTAREA",true);
18
19require("init.php");
20
21// if affiliate id is present, update visitor count & set cookie
22if ($aff = $whmcs->get_req_var('aff')) {
23 update_query("tblaffiliates",array("visitors"=>"+1"),array("id"=>$aff));
24 Cookie::set('AffiliateID',$aff,'3m');
25
26 $referrer = trim($_SERVER['HTTP_REFERER'] ?? '');
27 Referrer::firstOrCreate([
28 'affiliate_id' => $aff,
29 'referrer' => $referrer,
30 ])->hits()->create([
31 'affiliate_id' => $aff,
32 'created_at' => Carbon::now()->toDateTimeString(),
33 ]);
34
35}
36
37/**
38 * Executes when a user has clicked an affiliate referral link.
39 *
40 * @param int $affiliateId The unique id of the affiliate that the link belongs to
41 */
42run_hook("AffiliateClickthru", array(
43 'affiliateId' => $aff,
44));
45
46// if product id passed in, redirect to order form
47if ($pid = $whmcs->get_req_var('pid')) redir("a=add&pid=".(int)$pid,"cart.php");
48
49// if product group id passed in, redirect to product group
50if ($gid = $whmcs->get_req_var('gid')) redir("gid=".(int)$gid,"cart.php");
51
52// if register = true, redirect to registration form
53if ($whmcs->get_req_var('register')) redir("","register.php");
54
55// if gocart = true, redirect to cart with request params
56if ($whmcs->get_req_var('gocart')) {
57 $reqvars = '';
58 foreach ($_GET AS $k=>$v) $reqvars .= $k.'='.urlencode($v).'&';
59 redir($reqvars,"cart.php");
60}
61
62// perform redirect
63header("HTTP/1.1 301 Moved Permanently");
64header("Location: ".$whmcs->get_config('Domain'),true,301);
65