Archive

Archive for August, 2008

Safe and Dirty Remote XMLHttpRequest

August 27th, 2008

Safe and Dirty Remote XMLHttpRequest Sometimes we need XMLHttpRequest work on diferents domains (cross domains) a simple solution, you need JavaScript and Perl:

The XMLHttpRequest(); example:

var req = new XMLHttpRequest() // IE7, Firefox, Safari, Opera
req.open('GET', 'http://localhost/cgi-bin/remote.pl?url='+url, false);
req.send(null);

To make the remote request work, use remote.pl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl
use LWP::UserAgent;
use CGI qw(:standard);
print "Content-type: text/plain\n";
my $url = param ('url');
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/8.0");
$req = HTTP::Request->new(GET => $url);
$req->header('Accept' => 'text/html');
$res =$ua->request($req);
if ($res->is_success) {
	$page = $res->content;
} else {
	$page = "error";
} print $page;

This is only a example:

The remote.pl could be much better implement (GET and POST methods, params and theirs values).

develop, web ,

JSC JavaScript Collection

August 24th, 2008

JSC JavaScript Collection

We need jsmin and gzip, compile the jsmin.c:

gcc -o jsmin jsmin.c

Script to generate a collection of the javascript files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
 
echo "JavaScript Collection"
 
  EXPECTED_ARGS=1
  E_BADARGS=65
 
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` {path of the javascripts}"
  exit $E_BADARGS
fi
 
# Join the javascript scripts in one file
cat $1/*.js >> $1/.__fullscripts.tmp
 
# use JSMIN to minifier the script
cat $1/.__fullscripts.tmp | jsmin > $1/.__fullscripts.min.tmp
 
# awk remove the first blank line, gzip compress the script
awk 'FNR>1{print}' $1/.__fullscripts.min.tmp | gzip > $1/collection_scripts.js.gz
 
# remove temporal files
rm $1/.__fullscripts.tmp $1/.__fullscripts.min.tmp

Howto: jsc public_html/web/js/

Now create one php script to replace all javascripts:

<script type="text/javascript" src="collection_scripts.php"></script>

Content of collection_scripts.php:

<?php
    header("Content-type: text/javascript; charset: UTF-8?");
    header("Content-Encoding: gzip");
    readfile("collection_scripts.js.gz");
?>

unix/linux , ,

Insert Any Video Of YouTube

August 23rd, 2008

Insert Any Video Of YouTube

I made this little Javascript function, this function insert any youtube video on a <div>.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Script: YouTubeVideo Version 1.0 (2008/07/22)
// Author: Alberto Isaac Ayala Esquivias (Albertux)
// Licence: http://creativecommons.org/licenses/by-nc-sa/3.0/
// FeedBack to: albertoi7@gmail.com
// About: Insert youtube video on a div doesn't matter if embed code is disable this script insert the video.
 
function YouTubeVideo(id,url) {
 
	var video_html ='<object width="425" height="344"><param name="movie" value="http://www.youtube.com/swf/l.swf?video_id=VIDEO&rel=1&iurl=null&autoplay=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/swf/l.swf?video_id=VIDEO&rel=1&iurl=null&autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>';
 
	var n = url.indexOf('v=');
 
	var id_video = url.substring(n+2,n+14);
 
	document.getElementById(id).innerHTML=video_html.replace(/VIDEO/g, id_video);
 
}

Howto use:

<input type="button" onclick="YouTubeVideo('contenedor','http://www.youtube.com/watch?v=7ykWgiZVJe0');" value="Watch  Video">
 
<div id="contenedor"></div>
  

On load page:

// JavaScript onload event example:
window.onload = function() { YouTubeVideo('contenedor','http://www.youtube.com/watch?v=7ykWgiZVJe0'); }
 
// jQuery example:
$(document).ready(function() {
	YouTubeVideo('contenedor','http://www.youtube.com/watch?v=7ykWgiZVJe0');
});

develop, web ,

Safari On Ubuntu

August 22nd, 2008

Safari On Ubuntu

Script to install Safari on Ubuntu 8.04:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
echo "Safary on Ubuntu Lazzy Script Installer (Ubuntu 8.04)"
sudo apt-get install msttcorefonts
sudo apt-get install wine
echo "Copy some MS-Fonts to local drive_c"
cp /usr/share/fonts/truetype/msttcorefonts/Arial*.ttf ~/.wine/drive_c/windows/fonts/
cp /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman*.ttf ~/.wine/drive_c/windows/fonts/
echo "Download SafariSetup and Flash Plugin"
wget http://ubuntu-debs.googlecode.com/files/SafariSetup.exe
wget http://ubuntu-debs.googlecode.com/files/install_flash_player.exe
echo "Install Safari"
wine SafariSetup.exe
echo "Install Flash Plugin"
wine install_flash_player.exe
echo "finish."

Reference:
http://www.ubuntu-unleashed.com/2008/03/howto-install-safari-on-ubuntu-with.html

unix/linux , , ,

Standalone Executable Perl script

August 19th, 2008

Standalone Executable Perl script

We need a PAR::Packer module of Perl

On GNU/Linux Perl is al ready installed, on Win32 you need to install Strawberry Perl (ActivePerl don’t work).

Now in your system command line (cmd.exe/bash) you need run:

cpan -i pp

You asume yes on all questions (Par::Packer needs another modules to work)

Now you can compile standalone executable perl scripts using this:

pp -o standalone myscript.pl

Note: on Win32 you can change standalone to standalone.exe (take more time make a standalone on Win32).

develop ,

Compile Perl Win32

August 17th, 2008

Compile Perl Win32

First you need download the Visual Studio 2008 C++ Express and Perl source code.

Use 7-zip to decompress perl-5.10.0.tar.gz

Edit Makefile (on perl-5.10.0\win32\) to set C++ Compiler:

CCTYPE = MSVC90FREE

Run CMD.EXE and write this:

"C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
cd perl-5.10.0
cd win32
nmake
nmake test
nmake install

Compile process will take some time but you can download MyPerl-5.10.0.zip
extract on C:\Perl to include Perl on your path:

set PATH=%PATH%;C:\Perl\bin

Alternatives if you don’t want to compile: ActivePerl or Strawberry Perl.

develop, windows , ,

GeoIP Database MySQL

August 8th, 2008

GeoIP Database MySQL

Sometimes we need to know the country of the users on the website, you can know using only the ip address.
I write this simple Bash script to install on a new database (or existing database) the table “geoip”, now you can use PHP, Python, Perl, Ruby or whatever you want.

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
#!/bin/bash
# Script: geoip.sh
# Author: Alberto Isaac Ayala Esquivias
# E-mail: <albertoi7@gmail.com>
# Web: http://albertux.ayalasoft.com
clear
echo "=====================================-"
echo " GeoIp MySQL Database Easy Installer. "
echo "======================================"
echo ""
echo "Script Created By http://AyalaSoft.com";
echo ""
stty -echo
read -p "MySQL root password: " mysql_pass; echo
stty echo
echo -n "Do you want to create database of MySQL (y/n): "
read op
if [ "$op" = "y" ]; then
echo -n "Create new database, name: "
	read database
	mysqladmin -u root --password=$mysql_pass create $database
else
echo -n "Which database do you want to use to insert 'geoip' table: "
read database
fi
echo -e "\nDownload  [ GeoIPCountryCSV.zip ]"
wget -c "http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip"
echo -e "\nUnZip [ GeoIPCountryCSV.zip ]"
unzip GeoIPCountryCSV.zip
echo -e "\nInsert geoip table to database $database"
awk -F, ' { print $3","$4","$5","$6 }' GeoIPCountryWhois.csv | sed s/\"//g > /tmp/geoip.txt
chmod 777 /tmp/geoip.txt
echo "
USE $database;
 
DROP TABLE IF EXISTS geoip;
 
CREATE TABLE geoip (
	ip_begin int unsigned,
	ip_end int unsigned,
	country varchar(2),
	country_name varchar(30)
);
 
LOAD DATA LOCAL INFILE '/tmp/geoip.txt'
INTO TABLE $database.geoip
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(ip_begin, ip_end, country, country_name);
" > geoip.sql
mysql -u root --password=$mysql_pass $database < geoip.sql
rm /tmp/geoip.txt
echo -e "\nFinish."

database, develop, network, unix/linux , ,