Saturday, August 27, 2011

Leverage browser caching - Apache optimization

Leverage browser caching. Specify an expiration at least one week in the future. Apache optimization.

$ sudo cp /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/

# Add to /etc/apache2/sites-enabled/000-default or apache2.conf :
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

$ sudo /etc/init.d/apache2 restart

Javascript Regexp Match Test

Javascript Regexp Match Test

var my_string = '2010-09-01';
var regExp = new RegExp('^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$', 'gim');
if(my_string.match(regExp)) {
    alert('String matches expression');
} else {
    alert('String doesn't match expression');
}

Specify a character set in HTTP headers - Apache optimization

Specify a character set in HTTP headers - Apache optimization

AddDefaultCharset utf-8

Friday, August 19, 2011

PHP - Better and faster random values with mt_rand()

It's recommended to use mt_rand() instead of rand() function.

Powered by Mersenne Twister algorithm, mt_rand() is faster and produces better random values.

Ex: $rand = mt_rand(2, 200);

Ubuntu: Convert JPG files to PDF

Convert multiple JPG files to one PDF document

$ sudo apt-get install imagemagick

$ cd /...path-to-jpg-files...

$ convert jpg-file-* document.pdf

Ubuntu: Mass resize pictures

1) Applications -> Graphics -> gThumb Image Viewer

2) Select photos to be resized

3) Tools -> Scale images

Wednesday, August 17, 2011

Ubuntu 10.04 - Eclipse 3.2.2 - Projects list disappears after Ubuntu update

Problem description: After an Ubuntu update the Navigator in Eclipse is empty, it contains no projects. Creating new projects and restarting Eclipse (after reboot) ends with an empty project list.

Eclipse Error (error log file):
!MESSAGE Skipping builder org.eclipse.dltk.core.scriptbuilder for project PROJECT_NAME. Either the builder is missing from the install, or it belongs to a project nature that is missing or disabled.

Solution:

1) Install Java JDK: http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html

2) Convert RPM to DEB (with alien)

3) sudo dpkg -i jdk_xxxxx.deb

4) reboot

Problem fixed!

PHP - Messages handling - Success and error messages, session based system

Handle the display of errors and success messages across all website sections.

The functionality is very simple, allowing to set a success or error message anywhere in the execution thread (in the main file, in a function or in a class method). Once the message is stored (written to session in this case), it is scheduled to be displayed. After the message is displayed, it is deleted from session. The Singleton implementation for the Messages class offers extra programming speed and ease of use.

1) Class definition

class SessionMessages {
   
    static private $instance = null;
   
    private $session_name = 'session_messages';
   
    public function __construct() {
        if(!isset($_SESSION[$this->session_name])) {
            $_SESSION[$this->session_name]['message'] = '';
            $_SESSION[$this->session_name]['message_available'] = false;
            $_SESSION[$this->session_name]['message_error'] = '';
            $_SESSION[$this->session_name]['message_error_available'] = false;
        }
    }
   
    public function getInstance() {
        if(!self::$instance) {
            self::$instance = new SessionMessages();
        }
        return self::$instance;
    }
   
    public function setMessage($_message) {
        $_SESSION[$this->session_name]['message'] = $_message;
        $_SESSION[$this->session_name]['message_available'] = true;
    }
   
    public function messageAvailableToDisplay() {
        return $_SESSION[$this->session_name]['message_available'];
    }
   
    public function getMessageForDisplay() {
        $_SESSION[$this->session_name]['message_available'] = false;
        return $_SESSION[$this->session_name]['message'];
    }
   
    public function setMessageError($_message) {
        $_SESSION[$this->session_name]['message_error'] = $_message;
        $_SESSION[$this->session_name]['message_error_available'] = true;
    }
   
    public function messageErrorAvailableToDisplay() {
        return $_SESSION[$this->session_name]['message_error_available'];
    }
   
    public function getMessageErrorForDisplay() {
        $_SESSION[$this->session_name]['message_error_available'] = false;
        return $_SESSION[$this->session_name]['message_error'];
    }
   
}

2) Storing a success message
if(...database insert ok...) {
    SessionMessages::getInstance()->setMessage('Custom OK message.');
}

3) Storing an error message
if(...database insert failed...) {
    SessionMessages::getInstance()->setMessageError('Errors encountered.');
}

4) The file which displays the messages (included in header.php for example):

if(SessionMessages::getInstance()->messageAvailableToDisplay()) {
    echo '<div class="msg">' . SessionMessages::getInstance()->getMessageForDisplay() . '</div>';
}

if(SessionMessages::getInstance()->messageErrorAvailableToDisplay()) {
    echo '<div class="errors">' . SessionMessages::getInstance()->getMessageErrorForDisplay() . '</div>';
}

It's the most rapid solution for managing messages in any PHP application.

Ubuntu - Convert RPM to DEB

Install alien:
$sudo apt-get update
$sudo apt-get install alien

Convert to deb:
$sudo alien -k file.rpm

Install deb package:
$sudo dpkg -i file.deb

Tuesday, August 16, 2011

WTX - Wstats Trafic Index - new Wstats.net Index

Wstats Trafic Index - WTX

WTX is the newest and most important indicator for a website and a user.
The four most important values for a website (Visitors, Visits, Pageviews, Bounce) are used to compute WTX, according to the formula:

Avg1 = (Visitors + Visits + Pageviews) / 3;
Val1 = (Visitors + Visits + Avg1) / 3;
Val2 = sqrt(1 + (1 - Bounce / 100));
WTX = 17 * sqrt(Val1 * Val2);
sqrt - square root

WTX index for a user is the sum of all WTX indexes of the websites belonging to the user.

This is a major update for Wstats.net, now every user having a public indicator that can be consulted anytime, by anyone, an indicator which doesn't divulge the private values: visitors, visits, pageviews or bounce rate, but yet offers an image of the user's activity.

User example: http://wstats.net/en/user/eydos
Website example: http://wstats.net/en/website/sursadestiri.ro

Monday, August 15, 2011

Javascript - Lowercase HTML input field

Lowercase HTML input field example:

<input id="title_x" type="text" name="title" value="Uppercase TEXT SAMPLE"/>

<span onclick="document.getElementById('title_x').value = document.getElementById('title_x').value.toLowerCase();" style="cursor: pointer;">lowercase</span>

Thursday, August 11, 2011

Gropi cosmice, partea a 2-a

Acum aproximativ o luna, Guatemala. Cauza fenomenului (versiunea oficiala): furtunile tropicale.

Acelasi fenomen, in 2010, in Germania (unde nu exista furtuni tropicale) si Guatemala: http://ionutb.eydos.ro/2010/11/gropi-cosmice.html

Tuesday, August 9, 2011

Jim Rogers on U.S. Downgrade, Global Markets, 08 aug 2011

Best Ubuntu mp3 player: Audacious

Audacious offers the best sound quality and also has the old xmms style (perhaps also the code).

Other mp3 players tested under Ubuntu, Gnome:
- Exaile Music Player
- RhythmBox Music Player
- Amarok