1
|
|
|
<?php |
2
|
|
|
/******************************************************************* |
3
|
|
|
* Created by: Marko Kungla @ OkramLabs on Aug 6, 2012 - 10:02:48 |
4
|
|
|
* Contact: [email protected] - https://okramlabs.com |
5
|
|
|
* @copyright 2015 OkramLabs - https://okramlabs.com |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* Package name:libhowi-filesystem |
9
|
|
|
* @category HOWI3 |
10
|
|
|
* @package libhowi |
11
|
|
|
* @subpackage filesystem |
12
|
|
|
* |
13
|
|
|
* Lang: PHP |
14
|
|
|
* Encoding: UTF-8 |
15
|
|
|
* File: DirectoryPlaceholderObject.inc |
16
|
|
|
* @link https:// |
17
|
|
|
******************************************************************** |
18
|
|
|
* Contributors: |
19
|
|
|
* @author Marko Kungla <[email protected]> |
20
|
|
|
* Github: https://github.com/mkungla |
21
|
|
|
******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
*/ |
24
|
|
|
namespace HOWI3\libhowi\Filesystem\php7\Objects; |
25
|
|
|
|
26
|
|
|
use \Directory; |
27
|
|
|
use \HOWI3\libhowi\Filesystem\Commons\ObjectInterfaces\DirectoryPlaceholderInterface; |
28
|
|
|
use \HOWI3\libhowi\Filesystem\Commons\SharedMethodsInterface; |
29
|
|
|
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse; |
30
|
|
|
use \HOWI3\libhowi\Filesystem\php7\TraitForSharedMethods; |
31
|
|
|
|
32
|
|
View Code Duplication |
class DirectoryPlaceholderObject extends Directory implements DirectoryPlaceholderInterface, |
|
|
|
|
33
|
|
|
SharedMethodsInterface |
34
|
|
|
{ |
35
|
|
|
use TraitForResponse; |
36
|
|
|
use TraitForSharedMethods; |
37
|
|
|
|
38
|
|
|
public $path = ''; |
39
|
|
|
|
40
|
|
|
public $handle; |
41
|
|
|
|
42
|
|
|
private $mode = 750; |
43
|
|
|
|
44
|
|
|
private $recursive = true; |
45
|
|
|
|
46
|
|
|
private $context = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
6 |
|
public function __construct($pathname = false, $recursive = true, $mode = false, $context = false, $logfile = false, |
54
|
|
|
$loglevel = false, $UID = false, $username = false) |
55
|
|
|
{ |
56
|
6 |
|
if (! empty($logfile) && file_exists($logfile) && ! empty($loglevel)) { |
57
|
5 |
|
$this->setLogfile($logfile); |
|
|
|
|
58
|
5 |
|
$this->setLoglevel($loglevel); |
|
|
|
|
59
|
5 |
|
$this->setUID($UID); |
60
|
5 |
|
$this->setUsername($username); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
$pathname = (! empty($pathname) ? $this->makeAbsolute($pathname) : $this->path); |
64
|
|
|
|
65
|
6 |
|
$HID = md5($pathname); |
66
|
|
|
|
67
|
6 |
|
$this->dirsetPath($pathname, $HID); |
|
|
|
|
68
|
6 |
|
$this->dirsetMode(! empty($mode) ? $mode : $this->mode, $HID); |
|
|
|
|
69
|
|
|
|
70
|
6 |
|
$this->dirsetRecursive(! empty($recursive) ? true : false, $HID); |
|
|
|
|
71
|
|
|
|
72
|
6 |
|
$context = ! empty($context) ? $this->dirsetContext($context, $HID) : false; |
|
|
|
|
73
|
|
|
|
74
|
6 |
|
$this->info(701, $pathname); |
|
|
|
|
75
|
6 |
|
$this->response()->setStatus($this->dirCreate($HID)); |
|
|
|
|
76
|
6 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
6 |
|
public function dirsetPath($pathname = false, $HID = false) |
84
|
|
|
{ |
85
|
6 |
|
if (! empty($pathname) && is_string($pathname) && ! empty($HID)) { |
86
|
|
|
|
87
|
6 |
|
$this->path = $this->makeAbsolute($pathname); |
|
|
|
|
88
|
|
|
|
89
|
6 |
|
return true; |
90
|
|
|
} else { |
91
|
1 |
|
$this->debug(802, 'DirectoryTrait->dirsetPath'); |
92
|
1 |
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
6 |
|
public function dirsetMode($mode = false, $HID = false) |
102
|
|
|
{ |
103
|
6 |
|
if (! empty($HID) && ! empty($mode) && is_int($mode)) { |
104
|
6 |
|
$this->mode = $mode; |
105
|
6 |
|
return true; |
106
|
|
|
} else { |
107
|
|
|
|
108
|
1 |
|
$this->debug(802, 'DirectoryTrait->dirsetMode'); |
109
|
1 |
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
* |
117
|
|
|
*/ |
118
|
6 |
|
public function dirsetRecursive($recursive = false, $HID = false) |
119
|
|
|
{ |
120
|
6 |
|
if (! empty($HID) && is_bool($recursive)) { |
121
|
6 |
|
$this->recursive = $recursive; |
122
|
6 |
|
return true; |
123
|
|
|
} else { |
124
|
1 |
|
$this->debug(802, 'DirectoryTrait->dirsetRecursive'); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
* |
134
|
|
|
*/ |
135
|
2 |
|
public function dirsetContext($context = false, $HID = false) |
136
|
|
|
{ |
137
|
2 |
|
if (! empty($HID) && is_resource($context)) { |
138
|
1 |
|
$this->context = $context; |
|
|
|
|
139
|
1 |
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
else { |
143
|
1 |
|
$this->debug(802, 'DirectoryTrait->dirsetContext'); |
144
|
1 |
|
return false; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
* |
152
|
|
|
*/ |
153
|
6 |
|
public function dirCreate($HID = false) |
154
|
|
|
{ |
155
|
6 |
|
if ((! is_dir(dirname($this->path)) && $this->recursive === false) || |
156
|
6 |
|
is_dir(dirname($this->path)) && ! $this->isWritable(dirname($this->path))) { |
|
|
|
|
157
|
2 |
|
$this->warning(502, $this->path); |
158
|
2 |
|
$result = false; |
159
|
|
|
} else { |
160
|
5 |
|
if (! empty($HID) && ! $this->exists($this->path) && ! empty($this->context)) { |
|
|
|
|
161
|
1 |
|
$result = mkdir($this->path, octdec(str_pad($this->mode, 4, 0, STR_PAD_LEFT)), |
162
|
1 |
|
$this->recursive, $this->context); |
163
|
|
|
} |
164
|
5 |
|
if (! empty($HID) && ! $this->exists($this->path) && empty($this->context)) { |
|
|
|
|
165
|
|
|
|
166
|
5 |
|
$result = mkdir($this->path, octdec(str_pad($this->mode, 4, 0, STR_PAD_LEFT)), true); |
167
|
|
|
} |
168
|
|
|
} |
169
|
6 |
|
return $result; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.