skins.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2006 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This file implements the skin selection code for PmWiki. Skin
  8. selection is controlled by the $Skin variable, which can also
  9. be an array (in which case the first skin found is loaded).
  10. In addition, $ActionSkin[$action] specifies other skins to be
  11. searched based on the current action.
  12. */
  13. SDV($Skin, 'pmwiki');
  14. SDV($ActionSkin['print'], 'print');
  15. SDV($FarmPubDirUrl, $PubDirUrl);
  16. SDV($PageLogoUrl, "$FarmPubDirUrl/skins/pmwiki/pmwiki-32.gif");
  17. SDVA($TmplDisplay, array('PageEditFmt' => 0));
  18. # $PageTemplateFmt is deprecated
  19. if (isset($PageTemplateFmt)) LoadPageTemplate($pagename,$PageTemplateFmt);
  20. else {
  21. $x = array_merge((array)@$ActionSkin[$action], (array)$Skin);
  22. SetSkin($pagename, $x);
  23. }
  24. SDV($PageCSSListFmt,array(
  25. 'pub/css/local.css' => '$PubDirUrl/css/local.css',
  26. 'pub/css/{$Group}.css' => '$PubDirUrl/css/{$Group}.css',
  27. 'pub/css/{$FullName}.css' => '$PubDirUrl/css/{$FullName}.css'));
  28. foreach((array)$PageCSSListFmt as $k=>$v)
  29. if (file_exists(FmtPageName($k,$pagename)))
  30. $HTMLHeaderFmt[] = "<link rel='stylesheet' type='text/css' href='$v' />\n";
  31. # SetSkin changes the current skin to the first available skin from
  32. # the $skin array.
  33. function SetSkin($pagename, $skin) {
  34. global $Skin, $SkinLibDirs, $SkinDir, $SkinDirUrl,
  35. $IsTemplateLoaded, $PubDirUrl, $FarmPubDirUrl, $FarmD;
  36. SDV($SkinLibDirs, array(
  37. "./pub/skins/\$Skin" => "$PubDirUrl/skins/\$Skin",
  38. "$FarmD/pub/skins/\$Skin" => "$FarmPubDirUrl/skins/\$Skin"));
  39. foreach((array)$skin as $sfmt) {
  40. $Skin = FmtPageName($sfmt, $pagename);
  41. foreach($SkinLibDirs as $dirfmt => $urlfmt) {
  42. $SkinDir = FmtPageName($dirfmt, $pagename);
  43. if (is_dir($SkinDir))
  44. { $SkinDirUrl = FmtPageName($urlfmt, $pagename); break 2; }
  45. }
  46. }
  47. if (!is_dir($SkinDir)) {
  48. unset($Skin);
  49. Abort("?unable to find skin from list ".implode(' ',(array)$skin));
  50. }
  51. $IsTemplateLoaded = 0;
  52. if (file_exists("$SkinDir/$Skin.php"))
  53. include_once("$SkinDir/$Skin.php");
  54. else if (file_exists("$SkinDir/skin.php"))
  55. include_once("$SkinDir/skin.php");
  56. if ($IsTemplateLoaded) return;
  57. if (file_exists("$SkinDir/$Skin.tmpl"))
  58. LoadPageTemplate($pagename, "$SkinDir/$Skin.tmpl");
  59. else if (file_exists("$SkinDir/skin.tmpl"))
  60. LoadPageTemplate($pagename, "$SkinDir/skin.tmpl");
  61. else if (($dh = opendir($SkinDir))) {
  62. while (($fname = readdir($dh)) !== false) {
  63. if (substr($fname, -5) != '.tmpl') continue;
  64. if ($IsTemplateLoaded)
  65. Abort("?unable to find unique template in $SkinDir");
  66. LoadPageTemplate($pagename, "$SkinDir/$fname");
  67. }
  68. closedir($dh);
  69. }
  70. if (!$IsTemplateLoaded) Abort("Unable to load $Skin template");
  71. }
  72. # LoadPageTemplate loads a template into $TmplFmt
  73. function LoadPageTemplate($pagename,$tfilefmt) {
  74. global $PageStartFmt, $PageEndFmt, $HTMLHeaderFmt, $HTMLFooterFmt,
  75. $IsTemplateLoaded, $TmplFmt, $TmplDisplay,
  76. $PageTextStartFmt, $PageTextEndFmt;
  77. # $BasicLayoutVars is deprecated
  78. global $BasicLayoutVars;
  79. if (isset($BasicLayoutVars))
  80. foreach($BasicLayoutVars as $sw) $TmplDisplay[$sw] = 1;
  81. SDV($PageTextStartFmt, "\n<div id='wikitext'>\n");
  82. SDV($PageTextEndFmt, "</div>\n");
  83. $sddef = array('PageEditFmt' => 0);
  84. $k = implode('',file(FmtPageName($tfilefmt,$pagename)));
  85. $sect = preg_split(
  86. '#[[<]!--(/?(?:Page[A-Za-z]+Fmt|(?:HT|X)ML(?:Head|Foot)er|HeaderText|PageText).*?)--[]>]#',
  87. $k, 0, PREG_SPLIT_DELIM_CAPTURE);
  88. $TmplFmt['Start'] = array_merge(array('headers:'),
  89. preg_split('/[[<]!--((?:wiki|file|function|markup):.*?)--[]>]/s',
  90. array_shift($sect),0,PREG_SPLIT_DELIM_CAPTURE));
  91. $TmplFmt['End'] = array($PageTextEndFmt);
  92. $ps = 'Start';
  93. while (count($sect)>0) {
  94. $k = array_shift($sect);
  95. $v = preg_split('/[[<]!--((?:wiki|file|function|markup):.*?)--[]>]/',
  96. array_shift($sect),0,PREG_SPLIT_DELIM_CAPTURE);
  97. $TmplFmt[$ps][] = "<!--$k-->";
  98. if ($k{0} == '/')
  99. { $TmplFmt[$ps][] = (count($v) > 1) ? $v : $v[0]; continue; }
  100. @list($var, $sd) = explode(' ', $k, 2);
  101. $GLOBALS[$var] = (count($v) > 1) ? $v : $v[0];
  102. if ($sd > '') $sddef[$var] = $sd;
  103. if ($var == 'PageText') { $ps = 'End'; }
  104. if ($var == 'HTMLHeader' || $var == 'XMLHeader')
  105. $TmplFmt[$ps][] = &$HTMLHeaderFmt;
  106. if ($var == 'HTMLFooter' || $var == 'XMLFooter')
  107. $TmplFmt[$ps][] = &$HTMLFooterFmt;
  108. ## <!--HeaderText--> deprecated, 2.1.16
  109. if ($var == 'HeaderText') { $TmplFmt[$ps][] = &$HTMLHeaderFmt; }
  110. $TmplFmt[$ps][$var] =& $GLOBALS[$var];
  111. }
  112. array_push($TmplFmt['Start'], $PageTextStartFmt);
  113. $PageStartFmt = 'function:PrintSkin Start';
  114. $PageEndFmt = 'function:PrintSkin End';
  115. $IsTemplateLoaded = 1;
  116. SDVA($TmplDisplay, $sddef);
  117. }
  118. # This function is called to print a portion of the skin template
  119. # according to the settings in $TmplDisplay.
  120. function PrintSkin($pagename, $arg) {
  121. global $TmplFmt, $TmplDisplay;
  122. foreach ($TmplFmt[$arg] as $k => $v)
  123. if (!isset($TmplDisplay[$k]) || $TmplDisplay[$k])
  124. PrintFmt($pagename, $v);
  125. }