Limit bandwidth needed for system updates

December 17th, 2010

Watching movies online and updating my Ubuntu laptop at the same time doesn’t go well together with my DSL internet connection. This apt configuration snippet, makes sure apt will not consume your bandwith:

Acquire
{
        Queue-mode "access";
        http
        {
                Dl-Limit "20";
        };
};

It should be placed in a file in /etc/apt/apt-conf.d/. For more info, consult the apt.conf man page.

Cakephp logger plugin

April 7th, 2010

For one of my projects, I wanted to have a live view of the cake log file in the admin page. What started out as some hacked controller methods, turned into a plugin that can be inserted in any random cake app. Ingredients:

  • a php implementation of the unix tail command, by Paul Gregg
  • a session variable to keep track of what part of the log file has been sent
  • some jquery code (using the periodicalupdater plugin) that adds new lines to a div element as they appear in the log file, scrolls down so the new content becomes visible

The online demo consist of a bare cake ‘install’ with the plugin dropped into the plugins directory. As you can see, there’s no models, views or controllers. Visiting the app will actually log some messages to the debug log. To monitor the log file, visit the plugin path.
I’ve also added two links that lets you add lines to the debug log.

The theoretical advantage of using a plugin is that you can simply drop the code in the plugins directory and voila! it works!
In practice, it’s quite annoying that plugins are similar to, but not exactly structured like cake apps. For example: images and js code should be placed in the vendors folder instead of webroot. Secondly: app_model.php and app_controller.php need to be prefixed by the (singular) plugin name.
Finally, it’s not clear how to integrate the plugin with your actual cake app. Like using the app’s authentication methods to only allow ‘admin’ users to view the log file.

I’m actually not sure a plugin is the right way of implementing an online ‘tail -f’. A component might do a better job. What do you think?

Online demo

The plugin can be downloaded here.

Cakeshell

March 19th, 2010

I’ve written a little wrapper script to ease the execution of cake shells. Just by creating symbolic links, you can turn any custom) cake shell into a shell command that can be called from any location. Ie: you don’t have to cd to your cake app directory. Just make sure the symlink is in your $PATH.

$ ./cakeshell
Error: script should be symlinked
cakeshell 0.1 - (c)2010 nerdnotes.org

This wrapper script is built in order to ease running cake shells.

Usage:
The script should be located somewhere your cake directory structure.
Make sure you configure the relative path of the cake script from the
directory this script lives in (REL_CAKE_PATH).

For each of the shells you want to be available, create a symlink to
./cakeshell with the name of the cake shell you want to execute

Example*
*using some real world paths here

To simplify the following cake shell call:

./cake/console/cake bake

You'll need to do this once:

cd ~/bin
ln -s ../public_html/cakesite/cakeshell bake

If ~/bin is in your PATH, you can simply run the bake script from anywhere


That’s the output, here’s the script itself.

mt-daapd

January 9th, 2010

Mt-daapd is an ‘iTunes’ server that you can run on a linux system holding your music collection.
When fiddling around with configuration, I came accros the following error message in the syslog:

Error loading plugin /usr/lib/mt-daapd/plugins/ssc-script.so: plugin declined to load

I was trying to setup a custom transcoding script. That required to load the ‘ssc-script.so’ plugin first.
When increasing the log level, it gave a more descriptive hint to what is going on:

No codectypes specified for script transcoder.
Error loading plugin /usr/lib/mt-daapd/plugins/ssc-script.so: plugin declined to load

After uncommenting the following line in /etc/mt-daapd.conf:

ssc_codectypes = ogg,flac,alac

…it showed no error messages anymore:

Loaded plugin /usr/lib/mt-daapd/plugins/ssc-script.so (ssc-script/svn-1696)

CakePHP Email component

October 20th, 2009

This is quite a simple component that integrates PHPMailer with CakePHP. Save the following code to /app/controllers/components/email.php

<?php 

App::import('Vendor', 'PhpMailer', array('file' => 'phpmailer' . DS . 'class.phpmailer.php'));

/*
    Depends on "unhtml"
*/

class EmailComponent extends PHPMailer
{
	// phpmailer
	var $Mailer = 'sendmail'; // choose 'sendmail', 'mail', 'smtp'
	var $unhtml_bin = '/usr/bin/unhtml';

	// component
	var $controller;

	function startup( &$controller )
	{
		$this->controller = &$controller;
	}

	function renderBody($view)
	{
		// render the view and use its output to set the body text of the email
		$this->Body = $this->controller->render('emails/' . $view, 'email');

		// reset the output of the controller
		$this->controller->output = '';

		// create plain text version of the email
		//
		// create temporary files
		$htmlfile = tempnam(TMP, 'htmlfile');
		$textfile = tempnam(TMP, 'textfile');

		// write html to temporary file
		file_put_contents($htmlfile, $this->Body);

		// convert the html file to plain text
		$cmd = "cat $htmlfile | $this->unhtml_bin > $textfile";
		system($cmd);

		// set the plain text body of the email
		$this->AltBody = file_get_contents($textfile);

		// remove temporary files
		unlink($htmlfile);
		unlink($textfile);
	}
}

?>

In addition to that, make sure phpmailer is installed in /app/vendors/phpmailer. I just extracted the phpmailer archive in /app/vendors/ and created a symlink:

$ tar xzf PHPMailer_v5.0.2.tar.gz
$ ln -s PHPMailer_v5.0.2 phpmailer

Usage
In your controller code, make sure the component is included by adding it to the components array:

var $components = array('Email');

In app/views/layouts, create a new layout called email.ctp. It should contain something like:

<html>
<body>
<?php echo $content_for_layout; ?>
</body>
</html>

Let’s send some emails!
In controller snippet, the renderBody(‘test’) call will create a html email from the view in app/views/users/emails/test.ctp. Additionally, a plain text version will be generated using ‘unhtml’. Unhtml has a package in the Ubuntu / Debian repository.
The action works just like rendering a normal view. The $this->Email object is just an instance of PHPMailer and all PHPMailers’ methods and properties apply.

function testmail()
{
	// *Some notes*
	// Email methods:    http://phpmailer.worxware.com/index.php?pg=methods
	// Email properties: http://phpmailer.worxware.com/index.php?pg=properties
	//
	// Email configuration is done in /app/controllers/components/email.php

	$this->set(compact('some', 'vars'));

	// the email content is just a (html) view in app/views/{controller}/emails/testmail.ctp
	$this->Email->renderBody('test');

	// subject
	$this->Email->Subject = 'Test from example.com';

	// sender
	$this->Email->SetFrom('test@example.com', 'Test');

	// recipients
	$this->Email->AddAddress('joe@example.com', 'Joe');
	$this->Email->AddAddress('jane@example.com', 'Jane');

	// send!
	$this->Email->Send();
}

Arduino footprints

July 20th, 2009

Here’s an Eagle component library with the Arduino Duemilanove and the Nano. If you’re planning to integrate Arduino’s as a component in your PCB design, this is a great way to start! I’ve basically merged and edited the two components from other existing libraries:

The Arduino library can be opened in Eagle 4.15. What to expect? Here are some screenshots:
Arduino symbolsArduino packages

Cakephp ThumbnailHelper

April 7th, 2009

This thumbnail helper is based on the one published by Studio Canaria. Additional features are multiple size caching and an extra method which is a truly drop-in replacement for $html->image().
Usage:

<?php

echo $thumbnail->image('picture.jpg', array(
 	'alt' => 'test thumbnail',
 	'width' => '100',
 	'height' => '75'
 	)
 );

// result:
// <img src="/img/thumbs/100x75/picture.jpg"
//	alt="test thumbnail" height="75" width="100">

?>

Installation:

  • Download thumbnail.txt
  • Rename it to thumbnail.php and place it in /app/views/helpers/
  • download phpThumb()
  • Extract the phpThumb archive in a directory named /app/vendors/phpthumb/
  • Add ‘Thumbnail’ to the ‘helpers’ array your controller
  • Create a directory named ‘thumbs’ in /app/webroot/img/. Make sure it’s writable for the webserver.

That’s it!
In the above example, the thumb helper will first check if there is a file named /app/webroot/img/thumbs/100×75/picture.jpg. If it isn’t there yet, it will instantiate phpThumb and create the thumbnail.
For more advanced usage, there’s also the render() method. It will give you the flexibility to use images located outside /app/webroot/img and also make use of most of the phpThumb options.
Advanced example:

<?php

$thumb = $thumbnail->render(
	$data['Picture']['path'],
	array(
		'w' => 320,
		'h' => 200,
		'ra' => 10
	),
	'5271a9e66c99c84c092d7fd81cebcd57'
);

echo $html->image($thumb);

?>

This will generate a picture named /app/webroot/img/thumbs/320×200/5271a9e66c99c84c092d7fd81cebcd57.jpg, which is a 10 degrees rotated thumbnail of the original image specified with $data['Picture']['path'].

explorerTree demo files

April 6th, 2009

tree.png
To save you some typing, this archive contains a working example of the tree helper code described in the bakery. The archive includes cake 1.2, three images and jquery. In the js-code, some hard-coded paths were corrected. It also contains a file named tree.sql which contains a dump of the database including some sample data.

cakephp, user_dir and mod_rewrite

April 2nd, 2009

Here’s a little note on getting a cakephp app working in your public_html directory. In order to get the mod_rewrite configuration working, add a RewriteBase rule to the .htaccess files in the cakephp source. If your app is located in /home/me/public_html/cakesite/ and the URL to the site is http://host/~me/cakesite you’ll need to change the following .htaccess files:

/home/me/public_html/cakesite/.htaccess
/home/me/public_html/cakesite/app/webroot/.htaccess

Add the following line to these files, just below the line stating “RewriteEnginge On”:

RewriteBase /~me/cakesite/

For example, the first .htaccess file will look like:

<ifModule mod_rewrite.c>
	RewriteEngine on
	RewriteBase /~me/cakesite/
	RewriteRule    ^$ app/webroot/    [L]
	RewriteRule    (.*) app/webroot/$1 [L]
</ifModule>

Bonus notes with instructions for Debian/Ubuntu systems
You should have mod_rewrite enabled:

$ sudo a2enmod rewrite
$ sudo /etc/init.d/apache2 force-reload

To make sure the use of .htaccess files is actually allowed. In the Directory section of your apache configuration, there should be a line like:

AllowOverride FileInfo

Also, be sure to force-reload Apache after making this change.

cmdline screenshots in OSX

February 18th, 2009

Geekology describes a nice osx builtin command to create screen captures: screencapture. I wanted to create a screenshot of a mouse interaction. That requires some sort of a delay after initiating the capture. The “-T” option described facilitates this. It looks like the -T option is missing in the tiger version of this utility. Of course you can use ‘sleep’ to wait a couple of seconds:

$ sleep 5;screencapture -C ~/Desktop/screenshot.png

See also my previous post on screenshots.