/
pugpig_article_rewrite.php
270 lines (225 loc) · 8.2 KB
/
pugpig_article_rewrite.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* @file
* Pugpig Article Rewrite
*/
/*
Licence:
==============================================================================
(c) 2011, Kaldor Holdings Ltd
This module is released under the GNU General Public License.
See COPYRIGHT.txt and LICENSE.txt
/************************************************************************
Change the permalink to rewrite outgoing links
*************************************************************************/
require __DIR__ . '/vendor/autoload.php';
require_once 'common/url_to_absolute/add_relative_dots.php';
require_once 'common/pugpig_manifests.php';
require_once 'common/pugpig_utilities.php';
require_once 'pugpig_url_rewrites.php';
if (!class_exists('\simple_html_dom_node')) {
require_once 'vendor/simple-html-dom/simple-html-dom/simple_html_dom.php';
}
add_filter('post_link','pugpig_permalink');
add_filter('post_type_link','pugpig_permalink');
add_filter('page_link','pugpig_permalink');
add_filter('category_link','pugpig_permalink');
add_filter('tag_link','pugpig_permalink');
add_filter('author_link','pugpig_permalink');
add_filter('day_link','pugpig_permalink');
add_filter('month_link','pugpig_permalink');
add_filter('year_link','pugpig_permalink');
function pugpig_permalink($permalink, $endpoint = null)
{
// Ignore this if we are outputting a feed
if (pugpig_is_pugpig_url()) {
$permalink = pugpig_rewrite_html_url($permalink);
}
return $permalink;
}
/************************************************************************
Show the Pugpig theme if required
*************************************************************************/
add_filter( 'template', 'pugpig_theme_switcher' );
add_filter( 'stylesheet', 'pugpig_theme_switcher' );
function pugpig_theme_switcher($theme)
{
// See if we have a setting for theme override
$theme_switch = get_option("pugpig_opt_theme_switch");
if (empty($theme_switch)) return $theme;
if (pugpig_is_pugpig_url()) {
return $theme_switch;
}
return $theme;
}
/************************************************************************
Register a buffer handlers so we can search/replace all the output
TODO: Find better places to register these hooks
shutdown doesn't seem to work
************************************************************************/
add_action('template_redirect','pugpig_ob_start');
function pugpig_ob_start()
{
if (pugpig_is_pugpig_url() && !isset($_REQUEST['norewrite'])) {
ob_start('pugpig_rewrite_content');
}
}
add_action('wp_print_footer_scripts','pugpig_ob_end');
function pugpig_ob_end()
{
if (pugpig_is_pugpig_url()) {
ob_end_flush();
}
}
/************************************************************************
Get the root so we can make relative links
We want everything up to the third slash
************************************************************************/
function pugpig_get_root()
{
$url = get_bloginfo('url');
while (substr_count($url, '/') > 2) { // all we need is the :// from the protocol
$array = explode('/', $url);
array_pop($array);
$url = implode('/', $array);
}
return $url;
}
/************************************************************************
Remove domain from URLs that could be root relative
************************************************************************/
function pugpig_rewrite_content($content)
{
// This is no longer required as the link to the manifest is in the ATOM feed
// $id = 'post-' . get_the_ID() . ".manifest";
// $manifest = pugpig_path_to_rel_url(PUGPIG_MANIFESTPATH . $id);
// $pattern = '/<html/i';
// $content = preg_replace($pattern,'<html manifest="' . $manifest . '"',$content);
// TODO: Need to make this less aggresive - we want absolute URLs for non html cases
//if (strpos($content, '<html') !== FALSE) {
$absoluteprefix = pugpig_get_root();
$content = str_replace($absoluteprefix, '', $content);
// $content = str_replace(" site", " Puggers site", $content);
//}
$content = pugpig_rewrite_wpcontent_links($content);
return $content;
}
/************************************************************************
Returns a block of markup with image URLs fixed
This will also remove a ?ver=123 from the query string
************************************************************************/
function pugpig_rewrite_wpcontent_links($markup)
{
$content_fragment = pugpig_strip_domain(content_url());
$regex = '#([\'"])('.$content_fragment.'/.*?)\1#i';
$index = 0;
if (preg_match_all($regex, $markup, $matches)) {
foreach ($matches[2] as $src) {
$quote = $matches[1][$index];
$new_uri = url_create_deep_dot_url($src);
// Strip version number?
$new_uri = remove_query_arg( 'ver', $new_uri );
$markup= str_replace($quote.$src.$quote, $quote.$new_uri.$quote, $markup);
++$index;
}
}
return _pugpig_rewrite_pugpig_html_links($markup);
}
function _pugpig_rewrite_pugpig_html_links($markup) {
$html = new simple_html_dom();
$html->load($markup, false, false);
$anchors = $html->find('a[href$="/'.PUGPIG_HTML_FILE_NAME.'"]');
foreach ($anchors as $anchor) {
$anchor->href = url_create_deep_dot_url($anchor->href);
}
return $html->save();
}
/************************************************************************
Utility functions for when we want to proxy to images that are served
from another domain. We need to:
- replace the paths of all images in the body to be local URLS
- include these new paths in the manifest
- intercept all calls to these new paths and redirect to the source
************************************************************************/
// Extract all image paths from the block
function pugpig_get_image_urls($markup)
{
$assets = array();
if (preg_match_all('#<img.*?src=([\'"])(.*?)\1#i', $markup, $matches)) {
foreach ($matches[2] as $src) {
array_push($assets, $src);
}
}
return $assets;
}
function pugpig_get_encoded_image_url($url, $prefix, $make_relative=true)
{
$url = apply_filters('pugpig_rewrite_external_image_url', $url);
$out = strrchr(content_url(), '/') . "/$prefix/" . base64_encode($url) . ".jpeg";
$out = str_replace("=", "_", $out);
if ($make_relative) {
$out = url_create_deep_dot_url($out);
}
return $out;
}
class _PugpigImageReplacer
{
public $prefix;
public $base;
public $make_relative;
public function pugpig_replace_image_urls($markup, $prefix, $base='', $make_relative=true)
{
$this->prefix = $prefix;
$this->base = $base;
$this->make_relative = $make_relative;
$out = preg_replace_callback('/(<img\s+.*?src=([\'"]))(.*?)(\2)/mi',
array($this, "pugpig_replace_image_callback"),
$markup
);
return $out;
}
public function pugpig_replace_image_callback($matches)
{
$orig_src = $matches[3];
if (strncmp($orig_src, '/', 1)===0) {
$orig_src = $this->base .$orig_src;
}
$new_src = pugpig_get_encoded_image_url($orig_src, $this->prefix, $this->make_relative);
return $matches[1] . $new_src . $matches[4];
}
}
// Replace all image paths to the generic serve-from-source base64 path
// Creates . relative URLs
function pugpig_replace_image_urls($markup, $prefix, $base='', $make_relative=true)
{
$image_replacer = new _PugpigImageReplacer();
return $image_replacer->pugpig_replace_image_urls($markup, $prefix, $base, $make_relative);
}
function pugpig_add_inline_images_to_manifest($markup, $prefix, $base='')
{
$fixed_markup = pugpig_replace_image_urls($markup, $prefix, $base, false);
$images = pugpig_get_image_urls($fixed_markup);
$ret = "\n# Adding images found in the body\n";
$ret .= implode("\n", $images) . "\n\n";
return $ret;
}
// Check if we are on one of our paths and redirect to the source if yes
function pugpig_intercept_remote_image_urls($prefix)
{
$uri = $_SERVER['REQUEST_URI'];
if (empty($uri)) {
return;
}
$parts = split_url($uri);
$matches = array();
$pattern = "/\\" . strrchr(content_url(), '/') . "\/$prefix\/(.*)\.([^\.]+)$/i";
$results = preg_match($pattern, $uri, $matches);
if ($results > 0) {
$id = $matches[1];
$new_id = str_replace("_", "=", $id);
$image_url = base64_decode($new_id);
//echo("DEBUG. I SHOULD REDIRECT TO\nLocation: ". $image_url); exit();
header('Location: '. $image_url);
exit();
}
}