Archive for 2008
i hate working with dates
This little snippet gets all the days in a month, and groups them by week.
<?php
error_reporting(E_ALL);
// Return an array of all the days in a month grouped by week number
// (Sunday is considered to be the first day of the week)
function weeksInMonth($month, $year)
{
[...]
K100D Gallery Added
Just added a couple of quick photos to the k100d gallery. Really wish I could have speant longer watching the surfers near The Spit
Camera is Pentax K100D 6MP with a Sigma 70-300 / Sigma 28-70 F2.8
.thumbnail
{
float: left;
width: 140px;
height: 140px;
padding: 5px 0px 0px 5px;
text-align: center;
font-size: 0.8em;
}
.thumbnail img
{
border: 0;
}
.thumbnail a
{
text-decoration: None;
}
20080713 Springbrook.jpeg
(924 x [...]
auth.log noise
Below are the top 50 login names when trying to gain access to this web server over the last 4 weeks
Name
Attempts
root
3273
admin
119
test
110
mysql
44
guest
41
user
37
oracle
37
temp
25
sales
24
info
21
webmaster
21
postgres
21
dan
18
robert
18
student
17
ftpuser
17
ftp
17
richard
16
apache
16
web
15
adm
15
webadmin
15
john
15
paul
15
office
15
tony
14
james
14
postfix
13
michael
13
alex
13
david
13
amanda
13
adam
12
mike
12
staff
12
steven
12
recruit
12
jeff
12
pgsql
12
library
12
username
12
frank
12
susan
11
cyrus
11
dave
11
gast
10
postmaster
10
nagios
10
martin
10
admins
10
Note: No external connections are allowed to log in as root (of course)
Note2: After 5 failed login attempt, the IP address is temporary banned (fail2ban)
import life
A console based implementation of Conway’s Game of Life in Python.
As fascinating as it is useless.
#!/usr/bin/python
import sys
import random
import time
import os
import copy
WIDTH = 32
HEIGHT = 16
class Grid:
def __init__(self, width, height):
self.width = width
self.height = height
self.create_blank_grid()
def create_blank_grid(self):
self.grid = [...]
Pretending PHP doesn’t suck
<?php
// Perhaps the simplest practical use of a fold?
//
// (While this is fairly consise (even in PHP) it calculates the length of a
// string 2n-2 times [which is n-2 times more than needed])
function getLongestWord($words)
{
$fun = create_function(’$a,$b’, ‘return (strlen($a) > strlen($b)) ? $a : $b;’);
return array_reduce($words, $fun);
}
// This looks a little better, but [...]
Extracing links from HTML using PHP
Many months ago there was a PHP competition to make the smallest script to extract all the links from a document. I’ve lost a link to the actual site, but the rules and conditions were set up expecting everyone to solve the problem with regular expressions. In my opinion relying on regular expressions [...]
Simple type checking in PHP
<?php
error_reporting(E_ALL | E_STRICT);
/*
Manual optional type checking for PHP functions
Basic example:
function log_error($line_number, $filename, $desc)
{
CheckFunctionArgs(’integer’, ’string’, ’string’);
[snip]
}
Object example:
class LogObject {}
function register_object($obj)
{
// Check for [...]
sizeof(int) = 68
Pankaj Kumar has a slightly disturbing look at memory usage in PHP.
Each element requires a value structure (zval) which takes 16 bytes.
Also requires a hash bucket - which takes 36 bytes. That gives 52 bytes
per value. Memory allocation headers take another 8 bytes*2 - which
gives 68 bytes. Pretty close to what you have.
fun with anagrams
<?php
define(’WORD_LIST_FILENAME’, ‘/usr/share/dict/words’);
class AnagramLookup
{
private $lookup;
// Loads a file with one word per line
private function load_word_list($filename)
{
$lines = file($filename); // One word per line
$lines = array_map(’trim’, $lines); // Strip [...]
Javascript…
Javascript is both infuriating and awesome at the same time. I don’t think I’ve ever speant so much time tracking down annoying bugs (even compared to PHP), yet at the same time it makes functions like the one below very simple to write.
For reference, the below code returns a `getter` method that we use [...]