highl2.php

[Download]

1<?php
2    require_once("../../../logger.php");
3    
4    //////////////////////////////////////////////////////////////
5    // highl2.php
6    // 
7    // Version 2.4
8    // 
9    // A script for highlighting the code of a php-script
10    // with functionality for downloading the code as well.
11    // 
12    // Params:
13    //     o fn - filename of script to parse
14    // 
15    // Created in May 2003 by Niklas Lindgren (nikke@nikke.letm.com)
16    // 
17    // 
18    // Feel free to use free of charge. However I would appreciate
19    // if you would drop me a line if you do find this useful :)
20    //////////////////////////////////////////////////////////////
21
22    if( isset($_GET['fn']) && (is_file($_GET['fn'])) ) {       
23        $fn $_GET['fn'];
24        
25        if (ereg("(^/)|(\.\.)"$fn)) {
26            die("Illegal chars in filename");
27        }
28        
29        $fileisok true;
30    } else {
31        $fileisok false;
32    }
33
34    // check for download
35    if( isset($_GET['dl']) && ($_GET['dl'] == 1) && $fileisok ) {
36        header("Content-Type: application/octet-stream\n" );
37        header("Content-Disposition: attachment; filename=" $fn "\n\n");
38        readfile($fn);
39        exit(0);
40    
41?>
42
43<html><head>
44    <title>Sourcecode of <?php print( $fn ); ?></title>
45    <style type="text/css">
46        table,tr,td { font: 10pt courier; }        
47        .over { background-color: #dddddd; }
48        .out { background-color: white; }        
49        .rn {
50            background-color: #dddddd;
51            text-align: right;
52        }
53    </style>
54</head><body>
55    <?php if( ! $fileisok ): ?>
56        Invalid filename <?php print($_GET['fn']); ?>...
57    <?php else: ?>
58        <h1><?php print($fn); ?></h1>
59        [<a href="<?php printf"%s?fn=%s&dl=1"$_SERVER['PHP_SELF'], $fn )?>">Download</a>]<br>
60        <br>
61        <table cellspacing="0" cellpadding="2">
62        <?php
63            function striptags$str ) {
64                // hightlight_string() requires < ? and ? > to actually do something.
65                // this part removes them since we don't want them on every line
66                $pone strpos$str"&lt;?php" );
67                $ptwo strrpos$str"?&gt;" );
68                $str substr$str0$pone ).substr$str$pone+8$ptwo-($pone+8) ).substr$str$ptwo+);
69                
70                return $str;
71            }
72        
73            // read file contens into array
74            $linearray file$fn );            
75
76            for( $i 0$php 0$mlc 0$i sizeof($linearray); $i++ ) {
77                // in order for highlight to work correctly we need to check if this is explicitly html or php.
78                // or actually, the start of a php-block. Rows with "inline"-php don't require any state changes :)
79                if( strpos($linearray[$i], "<"."?") === FALSE || strpos($linearray[$i], "?".">") === FALSE ) {
80                    if( strpos($linearray[$i], "<"."?") !== FALSE ) {
81                        $php 1;
82                    } else
83                    if( strpos($linearray[$i], "?".">") !== FALSE ) {
84                        $php 2;
85                    }
86                }
87                
88
89                  * this part is here because multiline comments caused errors.
90                  * comment state is controlled here
91                  */
92                if( strpos($linearray[$i], "/*") !== FALSE && strpos($linearray[$i], "*/") === FALSE ) {
93                    $mlc 1;
94                    $linearray[$i] .= " */";
95                } else
96                if( $mlc != && strpos($linearray[$i], "*/") !== FALSE ) {
97                    $mlc 3;
98                }
99                
100                
101
102                  * this part is here because multiline-comments caused errors.
103                  * comment-markings are added here
104                  */
105                if( $mlc ) {
106                    $linearray[$i] = " // " $linearray[$i];
107                }
108                
109                switch( $php ) {
110                    case 0// Highlight html, or php which starts and ends on the same row
111                        $hrow highlight_string($linearray[$i], true);
112                        break;
113                    case 1// Highlight explicit php-code.
114                        $hrow striptagshighlight_string("<"."?php".$linearray[$i]."?".">"true) );
115                        break;
116                    case 2// Highlight the last row of a php-block
117                        $php 0;
118                        $hrow 
119                            striptags
120                                highlight_string("<"."?php".
121                                    substr($linearray[$i], 0strpos($linearray[$i], "?".">")+2).
122                                "?".">"true).
123                            highlight_string(substr($linearray[$i], strpos($linearray[$i], "?".">")+2), true) );  
124                        break;
125                }
126            
127
128                  * this part is here because multiline comments caused errors.
129                  * the added comment-markings are removed here.
130                  */
131                switch( $mlc )
132                {
133                    case 1:
134                        $hrow substr$hrow0strpos($hrow" */") ).substr$hrowstrpos($hrow"*/") + );
135                        $mlc++;
136                        break;
137                    case 3:
138                        $mlc 0;
139                    case 2:
140                        $hrow substr$hrow0strpos($hrow"// ") ).substr$hrowstrpos($hrow"//") + );
141                        break;
142                }
143                
144                // output the result and become very very happy
145                print( 
146                    "<tr><td class=\"rn\">".($i+1)."</td>".
147                    "<td onMouseOver=\"this.className = 'over'\" onMouseOut=\"this.className = 'out'\" nowrap>".
148                        str_replace"\n"""$hrow ).
149                    "</td></tr>\n" 
150                );
151            }
152        ?> 
153        </table>        
154    <?php endif; ?>
155</body></html>