1717use Contao \CoreBundle \Exception \InsufficientAuthenticationException ;
1818use Contao \CoreBundle \Exception \PageNotFoundException ;
1919use Contao \CoreBundle \Framework \ContaoFramework ;
20+ use Contao \CoreBundle \Security \Authentication \Token \TokenChecker ;
21+ use Contao \Date ;
2022use Contao \Dbafs ;
2123use Contao \FilesModel ;
2224use Contao \FrontendUser ;
@@ -35,14 +37,16 @@ class FilesController
3537 protected $ framework ;
3638 protected $ security ;
3739 protected $ db ;
40+ protected $ tokenChecker ;
3841
39- public function __construct (string $ rootDir , Session $ session , ContaoFramework $ framework , Security $ security , Connection $ db )
42+ public function __construct (string $ rootDir , Session $ session , ContaoFramework $ framework , Security $ security , Connection $ db, TokenChecker $ tokenChecker )
4043 {
4144 $ this ->rootDir = $ rootDir ;
4245 $ this ->session = $ session ;
4346 $ this ->framework = $ framework ;
4447 $ this ->security = $ security ;
4548 $ this ->db = $ db ;
49+ $ this ->tokenChecker = $ tokenChecker ;
4650 }
4751
4852 public function fileAction (Request $ request , string $ file ): BinaryFileResponse
@@ -56,9 +60,9 @@ public function fileAction(Request $request, string $file): BinaryFileResponse
5660
5761 // Initialize the Contao framework
5862 $ this ->framework ->initialize (true );
59-
63+ FilesModel:: findById ( null );
6064 // Set the root page for the domain as the pageModel attribute
61- $ root = PageModel:: findFirstPublishedRootByHostAndLanguage ($ request ->getHost (), $ request ->getLocale ());
65+ $ root = $ this -> findFirstPublishedRootByHostAndLanguage ($ request ->getHost (), $ request ->getLocale ());
6266
6367 if (null !== $ root ) {
6468 $ request ->attributes ->set ('pageModel ' , $ root );
@@ -117,7 +121,7 @@ public function fileAction(Request $request, string $file): BinaryFileResponse
117121 }
118122
119123 // Get the parent folder
120- $ filesModel = FilesModel::findById ($ filesModel ->pid );
124+ $ filesModel = $ filesModel -> pid ? FilesModel::findById ($ filesModel ->pid ) : null ;
121125 } while (null !== $ filesModel );
122126
123127 // Throw 404 exception, if there were no user homes or folders with member groups
@@ -145,4 +149,18 @@ public function fileAction(Request $request, string $file): BinaryFileResponse
145149 // Return file to browser
146150 return new BinaryFileResponse (Path::join ($ this ->rootDir , $ file ));
147151 }
152+
153+ protected function findFirstPublishedRootByHostAndLanguage (string $ host , string $ language ): ?PageModel
154+ {
155+ $ columns = ["type='root' AND (dns=? OR dns='') AND (language=? OR fallback='1') " ];
156+ $ values = [$ host , $ language ];
157+ $ options = ['order ' => 'dns DESC, fallback ' ];
158+
159+ if (!$ this ->tokenChecker ->isPreviewMode ()) {
160+ $ time = Date::floorToMinute ();
161+ $ columns [] = "published='1' AND (start='' OR start<=' $ time') AND (stop='' OR stop>' $ time') " ;
162+ }
163+
164+ return PageModel::findOneBy ($ columns , $ values , $ options );
165+ }
148166}
0 commit comments