<?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>dan.thoughts</title>
	<atom:link href="http://blog.sosedoff.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sosedoff.com</link>
	<description></description>
	<lastBuildDate>Thu, 14 Jan 2010 13:30:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Back to business</title>
		<link>http://blog.sosedoff.com/2010/01/14/back-to-business/</link>
		<comments>http://blog.sosedoff.com/2010/01/14/back-to-business/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 13:30:26 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=195</guid>
		<description><![CDATA[Im back to my blog, which i havent updated since last october. Need to throw some more articles. 
]]></description>
			<content:encoded><![CDATA[<p>Im back to my blog, which i havent updated since last october. Need to throw some more articles. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/01/14/back-to-business/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to identify your clients` browser type</title>
		<link>http://blog.sosedoff.com/2009/10/02/how-to-identify-your-clients-browser-type/</link>
		<comments>http://blog.sosedoff.com/2009/10/02/how-to-identify-your-clients-browser-type/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 18:44:40 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[agent]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=178</guid>
		<description><![CDATA[Some time ago i wrote a simple regex patterns to determine whether my client crawler bot, mobile client or just regular one. Easy to expand and to use.

function is_mobile&#40;$agent&#41; &#123;
    $pattern = '/(blackberry&#124;motorokr&#124;motorola&#124;sony&#124;windows ce&#124;240x320&#124;176x220&#124;palm&#124;mobile&#124;iphone&#124;ipod&#124;symbian&#124;nokia&#124;samsung&#124;midp)/i';
    return &#40;bool&#41;preg_match&#40;$pattern, $agent&#41;;
&#125;
&#160;
function is_crawler&#40;$agent&#41; &#123;
    pattern = '/(google&#124;yahoo&#124;baidu&#124;bot&#124;webalta&#124;ia_archiver)/';
    return [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago i wrote a simple regex patterns to determine whether my client crawler bot, mobile client or just regular one. Easy to expand and to use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_mobile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$agent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/(blackberry|motorokr|motorola|sony|windows ce|240x320|176x220|palm|mobile|iphone|ipod|symbian|nokia|samsung|midp)/i'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$agent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> is_crawler<span style="color: #009900;">&#40;</span><span style="color: #000088;">$agent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    pattern <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/(google|yahoo|baidu|bot|webalta|ia_archiver)/'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$agent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/10/02/how-to-identify-your-clients-browser-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails-like PHP url router</title>
		<link>http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/</link>
		<comments>http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 16:33:03 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[rails-like merb-like php url route]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=167</guid>
		<description><![CDATA[My previous version of php url router was not good enough, so i`ve done some core modification to the class. Its more flexible now. The whole idea is very similar to Rails` and Merb`s url router (merb is a rails-fork). In fact, previous version doesn`t even support GET variables in the request uri. New version [...]]]></description>
			<content:encoded><![CDATA[<p>My previous version of php url router was not good enough, so i`ve done some core modification to the class. Its more flexible now. The whole idea is very similar to Rails` and Merb`s url router (merb is a rails-fork). In fact, previous version doesn`t even support GET variables in the request uri. New version just merges variables from request string into the same parameters array where other variables are stored. Also there is a command &#8220;default_routes&#8221; that maps default routes like this &#8220;/:controller/:action/:id&#8221;. So, basically you have two options &#8211; use defaults &#038; custom routes or use only custom, which means that all other requests will be ignored. Function default_routes must be declared last. </p>
<p>Here is the sources:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROUTER_DEFAULT_CONTROLLER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'home'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROUTER_DEFAULT_ACTION'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Router <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$request_uri</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$routes</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">,</span> <span style="color: #000088;">$controller_name</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$action</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$params</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$route_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request_uri</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">routes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> map<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">routes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Route<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request_uri</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> default_routes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/:controller'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/:controller/:action'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/:controller/:action/:id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> set_route<span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">route_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$route</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span> <span style="color: #339933;">=</span> ROUTER_DEFAULT_CONTROLLER<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span> <span style="color: #339933;">=</span> ROUTER_DEFAULT_ACTION<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$w</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$w</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller_name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">routes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$route</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_matched</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_route</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$route</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Route <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$is_matched</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$params</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request_uri</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conditions</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$p_names</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$p_values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@:([\w]+)@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p_names</span><span style="color: #339933;">,</span> PREG_PATTERN_ORDER<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$p_names</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$p_names</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$url_regex</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@:[\w]+@'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'regex_url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$url_regex</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'/?'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@^'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url_regex</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'$@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request_uri</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p_values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_names</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_values</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_matched</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_names</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> regex_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conditions</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'('</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conditions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">')'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'([a-zA-Z0-9_\+\-%]+)'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Setup example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Router<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// create router instance </span>
&nbsp;
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'home'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// main page will call controller &quot;Home&quot; with method &quot;index()&quot;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/login'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'auth'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/logout'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'auth'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'logout'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/signup'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'auth'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'signup'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/profile/:action'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'profile'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will call controller &quot;Profile&quot; with dynamic method &quot;:action()&quot;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/users/:id'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'[\d]{1,8}'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// define filters for the url parameters</span>
&nbsp;
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">default_routes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Usage example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$router</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Router<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ... some configs ...</span>
<span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will return name as it appears in url, ex: 'user_images'</span>
<span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller_name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will return processed name of controller</span>
<span style="color: #666666; font-style: italic;">// for example, if class name in url is 'user_images', then 'controller_name' var will be UserImages</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">action</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// if parameter :id presents</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// array(...)</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">route_matched</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// true - if route found, false - if not</span></pre></div></div>

<p>Small and useful class. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date separated MySQL backups</title>
		<link>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/</link>
		<comments>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 14:04:59 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[shell scripting]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=163</guid>
		<description><![CDATA[Here is the bash shell script that makes archived dumps of your database server. All databases are separated from each other and stored into date based folders. 

#!/bin/bash

MyUSER="root"
MyPASS=""
MyHOST="localhost"
NOW="$(date +"%d-%m-%Y")"
STOREDIR="/home/storage/backup/database/by_dates/$NOW"
DBLIST="$(mysql -u $MyUSER -h $MyHOST -Bse 'show databases')"

[ ! -d $STOREDIR ] &#038;&#038; mkdir -p $STOREDIR &#124;&#124; :

for db in $DBLIST
do
	FILE="$STOREDIR/$db.gz"
	mysqldump -u $MyUSER -h $MyHOST $db &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the bash shell script that makes archived dumps of your database server. All databases are separated from each other and stored into date based folders. </p>
<pre>
#!/bin/bash

MyUSER="root"
MyPASS=""
MyHOST="localhost"
NOW="$(date +"%d-%m-%Y")"
STOREDIR="/home/storage/backup/database/by_dates/$NOW"
DBLIST="$(mysql -u $MyUSER -h $MyHOST -Bse 'show databases')"

[ ! -d $STOREDIR ] &#038;&#038; mkdir -p $STOREDIR || :

for db in $DBLIST
do
	FILE="$STOREDIR/$db.gz"
	mysqldump -u $MyUSER -h $MyHOST $db | gzip -9 > $FILE
done
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog is back!</title>
		<link>http://blog.sosedoff.com/2009/09/17/blog-is-back/</link>
		<comments>http://blog.sosedoff.com/2009/09/17/blog-is-back/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 10:22:56 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/2009/09/17/blog-is-back/</guid>
		<description><![CDATA[Recovered blog. Will start write some other stuff soon.
]]></description>
			<content:encoded><![CDATA[<p>Recovered blog. Will start write some other stuff soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/09/17/blog-is-back/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to deploy Sinatra / Merb applications with nginx</title>
		<link>http://blog.sosedoff.com/2009/07/04/how-to-deploy-sinatra-merb-applications-with-nginx/</link>
		<comments>http://blog.sosedoff.com/2009/07/04/how-to-deploy-sinatra-merb-applications-with-nginx/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 15:59:47 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[nginx]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[sinatra]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=158</guid>
		<description><![CDATA[Developing with Merb or Sinatra ? Like web server nginx ? Ok, lets go.
Basically, Merb/Sinatra uses thin (based on eventmachine) server module, so it is working as stand-alone application. So, there is a way to connect nginx with these applications. Very simple &#8211; all you need to proxy requests to backend server. And all static [...]]]></description>
			<content:encoded><![CDATA[<p>Developing with <a href="http://merbivore.com/">Merb</a> or <a href="http://sinatrarb.com/">Sinatra</a> ? Like web server <a href="http://sysoev.ru/nginx">nginx</a> ? Ok, lets go.</p>
<p>Basically, Merb/Sinatra uses <a href="http://code.macournoyer.com/thin/">thin</a> (based on <a href="http://rubyeventmachine.com/">eventmachine</a>) server module, so it is working as stand-alone application. So, there is a way to connect nginx with these applications. Very simple &#8211; all you need to proxy requests to backend server. And all static files will be served by nginx.</p>
<pre>
upstream your_app {
  server 127.0.0.1:YOUR_PORT_NUMBER;
}

server {
  server_name YOUR_SERVERNAME;
  listen 80;
  charset utf-8;
  client_max_body_size 64M; # maximum of proxied filesize (for uploads)

  location / {
    root /path/to/app/root;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect false;
    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      proxy_pass http://your_app;
      break;
    }
  }

  # static content (images, flash, styles, etc.)
  location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ {
    root /path/to/your/public/dir;
    access_log off;
  }
}
</pre>
<p>Then, all you need &#8211; start your application in daemon mode. Dont forget to bind application server to listen only on local address (127.0.0.1). </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/07/04/how-to-deploy-sinatra-merb-applications-with-nginx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple php url routing controller</title>
		<link>http://blog.sosedoff.com/2009/07/04/simpe-php-url-routing-controller/</link>
		<comments>http://blog.sosedoff.com/2009/07/04/simpe-php-url-routing-controller/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 09:50:46 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[dispatch]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=154</guid>
		<description><![CDATA[!!! CONSIDER THIS AS AN UPDATE:Rails-like PHP Url Router
Here is simple url parser, that produces output parameters lile: controller, action, parameters. Also, it support simple virtual key rules, &#8220;rails&#8221; style. 
For example, url like &#8220;http://foo.bar/book/view/12&#8243; will produce parameters: controller = book, action = view, id = 12. 
Here is php code:

&#60;?
&#160;
/* ------------------------------------------------------------- */
/* URL Router [...]]]></description>
			<content:encoded><![CDATA[<p><b>!!! CONSIDER THIS AS AN UPDATE:</b><a href="http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/">Rails-like PHP Url Router</a></p>
<p>Here is simple url parser, that produces output parameters lile: controller, action, parameters. Also, it support simple virtual key rules, &#8220;rails&#8221; style. </p>
<p>For example, url like &#8220;http://foo.bar/book/view/12&#8243; will produce parameters: controller = book, action = view, id = 12. </p>
<p>Here is php code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* ------------------------------------------------------------- */</span>
<span style="color: #666666; font-style: italic;">/* URL Router class */</span>
<span style="color: #666666; font-style: italic;">/* ------------------------------------------------------------- */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Router <span style="color: #009900;">&#123;</span>
  static protected <span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
  static protected <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span>
  static protected <span style="color: #000088;">$action</span><span style="color: #339933;">;</span>
  static protected <span style="color: #000088;">$params</span><span style="color: #339933;">;</span>
  static protected <span style="color: #000088;">$rules</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span><span style="color: #009900;">&#41;</span> and <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span> instanceof <span style="color: #000000; font-weight: bold;">self</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> arrayClean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>  
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> ruleMatch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    
    <span style="color: #000088;">$ruleItems</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">arrayClean</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$ruleItems</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dataItems</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">arrayClean</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$dataItems</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ruleItems</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dataItems</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ruleItems</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ruleKey</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ruleValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^:[\w]{1,}$/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$ruleValue</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$ruleValue</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ruleValue</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ruleValue</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dataItems</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ruleKey</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ruleValue</span><span style="color: #339933;">,</span><span style="color: #000088;">$dataItems</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ruleKey</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> defaultRoutes<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// process default routes</span>
    <span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// remove empty blocks</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// extract data</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$items</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  protected <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$rules</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$isCustom</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$rules</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$rules</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ruleKey</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ruleData</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">ruleMatch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ruleKey</span><span style="color: #339933;">,</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>          
          <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ruleData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'controller'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ruleData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$isCustom</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$isCustom</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">defaultRoutes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$controller</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'home'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$action</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> addRule<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$rules</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$target</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getController<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$controller</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$action</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getParams<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$params</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getParam<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Basic usage:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$router</span> <span style="color: #339933;">=</span> Router<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// init router</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addRule</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/books/:id/:keyname'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'books'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// add simple rule</span>
<span style="color: #666666; font-style: italic;">// add some more rules</span>
<span style="color: #000088;">$router</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// execute router</span></pre></div></div>

<p>So, as you can see, the router class can be accessible from anywhere in your code. Just because the class has beed designed as one-instance object, you cannot call for new instance. To get instance of router you need to call Router::getInstance() instead.<br />
We have added simple rule for url`s like this &#8216;/books/12345/this-is-a-book-keyname&#8217;. Default routing system will identify this url incorrectly (controller => books, action => 12345). So the action 12345 not exists. That`s why we need to add custom rule. It will parse this url correctly: controller => books, action => view. And the rest of parameters will be automatically added to parameteres array. To access this array you need to call $router->getParams(); If you need to get just one parameter, call directly: $var = $router->getParam(0). </p>
<p>This router is really simple and can be used in small web sites that not require complete framework. Also it is fast.</p>
<p><a href='http://files.sosedoff.com/d55d3f5e/'>Download file Router.php</a><br />
<a href="http://pastie.org/private/rdpzg80xh6ksezoozyikyw">View paste</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/07/04/simpe-php-url-routing-controller/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Install Git on CentOS 5.2</title>
		<link>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/</link>
		<comments>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 03:28:19 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[centos]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=151</guid>
		<description><![CDATA[First, we need to install all dependencies:

# yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel

Next, get the git 1.6.x sources:

# wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz

Then, unpack and cd into git sources folder and install it:

# make &#38;&#38; make install &#38; make clean

That`s it, now you`ll have git system ready to go.
]]></description>
			<content:encoded><![CDATA[<p>First, we need to install all dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel</span></pre></div></div>

<p>Next, get the git 1.6.x sources:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz</span></pre></div></div>

<p>Then, unpack and cd into git sources folder and install it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># make &amp;&amp; make install &amp; make clean</span></pre></div></div>

<p>That`s it, now you`ll have git system ready to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jabber bot with Ruby</title>
		<link>http://blog.sosedoff.com/2009/05/20/jabber-bot-with-ruby/</link>
		<comments>http://blog.sosedoff.com/2009/05/20/jabber-bot-with-ruby/#comments</comments>
		<pubDate>Wed, 20 May 2009 22:20:39 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[jabber]]></category>
		<category><![CDATA[xmpp4r]]></category>
		<category><![CDATA[xmpp4r-simple]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=143</guid>
		<description><![CDATA[Ruby has very powerful tools to create random jabber bots with XMPP protocol. In this article i`ll show just a small sample with a few commands available. 
I got xmpp4r-simple library from Google Code page &#8211; http://code.google.com/p/xmpp4r-simple/. Its kinda old, not updated since 2006, but the sources are really easy to read and understand, and [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby has very powerful tools to create random jabber bots with XMPP protocol. In this article i`ll show just a small sample with a few commands available. </p>
<p>I got xmpp4r-simple library from Google Code page &#8211; <a href="http://code.google.com/p/xmpp4r-simple/">http://code.google.com/p/xmpp4r-simple/</a>. Its kinda old, not updated since 2006, but the sources are really easy to read and understand, and probably modify. </p>
<p>First, we need to install dependencies (gems):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$ gem install xmpp4r
$ gem install xmpp4r<span style="color:#006600; font-weight:bold;">-</span>simple</pre></div></div>

<p>And now, the actual ruby code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'xmpp4r'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'xmpp4r-simple'</span>
&nbsp;
<span style="color:#ff6633; font-weight:bold;">$jabber_login</span> = <span style="color:#996600;">&quot;YOUR_JABBER_ID_HERE&quot;</span> <span style="color:#008000; font-style:italic;"># test@jabber.org</span>
<span style="color:#ff6633; font-weight:bold;">$jabber_password</span> = <span style="color:#996600;">&quot;YOUR_PASSWORD_HERE&quot;</span> 
<span style="color:#ff6633; font-weight:bold;">$client</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># create jabber connection</span>
<span style="color:#9966CC; font-weight:bold;">def</span> jabber_connect<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    conn = <span style="color:#6666ff; font-weight:bold;">Jabber::Simple</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>$jabber_login,$jabber_password<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> conn
  <span style="color:#9966CC; font-weight:bold;">rescue</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># send message to jabber client</span>
<span style="color:#9966CC; font-weight:bold;">def</span> jabber_respond<span style="color:#006600; font-weight:bold;">&#40;</span>to, msg<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#ff6633; font-weight:bold;">$client</span>.<span style="color:#9900CC;">deliver</span><span style="color:#006600; font-weight:bold;">&#40;</span>to,msg,:chat<span style="color:#006600; font-weight:bold;">&#41;</span>	
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># get sender jabber id</span>
<span style="color:#9966CC; font-weight:bold;">def</span> jabber_get_jid<span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span>
  matches = str.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">-</span>z\d_.\<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">32</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>@<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">-</span>z\d.<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">32</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>\<span style="color:#006600; font-weight:bold;">//</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;">&quot;#{matches[1]}@#{matches[2]}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># ------------------------------------------------------------------- #</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># send server time</span>
<span style="color:#9966CC; font-weight:bold;">def</span> app_time<span style="color:#006600; font-weight:bold;">&#40;</span>jid<span style="color:#006600; font-weight:bold;">&#41;</span>
  jabber_respond<span style="color:#006600; font-weight:bold;">&#40;</span>jid, <span style="color:#996600;">&quot;Server time is: #{Time.now}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># send some 'help' :)</span>
<span style="color:#9966CC; font-weight:bold;">def</span> app_help<span style="color:#006600; font-weight:bold;">&#40;</span>jid<span style="color:#006600; font-weight:bold;">&#41;</span>
  jabber_respond<span style="color:#006600; font-weight:bold;">&#40;</span>jid, <span style="color:#996600;">&quot;Nobody can help you. You`re alone.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># process received message</span>
<span style="color:#9966CC; font-weight:bold;">def</span> app_parse_msg<span style="color:#006600; font-weight:bold;">&#40;</span>jid, msg<span style="color:#006600; font-weight:bold;">&#41;</span>
  cmd = msg.<span style="color:#9900CC;">body</span>.<span style="color:#9900CC;">strip</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> cmd
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>^help$<span style="color:#006600; font-weight:bold;">/</span>i <span style="color:#9966CC; font-weight:bold;">then</span> app_help<span style="color:#006600; font-weight:bold;">&#40;</span>jid<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>^time$<span style="color:#006600; font-weight:bold;">/</span>i <span style="color:#9966CC; font-weight:bold;">then</span> app_time<span style="color:#006600; font-weight:bold;">&#40;</span>jid<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>^jid$<span style="color:#006600; font-weight:bold;">/</span>i <span style="color:#9966CC; font-weight:bold;">then</span> jabber_respond<span style="color:#006600; font-weight:bold;">&#40;</span>jid, <span style="color:#996600;">&quot;Your jabber id: #{jid}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        jabber_respond<span style="color:#006600; font-weight:bold;">&#40;</span>jid, <span style="color:#996600;">&quot;Unknown command. Try something different.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> ex
    jabber_respond<span style="color:#006600; font-weight:bold;">&#40;</span>jid, <span style="color:#996600;">&quot;SYSTEM_ERROR: #{ex}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># ------------------------------------------------------------------- #</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Connecting to jabber server...&quot;</span>
<span style="color:#ff6633; font-weight:bold;">$client</span> = jabber_connect<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#ff6633; font-weight:bold;">$client</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Connected. Waiting for messages...&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">loop</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#ff6633; font-weight:bold;">$client</span>.<span style="color:#9900CC;">received_messages</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>message<span style="color:#006600; font-weight:bold;">|</span>
      jid = jabber_get_jid<span style="color:#006600; font-weight:bold;">&#40;</span>message.<span style="color:#9900CC;">from</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Received message from #{jid}: #{message.body}&quot;</span>
      app_parse_msg<span style="color:#006600; font-weight:bold;">&#40;</span>jid, message<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">sleep</span> <span style="color:#006666;">0.1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Cannot connect. Please try again later.&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I think, the comments in source code are enough to understand what this bot supposed to do. There are 3 commands available: &#8216;help&#8217;, &#8216;time&#8217;, &#8216;jid&#8217;</p>
<p>Download script here &#8211; <a href="http://files.sosedoff.com/204ab61c/">http://files.sosedoff.com/204ab61c/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/05/20/jabber-bot-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WebDAV client in ruby</title>
		<link>http://blog.sosedoff.com/2009/05/02/webdav-client-in-ruby/</link>
		<comments>http://blog.sosedoff.com/2009/05/02/webdav-client-in-ruby/#comments</comments>
		<pubDate>Sat, 02 May 2009 05:03:30 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[sockets]]></category>
		<category><![CDATA[webdav]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=125</guid>
		<description><![CDATA[Here is a simple example how to make native WebDAV client with Ruby sockets. No additional gems or extensions needed &#8211; just all basic classes.

class WebDAV
	attr_reader :host, :port, :protocol, :chunk_size
	@socket = nil
&#160;
	def initialize&#40;host,port=80,protocol='HTTP/1.1',chunk=8096&#41;
		@host = host.to_s
		@port = port.to_i
		@protocol = protocol
		@chunk_size = chunk.to_i
	end
&#160;
	def build_header&#40;method, path, content_length=nil&#41;
		header = &#34;#{method} #{path} #{@protocol} \r\n&#34;
		header += &#34;Content-Length: #{content_length}\r\n&#34; if !content_length.nil?
		header += [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple example how to make native WebDAV client with Ruby sockets. No additional gems or extensions needed &#8211; just all basic classes.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> WebDAV
	attr_reader <span style="color:#ff3333; font-weight:bold;">:host</span>, <span style="color:#ff3333; font-weight:bold;">:port</span>, <span style="color:#ff3333; font-weight:bold;">:protocol</span>, <span style="color:#ff3333; font-weight:bold;">:chunk_size</span>
	<span style="color:#0066ff; font-weight:bold;">@socket</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>host,port=<span style="color:#006666;">80</span>,protocol=<span style="color:#996600;">'HTTP/1.1'</span>,chunk=<span style="color:#006666;">8096</span><span style="color:#006600; font-weight:bold;">&#41;</span>
		<span style="color:#0066ff; font-weight:bold;">@host</span> = host.<span style="color:#9900CC;">to_s</span>
		<span style="color:#0066ff; font-weight:bold;">@port</span> = port.<span style="color:#9900CC;">to_i</span>
		<span style="color:#0066ff; font-weight:bold;">@protocol</span> = protocol
		<span style="color:#0066ff; font-weight:bold;">@chunk_size</span> = chunk.<span style="color:#9900CC;">to_i</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> build_header<span style="color:#006600; font-weight:bold;">&#40;</span>method, path, content_length=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
		header = <span style="color:#996600;">&quot;#{method} #{path} #{@protocol} <span style="color:#000099;">\r</span><span style="color:#000099;">\n</span>&quot;</span>
		header <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">&quot;Content-Length: #{content_length}<span style="color:#000099;">\r</span><span style="color:#000099;">\n</span>&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> !content_length.<span style="color:#0000FF; font-weight:bold;">nil</span>?
		header <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">&quot;Host: #{@host}<span style="color:#000099;">\r</span><span style="color:#000099;">\n</span>&quot;</span>
		header <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#996600;">&quot;Connection: close<span style="color:#000099;">\r</span><span style="color:#000099;">\n</span><span style="color:#000099;">\r</span><span style="color:#000099;">\n</span>&quot;</span>
		<span style="color:#0000FF; font-weight:bold;">return</span> header
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> request<span style="color:#006600; font-weight:bold;">&#40;</span>method, path<span style="color:#006600; font-weight:bold;">&#41;</span>
		<span style="color:#CC0066; font-weight:bold;">open</span>
		header = build_header<span style="color:#006600; font-weight:bold;">&#40;</span>method, path<span style="color:#006600; font-weight:bold;">&#41;</span>
		<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@socket</span>.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>header<span style="color:#006600; font-weight:bold;">&#41;</span> == header.<span style="color:#9900CC;">length</span> <span style="color:#9966CC; font-weight:bold;">then</span>
			<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@socket</span>.<span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> delete<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
		request<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'DELETE'</span>, path<span style="color:#006600; font-weight:bold;">&#41;</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> head<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
		request<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'HEAD'</span>, path<span style="color:#006600; font-weight:bold;">&#41;</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> mkcol<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
		request<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'MKCOL'</span>, path<span style="color:#006600; font-weight:bold;">&#41;</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> put<span style="color:#006600; font-weight:bold;">&#40;</span>path, localfile, auto_head=<span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
		<span style="color:#9966CC; font-weight:bold;">if</span> !<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>localfile<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> !<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">readable</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>localfile<span style="color:#006600; font-weight:bold;">&#41;</span>
			<span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;File not exists or not accessible for reading!&quot;</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
		<span style="color:#CC0066; font-weight:bold;">open</span>
&nbsp;
		datalen = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#40;</span>localfile<span style="color:#006600; font-weight:bold;">&#41;</span>
		header = build_header<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'PUT'</span>, path, datalen<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
		<span style="color:#9966CC; font-weight:bold;">begin</span>
			<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@socket</span>.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>header<span style="color:#006600; font-weight:bold;">&#41;</span> == header.<span style="color:#9900CC;">length</span> <span style="color:#9966CC; font-weight:bold;">then</span>
				written = <span style="color:#006666;">0</span>
				<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>localfile,<span style="color:#996600;">'r'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> 
					<span style="color:#9966CC; font-weight:bold;">until</span> f.<span style="color:#9900CC;">eof</span>? <span style="color:#9966CC; font-weight:bold;">do</span>
						written <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#0066ff; font-weight:bold;">@socket</span>.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>f.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>@chunk_size<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
					<span style="color:#9966CC; font-weight:bold;">end</span>
				<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
				<span style="color:#9966CC; font-weight:bold;">if</span> written == datalen
					close
					<span style="color:#9966CC; font-weight:bold;">if</span> !auto_head
						<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span>
					<span style="color:#9966CC; font-weight:bold;">else</span>
						<span style="color:#0000FF; font-weight:bold;">return</span> head<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
					<span style="color:#9966CC; font-weight:bold;">end</span>
				<span style="color:#9966CC; font-weight:bold;">end</span>
			<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
			<span style="color:#CC0066; font-weight:bold;">puts</span> e
			<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">open</span>
		<span style="color:#9966CC; font-weight:bold;">begin</span> 
			<span style="color:#0066ff; font-weight:bold;">@socket</span> = TCPSocket.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>@host,@port<span style="color:#006600; font-weight:bold;">&#41;</span>
			<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span>
		<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
			<span style="color:#CC0066; font-weight:bold;">puts</span> e
			<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> close
		<span style="color:#9966CC; font-weight:bold;">begin</span>
			<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@socket</span>.<span style="color:#9900CC;">close</span>
		<span style="color:#9966CC; font-weight:bold;">rescue</span> 
			<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This class supports only basic http/dav methods (PUT, DELETE, MKCOL, HEAD) and can be extended very easily and designed to work with all files, reading them by small chunks (default is 8096 bytes).<br />
Im using this class sometimes with nginx.  </p>
<p>Deps:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'socket'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'digest'</span></pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># create connection</span>
conn = WebDAV.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'your.host.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># upload file (without autocheck), return true/false value</span>
result = conn.<span style="color:#9900CC;">put</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/test.mp3'</span>,<span style="color:#996600;">'/home/.../..../..../file.mp3'</span>, <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># upload file with autocheck, returns http response code (201, 404, ... ) so you`ll know what exactly happened</span>
result = conn.<span style="color:#9900CC;">put</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/test2.mp3'</span>,<span style="color:#996600;">'/home/.../file.mp3'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Also, here is a wrapper class to produce MD5, SHA1 file hashes that supports big files.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FileHash 
	<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">md5</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
		d = <span style="color:#6666ff; font-weight:bold;">Digest::MD5</span>.<span style="color:#9900CC;">new</span>
		<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path,<span style="color:#996600;">'r'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> 
			d.<span style="color:#9900CC;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span>f.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">8192</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">until</span> f.<span style="color:#9900CC;">eof</span>?
		<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#0000FF; font-weight:bold;">return</span> d.<span style="color:#9900CC;">hexdigest</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">sha1</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
		d = <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">new</span>
		<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path,<span style="color:#996600;">'r'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> 
			d.<span style="color:#9900CC;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span>f.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">8192</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">until</span> f.<span style="color:#9900CC;">eof</span>?
		<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#0000FF; font-weight:bold;">return</span> d.<span style="color:#9900CC;">hexdigest</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">FileHash.<span style="color:#9900CC;">md5</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/file'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
FileHash.<span style="color:#9900CC;">sha1</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/path/to/file'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This webdav class not pretending to be stable in production environment, but can be useful for some &#8220;one-time&#8221; tasks with less code. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/05/02/webdav-client-in-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
