<?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>Untitled &#187; php</title>
	<atom:link href="http://ammonlauritzen.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://ammonlauritzen.com/blog</link>
	<description>and still for good reason.</description>
	<lastBuildDate>Tue, 13 Dec 2011 21:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>dead simple traditional style rotation</title>
		<link>http://ammonlauritzen.com/blog/2010/02/05/dead-simple-traditional-style-rotation/</link>
		<comments>http://ammonlauritzen.com/blog/2010/02/05/dead-simple-traditional-style-rotation/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 17:51:26 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[logfiles]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=1105</guid>
		<description><![CDATA[In response to my two-step rotation post earlier this week, I figure I may as well share the logic I use for a more traditional logfile rotation scheme. I think this is as simple as I can possibly make it: &#38;lt;? define&#40; 'MAX_COPIES', 3 &#41;; $back_fname = &#38;quot;/path/to/log/file/abc.log&#38;quot;; function trace&#40; $msg &#41; &#123; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>In response to <a href='http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/'>my two-step rotation post</a> earlier this week, I figure I may as well share the logic I use for a more traditional logfile rotation scheme.</p>
<p>I think this is as simple as I can possibly make it:</p>
<div class="syntax_hilite">
<div id="php-3">
<div class="php">&amp;lt;?<br />
<a href="http://www.php.net/define"><span style="color:#000066;">define</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">'MAX_COPIES'</span>, <span style="color:#CC66CC;">3</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$back_fname</span> = &amp;quot;/path/to/<a href="http://www.php.net/log"><span style="color:#000066;">log</span></a>/<a href="http://www.php.net/file"><span style="color:#000066;">file</span></a>/abc.log&amp;quot;;</p>
<p><span style="color:#000000; font-weight:bold;">function</span> trace<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> &amp;quot;- <span style="color:#0000FF;">$msg</span>\n&amp;quot;;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><a href="http://www.php.net/exec"><span style="color:#000066;">exec</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;ls -r $<span style="color:#006600; font-weight:bold;">&#123;</span>back_fname<span style="color:#006600; font-weight:bold;">&#125;</span>*&amp;quot;, <span style="color:#0000FF;">$copies</span>, <span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span>&amp;gt;= MAX_COPIES <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$fname</span> = <a href="http://www.php.net/array_shift"><span style="color:#000066;">array_shift</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;deleting &amp;quot;.<span style="color:#0000FF;">$fname</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#0000FF;">$next</span> = <a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fname</span> = <a href="http://www.php.net/array_shift"><span style="color:#000066;">array_shift</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; --<span style="color:#0000FF;">$next</span>;<br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;rotating <span style="color:#0000FF;">$fname</span> -&amp;gt; <span style="color:#0000FF;">$next</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/rename"><span style="color:#000066;">rename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fname</span>, &amp;quot;<span style="color:#0000FF;">$back_fname</span>.<span style="color:#0000FF;">$next</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;creating <span style="color:#0000FF;">$back_fname</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<a href="http://www.php.net/touch"><span style="color:#000066;">touch</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$back_fname</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
?&amp;gt;</div>
</div>
</div>
<p></p>
<p>A sample series of executions might look like this:</p>
<div class="syntax_hilite">
<div id="code-4">
<div class="code">ammon@wernstrom:/path/to/log/file$ touch abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">0</span> -&amp;gt; <span style="color:#800000;">1</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">1</span> -&amp;gt; <span style="color:#800000;">2</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">0</span> -&amp;gt; <span style="color:#800000;">1</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- deleting /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">2</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">1</span> -&amp;gt; <span style="color:#800000;">2</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">0</span> -&amp;gt; <span style="color:#800000;">1</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span></div>
</div>
</div>
<p></p>
<p>This doesn't have any failsafes, doesn't compress anything, depends on an external call to 'ls', and it actually deletes old files in stead of overwriting them... but it is the shortest, simplest method I've come up with to get the job done.</p>
<p>If I feel like making this a full-fledged series, I might actually post a more thorough implementation later <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2010/02/05/dead-simple-traditional-style-rotation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>simple two-step logfile rotation</title>
		<link>http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/</link>
		<comments>http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 19:23:08 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[logfiles]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=1103</guid>
		<description><![CDATA[This is the result of 10 minutes of pounding on the keyboard after yet another disappointing experience with trying to get logrotate to do something vaguely more flexible. This simple script scans all normal files in a log directory, and if they are older than a certain cutoff, moves them into a holding directory for [...]]]></description>
			<content:encoded><![CDATA[<p>This is the result of 10 minutes of pounding on the keyboard after yet another disappointing experience with trying to get logrotate to do something vaguely more flexible.</p>
<p>This simple script scans all normal files in a log directory, and if they are older than a certain cutoff, moves them into a holding directory for old logs. Future passes will check files in the old directory for another age setting and will delete them. That's all there is to it.</p>
<p>Configure your cutoffs, directories of interest, and optionally plug in a better logging mechanism and you're set. (Oh, and change the #! if necessary, of course).</p>
<div class="syntax_hilite">
<div id="php-6">
<div class="php"><span style="color:#008000; font-style:italic;">#!/usr/bin/php</span><br />
&amp;lt;?<br />
<span style="color:#0000FF;">$cutoff_rotate</span> = &amp;quot;<span style="color:#CC66CC;">3</span> days&amp;quot;;<br />
<span style="color:#0000FF;">$cutoff_delete</span> = &amp;quot;<span style="color:#CC66CC;">7</span> days&amp;quot;;<br />
<span style="color:#0000FF;">$dir_log</span> = &amp;quot;/logs&amp;quot;;<br />
<span style="color:#0000FF;">$dir_old</span> = &amp;quot;/logs.old&amp;quot;;</p>
<p><span style="color:#000000; font-weight:bold;">function</span> trace<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$debug</span> = <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// appropriate logging mechanism can be plugged in here</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> &amp;quot;<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#0000FF;">$msg</span>\n&amp;quot;;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// scan old files for deletion</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir_old</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$dh</span> = <a href="http://www.php.net/opendir"><span style="color:#000066;">opendir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_old</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chdir"><span style="color:#000066;">chdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_old</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;scanning <span style="color:#0000FF;">$dir_old</span> <span style="color:#616100;">for</span> logs more than <span style="color:#0000FF;">$cutoff_delete</span> old&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$cutoff</span> = <a href="http://www.php.net/strtotime"><span style="color:#000066;">strtotime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;-<span style="color:#0000FF;">$cutoff_delete</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;cutoff is &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$cutoff</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span> = <a href="http://www.php.net/readdir"><span style="color:#000066;">readdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dh</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;skipping <span style="color:#0000FF;">$file</span>&amp;quot;, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$ts</span> = <a href="http://www.php.net/filemtime"><span style="color:#000066;">filemtime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$ts</span> &amp;lt;<span style="color:#0000FF;">$cutoff</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;deleting <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/unlink"><span style="color:#000066;">unlink</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;failed to <a href="http://www.php.net/unlink"><span style="color:#000066;">unlink</span></a> <span style="color:#0000FF;">$file</span>!&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;ignoring <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/closedir"><span style="color:#000066;">closedir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;no old <a href="http://www.php.net/log"><span style="color:#000066;">log</span></a> <a href="http://www.php.net/dir"><span style="color:#000066;">dir</span></a> <span style="color:#0000FF;">$dir_old</span> to scan yet&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// scan current files for rotation</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir_old</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;creating <span style="color:#0000FF;">$dir_old</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/mkdir"><span style="color:#000066;">mkdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_old</span>, <span style="color:#CC66CC;">0775</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;mkdir failed, aborting rotation&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir_log</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$dh</span> = <a href="http://www.php.net/opendir"><span style="color:#000066;">opendir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_log</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chdir"><span style="color:#000066;">chdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_log</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;scanning <span style="color:#0000FF;">$dir_log</span> <span style="color:#616100;">for</span> logs more than <span style="color:#0000FF;">$cutoff_rotate</span> old&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$cutoff</span> = <a href="http://www.php.net/strtotime"><span style="color:#000066;">strtotime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;-<span style="color:#0000FF;">$cutoff_delete</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;cutoff is &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$cutoff</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span> = <a href="http://www.php.net/readdir"><span style="color:#000066;">readdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dh</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;skipping <span style="color:#0000FF;">$file</span>&amp;quot;, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$ts</span> = <a href="http://www.php.net/filemtime"><span style="color:#000066;">filemtime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$ts</span> &amp;lt;<span style="color:#0000FF;">$cutoff</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;rotating <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/rename"><span style="color:#000066;">rename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$file</span>, <span style="color:#0000FF;">$dir_old</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;failed to rotate <span style="color:#0000FF;">$file</span>!&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;ignoring <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/closedir"><span style="color:#000066;">closedir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
?&amp;gt;</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>lazy php profiler</title>
		<link>http://ammonlauritzen.com/blog/2010/01/12/lazy-php-profiler/</link>
		<comments>http://ammonlauritzen.com/blog/2010/01/12/lazy-php-profiler/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 01:06:56 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=1096</guid>
		<description><![CDATA[Caveman profiling with a side of "where were you at 9pm on the night in question?" As always, season to taste. &#38;lt;? $_profile_log = &#38;quot;/tmp/php-profile.log&#38;quot;; function _profile&#40;&#41; &#123; &#160; &#160; static $fh; &#160; &#160; if&#40; !isset&#40;$fh&#41; &#41; &#123; &#160; &#160; &#160; &#160; global $_profile_log; &#160; &#160; &#160; &#160; if&#40; !file_exists&#40;$_profile_log&#41; &#41; &#123; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Caveman profiling with a side of "where were you at 9pm on the night in question?" As always, season to taste.</p>
<div class="syntax_hilite">
<div id="php-10">
<div class="php">&amp;lt;?<br />
<span style="color:#0000FF;">$_profile_log</span> = &amp;quot;/tmp/php-profile.log&amp;quot;;</p>
<p><span style="color:#000000; font-weight:bold;">function</span> _profile<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/static"><span style="color:#000066;">static</span></a> <span style="color:#0000FF;">$fh</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fh</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$_profile_log</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_profile_log</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/touch"><span style="color:#000066;">touch</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$_profile_log</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/chmod"><span style="color:#000066;">chmod</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$_profile_log</span>, <span style="color:#CC66CC;">0664</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$fh</span> = @<a href="http://www.php.net/fopen"><span style="color:#000066;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$_profile_log</span>, &amp;quot;a&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$fh</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;</p>
<p>&nbsp; &nbsp; <span style="color:#0000FF;">$stack</span> = <a href="http://www.php.net/debug_backtrace"><span style="color:#000066;">debug_backtrace</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$stack</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$base</span> = <span style="color:#0000FF;">$stack</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$base</span> = <span style="color:#0000FF;">$stack</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$buf</span> = <span style="color:#0000FF;">$base</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'file'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.&amp;quot;:&amp;quot;.<span style="color:#0000FF;">$base</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'line'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.&amp;quot;, &amp;quot;;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$base</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'class'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$buf</span> .= <span style="color:#0000FF;">$base</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'class'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF;">$base</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'type'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$buf</span> .= <span style="color:#0000FF;">$base</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'function'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</p>
<p>&nbsp; &nbsp; <span style="color:#0000FF;">$buf</span> = <a href="http://www.php.net/sprintf"><span style="color:#000066;">sprintf</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;quot;<span style="color:#006600; font-weight:bold;">&#91;</span>%s<span style="color:#006600; font-weight:bold;">&#93;</span> %s\n&amp;quot;,<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;quot;H:i:s&amp;quot;<span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#0000FF;">$buf</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">return</span> @<a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fh</span>, <span style="color:#0000FF;">$buf</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
?&amp;gt;</div>
</div>
</div>
<p>
<span id="more-1096"></span><br />
Sample use:</p>
<div class="syntax_hilite">
<div id="php-11">
<div class="php">&amp;lt;?<br />
<span style="color:#616100;">require_once</span> &amp;quot;profiler.php&amp;quot;;</p>
<p><span style="color:#FF9933; font-style:italic;">// setup</span><br />
<span style="color:#000000; font-weight:bold;">function</span> func<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; _profile<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#000000; font-weight:bold;">class</span> obj <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _profile<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> other_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; func<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> indirect_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&amp;gt;method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#0000FF;">$obj</span> = <span style="color:#000000; font-weight:bold;">new</span> obj<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// actual invocation cases</span><br />
_profile<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
func<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
obj::<span style="color:#006600;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$obj</span>-&amp;gt;method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$obj</span>-&amp;gt;other_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$obj</span>-&amp;gt;indirect_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
?&amp;gt;</div>
</div>
</div>
<p></p>
<p>Output:</p>
<div class="syntax_hilite">
<div id="code-12">
<div class="code">ammon@elzar:~$ cat /tmp/php-profile.<span style="">log</span> <br />
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">19</span>:<span style="color:#800000;">00</span>:<span style="color:#800000;">21</span><span style="color:#006600; font-weight:bold;">&#93;</span> /home/ammon/test.<span style="">php</span>:<span style="color:#800000;">20</span>, _profile<br />
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">19</span>:<span style="color:#800000;">00</span>:<span style="color:#800000;">21</span><span style="color:#006600; font-weight:bold;">&#93;</span> /home/ammon/test.<span style="">php</span>:<span style="color:#800000;">21</span>, func<br />
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">19</span>:<span style="color:#800000;">00</span>:<span style="color:#800000;">21</span><span style="color:#006600; font-weight:bold;">&#93;</span> /home/ammon/test.<span style="">php</span>:<span style="color:#800000;">22</span>, obj::<span style="">method</span><br />
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">19</span>:<span style="color:#800000;">00</span>:<span style="color:#800000;">21</span><span style="color:#006600; font-weight:bold;">&#93;</span> /home/ammon/test.<span style="">php</span>:<span style="color:#800000;">24</span>, obj-&amp;gt;method<br />
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">19</span>:<span style="color:#800000;">00</span>:<span style="color:#800000;">21</span><span style="color:#006600; font-weight:bold;">&#93;</span> /home/ammon/test.<span style="">php</span>:<span style="color:#800000;">13</span>, func<br />
<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">19</span>:<span style="color:#800000;">00</span>:<span style="color:#800000;">21</span><span style="color:#006600; font-weight:bold;">&#93;</span> /home/ammon/test.<span style="">php</span>:<span style="color:#800000;">16</span>, obj-&amp;gt;method</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2010/01/12/lazy-php-profiler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>gearman 0.3 php extension api</title>
		<link>http://ammonlauritzen.com/blog/2009/05/26/gearman-03-php-extension-api/</link>
		<comments>http://ammonlauritzen.com/blog/2009/05/26/gearman-03-php-extension-api/#comments</comments>
		<pubDate>Tue, 26 May 2009 21:10:17 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[gearman]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=500</guid>
		<description><![CDATA[No real preamble to be made here. Gearman is a distributed job queuing system by the fine folks who brought us memcached. It is nicer than anything else I've looked at. I am attempting to switch one of my projects over to it (replacing a crufty curl + unix sockets + memcached monstrosity that attempted [...]]]></description>
			<content:encoded><![CDATA[<p>No real preamble to be made here. <a href='http://gearman.org'>Gearman</a> is a distributed job queuing system by the <a href='http://danga.com/'>fine folks</a> who brought us <a href='http://danga.com/memcached/'>memcached</a>. It is nicer than anything else I've looked at. I am attempting to switch one of my projects over to it (replacing a crufty curl + unix sockets + memcached monstrosity that attempted to do the same job).</p>
<p>The documentation is lacking, but if the discussion group is any indication, real docs are a high priority for the project team. Today, I visited the IRC channel to ask for a status update on docs for the PHP extension api (as opposed to the PEAR all-script api, whose auto-generated docs are broken). Turns out my suspicions were right. Documentation is a high priority and none currently exists for the api in question. However... I was informed that the classes support reflection... so <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A quick grep of the source for the extension tells me that I am looking at four classes: GearmanClient, GearmanWorker, GearmanJob, and GearmanTask. A ridiculously short php script later...</p>
<div class="syntax_hilite">
<div id="php-14">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
Reflection::<span style="color:#006600;">export</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">new</span> ReflectionClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'GearmanWorker'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
Reflection::<span style="color:#006600;">export</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">new</span> ReflectionClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'GearmanClient'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
Reflection::<span style="color:#006600;">export</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">new</span> ReflectionClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'GearmanJob'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
Reflection::<span style="color:#006600;">export</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">new</span> ReflectionClass<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'GearmanTask'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
And I can at least try to make a human readable list of available methods.</p>
<h3>GearmanWorker</h3>
<ul>
<li>__construct()</li>
<li>clone()</li>
<li>error()</li>
<li>returnCode()</li>
<li>setOptions( $option, $data )</li>
<li>addServer( $host, $port ) - both args optional, examples say defaults are localhost on port 4730.</li>
<li>addFunction( $function_name, $function, $data, $timeout ) - data and timeout optional</li>
<li>work()</li>
</ul>
<h3>GearmanClient</h3>
<ul>
<li>__construct()</li>
<li>clone()</li>
<li>error()</li>
<li>setOptions( $option, $data )</li>
<li>addServer( $host, $port ) - reflection says REQUIRED, however the provided examples and personal experience says otherwise</li>
<li>do( $function_name, $workload, $unique ) - unique is optional</li>
<li>doHigh( $function_name, $workload, $unique ) - unique is optional</li>
<li>doLow( $function_name, $workload, $unique ) - unique is optional</li>
<li>doJobHandle()</li>
<li>doStatus()</li>
<li>doBackground( $function_name, $workload, $unique ) - unique is optional</li>
<li>doHighBackground( $function_name, $workload, $unique ) - unique is optional</li>
<li>doLowBackground( $function_name, $workload, $unique ) - unique is optional</li>
<li>jobStatus( $job_handle )</li>
<li>echo( $workload )</li>
<li>addTask( $function_name, $workload, $data, $unique ) - data and unique are optional</li>
<li>addTaskHigh( $function_name, $workload, $data, $unique ) - data and unique are optional</li>
<li>addTaskLow( $function_name, $workload, $data, $unique ) - data and unique are optional</li>
<li>addTaskBackground( $function_name, $workload, $data, $unique ) - data and unique are optional</li>
<li>addTaskHighBackground( $function_name, $workload, $data, $unique ) - data and unique are optional</li>
<li>addTaskLowBackground( $function_name, $workload, $data, $unique ) - data and unique are optional</li>
<li>addTaskStatus( $job_handle, $data ) - data is optional</li>
<li>setWorkloadCallback( $callback )</li>
<li>setCreatedCallback( $callback)</li>
<li>setClientCallback( $callback)</li>
<li>setWarningCallback( $callback)</li>
<li>setStatusCallback( $callback)</li>
<li>setCompleteCallback( $callback)</li>
<li>setExceptionCallback( $callback)</li>
<li>setFailCallback( $callback)</li>
<li>clearCallbacks()</li>
<li>data()</li>
<li>setData( $data )</li>
<li>runTasks()</li>
</ul>
<h3>GearmanJob</h3>
<ul>
<li>__construct()</li>
<li>returnCode()</li>
<li>workload()</li>
<li>workloadSize()</li>
<li>warning( $warning )</li>
<li>status( $numerator, $denominator )</li>
<li>handle()</li>
<li>unique()</li>
<li>data( $data )</li>
<li>complete( $result )</li>
<li>exception( $exception )</li>
<li>fail()</li>
<li>functionName()</li>
<li>setReturn( $gearman_return_t )</li>
</ul>
<h3>GearmanTask</h3>
<ul>
<li>__construct()</li>
<li>returnCode()</li>
<li>create()</li>
<li>free()</li>
<li>function()</li>
<li>uuid()</li>
<li>jobHandle()</li>
<li>isKnown()</li>
<li>isRunning()</li>
<li>taskNumerator()</li>
<li>taskDenominator()</li>
<li>data()</li>
<li>dataSize()</li>
<li>takeData( $task_object ) - optional</li>
<li>sendData( $data )</li>
<li>recvData( $data_len )</li>
</ul>
<p>The extension also appears to expose all constants defined in the C api.</p>
<p>I have since added this to the official <a href='http://www.gearman.org/doku.php?id=php_reflection'>wiki</a> - so there are at least SOME docs on the site now <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2009/05/26/gearman-03-php-extension-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php autoload</title>
		<link>http://ammonlauritzen.com/blog/2008/10/28/php-autoload/</link>
		<comments>http://ammonlauritzen.com/blog/2008/10/28/php-autoload/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 23:06:40 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=407</guid>
		<description><![CDATA[As of version 5.0, PHP has had the ability to dynamically include required classes as needed - without requiring the developer to manually include all possible dependencies beforehand. This means that in cases where your code execution never touches 39 of the 40 classes in the project, it loads, parses, and runs that much faster. [...]]]></description>
			<content:encoded><![CDATA[<p>As of version 5.0, PHP has had the ability to dynamically include required classes as needed - without requiring the developer to manually include all possible dependencies beforehand. This means that in cases where your code execution never touches 39 of the 40 classes in the project, it loads, parses, and runs that much faster.</p>
<p>There is a performance hit for actually having to call the <a href='http://php.net/autoload'>__autoload()</a> method, but if you're in a situation where the hit for executing a few extra comparison calls is unacceptable... you probably aren't developing in PHP in the first place <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Almost all of the php I've written in the last 2-3 years uses autoloading, and it has probably saved me hundreds of hours of aggravation.</p>
<p>In most of my projects, the first line of any script or class usually looks something like this:</p>
<div class="syntax_hilite">
<div id="php-21">
<div class="php"><span style="color:#616100;">require_once</span> <span style="color:#FF0000;">"/var/www/common/lib.php"</span>;</div>
</div>
</div>
<p>
Then lib.php usually reads something like this:</p>
<div class="syntax_hilite">
<div id="php-22">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
<span style="color:#000000; font-weight:bold;">function</span> __autoload<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$class</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#616100;">include_once</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"$class.php"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p></p>
<p>And that is all that is strictly required to make the magic happen. It is fast, it is easy to understand, it is easy to use. You can use require_once() or include_once() and there is very little meaningful difference.</p>
<p>I've looked around the net and found several other attempts at improving on this simple mechanism. But they invariably overcomplicate things. They attempt to recurse source directories, cache filename->class differences to the filesystem, and otherwise turn what should be a simple filesystem operation that the php environment supports natively into a mess of exception handling and wheel reinvention.</p>
<p>There are obviously theoretical instances where you might want to have more than the one require_once/include_once line... but I've honestly never encountered one myself.</p>
<p>I mean, you could try to throw an exception if the file didn't exist or otherwise failed to load... but nothing will happen. Failure to instantiate a nonexistant class is a fatal error in PHP, and will be handled as such with or without you - preempting any attempt at throwing an exception.</p>
<p>The only thing you can add is a bit of extra diagnostics or maybe logging to a separate location.</p>
<p>Assume that we have a file 'test.php':</p>
<div class="syntax_hilite">
<div id="php-23">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
<span style="color:#616100;">require_once</span> <span style="color:#FF0000;">"autoload.php"</span>;<br />
<span style="color:#0000FF;">$frog</span> = <span style="color:#000000; font-weight:bold;">new</span> Frog<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
If autoload.php contains a simple simple autoload function that uses require_once(), and Frog.php doesn't exist anywhere in your include path, the results will look something like this:</p>
<div class="syntax_hilite">
<div id="code-24">
<div class="code">ammon@kif:~$ php test.<span style="">php</span> </p>
<p>Warning: require_once<span style="color:#006600; font-weight:bold;">&#40;</span>Frog.<span style="">php</span><span style="color:#006600; font-weight:bold;">&#41;</span>: failed to open stream: No such file or directory in /home/ammon/autoload.<span style="">php</span> on line <span style="color:#800000;">3</span></p>
<p>Fatal error: require_once<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>: Failed opening required <span style="color:#CC0000;">'Frog.php'</span> <span style="color:#006600; font-weight:bold;">&#40;</span>include_path=<span style="color:#CC0000;">'.:/usr/share/php:/usr/share/pear'</span><span style="color:#006600; font-weight:bold;">&#41;</span> in /home/ammon/autoload.<span style="">php</span> on line <span style="color:#800000;">3</span></div>
</div>
</div>
<p>
If we had used an include_once() call, the output is similar, but slightly more informative:</p>
<div class="syntax_hilite">
<div id="code-25">
<div class="code">ammon@kif:~$ php test.<span style="">php</span> </p>
<p>Warning: include_once<span style="color:#006600; font-weight:bold;">&#40;</span>Frog.<span style="">php</span><span style="color:#006600; font-weight:bold;">&#41;</span>: failed to open stream: No such file or directory in /home/ammon/autoload.<span style="">php</span> on line <span style="color:#800000;">3</span></p>
<p>Warning: include_once<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>: Failed opening <span style="color:#CC0000;">'Frog.php'</span> for inclusion <span style="color:#006600; font-weight:bold;">&#40;</span>include_path=<span style="color:#CC0000;">'.:/usr/share/php:/usr/share/pear'</span><span style="color:#006600; font-weight:bold;">&#41;</span> in /home/ammon/autoload.<span style="">php</span> on line <span style="color:#800000;">3</span></p>
<p>Fatal error: Class <span style="color:#CC0000;">'Frog'</span> not found in /home/ammon/test.<span style="">php</span> on line <span style="color:#800000;">4</span></div>
</div>
</div>
<p>
So that's probably a bit more useful in tracking down the error. Require calls don't return anything - they throw a fatal error on failure. Include calls, however, return FALSE on failure and TRUE if the file is (or, in the case of include_once, has already been) successfully included. So you can include_once() and write to a separate logfile (or to the output stream...) if you need more information than the fatal error already provides you.</p>
<h3>&lt;rant&gt;</h3>
<p>To those who insist on giving your classes and their containing files different names... umm. Wow.</p>
<p>If I have a class called DatabaseConnection, I'm going to put it in a file called DatabaseConnection.php. If I'm working with strange people who somehow don't think that is explicit enough, I might call it DatabaseConnection.class.php and tweak the autoload method ever so slightly to compensate. There's no good reason to put it in a file called projx-database_connection.incl or something. No. There isn't.</p>
<p>If you want to organize your classes into a meaningful directory structure... good for you. Use PHP's built-in <a href='http://php.net/get_include_path'>include_path</a> ini option. Don't waste time trying to cascade down a directory structure searching for the classes - just make sure your includes are all in a set of reliable locations. You don't actually have to edit the php.ini file and bounce Apache or your php-cgi processes, just define the additional include paths in the same file where you define your autoloader:</p>
<div class="syntax_hilite">
<div id="php-26">
<div class="php"><a href="http://www.php.net/set_include_path"><span style="color:#000066;">set_include_path</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/get_include_path"><span style="color:#000066;">get_include_path</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> . PATH_SEPARATOR .<br />
&nbsp; &nbsp; <span style="color:#FF0000;">"/var/www/includes"</span> . PATH_SEPARATOR .<br />
&nbsp; &nbsp; <span style="color:#FF0000;">"/var/www/includes/apple"</span> . PATH_SEPARATOR .<br />
&nbsp; &nbsp; <span style="color:#FF0000;">"/var/www/includes/banana"</span><br />
<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>Naturally, you could turn that into some function calls to dynamically register and unregister directories, etc... but at that point, you're probably hurting yourself again. If your codebase is being reorganized enough to make maintenance of the list of include dirs onerous without full time intervention, something else has probably already gone very wrong. At best, the code probably doesn't work anyway, so any brief delay in updating the list can't hurt any more than whatever else is happening.</p>
<h3>&lt;/rant&gt;</h3>
<p>But seriously. __autoload() is your friend. It will help clean up your code if you let it. It can help enforce naming conventions. It can even improve performance... so long as you refrain from using it to shoot yourself in the foot. <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/10/28/php-autoload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php signals while selecting</title>
		<link>http://ammonlauritzen.com/blog/2008/10/27/php-signals-while-selecting/</link>
		<comments>http://ammonlauritzen.com/blog/2008/10/27/php-signals-while-selecting/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 20:26:38 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[posix signals]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=399</guid>
		<description><![CDATA[So a fairly longstanding gripe of mine has been that PHP fails to execute registered signal handlers when it receives a signal in the middle of a blocking select call. Today, I finally bumped into a situation where I couldn't just change the spec to avoid the situation... and I've finally figured out how to [...]]]></description>
			<content:encoded><![CDATA[<p>So a fairly longstanding gripe of mine has been that PHP fails to execute registered signal handlers when it receives a signal in the middle of a blocking select call. Today, I finally bumped into a situation where I couldn't just change the spec to avoid the situation... and I've finally figured out how to make it work.</p>
<p>The bug has been reported <a href='http://bugs.php.net/44614'>here</a>, where it was ignored for a few months before being shot down and ignored some more as per php dev team regulations.</p>
<p>Sample code given by the reporter of the bug is markedly similar to the situations I've encountered the problem:</p>
<div class="syntax_hilite">
<div id="php-35">
<div class="php">pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span>SIGINT, <span style="color:#FF0000;">"sig_handler"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$sock</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$port</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$read_socks</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$sock</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
<span style="color:#0000FF;">$foo</span> = <a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$read_socks</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p>
By filling in his blanks, my first test case looks something like this:</p>
<div class="syntax_hilite">
<div id="php-36">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
<span style="color:#000000; font-weight:bold;">function</span> sig_handler<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"received sig #$signo<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGINT, <span style="color:#FF0000;">"sig_handler"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$socket</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1234</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$r</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$socket</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$foo</span> = <a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"select returned '$foo'<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
When executing the script and pressing ^C (which sends SIGINT), the following occurs:</p>
<div class="syntax_hilite">
<div id="code-37">
<div class="code">ammon@morbo:~$ php sigtest.<span style="">php</span> <br />
PHP Warning:&nbsp; socket_select<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>: unable to select <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span>: Interrupted system call in /home/ammon/sigtest.<span style="">php</span> on line <span style="color:#800000;">13</span><br />
select returned <span style="color:#CC0000;">''</span></div>
</div>
</div>
<p></p>
<p>Ok, so the warning is to be expected, and we can easily squelch that.</p>
<p>The real problem is that the signal handler never runs.</p>
<p>However... for the first time in my life, a response to a php bug report proves enlightening. The dev who answered this ticket provides his sample code and says he can't duplicate the bug. Upon looking at the differences between their code, only one difference stands out:</p>
<div class="syntax_hilite">
<div id="php-38">
<div class="php">declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks=<span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p>
The <a href='http://php.net/declare'>declare(ticks)</a> directive is deprecated as of php 5.3 and will not be with us in php 6.0. Ticks are an unreliable, unpredictable, and generally bad thing in php. I've neither successfully used them nor seen a successful and justified use.</p>
<p>That being said... turning the tick on but not telling it to do anything appears to address the problem of discarded interrupts:</p>
<div class="syntax_hilite">
<div id="php-39">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks=<span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#000000; font-weight:bold;">function</span> sig_handler<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"received sig #$signo<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGINT, <span style="color:#FF0000;">"sig_handler"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$socket</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1234</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$r</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$socket</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$foo</span> = @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"select returned '$foo'<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
And execution:</p>
<div class="syntax_hilite">
<div id="code-40">
<div class="code">ammon@morbo:~$ php sigtest.<span style="">php</span> <br />
received sig #2<br />
select returned <span style="color:#CC0000;">''</span></div>
</div>
</div>
<p>
Which is precisely the desired behavior.</p>
<p>I don't know what the performance hit for turning ticks on is, I haven't had time to research this. But I can confirm that by declaring ticks globally, it does work in an OO environment as well:</p>
<div class="syntax_hilite">
<div id="php-41">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks=<span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#000000; font-weight:bold;">class</span> signal_tester <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGINT, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;<span style="color:#0000FF;">$this</span>,<span style="color:#FF0000;">"sig_handler"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> sig_handler<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$signo</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"received sig #$signo<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">function</span> start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$socket</span> = <a href="http://www.php.net/socket_create_listen"><span style="color:#000066;">socket_create_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1234</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$r</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$socket</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$n</span> = <span style="color:#000000; font-weight:bold;">NULL</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$foo</span> = @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#0000FF;">$n</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"select returned '$foo'<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#0000FF;">$test</span> = <span style="color:#000000; font-weight:bold;">new</span> signal_tester<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
Executing and hitting ^C:</p>
<div class="syntax_hilite">
<div id="code-42">
<div class="code">ammon@morbo:~$ php sigtest.<span style="">php</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
received sig #2<br />
select returned <span style="color:#CC0000;">''</span></div>
</div>
</div>
<p>
After a few minutes of largely unscientific testing, it appears that turning ticks on globally costs a whopping 4 bytes of ram and causes the script to occasionally consume more cpu than the top process I used to monitor it. So... at first glance the cost is pretty negligible and all I can say is that if you ever need to handle signals (SIGTERM, SIGHUP, etc...) from within a blocking select call in php, it looks like declare ticks is the only option for now.</p>
<p>I did the initial tests in 5.1.6, but can confirm the same behavior in 5.2.5. I don't know how the behavior is going to be in 5.3, since I don't run alpha releases on my servers but my gut likes to think that it will continue to work the same for now... and will hopefully not break until 6.0 (when everything else will explode for a few years). Shrug.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/10/27/php-signals-while-selecting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>php tail</title>
		<link>http://ammonlauritzen.com/blog/2008/05/27/php-tail/</link>
		<comments>http://ammonlauritzen.com/blog/2008/05/27/php-tail/#comments</comments>
		<pubDate>Tue, 27 May 2008 19:08:21 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=389</guid>
		<description><![CDATA[I have a php script that frequently needs to email me the last few lines of a log file. I can't afford to exec() a binary tail process, so the solution has to be in pure php. Originally, the files in question never exceeded more than a few thousand lines. Unfortunately, I am encountering cases [...]]]></description>
			<content:encoded><![CDATA[<p>I have a php script that frequently needs to email me the last few lines of a log file. I can't afford to exec() a binary tail process, so the solution has to be in pure php.</p>
<p>Originally, the files in question never exceeded more than a few thousand lines. Unfortunately, I am encountering cases now where the files are now occasionally 50,000 lines or longer. This causes PHP's memory consumption to explode.</p>
<p><i>Note: Code snippets provided here are not fully functional standalone shell scripts. The scripts I ran to benchmark the algorithms contain some rudimentary setup logic that is not important here, so has not been included.</i></p>
<p>My original method:</p>
<div class="syntax_hilite">
<div id="php-47">
<div class="php"><span style="color:#FF9933; font-style:italic;">// tail-file.php</span><br />
<span style="color:#0000FF;">$arr</span> = @<a href="http://www.php.net/file"><span style="color:#000066;">file</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fname</span>, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$arr</span> = <a href="http://www.php.net/array_slice"><span style="color:#000066;">array_slice</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span>, -<span style="color:#0000FF;">$lines</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$buf</span> = <a href="http://www.php.net/implode"><span style="color:#000066;">implode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;quot;\\\n&amp;quot;,<span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>This is easy to understand and is pretty fast, all things considered. Unfortunately, the memory footprint for loading a file into an array is obscene. Loading a 4400 line log file with this method could consume more than 17mb of ram. 50,000 line files easily stressed the 256mb limit I am able to provide the process.</p>
<p>So, the obvious solution to the memory consumption is to avoid loading the entire file at once. What if we kept a rotating list of lines in the file?</p>
<div class="syntax_hilite">
<div id="php-48">
<div class="php"><span style="color:#FF9933; font-style:italic;">// tail-array.php</span><br />
<span style="color:#0000FF;">$arr</span> = <a href="http://www.php.net/array_fill"><span style="color:#000066;">array_fill</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">0</span>, <span style="color:#0000FF;">$lines</span>+<span style="color:#CC66CC;">1</span>, &amp;quot;\\\n&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$fp</span> = <a href="http://www.php.net/fopen"><span style="color:#000066;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fname</span>, &amp;quot;r&amp;quot;<span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/feof"><span style="color:#000066;">feof</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$line</span> = <a href="http://www.php.net/fgets"><span style="color:#000066;">fgets</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span>, <span style="color:#CC66CC;">4096</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$line</span>; <span style="color:#FF9933; font-style:italic;">// faster than array_push()</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/array_shift"><span style="color:#000066;">array_shift</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$buf</span> = <a href="http://www.php.net/implode"><span style="color:#000066;">implode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;quot;&amp;quot;,<span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>This method works by keeping the $lines-many most recent lines of the file in an array. Memory consumption remains sane, but the performance hit for performing so many array pushes and shifts is bad. Really bad. With small files, I can't notice any difference between this method and the file() method... but with longer files, it adds up quickly.</p>
<p>Given a 51 line, 4kb file, an average execution ($lines = 20) might look like this:</p>
<div class="syntax_hilite">
<div id="code-49">
<div class="code">ammon@zapp:~$ time ./tail-file.<span style="">php</span> a.<span style="">log</span>&amp;gt;/dev/null</p>
<p>real&nbsp; &nbsp; 0m0.015s<br />
user&nbsp; &nbsp; 0m0.009s<br />
sys&nbsp; &nbsp; &nbsp;0m0.007s</p>
<p>ammon@zapp:~$ time ./tail-array.<span style="">php</span> a.<span style="">log</span>&amp;gt;/dev/null</p>
<p>real&nbsp; &nbsp; 0m0.016s<br />
user&nbsp; &nbsp; 0m0.010s<br />
sys&nbsp; &nbsp; &nbsp;0m0.006s</div>
</div>
</div>
<p></p>
<p>Comparable enough. But given a 50,004 line (3.3mb) log file:</p>
<pre>
ammon@zapp:~$ time ./tail-file.php b.log >/dev/null                  

real    0m0.079s
user    0m0.058s
sys     0m0.021s

ammon@zapp:~$ time ./tail-array.php b.log >/dev/null                 

real    0m0.119s
user    0m0.112s
sys     0m0.007s
</pre>
<p>The difference becomes quite clear. However... what if my log file grows obscenely large? I've got a 9 million line log file (1.6gb) lying around to test with...</p>
<pre>
ammon@zapp:~$ time ./tail-file.php c.log >/dev/null

real    0m0.015s
user    0m0.008s
sys     0m0.008s

ammon@zapp:~$ time ./tail-array.php c.log >/dev/null                 

real    0m19.351s
user    0m18.545s
sys     0m0.803s
</pre>
<p>The file() method crashes because it can't allocate enough ram to hold a 9 million element array and the array method takes almost 20 seconds to execute. It's slow... but at least it works.</p>
<p>Of course, there are other methods. The one I finally settled on is this:</p>
<div class="syntax_hilite">
<div id="php-50">
<div class="php"><span style="color:#FF9933; font-style:italic;">// tail-seek.php</span><br />
<span style="color:#0000FF;">$fp</span> = <a href="http://www.php.net/fopen"><span style="color:#000066;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fname</span>, &amp;quot;r&amp;quot;<span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$lines_read</span> = <span style="color:#CC66CC;">0</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fp</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/fseek"><span style="color:#000066;">fseek</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fp</span>, <span style="color:#CC66CC;">0</span>, SEEK_END <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$pos</span> = <span style="color:#0000FF;">$eof</span> = <a href="http://www.php.net/ftell"><span style="color:#000066;">ftell</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">do</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; --<span style="color:#0000FF;">$pos</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/fseek"><span style="color:#000066;">fseek</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span>, <span style="color:#0000FF;">$pos</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$c</span> = <a href="http://www.php.net/fgetc"><span style="color:#000066;">fgetc</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$c</span> == &amp;quot;\\\n&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$lines_read</span>++;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$pos</span>&amp;gt; <span style="color:#CC66CC;">0</span> &amp;amp;&amp;amp; <span style="color:#0000FF;">$lines_read</span> &amp;lt;= <span style="color:#0000FF;">$lines</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$buf</span> = <a href="http://www.php.net/fread"><span style="color:#000066;">fread</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span>, <span style="color:#0000FF;">$eof</span>-<span style="color:#0000FF;">$pos</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$fp</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>This method doesn't waste time reading the bulk of the file. It jumps to the end and scans backward until enough newlines have been located. The only problem here is that your average filesystem isn't optimized for reading backwards... but since we're not really reading very much data, it doesn't much matter.</p>
<pre>
ammon@zapp:~$ time ./tail-seek.php a.log >/dev/null

real    0m0.017s
user    0m0.009s
sys     0m0.008s

ammon@zapp:~$ time ./tail-seek.php b.log >/dev/null                  

real    0m0.017s
user    0m0.008s
sys     0m0.010s

ammon@zapp:~$ time ./tail-seek.php c.log >/dev/null                  

real    0m0.023s
user    0m0.015s
sys     0m0.008s
</pre>
<p>Performance is a trifle slower on small files, but it's astronomically better on long ones. This is similar to the method used by most unix 'tail' commands, and is the clear winner for actual use in my application.</p>
<p>Of course, it needs a bit of cleanup from the state I've provided it in, and isn't appropriate for all environments... but it's a trifle better than requiring 20 seconds and 20gb of ram to execute <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/05/27/php-tail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>flash policy service daemon</title>
		<link>http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/</link>
		<comments>http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 09:05:10 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=362</guid>
		<description><![CDATA[Sorry it took me so long to post this, but WordPress 2.5 doesn't seem to like me trying to upload gz/zip files, so I had to upload the source manually. Well, it's been months since I promised to post some usable socket policy service code, so I will. The script here is meant to serve [...]]]></description>
			<content:encoded><![CDATA[<p><i>Sorry it took me so long to post this, but WordPress 2.5 doesn't seem to like me trying to upload gz/zip files, so I had to upload the source manually.</i></p>
<p>Well, it's been months since <a href='http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/'>I promised</a> to post some usable socket policy service code, so I will.</p>
<p>The script here is meant to serve as a good starting point for people whose servers need to allow flash clients to make socket connections. I have not actually used this exact code in a production environment, but I have been using code that is 99% identical for a while now. I am confident that any blatant flaws are the result of simple copy-paste errors as I compiled the package. Please let me know if you find any.</p>
<p>I <u>have</u> however, stress tested the heck out of this service. One instance successfully served up over 16000 policy file requests fed into it as rapidly as I could send them. The same networking code has also handled requests from at least 100 different hosts at roughly the same time.</p>
<p>Everything has been combined into a single cli php script that requires no special installation. Just plop it down on the server and run it as root. It will take care of the rest. The config defaults should be safe, but you probably want to specify them more clearly - just to be safe.</p>
<p>The daemon is made of three classes:</p>
<ul>
<li><b>Logger</b> - A rudimentary log file management class that I copy from project to project in one form or another. The included version is stripped down from some of the other versions I've written, and I'm planning on releasing a more feature-rich version in the future.</li>
<li><b>Daemon</b> - A simple class for daemonizing a process. Adapted and re-adapted countless times from an original php4 class I found on the net a few years ago by some guy named Seth (whose email domain no longer exists).</li>
<li><b>FlashPolicyService</b> - The meat and potatoes, a child of Daemon. Mostly, this is just the requisite networking code and glue to make everything work together.</li>
</ul>
<p>As with any of my other code, this is licensed under <a href='http://creativecommons.org/licenses/by/3.0/'>CC Attribution 3.0</a>.</p>
<p>Download:</p>
<ul>
<li><a href='http://ammonlauritzen.com/FlashPolicyService-09c.zip'>FlashPolicyService-09c.zip</a> (4.4kb)</li>
</ul>
<p>Source code after the jump.<br />
<span id="more-362"></span></p>
<div class="syntax_hilite">
<div id="php-52">
<div class="php"><span style="color:#008000; font-style:italic;">#!/usr/local/bin/php</span><br />
<span style="color:#000000; font-weight:bold;">&lt;?php</span><br />
<span style="color:#008000;">/**<br />
&nbsp;* Flash Policy Service v0.9.c<br />
&nbsp;*<br />
&nbsp;* This script listens for &lt;policy-file-request/&gt; on port 843 and serves up<br />
&nbsp;* an xml crossdomain policy file. This sort of service is necessary if any<br />
&nbsp;* flash content is going to connect to sockets on the running host.<br />
&nbsp;*<br />
&nbsp;* I have made every effort to package everything you need into a single<br />
&nbsp;* file here, even though it could easily have been split into 3 or more<br />
&nbsp;* scripts.<br />
&nbsp;*<br />
&nbsp;* Requirements:<br />
&nbsp;*&nbsp; - PHP 5 with sockets, pcntl, and posix extensions<br />
&nbsp;*&nbsp; - Root access (to bind to a &lt;1024 port)<br />
&nbsp;*<br />
&nbsp;* Use:<br />
&nbsp;*&nbsp; Simply execute the script from the command line as root:<br />
&nbsp;*&nbsp; &nbsp; # ./FlashPolicyService.php<br />
&nbsp;*&nbsp; You can enable debug mode by invoking the script with '-d' as a parameter:<br />
&nbsp;*&nbsp; &nbsp; # ./FlashPolicyService.php -d<br />
&nbsp;*&nbsp; To stop the daemon, simply send it a SIGTERM and it should attempt to<br />
&nbsp;*&nbsp; exit cleanly.<br />
&nbsp;*<br />
&nbsp;*&nbsp; If you get 'bad interpreter' errors or the like, change the #! line to<br />
&nbsp;*&nbsp; reflect the actual installed location of your php cli.<br />
&nbsp;*<br />
&nbsp;* Configuration:<br />
&nbsp;*&nbsp; At present, there aren't very many config options. Simply edit the<br />
&nbsp;*&nbsp; section immediately following this header. Options are commented.<br />
&nbsp;*<br />
&nbsp;* License:<br />
&nbsp;*&nbsp; This code is made available under a Creative Commons Attribution 3.0<br />
&nbsp;*&nbsp; License. Basically, you can use it however you like, but I would<br />
&nbsp;*&nbsp; appreciate some credit when you do.<br />
&nbsp;*<br />
&nbsp;* Disclaimer:<br />
&nbsp;*&nbsp; I make no guarantees that this code won't make your server explode in a<br />
&nbsp;*&nbsp; shower of blue flames. However, I don't expect that it will. I am actually<br />
&nbsp;*&nbsp; confident that this code will be helpful.<br />
&nbsp;*<br />
&nbsp;*&nbsp; That said, this is still my beta release. If you find any bugs, please<br />
&nbsp;*&nbsp; let me know so I can fix them.<br />
&nbsp;*<br />
&nbsp;* - Ammon Lauritzen [Apr 21, '08]<br />
&nbsp;*&nbsp; &nbsp;http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/<br />
&nbsp;*<br />
&nbsp;* Changelog<br />
&nbsp;* 0.9.c<br />
&nbsp;*&nbsp; &nbsp;- Fixed some typoes that were the result of how I combined everything<br />
&nbsp;*&nbsp; &nbsp; &nbsp;into a single file. Thanks Alex!<br />
&nbsp;* 0.9.b<br />
&nbsp;*&nbsp; &nbsp;- Original version to be posted at this url.<br />
&nbsp;* 0.9.a<br />
&nbsp;*&nbsp; &nbsp;- Original proof of concept code posted online.<br />
&nbsp;*/</span></p>
<p><span style="color:#008000;">/*** Config ***/</span></p>
<p><span style="color:#FF9933; font-style:italic;">// where should we save the log output?</span><br />
<span style="color:#0000FF;">$log_filename</span> = <span style="color:#FF0000;">"/tmp/flash-policy.log"</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// uncomment these lines to choose which user to run the daemon as</span><br />
<span style="color:#FF9933; font-style:italic;">// default behavior is to look up and attempt to run as 'nobody'</span><br />
<span style="color:#008000; font-style:italic;"># $daemon_uid = 99;</span><br />
<span style="color:#008000; font-style:italic;"># $daemon_gid = 99;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// set this if you want to use an external file in stead of the default</span><br />
<span style="color:#0000FF;">$xml_filename</span> = <span style="color:#FF0000;">""</span>;<br />
<span style="color:#0000FF;">$default_xml</span> =<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;'</span>.<span style="color:#FF0000;">'?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?'</span>.<span style="color:#FF0000;">'&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;cross-domain-policy xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd&quot;&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;allow-access-from domain=&quot;*&quot; to-ports=&quot;*&quot; secure=&quot;false&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;site-control permitted-cross-domain-policies=&quot;all&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;/cross-domain-policy&gt;'</span>;</p>
<p><span style="color:#008000;">/*** You shouldn't have to edit anything below here ***/</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#000000; font-weight:bold;">class</span> Logger <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$logfile</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span> = <span style="color:#0000FF;">$logfile</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">open_log</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: constructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fflush"><span style="color:#000066;">fflush</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: destructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> <a href="http://www.php.net/log"><span style="color:#000066;">log</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// deal with redundant log spam</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> == <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Last message repeated "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span>.<span style="color:#FF0000;">" times."</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">lasg_msg</span> = <span style="color:#0000FF;">$msg</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">last_msg_count</span> = <span style="color:#CC66CC;">0</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// actually write the log message out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: log</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> write<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$msg</span> = <a href="http://www.php.net/sprintf"><span style="color:#000066;">sprintf</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"[%s] %s<span style="color:#000099; font-weight:bold;">\\</span>n"</span>,<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"y-m-d H:i:s"</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#0000FF;">$msg</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/fwrite"><span style="color:#000066;">fwrite</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span>, <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$succ</span> === <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$msg</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: write</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> open_log<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/touch"><span style="color:#000066;">touch</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chmod"><span style="color:#000066;">chmod</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span>, <span style="color:#CC66CC;">0664</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">fh</span> = @<a href="http://www.php.net/fopen"><span style="color:#000066;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logfile</span>, <span style="color:#FF0000;">"a"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: logger class</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#000000; font-weight:bold;">class</span> Daemon <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/error_reporting"><span style="color:#000066;">error_reporting</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/set_time_limit"><span style="color:#000066;">set_time_limit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$log_filename</span>, <span style="color:#0000FF;">$pid_filename</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logger</span> = <span style="color:#000000; font-weight:bold;">new</span> Logger<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$log_filename</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pid_filename</span> = <span style="color:#0000FF;">$pid_filename</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log_tag</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> = <span style="color:#FF0000;">"launcher"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"constructed"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: constructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"destructing"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: destructor</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> <a href="http://www.php.net/log"><span style="color:#000066;">log</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$tag</span> = <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$tag</span> == <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$tag</span> = <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log_tag</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">logger</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$tag</span>.<span style="color:#FF0000;">": "</span>.<span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: log</span><br />
&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> debug<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$tag</span> = <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> DEBUG <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: debug</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> main<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"override main() in daemon subclass"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">stop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: main</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"starting daemon"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;_start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to start daemon"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// report execution details</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"uid = "</span>.<a href="http://www.php.net/posix_getuid"><span style="color:#000066;">posix_getuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">", gid = "</span>.<a href="http://www.php.net/posix_getgid"><span style="color:#000066;">posix_getgid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"cwd = "</span>.<a href="http://www.php.net/getcwd"><span style="color:#000066;">getcwd</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// invoke main loop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">running</span> = <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">running</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">main</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: start</span><br />
&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> _start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;_fork<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: try to fork</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/posix_setsid"><span style="color:#000066;">posix_setsid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to setsid()"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: try to set sid</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;_suid<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: try to set uid</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// register signal handler</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; declare<span style="color:#006600; font-weight:bold;">&#40;</span>ticks = <span style="color:#CC66CC;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; pcntl_signal<span style="color:#006600; font-weight:bold;">&#40;</span> SIGTERM, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>&amp;<span style="color:#0000FF;">$this</span>, <span style="color:#FF0000;">"on_sigterm"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// chdir somewhere moderately safe by default</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chdir"><span style="color:#000066;">chdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">'/tmp'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: _start</span><br />
&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> _fork<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"forking..."</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$pid</span> = pcntl_fork<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$pid</span> == -<span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// error</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to fork"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$pid</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// parent</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"done with parent"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// child</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> = <span style="color:#FF0000;">"daemon"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">child</span> = <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pid</span> = <a href="http://www.php.net/posix_getpid"><span style="color:#000066;">posix_getpid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"child pid = "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">pid</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: _fork</span><br />
&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> _suid<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$daemon_uid</span>, <span style="color:#0000FF;">$daemon_gid</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$daemon_uid</span><span style="color:#006600; font-weight:bold;">&#41;</span> || !<a href="http://www.php.net/isset"><span style="color:#000066;">isset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$daemon_gid</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// we didn't get a uid/gid, try to make one up</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"searching for info on 'nobody'"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$res</span> = <a href="http://www.php.net/posix_getpwnam"><span style="color:#000066;">posix_getpwnam</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"nobody"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$res</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$uid</span> = <span style="color:#0000FF;">$res</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'uid'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$gid</span> = <span style="color:#0000FF;">$res</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'gid'</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// the 'nobody' user doesn't exist on this system, refuse</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// to daemonize as root</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to find info on 'nobody' user"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got uid/gid of $daemon_uid/$daemon_gid"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$uid</span> = <span style="color:#0000FF;">$daemon_uid</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$gid</span> = <span style="color:#0000FF;">$daemon_gid</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// actually try to switch now</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/posix_setgid"><span style="color:#000066;">posix_setgid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$gid</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to set gid to "</span>.<span style="color:#0000FF;">$gid</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/posix_setuid"><span style="color:#000066;">posix_setuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$uid</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to set uid to "</span>.<span style="color:#0000FF;">$uid</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: _suid</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> stop<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"stopping daemon"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">running</span> = <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: stop</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> on_sigterm<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sig</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sig</span> == SIGTERM <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got SIGTERM"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">daemon_tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">stop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: on_sigterm</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: daemon class</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#000000; font-weight:bold;">class</span> FlashPolicyService extends Daemon <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log_tag</span> = <span style="color:#FF0000;">"fps"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"constructing"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">request_str</span> = <span style="color:#FF0000;">"&lt;policy-file-request/&gt;"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">port</span> = <span style="color:#CC66CC;">843</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// get our xml</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/global"><span style="color:#000066;">global</span></a> <span style="color:#0000FF;">$xml_filename</span>, <span style="color:#0000FF;">$default_xml</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/strlen"><span style="color:#000066;">strlen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$xml_filename</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#CC66CC;">0</span> || !<a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$xml_filename</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to read xml from '$xml_filename', using default"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> = <span style="color:#0000FF;">$default_xml</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"reading policy xml from $xml_filename"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> = <a href="http://www.php.net/file_get_contents"><span style="color:#000066;">file_get_contents</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$xml_filename</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"policy xml: "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// make sure we're null terminated</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> = <a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\\</span>n<span style="color:#000099; font-weight:bold;">\\</span>0"</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// and get going</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">init</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: constructor</span></p>
<p>&nbsp; &nbsp; public <span style="color:#000000; font-weight:bold;">function</span> __destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parent::__destruct<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: destructor</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> init<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"init..."</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">check_socket</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::<span style="color:#006600;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"not starting daemon"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: init</span></p>
<p>&nbsp; &nbsp; protected <span style="color:#000000; font-weight:bold;">function</span> main<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">accept_socket</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"server socket is closed?!"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::<span style="color:#006600;">stop</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// paranoia to keep from absolutely hosing cpu if something goes wrong</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/usleep"><span style="color:#000066;">usleep</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">10000</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// 10ms</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: main</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> check_socket<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> = @<a href="http://www.php.net/socket_create"><span style="color:#000066;">socket_create</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> AF_INET, SOCK_STREAM, SOL_TCP <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to create socket"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_bind"><span style="color:#000066;">socket_bind</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span>, <span style="color:#FF0000;">"0.0.0.0"</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">port</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to bind to port "</span>.<span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">port</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$backlog</span> = <span style="color:#CC66CC;">100</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_listen"><span style="color:#000066;">socket_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span>, <span style="color:#0000FF;">$backlog</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"unable to listen with backlog of $backlog"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// everything's good</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: able to bind</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: able to create socket</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// if we got here, it's an error. abort.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$errno</span> = <a href="http://www.php.net/socket_last_error"><span style="color:#000066;">socket_last_error</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$errstr</span> = <a href="http://www.php.net/socket_strerror"><span style="color:#000066;">socket_strerror</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$errno</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"socket error: $errno: $errstr"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: check_socket</span></p>
<p>&nbsp; &nbsp; private <span style="color:#000000; font-weight:bold;">function</span> accept_socket<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$r_socks</span> = <a href="http://www.php.net/array_merge"><span style="color:#000066;">array_merge</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"selecting on "</span>.<a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$r_socks</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">" sockets"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// block until something interesting happens</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$select</span> = @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r_socks</span>, <span style="color:#0000FF;">$w_socks</span> = <span style="color:#000000; font-weight:bold;">NULL</span>, <span style="color:#0000FF;">$e_socks</span> = <span style="color:#000000; font-weight:bold;">NULL</span>, <span style="color:#000000; font-weight:bold;">NULL</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$select</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">return</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// did we get a new connection?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/in_array"><span style="color:#000066;">in_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span>, <span style="color:#0000FF;">$r_socks</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$conn</span> = @<a href="http://www.php.net/socket_accept"><span style="color:#000066;">socket_accept</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> !== <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/socket_getpeername"><span style="color:#000066;">socket_getpeername</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$addr</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"connection accepted from $addr, $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$conn</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: got a new connection</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// check for policy requests</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r_socks</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$conn</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// ignore the server socket</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> == <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">continue</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// read from the client</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$data</span> = @<a href="http://www.php.net/socket_read"><span style="color:#000066;">socket_read</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#CC66CC;">1024</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$data</span> === <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got disconnect from $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: client closed connection</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">debug</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"read '"</span>.<a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">"' from $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/strpos"><span style="color:#000066;">strpos</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">request_str</span><span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"sending policy xml to $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/socket_write"><span style="color:#000066;">socket_write</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">xml</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">log</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"got invalid request from $conn"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: got data from the client</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// and always disconnect after having read something, whether</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// it was a valid request or not - especially if it wasn't <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<a href="http://www.php.net/socket_close"><span style="color:#000066;">socket_close</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$key</span> = <a href="http://www.php.net/array_search"><span style="color:#000066;">array_search</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$key</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/unset"><span style="color:#000066;">unset</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">connections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$key</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: foreach socket</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: accept_socket</span></p>
<p><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: flash policy service class</span></p>
<p><span style="color:#FF9933; font-style:italic;">/////////////////////////////////////////////////////////////////////////////</span><br />
<span style="color:#008000;">/**<br />
&nbsp;* Actual execution code here. This checks if the php install has all of our<br />
&nbsp;* requisite extensions, makes sure we're launching as root, and checks if<br />
&nbsp;* debug mode was requested before actually starting the daemon.<br />
&nbsp;*/</span></p>
<p><span style="color:#FF9933; font-style:italic;">// make sure we have required extensions</span><br />
<span style="color:#0000FF;">$required_extensions</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"sockets"</span>, <span style="color:#FF0000;">"posix"</span>, <span style="color:#FF0000;">"pcntl"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$missing_extension</span> = <span style="color:#000000; font-weight:bold;">false</span>;<br />
<span style="color:#616100;">foreach</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$required_extensions</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$ext</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/extension_loaded"><span style="color:#000066;">extension_loaded</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$ext</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Missing required php extension '$ext'.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$missing_extension</span> = <span style="color:#000000; font-weight:bold;">true</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$missing_extension</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// make sure we launch as root, otherwise we can't bind to 843</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/posix_getuid"><span style="color:#000066;">posix_getuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#CC66CC;">0</span> || <a href="http://www.php.net/posix_getgid"><span style="color:#000066;">posix_getgid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"Policy service must be started as root.<span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// see if we're in debug mode</span><br />
<a href="http://www.php.net/define"><span style="color:#000066;">define</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"DEBUG"</span>, <a href="http://www.php.net/in_array"><span style="color:#000066;">in_array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'-d'</span>,<span style="color:#0000FF;">$argv</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// start the daemon</span><br />
<span style="color:#0000FF;">$fps</span> = <span style="color:#000000; font-weight:bold;">new</span> FlashPolicyService<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>lazy image browser</title>
		<link>http://ammonlauritzen.com/blog/2008/03/21/lazy-image-browser/</link>
		<comments>http://ammonlauritzen.com/blog/2008/03/21/lazy-image-browser/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 17:26:50 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2008/03/21/lazy-image-browser/</guid>
		<description><![CDATA[The other day, I heard a few people talking about needing an easy way to browse images on a remote Apache server that has Indexes disabled. They had a ~20 line php script that they were dropping into each directory in order to generate indices. The problem came when they started organizing the images into [...]]]></description>
			<content:encoded><![CDATA[<p>The other day, I heard a few people talking about needing an easy way to browse images on a remote Apache server that has Indexes disabled.</p>
<p>They had a ~20 line php script that they were dropping into each directory in order to generate indices. The problem came when they started organizing the images into subdirectories. Eventually, it became necessary to copy the new script into a mind-bogglingly large number of directories. Inevitably, dirs were missed, etc...</p>
<p>I interjected that I could probably fix their problem in 30 minutes.</p>
<p>So I did.</p>
<div class="syntax_hilite">
<div id="php-54">
<div class="php"><span style="color:#000000; font-weight:bold;">&lt;?</span><br />
<span style="color:#0000FF;">$base</span> = <a href="http://www.php.net/getcwd"><span style="color:#000066;">getcwd</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$subdir</span> = <a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_GET</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'dir'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$dir</span> = <a href="http://www.php.net/realpath"><span style="color:#000066;">realpath</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"$base/$subdir"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$valid</span> = <a href="http://www.php.net/strpos"><span style="color:#000066;">strpos</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir</span>, <span style="color:#0000FF;">$base</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$dir</span> || <span style="color:#0000FF;">$valid</span> === <span style="color:#000000; font-weight:bold;">FALSE</span> || <span style="color:#0000FF;">$valid</span> != <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$imgdir</span> = <a href="http://www.php.net/dirname"><span style="color:#000066;">dirname</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'SCRIPT_NAME'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"&lt;h3&gt;$subdir&lt;/h3&gt;<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
<span style="color:#0000FF;">$dirs</span> = <span style="color:#FF0000;">""</span>;<br />
<span style="color:#0000FF;">$imgs</span> = <span style="color:#FF0000;">"&lt;hr/&gt;<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;</p>
<p><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir</span><span style="color:#006600; font-weight:bold;">&#41;</span> &amp;&amp; <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$dh</span> = <a href="http://www.php.net/opendir"><span style="color:#000066;">opendir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">false</span> !== <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span> = <a href="http://www.php.net/readdir"><span style="color:#000066;">readdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dh</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$file</span> == <span style="color:#FF0000;">"."</span> || <span style="color:#0000FF;">$file</span> == <span style="color:#FF0000;">".."</span> || <span style="color:#0000FF;">$file</span> == <span style="color:#FF0000;">".svn"</span> || <a href="http://www.php.net/substr"><span style="color:#000066;">substr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span>,-<span style="color:#CC66CC;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#FF0000;">".php"</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"$base/$subdir/$file"</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$dirs</span> .= <span style="color:#FF0000;">"&lt;span&gt;|&lt;a href='?dir=$subdir/$file'&gt;$file&lt;/a&gt;|&lt;/span&gt;<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$imgs</span> .= <span style="color:#FF0000;">"&lt;div style='float:left; margin:15px;'&gt;&lt;a href='$imgdir/$subdir/$file'&gt;&lt;img style='border: none;' src='$imgdir/$subdir/$file'/&gt;&lt;/a&gt;&lt;/div&gt;<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$dirs</span>;<br />
<a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$imgs</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
It's not elegant. It's not pretty. It has plenty of room for improvement - it'll generate links to Windows explorer thumbnail db's, etc... But it is fast and should be moderately secure. Just drop it in the root directory of your image structure and you're good.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2008/03/21/lazy-image-browser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>new flash security policies</title>
		<link>http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/</link>
		<comments>http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 22:14:20 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/</guid>
		<description><![CDATA[So... I am not happy with Adobe right now. With the push of Flash Player 9,0,115,0 "moviestar", which included such awesome features as H.264 and AAC codec support and improvements to fullscreen mode, they kind of ambushed me with some sweeping changes to their security policy. I'd been running pre-release nightly builds of the player [...]]]></description>
			<content:encoded><![CDATA[<p>So... I am not happy with Adobe right now. With the push of Flash Player 9,0,115,0 "moviestar", which included such awesome features as <a href='http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player.html'>H.264 and AAC</a> codec support and <a href='http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html'>improvements to fullscreen mode</a>, they kind of ambushed me with some sweeping changes to their security policy.</p>
<p>I'd been running pre-release nightly builds of the player since 9,0,60,x... and had noticed some strange warnings. Mysterious "Socket Security Error #2048" exceptions that were being thrown at random - even though I was serving an appropriate (for the time) crossdomain.xml file, unexplained timeouts attempting to talk to an xml socket server when I was very clearly not attempting to do any such thing, etc... My regularly repeated attempts to find documentation on what the warnings actually meant proved fruitless. I believe that is because <a href='http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security.html'>the appropriate document</a> was not actually released to the public until 9,0,115,0 was released.</p>
<p>Now, the bit where they improved the format for <a href='http://marstonstudio.com/index.php/2007/12/06/new-security-policy-settings-in-flash-moviestar/'>crossdomain.xml</a> files doesn't really affect me one way or the other. I approve of the improvements but could really care less in this case. They don't really affect anything I'm doing.</p>
<p>The part that really chaps my hide is the fact that they've completely redone the way that socket security policies are handled. The important parts:</p>
<blockquote>
<ul>
<li>A SWF file may no longer make a socket connection to its own domain without a socket policy file. Prior to version 9,0,115,0, a SWF file was permitted to make socket connections to ports 1024 or greater in its own domain without a policy file.</li>
<li>HTTP policy files may no longer be used to authorize socket connections. Prior to version 9,0,115,0, an HTTP policy file, served from the master location of /crossdomain.xml on port 80, could be used to authorize a socket connection to any port 1024 or greater on the same host.</li>
</ul>
</blockquote>
<p>That's right. Your socket policy data can't live in the sitewide crossdomain.xml file that Apache serves any more.</p>
<blockquote><p>
Flash Player 9,0,115,0 introduces a concept of socket master policy files, which are served from the fixed TCP port number 843.
</p></blockquote>
<blockquote><p>
Socket policy files may be obtained from the same port as a main connection (the socket connection being made by ActionScript, which is authorized by a socket policy file), or from a different port, separate from the main connection. If you opt to serve a socket policy file from the same port as a main connection, the server listening on that port must understand socket policy file requests (which are indicated by a transmission of
<policy-file-request/> from Flash Player), and must respond differently for policy file requests and main connection requests.
</p></blockquote>
<blockquote>
<ul>
<li>When a SWF file attempts to make a socket connection, even to its own domain, Flash Player will first attempt to contact port 843 to see if the host is serving a socket master policy file.</li>
</ul>
</blockquote>
<p>So... regardless of whether you're even using a custom port 843 client, the Flash Player is going to try to hit it. What if your firewall doesn't allow/route traffic to sub-1024 ports w/o special configuration? What if you don't have the access to bind to a sub-1024 port and can't rewrite your other server process to serve the policy data on its port?</p>
<blockquote>
<ul>
<li>Socket meta-policies can only be declared in a socket master policy file. The syntax is the same as for declaring a meta-policy in an URL master policy file, using the &lt;site-control&gt; tag. Socket meta-policies cannot be declared in HTTP response headers, as HTTP is not involved.</li>
</ul>
</blockquote>
<p>This implies that you can't even tell apache to listen to port 843 and serve up the data. You HAVE to either maintain a separate server process specifically for the purpose of serving this policy data, or you have to edit the process that SWF's are connecting to and make them serve the data..</p>
<p>As of the time of this writing (10 days after moviestar's release), they have yet to release <a href='http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html'>promised help</a> on how to deploy a solution to these new changes. Granted, the one article they did release explains what needs to be done in high level terms. It was sufficient to help me out. I wrote a server that simply listens on port 843 and spews the required xml. But... I'd have really appreciated specific examples, and I suspect plenty of people would appreciate drop-in solutions to the issue.</p>
<p>A 5-minute skeleton implementation (not recommended for production use by any means) written as a PHP cli script might look something like this:</p>
<div class="syntax_hilite">
<div id="php-56">
<div class="php"><span style="color:#008000; font-style:italic;">#!/usr/bin/php</span><br />
<span style="color:#000000; font-weight:bold;">&lt;?</span><br />
<span style="color:#008000;">/**<br />
&nbsp;* Ugly Flash socket policy file service. This script must be run as root from<br />
&nbsp;* the command line. It binds to port 843 on all interfaces and waits<br />
&nbsp;* indefinitely for connections. When a connection is detected, the script spits<br />
&nbsp;* out a chunk of xml and disconnects. It can only serve one request at a time,<br />
&nbsp;* but that shouldn't be much of a problem.<br />
&nbsp;*<br />
&nbsp;* One potential problem with this script is that you can easily lock port 843<br />
&nbsp;* up for an indeterminate amount of time if the script doesn't exit cleanly.<br />
&nbsp;* The OS should clear the port up for you eventually, but you could be stuck<br />
&nbsp;* playing the waiting game.<br />
&nbsp;*<br />
&nbsp;* This particular version of the script has only been tested very lightly.<br />
&nbsp;* Deploy at your own peril <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  YMMV.<br />
&nbsp;*<br />
&nbsp;* - Ammon Lauritzen [12/13/07]<br />
&nbsp;*/</span></p>
<p><span style="color:#FF9933; font-style:italic;">// define the xml policy &quot;file&quot;</span><br />
<span style="color:#0000FF;">$policy_file</span> = <br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;'</span>.<span style="color:#FF0000;">'?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?'</span>.<span style="color:#FF0000;">'&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;cross-domain-policy xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd&quot;&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;allow-access-from domain=&quot;*&quot; to-ports=&quot;*&quot; secure=&quot;false&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF0000;">'&lt;site-control permitted-cross-domain-policies=&quot;master-only&quot; /&gt;'</span>.<br />
&nbsp; &nbsp; <span style="color:#FF0000;">'&lt;/cross-domain-policy&gt;'</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// make sure everything launches correctly</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/posix_getuid"><span style="color:#000066;">posix_getuid</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> != <span style="color:#CC66CC;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"You must run this script as root.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$sock</span> = @<a href="http://www.php.net/socket_create"><span style="color:#000066;">socket_create</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> AF_INET, SOCK_STREAM, SOL_TCP <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Unable to create socket.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_bind"><span style="color:#000066;">socket_bind</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span>, <span style="color:#FF0000;">"0.0.0.0"</span>, <span style="color:#CC66CC;">843</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Unable to bind to port 843.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/socket_listen"><span style="color:#000066;">socket_listen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">"Unable to start listening.<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p><span style="color:#FF9933; font-style:italic;">// start serving policies</span><br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$r</span> = <span style="color:#0000FF;">$w</span> = <span style="color:#0000FF;">$e</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> @<a href="http://www.php.net/socket_select"><span style="color:#000066;">socket_select</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$r</span>, <span style="color:#0000FF;">$w</span>, <span style="color:#0000FF;">$e</span>, <span style="color:#000000; font-weight:bold;">null</span> <span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$conn</span> = @<a href="http://www.php.net/socket_accept"><span style="color:#000066;">socket_accept</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> !== <span style="color:#000000; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// somebody connected, just dump the xml and close</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/socket_write"><span style="color:#000066;">socket_write</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span>, <span style="color:#0000FF;">$policy_file</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/socket_close"><span style="color:#000066;">socket_close</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$conn</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"socket_accept() failed?<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"socket_select() failed?<span style="color:#000099; font-weight:bold;">\\</span><span style="color:#000099; font-weight:bold;">\n</span>"</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">break</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#FF9933; font-style:italic;">// end: listen forever</span></p>
<p><span style="color:#FF9933; font-style:italic;">// clean up</span><br />
<a href="http://www.php.net/socket_close"><span style="color:#000066;">socket_close</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$sock</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">?&gt;</span></div>
</div>
</div>
<p>
I'll try to make my production version of this a bit more suitable for public consumption and release it as soon as I can.</p>
<p>The random #2048 security errors continue, despite having deployed my port 843 policy xml server. Granted, they happen less than before... but they still happen. And even when my policy server isn't running, the errors aren't thrown 100% of the time. This just baffles me. If they were consistent, that would be one thing. But when you get a security error 1 time in 20... that's not security, that's not even a lame deterrent. It's just incentive to hammer the same port over and over again until something finally gives.</p>
<p>Now, I admit that I could be wrong here... but I've re-re-read the documentation on these policies a few times now, and cannot find any reason for the behaviors I'm seeing.</p>
<h3>update</h3>
<p>On April 22nd, 2008, I released a much better, much more reliable version of this daemon. Head <a href='http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/'>over there</a> for more details and source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

