Recaptcha basic php class

See Demo
@ PhpClasses.org

<?php
/*
 *	Easy Recaptcha : Basic class
 *	Abdelkader ELKALIDI | contact@updel.com
 *	More infos : https://www.google.com/recaptcha
 */
class Recaptcha{
	public 		$theme		=	'clean';
	public 		$lang		=	'en';
	public		$publicKey;
	public 		$privateKey;
	public 		$messages	=	array(
									'invalid-site-private-key'	=>	'We weren\'t able to verify the private key.',
									'invalid-request-cookie'	=>	'The challenge parameter of the verify script was incorrect.',
									'incorrect-captcha-sol'		=>	'The CAPTCHA solution was incorrect.',
									'recaptcha-not-reachable'	=>	'reCAPTCHA never returns this error code. A plugin should manually return this code in the unlikely event that it is unable to contact the reCAPTCHA verify server.',
								);
	protected 	$error;
	protected 	$recaptchaServer 		= 	'http://www.google.com/recaptcha/api';
	protected 	$recaptchaSslServer 	= 	'https://www.google.com/recaptcha/api';
	protected 	$recaptchaCheckServer 	= 	'http://www.google.com/recaptcha/api/verify';

	/*
	 *	Nothing
	 */
	function __construct(){
	}

	/*
	 *	Debugger
	 */
	public function getError() {
		return !empty($this->error)	? $this->messages[$this->error] : false;
	}

	/*
	 *	Get Recaptcha
	 */
	public function getCaptcha($ssl = 0){
		$server	=	($ssl)	?	$this->recaptchaSslServer	:	$this->recaptchaServer;
		$error	=	(!$this->getError()) ? null : "&amp;error=" . $this->getError();
		$html	=	'
		<script type= "text/javascript">
			var RecaptchaOptions = {theme: \''.$this->theme.'\', lang : \''.$this->lang.'\'	};
		</script>
		<script type="text/javascript" src="'.$server.'/challenge?k='.$this->publicKey.$error.'"></script><noscript><iframe src="'.$server.'/noscript?k='.$this->publicKey.$error.'" height="300" width="500" frameborder="0"></iframe><br /><textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea><input type="hidden" name="recaptcha_response_field" value="manual_challenge"/></noscript>';
		return	$html;
	}

	/*
	 *	Check Recaptcha
	 */
	public function checkCaptcha(){
        $data 	= 	$this->getBySocket(
								$this->recaptchaCheckServer,
								array (
									'privatekey'	=> 	$this->privateKey,
									'remoteip'		=> 	$_SERVER["REMOTE_ADDR"],
									'challenge'		=> 	$_POST['recaptcha_challenge_field'],
									'response'		=> 	$_POST['recaptcha_response_field'],
								)
							);

        $array 			= 	explode("\n", $data);
		$this->error 	= 	(trim($array[0]) == 'false') ? trim($array[1]) : null;
	}

	/*
	 *	Send Data using Sockets
	 */
	private function getBySocket($url, $data) {
		$data		=	$this->concatData($data);
		$post	 	= 	curl_init();
		curl_setopt($post, CURLOPT_URL, $url);
		curl_setopt($post, CURLOPT_USERAGENT, $data);
		curl_setopt($post, CURLOPT_POST, 1);
		curl_setopt($post, CURLOPT_POSTFIELDS,$data);
		curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
		$response = curl_exec($post);
		curl_close($post);
		return $response;
	}

	/*
	 *	Concat Data
	 */
	private function concatData($data) {
		if(is_array($data)){
			foreach ($data as $key => $value){
				$var[]	= $key."=".$value;
			}
			return implode("&",$var);
		}
		else{
			return $data;
		}
	}
}

Example :

<form method="post" action="">
<?php
    require 'recaptcha.class.php';

    $captcha    =    new Recaptcha;
    $captcha->publicKey        =    'your public API key';
    $captcha->privateKey    =    'your private API key';

    if(isset($_POST) AND !empty($_POST)){
        $captcha->checkCaptcha();
        $debug    =    $captcha->getError();
        if(!$debug){
            echo 'All right';
        }
        else{
            echo $debug;
        }
    }

    echo $captcha->getCaptcha();
?>
<input type="submit" value="Check captcha" />
</form>

Zend Framework : Modular Application [Part 1]

Project description : A simple guestbook module using Zend Framework 1.11 .

Project

Create new project using Zend_Tool :

zf create project MyApp
cd MyApp

Create module :

zf create module Guestbook

Create controller for Guestbook (1 = index action included):

zf create controller Index 1 Guestbook

Now go to /MyApp/application/configs/application.ini and comment/delete the line :

;resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

Then add the lines

;Modular suport
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =

And add this lines to set Guestbook as default module

resources.frontController.params.prefixDefaultModule = "1"
resources.frontController.defaultModule = "Guestbook"

Create php file “Bootstrap.php” under folder /MyApp/application/modules/Guestbook and past the code

<?php
class Guestbook_Bootstrap extends Zend_Application_Module_Bootstrap{}

Now test your app

Read the rest of this entry »

Check if “is digits” and does not begin with zero

Protecting Your PHP/SQL Queries from SQL Injection is one of coding obligations, if you have tables with AUTO_INCREMENT and you want to check IDs before running queries, this function is yours.

function is_digits($string){
	return preg_match("!^[1-9]([0-9]+)?$!",$string);
}

Examples :

is_digits(0); // Return 0
is_digits(1); // Return 1
is_digits("1"); // Return 1
is_digits(01); // Return 0
is_digits(-1); // Return 0
is_digits("1a"); // Return 0
is_digits("a1"); // Return 0
is_digits(15); // Return 1
is_digits(1,5); // Return 0
is_digits(1.5); // Return 0

Mysql FIND_IN_SET()

FIND_IN_SET() : A function that I discovered some years ago and which I find very impressive if you’re addicted to query optimization.
This function is a bit like in_array() but is doing research in StringList separated by commas “,”

Here is an example to understand:

Table : Clubs
table_clubs

Table : Leagues
championnats

To select leagues which participates BARCA (having id 11), SQL code would look like this :

SELECT    league_id,name
FROM      Leagues
WHERE     FIND_IN_SET(11, clubs_id)

Create a new directory having 0777 permissions

I need to create unique directory for every new member, which will be used to store their pictures. I’ve tried :

mkdir("user_name", 0777);

I got : mkdir() [function.mkdir]: Permission denied

It work when I chmod parent directory but can not give permissions “0777”, therefore I add umask()

umask(000);
mkdir("user_name", 0777);

Eurêka BUT ! the new folder is now owned by mr apache

Owned by apache

Now imagine that we want to create a subfolder like “user_name/gallery” ! we will get an error like :

Warning: mkdir() [function.mkdir]: SAFE MODE Restriction in effect. The script whose uid is XXX is not allowed to access /var/www/vhosts/updel.com/httpdocs/test/test owned by uid 48 in /var/www/vhosts/updel.com/httpdocs/test.php on line 4

It mean we do not have permission to touch a directory owned by another user !!!!!!!!

The solution : FORGET mkdir() and through ftp using PHP, the code look like this

$connexion_ftp = ftp_connect("Ftp_url"); //Connexion ftp
ftp_login($connexion_ftp, "Ftp_login", "Ftp_Password");
ftp_mkdir($connexion_ftp, "new_dir"); // user_name in my case
ftp_site($connexion_ftp,"CHMOD 777 new_dir");

Thumbnail Drag then Crop like facebook using mootools

I like facebook profile code, when you upload picture you can drag and drop it inside a small box, to get a thumbnail showing the good part of the picture.

facebook_thumbnail

It’s very easy to do it using mootools core and mootools Drag plugin

Click here to see demo
Read the rest of this entry »

PHP cli progress Bar

This is a simple trick to make your PHP console application more funny

Read the rest of this entry »