Contents of file 'viewsrc.php':
1 <?php
2 //============================================================================
3 // File: viewsrc.php [PHP script]
4 // Created: August 21, 2005 [v.1.0]
5 // Last change: November 14, 2010 [v.1.9]
6 // Author: Fredrik Jonsson <http://jonsson.eu/>
7 //
8 // DESCRIPTION
9 // The VIEWSRC script generates (X)HTML code for displaying "typewriter-
10 // styled" source code, wrapped in a <div> block which maintains the visual
11 // appearance of the page. The (X)HTML code generated by the VIEWSRC script
12 // conforms to the XHTML 1.0 Strict specification and makes sure that any
13 // special characters present in the code are properly html:icized before
14 // display.
15 //
16 // USAGE
17 // Put the following block into the web page where you would like to display
18 // the file contents as code (using the default "en" language option):
19 //
20 // < ? php include "/php/viewsrc.php"; viewsrc("en"); ? >
21 // <rest of your (X)HTML code for the page>
22 //
23 // The viewsrc($lang); statement will remain silent without any action until
24 // a valid MD5 checksum appears as input value to the 'viewsrc' parameter
25 // in the URI. If found to match a valid file in the list of allowed files
26 // for display, this will generate a monospaced output of the contents of
27 // the file. Example of a valid URI, where the input value is the MD5
28 // checksum of 'cv.tex':
29 //
30 // http://jonsson.eu/about/?viewsrc=338066b225e308fc980f5a7327765c68
31 //
32 // The viewsrc($lang) script will then typically generate (X)HTML output of
33 // the following form:
34 //
35 // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
36 // "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
37 // <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
38 // <body id="oneColLayout">
39 // <div id="sourceView">
40 // <p><b>Contents of file 'cv.tex':</b></p>
41 // <pre>
42 // <tt>
43 // <code>
44 // [Your code in 'cv.tex' goes here.]
45 // </code>
46 // </tt>
47 // </pre>
48 // </div> <!-- End of sourceView div -->
49 // </body>
50 // </html>
51 //
52 // DEPENDENCIES
53 // For proper operation, the VIEWSRC script relies on the presence of the
54 // following PHP scripts, available at http://jonsson.eu/programs/php/:
55 //
56 // prevpage.php [http://jonsson.eu/programs/php/prevpage/]
57 // htmlicise.php [http://jonsson.eu/programs/php/htmlicise/]
58 // footer.php [http://jonsson.eu/programs/php/footer/]
59 //
60 // PARAMETERS
61 // The user-end interface to the routine is the function viewsrc($lang), in
62 // which the $lang parameter specifies which language to use for headers.
63 // Currently only two choices are valid: "en" (English) or "sv" (Swedish).
64 //
65 // CONFIGURATION
66 // Any file to be displayed by the VIEWSRC script must first be entered into
67 // the list of allowed files. This way, no file except the allowed ones may
68 // be displayed, implying that no file which otherwise is hidden by the PHP
69 // engine (such as password files or other files containing sensitive
70 // information) can be accessed. This list of allowed files is declared
71 // globally in the user-customizable section immediately below.
72 //
73 // Copyright (C) 2005-2010, Fredrik Jonsson <http://jonsson.eu/>
74 // Non-commercial copying welcome.
75 //============================================================================
76 //
77 // ###########################################################################
78 // ######## CUSTOMIZE THE FOLLOWING PARAMETERS WITH YOUR OWN SETTINGS ########
79 // ######## FOR DETAILS, SEE CONFIG INSTRUCTIONS IN THE HEADER ABOVE #########
80 // ###########################################################################
81 //
82 // Set the $display_linenumbers parameter to 'true' to have the viewsrc script
83 // displaying line numbers along the code to be displayed. Otherwise, the input
84 // source will be displayed clean, without any linenumbering.
85 //
86 $display_linenumbers=true;
87
88 //
89 // Define the files allowed for display by the VIEWSRC script. These are set
90 // as global-level character strings which are accessed by $GLOBALS[...].
91 //
92 // All files valid for viewing are here hardcoded as a list directly in the
93 // PHP source, to avoid any potential security issues from loading data from
94 // external text files (unless these files are already protected by the PHP
95 // engine, that is to say). For further details, see "PHP in a Nutshell" by
96 // Paul Hudson (O'Reilly, 2005).
97 //
98 $k=0;
99 $allowedfiles[$k++]="Makefile";
100 $allowedfiles[$k++]="magbragg/magbragg.w";
101 $allowedfiles[$k++]="magbragg/magbragg.c";
102 $allowedfiles[$k++]="magbragg/magbragg.tex";
103 $allowedfiles[$k++]="magbragg/Makefile";
104 $allowedfiles[$k++]="boxcount/boxcount.w";
105 $allowedfiles[$k++]="boxcount/boxcount.c";
106 $allowedfiles[$k++]="boxcount/boxcount.tex";
107 $allowedfiles[$k++]="boxcount/Makefile";
108 $allowedfiles[$k++]="boxcount/koch/koch.c";
109 $allowedfiles[$k++]="boxcount/koch/Makefile";
110 $allowedfiles[$k++]="dvlabel/dvlabel.w";
111 $allowedfiles[$k++]="dvlabel/dvlabel.c";
112 $allowedfiles[$k++]="dvlabel/dvlabel.tex";
113 $allowedfiles[$k++]="dvlabel/Makefile";
114 $allowedfiles[$k++]="epsimg/epsimg.w";
115 $allowedfiles[$k++]="epsimg/epsimg.c";
116 $allowedfiles[$k++]="epsimg/epsimg.tex";
117 $allowedfiles[$k++]="epsimg/Makefile";
118 $allowedfiles[$k++]="poincare/poincare.c";
119 $allowedfiles[$k++]="poincare/Makefile";
120 $allowedfiles[$k++]="poincare/polstate.mp";
121 $allowedfiles[$k++]="poincare/example-a.mp";
122 $allowedfiles[$k++]="poincare/example-b.mp";
123 $allowedfiles[$k++]="poincare/example-cs.mp";
124 $allowedfiles[$k++]="poincare/example-cc.mp";
125 $allowedfiles[$k++]="poincare/example-ccr.mp";
126 $allowedfiles[$k++]="poincare/example-cd.mp";
127 $allowedfiles[$k++]="poincare/example-d.mp";
128 $allowedfiles[$k++]="poincare/stoke.mp";
129 $allowedfiles[$k++]="epsbb/epsbb.c";
130 $allowedfiles[$k++]="epsbb/Makefile";
131 $allowedfiles[$k++]="lect1/lect1.tex";
132 $allowedfiles[$k++]="lect2/lect2.tex";
133 $allowedfiles[$k++]="lect3/lect3.tex";
134 $allowedfiles[$k++]="lect4/lect4.tex";
135 $allowedfiles[$k++]="lect5/lect5.tex";
136 $allowedfiles[$k++]="lect6/lect6.tex";
137 $allowedfiles[$k++]="lect7/lect7.tex";
138 $allowedfiles[$k++]="lect8/lect8.tex";
139 $allowedfiles[$k++]="lect9/lect9.tex";
140 $allowedfiles[$k++]="lect10/lect10.tex";
141 $allowedfiles[$k++]="lect11/lect11.tex";
142 $allowedfiles[$k++]="lect12/lect12.tex";
143 $allowedfiles[$k++]="ccubsolv.f";
144 $allowedfiles[$k++]="ccubsolvdrv.f";
145 $allowedfiles[$k++]="zgaussj.f";
146 $allowedfiles[$k++]="zgaussjdrv.f";
147 $allowedfiles[$k++]="mopagras.f";
148 $allowedfiles[$k++]="mopagrat.f";
149 $allowedfiles[$k++]="photoview.php";
150 $allowedfiles[$k++]="photoview.css";
151 $allowedfiles[$k++]="toledo.c";
152 $allowedfiles[$k++]="westley.c";
153 $allowedfiles[$k++]="emailform.php";
154 $allowedfiles[$k++]="../../../css/jonssona.css";
155 $allowedfiles[$k++]="../../../css/jonssonb.css";
156 $allowedfiles[$k++]="../../../css/jonssonc.css";
157 $allowedfiles[$k++]="../../../css/jonssond.css";
158 $allowedfiles[$k++]="/css/jonssona.css";
159 $allowedfiles[$k++]="/css/jonssonb.css";
160 $allowedfiles[$k++]="/css/jonssonc.css";
161 $allowedfiles[$k++]="/css/jonssond.css";
162 $allowedfiles[$k++]="navmenu.php";
163 $allowedfiles[$k++]="dailyquote.php";
164 $allowedfiles[$k++]="errata.tex";
165 $allowedfiles[$k++]="cv.tex";
166 $allowedfiles[$k++]="viewsrc.php"; // Yes, this very script!
167 $allowedfiles[$k++]="viewimg.php";
168 $allowedfiles[$k++]="bcconven.tex";
169 $allowedfiles[$k++]="eqnum.php";
170 $allowedfiles[$k++]="styleswitch.php";
171 $allowedfiles[$k++]="sitemap.php";
172 $allowedfiles[$k++]=".sitemap";
173 $allowedfiles[$k++]="dogm16x.c";
174 $allowedfiles[$k++]="dogm16x.h";
175 $allowedfiles[$k++]="main.c";
176 $allowedfiles[$k++]="Makefile_hex";
177 $allowedfiles[$k++]="Makefile_lib";
178 $allowedfiles[$k++]="../sitemap.xml";
179 $allowedfiles[$k++]="wiener/wiener.w";
180 $allowedfiles[$k++]="wiener/wiener.c";
181 $allowedfiles[$k++]="wiener/wiener.tex";
182 $allowedfiles[$k++]="wiener/Makefile";
183 $allowedfiles[$k++]="sgfilter/sgfilter.w";
184 $allowedfiles[$k++]="sgfilter/sgfilter.c";
185 $allowedfiles[$k++]="sgfilter/sgfilter.h";
186 $allowedfiles[$k++]="sgfilter/sgfilter.tex";
187 $allowedfiles[$k++]="sgfilter/example.c";
188 $allowedfiles[$k++]="sgfilter/example-0.0.dat";
189 $allowedfiles[$k++]="sgfilter/example-0.5.dat";
190 $allowedfiles[$k++]="sgfilter/example-1.0.dat";
191 $allowedfiles[$k++]="sgfilter/example-2.0.dat";
192 $allowedfiles[$k++]="sgfilter/fig1.mp";
193 $allowedfiles[$k++]="sgfilter/fig2.mp";
194 $allowedfiles[$k++]="sgfilter/fig3.mp";
195 $allowedfiles[$k++]="sgfilter/fig4.mp";
196 $allowedfiles[$k++]="sgfilter/fig5.mp";
197 $allowedfiles[$k++]="sgfilter/fig6.mp";
198 $allowedfiles[$k++]="sgfilter/Makefile";
199 //
200 // ###########################################################################
201 // ######## END OF USER CUSTOMIZABLE PARAMETERS ##############################
202 // ###########################################################################
203 //
204
205 function viewsrc($lang) {
206 if (($md5hash=$_GET["viewsrc"])!="") {
207 $platform=$_SERVER['SERVER_SOFTWARE'];
208 //
209 // I admit that the following construction with a hard-coded site root
210 // is NOT ok; it is here merely to overcome the very strange behaviour
211 // which I have found with the PHP engine on the Microsoft NT Server,
212 // which a previous webhost used. There, I was simply refused to extract
213 // the server-side site root of my web site by the usual
214 // $_SERVER['DOCUMENT_ROOT'], while a hard-coded string was alright.
215 // Simply look upon this construction as a brute-force way of overcoming
216 // this NT Server issue in case you need it. As usual, Apache users need
217 // not to worry.
218 //
219 if ((strpos($platform,"Microsoft")!==false)
220 &&(strpos($platform,"IIS")!==false)) {
221 $siteroot="e:\\jonsson";
222 } else {
223 $siteroot=$_SERVER['DOCUMENT_ROOT'];
224 }
225 include_once $siteroot."/php/prevpage.php";
226 include_once $siteroot."/php/htmlicise.php";
227 include_once $siteroot."/php/footer.php";
228
229 $num_allowed_files=count($GLOBALS['allowedfiles']);
230 $file_to_view="";
231 for ($k=1;$k<=$num_allowed_files;$k++) {
232 if (!strcmp(trim($md5hash),md5(trim($GLOBALS['allowedfiles'][$k]))))
233 $file_to_view=trim($GLOBALS['allowedfiles'][$k]);
234 }
235 echo "<div id=\"sourceView\">\n";
236 if ($file_to_view=="") {
237 for ($k=1;$k<=8;$k++) echo "<br />\n";
238 echo "<p class=\"center\"><b>Error: Invalid file ID!</b></p>\n";
239 for ($k=1;$k<=18;$k++) echo "<br />\n";
240 } else {
241 prevpage($lang);
242 switch ($lang) {
243 case en:
244 echo "<p><b>Contents of file '$file_to_view':</b></p>\n";
245 break;
246 case sv:
247 echo "<p><b>Innehåll av filen '$file_to_view':</b></p>\n";
248 break;
249 default:
250 echo "PHP Error: Language switch \"".$lang."\" is not recognized!\n";
251 }
252 echo "<pre>\n";
253 echo "<tt>\n";
254 echo "<code>\n";
255 htmlicise($file_to_view,$GLOBALS['display_linenumbers']);
256 echo "</code>\n";
257 echo "</tt>\n";
258 echo "</pre>\n";
259 prevpage($lang);
260 }
261 echo "<p class=\"viewsrc\">\n";
262 $viewsrc_url="http://jonsson.eu/programs/php/viewsrc/";
263 echo "Generated by <a href=\"".$viewsrc_url."\">::viewsrc::</a>\n";
264 echo "</p>\n";
265 echo "</div> <!-- End of sourceView div -->\n";
266 echo "<div class=\"clear\"></div>\n";
267 footer($lang);
268 echo "</div> <!-- End of container div -->\n";
269 echo "</body>\n";
270 echo "</html>\n";
271 exit(0);
272 }
273 }
274 ?>
275
Generated by ::viewsrc::