#! /usr/bin/perl -wT
#
######################
#
# notepad.pl
# v0.10
# Michael K. Neylon
# mneylon-pm@masemware.com
#
# Creates a 'notes-to-self' page, which you can write notes to yoursel
+f,
# and also delete those notes after you no longer need them. Ideal us
+e
# would be for one with multiple connect points to the internet (work
+&
# home), and with own private server that you want to drop notes onto.
+
# Note that there is NO security built into this module; this should b
+e
# done at the server level.
#
# Requires nothing beyond a standard perl install (uses CGI.pm).
#
# Configuration options:
# $notes_dir : Location of directory where data should be stored fro
+m
# the script. This *should* be absolute unless you kno
+w
# for sure what directory your scripts start up in.
#
# Suggestions/Ideas/Comments please email me at mneylon-pm@masemware.c
+om
#
######################
use strict;
use CGI;
#### CONFIGURATION ####
# $notes_dir : Location of directory where data should be stored fro
+m
# the script. This *should* be absolute unless you kno
+w
# for sure what directory your scripts start up in.
my $notes_dir = '/home/services/www/notes/data';
#### END OF CONFIGURATION ####
# Typical starting point
my $cgi = new CGI;
print $cgi->header();
print $cgi->start_html( -title=>'Personal Notemaker' );
print $cgi->h1( { -align=>'center'}, 'Personal Notemaker' );
# Since this page is pretty much a single form...
print $cgi->start_form;
###
# Process the changes from the last screen
###
# First, if a new note has been made, write it out.
my $note = $cgi->param( 'new_note' ) || '';
$note =~ s/^\s*//;
$note =~ s/\s*$//;
if ( $note ne '' ) {
my $time = time();
# note that in case we suddently go to 64 bit time, 20 decimal place
+s
# should be enough to take care of it. But as long as the filename
# is right justified and zero padded, the script won't care.
open ( FILE, ">$notes_dir/" . sprintf( "%020d", $time ) . '.txt' ) o
+r
die "Can't write out new note : $!";
print FILE $note;
close FILE;
$cgi->delete( 'new_note' ); # So that it doesn't show up again...
}
# Now handle any notes for deletion - must be checked to be removed
foreach my $param ( $cgi->param ) {
if ( ( $cgi->param( $param ) eq 'on' ) && ( $param =~ /^delete_(.*)$
+/ ) ) {
my $file = $1;
if ( -e "$notes_dir/$file.txt" ) {
unlink "$notes_dir/$file.txt" or die "Can't delete note file : $
+!";
}
}
}
###
# Present a slot for a new note
###
print $cgi->div( { -align=>'center' },
$cgi->p( $cgi->b( "New note" ) ),
$cgi->textarea( -name => 'new_note',
-rows => 4,
-columns => 80) );
print $cgi->hr;
###
# Write existing notes to output
###
# First open and grab list of files...
opendir( DIR, $notes_dir ) or die "Can't open notes directory : $!";
my @files = readdir( DIR ) or die "Can't read notes directory : $!";
closedir( DIR );
# readdir sucks in . and .., so strip it via grep
@files = grep { /^.*\.txt$/ } @files;
# Since filenames are stored via unix time, it's simply a matter of so
+rting
# them now, in ascending date order (though these are still strings, s
+o cmp)
@files = sort { $a cmp $b } @files;
# Now we've got a sorted list of files that contain our notes. Presen
+t them.
# Note that to avoid memory taxing, we'll won't try to get fancy with
+the
# table output.
# But of course, if no output, let's not do this...
if ( scalar @files == 0 ) {
print $cgi->p( { -align=>'center'}, 'You have no notes waiting.' );
} else {
print $cgi->start_table( { -width => '100%' } );
print $cgi->Tr( $cgi->th( ['Delete', 'Message', 'Time' ] ) );
foreach my $file ( @files ) {
# time data is set in the file name.
my ($time) = ( $file =~ /^(.*)\.txt/ );
my $ltime = localtime( $time );
# Open the file and read in data
open( FILE, "<$notes_dir/$file" )
or die "Can't open a note file $file : $!";
my @lines = <FILE>;
close( FILE );
my $text = join('', @lines);
# Print the table row.
print $cgi->Tr(
$cgi->td( { -align=>'center' },
$cgi->checkbox( -name => "delete_$time",
-label => '' ) ),
$cgi->td( { -width=>'80%' }, $text ),
$cgi->td( $cgi->small( $ltime ) )
);
}
print $cgi->end_table;
}
# Submit button
print $cgi->p( { -align => 'center' },
$cgi->submit ) ;
# Clean up
print $cgi->end_form;
print $cgi->end_html;
1;
|