http://www.perlmonks.org?node_id=72711
Category: Web Stuff
Author/Contact Info Michael K. Neylon (mneylon-pm@masemware.com)
Description: A notes-to-self CGI script, useful for those (like me) that have multiple locations where they work, and want a quick, low-overhead way to leave notes to themselves on a central server (eg no DBI). Note that the script has no security checks, but this can easily be done at web server level.
#! /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;
Replies are listed 'Best First'.
Re: Notepad.pl
by chromatic (Archbishop) on Apr 20, 2001 at 09:17 UTC
    I've been playing with a personal Wiki for this sort of thing, so there's now one included in Jellybean.

    It requires a few other modules to use, but it offers some very compelling features... this is a great idea, and it's probably something that someone somewhere will exploit to make lots of money and kick off some killer new technology.

Re: Notepad.pl
by Anonymous Monk on Apr 13, 2004 at 05:06 UTC
    I like this script so much that i took a step further to add in these features:
    (1) AllowHTML becomes an option (0:HTML stripped, 1:enabled, 2:displayed as source;)
    (2) AutoHotlink;
    (3) Edit existing notes;
    (4) password required for adding/editing/deleting notes.

    :-) Tarty (http://198.64.185.46/)

Re: Notepad.pl
by Anonymous Monk on Jan 19, 2004 at 01:38 UTC
    Does anyone know why I can do everything but delete?