2222 */
2323namespace OCA \Theming \Service ;
2424
25+ use OCA \Theming \AppInfo \Application ;
2526use OCA \Theming \Themes \DefaultTheme ;
27+ use OCP \IConfig ;
2628use OCP \IURLGenerator ;
29+ use OCP \IUserSession ;
2730use OCP \Util ;
2831
2932class ThemeInjectionService {
3033
3134 private IURLGenerator $ urlGenerator ;
3235 private ThemesService $ themesService ;
3336 private DefaultTheme $ defaultTheme ;
37+ private IConfig $ config ;
38+ private ?string $ userId ;
3439
3540 public function __construct (IURLGenerator $ urlGenerator ,
3641 ThemesService $ themesService ,
37- DefaultTheme $ defaultTheme ) {
42+ DefaultTheme $ defaultTheme ,
43+ IConfig $ config ,
44+ IUserSession $ userSession ) {
3845 $ this ->urlGenerator = $ urlGenerator ;
3946 $ this ->themesService = $ themesService ;
4047 $ this ->defaultTheme = $ defaultTheme ;
48+ $ this ->config = $ config ;
49+ if ($ userSession ->getUser () !== null ) {
50+ $ this ->userId = $ userSession ->getUser ()->getUID ();
51+ } else {
52+ $ this ->userId = null ;
53+ }
4154 }
4255
4356 public function injectHeaders () {
@@ -50,13 +63,13 @@ public function injectHeaders() {
5063
5164 // Default theme fallback
5265 $ this ->addThemeHeader ($ defaultTheme ->getId ());
53-
66+
5467 // Themes applied by media queries
5568 foreach ($ mediaThemes as $ theme ) {
5669 $ this ->addThemeHeader ($ theme ->getId (), true , $ theme ->getMediaQuery ());
5770 }
5871
59- // Themes
72+ // Themes
6073 foreach ($ this ->themesService ->getThemes () as $ theme ) {
6174 // Ignore default theme as already processed first
6275 if ($ theme ->getId () === $ this ->defaultTheme ->getId ()) {
@@ -68,15 +81,24 @@ public function injectHeaders() {
6881
6982 /**
7083 * Inject theme header into rendered page
71- *
84+ *
7285 * @param string $themeId the theme ID
7386 * @param bool $plain request the :root syntax
7487 * @param string $media media query to use in the <link> element
7588 */
7689 private function addThemeHeader (string $ themeId , bool $ plain = true , string $ media = null ) {
90+ $ cacheBuster = $ this ->config ->getAppValue ('theming ' , 'cachebuster ' , '0 ' );
91+ if ($ this ->userId !== null ) {
92+ // need to bust the cache for the CSS file when the user background changed as its
93+ // URL is served in those files
94+ $ userCacheBuster = $ this ->config ->getUserValue ($ this ->userId , Application::APP_ID , 'userCacheBuster ' , '0 ' );
95+ $ cacheBuster .= $ this ->userId . '_ ' . $ userCacheBuster ;
96+ }
97+
7798 $ linkToCSS = $ this ->urlGenerator ->linkToRoute ('theming.Theming.getThemeStylesheet ' , [
7899 'themeId ' => $ themeId ,
79100 'plain ' => $ plain ,
101+ 'v ' => substr (sha1 ($ cacheBuster ), 0 , 8 ),
80102 ]);
81103 Util::addHeader ('link ' , [
82104 'rel ' => 'stylesheet ' ,
0 commit comments