-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
200 lines (161 loc) · 5.5 KB
/
index.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
<?php
require 'vendor/autoload.php';
use Mf2\Parser;
$composer = json_decode(file_get_contents(dirname(__FILE__).'/composer.lock'));
$version = 'unknown';
foreach($composer->packages as $pkg) {
if($pkg->name == 'mf2/mf2') {
if($pkg->version == 'dev-master') {
$version = substr($pkg->source->reference,0,7);
} else {
$version = $pkg->version;
}
}
}
$debugMsg = array(
'package' => 'https://packagist.org/packages/mf2/mf2',
'source' => 'https://github.com/indieweb/php-mf2',
'version' => $version,
'note' => array(
'This output was generated from the php-mf2 library available at https://github.com/indieweb/php-mf2',
'Please file any issues with the parser at https://github.com/indieweb/php-mf2/issues'
)
);
if(class_exists('Masterminds\\HTML5')) {
$debugMsg['note'][] = 'Using the Masterminds HTML5 parser';
}
$PATH = preg_replace('~index.php$~', '', $_SERVER['SCRIPT_NAME']);
$STORAGE_MODE = getenv('REDIS_URL') ? 'redis' : 'file';
$EXPIRE_HOURS = 72;
if($STORAGE_MODE == 'redis') {
$redis = new Predis\Client(getenv('REDIS_URL'));
}
if(get('url')) {
$url = get('url');
if(!preg_match('/^http/', $url))
$url = 'http://' . $url;
$client = new p3k\HTTP('php.microformats.io (php-mf2/' . $version . ') Mozilla/5.0 Chrome/29.0.1547.57 Safari/537.36');
$headers = ['Accept: text/html, */*'];
$page = $client->get($url);
if(get('debug')) {
echo '<pre>';
print_r($page);
die();
}
if ($page['error'] !== '') {
$debugMsg = ['error' => [ 'type' => $page['error'], 'description' => $page['error_description'] ]] + $debugMsg;
}
$parser = new Parser($page['body'], $page['url'], true);
$parser->lang = true;
$output = $parser->parse();
$output['debug'] = $debugMsg;
if(array_key_exists('callback', $_GET)) {
header('Content-type: text/javascript');
echo $_GET['callback'].'('.json_encode($output).');';
} else {
header('Content-Type: application/json');
echo json_encode($output, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
} elseif(post('html')) {
$url = post('url');
if($url && !preg_match('/^http/', $url))
$url = 'http://' . $url;
$parser = new Parser(post('html'), $url, true);
$parser->lang = true;
$output = $parser->parse();
if(post('save') == 1) {
$scheme = array_key_exists('REQUEST_SCHEME', $_SERVER) ? $_SERVER['REQUEST_SCHEME'] : 'http';
list($usec, $sec) = explode(" ", microtime());
$id = date('YmdHis').sprintf('%03d',round($usec*1000));
preg_match('/^(\d{6})(\d{11})$/',$id,$match);
if($STORAGE_MODE == 'file') {
@mkdir(dirname(__FILE__).'/data/'.$match[1], 0755, true);
file_put_contents(dirname(__FILE__).'/data/'.$match[1].'/'.$match[2].'.url', json_encode(array(
'url' => $url,
'show_html' => post('show_html'),
)));
file_put_contents(dirname(__FILE__).'/data/'.$match[1].'/'.$match[2].'.html', post('html'));
} elseif($STORAGE_MODE == 'redis') {
$data = json_encode([
'html' => post('html'),
'settings' => [
'url' => $url,
'show_html' => post('show_html')
]
]);
$redis->setex('phpmf2-'.$id, $EXPIRE_HOURS*60*60, $data);
}
header('Location: '.$scheme.'://'.$_SERVER['SERVER_NAME'].$PATH.'?id='.$id);
} else {
$output['debug'] = $debugMsg;
$json = json_encode($output, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
$html = post('html');
$url = post('url');
$show_html = post('show_html');
$save_html = false;
if(post('format') == 'json') {
header('Content-Type: application/json');
echo $json;
} else {
require('result.php');
}
}
} elseif(post('json')) {
require('json-validator.php');
// Validate MF2 JSON
$data = json_decode(post('json'));
if(!$data) {
$json = post('json');
$valid = false;
$error = 'The input was not valid JSON';
} else {
$json = json_encode($data, JSON_PRETTY_PRINT+JSON_UNESCAPED_SLASHES);
if(post('input-type') == 'list')
list($valid, $error) = is_valid_mf2_list($data);
else
list($valid, $error) = is_valid_mf2_object($data);
}
require('json-result.php');
} elseif(get('id')) {
if(preg_match('/^(\d{6})(\d{11})$/',get('id'),$match)) {
if($STORAGE_MODE == 'file') {
$htmlfile = dirname(__FILE__).'/data/'.$match[1].'/'.$match[2].'.html';
$urlfile = dirname(__FILE__).'/data/'.$match[1].'/'.$match[2].'.url';
$exists = file_exists($htmlfile);
} elseif($STORAGE_MODE == 'redis') {
$data = $redis->get('phpmf2-'.$match[0]);
$exists = $data ?: false;
}
if(!$exists) {
header('HTTP/1.1 404 Not Found');
header('Content-Type: text/plain');
echo 'Not Found';
die();
}
if($STORAGE_MODE == 'file') {
$html = file_get_contents($htmlfile);
$settings = json_decode(file_get_contents($urlfile));
} elseif($STORAGE_MODE == 'redis') {
$data = json_decode($data);
$html = $data->html;
$settings = $data->settings;
}
$url = $settings->url;
$show_html = $settings->show_html;
$save_html = true;
$parser = new Parser($html, $url, true);
$parser->lang = true;
$output = $parser->parse();
$output['debug'] = $debugMsg;
$json = json_encode($output, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
require('result.php');
}
} else {
require('form.php');
}
function get($k, $default=null) {
return array_key_exists($k, $_GET) ? $_GET[$k] : $default;
}
function post($k, $default=null) {
return array_key_exists($k, $_POST) ? $_POST[$k] : $default;
}