Convert ASP to PHP with ASPA

If you’ve been around PHP a little, you’ve probably run into asp2php before, an app which attempts to convert ASP 3.0 code (VBScript mainly) to PHP automatically. I’ve mentioned and joked about this stuff before but recently been playing with something that looks serious.

While asp2php is a brave project and I have no wish to knock it, from past experiments found it unconvincing. Yes it delivers something resembling PHP but the real issue is what still remains TODO – basically an 80/20 thing. And while that is to be expected with any tool of this kind, the problem is there’s no way to control what get’s generated, other than by modifying the C source.

A while back, while looking for tools able to parse PHP1), run into ASPA, developed by Anakreon Mejdi: “ASPA attempts to automate the process of translating asp pages into php. The supported languages are JScript and VbScript.”.

ASPA is written in Java and builds on top of ANTLR, a mature, Open Source parser generator framework. What makes it special, IMO, is it’s fairly easy to control the output PHP by virtue of XML “mapping” files which are loaded at runtime (during conversion). You’ll find this describe a little here (PDF).

After playing with ASPA for a while, here’s a few notes / thoughts.

- Expect to have to spend time installing – you’ve got usual Java pain to deal with here, including ant – I needed to rebuild ASPA. Here I’m only going to give hints based “it worked for me” on WinXP.

- I used version 1.2 of ASPA. It lacks various dependencies namely antlr, log4j, dom4j and commons-collections, which Anakreon describes. A quick way to get them all is to download the 1.1 of ASPA which has them all in the ./lib subdirectory – just copy across the v1.2 ./lib subdirectory. What’s missing though is the Jaxen .jar, which also needs to go into the ./lib directory – you can get a built version from here

- If you look in the ./etc/log4j.properties file you’ll find it identifies a log file like log4j.appender.FILE.File=logs/log.txt – this is relative to the base ASPA directory and you’ll need to make sure this file / directory exists. Once you have ASPA built with ant, the aspa-1.2/build/classes/gr/omadak/leviathan/asp/log4j.properties file can be used to control how much logging happens every time you convert

- With ASPA built, the XML translation files can be found at aspa-1.2/build/classes/gr/omadak/leviathan/asp/objects. I believe (not tested) you can create new XML files here with filenames corresponding to the ASP / ActiveX objects you want to convert to PHP as described here (PDF). While what ASPA does here is a great start, it strikes me that to really empower users, something like Rhino or Jython is needed, combined with access to parse time information from ASPA.

- To put it to the test, I picked the first ASP Blogging app, published under GPL, that I could find via Google – Ublog. For comparison, I’ve dumped the results here along with the ASPA log file. Haven’t modified that or done any significant tuning of the output. Basically it’s at the 80% mark I’d guess. Some files it failed to convert, as explained in the log. Other things like calls to require name a .asp which will have been converted to a .php file so no longer exist. Meanwhile some thought need to go into XSS implications of all those print statements. And thought is needed as to whether APIs have been translated in the smartest manner e.g. the DB calls which ASPA primes for use with John Lim’s ADODB but look to be about 90% there API-wise. But the donkey work has been done – the last 20% is doable and by pushing at much as possible into the XML translation files, it may be possible to get down to 5% or lower.

Overall ASPA is looks to be very much at the “it works” stage but now needs to aim for “it’s accessible”. In particlar the ability to modify the output PHP fairly easy marks it as significant and there’s potentially a serious tool for “the masses” here, if an easy to install binary distribution can be put to together and, better yet, if a GUI to be stuck on top to help manage conversions (e.g. the XML translation files are “global” to ASPA but their use would likely be application specific). In it’s current form I guess this would be a big help to ASP developers interested in having PHP versions of their code. There’s also opportunity to inject some kind of PHP framework into the mix, as the target output API – one that’s primed to support common ASP idioms. Perhaps those droves might happen one day after all ;)

Side note: What led me to ASPA was looking for tools able to parse PHP scripts, for doing something like a PHP lint. Interestingly there are hardly any that really “understand” PHP and when they do, it seems to lead to people wanting to keep the results closed source. Aside from hand coded stuff like phpxref, two exceptions are scintilla (which you can tap into in Python via SilverCity or wx.STC but the tokens are very general) and spident (which was more Java than I cared to submit myself to).

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://www.phpism.net Maarten Manders

    Doesn’t Zend Studio’s code analyzer have basic lint/splint features?

    Of course that one’s closed source as well. It sounds like a nice project to build an open source php code analyzer. As far as I can see it can’t be done in PHP though because it seems that there aren’t any BNF parser generators around for PHP to start with. The remaining functionality should be hacked together rather quickly.

  • http://www.phppatterns.com HarryF

    Doesn’t Zend Studio’s code analyzer have basic lint/splint features?

    No idea there.

    As far as I can see it can’t be done in PHP though because it seems that there aren’t any BNF parser generators around for PHP to start with. The remaining functionality should be hacked together rather quickly.

    Probably the easiest path there is the tokenizer + parsekit extensions. That said, did run into this once; http://metaphp.sourceforge.net/;

    A generator for context-free-grammar parsers, written in and for PHP. (This is way cool, dudes.)

    One other option – been looking at the Gold Parser – one of the downloads is a complete reimplementation in Python – doesnt look like too massive an effort.

  • http://www.phpism.net Maarten Manders

    Thanks for the hints! Looks like we just need find someone willing to put it together.. ;-)

    Here’s some sample output of the ZS-embedded code analyzer:


    Debug Strict (PHP 5): var: Deprecated. Please use the public/private/protected modifiers (line 68)
    Variable $mysql appears only once (line 80)
    The value of variable $mysql was never used (line 80)
    Global variable $strMode was used before it was defined (line 2)
    Assignment in condition (line 200)
    etc...

    I never use it, however, because I don’t find it to be very useful. Making it a little bit smarter and able to scan whole projects would change my opinion.

  • http://www.thinkcolony.com Richard Conyard

    Looking at the approach I don’t think we’re going to get much automatic conversion except for the easiest of scripts, and certainly nothing going down the COM+ / DNA route.

  • http://www.phppatterns.com HarryF

    Looking at the approach I don’t think we’re going to get much automatic conversion except for the easiest of scripts,

    By default agreed. What’s interesting about ASPA in the XML transformation files. Perhaps a better explaination of how these work would be to say they’re something like XSLT stylesheets.

    Initially ASPA gives you a default set of “transformation stylesheets” which work for basic ASP syntax plus well known objects like ADO DB and translate to obvious PHP equivalents. For code embedded in HTML, that might at least be a time saver. To maintain parallel versions of a code base, as Joel does, that might already be a good enough start.

    From there you need to adjust / extend those sheets to match the specific project you’re converting. For example if the ASP code uses the AspEmail component, you might translate that phpmailer but adding the appropriate XML transformation file.

    Where this seems a bit limited right now is the mappings seem to be “one to one” – it’s not as flexible as something like XSLT in terms of how you relate elements of the source ASP to output PHP. That may be where something like jython could help, for embedding in the XML mapping files and providing greater control.

    and certainly nothing going down the COM+ / DNA route.

    There I can’t really comment. There is this stuff but I guess if you’re using things like COM+ in an ASP application, you’d be more interested in moving to .NET.

    Would imagine ASPA is more interesting to typical database driven applications that run on shared hosts.

  • http://www.thinkcolony.com Richard Conyard

    .Net …. shudder, I’ll think I’ll stick with scripted solutions, XML and XSLT. Whoever thought it was a good idea over at Redmond to apply the same programming techniques of application interface building to web application interface building needs to be shot.

    Thanks for your feedback though, I’ll check those out.

  • Pingback: kbglob - tecnologia para geeks, no para tu mamá » Convertir ASP en PHP

  • ava

    response.redirect

  • Anonymous
  • Brett

    http://storm.cs.unipi.gr/~anakreon/aspa.html has been taken offline :(. What else is their besides asp2php now?

  • Anonymous

    <?php
    header(“Content-type: text/xml”);
    require(“db.php”);

    function parseToXML($htmlStr)
    {
    $xmlStr=str_replace(‘<’,”,’>’,$xmlStr);
    $xmlStr=str_replace(‘”‘,’”‘,$xmlStr);
    $xmlStr=str_replace(“‘”,”’,$xmlStr);
    $xmlStr=str_replace(“&”,’&’,$xmlStr);
    return $xmlStr;
    }

    // Get parameters from URL
    $center_lat = $_GET["lat"];
    $center_lng = $_GET["lng"];
    $radius = $_GET["radius"];

    // Opens a connection to a MySQL server
    $connection=mysql_connect (localhost, $username, $password);
    if (!$connection) {
    die(‘Not connected : ‘ . mysql_error());
    }

    // Set the active MySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
    die (‘Can’t use db : ‘ . mysql_error());
    }

    // Select all the rows in the markers table
    $query = sprintf(“SELECT address, name, lat, lng, ( 3959 * acos( cos( radians(‘%s’) ) * cos( radians( lat ) ) * cos( radians( lng ) – radians(‘%s’) ) + sin( radians(‘%s’) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < ‘%s’ ORDER BY distance LIMIT 0 , 20″,
    mysql_real_escape_string($center_lat),
    mysql_real_escape_string($center_lng),
    mysql_real_escape_string($center_lat),
    mysql_real_escape_string($radius));

    $result = mysql_query($query);
    if (!$result) {
    die(‘Invalid query: ‘ . mysql_error());
    }

    // Start XML file, echo parent node
    echo “n”;
    // Iterate through the rows, printing XML nodes for each
    while ($row = @mysql_fetch_assoc($result)){
    // ADD TO XML DOCUMENT NODE
    echo ‘ ‘;
    echo ‘name=”‘ . parseToXML($row['name']) . ‘” ‘;
    echo ‘address=”‘ . parseToXML($row['address']) . ‘” ‘;
    echo ‘lat=”‘ . $row['lat'] . ‘” ‘;
    echo ‘lng=”‘ . $row['lng'] . ‘” ‘;
    echo ‘distance=”‘ . $row['distance'] . ‘” ‘;
    echo “/>n”;
    }

    // End XML file
    echo “n”;

    ?>

  • dflock

    ASPA is now available here, if anyone wants it:

    http://rainbow.cs.unipi.gr/projects/aspa

    It hasn’t been updated in quite some time though.