source view: source-view.php


<?php
  


  function generateRandomString ()
  {
    $max_length = 20;
    $length     = rand( $max_length / 2, $max_length );

    $s = '';
    for ( $i = 0; $i < $length; ++$i )
    {
      $char_num = rand( 0, 61 );

      if ( $char_num < 10 )
      {
        $s .= chr( 48 + $char_num );
      }
      else if ( $char_num < 36 )
      {
        $s .= chr( 55 + $char_num );
      }
      else
      {
        $s .= chr( 61 + $char_num );
      }
    }

    return $s;
  }
  
  
  
  
  
  $source_page_name = 'index.php';
  $source_page_type = 'php';
  if ( isset( $_GET['page'] ) && preg_match( '/^\/?([_a-z0-9\.\-\+\.\/]+(php|css|js))$/i', $_GET['page'], $matches ) )
  {
    $filename = $matches[1];
    $filename = preg_replace( array( '/\.\.+/', '/\/+/', '/(\/\.)+\//' ), array( '..', '/', '/' ), $filename );
    $baselen  = strlen( $filename );
    
    while ( true )
    {
      $fnlen    = strlen( $filename );
      $filename = preg_replace( '/\/?[^\/]+\/\.\.\//', '/', $filename );
      
      if ( strlen( $filename ) == $fnlen )
      {
        break;
      }
    }
    
    if ( strlen( $filename ) != $baselen )
    {
      header( 'Location: /source-view.php?page=' . $filename );
      exit;
    }
    
    $filename = preg_replace( '/(?:\.{1,2}\/)+/', '', $filename );
    if ( file_exists( $filename ) )
    {
      $source_page_name = $filename;
      $source_page_type = strtolower( $matches[2] );
    }
  }
  
  preg_match( '/^(.*?\/?)([^\/]+)$/', $source_page_name, $matches );
  $header_page_name = $matches[2];
  $directory_base   = $matches[1];
  
  
  



  require './classes/Page.php';
  $page = new Page();
  $page->setTitle( 'source view' );
  $page->setParentURL( 'experience.php' );



  $page->addHeader( '<link rel="stylesheet" href="/lib/prism.css" type="text/css" />' );



  $page->printPageHeader();


  
?>
<h3>source view: <a href="<?php print $source_page_name; ?>"><?php print $header_page_name;?></a></h3>
<pre class="language-<?php print $source_page_type;?>" id="source"><code class="language-<?php print $source_page_type; ?>">
<?php

  $replace_from = array( '&', '<', '>', "\t", "\r\n" );
  $replace_to   = array( '&amp;', '&lt;', '&gt;', '  ', "\n" );

  if ( !isset( $_GET['rendered'] ) )
  {
    $source_handle = fopen( $source_page_name, 'r' );
  }
  else
  {
    $source_handle  = fsockopen( $_SERVER['SERVER_NAME'], 80, $error_number, $error_message );
    $source_request = "GET /$source_page_name HTTP/1.1\r\nHost: " . $_SERVER['SERVER_NAME'] . "\r\nConnection: Close\r\n\r\n";
    fwrite( $source_handle, $source_request );
  }

  while ( !feof( $source_handle ) )
  {
    $line = str_replace( $replace_from, $replace_to, fgets( $source_handle ) );

    if ( preg_match( "/^\r?\n?[a-f0-9]+\r?\n?$/", $line ) )
    {
      continue;
    }

    //if ( preg_match( '/\bPROTECTED_VALUES\[\'.*?\'\]\s*=/i', $line ) )
    if ( preg_match( '/\bPROTECTED_VALUE/i', $line ) ) // new version is more liberal about blocking, because why not
    {
      $line = preg_replace( '/(=\s*)\'.*\'/' , '$1\'' . generateRandomString() . '\'', $line );
    }

    print $line;
  }
  fclose( $source_handle );
  
?>
</code></pre>
<?php
      



  

  $js_droot  = preg_replace( '/\'/', '\\\'', $directory_base );
  $page->addFooter( <<<EOJ
<script type="text/javascript">
  var droot = '$js_droot';
  var clink = /([^'"\s]+\.(?:php|css|js|py))(?![a-z0-9\.])/i;

  function relink ( e )
  {
    for ( var i = 0; i < e.childNodes.length; ++i )
    {
      if ( e.childNodes[i].childNodes.length < 1 )
      {
        var clinks = e.childNodes[i].data.split( clink );
        if ( clinks.length > 1 )
        {
          var span = document.createElement( 'span' );
          for ( var j in clinks )
          {
            if ( clink.exec( clinks[j] ) )
            {
              var f = clinks[j];
              f     = f.replace( /^https?:\/\/[^\.]*\.618034\.com/, '' );
              f     = ( clinks[j].charAt( 0 ) != '/' ? droot : '' ) + f;
              var a = document.createElement( 'a' );
              a.setAttribute( 'href', '/source-view.php?page=' + f );
              a.appendChild( document.createTextNode( clinks[j] ) );
              
              a.style.color          = 'inherit';
              a.style.textDecoration = 'none';
              a.style.borderBottom   = '1px dotted ' + window.getComputedStyle( e ).color;
              
              span.appendChild( a );
            }
            else
            {
              span.appendChild( document.createTextNode( clinks[j] ) );
            }
          }
          
          e.replaceChild( span, e.childNodes[i] );
        }
        
        continue;
      }
      
      relink( e.childNodes[i] );
    }
  }
  
  window.addEventListener( 'load', function () { relink( document.getElementById( 'source' ) ); } );
</script>
EOJ
);
  $page->addFooter( '<script src="/lib/prism.js" data-default-language="markup"></script>' );
  $page->addFooter( '<script src="/js/prism-scroller.js"></script>' );



  $page->printPageFooter();
  
  
  
  
  
  
?>