comparison Options.inc.php @ 134:b6b4a58c7625

Using .inc.php rather than just .inc for include files.
author Tom Fredrik Blenning <bfg@bfgconsult.no>
date Sun, 22 Jan 2023 19:22:00 +0100
parents Options.inc@14959382c901
children 2fe6713ccd64
comparison
equal deleted inserted replaced
133:00255ca89459 134:b6b4a58c7625
1 <?php
2 /**
3 */
4 abstract class Cacheable {
5 const NO = 0;
6 const YES = 1;
7 const UNDETERMINED = -1;
8 }
9
10 /**
11 * Contains alle configurable parameters, and "globals"
12 *
13 * @author Tom Fredrik Blenning Klaussen
14 */
15 class Options
16 {
17 private $defaultLang;
18 private $lang;
19 private $name;
20 private $acceptedLanguages = array();
21 private $inputDefaults = array();
22 private $cache;
23 private $urlParams = array();
24 private $basePath;
25 private $flagUrl;
26 private $baseUrl;
27 private $cacheable = Cacheable::YES;
28
29 /**
30 * Gets the default language
31 *
32 * @return two letter code for the language
33 */
34 function getDefaultLang()
35 {
36 return $this->defaultLang;
37 }
38
39 /**
40 * Gets the selected language
41 *
42 * @return two letter code for the language
43 */
44 function getLang()
45 {
46 return $this->lang;
47 }
48
49 /**
50 * Sets wether or not this page may be cached
51 *
52 * @param $cacheable Cacheable
53 */
54 function setCacheable($cacheable)
55 {
56 $this->cacheable = $cacheable;
57 }
58
59 /**
60 * Gets wether or not this page may be cached
61 *
62 * @return Cacheable, default is YES
63 */
64 function getCacheable()
65 {
66 return $this->cacheable;
67 }
68
69 /**
70 * Get the base url, or if non set, extracts it from _SERVER
71 *
72 * @return the baseurl for the scripts
73 */
74 function getBaseUrl()
75 {
76 if ($this->baseUrl)
77 return $this->baseUrl;
78
79 $request = $_SERVER['REQUEST_URI'];
80
81 $l = strpos($request, '?');
82 $base = ($l) ? substr($request, 0 , $l) : $request;
83 $base = rtrim($base, '/');
84
85 return "//" . $_SERVER['HTTP_HOST'] . $base;
86 }
87
88 /**
89 * Replaces placeholder variables, with actual values.
90 *
91 * Currently supported values:
92 * @li \%HOST\%
93 *
94 * @param $value string to replace values in
95 * @return the processed string
96 */
97 static function replacePlaceholders($value)
98 {
99 $value = preg_replace('/%HOST%/', $_SERVER['HTTP_HOST'], $value);
100 return $value;
101 }
102
103 /**
104 * Sets the base url where scripts are located
105 *
106 * @param $baseUrl the url where scripts are located
107 */
108 function setBaseUrl($baseUrl)
109 {
110 $baseUrl = self::replacePlaceholders($baseUrl);
111 $this->baseUrl = $baseUrl;
112 }
113
114 /**
115 * Sets the url for the flag script
116 *
117 * @param $flagUrl for flag script
118 */
119 function setFlagUrl($flagUrl)
120 {
121 $flagUrl = self::replacePlaceholders($flagUrl);
122 $this->flagUrl = $flagUrl;
123 }
124
125 /**
126 * Gets the url for the flag script, or autogenerate if not set
127 *
128 * @return url for flag script
129 */
130 function getFlagUrl()
131 {
132 if ($this->flagUrl)
133 return $this->flagUrl;
134
135 return $this->getBaseUrl() . "/flag.php";
136 }
137
138 /**
139 * Sets the selected language
140 *
141 * @param $lang two letter code for the language
142 */
143 function setLang($lang)
144 {
145 $this->lang = $lang;
146 }
147
148 /**
149 * Gets the path where the scripts are located
150 *
151 * @return path where scripts are located
152 */
153 function getBasePath()
154 {
155 return $this->basePath;
156 }
157
158 /**
159 * Sets the path where the scripts are located
160 *
161 * @param $basePath path where scripts are located
162 */
163 function setBasePath($basePath)
164 {
165 $this->basePath = $basePath;
166 }
167
168 /**
169 * Sets a set of urlparameters
170 *
171 * @param $urlParams list of parameters to get from the URL
172 */
173 function setUrlParams($urlParams)
174 {
175 foreach($urlParams as $key) {
176 $value = array_key_exists($key, $_GET) ? $_GET[$key] : '';
177 $this->urlParams[$key] = $value;
178 }
179 }
180
181 /**
182 * Gets the default language
183 *
184 * @return associative array of key and value for the url parameters
185 */
186 function getUrlParams()
187 {
188 return $this->urlParams;
189 }
190
191 /**
192 * Sets the name(identity) for this page
193 *
194 * @param $name name(identity)
195 */
196 function setName($name)
197 {
198 $this->name = $name;
199 }
200
201 /**
202 * Gets the name(identity) for this page
203 *
204 * @return name(identity)
205 */
206 function getName()
207 {
208 return $this->name;
209 }
210
211 /**
212 * Sets a cache object
213 *
214 * @param $cache CacheTimeCheck object
215 */
216 function setCache($cache)
217 {
218 $this->cache = $cache;
219 }
220
221 /**
222 * Gets the cache object
223 *
224 * @return cache object
225 */
226 function getCache()
227 {
228 return $this->cache;
229 }
230
231 /**
232 * A list of languages which this configuration supports.
233 *
234 * @return array of two letter language codes
235 */
236 function getAcceptedLanguages()
237 {
238 return $this->acceptedLanguages;
239 }
240
241 /**
242 * Gets the default value associated whith the key
243 *
244 * @param $key as specified in master xml file.
245 * @return associated default
246 */
247 function getInputDefault($key)
248 {
249 return $this->inputDefaults[$key];
250 }
251
252 /**
253 * Constructs an options object
254 *
255 * This contstructor will consume any tag with the type option, and
256 * extract values from any tag with type input
257 *
258 * @include master.xml
259 *
260 * @param $baseDocument An open xml file
261 */
262 function __construct($baseDocument)
263 {
264 $params = $baseDocument->getElementsByTagName("param");
265 $toRemove = array();
266 foreach ($params as $param) {
267 if ($param->getAttribute("type") == "option") {
268 $id = $param->getAttribute("id");
269 if ($id == "lang") {
270 $this->defaultLang = $param->getAttribute("default");
271 $accepts = $param->getElementsByTagName("accept_value");
272 foreach($accepts as $accept) {
273 foreach($accept->childNodes as $child) {
274 array_push($this->acceptedLanguages, $child->nodeValue);
275 }
276 }
277 }
278 elseif ($id == "baseUrl") {
279 $value = $param->getAttribute("value");
280 if($value)
281 $this->setBaseUrl($value);
282 }
283 elseif ($id == "flagUrl") {
284 $value = $param->getAttribute("value");
285 if($value)
286 $this->setFlagUrl($value);
287 }
288 else {
289 warn("Invalid option: ${id}");
290 }
291 //We need to iterate in the opposite direction when removing,
292 //so best shifting.
293 array_unshift($toRemove, $param);
294 }
295 elseif ($param->getAttribute("type") == "input") {
296 $id = $param->getAttribute("id");
297 $default = $param->getAttribute("default");
298 $this->inputDefaults[$id] = $default;
299 }
300 }
301 foreach($toRemove as $param) {
302 $parent = $param->parentNode;
303 $parent->removeChild($param);
304 }
305 }
306 }
307 ?>