Installing Laravel 4 in Windows 7 with IIS7

Installing-Laravel-4-in-Windows-7-with-IIS7
There are always some boring steps until we can actually start using a framework.
Moreover working with Windows and IIS makes things more difficult/tedious yet. I always find myself having to google these kind of things and this time I decided to keep a record of the steps I followed. This way I won’t have to face the same problems in future installations in other machines. Also, hopefully this can help someone else in the same situation!
Continue reading

Validating CakePHP users from other PHP service

auth-cakephp-php
Nowadays is very common to synchronize different applications, relate them somehow or even share the users accounts.

Working with a framework such as CakePHP make thing easier but at the same time it hides all those actions taking place behind the scenes.

CakePHP automatically hashes the user passwords to secure them and store the hash in the users table of the database. A hashed password with CakePHP 2.3 looks like this: f061c4f1a0b786c3b05dd0013a0230d767l19b77

Continue reading

Disabling JavaScript console in production enviroments and Internet Explorer

We don’t want to show our stuff in public. Having debug information in production doesn’t make any sense.

We usually use the console.log() function of JavaScript for this tasks, and is correct, but we don’t want to comment and uncomment all of those lines every time we move our files to the production server.

Also, console.log() function doesn’t work in most versions of Interet Explorer (8 inclusive if you don’t enable and actually use the development tools functionality).

Having say that, lets fix it in a very simple way:

var console = { log: function() {} };

We have just overwritten the console.log function for an empty function which will not do anything.
Now, if we just want to do it in the production mode, we can add that javascript lines inside a condition like this one:

if(Configure::read('environment') != 'development' || strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE')){
    echo '
        <script type="text/javascript">
            var console = { log: function() {} };
        </script>
   ';
}

And what if we want to disable it with any Internet Explorer version?
We could have done it with jQuery, JavaScript or even HTML comments, but i preferred the PHP method for it:

if(strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE')){ ...

Then we just mix it with the previous code and we have the magic done:

//disable console.log in production and in Internet Explorer
if(Configure::read('environment') != 'development' || strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE')){
    echo '
        <script type="text/javascript">
            var console = { log: function() {} };
        </script>
   ';
}

Using php GD Library to write text over images using TrueType fonts

textPainter class
Once i needed to write text over an image and generate a final image with it.
In order to do it, i used the php GD library functions and a TrueType font as i wanted to use high font sizes.

Now, i have created a PHP class to facilitate all this process.
It is compatible with the following image formats:

  • jpg / jpeg
  • png
  • gif
  • wbmp

And it allows us to positionate the text wherever we want under predefied positions or custom ones.
Continue reading

Creating Url Helper for CakePHP framework

The other day i created a simple helper for CakePHP framework.
I called it URL Helper. It contains the following methods:

  • getRealURL($short)
  • getDomain($url)
  • isSecure($url)
  • getProtocol($url)
  • getPort($url)
  • getExtension($url)
  • addHttp($url)
  • getShortURL($url)
  • isYoutubeVideo($url)
  • getParam($url, $var)
  • getNumberOfParams($url)
  • getParams($url, $vars)
  • isImage($url)
  • getURLFromText($text)
  • eraseURlsFromText($text)
  • shortURLsFromText($text)

Continue reading

How to get the category of a youtube video

Through Youtube API we can get the video category pretty simple. I had to do this at once in order to improve text categorization in case the text contained youtube videos, so i programmed this simple function:

/**
	 * Extracts the category of a youtube video from its API.
	 *
	 * @access public
	 * @param string $idVideo youtube video's id
	 * @return string youtube category (Sports, Tech...)
	 */
	public function getCategory($idVideo){

		//special case for videos id starting with "-" (bug on youtube API)
		if($idVideo[0]=='-'){
			$idVideo = substr($idVideo, 1);
		}

		// set feed URL
		$feedURL = 'http://gdata.youtube.com/feeds/api/videos?ir=ES&q='.$idVideo;

		// read feed into SimpleXML object
		$sxml = simplexml_load_file($feedURL);

		$entry = $sxml->entry[0];
		$media = $entry->children('media', true);
		$group = $media[0];

		return $group->category;
	}

Continue reading

Getting the real URL from a shorted URL

Nowadays it’s very common to find shorted URLs everywhere. I had to deal with this stuff when i had to work with Twitter and its tweets data and i think it might be useful for other people too.

This is what i did:

	/**
	 * Gets the real URL from a shorted one.
	 * In case the URL isn't shorted, it returns the same one.
	 *
	 * @access public
	 * @param string $short URL
	 * @return string real URL.
	 */
	public function getReal($short){
		$headers = get_headers($short, 1);
		$loc = $headers['Location'];

		if(is_array($loc)){
			// get the highest numeric index
			// in case the URL is shorted more than once
			$key = max(array_keys( $loc));

			return $loc[$key];
		}else if(!empty($loc)){
			return $loc;
		}else{
			return $short;
		}
	}

You can have a look at get_headers function at the PHP documentation site:
http://php.net/manual/es/function.get-headers.php