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