Skip to content

Instantly share code, notes, and snippets.

@chrisyour
Created August 25, 2009 23:48
Show Gist options
  • Save chrisyour/175142 to your computer and use it in GitHub Desktop.
Save chrisyour/175142 to your computer and use it in GitHub Desktop.
// CakePHP Enkoder
// USAGE:
// Uses the same syntax as $html->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle)
<?=$enkode->mailto('Email Me', 'user@example.com', array('subject'=>'Hello world!'))?>
// HELPER:
// app/views/helpers/enkode.php
<?
# Plugin: CakePHPEnkoder
# Author: Chris Your
# Author URI: http://ignitionindustries.com
# Derivative work of: PHPEnkoder by Michael Greenberg
# Derivative work of: Hivelogic Enkoder by Dan Benjamin
# LICENSE (Modified BSD)
# Copyright (c) 2009, Ignition Industries Inc.
# Derivative work of the PHPEnkoder, Michael Greenberg.
# Derivative work of the Hivelogic Enkoder, Dan Benjamin.
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
# 3. Neither the name of Chris Your, Ignition Industries Inc.
# nor the names of its contributors may be used to endorse or
# promote products derived from this software without specific
# prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/**
* EnkodeHelper
*/
class EnkodeHelper extends AppHelper{
var $helpers = array('Html');
/**
* Enkode variables
*
* @var number JS_LEN
* @var string enk_dec_reverse
* @var string enk_dec_num
* @var string enk_dec_swap
*
*/
var $JS_LEN = 269;
var $enk_dec_reverse = "kode=kode.split('').reverse().join('')";
var $enk_dec_num = "kode=kode.split(' ');x='';for(i=0;i<kode.length;i++){x+=String.fromCharCode(parseInt(kode[i])-3)}kode=x";
var $enk_dec_swap = "x='';for(i=0;i<(kode.length-1);i+=2){x+=kode.charAt(i+1)+kode.charAt(i)}kode=x+(i<kode.length?kode.charAt(kode.length-1):'')";
/**
* mailto
*
* @param string $text
* @param string $email
* @param array $htmlAttributes
* @param boolean $escapeTitle
* @return javascript enkoded mailto link
* @author Chris Your
*/
function mailto($title, $email, $htmlAttributes=null, $confirmMessage=null, $escapeTitle=false){
# Build enkodings array
$this->enkodings = array(
array('enk_enc_reverse', $this->enk_dec_reverse),
array('enk_enc_num', $this->enk_dec_num),
array('enk_enc_swap', $this->enk_dec_swap)
);
# Build url
$url = 'mailto:' . $email;
if(isset($htmlAttributes['subject'])){
$url .= '?subject=' . $htmlAttributes['subject'];
unset($htmlAttributes['subject']);
}
# Generate link using the Html helper
$content = $this->Html->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle);
# Return the enkoded mailto link
return $this->output($this->enkode($content));
}
/**
* Private Methods
*
* enkode()
* enkode_pass()
* enk_build_js()
* enk_enc_reverse()
* enk_enc_num()
* enk_enc_swap()
*
*/
private function enkode($content, $text = NULL, $max_passes = 20, $max_length = 1024) {
# Our base case -- we'll eventually evaluate this code
$kode = "document.write(\"" . addslashes($content) . "\");";
$max_length = max($max_length, strlen($kode) + $this->JS_LEN + 1);
$result = "";
# Build up as many encodings as possible
for ($passes = 0;
$passes < $max_passes &amp;&amp; strlen($kode) < $max_length;
$passes++) {
# Pick a random encoding
$idx = rand(0, count($this->enkodings) - 1);
$enc = $this->enkodings[$idx][0];
$dec = $this->enkodings[$idx][1];
$kode = $this->enkode_pass($kode, $enc, $dec);
}
# Mandatory numerical encoding, prevents catching @ signs and
# interpreting neighboring characters as e-mail addresses
$kode = $this->enkode_pass($kode, 'enk_enc_num', $this->enk_dec_num);
# Return the enkoded JavaScript
return $this->enk_build_js($kode, $text);
}
private function enkode_pass($kode, $enc, $dec) {
# First encode
$kode = addslashes($this->$enc($kode));
# Then generate encoded code with decoding
$kode = "kode=\"$kode\";$dec;";
return $kode;
}
private function enk_build_js($kode, $text = NULL) {
$clean = addslashes($kode);
$msg = $text;
$span = "enkoder_" . rand();
$js = "<span id=\"$span\">$msg</span><script type=\"text/javascript\">
/* <!-- */
function hivelogic_enkoder() {
var kode=\"$clean\";var i,c,x;while(eval(kode));
}
hivelogic_enkoder();
var span = document.getElementById('$span');
span.parentNode.removeChild(span);
/* --> */
</script>";
return $js;
}
private function enk_enc_reverse($s) {
return strrev($s);
}
private function enk_enc_num($s) {
$nums = "";
$len = strlen($s);
for ($i = 0;$i < $len;$i++) {
$nums .= strval(ord($s[$i]) + 3);
if ($i < $len - 1) { $nums .= ' '; }
}
return $nums;
}
private function enk_enc_swap($s) {
$swapped = strval($s);
$len = strlen($s);
for ($i = 0;$i < $len - 1;$i += 2) {
$tmp = $swapped[$i + 1];
$swapped[$i + 1] = $swapped[$i];
$swapped[$i] = $tmp;
}
return $swapped;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment