discuz 性息:discuz论坛门户资讯入库接口【原创】

   最近想打造一个社区门户站点,所以写了这个入库接口,可以对接数据入库。

    

<?php/* * Discuz x3.2 门户免登陆发布接口 * 2018-08-10 * Copyright 68xi.Ltd * Author: 68xi * * 获取栏目列表: /jiekou_portal.php?get_cat=true&password=PWD * * 发布参数: (说明: 参数前面带 "*" 的为必填参数) * *uid: 发布用户ID, 可单个, 也可多个, 多个发布用户ID之间请使用逗号(,)分割 (当填写多个用户ID, 接口会随机使用其中一个, 发布用户ID必须是系统已存在的用户ID) * *catid: 栏目ID * *title: 标题 * *content: 内容 * dateline: 发布时间, 默认当前时间, 格式: 2013-11-15 15:30:11 * summary: 内容摘要, 此参数为空时, 将使用discuz内部提取正文方式来获取摘要 * author: 原作者 * from: 来源 * fromurl: 来源网址 * viewnum: 文章阅读数字, 默认1 * setconver: 提取内容里的第一张图片为封面, 无论是本地图片还是网络图片, 都会尝试提取*/header('Content-type:text/html;charset=UTF-8');//接口密码, 请保持与采集器上设置的密码一致, 为了网站安全, 强烈建议修改此值!!!define('PASSWORD', '123456');//调试模式, 上线后务必设置成false!!!define('DEBUG', false);//输出信息并退出function showMsg($msg) {echo $msg;exit;}//打印变量信息 - 用于DEBUGfunction dd($data, $exit = false) {echo '<pre>';print_r($data);echo '</pre>';if($exit) exit;}//检测接口密码if(!isset($_GET['password']) || $_GET['password'] != PASSWORD) showMsg('接口密码不正确!');/******************* discuz头文件 开始 ********************/#/portal.phpdefine('APPTYPEID', 4);define('CURSCRIPT', 'portal');require './source/class/class_core.php';$discuz = C::app();$cachelist = array('userapp', 'portalcategory', 'diytemplatenameportal');$discuz->cachelist = $cachelist;$discuz->init();require DISCUZ_ROOT.'./source/function/function_home.php';require DISCUZ_ROOT.'./source/function/function_portal.php';define('CURMODULE', 'portalcp');#/souce/module/portal/portalportalcp.php$ac = 'article';require_once libfile('function/portalcp');#/source/include/portalcp/portalcp_article.php/******************* discuz头文件 结束 ********************/$app = new App();class App {protected $global; //equal $_Gprotected $uid; //发布用户ID//构造函数public function __construct() {global $_G;$this->global = &$_G;//当前时间define('DATE_TIME', date('Y-m-d H:i:s'));if(isset($_GET['get_cat']) && $_GET['get_cat'] == 'true') {$this->get_cat(); //获取栏目} else {$this->publish_content(); //发布内容}}//获取栏目protected function get_cat() {$cat = $this->get_cat_list();if(empty($cat)) showMsg('当前网站栏目为空, 请添加门户栏目后再重试');$catStr = '';foreach($cat as $key => $value) {$catStr .= '<option value="'. $value['catid'] .'">'. $value['catname'] .'</option>';}$catStr = '<select>'. $catStr .'</select>';showMsg($catStr);}//获取栏目列表protected function get_cat_list() {//优化提示: 如果您网站的门户栏目在发布数据期间不会变动, 可以注释下面一行代码, 来加速接口执行速度loadcache('portalcategory');return $this->global['cache']['portalcategory'];}//设置发布用户IDprivate function set_uid() {if(empty($_POST['uid'])) showMsg('发布用户ID不能为空');$uidstr = str_replace(' ', '', $_POST['uid']);if(strpos($uidstr, ',')) { //多发布用户ID$uid_list = explode(',', $uidstr);shuffle($uid_list);$uid = intval(array_shift($uid_list));} else { //单发布用户ID$uid = intval($uidstr);}if($uid == 0) showMsg('发布用户ID格式不正确');$info = C::t('common_member')->fetch($uid);if(empty($info)) showMsg('不存在该用户 (uid: '. $uid .')');$this->uid = $uid;$this->global['uid'] = $uid;$this->global['member']['uid'] = $uid;$this->global['username'] = $info['username'];$this->global['session']['uid'] = $uid;//绕过discuz权限检测$this->global['group']['allowmanagearticle'] = true;$this->global['group']['allowpostarticlemod'] = true;}//将一些POST参数赋值至GET中, 因为discuz是通过$_GET来获取这些参数private function post2get() {foreach(array('author', 'url', 'from', 'fromurl', 'dateline', 'highlight_style') as $key) {$_GET[$key] = isset($_POST[$key]) && $_POST[$key] ? $_POST[$key] : '';}}//发布内容前的初始化private function init() {//构造以下POST参数仅用于程序不抛错误提示$_POST['id'] = '';$_POST['url'] = '';$_POST['raids'] = ''; //相关文章ID$_POST['tag'] = ''; //聚合标签$_POST['pagetitle'] = '';$_POST['htmlname'] = '';$_POST['highlight_style'] = '';$_POST['pagetitle'] = '';$_POST['attach_ids'] = '';$_POST['highlight_style'] = array('#000',0,0,0,);//设置文章阅读数$_POST['viewnum'] = isset($_POST['viewnum']) && $_POST['viewnum'] ? intval($_POST['viewnum']) : 1;}//处理上传图片private function process_attach() {global $_G;if($_G['uid']) {$_G['member'] = getuserbyuid($_G['uid']);}$_G['groupid'] = $_G['member']['groupid'];loadcache('usergroup_'.$_G['member']['groupid']);$_G['group'] = $_G['cache']['usergroup_'.$_G['member']['groupid']];$aid = 0;$catid = intval($_POST['catid']);$msg = '';$errorcode = 0;require_once libfile('function/portalcp');if($aid) {$article = C::t('portal_article_title')->fetch($aid);if(!$article) {$errorcode = 1;}if(check_articleperm($catid, $aid, $article, false, true) !== true) {$errorcode = 2;}} else {if(0 && check_articleperm($catid, $aid, null, false, true) !== true) {$errorcode = 3;}}$upload = new discuz_upload();$_FILES["Filedata"]['name'] = addslashes(diconv(urldecode($_FILES["Filedata"]['name']), 'UTF-8'));$upload->init($_FILES['Filedata'], 'portal');$attach = $upload->attach;if(!$upload->error()) {$upload->save();}if($upload->error()) {$errorcode = 4;}if(!$errorcode) {if($attach['isimage'] && empty($_G['setting']['portalarticleimgthumbclosed'])) {require_once libfile('class/image');$image = new image();$thumbimgwidth = $_G['setting']['portalarticleimgthumbwidth'] ? $_G['setting']['portalarticleimgthumbwidth'] : 300;$thumbimgheight = $_G['setting']['portalarticleimgthumbheight'] ? $_G['setting']['portalarticleimgthumbheight'] : 300;$attach['thumb'] = $image->Thumb($attach['target'], '', $thumbimgwidth, $thumbimgheight, 2);$image->Watermark($attach['target'], '', 'portal');}if(0 && getglobal('setting/ftp/on') && ((!$_G['setting']['ftp']['allowedexts'] && !$_G['setting']['ftp']['disallowedexts']) || ($_G['setting']['ftp']['allowedexts'] && in_array($attach['ext'], $_G['setting']['ftp']['allowedexts'])) || ($_G['setting']['ftp']['disallowedexts'] && !in_array($attach['ext'], $_G['setting']['ftp']['disallowedexts']))) && (!$_G['setting']['ftp']['minsize'] || $attach['size'] >= $_G['setting']['ftp']['minsize'] * 1024)) {if(ftpcmd('upload', 'portal/'.$attach['attachment']) && (!$attach['thumb'] || ftpcmd('upload', 'portal/'.getimgthumbname($attach['attachment'])))) {@unlink($_G['setting']['attachdir'].'/portal/'.$attach['attachment']);@unlink($_G['setting']['attachdir'].'/portal/'.getimgthumbname($attach['attachment']));$attach['remote'] = 1;} else {if(getglobal('setting/ftp/mirror')) {@unlink($attach['target']);@unlink(getimgthumbname($attach['target']));$errorcode = 5;}}}$setarr = array('uid' => $_G['uid'],'filename' => $attach['name'],'attachment' => $attach['attachment'],'filesize' => $attach['size'],'isimage' => $attach['isimage'],'thumb' => $attach['thumb'],'remote' => $attach['remote'],'filetype' => $attach['extension'],'dateline' => $_G['timestamp'],'aid' => $aid);$setarr['attachid'] = C::t('portal_attachment')->insert($setarr, true);if($attach['isimage']) {require_once libfile('function/home');$smallimg = pic_get($attach['attachment'], 'portal', $attach['thumb'], $attach['remote']);$bigimg = pic_get($attach['attachment'], 'portal', 0, $attach['remote']);$coverstr = addslashes(serialize(array('pic'=>'portal/'.$attach['attachment'], 'thumb'=>$attach['thumb'], 'remote'=>$attach['remote'])));$returnstr = "{\"aid\":$setarr[attachid], \"isimage\":$attach[isimage], \"smallimg\":\"$smallimg\", \"bigimg\":\"$bigimg\", \"errorcode\":$errorcode, \"cover\":\"$coverstr\"}";return json_decode($returnstr, true);exit();} else {$fileurl = 'portal.php?mod=attachment&id='.$attach['attachid'];echo "{\"aid\":$setarr[attachid], \"isimage\":$attach[isimage], \"file\":\"$fileurl\", \"errorcode\":$errorcode}";exit();}} else {echo "{\"aid\":0, \"errorcode\":$errorcode}";}}//获取网络图片, 用于封面图片private function downloadremotefile($imageurl) {global $_G;$aid = 0;$upload = new discuz_upload();$attach['ext'] = $upload->fileext($imageurl);if(!$upload->is_image_ext($attach['ext'])) return;$content = dfsockopen($imageurl);if(empty($content)) continue;$temp = explode('/', $imageurl);$attach['name'] = trim($temp[count($temp)-1]);$attach['thumb'] = '';$attach['isimage'] = $upload -> is_image_ext($attach['ext']);$attach['extension'] = $upload -> get_target_extension($attach['ext']);$attach['attachdir'] = $upload -> get_target_dir('portal');$attach['attachment'] = $attach['attachdir'] . $upload->get_target_filename('portal').'.'.$attach['extension'];$attach['target'] = getglobal('setting/attachdir').'./portal/'.$attach['attachment'];if(!@$fp = fopen($attach['target'], 'wb')) {return;} else {flock($fp, 2);fwrite($fp, $content);fclose($fp);}if(!$upload->get_image_info($attach['target'])) {@unlink($attach['target']);return;}$attach['size'] = filesize($attach['target']);$attach = daddslashes($attach);if(!$attach) return;if($attach['isimage'] || empty($_G['setting']['portalarticleimgthumbclosed'])) {require_once libfile('class/image');$image = new image();$thumbimgwidth = $_G['setting']['portalarticleimgthumbwidth'] ? $_G['setting']['portalarticleimgthumbwidth'] : 300;$thumbimgheight = $_G['setting']['portalarticleimgthumbheight'] ? $_G['setting']['portalarticleimgthumbheight'] : 300;$attach['thumb'] = $image->Thumb($attach['target'], '', $thumbimgwidth, $thumbimgheight, 2);$image->Watermark($attach['target'], '', 'portal');}$setarr = array('uid' => $_G['uid'],'filename' => $attach['name'],'attachment' => $attach['attachment'],'filesize' => $attach['size'],'isimage' => $attach['isimage'],'thumb' => $attach['thumb'],'remote' => $attach['remote'],'filetype' => $attach['extension'],'dateline' => $_G['timestamp'],'aid' => $aid);$setarr['attachid'] = C::t('portal_attachment')->insert($setarr, true);if($attach['isimage']) {$errorcode = 0;require_once libfile('function/home');$smallimg = pic_get($attach['attachment'], 'portal', $attach['thumb'], $attach['remote']);$bigimg = pic_get($attach['attachment'], 'portal', 0, $attach['remote']);$coverstr = addslashes(serialize(array('pic'=>'portal/'.$attach['attachment'], 'thumb'=>$attach['thumb'], 'remote'=>$attach['remote'])));$returnstr = "{\"aid\":$setarr[attachid], \"isimage\":$attach[isimage], \"smallimg\":\"$smallimg\", \"bigimg\":\"$bigimg\", \"errorcode\":$errorcode, \"cover\":\"$coverstr\"}";$return = json_decode($returnstr, true);return $return;}return;}//发布内容protected function publish_content() {if(DEBUG) {//调试模式下显示所有级别错误提示error_reporting(E_ALL);if(0) {//构造发布参数, 用于页面调试$_POST = array('uid' => '1','catid' => '1','title' => '测试标题 - '. DATE_TIME,'content' => '测试内容 - '. DATE_TIME,'summary' => '测试摘要~ - '. DATE_TIME,'dateline' => DATE_TIME,'author' => '清顽于世','from' => '新浪网','fromurl' => 'http://www.baidu.com/','viewnum' => '15','setconver' => '1',);}}//图片上传$upload = array();if(!empty($_FILES) && !empty($_FILES['attach_0']['name'])) {$files = $_FILES;foreach ($files as $key => $value) {$_FILES['Filedata'] = $value;$attach = $this->process_attach();//处理内容里的图片地址if(!empty($attach)) {$upload[] = $attach;$_POST['content'] = preg_replace('#<img src="('. preg_quote($value['name']) .')" />#iUs', '<img src="'. $attach['bigimg'] .'" />', $_POST['content']);}}}提取第一张图片至封面if(!empty($_POST['setconver'])) {//检测否是需要下载网络图片, 用于封面preg_match_all("/\<img.+src=('|\"|)?(.*)(\\1)([\s].*)?\>/ismUe", $_POST['content'], $matches, PREG_SET_ORDER);if(!empty($matches) && is_array($matches)) {$imageurl = $matches[0][2];if(substr($imageurl, 0, 7) == 'http://' || substr($imageurl, 0, 8) == 'https://') { //内容里第一张图片是网络图片$data = $this->downloadremotefile($imageurl);if(!empty($data)) {$conver = $data['cover'];} else {$conver = '';}} else { //第一张图片为本地图片$conver = $upload[0]['cover'];}$_POST['conver'] = $conver;}}//发布内容前的初始化$this->init();//POST参数转为GET参数$this->post2get();//设置发布用户ID$this->set_uid();$catid = $_POST['catid'];check_articleperm($catid);$_POST['title'] = getstr(trim($_POST['title']), 80);if(strlen($_POST['title']) < 1) {showMsg('标题不能少于 2 个字符');}$_POST['title'] = censor($_POST['title']);$_POST['pagetitle'] = getstr(trim($_POST['pagetitle']), 60);$_POST['pagetitle'] = censor($_POST['pagetitle']);$htmlname = basename(trim($_POST['htmlname']));$highlight_style = $_GET['highlight_style'];$style = '';$style = implode('|', $highlight_style);if(empty($_POST['summary'])) $_POST['summary'] = preg_replace("/(\s|\<strong\>##########NextPage(\[title=.*?\])?##########\<\/strong\>)+/", ' ', $_POST['content']);$summary = portalcp_get_summary($_POST['summary']);$summary = censor($summary);$_GET['author'] = dhtmlspecialchars($_GET['author']);$_GET['url'] = str_replace('&', '&', dhtmlspecialchars($_GET['url']));$_GET['from'] = dhtmlspecialchars($_GET['from']);$_GET['fromurl'] = str_replace('&', '&', dhtmlspecialchars($_GET['fromurl']));$_GET['dateline'] = !empty($_GET['dateline']) ? strtotime($_GET['dateline']) : TIMESTAMP;if(empty($_GET['dateline'])) $_GET['dateline'] = TIMESTAMP;if(substr($_GET['url'], 0, 7) !== 'http://') {$_GET['url'] = '';}if(substr($_GET['fromurl'], 0, 7) !== 'http://') {$_GET['fromurl'] = '';}if(0 && (censormod($_POST['title']) || $_G['group']['allowpostarticlemod'])) { //让文章发布状态始终为true$article_status = 1;} else {$article_status = 0;}$setarr = array('idtype' => '','title' => $_POST['title'],'author' => $_GET['author'],'from' => $_GET['from'],'fromurl' => $_GET['fromurl'],'dateline' => intval($_GET['dateline']),'url' => $_GET['url'],'allowcomment' => !empty($_POST['forbidcomment']) ? '0' : '1','summary' => $summary,'catid' => intval($_POST['catid']),'tag' => article_make_tag($_POST['tag']),'status' => $article_status,'highlight' => $style,'showinnernav' => empty($_POST['showinnernav']) ? '0' : '1',);if(empty($setarr['catid'])) {showMsg('栏目不能为空');}if(!empty($_POST['conver'])) {$converfiles = dunserialize($_POST['conver']);$setarr['pic'] = $converfiles['pic'];$setarr['thumb'] = intval($converfiles['thumb']);$setarr['remote'] = intval($converfiles['remote']);}$id = 0;$idtype = '';if(empty($article)) {$setarr['uid'] = $this->global['uid'];$setarr['username'] = $this->global['username'];$setarr['id'] = intval($_POST['id']);$setarr['htmlname'] = $htmlname;$table = '';if(0 && $setarr['id']) {if($_POST['idtype']=='blogid') {$table = 'home_blogfield';$setarr['idtype'] = 'blogid';$id = $setarr['id'];$idtype = $setarr['idtype'];} else {$table = 'forum_thread';$setarr['idtype'] = 'tid';require_once libfile('function/discuzcode');$id = C::t('forum_post')->fetch_threadpost_by_tid_invisible($setarr['id']);$id = $id['pid'];$idtype = 'pid';}}$aid = C::t('portal_article_title')->insert($setarr, 1);if(0 && $table) {if($_POST['idtype']=='blogid') {C::t('home_blogfield')->update($setarr['id'], array('pushedaid' => $aid));} elseif($setarr['idtype']=='tid') {$modarr = array('tid' => $setarr['id'],'uid' => $_G['uid'],'username' => $_G['username'],'dateline' => TIMESTAMP,'action' => 'PTA','status' => '1','stamp' => '',);C::t('forum_threadmod')->insert($modarr);C::t('forum_thread')->update($setarr['id'], array('moderated' => 1, 'pushedaid' => $aid));}}C::t('common_member_status')->update($this->global['uid'], array('lastpost' => TIMESTAMP), 'UNBUFFERED');C::t('portal_category')->increase($setarr['catid'], array('articles' => 1));C::t('portal_category')->update($setarr['catid'], array('lastpublish' => TIMESTAMP));C::t('portal_article_count')->insert(array('aid'=>$aid, 'catid'=>$setarr['catid'], 'viewnum'=>$_POST['viewnum']));} else {//这里面基本不可能执行到if($htmlname && $article['htmlname'] !== $htmlname) {$setarr['htmlname'] = $htmlname;$oldarticlename = $article['htmldir'].$article['htmlname'];unlink($oldarticlename.'.'.$_G['setting']['makehtml']['extendname']);for($i = 1; $i < $article['contents']; $i++) {unlink($oldarticlename.$i.'.'.$_G['setting']['makehtml']['extendname']);}}C::t('portal_article_title')->update($aid, $setarr);}$content = getstr($_POST['content'], 0, 0, 0, 0, 1);$content = censor($content);if(0 && (censormod($content) || $this->global['group']['allowpostarticlemod'])) {$article_status = 1;} else {$article_status = 0;}$regexp = '/(\<strong\>##########NextPage(\[title=(.*?)\])?##########\<\/strong\>)+/is';preg_match_all($regexp, $content ,$arr);$pagetitle = !empty($arr[3]) ? $arr[3] : array();$pagetitle = array_map('trim', $pagetitle);array_unshift($pagetitle, $_POST['pagetitle']);$contents = preg_split($regexp, $content);$cpostcount = count($contents);$dbcontents = C::t('portal_article_content')->fetch_all($aid);$pagecount = $cdbcount = count($dbcontents);if($cdbcount > $cpostcount) {$cdelete = array();foreach(array_splice($dbcontents, $cpostcount) as $value) {$cdelete[$value['cid']] = $value['cid'];}if(!empty($cdelete)) {C::t('portal_article_content')->delete($cdelete);}$pagecount = $cpostcount;}foreach($dbcontents as $key => $value) {C::t('portal_article_content')->update($value['cid'], array('title' => $pagetitle[$key], 'content' => $contents[$key], 'pageorder' => $key+1));unset($pagetitle[$key], $contents[$key]);}if($cdbcount < $cpostcount) {foreach($contents as $key => $value) {C::t('portal_article_content')->insert(array('aid' => $aid, 'id' => $setarr['id'], 'idtype' => $setarr['idtype'], 'title' => $pagetitle[$key], 'content' => $contents[$key], 'pageorder' => $key+1, 'dateline' => TIMESTAMP));}$pagecount = $cpostcount;}$updatearticle = array('contents' => $pagecount);if($article_status == 1) {$updatearticle['status'] = 1;updatemoderate('aid', $aid);manage_addnotify('verifyarticle');}$updatearticle = array_merge($updatearticle, portalcp_article_pre_next($catid, $aid));C::t('portal_article_title')->update($aid, $updatearticle);$newaids = array();$_POST['attach_ids'] = explode(',', $_POST['attach_ids']);foreach ($_POST['attach_ids'] as $newaid) {$newaid = intval($newaid);if($newaid) $newaids[$newaid] = $newaid;}if($newaids) {C::t('portal_attachment')->update_to_used($newaids, $aid);}addrelatedarticle($aid, $_POST['raids']);if(0 && $_GET['from_idtype'] && $_GET['from_id']) {$id = intval($_GET['from_id']);$notify = array();switch ($_GET['from_idtype']) {case 'blogid':$blog = C::t('home_blog')->fetch($id);if(!empty($blog)) {$notify = array('url' => "home.php?mod=space&uid=$blog[uid]&do=blog&id=$id",'subject' => $blog['subject']);$touid = $blog['uid'];}break;case 'tid':$thread = C::t('forum_thread')->fetch($id);if(!empty($thread)) {$notify = array('url' => "forum.php?mod=viewthread&tid=$id",'subject' => $thread['subject']);$touid = $thread['authorid'];}break;}if(!empty($notify)) {$notify['newurl'] = 'portal.php?mod=view&aid='.$aid;notification_add($touid, 'pusearticle', 'puse_article', $notify, 1);}}if(trim($_GET['from']) != '') {$from_cookie = '';$from_cookie_array = array();$from_cookie = getcookie('from_cookie');$from_cookie_array = explode("\t", $from_cookie);$from_cookie_array[] = $_GET['from'];$from_cookie_array = array_unique($from_cookie_array);$from_cookie_array = array_filter($from_cookie_array);$from_cookie_num = count($from_cookie_array);$from_cookie_start = $from_cookie_num - 10;$from_cookie_start = $from_cookie_start > 0 ? $from_cookie_start : 0;$from_cookie_array = array_slice($from_cookie_array, $from_cookie_start, $from_cookie_num);$from_cookie = implode("\t", $from_cookie_array);dsetcookie('from_cookie', $from_cookie);}dsetcookie('clearUserdata', 'home');$op = 'add_success';$article_add_url = 'portal.php?mod=portalcp&ac=article&catid='.$catid;$article = C::t('portal_article_title')->fetch($aid);$viewarticleurl = $_POST['url'] ? "portal.php?mod=list&catid=$_POST[catid]" : fetch_article_url($article);showMsg('发布成功');}}//接口需要用到的函数库function portalcp_get_summary($message) {$message = preg_replace(array("/\[attach\].*?\[\/attach\]/", "/\&[a-z]+\;/i", "/\<script.*?\<\/script\>/"), '', $message);$message = preg_replace("/\[.*?\]/", '', $message);$message = getstr(strip_tags($message), 200);return $message;}function portalcp_article_pre_next($catid, $aid) {$data = array('preaid' => C::t('portal_article_title')->fetch_preaid_by_catid_aid($catid, $aid),'nextaid' => C::t('portal_article_title')->fetch_nextaid_by_catid_aid($catid, $aid),);if($data['preaid']) {C::t('portal_article_title')->update($data['preaid'], array('preaid' => C::t('portal_article_title')->fetch_preaid_by_catid_aid($catid, $data['preaid']),'nextaid' => C::t('portal_article_title')->fetch_nextaid_by_catid_aid($catid, $data['preaid']),));}return $data;}?>

转载于:https://www.cnblogs.com/68xi/p/9455812.html

相关推荐

相关文章