A nerds discoveries

Fake php time on your ubuntu server

by robert on Dec.16, 2011, under Uncategorized

Some time it is necessary to fool your PHP application to test functionality related to a “current date”.

Fooling php cli is simple using faketime:

Say you have this PHP script:

1
2
3
#!/usr/bin/env php
<?php
print date( 'F j. Y [H:i]' )."\n";

Rendering this script with php through faketime will give you results like this:

1
2
$ faketime 'last Friday 5 pm' ./time.php
December 9. 2011 [17:00]

The same can be achieved using the datefudge command:

1
2
$ datefudge "2007-04-01 10:23" ./time.php
April 1. 2007 [10:23]

Faking time for a webapplication

Faking time for a web application is not that simple since apache will fork a process for each request and thus create a new php processes.
In this post I will show you how to use FakeTime Preload Library to fake time system wide while running tests on a web application.

First I need to install faketimelib as described on the librarys homepage.
In short:

1
2
3
4
5
6
7
wget http://www.code-wizards.com/projects/libfaketime/libfaketime-0.8.1.tar.gz
tar -xvzf libfaketime-0.8.1.tar.gz
cd libfaketime-0.8.1
vim README
make
sudo make install
export LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1

For the demonstration I will use a php script like this:

1
2
<?php
printf( "The current time of the server is: %s\n", date('l F j. Y [H:i:s]') );

Running this script should yield something like:

1
2
$ php time.php
The current time of the server is: Friday December 16. 2011 [08:02:43]

Now I create a file in my home folder, faketimerc, with a new future time. I will use this file in different ways to show how I can manipulate the time.

1
echo "@2012-12-21 12:12:12" > faketimerc

If you now create an environment variable, FAKETIME, give it a future time, and running the same script would yield something like this:

1
2
3
4
5
6
7
8
$ php time.php
The current time of the server is: Friday December 16. 2011 [08:19:39]
$ export FAKETIME=$(cat faketimerc)
$ php time.php
The current time of the server is: Friday December 21. 2012 [12:12:12]
$ unset FAKETIME
$ php time.php
The current time of the server is: Friday December 16. 2011 [08:19:56]

As you can see, the processes you run while the environment variable is set to a future time will get a fake time. Once the variable is unset new processes will get normal time.

This can be achieved also by creating a file. “.faketimerc” in your home folder:

1
2
3
4
5
6
7
8
$ php time.php
The current time of the server is: Friday December 16. 2011 [08:23:12]
$ cp faketimerc .faketimerc
$ php time.php
The current time of the server is: Friday December 21. 2012 [12:12:12]
$ rm -f .faketimerc
$ php time.php
The current time of the server is: Friday December 16. 2011 [08:23:36]

If you want to change time for a php application that runs though apache you may want to set the fake time system wide so that the php processes spowned use the faked time. To do this you need to create a file, /etc/.faketimerc, just the same as the one I created in my home folder.

I will use w3m for this demonstration. I assume a normal ubuntu server with apache2 installed.

1
2
3
4
5
6
7
8
9
10
sudo cp time.php /var/www
sudo cp faketimerc /etc/
$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                                                                                                                                                                                                                                                   ... waiting
$ php /var/www/time.php
The current time of the server is: Friday December 21. 2012 [12:12:12]
$ date
fr. 21. des. 12:12:12 +0100 2012
$ w3m -dump localhost/time.php
The current time of the server is: Friday December 16. 2011 [11:16:24]

As you can see, the date command and when rendering the script with php, gives me the fake time, but when rendering the script through apache I loose the fake time. This is because the environment variable LD_PRELOAD is not set for the apache process.

To fix this I need to set LD_PRELOAD for apache by editing /etc/apache/envvars. Lets test it again:

1
2
3
4
5
$ sudo bash -c "echo \"export LD_PRELOAD='/usr/local/lib/faketime/libfaketime.so.1'\" >> /etc/apache2/envvars"
$ sudo /etc/init.d/apache2 restart
 * Restarting web server apache2                                                                                                                                                                                                                                                   ... waiting
$ w3m -dump localhost/time.php
The current time of the server is: Friday December 21. 2012 [12:12:16]

Fake time! :)

PS! If you also want your PostgreSQL server to use fake time:

1
2
$ sudo bash -c "echo \"LD_PRELOAD='/usr/local/lib/faketime/libfaketime.so.1'\" >> /etc/postgresql/8.4/main/environment"
$ sudo /etc/init.d/postgresql-8.4 restart
Leave a Comment more...

ubuntu-vm-builder and vmbuilder

by robert on Oct.23, 2011, under Virtualization

In case you should wonder what is the difference between the /usr/bin/ubuntu-vm-builder and /ur/bin/vmbuilder, there is no difference.

Both scripts use the same python VMBuilder library, and contain accactly the same code…

Leave a Comment more...

Veewee and ubuntu-10.04.3

by robert on Sep.27, 2011, under Vagrant

You want to create a VirtualBox with Ubuntu 10.04.3 using Veewee, but there is no ubuntu-10.04.3 template?

Here is what you can do:

Download (git clone) a copy of veewee from GitHub and copy the templates.

1
2
3
git clone https://github.com/jedi4ever/veewee.git
cd veewee
sudo cp -r templates/ubuntu-10.04.3-server-* /usr/lib/ruby/gems/1.8/gems/veewee-0.2.0/templates/

To verify that you have the new templates:

1
vagrant basebox templates
Leave a Comment more...

Bash completion and git prompt

by robert on Jul.07, 2011, under Bash, git

Recently I discovered that the virtual server I was working on did not give me the usual bash completion I’m so used to on my Kubuntu workstations. Working on git repositories and upgrading web applications made this to an itch I just had to scratch.

After a bit of searching it appeared that I was missing a file: /etc/bash_completion. Next I discovered that there is a /etc/bash_completion.d/ directory and under this a file named ‘git’. I copied the bash_completion file from my workstation and followed the instruction in /etc/bash_completion.d/git and suddenly I had bash completion and a prompt that gave me branch name and the status of the git repository I was currently in.

Just to give you a hint, here is the comment from the /etc/bash_completion.d/git file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!bash
#
# bash completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# The contained completion routines provide support for completing:
#
#    *) local and remote branch names
#    *) local and remote tag names
#    *) .git/remotes file names
#    *) git 'subcommands'
#    *) tree paths within 'ref:path/to/file' expressions
#    *) common --long-options
#
# To use these routines:
#
#    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
#    2) Added the following line to your .bashrc:
#        source ~/.git-completion.sh
#
#       Or, add the following lines to your .zshrc:
#        autoload bashcompinit
#        bashcompinit
#        source ~/.git-completion.sh
#
#    3) Consider changing your PS1 to also show the current branch:
#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#
#       The argument to __git_ps1 will be displayed only if you
#       are currently in a git repository.  The %s token will be
#       the name of the current branch.
#
#       In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
#       value, unstaged (*) and staged (+) changes will be shown next
#       to the branch name.  You can configure this per-repository
#       with the bash.showDirtyState variable, which defaults to true
#       once GIT_PS1_SHOWDIRTYSTATE is enabled.
#
#       You can also see if currently something is stashed, by setting
#       GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
#       then a '$' will be shown next to the branch name.
#
#       If you would like to see if there're untracked files, then you can
#       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
#       untracked files, then a '%' will be shown next to the branch name.
#
#       If you would like to see the difference between HEAD and its
#       upstream, set GIT_PS1_SHOWUPSTREAM="auto".  A "<" indicates
#       you are behind, ">" indicates you are ahead, and "<>"
#       indicates you have diverged.  You can further control
#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
#       list of values:
#           verbose       show number of commits ahead/behind (+/-) upstream
#           legacy        don't use the '--count' option available in recent
#                         versions of git-rev-list
#           git           always compare HEAD to @{upstream}
#           svn           always compare HEAD to your SVN upstream
#       By default, __git_ps1 will compare HEAD to your SVN upstream
#       if it can find one, or @{upstream} otherwise.  Once you have
#       set GIT_PS1_SHOWUPSTREAM, you can override it on a
#       per-repository basis by setting the bash.showUpstream config
#       variable.
#
#
# To submit patches:
#
#    *) Read Documentation/SubmittingPatches
#    *) Send all patches to the current maintainer:
#
#       "Shawn O. Pearce" <spearce@spearce.org>
#
#    *) Always CC the Git mailing list:
#
#       git@vger.kernel.org
#
Leave a Comment more...

Prompt with git branch

by robert on Nov.10, 2010, under git

I think I like this:

1
2
3
4
5
6
7
8
function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}

export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;33m\]\w\[\033[01;32m\] $(parse_git_branch) \[\033[01;36m\]$ \[\033[00m\]'
Leave a Comment more...

Thunderbird 3 account setup tip

by admin on Oct.16, 2010, under Uncategorized

Wish you could set up an account in Thunderbird manually?

You’re not alone.

The trick: Hit the “stop” button as soon as you enter the automatic setup window and set your settings manually. If you wait to press “stop” it will not stop and the best thing you can do is to cansel and start over again.

Ref: http://www.givesatisfaction.com/mozilla_messaging/topics/can_t_setup_an_imap_connection_in_thunderbird_3

Leave a Comment more...

Fix to vims taglist plugin (ctags tags comments in PHP)

by robert on Jan.31, 2010, under Vim

Taglist uses exuberant ctags, which supports many languages including PHP. I use vim on a daily basis to edit PHP files. Often classes and functions have PHPDoc and some like to use the words ‘class’ or ‘function’ in their comments. Fair enough, but ctags regex strings ignores that lines may have a preceding ‘*’ or ‘//’ and thus create tags of funny classes and functions. In some cases browsing large files of PHP source code with taglist is pointless.

The solution to this is writing your own ctags configuration file (~/.ctags) and letting ctags know what regex strings You want to tag your PHP files with:

1
2
3
4
5
6
7
8
9
10
-f ./tags
--langmap=php:+.inc
-h '.php.inc'
-R
--totals=yes
--tag-relative=yes
--PHP-kinds=+cf-v
--regex-PHP
--regex-PHP=/(^|^[^*]+[[:blank:]])class[[:blank:]]+([[:alpha:]][[:alnum:]_]*)/\2/c/
--regex-PHP=/(^|^[^*]+[[:blank:]])function[[:blank:]]+&?[[:blank:]]*([[:alpha:]][[:alnum:]_]*)/\2/f/
5 Comments more...

MyPasswordSafe to KeepassX converter

by admin on Jan.05, 2010, under PHP

I’ve used MyPasswordSafe for some time, but after I installed Kubuntu Karmic Koala AMD64 it would’nt start. I googled the Internet for a better program and settled with KeepassX.

Next trouble was to move my passwords from MyPasswordSafe to KeepassX. Both programs can export/import XML files, so I made a PHP file that did the job. I even created a project on githut for it.

Leave a Comment : more...

Customer spesific settings in vim using git branches

by robert on Sep.08, 2009, under Vim

I work as an consultant and thus write code for different customers that may have quite different coding standards and license policies. Adapting to a customer often involves changing many files in the vim config files and plugins, but after I initialized a git repo in my ~/.vim folder I only have to change branch for each customer I work for. git br (git branch) gives me a neat list of predefined customer settings to choose from. Then its just git co [customer] and start to work.

Leave a Comment more...

Back in time with Vim

by robert on Aug.24, 2009, under Vim

My college came by my desk today wanting to know how to go back in time with vim. He had been undoing and redoing a lot and suddenly realised he had messed up his buffers. I had never heard about going back in time with vim, but Google had the answer on this post: lifehacker.com/202093/go-back-in-text-file-time-with-vim-70.

Apparently the past is but a

1
:earlier 10m

away!

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...