<?php
/// dot-mailto.php --- Hide your email address in a .mailto file.

// Copyright (C) 2005 Aaron Hawley <ashawley at gnu uvm edu>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.

// $Id: dot-mailto.php,v 1.1 2005-12-08 20:34:00-05 ashawley Exp $

// Commentary:

// Place your email address in a .mailto file in the current
// directory, and make a link to this script, or directoly to its form
// with an HTML input button with the value of "submit".

class Dot_MailTo {
    var
$filename = ".mailto";
    var
$button_phrase = "Contact by mail";

    function
fileExists() {
        return
file_exists($this->filename);
    }

    function
getHtmlButton() {
        if (!
$this->fileExists()) {
            return
"<p>Mailto file does not exist.</p>\n";
        }
        
$html = array();
        
$html[] = "<form method=\"post\"";
        
$html[] = "action=\"{$_SERVER['PHP_SELF']}\">";
        
$html[] = "<input type=\"submit\" id=\"submit\"";
        
$html[] = "style=\"font-size: 2em; background-color: #fff;\"";
        
$html[] = "name=\"submit\" value=\"{$this->button_phrase}\"/>";
        
$html[] = "</form>";
        return
join("\n", $html);
    }

    function
getMailTo()
    {
        if (!
$this->fileExists()) {
            
header("HTTP/1.0 404 Not Found");
            return;
        }
        
$mailto = $this->getEmailAddress();
    
//    echo("Location: mailto:$mailto");
        
header("Location: mailto:$mailto");
    }

    function
getEmailAddress()
    {
        
$lines = file($this->filename);
        
$email = preg_replace('/[^a-z0-9@.]/', '', $lines[0]);
        return
$email;
    }

    function
isSubmit()
    {
        return isset(
$_POST['submit']) && $_POST['submit'] !== '';
    }

    function
main()
    {
        if (!
$this->isSubmit()) {
            return
$this->getHtmlButton();
        } else {
            
$this->getMailTo();
            exit();
        }
    }
}
// end class Dot_MailTo

$dot_mailto = new Dot_MailTo;
$html = $dot_mailto->main();

?>
<html>
<title>Click button to send a message</title>

<h1>Click button to send a message</h1>

<?php
if ($html)
    print
$html;
?>

</html>