<?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"
	>

<channel>
	<title>Benjamin Holmberg</title>
	<atom:link href="http://benajnim.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://benajnim.com</link>
	<description>More than the bits &#38; pieces</description>
	<pubDate>Fri, 03 Jul 2009 04:06:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>Parsing ifconfig output in PHP</title>
		<link>http://benajnim.com/index.php/php/parsing-ifconfig-output-in-php/</link>
		<comments>http://benajnim.com/index.php/php/parsing-ifconfig-output-in-php/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 04:06:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://benajnim.com/?p=25</guid>
		<description><![CDATA[I recently wrote a class to parse the output of ifcofig (on a debian-based system) and turn the list of interfaces and their attributes into a usable data structure.

Enjoy!
&#160;
class NetworkConfig &#123;
    /*
     * Pass the output of ifconfig into the constructor and this class will create a
  [...]]]></description>
			<content:encoded><![CDATA[<p>I recently wrote a class to parse the output of ifcofig (on a debian-based system) and turn the list of interfaces and their attributes into a usable data structure.<br />
<span id="more-25"></span><br />
Enjoy!</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> NetworkConfig <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/*
     * Pass the output of ifconfig into the constructor and this class will create a
     * usable data structure to access config info.  To define parsing fields for
     * the various link encapsulation types, modify the $baseFieldMap or
     * $encapFieldMap arrays as needed
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$interface</span> = <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$baseFieldMap</span>  = <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
                            <span style="color: #ff0000;">'rx'</span>          =&gt; <span style="color: #ff0000;">'RX bytes:'</span>,
                            <span style="color: #ff0000;">'tx'</span>          =&gt; <span style="color: #ff0000;">'TX bytes:'</span>
                            <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$encapFieldMap</span> = <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">'Ethernet'</span>       =&gt; <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
                            <span style="color: #ff0000;">'mac_address'</span> =&gt; <span style="color: #ff0000;">'HWaddr '</span>,
                            <span style="color: #ff0000;">'ip_address'</span>  =&gt; <span style="color: #ff0000;">'inet addr:'</span>,
                            <span style="color: #ff0000;">'broadcast'</span>   =&gt; <span style="color: #ff0000;">'Bcast:'</span>,
                            <span style="color: #ff0000;">'netmask'</span>     =&gt; <span style="color: #ff0000;">'Mask:'</span><span style="color: #66cc66;">&#41;</span>,
        <span style="color: #ff0000;">'Local Loopback'</span> =&gt; <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
                            <span style="color: #ff0000;">'ip_address'</span>  =&gt; <span style="color: #ff0000;">'inet addr:'</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$config</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$interfaces</span> = <a rel="nofollow" href="http://www.php.net/split"><span style="color: #000066;">split</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #0000ff;">$config</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$interfaces</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$if</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$if</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$interface</span> = <span style="color: #000000; font-weight: bold;">new</span> stdClass;
                <span style="color: #0000ff;">$interface</span>-&gt;<span style="color: #006600;">name</span> = <a rel="nofollow" href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$if</span>, <span style="color: #cc66cc;">0</span>, <a rel="nofollow" href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$if</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
                <span style="color: #808080; font-style: italic;">//$interface-&gt;raw_output = $if; // raw ifconfig output for the interface</span>
                <span style="color: #0000ff;">$interface</span>-&gt;<span style="color: #006600;">encapsulation</span> = TextParser::<span style="color: #006600;">extractBetween</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$if</span>, <span style="color: #ff0000;">&quot;Link encap:&quot;</span>, <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #ff0000;">&quot;  &quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
                <span style="color: #0000ff;">$extractFields</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">baseFieldMap</span>;
                <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">encapFieldMap</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$interface</span>-&gt;<span style="color: #006600;">encapsulation</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <a rel="nofollow" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">encapFieldMap</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$interface</span>-&gt;<span style="color: #006600;">encapsulation</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0000ff;">$extractFields</span> = <a rel="nofollow" href="http://www.php.net/array_merge"><span style="color: #000066;">array_merge</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$extractFields</span>, <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">encapFieldMap</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$interface</span>-&gt;<span style="color: #006600;">encapsulation</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #66cc66;">&#125;</span>
&nbsp;
                <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$extractFields</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$field</span> =&gt; <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0000ff;">$interface</span>-&gt;<span style="color: #0000ff;">$field</span> = TextParser::<span style="color: #006600;">extractBetween</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$if</span>, <span style="color: #0000ff;">$value</span>, <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #ff0000;">&quot;  &quot;</span>, <span style="color: #ff0000;">'  '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #66cc66;">&#125;</span>
                <span style="color: #0000ff;">$this</span>-&gt;__set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$interface</span>-&gt;<span style="color: #006600;">name</span>, <span style="color: #0000ff;">$interface</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span>, <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">interface</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$value</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/array_key_exists"><span style="color: #000066;">array_key_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span>, <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">interface</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">interface</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #0000ff;">$trace</span> = <a rel="nofollow" href="http://www.php.net/debug_backtrace"><span style="color: #000066;">debug_backtrace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <a rel="nofollow" href="http://www.php.net/trigger_error"><span style="color: #000066;">trigger_error</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Undefined property via __get(): '</span> . <span style="color: #0000ff;">$name</span> . <span style="color: #ff0000;">' in '</span> . <span style="color: #0000ff;">$trace</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'file'</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">' on line '</span> . <span style="color: #0000ff;">$trace</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'line'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #000000; font-weight: bold;">E_USER_NOTICE</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInterfaceNames<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <a rel="nofollow" href="http://www.php.net/array_keys"><span style="color: #000066;">array_keys</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">interface</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInterfacesByType<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$type</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$return</span> = <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$ifs</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getInterfaceNames</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ifs</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$if</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$iface</span> = <span style="color: #0000ff;">$this</span>-&gt;__get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$if</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$iface</span>-&gt;<span style="color: #006600;">encapsulation</span> == <span style="color: #0000ff;">$type</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$return</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$iface</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$return</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TextParser <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/*
     * $startInclusive - include the startString matched upon in the final output
     * $stopSting - can accept an array, and will match/terminate on the first value found in the string
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <a rel="nofollow" href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> extractBetween<span style="color: #66cc66;">&#40;</span>&amp;<span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$startString</span>, <span style="color: #0000ff;">$stopString</span>, <span style="color: #0000ff;">$startInclusive</span> = <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$startAdd</span> = <span style="color: #cc66cc;">0</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$startInclusive</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$startAdd</span> = <a rel="nofollow" href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$startString</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #0000ff;">$startPos</span> = <a rel="nofollow" href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$startString</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$startPos</span> += <span style="color: #0000ff;">$startAdd</span>;
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stopString</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$stopCompare</span> = <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stopString</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$ss</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$stp</span> = <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$ss</span>, <span style="color: #0000ff;">$startPos</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #0000ff;">$startPos</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stp</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0000ff;">$stopCompare</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$stp</span>;
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stopCompare</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$extractionLength</span> = <span style="color: #cc66cc;">0</span>;
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$extractionLength</span> = <a rel="nofollow" href="http://www.php.net/min"><span style="color: #000066;">min</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stopCompare</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$extractionLength</span> = <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$stopString</span>, <span style="color: #0000ff;">$startPos</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #0000ff;">$startPos</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #808080; font-style: italic;">//echo '$string: ' . $string . &quot;\n&quot; . '$extractionLength: ' . $extractionLength . &quot;\n&quot;;</span>
        <span style="color: #808080; font-style: italic;">//echo '$extractionLength: ' . $extractionLength . &quot;\n&quot;;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$extractionLength</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$rtn</span> = <a rel="nofollow" href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$startPos</span>, <span style="color: #0000ff;">$extractionLength</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$extractionLength</span> &lt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$rtn</span> = <a rel="nofollow" href="http://www.php.net/trim"><span style="color: #000066;">trim</span></a><span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$startPos</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$rtn</span> = <span style="color: #ff0000;">''</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$rtn</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/php/parsing-ifconfig-output-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating a Custom ServerSignature in Apache</title>
		<link>http://benajnim.com/index.php/security/creating-a-custom-serversignature-in-apache/</link>
		<comments>http://benajnim.com/index.php/security/creating-a-custom-serversignature-in-apache/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 02:27:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Apache]]></category>

		<category><![CDATA[Code]]></category>

		<category><![CDATA[Security]]></category>

		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://benajnim.com/?p=18</guid>
		<description><![CDATA[By default, Apache reveals its version number a couple of ways.  Most often, the server version is returned in server response headers although it is also printed in the default footer of directory listings generated via the handy mod_autoindex module.
Revealing the server type and version number can make it easy to identify servers vulnerable to [...]]]></description>
			<content:encoded><![CDATA[<p>By default, Apache reveals its version number a couple of ways.  Most often, the server version is returned in server response headers although it is also printed in the default footer of directory listings generated via the handy mod_autoindex module.</p>
<p>Revealing the server type and version number can make it easy to identify servers vulnerable to attacks affecting a given web server version.  By hiding or changing the server signature, a system wont be inherently more secure, but it will make it less visible considering most machines will more easily give up their version numbers.  Perhaps you want to display a message to the users in an auto-indexed sectioned of your website.  You can setup a "readme" file and tell apache to include it as a footer to auto-index pages with the ReadmeName directive.  What if you wanted to hard code a message, or even display a message after a readme file has been included?</p>
<p>Need to modify the Apache source...</p>
<p><span id="more-18"></span></p>
<p>First, these directives will disable the display of version numbers &amp; installed modules:</p>
<p><a href="http://httpd.apache.org/docs/2.2/mod/core.html#serversignature">ServerSignature</a> Off</p>
<p><a href="http://httpd.apache.org/docs/2.2/mod/core.html#servertokens">ServerTokens</a> Prod</p>
<p>Now to customize the sig, there is a function called ap_psignature, this is what generates the ServerSignature default footer.  I never intended to use the "Email" feature of the signature, so I removed it.</p>
<p>This is what the function looks like after I've modified it to include a link to the homepage of the site (source version 2.2.10), change to suit.<br />
Open: server/core.c<br />
Look for: ap_psignature</p>
<pre class="c">AP_DECLARE<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> *<span style="color: #66cc66;">&#41;</span> ap_psignature<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> *prefix, request_rec *r<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #993333;">char</span> sport<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#93;</span>;
    core_dir_config *conf;
&nbsp;
    conf = <span style="color: #66cc66;">&#40;</span>core_dir_config *<span style="color: #66cc66;">&#41;</span>ap_get_module_config<span style="color: #66cc66;">&#40;</span>r-&amp;gt;per_dir_config,
                                                   &amp;amp;core_module<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>conf-&amp;gt;server_signature == srv_sig_off<span style="color: #66cc66;">&#41;</span>
            || <span style="color: #66cc66;">&#40;</span>conf-&amp;gt;server_signature == srv_sig_unset<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    apr_snprintf<span style="color: #66cc66;">&#40;</span>sport, <span style="color: #993333;">sizeof</span> sport, <span style="color: #ff0000;">&quot;%u&quot;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">unsigned</span><span style="color: #66cc66;">&#41;</span> ap_get_server_port<span style="color: #66cc66;">&#40;</span>r<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> apr_pstrcat<span style="color: #66cc66;">&#40;</span>r-&amp;gt;pool, prefix, <span style="color: #ff0000;">&quot; Visit our homepage: &lt;a href=&quot;</span>&amp;quot;,
                       ap_escape_html<span style="color: #66cc66;">&#40;</span>r-&amp;gt;pool, ap_get_server_name<span style="color: #66cc66;">&#40;</span>r<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
                       &amp;quot;\&amp;quot;&amp;gt;&amp;quot; , ap_escape_html<span style="color: #66cc66;">&#40;</span>r-&amp;gt;pool, ap_get_server_name<span style="color: #66cc66;">&#40;</span>r<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> , &amp;quot;&amp;lt;/a&amp;gt;&amp;quot;,
                       &amp;quot;\n&amp;quot;, <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>To always display the default footer, even when a Readme file is used (it will appear below the readme block).<br />
Open: modules/generators/mod_autoindex.c<br />
Find this and comment it out:</p>
<pre class="c">&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!suppress_sig<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
ap_rputs<span style="color: #66cc66;">&#40;</span>ap_psignature<span style="color: #66cc66;">&#40;</span>&amp;quot;&amp;quot;, r<span style="color: #66cc66;">&#41;</span>, r<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now you want to build the application as you normally would when compiling from source.  Start it up after the install and you should see your message appear on generated autoindex pages!  Changing the source is never a first option, but luckily Apache is open source and easy to modify!</p>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/security/creating-a-custom-serversignature-in-apache/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Removing duplicate functions from a (javascript) source file</title>
		<link>http://benajnim.com/index.php/php/removing-duplication-functions-from-a-javascript-source-file/</link>
		<comments>http://benajnim.com/index.php/php/removing-duplication-functions-from-a-javascript-source-file/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 00:25:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://benajnim.com/index.php/php/removing-duplication-functions-from-a-javascript-source-file/</guid>
		<description><![CDATA[The impetus for this solution was to eliminate the duplication of functions a javascript source file.  The concepts could be applied with little modification to any language with c-like syntax.   I chose to implement in PHP because that's what I happen to be working with these days though I do make use of a [...]]]></description>
			<content:encoded><![CDATA[<p>The impetus for this solution was to eliminate the duplication of functions a javascript source file.  The concepts could be applied with little modification to any language with c-like syntax.   I chose to implement in PHP because that's what I happen to be working with these days though I do make use of a grep trick which means it would be easiest to run this on some type of posix system with grep &amp; php.<br />
<span id="more-12"></span><br />
A project I've been working on had a vexing issue of a bunch of duplicate function declarations in one of its source files.  My guess is it is attributed to pasting the content of the file.  Unfortunately, the file had undergone many changes since the issue was first discovered.  If a function was updated that had two copies of it, either the top or bottom block would have the other declaration manually removed.  This made it impossible to just remove a single chunk representing the first or second set of changes because that could potentially remove a vital function breaking the whole app!</p>
<p>So without further ado:</p>
<pre class="php">&nbsp;
<span style="color: #0000ff;">$file</span> = <span style="color: #ff0000;">&quot;source.js&quot;</span>;
<span style="color: #0000ff;">$grep</span> = <a rel="nofollow" href="http://www.php.net/system"><span style="color: #000066;">system</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;grep -hEo <span style="color: #000099; font-weight: bold;">\&quot;</span>^<span style="color: #000099; font-weight: bold;">\s</span>*function <span style="color: #000099; font-weight: bold;">\w</span>+<span style="color: #000099; font-weight: bold;">\&quot;</span> $file | sort | uniq -d&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$functions</span> = <a rel="nofollow" href="http://www.php.net/split"><span style="color: #000066;">split</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #0000ff;">$grep</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// I'd recommend taking a look at the functions to ensure $functions seems consistent with your data &amp;amp; goals</span>
&nbsp;
<span style="color: #0000ff;">$blob</span> = <a rel="nofollow" href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$functions</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$function</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$start</span> = <a rel="nofollow" href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$javascript</span>, <span style="color: #0000ff;">$function</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// loop through each char until we get to the close bracket</span>
    <span style="color: #0000ff;">$stack</span> = <a rel="nofollow" href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #0000ff;">$start</span>, <span style="color: #0000ff;">$j</span> = <a rel="nofollow" href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$javascript</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #0000ff;">$i</span> &lt; <span style="color: #0000ff;">$j</span>; ++<span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$letter</span> = <a rel="nofollow" href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$javascript</span>, <span style="color: #0000ff;">$i</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$letter</span> == <span style="color: #ff0000;">&quot;{&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$stack</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;true&quot;</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$letter</span> == <span style="color: #ff0000;">&quot;}&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <a rel="nofollow" href="http://www.php.net/array_shift"><span style="color: #000066;">array_shift</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stack</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a rel="nofollow" href="http://www.php.net/count"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$stack</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #0000ff;">$lastBracket</span> = <span style="color: #0000ff;">$i</span><span style="color: #cc66cc;">+1</span>; <span style="color: #b1b100;">break</span>; <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #808080; font-style: italic;">//echo substr($javascript, $start, ($lastBracket - $start)) . &quot;\n&quot;;</span>
    <span style="color: #0000ff;">$javascript</span> = <a rel="nofollow" href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$javascript</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">$start</span><span style="color: #66cc66;">&#41;</span> . <a rel="nofollow" href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$javascript</span>, <span style="color: #0000ff;">$lastBracket</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<a rel="nofollow" href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$javascript</span>;
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/php/removing-duplication-functions-from-a-javascript-source-file/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Firefox tip of the moment</title>
		<link>http://benajnim.com/index.php/tips/firefox-tip-of-the-moment/</link>
		<comments>http://benajnim.com/index.php/tips/firefox-tip-of-the-moment/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 05:31:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://benajnim.com/index.php/tips/firefox-tip-of-the-moment/</guid>
		<description><![CDATA[When using the tabbed browsing feature of Mozilla Firefox, I run sessions with 10s of tabs.  Often times I will close a tab by mistake as I try to quickly pare down on tabs as I'm processing information.
To "undo" the closing of a tab, you can press [ctrl]+[shift]+t.  Try it out.
]]></description>
			<content:encoded><![CDATA[<p>When using the tabbed browsing feature of Mozilla Firefox, I run sessions with 10s of tabs.  Often times I will close a tab by mistake as I try to quickly pare down on tabs as I'm processing information.</p>
<p>To "undo" the closing of a tab, you can press <strong>[ctrl]+[shift]+t</strong>.  Try it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/tips/firefox-tip-of-the-moment/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Asus EEE En Route!</title>
		<link>http://benajnim.com/index.php/hardware/asus-eee-en-route/</link>
		<comments>http://benajnim.com/index.php/hardware/asus-eee-en-route/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 19:06:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">http://benajnim.com/index.php/hardware/asus-eee-en-route/</guid>
		<description><![CDATA[I have gotten to the point of needing another laptop, and I've been hearing so much about the EEE.
I decided to give it a shot.

When I received the shipment notification from the retailer (J &#38; R Electronics), I logged into the UPS tracking website to see where it was and when it was scheduled for [...]]]></description>
			<content:encoded><![CDATA[<p>I have gotten to the point of needing another laptop, and I've been hearing so much about the EEE.</p>
<p>I decided to give it a shot.</p>
<p><span id="more-10"></span><br />
When I received the shipment notification from the retailer (J &amp; R Electronics), I logged into the UPS tracking website to see where it was and when it was scheduled for delivery - Friday, January 25, 2008.</p>
<p>So I just logged in this morning to see if I could get details on ETA, and low and behold, this was one of the entries:</p>
<hr size="1" width="450" />
<table cellpadding="3">
<tr>
<td valign="top">Hodgkins, IL,  US</td>
<td valign="top">01/24/2008</td>
<td valign="top">4:00 A.M.</td>
<td valign="top">The package is delayed due to a train derailment</td>
</tr>
</table>
<hr size="1" width="450" /> I almost fell off my chair!  The updated arrival date is for next Monday.  A bummer, but not much one can do about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/hardware/asus-eee-en-route/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsing the Links in HTML Source</title>
		<link>http://benajnim.com/index.php/php/parsing-the-links-in-html-source/</link>
		<comments>http://benajnim.com/index.php/php/parsing-the-links-in-html-source/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 01:11:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Parsing]]></category>

		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://benajnim.com/index.php/regex/parsing-the-links-in-html-source/</guid>
		<description><![CDATA[I use this handy-dandy Regular Expression:
preg_match_all("/href=[\'&#124;\"]http:\/\/([a-zA-Z0-9\.\/\?\-\_\:\=]*)[\'&#124;\"]/i", $haystack, $links_array);
This fills an array ($links_array) with any URLs found in $haystack.  No more complex than that, though as you can see, regular expressions can be complex in their own right - they're powerful!
]]></description>
			<content:encoded><![CDATA[<p>I use this handy-dandy Regular Expression:</p>
<p>preg_match_all("/href=[\'|\"]http:\/\/([a-zA-Z0-9\.\/\?\-\_\:\=]*)[\'|\"]/i", $haystack, $links_array);</p>
<p>This fills an array ($links_array) with any URLs found in $haystack.  No more complex than that, though as you can see, regular expressions can be complex in their own right - they're powerful!</p>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/php/parsing-the-links-in-html-source/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Advice To Beginner Database Developers</title>
		<link>http://benajnim.com/index.php/musings/advice-to-beginner-database-developers/</link>
		<comments>http://benajnim.com/index.php/musings/advice-to-beginner-database-developers/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 00:02:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Database]]></category>

		<category><![CDATA[Musings]]></category>

		<guid isPermaLink="false">http://benajnim.com/index.php/musings/advice-to-beginner-database-developers/</guid>
		<description><![CDATA[If I were just starting out learning about databases, this is what I would do.
Make yourself a personal goal to learn as much about database concepts as you can. Don't limit yourself to MySQL or any one database system for that matter. There are a lot of peculiarities in the way certain features are implemented [...]]]></description>
			<content:encoded><![CDATA[<p>If I were just starting out learning about databases, this is what I would do.</p>
<p>Make yourself a personal goal to learn as much about database concepts as you can. Don't limit yourself to MySQL or any one database system for that matter. There are a lot of peculiarities in the way certain features are implemented in certain ways, depending on the engine that you will not find in any other database system.</p>
<p><span id="more-7"></span><br />
There is a good advantage you will have by learning about the pros &amp; cons of the available systems.  What open source engine is the best for big data sets?  Which one is the easiest to cluster with?  There are methodologies and best practices you will learn about by seeing how a particular problem is solved in one system versus another.</p>
<p>Definitely familiarize yourself with ACID compliant database and the SQL standard.  MySQL is always a popular choice for applications, especially web applications.  Unfortunately there are a lot of short-comings and outright hacks you have to do to accomplish certain things.  Date manipulation in MySQL has been notoriously clumsy.  Other database engines allow you to do arithmetic on date values as easily as writing an algebraic forumula.  MySQL took the approach of using arcane functions such as date_add and date_diff.  The same goes for languages.</p>
<p>Oracle DBA's make good money if a corporate career is something you seek.  Getting certifications and even a computer science degree are common ways people pursue this career path.</p>
<p>A friend of mine told me a great piece of advice during a conversation we once had about marketable business skills.  Investing in SQL is the best shot in the arm any "knowledge worker" can do to increase the value they can add to any employer.  It is the most ubiquitous, advanced business tool for storing and reporting on business data in this age.</p>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/musings/advice-to-beginner-database-developers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Sanitizing User Input in PHP: Bobby Tables Strikes Again!</title>
		<link>http://benajnim.com/index.php/php/sanitizing-user-input-in-php/</link>
		<comments>http://benajnim.com/index.php/php/sanitizing-user-input-in-php/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 20:45:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Security]]></category>

		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://benajnim.com/index.php/php/sanitizing-user-input-in-php/</guid>
		<description><![CDATA[This is a solved problem, yet the volume of applications that are exposed to vulnerabilities due to improperly filtered input is staggering.
 What is the problem?
Given certain input, an application will interpret the input as executable code (either executable in SQL or the language of the application by using input in an "eval" block).  [...]]]></description>
			<content:encoded><![CDATA[<p>This is a solved problem, yet the volume of applications that are exposed to vulnerabilities due to improperly filtered input is staggering.</p>
<p><span id="more-5"></span> <strong>What is the problem?</strong></p>
<p>Given certain input, an application will interpret the input as executable code (either executable in SQL or the language of the application by using input in an "eval" block).  There have been numerous high-profile examples of these attacks being used against web sites.  The infamous RIAA's website just suffered from one such <a href="http://reddit.com/info/660oo/comments/">attack</a>.</p>
<p><strong>What does an attack of this vulnerability look like?</strong></p>
<p>An attack might best be described by this short cartoon:</p>
<p><img src="http://imgs.xkcd.com/comics/exploits_of_a_mom.png" height="205" width="666" /></p>
<p><strong>What is the best practice for sanitizing input?</strong></p>
<p>This is a simplified example of how I do it, in PHP.  PHP implements Perl-Compatible Regular Expressions, and any language implementing regular expression syntax (for instance, Perl) in this manner should have a similar snippet of code that effectively does the same thing.</p>
<p>$cleanvar = ereg_replace('[^a-zA-Z0-9]', '', $_POST['var']); // will only allow upper &amp; lower alphas and integers</p>
]]></content:encoded>
			<wfw:commentRss>http://benajnim.com/index.php/php/sanitizing-user-input-in-php/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
