-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathting_proxy.module
203 lines (178 loc) · 6.15 KB
/
ting_proxy.module
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* @file
* Implements a proxy to rewrite online material urls to library specific urls.
*/
/**
* Implements hook_menu().
*/
function ting_proxy_menu() {
$items = array();
// Administration UI.
$items['admin/config/ting/proxy'] = array(
'title' => 'Proxy settings',
'description' => 'Ting Proxy settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_proxy_settings_form'),
'access arguments' => array('administer ting settings'),
'file' => 'ting_proxy.admin.inc',
'file path' => drupal_get_path('module', 'ting_proxy') . '/includes',
);
return $items;
}
/**
* Implements of hook_element_info().
*
* It defines a new form element named ting_proxy_hostname.
*
* It calls the theme function theme_ting_proxy_hostname, if not defined
* no elements will be displayed.
*
*/
function ting_proxy_element_info() {
$types = array();
$types['ting_proxy_hostname'] = array(
'#input' => TRUE,
'#process' => array('ting_proxy_hostname_element_process'),
'#element_validate' => array('ting_proxy_hostname_element_validate'),
);
return $types;
}
/**
* Implements hook_theme().
*
* Defines the new form element "ting_proxy_hostname" theme function. Which is
* required by hook_elements.
*/
function ting_proxy_theme() {
return array(
'ting_proxy_hostname' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Theme function to format the custom form element (ting_proxy_hostname).
*/
function theme_ting_proxy_hostname($element) {
return theme('form_element', $element);
}
/**
* Process callback to expand our form element into several fields.
*/
function ting_proxy_hostname_element_process($element, $form_state) {
$element['#tree'] = TRUE;
$element['hostname'] = array(
'#type' => 'textfield',
'#title' => t('Hostname'),
'#required' => TRUE,
'#size' => 30,
'#default_value' => ((isset($element['#value']['hostname'])) ? $element['#value']['hostname'] : ''),
'#description' => t('Enter hostname, e.g. <em>magazine.example.com</em>. Do not include the <em>http://</em> prefix.'),
'#attributes' => array('class' => array('url-text-field')),
);
// Create collapsible field set, because only a small subset will use these
// options.
$element['expression'] = array(
'#type' => 'fieldset',
'#title' => t('Replacement'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$element['expression']['regex'] = array(
'#type' => 'textfield',
'#title' => t('Regular expression'),
'#size' => 30,
'#default_value' => isset($element['#value']['expression']) ? $element['#value']['expression']['regex'] : '',
'#description' => t('Use regular expression to substitut parts of the url, e.g. "<em>%regex</em>".', array('%regex' => '/bib\w{5,6}/')),
);
$element['expression']['replacement'] = array(
'#type' => 'textfield',
'#title' => t('Replacement'),
'#size' => 30,
'#default_value' => isset($element['#value']['expression']) ? $element['#value']['expression']['replacement'] : '',
'#description' => t('The replacement value for the regular expression.'),
);
// Enables the option of not prefixing the url.
// In some cases it's not needed and replacements still work.
$element['disable_prefix'] = array(
'#type' => 'checkbox',
'#title' => t('Do not use proxy prefix for this hostname'),
'#default_value' => (isset($element['#value']['disable_prefix']) ? $element['#value']['disable_prefix'] : FALSE),
);
// Placeholder to mark proxies for removal.
$element['removed'] = array(
'#type' => 'hidden',
'#default_value' => 0,
'#attributes' => array('class' => array('removed')),
);
$element['remove'] = array(
'#type' => 'button',
'#value' => t('Remove'),
'#attributes' => array('class' => array('remove')),
);
return $element;
}
/**
* Our element's validation function.
*/
function ting_proxy_hostname_element_validate($element, &$form_state) {
return $element;
}
/**
* Implements hook_ting_online_url_alter().
*
* Hooks into the url and rewrites it based on the configuration.
*/
function ting_proxy_ting_online_url_alter(&$url) {
$proxy = variable_get('ting_proxy', array());
// Alter the url if we have a valid proxy configuration,
if (isset($proxy['prefix']) && $proxy['prefix'] &&
isset($proxy['hostnames']) && is_array($proxy['hostnames'])) {
$url = ting_proxy_rewrite_download_url($url, $proxy['prefix'], $proxy['hostnames']);
}
}
/**
* Rewrite the URL of external restricted-access resources.
*
* This allow access to the resources through the library's proxy server.
*
* @param string $url
* The url to rewrite
* @param string $proxy_url
* The url to the proxy server to prepend to the url
* @param array $hostnames
* Array of hostname configurations containing keys
* - hostname: The hostname for which the configuration applies in the
* format www.example.com
* - expression: An array with a regex and a replacement to support
* replacing values in the url e.g. replace /librarycode/ with /123456/
* - disable_prefix: Do not prefix the url with the proxy url
*
* @return string
* The rewritten url.
*/
function ting_proxy_rewrite_download_url($url, $proxy_url, $hostnames) {
$host = parse_url($url, PHP_URL_HOST);
// Search host names (can this be optimized ? yes - save data in other format).
foreach ($hostnames as $config) {
if ($host == $config['hostname']) {
// Rewrite/convert url using regex.
if ((isset($config['expression']['regex']) && !empty($config['expression']['regex'])) &&
(isset($config['expression']['replacement']) && !empty($config['expression']['replacement']))) {
$url = preg_replace($config['expression']['regex'],
$config['expression']['replacement'],
$url);
}
// Add prefix, if chosen.
if (!$config['disable_prefix']) {
// The URL is not encoded as it's send on to online resources proxies
// (ezproxy), which fails if the url is encoded.
$url = $proxy_url . $url;
}
// Exit the foreach loop.
break;
}
}
return $url;
}