-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·200 lines (187 loc) · 8.28 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
<?php
//assign value for title of page
$pageTitle = 'EmbedThis (Oembed)';
$subTitle = 'Pass a URL & Get Embed Code';
//declare filename for additional stylesheet variable - default is "none"
$customCSS = 'master.css';
//create an array with filepaths for multiple page scripts - default is meta/scripts/global.js
$customScript[0] = 'none';
//get and set url protocol
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
//set and sanitize global variables for URL construction
$server = htmlentities(strip_tags($_SERVER['SERVER_NAME']));
$path = htmlentities(strip_tags(dirname($_SERVER['PHP_SELF'])));
$fileName = htmlentities(strip_tags(basename($_SERVER['SCRIPT_NAME'])));
$fileNameURI = htmlentities(strip_tags($_SERVER['REQUEST_URI']));
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title><?php echo($pageTitle); ?> - jason a. clark</title>
<meta name="description" content="Utility app built around an HTML form & PHP functions that check for an Oembed endpoint & return HTML embed code for various popular web sites."/>
<!-- Social Media Tags -->
<meta property="og:title" content="EmbedThis (Oembed)"/>
<meta property="og:description" content="Utility app that checks for an Oembed endpoint & returns HTML embed code."/>
<meta property="og:image" content="<?php echo $protocol.$server.$path; ?>/meta/img/share-code-small.png"/>
<meta property="og:url" content="<?php echo $protocol.$server.$path.'/'.$fileName; ?>"/>
<meta property="og:type" content="website"/>
<meta name="twitter:creator" property="og:site_name" content="@jaclark"/>
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:site" content="http://www.jasonclark.info"/>
<!-- End Social Media Tags -->
<link rel="alternate" type="application/rss+xml" title="diginit - jason clark" href="http://feeds.feedburner.com/diginit" />
<?php if ($customCSS != 'none') {
?>
<link rel="stylesheet" href="<?php echo dirname($_SERVER['PHP_SELF']); ?>/meta/styles/<?php echo $customCSS; ?>">
<?php
}
if ($customScript[0] != 'none') {
$counted = count($customScript);
for ($i = 0; $i < $counted; $i++) {
echo '<script type="text/javascript" src="'.$customScript[$i].'"></script>'."\n";
}
}
?>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="<?php if(!isset($_GET['view'])) { echo 'home'; } else { echo $_GET['view']; } ?>" role="document" vocab="http://schema.org/" typeof="WebPage">
<header role="banner" property="name description">
<h1><?php echo($pageTitle); ?></h1>
<h2><?php echo($subTitle); ?></h2>
</header>
<div class="main">
<main role="main">
<?php
//turn on full error reports for development - REMOVE when in production
//error_reporting(E_ALL);
if (isset($_POST['passField']) && trim($_POST['passField']) != ''):
?>
<h2>Thanks for posting a general inquiry, ROBOT. Please, go die in a fire!</h2>
<?php
elseif (isset($_REQUEST['submitCheck']) && $_REQUEST['submitCheck'] == 1 || isset($_GET['uri'])):
//set default value for URL
$uri = isset($_REQUEST['uri']) ? strip_tags($_REQUEST['uri']) : null;
?>
<h3>Hmmm... Thanks for trying out the URL at <a href="<?php echo $uri; ?>"><?php echo $uri; ?></a></h3>
<p>Here's what I found:</p>
<?php
function getEmbedCode($url = "", $maxwidth = 320) {
$embedResult = '';
//instagram only allowing http for oembed, stripping out https
if (stristr($url, 'instagram.com')) {
$url = 'http://'.substr($url, 8);
//echo $url;
}
//sources available from https://oembed.com/providers.json
$sources = array(
'codepen.io' => 'http://codepen.io/api/oembed',
'dribbble.com' => 'https://api.embed.ly/v1/api/oembed',
'flickr.com' => 'https://www.flickr.com/services/oembed/',
//Facebook also allows video oembed - https://developers.facebook.com/docs/plugins/oembed-endpoints
//note: Facebook posts or videos must be public
'facebook.com' => 'https://graph.facebook.com/v8.0/oembed_post',
'giphy.com' => 'https://giphy.com/services/oembed',
//note: getty needs short url pattern + asset id -> http://gty.im/[asset-id]
'gty.im' => 'http://embed.gettyimages.com/oembed',
//'github.com' => 'https://github.com/api/oembed',
//'hulu.com' => 'http://www.hulu.com/api/oembed.json',
'ifttt.com' => 'https://ifttt.com/oembed',
'instagram.com' => 'https://graph.facebook.com/v8.0/instagram_oembed',
'kickstarter.com' => 'https://www.kickstarter.com/services/oembed',
'meetup.com' => 'https://api.meetup.com/oembed',
'nytimes.com' => 'https://www.nytimes.com/svc/oembed/json/',
'reddit.com' => 'https://www.reddit.com/oembed',
'slideshare.net' => 'http://www.slideshare.net/api/oembed/2',
'soundcloud.com' => 'https://soundcloud.com/oembed',
'speakerdeck.com' => 'https://speakerdeck.com/oembed.json',
//'spotify.com' => 'https://embed.spotify.com/oembed/',
'ted.com' => 'https://www.ted.com/talks/oembed.json',
'tiktok.com' => 'https://www.tiktok.com/oembed',
'twitter.com' => 'https://publish.twitter.com/oembed',
'ustream.tv' => 'http://www.ustream.tv/oembed',
'vine.co' => 'https://vine.co/oembed.json',
'vimeo.com' => 'https://vimeo.com/api/oembed.json',
'wordpress.com' => 'http://public-api.wordpress.com/oembed/',
'youtube.com' => 'https://www.youtube.com/oembed'
);
$service = false;
foreach($sources as $domain => $source) {
if (stristr($url, $domain)) {
$service = $source;
}
}
if (!$service) {
$message = 'Could not find an Oembed service endpoint. Note that Facebook requires posts or videos to be public.';
return $message;
}
$options = array(
CURLOPT_URL => $service.'?url='.urlencode($url).'&format=json&maxwidth='.$maxwidth,
CURLOPT_USERAGENT => 'jasonclark.info',
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result, true);
if (isset($response['type'])) {
switch ($response['type']) {
case 'photo':
$img = '<img src="'.$response['url'].'" alt="'.$response['title'].'" />';
$embedResult = $response['title'].'<br />'.$img.'<p>embed source code:<br /><pre contentEditable="true">'.htmlspecialchars($img).'</pre></p>';
break;
default:
$embedResult = $response['title'].'<br />'.$response['html'].'<p>embed source code:<br /><pre contentEditable="true">'.htmlspecialchars($response['html']).'</pre></p>';
break;
}
}
if (empty($response['type'])) {
$message = 'Oembed result not available for this service. Note that Facebook requires posts or videos to be public.';
return $message;
} else {
return $embedResult;
}
}
echo getEmbedCode($uri);
?>
<p class="control"><a href="<?php echo htmlentities(strip_tags(basename(__FILE__))); ?>" class="refresh">Reset</a></p>
<?php
else:
?>
<form method="post" action="<?php echo htmlentities(strip_tags(basename(__FILE__))); ?>">
<input type="hidden" name="submitCheck" value="1" />
<span id="access">
<label for="passField">omit field (bot test):</label>
<input id="passField" name="passField" type="text" autofill="off" />
<script>(function () { var e = document.getElementById("access"); e.parentNode.removeChild(e); })();</script>
</span>
<fieldset>
<label for="uri">Enter URL:</label>
<input title="Enter URL" type="text" name="uri" id="uri" placeholder="URL from youtube, tiktok, flickr, reddit..." autofocus />
<button type="submit" class="button">Get Code</button>
</fieldset>
</form>
<?php
endif;
?>
</main>
</div><!--end .main div-->
<aside role="complementary">
<nav role="navigation">
<h3>Key:</h3>
<a href="./index.php">Home</a>
<a href="./what.php">What?</a>
<a href="./code.php">Code</a>
<a title="Bibliography by Suzanne Chapman (CC BY-NC-SA 2.0)" href="https://www.flickr.com/photos/sukisuki/4413551329/">Credit</a>
<a href="http://twitter.com/jaclark" class="twitter">@jaclark</a>
</nav>
</aside>
</body>
</html>