File: subscriptions.cgi

package info (click to toggle)
libcgi-session-perl 4.48-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 616 kB
  • sloc: perl: 1,920; makefile: 5
file content (461 lines) | stat: -r-xr-xr-x 11,743 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/perl -w

# $Id$

use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';
use URI::Escape;
use vars qw($SELF_URL);

use lib '/home/sherzodr/perllib';

# Check for some non-standard Perl modules
my @required = qw(MIME::Lite HTML::Template CGI::Session);
for my $mod ( @required ) {
    eval "require $mod";
    if ( $@ ) {
        print "Content-Type: text/html\n\n";
        print "$mod is required. If it's installed in a non-standard path, " .
                "please 'use lib' line in $0";
        exit(0);
    }
}

my $cgi     = CGI->new();
my $session = CGI::Session->load() or die CGI::Session->errstr;

if ( $session->is_expired ) {
    print $session->header();
    print "Your session expired, inevitably!";
    exit(0);
} elsif ( $session->is_empty ) {
    $session = $session->new();
}

$session->expire("+30s");

my $cmd     = $cgi->param('cmd') || $session->param("last_cmd") || 'directions';

$SELF_URL = $cgi->url() || $0;


# save the last executed command:
$session->param(last_cmd => $cmd);

if ( $cmd eq "directions" ) {
    print directions($cgi, $session);

} elsif ( $cmd eq 'step1' ) {
    print step1($cgi, $session);

} elsif ( $cmd eq 'step2' ) {
    print step2($cgi, $session);

} elsif ( $cmd eq 'step3' ) {
    print step3($cgi, $session);

} elsif ( $cmd eq 'finish') {
    print finish($cgi, $session);

} elsif ( $cmd eq 'clear' ) {
    print clear($cgi, $session);

} elsif ( $cmd eq "show-dump" ) {
    print show_dump($cgi, $session);

} else {
    print "Error: CMD: $cmd is not valid";

}














#--------------------------------------------------------------------
# functions start here
#--------------------------------------------------------------------
sub directions {
    my ($cgi, $session) = @_;

    my $dirver = ref($session);
    my $version = $session->VERSION();

    my $HTML = <<HTML;
<h2>Welcome to CGI::Session Demo Script</h2>

<div><strong>Driver:</strong> $dirver/$version</div>

<p>
The tricks are endless! This script is to demonstrate basic usage of
CGI::Session in CGI applications.
</p>

<h3>So what is the test all about?</h3>

<p>
Test consists of a single multi-page mailing list subscription form.
First form asks to fill in personal information, the second screen
asks to choose subscriptions you are interested in. The 3rd page
is a confirmation/summary of your subscriptions. Once you click on
"Finish" button, the program sends your subscription details to your
email you provided during subscriptions, and also attaches the source
code of this script.
</p>

<p>
During the process, application provides "Back", "Next" and"Clear Form"
buttons so that you can go back to previous forms to fill in/correct
the details. While going back, notice how the script remembers all the
data you have previously submitted, and presents pre-filled/pre-selected
form elements.
</p>

<p>
While somewhere in the middle of subscription, close the browser
intentionally, and reopen the page: <a href="$SELF_URL">$SELF_URL</a>. Notice how the script remembers which form you were filling out before you closed the browser, and displays
respective form, instead of taking you to the default page. Your previously
filled in form data are also kept.
</p>
<p>
In each page, the script also provides you with "show-dump" link at the bottom
of each screen. You can click on the link to view internal <b>_DATA</b> table
of CGI::Session, and see what kind of information are stored in the object
at each step.
</p>

<p>
Should you have any suggestions or comments, feel free to send me an email:
<a href="mailto:sherzodr\@cpan.org">sherzodr\@cpan.org</a>.
</p>

<form>
<input type="hidden" name="CGISESSID" value="%_session_id%" />
<input type="hidden" name="cmd" value="step1" />
<input type="submit" value="Start The Demo" />
</form>
HTML
    return template(\$HTML, $cgi, $session);
}




sub step1 {
    my ($cgi, $session) = @_;
    my $HTML = <<'HTML';
<h4>Step 1 out of 3</h4>
<p> Hi %name%! Please fill out your personal information below </p>
<form method="post">
    <input type="hidden" name="CGISESSID", value="%_session_id%" />
    <input type="hidden" name="cmd" value="step2" />
    <div>Your full name:</div>
        <input type="text" name="name" value="%name%" size="35" />
    <div>Your email address:</div>
        <input type="text" name="email" value="%email%" size="35" />
    <div>Your website URL:</div>
        <input type="text" name="website" value="%website%" size="35" />
    <br />
    <input type="button" value="Cancel" onClick="clearTheForm(this.form)" />
    <input type="submit" value="Next &gt;&gt;" />
</form>
HTML

    return template(\$HTML, $cgi, $session);
}



sub step2 {
    my ($cgi, $session) = @_;

    $session->save_param($cgi);
    $session->load_param($cgi, ["subscriptions"]);

    my $HTML = <<HTML;
<h4>Step 2 out of 3</h4>
<p>Dear %name%.</p>
<p>
    Following are available newspaper subscriptions we offer.<br />
    Choose the subscriptions you are interested in, and click "Next &gt;&gt;"<br />button.  To select more than one subscription, press and hold [CTRL] key while selecting.<br />
    Should you wish to update your profile, click "&lt;&lt;Back" button.<br />

</p>

<form method="post">
<input type="hidden" name="CGISESSID" value="%_session_id%" />
<input type="hidden" name="cmd" value="step3" />
<div>Subscriptions:</div>
    %subscriptions_scrolling%
<br />
<input type="button" value="&lt;&lt;Back" onClick="location='$SELF_URL?cmd=step1;CGISESSID=%_session_id%'"/>
<input type="button" value="Cancel" onClick="clearTheForm(this.form)" />
<input type="submit" value="Next &gt;&gt;" />
</form>
HTML
    return template(\$HTML, $cgi, $session);
}



sub step3 {
    my ($cgi, $session) = @_;

    $session->save_param($cgi, ["subscriptions"]);
    $session->load_param($cgi, ["subscriptions"]);

    my $HTML = <<HTML;
<h4>Step 3 out of 3 - final!</h4>
<p>Dear %name%.</p>
<p>
Before you finalize your subscription, you need to review the following<br />
information you have submitted. Update if necessary. <br />
When you are down, click on "Finish" button. Voila!
</p>

<form method="post">
<input type="hidden" name="CGISESSID" value="%_session_id%" />
<input type="hidden" name="cmd" value="finish" />


<div><b>Your Profile</b> [<a href="%edit_profile%">edit</a>]</div>

<table border="0" cellpadding="2" cellspacing="0"
  frame="void" rules="none" summary="">
    <tr>
        <td>Name:</td>
        <td>%name%</td>
    </tr>
    <tr>
        <td>Email address:</td>
        <td><a href="mailto:%email%">%email%</a></td>
    </tr>
    <tr>
        <td>Your website:</td>
        <td><a href="%website%">%website%</a></td>
    </tr>
</table>

<br />

<div><b>Your Subscriptions</b> [<a href="%edit_subs%">edit</a>]</div>

%subscriptions_checkbox%

<input type="button" value="&lt;&lt;Back" onClick="location='$SELF_URL?cmd=step2;CGISESSID=%_session_id%'" />
    <input type="button" value="Update" onClick="updateTheForm(this.form)" />
    <input type="button" value="Cancel" onClick="clearTheForm(this.form)" />
    <input type="submit" value="Finish" />
</form>
HTML
    return template(\$HTML, $cgi, $session);
}




sub finish {
    my ($cgi, $session) = @_;

    my $to = sprintf("%s <%s>", $session->param('name'),
                                $session->param('email'));
    my $msg = MIME::Lite->new(
        From        => 'Sherzod Ruzmetov <sherzodr@cpan.org>',
        To          => $to,
        Subject     => 'CGI-Session Demo',
        Type        => 'multipart/mixed'
    );

    $msg->attach(   Type => 'text/plain',
                    Data => _data($cgi, $session));
    $msg->attach(   Type => 'application/octet-stream',
                    Path => $0,
                    Filename => 'session.cgi'
    );


    open(SENDMAIL, "|/usr/sbin/sendmail -t -oi") or die $!;
    $msg->print(\*SENDMAIL);
    close(SENDMAIL);

    $session->clear();
    return $cgi->redirect(-uri=>$ENV{SCRIPT_NAME});

}



sub _data {
    my ($cgi, $session) = @_;

    my $HTML = <<'HTML';
Thank you, %name%, for trying out our CGI::Session Demo Application.
Here are the information you submitted to the script. Notice that
we're also attaching the source code of the script to this email
together with the session object dump at the time this email was
being sent.

+-------------------------
| Personal Info:
+-------------------------
    name:       %name%
    email:      %email%
    website:    %website%

+-------------------------
| Subscriptions:
+-------------------------
%subscriptions_plain%

+-------------------------
| Session Object Dump:
+-------------------------
    %dump%


Regards,

Sherzod Ruzmetov <sherzodr@cpan.org>
HTML

    return template(\$HTML, $cgi, $session, 1);

}



sub template {
    my ($HTML, $cgi, $session, $no_html) = @_;
    my $t = HTML::Template->new( scalarref=>$HTML,
                                vanguard_compatibility_mode=>1,
                                associate => [$session, $cgi] );

    my @papers = (
        "The Perl Journal",
        "The SysAdmin Magazine",
        "The Coolest CGI::Session tricks mailing list",
        "XML.com weekly news and updates",
        "Perl5porters mailing list",
    );

    my $sid = $session->id();

    $t->param(
        edit_profile  => "$SELF_URL?cmd=step1;CGISESSID=$sid",
        edit_subs     => "$SELF_URL?cmd=step2;CGISESSID=$sid",
        subscriptions_scrolling => $cgi->scrolling_list(
                            -name=>'subscriptions',
                            -values => \@papers,
                            -size => 5,
                            -multiple => 1),

        subscriptions_checkbox => scalar($cgi->checkbox_group(
                            -name=>'subscriptions',
                            -values => \@papers, -linebreak=>1)),
        'dump'            => $session->dump(undef, 1),
    );

    if ( $no_html ) {
        return $t->output();
    }

    my $cookie = $cgi->cookie(-name=>CGI::Session->name(), -value=>$sid, -expires=>"+10h");

    $HTML = $cgi->header(-cookie=>$cookie) .
        $cgi->start_html(-title=>"CGI::Session Test Script", -script=>{code=>_js()},
        -style => {code=>_css()} ) .
        $t->output();

    unless ( $cgi->param("cmd") ) {
        $cgi->param(cmd => 'directions');
    }

    my $dump_url = sprintf("%s?cmd=show-dump;CGISESSID=%s;ref=%s",
            $SELF_URL, $sid, uri_escape($cgi->self_url()));

    if ( $session->param("_display_dump") ) {
        $HTML .= $cgi->a({-href=>$dump_url}, "hide-dump");
        $HTML .= $cgi->pre($session->dump(undef, 1));
    } else {
        $HTML .= $cgi->a({-href=>$dump_url}, "show-dump");
    }

    $HTML .= $cgi->end_html();

    return $HTML;
}


sub show_dump {
    my ($cgi, $session) = @_;

    if ( $session->param("_display_dump") ) {
        $session->clear(["_display_dump"]);

    } else {
        $session->param(_display_dump => 1);
    }

    return $cgi->redirect(-uri=>$cgi->param('ref') );
}



sub clear {
    my ($cgi, $session) = @_;
    $session->clear([$session->param()]);

    return $cgi->redirect(-uri=>$ENV{SCRIPT_NAME});
}



sub _js {
    return <<'JS';
function clearTheForm(obj) {
    if ( confirm("If you cancel the form, all your previously submitted data will be lost. Are you sure you want to continue?") ) {
        obj["cmd"].value = "clear";
        obj.submit();
        return true;
    }
    return false;
}
function updateTheForm(obj) {
    obj["cmd"].value = "step3";
    obj.submit();
    return true;
}
JS
}




sub _css {
    return <<'CSS';
Body {
    background-color:White;
    margin:         70px;
}

P {
    width:          600px;
    font-family:    Verdana, Arial, Sans-serif;
    font-size:      13px;
}

CSS
}


# $Id$