Thursday, February 25, 2010

Eclipse PHP and Remote Editing

Original Post: September 4th, 2008
 
Eclipse PDT (former PHP IDE) is a free alternative to expensive PHP IDEs.  It comes loaded with a lot of features and a large plugin database.   One of the most usefull plugins is the Remote System Explorer (RSE). The Remote System Explorer (RSE) allows you to work remotely over the internet.  For this article, I explain how to mount a remote PHP project over SSH.
Eclipse PDT Installation
The easies way to install Eclipse PDT is to download the latest all-in-one version from http://www.eclipse.org/pdt/.  Once on the page:
  • Click on the Download link.
  • Find the latest release (stable release is recommended).
  • Look for All-in-one option and click on the link corresponding to your platform (Linux, Max, Windows).
  • Once downloaded, Uncompress the file and open the eclipse executable
Installing the Remote System Explorer (RSE)
Once your eclipse platform is open
  • Click on the Help menu.
  • Navigate your way to Software Updates and click on “Find and Install…”.
  • The “Feature Updates” window will open, then Select “Search for new features to install” and click on Next.
  • Check the the “Europa Discovery Site” and click on Finish.
  • On the “Update Site Mirrors” window, select a mirror or just click OK.
  • On the “Search Results” window, expand the “Europa Discovery Site”.
  • Check the “Remote Access and Device Development” box, then click on the “Select Required” button.
  • Click Next.
  • Accept the terms and click on Next.
  • Click on Finish. (wait for the software to download)
  • When the “Verification” window pops up, click on “Install All”.
  • After installation is complete, say “Yes” to restart the eclipse.
Open the Remote System Explorer Perspective
  • Click on the Window menu.
  • Navigate to “Open Perspective” and click on “Other…”.
  • Select “Remote System Explorer” and and click OK.
  • Click on the “Define a connection to remote system” icon.Define a connection to remote system
  • Select the “SSH Only” as the System Type, and click on Next.Select Connection Type
  • Type the Host Name of the SSH Server and click on Finish.Type Host Name
  • Navigate your way to the folder that has your PHP code.  While doing this, Eclipse will ask you for your login credentials. Type your SSH Login and Password and click OK.ssh navigationeclipse ssh login credentials
  • Once you find the folder you are looking for, Right-Click on it, and click on “Create Remote Project”. Depending on the size of the project this could take a while.
  • Now you should be able to open your PHP prospective by clicking on the Window -> Open Prospective -> “Other …” menu.  Then slect PHP from the list and click OK.
This is it, You should now be able to work remotely as if it where on the local machine.
Note:  If your PDT Eclipse doesn’t recognize the project as a PHP Project, then you must manually edit the .project file on the remote server and add the following code:
Between the <buildSpec> tags add:

<buildCommand>
<name>org.eclipse.php.core.PhpIncrementalProjectBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.php.core.ValidationManagerWrapper</name>
<arguments>
</arguments>
</buildCommand>

and between the <nature> Tags add:

<nature>org.eclipse.php.core.PHPNature</nature>

Then Restart Eclipse.

Enjoy.

Protocol Wrapper to parse PHP

Original Post: August 21st, 2008

Why do I need a protocol wrapper to parse a PHP file?

There are some cases in which one wants to pass a dynamic string to a function that only takes a file path.  One of those cases is the SoapServer class in PHP. The SoapServer class takes the $WSDL argument as a local file path.  If you ever needed to have a dynamic WSDL file for different customers you would probably know what I’m talking about.

Creating the Protocol/Wrapper to parse a file using PHP

The trick here is to use the PHP function stream_wrapper_register to define a protocol that parses PHP.
1. create a class handler for the new protocol wrapper. Click on the link to see the code.

PhpFileStream.php

Note that I have added functionality to accept a query string.

2. Create your dynamic file
hello.php



Hello <?=$this->first_name?>!




3. Register your Protocol/Wrapper some where in your code and use it!


<?php

//include the class

include('PhpFileStream.php');

//

//register the wrapper

stream_wrapper_register('phpfile', 'PhpFileStream');

//

//you can now pass the file path with the new protocol wrapper and it will be parsed


echo file_get_contents('phpfile:///./hello.php?first_name=John');



This code will print:


Hello John!


Note that You can pass a URL encoded query string to the file. The values can be accessed from within the file using $this->[key_name] in the target file.
Now all you would have to do If you ever needed to create a dynamic WSDL file was to use the class I provide here and pass the wrapper path this way:



<?php

//...

SoapServer('phpfile:///path/to/dynamic.wsdl?key1=val1&key2=val2');

//...


Now you should be able to include PHP code in you WSDL for you dynamic needs.

Enjoy!

Writing valid HTML or XHTML

Original Post: May 29th, 2008 
 
More than 98% of the web pages out there do not pass the W3C validation. You can verify this by running your favorite site on the W3C validation page. It’s very hard for web developers to keep track of every closing ‘/’ (slash) on single tags or ALT text on images, specially when you are in a project that has a dead line of Yesterday!, sounds familiar?

If you want to write perfect HTML or XHTML code, I recommend installing the HTML Validator Plug-in for Firefox. The HTML Validator Plug-in places it self in on the status bar.

Html Validator

It gives you live validation for any web site you visit. It will also give you tips on how to fix your code when double clicking on the icon.
After installing this plugin, you will be amazed (annoyed) to know how many web sites have broken HTML.

MySQL Hierarchical Data

Original Post: May 21st, 2008

Two common ways of storing hierarchical data in MySQL are the Adjacency List Model and the Nested Set Model. The Adjacency uses a `parent id` to determine de parent of a record. The Nested Set Model uses the `left` and `right` values to determine the parent.


Adjacency List Model

Pros

  • Easy to implement
  • Queries are simpler to write
  • Fast updates
  • Fast Inserts

Cons

  • Retrieving the data is slower since it requires recursion to do it.

Nested Set Model

Pros

  • Faster selects.
  • Indefinite number of levels since with one query.

Cons

  • Queries are a little bit more complicated to write.
  • Updates are slow since multiple rows have to be updated.
  • Inserts are slower since multiple rows have to be updated.


Sample table structure for Adjacency List Model:

CREATE TABLE category(
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
parent INT DEFAULT NULL);

Sample Table Structure for Nested Set Model:

CREATE TABLE nested_category (
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
lft INT NOT NULL,
rgt INT NOT NULL
);

For more information on how to use these models visit the MySQL article by Mike Hillyer at http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

Forward mail to another MTA (overwriting the php mail() function)

Original Post: May 8th, 2008

This post explains how to forward all the mail for an existing site using an MTA other than the site’s server it self. The usual technique to achieve this is to configure the local MTA to forward all the mail to another server. In this document I apply a different technique that overwrites the mail() function call to the `sendmail` command.

The advantage of this technique is that no extra headers are added to the message envelope. Why it is necessary to avoid adding extra headers differs from business to business.

Note: the technique here only applies to web applications running on *Nix machines. Windows machines running PHP applications can provide host and port in the php.ini or the apache configuration to achieve the same results.
Overwriting the sendmail Call

There is a couple of ways the `sendmail` path can be overwritten. The most common one is the php.ini and the least common one the php_admin_value directive in the apache configuration. We are going focus on the later one since this directive gives us the possibility to change the value for a single site on a machine that runs multiple virtual web accounts.
Configuration:

1. AS root create a directory for your `sendmail` replacement
mkdir -p /usr/local/forwarder

2. Download the SMTP Class.
wget http://theclickpro.com/articles/wp-content/uploads/2008/05/smtpclassphp.txt -O /usr/local/forwarder/smtp.class.php

3. Download the sendmail replacement script
wget http://theclickpro.com/articles/wp-content/uploads/2008/05/mailphp.txt -O /usr/local/forwarder/mail.php

4. Open the mail.php file on your favorite editor and change the $HOST, $default_from and
$default_return_path variables to suit your needs.

$HOST = 'mail.example.com'; //mx server to forward to
$default_from = '"Web Master" '; //default from line
$default_return_path = ''; // address to deliver the bounces to


5. Open your Apache virtual host configuration file for your domain and put the following line some where in the <VirtualHost>...</VirtualHost> tags

<VirtualHost>
...
php_admin_value sendmail_path /usr/local/bin/forwarder/mail.ph
... 
</VirtualHost>

6. Restart you Apache server.

Enjoy!

Eclipse PHP and Remote Editing

Original Post: September 4th, 2008
Eclipse PDT (former PHP IDE) is a free alternative to expensive PHP IDEs. It comes loaded with a lot of features and a large plugin database. One of the most usefull plugins is the Remote System Explorer (RSE). The Remote System Explorer (RSE) allows you to work remotely over the internet. For this article, I explain how to mount a remote PHP project over SSH.
Eclipse PDT Installation
The easies way to install Eclipse PDT is to download the latest all-in-one version from http://www.eclipse.org/pdt/. Once on the page:
  • Click on the Download link.
  • Find the latest release (stable release is recommended).
  • Look for All-in-one option and click on the link corresponding to your platform (Linux, Max, Windows).
  • Once downloaded, Uncompress the file and open the eclipse executable
Installing the Remote System Explorer (RSE)
Once your eclipse platform is open
  • Click on the Help menu.
  • Navigate your way to Software Updates and click on “Find and Install…”.
  • The “Feature Updates” window will open, then Select “Search for new features to install” and click on Next.
  • Check the the “Europa Discovery Site” and click on Finish.
  • On the “Update Site Mirrors” window, select a mirror or just click OK.
  • On the “Search Results” window, expand the “Europa Discovery Site”.
  • Check the “Remote Access and Device Development” box, then click on the “Select Required” button.
  • Click Next.
  • Accept the terms and click on Next.
  • Click on Finish. (wait for the software to download)
  • When the “Verification” window pops up, click on “Install All”.
  • After installation is complete, say “Yes” to restart the eclipse.
Open the Remote System Explorer Perspective
Select Connection Type
  • Click on the Window menu.
  • Navigate to “Open Perspective” and click on “Other…”.
  • Select “Remote System Explorer” and and click OK.
  • Click on the “Define a connection to remote system” icon.Define a connection to remote system
  • Select the “SSH Only” as the System Type, and click on Next.
  • Type the Host Name of the SSH Server and click on Finish.Type Host Name
  • Navigate your way to the folder that has your PHP code. While doing this, Eclipse will ask you for your login credentials. Type your SSH Login and Password and click OK.ssh navigationeclipse ssh login credentials
  • Once you find the folder you are looking for, Right-Click on it, and click on “Create Remote Project”. Depending on the size of the project this could take a while.
  • Now you should be able to open your PHP prospective by clicking on the Window -> Open Prospective -> “Other …” menu. Then slect PHP from the list and click OK.
This is it, You should now be able to work remotely as if it where on the local machine.
Note: If your PDT Eclipse doesn’t recognize the project as a PHP Project, then you must manually edit the .project file on the remote server and add the following code:
Between the tags add:

org.eclipse.php.core.PhpIncrementalProjectBuilder




org.eclipse.php.core.ValidationManagerWrapper


and between the Tags add:
org.eclipse.php.core.PHPNature
Then Restart Eclipse.
Enjoy.