PHP class autoloader

Filed in Development | General Leave a comment

I have been working on a version of the PHP class autoloader. I have a config file that sets up the path information for the program. Generally you would want to include the config and the includes file in the entry point of the program. Continue Reading

PHP, MySQL, and Apache on Windows 7

Filed in Dev Tools Leave a comment

I started my new position with McAfee last week and got a brand new Windows 7 laptop. To get my development environment up and running I had to install PHP, MySQL, and Apache. I ran into a problem with the mysql extension for PHP. Continue Reading

,

jqGrid

Filed in Development Leave a comment

jqGrid is an excellent data grid plugin for jQuery which I wanted to use in my TV Schedule application. It went very smoothly except for the order in which the columns are displayed. jqGrid will display the columns in the order that it receives them. Continue Reading

, ,

Apache, SSL, Red Hat 4

Filed in Development Leave a comment

A new requirement came down for the Solo Tech application the other day. We need to serve it using SSL and in addition the SOAP calls to the API need to use SSL as well. There was a lot of hoop jumping in order to get this working. I had a previous install of Apache using DSO and I was hoping to load mod_ssl dynamically along with my other modules. However I could not get apxs to create the mod_ssl module. So I ended up recompiling Apache and PHP from scratch.

First I installed the latest version of openSSl from here.
(remember to do installs as root)

# gzip -d openssl.tar.gz
# tar -xvf openssl.tar
# cd openssl
# ./configure -fPIC os/compiler:gcc
# make
# make test
# make install

Then compile and install Apache to use ssl, php, rewrite and so (shared objects). Unzip and untar as above and switch into the new directory.

# ./configure --enable-ssl=shared --with-ssl=/usr/local/ssl/ --enable-rewrite=shared --enable-setenvif --enable-so
# make
# make install

I then created a key and certificate by doing the following and storing them in apache2/conf/certs

The last line creates a self signed certificate.

# openssl genrsa -out hostname.key 1024
# openssl req -new -key hostname.key -out hostname.csr
# openssl x509 -req -days 365 -in hostname.csr -signkey hostname.key -out hostname.crt

Open up apache2/conf/httpd.conf and uncomment the following line:

Include conf/extra/httpd-ssl.conf

Then open up apache2/conf/extra/httpd-ssl.conf and un-comment and set the paths to the certificate and key you just created.

SSLCertificateFile "/usr/local/apache2/conf/certs/hostname.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/certs/hostname.key"

Restart Apache. (I have apachectl in my path)

# apachectl -k restart

If everything is cool then you should be able to request a page from your server through https. The browser will complain about the certificate. You can accept the certificate and then you should be communicating through SSL.

The next step is setting up PHP. So stop Apache.

# apachectl -k stop

Repeat the unzip and untar process from above on the downloaded PHP archive and proceed with configuration and installation.

#  ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql --with-curl --enable-soap --with-openssl=/usr/local/ssl
# make
# make test
# make install

Your path to openssl may differ to just verify its location with:

# whereis openssl

Move the PHP ini file to its final location:

# cp php.ini-dist /usr/local/lib/php.ini

Again this may differ on your system.
Open apache2/conf/httpd.conf again and add the following lines at the end.

          SetHandler application/x-httpd-php

Make sure the php module is being loaded by the conf file. You should see this:

LoadModule ssl_module modules/mod_ssl.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module        modules/libphp5.so

Restart the server.

For the SOAP stuff I set this in my app config.php file.

define('SOAP_URL', 'https:<URL>?wsdl');
define('SOAP_ORGNAME', 'ORG1002');
define('SOAP_API_VERSION', '3.2');
define('SOAP_WAIT', '0');
define('SSL_CERT_PATH', '/usr/local/apache2/certs/triad.pem');

And then when I instantiate the SOAP client I do this:

 
        try
        {
            $this->client = new SoapClient(
	            SOAP_URL,
		            array (
		            "trace"=>true
		            , "exceptions"=>true
		            , 'features'=>SOAP_SINGLE_ELEMENT_ARRAYS
					, 'local_cert' => SSL_CERT_PATH
		            )
            );
        }
        catch(SoapFault $f)
        {
            throw new Exception($f->getMessage());
        }

, , ,

Installing Apache and PHP on Red Hat 5

Filed in Development Leave a comment

As part of building up my dev environment at ETI I had to install Apache2.2 and PHP5 on Red Hat 5. Here are my install notes.

Do this as root.

First download the source for Apache and PHP. This command will get the file from the URL and put it at your present location.

#wget

Unzip and untar the Apache Archive

#gzip -d  
#tar xvf

Switch into the apache directory created from untarring the file.

Configure Apache with Dynamic Object support so that you add new modules easily at a later time.

#./configure ./configure --enable-so
#make
#make install

Done. Now you can test by starting and stopping the server

#/usr/local/apache2/bin/apachectl -k [start | stop]

If there are no errors when running it you should be able to browse to the test page:

http://<server ip>

PHP

Repeat the unzip and untar instructions above for the PHP archive.
Configure PHP with options you need. In my case I need mysql, cURL and SOAP. Notice the first option with the path to apxs. This is actually going to compile the PHP module and move it to Apache’s module directory. This is also used later when want to build and install new Apache modules.

#./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql --with-curl --enable-soap
#make
#make install

Then you want to move the distributed PHP config file to the final location. In my case it was this:

#cp php.ini-dist /usr/local/lib/php.ini

You also need to edit the Apache config file to tell it to process PHP files. I put this at the end of my httpd.conf file.

    SetHandler application/x-httpd-php

Finally I wanted to install mod_rewrite. In the directory that contains the source files look for the modules directory that contains the module you need. In my case it is mappers. Then I ran this command which compiles and installs the mod_rewrite module.

#/usr/local/apache2/bin/apxs -i -a -c mod_rewrite.c

Restart Apache and off you go.

Useful Links
http://httpd.apache.org/docs/2.0/programs/apxs.html
http://httpd.apache.org/docs/2.2/dso.html
http://www.php.net/manual/en/install.unix.apache2.php

, ,

Class Loader

Filed in Lab Leave a comment

I needed a class loader for my little framework as I dont want to include all the files in on every request which is what listing them in a long list of includes will do. So I checked out the PHP.net site which has this code sample. I used it as a starting point for this:

function __load_class($classname, $dir)
{
	$file = $dir . '/' . $classname . '.php';
	if (file_exists($file))
	{
		require_once ($file);
		return true;
	}
	return false;
}
 
function __autoload($classname)
{
	$inc[] = '../fwork';
	$inc[] = '../fwork/controllers';
	$inc[] = '../fwork/models';
	$inc[] = '../app';
	$inc[] = '../app/controllers';
	$inc[] = '../app/models';
 
	foreach ($inc as $dir)
	{
		if (__load_class($classname, $dir))
		{
			if(DEBUG_LEVEL == 2)
			{
				echo 'Loading class(' . $classname . ")<br>";
			}
			return;
		}
	}
}

I need to do some more testing and abstraction but its a good start for what I need.

Autocomplete field with jQuery and PHP

Filed in Lab Leave a comment

For the recipe manager project I need to allow the entering of ingredients that the system does not know about. I decided to use an auto complete field to allow users to easily see what was already in the system. If the ingredient does not exist then the one they entered will added to the system.

To test this out I wrote a quick little program that assigns employees to an assigned employees table. The autocomplete does a look up in the employees table. If the entered employee is not found then the new employee is added to the employees table and then assigned.

I used the following jQuery stuff:

jQuery1.2.6
Autocomplete – jQuery plugin 1.0.2 (copyright (c) 2007 Dylan Verheul, Dan G. Switzer, Anjesh Tuladhar, Jörn Zaefferer)

Main interface: index.php

<?php
 
	include('config.php');
	$employees = array();
	$assigned_employees = array();
 
 
	$query = "select * from employees";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
	{
		$employees[$line['id']] = $line['first_name'];
	}	
 
	$query = "select * from assigned_employees";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
	{
		$assigned_employees[$line['id']] = $line['first_name'];
	}
 
?>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<script src="../javascript/jquery-latest.js"></script>
 	<link rel="stylesheet" href="../javascript/jquery.autocomplete.css" type="text/css" />
	<script type="text/javascript" src="../javascript/jquery.autocomplete.js"></script>
	<script>
		$(document).ready(function()
		{
			// var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
			$("#first_name").autocomplete('autocomplete.php');
 
			$("#first_name").result(function() 
			{
				console.log(arguments);
			}
			);
		}
		);
  </script>
 
</head>
<body>
	<form action="insert.php" method="post">
		Assigned an employee: <input id="first_name" name="first_name" />
		<input type="submit" value="Submit" />
	</form>
 
	<h3>Employees</h3>
<?php
	foreach($employees as $employee)
	{
		echo "$employee <br>";
	}
?>	
 
<h3>Assigned Employees</h3>
<?php
	foreach($assigned_employees as $employee)
	{
		echo "$employee <br>";
	}
?>
 
<div>--------------------------------------------</div>
<a href="clear.php">Remove all assignments</a>
 
</body>
</html>

Auto Complete Function: autocomplete.php

<?php
include('config.php');
 
$q = $_GET['q'];
 
// Performing SQL query
$query = "SELECT id, first_name FROM employees";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
	if (substr($line['first_name'], 0, strlen($q)) == $q) 
	{
		echo $line['first_name'] .'|'. $line['id'] . "\n";
	}
}
 
?>

Insert Function: insert.php

<?php
include('config.php');
 
//get the first_name from the form
$first_name = $_POST['first_name'];
 
// see if we have it in the database. If so then grab the ID
$query = "SELECT id FROM employees WHERE first_name = '$first_name'";
 
//print "$query<br>";
 
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
if(!$row = mysql_fetch_assoc($result))
{
	// If not then insert it into the employees  table. Then get the ID of the new employee from the employees table.
	$query = "INSERT INTO employees(first_name) VALUES('$first_name')";
	//print "$query<br>";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
	$id = mysql_insert_id();
}
else
{
	$id = $row['id'];
}
 
// Now insert into the ingredient list table.
 
$query = "INSERT INTO assigned_employees(id, first_name) VALUES($id, '$first_name')";
//print "$query<br>";
 
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
header( 'Location: http://localhost/lab/autocomplete/index.php' ) ;
 
?>

Clear Function: clear.php

<?php
include('config.php');
 
$query = "delete FROM assigned_employees";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
header( 'Location: http://localhost/lab/autocomplete/index.php' );
?>

Database Schema

CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET latin1 */;
 
USE `test`;
 
/*Table structure for table `assigned_employees` */
 
DROP TABLE IF EXISTS `assigned_employees`;
 
CREATE TABLE `assigned_employees` (
  `id` INT(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(64) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
 
/*Data for the table `assigned_employees` */
 
INSERT  INTO `assigned_employees`(`id`,`first_name`) VALUES (0000000002,'sam');
 
/*Table structure for table `employees` */
 
DROP TABLE IF EXISTS `employees`;
 
CREATE TABLE `employees` (
  `id` INT(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
 
/*Data for the table `employees` */
 
INSERT  INTO `employees`(`id`,`first_name`) VALUES (0000000001,'steve'),(0000000002,'sam'),(0000000003,'heather'),(0000000004,'heath'),(0000000005,'shamus'),(0000000006,'jennifer'),(0000000007,'geronimo'),(0000000008,'gerald');
 
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

,

TOP