forked from crosswalk-project/crosswalk-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfm.php
executable file
·152 lines (128 loc) · 3.87 KB
/
gfm.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
<?php
require_once ('smart-match.inc');
require_once ('wiki-pages.php');
require_once ('wiki-history.php');
require_once ('http.php');
require_once ('cache.php');
// create an HTTP client with the proxy configuration file 'proxy.config';
// if this file is not available, no proxy is used
$base_client = new HttpClient ('proxy.config');
// cache implementation
$cache_time_secs = 5 * 60; // 5 minutes
$cache_dir = 'wiki';
$cache = new Cache ($cache_time_secs, $cache_dir);
// caching http client, used for wiki page fetches
$client = new CachingHttpClient ($base_client, $cache);
$file = 'Home';
if (isset($_REQUEST) && array_key_exists('f', $_REQUEST)) {
$file = $_REQUEST['f'];
}
else if (PHP_SAPI === 'cli') {
$file = $argv[1];
}
function missing ($f) {
header('HTTP/1.0 404 Not found');
print '<p>The page you requested is not available: <span class="missing">'
. $f .
'</span></p>' .
'<p>Please report this to the site administrators, stating ' .
'the URL of the missing page.</p>';
exit;
}
function ob_callback ($buffer) {
global $d;
fwrite ($d, $buffer);
}
$md = file_smart_match (dirname (__FILE__).'/'.$file);
$md = realpath ($md);
if (preg_match ('/(\.html)|(\.php)$/', $md)) {
require ($md);
exit;
}
/*
* Special case for Pages request which is dynamically built
* from the list of pages in the main Wiki directory
*/
if (strtolower ($file) == 'wiki/pages' ||
strtolower ($file) == 'wiki/pages.md') {
$success = wiki_pages ();
if ($success) {
require ('wiki/pages.md.html');
}
else {
print 'could not create wiki/pages.md.html page';
}
exit;
}
/*
* Special case for History request which is dynamically built
* from the list of pages in the main Wiki directory
*/
if (strtolower ($file) == 'wiki/history' ||
strtolower ($file) == 'wiki/history.md') {
// generate the wiki history HTML page
$success = wiki_history ();
if ($success) {
require ('wiki/history.md.html');
}
else {
print 'could not create wiki/history.md.html page';
}
exit;
}
/* If this is a simple wiki/ request (not in a sub-directory), redirect to GitHub */
if (preg_match ('#^wiki/#', $file)) {
try {
$url = 'https://github.com/crosswalk-project/crosswalk-website/'.$file;
$result = $client->get_url ($url);
print $result['body'];
}
catch (Exception $e) {
print 'Error: ' . $e->getMessage();
}
exit;
}
if (!preg_match ('#^'.dirname (__FILE__).'/#', $md)) {
missing ($file);
}
$cache = @stat ($md.'.html');
$source = @stat ($md);
if (!$cache || $source['mtime'] > $cache['mtime']) {
$file = preg_replace ('#^'.dirname (__FILE__).'/#', '', $md);
$d = @fopen ($md.'.html', 'w');
if (!$d) {
print " !!!! Unable to create file $md.html. Check that the server " .
"has access to the directory.\n";
exit;
}
if (preg_match ('/\.php$/', $file)) {
/* ob_callback uses $d to write the buffer to */
ob_start ("ob_callback");
print '<div id="wiki-content">';
print '<div class="markdown-body">';
require ($file);
print '</div>';
print '</div>';
ob_end_flush ();
} else {
$file = preg_replace ('/((\.md)|(\.mediawiki)|(\.org)|(\.php))$/',
'', $file);
// use the non-caching HTTP client to fetch content from the
// gollum server for every request
try {
$result = $base_client->get_url ('http://localhost:4567/'.$file);
print $result['body'];
fwrite ($d, $result['body']);
fflush ($d);
}
catch (Exception $e) {
print $e->getMessage();
}
fclose ($d);
}
}
if (filesize ($md.'.html') == 0) {
unlink ($md.'.html');
missing ($md.'.html');
}
require ($md.'.html');