<?php
    //-----------------------------------------------------------------------------
    // makegallery.php
    // render a static MakeGallery gallery in this window
    //
    // status : not done!
    //-----------------------------------------------------------------------------
    
     
    function cleanPath($path)
    {
        $path = ereg_replace('/+', '/', $path);
        $patharray = explode('/', dirname($path));
        $path = '';
        $count = count($patharray);
        for ($i = 0; $i < $count; $i++)
        {
            if ($patharray[$i] == '.' )
            {
            }
            else if ($patharray[$i] == '..' )
            {
                $path = dirname($path).'/';
            }
            else
            {
                $path .= $patharray[$i].'/';
            }
        }
         
        $path = preg_replace("/^\.\//", '', $path);
         
        return $path;
    }
     
     
     
    switch ($action):
    case "g_showgallery":
    drawMainTop();
     
    # Clean up path (ie: ./dir/dir2/../../././////./dir/..)
    $dir = cleanPath($_GET['file']);
    $file = $_GET['file'];
     
    # Added security to only allow certain file extensions
    $flag = 0;
    # Allowable extensions
    $extArray = array('htm', 'html', 'jpg', 'JPG');
    foreach($extArray as $ext)
    {
        if (preg_match("/\.${ext}$/", $file))
        {
            $flag = 1;
        }
    }
     
     
    if ($flag == 0 )
    {
        echo "Restricted File Access";
        exit;
    }
     
     
    $extension = substr($file , -3 );
    if (strcasecmp($extension , 'jpg' ) != 0  )
    {
        # Get HTML content
        $html = file_get_contents($file );
         
        # look for href tags and replace with "static.php?=dir/html_file"
        //$link = "index.php?shard=makegallery&amp;action=g_showgallery&amp;file=$dir\\1";
        // use this if you want the image to just open up in a window all its own; not *within* the blur6ex windows.
        $link = "$dir\\1";
         
        $html = preg_replace("/(?<=[fF]=\")(?!\w*\:\/\/\b|\/|\#|[mM][aA][iI][lL][tT][oO]\:)([^\"]*)/", $link, $html);
         
        # look for "src=" tags and prepend "static/$dir/" PATH
        $html = preg_replace("/(?<=[sS][Rr][Cc]=\")([^\"]*)/", "$dir\\1", $html);
         
        # Display file
         
        echo $html;
         
         
    }
    else
    {
         
        print("<html><body><center><img src='$file' alt='image'></center></body></html>");
    }
     
    drawMainBottom();
    break;
     
    endswitch;
   
?>
