Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 66d2e7f

Browse files
committed
update, some change ...
1 parent ed9df5a commit 66d2e7f

File tree

10 files changed

+300
-242
lines changed

10 files changed

+300
-242
lines changed

src/files/Directory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,19 @@ static public function getFiles($dirName, $ext=null, $recursive=0, &$list=[])
152152
*/
153153
static public function create($path, $mode=0664, $recursive = true)
154154
{
155-
return is_dir($path) || mkdir($path, $mode, $recursive);
155+
return self::make($path, $mode, $recursive);
156156
}
157157

158158
/**
159159
* ********************** 创建多级目录 **********************
160160
* @param $path - 目录字符串
161161
* @param int $mode =0664 - 权限,默认 0664
162+
* @param bool $recursive
162163
* @return bool
163164
*/
164-
static public function make($path, $mode=0664)
165+
static public function make($path, $mode=0664, $recursive = true)
165166
{
166-
return (is_dir($path) || mkdir($path, $mode, true)) && is_writable($path);
167+
return (is_dir($path) || mkdir($path, $mode, $recursive)) && is_writable($path);
167168
}
168169

169170
//复制目录内容

src/files/File.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static function getName($file, $clearExt=false)
3535
/**
3636
* 获得文件扩展名、后缀名,带点 .jpg
3737
* @param $filename
38+
* @param bool $clearPoint
3839
* @return string
3940
*/
4041
public static function getSuffix($filename, $clearPoint=false)
@@ -47,11 +48,14 @@ public static function getSuffix($filename, $clearPoint=false)
4748
/**
4849
* 获得文件扩展名、后缀名,没有带点 jpg
4950
* @param $path
51+
* @param bool $clearPoint
5052
* @return string
5153
*/
52-
public static function getExtension($path)
54+
public static function getExtension($path, $clearPoint=false)
5355
{
54-
return pathinfo($path,PATHINFO_EXTENSION);
56+
$ext = pathinfo($path,PATHINFO_EXTENSION);
57+
58+
return $clearPoint ? $ext : '.' . $ext;
5559
}
5660

5761
public static function getInfo($filename, $check=true)

src/helpers/CurlHelper.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace inhere\librarys\helpers;
1010

11+
use inhere\librarys\files\Directory;
1112
use inhere\librarys\files\File;
1213

1314
/**
@@ -28,30 +29,45 @@ class CurlHelper
2829
/**
2930
* @param string $imgUrl image url e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg
3031
* @param string $savePath 图片保存路径
31-
* @param string $rename 图片重命名(只写名称,不用后缀) 为空则使用原名称
32+
* @param string $rename 图片重命名(只写名称,不用后缀) 为空则使用原名称
33+
* @return string
3234
*/
3335
public static function fetchImg($imgUrl, $savePath, $rename = '')
3436
{
37+
// e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg?t=34512323
38+
if ( strpos($imgUrl, '?')) {
39+
list($real,) = explode('?', $imgUrl, 2);
40+
} else {
41+
$real = $imgUrl;
42+
}
43+
44+
$last = trim(strrchr($real, '/'), '/');
45+
46+
// special url e.g http://img.blog.csdn.net/20150929103749499
47+
if ( false === strpos($last, '.')) {
48+
$suffix = '.jpg';
49+
$name = $rename ? : $last;
50+
} else {
51+
$suffix = File::getSuffix($real) ?: '.jpg';
52+
$name = $rename ? : File::getName($real, 1);
53+
}
54+
55+
$imgFile = $savePath . '/' . $name .$suffix;
56+
57+
if ( file_exists($imgFile) ) {
58+
return $imgFile;
59+
}
3560
$ch = curl_init();
3661

3762
curl_setopt($ch, CURLOPT_URL, UrlHelper::encode2($imgUrl));
3863
// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
3964
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4065
// 伪造网页来源地址,伪造来自百度的表单提交
41-
curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");
66+
curl_setopt($ch, CURLOPT_REFERER, 'http://www.baidu.com');
4267

4368
$imgData = self::execute($ch);
4469

45-
// e.g. http://static.oschina.net/uploads/user/277/554046_50.jpg?t=34512323
46-
if ( strpos($imgUrl, '?')) {
47-
list($real,) = explode('?', $imgUrl, 2);
48-
} else {
49-
$real = $imgUrl;
50-
}
51-
52-
$suffix = File::getSuffix($real);
53-
$name = $rename ? : File::getName($real, 1);
54-
$imgFile = $savePath . '/' . $name .$suffix;
70+
Directory::make($savePath);
5571

5672
file_put_contents($imgFile, $imgData);
5773

@@ -88,7 +104,7 @@ public static function get($url, array $params = [], array $headers = [])
88104
* send POST request
89105
*
90106
* @param string $url url
91-
* @param array $params url params
107+
* @param array $data url params
92108
* @param array $headers HEADER info
93109
* @return string
94110
*/
@@ -118,6 +134,7 @@ public static function post($url, array $data = [], array $headers = [])
118134
*/
119135
public static function execute($ch, $retries = 3, $closeAfterDone = true)
120136
{
137+
$ret = '';
121138
while ($retries--) {
122139
if ( ($ret = curl_exec($ch)) === false) {
123140
$curlErrno = curl_errno($ch);
@@ -143,4 +160,4 @@ public static function execute($ch, $retries = 3, $closeAfterDone = true)
143160

144161
return $ret;
145162
}
146-
}
163+
}

src/helpers/DateHelper.php

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,128 @@
88
*/
99

1010
namespace inhere\librarys\helpers;
11-
use \inhere\librarys\traits\TraitDateHelper;
1211

12+
/**
13+
* Class DateHelper
14+
* @package inhere\librarys\helpers
15+
*/
1316
class DateHelper
1417
{
15-
use TraitDateHelper;
18+
/*
19+
$tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
20+
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
21+
$nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);
22+
23+
echo strtotime("now"), "\n";
24+
echo strtotime("10 September 2000"), "\n";
25+
echo strtotime("+1 day"), "\n";
26+
echo strtotime("+1 week"), "\n";
27+
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
28+
echo strtotime("next Thursday"), "\n";
29+
echo strtotime("last Monday"), "\n";
30+
31+
*/
32+
/**
33+
* [isDate 判断给定的 字符串 是否是个 日期时间 ]
34+
* @param [type] $strTime 时间戳 | 日期格式的字符串
35+
* @param string $format [description]
36+
* @return boolean | string datetime
37+
*/
38+
public static function isDate($strTime,$format= 'Y/m/d')
39+
{
40+
if (!$strTime || (!is_string($strTime) && !is_numeric($strTime)) ) {
41+
return false;
42+
}
43+
44+
$strTime = trim($strTime);
45+
46+
//@example timestamp 1325347200
47+
if ( is_int($strTime) ) {
48+
$date = date($format,$strTime);
49+
50+
return $date ? : false;
51+
52+
//@example date 2015/04/05
53+
} else {
54+
$time = strtotime($strTime);
55+
56+
return $time ? date($format,$time) : false;
57+
}
58+
}
59+
60+
//获取指定日期所在月的第一天和最后一天
61+
public static function getTheMonth($date)
62+
{
63+
$firstDay = date('Y-m-01',strtotime($date));
64+
$lastDay = date('Y-m-d', strtotime("$firstDay +1 month -1 day"));
65+
66+
return array($firstDay,$lastDay);
67+
}
68+
69+
//获取指定日期上个月的第一天和最后一天
70+
public static function getPurMonth($date)
71+
{
72+
$time = strtotime($date);
73+
$firstDay = date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)-1).'-01'));
74+
$lastDay = date('Y-m-d',strtotime("$firstDay +1 month -1 day"));
75+
76+
return array($firstDay,$lastDay);
77+
}
78+
79+
//获取指定日期下个月的第一天和最后一天
80+
public static function getNextMonth($date)
81+
{
82+
$arr = getdate();
83+
84+
if ($arr['mon'] === 12) {
85+
$year = $arr['year'] +1;
86+
$month = $arr['mon'] -11;
87+
$day = $arr['mday'];
88+
89+
$mday = $day < 10 ? '0'.$day : $day;
90+
91+
$firstDay = $year.'-0'.$month.'-01';
92+
$lastDay = $year.'-0'.$month.'-'.$mday;
93+
} else {
94+
$time = strtotime($date);
95+
$firstDay = date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)+1).'-01'));
96+
$lastDay = date('Y-m-d',strtotime("$firstDay +1 month -1 day"));
97+
}
98+
99+
return [$firstDay,$lastDay];
100+
}
101+
102+
/**
103+
* 获得几天前,几小时前,几月前
104+
* @param $time
105+
* @param null|array $unit
106+
* @return string
107+
*/
108+
public static function before($time,$unit=null)
109+
{
110+
if (!is_int($time)) {
111+
return false;
112+
}
113+
114+
$unit = $unit ?: ['','','星期','','小时','分钟',''];
115+
$nowTime = time();
116+
117+
switch(true) {
118+
case $time<($nowTime - 31536000):
119+
return floor(($nowTime - $time)/31536000).$unit[0];
120+
case $time<($nowTime - 2592000):
121+
return floor(($nowTime - $time)/2592000).$unit[1];
122+
case $time<($nowTime - 604800):
123+
return floor(($nowTime - $time)/604800).$unit[2];
124+
case $time<($nowTime - 86400):
125+
return floor(($nowTime - $time)/86400).$unit[3];
126+
case $time<($nowTime - 3600):
127+
return floor(($nowTime - $time)/3600).$unit[4];
128+
case $time<($nowTime - 60):
129+
return floor(($nowTime - $time)/60).$unit[5];
130+
default:
131+
return floor($nowTime - $time).$unit[6];
132+
}
133+
}
16134

17-
}
135+
}

src/helpers/FormatHelper.php

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99

1010
namespace inhere\librarys\helpers;
1111

12-
13-
use inhere\librarys\traits\TraitJsonFormat;
14-
12+
/**
13+
* Class FormatHelper
14+
* @package inhere\librarys\helpers
15+
*/
1516
class FormatHelper
1617
{
17-
use TraitJsonFormat;
18-
19-
2018
/**
2119
* Replaces &amp; with & for XHTML compliance
2220
*
@@ -57,52 +55,4 @@ static public function cleanText(&$text)
5755

5856
return $text;
5957
}
60-
61-
/**
62-
* Strip img-tags from string
63-
*
64-
* @param string $string Sting to be cleaned.
65-
*
66-
* @return string Cleaned string
67-
*/
68-
static public function stripImages($string)
69-
{
70-
return preg_replace('#(<[/]?img.*>)#U', '', $string);
71-
}
72-
73-
/**
74-
* Strip iframe-tags from string
75-
*
76-
* @param string $string Sting to be cleaned.
77-
*
78-
* @return string Cleaned string
79-
*/
80-
static public function stripIframes($string)
81-
{
82-
return preg_replace('#(<[/]?iframe.*>)#U', '', $string);
83-
}
84-
85-
/**
86-
* stripScript
87-
*
88-
* @param string $string
89-
*
90-
* @return mixed
91-
*/
92-
static public function stripScript($string)
93-
{
94-
return preg_replace("'<script[^>]*>.*?</script>'si", '', $string);
95-
}
96-
97-
/**
98-
* stripStyle
99-
*
100-
* @param string $string
101-
*
102-
* @return mixed
103-
*/
104-
static public function stripStyle($string)
105-
{
106-
return preg_replace("'<style[^>]*>.*?</style>'si", '', $string);
107-
}
108-
}// end class FormatHelper
58+
}// end class FormatHelper

0 commit comments

Comments
 (0)