Archive for 'Code'

Domain Name Validation Class for PHP

Learn PHPBy the time I finish writing this, there will probably be 10 new domain validation routines uploaded somewhere on the Internet.  The only problem is, the majority of them are incomplete. Incomplete would be the equivalent to wrong, depending on what you are looking for in a validation routine. The problem? Most developers don’t read the requirements / specifications. The majority of developers do not take time to RTFM (read the freaking manual) for anything. What makes you think we will spend time reading the ever so boring RFC documents?

Domain validation is a common issue as evidenced by the more than 42,000,000 results on Google when you search for domain name validation php. But who can you trust to do it right? If I were to ask you what makes a valid domain name, do you know? Do you know how long a domain name can be? Do you even really know what a domain name is? Continue reading, and I bet you will be enlightened.
Read more

PHP Phone Validation Class

A few days ago, I wrote a post about validating NANPA regulated phone numbers. So late last night, I figured since I have already done the research and figured out the work, why not build a class and put it online for everyone? So that’s what I did.

During the process, I realized how easy it would be to apply the “optional” filters I discussed in my previous post. I document them on the Google Code wiki where my open source repository is.
Read more

PHP Regex for Validating Phone Numbers

So, you want to validate a phone number, huh? This is much different than verifying a phone number. To verify a phone number you could simply call the number to see if it connects to someone. But applications and databases don’t necessarily need to verify the number. On most occasions however, the data should be validated so that before a user sees it, they know it has been validated as a “good” number. I define validated as a number that can be assigned based on the North American Numbering Plan (NANPA).

I have seen numerous attempts to validate a phone number using a simple regex but they are all wrong. They best they can do is validate the number of digits. This method and any similar variation is completely useless! Even if you are checking for all the possible delimiters and removing spaces, etc. So how should it be done then, you ask? Easy!
Read more

Adding SVN Information to File Headers Revisited

You may already be familiar with my previous post on Adding SVN Information to File Headers. After further review, I have decided to update the content to be more explicit. I want to better define how to establish the keywords and their usage in subversion repositories.

If you want to apply a standardized header to the files you are committing to your svn repository, you must first define the keyword list that you want to use. For this explanation and ease of use, I will use the same set of keywords I used in the previous post. Here is the sample header you can add to the top of your file. I use this specifically with php.
Read more

GD and mcrypt on Mac OS X

Recently I have moved to the MacBook Pro. I completely love it! I love the simpleness of the windows environment but the robustness of the underlying power the OS provides. All without the blue screen of death… ever. Having said that, I was struggling with the LAMP integration that comes with the Mac. It is missing two php modules I desire; GD and mcrypt.

So of course I set out on my Google journey. I found a blog that described how to curl -O {URL} some source code, mv /use/local/php, etc. and started to dread what it would take to get the two modules installed. That is until I visited Entropy. I found that their latest release comes as a .pkg that has a built in installer. I was a little nervous that it may mess up my machine. I thought about getting backups in place and started heading that direction. But being impatient, I decided to run with the install. If it fubar’s the machine, I will flame them in their forums. But to my surprise, it couldn’t have been an easier migration. Following the simplicity that the Mac offers, Entropy certainly delivered!

Here is the path I followed:

- download the latest php from http://www.entropy.ch/software/macosx/php/
- dbl click on the downloaded package
- follow the instructions

Now I have GD and mcrypt on my machine and development is still going strong! Thank you Entropy!

CakePHP 1.2 open_base_dir Error

While trying to migrate an application built in CakePHP 1.1.x to CakePHP 1.2.x, we found that the open_base_dir error was causing all kinds of havoc with our application. For security sake, although this is a debatable topic, we are using open_base_dir to “jail” our environments. We contacted the cake irc channel and were told this had already been resolved. Yet we continued to get errors. After 3 days of investigation, we discovered what was actually happening.


Read more

CakePHP 1.2 Cron Dispatcher

While working on a personal project, I discovered I could use a cron_dispatcher. The only problem is I was not certain that the 1.1 version of the cron_dispatcher would work in CakePHP 1.2, so I tried it. The results were frightening… at first. I set everything up and ran the cron from the command line and … Segmentation Fault! EEEK! So I started digging. I soon found there were some simple tweaks to apply to get the 1.1 cron_dispatcher to work.

First, copy and paste the webroot/index.php to your app directory and rename to cron_dispatcher.php.

Edit the bottom of the cron_dispatcher.php. You will replace:

if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
return;
} else {
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch($url);
}
if (Configure::read() > 0) {
echo "<!-- " . round(getMicrotime() - $TIME_START, 4) . "s -->";
}

with

define(CRON_DISPATCHER,true);</code>

<code>if($argc == 2) {
$Dispatcher= new Dispatcher();
$Dispatcher->dispatch($argv[1]);
}

Next, add your cron function to the controller you will be using for the cron. You must add a few security lines to the function call within the controller. This will prevent someone from calling your cron from a URL. It will also prevent the view from being called when the cron is running, since the view is not needed.

function cron_function(){</code>

<code>// Check the action is being invoked by the cron dispatcher (security)
if (!defined('CRON_DISPATCHER')) { $this->redirect('/'); exit(); }
$this->layout = null; // turn off the layout</code>

<code>// do something (place your cron action here)</code>

<code>exit;</code>

<code>}

Now the final and most critical part. You must make sure debug, in app/core/config.php is set to 0. If not, you will get the segfault.

A note About Auth

In my application, I am using the Auth component. This is a great way to provide security to your application. It just so happens that I have Auth turned on globally for my app. This means that you can’t even view the home page without logging in. So I have app/app_controller.php set up with var $components = array('Auth'); to accomplish this. So when I try to run my cron from the command line, it denies access to the cron_function and reroutes me to the login.

To get around auth, simply add the beforeFilter to the controller the cron function is called from.

function beforeFilter(){
// turn off auth for cron jobs
$this->Auth->allow('cron_function');
}

This is a much more effective solution than using wget for multiple reasons. Two of the biggest being 1) if you are using security, you can override it internally without the risk. 2) With wget you run into issues if the cron you are running takes awhile to process as it could cause a timeout.

There you have it. A working cron_dispatcher for CakePHP 1.2.

Simple onLoad Function without the BODY tag.

While trying to find a solution to creating an onLoad function without having to specify the onLoad() function inside the body tag, I found a simple method to accomplish the same thing. This works well with CakePHP so you do not have to load a different template for the specific controller/view you are trying to add it to. It also prevents you from having to put the onLoad in every page or writing a silly IF statement to accommodate for the one location you do need it.

 <script>
function init(){
alert(“Testing”);
}

window.onload=function(){
init();
}
</script>

Put this inside the <body> </body> and it creates an onLoad function as if it had been added to the body tag. Simply replace the guts of the init() function with what you want it to do and you are done.

CakePHP Cron Dispatcher

While working with my team to develop a method whereby we can create cron jobs that inherit core functionality, database connections, etc. from the cakePHP framework, I found a blog post that discusses an elegant method to do just that. It is referred to as the “cron dispatcher”.  It turns out, this method is very simple and works quite nicely. It provides access to all of the functions within the code set.

Here is the step-by-step set of instructions for implementation:

1. Make a copy of /app/webroot/index.php and rename it to /app/cron_dispatcher.php. (Note: You can actualy place this anywhere, you just need to make sure that the ROOT and APP_DIR  constants are defined correctly.)
2. Open the newly renamed file and scroll down to the line: require CORE_PATH.'cake'.DS.'bootstrap.php'; and replace everything below it with:

// Dispatch the controller action given to it
// eg php cron_dispatcher.php /controller/action
define('CRON_DISPATCHER',true);
if($argc == 2) {
$Dispatcher= new Dispatcher();
$Dispatcher->dispatch($argv[1]);

(Note: You may opt to leave in the debug code [ if (DEBUG) {echo "<!-- " . round(getMicrotime() - $TIME_START, 4) . "s -->";}  ])

3. Now you are ready to call your cakePHP driven APP via a cron job like: php cron_dispatcher.php /mailouts/send

The content here is taken liberally from the cakePHP bakery post that can be found here.

Browser Scrollbar Page Shifting?

When designing a website while doing some volunteer work for a historical museum, I discovered that pages with less content wouldn’t bring up a scrollbar in the browser. This would cause the header, along with the entire page, to shift to the right. This gave the website the appearance that the CSS was coded incorrectly for those pages. When clicking back and forth between the pages, they had a 7px page shift to the left and back to the right making the pages “dance”, and it was highly noticeable.

While reviewing a few forums and blogs, I found comment after comment indicating the only fix was to add code that would force the page to extend just pass the fold to bring up the scrollbar. Other options included adding additional code to every page to make sure it would extend pass the fold. The problem with these options is they are not the “proper” way to do it. They are just “hacks” to get the desired outcome. When writing code, be it HTML or otherwise, I prefer to do things the proper way, and not just hack things together.

While digging a little further, I found a forum on webproworld.com with a post from DrTandem1 (drtandem.com) that provided the correct solution and it works like a charm:

html { min-height: 100%; margin-bottom: 1px; }

There is no extra code required in any pages within the site. It automatically extends the HTML tag (which is contained in every properly formated webpage) beyond the fold by 1px thus forcing the scrollbar. The nice thing about this solution is you don’t have to guess where the fold will be on various screen resolutions and your pages don’t get cluttered with garbage code to hack a solution.