last update: Sat, 07 Feb 2015 11:23:09 +0000
  1. <?php
  2. require_once('mysql_cfg.php');
  3. function debug_print ($ar) {
  4. foreach ($ar as $a) {
  5. echo "$a --- ";
  6. }
  7. echo "<br \>\n";
  8. }
  9. function debug_print2 ($ar, $fh) {
  10. foreach ($ar as $a) {
  11. fwrite($fh, "$a --- ");
  12. }
  13. fwrite($fh, "<br \>\n");
  14. }
  15. function makeGraph($text, $options) {
  16. $text = trim($text)."\n";
  17. $pos = 0;
  18. $len = strlen($text);
  19. /*$debug = "<pre>== Text ==\n$text</pre>
  20. <pre>== Options ==\n$options</pre>";
  21. $fh = fopen("svgtemp/debug.html", 'w');
  22. fwrite($fh, $debug);*/
  23. $state = 0;
  24. $parents = array();
  25. $tabs = 0;
  26. $word = "";
  27. /*$output = "digraph G {
  28. splines=true;
  29. overlap=false;
  30. edge [len=0.1];
  31. edge [weight=3.0];
  32. ";*/
  33. $output = 'digraph G {
  34. ';
  35. $output.=$options."\n";
  36. $shapes = array();
  37. while ($pos < $len) {
  38. $c = $text[$pos];
  39. switch ($state) {
  40. case 0:
  41. //start of line / reading tabs
  42. switch ($c) {
  43. case "\n":
  44. //skip
  45. break;
  46. case " ":
  47. //tab
  48. $tabs++;
  49. break;
  50. case " ":
  51. //space
  52. //$tabs++;
  53. break;
  54. default:
  55. $word.=$c;
  56. $state = 1;
  57. }
  58. break;
  59. case 1:
  60. //reading the word
  61. switch ($c) {
  62. case "\n":
  63. //word finished
  64. $word = trim($word);
  65. if (strlen($word) > 0) {
  66. $parent = null;
  67. if ($tabs >= count($parents)) {
  68. //mehr tabs -> parent ist oberstes element
  69. if (count($parents) > 0) {
  70. $parent = $parents[count($parents)-1];
  71. }
  72. } else {
  73. //weniger tabs -> vom parentsstapel werfen
  74. while ($tabs <= count($parents)) {
  75. $parent = array_pop($parents);
  76. if ($parent == null) {
  77. break;
  78. }
  79. }
  80. if ($parent != null) {
  81. $parents[] = $parent;
  82. }
  83. }
  84. if ($parent != null) {
  85. $colPos = strpos($word, ":");
  86. $shape = false;
  87. if ($colPos > 0) {
  88. $newword = trim(substr($word, 0, $colPos));
  89. $shape = trim(substr($word, $colPos+1));
  90. $word = $newword;
  91. }
  92. $gtPos = strpos($word, ">");
  93. if ($gtPos) {
  94. $label = trim(substr($word, 0, $gtPos));
  95. $word = trim(substr($word, $gtPos+1));
  96. $output .= "\"$parent\" -> \"$word\" [label = \"$label\"];\n";
  97. } else {
  98. $output .= "\"$parent\" -> \"$word\";\n";
  99. }
  100. if ($shape) {
  101. $shapes[$word] = $shape;
  102. }
  103. } else {
  104. //no parent
  105. $colPos = strpos($word, ":");
  106. $shape = false;
  107. if ($colPos > 0) {
  108. $newword = trim(substr($word, 0, $colPos));
  109. $shape = trim(substr($word, $colPos+1));
  110. $word = $newword;
  111. $shapes[$word] = $shape;
  112. }
  113. }
  114. $parents[] = $word;
  115. }
  116. $word = "";
  117. $state = 0;
  118. $tabs = 0;
  119. break;
  120. default:
  121. $word.=$c;
  122. }
  123. break;
  124. }
  125. $pos++;
  126. }
  127. foreach ($shapes as $word => $shape) {
  128. if (in_array($shape, array('ellipse', 'box', 'circle', 'doublecircle', 'diamond','plaintext'))) {
  129. $output .= "\"$word\" [shape = $shape];\n";
  130. }
  131. }
  132. $output .= "}";
  133. return $output;
  134. }
  135. if (!isset($_GET['id'])) {
  136. die('ungültige Adresse');
  137. } else {
  138. $id = intval($_GET['id']);
  139. $format = "png";
  140. if (isset($_GET['format'])) {
  141. if (in_array($_GET['format'], array("jpg", "png", "svg"))) {
  142. $format = $_GET['format'];
  143. }
  144. }
  145. $outfile = "svgtemp/graph$id.$format";
  146. if (!file_exists($outfile)) {
  147. //file does not exist, create it
  148. $qry = 'SELECT * FROM graph WHERE `id` = "'.$id.'"';
  149. $data = mysql_query($qry);
  150. if ($row = mysql_fetch_array($data)) {
  151. $text = $row['text'];
  152. $options = $row['options'];
  153. $engine = $row['engine'];
  154. $debug = "<pre>== Row ==\n".print_r($row, true)."</pre>";
  155. $fh = fopen("svgtemp/debug.html", 'w');
  156. fwrite($fh, $debug);
  157. fclose($fh);
  158. $filename = "svgtemp/graph$id.dot";
  159. $graph = makeGraph($text, $options);
  160. $fh = fopen($filename, 'w');
  161. fwrite($fh, $graph);
  162. fclose($fh);
  163. $output = array();
  164. exec("$engine -T$format $filename -o $outfile -Gcharset=latin1", $output);
  165. if (count($output) > 0 ) {
  166. print_r("Eror when running graphviz: $output");
  167. die();
  168. }
  169. } else {
  170. die('Bild existiert nicht.');
  171. }
  172. }
  173. if (!file_exists($outfile)) {
  174. die('Graph konnte nicht erstellt werden.');
  175. }
  176. /*
  177. if (!preg_match("/firefox/i",$_SERVER['HTTP_USER_AGENT']))
  178. {
  179. //echo "no firefox";
  180. header( 'Content-disposition: atachment;filename="'.basename($outfile).'"');
  181. }
  182. else
  183. {*/
  184. header( 'filename="'.basename($outfile).'"');
  185. //}
  186. //header( 'Content-disposition: atachment');
  187. //header("Expires: 0");
  188. header("Expires: " + gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
  189. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  190. header("Cache-Control: private",false);
  191. header("Content-Transfer-Encoding: binary");
  192. header( 'Content-Length: ' . filesize( $outfile ) );
  193. header( 'Connection: close');
  194. $imageFormat = $format;
  195. if ($imageFormat == "svg") {
  196. $imageFormat = "svg+xml";
  197. }
  198. header( 'Content-Type: image/'.$imageFormat);
  199. readfile($outfile);
  200. }
  201. /*
  202. $text = "
  203. inWarcraft
  204. Esport
  205. News
  206. Coverage
  207. Szene-Forum
  208. Replays
  209. Downloads
  210. Forum
  211. Audiocomments
  212. Downloads
  213. Forum
  214. Movies
  215. Downloads
  216. Tutorials (im Forum)
  217. Forum
  218. Das Spiel
  219. WarCraft I
  220. WarCraft II
  221. WC3 alpha/beta
  222. Reign of Chaos
  223. The Frozen Throne
  224. Strategien
  225. Orc
  226. Human
  227. Nightelf
  228. Undead
  229. Walkthrough
  230. Dota
  231. Heros
  232. Items
  233. Map-Download
  234. Replays
  235. Audiocomments
  236. Maps
  237. News
  238. Tutorials
  239. Map-Datenbank
  240. Downloads
  241. Audiocomments
  242. Movies
  243. Tools
  244. Replays
  245. Maps
  246. ";*/
  247. /*
  248. $id = uniqid();
  249. $filename = "svgtemp/$id.dot";
  250. $outfile = "svgtemp/$id.png";
  251. $graph = makeGraph($text);
  252. //echo $graph;
  253. //die();
  254. $fh = fopen($filename, 'w');
  255. fwrite($fh, $graph);
  256. fclose($fh);
  257. $output = array();
  258. exec("fdp -Tpng $filename -o $outfile", &$output);
  259. if (count($output) > 0 ) {
  260. print_r($output);
  261. die();
  262. }
  263. */
  264. ?>

goto line:
Compare with:
text copy window edit this code post new code