Source of “lister.php”.
266 lines, 6.7 KBytes.   Last modified 6:00 pm, 31st August 2015 PDT.
1 <?php // Emacs settings: -*- mode: Fundamental; tab-width: 4; -*- 2 3 //////////////////////////////////////////////////////////////////////////// 4 // // 5 // Source code lister // 6 // // 7 // Copyright 1998-2014, Andrew D. Birrell // 8 // // 9 // View a source file as HTML, with tab stops every 4 character positions // 10 // // 11 // Restricted to files in the current directory, and support directories // 12 // // 13 //////////////////////////////////////////////////////////////////////////// 14 15 function putHtmlHeader($title) { 16 // Write HTML up to and including the opening <body> tag. 17 // 18 // Caller should html-escape $title 19 // 20 ?> 21 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 22 "http://www.w3.org/TR/html4/strict.dtd"> 23 <html> 24 <head> 25 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 26 <meta name="viewport" content="width=656"> 27 28 <style type="text/css"><!-- 29 30 A:link { color: #000099; } 31 A:active { color: #ff0000; } 32 A:visited { color: #000099; } 33 A:hover { color: #006600; } 34 35 body { 36 margin: 0px; 37 padding: 0px; 38 -webkit-text-size-adjust: 100%; 39 -moz-text-size-adjust: 100%; 40 -ms-text-size-adjust: 100%; 41 } 42 .outer { 43 position: absolute; 44 top: 0px; 45 bottom: 8px; /* allows for .divider height */ 46 width: 100%; 47 } 48 .divider, .dividerHighlight { 49 border: 2px outset #dddddd; 50 background-color: #bbbbbb; 51 height: 4px; 52 cursor: pointer; 53 } 54 .dividerHighlight { 55 border-style: inset !important; 56 } 57 .inner { 58 overflow: auto; 59 -webkit-overflow-scrolling: touch; 60 } 61 #topPart { 62 height: 50%; 63 display: none; 64 } 65 #bottomPart { 66 height: 100%; 67 } 68 .cr { 69 background-color: #999999; 70 color: #ffffff; 71 border-top: 2px #cccccc ridge; 72 border-bottom: 2px #cccccc ridge; 73 padding-left: 1em; 74 padding-right: 1em; 75 font-size: 0.75em; 76 line-height: 1.5; 77 } 78 .right { 79 float: right; 80 } 81 .source, .pad { 82 white-space: pre; 83 font-family: monospace, courier, "courier new", lucida; 84 font-size: 13px; 85 } 86 div.pad { 87 line-height: 0.5em; 88 overflow: hidden; 89 } 90 span.n { 91 background-color: #bbbbbb; 92 color: #ffffff; 93 border-left: 1px #999999 solid; 94 border-right: 1px #999999 solid; 95 } 96 @media screen and (max-device-width: 500px) { 97 /* disable the split-screen machinery so that UI hiding and landscape 98 mode work nicely on the iPhone */ 99 .outer { 100 position: static; 101 } 102 .divider { 103 display: none; 104 } 105 } 106 --></style> 107 108 <script type="text/javascript"><!-- 109 110 var cloned = false; 111 var divided = false; 112 var savedPos = 0; 113 114 function splitUnsplit() { 115 // Flip between split and un-split views 116 // 117 var topPart = document.getElementById("topPart"); 118 var bottomPart = document.getElementById("bottomPart"); 119 if (!cloned) { 120 // topPart.innerHTML = bottomPart.innerHTML; 121 var children = bottomPart.childNodes; 122 for (var i = 0; i < children.length; i++) { 123 topPart.appendChild(children[i].cloneNode(true)); 124 } 125 cloned = true; 126 } 127 if (divided) { 128 savedPos = topPart.scrollTop; 129 topPart.style.display = "none"; 130 bottomPart.style.height = "100%"; 131 } else { 132 topPart.style.display = "block"; 133 bottomPart.style.height = "50%"; 134 topPart.scrollTop = savedPos; 135 } 136 divided = !divided; 137 return false; 138 } 139 140 function mouseDown(elt) { 141 elt.className = "dividerHighlight"; 142 return true; 143 } 144 145 function mouseUpOut(elt) { 146 elt.className = "divider"; 147 return true; 148 } 149 150 --></script> 151 152 <title><?php echo $title; ?></title> 153 </head> 154 155 <body> 156 <?php 157 } 158 159 function putTopLine($s) { 160 // Write HTML for the top credit line with given contents 161 // 162 ?> 163 <div class=cr><?php echo htmlentities($s) ?></div> 164 <?php 165 } 166 167 function putHtmlTrailer() { 168 // Write HTML from the closing body tag to the end 169 // 170 ?> 171 </body> 172 </html> 173 <?php 174 } 175 176 function replaceAll($pattern, $replacement, $target) { 177 // Iterate replacement while it's making a difference 178 // 179 while (true) { 180 $new = preg_replace($pattern, $replacement, $target); 181 if ($new == $target) break; 182 $target = $new; 183 } 184 return $new; 185 } 186 187 function addTab($str) { 188 // Add spaces equivalent to a 4-space tab stop at end of $str 189 // 190 $str = stripslashes($str); // preg "e" added slash escapes 191 return $str . str_repeat(" ", (4-((strlen($str)-1) % 4))); 192 } 193 194 if (!isset($_GET["file"])) { 195 // Missing argument 196 putHtmlHeader("Source Lister"); 197 putTopLine("Usage: lister.php?file=filename"); 198 putHtmlTrailer(); 199 } else { 200 $file = $_GET["file"]; 201 if (get_magic_quotes_gpc() == 1) $file = stripslashes($file); 202 if ($file == "" || $file != basename($file) || is_dir($file)) { 203 header("HTTP/1.0 403 Forbidden"); 204 putHtmlHeader("Forbidden"); 205 putTopLine("The argument should be a simple file name, " . 206 "not a path or directory name."); 207 putHtmlTrailer(); 208 } else if (!is_readable($name = $file) && 209 !is_readable($name = "/var/www/birrell-org/support/$file") && 210 !is_readable($name = "/var/www/allsites/$file")) { 211 header("HTTP/1.0 404 Not Found"); 212 putHtmlHeader("Not Found"); 213 putTopLine("The file does not exist, or is not readable."); 214 putHtmlTrailer(); 215 } else { 216 $str = file_get_contents($name); 217 $str = preg_replace("#\r\n?#", "\n", $str); 218 $str = replaceAll("#(\n[^\t\n]*)\t#e", "addTab('$1')", $str); 219 $str = rtrim($str); 220 $lines = explode("\n", $str); 221 header("Content-Type: text/html; charset=ISO-8859-1"); 222 putHtmlHeader(htmlentities($file)); 223 ?> 224 <div class=outer> 225 <div id=topPart class=inner></div> 226 <div class=divider 227 title="Click to split or un-split the window" 228 onmousedown="return mouseDown(this)" 229 onmouseup="return mouseUpOut(this)" 230 onmouseout="return mouseUpOut(this)" 231 onclick="return splitUnsplit()"> 232 </div> 233 <div id=bottomPart class=inner> 234 <div class=cr> 235 Source of &ldquo;<?php echo htmlentities($file); ?>&rdquo;. 236 <div class=right> 237 <?php echo count($lines) ?> lines, <?php 238 echo round(filesize($name)/102.4)/10 ?> KBytes. &nbsp; 239 Last modified <?php echo date("g:i a, jS F Y T", filemtime($name)) ?>. 240 </div> 241 </div> <!-- cr --> 242 <div class=pad><span class=n> </span></div> 243 <div class=source><?php 244 $n = 0; 245 foreach ($lines as $line) { 246 printf("<span class=n>%5d </span> %s\n", 247 (++$n), 248 htmlentities( 249 (strlen($line) > 76 ? substr($line, 0, 76) : $line))); 250 while (strlen($line) > 76) { 251 $line = substr($line, 76); 252 echo "<span class=n> </span> " . 253 htmlentities(substr($line, 0, 76)) . "\n"; 254 } 255 } 256 ?> 257 </div> 258 <div class=pad><span class=n> </span></div> 259 <div class=cr>End of listing</div> 260 </div> <!-- inner --> 261 </div> <!-- outer --> 262 <?php 263 putHtmlTrailer(); 264 } 265 } 266 ?>
End of listing