Automatic login with SSH private key in PuTTY

These steps will enable you to automatically login to a remote computer through SSH protocol with PuTTY.

This trick might not work if your user’s home directory is shared with another user from the same group but unfortunately .ssh and authorized_keys are owned by him. Those need group read and write (660) permissions. However, sshd needs requires 600 permissions. This can be bypassed by setting StrictModes to no at /etc/ssh/sshd_config. Security may suffer, but hey - you have to do what you have to do, right?

Setup Subversion on CentOS and connect through svn+ssh in Eclipse

These steps describe how to establish SVN collaboration environment on Linux CentOS server and Eclipse IDE.

Server configuration

Eclipse configuration

"With Sun Microsoftware’s help, the PHP of today (as opposed to yesterday) is a fully Orient Objected language suitable for cross flatworm use on a variety of scalable architectures. No one really knows what any of that means, but it sounds very good in the brochures."

Uncyclopedia

(Source: uncyclopedia.wikia.com)

Single (monolithic) controllers in OOP

Nokia 3310Sometimes I need to do some fancy coding in PHP but do not want to overcomplicate my projects with all those trending tools and frameworks. I simply scrape a single class which provides project’s functionality within its scope.

Now I know it’s not the best practice to write monolithic controllers, but seriously, I’m not doing anything nasty either. A lot of `monolithic` things are awesome, e.g. this “Nokia 3310” phone; it’s handy and durable.

This is a perfectly valid PHP script:

<?php

global $data;

function get_some() {
	global $data;
	$data = 'A well known fact';
}

function get_all() {
	global $data;
	$data .= ' and a secret';
}

function login() {
	// Check login...
	return TRUE;
}

get_some();

if(login()) {
	get_all();
}

echo $data;

There’s nothing wrong with this CodeIgniter MVC controller either:

<?php

class Home extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->load->model('User_model');
		$this->load->model('Data_model');
		// Model implementation is arbitrary...
	}

	public function index()
	{
		$this->Data_model->get_some();

		if($this->User_model->login())
		{
			$this->Data_model->get_all();
		}

		$this->Data_model->print_data();
	}

}

Instead of going dirty with procedural PHP or overcomplicating it with a MVC framework, you may consider writing your app as a class (and instantiate it afterwards). As in (almost) every MVC controller, the point is to have all available resources at your disposal within a single class avoiding the use of global $variables. No separate files for a small project is also a plus.

Here we go:

<?php

class App {

	private $data;

	public function __construct() {
		$this->get_some();

		if($this->login()) {
			$this->get_all();
		}

		echo $this->data;
	}

	public function get_some() {
		$this->data = 'A well known fact';
	}

	public function get_all() {
		$this->data .= ' and a secret';
	}

	public function login() {
		// Check login...
		return TRUE;
	}

}

$app = new App();

Quite neat, isn’t it?

Maintaining JavaScript float precision

Sometimes in JavaScript (and other languages) float numbers lose precision, e.g. 1.991 * 100 would be 199.10000000000002. That happens due to CPU limitations. Quite brutal but effective solutions exists.

function maintainFloatPrecision(number) {
	if(number%1 == 0) { // Check if number has a remainder.
		return number;
	} else {
		var str = "" + number;
		if(str.match(/\d+\.\d*0{6,}\d/)) {
			pos = str.search(/0{6,}\d/g);
			return number.toFixed(pos - 2);
		} else if(str.match(/\d+\.\d*9{6,}\d/)) {
			pos = str.search(/9{6,}\d/g);
			return parseFloat(number.toFixed(pos - 1));
		} else {
			return number;
		}
	}
}

Note: This function proves that 100 ÷ 3 × 3 = 100. :)

yum tips

Using Bitbucket with Eclipse and msysgit

  1. Register at Bitbucket (https://bitbucket.org/).
  2. Install msysgit (http://code.google.com/p/msysgit/).
  3. Get a SSH key (in Git Gui tool click menu Help / Show SSH Key) and add it to your Bitbucket account (https://bitbucket.org/account/).
  4. Create/clone your repo as shown in Doug’s videos: 1st video, 2nd video.