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: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148:
<?php
$plugin_is_filter = 5 | CLASS_PLUGIN;
$plugin_description = gettext('A plugin to redirect internal URLs. Primarily intended for URLs that otherwise would cause 404 not found errors.');
$plugin_author = "Malte Müller (acrylian)";
$plugin_category = gettext('Admin');
$option_interface = 'redirectorOptions';
zp_register_filter('redirection_handler', 'redirector::handleRequest');
class redirectorOptions {
function __construct() {
setOptionDefault('redirector_catalogue', '');
}
function getOptionsSupported() {
$catalogues = self::getRedirectionFiles();
return array(
gettext('Redirection catalogue') => array(
'key' => 'redirector_catalogue',
'type' => OPTION_TYPE_SELECTOR,
'selections' => $catalogues,
'order' => 0,
'desc' => gettext('Place a JSON or CSV file within /plugins/redirector/ to use for redirecting.')),
gettext('Debug mode') => array(
'key' => 'redirector_debugmode',
'type' => OPTION_TYPE_CHECKBOX,
'order' => 1,
'desc' => gettext('If enabled valid redirections will not be executed but logged in the debug log.'))
);
}
static function getRedirectionFiles() {
$catalogues = array();
$files = getPluginFiles('redirector/*.*');
foreach ($files as $file) {
if (in_array(getSuffix($file), array('csv', 'json'))) {
$catalogues[basename($file)] = $file;
}
}
return $catalogues;
}
}
class redirector {
static function handleRequest($request) {
$redirections = redirector::loadRedirections();
if (!empty($redirections)) {
foreach ($redirections as $key => $val) {
$old = trim($key);
$new = trim($val);
if ($request == $old) {
if (getOption('redirector_debugmode')) {
debugLog('redirector plugin redirection debugging: ' . $old . ' => ' . $new);
return $request;
} else {
return $new;
}
}
}
}
return $request;
}
static function loadRedirections() {
$file = getOption('redirector_catalogue');
$redirections = array();
if (!empty($file)) {
switch (getSuffix($file)) {
case 'csv':
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$redirections[$data[0]] = $data[1];
}
} else {
debugLog(gettext('redirector Error: Could not open and load the redirections catalogue file: ' . $file));
}
break;
case 'json':
$raw = file_get_contents($file);
if ($raw !== false) {
$redirections = json_decode($raw);
unset($raw);
} else {
debugLog(gettext('redirector Error: Could not open and load the redirections catalogue file: ' . $file));
}
break;
}
}
return $redirections;
}
}