<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Tips &#38; Tricks</title>
	<atom:link href="http://tech.blodji.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.blodji.com</link>
	<description>Tips on various topics - PHP, Windows, FreeBSD, etc.</description>
	<pubDate>Wed, 09 Jul 2008 16:23:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Logical algorithm for previous and next items</title>
		<link>http://tech.blodji.com/2008/07/09/logical-algorithm-for-previous-and-next-items/</link>
		<comments>http://tech.blodji.com/2008/07/09/logical-algorithm-for-previous-and-next-items/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 16:22:54 +0000</pubDate>
		<dc:creator>strahil</dc:creator>
		
		<category><![CDATA[php]]></category>

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

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

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

		<guid isPermaLink="false">http://tech.blodji.com/?p=9</guid>
		<description><![CDATA[This can be used on pictures, news, shop items, etc. with JavaScript events for &#8220;previous&#8221; and &#8220;next&#8221;

    if (prev === null) {
      prev = current &#038;&#038; next = current + 1
    } elseif (next === null) {
      next = [...]]]></description>
			<content:encoded><![CDATA[<p>This can be used on pictures, news, shop items, etc. with JavaScript events for &#8220;previous&#8221; and &#8220;next&#8221;</p>
<pre>
    if (prev === null) {
      prev = current &#038;&#038; next = current + 1
    } elseif (next === null) {
      next = current &#038;&#038; prev = current -1
    } else {
      if (action == prev) {
        next = current
        prev = current - 1
      } elseif (action == next) {
        prev = current
        next = current + 1
      } else {
        prev = current - 1
        next = current + 1
      }
    }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.blodji.com/2008/07/09/logical-algorithm-for-previous-and-next-items/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Generate past years (with range) using PHP</title>
		<link>http://tech.blodji.com/2008/06/23/generate-past-years-with-range-using-php/</link>
		<comments>http://tech.blodji.com/2008/06/23/generate-past-years-with-range-using-php/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 12:51:29 +0000</pubDate>
		<dc:creator>strahil</dc:creator>
		
		<category><![CDATA[php]]></category>

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

		<guid isPermaLink="false">http://tech.blodji.com/?p=8</guid>
		<description><![CDATA[Code:

    $cy = date('Y'); // current year
    $ld = substr($cy, 3); // last digit
    $ftd = substr($cy, 0, 3); // first three digits

    for ($i = $ld; $i > $ld - 3; $i--) {
      $j = $i;
  [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Code:</strong></p>
<pre>
    $cy = date('Y'); // current year
    $ld = substr($cy, 3); // last digit
    $ftd = substr($cy, 0, 3); // first three digits

    for ($i = $ld; $i > $ld - 3; $i--) {
      $j = $i;
      if ($i < 0) {
        if ($j == -1) {
          $ftd--;
        }
        $j = (9 + $i) + 1;
      }
      $years[] = $ftd . $j;
      $newld = $j;
    }

    $ld = --$newld;

    if ($ld > 0) {
      for ($i = $ld; $i > $ld - 5 || $i == 0; $i&#8211;) {
        if ($i > 0) {
          if ($i == $ld) {
            $ys = $ftd . $i;
          }

          if ($i-1 == $ld - 5) {
            if ($i == 9) {
              $ys2 = $ftd . ++$i;
            } else {
              $ys2 = $ftd . $i;
            }
            $newld = $i;
            break;
          }
        } else {
          $ys2 = $ftd . $i;
          $newld = 0;
          $ftd&#8211;;
          break;
        }
      }
      if ($ys2[3] == 1) {
        $ys2[3] = 0;
        $ftd&#8211;;
      }
      $years[] = $ys .&#8217;-&#8217;. $ys2;
    } else {
      if (!in_array($ftd . 0, $years)) {
        $years[] = $ftd . 0;
      }
      $newld = 0;
      $ftd&#8211;;
    }

    $ld = &#8211;$newld;

    if ($ld > 0) {
      for ($i = $ld; $i > 0; $i&#8211;) {
        if ($i == $ld) {
          $ys = $ftd . $ld .&#8217;-';
        }

        if ($i-1 == 0) {
          $ys .= $ftd . 0;
          $ftd&#8211;;
        }
      }
      $years[] = $ys;
    }

    $ftwod = substr($ftd, 0, 2); // first two digits
    $td = substr($ftd, 2); // third digit
    $j = 9;

    for ($i = $td; $i >= 0; $i&#8211;) {
      $fy = $ftd . $i . $j;
      $years[] = $ftwod . $i . &#8216;9-&#8217;. $ftwod . $i . &#8216;0&#8242;;
    }
</pre>
<p><strong>Output:</strong></p>
<pre>
  'years' =>
    array
      0 => string '2008' (length=4)
      1 => string '2007' (length=4)
      2 => string '2006' (length=4)
      3 => string '2005-2000' (length=9)
      4 => string '1999-1990' (length=9)
      5 => string '1989-1980' (length=9)
      6 => string '1979-1970' (length=9)
      7 => string '1969-1960' (length=9)
      8 => string '1959-1950' (length=9)
      9 => string '1949-1940' (length=9)
      10 => string '1939-1930' (length=9)
      11 => string '1929-1920' (length=9)
      12 => string '1919-1910' (length=9)
      13 => string '1909-1900' (length=9)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.blodji.com/2008/06/23/generate-past-years-with-range-using-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP function for reading DOM XML into an array</title>
		<link>http://tech.blodji.com/2008/06/11/php-function-for-reading-dom-xml-into-an-array/</link>
		<comments>http://tech.blodji.com/2008/06/11/php-function-for-reading-dom-xml-into-an-array/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 08:03:07 +0000</pubDate>
		<dc:creator>strahil</dc:creator>
		
		<category><![CDATA[php]]></category>

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

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

		<guid isPermaLink="false">http://tech.blodji.com/2008/06/11/php-function-for-reading-dom-xml-into-an-array/</guid>
		<description><![CDATA[
$xml = read($dom);


  function read($node) {
    $array = array();
    if ($node->firstChild !== null) {
      $current = $node->firstChild;
    } else {
      return;
    }

    while (true) {
     [...]]]></description>
			<content:encoded><![CDATA[<p><code><br />
$xml = read($dom);<br />
</code></p>
<pre>
  function read($node) {
    $array = array();
    if ($node->firstChild !== null) {
      $current = $node->firstChild;
    } else {
      return;
    }

    while (true) {
      switch ($current->nodeType) {
        case 1: // XML_ELEMENT_NODE
          $array[$current->nodeName][] = $this->read($current);
          break;
        case 3: // XML_TEXT_NODE
        case 4: // XML_CDATA_SECTION_NODE
          if (strlen(trim($current->nodeValue)) > 0) {
            return $current->nodeValue;
          }
          break;
        default:
          echo $current->nodeType;
          break;
      }

      if ($current->nextSibling !== null) {
        $current = $current->nextSibling;
      } else {
        return $array;
      }
    }

    return $array;
  }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.blodji.com/2008/06/11/php-function-for-reading-dom-xml-into-an-array/feed/</wfw:commentRss>
		</item>
		<item>
		<title>nginx configuration - php (fastcgi), perl, proxy, rrd, nagios</title>
		<link>http://tech.blodji.com/2008/02/25/nginx-configuration-php-fastcgi-perl-proxy-rrd-nagios/</link>
		<comments>http://tech.blodji.com/2008/02/25/nginx-configuration-php-fastcgi-perl-proxy-rrd-nagios/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 13:50:52 +0000</pubDate>
		<dc:creator>strahil</dc:creator>
		
		<category><![CDATA[nginx]]></category>

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

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

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

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

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

		<guid isPermaLink="false">http://tech.blodji.com/?p=4</guid>
		<description><![CDATA[nginx.conf

worker_processes 5;

error_log logs/error.log;
error_log logs/error.log info;

events {
  use kqueue;
  worker_connections 2048;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 64;

  log_format main ‘$remote_addr - $remote_user [$time_local] $request ‘
                  ‘”$status” $body_bytes_sent “$http_referer” ‘
    [...]]]></description>
			<content:encoded><![CDATA[<p>nginx.conf</p>
<pre>
worker_processes 5;

error_log logs/error.log;
error_log logs/error.log info;

events {
  use kqueue;
  worker_connections 2048;
}

http {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 64;

  log_format main ‘$remote_addr - $remote_user [$time_local] $request ‘
                  ‘”$status” $body_bytes_sent “$http_referer” ‘
                  ‘”$http_user_agent” “$http_x_forwarded_for”‘;

  access_log logs/access.log main;

  sendfile on;

  keepalive_timeout 65;

  tcp_nopush on;

  upstream proxy {
    server 192.168.0.2:80 weight=2;
    server 192.168.0.3:80;
  }

  server {
    listen 80;
    server_name my.example.com 192.168.0.1;

    access_log logs/my.example.com.access.log main;

    location /status {
      stub_status on;
      access_log off;
      allow 192.168.0.1;
      deny all;
    }

    location / {
      root /usr/local/www/status;
      index index.php;
      allow 192.168.100.1;
      deny all;
    }

    location ~ \.php$ {
      fastcgi_pass unix:/tmp/php-fastcgi.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /usr/local/www/status$fastcgi_script_name;
      include fastcgi_params;
    }

    location /nagios {
      root /usr/local/www;
      allow 192.168.100.1;
      deny all;
    }

    location ~ \.cgi$ {
      root /usr/local/www/nagios/cgi-bin;
      rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
      fastcgi_index index.cgi;
      allow 192.168.100.1;
      deny all;
      fastcgi_pass unix:/tmp/perl_cgi-dispatch.sock;
      fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;
      fastcgi_param SCRIPT_FILENAME /usr/local/www/nagios/cgi-bin$fastcgi_script_name;
      include fastcgi_params;
    }
  }

  server {
    listen 80;
    server_name proxy.example.com;

    access_log logs/proxy.example.com.access.log main;

    location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_pass http://proxy;
    }
  }
}
</pre>
<p>nginx-rrd.conf</p>
<pre>
#####################################################
#
# dir where rrd databases are stored
RRD_DIR="/var/spool/nginx-rrd";
# dir where png images are presented
WWW_DIR="/usr/local/www/status";
# process nice level
NICE_LEVEL="-19";
# bin dir
BIN_DIR="/usr/sbin";
# servers to test
# server_utl;server_name
SERVERS_URL="http://my.example.com/status;my.example.com http://192.168.0.2/status;2"
</pre>
<p>fastcgi-php (creates php fastcgi socket, executable init script for FreeBSD, can be ported easily to other systems)</p>
<pre>
. /etc/rc.subr

name="fcgiphp"
rcvar=`set_rcvar`

load_rc_config $name

: ${fcgiphp_enable="NO"}
: ${fcgiphp_bin_path="/usr/local/bin/php-cgi"}
: ${fcgiphp_user="www"}
: ${fcgiphp_group="www"}
: ${fcgiphp_children="10"}
: ${fcgiphp_port="8002"}
: ${fcgiphp_socket="/tmp/php-fastcgi.sock"}
: ${fcgiphp_env="SHELL PATH USER"}
: ${fcgiphp_max_requests="500"}
: ${fcgiphp_addr="localhost"}

pidfile=/var/run/fastcgi/fcgiphp.pid
procname="${fcgiphp_bin_path}"
command_args="/usr/local/bin/spawn-fcgi -f ${fcgiphp_bin_path} -u ${fcgiphp_user} -g ${fcgiphp_group} -C ${fcgiphp_children} -P ${pidfile}"
start_precmd=start_precmd
stop_postcmd=stop_postcmd

start_precmd()
{
        PHP_FCGI_MAX_REQUESTS="${fcgiphp_max_requests}"
        FCGI_WEB_SERVER_ADDRS=$fcgiphp_addr
        export PHP_FCGI_MAX_REQUESTS
        export FCGI_WEB_SERVER_ADDRS
        allowed_env="${fcgiphp_env} PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"
# copy the allowed environment variables
        E=""
        for i in $allowed_env; do
                eval "x=\$$i"
                E="$E $i=$x"
        done
        command="env - $E"

        if [ -n "${fcgiphp_socket}" ]; then
                command_args=&#8221;${command_args} -s ${fcgiphp_socket}&#8221;
        elif [ -n "${fcgiphp_port}" ]; then
                command_args=&#8221;${command_args} -p ${fcgiphp_port}&#8221;
        else
                echo &#8220;socket or port must be specified!&#8221;
                exit
        fi
}

stop_postcmd()
{
        rm -f ${pidfile}
#       eval &#8220;ipcs | awk &#8216;{ if (\$5 == \&#8221;${fcgiphp_user}\&#8221;) print \&#8221;ipcrm -s \&#8221;\$2}&#8217; | /bin/sh&#8221;
}

run_rc_command &#8220;$1&#8243;
</pre>
<p>perl-fcgi.pl (creates perl socket) - you need the FCGI perl module for this</p>
<pre>
#!/usr/bin/perl

use FCGI;
#perl -MCPAN -e 'install FCGI'
use Socket;

#this keeps the program alive or something after exec'ing perl scripts
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit}; if ($@) { exit unless $@ =~ /^fakeexit/; } ;

&amp;main;

sub main {
        #$socket = FCGI::OpenSocket( ":3461", 10 ); #use IP sockets
        $socket = FCGI::OpenSocket( "/tmp/perl_cgi-dispatch.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folde
r!!
        $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
        if ($request) { request_loop()};
            FCGI::CloseSocket( $socket );
}

sub request_loop {
        while( $request-&gt;Accept() &gt;= 0 ) {

           #processing any STDIN input from WebServer (for CGI-POST actions)
           $stdin_passthrough ='';
           $req_len = 0 + $req_params{'CONTENT_LENGTH'};
           if (($req_params{'REQUEST_METHOD'} eq 'POST') &amp;&amp; ($req_len != 0) ){
                        while ($req_len) {
                            $stdin_passthrough .= getc(STDIN);
                            $req_len--;
                        }
            }

            #running the cgi app
            if ( (-x $req_params{SCRIPT_FILENAME}) &amp;&amp;  #can I execute this?
                 (-s $req_params{SCRIPT_FILENAME}) &amp;&amp;  #Is this file empty?
                 (-r $req_params{SCRIPT_FILENAME})     #can I read this file?
            ){
                foreach $key ( keys %req_params){
                   $ENV{$key} = $req_params{$key};
                }
                #http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
                open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app ret
urned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
                if ($cgi_app) {print &lt;$cgi_app&gt;; close $cgi_app;}
            }
            else {
                print("Content-type: text/plain\r\n\r\n");
                print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
            }

        }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.blodji.com/2008/02/25/nginx-configuration-php-fastcgi-perl-proxy-rrd-nagios/feed/</wfw:commentRss>
		</item>
		<item>
		<title>recursive unrar</title>
		<link>http://tech.blodji.com/2008/02/25/recursive-unrar/</link>
		<comments>http://tech.blodji.com/2008/02/25/recursive-unrar/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 08:50:25 +0000</pubDate>
		<dc:creator>strahil</dc:creator>
		
		<category><![CDATA[windows]]></category>

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

		<guid isPermaLink="false">http://tech.blodji.com/?p=3</guid>
		<description><![CDATA[Go into the root dir of your archived files (the new files will be placed here), open your command prompt and execute this:
C:\> for /R %i IN (.) do "c:\program files\winrar\rar.exe" x %i/*.rar
]]></description>
			<content:encoded><![CDATA[<p>Go into the root dir of your archived files (the new files will be placed here), open your command prompt and execute this:</p>
<p><code>C:\> for /R %i IN (.) do "c:\program files\winrar\rar.exe" x %i/*.rar</code></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.blodji.com/2008/02/25/recursive-unrar/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
