|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NFePHP\Common\Files; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Classe auxiliar para criar, listar e testar os diretórios utilizados pela API |
|
7
|
|
|
* @category NFePHP |
|
8
|
|
|
* @package NFePHP\Common\Files |
|
9
|
|
|
* @copyright Copyright (c) 2008-2015 |
|
10
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
11
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
12
|
|
|
* @link http://github.com/nfephp-org/nfephp for the canonical source repository |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use NFePHP\Common\Exception; |
|
16
|
|
|
|
|
17
|
|
|
class FilesFolders |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
protected static $ambientes = array('homologacao','producao'); |
|
21
|
|
|
protected static $subdirs = array( |
|
22
|
|
|
'entradas', |
|
23
|
|
|
'assinadas', |
|
24
|
|
|
'validadas', |
|
25
|
|
|
'rejeitadas', |
|
26
|
|
|
'enviadas', |
|
27
|
|
|
'enviadas/aprovadas', |
|
28
|
|
|
'enviadas/denegadas', |
|
29
|
|
|
'enviadas/rejeitadas', |
|
30
|
|
|
'enviadas/encerradas', |
|
31
|
|
|
'canceladas', |
|
32
|
|
|
'inutilizadas', |
|
33
|
|
|
'cartacorrecao', |
|
34
|
|
|
'eventos', |
|
35
|
|
|
'dpec', |
|
36
|
|
|
'temporarias', |
|
37
|
|
|
'recebidas', |
|
38
|
|
|
'consultadas', |
|
39
|
|
|
'pdf' |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* getAmbiente |
|
44
|
|
|
* @param string $tpAmb |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function getAmbiente($tpAmb = '2') |
|
48
|
|
|
{ |
|
49
|
|
|
if ($tpAmb == '2') { |
|
50
|
|
|
return 'homologacao'; |
|
51
|
|
|
} |
|
52
|
|
|
return 'producao'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* getFilePath |
|
57
|
|
|
* @param string $tpAmb |
|
58
|
|
|
* @param string $dirbase |
|
59
|
|
|
* @param string $subdir |
|
60
|
|
|
* @return string |
|
61
|
|
|
* @throws Exception\RuntimeException |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function getFilePath($tpAmb = '2', $dirbase = '', $subdir = '') |
|
64
|
|
|
{ |
|
65
|
|
|
$path = $dirbase |
|
66
|
|
|
. DIRECTORY_SEPARATOR |
|
67
|
|
|
. self::getAmbiente($tpAmb) |
|
68
|
|
|
. DIRECTORY_SEPARATOR |
|
69
|
|
|
. $subdir; |
|
70
|
|
|
|
|
71
|
|
|
if (! is_dir($path)) { |
|
72
|
|
|
$msg = "Não existe o diretorio $path !"; |
|
73
|
|
|
throw new Exception\RuntimeException($msg); |
|
74
|
|
|
} |
|
75
|
|
|
return $path; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* createFolders |
|
80
|
|
|
* Cria a estrutura de diretorios para a guarda dos arquivos |
|
81
|
|
|
* @param string $dirPath path do diretorio a ser criado |
|
82
|
|
|
* @return boolean |
|
83
|
|
|
* @throws Exception\RuntimeException |
|
84
|
|
|
*/ |
|
85
|
2 |
|
public static function createFolders($dirPath = '') |
|
86
|
|
|
{ |
|
87
|
|
|
//monta a arvore de diretórios necessária e estabelece permissões de acesso |
|
88
|
2 |
|
self::createFolder($dirPath); |
|
89
|
2 |
|
foreach (self::$ambientes as $ambiente) { |
|
90
|
2 |
|
$folder = $dirPath.DIRECTORY_SEPARATOR.$ambiente; |
|
91
|
2 |
|
self::createFolder($folder); |
|
92
|
1 |
|
foreach (self::$subdirs as $subdir) { |
|
93
|
1 |
|
$folder = $dirPath.DIRECTORY_SEPARATOR.$ambiente.DIRECTORY_SEPARATOR.$subdir; |
|
94
|
1 |
|
self::createFolder($folder); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
1 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* createFolder |
|
102
|
|
|
* @param string $folder |
|
103
|
|
|
* @throws Exception\RuntimeException |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public static function createFolder($folder = '') |
|
106
|
|
|
{ |
|
107
|
2 |
|
if (! is_dir($folder)) { |
|
108
|
2 |
|
if (! mkdir($folder, 0777, true)) { |
|
109
|
|
|
throw new Exception\RuntimeException( |
|
110
|
|
|
"Não foi possivel criar o diretorio $folder. Verifique as permissões" |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* saveFile |
|
118
|
|
|
* @param string $path |
|
119
|
|
|
* @param string $filename |
|
120
|
|
|
* @param string $content |
|
121
|
|
|
* @return boolean |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function saveFile($path = '', $filename = '', $content = '') |
|
124
|
|
|
{ |
|
125
|
|
|
self::createFolder($path); |
|
126
|
|
|
$filePath = $path.DIRECTORY_SEPARATOR.$filename; |
|
127
|
|
|
if (! file_put_contents($filePath, $content)) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
if (! chmod($filePath, 0777)) { |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
return true; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* listDir |
|
138
|
|
|
* Obtem todo o conteúdo de um diretorio, e que atendam ao critério indicado. |
|
139
|
|
|
* @param string $dir Diretorio a ser pesquisado |
|
|
|
|
|
|
140
|
|
|
* @param string $fileMatch Critério de seleção pode ser usados coringas como *-nfe.xml |
|
141
|
|
|
* @param boolean $retpath se true retorna o path completo dos arquivos se false so retorna o nome dos arquivos |
|
142
|
|
|
* @return array com os nome dos arquivos que atendem ao critério estabelecido ou false |
|
143
|
|
|
* @throws Exception\InvalidArgumentException |
|
144
|
|
|
*/ |
|
145
|
2 |
|
public static function listDir($folder, $fileMatch = '*-nfe.xml', $retpath = false) |
|
146
|
|
|
{ |
|
147
|
2 |
|
if ($folder == '' || $fileMatch == '') { |
|
148
|
|
|
throw new Exception\InvalidArgumentException( |
|
149
|
|
|
"É necessário passar os parametros diretório e filtro!!!" |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
2 |
|
if (! is_dir($folder)) { |
|
153
|
1 |
|
throw new Exception\InvalidArgumentException( |
|
154
|
1 |
|
"O diretório não existe $folder !!!" |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
1 |
|
$aList = array(); |
|
|
|
|
|
|
158
|
1 |
|
$search = $folder; |
|
159
|
1 |
|
if (substr($folder, -1) == DIRECTORY_SEPARATOR) { |
|
160
|
1 |
|
$search = substr($folder, 0, strlen($folder)-1); |
|
161
|
|
|
} |
|
162
|
1 |
|
$searchmatch = $search.DIRECTORY_SEPARATOR.$fileMatch; |
|
163
|
1 |
|
$aGlob = glob($searchmatch); |
|
164
|
1 |
|
$aList = $aGlob; |
|
165
|
1 |
|
if (! $retpath && ! empty($aGlob)) { |
|
166
|
|
|
$aList = array(); |
|
167
|
|
|
foreach ($aGlob as $pathFile) { |
|
168
|
|
|
$aList[] = str_replace($search.DIRECTORY_SEPARATOR, '', $pathFile); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
1 |
|
return $aList; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Rotina para teste de escrita no path especificado |
|
176
|
|
|
* Usada na rotina de configuração (install.php) |
|
177
|
|
|
* @param string $path |
|
178
|
|
|
* @param string $message |
|
179
|
|
|
* @param string $respHtml passado por referencia irá conter a resposta em html |
|
180
|
|
|
* @return boolean |
|
181
|
|
|
*/ |
|
182
|
|
|
public static function writeTest($path = '', $message = '', &$respHtml = '') |
|
183
|
|
|
{ |
|
184
|
|
|
$cRed = '#FF0000'; |
|
185
|
|
|
$cGreen = '#00CC00'; |
|
186
|
|
|
$comentDir = 'O diretório NÃO EXISTE'; |
|
187
|
|
|
$corDir = $cRed; |
|
188
|
|
|
if (is_dir($path)) { |
|
189
|
|
|
$filen = $path.DIRECTORY_SEPARATOR.'teste.txt'; |
|
190
|
|
|
$comentDir = ' Sem permissão !!'; |
|
191
|
|
|
if (file_put_contents($filen, "teste\r\n")) { |
|
192
|
|
|
$corDir = $cGreen; |
|
193
|
|
|
$comentDir = ' Permissão OK'; |
|
194
|
|
|
unlink($filen); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
$respHtml = "<tr bgcolor=\"#FFFFCC\">" |
|
198
|
|
|
. "<td>$message</td>" |
|
199
|
|
|
. "<td bgcolor=\"$corDir\"><div align=\"center\">$comentDir</div></td>" |
|
200
|
|
|
. "<td>O diretório deve ter permissão de escrita</td></tr>"; |
|
201
|
|
|
if ($corDir == $cRed) { |
|
202
|
|
|
return false; |
|
203
|
|
|
} |
|
204
|
|
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Apaga um diretorio e todo o seu conteúdo |
|
209
|
|
|
* @param string $dirPath |
|
210
|
|
|
* @return boolean |
|
211
|
|
|
* @throws Exception\RuntimeException |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public static function removeFolder($dirPath) |
|
214
|
|
|
{ |
|
215
|
1 |
|
$files = array_diff(scandir($dirPath), array('.','..')); |
|
216
|
1 |
|
foreach ($files as $file) { |
|
217
|
1 |
|
if (is_dir("$dirPath/$file")) { |
|
218
|
1 |
|
self::removeFolder("$dirPath/$file"); |
|
219
|
|
|
} else { |
|
220
|
|
|
if (! unlink("$dirPath/$file")) { |
|
221
|
|
|
throw new Exception\RuntimeException( |
|
222
|
1 |
|
"Falha! sem permissão de exclusão do arquivo $dirPath/$file" |
|
223
|
|
|
); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
1 |
|
if (! rmdir($dirPath)) { |
|
228
|
|
|
$msg = "Falha! sem permissão de exclusão do diretório $dirPath"; |
|
229
|
|
|
throw new Exception\RuntimeException($msg); |
|
230
|
|
|
} |
|
231
|
1 |
|
return true; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* readFile |
|
236
|
|
|
* @param string $pathFile |
|
237
|
|
|
* @return string |
|
238
|
|
|
* @throws Exception\InvalidArgumentException |
|
239
|
|
|
* @throws Exception\RuntimeException |
|
240
|
|
|
*/ |
|
241
|
6 |
|
public static function readFile($pathFile = '') |
|
242
|
|
|
{ |
|
243
|
6 |
|
if ($pathFile == '') { |
|
244
|
|
|
$msg = "Um caminho para o arquivo deve ser passado!!"; |
|
245
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
246
|
|
|
} |
|
247
|
6 |
|
if (! is_file($pathFile)) { |
|
248
|
|
|
$msg = "O arquivo indicado não foi localizado!! $pathFile"; |
|
249
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
250
|
|
|
} |
|
251
|
6 |
|
if (! is_readable($pathFile)) { |
|
252
|
|
|
$msg = "O arquivo indicado não pode ser lido. Permissões!! $pathFile"; |
|
253
|
|
|
throw new Exception\RuntimeException($msg); |
|
254
|
|
|
} |
|
255
|
6 |
|
return file_get_contents($pathFile); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* removeFile |
|
260
|
|
|
* @param string $pathFile |
|
261
|
|
|
* @return boolean |
|
262
|
|
|
*/ |
|
263
|
|
|
public static function removeFile($pathFile = '') |
|
264
|
|
|
{ |
|
265
|
|
|
if ($pathFile == '' || !is_file($pathFile)) { |
|
266
|
|
|
return true; |
|
267
|
|
|
} |
|
268
|
|
|
return unlink($pathFile); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.