Jun 15, 2017 Blog相关 No Comments bo-blog转换zblog程序代码-基于thinkphp5 ```php //转换分类 public function convert_cate(){ $page = input('page') ? input('page') : 1 ; $pageSize = 10; $start = ($page-1)*$pageSize; $where = array(); $info = \think\Db::table('blog_categories')->where($where)->limit($start . ','.$pageSize)->order('cateid asc')->select(); //dump($info);die; if(!$info) { echo 'Convert complete';die; } foreach($info as $key=>$val) { $data = array(); $data[cate_ID] = $val[cateid]; $data[cate_Name] = $val[catename]; $data[cate_Order] = $val[cateorder]; $data[cate_Alias] = $val[cateurlname]; $data[cate_Intro] = html_entity_decode($val[catedesc]); $info = \think\Db::table('zbp_category')->insert($data); } $this->success("成功转换数据$start -- ".$page*$pageSize, url('convert_cate', array('page'=>$page+1)), 1); } //转换tag标签,所有数据转换完成后可以清空tag_Intro、tag_Alias字段 public function convert_tag(){ $page = input('page') ? input('page') : 1 ; $pageSize = 10; $start = ($page-1)*$pageSize; $where = array(); $info = \think\Db::table('blog_tags')->where($where)->limit($start . ','.$pageSize)->order('tagid asc')->select(); //dump($info);die; if(!$info) { echo 'Convert complete';die; } foreach($info as $key=>$val) { $data = array(); $data[tag_Name] = $val[tagname]; $data[tag_Count] = $val[tagcounter]; $data[tag_Intro] = $val[tagentry]; $data[tag_Alias] = $val[tagid]; $info = \think\Db::table('zbp_tag')->insert($data); } $this->success("成功转换数据$start -- ".$page*$pageSize, url('convert_tag', array('page'=>$page+1)), 1); } //转换upload附件,所有数据转换完成后可以清空ul_Intro、ul_Meta字段内容,注意附件格式需要是:/2017/05/ public function convert_upload(){ $page = input('page') ? input('page') : 1 ; $pageSize = 10; $start = ($page-1)*$pageSize; $where = array(); $info = \think\Db::table('blog_upload')->where($where)->limit($start . ','.$pageSize)->order('fid asc')->select(); if(!$info) { echo 'Convert complete';die; } foreach($info as $key=>$val) { $data = array(); $data[ul_AuthorID] = 1; $data[ul_Name] = explode('/', $val[filepath])[2]; $ul_Meta = str_replace("attachment","https://www.fity.cn/usr/uploads",$val[filepath]); $ul_Meta = explode('/', $ul_Meta); $data[ul_Meta] = $ul_Meta[0].'/'.$ul_Meta[1].'/'.substr($ul_Meta[2] , 0 , 4).'/'.substr($ul_Meta[2] , 4 , 2).'/'.$ul_Meta[3]; // https://www.fity.cn/usr/uploads/2017/05/201705191495194584213280.gif // https://www.fity.cn/usr/uploads/201305/1369327844_617989de.jpg // attachment/201305/1369327844_617989de.jpg $data[ul_SourceName] = $val[originalname]; if(strpos($val[filepath], 'jpg') !== false){ $data[ul_MimeType] = 'image/jpg'; } if(strpos($val[filepath], 'jpeg') !== false){ $data[ul_MimeType] = 'image/jpeg'; } if(strpos($val[filepath], 'png') !== false){ $data[ul_MimeType] = 'image/png'; } if(strpos($val[filepath], 'gif') !== false){ $data[ul_MimeType] = 'image/gif'; } if(strpos($val[filepath], 'zip') !== false){ $data[ul_MimeType] = 'application/octet-stream'; } if(strpos($val[filepath], 'rar') !== false){ $data[ul_MimeType] = 'application/octet-stream'; } if(strpos($val[filepath], 'pdf') !== false){ $data[ul_MimeType] = 'application/octet-stream'; } $data[ul_PostTime] = $val[uploadtime]; $data[ul_DownNums] = $val[dltime]; // $data[ul_LogID] = $val[tagid]; $data[ul_Intro] = $val[fid]; //旧fid记录 $info = \think\Db::table('zbp_upload')->insert($data); } $this->success("成功转换数据$start -- ".$page*$pageSize, url('convert_upload', array('page'=>$page+1)), 1); } //转换评论 public function convert_comment(){ $page = input('page') ? input('page') : 1 ; $pageSize = 10; $start = ($page-1)*$pageSize; $where = array(); //$where['blogid'] = '486'; $info = \think\Db::table('blog_replies')->where($where)->limit($start . ','.$pageSize)->order('repid asc')->select(); //dump($info);die; if(!$info) { echo 'Convert complete';die; } foreach($info as $key=>$val) { $data = array(); $re_data = array(); //$data[comm_ID] = $val[repid]; $data[comm_LogID] = $val[blogid]; $data[comm_IsChecking] = 0; $data[comm_RootID] = 0; $data[comm_ParentID] = 0; $data[comm_AuthorID] = 0; $data[comm_Name] = $val[replier]; $data[comm_Email] = $val[repemail]; $data[comm_HomePage] = $val[repurl]; $data[comm_Content] = preg_replace('/\[emot\]([^\[]*)\[\/emot\]/','',$val[repcontent]); $data[comm_PostTime] = $val[reptime]; $data[comm_IP] = $val[repip]; $data[comm_Agent] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'; $data[comm_Meta] = ''; $info = \think\Db::table('zbp_comment')->insert($data); $comm_ParentID = \think\Db::table('zbp_comment')->getLastInsID(); \think\Db::table('zbp_post')->where('log_ID', $val[blogid])->setInc('log_CommNums'); //更新评论数 if($val[adminrepcontent]){ $re_data[comm_LogID] = $val[blogid]; $re_data[comm_IsChecking] = 0; $re_data[comm_RootID] = $comm_ParentID; $re_data[comm_ParentID] = $comm_ParentID; $re_data[comm_AuthorID] = 1; $re_data[comm_Name] = $val[adminreplier]; $re_data[comm_Email] = '136654168@qq.com'; $re_data[comm_HomePage] = 'https://www.fity.cn'; $re_data[comm_Content] = preg_replace('/\[emot\]([^\[]*)\[\/emot\]/','',$val[adminrepcontent]); $re_data[comm_PostTime] = $val[adminreptime]; $re_data[comm_IP] = '::1'; $re_data[comm_Agent] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'; $re_data[comm_Meta] = ''; $info = \think\Db::table('zbp_comment')->insert($re_data); \think\Db::table('zbp_post')->where('log_ID', $val[blogid])->setInc('log_CommNums'); //更新评论数 } } $this->success("成功转换数据$start -- ".$page*$pageSize, url('convert_comment', array('page'=>$page+1)), 1); } //转换文章 public function convert_article(){ // echo base64_encode(' 1123');die; header("Content-type: text/html; charset=utf-8"); $page = input('page') ? input('page') : 1 ; $pageSize = 10; $start = ($page-1)*$pageSize; $where = array(); // $info = \think\Db::table('blog_blogs')->where('blogid=604')->limit($start . ','.$pageSize)->order('blogid asc')->select(); $info = \think\Db::table('blog_blogs')->limit($start . ','.$pageSize)->order('blogid asc')->select(); if(!$info) { echo 'Convert complete';die; } foreach($info as $key=>$val) { $data = array(); $data[log_ID] = $val[blogid]; $data[log_CateID] = $val[category]; //分类 $data[log_AuthorID] = 1; $data[log_Status] = ($val[property] == 3) ? 1 : 0; $data[log_Type] = 0; $data[log_IsTop] = ($val[sticky] != 0) ? 2 : 0; $data[log_Title] = $val[title]; $data[log_PostTime] = $val[pubtime]; $data[log_CommNums] = ($val[replies] > 4) ? 1 : 0; $data[log_ViewNums] = $val[views]; //摘要 $newDescription = preg_replace(array("/\[url=([^\[]*)\](.+?)\[\/url\]/is","/\s*\[attach\](.*?)\[\/attach\]\s*/is","/\s*\[img(.*?)\/img\]\s*/is","/\s*\[clickandsee(.+?)\/clickandsee\]\s*/is","/\s*\[code\](.+?)\[\/code\]\s*/is","/\s*\[codes(.*?)\/codes\]\s*/is","/\s*\[hide\](.*?)\[\/hide\]\s*/is"),array("\\2","","","","","",""),$val['content']); $newDescription = $this->msubstr(strip_tags(preg_replace("([a-zA-z]+://[^\s|\<]*)","",$this->strip_ubbs($newDescription))), 0, 180); $data[log_Intro] = $newDescription; //正文 $content = str_replace('[newpage]', '[separator]', $val['content']); $content = str_replace('[separator]', '', $content); $content = $this->convert_ubb($content,1,0,$val[blogid]); $data[log_Content] = $content; //tags标签转换:>知识>google> $tags = trim($val[tags],'>'); $tags = explode('>', $tags); $new_tag = ''; foreach ($tags as $key=>$val){ $tagid = ''; $map = array(); $map['tag_Name'] = $val; $tagid = \think\Db::table('zbp_tag')->where($map)->value('tag_ID'); $new_tag .= ($tagid) ? '{'.$tagid.'}' : ''; } $data[log_Tag] = $new_tag; //标签 // print_r($data); // die; //分隔符处理 //$entry['content']=preg_replace("/\[separator\]/", "", $entry['content'], 1); //$entry['content']=@str_replace('[separator]', '', $entry['content']); $info = \think\Db::table('zbp_post')->insert($data); } $this->success("成功转换数据$start -- ".$page*$pageSize, url('convert_article', array('page'=>$page+1)), 1); } private function convert_ubb ($str, $advanced=0, $inrss=0,$id) { // if ($logstat!=1) { // $str=preg_replace("/\[hide\](.+?)\[\/hide\]/is", "隐藏内容这部分内容只能在登入之后看到。请先 {$lnc[79]} {$lnc[235]} {$lnc[89]} ", $str); // } else { // $str=str_replace(array('[hide]','[/hide]'), '', $str); // } $str=str_replace(array('[hide]','[/hide]'), '', $str); $str=str_replace(array('{','}'), array('{', '}'), $str); //$str=plugin_walk('ubbanalyse', $str); $basicubb_search=array('[hr]', ''); $basicubb_replace=array('', ''); $str=str_replace($basicubb_search, $basicubb_replace, $str); //[IMG] if ($advanced==1) { $str = preg_replace_callback("/\[url=([^\[]*)\]\[img( align=L| align=M| align=R)?( width=[0-9]+)?( height=[0-9]+)?\]\s*(\S+?)\s*\[\/img\]\[\/url\]/",function ($matches) { $aligncode = $matches[1]; $widthcode = $matches[2]; $heightcode = $matches[3]; $src = $matches[4]; $inrss = 0; $align=str_replace(' align=', '', strtolower($aligncode)); if ($align=='l') $show=' align="left"'; elseif ($align=='r') $show=' align="right"'; else $alignshow=''; $width=str_replace(' width=', '', strtolower($widthcode)); if (!empty($width)) $show.=" width=\"{$width}\""; $height=str_replace(' height=', '', strtolower($heightcode)); if (!empty($height)) $show.=" height=\"{$height}\""; if ($inrss==1) $src=(substr(strtolower($src), 0, 4) == 'http') ? $src : 'https://www.fity.cn//'.$src; $temp_fid = explode('[/attach]',explode('[attach]', $src)[1])[0]; if($temp_fid){ $src = \think\Db::table('zbp_upload')->where('ul_Intro='.$temp_fid)->value('ul_Meta'); } //$onloadact=($inrss==0 && !empty($mbcon['autoresizeimg'])) ? " onload=\"if(this.width>{$mbcon['autoresizeimg']}) {this.resized=true; this.width={$mbcon['autoresizeimg']};}\"" : ''; $code=""; return $code; },$str); $str = preg_replace_callback("/\[img( align=L| align=M| align=R)?( width=[0-9]+)?( height=[0-9]+)?\]\s*(\S+?)\s*\[\/img\]/",function ($matches) { $aligncode = $matches[1]; $widthcode = $matches[2]; $heightcode = $matches[3]; $src = $matches[4]; $inrss = 0; //global $lnc, $mbcon, $config; $align=str_replace(' align=', '', strtolower($aligncode)); if ($align=='l') $show=' align="left"'; elseif ($align=='r') $show=' align="right"'; else $alignshow=''; $width=str_replace(' width=', '', strtolower($widthcode)); if (!empty($width)) $show.=" width=\"{$width}\""; $height=str_replace(' height=', '', strtolower($heightcode)); if (!empty($height)) $show.=" height=\"{$height}\""; if ($inrss==1) $src=(substr(strtolower($src), 0, 4) == 'http') ? $src : 'https://www.fity.cn//'.$src; $temp_fid = explode('[/attach]',explode('[attach]', $src)[1])[0]; if($temp_fid){ $src = \think\Db::table('zbp_upload')->where('ul_Intro='.$temp_fid)->value('ul_Meta'); } //$onloadact=($inrss==0 && !empty($mbcon['autoresizeimg'])) ? " onload=\"if(this.width>{$mbcon['autoresizeimg']}) {this.resized=true; this.width={$mbcon['autoresizeimg']};}\"" : ''; $code=""; return $code; },$str); //代码高亮处理,如果你安装了dphighlighter插件 session('bid', null); session('bid', $id); $str_temp = preg_replace_callback("/\s*\[codes=(.+?)\][\n\r]*(.+?)[\n\r]*\[\/codes\]\s*/",function ($matches) { $bid = session('bid'); $type = $matches[1]; $strtemp = base64_encode($matches[2]); $strtemp = base64_decode(str_replace(" ","+",$strtemp)); $strtemp = str_replace("", "\n" , stripslashes($strtemp)); echo ''; echo ''; echo ''; $codes_str = "".htmlspecialchars_decode($strtemp).""; print_r($codes_str); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; return html_entity_decode($codes_str); },$str); }else { //$str=preg_replace("/\[img( align=L| align=M| align=R)?( width=[0-9]+)?( height=[0-9]+)?\]\s*(\S+?)\s*\[\/img\]/ise","$this->makeimginrss('\\4')",$str); } //替换所有h6标签为为h4 $str = str_replace("h6","h4",$str); // if ($mbcon['countdownload']=='1' && $inrss==0) $str=preg_replace(array("/\[sfile\]\s*\[attach\]([0-9]+)\[\/attach\]\s*\[\/sfile\]/ise", "/\[file\]\s*\[attach\]([0-9]+)\[\/attach\]\s*\[\/file\]/ise"), array("self::makedownload('\\1', 1, 0, true)", "self::makedownload('\\1', 0, 0, true)"), $str); $str=preg_replace("/\[attach\]([0-9]+)\[\/attach\]/is", "attachment.php?fid=\\1", $str); // $str=preg_replace("/\[sfile\]\s*(\S+?)\s*\[\/sfile\]/ise", "self::makedownload('\\1', 1, $inrss, false)", $str); $str=preg_replace("/\[file\]\s*(\S+?)\s*\[\/file\]/ise", "self::makedownload('\\1', 0, $inrss, false)", $str); //Auto add url link if ($mbcon['autoaddlink']==1) $str=preg_replace("/(?<=[^\]a-z0-9-=\"'\\/])((https?|ftp|gopher|news|telnet|rtsp|mms|callto|ed2k):\/\/|www\.)([a-z0-9\/\-_+=.~!%@?#%&;:$\\()|]+)/i", "[autourl]\\1\\3[/autourl]", $str); $regubb_search = array( "/\[size=([^\[\<]+?)\](.+?)\[\/size\]/ie", "/\[tbl( width=[0-9]+)?(%)?( bgcolor=[^ ]*)?( border=[^ ]*)?\](.+?)\[\/tbl\]/ise", "/\s*\[quote\][\n\r]*(.+?)[\n\r]*\[\/quote\]\s*/is", "/\s*\[quote=(.+?)\][\n\r]*(.+?)[\n\r]*\[\/quote\]\s*/is", "/\s*\[code\][\n\r]*(.+?)[\n\r]*\[\/code\]\s*/ie", "/\[autourl\]([^\[]*)\[\/autourl\]/ie", "/\[url\]([^\[]*)\[\/url\]/ie", "/\[url=www.([^\[\"']+?)\](.+?)\[\/url\]/is", "/\[url=([^\[]*)\](.+?)\[\/url\]/is", "/\[email\]([^\[]*)\[\/email\]/is", "/\[acronym=([^\[]*)\](.+?)\[\/acronym\]/is", "/\[color=([a-zA-Z0-9#]+?)\](.+?)\[\/color\]/i", "/\[font=([^\[\<:;\(\)=\.\+\*\/]+?)\](.+?)\[\/font\]/i", "/\[p align=([^\[\<]+?)\](.+?)\[\/p\]/i", "/\[b\](.+?)\[\/b\]/i", "/\[i\](.+?)\[\/i\]/i", "/\[u\](.+?)\[\/u\]/i", "/\[strike\](.+?)\[\/strike\]/i", "/\[sup\](.+?)\[\/sup\]/i", "/\[sub\](.+?)\[\/sub\]/i", "/\s*\[php\][\n\r]*(.+?)[\n\r]*\[\/php\]\s*/ie" ); $regubb_replace = array( "self::makefontsize('\\1', '\\2')", "self::maketable('\\5','\\1','\\2','\\3','\\4')", "\\1", "\\2 \\1", "self::makecode('\\1')", "self::makeurl('\\1')", "self::makeurl('\\1')", "\\2", "\\2", "\\1", "\\2", "\\2", "\\2", "\\2", "\\1", "\\1", "\\1", "\\1", "\\1", "\\1", "self::xhtmlHighlightString('\\1')" ); $str=preg_replace($regubb_search, $regubb_replace, $str); //Multimedia Objects, dangerous, so visitors shall never be allowed to post such an object directly if ($advanced==1) { $str =($inrss==0) ? preg_replace("/\[(wmp|swf|real|flv)=([^\[\<]+?),([^\[\<]+?)\]\s*([^\[\<\r\n]+?)\s*\[\/(wmp|swf|real|flv)\]/ies", "makemedia('\\1', '\\4', '\\2', '\\3')", $str) : preg_replace("/\[(wmp|swf|real|flv)=([^\[\<]+?),([^\[\<]+?)\]\s*([^\[\<\r\n]+?)\s*\[\/(wmp|swf|real|flv)\]/is", "此处包含一个多媒体文件,请用网页方式查看。", $str); //$str=plugin_walk('ubbanalyseadvance', $str); } return $str; } //解析高亮代码内容为html ol格式,这里主要考虑后续不再引入使用该插件前端相关正则替换的js文件,用markdown编辑器替代 public function convert_article_codes(){ header("Content-type: text/html; charset=utf-8"); $page = input('page') ? input('page') : 1 ; $pageSize = 10; $start = ($page-1)*$pageSize; $where = array(); // $info = \think\Db::table('zbp_post')->where('log_ID=604')->limit($start . ','.$pageSize)->order('log_ID asc')->select(); $info = \think\Db::table('zbp_post')->limit($start . ','.$pageSize)->order('log_ID asc')->select(); if(!$info) { echo 'Convert complete';die; } foreach($info as $key=>$val) { $data = array(); $log_ID = $val[log_ID]; $content = $val[log_Content]; session('bid', null); session('bid', $log_ID); $str = preg_replace_callback("/\s*\[codes=(.+?)\][\n\r]*(.+?)[\n\r]*\[\/codes\]\s*/",function ($matches) { $bid = session('bid'); $type = base64_encode($matches[1]); $strtemp = base64_encode($matches[2]); $map = array(); $map['bid'] = $bid; $map['type'] = $type; $map['oldhtml'] = $strtemp; $codes = \think\Db::table('zbp_zzz')->where($map)->order('id desc')->find(); return html_entity_decode($codes[html]); },$content); $data[log_Content] = $str; $info = \think\Db::table('zbp_post')->where('log_ID='.$log_ID)->update($data); } $this->success("成功更新数据$start -- ".$page*$pageSize, url('convert_article_codes', array('page'=>$page+1)), 1); } //保存解析后的高亮代码 public function saveCodeHtml(){ $tempStr = input('post.html_new'); $data = array(); $data['html'] = $tempStr; $data['bid'] = input('post.bid'); $data['type'] = input('post.type'); $data['oldhtml'] = input('post.oldhtml'); \think\Db::table('zbp_zzz')->insert($data); } private function msubstr($str,$start,$end,$len=0) { //UTF-8 Cutting preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $str, $info); $lens=sizeof($info[0]); if ($len==1) return array(join("",array_slice($info[0],$start,$end)), $lens); else return join("",array_slice($info[0],$start,$end)); } private function strip_ubbs ($str) { $str=preg_replace("/\[(.+?)\]/is", "", $str); $str=preg_replace("/&(.+?);/is", "", $str); return $str; } private function makecode ($str) { $str=str_replace('[autourl]', '', $str); $str=str_replace('[/autourl]', '', $str); return "{$str}"; } private function makefontsize ($size, $word) { $word=stripslashes($word); $sizeitem=array (0, 10, 12, 15, 16, 18, 22, 36); $size=$sizeitem[$size]; //add by rinald 2014-01-05 Start if($size >= 16){ $lineheight='line-height:2.5em;'; } //add by rinald 2014-01-05 Over return "{$word}"; } private function maketable($tablebody, $widthcode, $ifpercentage, $bgcolorcode, $bordercolorcode) { $tablebody=stripslashes($tablebody); $show="\n\n"; $bgcolor=str_replace(' bgcolor=', '', strtolower($bgcolorcode)); $bordercolor=str_replace(' border=', '', strtolower($bordercolorcode)); if (!$bordercolor) $bordercolor="#000000"; if (!$bgcolor) $bgcolor="#ffffff"; $show.="\n"; $show.="\n"; $tablebody=str_replace(',', "\n", $tablebody); $tablebody=str_replace('', "\n", $tablebody); $tablebody=str_replace('', "\n", $tablebody); $show.=$tablebody; $show.="\n\n\n"; return $show; } private function makeurl($url) { global $mbcon; $urllink="'; if($mbcon['shortenurl']=='1' && strlen($url) > $mbcon['urlmaxlen']) { $halfmax=floor($mbcon['urlmaxlen']/2); $url = substr($url, 0, $halfmax).'...'.substr($url, 0-$halfmax); } $urllink .= $url.''; return $urllink; } private function xhtmlHighlightString($str) { $str=base64_decode($str); if (PHP_VERSION<'4.2.0') return "$str"; $hlt = highlight_string($str, true); if (PHP_VERSION>'5') return "$hlt"; $fon = str_replace(array(''), array(''), $hlt); $ret = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $fon); return "$ret"; } private function makedownload ($url, $sfile, $inrss, $isattached=false) { $temp_fid = explode('=',$url)[1]; $url = \think\Db::table('zbp_upload')->where('ul_Intro='.$temp_fid)->find(); $str="点击下载{$url[ul_SourceName]}"; return $str; } ``` 最后更新于 2019-06-27 13:50:07 并被添加「」标签,已有 1044 位童鞋阅读过。 本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
此处评论已关闭