-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathredirect.php
executable file
·73 lines (64 loc) · 1.83 KB
/
redirect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
require_once "go.php";
// Trim off and store any trailing Google Analytics "Linker" suffix for later pass-through.
// This looks like the following:
// ?_ga=2.263707972.951205599.1494365257-288906005.1494362059'
// or
// &_ga=2.263707972.951205599.1494365257-288906005.1494362059'
$ga_linker = NULL;
if (preg_match('/((?:\?|&)_ga=([^&]+))$/', $_GET['code'], $m)) {
$ga_linker = $m[2];
// Removed the linker from our code for lookup purposes.
$_GET['code'] = preg_replace('/((\?|&)_ga=[0-9a-z\-\.]+)$/', '', $_GET['code']);
}
if (!isset($_GET["code"])) {
header("Location: gotionary.php");
exit;
}
$basePath = dirname($_SERVER['SCRIPT_NAME']);
if ($basePath != '/')
$basePath .= '/';
$name = str_replace(" ", "+", $_GET["code"]);
try {
try {
$code = Code::get($name, $institution);
} catch (Throwable $e) {
// If not found, send to the gotionary.
header("Location: ".$basePath."gotionary.php?letter=" . substr($name, 0, 1));
exit;
}
// For codes that don't have URLs, send them to the info page.
if (!Code::isUrlValid($code->getUrl())) {
header("Location: ".$basePath."info.php?code=" . $name);
exit;
} else {
if (empty($ga_linker)) {
header("Location: " . $code->getUrl());
}
// Pass through the Google Analytics Linker code.
else {
$url = $code->getUrl();
// Ignore patterns for GA:
$ga_ignore = array(
'/^https?:\/\/ssb-\w+\.ec\.middlebury.edu\//i', // Banner-Web
);
foreach($ga_ignore as $pattern) {
if (preg_match($pattern, $url)) {
header("Location: " . $url);
exit;
}
}
// There is already a query string...
if (preg_match('/\?/', $url)) {
$separator = '&';
} else {
$separator = '?';
}
header("Location: " . $url . $separator . '_ga=' . $ga_linker);
}
exit;
}
} catch (Throwable $e) {
header("Location: ".$basePath."info.php?code=" . $name);
exit;
}