Skip to content

Instantly share code, notes, and snippets.

@kaz
Last active January 31, 2016 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaz/47d61a13f7b423ced889 to your computer and use it in GitHub Desktop.
Save kaz/47d61a13f7b423ced889 to your computer and use it in GitHub Desktop.
niconicoをhttpsページで頑張ってembedする
<?php
$url = $_SERVER["QUERY_STRING"];
if(preg_match("@^http://(.+?)\.(.+?)/@", $url, $m)){
if($m[1] === "www" && $m[2] === "nicovideo.jp"){
header("HTTP/1.1 301 Moved Permanently");
header("Location: {$url}");
}else if($m[2] === "nicovideo.jp" || $m[2] === "nimg.jp" || $m[2] === "smilevideo.jp"){
$hash = md5($url);
if(file_exists("{$hash}.content") && file_exists("{$hash}.type")){
$content = file_get_contents("{$hash}.content");
$type = file_get_contents("{$hash}.type");
}else{
curl_setopt($ch = curl_init($url), CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
$content = str_replace("http://", "?http://", $content);
$content = str_replace("url('", "url('?http://{$m[1]}.{$m[2]}", $content);
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if(strpos($type, "html") === false){
file_put_contents("{$hash}.content", $content);
file_put_contents("{$hash}.type", $type);
}
}
header("Content-Type: {$type}");
echo($content);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment