-
Notifications
You must be signed in to change notification settings - Fork 0
/
bussdatabasen_wp.php
237 lines (221 loc) · 6.6 KB
/
bussdatabasen_wp.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
<?Php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/*
Plugin Name: Bussdatabasen WP
Plugin URI: https://github.com/datagutten/bussdatabasen-wp
Description:
Version: 0.1
Author: Anders Birkenes
Author URI: https://github.com/datagutten
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
apply_filters( 'wp_title', 'foo', 100);
class bussdatabasen {
public $ch;
public $wikiurl; //Full url to the wiki
public $django_url;
public $wikidir; //Wiki subdir to be used a source for relative link rewriting
public $page;
public $version = '0.1';
public $response;
/**
* @var DOMDocument
*/
public $dom;
public $error;
public $frame_page_id;
/**
* bussdatabasen constructor.
*/
function __construct() {
//Initialize the plugin on WordPress initialization
add_action( 'init', array( $this, 'init' ) );
add_action( 'wp_loaded', array ($this, 'preload_page') );
apply_filters( 'pre_get_document_title', array($this, 'title'), 100);
//apply_filters( 'wp_title', array($this, 'title'), 100);
//Initalize options
add_option( 'django_url' , 'https://db.bussdatabasen.no');
add_option( 'django_page_id');
$this->django_url = get_option( 'django_url' );
$this->frame_page_id = get_option('django_page_id');
$this->wikidir = basename( $this->django_url );
//Admin menu
require 'admin_menu.php';
}
//Register shortcode, add template redirect and add CSS
function init() {
add_shortcode( 'bussdatabasen', array($this, 'shortcode') );
//add_action( 'template_redirect', array( $this, 'get_page' ) );
wp_enqueue_style( 'wiki-embed-style', plugins_url( '/wikiembedder/wikiembedder.css' ), false, $this->version, 'screen' );
}
//Called by add_action( 'wp_loaded' ...
function preload_page()
{
if(!isset($_GET['page'])) //Home page
$url=$this->django_url;
else
{
$page=$_GET['page'];
if($page[0]=='/') //Internal link
{
$url=$this->django_url.$page;
}
else //External link
{
$url=$page;
trigger_error('External link loaded: '.$page);
}
}
if(!empty($_POST))
{
if(!empty($_FILES))
trigger_error('File upload not supported, form should be sent directly to django',E_USER_WARNING);
/*{
$fp=fopen($file=$_FILES['bilde']['tmp_name']);
/*$data=file_get_contents($_FILES['bilde']['tmp_name']);
$base64=base64_encode($data);
$_POST['bilde']=$base64;
trigger_error(print_r($_FILES,true));*/
/*$file_data=fread($fp,$_FILES['bilde']['size']);
$_POST['bilde']=$file_data;
}*/
$referer = $_SERVER['HTTP_REFERER'];
$referer_page = preg_replace('/.+page=(.+)/', '$1', $referer);
$referer_page = urldecode($referer_page);
$referer = sprintf('%s%s', $this->django_url, $referer_page);
//$referer = $_GET['page'];
//var_dump($referer);
$response = wp_remote_post( $url, array('cookies'=>$_COOKIE, 'body'=>$_POST, 'redirection'=>0, 'referer'=>$referer));
}
else
$response = wp_remote_get( $url , array('cookies'=>$_COOKIE, 'redirection'=>0));
$this->response=$response;
if( is_wp_error( $response ) )
{
$this->error=$response->get_error_message();
return false;
}
else
{
$cookies = wp_remote_retrieve_header($response, 'set-cookie');
if(!is_array($cookies))
$cookies = array($cookies);
foreach($cookies as $cookie)
{
if(preg_match_all('/(\S+)=(.+);/U',$cookie.';',$fields)) //Parse cookies
{
$name=$fields[1][0];
$fields=array_combine($fields[1],$fields[2]);
if(!isset($fields['expires']))
$expire=0;
else
$expire=strtotime($fields['expires']);
if(!isset($fields['Path']))
$fields['Path']='';
if(!isset($fields['Domain']))
$fields['Domain']='';
setcookie($name,$fields[$name],$expire,$fields['Path'],$fields['Domain']);
}
}
$response_code = wp_remote_retrieve_response_code($response);
if($response_code >=300 && $response_code<=399)
{
$location = wp_remote_retrieve_header($response, 'location');
trigger_error('Location: '.$location);
if(strpos($location,'?next=')!==false)
{
//Fjerne next og la login gå til default?
//Sjekke at det er login
//Fjerne next hvis login
parse_str(parse_url($location,PHP_URL_QUERY),$parameters);
$next_link='https://www.bussdatabasen.no/bevarte-busser/?page='.$parameters['next'];
$location=str_replace('next='.$parameters['next'],'next='.$next_link,$location);
/*trigger_error(print_r($parameters,true));
trigger_error($location);
trigger_error($next_link);*/
}
$url='https://www.bussdatabasen.no/bevarte-busser/?page=';
if($location[0]=='/')
header('Location: '.$url.urlencode($location));
else
header('Location: '.$location);
//wp_die();
die();
}
else
{
if(!class_exists('DOMDocument'))
return 'DOMDocument is not available';
$this->dom=new DOMDocument;
$this->dom->loadHTML($response['body']);
}
}
}
function get_title()
{
$title=$this->dom->getElementsByTagName('title');
return $title->item(0)->textContent;
}
//Rewrite links and image tags
function rewrite ()
{
if(empty($this->dom))
return $this->error;
$post = get_post();
if($post->ID!=$this->frame_page_id)
{
//print_r(get_post($this->frame_page_id));
$url_prefix = get_permalink($this->frame_page_id);
}
else
$url_prefix ='';
$dom=$this->dom;
$body=$dom->getElementById('bussdatabasen');
$xpath=new DOMXPath($dom);
$images=$dom->getElementsByTagName('img');
foreach($images as $image) //Rewrite relative image paths to absolute
{
$src=$image->getAttribute('src');
if($src[0]=='/')
{
$src=$this->django_url.$src;
$image->setAttribute('src',$src);
}
}
//$attrs['src']=$xpath->query('//*[@src]');
$attrs['href']=$xpath->query('//*[@href]');
$attrs['action']=$xpath->query('//form[@action]');
foreach($attrs as $attr_name=>$attr)
{
foreach($attr as $element)
{
$uri=$element->getAttribute($attr_name);
if($uri[0]!='/')
continue;
if(strpos($uri,'/media')===false && strpos($uri,'/bussbilder')===false)
$element->setAttribute($attr_name,$url_prefix.'?page='.urlencode($uri));
else
$element->setAttribute($attr_name,$this->django_url.$uri);
}
}
return $dom->saveXML($body, LIBXML_NOEMPTYTAG);
}
//Shortcode handler
function shortcode( $atts ) {
$a = shortcode_atts( array(
'page'=>'',
'namespace'=>false)
, $atts );
if(!empty($_GET['page']))
$a['page']=$_GET['page'];
elseif(!empty($a['page']))
{
$_GET['page']=$a['page'];
$this->preload_page();
}
$html=$this->rewrite();
return $html;
}
}
$bussdatabasen=new bussdatabasen();