1<?php
2
3class helper_plugin_pot extends DokuWiki_Plugin {
4
5	// Original code from plugin:color
6	// validate color value $c
7	// this is cut price validation - only to ensure the basic format is
8	// correct and there is nothing harmful
9	// three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
10
11
12	function _isValidcolor($c) {
13
14		$c = trim($c);
15
16		$pattern = "/
17            (^[a-zA-Z]+$)|                                #colorname - not verified
18            (^\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$)|        #colorvalue
19            (^rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\)$)     #rgb triplet
20            /x";
21		//print("<pre>".print_r($c, true)."</pre>");
22		preg_match($pattern, $c, $out);
23		//		return (preg_match($pattern, $c));
24		return $out[0];
25	}
26
27
28	/**
29	* format settings
30	* we expect something like: " key=value | key2=value2 | key3=value3 "
31	* and format it to an array[key][value]
32	*/
33	function _formatsettings($potsettings) {
34		$pureset = $potsettings;
35		$potsettings = explode('|', $potsettings); // splitt template name and arguments
36		$potsettings = array_map('trim', $potsettings); //remove whitespaces, linebreaks
37		$potsettings = array_filter($potsettings); //remove empty array entries
38		$formatedsettings = array();
39
40		foreach ($potsettings as $potsets) {
41			$temp = explode('=', $potsets);
42			$key = trim($temp[0]);
43			$value = trim($temp[1]);
44
45			// check some of the key=value to fit the value the way we like
46			switch ($key) {
47				case 'potid':
48					$formatedsettings[$key] = $value;
49					break;
50				case 'color+':
51					if ($value != '') {
52						$formatedsettings[$key] = "color:".$this->_isValidcolor($value).";";
53					} else {
54						$value = "";
55					}
56					break;
57				case 'color-':
58					if ($value != '') {
59						$formatedsettings[$key] = "color:".$this->_isValidcolor($value).";";
60					} else {
61						$value = "";
62					}
63					break;
64				case 'display': // only limitet styles are allowed
65					if ($value == 'none') {
66						$value = 'display:none;';
67					} else {
68						//$value = 'display:inline-block;';
69						$value = 'display:flex;';
70					}
71					$formatedsettings[$key] = $value;
72					break;
73				case 'float': // only limitet styles are allowed
74					if ($value == 'right') {
75						$value = 'float:right;';
76					} else {
77						//$value = 'display:inline-block;';
78						$value = 'float:left;';
79					}
80					$formatedsettings[$key] = $value;
81					break;
82				case 'width': // TODO check value
83					if ($value == '' || $value == 0) {
84						$value = "";
85						if ($formatedsettings['display'] != 'none') {
86							$formatedsettings['display'] = ""; //no "width="" forces other no "display="
87						} else {
88							$value = "width:$value;";
89						}
90					}
91					$formatedsettings[$key] = $value;
92					break;
93				case 'set':
94					$formatedsettings[$key] = $value;
95					$formatedsettings['pagedefault'] = preg_replace('/set|field/', 'var$0', $pureset); //replace var and field to avoid circles ?
96					break;
97				case 'defaultsettings':
98					$formatedsettings[$key] = $value;
99					break;
100
101				default:
102					$formatedsettings[$key] = $value;
103					break;
104			}
105		}
106		return $formatedsettings;
107	}
108
109
110	/**
111	* calculating
112	* we need: formula, decimals, sign for decimal-point, sign for thousend-point
113	* we get: array with: result, formula, type
114	*/
115	function _calculate($formula, $mdec) {
116
117		$calc = $formula;
118		/**
119		* this program section is based on
120		* Plugin calc : petite calculatrice.
121		*
122		* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
123		* @author     Etienne Mauvais <emauvaisfr@yahoo.fr>
124		*/
125		$encore = 1;
126		while ($encore) {
127			$encore = 0;
128			$calc = preg_replace_callback("/([a-z].+?\(.*?\))/",
129				create_function('$f',
130					'GLOBAL $encore;
131							   $autorisees=Array("abs", "acos", "acosh", "asin", "asinh", "atan", "atan2", "atanh",
132							                     "base_convert", "bindec",
133							                     "ceil", "cos", "cosh",
134							                     "decbin", "dechex", "decoct", "deg2rad",
135							                     "exp", "expm1",
136							                     "floor", "fmod",
137							                     "getrandmax",
138							                     "hexdec", "hypot",
139							                     "is_finite", "is_infinite", "is_nan",
140							                     "lcg_value", "log", "log10", "log1p",
141							                     "max", "min", "mt_getrandmax", "mt_rand", "mt_srand",
142							                     "octdec",
143							                     "pi", "pow",
144							                     "rad2deg", "rand", "round",
145							                     "sin", "sinh", "sqrt", "srand",
146							                     "tan", "tanh");
147
148							   //print "  Trouve : ".$f[1];
149
150							   $nomF=explode("(",$f[1]);
151							   $nomF=$nomF[0];
152
153							   if(in_array($nomF, $autorisees)) {
154							     //print " -> autorisee\n";
155							     return $f[1];
156							   }
157							   else {
158							     //print " -> interdite !!!\n";
159							     $encore=1;
160							     return "";
161							   }'
162				),
163				$calc);
164		}
165
166		//On retire les ";" et les "$"
167		//Remove the ";" and the "$".
168		$calc = preg_replace("/;/", "", $calc);
169		$calc = preg_replace("/\$/", "", $calc);
170		//if (is_numeric($calc)) { $calc = $calc; } else { $calc = "'$calc'"; }
171
172		//On affiche un commentaire dans le source de la page
173		//A comment is displayed in the source of the page
174		//$renderer->doc .= "\n<!-- Calc : $calc -->\n";
175
176
177		$calc_propre = $calc;
178		$calc = "\$n=$calc;";
179
180		//On passe en mode track_errors (apres avoir recupere l'ancien mode)
181		//We switch to track_errors mode (after recovering the old mode)
182		$track = ini_get('track_errors');
183		ini_set('track_errors', 'true');
184		$php_errormsg = "";
185		try {
186			eval($calc);
187		}
188		catch (Exception $e) {
189			$error = "\"<b>Bad value...</b> ".$e->getMessage()."\"";
190			//	      $renderer->doc .= "\"".$this->getLang('calc_valeurincorrecte')."\"";
191			//	return true;
192		}
193		//On repasse dans l'ancien mode track_error
194		//We go back to the old track_error mode
195		ini_set('track_error', $track);
196
197		//S'il y a eu une erreur (non catchee par try)
198		//If there was an error (not catchee by try)
199		if ($php_errormsg) {
200			$error = "<b>pot Error: </b> ".$php_errormsg." <br /> check the formula:  ";
201			//	      $renderer->doc .= "\"<b>".$this->getLang('calc_erreur')."</b> ".$php_errormsg."\"";
202			//return true;
203		}
204
205		//Si la valeur retournee n'est pas un nombre (ex : sqrt(-1)
206		//If the returned value is not a number (ex : sqrt(-1)
207		if (is_nan($n)) {
208			$error = "\"Bad value...\"";
209			//	      $renderer->doc .= "\"".$this->getLang('calc_valeurincorrecte')."\"";
210			//return true;
211		}
212
213		$out = $n;
214		$result = preg_replace("/ /", "&nbsp;", $out);
215
216		$type = 'formula';
217		$calculated['error'] = $error;
218		$calculated['type'] = $type;
219		$calculated['result'] = $result;
220		$calculated['formula'] = $formula;
221
222		return $calculated;
223	}
224}