-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.php
83 lines (73 loc) · 2.78 KB
/
controller.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
<?php
namespace Concrete\Package\SslRedirectConf;
use Config;
use Events;
use Concrete\Core\Backup\ContentImporter;
use Concrete\Core\Routing\RedirectResponse;
use Concrete\Core\Url\Url;
use Concrete\Package\SslRedirectConf\Http\UserRequestMatcher;
use Concrete\Package\SslRedirectConf\Http\PathRequestMatcher;
class Controller extends \Concrete\Core\Package\Package
{
protected $pkgHandle = 'ssl_redirect_conf';
protected $appVersionRequired = '5.7.5.4';
protected $pkgVersion = '0.9.2';
protected $pkgAutoloaderMapCoreExtensions = true;
public function getPackageName()
{
return t('SSL Redirect Configuration');
}
public function getPackageDescription()
{
return t('Redirect users to https URL with simple rules configuration.');
}
public function install()
{
$pkg = parent::install();
$ci = new ContentImporter();
$ci->importContentFile($pkg->getPackagePath() . '/config/dashboard.xml');
}
public function on_start()
{
Events::addListener('on_page_view', function ($event) {
$request = $event->getRequest();
$url = Url::createFromUrl($request->getUri());
$isSSL = false;
$pathMatcher = new PathRequestMatcher();
if ($pathMatcher->matches($request)) {
$isSSL = true;
}
if (!$pathMatcher->matches($request)) {
$isSSL = false;
}
$userMatcher = new UserRequestMatcher();
if ($userMatcher->matches($request)) {
$isSSL = true;
}
if (!$userMatcher->matches($request) && $isSSL === false) {
$isSSL = false;
}
if ($isSSL === true && $request->getScheme() == 'http') {
$config_canonical_ssl_url = Config::get('concrete.seo.canonical_ssl_url');
if (strlen($config_canonical_ssl_url)) {
$canonical_ssl_url = Url::createFromUrl($config_canonical_ssl_url);
$url->setHost($canonical_ssl_url->getHost());
}
$url->setScheme('https');
$response = new RedirectResponse($url);
} elseif ($isSSL === false && $request->getScheme() == 'https') {
$config_canonical_url = Config::get('concrete.seo.canonical_url');
if (strlen($config_canonical_url)) {
$canonical_url = Url::createFromUrl($config_canonical_url);
$url->setHost($canonical_url->getHost());
}
$url->setScheme('http');
$response = new RedirectResponse($url);
}
if (isset($response)) {
$response->send();
exit;
}
});
}
}