Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Theme.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Piwik\Plugin\Manager;
  11. /**
  12. * This class contains logic to make Themes work beautifully.
  13. *
  14. */
  15. class Theme
  16. {
  17. /** @var string */
  18. private $themeName;
  19. /** @var \Piwik\Plugin */
  20. private $theme;
  21. /**
  22. * @var Plugin $plugin
  23. */
  24. public function __construct($plugin = false)
  25. {
  26. $this->createThemeFromPlugin($plugin ? $plugin : Manager::getInstance()->getThemeEnabled());
  27. }
  28. /**
  29. * @param Plugin $plugin
  30. */
  31. private function createThemeFromPlugin($plugin)
  32. {
  33. $this->theme = $plugin;
  34. $this->themeName = $plugin->getPluginName();
  35. }
  36. public function getStylesheet()
  37. {
  38. if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) {
  39. return false;
  40. }
  41. $info = $this->theme->getInformation();
  42. if (!isset($info['stylesheet'])) {
  43. return false;
  44. }
  45. $themeStylesheet = 'plugins/' . $this->theme->getPluginName() . '/' . $info['stylesheet'];
  46. return $themeStylesheet;
  47. }
  48. public function getJavaScriptFiles()
  49. {
  50. if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) {
  51. return false;
  52. }
  53. $info = $this->theme->getInformation();
  54. if (empty($info['javascript'])) {
  55. return false;
  56. }
  57. $jsFiles = $info['javascript'];
  58. if(!is_array($jsFiles)) {
  59. $jsFiles = array($jsFiles);
  60. }
  61. foreach($jsFiles as &$jsFile) {
  62. $jsFile = 'plugins/' . $this->theme->getPluginName() . '/' . $jsFile;
  63. }
  64. return $jsFiles;
  65. }
  66. public function rewriteAssetsPathToTheme($output)
  67. {
  68. if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) {
  69. return $output;
  70. }
  71. $pattern = array(
  72. // Rewriting scripts includes to overrides
  73. '~<script type=[\'"]text/javascript[\'"] (src)=[\'"]([^\'"]+)[\'"]>~',
  74. '~<script (src)=[\'"]([^\'"]+)[\'"] type=[\'"]text/javascript[\'"]>~',
  75. '~<link (rel)=[\'"]stylesheet[\'"] type=[\'"]text/css[\'"] href=[\'"]([^\'"]+)[\'"] ?/?>~',
  76. // Images as well
  77. '~(src|href)=[\'"]([^\'"]+)[\'"]~',
  78. // rewrite images in CSS files
  79. '~(url\()[\'"]([^\)]?[plugins]+[^\)]+[.jpg|png|gif|svg]?)[\'"][\)]~',
  80. // url(plugins/....)
  81. '~(url\()([^\)]?[plugins]+[^\)]+[.jpg|png|gif|svg]?)[\)]~',
  82. // rewrites images in JS files
  83. '~(=)[\s]?[\'"]([^\'"]+[.jpg|.png|.gif|svg]?)[\'"]~',
  84. );
  85. return preg_replace_callback($pattern, array($this,'rewriteAssetPathIfOverridesFound'), $output);
  86. }
  87. private function rewriteAssetPathIfOverridesFound($src)
  88. {
  89. $source = $src[0];
  90. $pathAsset = $src[2];
  91. // Basic health check, we dont replace if not starting with plugins/
  92. $posPluginsInPath = strpos($pathAsset, 'plugins');
  93. if( $posPluginsInPath !== 0) {
  94. return $source;
  95. }
  96. // or if it's already rewritten
  97. if(strpos($pathAsset, $this->themeName) !== false) {
  98. return $source;
  99. }
  100. $pathPluginName = substr($pathAsset, strlen('plugins/'));
  101. $nextSlash = strpos($pathPluginName, '/');
  102. if($nextSlash === false) {
  103. return $source;
  104. }
  105. $pathPluginName = substr($pathPluginName, 0, $nextSlash);
  106. // replace all plugin assets to the theme, if the theme overrides this asset
  107. // when there are name conflicts (two plugins define the same asset name in same folder),
  108. // we shall rename so there is no more conflict.
  109. $defaultThemePath = "plugins/" . $pathPluginName;
  110. $newThemePath = "plugins/" . $this->themeName;
  111. $overridingAsset = str_replace($defaultThemePath, $newThemePath, $pathAsset);
  112. // Strip trailing query string
  113. $fileToCheck = $overridingAsset;
  114. $queryStringPos = strpos($fileToCheck, '?');
  115. if( $queryStringPos !== false) {
  116. $fileToCheck = substr($fileToCheck, 0, $queryStringPos);
  117. }
  118. if(file_exists($fileToCheck)) {
  119. return str_replace($pathAsset, $overridingAsset, $source);
  120. }
  121. return $source;
  122. }
  123. /**
  124. * @return string
  125. */
  126. public function getThemeName()
  127. {
  128. return $this->themeName;
  129. }
  130. }