commonFun.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. if (get_magic_quotes_gpc ()) {
  3. function stripslashes_deep($value) {
  4. $value = is_array ( $value ) ? array_map ( 'stripslashes_deep', $value ) : stripslashes ( $value );
  5. return $value;
  6. }
  7. $_POST = array_map ( 'stripslashes_deep', $_POST );
  8. $_GET = array_map ( 'stripslashes_deep', $_GET );
  9. $_COOKIE = array_map ( 'stripslashes_deep', $_COOKIE );
  10. }
  11. /**
  12. * 裁剪字符串
  13. * @param unknown $str
  14. * @param unknown $len
  15. * @return string
  16. */
  17. function cutString($str,$len){
  18. $str=strip_tags($str);
  19. $strLen=strlen($str);
  20. //$str=$strLen.$str;
  21. if($strLen<=$len*3){
  22. return $str;
  23. }
  24. return mb_substr($str,0,$len,'utf-8')."...";
  25. }
  26. /**
  27. * 序列化
  28. * @param unknown $obj
  29. * @return string
  30. */
  31. function my_serialize($obj) {
  32. return base64_encode ( gzcompress ( serialize ( $obj ) ) );
  33. }
  34. // 反序列化
  35. /**
  36. * 反序列化
  37. * @param unknown $txt
  38. * @return mixed
  39. */
  40. function my_unserialize($txt) {
  41. return unserialize ( gzuncompress ( base64_decode ( $txt ) ) );
  42. }
  43. /**
  44. * 获取参数
  45. *
  46. * @param unknown $key
  47. * @param unknown $df_value
  48. */
  49. function request($key, $df_value = '') {
  50. return isset ( $_REQUEST [$key] ) ? trim ( $_REQUEST [$key] ) : $df_value;
  51. }
  52. /**
  53. * 获取参数,先get,后post,最后cookie
  54. * @param unknown $key 参数名
  55. * @return unknown|mixed|string
  56. */
  57. function getParam($key){
  58. //$str="oetCntxEFGHI22Ko4CDqQCoxjAB";
  59. $val=request($key);
  60. if(empty($val)){
  61. $val=cookie($key);
  62. }
  63. return $val;
  64. }
  65. /**
  66. * 输出图片。根据需求自动裁剪图片
  67. *
  68. * @param unknown $file
  69. * @param number $width
  70. * @param number $height
  71. * @param string $def
  72. * @return unknown|mixed 使用例子:<img src="{:img('/wyzxqy/Tools/Upload/../../Upload/image/20150529/20150529103500_39141.jpg',90,90)}" />
  73. */
  74. function img($file, $width = 200, $height = 200, $def = '') {
  75. if (preg_match ( '/^http:\/\//', $file )) {
  76. // 如果是远程文件
  77. return $file;
  78. }
  79. //获得文件扩展名
  80. $temp_arr = explode(".", $file);
  81. $file_ext = array_pop($temp_arr);
  82. $file_ext = trim($file_ext);
  83. $file_ext = strtolower($file_ext);
  84. $baseFile = basename ( $file ); // 找到文件名
  85. $basePath = str_replace ( $baseFile, "", $file ) . "temp/"; // 找到目录
  86. $baseFile = str_replace ( ".", "", $baseFile ); // 替换掉“.”
  87. $baseFile .= $width . "x" . $height . ".jpg";
  88. $basePath = str_replace ( C ( 'VIR_DIR' ), ".", $basePath ); // 替换掉虚拟目录
  89. if (! is_readable ( $basePath )) { // 判断文件夹是否存在,不存在就创建
  90. is_file ( $basePath ) or mkdir ( $basePath, 0777 );
  91. }
  92. $baseFile = $basePath . $baseFile;
  93. // trace ( $baseFile, "basefiel" );
  94. $file = str_replace ( C ( 'VIR_DIR' ), ".", $file ); // 替换掉虚拟目录
  95. if (! file_exists ( $file )) { // 判断原文件是否存在,不存在直接返回。
  96. if (empty ( $def )) { // 如果没有默认图片
  97. return $file;
  98. } else {
  99. // trace($def,"def");
  100. return $def;
  101. }
  102. }
  103. //后缀名
  104. if($file_ext=="gif"){
  105. return $file;
  106. }
  107. if (! file_exists ( $baseFile )) { // 判断文件是否存在
  108. $image = new \Think\Image ();
  109. $image->open ( $file ); // 生成一个缩放后填充大小的缩略图并保存
  110. $image->thumb ( $width, $height, \Think\Image::IMAGE_THUMB_FILLED )->save ( $baseFile ); // 生成缩略图
  111. }
  112. $str2 = substr ( $baseFile, 0, 2 ); // 取前两个字符串
  113. if ($str2 == "./") {
  114. $baseFile = C ( 'VIR_DIR' ) . substr ( $baseFile, 1 ); // 取前两个字符串
  115. }
  116. return $baseFile;
  117. }
  118. /**
  119. * 从摘要和内容中显示关键字
  120. * @param unknown $descript
  121. * @param unknown $content
  122. * @param unknown $keyword
  123. */
  124. function getDescKey($descript,$content,$keyword){
  125. $startPre=80;//开始的位置
  126. $len=160;//长度
  127. //在摘要中找
  128. $index=intval( stripos($descript,$keyword));
  129. if($index>0){//如果摘要中就已经有关键字了。
  130. $begin = $index - $startPre > 0 ? $index - $startPre : 0;
  131. $descript = mb_strcut ( $descript, $begin, $len, "utf-8" ); // 视为字节流,utf-8下一个汉
  132. return showKeyword($keyword,$descript);
  133. }
  134. $content=strip_tags($content);
  135. $index=intval( stripos($content,$keyword));
  136. if($index>0){//如果正文中就已经有关键字了。
  137. $begin=$index-$startPre>0?$index-$startPre:0;
  138. $content= mb_strcut($content,$begin,$len,"utf-8");//视为字节流,utf-8下一个汉
  139. return showKeyword($keyword,$content);
  140. }
  141. return getDescript($descript,$content);//如果都没有关键字
  142. //echo intval( stripos($str,'斯'));
  143. //echo mb_strcut($str,4,10,"utf-8");//视为字节流,utf-8下一个汉
  144. }
  145. /**
  146. * 跳转
  147. *
  148. * @param unknown $url
  149. */
  150. function jumpUrl($url) {
  151. if (! empty ( $url )) {
  152. redirect ( $url );
  153. exit ();
  154. }
  155. }
  156. function simpleHtmlEncode($str){
  157. $str = str_replace ( "\r\n", "<br/>", $str ); // \r\n
  158. $str= str_replace ( "\r", "<br/>", $str ); // 替换"\r"
  159. $str= str_replace ( "\n", "<br/>", $str ); // 替换"\n"
  160. $str= str_replace ( " ", "&nbsp;", $str ); // 替换"\n"
  161. return $str;
  162. }
  163. function simpleHtmlDecode($str){
  164. $str=str_replace("<br/>","\r\n",$str);
  165. $str=str_replace("&nbsp;"," ",$str);
  166. return $str;
  167. }
  168. /**
  169. * 显示关键字
  170. * @param unknown $keyword
  171. * @param unknown $content
  172. * @return mixed
  173. */
  174. function showKeyword($keyword,$content){
  175. return str_replace($keyword,"<font class='red'>$keyword</font>",$content);
  176. }
  177. function sendMail($mailto,$subject,$content){
  178. vendor("Mail.smtp");
  179. $conditon['code']='email';
  180. $result=M('config')->where($conditon)->find();
  181. $model=unserialize($result['content']);
  182. //使用163邮箱服务器
  183. $smtpserver =$model['smtpserver'];// "smtp.qq.com";
  184. //163邮箱服务器端口
  185. $smtpserverport = 25;
  186. //你的163服务器邮箱账号
  187. $smtpusermail =$model['smtpusermail'];// "ycxxkj002@qq.com";
  188. //收件人邮箱
  189. $smtpemailto =$mailto;// "lzj500@qq.com";
  190. //你的邮箱账号(去掉@163.com)
  191. $smtpuser =$model['smtpuser'];// "2936890167";//SMTP服务器的用户帐号
  192. //你的邮箱密码
  193. $smtppass = $model['smtppass'];//"mail2015"; //SMTP服务器的用户密码
  194. //邮件主题
  195. $mailsubject = $subject;//"测试邮件发送";
  196. //邮件内容
  197. $mailbody =$content;// "<strong> PHP</strong>+<font style='color:red'>MySQL</font>+<a href='http://www.baidu.com'>点击跳转</a>";
  198. //邮件格式(HTML/TXT),TXT为文本邮件
  199. $mailtype = "HTML";
  200. //这里面的一个true是表示使用身份验证,否则不使用身份验证.
  201. $smtp = new \smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
  202. //是否显示发送的调试信息
  203. $smtp->debug =false;// TRUE;
  204. //$smtp->debug = TRUE;
  205. //发送邮件
  206. $res=$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  207. }
  208. function get_url() {
  209. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  210. $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
  211. $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  212. $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
  213. return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
  214. }
  215. ?>