Jump to content

Source Code Viewer


euroiplayer

Recommended Posts

Hello,
I need help editing a code for a Source Code Viewer-php based.  By Source Code Viewer, I don't mean viewing the source code after the server renders it, I mean viewing it as in 'raw' (viewing source code of .php, .asp, .cgi...all files)

Here are two sites that provide the code, but I can't get either one to clearly work on my website.
I need help editing them to fully work on my website, please -- this stuff is amazing!

[b]Source Code Viewer into action:[/b]
1. [url=http://www.dew-code.com/source/]http://www.dew-code.com/source/[/url]
2. [url=http://www.randomchaos.com/source/index.php]http://www.randomchaos.com/source/index.php[/url]

[b]Source Code Viewer's source code:[/b]
1. [url=http://www.dew-code.com/source/?filename=./source_viewer.1.2.php]http://www.dew-code.com/source/?filename=./source_viewer.1.2.php[/url]
2. [url=http://www.randomchaos.com/source/?source=http%3A%2F%2Fwww.randomchaos.com%2Fsource%2Findex.php]http://www.randomchaos.com/source/?source=http%3A%2F%2Fwww.randomchaos.com%2Fsource%2Findex.php[/url]


/// What I would like the Source Code Viewer (php) to do is list all files within all directories (view source code), not just those beneath of where the file would be located. (Hope you know what I mean..?)


Thanks
Link to comment
Share on other sites

I have made a script, it is very simple. Not sure if you want something so simple. But here is it anyway.
(no highlighting)

index.php
[code]<?php

if (isset($_POST["submit"]))
{
if ($_POST["filename"] == "")
{
echo "Please enter a file name";
}
else
{
if (file_exists($_POST["filename"]) == false)
{
echo "This file does not exist";
}
else {

$filename = $_POST["filename"];
echo "<form action=\"editfile.php\" method=\"post\">
<textarea name=\"content\" cols=\"100\" rows=\"30\">". file_get_contents($filename) ."</textarea>
<input name=\"location\" type=\"hidden\" value=\"". $filename ."\" /><br><br>
<input name=\"submit\" type=\"submit\" value=\"Make Changes to the file\"/>
</form>";
}
}

}
else
{
echo "<form action=\"". $_SERVER["PHP_SELF"]. "\" method=\"post\">
Enter the file location you want to edit:<input name=\"filename\" type=\"text\" /><br /><br />
<input name=\"submit\" type=\"submit\" value=\"Click here to edit\" />
</form>";
}

?>[/code]

editfile.php
[code]<?php

if (isset($_POST["content"]))
{
//the following will be processed if the content feild has something set to it
$content = $_POST["content"];
$file = $_POST["location"];
$open = fopen($file, "w+");
$write = fwrite($open,$content);

$write;
//checks if the file has been edited:
if ($write == true)
{
echo "File editted sucessfully!";
}
//If the file has had a problem being written to, the follwing is run:
else
{
echo "There was a problem, please try again. Problems may include that you did not make any changes to the file.";
}

fclose($open);
}

//the following will be processed if the content feild does NOT have anyting set to it
else
{
echo "No file has been selected to edit.";
}
?>[/code]

Let me know if this works for you.
Link to comment
Share on other sites

Thanks devnerds and alpine.  Yes, devnerds your script did work, as well as yours alpine, but thats not what I am exactly looking for.

Devners, but your code wasn't able to view/edit source code from other directories, but it was able only those that were beneath it beneath it.

These two things I'd like the script to do:
1. View all the files from all directories, not just the ones beneath of where this (source code viewer files) is located at.
2. Then if you click on one of the links it provide the source code (not necessarily edit it and save it, just view it.)

( Just like at http://www.dew-code.com/source/ )
Link to comment
Share on other sites

I've taken the SCV (Source Code Viewer) from http://www.randomchaos.com/

Code: http://www.randomchaos.com/source/?source=http%3A%2F%2Fwww.randomchaos.com%2Fsource%2Findex.php

I took the code and tried to edit it as much as I can and make it work, but I am getting a couple of errors.

Eorrs Im getting:
[quote]Notice: Undefined index: source in /www/1111mb.com/e/u/r/euroiplaye/htdocs/source.php on line 114

Notice: Undefined index: source in /www/1111mb.com/e/u/r/euroiplaye/htdocs/source.php on line 114

Notice: Undefined index: format in /www/1111mb.com/e/u/r/euroiplaye/htdocs/source.php on line 123

Fatal error: Call to undefined function: getheader() in /www/1111mb.com/e/u/r/euroiplaye/htdocs/source.php on line 180[/quote]

Here is the code:
[quote] <?php   

    // Source
    // View source on randomchaos.com
    //
    // Copyright 2002-2005 Scott Reynen
    //
    // This program is free software; you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation; either version 2 of the License, or
    // (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details:
    // <http://www.gnu.org/licenses/gpl.txt>

    $root = '/www/1111mb.com/e/u/r/euroiplaye/htdocs/';

   
    $color_to_class = array(
        '#ff8000' => 'comment' ,
        '#dd0000' => 'quote' ,
        '#0000bb' => 'variable' ,
        '#007700' => 'keyword'
    );
   
    function from_replace( $matches )
    {
   
        global $database_base;
       
        return preg_replace(
            '#`([^`]*)`#' ,
            '`<a href="' . $database_base . '\1/">\1</a>`' ,
            $matches[0]
        );
   
    } // from_callback
   
    function lowercase_color( $matches )
    {
   
        global $color_to_class;
       
        $class = $color_to_class[ strtolower( $matches[1] ) ];
       
        if ( $class != '' ) return 'class="' . $class . '"';
   
        return 'style="color: ' . strtolower( $matches[1] ) . '"';
   
    } // lowercase_color
   
    /**
    * The following function adapted from:
    * http://us3.php.net/manual/en/function.highlight-string.php#56642
    */
   
    function xhtml_highlight( $str )
    {
   
        $hlt = highlight_string( stripslashes( $str ) , true );
        $fon = str_replace(
            array( '<font ' , '</font>' ) ,
            array( '<span ' , '</span>' ) ,
            $hlt
        );
        return preg_replace_callback(
            '#style="color: (.*?)"#' , 'lowercase_color' , $fon
        );
       
    } // xhtml_highlight
   
    $disallow = '/(accounts.|disemployed|ownsociety|bloomington-normal|expeditious|boobtubecube|fastr)/';
   
    $files['url'] = array();
    $skipDir = array(
        '/www/1111mb.com/e/u/r/euroiplaye/htdocs/66' ,
    );

    function getFiles( $dir ) {
   
        global $files , $skipDir;
             
        error_reporting( E_ERROR | E_PARSE );
       
        if (
            ( ! in_array( $dir , $skipDir ) ) &&
            ( $handle = opendir( $dir ) )
        )
        {
                 
            while ( false !== ( $file = readdir( $handle ) ) )
            {
           
                if ( preg_match( "/.(php|js|inc|css)$/" , $file ) )
                    $files['url'][] = $dir . '/' . $file;
               
                if (
                    ($file != '.' && $file != '..' ) &&
                    ( is_dir( $dir . '/' . $file ) )
                )
                    getFiles( $dir.'/'.$file );
                       
            } // while
           
            closedir( $handle );
               
        } // if
           
    } // getFiles
   
    $source = ( $_GET['source']  != '' ) ? $_GET['source'] : $_POST['source'];
   
    /**
    * Remove any query string
    */
   
    $source_parts = explode( '?' , $source );
    $source = $source_parts[0];
   
    if ( $_GET['format'] == 'txt' )
    {
       
        if ( $source == '' )
            header( 'Location: ' . $THIS_URL );
                   
        /**
        * Get requested file   
        */
       
        $fileName = '../' . preg_replace(
            array(
                '#http://euroiplaye.1111mb.com/#' ,
            ) , array( '' , '\1/' ) , $source
        );
       
        /**
        * Check for disallowed files
        */
       
        if (
            preg_match( $disallow , $fileName ) ||
            ( substr( $source , 0 , 7 ) != 'http://' )
        ) header( 'Location: ' . $THIS_URL );
   
        if ( substr( $fileName , -1 , 1 ) == '/' )
            $fileName.= 'index.php';
           
        if (! $source_file = @file( $fileName ) )
            header( 'Location: ' . $THIS_URL );
   
        $file_name_parts = explode( '/' , $fileName );
        $simple_file_name = $file_name_parts[ count( $file_name_parts ) - 1 ];
   
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . $simple_file_name );
        header("Pragma: no-cache");
        header("Expires: 0");
        echo( implode( '' , $source_file ) );
        die();
   
    } // if
    else
    {
   
?>
<?= '<?xml version="1.0" encoding="utf-8"?>' ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>TITLE</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="new_randomchaos.css" type="text/css" />
    </head>
    <body>
        <div id="container">
            <?= getHeader() ?>
            <div id="content">
<?php
   
    if ( $source != '' ) {
               
        /**
        * Show requested file   
        */
       
        $fileName = '../' . preg_replace(
            array(
                '#http://euroiplaye.1111mb.com/#'
            ) , array( '' , '\1/' ) , $source
        );
                       
?>
                <h2>Source for '<?= htmlentities( $source ) ?>':</h2>
<?php
       
        /**
        * Check for disallowed files
        */
       
        if (
            preg_match( $disallow , $fileName ) ||
            ( substr( $source , 0 , 7 ) != 'http://' )
        ) {
       
?>
                <p class="message">Sorry, the source you requested is hidden for security.</p>
<?php

        } // if
        else
        {
     
           
            if ( substr( $fileName , -1 , 1 ) == '/' )
                $fileName.= 'index.php';
           
            if (! $source_file = @file( $fileName ) )
            {
           
?>
                <p class="message">Sorry, the file you requested could not be opened.</p>
<?php
               
            } // if
            else {
                     
                $highlighted_source = xhtml_highlight( implode( '' , $source_file ) , TRUE );
                                   
                /**
                * Link references to other files or database tables
                */
           
                $link_base = 'http://euroiplaye.1111mb.com/?source=';
                $link_base.= urlencode( 'http://euroiplaye.1111mb.com/' );
               
                $include_regex = '#(<span[^>]*>s*$roots*</span>';
                $include_regex.= '<span[^>]*>s*.s*</span><span[^>]*>';
                $include_regex.= '[\'|"])([^\'"]*)([\'|"])#';
               
                $database_base = 'http://euroiplaye.1111mb.com/';
               
                $sql_regex = '#((?:UPDATE|DELETE FROM|INSERT INTO)s*`)([^`]*)(`)#';

                $regex = array(
                    $include_regex ,
                    $sql_regex
                );
               
                $replace = array(
                    '\1<a href="' . $link_base . '\2">\2</a>\3' ,
                    '\1<a href="' . $database_base . '\2/">\2</a>\3'
                );
               
                $linked_source = preg_replace(
                    $regex , $replace , $highlighted_source
                );
                       
                $from_regex = '#FROM.*?WHERE#';
               
                $linked_source = preg_replace_callback(
                    $from_regex , 'from_replace' , $linked_source
                );
                       
?>
            <p><a href="<?= $THIS_URL ?>?source=<?= urlencode( $source ) ?>&format=txt">Download</a></p>
            <p>See room for improvement? Please <a href="#comments">leave a comment</a> with any suggested changes. Any changes I use will be credited in the source code.</p>
            <div>
                <?= $linked_source ?>
            </div>
<?

                /**
                * If URL doesn't have record in database, add it
                */
               
                $clean_source = preg_replace( '#index.php$#' , '' , $source );
               
                $url_sql = 'SELECT `id` FROM `url`
                WHERE `url` = "' . $clean_source . '"';
               
                $url_result = mysql_query( $url_sql , DB );
               
                if ( mysql_num_rows( $url_result ) == 0 )
                {
               
                    $url_insert_sql = 'INSERT INTO `url`
                    ( `url` ) VALUES ( "' . $clean_source . '" )';
               
                    mysql_query( $url_insert_sql , DB );
                   
                    $url_id = mysql_insert_id( DB );
               
                } // if
                else
                {
               
                    $url = mysql_fetch_object( $url_result );
                    $url_id = $url->id;
               
                } // else
               
                /**
                * If URL record isn't referenced by source, add reference
                */
               
                $reference_sql = 'SELECT `id` FROM `reference`
                WHERE `toid` = ' . $url_id . '
                AND `totype` = "url"
                AND `fromid` = 1
                AND `fromtype` = "url"';
               
                $reference_result = mysql_query( $reference_sql , DB )
                    or die( mysql_error() );
               
                if ( mysql_num_rows( $reference_result ) == 0 )
                {
               
                    $reference_insert_sql = 'INSERT INTO `reference`
                    ( `toid` , `totype` , `fromid` , `fromtype` )
                    VALUES
                    ( ' . $url_id . ' , "url" , 1 , "url" )';
               
                    mysql_query( $reference_insert_sql , DB );
                   
                    $reference_id = mysql_insert_id( DB );
                                   
                } // if
                else
                {
               
                    $reference = mysql_fetch_object( $reference_result );
                    $reference_id = $reference->id;
               
                } // else
                               
                /**
                * Point comments to source reference of URL
                */
               
                $reference_sql = 'SELECT * FROM `reference`
                WHERE `id` = ' . $reference_id;
               
                $reference_result = mysql_query( $reference_sql , DB );

                $reference = new reference( mysql_fetch_object( $reference_result ) );

                $reference->html_display_comments();

            } // else             

        } // else

       
    } // if
    else
    {

        /**
        * Show file list
        */
   
?>
                <p>The following is a list of all available source code on randomchaos.com. At the bottom of every page is a link to that page's source code. This list includes all such pages as well as javascript and other external source code. If you have any questions or comments on the code, please contact me.</p>
                <ul>
<?php
       
        $path = realpath( '../' );
               
        getFiles( $path );
       
        while (list( $key , $val ) = each( $files['url'] ) )
        {

            $files['url'][$key] = preg_replace(
                array(
                    '#/www/1111mb.com/e/u/r/euroiplaye/htdocs/svhp#'
                ) ,
                array(   
                    'http://weblog.randomchaos.com/'
                ) , $val
            );

            if ( ! preg_match( $disallow , $files['url'][$key] ) )
            {

?>
                    <li><a href="<?= $THIS_URL ?>?source=<?= urlencode($files['url'][$key]) ?>"><?= $files['url'][$key] ?></a></li>
<?php
           
            } // if
               
        } // while
       
?>
                </ul>
<?php

    } // else

?>
            </div>
            <?= getFooter() ?>
        </div>
    </body>
</html>
<?

    } // if

?> [/quote]


Here is the test site:
http://euroiplaye.1111mb.com/source.php
http://euroiplaye.1111mb.com/source.txt


Anybody could fix these few bugs and make the code work?

Thanks
Link to comment
Share on other sites

Without being rude:

[quote author=euroiplayer link=topic=117242.msg478835#msg478835 date=1165279997]

These two things I'd like the script to do:
1. View all the files from all directories, not just the ones beneath of where this (source code viewer files) is located at.
2. Then if you click on one of the links it provide the source code (not necessarily edit it and save it, just view it.)

( Just like at http://www.dew-code.com/source/ )
[/quote]

The above quote looks like a wish-list to me. That belongs in the Freelance section: http://www.phpfreaks.com/forums/index.php/board,8.0.html

And the latest code you want help on is a third party script and should according to forum guidelines be posted in the 3'rd Party PHP Scripts section: http://www.phpfreaks.com/forums/index.php/board,34.0.html

Someone (including myself at times) can of course provide the topic starter with a working code or preferrably just a snippet to help on getting one step further. As i did here.

That was the answers.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.