Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

notes.cgi

by tcf03 (Deacon)
on Jun 24, 2005 at 00:31 UTC ( #469576=sourcecode: print w/ replies, xml ) Need Help??

Category: Web Stuff
Author/Contact Info Ted Fiedler<fiedlert@gmail.com>
Description: Simple app for leaving quick notes between myself and some other admins at work.
#!/usr/bin/perl -T 
# notes.cgi
# Ted Fiedler


use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use warnings;
#use Data::Dumper;


$CGI::POST_MAX=1024 * 100;  # max 100K posts
$CGI::DISABLE_UPLOADS = 1;  # no uploads


# Clean up our UNIX environment
# for more infor read perldoc perlsec
$ENV{'PATH'} = '/home/tfiedler/stuff/scripts/web/cgi-bin';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};


# Flush the output buffer
$|++;


# define our vars
my $date = scalar(localtime);
my @text;
my $file="/home/tfiedler/stuff/scripts/web/cgi-bin/notes.tcf";
my $archive="/home/tfiedler/stuff/scripts/web/cgi-bin/archives.tcf";
my $project_name="webtop";


if (param('Archive'))
{
    open (TFILE, "$file") or
          die "unable to open $file for reading: $!\n";
    #( -s "${file}_arc" ) ? open (TFILE2, ">>${file}_arc") or die($!)
    #                     : open (TFILE2, ">${file}_arc")  or die($!);
    open (TFILE2, ">>$archive") or
          die "Unable to open $archive\n";
    print TFILE2 "$_" for (<TFILE>);
    close(TFILE);
    close(TFILE2);
    unlink($file);
}


if ( param('delete') )
{
    unlink($file);
}



if ( ! -e $file )
{
    open (TOUCH, ">$file") or
          die("Unable to create $file: $!\n");
    close (TOUCH);
}


if ( defined (param('NOTES')) and param('NOTES') =~ m/\S/ )
{ 
    @text=param('NOTES');
    open (SERVER_NOTES, ">>$file") or
          die("Unable to open $file: $!\n");
     for $_(@text) 
    {
        # remove HTML tags <> </>
        s/<(?:[^>'"]*|(['"]).*?\1)*>//gs unless param('HTML');
        # print the rest to file...
        print SERVER_NOTES "[<font color=\"#ff0000\" size=\"-1\">$ENV{
+'REMOTE_ADDR'} - $date</font>]\n",
              br, "<font size=\"-1\">$_</font>", br;
    }
    close (SERVER_NOTES);
}


## Start HTML ##
print header;
print start_html(-title=>"$project_name notes", 
                 -BGCOLOR=>"#cccc99" ), "\n";


print "<tt><center>\n";
print h3("project $project_name notes"),"\n";
print "</center>\n";
print hr,"\n";



print "\n\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" wid
+th=\"70%\" align=\"center\">\n\n";
print "<tr align=\"center\" bgcolor=\"#999966\">\n
       <th>notes</th>\n
       </tr>\n";
print br,br;
print Delete_all() if ( param('delete') or param('Archive') or param('
+HTML') ); 
if ( -s $file ) 
{
    print Delete_all();
    open ( FH, $file ) or
         die("unable to open notes: $!\n");
    my @notes=<FH>;
    print "<td>@notes</td>\n"; 

}
print "</table>\n";


print br,br,br;


print "\n\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" wid
+th=\"70%\" align=\"center\">\n\n";
print "<tr align=\"center\" bgcolor=\"#999966\">\n
       <th>change $file </th>\n
       </tr>\n";
print "</table>\n",br;


print start_form;
print "<center>\n";
print textarea('NOTES',"", 15, 80), "\n",br;
print checkbox(-name=> 'HTML',     -value=> 'off'),   "  ",
      checkbox(-name=> 'delete',   -value=> 'off'),   "  ", 
      checkbox(-name=> 'Archive',  -value=> 'off'),   "  ", 
      submit('Submit');
print "</center>\n",br;


if ( -s $archive )
{
    print "\n\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\"
+ width=\"70%\" align=\"center\">\n\n";
    print "<tr align=\"center\" bgcolor=\"#999966\">\n
           <th>Archives</th>\n
           </tr>\n";
    open ( FH2, "$archive" ) or
           die "Unable to open $archive: $!\n";
    my @archives=<FH2>;
    print "<td>@archives</td>\n";
    print "</table>\n";
}


print "\n\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" hei
+ght=\"20\" width=\"70%\" align=\"center\">\n\n";
print "<tr align=\"center\" bgcolor=\"#999966\">\n
       <td> </td>\n
       </tr>\n";
print "</table>\n",br;


print end_html;


__END__

=head1 NAME

notes.cgi - Leave notes for other users, logs IP address & date

=head1 AUTHOR

Ted Fiedler<fiedlert@gmail.com>


Comment on notes.cgi
Download Code
Re: notes.cgi
by aukjan (Friar) on Jun 24, 2005 at 06:12 UTC
    Ever heard of Post-Its ;)...
Re: notes.cgi
by chanio (Priest) on Jun 25, 2005 at 18:17 UTC
    A good cgi example.

    I would put the file directory outside from the server reach, though. Remember that the script can access any directory, but the users shouldn't.

    To make it prettier & smaller, I would use some template to display it. If the template has a .CSS file linked to it, you would be able to change completely the screen's look every time you might need to.

    .{\('v')/}   C H E E R   U P !
     _`(___)' ___a_l_b_e_r_t_o_________
    
    Wherever I lay my KNOPPIX disk, a new FREE LINUX nation could be established.

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://469576]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-07-25 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls