-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrap-dhamma-org.php
165 lines (138 loc) · 5.58 KB
/
wrap-dhamma-org.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
<?php
/*
Plugin Name: wrap-dhamma-org
Description: retrieves, re-formats, and emits HTML for selected pages from www.dhamma.org
Version: 3.01
Authors: Joshua Hartwell <JHartwell@gmail.com> & Jeremy Dunn <jeremy.j.dunn@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
function wrap_dhamma( $page, $lang = null ) {
$lang = isset($lang) ? $lang : substr(get_bloginfo('language'), 0, 2);
// validate page
switch ( $page ) {
case 'vipassana':
case 'code':
case 'goenka':
case 'art':
case 'qanda':
case 'dscode':
case 'osguide':
case 'privacy':
$url = 'https://www.dhamma.org/' . $lang . '/' . $page . "?raw";
$text_to_output = pull_page( $url, $lang );
break;
case 'video':
$url = 'https://video.server.dhamma.org/video/';
$text_to_output = pull_video_page( $url );
break;
default:
die ( "invalid page '".$page."'" );
}
// emit the required comment
echo '<!-- ' . $url . ' has been dynamically reformatted on ' . date("D M j G:i s Y T") . '. -->';
// emit the reformatted page
echo $text_to_output;
echo '<!-- end dynamically generated content.-->';
// we're done
}
function pull_page ( $url, $lang ) {
$raw = file_get_contents ( $url );
if ($raw === false) {
echo "Error retrieving content.";
}
$raw = stripH1 ( $raw );
return $raw;
}
function pull_video_page ( $url ) {
$raw = file_get_contents ( $url );
$raw = getBodyContent ( $raw );
$raw = stripH1 ( $raw );
$raw = stripHR ( $raw );
$raw = stripTableTags ( $raw );
$raw = stripExessVideoLineBreaks ( $raw );
$raw = fixVideoURLS ( $raw );
$raw = fixBlueBallImages ( $raw );
$raw = stripHomeLink ( $raw );
return $raw;
}
function fixURLs ( $raw, $lang ) {
$LOCAL_URLS = array(
'art' => '/about/art-of-living/',
'goenka' => '/about/goenka/',
'vipassana' => '/about/vipassana/',
'/' => '',
);
foreach ( $LOCAL_URLS as $from => $to ) {
$raw = str_replace('<a href="' . $from . '">', '<a href="' . get_option('home') . $to . '">', $raw);
$raw = str_replace("<a href='" . $from . "'>", '<a href="' . get_option('home') . $to . '">', $raw);
}
$raw = preg_replace("#<a href=[\"']/?code/?[\"']>#", '<a href="' . get_option('home') . '/courses/code/">', $raw);
$raw = str_replace("<a href='/bycountry/'>", "<a target=\"_blank\" href=\"http://courses.dhamma.org/en-US/schedules/schdhara\">", $raw);
$raw = str_replace("<a href='/docs/core/code-en.pdf'>here</a>", "<a href='http://www.dhamma.org/en/docs/core/code-en.pdf'>here</a>", $raw);
$raw = str_replace('"/en/docs/forms/Dhamma.org_Privacy_Policy.pdf"',
'"https://www.dhamma.org/en/docs/forms/Dhamma.org_Privacy_Policy.pdf"', $raw);
return $raw;
}
function fixVideoURLS ( $raw ) {
$raw = preg_replace( "#<a href='./intro/#si", "<a href='http://video.server.dhamma.org/video/intro/", $raw );
return $raw;
}
function stripH1( $raw ) {
return preg_replace('@<h1[^>]*?>.*?<\/h1>@si', '', $raw); //This isn't a great solution, not very dynamic, but it gets the job done.
}
function stripHR ( $raw ) {
return preg_replace("@<hr.*?>@si", '', $raw);
}
function changeTag ( $source, $oldTag, $newTag ) {
$source = preg_replace( "@<{$oldTag}>@si", "<{$newTag}>", $source );
$source = preg_replace( "@</{$oldTag}>@si", "</{$newTag}>", $source );
return $source;
}
function fixGoenkaImages ( $raw ) {
//Make the Goenkaji images work - JDH 10/12/2014
$raw = preg_replace( '#/images/sng/#si', 'https://www.dhamma.org/images/sng/', $raw );
//Make the goenka images inline - JDH 10/12/2014
$raw = str_replace('class="www-float-right-bottom"', "align='right'", $raw);
$raw = str_replace('<img alt="S. N. Goenka at U.N."', '<img alt="S. N. Goenka at U.N." style="display: block; margin-left: auto; margin-right: auto;"', $raw);
$raw = str_replace('Photo courtesy Beliefnet, Inc.', '<p style="text-align:center">Photo courtesy Beliefnet, Inc.</p>', $raw);
$dir = plugin_dir_path( __FILE__ );
$raw = str_replace ( 'src="https://www.dhamma.org/assets/sng/sng-f01f4d6595afa4ab14edced074a7e45c.gif"', 'id="goenka-image" src="/wp-content/plugins/wrap-dhamma-org/goenka.png"', $raw );
return $raw;
}
function stripTableTags ( $raw ) {
$raw = preg_replace("@</*?table.*?>@si", '', $raw);
$raw = preg_replace("@</*?tr.*?>@si", '', $raw);
$raw = preg_replace("@</*?td.*?>@si", '', $raw);
return $raw;
}
function stripExessVideoLineBreaks ( $raw ) {
$raw = preg_replace( "@\n@si", '', $raw );
$raw = preg_replace( "@[ ]+@", ' ', $raw );
return $raw;
}
function getBodyContent ( $raw ) {
// take HTML between <body> and </body>
$bodypos = strpos($raw, '<body>');
$nohead = substr($raw, $bodypos + 6); // strip <body> tag as well
$bodyendpos = strpos($nohead, '</body>');
$raw = substr($nohead, 1, ($bodyendpos -1));
return $raw;
}
function fixBlueBallImages ( $raw ) {
$raw = preg_replace ( '#<IMG SRC="/images/icons/blueball.gif">#si', '', $raw );
return $raw;
}
function stripHomeLink ( $raw ) {
$raw = preg_replace ( "#Download a free copy of <a href='http://www.real.com'>RealPlayer</a>.#si", "", $raw );
$raw = preg_replace ( "#<br/> <a href='http://www.dhamma.org/'><img style='border:0' src='/images/icons/home.gif' alt=' '></A>#si", "", $raw );
return $raw;
}
?>