/
private-site.php
124 lines (107 loc) · 3.77 KB
/
private-site.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page;
use Grav\Common\Data\Blueprints;
use RocketTheme\Toolbox\Event\Event;
/**
* Class PrivateSitePlugin
* @package Grav\Plugin
*/
class PrivateSitePlugin extends Plugin
{
/**
* @var
*/
protected $_privateConfig;
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
{
// Check for required plugins
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
if (!$this->grav['config']->get('plugins.login.enabled') || !$this->grav['config']->get('plugins.form.enabled') || !$this->grav['config']->get('plugins.email.enabled')) {
throw new \RuntimeException('One of the required plugins is missing or not enabled');
}
if ($this->isAdmin()) {
// Enable the main event we are interested in
$this->enable([
'onBlueprintCreated' => ['onBlueprintCreated', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0]
]);
} else {
// Enable the main event we are interested in
$this->_privateConfig = $this->grav['config']->get('plugins.private-site', null);
$this->enable([
'onPageInitialized' => ['onPageInitialized', 1000]
]);
}
}
/**
* Check if site/page is private
*/
public function onPageInitialized()
{
if( isset($this->grav['page']->header()->access) && isset($this->grav['page']->header()->access['site.login'] )){
return;
}
switch ($this->_privateConfig['private_site']) {
case true:
default:
if ( isset($this->grav['page']->header()->access) && isset($this->grav['page']->header()->access['site.login']) ) {
if ($this->grav['page']->header()->access['site.login'] != true) {
$this->grav['page']->modifyHeader('access', array('site.login' => true));
}
} else {
$this->grav['page']->modifyHeader('access', array('site.login' => true));
}
break;
case false:
if ( isset($this->grav['page']->header()->private) && $this->grav['page']->header()->private == true ){
$this->grav['page']->modifyHeader('access', array('site.login' => true));
}
break;
}
}
/**
* Extend Admin Page Blueprints
*/
public function onBlueprintCreated(Event $event)
{
static $inEvent = false;
$newtype = $event['type'];
if (strpos($newtype, 'modular/') !== 0) {
$blueprint = $event['blueprint'];
if (!$inEvent && $blueprint->get('form/fields/tabs', null, '/')) {
$inEvent = true;
$blueprints = new Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get('private');
$blueprint->extend($extends, false);
$inEvent = false;
}
}
}
/**
* Add page template types.
*/
public function onGetPageTemplates(Event $event){
$types = $event->types;
$types->register('private');
}
}