Completed
Push — master ( 89223c...9f8254 )
by Andreas
51:18 queued 05:30
created

midcom_services_cache_module_content::_check_hit()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 59
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 15.123

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 34
c 1
b 0
f 0
nc 17
nop 1
dl 0
loc 59
ccs 22
cts 35
cp 0.6286
crap 15.123
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\BinaryFileResponse;
12
use Symfony\Component\HttpKernel\Event\ResponseEvent;
13
use Symfony\Component\HttpKernel\Event\RequestEvent;
14
use Doctrine\Common\Cache\CacheProvider;
15
16
/**
17
 * This is the Output Caching Engine of MidCOM. It will intercept page output,
18
 * map it using the currently used URL and use the cached output on subsequent
19
 * requests.
20
 *
21
 * <b>Important note for application developers</b>
22
 *
23
 * Please read the documentation of the following functions thoroughly:
24
 *
25
 * - midcom_services_cache_module_content::no_cache();
26
 * - midcom_services_cache_module_content::uncached();
27
 * - midcom_services_cache_module_content::expires();
28
 * - midcom_services_cache_module_content::invalidate_all();
29
 * - midcom_services_cache_module_content::content_type();
30
 * - midcom_services_cache_module_content::enable_live_mode();
31
 *
32
 * You have to use these functions everywhere where it is applicable or the cache
33
 * will not work reliably.
34
 *
35
 * <b>Caching strategy</b>
36
 *
37
 * The cache takes three parameters into account when storing in or retrieving from
38
 * the cache: The current User ID, the current language and the request's URL.
39
 *
40
 * Only on a complete match a cached page is displayed, which should take care of any
41
 * permission checks done on the page. When you change the permissions of users, you
42
 * need to manually invalidate the cache though, as MidCOM currently cannot detect
43
 * changes like this (of course, this is true if and only if you are not using a
44
 * MidCOM to change permissions).
45
 *
46
 * When the HTTP request is not cacheable, the caching engine will automatically and
47
 * transparently go into no_cache mode for that request only. This feature
48
 * does neither invalidate the cache or drop the page that would have been delivered
49
 * normally from the cache. If you change the content, you need to do that yourself.
50
 *
51
 * HTTP 304 Not Modified support is built into this module, and will send a 304 reply if applicable.
52
 *
53
 * <b>Module configuration (see also midcom_config)</b>
54
 *
55
 * - <i>string cache_module_content_name</i>: The name of the cache database to use. This should usually be tied to the actual
56
 *   MidCOM site to have exactly one cache per site. This is mandatory (and populated by a sensible default
57
 *   by midcom_config, see there for details).
58
 * - <i>boolean cache_module_content_uncached</i>: Set this to true to prevent the saving of cached pages. This is useful
59
 *   for development work, as all other headers (like E-Tag or Last-Modified) are generated
60
 *   normally. See the uncached() and _uncached members.
61
 *
62
 * @package midcom.services
63
 */
64
class midcom_services_cache_module_content extends midcom_services_cache_module
65
{
66
    /**
67
     * Flag, indicating whether the current page may be cached. If
68
     * false, the usual no-cache headers will be generated.
69
     *
70
     * @var boolean
71
     */
72
    private $_no_cache = false;
73
74
    /**
75
     * Page expiration in seconds. If null (unset), the page does
76
     * not expire.
77
     *
78
     * @var int
79
     */
80
    private $_expires;
81
82
    /**
83
     * An array storing all HTTP headers registered through register_sent_header().
84
     * They will be sent when a cached page is delivered.
85
     *
86
     * @var array
87
     */
88
    private $_sent_headers = [];
89
90
    /**
91
     * Set this to true if you want to inhibit storage of the generated pages in
92
     * the cache database. All other headers will be created as usual though, so
93
     * 304 processing will kick in for example.
94
     *
95
     * @var boolean
96
     */
97
    private $_uncached = false;
98
99
    /**
100
     * Controls cache headers strategy
101
     * 'no-cache' activates no-cache mode that actively tries to circumvent all caching
102
     * 'revalidate' is the default which sets must-revalidate. Expiry defaults to current time, so this effectively behaves like no-cache if expires() was not called
103
     * 'public' and 'private' enable caching with the cache-control header of the same name, default expiry timestamps are generated using the default_lifetime
104
     *
105
     * @var string
106
     */
107
    private $_headers_strategy = 'revalidate';
108
109
    /**
110
     * Controls cache headers strategy for authenticated users, needed because some proxies store cookies, too,
111
     * making a horrible mess when used by mix of authenticated and non-authenticated users
112
     *
113
     * @see $_headers_strategy
114
     * @var string
115
     */
116
    private $_headers_strategy_authenticated = 'private';
117
118
    /**
119
     * Default lifetime of page for public/private headers strategy
120
     * When generating the default expires header this is added to time().
121
     *
122
     * @var int
123
     */
124
    private $_default_lifetime = 0;
125
126
    /**
127
     * Default lifetime of page for public/private headers strategy for authenticated users
128
     *
129
     * @see $_default_lifetime
130
     * @var int
131
     */
132
    private $_default_lifetime_authenticated = 0;
133
134
    /**
135
     * A cache backend used to store the actual cached pages.
136
     *
137
     * @var Doctrine\Common\Cache\CacheProvider
138
     */
139
    private $_data_cache;
140
141
    /**
142
     * GUIDs loaded per context in this request
143
     */
144
    private $context_guids = [];
145
146
    /**
147
     * @var midcom_config
148
     */
149
    private $config;
150
151
    /**
152
     * Initialize the cache.
153
     *
154
     * The first step is to initialize the cache backends. The names of the
155
     * cache backends used for meta and data storage are derived from the name
156
     * defined for this module (see the 'name' configuration parameter above).
157
     * The name is used directly for the meta data cache, while the actual data
158
     * is stored in a backend postfixed with '_data'.
159
     *
160
     * After core initialization, the module checks for a cache hit (which might
161
     * trigger the delivery of the cached page and exit) and start the output buffer
162
     * afterwards.
163
     */
164 1
    public function __construct(midcom_config $config, CacheProvider $backend, CacheProvider $data_cache)
165
    {
166 1
        parent::__construct($backend);
167 1
        $this->config = $config;
168 1
        $this->_data_cache = $data_cache;
169 1
        $this->_data_cache->setNamespace($backend->getNamespace());
170
171 1
        $this->_uncached = $config->get('cache_module_content_uncached');
172 1
        $this->_headers_strategy = $this->get_strategy('cache_module_content_headers_strategy');
173 1
        $this->_headers_strategy_authenticated = $this->get_strategy('cache_module_content_headers_strategy_authenticated');
174 1
        $this->_default_lifetime = (int)$config->get('cache_module_content_default_lifetime');
175 1
        $this->_default_lifetime_authenticated = (int)$config->get('cache_module_content_default_lifetime_authenticated');
176
177 1
        if ($this->_headers_strategy == 'no-cache') {
178
            // we can't call no_cache() here, because it would try to call back to this class via the global getter
179
            $header = 'Cache-Control: no-store, no-cache, must-revalidate';
180
            $this->register_sent_header($header);
181
            midcom_compat_environment::get()->header($header);
182
            $this->_no_cache = true;
183
        }
184 1
    }
185
186
    /**
187
     * @param RequestEvent $event
188
     */
189 338
    public function on_request(RequestEvent $event)
190
    {
191 338
        if ($event->isMasterRequest()) {
192 1
            $request = $event->getRequest();
193
            /* Load and start up the cache system, this might already end the request
194
             * on a content cache hit. Note that the cache check hit depends on the i18n and auth code.
195
             */
196 1
            if ($response = $this->_check_hit($request)) {
197 1
                $event->setResponse($response);
198
            }
199
        }
200 338
    }
201
202
    /**
203
     * This function holds the cache hit check mechanism. It searches the requested
204
     * URL in the cache database. If found, it checks, whether the cache page has
205
     * expired. If not, the response is returned. In all other cases this method simply
206
     * returns void.
207
     *
208
     * The midcom-cache URL methods are handled before checking for a cache hit.
209
     *
210
     * Also, any HTTP POST request will automatically circumvent the cache so that
211
     * any component can process the request. It will set no_cache automatically
212
     * to avoid any cache pages being overwritten by, for example, search results.
213
     *
214
     * Note, that HTTP GET is <b>not</b> checked this way, as GET requests can be
215
     * safely distinguished by their URL.
216
     *
217
     * @param Request $request The request object
218
     * @return void|Response
219
     */
220 1
    private function _check_hit(Request $request)
221
    {
222 1
        foreach (midcom_connection::get_url('argv') as $arg) {
223 1
            if (in_array($arg, ["midcom-cache-invalidate", "midcom-cache-nocache"])) {
224
                // Don't cache these.
225
                debug_add("uncached: $arg");
226
                return;
227
            }
228
        }
229
230 1
        if (!$request->isMethodCacheable()) {
231
            debug_add('Request method is not cacheable, setting no_cache');
232
            $this->no_cache();
233
            return;
234
        }
235
236
        // Check for uncached operation
237 1
        if ($this->_uncached) {
238
            debug_add("Uncached mode");
239
            return;
240
        }
241
242
        // Check that we have cache for the identifier
243 1
        $request_id = $this->generate_request_identifier($request);
244
        // Load metadata for the content identifier connected to current request
245 1
        $content_id = $this->backend->fetch($request_id);
246 1
        if ($content_id === false) {
247 1
            debug_add("MISS {$request_id}");
248
            // We have no information about content cached for this request
249 1
            return;
250
        }
251 1
        debug_add("HIT {$request_id}");
252
253 1
        $data = $this->backend->fetch($content_id);
254 1
        if ($data === false) {
255
            debug_add("MISS meta_cache {$content_id}");
256
            // Content cache data is missing
257
            return;
258
        }
259
260 1
        if (!isset($data['last-modified'])) {
261
            debug_add('Current page is in cache, but has insufficient information', MIDCOM_LOG_INFO);
262
            return;
263
        }
264
265 1
        debug_add("HIT {$content_id}");
266
267 1
        $response = new Response('', Response::HTTP_OK, $data);
268 1
        if (!$response->isNotModified($request)) {
269 1
            $content = $this->_data_cache->fetch($content_id);
270 1
            if ($content === false) {
271
                debug_add("Current page is in not in the data cache, possible ghost read.", MIDCOM_LOG_WARN);
272
                return;
273
            }
274 1
            $response->setContent($content);
275
        }
276
        // disable cache writing in on_response
277 1
        $this->_no_cache = true;
278 1
        return $response;
279
    }
280
281
    /**
282
     * This completes the output caching, post-processes it and updates the cache databases accordingly.
283
     *
284
     * The first step is to check against _no_cache pages, which will be delivered immediately
285
     * without any further post processing. Afterwards, the system will complete the sent
286
     * headers by adding all missing headers. Note, that E-Tag will be generated always
287
     * automatically, you must not set this in your component.
288
     *
289
     * If the midcom configuration option cache_uncached is set or the corresponding runtime function
290
     * has been called, the cache file will not be written, but the header stuff will be added like
291
     * usual to allow for browser-side caching.
292
     *
293
     * @param ResponseEvent $event The request object
294
     */
295 339
    public function on_response(ResponseEvent $event)
296
    {
297 339
        if (!$event->isMasterRequest()) {
298 338
            return;
299
        }
300 1
        $response = $event->getResponse();
301 1
        if ($response instanceof BinaryFileResponse) {
302
            return;
303
        }
304 1
        foreach ($this->_sent_headers as $header => $value) {
305
            // This can happen in streamed responses which enable_live_mode
306
            if (!headers_sent()) {
307
                header_remove($header);
308
            }
309
            $response->headers->set($header, $value);
310
        }
311 1
        $request = $event->getRequest();
312 1
        if ($this->_no_cache) {
313
            $response->prepare($request);
314
            return;
315
        }
316
317 1
        $cache_data = $response->getContent();
318
319
        // Register additional Headers around the current output request
320 1
        $this->complete_sent_headers($response);
321 1
        $response->prepare($request);
322
323
        // Generate E-Tag header.
324 1
        if (empty($cache_data)) {
325
            $etag = md5(serialize($response->headers->all()));
326
        } else {
327 1
            $etag = md5($cache_data);
328
        }
329 1
        $response->setEtag($etag);
330
331 1
        if ($this->_uncached) {
332
            debug_add('Not writing cache file, we are in uncached operation mode.');
333
            return;
334
        }
335 1
        $content_id = 'C-' . $etag;
336 1
        $this->write_meta_cache($content_id, $request, $response);
337 1
        $this->_data_cache->save($content_id, $cache_data);
338 1
    }
339
340
    /**
341
     * Generate a valid cache identifier for a context of the current request
342
     */
343 1
    private function generate_request_identifier(Request $request) : string
344
    {
345 1
        $context = $request->attributes->get('context')->id;
346
        // Cache the request identifier so that it doesn't change between start and end of request
347 1
        static $identifier_cache = [];
348 1
        if (isset($identifier_cache[$context])) {
349 1
            return $identifier_cache[$context];
350
        }
351
352 1
        $module_name = $this->config->get('cache_module_content_name');
353 1
        if ($module_name == 'auto') {
354 1
            $module_name = midcom_connection::get_unique_host_name();
355
        }
356 1
        $identifier_source = 'CACHE:' . $module_name;
357
358 1
        $cache_strategy = $this->config->get('cache_module_content_caching_strategy');
359
360 1
        switch ($cache_strategy) {
361 1
            case 'memberships':
362
                if (!midcom_connection::get_user()) {
363
                    $identifier_source .= ';USER=ANONYMOUS';
364
                    break;
365
                }
366
                $mc = new midgard_collector('midgard_member', 'uid', midcom_connection::get_user());
367
                $mc->set_key_property('gid');
368
                $mc->execute();
369
                $gids = $mc->list_keys();
370
                $identifier_source .= ';GROUPS=' . implode(',', array_keys($gids));
371
                break;
372 1
            case 'public':
373
                $identifier_source .= ';USER=EVERYONE';
374
                break;
375 1
            case 'user':
376
            default:
377 1
                $identifier_source .= ';USER=' . midcom_connection::get_user();
378 1
                break;
379
        }
380
381 1
        $identifier_source .= ';URL=' . $request->getRequestUri();
382 1
        debug_add("Generating context {$context} request-identifier from: {$identifier_source}");
383
384 1
        $identifier_cache[$context] = 'R-' . md5($identifier_source);
385 1
        return $identifier_cache[$context];
386
    }
387
388 1
    private function get_strategy(string $name) : string
389
    {
390 1
        $strategy = strtolower($this->config->get($name));
391 1
        $allowed = ['no-cache', 'revalidate', 'public', 'private'];
392 1
        if (!in_array($strategy, $allowed)) {
393
            throw new midcom_error($name . ' is not valid, try ' . implode(', ', $allowed));
394
        }
395 1
        return $strategy;
396
    }
397
398
    /**
399
     * Call this, if the currently processed output must not be cached for any
400
     * reason. Dynamic pages with sensitive content are a candidate for this
401
     * function.
402
     *
403
     * Note, that this will prevent <i>any</i> content invalidation related headers
404
     * like E-Tag to be generated automatically, and that the appropriate
405
     * no-store/no-cache headers from HTTP 1.1 and HTTP 1.0 will be sent automatically.
406
     * This means that there will also be no 304 processing.
407
     *
408
     * You should use this only for sensitive content. For simple dynamic output,
409
     * you are strongly encouraged to use the less strict uncached() function.
410
     *
411
     * @see uncached()
412
     */
413 191
    public function no_cache(Response $response = null)
414
    {
415 191
        $settings = 'no-store, no-cache, must-revalidate';
416
        // PONDER: Send expires header (set to long time in past) as well ??
417
418 191
        if ($response) {
419
            $response->headers->set('Cache-Control', $settings);
420 191
        } elseif (!$this->_no_cache) {
421
            if (headers_sent()) {
422
                debug_add('Warning, we should move to no_cache but headers have already been sent, skipping header transmission.', MIDCOM_LOG_ERROR);
423
            } else {
424
                midcom::get()->header('Cache-Control: ' . $settings);
425
            }
426
        }
427 191
        $this->_no_cache = true;
428 191
    }
429
430
    /**
431
     * Call this, if the currently processed output must not be cached for any
432
     * reason. Dynamic pages or form processing results are the usual candidates
433
     * for this mode.
434
     *
435
     * Note, that this will still keep the caching engine active so that it can
436
     * add the usual headers (ETag, Expires ...) in respect to the no_cache flag.
437
     * As well, at the end of the processing, the usual 304 checks are done, so if
438
     * your page doesn't change in respect of E-Tag and Last-Modified, only a 304
439
     * Not Modified reaches the client.
440
     *
441
     * Essentially, no_cache behaves the same way as if the uncached configuration
442
     * directive is set to true, it is just limited to a single request.
443
     *
444
     * If you need a higher level of client side security, to avoid storage of sensitive
445
     * information on the client side, you should use no_cache instead.
446
     *
447
     * @see no_cache()
448
     */
449 3
    public function uncached(bool $uncached = true)
450
    {
451 3
        $this->_uncached = $uncached;
452 3
    }
453
454
    /**
455
     * Sets the expiration time of the current page (Unix (GMT) Timestamp).
456
     *
457
     * <b>Note:</B> This generate error call will add browser-side cache control
458
     * headers as well to force a browser to revalidate a page after the set
459
     * expiry.
460
     *
461
     * You should call this at all places where you have timed content in your
462
     * output, so that the page will be regenerated once a certain article has
463
     * expired.
464
     *
465
     * Multiple calls to expires will only save the
466
     * "youngest" timestamp, so you can safely call expires where appropriate
467
     * without respect to other values.
468
     *
469
     * The cache's default (null) will disable the expires header. Note, that once
470
     * an expiry time on a page has been set, it is not possible, to reset it again,
471
     * this is for dynamic_load situation, where one component might depend on a
472
     * set expiry.
473
     *
474
     * @param int $timestamp The UNIX timestamp from which the cached page should be invalidated.
475
     */
476
    public function expires($timestamp)
477
    {
478
        if (   $this->_expires === null
479
            || $this->_expires > $timestamp) {
480
            $this->_expires = $timestamp;
481
        }
482
    }
483
484
    /**
485
     * Sets the content type for the current page. The required HTTP Headers for
486
     * are automatically generated, so, to the contrary of expires, you just have
487
     * to set this header accordingly.
488
     *
489
     * This is usually set automatically by MidCOM for all regular HTML output and
490
     * for all attachment deliveries. You have to adapt it only for things like RSS
491
     * output.
492
     *
493
     * @param string $type    The content type to use.
494
     */
495 8
    public function content_type($type)
496
    {
497 8
        midcom::get()->header('Content-Type: ' . $type);
498 8
    }
499
500
    /**
501
     * Put the cache into a "live mode". This will disable the
502
     * cache during runtime, correctly flushing the output buffer (if it's not empty)
503
     * and sending cache control headers.
504
     *
505
     * The midcom-exec URL handler of the core will automatically enable live mode.
506
     *
507
     * @see midcom_application::_exec_file()
508
     */
509
    public function enable_live_mode()
510
    {
511
        $this->no_cache();
512
        Response::closeOutputBuffers(0, ob_get_length() > 0);
513
    }
514
515
    /**
516
     * Store a sent header into the cache database, so that it will
517
     * be resent when the cache page is delivered. midcom_application::header()
518
     * will automatically call this function, you need to do this only if you use
519
     * the PHP header function.
520
     *
521
     * @param string $header The header that was sent.
522
     */
523 17
    public function register_sent_header($header)
524
    {
525 17
        if (strpos($header, ': ') !== false) {
526 17
            [$header, $value] = explode(': ', $header, 2);
527 17
            $this->_sent_headers[$header] = $value;
528
        }
529 17
    }
530
531
    /**
532
     * Looks for list of content and request identifiers paired with the given guid
533
     * and removes all of those from the caches.
534
     *
535
     * {@inheritDoc}
536
     */
537 288
    public function invalidate($guid, $object = null)
538
    {
539 288
        $guidmap = $this->backend->fetch($guid);
540 288
        if ($guidmap === false) {
541 288
            debug_add("No entry for {$guid} in meta cache, ignoring invalidation request.");
542 288
            return;
543
        }
544
545
        foreach ($guidmap as $content_id) {
546
            if ($this->backend->contains($content_id)) {
547
                $this->backend->delete($content_id);
548
            }
549
550
            if ($this->_data_cache->contains($content_id)) {
551
                $this->_data_cache->delete($content_id);
552
            }
553
        }
554
    }
555
556
    public function invalidate_all()
557
    {
558
        parent::invalidate_all();
559
        $this->_data_cache->flushAll();
560
    }
561
562
    /**
563
     * All objects loaded within a request are stored into a list for cache invalidation purposes
564
     */
565 394
    public function register($guid)
566
    {
567
        // Check for uncached operation
568 394
        if ($this->_uncached) {
569 394
            return;
570
        }
571
572
        $context = midcom_core_context::get()->id;
573
        if ($context != 0) {
574
            // We're in a dynamic_load, register it for that as well
575
            if (!isset($this->context_guids[$context])) {
576
                $this->context_guids[$context] = [];
577
            }
578
            $this->context_guids[$context][] = $guid;
579
        }
580
581
        // Register all GUIDs also to the root context
582
        if (!isset($this->context_guids[0])) {
583
            $this->context_guids[0] = [];
584
        }
585
        $this->context_guids[0][] = $guid;
586
    }
587
588
    /**
589
     * Writes meta-cache entry from context data using given content id
590
     * Used to be part of on_request, but needed by serve-attachment method in midcom_core_urlmethods as well
591
     */
592 1
    public function write_meta_cache($content_id, Request $request, Response $response)
593
    {
594 1
        if (   $this->_uncached
595 1
            || $this->_no_cache) {
596
            return;
597
        }
598
599 1
        if ($this->_expires !== null) {
600
            $lifetime = $this->_expires - time();
601
        } else {
602
            // Use default expiry for cache entry, most components don't bother calling expires() properly
603 1
            $lifetime = $this->_default_lifetime;
604
        }
605
606
        // Construct cache identifier
607 1
        $request_id = $this->generate_request_identifier($request);
608
609
        $entries = [
610 1
            $request_id => $content_id,
611 1
            $content_id => $response->headers->all()
612
        ];
613 1
        $this->backend->saveMultiple($entries, $lifetime);
614
615
        // Cache where the object have been
616 1
        $context = midcom_core_context::get()->id;
617 1
        $this->store_context_guid_map($context, $content_id, $request_id);
618 1
    }
619
620 1
    private function store_context_guid_map($context, string $content_id, string $request_id)
621
    {
622
        // non-existent context
623 1
        if (!array_key_exists($context, $this->context_guids)) {
624 1
            return;
625
        }
626
627
        $maps = $this->backend->fetchMultiple($this->context_guids[$context]);
628
        $to_save = [];
629
        foreach ($this->context_guids[$context] as $guid) {
630
            // Getting old map from cache or create new, empty one
631
            $guidmap = $maps[$guid] ?? [];
632
633
            if (!in_array($content_id, $guidmap)) {
634
                $guidmap[] = $content_id;
635
                $to_save[$guid] = $guidmap;
636
            }
637
638
            if (!in_array($request_id, $guidmap)) {
639
                $guidmap[] = $request_id;
640
                $to_save[$guid] = $guidmap;
641
            }
642
        }
643
644
        $this->backend->saveMultiple($to_save);
645
    }
646
647 16
    public function check_dl_hit(Request $request)
648
    {
649 16
        if ($this->_no_cache) {
650 16
            return false;
651
        }
652
        $dl_request_id = 'DL' . $this->generate_request_identifier($request);
653
        $dl_content_id = $this->backend->fetch($dl_request_id);
654
        if ($dl_content_id === false) {
655
            return false;
656
        }
657
658
        return $this->_data_cache->fetch($dl_content_id);
659
    }
660
661 4
    public function store_dl_content($context, $dl_cache_data, Request $request)
662
    {
663 4
        if (   $this->_no_cache
664 4
            || $this->_uncached) {
665 4
            return;
666
        }
667
        $dl_request_id = 'DL' . $this->generate_request_identifier($request);
668
        $dl_content_id = 'DLC-' . md5($dl_cache_data);
669
670
        if ($this->_expires !== null) {
671
            $lifetime = $this->_expires - time();
672
        } else {
673
            // Use default expiry for cache entry, most components don't bother calling expires() properly
674
            $lifetime = $this->_default_lifetime;
675
        }
676
        $this->backend->save($dl_request_id, $dl_content_id, $lifetime);
677
        $this->_data_cache->save($dl_content_id, $dl_cache_data, $lifetime);
678
        // Cache where the object have been
679
        $this->store_context_guid_map($context, $dl_content_id, $dl_request_id);
680
    }
681
682
    /**
683
     * This little helper ensures that the headers Content-Length
684
     * and Last-Modified are present. The lastmod timestamp is taken out of the
685
     * component context information if it is populated correctly there; if not, the
686
     * system time is used instead.
687
     *
688
     * To force browsers to revalidate the page on every request (login changes would
689
     * go unnoticed otherwise), the Cache-Control header max-age=0 is added automatically.
690
     */
691 1
    private function complete_sent_headers(Response $response)
692
    {
693 1
        if ($date = $response->getLastModified()) {
694
            if ((int) $date->format('U') == -1) {
695
                debug_add("Failed to extract the timecode from the last modified header, defaulting to the current time.", MIDCOM_LOG_WARN);
696
                $response->setLastModified(new DateTime);
697
            }
698
        } else {
699
            /* Determine Last-Modified using MidCOM's component context,
700
             * Fallback to time() if this fails.
701
             */
702 1
            $time = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_LASTMODIFIED) ?: time();
703 1
            $response->setLastModified(DateTime::createFromFormat('U', (string) $time));
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromFormat('U', (string)$time) can also be of type false; however, parameter $date of Symfony\Component\HttpFo...onse::setLastModified() does only seem to accept DateTimeInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

703
            $response->setLastModified(/** @scrutinizer ignore-type */ DateTime::createFromFormat('U', (string) $time));
Loading history...
704
        }
705
706 1
        if (!$response->headers->has('Content-Length')) {
707
            /* TODO: Doublecheck the way this is handled, now we just don't send it
708
             * if headers_strategy implies caching */
709 1
            if (!in_array($this->_headers_strategy, ['public', 'private'])) {
710 1
                $response->headers->set("Content-Length", strlen($response->getContent()));
711
            }
712
        }
713
714 1
        $this->cache_control_headers($response);
715 1
    }
716
717
    /**
718
     * @param Response $response
719
     */
720 1
    public function cache_control_headers(Response $response)
721
    {
722
        // Just to be sure not to mess the headers sent by no_cache in case it was called
723 1
        if ($this->_no_cache) {
724
            $this->no_cache($response);
725
        } else {
726
            // Add Expiration and Cache Control headers
727 1
            $strategy = $this->_headers_strategy;
728 1
            $default_lifetime = $this->_default_lifetime;
729 1
            if (   midcom::get()->auth->is_valid_user()
730 1
                || midcom_connection::get_user()) {
731
                $strategy = $this->_headers_strategy_authenticated;
732
                $default_lifetime = $this->_default_lifetime_authenticated;
733
            }
734
735 1
            $now = time();
736 1
            if ($strategy == 'revalidate') {
737
                // If expires is not set, we force the client to revalidate every time.
738
                // The timeout of a content cache entry is not affected by this.
739 1
                $expires = $this->_expires ?? $now;
740
            } else {
741
                $expires = $this->_expires ?? $now + $default_lifetime;
742
                if ($strategy == 'private') {
743
                    $response->setPrivate();
744
                } else {
745
                    $response->setPublic();
746
                }
747
            }
748 1
            $max_age = $expires - $now;
749
750
            $response
751 1
                ->setExpires(DateTime::createFromFormat('U', $expires))
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromFormat('U', $expires) can also be of type false; however, parameter $date of Symfony\Component\HttpFo...\Response::setExpires() does only seem to accept DateTimeInterface|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

751
                ->setExpires(/** @scrutinizer ignore-type */ DateTime::createFromFormat('U', $expires))
Loading history...
752 1
                ->setMaxAge($max_age);
753 1
            if ($max_age == 0) {
754 1
                $response->headers->addCacheControlDirective('must-revalidate');
755
            }
756
        }
757 1
    }
758
}
759