Archive

Posts Tagged ‘php’

Easy Host Fake Win32

December 27th, 2008

Easy Host Fake Win32

Sometimes we want to redirect some host to another ip address, the quick option is edit the hosts file.

Well i made this tool to do that.

Java Desktop Application, needs Java RunTime:

Easy Host Fake Java Setup

Is my first Java desktop application on NetBeans

PHP5 script, needs Console Getopt Pear Package (getopt function don’t work on Windows, in the next php 5.3.0 could be)

need pear? run go-pear.bat on your php5 directory

c:\>pear install Console_Getopt

Windows Batch file:

:save this ehf.cmd on your %windir%
:set php-cli to your php.exe path
@set php-cli=c:\php5\php.exe
:set ehf script path
@set ehf=c:\develop\php\scripts\ehf.php
@%php-cli% %ehf% %1 %2 %3 %4

Script ehf.php:

 /* Script: Easy Host Fake
  * Author: Albertux (Alberto Isaac Ayala Esquivias)
  * WebSite: http://Albertux.AyalaSoft.com
  * FeedBack: <albertoi7@gmail.com>
  * License: GPLv3 (http://www.gnu.org/licenses/gpl.txt)
  * Note: you need Pear Package Console Getopt
  */
$ip_default = '127.0.0.1'; // default ip
$hosts_file = 'c:/windows/system32/drivers/etc/hosts'; // default hosts file
 
require_once 'Console/Getopt.php';
$options = Console_Getopt::getopt($_SERVER["argv"],"a:e:r:");
 
function about() {
echo "
Easy Host Fake (version 1.0.1) [2008-12-27]
 
Usage: ehf [OPTIONS]
 
Mandatory: \"-r\" or \"-e\" [host]
 
Options:
 
  -r [host] [redirect host to 127.0.0.1 (or other ip address)]
  -e [host] [erase host on hosts file]
  -a [ip address]
 
Examples:
 
  ewf -r google.com -a 192.168.1.1
  ewf -e google.com
 
More info [http://Albertux.AyalaSoft.com/2008/12/28/easy-host-fake-win32/]
";
}
 
function AddHost($host,$ip) {
	global $hosts_file;
	$handle = fopen($hosts_file,"a");
	fwrite($handle,"\r\n$ip\t$host\t# Easy Host Fake");
	fclose($handle);
}
 
function removeHost($host) {
	global $hosts_file;
	$handle = fopen($hosts_file, "r");
	$contents = fread($handle, filesize($hosts_file));
	fclose($handle);
	$pattern = '/\\r\\n(\d+)\.(\d+)\.(\d+)\.(\d+)\t'.$host.'\t# Easy Host Fake/';
	$replacement = "";
	$newcontents = preg_replace($pattern, $replacement, $contents);
	$handle = fopen($hosts_file,"w");
	fwrite($handle,$newcontents);
	fclose($handle);
}
 
function getValue($data,$var) {
	for ($i=0; $i<count($data[0]); $i++) {
		if ($var == $data[0][$i][0]) {
			break;
		}
	}
	return $data[0][$i][1];
}
 
if ($options->message) { about(); die(); }
 
$host = getValue($options,"r");
$ip = getValue($options,"a");
 
if (!$host) {
	$host = getValue($options,"e");
	if ($ip) { about(); die(); }
	if (!$host) { about(); die(); }
	removeHost($host); die();
}
 
if (!$host) { about(); die(); }
if (!$ip) { $ip = $ip_default; }
 
addHost($host,$ip);

Download php script + batch cmd

Note: on Windows Vista you need some permissions on hosts file, and remember all Web browsers use cache.

network, projects, security, web, windows , , ,

SQL File to PHP5 Classes (sql2class)

November 25th, 2008

SQL File to PHP5 Classes (sql2class)

(shell prompt) perl sql2class.pl database.sql > classes.php

Download sql2class-0.2.tar.gz sql2class-0.3.tar.gz

I have little time programming in Perl hope to expand this script to support other languages in the future

With this post I dismissal from 2008 until January 2009.

database, develop , ,

PHP Access Control List (ACL)

November 13th, 2008

PHP Access Control List (ACL)

I made this Access Control List for Functions:

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
// Author Albertux (Alberto Isaac Ayala Esquivias)
// E-mail: albertoi7@gmail.com
// Web: http://albertux.ayalasoft.com
// Class: Access Control List
 
class ACL {
	public $functions;
 
	public function acl_function($function,$status) {
		if (function_exists($function)) {
			if($status==0) {
				$this->functions[$function]=0;
			} else {
				$this->functions[$function]=1;
			}
		}
	}
 
	public function acl_methods_class($class,$value) {
		$class_methods = get_class_methods($class);
		foreach ($class_methods as $method_name) {
			$this->functions[$class.'::'.$method_name]=$value;
		}
	}
 
	public function acl_functions($functions) {
		foreach($functions as $function => $value) {
			$this->functions[$function]=$value;
		}
	}
 
	public function execute($function,$params=NULL)  {
		$output='';  
		if($this->functions[$function]==1) {
				$output = call_user_func_array($function, $params);
			} 
		return $output;
	}
}

HOWTO use:

$ACL = new ACL();
 
// Array Functions, 0 = don't execute, 1 = execute
$functions = array ("somefunction" => 0, "otherfunction" => 0, "anotherfunction" => 1);
 
// Add functions on ACL
$ACL->acl_functions($functions);
 
$params = array("param 1", "param 2", "param 3");
$ACL->execute('somefunction',$params); // don't execute because on functions array 'somefunction ' => 0
 
// Add or modify function access
$ACL->acl_function('somefunction',1);
$ACL->execute('somefunction',$params); // execute, now 'somefunction' => 1
 
// Put the functions of the class 
$ACL->acl_methods_class('SomeClass',1);
 
// Execute a method from class
$ACL->execute('SomeClass::demo', $params);

develop, security

Using Windows ? Don’t kill Yourself

October 27th, 2008

Using Windows ? Don’t Kill Yourself

Supose you are Linux user and you have a Windows system.

Alternatives: Cygwin, Mingw or Colinux

You need some (or all) of this interpreters:

Perl, PHP, Python and Ruby

Modify the %PATH%

Example you have a bin dir, on C:\ to include the executables on the %PATH% do this:

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

On Windows Vista, you can use your mouse:

computer > properties > Advances system settings > (continue) > Advanced > Enviroment Variables > System Variables > Path (edit)

Example run Kompozer on cmd.exe but no modify %PATH%

: kompozer.bat save on %WINDIR%
@"C:\Program Files\Kompozer\kompozer.exe"

Doble click on your scripts to run:

: Perl Scripts
 assoc .pl=Perl.File
 ftype Perl.File=C:\strawberry\perl\bin\perl.exe "%1" %*
 set PATHEXT=%PATHEXT%;.pl
: Python Scripts
 assoc .py=Python.File
 ftype Python.File=C:\Python26\python.exe "%1" %*
 set PATHEXT=%PATHEXT%;.py

Note:

On Windows Vista you need run cmd.exe as Administrator don’t work as normal user

Apache, PHP and MySQL ?

XAMPP and Server2Go

Notes:

XAMPP: simple, quick and easy install.

Server2Go: is great to make CD demo of a Web Application.

Using Perl or Python as CGI on Windows ?

Using Python 2.6

#!\python26\python.exe

Using Strawberry Perl:

#!\strawberry\perl\bin\perl.exe

Now your script works on http://localhost/cgi-bin/script.cgi

develop, unix/linux, windows , , , , ,

PDF Libraries

October 2nd, 2008

PDF Libraries

Sometimes we need create or export data into a PDF:

.NET:

http://www.pdfsharp.com, http://www.codeplex.com/Print2Pdf

C++:

http://libharu.org

Perl:

http://search.cpan.org/~markusb/PDF-Create/lib/PDF/Create.pm

sudo perl -MCPAN -e 'install PDF::Create'

PHP:

http://www.fpdf.org, http://www.digitaljunkies.ca/dompdf, http://pear.php.net/package/File_PDF

Python:

http://www.reportlab.org

wget -c http://www.reportlab.org/ftp/ReportLab_2_2.tgz
tar xvzf ReportLab_2_2.tgz
cd ReportLab_2_2
sudo python setup.py install

Java:

http://www.lowagie.com/iText, http://www.pdfbox.org

Ruby:

http://ruby-pdf.rubyforge.org/pdf-writer

sudo gem install pdf-writer

develop , , , , , ,

GeoIP PHP Script

October 1st, 2008

GeoIP PHP Script

I made this script utility to check what country is some ip, you need GeoIP Database

I hope you have PHP in your path:

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
#!/usr/bin/php -q
<?
 
if (count($_SERVER['argv']) <= 1) {
	echo "use: geoip xxx.xxx.xxx.xxx\n";
}
else {
	$ip=$_SERVER['argv'][1];
	$out=sprintf("%u", ip2long($ip)); // don't ask why, you need this line	
 
	$sql="SELECT country_name FROM geoip WHERE ($out BETWEEN ip_begin AND ip_end)";	
 
	$link = mysql_connect('localhost','root',''); // HOST, USER, PASS
	mysql_select_db('GEOIP'); // DATABASE
 
	$query = mysql_query($sql,$link);	
 
	while ($row = @mysql_fetch_array($query,MYSQL_ASSOC)) {
		$data[] = $row;
	}	
 
	if (count($data) == 1) {
		echo $data[0]['country_name']."\n";
	}
	else {
		echo "Unknown\n";
	}
}
?>

Examples:

~$ geoip 79.130.90.12
Greece
~$ geoip 207.33.11.33
United States
~$ geoip 148.234.13.23
Mexico

I think i made a good tool.

database, network , ,

qTranslate Tag Cloud Fixed

September 29th, 2008

qTranslate Tag Cloud Fixed

If you have qTranslate plugin for wordpress and you have some issues on your tag cloud.

The problem was that the function that generated the tag cloud in spanish the size of the sources they put comma instead of point

On /wp-includes/category-template.php in function wp_generate_tag_cloud check the line 422, str_replace() fix the problem:

418
419
420
421
422
423
424
	foreach ( $counts as $tag => $count ) {
		$tag_id = $tag_ids[$tag];
		$tag_link = clean_url($tag_links[$tag]);
		$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __ngettext('%d topic','%d topics',$count), $count ) ) . "'$rel style='font-size: " .
			str_replace(",",".",( $smallest + ( ($count - $min_count ) * $font_step ) ))
			. "$unit;'>$tag</a><!-- $font_step -->";
	}

web ,