Search code examples
phpvbscriptasp-classiccode-translation

Translation of some Classic ASP (vbScript) into PHP


I'm trying to translate a block of Classic ASP (vbScript) into PHP. I've made an honest attempt, but my translation doesn't appear to be correct. Could anybody help me out?

First, the vbScript Code:

szTemp = Request.ServerVariables("URL")
aryCrumbs = Split(szTemp,"/")
iMax = Ubound(aryCrumbs)

szCrumbPath = "http://" & Request.ServerVariables("SERVER_NAME")
szTemp = "<a href=""" & szCrumbPath & """ style=""color:#600;"">Home</a>"

For i = 0 To iMax -2
szCrumbPath = szCrumbPath & "/" & aryCrumbs(i)
szTemp = szTemp & "  &gt; <a href=""" & szCrumbPath & """ style=""color:#600;"">" & CleanUp(aryCrumbs(i)) & "</a>"    'Note: The &gt; in this line refers to a server request variable. 
Next

GetCrumbsArticleCategoryLevel = szTemp & "<span style=""color:#600;"">  &gt; " & CleanUp(aryCrumbs(i)) & "</span>"

And here's my attempt at translation into PHP:

$szTemp = $_SERVER["PATH_INFO"];    // Get current URL path (doesn't include www)
$aryCrumbs = explode("/",$szTemp);  // Split path name by slashes into an array
$iMax = count($aryCrumbs);          // Count array.
$szCrumbPath = "http://". $_SERVER["HTTP_HOST"];      // Add on http to web server name
$szTemp = '<a href="' . $szCrumbPath . '" style=&quot;color:#600;&quot;>Home</a>'; 

for ($i=0; $i<=($iMax-2); $i++) {

$szCrumbPath = $szCrumbPath . "/" . $aryCrumbs[$i];
$szTemp = $szTemp ." &gt; <a href=&quot;" . $szCrumbPath . "&quot; style=&quot;color:#600;&quot;". ">" . CleanUp($aryCrumbs[$i]) . "</a>";
}

$GetCrumbsArticleCategoryLevel = $szTemp."<span style=&quot;color:#600;&quot;>&gt; ".CleanUp($aryCrumbs[$i])."</span>";

Solution

  • In PHP in order to get a " you need to delimit it with \, so " becomes \"

    Example:

    $szTemp = "<a href=\"" . $szCrumbPath . "\" style=\"color: #600\">Home</a>";
    

    Translation

    I assumed that you were using Request.ServerVariables("gt"), which in PHP the equivelent is $_SERVER, otherwise for Request.Form use $_POST or $_GET for Request.QueryString.

    Make sure if the user can change the values that you html encode using htmlspecialchars() function, otherwise you leave things open for a Cross Site Scripting attack [XSS]

    $szTemp = $_SERVER['REQUEST_URI'];
    $aryCrumbs = explode("/", $szTemp);
    $iMax = count($aryCrumbs);
    
    $szCrumbPath = "http://". $_SERVER["HTTP_HOST"];
    $szTemp = "<a href=\"" . $szCrumbPath . "\" style=\"color: #600\">Home</a>";
    
    for ($i=0; $i <= ($iMax - 2); $i++) {
        $szCrumbPath = $szCrumbPath . "/" . $aryCrumbs[$i];
        $szTemp = $szTemp . " &gt; <a href=\"" . $szCrumbPath . "\" style=\"color: #600;\">" . CleanUp($aryCrumbs[$i]) . "</a>"; //The htmlspecialchars prevents a XSS attack
    }
    $GetCrumbsArticleCategoryLevel = $szTemp . "<span style=\"color:#600\"> &gt; " . CleanUp($aryCrumbs[$i]) . "</span>";