#!/usr/bin/perl -w
#----------------------------------------------------------------------
# create_link.pl - this prompts for a URL and creates a clickable link
# written by Eric Pence
#----------------------------------------------------------------------
use strict;

use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);

# Define global variables
use vars qw( $error_msg $myurl $new_link );

BEGIN {
# Set up error log file
my $log_file = "error_log.txt";
use CGI::Carp qw(carpout);
open(LOG, ">>$log_file")
or die "Unable to append to error log: $!";
carpout(*LOG);
}
use FileHandle;

my $q = CGI->new();

$new_link = '';
$error_msg = '';
$myurl = '';

# Process the input
if ($q->param( 'submitted' )) {
$new_link = '';
$myurl = $q->param( 'myurl' );
# Check for nothing entered
if (!$myurl) {
$error_msg = 'Enter a URL.';
}
else {
# Confirm that URL starts with a protocol
if ($myurl !~ /:\/\//) {
$myurl = "http://$myurl";
}
$new_link = "<font size=\"+1\"><a href=\"$myurl\">$myurl</a></font>";
}
}

print $q->header();
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Create link</title>
<link href=\"../../penceland.css\" rel=\"stylesheet\" type=\"text/css\">
<script language=\"JavaScript\" type=\"text/javascript\">
<!--
// Check for message to display
function messageCheck() {
var msg = '$error_msg' || '';
if (msg) {
alert( msg );
}
}
// Clear the URL
function clearURL() {
document.getElementById(\"myurl\").value = '';
}
// -->
</script>
</head>
<body background=\"../../images/myback.gif\" onLoad=\"messageCheck()\">
<center>
<p>
&nbsp;
<p>
<h2>Create link from URL</h2>
<p>&nbsp;</p>
<table width=\"60%\"><tr><td>
This <a href=\"../cgi-bin/print_source.pl?src=create_link.pl\" class=\"green\"><span title=\"Click to view source\"><b>Perl script</b></span></a>
creates a clickable link out of any URL text you enter. It can be used to access the browser's link context menu options for any URL. For instance,
to save a copy of a file from a remote server, create a link of the URL including the filename, click on it with the <i>right</i> mouse button and
select Save Target/Link As...
</td></tr></table>
<p>&nbsp;</p>
<form name=\"entry_form\" action=\"create_link.pl\" method=\"post\">
<input type=\"hidden\" name=\"submitted\" value=\"submitted\">
<table border=\"2\"width=\"650\">
<tr>
<td>
<table bgcolor=\"#cccccc\" cellspacing=\"10\" width=\"644\">
<tr>
<td width=\"15%\" align=\"right\" nowrap>Enter&nbsp;URL:</td>
<td width=\"60%\"><input type=\"text\" name=\"myurl\" id=\"myurl\" size=\"80\" value=\"$myurl\"></td>
<td width=\"13%\"><input type=\"submit\" name=\"sbmt_btn\" value=\"Create link\"></td>
<td width=\"12%\"><input type=\"button\" value=\"Clear\" onClick=\"javascript:clearURL()\"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<p>
&nbsp;
<p>
&nbsp;$new_link&nbsp;
</center>
</body>
</html>";