Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create redirect on alias update #27

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ core.config.php
config.core.php
.idea
nbproject
/_build/build.properties.php
/_build/build.config.php
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Create redirect on alias update

Now creates records automatically whenever a resource alias is modified. Forked from https://github.com/splittingred/Redirector.

* Modified build.transport.php to support two new events, OnDocFormRender and OnDocFormSave
* Modified plugin.redirector.php to capture alias prior to save (OnDocFormRender). If the alias has changed and the system settings permit then add a new redirect record (OnDocFormSave)
* Created new system setting: redirector.on_alias_update (Yes/No)
* Updated readme.txt
1 change: 0 additions & 1 deletion _build/build.schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ function __construct(& \$xpdo) {
$generator->parseSchema($sources['model'] . 'schema/'.PKG_NAME_LOWER.'.'.$driver.'.schema.xml', $sources['model']);
}


$mtime= microtime();
$mtime= explode(" ", $mtime);
$mtime= $mtime[1] + $mtime[0];
Expand Down
63 changes: 48 additions & 15 deletions _build/build.transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
/* define package names */
define('PKG_NAME','Redirector');
define('PKG_NAME_LOWER','redirector');
define('PKG_VERSION','1.0.3');
define('PKG_RELEASE','pl');
define('PKG_VERSION','1.0.4');
define('PKG_RELEASE','beta');

/* define build paths */
$root = dirname(dirname(__FILE__)).'/';
$sources = array(
'root' => $root,
'root' => $root,
'build' => $root . '_build/',
'data' => $root . '_build/data/',
'resolvers' => $root . '_build/resolvers/',
Expand All @@ -39,7 +39,7 @@
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';

$modx= new modX();
$modx->initialize('mgr');
$modx->initialize('mgr');
echo '<pre>'; /* used for nice formatting of log messages */
$modx->setLogLevel(modX::LOG_LEVEL_INFO);
$modx->setLogTarget('ECHO');
Expand All @@ -50,23 +50,39 @@
$builder->registerNamespace(PKG_NAME_LOWER,false,true,'{core_path}components/'.PKG_NAME_LOWER.'/');

/* add plugin */
$modx->log(modX::LOG_LEVEL_INFO,'Packaging in plugin...');
$plugin= $modx->newObject('modPlugin');
$modx->log(modX::LOG_LEVEL_INFO,'Packaging in Redirector plugin...');

$plugin = $modx->newObject('modPlugin');

$plugin->fromArray(array(
'id' => 1,
'name' => 'Redirector',
'description' => 'Handles site redirects.',
'plugincode' => getSnippetContent($sources['elements'].'plugins/plugin.redirector.php'),
),'',true,true);
$events = array();
$events['OnPageNotFound']= $modx->newObject('modPluginEvent');
$events['OnPageNotFound']->fromArray(array(
'event' => 'OnPageNotFound',
'priority' => 0,
'propertyset' => 0,
),'',true,true);
$plugin->addMany($events);
unset($events);

$events = array();
$events['OnPageNotFound'] = $modx->newObject('modPluginEvent');
$events['OnPageNotFound']->fromArray(array(
'event' => 'OnPageNotFound',
'priority' => 0,
'propertyset' => 0,
),'',true,true);
$events['OnDocFormRender'] = $modx->newObject('modPluginEvent');
$events['OnDocFormRender']->fromArray(array(
'event' => 'OnDocFormRender',
'priority' => 0,
'propertyset' => 0,
),'',true,true);
$events['OnDocFormSave'] = $modx->newObject('modPluginEvent');
$events['OnDocFormSave']->fromArray(array(
'event' => 'OnDocFormSave',
'priority' => 0,
'propertyset' => 0,
),'',true,true);

$plugin->addMany($events);
unset($events);

$attributes= array(
xPDOTransport::UNIQUE_KEY => 'name',
Expand Down Expand Up @@ -94,6 +110,21 @@
));
$builder->putVehicle($vehicle);

/* settings */
$settings = include_once $sources['data'].'transport.settings.php';
$attributes= array(
xPDOTransport::UNIQUE_KEY => 'key',
xPDOTransport::PRESERVE_KEYS => true,
xPDOTransport::UPDATE_OBJECT => false,
);
if (!is_array($settings)) { $modx->log(modX::LOG_LEVEL_FATAL,'Adding settings failed.'); }
foreach ($settings as $setting) {
$vehicle = $builder->createVehicle($setting,$attributes);
$builder->putVehicle($vehicle);
}
$modx->log(modX::LOG_LEVEL_INFO,'Packaged in '.count($settings).' system settings.'); flush();
unset($settings,$setting,$attributes);

/* load menu */
$modx->log(modX::LOG_LEVEL_INFO,'Packaging in menu...');
$menu = include $sources['data'].'transport.menu.php';
Expand All @@ -111,10 +142,12 @@
),
),
));

$modx->log(modX::LOG_LEVEL_INFO,'Adding in PHP resolvers...');
$vehicle->resolve('php',array(
'source' => $sources['resolvers'] . 'resolve.tables.php',
));

$builder->putVehicle($vehicle);
unset($vehicle,$menu);

Expand Down
36 changes: 27 additions & 9 deletions _build/data/transport.plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,32 @@
'description' => 'Handles site redirects.',
'plugincode' => file_get_contents($sources['elements'].'plugins/plugin.redirector.php'),
),'',true,true);
$events = array();
$events['OnPageNotFound']= $modx->newObject('modPluginEvent');
$events['OnPageNotFound']->fromArray(array(
'event' => 'OnPageNotFound',
'priority' => 0,
'propertyset' => 0,
),'',true,true);
$plugins[1]->addMany($events);
unset($events);

$events = array();
$events['OnPageNotFound']= $modx->newObject('modPluginEvent');
$events['OnPageNotFound']->fromArray(array(
'event' => 'OnPageNotFound',
'priority' => 0,
'propertyset' => 0,
),'',true,true);

/* new OnDocFormRender event, added by cc */
$events['OnDocFormRender'] = $modx->newObject('modPluginEvent');
$events['OnDocFormRender']->fromArray(array(
'event' => 'OnDocFormRender',
'priority' => 0,
'propertyset' => 0,
),'',true,true);

/* new OnDocFormSave event, added by cc */
$events['OnDocFormSave'] = $modx->newObject('modPluginEvent');
$events['OnDocFormSave']->fromArray(array(
'event' => 'OnDocFormSave',
'priority' => 0,
'propertyset' => 0,
),'',true,true);

$plugins[1]->addMany($events);
unset($events);

return $plugins;
23 changes: 23 additions & 0 deletions _build/data/transport.settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$s = array(
'Default' => array(
'on_alias_update' => 'Yes',
),
);

$settings = array();
foreach ($s as $area => $sets) {
foreach ($sets as $key => $value) {
$settings['redirector.'.$key] = $modx->newObject('modSystemSetting');
$settings['redirector.'.$key]->set('key', 'redirector.'.$key);
$settings['redirector.'.$key]->fromArray(array(
'value' => $value,
'xtype' => 'textfield',
'namespace' => 'redirector',
'area' => $area
));
}
}

return $settings;
4 changes: 4 additions & 0 deletions core/components/redirector/docs/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changelog file for Redirector component.

Redirector 1.0.4 (beta)
====================================
- Redirect created on alias update. Enable/disable via System Settings

Redirector 1.0.3
====================================
- Add sqlsrv support
Expand Down
2 changes: 1 addition & 1 deletion core/components/redirector/docs/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Version: 1.0
Since: April 21st, 2010
Author: Shaun McCormick <[email protected]> and Jason Coward <[email protected]>

Handles 301 redirects for your site.
Handles 301 redirects for your site.
109 changes: 71 additions & 38 deletions core/components/redirector/elements/plugins/plugin.redirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,86 @@
/**
* @package redirector
*/

/* load redirector class */
$corePath = $modx->getOption('redirector.core_path',$scriptProperties,$modx->getOption('core_path').'components/redirector/');
$redirector = $modx->getService('redirector','Redirector',$corePath.'model/redirector/',$scriptProperties);
if (!($redirector instanceof Redirector)) return '';

/* handle redirects */
$search = $_SERVER['REQUEST_URI'];
$baseUrl = $modx->getOption('base_url',null,MODX_BASE_URL);
if (!empty($baseUrl) && $baseUrl != '/' && $baseUrl != ' ') {
$search = str_replace($baseUrl,'',$search);
}
$search = ltrim($search,'/');
$extPos = strrpos($search, '.');
if ($extPos) {
if ($search) {
$redirect = $modx->getObject('modRedirect', array(
'pattern' => $search,
'active' => 1,
));
if ($redirect) {
$target = $redirect->get('target');
$modx->parser->processElementTags('', $target, true, true);
if ($target != $modx->resourceIdentifier && $target != $search) {
if (!strpos($target, '://')) {
$target = $modx->getOption('site_url').$target;
switch ($modx->event->name) {
case 'OnPageNotFound':
$search = $_SERVER['REQUEST_URI'];
$baseUrl = $modx->getOption('base_url',null,MODX_BASE_URL);
if (!empty($baseUrl) && $baseUrl != '/' && $baseUrl != ' ') {
$search = str_replace($baseUrl,'',$search);
}
$search = ltrim($search,'/');
$extPos = strrpos($search, '.');
if ($extPos) {
if ($search) {
$redirect = $modx->getObject('modRedirect', array(
'pattern' => $search,
'active' => 1,
));
if ($redirect) {
$target = $redirect->get('target');
$modx->parser->processElementTags('', $target, true, true);
if ($target != $modx->resourceIdentifier && $target != $search) {
if (!strpos($target, '://')) {
$target = $modx->getOption('site_url').$target;
}
$modx->log(modX::LOG_LEVEL_INFO, 'Redirector plugin redirecting request for ' . $search . ' to ' . $target);
header('HTTP/1.1 301 Moved Permanently');
$modx->sendRedirect($target);
}
}
$modx->log(modX::LOG_LEVEL_INFO, 'Redirector plugin redirecting request for ' . $search . ' to ' . $target);
header('HTTP/1.1 301 Moved Permanently');
$modx->sendRedirect($target);
}
$search = substr($search, 0, $extPos);
}
}
$search = substr($search, 0, $extPos);
}
if ($search) {
$redirect = $modx->getObject('modRedirect', array('pattern' => $search, 'active' => 1));
if ($redirect) {
$target = $redirect->get('target');
$modx->parser->processElementTags('', $target, true, true);
if ($target != $modx->resourceIdentifier && $target != $search) {
if (!strpos($target, '://')) {
$target = $modx->getOption('site_url').$target;
if ($search) {
$redirect = $modx->getObject('modRedirect', array('pattern' => $search, 'active' => 1));
if ($redirect) {
$target = $redirect->get('target');
$modx->parser->processElementTags('', $target, true, true);
if ($target != $modx->resourceIdentifier && $target != $search) {
if (!strpos($target, '://')) {
$target = $modx->getOption('site_url').$target;
}
$modx->log(modX::LOG_LEVEL_INFO, 'Redirector plugin redirecting request for ' . $search . ' to ' . $target);
header('HTTP/1.1 301 Moved Permanently');
$modx->sendRedirect($target);
}
}
}
break;
case 'OnDocFormRender':
if ($mode == 'upd')
$_SESSION['modx_resource_alias'] = $resource->get('alias');
break;
case 'OnDocFormSave':
/* if alias has changed, add to redirects */
$on_alias_update = $modx->getOption('redirector.on_alias_update',null,'No');
$new_alias = $resource->get('alias');

if ($mode == 'upd' && $on_alias_update == 'Yes') {
$old_alias = $_SESSION['modx_resource_alias'];
if ($old_alias != $new_alias) {
/* alias changed */
$redirect = $modx->getObject('modRedirect', array('pattern' => $old_alias, 'active' => 1));
if (empty($redirect)) {
/* no record for old alias */
$new_redirect = $modx->newObject('modRedirect');
$new_redirect->fromArray(array('pattern' => $old_alias, 'target' => $new_alias, 'active' => 1));

if ($new_redirect->save() == false) {
return $modx->error->failure($modx->lexicon('redirector.redirect_err_save'));
}
}
}
$modx->log(modX::LOG_LEVEL_INFO, 'Redirector plugin redirecting request for ' . $search . ' to ' . $target);
header('HTTP/1.1 301 Moved Permanently');
$modx->sendRedirect($target);
}
}

$_SESSION['modx_resource_alias'] = $new_alias;
break;
}

return;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$xpdo_meta_map = array (
'xPDOSimpleObject' =>
array (
0 => 'modRedirect',
),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$xpdo_meta_map = array (
'xPDOSimpleObject' =>
array (
0 => 'modRedirect',
),
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/
$xpdo_meta_map['modRedirect']= array (
'package' => 'redirector',
'version' => NULL,
'table' => 'redirects',
'extends' => 'xPDOSimpleObject',
'fields' =>
array (
'pattern' => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/
$xpdo_meta_map['modRedirect']= array (
'package' => 'redirector',
'version' => NULL,
'table' => 'redirects',
'extends' => 'xPDOSimpleObject',
'fields' =>
array (
'pattern' => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<object class="modRedirect" table="redirects" extends="xPDOSimpleObject">
<field key="pattern" dbtype="nvarchar" precision="255" phptype="string" null="false" default="" index="unique" />
<field key="target" dbtype="nvarchar" precision="255" phptype="string" null="false" default="" index="index" />
<field key="active" dbtype="bit" phptype="boolean" null="false" default="1" index="index" />
<field key="active" dbtype="bit" phptype="boolean" null="false" default="1" index="index" />
</object>
</model>