forked from fecshop/yii2_fecshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.php
272 lines (242 loc) · 8.76 KB
/
Helper.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/*
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\services;
use Yii;
/**
* Helper service.
*
* @property \fecshop\services\helper\Appapi $appapi
* @property \fecshop\services\helper\Appserver $appserver appserver sub-service of helper service
* @property \fecshop\services\helper\AR $ar
* @property \fecshop\services\helper\Captcha $captcha
* @property \fecshop\services\helper\Country $country
* @property \fecshop\services\helper\Echart $echart
* @property \fecshop\services\helper\ErrorHandler $errorHandler
* @property \fecshop\services\helper\Errors $errors errors sub-service of helper service
* @property \fecshop\services\helper\Format $format
* @property \fecshop\services\helper\MobileDetect $mobileDetect
* @author Terry Zhao <[email protected]>
* @since 1.0
*/
class Helper extends Service
{
protected $_app_name;
protected $_param;
/**
* 得到当前的app入口的名字,譬如 appfront apphtml5 appserver等.
*/
public function getAppName()
{
return Yii::$app->params['appName'];
}
/**
* @param $var | String Or Array 需要进行Html::encode()操作的变量。
* @return $var | String Or Array 去除xss攻击字符后的变量
*/
public function htmlEncode($var)
{
if (is_array($var) && !empty($var)) {
foreach ($var as $k=>$v) {
if (is_array($v) && !empty($v)) {
$var[$k] = $this->htmlEncode($v);
} elseif (empty($v)) {
$var[$k] = $v;
} else {
if (is_string($v)) {
$var[$k] = \yii\helpers\Html::encode($v);
}
}
}
} elseif (empty($var)) {
} else {
if (is_string($var)) {
$var = \yii\helpers\Html::encode($var);
}
}
return $var;
}
/**
* @param $domain | String vue类型的appserver传递的domain
* 这个是appservice发送邮件,在邮件里面的url链接地址,在这里保存
*/
public function setAppServiceDomain($domain)
{
$this->_param['appServiceDomain'] = $domain;
return true;
}
public function getAppServiceDomain()
{
return isset($this->_param['appServiceDomain']) ? $this->_param['appServiceDomain'] : false;
}
/**
* 该端口是否是Api入口,譬如appserver appapi等,都是属于api的入口
* api入口都会将 Yii::$app->user->enableSession 关闭,因此通过该值判断, 是否是Api App
*
*/
public function isApiApp()
{
if (\Yii::$service->store->isApiStore() == true) {
return true;
} else {
return false;
}
}
public function getCustomerIp()
{
return Yii::$app->request->userIP;
}
function createNoncestr( $length = 32 ){
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
// 递归删除文件夹以及里面的所有的子文件夹和子文件
public function deleteDir($path) {
if (is_dir($path)) {
//扫描一个目录内的所有目录和文件并返回数组
$dirs = scandir($path);
foreach ($dirs as $dir) {
//排除目录中的当前目录(.)和上一级目录(..)
if ($dir != '.' && $dir != '..') {
//如果是目录则递归子目录,继续操作
$sonDir = $path.'/'.$dir;
if (is_dir($sonDir)) {
//递归删除
$this->deleteDir($sonDir);
//目录内的子目录和文件删除后删除空目录
@rmdir($sonDir);
} else {
//如果是文件直接删除
@unlink($sonDir);
}
}
}
@rmdir($path);
}
return true;
}
/**
* 图片文件复制,注意,如果某个文件不是图片类型,则不会被复制(仅仅复制图片)
* 文件夹图片文件拷贝, 如果文件存在,则会被强制覆盖。
* @param string $sourcePath 来源文件夹
* @param string $targetPath 目的地文件夹
* @param boolean $isForce 是否强制复制
* @return bool
*/
public function copyDirImage($sourcePath, $targetPath, $isForce = true)
{
if (empty($sourcePath) || empty($targetPath)) {
return false;
}
$dir = opendir($sourcePath);
$this->dir_mkdir($targetPath);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
$sourcePathFile = $sourcePath . '/' . $file;
$targetPathFile = $targetPath . '/' . $file;
if (is_dir($sourcePathFile)){
$this->copyDirImage($sourcePathFile, $targetPathFile);
} else if (Yii::$service->image->isAllowImgType($sourcePathFile, $file)){
if ($isForce) {
copy($sourcePathFile, $targetPathFile);
} else if (!file_exists($targetPathFile)) {
copy($sourcePathFile, $targetPathFile);
} else {
Yii::$service->helper->errors->add('target path:' . $targetPathFile . ' is exist.');
}
} else {
Yii::$service->helper->errors->add('file is not image:' . $sourcePathFile);
}
}
}
closedir($dir);
return true;
}
/**
* 文件夹文件拷贝
*
* @param string $sourcePath 来源文件夹
* @param string $targetPath 目的地文件夹
* @param boolean $isForce 是否强制复制
* @return bool
*/
public function copyDir($sourcePath, $targetPath, $isForce = true)
{
if (empty($sourcePath) || empty($targetPath))
{
return false;
}
$dir = opendir($sourcePath);
$this->dir_mkdir($targetPath);
while (false !== ($file = readdir($dir)))
{
if (($file != '.') && ($file != '..')) {
$sourcePathFile = $sourcePath . '/' . $file;
$targetPathFile = $targetPath . '/' . $file;
if (is_dir( $sourcePathFile)) {
$this->copyDir( $sourcePathFile, $targetPathFile);
} else {
//copy($sourcePath . '/' . $file, $targetPath . '/' . $file);
if ($isForce) {
copy($sourcePathFile, $targetPathFile);
} else if (!file_exists($targetPathFile)) {
copy($sourcePathFile, $targetPathFile);
} else {
Yii::$service->helper->errors->add('target path:' . $targetPathFile . ' is exist.');
}
}
}
}
closedir($dir);
return true;
}
/**
* 创建文件夹
*
* @param string $path 文件夹路径
* @param int $mode 访问权限
* @param bool $recursive 是否递归创建
* @return bool
*/
public function dir_mkdir($path = '', $mode = 0777, $recursive = true)
{
clearstatcache();
if (!is_dir($path))
{
mkdir($path, $mode, $recursive);
return chmod($path, $mode);
}
return true;
}
public function scanAllDirSubFile($dir, $subDir='/')
{
if(is_dir($dir)){
$files = array();
$child_dirs = scandir($dir);
foreach ($child_dirs as $child_dir){
//'.'和'..'是Linux系统中的当前目录和上一级目录,必须排除掉,
//否则会进入死循环,报segmentation falt 错误
if($child_dir != '.' && $child_dir != '..'){
if(is_dir($dir.'/'.$child_dir)){
//$files[$child_dir] = my_scandir($dir.'/'.$child_dir);
$files = array_merge($files, $this->scanAllDirSubFile($dir.'/'.$child_dir, $subDir.$child_dir.'/'));
}else{
$files[] = $subDir.$child_dir;
}
}
}
return $files;
}else{
return $subDir.$dir;
}
}
}