Res.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace WY\app\libs;
  3. use WY\app\libs\Mailer\PHPMailer;
  4. if (!defined('WY_ROOT')) {
  5. exit;
  6. }
  7. class Res
  8. {
  9. static function sendMail($subject, $mailset)
  10. {
  11. if (!$subject || !$mailset) {
  12. return false;
  13. }
  14. $mail = new PHPMailer();
  15. $mail->IsSMTP();
  16. $mail->SMTPAuth = true;
  17. $mail->SMTPSecure = 'ssl';
  18. $mail->Port = 465;
  19. $mail->CharSet = "UTF-8";
  20. $mail->Username = $mailset['smtp_email'];
  21. $mail->Password = $mailset['smtp_pwd'];
  22. $mail->Host = $mailset['smtp_server'];
  23. $mail->IsHTML(true);
  24. $mail->From = $mailset['smtp_email'];
  25. $mail->FromName = $mailset['sitename'];
  26. $mail->Subject = $subject['title'];
  27. $mail->Body = $subject['content'];
  28. $mail->AddAddress($subject['email'], $subject['email']);
  29. $result = $mail->Send();
  30. if($result){
  31. return '成功'.$result;
  32. }else{
  33. return '失败:'.$mail->ErrorInfo;
  34. }
  35. }
  36. static function getHttpStatusCode($url)
  37. {
  38. $curl = curl_init();
  39. curl_setopt($curl, CURLOPT_URL, $url);
  40. curl_setopt($curl, CURLOPT_HEADER, 1);
  41. curl_setopt($curl, CURLOPT_NOBODY, 1);
  42. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  43. curl_setopt($curl, CURLOPT_TIMEOUT, 5);
  44. curl_exec($curl);
  45. $rtn = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  46. curl_close($curl);
  47. return $rtn;
  48. }
  49. static function exportFile($filename, $content)
  50. {
  51. $ua = _S('HTTP_USER_AGENT');
  52. $ext = substr($filename, -4);
  53. $encoded_filename = urlencode($filename);
  54. $encoded_filename = str_replace("+", "%20", $encoded_filename);
  55. header('Pragam:no-cache');
  56. header('Expires:0');
  57. header('Content-Type: application/octet-stream');
  58. if ($ext == '.xls') {
  59. header("Content-type:application/vnd.ms-excel;charset=utf8");
  60. }
  61. if (preg_match("/MSIE/", $ua)) {
  62. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  63. } else {
  64. header('Content-Disposition: attachment; filename="' . $filename . '"');
  65. }
  66. if ($ext == '.xls') {
  67. echo $content;
  68. } else {
  69. echo mb_convert_encoding($content, 'gbk', 'utf-8');
  70. }
  71. exit;
  72. }
  73. //ip查询
  74. static function getIPLoc($queryIP){
  75. $url = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$queryIP;
  76. $ch = curl_init($url);
  77. curl_setopt($ch,CURLOPT_ENCODING ,'utf8');
  78. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  79. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
  80. $location = curl_exec($ch);
  81. $location = json_decode($location);
  82. curl_close($ch);
  83. $loc = "";
  84. if($location===FALSE) return "";
  85. if (empty($location->area)) {
  86. //$loc = $location->country.' '.$location->province.' '.$location->city.' '.$location->district.' '.$location->isp;
  87. //$loc = '女儿国'.' '.'悟空省'.' '.'八戒市'.' '.'沙僧区'.' '.'6.6.6.6';
  88. $loc = $location->data->country.' '.$location->data->region.' '.$location->data->city.' '.$location->data->county.' '.$location->data->ip;
  89. }else{
  90. $loc = $location->area;
  91. }
  92. return $loc;
  93. }
  94. //juhesms
  95. static function juhecurl($params=false,$ispost=0){
  96. $url = 'http://v.juhe.cn/sms/send';
  97. $httpInfo = array();
  98. $ch = curl_init();
  99. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  100. curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
  101. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
  102. curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
  103. curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  104. if( $ispost )
  105. {
  106. curl_setopt( $ch , CURLOPT_POST , true );
  107. curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  108. curl_setopt( $ch , CURLOPT_URL , $url );
  109. }
  110. else
  111. {
  112. if($params){
  113. curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  114. }else{
  115. curl_setopt( $ch , CURLOPT_URL , $url);
  116. }
  117. }
  118. $response = curl_exec( $ch );
  119. if ($response === FALSE) {
  120. //echo "cURL Error: " . curl_error($ch);
  121. return false;
  122. }
  123. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  124. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  125. curl_close( $ch );
  126. return $response;
  127. }
  128. static function getRandomString($len)
  129. {
  130. $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
  131. $charsLen = count($chars) - 1;
  132. shuffle($chars);
  133. $output = "";
  134. for ($i = 0; $i < $len; $i++) {
  135. $output .= $chars[mt_rand(0, $charsLen)];
  136. }
  137. $output = substr(md5(md5(uniqid()) . md5(microtime()) . md5($output)), 0, $len);
  138. return $output;
  139. }
  140. static function isMail($email)
  141. {
  142. return preg_match('/^([0-9a-zA-Z_-])+@([0-9a-zA-Z_-])+((\\.[0-9a-zA-Z_-]{2,3}){1,2})$/', $email);
  143. }
  144. static function subString($strings, $start, $length)
  145. {
  146. if (function_exists('mb_substr')) {
  147. return mb_substr($strings, $start, $length, 'utf8');
  148. }
  149. $str = substr($strings, $start, $length);
  150. $char = 0;
  151. for ($i = 0; $i < strlen($str); $i++) {
  152. if (ord($str[$i]) >= 128) {
  153. $char++;
  154. }
  155. }
  156. $str2 = substr($strings, $start, $length + 1);
  157. $str3 = substr($strings, $start, $length + 2);
  158. if ($char % 3 == 1) {
  159. if ($length <= strlen($strings)) {
  160. $str3 = $str3 .= '...';
  161. }
  162. return $str3;
  163. }
  164. if ($char % 3 == 2) {
  165. if ($length <= strlen($strings)) {
  166. $str2 = $str2 .= '...';
  167. }
  168. return $str2;
  169. }
  170. if ($char % 3 == 0) {
  171. if ($length <= strlen($strings)) {
  172. $str = $str .= '...';
  173. }
  174. return $str;
  175. }
  176. }
  177. static function fTime($time, $type = 0)
  178. {
  179. if ($type) {
  180. return strtotime($time);
  181. }
  182. return date('Y-m-d H:i:s', $time);
  183. }
  184. static function cTime($time)
  185. {
  186. $time = is_numeric($time) ? $time : strtotime($time);
  187. $now = time();
  188. $result = $now - $time;
  189. if ($result < 60) {
  190. return $result . '秒前';
  191. }
  192. if ($result / 60 < 60) {
  193. return intval($result / 60) . '分钟前';
  194. }
  195. if ($result / 60 / 60 < 24) {
  196. return intval($result / 60 / 60) . '小时 ' . ceil(($result / 3600 - intval($result / 60 / 60)) * 60) . '分钟前';
  197. }
  198. if ($result / 60 / 60 / 24 < 365) {
  199. return intval($result / 60 / 60 / 24) . '天前';
  200. }
  201. return date('Y-m-d H:i:s', $time);
  202. }
  203. static function redirect($url)
  204. {
  205. header('location:' . $url);
  206. exit;
  207. }
  208. static function isMobile()
  209. {
  210. if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
  211. return true;
  212. }
  213. if (isset($_SERVER['HTTP_VIA'])) {
  214. }
  215. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  216. $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile', 'ios');
  217. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  218. return true;
  219. }
  220. }
  221. if (isset($_SERVER['HTTP_ACCEPT'])) {
  222. if (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))) {
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. static function getOrderID()
  229. {
  230. return date('Y') . date('m') . date('d') . date('H') . date('i') . date('s') . mt_rand(10000, 99999); //平台ID
  231. }
  232. static function replaceMailTpl($mailtpl, $data = array())
  233. {
  234. if (!$mailtpl) {
  235. return false;
  236. }
  237. if ($data) {
  238. foreach ($data as $key => $val) {
  239. $title = str_replace('{' . $key . '}', $val, isset($title) ? $title : $mailtpl['title']);
  240. $content = str_replace('{' . $key . '}', $val, isset($content) ? $content : $mailtpl['content']);
  241. }
  242. return array('title' => $title, 'content' => $content);
  243. }
  244. return $mailtpl;
  245. }
  246. }
  247. ?>