<?php // dl.php Ver.20200209-1412 (C)2020 NMTV http://npc.0am.jp/ nmtvweb@gmail.com // 参考: https://web.archive.org/web/20170705080423/http://www.spencernetwork.org/memo/tips-5.php // 使用例: dl.php?file=[file]&dl=[0|1|2] // [file] 開くファイルを指定。指定がない場合はHTTP400エラーを返します。 例: file.htm // [dl] 0ならば装飾して表示、1ならばダウンロード、2ならば装飾せずに表示。未指定ならば0。 ini_set('display_errors',0);ini_set('log_errors','On'); // エラーを画面に表示しないけどログには記録する $ScriptName="dl.php"; $ScriptVersion="20200209-1412"; $ScriptYear="2020"; $ScriptAuthor="(C) NMTV"; $ScriptWebPage="http://npc.0am.jp/"; $ScriptEmail="nmtvweb@gmail.com"; // エラー表示時はプレーンテキストで表示。閲覧モード時 header("Content-Type: text/plain; charset=UTF-8"); // ファイル名を取得。取得できない場合はエラーを吐いて終了。但しhosts時はhostsを送る。 if($_GET['file']==false){ if(strpos($_SERVER["HTTP_HOST"],'hosts')!==false||strpos($_SERVER["REQUEST_URI"],'hosts')!==false){$path_file="hosts";} else{header("HTTP/1.1 400 Bad Request");die("400 Bad Request\nダウンロードするファイルを指定してください\nExample dl.php?file=text.txt");} }else{$path_file=htmlspecialchars($_GET['file']);} // ディレクトリが指定された場合は400を返して終了。 if(is_dir($path_file)){header("HTTP/1.1 400 Bad Request");die("400 Bad Request\nディレクトリはダウンロードできません");} $FCmatch=""; header("X-Filename: \"".$path_file."\""); header("X-FileCheck: \"".preg_match('/^[\\\/(\'\\)(\"\\)]*/',$path_file,$FCmatch)."\""); header("X-FileCheckMatch: \"".print_r($FCmatch)."\""); // 彩茂な要求は402を返して終了。#1 if(strpos($_SERVER["REQUEST_URI"],'(')!==false||strpos($_SERVER["REQUEST_URI"],')')!==false||strpos($_SERVER["REQUEST_URI"],"%28")!==false||strpos($_SERVER["REQUEST_URI"],"%29")!==false){header("HTTP/1.1 402 Payment Required");die("402 Payment Required\n同情するなら金をくれ");} // 彩茂な要求は402を返して終了。#2 if(strpos($_SERVER["REQUEST_URI"],':')!==false||strpos($_SERVER["REQUEST_URI"],'\x27')!==false||strpos($_SERVER["REQUEST_URI"],"\x3a")!==false||strpos($_SERVER["REQUEST_URI"],"\x27")!==false){header("HTTP/1.1 402 Payment Required");die("402 Payment Required\n同情するなら金をくれ給へ!");} // 彩茂な要求は402を返して終了。#3 if(strpos($_SERVER["REQUEST_URI"],'$')!==false||strpos($_SERVER["REQUEST_URI"],'"')!==false||strpos($_SERVER["REQUEST_URI"],"\x24")!==false||strpos($_SERVER["REQUEST_URI"],"\x22")!==false){header("HTTP/1.1 402 Payment Required");die("402 Payment Required\n同情するなら金をくれや");} // 不正な要求は403を返して終了。 if(strpos(substr($path_file,0,2),"\x2F")!==false||strpos($path_file,'..')!==false||strpos($path_file,'\0')!==false||strpos($path_file,"\r")!==false||strpos($path_file,"\n")!==false||strpos($path_file,'%')!==false||strpos($path_file,'\\')!==false||strpos($path_file,'//')!==false){header("HTTP/1.1 403 Forbidden");die("403 Forbidden\nアクセスが拒否されました");} // ファイルが存在しない場合は404を返して終了。 if(!is_file($path_file)){header("HTTP/1.1 404 Not Found");die("404 Not Found\nファイルが見つかりません");} // ★★ ファイルをダウンロードするか表示するかの判定 if($_GET['dl']=="2"){}else if(!$_GET['dl']==false){// ★★ ファイルをダウンロードする場合 // ファイルをダウンロードさせる header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=\"".basename($path_file)."\""); header('Content-Length: '.filesize($path_file)); header("Content-Type: application/octet-stream"); if(!readfile($path_file)){header("Content-Type: text/plain; charset=UTF-8");die("ファイルを開けません");}else{die;} } // ★★ ファイルを閲覧する場合 // 閲覧時はHTML表示するようHeaderを上書き header("Content-Type: text/html; charset=UTF-8"); header("Pragma: no-cache"); header("Cache-Control: no-cache"); if(filesize($path_file)>=4096000){$nopreview="1";} // /★★ ?><!doctype html><html lang="ja"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"> <!-- base-hs.htm Ver.20200107-0415 (C)2020 NMTV http://npc.0am.jp/ nmtvweb@gmail.com ◆◇ --> <script src="/site/base.js" charset="UTF-8"></script><link rel="stylesheet" href="/site/base.css"> <meta name="msapplication-TileImage" content="/site/icon.png"><link rel="apple-touch-icon" href="/apple-touch-icon.png"> <meta name="keywords" content="南部185,NMTV,NMTV Web,4E4D5456,自宅サーバー,ホームページ,HTML5,岩手県八戸市"> <link rel="mask-icon" sizes="any" href="/site/icon-mono.svg" color="#805500"><meta name="theme-color" content="#603c18"> <!-- /header --> <title><?php echo basename($path_file); ?> - NMTV Web</title> <style><!-- *{box-sizing:border-box;} html,body,.body{height:100%;width:100%;} .content{font-family:monospace,'MS ゴシック', 'MS Gothic';display:block;margin:0.75em 0.5em;white-space:pre-wrap;word-wrap:break-word;box-sizing:border-box;} --></style> <div id="body"> <?php if(!$_GET['dl']=="2"){echo '<script src="/site/pageheader.js" charset="UTF-8"></script>';} echo '<script>'; echo 'document.getElementById("AddScriptInfo").innerHTML="'.$ScriptName.' Ver.'.$ScriptVersion.'";'; echo 'var DLPHPFileName="'.basename($path_file).'";var DLPHPFileSize="'.filesize($path_file).'";'; echo '</script>'; ?> <script><!-- function FileNameDisp(){ var AddItem=document.getElementById("AddItemL"); var AddItemElem=document.createElement('div'); AddItemElem.innerHTML=DLPHPFileName+" ("+DLPHPFileSize+" bytes)"; AddItemElem.setAttribute('class','Header-Item Header-Item-Single Header-Item-DisablePoint'); AddItemElem.setAttribute('title','Filename'); AddItemElem.style.display="block"; AddItemElem.style.opacity="0.5"; AddItemElem=AddItem.parentNode.insertBefore(AddItemElem,AddItem); } FileNameDisp(); var AddItem=document.getElementById("AddItemR"); var AddItemRElement=document.createElement('div'); AddItemRElement.innerHTML="ダウンロード"; AddItemRElement.setAttribute('class','Header-Item Header-Item-Single'); AddItemRElement.setAttribute('title','Download'); AddItemRElement.onclick=new Function("dlphpfile();"); AddItemRElement.style.cursor="pointer"; AddItemRElement.style.display="block"; AddItemRElement=AddItem.parentNode.insertBefore(AddItemRElement,AddItem); function dlphpfile(){ if(location.search.match(/file=(.*?)(&|$)/)){var FileGetParam=decodeURIComponent(location.search.match(/file=(.*?)(&|$)/)[1]);} else{var FileGetParam="hosts";} var FileGetLink=location.protocol+"//"+location.hostname;if(location.port){FileGetLink+=":"+location.port;} FileGetLink+=location.pathname+"?file="+FileGetParam+"&dl=1";window.location=FileGetLink; } --></script> <?php if($nopreview=="1"||filesize($path_file)>=4096000){echo '<div class="info-box m-width-512"><div class="info info-skyblue"><div class="info-h-wrapper"><div class="info-h">お知らせ<span class="side">Notice</span></div></div><div class="info-c">ファイルサイズが大きすぎるため、プレビューを表示できません。<br><div onclick="dlphpfile();" style="margin:0.5em 0.25em;padding:0.55em 0.75em 0.45em;" class="button-base btn-cl-vivid-blue color-white bold pointer inline-block">⇒ ダウンロード</div>'.basename($path_file).' <span style="font-size:0.8em;">('.filesize($path_file).' B)</span></div></div></div>';} else{echo '<pre class="content">'.htmlentities(file_get_contents($path_file),ENT_QUOTES).'</pre>';} ?> <?php if(!$_GET['dl']=="2"){echo '<footer class="footer" id="footer"><hr><div id="footer-text"><a href="/">サイトトップ</a> | <a href="/contact/">お問い合わせ</a> | <a href="#">ページトップ</a></div><div id="Copyright">© NMTV</div></footer>';}else{echo '<div id="Copyright"></div>';} ?> </div></html>