<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>klenwell press &#187; Code Case</title>
	<atom:link href="http://klenwell.com/press/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://klenwell.com/press</link>
	<description>A Developer's Broadsheet</description>
	<lastBuildDate>Fri, 13 Jan 2012 14:51:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Google App Engine Memcache Limits</title>
		<link>http://klenwell.com/press/2011/06/memcache-limit/</link>
		<comments>http://klenwell.com/press/2011/06/memcache-limit/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 21:46:59 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[App Engine]]></category>
		<category><![CDATA[Code Case]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=222</guid>
		<description><![CDATA[Problem If you attempt to store an object more than approximately 1 MB in size using memcache in the Google App Engine, it will give a ValueError, something like this: ValueError: Values may not be more than 1000000 bytes in length; received 1088171 bytes Solution I&#8217;ve added a library to my Appswell framework that allows [...]]]></description>
			<content:encoded><![CDATA[<h4>Problem</h4>
<p>If you attempt to store an object more than approximately 1 MB in size using memcache in the Google App Engine, it will give a ValueError, something like this:</p>
<p><code>ValueError: Values may not be more than 1000000 bytes in length; received 1088171 bytes</code></p>
<h4>Solution</h4>
<p>I&#8217;ve added a library to my Appswell framework that allows you to get around this limit by serializing an object into multiple strings and storing these along with an index object that stores the key.</p>
<p><strong>Usage Example:</strong></p>
<pre class="brush: python">
import multicache as memcache

# cache params
cache_data = some_large_nested_dict
cache_key = &#039;test_multicache&#039;
cache_len = 60

# save data
memcache.set(cache_key, cache_data, cache_len)

# retrieve data
retrieved_data = memcache.get(cache_key)
</pre>
<p>The module can be easily extracted from the framework. See these links for additional details:</p>
<p>source code: <a href="http://code.google.com/p/appswell/source/browse/appspot/lib/multicache.py">http://code.google.com/p/appswell/source/browse/appspot/lib/multicache.py</a><br />
wiki page: <a href="http://klenwell.com/is/AppengineMulticache">http://klenwell.com/is/AppengineMulticache</a></p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2011/06/memcache-limit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Arcade Fire Website</title>
		<link>http://klenwell.com/press/2010/10/google-arcade-fire/</link>
		<comments>http://klenwell.com/press/2010/10/google-arcade-fire/#comments</comments>
		<pubDate>Sat, 09 Oct 2010 21:24:49 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Code Case]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=193</guid>
		<description><![CDATA[The Google Arcade Fire HTML5 demo is cool, but the code is surprisingly simple: from google import WebApp, Predict, Users, Maps, Html5, NsaProxy user = Users.lookup_by_existing_cookies_and_ip() address = Predict.get_address(&#039;Enter the address where you grew up:&#039;) data_raper = NsaProxy.init(user, address) try: map = Maps.load_data(address) if map.context == &#039;urban&#039;: kid = Html5.load_urban_kid() else: kid = Html5.load_suburban_kid() video [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://thewildernessdowntown.com/">Google Arcade Fire HTML5 demo</a> is cool, but the code is surprisingly simple:</p>
<pre class="brush: python">
from google import WebApp, Predict, Users, Maps, Html5, NsaProxy

user = Users.lookup_by_existing_cookies_and_ip()
address = Predict.get_address(&#039;Enter the address where you grew up:&#039;)
data_raper = NsaProxy.init(user, address)

try:
    map = Maps.load_data(address)

    if map.context == &#039;urban&#039;:
        kid = Html5.load_urban_kid()
    else:
        kid = Html5.load_suburban_kid()

    video = WebApp.play(Html5.load_videos(kid, map))

    while video.playing():
        data_raper.rapes_your_personal_data()

except Html5.MovieOver:
    WebApp.redirect_to_launchpage()
except:
    WebApp.redirect_to_chrome_required_page()
</pre>
<p>Great album, by the way.</p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2010/10/google-arcade-fire/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FormEncode (Pylons): What Is State?</title>
		<link>http://klenwell.com/press/2009/04/formencode-state/</link>
		<comments>http://klenwell.com/press/2009/04/formencode-state/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 18:43:45 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Code Case]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=131</guid>
		<description><![CDATA[If you're using <a href="http://pylonshq.com/">Pylons</a>, the Python framework, you're probably using <a href="http://formencode.org/">FormEncode</a>.  And if you're using FormEncode, you've probably have noticed, and blithely ignored, the state argument that's the last argument to a number of the validator class methods.

So what is state and why would you want to use it?]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using <a href="http://pylonshq.com/">Pylons</a>, the Python framework, you&#8217;re probably using <a href="http://formencode.org/">FormEncode</a>.  And if you&#8217;re using FormEncode, you&#8217;ve probably have noticed, and blithely ignored, the state argument that&#8217;s the last argument to a number of the validator class methods.</p>
<p>This is the official FormEncode explanation of state:</p>
<blockquote><p>All the validators receive a magic, somewhat meaningless state argument (which defaults to None). It&#8217;s used for very little in the validation system as distributed, but is primarily intended to be an object you can use to hook your validator into the context of the larger system.</p>
<p>For instance, imagine a validator that checks that a user is permitted access to some resource. How will the validator know which user is logged in? State! Imagine you are localizing it, how will the validator know the locale? State! Whatever else you need to pass in, just put it in the state object as an attribute, then look for that attribute in your validator.</p></blockquote>
<p>Hmmm.  Maybe an example would help.  Here&#8217;s the best example of its usage I could find on <a href="http://wiki.pylonshq.com/display/pylonscookbook/The+other+FormEncode+manual+%28UNFINISHED%29">the Pylons site</a>:</p>
<pre class="brush: python">
  this_schema = TheAppropriateSchema()
  try:
    state = dict() # this won&#039;t actually work. you&#039;ll need an object that formencode can hang things on.
    state[&quot;useful_state_information&quot;] = something_useful
    form_result = this_schema.to_python(postvars, state)

    # validation is successful - form_result contains good data for you to consume

    redirect_to(&quot;somewhere&quot;)

  except Invalid, e:
    defaults = request.params
    errors = e.error_dict
    unfilled_html = a_function_that_draws_your_form_page_html()
    filled_html = htmlfill.render(unfilled_html, defaults, errors)
    return filled_html
</pre>
<p>Notice the comment: <strong># this won&#8217;t actually work.</strong>  Thanks for the warning.</p>
<p>So what is state and why would you want to use it?</p>
<p>Let&#8217;s answer the second question first, because at work, where we&#8217;re using the Pylons framework, I came up with an excellent situation which helped me figure out what it is and why I would want to use it.</p>
<p>The situation, briefly: our application has two separate forms.  One is a form where a user can add new records in a multi-row table form.  Each row is an individual record.  The second is a review form, which looks just like the new record form, but is filled in from data uploaded in csv file (from another form).</p>
<p>Since DRY is a guiding principle of development in my office, the goal is to use the same underlying code for the form, controller, validation, etc.  But the problem is that data originate in two different formats: one is as POST data from the add form, the second as the contents of a csv file.  So how can we normalize these two data formats so that we can use the same underlying code? All together now: <strong>State!</strong>  Now you&#8217;re talking!</p>
<p>If we could just tell our FormEncode validator where the data was coming from, then we could create two separate normalization methods in our validation class that would transform the data into a form that the validator and template could deal with.  Anyway, enough verbiage.  Find below a representative generalized version of the FormEncode validator I created and the controller it&#8217;s used in.</p>
<p>One more note before I finish.  Notice the FormencodeState object comment. That explains why the example from the Pylons wiki does not work (in most cases).  My first impulse would be to use a dict, too, as the state object.  But the internals of the FormEncode validator class require an object.  This is what gets passed in the validator method state argument and it is what passes info from the general framework environment to the validator.</p>
<h4>Pylons Controller Code (with State Object)</h4>
<pre class="brush: python">
class FormencodeState(object):
    &quot;&quot;&quot;
    State class for formencode
    Although NOT well documented, to use the state argument in the to_python
    method in the context of schema that does complex, multistep validation,
    the state argument must be an object that formencode can hang additional
    attributes from, else you get errors like:
    Module formencode.schema:114 in _to_python
    &gt;&gt;  state.full_dict = value_dict
    &lt;type &#039;exceptions.AttributeError&#039;&gt;: &#039;dict&#039; object has no attribute &#039;full_dict&#039;
    &quot;&quot;&quot;
    pass

# ... within actual Controller class method
Validator = SourceDataPreValidator()
ControllerState = FormencodeState()
ControllerState.source = &#039;csv&#039;
Validator.to_python(DataValue, ControllerState)     # &lt;- state object in action!
</pre>
<h4>FormEncode Validator Code</h4>
<pre class="brush: python">
class SourceDataPreValidator(formencode.validators.FormValidator):
    &quot;&quot;&quot;normalize data from either a form submission or a csv file upload and validate&quot;&quot;&quot;
    validate_partial_form = True
    def _to_python(self, value, state):
        &quot;&quot;&quot;normalize csv input&quot;&quot;&quot;
        if state.source == &#039;csv&#039;:
            value[&#039;normal_data&#039;] = self._normalize_csv_data(value.get(&#039;csv_import_file&#039;), state)
        elif state.source == &#039;form&#039;:
            pass
        return value

    def _normalize_import_data(self, csv_file_string, state):
        NormalDataList = []
        ImportedLines = csv_file_string.split(&#039;\n&#039;)
        for i in range(len(ImportedLines)):
            ColValues = ImportedLines[i].split(&#039;,&#039;)
            fpre = &#039;csv_import-&#039; + str(i)
            NormalDataList.append((fpre + &#039;.id&#039;, str(ColValues[1])))
            NormalDataList.append((fpre + &#039;.amount&#039;, str(ColValues[2])))
            NormalDataList.append((fpre + &#039;.date&#039;,
                self._importdate_to_python_date(ColValues[4])))
        return NormalDataList

    def _importdate_to_python_date(self, datestr):
        &quot;&quot;&quot;some code to convert a date string to object&quot;&quot;&quot;
        pass
</pre>
<p>I hope that sheds a little light on this powerful mystery.  Questions or comments welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2009/04/formencode-state/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Comparing Big Numbers in PHP</title>
		<link>http://klenwell.com/press/2009/03/comparing-big-numbers-in-php/</link>
		<comments>http://klenwell.com/press/2009/03/comparing-big-numbers-in-php/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 17:49:53 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Code Case]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=122</guid>
		<description><![CDATA[Problem What if I need to compare really big numbers in PHP? Like comparing 2^66 &#62; 3^53? Overview This will work: print (int) (pow(2,66) &#62; pow(3,53)); PHP will convert the integers to scientific notation. But this script illustrates the limitation of normal operational syntax (i.e.: pow(2,66) &#62; pow(3,53) vs. bccomp(bcpow(2,66), bcpow(3,53),1) &#62; 0): $max = [...]]]></description>
			<content:encoded><![CDATA[<h4>Problem</h4>
<p>What if I need to compare really big numbers in PHP?  Like comparing 2^66 &gt; 3^53?</p>
<h4>Overview</h4>
<p>This will work:</p>
<pre class="brush: php">
print (int) (pow(2,66) &gt; pow(3,53));
</pre>
<p>PHP will convert the integers to scientific notation.  But this script illustrates the limitation of normal operational syntax (i.e.: <tt>pow(2,66) &gt; pow(3,53)</tt> vs. <tt>bccomp(bcpow(2,66), bcpow(3,53),1) &gt; 0</tt>):</p>
<pre class="brush: php">
$max = 1000;
foreach ( range(1,$max) as $x )
{
    if (
        ! ( pow(2,$x)+1 &gt; pow(2,$x) ) ||
        ! ( pow(2,$x)-1 &lt; pow(2,$x) ) ||
        ! ( pow(2,$x-1)*2 == pow(2,$x) )
    ) break;
}

$maxpow = $x-1;
$maxint = bcpow(2, $maxpow);
$Result[&#039;op&#039;] = &quot;max supported int: 2^$maxpow or $maxint\n&quot;;

foreach ( range(1,$max) as $x )
{
    if (
        ! ( bccomp(bcadd(bcpow(2,$x),1), bcpow(2,$x)) == 1 ) ||
        ! ( bccomp(bcsub(bcpow(2,$x),1), bcpow(2,$x)) == -1 ) ||
        ! ( bccomp(bcmul(bcpow(2,$x-1),2), bcpow(2,$x)) == 0 )
    ) break;
}

$maxpow = $x-1;
$maxint = bcpow(2, $maxpow);
$Result[&#039;bc&#039;] = &quot;max supported int &gt;= 2^$maxpow or $maxint\n&quot;;

printf(&quot;&lt;pre&gt;%s&lt;/pre&gt;&quot;, print_r($Result,1));
</pre>
<h4>Solution</h4>
<p>Use bccomp.  The script above can be found on the klenwell code site:</p>
<p><a href="http://code.google.com/p/klenwell/source/browse/trunk/pastebin/bc_demo.php">http://code.google.com/p/klenwell/source/browse/trunk/pastebin/bc_demo.php</a></p>
<p>Results will vary depending on the processing capacity of the system.  Here&#8217;s the results on my machine:</p>
<div class="code">
Array<br />
(<br />
	[op] => max supported int: 2^52 or 4503599627370496</p>
<p>	[bc] => max supported int >= 2^999 or 5357543035931336604742125245300009052807024058527668037218751941851755255624680612465991894078479290637973364587765734125935726428461570217992288787349287401967283887412115492710537302531185570938977091076523237491790970633699383779582771973038531457285598238843271083830214915826312193418602834034688<br />
)
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2009/03/comparing-big-numbers-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Validation Error Messages in CakePhp</title>
		<link>http://klenwell.com/press/2009/01/cakephp-dynamic-error-message/</link>
		<comments>http://klenwell.com/press/2009/01/cakephp-dynamic-error-message/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 17:28:34 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Code Case]]></category>
		<category><![CDATA[cakephp]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=86</guid>
		<description><![CDATA[Problem In CakePhp, I want to invalidate a field submitted in a form and set the error message dynamically. If the error message is set by a validation parameter within the model, the new message should override that error message and be displayed by the form in the view. Overview Consider the following three cases [...]]]></description>
			<content:encoded><![CDATA[<h4>Problem</h4>
<p>In CakePhp, I want to invalidate a field submitted in a form and set the error message dynamically. If the error message is set by a validation parameter within the model, the new message should override that error message and be displayed by the form in the view.</p>
<h4>Overview</h4>
<p>Consider the following three cases as a developer:</p>
<p>1. You have a textarea field which requires user input of at least 12 words.  When the user inputs less than 12 words, you&#8217;d like to display a message that states, &#8220;This field requires a response of at least 12 words.  Your response was $x words long&#8221; where $x is the actual number of words in the user&#8217;s input.</p>
<p>2. You have a form with three input fields.  None of them is required in and of itself.  However, for the form to be valid, 2 of the 3 must be filled in.</p>
<p>3. You have an application in which one user submits a form which is then reviewed and approved by a second.  The form has a &#8220;summary&#8221; field which the first user may fill in but does not have to.  However, the second user must fill it in if it is empty.</p>
<p>Each of these requirements describes a somewhat complex validation case that <a href="http://book.cakephp.org/view/125/Data-Validation">CakePhp&#8217;s normal method for configuring data validation within the model</a> doesn&#8217;t adequately address.</p>
<h4>Solution</h4>
<p>Rather than using the static form of validation described in the documentation, or even a <a href="http://book.cakephp.org/view/150/Custom-Validation-Rules">custom validation</a> rule which, although it may suffice, is still oriented around a single field with a static method, I recommend validating the field dynamically from within the controller or a custom model method using the model&#8217;s <tt>invalidate</tt> field.  </p>
<p>The following commented code demonstrates a technique which could be used to satisfy each of the three cases above:</p>
<pre class="brush: php">
&lt;?php

class MyModel extends AppModel
{
    var $name = &#039;MyModel&#039;;
    var $my_textarea_min_word_req = 12;
    var $ValidCodeList = array(
        0 =&gt; &#039;pass&#039;,
        1 =&gt; &#039;fail&#039;,
        2 =&gt; &#039;requires further review&#039;
    );

    // snipped

    // essentially a wrapper for save with some extra circumstantial validation
    function update_record($FormData)
    {
        $UpdateData = $FormData[&#039;MyModel&#039;];

        // situation-specific validation: this code is only required in this
        // instance so we don&#039;t set a rule within the model&#039;s validation parameters
        // above.  note: this could also be given its own separate method
        $textarea_wordlen = str_word_count($UpdateData[&#039;my_textarea&#039;]);
        if ( $textarea_wordlen &lt; $this-&gt;my_textarea_min_word_req )
        {
            $s_ = ( $textarea_wordlen === 1 ) ? &#039;&#039; : &#039;s&#039;;   // for the grammar nazis

            // note that it is here we&#039;re dynamically setting the error message
            $this-&gt;invalidate(&#039;my_textarea&#039;,
                &quot;This field requires at least {$this-&gt;my_textarea_min_word_req} words.  Your response was {$textarea_wordlen} word{$s_}.&quot;);
        }

        // in update only, an internal code is required
        if ( !isset($this-&gt;ValidCodeList[$UpdateData[&#039;internal_code&#039;]]) )
            $this-&gt;invalidate(&#039;internal_code&#039;, &#039;please select a valid review code&#039;);

        // if the other fields are valid, the model will be saved
        if ( $this-&gt;validates($UpdateData) )
        {
            $this-&gt;create();
            $this-&gt;id = $UpdateData[&#039;id&#039;];        // set id to update record
            return  $this-&gt;save($UpdateData);
        }
        // return false (which will lead back to the form)
        else
        {
            return 0;
        }
    }
}

?&gt;
</pre>
<p>In the end, it&#8217;s pretty simple and straightforward. Still, I could not find it clearly spelled out in any one place in the CakePhp documentation or elsewhere on the web.</p>
<p>One caveat to note that will further explain the inclination to forego usage of the model&#8217;s <tt>validate</tt> parameter above the I came across here: <a href="http://groups.google.com/group/cake-php/msg/095804b352539b97?hl=en">http://groups.google.com/group/cake-php/msg/095804b352539b97?hl=en</a>. If you do have a validation rule set for that field within the model, the message will get overridden by the default error message there if the field also fails the validation set there.</p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2009/01/cakephp-dynamic-error-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Php Input Validation</title>
		<link>http://klenwell.com/press/2009/01/php-input-validation/</link>
		<comments>http://klenwell.com/press/2009/01/php-input-validation/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 03:16:38 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Code Case]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=80</guid>
		<description><![CDATA[Problem I need to validate input from a textarea field. I want to allow a few tags, like a, i, b, etc. But everything else needs to be filtered out. And the input should be checked to see that its is nested properly. Overview User input sanitization and validation is one of those things that [...]]]></description>
			<content:encoded><![CDATA[<h4>Problem</h4>
<p>I need to validate input from a textarea field. I want to allow a few tags, like a, i, b, etc. But everything else needs to be filtered out. And the input should be checked to see that its is nested properly.  </p>
<h4>Overview</h4>
<p>User input sanitization and validation is one of those things that just needs to be done. Dealing with a textarea is more complicated and validating html for re-display on a web page is less trivial than it seems at a glance. It&#8217;s a pain in the ass so I like it when I can find someone who has already solved the issue. I found a new solution here: <a href="http://htmlpurifier.org/">HTML Purifier</a>. </p>
<p>The author, <a href="http://htmlpurifier.org/contact.html">ezyang</a>, offers a detailed study of the issue as well as a short history of the PHP HTML validators that preceded his and where they fall short. It&#8217;s well-done, an impressive work of engineering and scholarship, but I hesitate to use the class because it&#8217;s just so damned large. Also, the dependencies are a little bit confusing to me, though I am sure they could be sorted out easily enough by testing the class. On the plus side, the API is simple enough. But I don&#8217;t need the kind of comprehensive solution that&#8217;s offered here.</p>
<h4>The Process</h4>
<p>Since the solution I am looking for involves two parts: (1) sanitizing the input and (2) validating the markup, I figured I&#8217;d take the Reese&#8217;s approach and just try to combine two existing classes that will taste great together.  The data sanitization component is based on the <a href="http://www.phpclasses.org/browse/package/2189.html">Input Filter class</a> that the ezyang critiques, justifiably, as inadequate. It does the job sanitizing input. Where if falls short is in validating the markup to make sure it&#8217;s properly formed. That&#8217;s where the second source comes in: Simon Willison&#8217;s <a href="http://simon.incutio.com/code/php/SafeHtmlChecker.class.php.txt">Safe HTML Checker</a>. This relies on Php&#8217;s native XML parser and is nice and short.</p>
<p>I&#8217;m calling my class Input Baffle, where <a href="http://en.wiktionary.org/wiki/baffle">baffle</a> means &#8220;A device used to restrain or regulate.&#8221;  It basically acts as a wrapper (is this the <a href="http://en.wikipedia.org/wiki/Design_Patterns">composite pattern</a>?) for these two subordinate classes.</p>
<p>Even with the hard work done for me, the problem still proved thorny and took me several hours to work out all the significant wrinkles.  The major complications I encountered included:</p>
<h5>1. Parser Iteration in Safe HTML Checker</h5>
<p>I set up my class to create a single instance of the SafeHtmlChecker class as a member.  SafeHtmlChecker is essentially a wrapper for Php&#8217;s native <a href="http://www.php.net/manual/en/ref.xml.php">XML Parser</a>.  The problem with this is that there isn&#8217;t a way &ndash; at least, an obvious one &ndash; to reuse the parser after it has detected an error.  This is a problem, as in my unit test, where my class might be validating more than one field in a form.  The problem is identified <a href="http://www.php.net/manual/en/function.xml-parser-free.php#57370">here</a>.  The solution was to have each call to validate the submitted markup create, and destroy, a separate parser.</p>
<h5>2. Recursion Overflow in InputFilter</h5>
<p>Try running this <code>&lt;&lt;&lt;&gt;&gt;&gt;</code> through InputFilter.  Memory overflow.  To solve that problem, I added a preprocessor to my class that uses regex to eliminate this and a few other hobgoblins that InputFilter misses.</p>
<h5>3. InputFilter vs. the rsnake XSS Cheat Sheet</h5>
<p>Discovered <a href="http://ha.ckers.org/xss.html">the XSS cheat sheet</a> on the HTML Purifier site.  An excellent test set, I added methods (based on <a href="http://github.com/ezyang/htmlpurifier/blob/bfe474042f191abc87c49a8a373c39fc3b449833/smoketests/xssAttacks.php">HTML Purifier&#8217;s smoke test</a>), to run the test.  HTML Purifier aces the test.  InputFilter does not.  InputFilter, in conjunction with SafeHtmlChecker catches all the exploits except one.  So I just added that one to the preprocessor.</p>
<h4>Solution</h4>
<p>You can find my code, with unit tests, on my Google Code site:  </p>
<p><a href="http://klenwell.googlecode.com/svn/trunk/projects/php/kwoss/input_baffle/">http://klenwell.googlecode.com/svn/trunk/projects/php/kwoss/input_baffle/</a></p>
<p>My goal ultimately is to convert this to a CakePhp behavior.</p>
<p>I already knew the stakes involved with input filtering and validation.  And I knew it was not a trivial problem.  Working through this, and referring to ezyang&#8217;s more fastidious solution, gave me a much deeper appreciation for how complex it really is.</p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2009/01/php-input-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePhp: Custom Pagination Methods</title>
		<link>http://klenwell.com/press/2009/01/cakephp-custom-pagination/</link>
		<comments>http://klenwell.com/press/2009/01/cakephp-custom-pagination/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 19:05:59 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Code Case]]></category>
		<category><![CDATA[cakephp]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=70</guid>
		<description><![CDATA[I wanted to use the CakePhp pagination helper to paginate some complex records. The model that the paginator was referencing had both belongsTo and hasMany associations. belongsTo associations are not a problem. hasMany, I discovered, are &#8212; which makes sense when I think about. As the ticket notes: You can only paginate LEFT JOIN tables [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to use the CakePhp pagination helper to paginate some complex records.  The model that the paginator was referencing had both <code>belongsTo</code> and <code>hasMany</code> associations.  <code>belongsTo</code> associations are not a problem.  hasMany, <a href="https://trac.cakephp.org/ticket/5319" onclick="window.open(this.href,'_blank');return false;">I discovered</a>, are &mdash; which makes sense when I think about.</p>
<p>As <a href="https://trac.cakephp.org/ticket/5319" onclick="window.open(this.href,'_blank');return false;">the ticket</a> notes:</p>
<blockquote><p>You can only paginate LEFT JOIN tables by default. hasMany relationships are performed as a separate query, and thus cannot have conditions in the primary paginate() call. If you wish to do this, you need to implement paginate() and paginateCount() in your model.</p></blockquote>
<p>So that&#8217;s what I did.  Only it wasn&#8217;t quite as simple as I thought it would be.</p>
<p>The CakePhp manual does provide some guidance: <a href="http://book.cakephp.org/view/249/Custom-Query-Pagination" onclick="window.open(this.href,'_blank');return false;">4.9.4 Custom Query Pagination</a>.  This comes from the example originally posted <a href="http://www.littlehart.net/atthekeyboard/2008/03/04/custom-cakephp-12-pagination-queries/">here</a> (littlehart.net), where there are additional comments to both confuse and clarify a bit.</p>
<p>One thing that I didn&#8217;t see ever made explicit is how exactly you integrate the custom model methods you create, paginate and paginateCount, with the paginator syntax in your controller or helper.  Well, it turns out, as you would probably hope, that it&#8217;s fairly seamless.  You just use the standard pagination markup , and the custom methods override Cake&#8217;s default methods (<a href="https://trac.cakephp.org/changeset/5248" onclick="window.open(this.href,'_blank');return false;">this changeset</a> illustrates).</p>
<p>With hasMany associations, as is the case in the example above from Cake&#8217;s website, you are probably going to need to use <code>GROUP BY</code> syntax.  For my particular case, this required a custom SQL query, which meant using Cake&#8217;s Model class&#8217;s <code>query</code>.  (I&#8217;m a little surprised and concerned that it doesn&#8217;t support bind parameters.)</p>
<p>I&#8217;ve posted the relevant code to my wiki: <a href="http://www.klenwell.com/is/Paste20090117">http://www.klenwell.com/is/Paste20090117</a></p>
<p>I hope it helps steers someone to the answer they&#8217;re looking for.</p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2009/01/cakephp-custom-pagination/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CakePhp: Radio Select Columns</title>
		<link>http://klenwell.com/press/2009/01/cakephp-radio-select-columns/</link>
		<comments>http://klenwell.com/press/2009/01/cakephp-radio-select-columns/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 18:46:47 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Code Case]]></category>
		<category><![CDATA[cakephp]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=57</guid>
		<description><![CDATA[In HTML forms, I generally prefer radio groups over select menus. One advantage select elements have over radio groups is a smaller visual footprint. A long radio list takes up a lot more screen room than a long select list. One way to make a radio list more efficient is to break it up into [...]]]></description>
			<content:encoded><![CDATA[<p>In HTML forms, I generally prefer radio groups over select menus.  One advantage select elements have over radio groups is a smaller visual footprint.  A long radio list takes up a lot more screen room than a long select list.</p>
<p>One way to make a radio list more efficient is to break it up into columns.  I&#8217;ve written a CakePhp helper that accomplishes this.  An illustration:</p>
<div id="attachment_58" class="wp-caption alignnone" style="width: 310px"><a href="http://www.klenwell.com/press/wp-content/uploads/2009/01/radio_nocols_cols.png"><img class="size-medium wp-image-58" title="radio_nocols_cols" src="http://www.klenwell.com/press/wp-content/uploads/2009/01/radio_nocols_cols-300x112.png" alt="radio group without (l) and with columns (r)" width="300" height="112" /></a><p class="wp-caption-text">radio group without (l) and with (r) columns</p></div>
<p>It turns out that CakePhp Form helper&#8217;s input method gives me all the tools I needed it to adapt my code pretty easily to it.</p>
<p>I&#8217;ve pasted the code over on my wiki: <a href="http://www.klenwell.com/is/Paste20090110">http://www.klenwell.com/is/Paste20090110</a></p>
<p>The part of the code I think is most interesting is the code I use to divvy up the options array into the columns:</p>
<pre class="brush: php">
// get option list
$OptionList = explode($this-&gt;separator_marker, $middle);
$num_options = count($OptionList);

// snip

// divvy options into columns (wrote this code a while ago and seems to work)
for( $i = 1; $i &lt;= $num_cols; $i++ )
{
    $COL[$i] = ceil( $num_options/($num_cols+1-$i) );
    $num_options = $num_options - $COL[$i];
}
</pre>
<p>I look at it now and I know the gist of what it&#8217;s doing, but still have to really stop and think about it to fully understand it.  I think it&#8217;s interesting because it shows how my mind works better procedurally than it does mathematically.</p>
<p>Here&#8217;s an illustration showing again the columnized radio list along with the class and id markup that can be used for styling:</p>
<div id="attachment_59" class="wp-caption alignnone" style="width: 310px"><a href="http://www.klenwell.com/press/wp-content/uploads/2009/01/radio_cols_css.png"><img class="size-medium wp-image-59" title="radio_cols_css" src="http://www.klenwell.com/press/wp-content/uploads/2009/01/radio_cols_css-300x186.png" alt="radio columns with id/class markup" width="300" height="186" /></a><p class="wp-caption-text">radio columns with id/class markup</p></div>
<p>I&#8217;ll eventually add this the <a href="http://klenwell.googlecode.com/svn/trunk/projects/php/cakewell/">cakewell app</a> in my google code repository.</p>
<p>One caveat: this works with the Form helper&#8217;s input method (type set to &#8216;radio&#8217;), but does not seem to work with the Form helper&#8217;s <a href="http://book.cakephp.org/view/727/radio">radio method</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2009/01/cakephp-radio-select-columns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

