1<?php
2
3use dokuwiki\HTTP\DokuHTTPClient;
4
5if (!class_exists('PlantUmlDiagram')) {
6    class PlantUmlDiagram {
7        private $markup;
8        private $encoded;
9        private $basePath;
10
11        public function __construct($markup,$plantUmlUrl) {
12            $this->markup = nl2br($markup);
13            $this->encoded = $this->encodep($markup);
14            $this->basePath = $plantUmlUrl;
15        }
16
17        public function getMarkup() {
18            return $this->markup;
19        }
20
21        /**
22         * Get the SVG code
23         *
24         * @return string
25         */
26        public function getSVG()
27        {
28            return (new DokuHTTPClient())->get($this->getSVGDiagramUrl());
29        }
30
31        public function getSVGDiagramUrl() {
32            return $this->basePath."svg/".$this->encoded;
33        }
34
35        public function getPNGDiagramUrl() {
36            return $this->basePath."png/".$this->encoded;
37        }
38
39        public function getTXTDiagramUrl() {
40            return $this->basePath."txt/".$this->encoded;
41        }
42
43        private function encodep($text) {
44            $compressed = gzdeflate($text, 9);
45            return $this->encode64($compressed);
46       }
47
48        private function encode6bit($b) {
49            if ($b < 10) {
50                 return chr(48 + $b);
51            }
52            $b -= 10;
53            if ($b < 26) {
54                 return chr(65 + $b);
55            }
56            $b -= 26;
57            if ($b < 26) {
58                 return chr(97 + $b);
59            }
60            $b -= 26;
61            if ($b == 0) {
62                 return '-';
63            }
64            if ($b == 1) {
65                 return '_';
66            }
67            return '?';
68       }
69
70        private function append3bytes($b1, $b2, $b3) {
71            $c1 = $b1 >> 2;
72            $c2 = (($b1 & 0x3) << 4) | ($b2 >> 4);
73            $c3 = (($b2 & 0xF) << 2) | ($b3 >> 6);
74            $c4 = $b3 & 0x3F;
75            $r = "";
76            $r .= $this->encode6bit($c1 & 0x3F);
77            $r .= $this->encode6bit($c2 & 0x3F);
78            $r .= $this->encode6bit($c3 & 0x3F);
79            $r .= $this->encode6bit($c4 & 0x3F);
80            return $r;
81       }
82
83        private function encode64($c) {
84            $str = "";
85            $len = strlen($c);
86            for ($i = 0; $i < $len; $i+=3) {
87                   if ($i+2==$len) {
88                         $str .= $this->append3bytes(ord(substr($c, $i, 1)), ord(substr($c, $i+1, 1)), 0);
89                   } else if ($i+1==$len) {
90                         $str .= $this->append3bytes(ord(substr($c, $i, 1)), 0, 0);
91                   } else {
92                         $str .= $this->append3bytes(ord(substr($c, $i, 1)), ord(substr($c, $i+1, 1)),
93                             ord(substr($c, $i+2, 1)));
94                   }
95            }
96            return $str;
97       }
98    }
99}
100
101?>
102