July 9th, 2008
This can be used on pictures, news, shop items, etc. with JavaScript events for “previous” and “next”
if (prev === null) {
prev = current && next = current + 1
} elseif (next === null) {
next = current && 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
}
}
Tags: items, news, php, pictures
Posted in php | No Comments »
June 23rd, 2008
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;
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–) {
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–;
break;
}
}
if ($ys2[3] == 1) {
$ys2[3] = 0;
$ftd–;
}
$years[] = $ys .’-’. $ys2;
} else {
if (!in_array($ftd . 0, $years)) {
$years[] = $ftd . 0;
}
$newld = 0;
$ftd–;
}
$ld = –$newld;
if ($ld > 0) {
for ($i = $ld; $i > 0; $i–) {
if ($i == $ld) {
$ys = $ftd . $ld .’-';
}
if ($i-1 == 0) {
$ys .= $ftd . 0;
$ftd–;
}
}
$years[] = $ys;
}
$ftwod = substr($ftd, 0, 2); // first two digits
$td = substr($ftd, 2); // third digit
$j = 9;
for ($i = $td; $i >= 0; $i–) {
$fy = $ftd . $i . $j;
$years[] = $ftwod . $i . ‘9-’. $ftwod . $i . ‘0′;
}
Output:
'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)
Tags: date, php
Posted in php | No Comments »
June 11th, 2008
$xml = read($dom);
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;
}
Tags: dom, php, xml
Posted in php, xml | No Comments »
February 25th, 2008
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” ‘
‘”$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;
}
}
}
nginx-rrd.conf
#####################################################
#
# 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"
fastcgi-php (creates php fastcgi socket, executable init script for FreeBSD, can be ported easily to other systems)
. /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=”${command_args} -s ${fcgiphp_socket}”
elif [ -n "${fcgiphp_port}" ]; then
command_args=”${command_args} -p ${fcgiphp_port}”
else
echo “socket or port must be specified!”
exit
fi
}
stop_postcmd()
{
rm -f ${pidfile}
# eval “ipcs | awk ‘{ if (\$5 == \”${fcgiphp_user}\”) print \”ipcrm -s \”\$2}’ | /bin/sh”
}
run_rc_command “$1″
perl-fcgi.pl (creates perl socket) - you need the FCGI perl module for this
#!/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/; } ;
&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->Accept() >= 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') && ($req_len != 0) ){
while ($req_len) {
$stdin_passthrough .= getc(STDIN);
$req_len--;
}
}
#running the cgi app
if ( (-x $req_params{SCRIPT_FILENAME}) && #can I execute this?
(-s $req_params{SCRIPT_FILENAME}) && #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 <$cgi_app>; 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";
}
}
}
Tags: nagios, perl, php, proxy, rrd
Posted in nginx | No Comments »
February 25th, 2008
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
Tags: windows, winrar
Posted in windows | No Comments »