-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathplugin.autoredirector.php
More file actions
executable file
·124 lines (121 loc) · 5.37 KB
/
Copy pathplugin.autoredirector.php
File metadata and controls
executable file
·124 lines (121 loc) · 5.37 KB
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
<?php
$resourceEvents = array('OnBeforeDocFormSave', 'OnDocFormSave');
if (in_array($modx->event->name, $resourceEvents)) {
foreach($scriptProperties as & $object){
if(
is_object($object)
AND $object instanceof modResource
AND $original = $modx->getObject('modResource', $object->id)
){
$resource = $object;
break;
}
}
}
switch ($modx->event->name) {
case "OnManagerPageInit":
$cssFile = MODX_ASSETS_URL.'components/autoredirector/css/mgr/main.css';
$modx->regClientCSS($cssFile);
break;
case "OnBeforeDocFormSave":
$resources = array(
$resource,
$modx->getObject('modResource',$resource->get('parent'))
);
if (!empty($resource) && ($child_ids = $modx->getChildIds($resource->id, 50, array('context' => $resource->context_key)))) {
$children = $modx->getIterator('modResource', array('id:IN' => $child_ids));
foreach ($children as $child) {
$resources[] = $child;
}
}
case "OnResourceBeforeSort":
if (empty($resources)) {
foreach ($nodes as $node) {
$resources[] = $modx->getObject('modResource',$node['id']);
}
}
foreach ($resources as $res) {
if (!empty($res)) {
if (!$res->getProperty('old_uri','autoredirector')) {
$res->setProperty('old_uri',$res->get('uri'),'autoredirector');
$res->save();
}
}
}
break;
case "OnDocFormSave":
$resources = array(
$resource,
$modx->getObject('modResource',$resource->get('parent'))
);
if (!empty($resource) && ($child_ids = $modx->getChildIds($resource->id, 50, array('context' => $resource->context_key)))) {
$children = $modx->getIterator('modResource', array('id:IN' => $child_ids));
foreach ($children as $child) {
$resources[] = $child;
}
}
case "OnResourceSort":
if (empty($resources)) {
foreach ($nodesAffected as $node) {
$resources[] = $node;
}
}
$modelPath = $modx->getOption('autoredirector_core_path',null,$modx->getOption('core_path').'components/autoredirector/').'model/';
$modx->addPackage('autoredirector', $modelPath);
$processorProps = array('processors_path' => $modx->getOption('autoredirector_core_path',null,$modx->getOption('core_path').'components/autoredirector/').'processors/');
foreach ($resources as $res) {
if (!empty($res)) {
$old_uri = $res->getProperty('old_uri','autoredirector');
$current_uri = $res->getAliasPath($res->get('alias'));
if ($old_uri && $current_uri != $old_uri) {
$currentRuleQ = array('uri' => $current_uri);
if (!$modx->getOption('global_duplicate_uri_check')) {
$currentRuleQ['context_key'] = $res->get('context_key');
}
if ($currentRule = $modx->getObject('arRule', $currentRuleQ)) {
$response = $modx->runProcessor('mgr/item/remove', $currentRule->toArray(), $processorProps);
if ($response->isError()) {
$modx->log(modX::LOG_LEVEL_ERROR, 'AutoRedirector removing error. Message: '.$response->getMessage());
}
}
$arRule = array('uri' => $old_uri
, 'context_key' => $res->get('context_key')
, 'res_id' => $res->get('id'));
if (!$modx->getObject('arRule', $arRule)) {
$response = $modx->runProcessor('mgr/item/create', $arRule, $processorProps);
if ($response->isError()) {
$modx->log(modX::LOG_LEVEL_ERROR, 'AutoRedirector creating error. Message: '.$response->getMessage());
}
}
}
$res->setProperty('old_uri',$current_uri,'autoredirector');
$res->save();
}
}
break;
case "OnPageNotFound":
$uri = $_SERVER['REQUEST_URI'];
$uri = str_replace($modx->getOption("site_url"),"",$uri);
if (substr($uri, 0, 1) == "/") $uri = substr($uri, 1);
$getparams = '';
if (mb_strpos($uri,'?') !== FALSE) {
$uri_split = explode('?',$uri);
$uri = $uri_split[0];
if(!$modx->getOption("autoredirector_clear_get",null, 0)) {
$getparams = $uri_split[1];
}
}
$uri = urldecode($uri);
$RuleQ = array('uri' => $uri);
if (!$modx->getOption('global_duplicate_uri_check')) {
$RuleQ['context_key'] = $modx->context->get('key');
}
$modelPath = $modx->getOption('autoredirector_core_path',null,$modx->getOption('core_path').'components/autoredirector/').'model/';
$modx->addPackage('autoredirector', $modelPath);
if ($Rule = $modx->getObject('arRule', $RuleQ)) {
if ($url = $modx->makeUrl($Rule->get('res_id'),'',$getparams)) {
$modx->sendRedirect($url,array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
}
}
break;
}