<?php
################################################################################
# config
################################################################################

$base "/usr/www/users/angeles/ugh/graphviz";
$baseurl "http://urlgreyhot.com/graphviz";
$dotbase "/usr/home/angeles/bin";
$self $PHP_SELF;

// header and footer
function page_header() {
  echo 
'<html>
  <style>
  body {background: #fff; font-family: Verdana;}
  fieldset {border: 1px dotted #ccc; padding: 10px;}
  fieldset legend {color: #999; font-size: 1.5eml font-weight: bold;}
  fieldset label {font-weight: bold;}
  </style>
  <body>'
;
}

function 
page_footer() {
  echo 
"
  <p><small>
  Sitemap generator | 
  <a href=\"http://urlgreyhot.com/graphviz\">urlgreyhot.com/graphviz</a> |
  <a href=\"http://urlgreyhot.com/contact.php\">Feedback</a>
  <small></p>
  </body></html>"
;
}

################################################################################
# do stuff
################################################################################

page_header();
  global 
$baseurl;

// start doing stuff
// execute modules
if ($q) {
  
$mod $q;
  
module_invoke($mod$a1$a2$a3$a4);
} else {
// show the submit form
    
if(!$submit=="Submit"){
        echo 
"
        <h1>GraphViz sitemap generator</h1>
        <p><small>This web application accepts uploaded tab delimmited text files and converts them into clickable site maps using the GraphViz application. To get started you will have to <a href=\"$baseurl/?q=help&amp;a1=fileprep\" target=\"_blank\">prepare your file.</a> If you're interested in seeing the output of this application, take a look at the <a href=\"http://urlgreyhot.com/graphviz/?q=view&a1=urlgreyhot_demo\">demo diagram</a>.</small></p>
        <form enctype=\"multipart/form-data\" action=\"$PHP_SELF\" method=\"POST\">
          <fieldset>
            <legend>1. Upload your file (required)</legend>
            <label for=\"fname\">Name your diagram</label><br />
            <small>This should be one word with no spaces. Underscores and hyphens are ok. Used to identify your diagram.</small><br />
          <input type=\"text\" name=\"fname\" size=\"25\" /><br /><br />
            <label for=\"upload\">Upload tab delimmited description file</label><br />
            <small>This is your file describing nodes in the tree. <a href=\"$baseurl/?q=help&amp;a1=fileprep\" target=\"_blank\">How to prepare your file.</a></small><br />
            <input type=\"file\" size=\"25\" name =\"fupload\" /><br />
          </fieldset>
          <br /><br />
          <fieldset>
            <legend>2. Set graph attributes (optional)</legend>
            <label for=\"defaulturl\">The default (root) URL for your site</label><br />
            <small>This URL is used for mis-clicks in the image map.</small><br />
          <input type=\"text\" name=\"defaulturl\" size=\"25\" /><br /><br />
          </fieldset>
          <br /><br />
          <fieldset>
            <legend>3. Click and start the automagic</legend>
            <input type=\"submit\" name=\"submit\" value=\"Submit\" />
          </fieldset>
        </form>
        "
;
    } else {
  
// process submitted form
        
        // while we have uploaded file

        
foreach( $HTTP_POST_FILES as $file_name => $file_array ) {
          
/* debug
          print "path: ".$file_array['tmp_name']."<br>\n";
          print "name: ".$file_array['name']."<br>\n";
          print "type: ".$file_array['type']."<br>\n";
          print "size: ".$file_array['size']."<br>\n";
          */
          
          // process file
          
if ( 
            (
is_uploaded_file$file_array['tmp_name'] )) && 
            (
$file_array['type'] == "text/plain" )
            )
            {
            
// move uploaded file to new location
            
$file_dir "/usr/www/users/angeles/ugh/graphviz/input";
            
move_uploaded_file$file_array['tmp_name'], "$file_dir/$file_name") or die ("Couldn't copy");
            
chmod ("$file_dir/$file_name"0775);
            
copy ("$file_dir/$file_name""$file_dir/$fname");
            
chmod ("$file_dir/$fname"0775);
            
$a1 $fname;
            
$a2 $defaulturl;
        
            
// now create the dot file
            
dot_create_dot_file($a1$a2);
        
            
// create image and image map
            
dot_create_images($a1$a2); 

            
// link to image
            
view($a1);
          } else {
              
error("Sorry. Your diagram cannot be processed. Likely cause may be that your file is not in ASCII format");
          } 
// end process file
            
        
// end while we have uploaded file
        
    
// submit form
    
// stop doing stuff
page_footer();

################################################################################
# modules
################################################################################

// invoke module $name with optional arguments:
function module_invoke($name$a1 NULL$a2 NULL$a3 NULL$a4 NULL) {
  
$function $name;
  if (
function_exists($function)) {
    return 
$function($a1$a2$a3$a4);
  } else {
    
error("Sorry. You got here by mistake.");
  }
}

// create dot files in ./input/
function dot_create_dot_file($a1 NULL$a2 NULL

  global 
$base;
  
$infilename "$base/input/$a1";  
  
$outfilename "$base/input/$a1.dot";

  
// read uploaded file
  
$fhin fopen($infilename"r");
    
$file_contents fread($fhinfilesize($infilename));
    
$line explode("\n"$file_contents);
  
fclose($fhin);

  
// convert lines to dot format
  
$i 0;
  
$size sizeof($line) - 1;
  while(
$i <= $size) {
    
# split lines and assign values to fields
    
$dat explode("    "$line[$i]);
    
$datid rtrim($dat[0]);
    
$datparent rtrim($dat[1]);
    (
$dat[2] == '') ? $datlabel rtrim($dat[1]) : $datlabel rtrim($dat[2]);
    
$daturl rtrim($dat[3]);
    
# output the nav links
    
if ($datid != ''){
      
$dotlines .= "  $datid [label=\"$datlabel\", URL=\"$daturl\"];\n";
    }
    if (
$datid != $datparent){
      
$dotlines .= "  $datparent -> $datid;\n";
    }              
    
$i++;
  }
  
  
// prepare dot options
  
$dotfile "digraph G {\n";
  
$dotfile .= "  URL=\"$a2\";\n";
  
$dotfile .= "  fontpath=\"/usr/home/angeles/fonts\";\n";
  
$dotfile .= "  node [shape=\"box\",style=\"filled\",color=\"#dddddd\",fontname=\"verdana bold\",fontsize=\"10\",fontcolor=\"#000000\"];\n";
  
$dotfile .= "  edge [color=\"#cccccc\",arrowhead=\"none\"];\n";
  
$dotfile .= $dotlines;
  
$dotfile .= "}";

  
// write everything to the dot input file
  
$fhout fopen($outfilename'w');
    
fwrite($fhout$dotfile);
  
fclose($fhout);

  
chmod ($infilename0775);
  
chmod ($outfilename0775);
}


// create the dot files in ./output/
function dot_create_images($a1 NULL$a2 NULL)
{
  global 
$base;
  global 
$dotbase;
  
passthru("touch $base/output/$a1.gif");
  
passthru("touch $base/output/$a1.map");
  
passthru("$dotbase/dot -o $base/output/$a1.gif -Tgif $base/input/$a1.dot");
  
passthru("$dotbase/dot -o $base/output/$a1.map -Timap $base/input/$a1.dot");
  
chmod ("$base/output/$a1.gif"0775);
  
chmod ("$base/output/$a1.map"0775);
}

function 
view($a1)
{
  global 
$baseurl;
  echo 
"<h1>$a1</h1>";
  echo 
"<a href=\"$baseurl/output/$a1.map\"><img src=\"$baseurl/output/$a1.gif\" ismap=\"ismap\" ></a><br /><br />";
  echo 
"<small>Uploaded file: <a href=\"$baseurl/input/$a1\">$baseurl/input/$a1</a><br />";
  echo 
"Permalink: <a href=\"$baseurl/?q=view&amp;a1=$a1\">$baseurl/?q=view&amp;a1=$a1</a></small><br />";
}

// help
function help($a1)
{
  if (
$a1 "fileprep")
  {
    echo 
"
<h1>Preparing your upload file</h1>
<p>Upload files must be <b>ASCII/plain text files</b> produced in an ASCII editor (e.g. NotePad, BBEdit, TextEdit) or exported from an application such as MS Excel. Non-ascii files will be rejected.</p>

<p>Your text file should consist of 4 <b>tab delimmited</b> columns:</p>
<ul>
  <li>Unique ID number</li>
  <li>Parent ID number</li>
  <li>Node label</li>
  <li>Node URL</li>
</ul>

<p>The basic concept is that you are building a site map of a hierarchy (polyhierarchies are also supported). Each node can only have one parent. Each parent can have many children, grandchildren, etc.</p>

<p>Your home page should have Unique ID and Parent ID with a value of \"0\".</p>

<p>Here is an example of the text file used for a demo diagram. Spaces between columns are tabs.</p>
<pre>
0    0    urlgreyhot    http://urlgreyhot.com
1    0    contact    http://urlgreyhot.com/contact.php
2    0    blog    http://urlgreyhot.com/drupal/blog
3    0    topics    http://urlgreyhot.com/drupal/taxonomy_html
4    0    about this site    http://urlgreyhot.com/drupal/node/view/630
5    0    search    http://urlgreyhot.com/drupal/search
6    0    bloggregator    http://urlgreyhot.com/drupal/import
7    6    news by source    http://urlgreyhot.com/drupal/import/feeds
8    6    news by topic    http://urlgreyhot.com/drupal/import/bundles
9    6    sources    http://urlgreyhot.com/drupal/import/sources
</pre>

<p>And <a href=\"http://urlgreyhot.com/graphviz/?q=view&a1=urlgreyhot_demo\">here's the diagram this file produces</a>.</p>

<p>[<a href=\"javascript: window.close()\">Close window</a>]
    "
;
  }
}

// error messages
function error($msg) {
  echo 
"<h1>Error</h1><p>$msg</p>";
}

################################################################################
?>