xref: /template/strap/ComboStrap/Site.php (revision c3437056399326d621a01da73b649707fbb0ae69)
1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16use RuntimeException;
17
18class Site
19{
20
21    const STRAP_TEMPLATE_NAME = "strap";
22
23    const SVG_LOGO_IDS = array(
24        ':wiki:logo.svg',
25        ':logo.svg'
26    );
27
28    const PNG_LOGO_IDS = array(
29        ':logo.png',
30        ':wiki:logo.png',
31        ':favicon-32×32.png',
32        ':favicon-16×16.png',
33        ':apple-touch-icon.png',
34        ':android-chrome-192x192.png'
35    );
36
37
38    /**
39     * @return string|null the html img tag or null
40     */
41    public static function getLogoImgHtmlTag($tagAttributes = null): ?string
42    {
43        $logoIds = self::getLogoIds();
44        foreach ($logoIds as $logoId) {
45            if ($logoId->exists()) {
46                $mediaLink = MediaLink::createMediaLinkFromPath($logoId->getPath(), $tagAttributes)
47                    ->setLazyLoad(false);
48                return $mediaLink->renderMediaTag();
49            }
50        }
51        return null;
52    }
53
54    /**
55     * @return Image[]
56     */
57    private static function getLogoIds(): array
58    {
59        $logosPaths = PluginUtility::mergeAttributes(self::PNG_LOGO_IDS, self::SVG_LOGO_IDS);
60        $logos = [];
61        foreach ($logosPaths as $logoPath) {
62            $dokuPath = DokuPath::createMediaPathFromId($logoPath);
63            $logos[] = Image::createImageFromPath($dokuPath);
64        }
65        return $logos;
66    }
67
68
69    /**
70     * @return string|null
71     */
72    public static function getLogoUrlAsSvg()
73    {
74
75
76        $url = null;
77        foreach (self::SVG_LOGO_IDS as $svgLogo) {
78
79            $svgLogoFN = mediaFN($svgLogo);
80
81            if (file_exists($svgLogoFN)) {
82                $url = ml($svgLogo, '', true, '', true);
83                break;
84            };
85        }
86        return $url;
87    }
88
89    public static function getLogoUrlAsPng()
90    {
91
92        $url = null;
93        foreach (self::PNG_LOGO_IDS as $svgLogo) {
94
95            $svgLogoFN = mediaFN($svgLogo);
96
97            if (file_exists($svgLogoFN)) {
98                $url = ml($svgLogo, '', true, '', true);
99                break;
100            };
101        }
102        return $url;
103    }
104
105    /**
106     * https://www.dokuwiki.org/config:title
107     * @return mixed
108     */
109    public static function getTitle()
110    {
111        global $conf;
112        return $conf['title'];
113    }
114
115    /**
116     * @param string $sep - the separator - generally ("-") but not always
117     * @return string
118     *
119     * Locale always canonicalizes to upper case.
120     */
121    public static function getLocale(string $sep = "-"): ?string
122    {
123
124        $locale = null;
125
126        $lang = self::getLang();
127        if ($lang != null) {
128            $country = self::getLanguageRegion();
129            if ($country != null) {
130                $locale = strtolower($lang) . $sep . strtoupper($country);
131            }
132        }
133
134        return $locale;
135    }
136
137    /**
138     *
139     * ISO 3166 alpha-2 country code
140     *
141     */
142    public static function getLanguageRegion()
143    {
144        $region = PluginUtility::getConfValue(Region::CONF_SITE_LANGUAGE_REGION);
145        if (!empty($region)) {
146            return $region;
147        } else {
148
149            if (extension_loaded("intl")) {
150                $locale = locale_get_default();
151                $localeParts = preg_split("/_/", $locale, 2);
152                if (sizeof($localeParts) === 2) {
153                    return $localeParts[1];
154                }
155            }
156
157            return null;
158        }
159
160    }
161
162    /**
163     * @return mixed|null
164     * Wrapper around  https://www.dokuwiki.org/config:lang
165     */
166    public static function getLang()
167    {
168
169        global $conf;
170        $lang = $conf['lang'];
171        return ($lang ?: null);
172    }
173
174    public static function getBaseUrl(): string
175    {
176
177        /**
178         * In a {@link PluginUtility::isDevOrTest()} dev environment,
179         * don't set the
180         * https://www.dokuwiki.org/config:baseurl
181         * to be able to test the metadata / social integration
182         * via a tunnel
183         *
184         * Same as {@link getBaseURL()} ??
185         */
186
187        return DOKU_URL;
188
189    }
190
191    public static function getTag()
192    {
193        global $conf;
194        $tag = $conf['tag'];
195        return ($tag ? $tag : null);
196    }
197
198    /**
199     * @return string - the name of the sidebar page
200     */
201    public static function getSidebarName()
202    {
203        global $conf;
204        return $conf["sidebar"];
205    }
206
207    public static function setTemplate($template)
208    {
209        global $conf;
210        $conf['template'] = $template;
211    }
212
213    public static function setCacheXhtmlOn()
214    {
215        // ensure the value is not -1, which disables caching
216        // https://www.dokuwiki.org/config:cachetime
217        global $conf;
218        $conf['cachetime'] = 60 * 60;
219    }
220
221    public static function debugIsOn()
222    {
223        global $conf;
224        return $conf['allowdebug'];
225    }
226
227    public static function setTemplateToStrap()
228    {
229        global $conf;
230        $conf['template'] = 'strap';
231    }
232
233    public static function setTemplateToDefault()
234    {
235        global $conf;
236        $conf['template'] = 'dokuwiki';
237    }
238
239    public static function setCacheDefault()
240    {
241        // The value is -1, which disables caching
242        // https://www.dokuwiki.org/config:cachetime
243        global $conf;
244        $conf['cachetime'] = -1;
245    }
246
247    public static function useHeadingAsTitle()
248    {
249        // https://www.dokuwiki.org/config:useheading
250        global $conf;
251        $conf['useheading'] = 1;
252    }
253
254    public static function useHeadingDefault()
255    {
256        // https://www.dokuwiki.org/config:useheading
257        global $conf;
258        $conf['useheading'] = 0;
259    }
260
261    public static function getTemplate()
262    {
263        global $conf;
264        return $conf['template'];
265
266    }
267
268    public static function isStrapTemplate()
269    {
270        global $conf;
271        return $conf['template'] == self::STRAP_TEMPLATE_NAME;
272    }
273
274    public static function getAjaxUrl()
275    {
276        return self::getBaseUrl() . "lib/exe/ajax.php";
277    }
278
279    public static function getPageDirectory()
280    {
281        global $conf;
282        /**
283         * Data dir is the pages dir
284         */
285        return $conf['datadir'];
286    }
287
288    public static function disableHeadingSectionEditing()
289    {
290        global $conf;
291        $conf['maxseclevel'] = 0;
292    }
293
294    public static function setBreadCrumbOn()
295    {
296        global $conf;
297        $conf['youarehere'] = 1;
298    }
299
300    public static function isHtmlRenderCacheOn(): bool
301    {
302        global $conf;
303        return $conf['cachetime'] !== -1;
304    }
305
306    public static function getDataDirectory()
307    {
308        global $conf;
309        $dataDirectory = $conf['datadir'];
310        if ($dataDirectory === null) {
311            throw new RuntimeException("The base directory ($dataDirectory) is null");
312        }
313        $file = File::createFromPath($dataDirectory)->getParent();
314        return $file->getAbsoluteFileSystemPath();
315    }
316
317    public static function isLowQualityProtectionEnable(): bool
318    {
319        return PluginUtility::getConfValue(LowQualityPage::CONF_LOW_QUALITY_PAGE_PROTECTION_ENABLE) === 1;
320    }
321
322    public static function getHomePageName()
323    {
324        global $conf;
325        return $conf["start"];
326    }
327
328    /**
329     * @return mixed - Application / Website name
330     */
331    public static function getName()
332    {
333        global $conf;
334        return $conf["title"];
335    }
336
337    public static function getTagLine()
338    {
339        global $conf;
340        return $conf['tagline'];
341    }
342
343    /**
344     * @return int|null
345     */
346    public static function getCacheTime(): ?int
347    {
348        global $conf;
349        $cacheTime = $conf['cachetime'];
350        if ($cacheTime === null) {
351            return null;
352        }
353        if (is_numeric($cacheTime)) {
354            return intval($cacheTime);
355        }
356        return null;
357    }
358
359    /**
360     * Absolute vs Relative URL
361     * https://www.dokuwiki.org/config:canonical
362     */
363    public static function getCanonicalConfForRelativeVsAbsoluteUrl()
364    {
365        global $conf;
366        return $conf['canonical'];
367    }
368
369
370}
371