<?php

/*

Copyright (c) 2004, Paul Williams
                    and other contributors as mentioned at
                    http://www.pmwiki.org/wiki/Cookbook/GraphViz
		    François Elie <francois@elie.org>

All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright 
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in the 
      documentation and/or other materials provided with the distribution.
    * The names of its contributors may be used to endorse or promote 
      products derived from this software without specific prior written 
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

// allow to describe deirected and undirected graphviz graphs in a pmwiki page
// with support of automatic WikiWord links
// add (:graphviz:), (:graphmap:) and (:graphwiki:) directives 

# directory to put in the .dot and .png files, access by the filesystem
$Dot_Files="/var/www/html/pmwiki/graphs";
# access of the same deirectory by the web
$Dot_Web="http://your.server.org/pmwiki/graphs";
# url of webdot
$Dot_Cgi="http://your.server.org/cgi-bin/webdot/pmwiki/graphs";
# choose a method between dot and webdot 
# dot is more dangerous but without dot, client side map are not supported
$Dot_Method="webdot"; // choose between dot and webdot
# allow the dot and webdot method (for graphwiki directive)
$Dot_Dual="yes";

# Dont change anything below here.

#========================================

function graphviz_file(&$dot_description,&$engine)
    {
    global $KPV, $KeepToken,$Dot_Files;
    $dot = preg_replace( "/$KeepToken(\\d+?)$KeepToken/e", '$KPV[$1]', $dot_description);
    $dot = preg_replace( "/&gt;/", ">", $dot);
    $engine="dot";
    if (substr(ltrim($dot),0,2)!="di")
	$engine="neato";
    $Md5Name = md5( $dot);
    $filename="$Dot_Files/$Md5Name.dot";
    if (! file_exists($filename)) 
	fputs( fopen($filename,"wb"), $dot);
    return $Md5Name;
    }

markup('graphviz','directive','/\\(:graphviz\\s*(.*?):\\)/e',"FmtGraphviz($1,'png')");

function fmtgraphviz($dot_description) 
    {
    global $Dot_Method,$Dot_Files,$Dot_Cgi,$Dot_Web;
    $Md5Name=graphviz_file($dot_description,$engine); // creation of the dot file
    $file="$Dot_Files/$Md5Name";
    if ($Dot_Method=="dot")    
	{
	if (!file_exists("$file.png")) 
	    exec("$engine -Tpng $file.dot -o $file.png"); // graph processing
        $return="$Dot_Web/$Md5Name"; // return image
	}
    else
	$return="$Dot_Cgi/$Md5Name.dot"; // return graph processing link-image
    return "$return.png";
    }

//implementation of server side maps ("URL"+ allowed as node property)

markup('graphmap','directive','/\\(:graphmap\\s*(.*?):\\)/e',"Fmtgraphmap($1)");

function fmtgraphmap($dot_description) 
    {
    global $Dot_Method,$Dot_Files,$Dot_Cgi,$Dot_Web;
    $Md5Name=graphviz_file($dot_description,$engine); // creation of the dot file
    $file="$Dot_Files/$Md5Name";
    if (($Dot_Method=="dot") and (!file_exists("$file.png") or !file_exists("$file.map")))  
	exec("$engine -Tpng $file.dot -o $file.png -Tcmap $file.dot -o $file.map"); // graph and map processing
    $url="$Dot_Cgi/$Md5Name.dot";
    return "[=<a href=\"$url.map\"><img src=\"$url.png\" border=\"0\" ismap></a>=]";
    }

// implementation of client side maps - automatic links with WikiWords

markup('graphwiki','directive','/\\(:graphwiki\\s*(.*?):\\)/e',"Fmtgraphwiki($1)");

function fmtgraphwiki($dot_description) 
    {
    global $Dot_Method,$Dot_Files,$Dot_Cgi,$Dot_Web,$Dot_Dual;
    $Md5Name=graphviz_file($dot_description,$engine); // creation of the dot file
    $file="$Dot_Files/$Md5Name";
    if (($Dot_Method=="dot") or ($Dot_Dual=="yes"))
	{
	if (!file_exists("$file.png") or !file_exists("$file.map"))      
	    exec("$engine -Tpng $file.dot -o $file.png -Tcmap $file.dot -o $file.map"); // graph and map processing	
	$map="<map name=\"$Md5Name\" ".join(file("$file.map"),' ')."</map>"; // get map file content	    
	if (($Dot_Method=="dot") and !file_exists("$file.png")) 
	    {
	    exec("$engine -Tpng $file.dot -o $file.png"); // graph processing
	    $img="$Dot_Web/$Md5Name.png";
	    }
	else
	    $img="$Dot_Cgi/$Md5Name.dot.png";
	$return="[=$map<img border=\"0\"src=\"$img\" usemap=\"$Md5Name\">=]";
	}
    else 
        $return="%red%'-''graphwiki'' directive are not allowed in ''webdot only'' mode-'%%";
    return $return;
    }

?>