1: #!c:/perl/bin/perl -w
2:
3: ######################################################################
4: # Thanks for trying out NotePod! #
5: # What is NotePod? It's a small notepad used for notes and memos. #
6: # It's a cheap little script I threw together in an hour. #
7: # Under what license is this? None! Do *whatever* you want with it! #
8: # Modify it, throw it out, call it names, sell it, whatever. #
9: # I will not be held responsable for lost data, etc, etc. #
10: # Non-copyright 2002 by Nathan B. (you're not getting my last name!) #
11: ######################################################################
12:
13: use strict;
14:
15: my $version = "1.0";
16:
17: $|++; # output buffering
18:
19: # Load modules
20: use CGI::Carp qw(fatalsToBrowser);
21: use CGI;
22: use IO::Scalar;
23: use Data::Dumper;
24: use Fcntl ':flock';
25:
26: # Declare variables
27: use vars qw( $q %input %db $stdout $OUT $id );
28:
29:
30: # Get data input
31: $q = new CGI;
32: $input{$_} = $q->param($_) for $q->param();
33:
34:
35: # Load DB
36: open LOCK, ">>lock.lck" or die "Could not open the lock file: $!\n";
37: flock LOCK, LOCK_EX or die "Could not flock the lock file: $!\n";
38: %db = ();
39: do "db.dat";
40:
41:
42: # Print everything to a variable instead of STDOUT
43: $OUT = new IO::Scalar \$stdout;
44: select $OUT;
45:
46:
47: # Branch off depending on action
48: &main if !$input{'action'}; # Main Page
49: &main("Changes Discarded.") if $input{'action'} eq "Cancel"; # Cancel Save
50: &new_note if $input{'action'} eq "new"; # New Note
51: &save_note if $input{'action'} eq "Save Note"; # Save Note
52: &edit_note if $input{'action'} eq "edit"; # Edit Note
53: &del if $input{'action'} eq "Delete Note"; # Delete Note
54: &del(1) if $input{'action'} eq "delall"; # Delete All
55:
56:
57: # Print HTTP/HTML Headers
58: sub print_it {
59:
60: select STDOUT; # Print it all to STDOUT
61:
62: # Save DB
63: $Data::Dumper::Purity = 1; $Data::Dumper::Indent = 0;
64: open DATA, ">db.dat" or die "COuld not open database: $!\n";
65: flock DATA, LOCK_EX or die "Could not flock databse: $!\n";
66: print DATA Data::Dumper->Dump([\%db], ['*db']);
67: close DATA;
68: close LOCK;
69:
70: print qq|Content-type: text/html\n
71: <html>
72: <head>
73: <title>NotePod: $_[0]</title>
74: </head>
75:
76: <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" alink="#0000FF" vlink="#0000FF">
77:
78: <table style="width: 500px; border: solid #AAAAAA 2px; margin-top: 10px; margin-bottom: 10px;">
79: <tr>
80: <td style="height: 25px; text-align: center; border: solid #999999 2px; background-color: #BBBBBB; color: #000000;">
81: <strong>$_[1]</strong>
82: </td>
83: </tr>
84:
85: <tr>
86: <td valign="top" style="background-color: #DDDDDD;">
87: <form action="$ENV{'SCRIPT_NAME'}" method="post">
88: <table border="0" cellpadding="3" style="width: 100%; margin-top: 10px;">
89: $stdout
90: </table>
91: </form>
92: </td>
93: </tr>
94: </table>
95: |;
96: exit;
97:
98: }
99:
100:
101: # Main Screen
102: sub main {
103: $_[0] = "NotePod version $version" if !$_[0];
104: print qq|
105: <tr><td style="border: solid #BBBBBB 2px;">$_[0]</td><td colspan="2" align="right" style="border: solid #BBBBBB 2px;"><a href="$ENV{'SCRIPT_NAME'}?action=new">New Note</a> \| <a href="$ENV{'SCRIPT_NAME'}?action=delall">Delete All</a></td></tr>
106: <tr><th align="left">Title</th><th align="left">Length</th><th align="left">Last Modified</th></tr>
107: |;
108:
109: foreach my $id (keys %db) {
110: next if $id eq "num";
111: my $title = $db{$id}[0]; # Note Title
112: my $len = length($db{$id}[1]); # Length of Note
113: my $mod = $db{$id}[2]; # Last Modified Date
114:
115: print qq|<tr><td><a href="$ENV{'SCRIPT_NAME'}?action=edit&id=$id">$title</a></td><td>$len characters</td><td>$mod</td></tr>|;
116: }
117:
118: &print_it('Displaying Notes', 'Current Notes');
119:
120: }
121:
122:
123: # New Note
124: sub new_note {
125: $_[0] = "* Creating New Note." if !$_[0];
126: print qq|
127: <tr><td colspan="2" style="border: solid #BBBBBB 2px;">$_[0]<input type="hidden" name="id" value="$input{'id'}"></td></tr>
128:
129: <tr><th align="left">Title:</th><td><input type="text" name="title" value="$input{'title'}" style="width: 100%"></td></tr>
130: <tr><th align="left" valign="top">Contents:</th><td><textarea name="contents" rows="7" cols="48">$input{'contents'}</textarea></td></tr>
131: <tr><th colspan="2"><input type="submit" name="action" value="Save Note"> <input type="submit" name="action" value="Cancel"></th></tr>
132: |;
133:
134: &print_it('New Note', 'Create New Note');
135: }
136:
137:
138: sub save_note {
139: &new_note('* You need to enter a title and contents.') if (!$input{'title'} || !$input{'contents'});
140:
141: if ($input{'id'}) {
142: $db{$input{'id'}} = [$input{'title'}, $input{'contents'}, my $a=localtime];
143: } else {
144: $db{int(++$db{'num'})} = [$input{'title'}, $input{'contents'}, my $a=localtime];
145: }
146:
147: &main("Note Saved");
148: }
149:
150: sub edit_note {
151: print qq|
152: <tr><td colspan="2" style="border: solid #BBBBBB 2px;">* You are editting an existing note.<input type="hidden" name="id" value="$input{'id'}"></td></tr>
153: <tr><th align="left">Title:</th><td><input type="text" name="title" value="$db{$input{'id'}}[0]" style="width: 100%"></td></tr>
154: <tr><th align="left" valign="top">Contents:</th><td><textarea name="contents" rows="7" cols="48">$db{$input{'id'}}[1]</textarea></td></tr>
155: <tr><th colspan="2"><input type="submit" name="action" value="Save Note"> <input type="submit" name="action" value="Cancel"> <input type="submit" name="action" value="Delete Note"></th></tr>
156: |;
157: &print_it('New Note', 'Create New Note');
158: }
159:
160: # Delete Note(s)
161: sub del {
162: if ($_[0] == 1) {
163: %db = ();
164: &main("All Notes Deleted!");
165: } else {
166: delete $db{$input{'id'}};
167: &main("Note Deleted.");
168: }
169: }