API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
5 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
5 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
5 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
5 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
5 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
5 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Theme.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik; |
| 10 | |
| 11 | use Piwik\Plugin\Manager; |
| 12 | |
| 13 | /** |
| 14 | * This class contains logic to make Themes work beautifully. |
| 15 | * |
| 16 | */ |
| 17 | class Theme |
| 18 | { |
| 19 | /** @var string */ |
| 20 | private $themeName; |
| 21 | |
| 22 | /** @var \Piwik\Plugin */ |
| 23 | private $theme; |
| 24 | |
| 25 | /** |
| 26 | * @var Plugin $plugin |
| 27 | */ |
| 28 | public function __construct($plugin = false) |
| 29 | { |
| 30 | $this->createThemeFromPlugin($plugin ? $plugin : Manager::getInstance()->getThemeEnabled()); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param Plugin $plugin |
| 35 | */ |
| 36 | private function createThemeFromPlugin($plugin) |
| 37 | { |
| 38 | $this->theme = $plugin; |
| 39 | $this->themeName = $plugin->getPluginName(); |
| 40 | } |
| 41 | |
| 42 | public function getStylesheet() |
| 43 | { |
| 44 | if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | $info = $this->theme->getInformation(); |
| 49 | if (!isset($info['stylesheet'])) { |
| 50 | return false; |
| 51 | } |
| 52 | $themeStylesheet = 'plugins/' . $this->theme->getPluginName() . '/' . $info['stylesheet']; |
| 53 | return $themeStylesheet; |
| 54 | } |
| 55 | |
| 56 | public function getJavaScriptFiles() |
| 57 | { |
| 58 | if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | $info = $this->theme->getInformation(); |
| 63 | if (empty($info['javascript'])) { |
| 64 | return false; |
| 65 | } |
| 66 | $jsFiles = $info['javascript']; |
| 67 | if (!is_array($jsFiles)) { |
| 68 | $jsFiles = array($jsFiles); |
| 69 | } |
| 70 | foreach ($jsFiles as &$jsFile) { |
| 71 | $jsFile = 'plugins/' . $this->theme->getPluginName() . '/' . $jsFile; |
| 72 | } |
| 73 | return $jsFiles; |
| 74 | } |
| 75 | |
| 76 | public function rewriteAssetsPathToTheme($output) |
| 77 | { |
| 78 | if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME |
| 79 | && !Manager::getAlternativeWebRootDirectories()) { |
| 80 | return $output; |
| 81 | } |
| 82 | |
| 83 | $pattern = array( |
| 84 | // Rewriting scripts includes to overrides |
| 85 | '~<script type=[\'"]text/javascript[\'"] (src)=[\'"]([^\'"]+)[\'"]>~', |
| 86 | '~<script (src)=[\'"]([^\'"]+)[\'"] type=[\'"]text/javascript[\'"]>~', |
| 87 | '~<link (rel)=[\'"]stylesheet[\'"] type=[\'"]text/css[\'"] href=[\'"]([^\'"]+)[\'"] ?/?>~', |
| 88 | |
| 89 | // Images as well |
| 90 | '~(src|href)=[\'"]([^\'"]+)[\'"]~', |
| 91 | |
| 92 | // rewrite images in CSS files |
| 93 | '~(url\()[\'"]([^\)]?[plugins]+[^\)]+[.jpg|png|gif|svg]?)[\'"][\)]~', |
| 94 | |
| 95 | // url(plugins/....) |
| 96 | '~(url\()([^\)]?[plugins]+[^\)]+[.jpg|png|gif|svg]?)[\)]~', |
| 97 | |
| 98 | // rewrites images in JS files |
| 99 | '~(=)[\s]?[\'"]([^\'"]+[.jpg|.png|.gif|svg]?)[\'"]~', |
| 100 | ); |
| 101 | return preg_replace_callback($pattern, array($this, 'rewriteAssetPathIfOverridesFound'), $output); |
| 102 | } |
| 103 | |
| 104 | private function rewriteAssetPathIfOverridesFound($src) |
| 105 | { |
| 106 | $source = $src[0]; |
| 107 | $pathAsset = $src[2]; |
| 108 | |
| 109 | // Basic health check, we don't replace if not starting with plugins/ |
| 110 | $posPluginsInPath = strpos($pathAsset, 'plugins'); |
| 111 | if ($posPluginsInPath !== 0) { |
| 112 | return $source; |
| 113 | } |
| 114 | |
| 115 | // or if it's already rewritten |
| 116 | if (strpos($pathAsset, $this->themeName) !== false) { |
| 117 | return $source; |
| 118 | } |
| 119 | |
| 120 | $pathPluginName = substr($pathAsset, strlen('plugins/')); |
| 121 | $nextSlash = strpos($pathPluginName, '/'); |
| 122 | if ($nextSlash === false) { |
| 123 | return $source; |
| 124 | } |
| 125 | $pathPluginName = substr($pathPluginName, 0, $nextSlash); |
| 126 | |
| 127 | // replace all plugin assets to the theme, if the theme overrides this asset |
| 128 | // when there are name conflicts (two plugins define the same asset name in same folder), |
| 129 | // we shall rename so there is no more conflict. |
| 130 | $defaultThemePath = "plugins/" . $pathPluginName; |
| 131 | $newThemePath = "plugins/" . $this->themeName; |
| 132 | $overridingAsset = str_replace($defaultThemePath, $newThemePath, $pathAsset); |
| 133 | |
| 134 | // Strip trailing query string |
| 135 | $fileToCheck = $overridingAsset; |
| 136 | $queryStringPos = strpos($fileToCheck, '?'); |
| 137 | if ($queryStringPos !== false) { |
| 138 | $fileToCheck = substr($fileToCheck, 0, $queryStringPos); |
| 139 | } |
| 140 | |
| 141 | if (file_exists($fileToCheck)) { |
| 142 | return str_replace($pathAsset, $overridingAsset, $source); |
| 143 | } |
| 144 | |
| 145 | // not rewritten by theme, but may be located in custom webroot directory |
| 146 | foreach (Manager::getAlternativeWebRootDirectories() as $absDir => $webRootDirectory) { |
| 147 | $withoutPlugins = str_replace('plugins/', '', $pathAsset); |
| 148 | if (file_exists($absDir . '/' . $withoutPlugins)) { |
| 149 | return str_replace($pathAsset, $webRootDirectory . $withoutPlugins, $source); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return $source; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return string |
| 158 | */ |
| 159 | public function getThemeName() |
| 160 | { |
| 161 | return $this->themeName; |
| 162 | } |
| 163 | } |
| 164 |