RM - Source Code View

Source Code Viewer

This page lets you view source code from my server. The program uses a brute force code formatter to color code elements. NOTE: I wrote this program while trying to learn the vim text editor. This is not my usual coding style.

Use this select box to select a file.

Hide Lines Numbers

View: Source Code Viewer

The Source code view will pull live code from this server and format it is bright living colors.

Formatted Code

Below is the code all formatted with bright colors. The program links to files opened with include() and expands those opened with require(). Clicking on the require line should change visibility.

Source Code Viewer

001 <!DOCTYPE html> 002 <html lang="en"> 003 <head> 004 <meta charset="UTF-8" /> 005 <meta name="viewport" content="width=device-width, initial-scale=1" /> 006 <meta name="Author" content="Kevin Delaney" /> 007 <?php 008 // We break into PHP to get the data we need to print the header. 009 // These are the buttons we use to navigate the script: 010 const TEST_BTN = 'Test Button'; 011 const SHOW_SCRIPT_BTN = 'Show Script'; 012 const BLOB_BTN = 'Process Text'; 013 // scrub the user input 014 $scriptNo = (isset($_REQUEST['script']))? (int) $_REQUEST['script'] : 0; 015 $btn = (isset($_REQUEST['btn']))? $_REQUEST['btn'] : ''; 016 $hideLines = (isset($_POST['lines']))? $_POST['lines'] : 'N'; 017 // rmHead opens a database connection and functions for querying the database. 018 // functions include msgNote, dbConn, sqlRow, and sqlArr. 019 include('/var/www/php/rmHead.php'); // there should be a link in the include. 020 dbConn(DB_MAIN,'+code'); // attach the code database. 021 $sql = 'SELECT hit_cnt, display_cd, category_id, program_nm, code_path, 022 url, keywords, short_desc 023 FROM code.Code_Viewer 024 WHERE code_id = ?'; 025 026 list($hit_cnt, $display_cd, $category_id, $program_nm, $code_path, 027 $url, $keywords, $short_desc) = sqlRow($sql,[$scriptNo]); 028 // Update Page Count 029 if ($hit_cnt !== false) { 030 sqlExec('UPDATE code.Code_Viewer SET hit_cnt=hit_cnt+1 WHERE code_id=?',[$scriptNo]); 031 } 032 $pageTitle = ($btn==BLOB_BTN)? 'User Input' : $program_nm; 033 $scriptStr = ($scriptNo > 0)? '?script='.$scriptNo : ''; 034 echo ' <meta name="keywords" content="'.htmlentities($keywords).'" /> '.PHP_EOL; 035 echo ' <meta name="description" content="'.htmlentities($short_desc).'" />'.PHP_EOL; 036 echo ' <title>View Source Code - '.htmlentities($pageTitle).'</title>'.PHP_EOL; 037 038 $scriptStr = ($scriptNo > 0)? '?script='.$scriptNo : ''; 039 echo ' <link rel="canonical" href="https://yintercept.com/resources/view.php'.$scriptStr.'" />'.PHP_EOL; 040 ?> 041 <link rel="stylesheet" href="rm.css" type="text/css"> 042 <script type="text/javascript"> 043 function toggle_visibility(id) { 044 var e = document.getElementById(id); 045 if(e.style.display == 'none') 046 e.style.display = 'block'; 047 else 048 e.style.display = 'none'; 049 } 050 </script> 051 </head> 052 <body> 053 <h1 id="pageTitle"><a href="https://yintercept.com/resources/">RM - Source Code View</a></h1> 054 <!-- The most critical part of any web page is the Ad!, without the ad we perish --> 055 <div class="ad"> 056 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 057 <!-- yintercept - responsive goodle ad --> 058 <ins class="adsbygoogle" 059 style="display:block" 060 data-ad-client="ca-pub-7057064824800338" 061 data-ad-slot="4504818749" 062 data-ad-format="auto"></ins> 063 <script> 064 (adsbygoogle = window.adsbygoogle || []).push({}); 065 </script> 066 </div> 067 <div class="wider"> 068 <h2>Source Code Viewer</h2> 069 <p>This page lets you view source code from my server. The program uses a brute force code formatter to color code elements. NOTE: I wrote this program while trying to learn the <b>vim</b> text editor. This is not my usual coding style.</p> 070 <p>Use this select box to select a file.</p> 071 <form action="https://yintercept.com/resources/view.php" method="post"> 072 <?php 073 // we pop back into PHP for the main program. The code viewer expands the contents of 074 // files attached with the require statement, which you can hide by clicking on the line.
require('/var/www/php/codeFormatter.php');
001 <?php 002 /** 003 * This is a brute force program for displaying HTML & PHP code in vivid color. 004 * 005 * @see https://yintercept.com/resources/view.php 006 * 007 * NOTE: I wrote this program while trying to learn the vim editor This is 008 * an example of flow-of-concious programming. But it does what I want. 009 * 010 * The program looks at each character in a program. The flow constrol variable 011 * $fc keeps track of where you are in the code. 012 * 013 * It embeds links called with require() inline and links to include() programs 014 * 015 * The program can show line numbers, although they really just get in the way. 016 * 017 * 2015/12/29 I discovered that there is a professional program called docBlock 018 * for formatting comments. I adopted their keyword style. 019 * 020 * @version 0.4 021 * @author Kevin Delaney 022 * @copyright 2015 Kevin Delaney 023 * @license https://yintercept.com/resources/license.html 024 */ 025 // These are the values for the flow control character 026 const IN_HTML = 0; 027 const IN_TAG = 1; // in tag def section 028 const IN_ATTR = 2; // in tag attribute section 029 const IN_ATXT = 3; // in attribute text. 030 const IN_CSS = 10; // not implemented at the moment. 031 const IN_PHP = 20; 032 const IN_PVAR = 21; 033 const IN_PTXT = 22; 034 const IN_PCMT = 23; 035 const IN_DBLK = 24; // processing a docBlock tag 036 const IN_SCRIPT = 30; // not implemented yet 037 // PHP variables match the following pattern. 038 const VAR_PATTERN = '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/'; 039 class codeFormatter { 040 private $fc; // The section of code determines flow control 041 private $word; // The current word 042 private $closeTag; // holds the current close tag. 043 private $showNo = true; 044 private $lineNo = 0; // Counts the lines in the code. 045 private $lineStr = ''; 046 private $closer=''; 047 // I did not design program for recursion; So I will buffer the output 048 // and reassemble before echoing. 049 private $buffer = 0; //number of the active buffer 050 private $buffCnt = 0; // total number of buffers 051 private $buffArr = array(); // Holds the output 052 private $buffTree = array(); // Holds the buffer definitions. 053 private $queue = array(); // list files waiting processing. 054 055 // This array of arrays holds the name and location of the files to view: 056 private $programs = array(); 057 public $specialWords = array(); 058 function __construct($start = IN_HTML) { 059 $this->fc = $start; 060 // Special words comes from a file delimited by the line break. 061 $this->specialWords = explode("\n",strtr(file_get_contents('/var/www/php/specialwords.txt'),"\r",'')); 062 063 // I strip out /r in case someone edits it with a different editor 064 } 065 /** 066 * loads an array of documented programs 067 * @param array Give it an array of programs to parse 068 */ 069 public function setPrograms($arr) { 070 $this->programs = $arr; // a 2D array containing a list of programs. 071 } 072 /** 073 * If true the code will include a bunch of line numbers 074 * @param boolean set this to false to remove annoying line numbers 075 */ 076 public function showLineNumbers($boo) { 077 // The program defaults to showing line numbers. I find them annoying. 078 $this->showNo = ($boo === true)? true : false; 079 } 080 /** 081 * check the dictionary to see if this is a special word. 082 * it wraps special words in magenta. 083 * @param string the word to check 084 */ 085 private function chkW() { 086 $rv = $this->word; 087 // turns special words magenta. 088 if ($this->fc == IN_PCMT) { 089 // Added 12/29/15: Check for docBlock comments and links 090 if (substr($this->word,0,1) == '@') { 091 $rv = '<span class="g">'.$this->word.'</span>'; 092 } elseif (substr($this->word,0,4) == 'http') { 093 $rv = '<a href="'.$this->word.'">'.$this->word.'</a>'; 094 } else { 095 $rv = $this->word; 096 } 097 } elseif (strlen($this->word) > 1) { 098 if (in_array(trim($this->word), $this->specialWords)) { 099 $rv = '<span class="m">'.$rv.'</span>'; 100 } 101 } 102 // I am thinking of adding a dictionary with PHP functions that I would 103 // wrap in a green span. It would go here. 104 $this->word = ''; 105 return $rv; 106 } 107 /** 108 * This is the main program. You give it a line and it parses char by char 109 * the $fc variable keeps strack of where we are in the code. 110 * @param str line to parse 111 */ 112 private function parseLine($inStr) { 113 $str = strtr($inStr,"/n/r",'').' '; 114 $rv = ''; 115 $this->lineNo++; 116 if ($this->showNo) $this->lineStr = sprintf('%03s ',$this->lineNo); 117 $len = strlen($str); 118 for ($i=0; $i<$len; $i++) { 119 $chr=$str[$i]; 120 121 if ($chr == "\r" or $chr == "\n") { 122 $rv .= $this->chkW(); // print & clear the word buffer. 123 } elseif ($this->fc == IN_HTML) { 124 if ($chr == '<') { 125 if ($str[$i+1] == '/') { 126 $i++; 127 $rv .='<span class="c"></</span><span class="y">'; 128 $this->fc = IN_TAG; 129 } elseif (substr($str,$i+1,4) == '?php'){ 130 $i += 4; 131 $rv .='<span class="m"><?php</span>'; 132 $this->fc = IN_PHP; 133 } else { 134 $rv .= '<span class="c"><</span><span class="y">'; 135 $this->fc = IN_TAG; 136 } 137 } else { 138 $rv .= $chr; 139 } 140 } elseif ($this->fc == IN_TAG) { 141 if ($str == '/' && $str[$i+1] == '>'){ 142 $rv .= '</span><span class="c">/></span>'; 143 $i++; 144 $this->fc = IN_HTML; 145 } elseif ($chr == '>') { 146 $rv .= '</span><span class="c">></span>'; 147 $this->fc = IN_HTML; 148 } elseif ($chr==' ') { 149 // we are now in the tag attribute section. 150 $rv .= '</span> <span class="g">'; 151 $this->fc = IN_ATTR; 152 } else { 153 $rv .= $chr; 154 } 155 } elseif ($this->fc == IN_ATTR) { 156 if ($str == '/' && $str[$i+1] == '>'){ 157 $rv .= '</span><span class="c">/></span>'; 158 $i++; 159 $this->fc = IN_HTML; 160 } elseif ($chr == '>') { 161 $rv .= '</span><span class="c">></span>'; 162 $this->fc = IN_HTML; 163 } elseif ($chr=='=' && ($str[$i+1]=='"' or $str[$i+1]== "'")) { 164 // we are now in attribute text. 165 $i++; 166 $this->closer = $str[$i]; // this ie either " or ' 167 $rv .= '=</span><span class="r">'.$this->closer; 168 $this->fc = IN_ATXT; 169 } else { 170 $rv .= $chr; 171 } 172 } elseif ($this->fc == IN_ATXT) { 173 if ($chr == $this->closer) { 174 $rv .= $chr.'</span><span="g">'; 175 $this->fc = IN_ATTR; 176 } else { 177 $rv .= $chr; 178 } 179 } elseif ($this->fc == IN_PHP) { 180 if ($chr == '?' && $str[$i+1] == '>') { 181 $rv .= $this->chkW().'<span class="m">?></span>'; 182 $i++; 183 $this->fc = IN_HTML; 184 } elseif ($chr == '/' && $str[$i+1] == '/') { 185 // comment to the end of the line 186 $rv .= $this->chkW().'<span class="b">'.trim(substr($str,$i)).'</span>'; 187 $i = $len+1; // break out of loop. 188 } elseif ($chr == '$') { 189 $rv .= $this->chkW().'<span class="y">$'; 190 $this->fc = IN_PVAR; 191 } elseif ($chr == '>') { 192 $rv .= $this->chkW().'>'; 193 } elseif ($chr == '<') { 194 $rv .= $this->chkW().'<'; 195 } elseif ($chr == "'" or $chr== '"') { 196 $this->closer = $chr; 197 $rv .= $this->chkW().'<span class="r">'.$chr; 198 $this->fc = IN_PTXT; 199 } elseif ($chr == '/' && $str[$i+1] == '*') { 200 $rv .= $this->chkW().'<span class="b">/*'; 201 $i++; 202 $this->fc = IN_PCMT; 203 } elseif (strpos(' :;=}>-!.',$chr) !==false) { 204 $rv .= $this->chkW().$chr; 205 } else { 206 $this->word .= $chr; 207 if ($this->word == 'include') { 208 $chkFile = trim(strtr(substr($str,$i+1),'x"();\'','x ')); 209 $pPos = 0; 210 foreach ($this->programs as $pArr) { 211 if ($chkFile == $pArr[2]) { 212 $rv .= 'include(\'<a href="view.php?script='.$pPos.'">'.$chkFile.'</a>\'); '; 213 $rv .= '<span class="b">// links to <b>'.$pArr[0].'</b></span>'; 214 $this->word = ''; 215 $i = $len+1; 216 } 217 $pPos++; // the script is the position in the array. 218 } 219 } elseif ($this->word == 'require') { 220 $chkFile = trim(strtr(substr($str,$i+1),'"();\'',' ')); 221 if (file_exists($chkFile)) { 222 $this->buffCnt++; 223 $this->buffTree[$this->buffer]['child']=$this->buffCnt; 224 $this->buffArr[$this->buffer] .='<div class="inner" onclick="toggle_visibility(\'buff'.$this->buffCnt.'\')">require(\''.$chkFile.'\');</div><div id="buff'.$this->buffCnt.'">'; 225 $this->buffTree[$this->buffCnt]['file']=$chkFile; 226 // add child to the queue & increment counter for parent sibling 227 $this->queue[] = $this->buffCnt++; 228 $this->buffer = $this->buffCnt; 229 230 $this->word = ''; 231 // initialize array with a header 232 $this->buffArr[$this->buffer] = '<div class="inner" onclick="toggle_visibility(\'buff'.($this->buffCnt-1).'\'" >// End Require</div></div>'; 233 $this->fc = IN_PHP; 234 $i = $len+1; 235 } 236 } 237 } 238 } elseif ($this->fc == IN_PCMT) { 239 if ($chr == '/' && $str[$i-1] == '*') { 240 $rv .= $this->chkW().'/</span>'; 241 $this->fc = IN_PHP; 242 } elseif ($chr == ' ') { 243 $rv .= $this->chkW().' '; 244 } else { 245 $this->word .= $chr; 246 } 247 } elseif ($this->fc == IN_PVAR) { 248 if ($chr == '-' and $str[$i+1] == '>') { 249 //This is a class variable 250 $i++; 251 $rv .= '->'; 252 } elseif(!preg_match(VAR_PATTERN,$chr)){ 253 $rv .= '</span>'.$chr; 254 $this->fc = IN_PHP; 255 } else { 256 $rv .= $chr; 257 } 258 } elseif ($this->fc == IN_PTXT) { 259 // A blackslash escapes the next character. 260 if ($chr == "\\") { 261 $i++; 262 $rv .= "\\".$str[$i]; 263 } elseif ($chr == $this->closer) { 264 if ($str[$i+1] == $this->closer) { 265 // this is an escaped quote. 266 $i++; 267 $rv .= $chr.$chr; 268 } else { 269 // this is the real closer 270 $rv .= $chr.'</span>'; 271 $this->fc = IN_PHP; 272 } 273 } elseif ($chr == '>') { 274 $rv .= '>'; 275 } elseif ($chr == '<') { 276 $rv .= '<'; 277 } else { 278 $rv .= $chr; 279 } 280 } else { 281 echo 'This should not happen. '.$this->fc; 282 } 283 } 284 285 // $this->lineStr. 286 $rv .= $this->chkW(); 287 $this->buffArr[$this->buffer] .= $this->lineStr.$rv.PHP_EOL; 288 } 289 public function tokBlock (&$block) { 290 $tok = strtok($block, "\n"); 291 $this->lineNo = 0; 292 while ($tok !== false) { 293 $this->parseLine($tok); 294 $tok = strtok("\n"); 295 } 296 } 297 298 private function loadFile($fileName) { 299 // I prefer to loop through files with fgets. As I am pulling from source, 300 // I switched to file_get_contents to reduce file open time. 301 try { 302 if (file_exists($fileName)) { 303 $all = file_get_contents($fileName); 304 } else { 305 $all = ''; 306 } 307 $this->tokBlock($all); 308 //$handle=fopen($fileName,'r'); 309 //if ($handle) { 310 // while (($line = fgets($handle, 4096)) !== false ) { 311 // $this->parseLine($line); 312 // } 313 // } 314 } catch (Exception $e) { 315 echo 'Invalid file: '.$e->getMessage(); 316 } 317 } 318 public function processQueue() { 319 // I process at most 30 blocks. To prevent infinite loops 320 for ($i=0; $i<30; $i++) { 321 if (isset($this->queue[$i])) { 322 if (isset($this->buffTree[$this->queue[$i]]['file'])) { 323 $this->buffer = $this->queue[$i]; 324 if (!isset($this->buffArr[$this->buffer])) $this->buffArr[$this->buffer] = ''; 325 $this->loadFile($this->buffTree[$this->queue[$i]]['file']); 326 } 327 } else { 328 $i += 30; 329 } 330 } 331 } 332 public function queueFile($fileName) { 333 $this->buffTree[0]['file'] = $fileName; 334 $this->queue[0] = 0; 335 } 336 public function echoBuffer ($id = 0) { 337 echo $this->buffArr[$id]; 338 $next = (isset($this->buffTree[$id]['child']))? $this->buffTree[$id]['child'] : 0; 339 if ($next > 0) { 340 //buffers get created in pairs. 341 $this->echoBuffer($next); // the child 342 $this->echoBuffer($next +1); // a sibling of the parent. 343 } 344 } 345 public function processBlock($block) { 346 $this->buffArr[0] = ''; 347 $this->queue[0] = 0; 348 } 349 } 350 ?>
// End Require
075 076 // create an array of all programs available for viewing. 077 $sql = 'SELECT code_id, program_nm, code_path, hit_cnt 078 FROM code.Code_Viewer 079 WHERE category_id NOT IN (0,9)'; 080 $progArr = sqlAll($sql,[]); 081 $pCnt = count($progArr); 082 // produce check box. 083 $checked = ($hideLines == 'Y')? ' checked="checked"' : ''; 084 echo 'Hide Lines Numbers <input type="checkbox" name="lines" value="Y"'.$checked.' />'.PHP_EOL; 085 // Produce a select list. 086 echo '<select name="script">'; 087 for ($i=0; $i<$pCnt; $i++) { 088 if ($progArr[$i][0] == $scriptNo) { 089 $sel = ' selected'; 090 $pageViews = $progArr[$i][3] ; 091 } else { 092 $sel = ''; 093 } 094 echo '<option value="'.$progArr[$i][0].'"'.$sel.'>'.$progArr[$i][1].'</option>'.PHP_EOL; 095 } 096 echo '</select>'.PHP_EOL; 097 echo '<input type="submit" name="btn" value="'.SHOW_SCRIPT_BTN.'" /></p>'.PHP_EOL; 098 echo '</form>'; 099 echo '<h3>View: '.$pageTitle.'</h3>'; 100 echo '<p>'.$short_desc.'</p>'; 101 // we pop back into HTML 102 ?> 103 <h3>Formatted Code</h3> 104 <p>Below is the code all formatted with bright colors. The program links to files opened with <b>include</b>() and expands those opened with <b>require</b>(). Clicking on the require line should change visibility.</p> 105 <code id="code"> 106 <div class="codeBlack"><h2 class="inner"><?php echo $pageTitle; ?></h2> 107 <?php 108 // This is where the main program begins. 109 $cf = new codeFormatter(); 110 $cf->setPrograms($progArr); 111 if ($hideLines=='Y') $cf->showLineNumbers(false); 112 if ($btn == BLOB_BTN) { 113 if (isset($_POST['blob'])) { 114 $cf->processBlock($_POST['blob']); 115 } else { 116 $cf->processBlock('<html><body> 117 <p>You need to enter some text in the box for this to work.</p> 118 </body> 119 </html>'); 120 } 121 } else { 122 // if script outside range, show the zero script. 123 if (file_exists($code_path)) { 124 $filetime = date("F d Y H:i:s.", filectime($code_path)); 125 $cf->queueFile($code_path); 126 $cf->processQueue(); 127 $cf->echoBuffer(); 128 } else { 129 echo '<p>file '.$code_path.' not found on this server.</p>'; 130 $filetime = ''; 131 } 132 } 133 ?> 134 </div></code> 135 <p>Use "view source" from your browser to grab the output. Feel free to link to this project and check out the <a href="https://yintercept.com/resources">Resource Model</a> for information on PHP coding or my tumblr blog for picture of <a href="http://tumblr.arizonacolor.us">Arizona</a>, <a href="http://coloradocolor.tumblr.com">Colorado</a> or <a href="http://tumbler.UtahColor.com">Utah</a>.</p> 136 <?php 137 // stats and rov from the file. 138 echo '<p>File last modified at '.$filetime.'. This page has been viewed '.$hit_cnt.' Times.</p>'; 139 // Print a record of revisions: 140 $sql = 'SELECT version_id, revision_id, user_id, revision_dt, desc_txt 141 FROM code.Code_Revision 142 WHERE code_id = ? 143 ORDER BY version_id DESC, revision_id DESC'; 144 $row = sqlRow($sql,[$scriptNo]); 145 if (sqlRow(DB_CHK)) { 146 echo '<table style="margin: 1em 5% 1em 5%"> 147 <tr><th colspan=4">Record of Revisions</th></tr> 148 <tr><th>Rev</th><th>by</th><th>Date</th><th>Description</th></tr>'.PHP_EOL; 149 } 150 $rowCnt = 0; 151 while (sqlRow(DB_CHK)) { 152 if ($rowCnt++ > 100) break; 153 $classStr = (($rowCnt % 2)==0)? ' class="rowone"' : ' class="rowtwo"'; 154 echo '<tr '.$classStr.'><td>'.$row[0].'.'.$row[1].'</td><td>kd</td><td>'.$row[3].'</td><td>'.$row[4].'</td></tr>'.PHP_EOL; 155 $row = sqlRow(); 156 } 157 if ($rowCnt > 0) echo '</table>'; 158 // I added a log hit program. 159 logHit(0,31,$scriptNo); 160 ?> 161 </div> 162 <p style="text-align: center"> 163 <a href="http://blog.yintercept.com" style="color: #eea;">blog</a> 164 ~ <a href="https://yintercept.com/resrouces" style="color: #eea;">Resource Model</a> 165 ~ <a href="links.html" style="color: #eea;">links</a> 166 </p> 167 </body> 168 </html>

Use "view source" from your browser to grab the output. Feel free to link to this project and check out the Resource Model for information on PHP coding or my tumblr blog for picture of Arizona, Colorado or Utah.

File last modified at January 24 2016 20:08:20.. This page has been viewed 2307 Times.

Record of Revisions
RevbyDateDescription
0.5kd2016-01-01Reversed order of the Revsions; so newest will be on top. Started grouping code into packages.
0.4kd2015-12-29Added processing for docBlock comments and links.
0.3kd2015-12-24Added file time and this code revision table.
0.2kd2015-12-24Created the code.db database and hooked it to this function to count hits

blog ~ Resource Model ~ links