Location: PHPKode > scripts > YAMC > yamc/class.YAMC.php
<?php
/**
 * @author Dick Munroe <hide@address.com>
 * @copyright copyright @ 2005 by Dick Munroe, Cottage Software Works, Inc.
 * @license http://www.csworks.com/publications/ModifiedNetBSD.html
 * @version 1.0.1
 * @package YAMC
 */

//
// Yet Another Mailto Crypter
//
// Keeps mailto tags from being harvested by robots.
//
// Edit History:
//
//  Dick Munroe hide@address.com 22-Oct-2004
//      Initial Version Created
//
//  Dick Munroe hide@address.com 13-Nov-2004
//      Allow subject and body to be specified along with the
//      address.
//
//  Dick Munroe hide@address.com 06-Dec-2005
//      Keep things from being translated multiple time.
//
//  Dick Munroe hide@address.com 20-Mar-2006
//      Allow mailToString to be called as a static member function.
//
//  Dick Munroe (hide@address.com) 11-Jun-2006
//      PHP 5 testing caught an unprotected $this in mailToString.
//
//  Dick Munroe (hide@address.com) 13-Dec-2006
//      Generated javascript function should pass validation.
//

class YAMC
{
    var $m_at = " at " ;
    var $m_dot = " dot " ;
    var $m_additionalAttributes = "" ;

    //
    // Constructor:
    //
    //  Takes three parameters:
    //
    //      Replacement string for the @ character (defaults to " at ")
    //      Replacement string for the "." character (defaults to " dot ")
    //      Any additional HTML attributes to be added by default to all
    //      links generated by the mailto function.
    //

    function YAMC($at = " at ", $dot = " dot ", $additional = "")
    {
        $this->m_at = $at ;
        $this->m_dot = $dot ;
        $this->m_additionalAttributes = $additional ;
    }

    //
    // Generate a mailto link.
    //
    //  Takes three parameters:
    //
    //      Email address or associative array contaning the following:
    //                array (to = "hide@address.com",
    //                       subject = "Subject of email",
    //                       body = "Body of email")
    //            to must appear in the array, subject and body are both optional.
    //
    //      The text for the link.  Default is the email address after @ and . substitution.
    //
    //      additional HTML attributes to be added to this link.  If omitted, the additional
    //      attributes as provided to the constructor are used.
    //

    function mailto($address, $link = null, $additional = null)
    {
        print $this->mailtoString($address, $link, $additional) ;
    }

    /**
     * This member function can be called as a static member function and will return
     * the mailto URL for the specified address.  The "@" and "." replacements are
     * " at " and " dot " by default.
     *
     * @desc return the mailto string, properly munged and set up to invoke the
     *       javascript function to unmung it.
     * @param mixed $address The email address.
     * @param string [optional] The link displayed, by default the email address.
     * @param string [optional] $additional additional parameters for the mailto.
     * @param string [optional] $theAt the "@" replacement if called as a static function.
     * @param string [optional] $theDot the "." replacement if called as a static function.
     */

    function &mailtoString($address, $link = null, $additional = null, $theAt = NULL, $theDot = NULL)
    {
        $isObjectOriented = (isset($this) && is_subclass_of($this, 'yamc')) ;

        if ($theAt === NULL)
        {
            if ($isObjectOriented)
            {
                $theAt =& $this->m_at ;
            }
            else
            {
                $theAt = ' at ' ;
            }
        }

        if ($theDot === NULL)
        {
            if ($isObjectOriented)
            {
                $theDot =& $this->m_dot ;
            }
            else
            {
                $theDot = ' dot ' ;
            }
        }

        $theArgs = func_get_args() ;
        if (is_array($theArgs[0]))
        {
            $theArgs = $theArgs[0] ;
        }
        else
        {
            $theArgs = array('to' => $theArgs[0]) ;
        }

        $theMungedAddress = preg_replace("/@/", $theAt, $theArgs['to'], 1) ;
        $theMungedAddress = preg_replace("/\./", $theDot, $theMungedAddress) ;

        if (empty($link))
        {
            $link = $theMungedAddress ;
        }

        if ($additional == null)
        {
            if ($isObjectOriented)
            {
                $additional = $this->m_additionalAttributes ;
            }
        }

        $theLink = "<a onClick=\"YAMC(this)\" href=\"" . rawurlencode($theMungedAddress) . "\" " . $additional ;
        if (!empty($theArgs['subject']))
        {
            $theLink .= ' subject="' . rawurlencode($theArgs['subject']) . '"' ;
        }

        if (!empty($theArgs['body']))
        {
            $theLink .= ' body="' . rawurlencode($theArgs['body']) . '"' ;
        }

        $theLink .= ">" . $link . "</a>" ;

        return $theLink ;
    }

    /**
     * This member function can be called as a static function and will
     * default to setting the translation to the same as the default for
     * the object oriented case.
     *
     * @desc emit the javascript necessary to make all this work.
     * @param string [optional] $theAt the replacement for the "@" symbol.
     * @param string [optional] $theDot the replacement for the "." symbol.
     * @return string by reference Javascript to be emitted
     */

    function &javascript($thePrintFlag = TRUE, $theAt = NULL, $theDot = NULL)
    {
        $isObjectOriented = (isset($this) && is_subclass_of($this, 'yamc')) ;

        if ($theAt === NULL)
        {
            if ($isObjectOriented)
            {
                $theAt =& $this->m_at ;
            }
            else
            {
                $theAt = ' at ' ;
            }
        }

        if ($theDot === NULL)
        {
            if ($isObjectOriented)
            {
                $theDot =& $this->m_dot ;
            }
            else
            {
                $theDot = ' dot ' ;
            }
        }

        $js = sprintf('
<script language="JavaScript" type="text/javascript">
<!--
function YAMC(what)
{
    if (what.translated == undefined)
    {
            what.translated = 1 ;
    }
    else
    {
            return true ;
    } ;

    var at = /%s/ ;
    var dot = /%s/g ;
    var theAddress = what.href ;
    var theBody = what.getAttribute("body") ;
    var theEmail = "" ;
    var theSubject = what.getAttribute("subject") ;

    theAddress = unescape(theAddress) ;
    theAddress = theAddress.replace(/.*\//, "mailto:") ;
    theAddress = theAddress.replace(at, "@") ;
    theAddress = theAddress.replace(dot, ".") ;

    if (theBody != undefined)
    {
        theEmail = theEmail + "body=" + theBody + "&" ;
    }

    if (theSubject != undefined)
    {
        theEmail = theEmail + "subject=" + theSubject + "&" ;
    }

    if (theEmail != "")
    {
        theAddress = theAddress + "?" + theEmail.substr(0, theEmail.length - 1) ;
    }

    what.href = theAddress ;

    return true ;
}
-->
</script>
',             $theAt,
               $theDot) ;

        if ($thePrintFlag)
        {
            echo $js ;
        }

        return $js ;
    }
}
?>
Return current item: YAMC