forked from bjeavons/inBed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
213 lines (201 loc) · 7.74 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
error_reporting(0);
/**
* WithBaconfy
*
* Copyright (C) 2011 Ezra Barnett Gildesgame & Benjamin Jeavons
*/
function getURL() {
if (isset($_GET['q'])) {
$domain = $_GET['q'];
if (strpos($domain, 'http') === false) {
$domain = 'http://' . $domain;
}
list($scheme, $url) = explode(':/', $domain);
// A forward slash can be stripped by the browser so plan for it.
return $scheme . "://" . ltrim($url, '/');
}
else {
return false;
}
}
function validURL($url) {
return (bool) preg_match("
/^ # Start at the beginning of the text
(?:https?):\/\/ # Look for http, https or schemes
(?: # Userinfo (optional) which is typically
(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password
(?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination
)?
(?:
(?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address
|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address
)
(?::[0-9]+)? # Server port number (optional)
(?:[\/|\?]
(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional)
*)?
$/xi", $url);
}
function inBedify($url) {
require 'QueryPath/QueryPath.php';
$page = htmlqp($url);
if (!$page) {
return;
}
$url_parts = parse_url($url);
// Check response code @todo
$base = $url_parts['scheme'] . '://' . $url_parts['host'];
// Convert relative URLs to absolute.
foreach (qp($page, 'link') as $link) {
if ($link->hasAttr('href') && strpos($link->attr('href'), 'http') === false) {
$link->attr('href', $base . '/' . ltrim($link->attr('href'), '/'));
}
}
foreach (qp($page, 'script') as $script) {
if ($script->hasAttr('src') && strpos($script->attr('src'), 'http') === false) {
$script->attr('src', $base . '/' . ltrim($script->attr('src'), '/'));
}
}
foreach (qp($page, 'style') as $style) {
if (preg_match('/@import ?["\']\/(.*)/', $style->text(), $matches) && count($matches) > 1) {
$style->text('@import "' . $base . '/' . $matches[1]);
}
}
foreach (qp($page, 'img') as $img) {
if (strpos($img->attr('src'), 'http') === false) {
$img->attr('src', $base . '/' . ltrim($img->attr('src'), '/'));
}
}
// Rewrite same-domain URLs to run through InBedify.
foreach (qp($page, 'a') as $a) {
if (strpos($a->attr('href'), 'http') !== false) {
// Only rewrite same-domain URLs.
$host = parse_url($a->attr('href'), PHP_URL_HOST);
if ($host == $url_parts['host']) { //@todo
$a->attr('href', 'http://withbaconfy.com/' . $a->attr('href'));
}
}
else {
// Relative URL.
$a->attr('href', 'http://withbaconfy.com/' . $base . '/' . ltrim($a->attr('href'), '/'));
}
}
// InBedify!
// Speed this up @todo
foreach (qp($page, 'h1') as $header) {
inBedElement($header);
}
foreach (qp($page, 'h2') as $header) {
inBedElement($header);
}
foreach (qp($page, 'h3') as $header) {
inBedElement($header);
}
print $page->html();
exit;
}
function inBedElement($qp_element) {
// Check if text is in a child element.
if ($qp_element->is('a')) {
$qp_element->find('a');
}
// Handle punctuation.
$text = $qp_element->text();
if (preg_match('/(.*)([\?\.\!\*\)"])+/', $qp_element->text(), $matches)) {
$qp_element->text($matches[1] . ' with Bacon' . $matches[2]);
}
else {
$qp_element->append(' with Bacon');
}
}
function frontPage() {
print <<<eof
<!doctype html>
<html lang="en" dir="ltr"><!-- with bacon -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WithBaconfy.com : Add "with bacon" to any website</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Meta keywords with bacon" />
<meta name="description" content="Meta descriptions with bacon" />
<style type="text/css">
body{background-color:#fff;color:#333;font-family:Arial,Verdana,sans-serif;font-size:62.5%;margin:10% 5% 0 5%;text-align:center;}
.inbedit{font-weight:bold;}
a,a:visited,a:active{color:#0080ff;text-decoration:underline;font-weight:bold;}
a:hover{text-decoration:none;font-weight:bold;}
h1{font-size:5em;text-shadow: #999 5px 5px 8px;}
input[type=text]{border:1px solid #ccc;font-size:1em;padding:4px 6px 4px 6px;}
.ify{font-style:italic;font-size:1em;}
#content{clear:both;font-size:3em;margin:auto;}
#domain_input{width:380px;}
.cta{font-size:0.6em}
.footer{clear:both;padding-top:1em;}
.credits{font-size:1.8em;}
.fineprint{font-size:1.2em;}
.share{width:300px;margin-left:auto;margin-right:auto;margin-top:3em;}
.share-button{float:left;}
.fb iframe{text-align:center;}
</style>
<script type="text/javascript">
function clearDomainInput(e) {
if (e.cleared) { return; }
e.cleared = true;
e.value = '';
}
function formSubmit() {
domain = document.getElementById('domain_input').value;
window.location = '/' + domain;
return false;
}
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-432773-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
}
</script>
</head><!-- with bacon -->
<body>
<h1>With Bacon<span class="ify">fy</span></h1>
<div id="content">
<form method="GET" name="inbedify" action="/">
<span class="inbedit">You should read</span>
<input type="text" name="q" class="domain" id="domain_input" value="inbedify.com" onclick="clearDomainInput(this);">
<span class="inbedit"><a href="#" onclick="formSubmit();">with bacon</a>.</span>
<input type="submit" style="display: none;">
</form>
<p class="cta">Type in any address to WithBaconfy!</p>
</div>
<div class="share">
<div class="share-button tw"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://withbaconfy.com" data-text=""in bed" is not just for fortune cookies anymore: " data-count="horizontal">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>
<div class="share-button fb"><div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http://withbaconfy.com" send="true" layout="button_count" width="100" show_faces="false" font=""></fb:like></div>
</div>
<div class="footer">
<p class="credits">Made by <a href="https://twitter.com/benswords" onClick="recordOutboundLink(this, 'Outbound Links', 'twitter.com');return false;">@benswords</a> and <a href="https://twitter.com/ezrabg" onClick="recordOutboundLink(this, 'Outbound Links', 'twitter.com');return false;">@ezrabg</a>. Made tastier by <a href="https://twitter.com/rupl" onClick="recordOutboundLink(this, 'Outbound Links', 'twitter.com');return false;">@rupl</a> with bacon.</p>
<p class="fineprint">This is a novelty service, no ownership over served content is implied … in bed or with bacon.</p>
</div>
</body><!-- with bacon -->
</html>
eof;
exit;
}
$url = getURL();
if ($url) {
// Validate URL.
if (validURL($url)) {
inBedify($url);
}
}
frontPage();
?>