林中侠客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1 <?php
  2 //公共函数
  3 
  4 /**
  5 * 根据经纬度计算距离
  6 *
  7 * @access protected
  8 * @param float $lng1
  9 * @param float $lat1
 10 * @param float $lng2
 11 * @param float $lat2
 12 * @retrun float $s 距离,单位米
 13 */
 14 function pointDistance($lng1,$lat1,$lng2,$lat2){
 15 //将角度转为狐度
 16 $radLat1 = deg2rad($lat1);
 17 $radLat2 = deg2rad($lat2);
 18 $radLng1 = deg2rad($lng1);
 19 $radLng2 = deg2rad($lng2);
 20 $a = $radLat1 - $radLat2;//两纬度之差,纬度<90
 21 $b = $radLng1 - $radLng2;//两经度之差纬度<180
 22 $s = 2*asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137;
 23 return round(1000*$s,0);
 24 }
 25 
 26 /**
 27 * 加密密码
 28 *
 29 * @param string $password
 30 */
 31 function encryptPassword($password){
 32 return md5($password);
 33 }
 34 
 35 /**
 36 * 生成唯一码
 37 *
 38 * @param void
 39 * @return string
 40 */
 41 function buildUniqueId(){
 42 return strtoupper(md5(uniqid(rand(),true)));
 43 }
 44 
 45 /**
 46 * 求日期距离
 47 *
 48 * @param string $startdate
 49 * @param string $enddate
 50 * @return array('year'=>'',month=>'','day'=>'')
 51 */
 52 function dateDistance($startdate,$enddate){
 53 $startdate = date("Y-m-d",strtotime($startdate));
 54 $enddate = date("Y-m-d",strtotime($enddate));
 55 if($startdate>$enddate)
 56 {
 57 $startdate = $startdate^$enddate;
 58 $enddate = $enddate^$startdate;
 59 $startdate = $startdate^$enddate;
 60 }
 61 $s = explode('-',$startdate);
 62 $e = explode('-',$enddate);
 63 $y = $e[0] - $s[0];
 64 if($s[1].$s[2]>$e[1].$e[2]) $y--;
 65 $m = $s[1]>$e[1] ? $e[1] + 12 - $s[1] : $e[1] - $s[1];
 66 if($s[2]>$e[2]) $m--;
 67 $d = $s[2]>$e[2] ? date("t",strtotime($startdate))-$s[2]+$e[2] : $e[2] - $s[2];
 68 return array('year'=>$y,'month'=>$m,'day'=>$d);
 69 }
 70 
 71 /**
 72 * 解压zip
 73 *
 74 * @param string $path 文件
 75 * @param string $unzipto 解压目录
 76 * @return boolean 结果
 77 */
 78 function unzip($path,$unzipto){
 79 $zip = new ZipArchive;
 80 $res = $zip->open($path);
 81 if ($res === true){
 82 $zip->extractTo($unzipto);
 83 $zip->close();
 84 return true;
 85 } else {
 86 return false;
 87 }
 88 }
 89 
 90 /**
 91 * 检查并创建目录
 92 *
 93 * @param srring $path
 94 * @param string $mode
 95 * @return boolean
 96 */
 97 function checkDir($path,$mode='0777'){
 98 if (is_dir($path)) return true;
 99 return mkdir($path,$mode,true);
100 }
101 
102 /**
103 * unicode 转中文
104 *
105 * @param string $str
106 * @return string
107 */
108 function unescape($str) {
109 $str=str_replace("\\","%",$str);
110 //$str = rawurldecode($str);
111 preg_match_all("/(?:%u.{4})|.{4};|&#\d+;|.+/U",$str,$r);
112 $ar = $r[0];
113 foreach($ar as $k=>$v) {
114 if(substr($v,0,2) == "%u"){
115 $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));
116 }elseif(substr($v,0,3) == ""){
117 $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,3,-1)));
118 }elseif(substr($v,0,2) == "&#") {
119 $ar[$k] = iconv("UCS-2","GB2312",pack("n",substr($v,2,-1)));
120 }
121 }
122 $str = join("",$ar);
123 $str = str_replace("%/","/",$str);
124 return iconv('GB2312','utf-8',$str);
125 }
126 
127 /**
128 * 根据ip查询地址
129 *
130 * @param string $ip
131 * @return mixed
132 */
133 function ipToAddress($ip=null) {
134 if(!$ip) return false;
135 //$ip = '202.198.16.3';
136 $url = 'http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip='.$ip;
137 $result = httpGet($url,null,2000);
138 $result = json_decode($result,true);
139 if(!$result){
140 return false;
141 }elseif($result['status']){
142 return false;
143 }else{
144 return $result['content']['address_detail'];//数组
145 }
146 }
147 
148 /**
149 * 获取客户端IP
150 *
151 * @access public
152 *
153 * @param string $default 默认IP
154 *
155 * @return string
156 */
157 function clientIp($default = '0.0.0.0') {
158 $keys = array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR');
159 foreach ($keys as $key) {
160 if (!isset($_SERVER[$key]) || !$_SERVER[$key]) {
161 continue;
162 }
163 return htmlspecialchars($_SERVER[$key]);
164 }
165 return $default;
166 }
167 
168 /**
169 * 验证手机号码
170 *
171 * @param string $mobile
172 * @return bool
173 */
174 function isMobile($mobile){
175 if(preg_match('/^1([0-9]{10})/',$mobile)){//只验证位数
176 return true;
177 }else{
178 return false;
179 }
180 }
181 
182 /**
183 * 验证邮箱
184 *
185 * @param string $email
186 * @return boolean
187 */
188 function isEmail($email=null){
189 if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
190 return false;
191 }else{
192 return true;
193 }
194 }
195 
196 /**
197 * 验证是否为网址
198 *
199 * @param string $email
200 * @return boolean
201 */
202 function isUrl($url=null){
203 if(!filter_var($url, FILTER_VALIDATE_URL)){
204 return false;
205 }else{
206 return true;
207 }
208 }
209 
210 /**
211 * 验证用户名 长度6-16,字母加数字
212 *
213 * @param string $username
214 * @author yeweihua
215 * @return boolean
216 */
217 function isUserName($username=null){
218 if(preg_match('/^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i', $username)){
219 return true;
220 }else{
221 return false;
222 }
223 }
224 
225 /**
226 * 验证密码 长度6-16,字母加数字
227 *
228 * @param string $username
229 * @author yeweihua
230 * @return boolean
231 */
232 function isPassword($password=null){
233 if(preg_match('/^[a-zA-Z0-9]{6,16}$/i', $password)){
234 return true;
235 }else{
236 return false;
237 }
238 }
239 
240 /**
241 * @param $data
242 * 原型输出
243 */
244 function P($data){
245 echo "<pre>";
246 print_r($data);
247 echo "</pre>";
248 }
249 
250 /**
251 * @param $data
252 * 检查是否为空
253 */
254 function isnull(&$data){
255 if(is_array($data)){
256 if(array_is_null($data)){
257 $data=array();
258 }
259 }else{
260 if(empty($data)){
261 $data=array();
262 }
263 }
264 }
265 
266 /**
267 * @param null $arr
268 * @return bool
269 * 判断多维数组是否为空
270 */
271 function array_is_null($arr = null){
272 if(is_array($arr)){
273 foreach($arr as $k=>$v){
274 if($v&&!is_array($v)){
275 return false;
276 }
277 $t = array_is_null($v);
278 if(!$t){
279 return false;
280 }
281 }
282 return true;
283 }elseif(!$arr){
284 return true;
285 }else{
286 return false;
287 }
288 }
289 
290 /**
291 * http get请求
292 *
293 * @param string $url
294 * @param mixed $param
295 * @param int $time
296 * @author yeweihua
297 * @return mixed
298 */
299 function httpGet($url,$param = null,$time = 5000){
300 $url = empty($param) ? $url : $url .'?'. http_build_query($param);
301 $ch = curl_init();
302 // set URL and other appropriate options
303 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不验证证书
304 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不验证host
305 curl_setopt($ch, CURLOPT_URL, $url);
306 curl_setopt($ch, CURLOPT_HEADER, 0);
307 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS,500);//设置时间
308 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
309 curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
310 curl_setopt($ch, CURLOPT_TIMEOUT_MS, $time);
311 //curl_setopt($ch, CURLOPT_TIMEOUT, $time);//设置时间
312 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出
313 // grab URL and pass it to the browser
314 $result = curl_exec($ch);
315 if (curl_errno($ch)) {
316 //echo 'Errno2'.curl_error($ch);//捕抓异常
317 return false;
318 }
319 // close cURL resource, and free up system resources
320 curl_close($ch);
321 return $result;
322 }
323 
324 /**
325 * http post 请求
326 *
327 * @param string $url
328 * @param mixed $data
329 * @param int $time
330 * @author yeweihua
331 * @return mixed
332 */
333 function httpPost($url,$data,$time=5000){
334 if(1){
335 $log = date('Y-m-d H:i:s',time())."\r\n".$url."\r\n";
336 $log .= is_array($data) ? "post:".json_encode($data)."\r\n" : $data;
337 $log .= "\r\n";
338 file_put_contents('./App/Runtime/Logs/push'.date('m-d').'.log',$log,FILE_APPEND);
339 }
340 
341 $data = is_array($data) ? http_build_query($data) : $data;
342 $ch = curl_init($url) ;
343 curl_setopt($ch, CURLOPT_POST, 1);
344 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不验证证书
345 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不验证host
346 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
347 curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
348 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
349 curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
350 curl_setopt($ch, CURLOPT_TIMEOUT_MS, $time);
351 //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
352 $result = curl_exec($ch) ;
353 if (curl_errno($ch)) {
354 //echo 'Errno2'.curl_error($ch);//捕抓异常
355 return false;
356 }
357 curl_close($ch) ;
358 return $result;
359 }
360 
361 /**
362 * 验证是否为合法的日期格式
363 *
364 * @param string $datetime
365 * @author yeweihua
366 * @return boolean
367 */
368 function isDatetime($datetime){
369 $datetime = date("Y-m-d H:i:s",strtotime($datetime));
370 return $datetime=='1970-01-01 08:00:00' ? false : true;
371 }
372 
373 /**
374 * 获取设备类型
375 *
376 * @param void
377 * @return string
378 */
379 protected function getDeviceType(){
380 $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
381 $type = 'web';
382 if(strpos($agent, 'iphone') || strpos($agent, 'ipad')){
383 $type = 'ios';
384 }
385 if(strpos($agent, 'android')){
386 $type = 'android';
387 }
388 return $type;
389 }
390 
391 /**
392 * 计算两个时间是否连续 连续返回true
393 * $beforeday 上一个时间
394 * $day 下一个时间
395 */
396 public function iscontinuous($beforeday, $day){
397 $nextday = strtotime(date("Y-m-d", strtotime("+1 day", $beforeday)));
398 $newday = strtotime(date("Y-m-d", $day));
399 if ($nextday == $newday) {
400 return true;
401 } else {
402 return false;
403 }
404 }
405 
406 /**
407 * 系统邮件发送函数
408 *
409 * @param string $to 接收邮件者邮箱
410 * @param string $name 接收邮件者名称
411 * @param string $subject 邮件主题
412 * @param string $body 邮件内容
413 * @param string $attachment 附件列表
414 * @return boolean
415 */
416 function sendMail($to, $name, $subject = '', $body = '', $attachment = null, $config = '') {
417 $config = is_array($config) ? $config : C('SDK_EMAIL');
418 Vendor('PHPMailer.phpmailer#class'); //导入文件
419 $mail = new PHPMailer(); //PHPMailer对象
420 $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
421 $mail->IsSMTP(); // 设定使用SMTP服务
422 //$mail->IsHTML(true);
423 $mail->SMTPDebug = 0; // 关闭SMTP调试功能 1 = errors and messages2 = messages only
424 $mail->SMTPAuth = true; // 启用 SMTP 验证功能
425 if ($config['smtp_port'] == 465)
426 $mail->SMTPSecure = 'ssl'; // 使用安全协议
427 $mail->Host = $config['smtp_host']; // SMTP 服务器
428 $mail->Port = $config['smtp_port']; // SMTP服务器的端口号
429 $mail->Username = $config['smtp_user']; // SMTP服务器用户名
430 $mail->Password = $config['smtp_pass']; // SMTP服务器密码
431 $mail->SetFrom($config['from_email'], $config['from_name']);
432 $replyEmail = $config['reply_email'] ? $config['reply_email'] : $config['reply_email'];
433 $replyName = $config['reply_name'] ? $config['reply_name'] : $config['reply_name'];
434 $mail->AddReplyTo($replyEmail, $replyName);
435 $mail->Subject = $subject;
436 $mail->MsgHTML($body);
437 $mail->AddAddress($to, $name);
438 if (is_array($attachment)) { // 添加附件
439 foreach ($attachment as $file) {
440 if (is_array($file)) {
441 is_file($file['path']) && $mail->AddAttachment($file['path'], $file['name']);
442 } else {
443 is_file($file) && $mail->AddAttachment($file);
444 }
445 }
446 } else {
447 is_file($attachment) && $mail->AddAttachment($attachment);
448 }
449 return $mail->Send() ? true : $mail->ErrorInfo;
450 }
451 
452 /**
453 * 发送验证短信,同时把记录存到数据库
454 *
455 * @access protected
456 * @param string $mobile 手机号码
457 * @param string $info 内容
458 * @param string $verify 特殊内容
459 * @author yeweihua
460 * @return mixed
461 */
462 function sendSms($mobile,$content='',$verify='',$config=NULL){
463 $smsApi = is_array($config) ? $config : C('SDK_SMS');
464 $url = 'http://www.lanz.net.cn/LANZGateway/DirectSendSMSs.asp';
465 $content = iconv("UTF-8","GBK",$content);
466 unset($smsApi['pwd']);
467 $smsApi['SMSType'] = 1;
468 $smsApi['Content'] = $content;
469 $smsApi['Phones'] = $mobile;
470 $smsApi['sendDate'] = '';
471 $smsApi['sendtime'] = '';
472 $result = httpGet($url,$smsApi,2000);
473 if($result===false) return false;
474 $result = simplexml_load_string($result);
475 $content = iconv("GBK","UTF-8",$content);
476 $status = (string) $result->ErrorNum;
477 $data = array('content'=>$content,'verify'=>$verify,'mobile'=>$mobile,'sendtime'=>time(),'status'=>$status);
478 D('Sms')->data($data)->add();
479 if($status=='0'){
480 return true;
481 }else{
482 return false;
483 }
484 }

 

posted on 2014-11-10 15:53  林中侠客  阅读(816)  评论(0)    收藏  举报