1<?php
2/**
3 * DokuWiki Plugin dwtimeline (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  saggi <saggi@gmx.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_dwtimeline extends DokuWiki_Plugin {
13
14    /**
15     * Global direction memory
16     * @var type
17     */
18    protected $direction;
19    protected $align;
20
21    /**
22     * Change the current content of $direction String (left,right)
23     * @param type $direction
24     * @return string
25     */
26    public static function getDirection($direction)
27    {
28        if($direction === 'tl-right'){
29            $direction = 'tl-left';
30            }
31        else {
32                $direction = 'tl-right';
33            }
34        return $direction;
35    }
36
37    /**
38     * Match the options of a entity e.g. <dwtimeline opt1="value1" opt2="value2">
39     * @param type $match the cleaned option String: 'opt1="value1" opt2="value2"'
40     * @param type $boxtitle special identifier for the title-tag
41     * @return type
42     */
43    public static function getTitleMatches($match)
44    {
45        $data = [];
46        $titles=[];
47        global $align;
48        $data['align'] = $align; // Set Standard Alignment
49        $data['backcolor'] = '';
50        $data['data'] = '';
51        preg_match_all('/(?<title>\w+?\b=".*?")/',$match,$titles);
52        foreach ($titles['title'] as $title) {
53            $opttitle = explode('=',$title,2);
54            switch(trim($opttitle[0]))
55            {
56                case 'link':
57                    $data['link'] = self::getLink(trim($opttitle[1],' "'));
58                    break;
59                case 'data':
60                    $data[$opttitle[0]] = ' data-point="'.hsc(substr(trim($opttitle[1],' "'),0,2)).'" ';
61                    break;
62                case 'align':
63                    $data[$opttitle[0]] = self::checkValues(hsc(trim($opttitle[1],' "')),array("horz", "vert") , $align);
64                    break;
65                case 'backcolor':
66                    if(!self::isValidColor(hsc(trim($opttitle[1],' "')))) { break;}
67                    $data[$opttitle[0]] = ' style="background-color:'.self::isValidColor(hsc(trim($opttitle[1],' "'))).';" ';
68                    break;
69                default :
70                    $data[$opttitle[0]] = hsc(trim($opttitle[1],' "'));
71                    break;
72            }
73        }
74        return $data;
75    }
76
77    /**
78     * Check and get the link from given DokuWiki Link
79     * @param type $linkToCheck
80     * @return type
81     */
82    public static function getLink($linkToCheck) {
83       $pattern = '/\[\[(?<link>.+?)\]\]/';
84       $links = [];
85       preg_match_all($pattern, $linkToCheck,$links);
86       foreach ($links['link'] as $link) {
87           return hsc(substr($link,0,strpos($link,'|')));
88       }
89    }
90
91    public static function checkValues($toCheck,$allowed,$standard) {
92        if (in_array($toCheck, $allowed, true)) {
93            return $toCheck;
94        } else {
95            return $standard;
96        }
97    }
98
99    /**
100     * Validate color value $color
101     * this is cut price validation - only to ensure the basic format is correct and there is nothing harmful
102     * three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
103     */
104    Public static function isValidColor($color) {
105        $color = trim($color);
106        $COLOR_NAMES = ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black",
107            "BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate",
108            "Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod",
109            "DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","DarkOrange",
110            "DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey",
111            "DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick",
112            "FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Grey",
113            "Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender",
114            "LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow",
115            "LightGray","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue",
116            "LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen","Magenta",
117            "Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue",
118            "MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin",
119            "NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod",
120            "PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue",
121            "Purple","RebeccaPurple","Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen",
122            "SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen","SteelBlue",
123            "Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"];
124
125        if (in_array(strtolower($color), array_map('strtolower',$COLOR_NAMES))) {
126            return trim($color);
127        }
128
129        $pattern = "/^\s*(
130            (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))|        #colorvalue
131            (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\))     #rgb triplet
132            )\s*$/x";
133
134        if (preg_match($pattern, $color)) {
135            return trim($color);
136        }
137
138        return false;
139    }
140
141}
142