示例:discuz发帖 (DiscuzDemoSkycaiji)

  更新日期:2019-02-22 00:00
实现discuz发帖的功能,您可参考该代码开发更多discuz应用

插件代码

<?php
/*全局函数*/
namespace{
	/*discuz函数html2bbcode中用到*/
	function dhtmlspecialchars($string, $flags = null) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = dhtmlspecialchars($val, $flags);
			}
		} else {
			if($flags === null) {
				$string = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string);
				if(strpos($string, '&amp;#') !== false) {
					$string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
				}
			} else {
				$string = htmlspecialchars($string, $flags);
			}
		}
		return $string;
	};
	function dstripslashes($string) {
		if(empty($string)) return $string;
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = dstripslashes($val);
			}
		} else {
			$string = stripslashes($string);
		}
		return $string;
	}
	function libfile($libname, $folder = '') {
		$libpath = '/source/'.$folder;
		if(strstr($libname, '/')) {
			list($pre, $name) = explode('/', $libname);
			$path = "{$libpath}/{$pre}/{$pre}_{$name}";
		} else {
			$path = "{$libpath}/{$libname}";
		}
		return preg_match('/^[\w\d\/_]+$/i', $path) ? realpath($GLOBALS['DISCUZ_ROOT'].$path.'.php') : false;
	}
}
/**
 * 示例:discuz发帖
 * 您可参考代码自行开发discuz的更多功能
 * 您可以使用 thinkphp5的函数
 */
namespace plugin\release\cms{
class DiscuzDemoSkycaiji extends BaseCms{
	public $siteurl;//discuz网站网址
	public function init_extend(){
		//获取站点url
		$siteurl=$this->db()->table('__COMMON_SETTING__')->where("`skey`='siteurl'")->find();
		$this->siteurl=rtrim($siteurl['svalue'],'\\\/').'/';

		//调用discuz函数
		define('IN_DISCUZ',1);
		$GLOBALS['DISCUZ_ROOT']=$this->cmsPath.'/';
		include_once $this->cmsPath.'/source/function/function_editor.php';
	}
	//参数
	public $_params = array (
		'forumid' => array (
			'name' => '版块ID',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_forumid',
		),
		'author' => array (
			'name' => '用户名或ID',
			'require' => 1,
			'tag' => 'text',
		),
		'title' => array (
			'name' => '帖子标题',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'content' => array (
			'name' => '帖子内容',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'typeid' => array (
			'name' => '版块分类ID',
			'require' => 0,
			'tag' => 'number',
		),
	);
	
	
	/*
	 * 导入数据
	 * 必须以数组形式返回:
	 * id(必填)表示入库返回的自增id或状态
	 * target(可选)记录入库的数据位置(发布的网址等)
	 * desc(可选)记录入库的数据位置附加信息
	 * error(可选)记录入库失败的错误信息
	 * 入库的信息可在“已采集数据”中查看
	 * return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	 */
	public function runImport($params){
		//判断用户存在
		$userData=$this->db()->table('__COMMON_MEMBER__')->where('username',$params['author'])->whereOr('uid',$params['author'])->find();
		if(empty($userData)){
			return array('id'=>0,'error'=>$params['author'].'用户不存在');//返回错误信息
		}
		//添加新主题
		$newThread=array(
			'fid'=>$params['forumid'],
			'author'=>$userData['username'],
			'authorid'=>$userData['uid'],
			'subject'=>$params['title'],
			'dateline'=>time(),
			'lastpost'=>time(),
			'lastposter'=>$userData['username'],
			'typeid'=>$params['typeid'],
		);
		$target='';//目标网址
		$error='';//错误信息
		$threadId=$this->db()->table('__FORUM_THREAD__')->insert($newThread,false,true);//返回的id
		if($threadId>0){
			$postId=$this->db()->table('__FORUM_POST_TABLEID__')->insert(array('pid'=>0),false,true);//获取pid
			if($postId>0){
				//添加帖子
				$newPost=array(
					'pid'=>$postId,
					'fid'=>$params['forumid'],
					'tid'=>$threadId,
					'first'=>1,
					'author'=>$userData['username'],
					'authorid'=>$userData['uid'],
					'subject'=>$params['title'],
					'dateline'=>time(),
					'message'=>html2bbcode($params['content']),//html转成discuz格式
					'position'=>1,
				);
				
				$this->db()->table('__FORUM_POST__')->insert($newPost);
				
				$target=$this->siteurl.'forum.php?mod=viewthread&tid='.$threadId;
			}else{
				$error='添加帖子失败';
			}
		}else{
			$error='添加主题失败';
		}
		return array('id'=>$threadId,'target'=>$target,'error'=>$error);
	}
	/*
	 * 自定义方法:版块选项
	 * 必须返回键值对形式的数组
	 */
	public function param_option_forumid(){
		$forumDb=$this->db()->table('__FORUM_FORUM__')->where("`status`=1 and `type`<>'group'")->select();
		//读取论坛版块
		$forumList=array();
		foreach ($forumDb as $forum){
			$forumList[$forum['fid']]=auto_convert2utf8($forum['name']);//自动转码
		}
		return $forumList;
	}
}
}
?>