|
|
@@ -0,0 +1,277 @@ |
|
|
|
<?php |
|
|
|
/** |
|
|
|
* Created by PhpStorm. |
|
|
|
* User: Steven |
|
|
|
* Date: 2016/10/8 |
|
|
|
* Time: 17:02 |
|
|
|
*/ |
|
|
|
class zzcsUtils |
|
|
|
{ |
|
|
|
static function writeLog($log) |
|
|
|
{ |
|
|
|
$dir = __DIR__ . "/../Log/"; |
|
|
|
if (!is_dir($dir)) { |
|
|
|
mkdir($dir); |
|
|
|
} |
|
|
|
$filename = $dir . date("Y-m-d") . ".log"; |
|
|
|
$need_chmod = file_exists($filename); |
|
|
|
$log_str = '[' . date("Y-m-d H:i:s") . ']'; |
|
|
|
file_put_contents($filename, $log_str . PHP_EOL, FILE_APPEND); |
|
|
|
if ($need_chmod == false) { |
|
|
|
@chmod($filename, 0777); |
|
|
|
} |
|
|
|
} |
|
|
|
static function httpRequest($url, $data = null) |
|
|
|
{ |
|
|
|
$ch = curl_init(); |
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
|
|
if (!empty($data)) { |
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
|
|
|
} |
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
|
|
$output = curl_exec($ch); |
|
|
|
curl_close($ch); |
|
|
|
return $output; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送HTTP请求 |
|
|
|
* @param $url |
|
|
|
* @param array $param |
|
|
|
* @return mixed |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
static function httpsPost($url, $param = array()) |
|
|
|
{ |
|
|
|
$ch = curl_init(); // 初始化一个 cURL 对象 |
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); // 设置需要抓取的URL |
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); // // 设置header |
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 |
|
|
|
// 如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用。 |
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); // 传递一个作为HTTP “POST”操作的所有数据的字符串。//http_build_query:生成 URL-encode 之后的请求字符串 |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
|
|
|
'Content-type:application/x-www-form-urlencoded;charset=utf-8' |
|
|
|
)); |
|
|
|
$rtn = curl_exec($ch); // 运行cURL,请求网页 |
|
|
|
if ($errno = curl_errno($ch)) { |
|
|
|
throw new Exception ('Curl Error(' . $errno . '):' . curl_error($ch)); |
|
|
|
} |
|
|
|
curl_close($ch); // 关闭URL请求 |
|
|
|
return $rtn; // 返回获取的数据 |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 字段验证 |
|
|
|
* @params String 字段名,支持格式'attr1,attr2,attr3' |
|
|
|
* @post Array 被验证的数组,默认为$_POST |
|
|
|
*/ |
|
|
|
static function validateParams($params, $post = '') |
|
|
|
{ |
|
|
|
if ($post == '') |
|
|
|
$post = $_POST; |
|
|
|
$result = array( |
|
|
|
'status' => true, |
|
|
|
'info' => '' |
|
|
|
); |
|
|
|
foreach ($params as $attr => $type) { |
|
|
|
$list = explode(',', $attr); |
|
|
|
foreach ($list as $item) { |
|
|
|
$item = trim($item); |
|
|
|
if($item != ''){ |
|
|
|
if (!isset($post[$item]) || $post[$item] == '') { |
|
|
|
$result['status'] = false; |
|
|
|
$result['info'] = '请填写所有必填项'; |
|
|
|
return $result; |
|
|
|
} elseif (zzcsUtils::validate($post[$item], $type)) { |
|
|
|
continue; |
|
|
|
|
|
|
|
} else { |
|
|
|
$result['status'] = false; |
|
|
|
$result['info'] = ' 数据格式不正确'; |
|
|
|
return $result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 验证字段是否合法,URL必须带http(s),合法return true,非法return false |
|
|
|
* @input mixed需要验证的参数 |
|
|
|
* @type 需要的方法,Number, Float, Integer, ID, Email, Phone, URL, ShenFenZheng, |
|
|
|
*/ |
|
|
|
static function validate($input, $type = '') |
|
|
|
{ |
|
|
|
$typeArr = explode(',', $type); |
|
|
|
foreach ($typeArr as $type) { |
|
|
|
switch (strtolower($type)) { |
|
|
|
case 'amount': |
|
|
|
$reg = '/^([1-9]\d*\.?\d{0,2}$)|0\.?\d{0,2}$/'; |
|
|
|
$message = '金额'; |
|
|
|
break; |
|
|
|
case 'number': |
|
|
|
$reg = '/^-?[0-9]*$/'; |
|
|
|
$message = '数字'; |
|
|
|
break; |
|
|
|
case 'email': |
|
|
|
$reg = '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/'; |
|
|
|
$message = '邮箱'; |
|
|
|
break; |
|
|
|
case 'telorphone': //正则不正确,需再完善 |
|
|
|
$reg = '/^(d{3}-\d{8}|\d{4}-\d{7})|((0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8})$/'; |
|
|
|
$message = '电话'; |
|
|
|
break; |
|
|
|
case 'tel': |
|
|
|
$reg = '/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/'; |
|
|
|
$message = '手机'; |
|
|
|
break; |
|
|
|
case 'phone': |
|
|
|
$reg = '/^d{3}-\d{8}|\d{4}-\d{7}$/'; |
|
|
|
$message = '电话'; |
|
|
|
break; |
|
|
|
case 'url': |
|
|
|
$reg = '/^https?:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])$/i'; |
|
|
|
$message = '链接'; |
|
|
|
break; |
|
|
|
case 'card': |
|
|
|
$reg = '/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/'; |
|
|
|
$message = '身份证'; |
|
|
|
break; |
|
|
|
case 'dcode': |
|
|
|
$reg = '/^[1-9]\d{5}(?!\d)$/'; |
|
|
|
$message = '邮编'; |
|
|
|
break; |
|
|
|
case 'time': |
|
|
|
$reg = '/^(([0-1][0-9])|(2[0-3])):([0-5][0-9])$/'; |
|
|
|
$message = '时间'; |
|
|
|
break; |
|
|
|
case 'empty': |
|
|
|
default: |
|
|
|
$reg = '/^[\s\S]*$/'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$result = preg_match($reg, $input); |
|
|
|
if ($result == 0) |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 上传多张图片,不确定图片张数, |
|
|
|
* 图片路径以逗号分隔, |
|
|
|
* |
|
|
|
*/ |
|
|
|
static function uploadMultiple($name, $dirPath = '../coding/resource/upload/car/', $realPath = '../../resource/upload/car/') |
|
|
|
{ |
|
|
|
$maxSize = 2000000; |
|
|
|
$allow = array('image/gif', 'image/jpeg', 'image/png', 'image/pjpeg'); |
|
|
|
$path = ''; |
|
|
|
$type = ''; |
|
|
|
if (is_array($_FILES[$name]['name'])) { //多个文件 |
|
|
|
foreach ($_FILES[$name]['name'] as $key => $item) { |
|
|
|
//图片不为空,且图片对应的类型也不为空 |
|
|
|
if ($_POST['bus_img_type'][$key] != '' && $item != '' && in_array($_FILES[$name]['type'][$key], $allow) && $_FILES[$name]['size'][$key] <= $maxSize) { |
|
|
|
$newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$name]['name'][$key])); |
|
|
|
move_uploaded_file($_FILES[$name]["tmp_name"][$key], |
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . $dirPath . $newName); |
|
|
|
|
|
|
|
if ($path == '') { |
|
|
|
$path .= $realPath . $newName; |
|
|
|
$type .= $_POST['bus_img_type'][$key]; |
|
|
|
} else { |
|
|
|
$path .= ',' . $realPath . $newName; |
|
|
|
$type .= ',' . $_POST['bus_img_type'][$key]; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { //单个文件 |
|
|
|
|
|
|
|
} |
|
|
|
if ($path != '') |
|
|
|
return array('path' => $path, 'type' => $type); |
|
|
|
else |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* 上传文件 |
|
|
|
* @type 上传类型,主要是图片,还有附件rar, word, excel,还没有需求 |
|
|
|
*/ |
|
|
|
static function upload($type, $file) |
|
|
|
{ |
|
|
|
if ((($_FILES[$file]["type"] == "image/gif") |
|
|
|
|| ($_FILES[$file]["type"] == "image/jpeg") |
|
|
|
|| ($_FILES[$file]["type"] == "image/png") |
|
|
|
|| ($_FILES[$file]["type"] == "image/pjpeg")) |
|
|
|
&& ($_FILES[$file]["size"] < 200000) |
|
|
|
) { |
|
|
|
if ($_FILES[$file]["error"] > 0) { |
|
|
|
return ''; |
|
|
|
} else { |
|
|
|
$path = '../upload/car/'; |
|
|
|
$newName = zzcsUtils::initFileName($path, zzcsUtils::getSuffix($_FILES[$file]["name"])); |
|
|
|
move_uploaded_file($_FILES[$file]["tmp_name"], |
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . $path . $newName); |
|
|
|
return $path . $newName; |
|
|
|
} |
|
|
|
} else { |
|
|
|
return ''; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 在upload目录上传文件时,初始化一个不存在的文件名 |
|
|
|
*/ |
|
|
|
static function initFileName($path, $suffix) |
|
|
|
{ |
|
|
|
$flag = true; |
|
|
|
|
|
|
|
while ($flag) { |
|
|
|
$name = time() . rand(10000, 99999); |
|
|
|
if (!file_exists("$path$name$suffix")) { |
|
|
|
$flag = false; |
|
|
|
} |
|
|
|
} |
|
|
|
return $name . $suffix; |
|
|
|
} |
|
|
|
|
|
|
|
static function getSuffix($imgName)//获取图像文件类型 |
|
|
|
{ |
|
|
|
if (preg_match("/\.(jpg|jpeg|gif|png|bmp)$/i", $imgName, $matches)) { |
|
|
|
$type = strtolower($matches[0]); |
|
|
|
} else { |
|
|
|
$type = "string"; |
|
|
|
} |
|
|
|
return $type; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 删除车辆之前存放的图片 |
|
|
|
*/ |
|
|
|
static function deleteFiles($oldPath, $newPath) |
|
|
|
{ |
|
|
|
if (is_string($oldPath)) |
|
|
|
$oldPath = explode(',', $oldPath); |
|
|
|
if (is_string($newPath)) |
|
|
|
$newPath = explode(',', $newPath); |
|
|
|
foreach ($oldPath as $item) { |
|
|
|
if (!in_array($item, $newPath)) { |
|
|
|
if (file_exists($item) && is_file($item)) |
|
|
|
unlink($item); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |