No public Twitter messages.

Solar e-mail helper that stops spammers

August 19th, 2009 by Ray in Cookbook, PHP/MySQL, Solar | Tags: , , | No Comments

Tonight I created a helper for Solar that encodes mailto href addresses so SPAMers that scrape HTML pages will never get your addresses.

To use this helper, just do the following in your views/layouts:

echo $this->email('someuser@somehost.com', 'Ray');

Here is the helper code:

<?php

/**
 * Generates an encoded mailto href to stop spammers from scrapping email
 * addresses from your web pages.  This code is based on the PHP example
 * from http://rumkin.com/tools/mailto_encoder/
 *
 * @category Whitebox
 * @package Whitebox_View
 * @author Raymond J. Kolbe <rkolbe@gmail.com>
 */
class Whitebox_View_Helper_Email extends Solar_View_Helper {
    /**
     * The encoded email address.
     *
     * @var string
     */
    protected $_encoded_string = '';

    /**
     * The encoded index used to decode $_encoded_string/
     *
     * @var string
     */
    protected $_encoded_indexes = '';

    /**
     * Generates an encoded mailto href to stop spammers from
     * scrapping email addresses off your web pages.  Returns
     * an inline javascript code block that allows web browser
     * to read the mailto href.
     *
     * If javascript is disabled in the web browser, only the
     * link text is shown to the user.
     *
     * @param string $spec The email address (e.g. rkolbe@gmail.com)
     * @param string $text Href
     * @param array $attribs An array of href attributes
     * @return string An inline string of javascript to handle the
     * encoded mailto href
     */
    public function email($spec, $text = null, $attribs = null) {
        // escape the email address
        $spec = $this->_view->escape($spec);

        // build attribs, after dropping any 'href' attrib
        $attribs = (array) $attribs;
        unset($attribs['href']);
        $attribs = $this->_view->attribs($attribs);

        $this->_obfuscate("<a href=\"mailto:$spec\"$attribs>$text</a>");

        $script = $this->_getScript();
        $script .= '<noscript>'.$text.'</noscript>';

        return $script;
    }

    /**
     * Takes a given href and obfuscates it.
     *
     * @param string $link A full mailto href
     * @return void
     */
    protected function _obfuscate($link) {
        $scrambled_chars = str_shuffle($link);
        $this->_encoded_string = $this->_escapeString($scrambled_chars);

        $string_indexes = '';
        for ($i = 0; $i < strlen($link); $i++) {
            $index = strpos($scrambled_chars, substr($link, $i, 1)) + 48;
            $string_indexes .= chr($index);
        }

        $this->_encoded_indexes = $this->_escapeString($string_indexes);
    }

    /**
     * Returns a block of javascript used to decode the mailto href.
     * This only allows web browsers to see the mailto address.
     *
     * @param void
     * @return string An inline block of javascript
     */
    protected function _getScript() {
        return $this->_view->scriptInline('<!--
            chars = "'.$this->_encoded_string.'";
            indexes = "'.$this->_encoded_indexes.'";
            href = "";

            for(j = 0; j < indexes.length; j++){
                href += chars.charAt(indexes.charCodeAt(j) - 48);
            }

            document.write(href);
            // -->');
    }

    /**
     * Prepares (escapes) a string for javascript.
     *
     * @param string $string The string to escape
     * @return string The escaped string
     */
    protected function _escapeString($string) {
        $string = str_replace("\\", "\\\\", $string);
        $string = str_replace("\"", "\\\"", $string);

        return $string;
    }
}

Table view helper it out!

July 25th, 2009 by Ray in Cookbook, PHP/MySQL | Tags: , , | No Comments

Table view helper has been released! Since my last post about writing a helper such as this, a lot has changed. I hope to have API docs up soon. In the meantime you can download the code and check out the examples at its new project page.


Modifying Solar_View_Helper_Form

May 23rd, 2009 by Ray in Cookbook, PHP/MySQL, Solar | Tags: , | No Comments

Solar comes packed with many different view helpers that will make your life easier, such as inserting images, creating links, and generating forms. However, sometimes a generic helper doesn’t cut it for custom behavior in a project. For example, I wanted to change the way form field hints (field descriptions) and error messages were displayed for the registration form at http://fahwebmon.white-box.us. Note that I am using Solar_View_Helper_Form::auto() to generate my form automagically. The only downside to this is that you do not have control over where errors are displayed on a page.

Read the rest of this entry »


BBCode PHP Syntax Highlighting For Lazy People

April 26th, 2009 by Ray in Cookbook, PHP/MySQL | Tags: , | No Comments

While working on one of my little side projects, I came across a problem where I wanted to highlight bbcode syle php tags in my text (e.g. [PHP] echo “code goes here”; [/PHP]).

Being lazy and using regex (I love it!), I came up with the following solution:

// test text w/embedded code
$text = "This is a sample text string with PHP style tags. [PHP] echo 'Hello'; [/PHP]

// and I made sure to make this case insensitive.
$pattern = "/\[PHP\](.*)\[\/PHP\]/Uis";

// the magic!
$matchCount = preg_match_all($pattern, $text, $matches, PREG_SET_ORDER);
if ($matchCount !== false && $matchCount > 0) {
    foreach ($matches as $match) {
        // highlight_string forces us to start with at least the php opening tag
        $highlightedCode = highlight_string('<?php'.$match[1].'?>', true);

        // finally, replace the original non-highlighted text with the highlighted text
        $text = preg_replace($pattern, $highlightedCode, $text, 1);
    }
}

echo $text;

Just FYI, this was a quick one-off and wasn’t intended to scale with other bbcode style tags. Also, in my real code, the [PHP] tags are lowercase. The only reason they are uppercase is because the syntax highlighter in Wordpress isn’t smart enough to distinguish the tags in my code and tags to signify syntax highlighting.


Solar Cli

November 1st, 2008 by Ray in Cookbook, Solar | Tags: , , | 1 Comment

Overview

One of the nice things Solar has to offer is its CLI (Command Line Interface), which can be used to accomplish tasks that would otherwise eat up valuable time, such as creating applications, models, tests, documentation, and unit tests. In this entry, I will be showing you how to use the current (Solar v1.0.0 alpha2) CLI, detailing commands, available options, parameters, and usage examples. So make sure that you have a fresh pot of coffee and let’s get to it!

Read the rest of this entry »


Virtual hosts and multiple Solar projects

September 27th, 2008 by Ray in Cookbook, Solar | Tags: , | No Comments

In the first part of the series, Big Bang with Solar, I showed you how to download and install Solar for a single project. What I didn’t show you is a way to set up Solar for multiple virtual hosts under Apache, which is what I will be showing you today in our example.

For our example we will use the fictional web sites polygon-ex.com and square-ex.com. If these sites actually exist I apologize in advance for using the names without permission…please forgive me ;-)

Read the rest of this entry »


Big Bang with Solar – Part 1

September 25th, 2008 by Ray in Cookbook, Solar | Tags: , | No Comments

So you have read the manual and browsed the Solar wiki. Now you want to get your hands dirty with setting up your own project. Good news, you’ve found the right place. In this series, I plan to show you, step-by-step, how to set up your very own project using Solar.

To make sure we stay on track, here is the scope for part 1 of the series.

  • Download and Install Solar
  • Determine our project guidelines
  • Configure Apache using Virtual Hosts
  • Set up our bootstrap and configuration file
  • Create a “Hello World” example application

Read the rest of this entry »


SolarPHP authentication using MySQL

September 24th, 2008 by Ray in Cookbook, Solar | Tags: , | No Comments

This post is an extension to SolarPHP’s current manual page on User Authentication and is intended for folks that already have a project setup using the SolarPHP framework. What I hope to accomplish here is to show you how to setup user authentication using MySQL and SolarPHP, something the Solar manual currently does not show you how to do. What I do not cover here is setting up the login box for your project. The login box itself comes packaged with the Solar framework and will appear on your project page if you do not change the default layout.

Resources related to this tutorial can be found on the last page.

Read the rest of this entry »