1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Retour allows you to intelligently redirect legacy URLs, so that you don't |
6
|
|
|
* lose SEO value when rebuilding & restructuring a website |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\validators; |
13
|
|
|
|
14
|
|
|
use craft\helpers\UrlHelper; |
15
|
|
|
use nystudio107\retour\models\StaticRedirects; |
16
|
|
|
use yii\validators\Validator; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
20
|
|
|
* @package Retour |
|
|
|
|
21
|
|
|
* @since 3.0.0 |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
class UriValidator extends Validator |
24
|
|
|
{ |
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
public $skipOnEmpty = false; |
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
public $skipOnError = false; |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
|
|
|
|
38
|
|
|
public function init(): void |
39
|
|
|
{ |
40
|
|
|
parent::init(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
|
|
|
|
46
|
|
|
public function validateAttribute($model, $attribute): void |
47
|
|
|
{ |
48
|
|
|
/** @var StaticRedirects $model */ |
|
|
|
|
49
|
|
|
$value = $model->$attribute; |
50
|
|
|
$redirectMatchType = 'redirectMatchType'; |
51
|
|
|
// Always remove whitespace |
52
|
|
|
$value = preg_replace("/\r|\n/", '', $value); |
53
|
|
|
$value = trim($value); |
54
|
|
|
$model->$attribute = $value; |
55
|
|
|
// Only do any kind of validation for exact match redirects |
56
|
|
|
if ($model->$redirectMatchType !== 'exactmatch') { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
// Don't mess with URLs that are already full URLs |
60
|
|
|
if (UrlHelper::isFullUrl($value)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
// Make sure there is a leading / |
64
|
|
|
$value = '/' . ltrim($value, '/'); |
65
|
|
|
$model->$attribute = $value; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|