Marius van Witzenburg We fight for our survival, we fight!

11Nov/110

Apache Error Codes

Posted by mariusvw

With the function below you can add your own custom actions to error statuses of Apache.

Lets say you want a custom 404 error page. You could redirect a user upon a 404 error or show a complete different layout.

/**
 * HTTP Protocol defined status codes
 * @param int $num
 */
function HTTPStatus($num) {
   static $http = array (
       100 => "HTTP/1.1 100 Continue",
       101 => "HTTP/1.1 101 Switching Protocols",
       200 => "HTTP/1.1 200 OK",
       201 => "HTTP/1.1 201 Created",
       202 => "HTTP/1.1 202 Accepted",
       203 => "HTTP/1.1 203 Non-Authoritative Information",
       204 => "HTTP/1.1 204 No Content",
       205 => "HTTP/1.1 205 Reset Content",
       206 => "HTTP/1.1 206 Partial Content",
       300 => "HTTP/1.1 300 Multiple Choices",
       301 => "HTTP/1.1 301 Moved Permanently",
       302 => "HTTP/1.1 302 Found",
       303 => "HTTP/1.1 303 See Other",
       304 => "HTTP/1.1 304 Not Modified",
       305 => "HTTP/1.1 305 Use Proxy",
       307 => "HTTP/1.1 307 Temporary Redirect",
       400 => "HTTP/1.1 400 Bad Request",
       401 => "HTTP/1.1 401 Unauthorized",
       402 => "HTTP/1.1 402 Payment Required",
       403 => "HTTP/1.1 403 Forbidden",
       404 => "HTTP/1.1 404 Not Found",
       405 => "HTTP/1.1 405 Method Not Allowed",
       406 => "HTTP/1.1 406 Not Acceptable",
       407 => "HTTP/1.1 407 Proxy Authentication Required",
       408 => "HTTP/1.1 408 Request Time-out",
       409 => "HTTP/1.1 409 Conflict",
       410 => "HTTP/1.1 410 Gone",
       411 => "HTTP/1.1 411 Length Required",
       412 => "HTTP/1.1 412 Precondition Failed",
       413 => "HTTP/1.1 413 Request Entity Too Large",
       414 => "HTTP/1.1 414 Request-URI Too Large",
       415 => "HTTP/1.1 415 Unsupported Media Type",
       416 => "HTTP/1.1 416 Requested range not satisfiable",
       417 => "HTTP/1.1 417 Expectation Failed",
       500 => "HTTP/1.1 500 Internal Server Error",
       501 => "HTTP/1.1 501 Not Implemented",
       502 => "HTTP/1.1 502 Bad Gateway",
       503 => "HTTP/1.1 503 Service Unavailable",
       504 => "HTTP/1.1 504 Gateway Time-out"
   );
 
   header($http[$num]);
}
?>

You can download this 404 error page example to play with.

Tagged as: , , , No Comments
26Sep/110

How to easy CVS update your SRC and Ports tree with a simple Bash script (v2)

Posted by mariusvw

This is an update to this post: How to easy CVS update your SRC and Ports tree with a simple Bash script

I adjusted the script to use the local csup version of the FreeBSD World system and moved the cvsup files to /etc.

First install a simple tool to get the fastest cvs server

pkg_add -r fastest_cvsup

Or from Ports (if you have the collection already):

cd /usr/ports/sysutils/fastest_cvsup/
make install clean

Setup config and binary to easy update the repository for SRC and PORTS.

mkdir /etc/cvsup
cd /usr/share/examples/cvsup
cp stable-supfile /etc/cvsup
cp ports-supfile /etc/cvsup
cp doc-supfile /etc/cvsup
cd /etc/cvsup

Edit stable-supfile and change:

*default release=cvs tag=RELENG_8_2

Set this to your release version.

Edit /usr/local/sbin/runcvsup and add:

#!/bin/sh
arg=$@
sType="tld,nl" # Can be: all / tld / tld,nl,de,fr,us
if SERVER=`/usr/local/bin/fastest_cvsup -q -c $sType`; then
    echo "Using server: ${SERVER}"
    case "$arg" in
    "stable")
        echo "Updating: stable"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/stable-supfile
    ;;
    "ports")
        echo "Updating: ports"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/ports-supfile
    ;;
    "doc")  
        echo "Updating: doc"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/doc-supfile
    ;;
    *)
        echo "Updating: all"
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/stable-supfile
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/ports-supfile
        /usr/bin/csup -L 2 -g -z -h ${SERVER} /etc/cvsup/doc-supfile
    ;;
    esac    
fi

Set the right permissions:

chmod 700 /usr/local/sbin/runcvsup

Update everything:

runcvsup
27Aug/110

How to “disable” the VLAN1 on a Cisco SLM 2048 switch

Posted by mariusvw

I have been busy with this but with a wrong way of thinking...

How you can disable it? Simple, leave it on VLAN1 and move all ports except one to another VLAN.

You can do this in the port configuration, set the ports to your own VLAN by changing the PVID (VLAN ID).

15Aug/118

Ixy has passed away this morning. Rest in peace!

Posted by mariusvw

This came totally unexpected. Ixy is the most busy bird of the two and she seemed to be completely healthy. A good feather pack, good appetite, playful, good mood and always exploring new things.

My girlfriend was already making them new food when I came downstairs and like I do every morning is say hi to them.
Ixy grabbed the cage which I did not understand why, then she flipped back her neck and reacted really strange... Not even 2 seconds later she felt of the stick she was holding with her other leg on the rope, rolled over that rope and felt on the cage floor. I picked her up as fast as I could but it was already to late for her... She made some poor sounds like she was still hanging in there but this took for about 5 seconds and then she became completely quiet. I checked her life signs by listening and checking her pupils but she was already gone :-(

We got Ixy on 22-01-2011 from the shelter, we had no idea how old she was, she lived till 15-08-2011. Rest in peace our loved Ixy!

Here you see Iggy looking at us after he tried to wake Ixy.

Here some pictures of her being still alive.

   

   

23Jul/111

Ixy and her “What are you looking at!” look to the camera

Posted by mariusvw

It seems that Ixy likes seeing her self in the camera... She even doesn't get scared when I come with my hands near her. Normally she flies away even if you just point at her ;-)

17Jul/112

PHP Performance – isset() versus empty() versus PHP Notices

Posted by mariusvw

I’m cleaning up a lot of PHP code and always program with PHP error_reporting set to E_ALL and display_errors turned on so that I make sure to catch any PHP messages that come up. Since starting on this site, I have fixed literally hundreds (maybe thousands) of PHP Notices about using uninitialized variables and non-existent array indexes.

I have been fixing problems like this where $somevar is sometimes undefined:

if ($somevar)

by changing it to:

if (isset($somevar) && $somevar)

This successfully gets rid of the NOTICEs, but adds some overhead because PHP has to perform two checks. After fixing a lot of this in this manner, I’ve noticed that the pages seem to be generated a little slower.

So, to provide some conclusive results to myself, I wrote up a quick benchmarking script - available at php_empty_benchmark.php. It goes through 1,000,000 tests using each of these methods:

  1. if ($a) - This generates a notice if $a is not set
  2. if (isset($a)) - A simple clean way to check if the variable is set (note that it is not equivalent to the one above)
  3. if (isset($a) && ($a) - The one that I have been using which is equivalent to if($a), but doesn’t generate a notice.
  4. if (!empty($a)) - This is functionally equivalent to if($a), but doesn’t generate a notice.

It measures the time to perform 1 million tests using a defined percentage of values that are set. It then computes the difference as a percentage of the time taken for the original test (the one that generates the notices). A ‘diff’ of 100 means that the execution time is the same, greater than 100 means that it is faster, and less than 100 means that it is slower. A typical test produced these results:

With NOTICE: 0.19779300689697
With isset:  0.19768500328064 / Diff: 100.05463419811
With both:   0.21704912185669 / Diff: 91.128222590815
With !empty: 0.19779801368713 / Diff: 99.997468735875

In summary, using the if (isset($a) && $a) syntax is about 8-10% slower than generating the PHP Notice. Using !empty() should be a drop-in replacement that doesn’t generate the notice and has virtually no performance impact. Using ifset() also has no performance impact, but is not exactly the same as ‘if($a)’ since isset() will return true if the variable is set to a false value. I included it here, because it often make the code a little more readable than the !empty($a) syntax. For example:

$myvalue = !empty($_REQUEST['myvalue']) ? $_REQUEST['myvalue'] : '';

Versus

$myvalue = isset($_REQUEST['myvalue']) ? $_REQUEST['myvalue'] : '';

Source:
http://www.brandonchecketts.com/archives/php-performance-isset-versus-empty-versus-php-notices

17Jul/112

How to download sexdumpert.nl photo’s or video’s with a simple Bash script

Posted by mariusvw

Here is the simple script which downloads all photos from sexdumpert.nl

Just save the file as run.sh in a directory and run it. It will save all photos found.

#!/bin/bash
 
if [ ! -d 'files' ]
then
    mkdir files
fi
 
for ((a = 0; a <= 999; a++))
do
    ca=`printf "%03d" $a`
    for ((b = 0; b <= 999; b++))
    do
        cb=`printf "%03d" $b`
        for ((c = 0; c <= 999; c++))
        do      
            cc=`printf "%03d" $c`
            for ((d = 0; d <= 999; d++))
            do      
                cd=`printf "%03d" $d`
 
                url="http://mediadata.sexdumpert.nl/$ca/$cb/$cc/$cd.jpg"
                echo $url
                /usr/bin/curl $url -o "files/$ca-$cb-$cc-$cd.flv"
 
            done    
        done    
    done    
done

Here is the simple script which downloads all video's from sexdumpert.nl

Just save the file as run.sh in a directory and run it. It will save all video's found.

#!/bin/bash
 
if [ ! -d 'files' ]
then
    mkdir files
fi
 
for ((a = 0; a <= 999; a++))
do
    ca=`printf "%03d" $a`
    for ((b = 0; b <= 999; b++))
    do
        cb=`printf "%03d" $b`
        for ((c = 0; c <= 999; c++))
        do      
            cc=`printf "%03d" $c`
            for ((d = 0; d <= 999; d++))
            do      
                cd=`printf "%03d" $d`
 
                url="http://mediadata.sexdumpert.nl/$ca/$cb/$cc/$cd.flv"
                echo $url
                /usr/bin/curl $url -o "files/$ca-$cb-$cc-$cd.flv"
 
            done    
        done    
    done    
done

Hope this for fills some people their needs ;-)

17Jul/110

How to check if a key exists in an array with in_array for Bash

Posted by mariusvw

When working with Bash it might become handy to have a way to check if a record exists in an array. In PHP you have in_array for this... The below code adds a function similar to the PHP variant.

function in_array() {
  local x
  ENTRY=$1
  shift 1
  ARRAY=( "$@" )
  [ -z "${ARRAY}" ] && return 1
  [ -z "${ENTRY}" ] && return 1
  for x in ${ARRAY[@]}; do
    [ "${x}" == "${ENTRY}" ] && return 0
  done
  return 1
}
 
MASTER=()
CURRENT=()
FIRST=1
for SERVER in ${SERVERS}; do
  # collect all builds from server and populate CURRENT list
  COMMAND="${LS} -1fd ${WEBROOT}/${SITE}.*"
  BUILDS=`${SSH} ${SSHOPTS} root@${SERVER} "${COMMAND}"`
  for BUILD in ${BUILDS}; do
    CURRENT=( ${CURRENT[@]-} ${BUILD} )
  done
 
  # if this is our first time around, copy CURRENT to MASTER
  if [ ${FIRST} -eq 1 ]; then
    MASTER=( ${CURRENT[@]} )
    FIRST=0
  fi
 
  # now we do a compare between MASTER and CURRENT to see what builds
  # are common
  INTERSECT=()
  for ENTRY in ${CURRENT[@]}; do
    in_array "${ENTRY}" "${MASTER[@]}"
    RET=$?
    if [ "${RET}" -eq 0 ]; then
      INTERSECT=( ${INTERSECT[@]-} ${ENTRY} )
    fi
  done
  MASTER=( ${INTERSECT[@]} )
 
  # clear the CURRENT array
  CURRENT=()
done

Source: http://mykospark.net/tag/in_array/

17Jul/110

How to add Base64 encoding and decoding in C

Posted by mariusvw

I found this page on the web while searching for something else but I thought this might be useful to others :-)

Overview
libb64 is a library of ANSI C routines for fast encoding/decoding data into and from a base64-encoded format. C++ wrappers are included, as well as the source code for standalone encoding and decoding executables.

Base64 uses a subset of displayable ASCII characters, and is therefore a useful encoding for storing binary data in a text file, such as XML, or sending binary data over text-only email.

Usage

#include <b64/encode.h>
#include <iostream>
int main() {
	base64::encoder E;
	E.encode(std::cin, std::cout);
	return 0;
}

You can read more info and download the package on the url below:

http://libb64.sourceforge.net/

14Jul/110

Convert pipe diameters from Inches, DN (Diametre Nominal) and mm (Millimeter)

Posted by mariusvw

Pipe is made of a wide variety of materials - like galvanized steel, black steel, copper, cast iron, concrete, and various plastics such as ABS, PVC, CPVC, polyethylene, polybutylene and more.

Pipes are identified by "nominal" or "trade" names that are loosely related to the actual dimensions. For instance, a 2-inch galvanized steel pipe has an inside diameter of about 2 1/8 inches and an outside diameter of about 2 5/8 inches.

In plumbing pipe size is referred to as nominal pipe size - NPS, or "Nominal Pipe Size". The metric equivalent is called DN or "diametre nominel". The metric designations conform to International Standards Organization (ISO) usage and apply to all plumbing, natural gas, heating oil, and miscellaneous piping used in buildings. The use of NPS does not conform to American Standard pipe designations where the term NPS means "National Pipe Thread Straight".

Convert the pipe diameters with the inner and outer diameter in millimeters, the inner surface and wall thickness.

Related links:
NPS - 'Nominal Pipe Size' and DN - 'Diametre Nominal'
Difference between Pipes and Tubes

Page 1 of 1312345678910...Last »