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