-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.php
93 lines (69 loc) · 3.08 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
<?php
// url directory
$dir = dirname($_SERVER['PHP_SELF']);
$dir = $dir == "/" ? "" : $dir;
// url and url's extension
$url = isset($_GET['url']) ? preg_replace(array('/^(?!(ftp|http|https):\/)/', '/:\//'), array('http:/', '://'), trim($_GET['url'])) : 'http://';
$url_isset = $url != 'http://';
$url_extension = pathinfo($url, PATHINFO_EXTENSION);
if (preg_match('/^http:\/\/viewsource\.in/', $url)) {
header('Location: https://github.com/jonathantneal/viewsource');
}
// brush url (for Syntax Highlighter)
$brush_url = '//alexgorbatchev.com/pub/sh/current/';
// supported brushes, default is Xml
$brush_options = array(
'css' => 'Css',
'scss' => 'Css',
'js' => 'JScript',
'jscript' => 'JScript',
'javascript' => 'JScript'
);
$brush = isset($brush_options[$url_extension]) ? $brush_options[$url_extension] : 'Xml';
$brush_lowercase = strtolower($brush);
// title
$title = 'viewsource'.($url_isset ? ': '.$url : '');
$label = 'View Source';
?>
<!doctype html public "Yo dawg, we heard you like viewing the source, so we put our source in the source so you can view source while you view source.">
<title><?php print($title); ?></title>
<meta name="description" content="Share the source of anything on the www.">
<meta property="og:image" content="http://viewsource.in/favicon.png">
<link rel="stylesheet" href="<?php print($dir.'/style.css'); ?>">
<script>document.write("<meta name=viewport content=width=device-"+(this.orientation ? "height" : "width")+">");</script>
<?php if (!$url_isset): ?>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Press+Start+2P">
<form onsubmit="location='./'+this.url.value;return false;">
<h1><?php print($title); ?></h1>
<a href="javascript:location='http://viewsource.in/'+location;" id="bookmarklet"><span>View Source</span></a>
<input type="url" name="url" id="url" placeholder="http://" onchange="this.value=/^(f|ht)tps*:/.test(this.value)?this.value:'http://'+this.value" autofocus>
<input type="submit" id="submit" value="<?php print($label); ?>">
</form>
<?php else: ?>
<script src="<?php print($brush_url.'/scripts/shCore.js'); ?>"></script>
<script src="<?php print($brush_url.'/scripts/shBrush'.$brush.'.js'); ?>"></script>
<pre id="source" class="<?php print('brush:'.$brush_lowercase); ?>"><?php
// create a new cURL resource handle
$ch = curl_init();
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $url);
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Enable decoding of the response
curl_setopt($ch, CURLOPT_ENCODING, '');
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
// Prepare source
$source = preg_replace(array('/&/', '/</'), array('&', '<'), curl_exec($ch));
// Print source
print($source);
?></pre>
<script>url="<? print($url); ?>"</script>
<script src="<?php print($dir.'/script.js'); ?>"></script>
<? endif; ?>