?
Solved

View source of perl, anyway? Via browser

Posted on 2007-10-02
63
Medium Priority
?
660 Views
Last Modified: 2012-05-05
I am wondering if there is a way to view the source of a perl script via the browser? I did it with PHP to make it easier to show my code to others.

domain.com/view.cgi?url=XXX.cgi

Anything like that?

Thanks so much,

Ryan
0
Comment
  • 27
  • 18
  • 12
  • +3
63 Comments
 
LVL 4

Expert Comment

by:dharmanerd
ID: 20004575
hmm, don't know. you can change the mime type to plain text, but that would disable the perl scripts.
0
 
LVL 6

Expert Comment

by:nitinsawhney
ID: 20004607

Its not possible to view the source of executed cgi file residing under cgi-bin.

Even if you change the mime-type it would still be treated as cgi file and would be parsed by the server.

You could achieve what you wan't using another cgi-script. This script would take the file to be read as parameter and read its content, will then display the whole contents of the file as output.

Don't forget to HTML encode the content of the files you would like to show.

Hope this helps.

Cheers!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20005878
Hey, thanks all.

Yeah I am trying to do something like this:


You could achieve what you wan't using another cgi-script. This script would take the file to be read as parameter and read its content, will then display the whole contents of the file as output.

Any idea how to?

Thanks for everything.

Ryan
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20006302
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI ':standard';

print header(-type=>"text/plain");
my $file=param('url');
my $webroot='/path/to/webroot';

system("cat $webroot$file");
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007076
Thanks for that, I put that in a CGI file and from there what do I do with it? use the URL param the address bar?

Then what do I change the root too?

RYan
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20007191
The $webroot should be the root for DocumentRoot (as apache calls it, other webservers might call it something different).  It is the root path http requests.  Such as, if a request comes in "www.server.com", the server will return the file in "/path/to/webroot/index.html", then "/path/to/webroot" should be what you put in $webroot.

Put this in a directory that is allowed to execute perl code.  This is usually cgi-bin, but could be anywhere depending on your server configuration.

Then to view the source to "/cgi-bin/somefile.cgi", you could get "www.server.com/cgi-bin/viewsource.pl?url=/cgi-bin/somefile.cgi"
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007268
Alrighty, thank you, do you know if there is an easy way to figure that out? So I should be saving i as *.pl and not *.cgi

Thanks!

Ryan
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20007302
Rather than allowing the user to enter the path to any script they wish, I'd limit it to only the ones that I want them to see.  I'd use a form that presented them with a selection of filenames (without path info) which my script would lookup in a hash.

This doesn't include error handling, but gives a rough draft.

#!/usr/bin/perl

use warnings;
use strict;
use CGI;

my $cgi = CGI->new;
my $script = $cgi->param('script');

my %cgi_bin = ( 'adam314.pl' => '/var/www/cgi-bin/adam314.pl' );

open my $file, '<', $cgi_bin{$script} or die $!;
my $script_contents = join '<br>', <$file>;
close $file;

print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $script"),
      $cgi->cite($script_contents),
      $cgi->end_html;
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007305
And can think work on different servers other than my own? I am trying to work with a client of mine who is on a different one than mine.

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007334
I just got that second form, I would change what you have, with my root correct?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007375
I have:


#!/usr/bin/perl

use warnings;
use strict;
use CGI;

my $cgi = CGI->new;
my $script = $cgi->param('script');

my %cgi_bin = ( 'view.cgi' => '/public_html/cgi-bin/view.cgi' );

open my $file, '<', $cgi_bin{$script} or die $!;
my $script_contents = join '<br>', <$file>;
close $file;

print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $script"),
      $cgi->cite($script_contents),
      $cgi->end_html;

And I get a blank screen.

Ryan
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20007411
You'd want to use the full system path which yours is would be something like this:

my %cgi_bin = ( 'view.cgi' => '/home/my_username/public_html/cgi-bin/view.cgi' );
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20007449
The script I posed does have many security implications.  It will allow a user to view any file on your server (even those not within your webroot, if they use something like /../../../etc/passwd).

I based it on your original post, where someone enters the url "domain.com/view.cgi?url=XXX.cgi"

The script FishMonger provides much additional security, but with the drawback of having to define which files will be visible.  
To use that script, you would call it like:   domain.com/view.cgi?script=view.cgi



0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20007468
While testing/debugging, you might want to redirect the fatal errors to the browser.

Add this:

use CGI::Carp 'fatalsToBrowser';
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007476
Alrighty, I am un sure of what it is and its my schools server, is there a way to figure out what it is? A site thats finds it our what not?

http://elan.champlain.edu/

Thanks,

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007485
On my FTP I see public_html, and I cant go up anymore.

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20007569
I have this:

http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view-form.pl

I added in that line of code and still nothing.

Any way of finding out the path with a script or a link?

Thanks for your help,

Ryan
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20007627
I prefer to use the full system path in scripts like this, but relative paths are also fine.  Since you don't know your full system path, you might want to use the relative path.

Assuming that this script and the one you want to view are both in the cgi-bin dir, you can remove the path info.

my %cgi_bin = ( 'view.cgi' => './view.cgi' );
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20007735
If you have ssh or telnet access to the server, you can login and find the full system path with the pwd command.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20007839
You could put this in a script:
    system("pwd");
But there is usually an environment variable, such as SCRIPT_FILENAME or DOCUMENT_ROOT.
To view all env variables:
    foreach (keys %ENV) {
        print "$_=$ENV{$_}  <br />\n";
    }
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008078
Hey all, thank you very much for your help.

With what you said for the ssh access, I do have access using Putty, what command would I run?

And to adams:

#!/usr/bin/perl

    foreach (keys %ENV) {
        print "$_=$ENV{$_}  <br />\n";
    }
   
    system("pwd");

Does that seem right and what you are talking about?

Is it possible to view remote files doing this?
Thanks,

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008086
Fish:

I have:

#!/usr/bin/perl

use warnings;
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';

my $cgi = CGI->new;
my $script = $cgi->param('script');

my %cgi_bin = ( 'view.cgi' => './view.cgi' );

open my $file, '<', $cgi_bin{$script} or die $!;
my $script_contents = join '<br>', <$file>;
close $file;

print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $script"),
      $cgi->cite($script_contents),
      $cgi->end_html;

That and still I get a blank output, link:

http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view-form.cgi
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20008299
The code I gave wasn't a complete script.  Here it is as a complete script.  It will not show you the content of a file, but would be used to determine the absolute path for your script.

#!/usr/bin/perl
use CGI ':standard';
print header(-type=>'text/plain');
foreach (keys %ENV) {
   print "$_=$ENV{$_}  <br />\n";
}
system("pwd");
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008316
Alrighty, thank you for that!

Link to that: http://elan.champlain.edu/~rcoughlin32001/cgi-bin/test.cgi

My updated code for the viewer is:

#!/usr/bin/perl

use warnings;
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';

my $cgi = CGI->new;
my $script = $cgi->param('script');

my %cgi_bin = ( 'view.cgi' => './home/web32001fall07/rcoughlin/public_html/cgi-bin/' );

open my $file, '<', $cgi_bin{$script} or die $!;
my $script_contents = join '<br>', <$file>;
close $file;

print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $script"),
      $cgi->cite($script_contents),
      $cgi->end_html;

And I still get a blank output, but I do see the "Here's the contents of the"

Thanks,

Ryan
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20008348
Change it to this:

my %cgi_bin = ( 'view.cgi' => './home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi' );
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008392
I updated that and I have:

#!/usr/bin/perl

use warnings;
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';

my $cgi = CGI->new;
my $script = $cgi->param('script');

my %cgi_bin = ( 'view.cgi' => './home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi' );

open my $file, '<', $cgi_bin{$script} or die $!;
my $script_contents = join '<br>', <$file>;
close $file;

print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $script"),
      $cgi->cite($script_contents),
      $cgi->end_html;

Link: http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view-form.cgi
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20008394
Oops, I missed the leading 'dot' which makes the path relative instead of absolute.

If this doesn't work, then there's something screwy going on with the sever setup.

my %cgi_bin = ( 'view.cgi' => '/home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi' );
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008466
Hmm..I have:

my %cgi_bin = ( 'view.cgi' => '/home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi' );


And it doesnt show up. Even know the path with that file shows up as:

HTTP_HOST=elan.champlain.edu  <br />
/home/web32001fall07/rcoughlin/public_html/cgi-bin

See how it says "rcoughlin" and the link says "rcoughlin32001" do I have to do something special?

http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view-form.cgi
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20008491
You're getting closer.  This is the error that I now receive.

Software error:

No such file or directory at view-form.cgi line 13.

For help, please send mail to the webmaster (root@elan.champlain.edu), giving this error message and the time and date of the error.

------

Do you have a view.cgi file in the cgi-bin directory?

Let's adjust the die statement a little:
open my $file, '<', $cgi_bin{$script} or die "$script: $cgi_bin{$script} <$!>;
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008555
Yeah, I do have a file called view

http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view.cgi

Wont show anything but there is code there.

New code:

#!/usr/bin/perl      
                     
use warnings;        
use strict;          
use CGI;            
use CGI::Carp 'fatalsToBrowser';
                     
my $cgi = CGI->new;  
my $script = $cgi->param('script');
                   
my %cgi_bin = ( 'view.cgi' => '/home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi' );
                     
open my $file, '<', $cgi_bin{$script} or die "$script: $cgi_bin{$script} <$!>;
my $script_contents = join '<br>', <$file>;

Now we get some output:

http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view-form.cgi
close $file;        
                     
print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $script"),
      $cgi->cite($script_contents),
      $cgi->end_html;
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20008573
Another oops...I left out the ending quote.

open my $file, '<', $cgi_bin{$script} or die "script: $cgi_bin{$script} <$!>";
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008610
Alrighty, the pleasures of coding...haha.

New Code:

#!/usr/bin/perl      
                     
use warnings;        
use strict;          
use CGI;            
use CGI::Carp 'fatalsToBrowser';
                     
my $cgi = CGI->new;  
my $script = $cgi->param('script');
                   
my %cgi_bin = ( 'view.cgi' => '/home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi' );
                     
open my $file, '<', $cgi_bin{$script} or die "script: $cgi_bin{$script} <$!>";
my $script_contents = join '<br>', <$file>;
close $file;        
                     
print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $script"),
      $cgi->cite($script_contents),
      $cgi->end_html;

Link: http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view-form.cgi

There is a view.cgi in the cgi-bin and am not sure why it is not working. Maybe run it in Putty? Or will that not do anything?

Also, See how the URL has my name and at the end has "32001" and the "~" at the start.

Then the root as it as:

/home/web32001fall07/rcoughlin/public_html/cgi-bin

With no "32001" and "~"

Ryan
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20008708
Looks like they're doing url rewriting.
http://en.wikipedia.org/wiki/Rewrite_engine
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

I'm glad to see that we were able to help, but it looks like we may have done a good portion of your homework which we're normally not supposed to do.  :o)
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008739
Yeah, I didnt think of them doing that, using the rewrite mod. I have this also:

http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view.cgi?url=test.cgi

If you go there you will see it outputs, just wanted to try this method that was suggested on this post.

We can do groups for this and want to be able to use this to send back and fourth over AIM to work on files each other.

We did this for PHP instead of saving them to *.phps to view them as text.

Anyway to do a remote URL or just move backward to view our groups folder, so instead of my name, it would be under flast32001, etc.

Thank you so much,

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20008802
Wow, my bad! Haha, I didnt see this line:

my $script = $cgi->param('script');

For the param, that would make sense to use script=XX

Ryan
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20022490
Is there anyway to run this with remote files? Or set it up where you can use "../../"

Ryan
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 1000 total points
ID: 20022968
The first script I gave would allow that - it would allow anyone (who could run the script) to view any file on the server (or a mounted drive).  This makes it less secure.

The script FishMonger gave requires that you define all possible files before hand.  If someone requests a file which you haven't specified, the script will die.  You can list whatever files you want, and give them whatever name you want.  You do that with this line in the script:
    my %cgi_bin = ( 'view.cgi' => '/home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi' );
The key is the name that the user would request.  The value is the absolute path to the file.  So if you wanted somone to request "../../file.txt", you would use:
    my %cgi_bin = (
        'view.cgi' => '/home/web32001fall07/rcoughlin/public_html/cgi-bin/view.cgi',
        '../../file.txt' => '/home/web32001fall07/rcoughlin/public_html/file.txt');
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20023064
Alrighty, thank you for your reply.

This is the script I have from you:

#!/usr/bin/perl -w
use strict;
use warnings;
use CGI ':standard';

print header(-type=>"text/plain");
my $file=param('url');
my $webroot='/home/web32001fall07/rcoughlin/public_html/cgi-bin/';

system("cat $webroot$file");

URL: http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view.cgi?url=view.cgi

Say where:

my $webroot='/home/web32001fall07/rcoughlin/public_html/cgi-bin/';

I would just change the path to whatever, etc. Correct? I see how your's is compared to the other one from Fish.

Thank you very much, let me give this a shot!

Ryan



0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20023169
Using my approach you don't really need to hard code each file in the hash.  You could read the directory (or directories) and build the hash dynamically.  Also, since the hash values hold the full path, I wouldn't include any path info in the hash key as Adam's example shows.

If you want to serve up a file that's in "../../"
I'd do the hash entry like this:

'file.txt' => '/home/web32001fall07/rcoughlin/file.txt'
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20023196
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20023218
Alrighty, thank you!

CODE:

#!/usr/bin/perl -w
use strict;
use warnings;
use CGI ':standard';

print header(-type=>"text/plain");
my $file=param('url');
my $webroot='/home/web32001fall07/rcoughlin/public_html/cgi-bin/';

system("cat $webroot$file");

Does your code above create the hash dynamically depending on the folder?

And so there is no way to use the URL? Since that is kind of hard to do right, allowing access

Starting to all come together learning Perl wise, been playing with it alot lately.

Ryan
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20023334
You REALLY REALLY need to disable or delete that view.cgi script.

Here's one example why:
http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view.cgi?url=../../.*

I'm not going to test this against your files, but you might have a problem if someone passes something like this:

'files;rm -r ../../*'
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20023561
Currently my suggested approach doesn't build the hash dynamically.  Before we go that route, you'll want to come up with a plan on which files you want to give access and which ones you don't.  Personally, I'd put files that I want to allow access into a single directory.  If you have files that are in various other directories that you can't or don't want to move to a central location, you can create a link (hard or symbolic) in your central directory.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20024050
Here's a modified version that builds the hash dynamically.

#!/usr/bin/perl

use warnings;
use strict;
use CGI;
use File::Find;
use CGI::Carp 'fatalsToBrowser';

my $cgi = CGI->new;
my $script = $cgi->param('script');
my $src_dir = '/home/web32001fall07/rcoughlin/public_html/cgi-bin/';
my %cgi_bin;

find(\&scripts, $src_dir);

open my $file, '<', $cgi_bin{$script} or die "script: $cgi_bin{$script} <$!>";
my $script_contents = join '', <$file>;
close $file;

print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $cgi_bin{$script}"),
      $cgi->pre($script_contents),
      $cgi->end_html;

sub scripts {

 # This sub would/should be expanded to include
 # tests that filter and return only the desired files
 # as it is, it finds all files in and under the source directory

   next if -d;

   $cgi_bin{$_} = $File::Find::name;

}
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20024258
If you have two files with the same name in different directories, that will only allow one to be viewed.  That may not be a problem in this case though.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20028601
Hey guys, thank you for that. I like how that creates it dynamically. To get in other folders on that server, is it possible to use ../../?

And no go on using a remote URL?

Thanks again for this!

Ryan
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20036108
Instead of using ../.. you'd pass an array of directories to the find command.

For example:

my @src_dirs = ('/home/web32001fall07/rcoughlin/public_html/cgi-bin/',  '/src/perl/scripts/');

find(\&scripts, @src_dirs);

....
....

If you want to use a url to retrieve files from a remote server, that server will need to be running the same (or similar) script and you'd use LWP to access/retrieve that url.  Once you've retrieved it, you'll probably need to do some additional processing/filtering on it before sending its contents to the user.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20036208
If you really want to have the script allow anyone to view the contents of any file (again, this is not very secure), then a script like what I originally posted would be simplier.  To avoid the bug FishMonger pointed out with my first script (passing "files; rm ...."), it would be safer to not use system to display the file, but to open it with perl directly.
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20036265
"I am wondering if there is a way to view the source of a perl script via the browser?"

Make a copy of your Perl script with a .txt extension, and provide an HTML link to it.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20040404
Thanks for that, but I was thinking of a more dynamic way, using Perl and using parameters.

Ryan
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20042463
Do you want to allow anyone to view the source for any file?  Or do you want to be able to specify which files a user is allowed to view?
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20043029
You want to keep in mind that allowing anyone to view any file on the system they want is a really bad idea and should never be done.  Do you really want to allow anyone to view the passwd file?  Also, since this is being done on a college network, you could get into serious trouble opening this type of security hole.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20043300
I agree that allowing anyone to view any file could potentially be a problem... but the permissions of the OS will still apply.  So you will be able to see any file that the webserver has access to.

But you need to decide whether you want to provide a list of allowable files (the list can be auto generated from a particular directory or some other method), or not.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20043333
Yeah, I am going to be careful with it and not always keep it up, just use it group wise when sharing files.

What do you think the best method is to work with viewing other directories, say if i took my partners name:

my $src_dir = '/home/web32001fall07/dgordon/public_html/cgi-bin/';

And mine is:

my $src_dir = '/home/web32001fall07/rcoughlin/public_html/cgi-bin/';

Is there a way to also do his?

RYan
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20043540
Using the program FishMonger gave, you would just add your partners directory to the list passed to find
my @src_dirs =
  ('/home/web32001fall07/rcoughlin/public_html/cgi-bin/',
   '/home/web32001fall07/dgordon/public_html/cgi-bin/');
find(\&scripts, @src_dirs);

With the script I gave, you would just ask for the file you wanted:
http://elan.champlain.edu/~rcoughlin32001/cgi-bin/view.cgi?url=/home/web32001fall07/dgordon/public_html/cgi-bin/filename.cgi


0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20044581
Once you've populated FishMonger's hash, you can make an array from the keys:

    my @file_list = sort keys %cgi_bin;
   
And use this array to populate a drop-down menu to select the file to view:

    print $cgi->popup_menu(-name => "source_files", -values => \@file_list);
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20050049
Adam, so your script would allow you to use the whole path?

mjcoyne, so I take the code from fish and do I add those lines? Or do I replace them?

CODE:

#!/usr/bin/perl

use warnings;
use strict;
use CGI;
use File::Find;
use CGI::Carp 'fatalsToBrowser';

my $cgi = CGI->new;
my $script = $cgi->param('script');
my $src_dir = '/home/web32001fall07/rcoughlin/public_html/cgi-bin/';
my %cgi_bin;

find(\&scripts, $src_dir);

open my $file, '<', $cgi_bin{$script} or die "script: $cgi_bin{$script} <$!>";
my $script_contents = join '', <$file>;
close $file;

print $cgi->header, $cgi->start_html,
      $cgi->h1("Here's the contents of $cgi_bin{$script}"),
      $cgi->pre($script_contents),
      $cgi->end_html;

sub scripts {

 # This sub would/should be expanded to include
 # tests that filter and return only the desired files
 # as it is, it finds all files in and under the source directory

   next if -d;

   $cgi_bin{$_} = $File::Find::name;

}
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 1000 total points
ID: 20050620
mjcoyne's suggestion would be added, but there's also additional supporting code that would need to be added.

#!/usr/bin/perl

use warnings;
use strict;
use CGI;
use File::Find;
use CGI::Carp 'fatalsToBrowser';
use Data::Dumper;

my $cgi = CGI->new;
my $myself = $cgi->url;
my $script = $cgi->param('script');
my %cgi_bin;
my @src_dirs = ('/home/web32001fall07/rcoughlin/public_html/cgi-bin/'); # add additional dirs as needed

find(\&scripts, @src_dirs);
my @file_list = sort keys %cgi_bin;

print $cgi->header, $cgi->start_html("Source Code Viewer");

if ( $script ) {

   open my $file, '<', $cgi_bin{$script} or die "script: $cgi_bin{$script} <$!>";

   my $script_contents = join '', <$file>;
   close $file;

# we need to escape the code so that it displays as plain text
   $script_contents = $cgi->escapeHTML($script_contents);

   print $cgi->a({href=>$myself}, "Go back to form."),
         $cgi->h2("Here is the contents of $cgi_bin{$script}"),
         $cgi->pre($script_contents),
         $cgi->end_html;
}
else {
   print $cgi->p("Please select the file that you want to view"),
         $cgi->start_form(-method=>'GET'),
         $cgi->popup_menu(-name => "script", -values => \@file_list),
         $cgi->submit,
         $cgi->end_form,
         $cgi->end_html;
}

sub scripts {

 # This sub would/should be expanded to include
 # tests that filter and retourn only the desired files

   next if -d;
 #  next if /^\./;
   $cgi_bin{$_} = $File::Find::name;
}
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20050664
Nice, I like that method. Thank you for that. Gets the contents of a directory, prints in a drop down, with the value of the filename, then BOOM prints it out.

0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 20123715
Glad you liked and incorporated my idea.  Would have been even gladder if you'd thrown some points my way...
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20123729
That was my bad, I skipped over that, I am sorry man! You deserve them too, let me post to reopen.

Ryan
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question