]> git.mxchange.org Git - friendica.git/blob - view/theme/vier/style.php
Improve expectation for not modified check in theme/vier/style
[friendica.git] / view / theme / vier / style.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\Core\Logger;
23 use Friendica\DI;
24
25 $uid = $_REQUEST['puid'] ?? 0;
26
27 $style = DI::pConfig()->get($uid, 'vier', 'style');
28
29 if (empty($style)) {
30         $style = DI::config()->get('vier', 'style');
31 }
32
33 if (empty($style)) {
34         $style = "plus";
35 }
36
37 $stylecss = '';
38 $modified = '';
39
40 $style = \Friendica\Util\Strings::sanitizeFilePathItem($style);
41
42 foreach (['style', $style] as $file) {
43         $stylecssfile = $THEMEPATH . DIRECTORY_SEPARATOR . $file .'.css';
44         if (file_exists($stylecssfile)) {
45                 $stylecss .= file_get_contents($stylecssfile);
46                 $stylemodified = filemtime($stylecssfile);
47                 if ($stylemodified > $modified) {
48                         $modified = $stylemodified;
49                 }
50         } else {
51                 //TODO: use Logger::ERROR?
52                 Logger::log('Error: missing file: "' . $stylecssfile .'" (userid: '. $uid .')');
53         }
54 }
55 $modified = gmdate('r', $modified);
56
57 $etag = md5($stylecss);
58
59 // Only send the CSS file if it was changed
60 header('Cache-Control: public');
61 header('ETag: "'.$etag.'"');
62 header('Last-Modified: '.$modified);
63
64 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
65         $cached_modified = gmdate('r', strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']));
66         $cached_etag = str_replace(['"', "-gzip"], ['', ''],
67                                 stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
68
69         if (($cached_modified == $modified) && ($cached_etag == $etag)) {
70                 header('HTTP/1.1 304 Not Modified');
71                 exit();
72         }
73 }
74 echo $stylecss;