xref: /plugin/combo/ComboStrap/Site.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
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 ComboStrap\Meta\Field\PageTemplateName;
17use ComboStrap\Meta\Field\Region;
18use ComboStrap\Web\Url;
19use ComboStrap\Web\UrlRewrite;
20use Exception;
21
22class Site
23{
24
25    const STRAP_TEMPLATE_NAME = "strap";
26
27    const SVG_LOGO_IDS = array(
28        'wiki:logo.svg',
29        'logo.svg'
30    );
31
32    const PNG_LOGO_IDS = array(
33        'logo.png',
34        'wiki:logo.png',
35        'favicon-32×32.png',
36        'favicon-16×16.png',
37        'apple-touch-icon.png',
38        'android-chrome-192x192.png'
39    );
40    /**
41     * Name of the main header slot
42     */
43    public const SLOT_MAIN_HEADER_NAME = "slot_main_header";
44    /**
45     * Name of the main footer slot
46     */
47    public const SLOT_MAIN_FOOTER_NAME = "slot_main_footer";
48
49    public const SLOT_MAIN_SIDE_NAME = "slot_main_side";
50
51    const CANONICAL = "configuration";
52    /**
53     * Strap Template meta (version, release date, ...)
54     * @var array
55     */
56    private static $STRAP_TEMPLATE_INFO;
57    private $executingContext;
58
59    /**
60     * @param ExecutionContext $executingContext
61     */
62    public function __construct(ExecutionContext $executingContext)
63    {
64        $this->executingContext = $executingContext;
65    }
66
67
68    /**
69     * @return WikiPath[]
70     */
71    public static function getLogoImagesAsPath(): array
72    {
73        $logosPaths = PluginUtility::mergeAttributes(self::PNG_LOGO_IDS, self::SVG_LOGO_IDS);
74        $logos = [];
75        foreach ($logosPaths as $logoPath) {
76            $dokuPath = WikiPath::createMediaPathFromId($logoPath);
77            if (FileSystems::exists($dokuPath)) {
78                try {
79                    $logos[] = $dokuPath;
80                } catch (Exception $e) {
81                    // The image is not valid
82                    LogUtility::msg("The logo ($logoPath) is not a valid image. {$e->getMessage()}");
83                }
84            }
85        }
86        return $logos;
87    }
88
89    /**
90     * @deprecated see {@link SiteConfig::setUrlRewriteToDoku()}
91     */
92    public static function setUrlRewriteToDoku()
93    {
94        ExecutionContext::getActualOrCreateFromEnv()->getConfig()
95            ->setUrlRewriteToDoku();
96    }
97
98    /**
99     * Web server rewrite (Apache rewrite (htaccess), Nginx)
100     * @deprecated see {@link SiteConfig::setUrlRewriteToWebServer()}
101     */
102    public static function setUrlRewriteToWebServer()
103    {
104        ExecutionContext::getActualOrCreateFromEnv()->getConfig()
105            ->setUrlRewriteToWebServer();
106    }
107
108    /**
109     * https://www.dokuwiki.org/config:useslash
110     * @return void
111     */
112    public static function useSlashSeparatorInEndpointUrl()
113    {
114        global $conf;
115        $conf['useslash'] = 1; // use slash instead of ;
116    }
117
118
119    public static function getUrlEndpointSeparator(): string
120    {
121        $defaultSeparator = WikiPath::NAMESPACE_SEPARATOR_DOUBLE_POINT;
122        $slashSeparator = "/";
123        global $conf;
124        $key = 'useslash';
125        $value = $conf[$key];
126        try {
127            $valueInt = DataType::toInteger($value);
128        } catch (ExceptionBadArgument $e) {
129            LogUtility::internalError("The ($key) configuration does not have an integer value ($value). Default separator returned");
130            return $defaultSeparator;
131        }
132        switch ($valueInt) {
133            case 0:
134                return $defaultSeparator;
135            case 1:
136                return $slashSeparator;
137            default:
138                LogUtility::internalError("The ($key) configuration has an integer value ($valueInt) that is not a valid one (0 or 1). Default separator returned");
139                return $defaultSeparator;
140        }
141    }
142
143
144    public static function getTocMinHeadings(): int
145    {
146        global $conf;
147        $confKey = 'tocminheads';
148        $tocMinHeads = $conf[$confKey];
149        if ($tocMinHeads === null) {
150            return 0;
151        }
152        try {
153            return DataType::toInteger($tocMinHeads);
154        } catch (ExceptionBadArgument $e) {
155            LogUtility::error("The configuration ($confKey) is not an integer. Error:{$e->getMessage()}", self::CANONICAL);
156            return 0;
157        }
158    }
159
160
161    /**
162     * @param int $int
163     * @deprecated
164     */
165    public static function setTocMinHeading(int $int)
166    {
167        ExecutionContext::getActualOrCreateFromEnv()
168            ->getConfig()
169            ->setTocMinHeading($int);
170    }
171
172    public static function getTopTocLevel(): int
173    {
174        global $conf;
175        $confKey = 'toptoclevel';
176        $value = $conf[$confKey];
177        try {
178            return DataType::toInteger($value);
179        } catch (ExceptionBadArgument $e) {
180            LogUtility::error("The configuration ($confKey) has a value ($value) that is not an integer", self::CANONICAL);
181            return 1;
182        }
183    }
184
185    /**
186     * @param int $int
187     * @return void
188     * @deprecated
189     */
190    public static function setTocTopLevel(int $int)
191    {
192        ExecutionContext::getActualOrCreateFromEnv()
193            ->getConfig()
194            ->setTocTopLevel($int);
195
196    }
197
198    /**
199     * @return int
200     * https://www.dokuwiki.org/config:breadcrumbs
201     */
202    public static function getVisitedPagesCountInHistoricalBreadCrumb(): int
203    {
204        global $conf;
205        $confKey = 'breadcrumbs';
206        $visitedPagesInBreadCrumb = $conf[$confKey];
207        $defaultReturnValue = 10;
208        if ($visitedPagesInBreadCrumb === null) {
209            return $defaultReturnValue;
210        }
211        try {
212            return DataType::toInteger($visitedPagesInBreadCrumb);
213        } catch (ExceptionBadArgument $e) {
214            LogUtility::error("The configuration ($confKey) has value ($visitedPagesInBreadCrumb) that is not an integer. Error:{$e->getMessage()}");
215            return $defaultReturnValue;
216        }
217
218    }
219
220    /**
221     * This setting enables the standard DokuWiki XHTML renderer to be replaced by a render plugin that also provides XHTML output.
222     * @param string $string
223     * @return void
224     */
225    public static function setXhtmlRenderer(string $string)
226    {
227        global $conf;
228        $conf["renderer_xhtml"] = $string;
229    }
230
231    /**
232     * The host of the actual server
233     * (may be virtual)
234     * @return string
235     */
236    public static function getServerHost(): string
237    {
238        /**
239         * Based on {@link getBaseURL()}
240         * to be dokuwiki compliant
241         */
242        $remoteHost = $_SERVER['HTTP_HOST'];
243        if ($remoteHost !== null) {
244            return $remoteHost;
245        }
246        $remoteHost = $_SERVER['SERVER_NAME'];
247        if ($remoteHost !== null) {
248            return $remoteHost;
249        }
250        /**
251         * OS name
252         */
253        return php_uname('n');
254    }
255
256    public static function getLangDirection()
257    {
258        global $lang;
259        return $lang['direction'];
260    }
261
262    /**
263     * Set a site configuration outside a {@link ExecutionContext}
264     * It permits to configure the installation before execution
265     *
266     * For instance, we set the {@link PageTemplateName::CONF_DEFAULT_NAME default page layout} as {@link PageTemplateName::BLANK_TEMPLATE_VALUE}
267     * in test by default to speed ud test. In a normal environment, the default is {@link PageTemplateName::HOLY_TEMPLATE_VALUE}
268     *
269     * @param $key
270     * @param $value
271     * @param string|null $namespace - the plugin name
272     * @return void
273     * @deprecated use {@link SiteConfig::setConf()}
274     */
275    public static function setConf($key, $value, ?string $namespace = PluginUtility::PLUGIN_BASE_NAME)
276    {
277        global $conf;
278        if ($namespace !== null) {
279            $conf['plugin'][$namespace][$key] = $value;
280        } else {
281            $conf[$key] = $value;
282        }
283    }
284
285    public static function getLangObject(): Lang
286    {
287        return Lang::createFromValue(Site::getLang());
288    }
289
290
291    public static function getOldDirectory(): LocalPath
292    {
293        global $conf;
294        /**
295         * Data dir is the pages dir (savedir is the data dir)
296         */
297        $oldDirConf = $conf['olddir'];
298        if ($oldDirConf === null) {
299            throw new ExceptionRuntime("The old directory ($oldDirConf) is null");
300        }
301        return LocalPath::createFromPathString($oldDirConf);
302    }
303
304
305    function getEmailObfuscationConfiguration()
306    {
307        global $conf;
308        return $conf['mailguard'];
309    }
310
311    /**
312     * @return string|null
313     */
314    public static function getLogoUrlAsSvg(): ?string
315    {
316
317
318        $url = null;
319        foreach (self::SVG_LOGO_IDS as $svgLogo) {
320
321            $svgLogoFN = mediaFN($svgLogo);
322            if (file_exists($svgLogoFN)) {
323                $url = ml($svgLogo, '', true, '', true);
324                break;
325            }
326        }
327        return $url;
328    }
329
330    /**
331     * @throws ExceptionNotFound
332     */
333    public static function getLogoAsSvgImage(): WikiPath
334    {
335        foreach (self::SVG_LOGO_IDS as $svgLogo) {
336            $image = WikiPath::createMediaPathFromId($svgLogo);
337            if (FileSystems::exists($image)) {
338                return $image;
339            }
340        }
341        throw new ExceptionNotFound("No Svg Logo Image found");
342    }
343
344    /**
345     * @throws ExceptionNotFound
346     */
347    public static function getLogoAsRasterImage(): FetcherRaster
348    {
349        foreach (self::PNG_LOGO_IDS as $pngLogo) {
350            $pngLogoPath = WikiPath::createMediaPathFromId($pngLogo);
351            if (!FileSystems::exists($pngLogoPath)) {
352                continue;
353            }
354            try {
355                return FetcherRaster::createImageRasterFetchFromPath($pngLogoPath);
356            } catch (ExceptionCompile $e) {
357                LogUtility::error("Error while getting the log as raster image: The png logo ($pngLogo) returns an error. {$e->getMessage()}", self::CANONICAL, $e);
358                continue;
359            }
360        }
361        throw new ExceptionNotFound("No raster logo image was found");
362    }
363
364
365    public static function getLogoUrlAsPng()
366    {
367
368        $url = null;
369        foreach (self::PNG_LOGO_IDS as $svgLogo) {
370
371            $svgLogoFN = mediaFN($svgLogo);
372
373            if (file_exists($svgLogoFN)) {
374                $url = ml($svgLogo, '', true, '', true);
375                break;
376            };
377        }
378        return $url;
379    }
380
381    /**
382     * @return mixed
383     * @deprecated use {@link Site::getName()} instead
384     * https://www.dokuwiki.org/config:title
385     */
386    public static function getTitle()
387    {
388        global $conf;
389        return $conf['title'];
390    }
391
392    /**
393     * https://www.dokuwiki.org/config:title
394     */
395    public static function setName($name)
396    {
397        global $conf;
398        $conf['title'] = $name;
399    }
400
401    /**
402     * @param string $sep - the separator - generally ("-") but not always
403     * @return string
404     *
405     * Locale always canonicalizes to upper case.
406     */
407    public static function getLocale(string $sep = "-"): string
408    {
409
410        $locale = null;
411
412        $lang = self::getLang();
413
414        $country = self::getLanguageRegion();
415
416        $locale = strtolower($lang) . $sep . strtoupper($country);
417
418        return $locale;
419    }
420
421    /**
422     *
423     * ISO 3166 alpha-2 country code
424     *
425     */
426    public static function getLanguageRegion(): string
427    {
428        $region = SiteConfig::getConfValue(Region::CONF_SITE_LANGUAGE_REGION);
429        if (!empty($region)) {
430            return $region;
431        } else {
432
433            if (extension_loaded("intl")) {
434                $locale = locale_get_default();
435                $localeParts = explode("_", $locale, 2);
436                if (sizeof($localeParts) === 2) {
437                    return $localeParts[1];
438                }
439            }
440        }
441        return "us";
442
443    }
444
445    /**
446     * @return string
447     * Wrapper around  https://www.dokuwiki.org/config:lang
448     */
449    public static function getLang(): string
450    {
451        global $conf;
452        $lang = $conf['lang'];
453        if ($lang === null) {
454            return "en";
455        }
456        return $lang;
457    }
458
459    /**
460     * In a {@link PluginUtility::isDevOrTest()} dev environment,
461     * don't set the
462     * https://www.dokuwiki.org/config:baseurl
463     * to be able to test the metadata / social integration
464     * via a tunnel
465     *
466     */
467    public static function getBaseUrl(): string
468    {
469
470        /**
471         * Same as {@link getBaseURL()} ??
472         */
473        global $conf;
474        $baseUrl = $conf['baseurl'];
475        if (!empty($baseUrl)) {
476            return $baseUrl;
477        }
478        return DOKU_URL;
479
480    }
481
482    public static function getTag()
483    {
484        global $conf;
485        $tag = $conf['tag'];
486        return ($tag ? $tag : null);
487    }
488
489    public static function setTemplate($template)
490    {
491        global $conf;
492        $conf['template'] = $template;
493    }
494
495    /**
496     * @return void
497     * @deprecated
498     */
499    public static function setCacheXhtmlOn()
500    {
501        // ensure the value is not -1, which disables caching
502        // https://www.dokuwiki.org/config:cachetime
503        global $conf;
504        $conf['cachetime'] = 60 * 60;
505    }
506
507    public static function debugIsOn()
508    {
509        global $conf;
510        return $conf['allowdebug'];
511    }
512
513    public static function setCacheDefault()
514    {
515        // The value is -1, which disables caching
516        // https://www.dokuwiki.org/config:cachetime
517        global $conf;
518        $conf['cachetime'] = -1;
519    }
520
521    public static function useHeadingAsTitle()
522    {
523        // https://www.dokuwiki.org/config:useheading
524        global $conf;
525        $conf['useheading'] = 1;
526    }
527
528    public static function useHeadingDefault()
529    {
530        // https://www.dokuwiki.org/config:useheading
531        global $conf;
532        $conf['useheading'] = 0;
533    }
534
535    public static function getTemplate()
536    {
537        global $conf;
538        return $conf['template'];
539
540    }
541
542    public static function isStrapTemplate()
543    {
544        global $conf;
545        return $conf['template'] == self::STRAP_TEMPLATE_NAME;
546    }
547
548
549    public static function getPageDirectory(): LocalPath
550    {
551        global $conf;
552        /**
553         * Data dir is the pages dir (savedir is the data dir)
554         */
555        $pageDirectory = $conf['datadir'];
556        if ($pageDirectory === null) {
557            throw new ExceptionRuntime("The page directory ($pageDirectory) is null");
558        }
559        return LocalPath::createFromPathString($pageDirectory);
560    }
561
562    public static function disableHeadingSectionEditing()
563    {
564        ExecutionContext::getActualOrCreateFromEnv()
565            ->getConfig()
566            ->setDisableHeadingSectionEditing();
567    }
568
569    public static function setBreadCrumbOn()
570    {
571        global $conf;
572        $conf['youarehere'] = 1;
573    }
574
575    public static function isHtmlRenderCacheOn(): bool
576    {
577        return ExecutionContext::getActualOrCreateFromEnv()
578            ->getConfig()
579            ->isXhtmlCacheOn();
580    }
581
582    /**
583     * @return LocalPath
584     * @deprecated
585     */
586    public static function getDataDirectory(): LocalPath
587    {
588        return ExecutionContext::getActualOrCreateFromEnv()->getConfig()->getDataDirectory();
589    }
590
591    public static function isLowQualityProtectionEnable(): bool
592    {
593        return SiteConfig::getConfValue(LowQualityPage::CONF_LOW_QUALITY_PAGE_PROTECTION_ENABLE) === 1;
594    }
595
596    /**
597     * @return string
598     * @deprecated
599     */
600    public static function getIndexPageName()
601    {
602        return ExecutionContext::getActualOrCreateFromEnv()->getConfig()->getIndexPageName();
603    }
604
605    /**
606     * @return mixed - Application / Website name
607     */
608    public static function getName()
609    {
610        return self::getTitle();
611    }
612
613    public static function getTagLine()
614    {
615        global $conf;
616        return $conf['tagline'];
617    }
618
619    /**
620     * @return int
621     */
622    public static function getXhtmlCacheTime(): int
623    {
624        global $conf;
625        $cacheTime = $conf['cachetime'];
626        if ($cacheTime === null) {
627            LogUtility::internalError("The global `cachetime` configuration was not set.", self::CANONICAL);
628            return FetcherMarkup::MAX_CACHE_AGE;
629        }
630        try {
631            return DataType::toInteger($cacheTime);
632        } catch (ExceptionBadArgument $e) {
633            LogUtility::error("The global `cachetime` configuration has a value ($cacheTime) that is not an integer. Error: {$e->getMessage()}", self::CANONICAL);
634            return FetcherMarkup::MAX_CACHE_AGE;
635        }
636    }
637
638    /**
639     * Absolute vs Relative URL
640     * https://www.dokuwiki.org/config:canonical
641     */
642    public static function shouldUrlBeAbsolute(): bool
643    {
644        global $conf;
645        $value = $conf['canonical'];
646        if ($value === 1) {
647            return true;
648        }
649        return false;
650    }
651
652    /**
653     * @param string $description
654     * Same as {@link Site::setDescription()}
655     */
656    public static function setTagLine(string $description)
657    {
658        global $conf;
659        $conf['tagline'] = $description;
660    }
661
662    /**
663     * @param string $description
664     *
665     */
666    public static function setDescription(string $description)
667    {
668        self::setTagLine($description);
669    }
670
671    /**
672     * @param string $primaryColorValue
673     * @return void
674     * @deprecated
675     */
676    public static function setPrimaryColor(string $primaryColorValue)
677    {
678        ExecutionContext::getActualOrCreateFromEnv()
679            ->getConfig()
680            ->setPrimaryColor($primaryColorValue);
681    }
682
683    /**
684     * @param $default
685     * @return ColorRgb|null
686     * @deprecated use {@link SiteConfig::getPrimaryColor()} instead
687     */
688    public static function getPrimaryColor(string $default = null): ?ColorRgb
689    {
690        try {
691            return ExecutionContext::getActualOrCreateFromEnv()
692                ->getConfig()
693                ->getPrimaryColor();
694        } catch (ExceptionNotFound $e) {
695            if($default===null){
696                return null;
697            }
698            try {
699                return ColorRgb::createFromString($default);
700            } catch (ExceptionBadArgument $e) {
701                LogUtility::internalError("The value ($default) is not a valid color");
702                return null;
703            }
704        }
705    }
706
707    public static function getSecondaryColor($default = null): ?ColorRgb
708    {
709        $value = Site::getSecondaryColorValue($default);
710        if ($value === null) {
711            return null;
712        }
713        try {
714            return ColorRgb::createFromString($value);
715        } catch (ExceptionCompile $e) {
716            LogUtility::msg("The secondary color value configuration ($value) is not valid. Error: {$e->getMessage()}");
717            return null;
718        }
719    }
720
721    public static function setSecondaryColor(string $secondaryColorValue)
722    {
723        self::setConf(ColorRgb::SECONDARY_COLOR_CONF, $secondaryColorValue);
724    }
725
726    public static function unsetPrimaryColor()
727    {
728        self::setConf(BrandingColors::PRIMARY_COLOR_CONF, null);
729    }
730
731
732    /**
733     * @return bool
734     * @deprecated
735     */
736    public static function isBrandingColorInheritanceEnabled(): bool
737    {
738        return ExecutionContext::getActualOrCreateFromEnv()->getConfig()->isBrandingColorInheritanceEnabled();
739    }
740
741
742    /**
743     * @param $default
744     * @return string|null
745     */
746    public static function getPrimaryColorValue($default = null): ?string
747    {
748        $value = SiteConfig::getConfValue(BrandingColors::PRIMARY_COLOR_CONF, $default);
749        if ($value !== null && trim($value) !== "") {
750            return $value;
751        }
752        if (PluginUtility::isTest()) {
753            // too much trouble
754            // the load of styles is not consistent
755            return null;
756        }
757        $styles = ColorRgb::getDokuWikiStyles();
758        return $styles["replacements"]["__theme_color__"];
759
760    }
761
762    public static function getSecondaryColorValue($default = null)
763    {
764        $value = SiteConfig::getConfValue(ColorRgb::SECONDARY_COLOR_CONF, $default);
765        if ($value === null || trim($value) === "") {
766            return null;
767        }
768        return $value;
769    }
770
771    public static function setCanonicalUrlType(string $value)
772    {
773        self::setConf(PageUrlType::CONF_CANONICAL_URL_TYPE, $value);
774    }
775
776    public static function setCanonicalUrlTypeToDefault()
777    {
778        self::setConf(PageUrlType::CONF_CANONICAL_URL_TYPE, null);
779    }
780
781    public static function isBrandingColorInheritanceFunctional(): bool
782    {
783        return self::isBrandingColorInheritanceEnabled() && Site::getPrimaryColorValue() !== null;
784    }
785
786    public static function getMediaDirectory(): LocalPath
787    {
788        global $conf;
789        $mediaDirectory = $conf['mediadir'];
790        if ($mediaDirectory === null) {
791            throw new ExceptionRuntime("The media directory ($mediaDirectory) is null");
792        }
793        return LocalPath::createFromPathString($mediaDirectory);
794    }
795
796    public static function getCacheDirectory(): LocalPath
797    {
798        global $conf;
799        $cacheDirectory = $conf['cachedir'];
800        if ($cacheDirectory === null) {
801            throw new ExceptionRuntime("The cache directory ($cacheDirectory) is null");
802        }
803        return LocalPath::createFromPathString($cacheDirectory);
804    }
805
806
807    /**
808     * @throws ExceptionCompile
809     */
810    private static function checkTemplateVersion()
811    {
812        /**
813         * Check the version
814         */
815        if (self::$STRAP_TEMPLATE_INFO === null) {
816            self::$STRAP_TEMPLATE_INFO = confToHash(__DIR__ . '/../../../tpl/strap/template.info.txt');
817        }
818        $templateVersion = self::$STRAP_TEMPLATE_INFO['version'];
819        $comboVersion = PluginUtility::$INFO_PLUGIN['version'];
820        /** @noinspection DuplicatedCode */
821        if ($templateVersion !== $comboVersion) {
822            $strapName = "Strap";
823            $comboName = "Combo";
824            $strapLink = "<a href=\"https://www.dokuwiki.org/template:strap\">$strapName</a>";
825            $comboLink = "<a href=\"https://www.dokuwiki.org/plugin:combo\">$comboName</a>";
826            if ($comboVersion > $templateVersion) {
827                $upgradeTarget = $strapName;
828            } else {
829                $upgradeTarget = $comboName;
830            }
831            $upgradeLink = "<a href=\"" . wl() . "&do=admin&page=extension" . "\">upgrade <b>$upgradeTarget</b> via the extension manager</a>";
832            $message = "You should $upgradeLink to the latest version to get a fully functional experience. The version of $comboLink is ($comboVersion) while the version of $strapLink is ($templateVersion).";
833            LogUtility::msg($message);
834            throw new ExceptionCompile($message);
835        }
836    }
837
838    /**
839     * @throws ExceptionNotFound
840     */
841    public static function getLogoImage(): WikiPath
842    {
843        $logosImages = Site::getLogoImagesAsPath();
844        if (empty($logosImages)) {
845            throw new ExceptionNotFound("No logo image was installed", "logo");
846        }
847        return $logosImages[0];
848    }
849
850
851    /**
852     * @return void
853     * @deprecated
854     */
855    public static function enableSectionEditing()
856    {
857        ExecutionContext::getActualOrCreateFromEnv()->getConfig()->setEnableSectionEditing();
858    }
859
860    public static function setDefaultTemplate()
861    {
862        global $conf;
863        $conf['template'] = "dokuwiki";
864    }
865
866    /**
867     * @return LocalPath[]
868     */
869    public static function getConfigurationFiles(): array
870    {
871        $files = [];
872        foreach (getConfigFiles('main') as $fileConfig) {
873            $files[] = LocalPath::createFromPathString($fileConfig);
874        }
875        return $files;
876    }
877
878    /**
879     * @return string the base directory for all url
880     * @throws ExceptionNotFound
881     */
882    public static function getUrlPathBaseDir(): string
883    {
884        /**
885         * Based on {@link getBaseURL()}
886         */
887        global $conf;
888        if (!empty($conf['basedir'])) {
889            return $conf['basedir'];
890        }
891        $scriptName = LocalPath::createFromPathString($_SERVER['SCRIPT_NAME']);
892        if ($scriptName->getExtension() === 'php') {
893            return Url::toUrlSeparator($scriptName->getParent()->toAbsoluteId());
894        }
895        $phpSelf = LocalPath::createFromPathString($_SERVER['PHP_SELF']);
896        if ($phpSelf->getExtension() === "php") {
897            return Url::toUrlSeparator($scriptName->getParent()->toAbsoluteId());
898        }
899        if ($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']) {
900            $dir = preg_replace('/^' . preg_quote($_SERVER['DOCUMENT_ROOT'], '/') . '/', '',
901                $_SERVER['SCRIPT_FILENAME']);
902            return Url::toUrlSeparator(dirname('/' . $dir));
903        }
904        throw new ExceptionNotFound("No Base dir");
905
906    }
907
908    public static function getUrlRewrite(): string
909    {
910        global $conf;
911        $confKey = UrlRewrite::CONF_KEY;
912        $urlRewrite = $conf[$confKey];
913        try {
914            $urlRewriteInt = DataType::toInteger($urlRewrite);
915        } catch (ExceptionBadArgument $e) {
916            LogUtility::internalError("The ($confKey) configuration is not an integer ($urlRewrite)");
917            return UrlRewrite::NO_REWRITE;
918        }
919        switch ($urlRewriteInt) {
920            case UrlRewrite::NO_REWRITE_DOKU_VALUE:
921                return UrlRewrite::NO_REWRITE;
922            case UrlRewrite::WEB_SERVER_REWRITE_DOKU_VALUE:
923                return UrlRewrite::WEB_SERVER_REWRITE;
924            case UrlRewrite::DOKU_REWRITE_DOKU_VALUE:
925                return UrlRewrite::VALUE_DOKU_REWRITE;
926            default:
927                LogUtility::internalError("The ($confKey) configuration value ($urlRewriteInt) is not a valid value (0, 1 or 2). No rewrite");
928                return UrlRewrite::NO_REWRITE;
929        }
930    }
931
932    public static function getDefaultMediaLinking(): string
933    {
934        return SiteConfig::getConfValue(MediaMarkup::CONF_DEFAULT_LINKING, MediaMarkup::LINKING_DIRECT_VALUE);
935    }
936
937    public static function shouldEndpointUrlBeAbsolute(): bool
938    {
939        global $conf;
940        return $conf['canonical'] === 1;
941    }
942
943    public static function setEndpointToAbsoluteUrl()
944    {
945        global $conf;
946        $conf['canonical'] = 1;
947    }
948
949    public static function setEndpointToDefaultUrl()
950    {
951        global $conf;
952        $conf['canonical'] = 0;
953    }
954
955    public function getConfig(): SiteConfig
956    {
957        if (isset($this->config)) {
958            return $this->config;
959        }
960        $this->config = new SiteConfig($this->executingContext);
961        return $this->config;
962    }
963
964
965}
966