You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							381 lines
						
					
					
						
							14 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							381 lines
						
					
					
						
							14 KiB
						
					
					
				| <?php | |
| include_once(dirname(dirname(__FILE__))."/config/boot.inc.php"); | |
| include_once(SERVER_ROOT."/library/viewtpl/DisplaySmarty.php"); | |
| include_once(SERVER_ROOT."/library/viewtpl/DisplayJson.php"); | |
| include_once(SERVER_ROOT."/library/viewtpl/DisplayNone.php"); | |
| include_once(SERVER_ROOT."/library/autoLoad.php"); | |
| 
 | |
| class publicBase { | |
|     protected $para; | |
|     public $view = array(); | |
|     protected $error; | |
|     protected $viewFormat='html'; | |
|     protected $viewTpl=''; | |
| 
 | |
|     /** | |
|      * HTTP协议中的GET | |
|      * @param string $argv | |
|      */ | |
|     protected function get($argv) { | |
|         return empty($argv) ? $_GET : $_GET[$argv]; | |
| 
 | |
|         if(empty($argv)) return false; | |
|         if(empty($_GET[$argv])) return ''; | |
|         foreach($_GET[$argv] as $k => &$v) { | |
|             $v = get_magic_quotes_gpc() ? trim(stripslashes($v)) : trim($v); | |
|         } | |
|         return $_GET[$argv]; | |
|     } | |
| 
 | |
|     /** | |
|      * HTTP协议中的POST | |
|      * @param string $argv | |
|      */ | |
|     protected function post($argv) { | |
|         return empty($argv) ? $_POST : $_POST[$argv]; | |
| 
 | |
|         if(empty($argv)) return false; | |
|         if(empty($_POST[$argv])) return array(); | |
|         foreach($_POST[$argv] as $k => &$v) { | |
|             $v = get_magic_quotes_gpc() ? trim(stripslashes($v)) : trim($v); | |
|         } | |
|         return $_POST[$argv]; | |
|     } | |
| 
 | |
|     /** | |
|      * HTTP协议中的Cookie | |
|      * @param string $argv | |
|      */ | |
|     protected function cookie($argv) { | |
|         if(empty($argv)) return false; | |
|         if(empty($_COOKIE[$argv])) return array(); | |
|         foreach($_COOKIE[$argv] as $k => &$v) { | |
|             $v = get_magic_quotes_gpc() ? trim(stripslashes($v)) : trim($v); | |
|         } | |
|         return $_COOKIE[$argv]; | |
|     } | |
| 
 | |
|     /** | |
|      * 调用__call给对象属性赋值或取值. | |
|      * | |
|      * @param string $func | |
|      * @param array  $args | |
|      * @return mixed | |
|      */ | |
|     public function __call($func, $args) { | |
|         if(strtolower(substr($func, 0, 3)) === 'set') { | |
|             $var = strtolower(substr($func, 3, 1)).substr($func, 4); | |
|             if($var=='') return; | |
|             $this->$var = $args[0]; | |
|         } elseif(strtolower(substr($func, 0, 3)) === 'get') { | |
|             $var = strtolower(substr($func, 3, 1)).substr($func, 4); | |
|             if($var=='') return; | |
|             return $this->$var; | |
|         } | |
|         return; | |
|     } | |
| 
 | |
|     /** | |
|      * 给对象属性赋值. | |
|      * | |
|      * @param string $var | |
|      * @param mixed  $value | |
|      * @return boolean | |
|      */ | |
|     public function __set($var, $value) { | |
|         if($var=='') return false; | |
|         $this->$var = $value; | |
|         return true; | |
|     } | |
| 
 | |
|     /** | |
|      * 获取对象属性 | |
|      * | |
|      * @param mixed $var | |
|      * @return mixed | |
|      */ | |
|     public function __get($var) { | |
|         if($var=='') return; | |
|         return $this->$var; | |
|     } | |
| 
 | |
|     public function ajax($message, $nexturl='') { | |
|         if(empty($message)) return false; | |
|         $result = array(); | |
|         $result['status'] = 1; | |
|         $result['info'] = $message; | |
|         $result['nexturl'] = $nexturl; | |
| 
 | |
|         header("Content-Type:text/html; charset=utf-8"); | |
| 
 | |
|         exit(json_encode($result)); | |
|     } | |
| 
 | |
|     /** | |
|      * Ajax调用返回 | |
|      * | |
|      * 返回json数据,供前台ajax调用 | |
|      * @param array $data    返回数组,支持数组 | |
|      * @param string $info    返回信息, 默认为空 | |
|      * @param boolean $status    执行状态, 1为true, 0为false | |
|      * @return string | |
|      */ | |
|     public function ajax_json($status = 1, $info = null, $data = array()) { | |
| 
 | |
|         if($this->bd_sign) { | |
|             if($status == 1) $error_no = 0; | |
|             if($status == 0) $error_no = -1; | |
|             $this->json_result($error_no, $info, $data); | |
|         } else { | |
|             $result = array(); | |
|             $result['status'] = $status; | |
|             $result['info'] = !is_null($info) ? $info : ''; | |
|             $result['data'] = $data; | |
|         } | |
| 
 | |
|         header("Content-Type:text/html; charset=utf-8"); | |
| 
 | |
|         if($_POST['tt']=='form') { | |
|             $display = new DisplaySmarty('public/message_box.html'); | |
|             $display->setView($result); | |
|             $display->execute(); | |
|             exit; | |
|         } else { | |
|             exit(json_encode($result)); | |
|         } | |
|     } | |
| 
 | |
|     /* 弹窗提示 */ | |
|     public function show_message($msg, $nexturl='', $type='js') { | |
|         header("Content-Type:text/html;charset=utf-8"); | |
|         if($this->post('_token')!='') {   // 开放接口 | |
|             $this->ajax_json(1, $msg); | |
|             exit; | |
|         } | |
| 
 | |
|         if($type=='json') { | |
|             if($nexturl!='') { | |
|                 echo json_encode(array('msg'=>$msg, 'nexturl'=>$nexturl)); | |
|             } else { | |
|                 echo json_encode(array('msg'=>$msg)); | |
|             } | |
|             exit; | |
|         } | |
| 
 | |
|         if(!empty($nexturl)) { | |
|             echo "<script language='javascript'>alert('".str_replace("\n", "", $msg)."')</script>"; | |
|             if($nexturl=='goback') { | |
|                 echo "<script language='javascript'>history.go(-1)</script>"; | |
|             } else { | |
|                 echo "<meta http-equiv=\"Refresh\" content=\"0;URL=$nexturl\" />"; | |
|             } | |
|         }else{ | |
|             echo "<script language='javascript'>setTimeout(function(){alert('".str_replace("\n", "", $msg)."');}, 1000);</script>"; | |
|         } | |
|         exit; | |
|     } | |
|  | |
|     /** | |
|      * 当前秒、毫秒数 | |
|      * @return number | |
|      */ | |
|     public function microtime_float() { | |
|         list($usec, $sec) = explode(" ", microtime()); | |
|         return ((float)$usec + (float)$sec); | |
|     } | |
|  | |
|     /** | |
|      * | |
|      * @param number $status | |
|      * @param unknown $info | |
|      * @param array $data | |
|      * @param string $nexturl | |
|      */ | |
|     public function ajax_json_data($status = 1, $info = null, $data = array(), $nexturl="") { | |
|         $result = array(); | |
|         $result['status'] = $status; | |
|         $result['info'] = !is_null($info) ? $info : ''; | |
|         $result['data'] = $data; | |
|         if($nexturl) { | |
|             $result['nexturl'] = $nexturl; | |
|         } | |
|  | |
|         header("Content-Type:text/html; charset=utf-8"); | |
|  | |
|         if($_POST['tt']=='form') { | |
|             $display = new DisplaySmarty('public/message_box.html'); | |
|             $display->setView($result); | |
|             $display->execute(); | |
|             exit; | |
|         } else { | |
|             exit(json_encode($result)); | |
|         } | |
|     } | |
|  | |
|     /** | |
|      * 返回json结果(百度学术专用,其他请用ajax_json()方法) | |
|      * @param number $error_no  0(成功)  -1(失败) -2(该订单已提交过) | |
|      * @param unknown $message | |
|      * @param array $data | |
|      */ | |
|     public function json_result($error_no = 0, $message = null, $data = array()) { | |
|         $result = array(); | |
|         $result['error_no'] = $error_no; | |
|         $result['message'] = !is_null($message) ? $message : ''; | |
|         $result['data'] = $data; | |
|  | |
|         $log_path = BD_XUESHU_LOG_PATH.date("Y-m-d")."/json_result.log"; | |
|         $log_dir = dirname($log_path); | |
|         if(!is_dir($log_dir)) { | |
|             mkdir($log_dir, 0755, true); | |
|             chown($log_dir, 'nobody'); | |
|             chgrp($log_dir, 'nobody'); | |
|         } | |
|         error_log(date('Y-m-d H:i:s')."|json_result|".json_encode($result)."|"."\n", 3, $log_path); | |
|  | |
|         header("Content-Type:text/html; charset=utf-8"); | |
|         exit(json_encode($result)); | |
|     } | |
|  | |
|     /** | |
|      * 获取Linux本机IP真实地址 | |
|      * @return unknown|string | |
|      */ | |
|     public function getServerIp() { | |
|         $server_ip = '127.0.0.1'; | |
|         // 执行命令 | |
|         $result = shell_exec("/sbin/ifconfig"); | |
|         // 匹配ip 根据你机器的具体情况, 可能要对“inet ”进行调整, 如“addr:” | |
|         preg_match_all("/[\s\S]*?inet (\d+\.\d+\.\d+\.\d+)/", $result, $match); | |
|         if(!$match) preg_match_all("/[\s\S]*?addr:(\d+\.\d+\.\d+\.\d+)/", $result, $match); | |
|         if(!$match) return $server_ip; | |
|  | |
|         foreach ($match [0] as $k => $v) { | |
|             if($match[1][$k] == "127.0.0.1") continue; | |
|             if(preg_match("/docker\d+/", $match[0][$k])) continue; | |
|  | |
|             $server_ip = $match[1][$k]; | |
|             break; | |
|         } | |
|         return $server_ip; | |
|     } | |
|  | |
|     /** | |
|      * 在背景图上生成二维码 | |
|      * $bg_img    背景图地址 | |
|      * $url       二维码内容url | |
|      * $x   二维码在背景图片上位置的左边距,单位:px (例:436) | |
|      * $y   二维码在背景图片上位置的上边距,单位:px (例:1009) | |
|      * $qrcode_size  二维码(正方形)的尺寸,单位:px (例:238) | |
|      * $white_edge   二维码白边大小 | |
|      * $echo_path   生成的二维码海报图片存放路径 | |
|      **/ | |
|     public function getActivityImg($bg_img, $url, $x=0, $y=0, $qrcode_size, $white_edge=0, $echo_path){ | |
|         $errorCorrectionLevel = QR_ECLEVEL_L;          //容错级别 | |
|         $matrixPointSize = 2;                           //生成二维码图片大小 | |
|         $qrcode = "qrcod.png"; | |
|  | |
|         //生成二维码图片 | |
|         //参数内容:二维码储存内容,生成存储,防错等级,二维码大小,白边大小 | |
|         QRcode::png($url, $qrcode, $errorCorrectionLevel, $matrixPointSize, $white_edge); | |
|  | |
|         //合背景图和二维码 | |
|         $background = imagecreatefromstring(file_get_contents($bg_img)); | |
|         $qrcode_res = imagecreatefromstring(file_get_contents($qrcode)); | |
|         list($src_w, $src_h) = getimagesize($qrcode); | |
|         //参数内容:目标图象连接资源,源图象连接资源,目标X坐标点,目标Y坐标点,源的X坐标点,源的Y坐标点,目标宽度,目标高度,源图象的宽度,源图象的高度 | |
|         imagecopyresampled($background, $qrcode_res, $x, $y, 0, 0, $qrcode_size, $qrcode_size, $src_w, $src_h); | |
|  | |
|         //输出到本地文件夹,返回生成图片的路径 | |
|         if(!is_dir(dirname($echo_path))){ | |
|             mkdir(dirname($echo_path), 0755, true); | |
|             chown(dirname($echo_path), 'nobody'); | |
|             chgrp(dirname($echo_path), 'nobody'); | |
|         } | |
|  | |
|         imagepng($background,$echo_path); | |
|         imagedestroy($background); | |
|         imagedestroy($qrcode_res); | |
|         return $echo_path; | |
|     } | |
|  | |
|     /** | |
|      * 将两张图片合成一张 | |
|      * $bg_path    背景图地址 | |
|      * $poster     图片2 | |
|      * $x           图片2在背景图片上位置的左边距,单位:px (例:436) | |
|      * $y           图片2在背景图片上位置的上边距,单位:px (例:1009) | |
|      * $size        图片2(正方形)的尺寸,单位:px (例:238) | |
|      * $echo_path   生成的新图片存放路径 | |
|      **/ | |
|     public function createPoster($bg_path, $poster, $x, $y, $poster_w, $poster_y, $echo_path, $is_base64 = 0){ | |
|         $background = imagecreatefromstring(file_get_contents($bg_path)); | |
|         $poster_res = imagecreatefromstring(file_get_contents($poster)); | |
|         //参数内容:目标图象连接资源,源图象连接资源,目标X坐标点,目标Y坐标点,源的X坐标点,源的Y坐标点,目标宽度,目标高度,源图象的宽度,源图象的高度 | |
|         imagecopyresampled($background, $poster_res, $x, $y, 0, 0, $poster_w, $poster_y, $poster_w, $poster_y); | |
|         //输出到本地文件夹,返回生成图片的路径 | |
|         if(!is_dir(dirname($echo_path))){ | |
|             mkdir(dirname($echo_path), 0755, true); | |
|             chown(dirname($echo_path), 'nobody'); | |
|             chgrp(dirname($echo_path), 'nobody'); | |
|         } | |
|  | |
|         if($is_base64){ | |
|             ob_start (); | |
|             //imagepng展示出图片 | |
|             imagepng ($background); | |
|             $imageData = ob_get_contents (); | |
|             ob_end_clean (); | |
|             //得到这个结果,可以直接用于前端的img标签显示 | |
|             $res = "data:image/jpeg;base64,". base64_encode ($imageData); | |
|         }else{ | |
|             imagepng($background,$echo_path); | |
|             $res = $echo_path; | |
|         } | |
|         imagedestroy($background); | |
|         imagedestroy($poster_res); | |
|         return $res; | |
|     } | |
|  | |
|     /** | |
|      * 给图片加文字 | |
|      * $bg_path    背景图地址 | |
|      * $echo_path   生成的新图片存放路径 | |
|      **/ | |
|     public function imageAddText($bg_path, $text, $x, $y, $font_size, $echo_path){ | |
|         //将品牌名称和广告语写到背景海报上 | |
|         $background = imagecreatefromstring(file_get_contents($bg_path)); | |
|         $font = FONTS."simhei.ttf"; | |
|         $black = imagecolorallocate($background, 0, 0, 0);//字体颜色黑色 | |
|  | |
|         $str = mb_convert_encoding($text, "", "utf-8"); | |
|  | |
|         imagefttext($background, $font_size, 0, $x, $y, $black, $font, $str); | |
|         imagepng($background,$echo_path); | |
|         imagedestroy($background); | |
|         return $echo_path; | |
|     } | |
|  | |
|     /** | |
|      * 判断是不是手机移动端 | |
|      */ | |
|     public function isMobileClient() { | |
|         // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 | |
|         if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) return true; | |
|         // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 | |
|         if (isset ($_SERVER['HTTP_VIA'])) return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false; | |
|  | |
|         // 判断手机发送的客户端标志,兼容性有待提高,把常见的类型放到前面 | |
|         if (isset ($_SERVER['HTTP_USER_AGENT'])) { | |
|             $clientkeywords = array ('android', 'iphone', 'samsung', 'ucweb', 'wap', 'mobile', 'nokia', 'sony', 'ericsson', 'mot', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'ipod', 'blackberry', 'meizu', 'netfront', 'symbian', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp'); | |
|             if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) return true; // 从HTTP_USER_AGENT中查找手机浏览器的关键字 | |
|         } | |
|         // 协议法,因为有可能不准确,放到最后判断 | |
|         if (isset ($_SERVER['HTTP_ACCEPT'])) { | |
|             // 如果只支持wml并且不支持html那一定是移动设备 | |
|             // 如果支持wml和html但是wml在html之前则是移动设备 | |
|             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')))) return true; | |
|         } | |
|         return false; | |
|     } | |
| }
 |