Saurabh Chowdhury on programming and life.
in

A PHP4 version of PHPLiveX with ExternalJS property

The beauty of PHPLiveX is its astute simplicity that makes it among the best AJAX libraries for PHP. The dev team has now shifted to focus to PHP5, so ver. 2.3 is the last version for PHP4. Although a bit late, it was realized that for the sae of brevity and maintaining clean & tidy code support for outputting the javascript to a file rather than inline was made. Only a minor version too late for developers working on PHP4 code. So although folks using PHPLiveX 2.4 and above get to enjoy a more readable HTML output, the ExternalJS property is not supported by any official build for PHP4. But adding it is surprisingly easy!

I have been working to AJAXify an old PHP4 app on client insistence, this app is deployed on PHP4 and hence cannot use ExternalJS property. And I like my HTML to tidy up. So this is what I did to add ExternalJS to PHPLiveX 2.3 that was used in the project.

Start by adding a new property ExternalJS to the class. And then modify the Run method to something like this:

function Run(){
    $html = $this->CreateJS();
    for($i=0;$i<count($this->FList);$i++){
        $html .= $this->CreateFunction($this->FList[$i]);
    }
    if(empty($this->ExternalJS)){
        echo "<script type='text/javascript' language='javascript'>" . $html . "</script>";
    }
    else{
        if(!file_exists($this->ExternalJS)){
            $fp = fopen($this->ExternalJS, "w");
            if($fp != false){
                fwrite($fp, $html);
                fclose($fp);
            }
            else
                echo "<script type='text/javascript' language='javascript'>" . $html . "</script>";


        }
        echo "<script type='text/javascript' src='{$this->ExternalJS}'></script>";
        }
}

That's it! You can get rid of inline embedding of the PHPLiveX javascript. Similar to ver. 2.4 and above the call to Run() must not be inside the <script></script> tags. You can download a modded ver. 2.3 file with ExternalJS property from here.

You can further enhance this code a little more, like - Restrict the ExternalJS property to not use paths or you could restrict it to a bunch of desired paths.

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback url for this post:
http://darkknight.thinkers-inc.com/blogs/chad/trackback.ashx?PostID=3620

» Comments

  1. Some suggestions

    - Make the script run as a background job to generate the js file

    - Store the md5 sum of the file when you generate it and make sure that the match the md5 sum before you load the file (to make sure the js file was not altered)

    Raj Shekhar — January 13, 2008 10:22 PM
  2. - There is no need to run the script in background, the file will be created for the 1st time a request for the php page is made it is a one time operation and is not memory and/or processor intensive.

    - I wanted to talk about supporting a feature that I consider to be of great value for a PHP4 dev. looking to use PHPLiveX. Therefore, I kept my implementation to mimic the PHP 5 version.

    PHPLiveX is no doubt the easiest of all available frameworks and its security may appear dubious to you, but it is not.

    I would be very interested to see you elaborate a little more about the possible attack vectors you foresee in a scenarion where a MD5 checksum is not been verified.

    Saurabh — January 15, 2008 4:29 AM

» Leave a Comment

(required) 
(optional)
(required) 

Submit