Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Theme.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed6261334ccddee1084a315b39ef656a85c9a657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik;

use Piwik\Plugin\Manager;

/**
 * This class contains logic to make Themes work beautifully.
 *
 */
class Theme
{
    /** @var string  */
    private $themeName;

    /** @var \Piwik\Plugin  */
    private $theme;

    /**
     * @var Plugin $plugin
     */
    public function __construct($plugin = false)
    {
        $this->createThemeFromPlugin($plugin ? $plugin : Manager::getInstance()->getThemeEnabled());
    }

    /**
     * @param Plugin $plugin
     */
    private function createThemeFromPlugin($plugin)
    {
        $this->theme = $plugin;
        $this->themeName = $plugin->getPluginName();
    }

    public function getStylesheet()
    {
        if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) {
            return false;
        }

        $info = $this->theme->getInformation();
        if (!isset($info['stylesheet'])) {
            return false;
        }
        $themeStylesheet = 'plugins/' . $this->theme->getPluginName() . '/' . $info['stylesheet'];
        return $themeStylesheet;
    }

    public function getJavaScriptFiles()
    {
        if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) {
            return false;
        }

        $info = $this->theme->getInformation();
        if (empty($info['javascript'])) {
            return false;
        }
        $jsFiles = $info['javascript'];
        if (!is_array($jsFiles)) {
            $jsFiles = array($jsFiles);
        }
        foreach ($jsFiles as &$jsFile) {
            $jsFile = 'plugins/' . $this->theme->getPluginName() . '/' . $jsFile;
        }
        return $jsFiles;
    }

    public function rewriteAssetsPathToTheme($output)
    {
        if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) {
            return $output;
        }

        $pattern = array(
            // Rewriting scripts includes to overrides
            '~<script type=[\'"]text/javascript[\'"] (src)=[\'"]([^\'"]+)[\'"]>~',
            '~<script (src)=[\'"]([^\'"]+)[\'"] type=[\'"]text/javascript[\'"]>~',
            '~<link (rel)=[\'"]stylesheet[\'"] type=[\'"]text/css[\'"] href=[\'"]([^\'"]+)[\'"] ?/?>~',

            // Images as well
            '~(src|href)=[\'"]([^\'"]+)[\'"]~',

            // rewrite images in CSS files
            '~(url\()[\'"]([^\)]?[plugins]+[^\)]+[.jpg|png|gif|svg]?)[\'"][\)]~',

            // url(plugins/....)
            '~(url\()([^\)]?[plugins]+[^\)]+[.jpg|png|gif|svg]?)[\)]~',

            // rewrites images in JS files
            '~(=)[\s]?[\'"]([^\'"]+[.jpg|.png|.gif|svg]?)[\'"]~',
        );
        return preg_replace_callback($pattern, array($this, 'rewriteAssetPathIfOverridesFound'), $output);
    }

    private function rewriteAssetPathIfOverridesFound($src)
    {
        $source = $src[0];
        $pathAsset = $src[2];

        // Basic health check, we don't replace if not starting with plugins/
        $posPluginsInPath = strpos($pathAsset, 'plugins');
        if ($posPluginsInPath !== 0) {
            return $source;
        }

        // or if it's already rewritten
        if (strpos($pathAsset, $this->themeName) !== false) {
            return $source;
        }

        $pathPluginName = substr($pathAsset, strlen('plugins/'));
        $nextSlash = strpos($pathPluginName, '/');
        if ($nextSlash === false) {
            return $source;
        }
        $pathPluginName = substr($pathPluginName, 0, $nextSlash);

        // replace all plugin assets to the theme, if the theme overrides this asset
        // when there are name conflicts (two plugins define the same asset name in same folder),
        // we shall rename so there is no more conflict.
        $defaultThemePath = "plugins/" . $pathPluginName;
        $newThemePath = "plugins/" . $this->themeName;
        $overridingAsset = str_replace($defaultThemePath, $newThemePath, $pathAsset);

        // Strip trailing query string
        $fileToCheck = $overridingAsset;
        $queryStringPos = strpos($fileToCheck, '?');
        if ($queryStringPos !== false) {
            $fileToCheck = substr($fileToCheck, 0, $queryStringPos);
        }

        if (file_exists($fileToCheck)) {
            return str_replace($pathAsset, $overridingAsset, $source);
        }
        return $source;
    }

    /**
     * @return string
     */
    public function getThemeName()
    {
        return $this->themeName;
    }
}