1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2017 Salah Alkhwlani <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Yemenifree\WpSecurity\Modules; |
11
|
|
|
|
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Yemenifree\WpSecurity\Interfaces\Activable; |
14
|
|
|
use Yemenifree\WpSecurity\Traits\HasFilesReader; |
15
|
|
|
use Yemenifree\WpSecurity\Traits\HasHtaccessEditMode; |
16
|
|
|
|
17
|
|
|
class HtaccessSecurity implements Activable |
18
|
|
|
{ |
19
|
|
|
use HasHtaccessEditMode, HasFilesReader; |
20
|
|
|
|
21
|
|
|
protected $paths; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* HtaccessSecurity constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->paths = [ |
29
|
|
|
ABSPATH => $this->getMainHtaccess(), |
|
|
|
|
30
|
|
|
wp_upload_dir()['basedir'] => $this->getUploadsHtaccess(), |
|
|
|
|
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* get main htaccess content file from plugin. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
private function getMainHtaccess() |
40
|
|
|
{ |
41
|
|
|
return $this->getFileContent('.htaccess'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* get upload htaccess content from plugin. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
private function getUploadsHtaccess() |
50
|
|
|
{ |
51
|
|
|
return $this->getFileContent('.uploads-htaccess'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function activate() |
58
|
|
|
{ |
59
|
|
|
$this->getPaths()->each(function ($content, $path) { |
60
|
|
|
$this->setHtaccessPath($path)->setHtaccessRule($content)->addHtaccessContent(); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* get list paths & htaccess content. |
66
|
|
|
* |
67
|
|
|
* @return Collection |
68
|
|
|
*/ |
69
|
|
|
public function getPaths() |
70
|
|
|
{ |
71
|
|
|
return collect($this->paths); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function deactivate() |
78
|
|
|
{ |
79
|
|
|
$this->getPaths()->each(function ($content, $path) { |
80
|
|
|
$this->setHtaccessPath($path)->setHtaccessRule($content)->removeHtaccessContent(); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|