Skip to content

Commit

Permalink
URL redirection is working
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekasprzak committed May 13, 2017
1 parent 6afb048 commit 30ab54d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
5 changes: 4 additions & 1 deletion public-url.shortener/.htaccess
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#RewriteEngine on
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [B,L]

#RewriteRule ^(.*)$ https://ludumdare.com/$1 [R=301,L]
72 changes: 70 additions & 2 deletions src/public-url.redirect/main-redirect.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
<?php
@include __DIR__."/../config.php";
const CONFIG_PATH = "../shrub/";
const SHRUB_PATH = "../shrub/src/";
include_once __DIR__."/".CONFIG_PATH."config.php";
require_once __DIR__."/".SHRUB_PATH."api.php";

echo "huh";
json_Begin();

const DOMAIN_REDIRECT_TABLE = [
'url.ludumdare.org' => 'ludumdare.org',
'url.ludumdare.dev' => 'ludumdare.dev',
'url.jammer.work' => 'jammer.work',
'url.jammer.dev' => 'jammer.dev',

'ldj.am' => 'ldjam.com',
'jam.mr' => 'jammer.vg',
];

$action = json_ArgShift();
$node_id = null;
$specific = false;

if ( !$action ) {
json_EmitFatalError_BadRequest('What?', $RESPONSE);
}

// Do Specific Actions
switch ( $action ) {
case 'SPECIAL': //SPECIAL
$specific = true;

$RESPONSE['ham'] = 1;

break; // case 'SPECIAL': //SPECIAL

default:
if ( !isset(DOMAIN_REDIRECT_TABLE[$_SERVER['SERVER_NAME']]) ) {
json_EmitFatalError_BadRequest('Unknown domain mapping: '.$_SERVER['SERVER_NAME'], $RESPONSE);
}

$url = '//'.DOMAIN_REDIRECT_TABLE[$_SERVER['SERVER_NAME']];

// Do a raw ID decode
if ( $action[0] == '$' ) {
$node_id = intval(substr($action, 1));
}
else {
// TODO: munger
$node_id = 0;
}

if ( !$node_id ) {
json_EmitFatalError_BadRequest('Unknown request', $RESPONSE);
}

$paths = node_GetPathById($node_id, 1);
if ( !$paths ) {
json_EmitFatalError_BadRequest('Unable to trace node: '.$node_id, $RESPONSE);
}
$RESPONSE['paths'] = $paths;

$url .= $paths['path'];
$RESPONSE['url'] = $url;

// Redirect
header('Location: '.$url, true, /*301*/307);
die;

break;
};

json_End();
28 changes: 18 additions & 10 deletions src/shrub/src/core/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,25 @@ function core_RemovePathDotsFromArray($arr) {

/// Internal parser for the API request
function _core_GetAPIRequest() {
// If PATH_INFO is set, then Apache figured out our parts for us //
$PATH = '';

// If PATH_INFO is set, then Apache figured out our parts for us
if ( isset($_SERVER['PATH_INFO']) ) {
$ret = ltrim(rtrim(filter_var($_SERVER['PATH_INFO'], FILTER_SANITIZE_URL), '/'), '/');
if ( empty($ret) /*&& $val !== '0'*/ ) {
return [''];
}
else {
return array_values(array_filter(explode('/',$ret), function($val) {
return !((empty($val) && $val !== '0') || ($val[0] === '.'));
}));
}
$PATH = $_SERVER['PATH_INFO'];
}
// Alternatively if REDIRECT_URL is set, then this was a redirect
else if ( isset($_SERVER['REDIRECT_URL']) ) {
$PATH = $_SERVER['REDIRECT_URL'];
}

$ret = ltrim(rtrim(filter_var($PATH, FILTER_SANITIZE_URL), '/'), '/');
if ( empty($ret) /*&& $val !== '0'*/ ) {
return [''];
}
else {
return array_values(array_filter(explode('/', $ret), function($val) {
return !((empty($val) && $val !== '0') || ($val[0] === '.'));
}));
}

// If PATH_INFO isn't set, assume it's the same as '/'
Expand Down

0 comments on commit 30ab54d

Please sign in to comment.