<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    
    <title>Pet Pixels - PHP</title>
    <link>http://marcus.bointon.com/</link>
    <description>Skiing, Baking, Biking, PHP, Email, Music, in variable order</description>
    <dc:language>en</dc:language>
    <admin:errorReportsTo rdf:resource="mailto:marcusblog@bointon.com" />
    <generator>Serendipity 1.5.5 - http://www.s9y.org/</generator>
    <pubDate>Wed, 02 Nov 2011 22:36:13 GMT</pubDate>

    <image>
        <url>http://marcus.bointon.com/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: Pet Pixels - PHP - Skiing, Baking, Biking, PHP, Email, Music, in variable order</title>
        <link>http://marcus.bointon.com/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>PHP Base-62 encoding</title>
    <link>http://marcus.bointon.com/archives/92-PHP-Base-62-encoding.html</link>
            <category>PHP</category>
            <category>Techie</category>
    
    <comments>http://marcus.bointon.com/archives/92-PHP-Base-62-encoding.html#comments</comments>
    <wfw:comment>http://marcus.bointon.com/wfwcomment.php?cid=92</wfw:comment>

    <slash:comments>10</slash:comments>
    <wfw:commentRss>http://marcus.bointon.com/rss.php?version=2.0&amp;type=comments&amp;cid=92</wfw:commentRss>
    

    <author>marcus@bointon.com (Marcus Bointon)</author>
    <content:encoded>
    &lt;p&gt;There&#039;s a really horrible bug (they won&#039;t call it that, but I can&#039;t think of any use case for the default broken behaviour!) in Apache&#039;s mod_rewrite that means that urlencoded inputs in rewrites get unescaped in their transformation to output patterns. The underlying &#039;bug&#039; remains unfixed even in 2.3, though a workaround in the form of the &lt;a href=&quot;http://httpd.apache.org/docs/current/rewrite/flags.html#flag_b&quot;&gt;&#039;B&#039; flag&lt;/a&gt; first appeared in Apache 2.2.7, but was broken until 2.2.12 (which wasn&#039;t all that long ago). Put it like this: if you&#039;re not using the B flag in your mod_rewrite rules, your site is probably only working due to blind luck.&lt;/p&gt;
&lt;p&gt;With that in mind, several years ago I spent ages looking for a base-62 encoder/decoder for PHP to replace mod_rewrite&#039;s broken urlencoding handling. Nobody seemed to have the slightest interest in writing one. Base-62 is interesting as it can be made safe for use in URLs, DNS, email addresses and pathnames, unlike any available encoding of base-64, as it only includes [0-9A-Za-z]. As a workaround for the above bug, I was interested in base-62 encoding URLs for embedding in redirects. At the time &lt;a href=&quot;http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_21980473.html&quot;&gt;I wrote something using bc_math&lt;/a&gt;, but it was very slow (and weirdly got &lt;a href=&quot;http://www.wtfnix.com/2008/05/base-62-encoding-php/&quot; rel=&quot;nofollow&quot;&gt;ripped off&lt;/a&gt; by some dickhead and passed off as his own, despite that fact that I said it was crap!). I eventually gave up on that and switched to base-64, which led to occasional URL corruption. If you include hashes in URLs, keeping them in the default hex representation is quite wasteful, and can contribute to issues with line length in email. Having hashes in base-62 is a nice way of reducing their size.&lt;/p&gt;
&lt;p&gt;There are a few posts on base-62 in PHP, notably &lt;a href=&quot;http://programanddesign.com/php/base62-encode/&quot;&gt;this one&lt;/a&gt; and &lt;a href=&quot;http://snipplr.com/view/22246/&quot;&gt;this one&lt;/a&gt;, but they make the assumption that you&#039;re talking about a numeric value, and while a hash is a numeric value, it&#039;s way too big for PHP to handle as an integer. Others take the multiprecision artithmetic route, which treats the input binary as a single very large, and calculates its representation in another base; that works, but it&#039;s horribly slow.&lt;/p&gt;
&lt;p&gt;Since then, the gmp and bc_math extensions were improved in PHP 5.3.2, and now they handle (usefully) up to base-62. So here&#039;s a simple function for getting a hash in base-62:&lt;/p&gt;
&lt;div class=&quot;php geshi&quot; style=&quot;text-align: left&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; base62hash&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$source&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.php.net/gmp_strval&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;gmp_strval&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/gmp_init&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;gmp_init&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/md5&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;md5&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$source&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; 16&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; 62&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;and for converting to and from base-16 hashes:&lt;/p&gt;
&lt;div class=&quot;php geshi&quot; style=&quot;text-align: left&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; hash16to62&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$hash&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.php.net/gmp_strval&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;gmp_strval&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/gmp_init&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;gmp_init&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$hash&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; 16&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; 62&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; hash62to16&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$hash&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.php.net/gmp_strval&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;gmp_strval&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/gmp_init&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;gmp_init&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$hash&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; 62&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; 16&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;I could still use a proper base-62 encoder for longer arbitrary strings, but at least now it should be simpler to write something iterative now that these extensions have (ahem) their bases covered.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: I&#039;ve written a sufficiently usable PHP base-62 encoder for arbitrary-length binary strings that&#039;s not too slow. You can find it on github in &lt;a href=&quot;https://gist.github.com/1139429&quot;&gt;this gist&lt;/a&gt;. Let me know if you find it useful&lt;/p&gt;
&lt;p&gt;Incidentally I discovered that the gmp functions use [0-9a-f] up to base 16, but [0-9A-Za-z] (i.e. upper case first) from bases 17 to 62. This differs from most of the base-62 implementations I&#039;ve found that tend to use lower case first.&lt;/p&gt;
&lt;p&gt;This is all slightly academic now as the apache B-flag workaround works, so standard urlencoding works properly and I don&#039;t need to use a different encoding any more, however, there were so many examples of slow encoders, I thought the world could do with a usable one.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; Something else worth mentioning is that if you use the apache B flag, you most likely need to turn the &lt;a href=&quot;http://httpd.apache.org/docs/current/mod/core.html#allowencodedslashes&quot;&gt;AllowEncodedSlashes&lt;/a&gt; directive on too, as otherwise you&#039;ll get mysterious 404s. I posted &lt;a href=&quot;https://issues.apache.org/bugzilla/show_bug.cgi?id=51669&quot;&gt;a bug report&lt;/a&gt; against the apache docs to make this clearer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; Apache used &lt;a href=&quot;http://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_b&quot;&gt;my rewrite of the B-flag docs&lt;/a&gt;, yay!&lt;/p&gt; 
    </content:encoded>

    <pubDate>Wed, 10 Aug 2011 16:31:20 +0100</pubDate>
    <guid isPermaLink="false">http://marcus.bointon.com/archives/92-guid.html</guid>
    
</item>
<item>
    <title>Subversion 1.7 to 1.6 downgrade with MacPorts</title>
    <link>http://marcus.bointon.com/archives/94-Subversion-1.7-to-1.6-downgrade-with-MacPorts.html</link>
            <category>PHP</category>
            <category>Techie</category>
    
    <comments>http://marcus.bointon.com/archives/94-Subversion-1.7-to-1.6-downgrade-with-MacPorts.html#comments</comments>
    <wfw:comment>http://marcus.bointon.com/wfwcomment.php?cid=94</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://marcus.bointon.com/rss.php?version=2.0&amp;type=comments&amp;cid=94</wfw:commentRss>
    

    <author>marcus@bointon.com (Marcus Bointon)</author>
    <content:encoded>
    MacPorts told me that there had been a subversion update (1.7.1), which I went ahead and installed. Woo! Huge speed improvements for everything I tried with the CLI client, great stuff. A short time later my IDE (PHPStorm) fell over screaming. &lt;a href=&quot;http://youtrack.jetbrains.net/issue/IDEA-72400&quot;&gt;It doesn&#039;t like 1.7 yet&lt;/a&gt;, and it&#039;s a bit stuck until SVNKit supports it. I should have checked really.

So how to downgrade? Fortunately &lt;a href=&quot;http://reinout.vanrees.org/weblog/2008/09/19/macports-downgrading-subversion.html&quot;&gt;this post&lt;/a&gt; makes it very easy. So I just did:
&lt;p&gt;
&lt;tt&gt;sudo port deactivate subversion @1.7.0_1&lt;/tt&gt;&lt;br /&gt;
&lt;tt&gt;sudo port activate subversion @1.6.17_1&lt;/tt&gt;&lt;/p&gt;
But now I&#039;m stuck with a working copy in 1.7 format with uncommitted changes, and there is no tool to convert it back to 1.6 format. This is easily worked around; check out a new working copy (using svn 1.6) and sync across the changes, ignoring the .svn folders, like this:

&lt;p&gt;&lt;tt&gt;
rsync -av --update --exclude=&quot;.svn/***&quot; ~/Sites/myproject1.7/ ~/Sites/myproject1.6
&lt;/tt&gt;&lt;/p&gt;
All happy now. 
    </content:encoded>

    <pubDate>Wed, 02 Nov 2011 21:50:38 +0000</pubDate>
    <guid isPermaLink="false">http://marcus.bointon.com/archives/94-guid.html</guid>
    
</item>
<item>
    <title>Google Charts API Simple and Extended Encoders in PHP</title>
    <link>http://marcus.bointon.com/archives/88-Google-Charts-API-Simple-and-Extended-Encoders-in-PHP.html</link>
            <category>PHP</category>
            <category>Techie</category>
    
    <comments>http://marcus.bointon.com/archives/88-Google-Charts-API-Simple-and-Extended-Encoders-in-PHP.html#comments</comments>
    <wfw:comment>http://marcus.bointon.com/wfwcomment.php?cid=88</wfw:comment>

    <slash:comments>5</slash:comments>
    <wfw:commentRss>http://marcus.bointon.com/rss.php?version=2.0&amp;type=comments&amp;cid=88</wfw:commentRss>
    

    <author>marcus@bointon.com (Marcus Bointon)</author>
    <content:encoded>
    Google&#039;s charting API has been around for quite a while now, but I&#039;ve only just needed to actually look at it. It became immediately obvious that I needed a PHP encoding function, so off to google I went. Though I found several implementations, they were all incomplete or deficient in one way or another (and it didn&#039;t help that there was an error in google&#039;s extended encoding docs), so I&#039;ve written my own based on several different ones. Both simple and extended encoders support automatic scaling, inflated maximum and lower-bound truncation, so you can pretty much stuff whatever data you like in, with no particular regard for pre-scaling and you&#039;ll get a usable result out. They have an identical interface, so you can use either encoding interchangeably according to the output resolution you need (contrary to popular belief, the encoding to use has very little to do with the range of values you need to graph). By default, the full range of possible values is used as it just seems silly not to. I deliberately omit the &#039;s:&#039; and &#039;e:&#039; prefixes so that you can call these functions for multiple data series, and I include a function that does just that. You still need to generate your own URLs and other formatting, but that&#039;s a different problem. Read on for the code... &lt;br /&gt;&lt;a href=&quot;http://marcus.bointon.com/archives/88-Google-Charts-API-Simple-and-Extended-Encoders-in-PHP.html#extended&quot;&gt;Continue reading &quot;Google Charts API Simple and Extended Encoders in PHP&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Fri, 14 Aug 2009 09:34:27 +0100</pubDate>
    <guid isPermaLink="false">http://marcus.bointon.com/archives/88-guid.html</guid>
    <category>api</category>
<category>charts</category>
<category>encoding</category>
<category>google</category>
<category>php</category>

</item>

</channel>
</rss>
