-
Notifications
You must be signed in to change notification settings - Fork 1
/
RedirectPlugin.php
141 lines (121 loc) · 4.45 KB
/
RedirectPlugin.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
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
<?php
if (!defined('REDIRECT_DIR')) {
define('REDIRECT_DIR', dirname(__FILE__));
}
require_once 'functions.php';
class RedirectPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'initialize',
'install',
'uninstall',
'define_acl',
);
protected $_filters = array('admin_navigation_main', 'public_navigation_admin_bar');
/**
* Install the plugin.
*/
public function hookInstall()
{
// Create the table.
$db = $this->_db;
$sql = "CREATE TABLE `{$db->prefix}redirect` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key: Unique redirect ID.',
`uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The users.uid of the user who created the redirect.',
`source` varchar(255) NOT NULL COMMENT 'The source path to redirect from.',
`redirect` varchar(255) NOT NULL COMMENT 'The destination path to redirect to.',
`count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The number of times the redirect has been used.',
`access` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The timestamp of when the redirect was last accessed.',
`enabled` BOOLEAN NOT NULL DEFAULT '1' COMMENT 'Boolean indicating whether the redirect is enabled (visible to non-administrators).',
PRIMARY KEY (`id`),
UNIQUE KEY `source` (`source`)
) COMMENT='Stores information on redirects.';";
$db->query($sql);
$this->_installOptions();
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
// Drop the table.
$db = $this->_db;
$sql = "DROP TABLE IF EXISTS `{$db->prefix}redirect`";
$db->query($sql);
$this->_uninstallOptions();
}
public function hookInitialize() {
$db = get_db();
$source = trim($_SERVER['REQUEST_URI'], '/');
$d_args = array(
$source,
trim(str_replace(trim(public_url(''), '/'), '', $source), '/'),
);
$redirect = $db->query("SELECT id, redirect FROM `{$db->prefix}redirect`
WHERE (source = ? OR source = ?) AND enabled = 1", $d_args)->fetchObject();
if ($redirect) {
$db->query("UPDATE `{$db->prefix}redirect` SET count = count + 1, access = ? WHERE id = ?", array(time(), $redirect->id));
header('Location: /' . $redirect->redirect, TRUE, 301);
exit;
}
if (!is_null(current_user())) {
queue_css_string('a.redirect {
padding-left: 15px !important;
background: transparent url('.url('').'plugins/Redirect/views/shared/img/add.png) no-repeat 0 center;
line-height: 30px;
}');
}
}
public function hookDefineAcl($args)
{
// Restrict access to super and admin users.
$args['acl']->addResource('Redirect_Index');
}
public function filterAdminNavigationMain($nav) {
$nav[] = array(
'label' => __('Redirects'),
'uri' => url('redirect'),
'resource' => 'Redirect_Index',
'privilege' => 'index'
);
return $nav;
}
public function filterPublicNavigationAdminBar($navLinks)
{
$view = get_view();
if (isset($view->item)) {
$record = $view->item;
$aclRecord = $view->item;
}
if (isset($view->collection)) {
$record = $view->collection;
$aclRecord = $view->collection;
}
if (isset($view->simple_pages_page)) {
$record = $view->simple_pages_page;
$aclRecord = 'SimplePages_Page';
}
if (isset($view->exhibit_page)) {
$record = $view->exhibit_page;
$aclRecord = $view->exhibit;
}
if (!isset($record)) {
return $navLinks;
}
if (is_allowed($aclRecord, 'edit')) {
$components = explode('?', trim($_SERVER['REQUEST_URI'], '/'));
$editLinks = array(
'redirect' => array(
'label'=>'Add Redirect',
'uri' => admin_url('redirect/index/add', array(
'redirect' => array_pop($components)
)),
'class' => 'redirect',
'target' => '_blank',
),
);
}
$navLinks = array_merge($editLinks, $navLinks);
return $navLinks;
}
}