#!/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 );

# 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: $!";
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\">
<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 = '';
// -->
<body background=\"../../images/myback.gif\" onLoad=\"messageCheck()\">
<h2>Create link from URL</h2>
<table width=\"60%\"><tr><td>
This <a href=\"../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...
<form name=\"entry_form\" action=\"create_link.pl\" method=\"post\">
<input type=\"hidden\" name=\"submitted\" value=\"submitted\">
<table border=\"2\"width=\"650\">
<table bgcolor=\"#cccccc\" cellspacing=\"10\" width=\"644\">
<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>