<?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>R. Kris Hardy &#187; php</title>
	<atom:link href="http://www.rkrishardy.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rkrishardy.com</link>
	<description>Software Development, Web Applications and Business Analytics</description>
	<lastBuildDate>Tue, 19 Jul 2011 17:58:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Terry Chay &#8211; 1500 Lines of Code</title>
		<link>http://www.rkrishardy.com/2010/01/terry-chay-1500-lines-of-code/</link>
		<comments>http://www.rkrishardy.com/2010/01/terry-chay-1500-lines-of-code/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 02:34:12 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[software architecture]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.rkrishardy.com/?p=233</guid>
		<description><![CDATA[Here is an outstanding article on web development philosophy that really got me thinking today, written by Terry Chay of Wordpress.  It's long, but well worth the read.]]></description>
			<content:encoded><![CDATA[<p>Here is an outstanding article on web development philosophy that really got me thinking today, written by Terry Chay of WordPress.  It&#8217;s long, but well worth the read.</p>
<p><a href="http://terrychay.com/article/1500-lines-of-code.shtml">1500 Lines of Code</a></p>
<p>This really got me thinking, and I&#8217;ll probably work on a comment to it this week.</p>
<div class="diggthis_container">
<script type="text/javascript">
digg_url = 'http://www.rkrishardy.com/2010/01/terry-chay-1500-lines-of-code/';
digg_title = 'Terry Chay - 1500 Lines of Code';
digg_bodytext = 'Here is an outstanding article on web development philosophy that really got me thinking today, written by Terry Chay of WordPress.  It\'s long, but well worth the read....';
digg_skin = 'compact';
digg_window = 'new';
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div> 
<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/c%2B%2B' rel='tag' target='_self'>c++</a>, <a class='technorati-link' href='http://technorati.com/tag/java' rel='tag' target='_self'>java</a>, <a class='technorati-link' href='http://technorati.com/tag/php' rel='tag' target='_self'>php</a>, <a class='technorati-link' href='http://technorati.com/tag/software+architecture' rel='tag' target='_self'>software architecture</a>, <a class='technorati-link' href='http://technorati.com/tag/software+development' rel='tag' target='_self'>software development</a>, <a class='technorati-link' href='http://technorati.com/tag/web+development' rel='tag' target='_self'>web development</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.rkrishardy.com/2010/01/terry-chay-1500-lines-of-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Permission Denied (13) When Opening Socket in PHP &amp; Apache</title>
		<link>http://www.rkrishardy.com/2009/12/permission-denied-13-when-opening-socket-in-php-apache/</link>
		<comments>http://www.rkrishardy.com/2009/12/permission-denied-13-when-opening-socket-in-php-apache/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 14:56:53 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[permission denied]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[selinux]]></category>
		<category><![CDATA[stream_socket_client]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend_mail]]></category>

		<guid isPermaLink="false">http://www.rkrishardy.com/?p=212</guid>
		<description><![CDATA[This post covers two cases that I&#8217;ve run into that cause Permission Denied (13) errors when opening sockets in PHP. Situation #1:  SELinux denies httpd from opening socket I ran into this simple, but annoying, problem after I migrated my development workstation to Fedora 12. Problem: A large PHP application that I have developed at [...]]]></description>
			<content:encoded><![CDATA[<p>This post covers two cases that I&#8217;ve run into that cause Permission Denied (13) errors when opening sockets in PHP.</p>
<h1>Situation #1:  SELinux denies httpd from opening socket</h1>
<p>I ran into this simple, but annoying, problem after I migrated my development workstation to Fedora 12.</p>
<h2>Problem:</h2>
<p>A large PHP application that I have developed at <a href="http://www.submergedsolutions.com">Submerged Solutions</a> (<a href="http://www.submergedsolutions.com">SandPiper Accounting</a>) began throwing <strong>Permission Denied (13)</strong> system exceptions when attempting to send mail through<a href="http://www.zend.com"> Zend Framework&#8217;s</a> Zend_Mail library.</p>
<p>All the phpunit unit tests worked fine and could send e-mail, but would fail when the usability tests started and any HTTP requests that sent e-mail were handled through Apache.</p>
<p>The Apache instance was being run as user apache / group apache, and php (mod_php) is run as user apache / group apache.</p>
<p>The exception occurred in Zend_Mail_Protocol_Abstract-&gt;_connect(), immediately following the socket opening call &#8220;stream_socket_client(&#8230;)&#8221;.</p>
<h3>File: Zend/Mail/Protocol/Abstract.php; Line 224</h3>
<p><code> 50:    abstract class Zend_Mail_Protocol_Abstract<br />
51:    {<br />
...<br />
218:       protected function _connect($remote)<br />
219:       {<br />
220:           $errorNum = 0;<br />
221:           $errorStr = '';<br />
222:<br />
223:           // open connection<br />
224:           $this-&gt;_socket = @<strong>stream_socket_client</strong>($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);<br />
225:           ...<br />
</code></p>
<p>fopen() calls using http and ftp protocols also failed:</p>
<p style="padding-left: 30px;"><em>Warning</em>: fopen(&#8230;) [function.fopen]: failed to open stream: Permission denied in &#8230;</p>
<h2>The fix:</h2>
<p>The problem turned out to be the &#8220;<strong>httpd_can_network_connect</strong>&#8221; SELinux setting that is on by default in Fedora 12.</p>
<p>In a shell console, run as <strong>root</strong>:</p>
<p><code># /usr/sbin/setsebool httpd_can_network_connect=1<br />
</code></p>
<p><a href="http://www.php.net/manual/en/function.fopen.php#56551">Thanks to durwood, who pointed this out on PHP.net.</a></p>
<p><a href="http://bugzilla.redhat.com/show_bug.cgi?id=164700">&#8220;Bug&#8221; Report at RedHat.com.</a></p>
<p><a href="http://fedoraproject.org/wiki/SELinux">More info on SELinux.</a></p>
<h1>Situation #2: PHP forbids opening socket to 255.255.255.255</h1>
<p>A reader of this blog brought a problem of his to me.  I had never seen it before, so it was definitely interesting.</p>
<h2>Problem:</h2>
<p>This reader was using <a href="http://www.phpcs.com/codes/WAKE-ON-LAN-WAN-REVEILLER-MACHINE-SUR-INTERNET_48248.aspx">this PHP script</a> that he had found to do <a title="Wake-on-LAN" href="http://en.wikipedia.org/wiki/Wake-on-LAN">Wake-on-LAN</a> pings on his local network.  The script worked fine in Windows, but failed on his Fedora 10 server.  The error he received was Permission Denied (13).</p>
<p>His WoL packets were sent via udp to the broadcast address 255.255.255.255.  This worked fine in Windows, but failed in Linux.</p>
<p>His server&#8217;s PHP installation had socket support enabled, and udp was a registered stream socket transport.</p>
<p>His SELinux was disabled, so Situation #1 did not apply to him.</p>
<p>This is probably distribution specific.  I&#8217;m running Fedora 12, and had no such issues, whereas the person facing this problem was running Fedora 10.</p>
<h2>Solution:</h2>
<p>Either PHP or the the user running the PHP instance (&#8220;apache&#8221; in this case), was being forbidden from opening sockets to 255.255.255.255.  It turns out this is somewhat common.  Even when running the script as &#8220;root&#8221;, you can still get permission denied errors.</p>
<p>I came upon this short comment on php.net about someone else getting <a href="http://www.php.net/manual/en/ref.sockets.php#80397">permission denied errors on socket_connect() calls.</a></p>
<p>There was also this comment which showed an <a href="http://www.php.net/manual/en/ref.sockets.php#73162">easy way to get the broadcast address for the computer&#8217;s network interface.</a> This method seems to work, however, it is limited to Linux since it relies upon the following gnu utilities: <a href="http://en.wikipedia.org/wiki/Ifconfig">ifconfig</a>, <a href="http://en.wikipedia.org/wiki/Grep">grep</a> and <a href="http://en.wikipedia.org/wiki/Cut_%28Unix%29">cut</a>.  It may work if you compile Windows ports to these utilities, or use <a href="http://www.cygwin.com/">cygwin</a>.  (Note: The code snippet at PHP.net has errors.  A revised script is pasted below.)</p>
<p>Here&#8217;s the way to get the broadcast address:</p>
<pre>exec("ifconfig | grep Bcast | cut -d \":\" -f 3 | cut -d \" \" -f 1",$addr);
$addr=array_flip(array_flip($addr));
</pre>
<p>By getting the broadcast address of the network interface, you can send Wake-on-LAN magic packets to that address rather than to 255.255.255.255.  Doing this, the sockets can be connected successfully, and the permission denied errors were resolved.</p>
<p>Here&#8217;s the &#8220;fixed&#8221; code from PHP.net.  It hasn&#8217;t been tested, so you very well may need to modify it for your needs.</p>
<pre>
&lt;?php
/**
 * Wake-on-LAN
 *
 * @return boolean
 *   TRUE:    Socked was created successfully and the message has been sent.
 *   FALSE:   Something went wrong
 *
 * @param string|array  $mac   You will WAKE-UP this WOL-enabled computer, you
 *                             need to add the MAC-address here. Mac can be
 *                             array too.
 *
 * @param string|array  $addr  You will send and broadcast to this address.
 *                             Normally you need to use the 255.255.255.255
 *                             address, so I made it as the default. You don't need to do anything with this.
 *
 *                             If you get permission denied errors when using
 *                             255.255.255.255 have permission denied problems
 *                             you can set $addr = false to get the broadcast
 *                             address from the network interface using the
 *                             ifconfig command.
 *
 *                             $addr can be array with broadcast IP values
 *
 * Example 1:
 *   When the message has been sent you will see the message "Done...."
 *   if ( wake_on_lan('00:00:00:00:00:00'))
 *      echo 'Done...';
 *   else
 *      echo 'Error while sending';
 */

function wake_on_lan($mac, $addr=false, $port=7) {
    if ($addr === false){
        exec("ifconfig | grep Bcast | cut -d \":\" -f 3 | cut -d \" \" -f 1",$addr);
        $addr=array_flip(array_flip($addr));
    }
    if(is_array($addr)){
        $last_ret = false;
        for ($i = 0; $i < count($addr); $i++)
            if ($addr[$i] !== false) {
                $last_ret = wake_on_lan($mac, $addr[$i], $port);
            }
        return $last_ret;
    }
    if (is_array($mac)){
        $ret = array();
        foreach($mac as $k =&lt; $v)
            $ret[$k] = wake_on_lan($v, $addr, $port);
        return $ret;
    }
    //Check if it's an real MAC-address and split it into an array
    $mac = strtoupper($mac);
    if (!preg_match("/([A-F0-9]{1,2}[-:]){5}[A-F0-9]{1,2}/", $mac, $maccheck))
        return false;
    $addr_byte = preg_split("/[-:]/", $maccheck[0]);

    //Creating hardware adress
    $hw_addr = '';
    for ($a = 0; $a < 6; $a++)//Changing mac adres from HEXEDECIMAL to DECIMAL
        $hw_addr .= chr(hexdec($addr_byte[$a]));

    //Create package data
    $msg = str_repeat(chr(255),6);
    for ($a = 1; $a <= 16; $a++)
        $msg .= $hw_addr;
    //Sending data
    if (function_exists('socket_create')){
        //socket_create exists
        $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);    //Can create the socket
        if ($sock){
            $sock_data = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); //Set
            if ($sock_data){
                $sock_data = socket_sendto($sock, $msg, strlen($msg), 0, $addr,$port); //Send data
                if ($sock_data){
                    socket_close($sock); //Close socket
                    unset($sock);
                    return true;
                }
            }
        }
        @socket_close($sock);
        unset($sock);
    }
    $sock=fsockopen("udp://" . $addr, $port);
    if($sock){
        $ret=fwrite($sock,$msg);
        fclose($sock);
    }
    if($ret)
        return true;
    return false;
}

if (@wake_on_lan('00:00:00:00:00:00')) {
    echo 'Done...';
} else {
    echo 'Error while sending';
}
?&gt;
</pre>
<div class="diggthis_container">
<script type="text/javascript">
digg_url = 'http://www.rkrishardy.com/2009/12/permission-denied-13-when-opening-socket-in-php-apache/';
digg_title = 'Permission Denied (13) When Opening Socket in PHP &amp; Apache';
digg_bodytext = 'This post covers two cases that I\'ve run into that cause Permission Denied (13) errors when opening sockets in PHP.\n&lt;h1&gt;Situation #1:  SELinux denies httpd from opening socket&lt;/h1&gt;\nI ran into this ...';
digg_skin = 'compact';
digg_window = 'new';
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div> 
<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/apache' rel='tag' target='_self'>apache</a>, <a class='technorati-link' href='http://technorati.com/tag/Debugging' rel='tag' target='_self'>Debugging</a>, <a class='technorati-link' href='http://technorati.com/tag/permission+denied' rel='tag' target='_self'>permission denied</a>, <a class='technorati-link' href='http://technorati.com/tag/php' rel='tag' target='_self'>php</a>, <a class='technorati-link' href='http://technorati.com/tag/selinux' rel='tag' target='_self'>selinux</a>, <a class='technorati-link' href='http://technorati.com/tag/stream_socket_client' rel='tag' target='_self'>stream_socket_client</a>, <a class='technorati-link' href='http://technorati.com/tag/zend+framework' rel='tag' target='_self'>zend framework</a>, <a class='technorati-link' href='http://technorati.com/tag/zend_mail' rel='tag' target='_self'>zend_mail</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.rkrishardy.com/2009/12/permission-denied-13-when-opening-socket-in-php-apache/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MIT/Stanford/Syracuse Team Develop New PHP Intepreter-Based XSS and SQL Security Tester</title>
		<link>http://www.rkrishardy.com/2009/06/new-php-interpreter-based-xss-and-sql-security-tester/</link>
		<comments>http://www.rkrishardy.com/2009/06/new-php-interpreter-based-xss-and-sql-security-tester/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 10:55:18 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ardilla]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php security]]></category>
		<category><![CDATA[sql injection]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.rkrishardy.com/?p=181</guid>
		<description><![CDATA[A group of researchers from MIT, Stanford and Syracuse have developed a new program, named &#8220;Ardilla&#8221;, which can analyze PHP code for Cross-Site Scripting (XSS) and SQL injection attack vulnerabilities. Rather than just static analysis, this program actually traces the data through the software to determine whether the threat is real. This decreases false-positives significantly, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cs.washington.edu/homes/mernst/pubs/create-attacks-tr054.pdf"><img class="alignright size-full wp-image-184" title="Ardilla Paper" src="http://www.rkrishardy.com/wp-content/uploads/2009/06/2009-06-19_0812.png" alt="Ardilla Paper" width="150" height="194" /></a>A group of researchers from MIT, Stanford and Syracuse have developed a new program, named &#8220;Ardilla&#8221;, which can analyze PHP code for Cross-Site Scripting (XSS) and SQL injection attack vulnerabilities.</p>
<p>Rather than just static analysis, this program actually traces the data through the software to determine whether the threat is real.  This decreases false-positives significantly, compared to simple static analysis.</p>
<p>Here&#8217;s the technical paper for all us serious geeks&#8230;</p>
<p><span id="more-181"></span><a href="http://www.cs.washington.edu/homes/mernst/pubs/create-attacks-tr054.pdf">Automatic Creation of SQL Injection and Cross Site Scripting Attacks</a></p>
<p>A summary of the results is available here, along with the specifications for the XSS and SQL Injection attacks they used in the tests:</p>
<p><a href="http://groups.csail.mit.edu/pag/ardilla/">Ardilla Results</a></p>
<p>The tool was built from a modified version of the Zend Interpreter, based on licensed work done at IBM.  Unfortuantely, due to the license issues, it cannot be released as open source.  This would be a great tool in the arsenal of professional developers (especially those that inherit and refactor code from other developers).  Hopefully it  or an open-source version will find its way into the wild somehow.</p>
<p>The team is looking for help making the changes to the Zend Interpreter code so that the program can be released as Open Source.  Interested?  Talk to <a href="http://www.cs.washington.edu/homes/mernst/">Michael Earnst</a>.</p>
<p>(Via <a href="http://www.darkreading.com/security/app-security/showArticle.jhtml?articleID=218100143">DarkReading</a>)</p>
<div class="diggthis_container">
<script type="text/javascript">
digg_url = 'http://www.rkrishardy.com/2009/06/new-php-interpreter-based-xss-and-sql-security-tester/';
digg_title = 'MIT/Stanford/Syracuse Team Develop New PHP Intepreter-Based XSS and SQL Security Tester';
digg_bodytext = '&lt;a href=&quot;http://www.cs.washington.edu/homes/mernst/pubs/create-attacks-tr054.pdf&quot;&gt;&lt;img class=&quot;alignright size-full wp-image-184&quot; title=&quot;Ardilla Paper&quot; src=&quot;http://www.rkrishardy.com/wp-content/uploads...';
digg_skin = 'compact';
digg_window = 'new';
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div> 
<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/ardilla' rel='tag' target='_self'>ardilla</a>, <a class='technorati-link' href='http://technorati.com/tag/php' rel='tag' target='_self'>php</a>, <a class='technorati-link' href='http://technorati.com/tag/php+security' rel='tag' target='_self'>php security</a>, <a class='technorati-link' href='http://technorati.com/tag/sql+injection' rel='tag' target='_self'>sql injection</a>, <a class='technorati-link' href='http://technorati.com/tag/xss' rel='tag' target='_self'>xss</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.rkrishardy.com/2009/06/new-php-interpreter-based-xss-and-sql-security-tester/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

