|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TPageNoCacheBehavior class file. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Brad Anderson <[email protected]> |
|
7
|
|
|
* @link https://github.com/pradosoft/prado |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Util\Behaviors; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\TPropertyValue; |
|
14
|
|
|
use Prado\Web\UI\WebControls\TMetaTag; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* TPageNoCacheBehavior class. |
|
18
|
|
|
* |
|
19
|
|
|
* TPageNoCacheBehavior attaches to pages and adds no-cache meta to the head. |
|
20
|
|
|
* |
|
21
|
|
|
* {@link getCheckMetaNoCache} specifies whether or not to check the existing |
|
22
|
|
|
* meta tags (in THead of the TPage) before adding the no cache tags. By default |
|
23
|
|
|
* getCheckMetaNoCache is turned off for performance. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Brad Anderson <[email protected]> |
|
26
|
|
|
* @package Prado\Util\Behaviors |
|
27
|
|
|
* @since 4.2.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class TPageNoCacheBehavior extends \Prado\Util\TBehavior |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var bool check the existing meta tags for the no cache before adding them */ |
|
32
|
|
|
private $_checkMetaNoCache = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* This handles the TPage.OnInitComplete event to place no-cache |
|
36
|
|
|
* meta in the head. |
|
37
|
|
|
* @return array of events as keys and methods as values |
|
38
|
|
|
*/ |
|
39
|
|
|
public function events() |
|
40
|
|
|
{ |
|
41
|
|
|
return ['OnInitComplete' => 'addNoCacheMeta']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* This method places no-cache meta in the head. |
|
46
|
|
|
* @param $page object raising the event |
|
47
|
|
|
* @param $param mixed the parameter of the raised event |
|
48
|
|
|
*/ |
|
49
|
|
|
public function addNoCacheMeta($page, $param) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
if ($head = $page->getHead()) { |
|
52
|
|
|
$hasExpires = $hasPragma = $hasCacheControl = false; |
|
53
|
|
|
$metatags = $head->getMetaTags(); |
|
54
|
|
|
if ($this->_checkMetaNoCache) { |
|
55
|
|
|
foreach ($metatags as $meta) { |
|
56
|
|
|
$httpEquiv = strtolower($meta->getHttpEquiv()); |
|
57
|
|
|
if ($httpEquiv == 'expires') { |
|
58
|
|
|
$hasExpires = true; |
|
59
|
|
|
} elseif ($httpEquiv == 'pragma') { |
|
60
|
|
|
$hasPragma = true; |
|
61
|
|
|
} elseif ($httpEquiv == 'cache-control') { |
|
62
|
|
|
$hasCacheControl = true; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
if (!$hasExpires) { |
|
67
|
|
|
$meta = new TMetaTag(); |
|
68
|
|
|
$meta->setHttpEquiv('Expires'); |
|
69
|
|
|
$meta->setContent('Fri, Jan 01 1900 00:00:00 GMT'); |
|
70
|
|
|
$metatags->add($meta); |
|
71
|
|
|
} |
|
72
|
|
|
if (!$hasPragma) { |
|
73
|
|
|
$meta = new TMetaTag(); |
|
74
|
|
|
$meta->setHttpEquiv('Pragma'); |
|
75
|
|
|
$meta->setContent('no-cache'); |
|
76
|
|
|
$metatags->add($meta); |
|
77
|
|
|
} |
|
78
|
|
|
if (!$hasCacheControl) { |
|
79
|
|
|
$meta = new TMetaTag(); |
|
80
|
|
|
$meta->setHttpEquiv('Cache-Control'); |
|
81
|
|
|
$meta->setContent('no-cache'); |
|
82
|
|
|
$metatags->add($meta); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return bool checks existing meta tags for no cache |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getCheckMetaNoCache() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->_checkMetaNoCache; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param bool $value checks existing meta tags for no cache |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setCheckMetaNoCache($value) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->_checkMetaNoCache = TPropertyValue::ensureBoolean($value); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.