model_forum_thread.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: model_forum_thread.php 34684 2014-07-07 07:27:56Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class model_forum_thread extends discuz_model
  12. {
  13. public $forum;
  14. public $thread;
  15. public $post;
  16. public $tid;
  17. public $pid;
  18. public $feed = array();
  19. public function __construct($fid = null) {
  20. parent::__construct();
  21. if($fid) {
  22. include_once libfile('function/forum');
  23. loadforum($fid);
  24. }
  25. $this->forum = &$this->app->var['forum'];
  26. }
  27. public function newthread($parameters) {
  28. require_once libfile('function/post');
  29. $this->tid = $this->pid = 0;
  30. $this->_init_parameters($parameters);
  31. if(trim($this->param['subject']) == '') {
  32. return $this->showmessage('post_sm_isnull');
  33. }
  34. if(!$this->param['sortid'] && !$this->param['special'] && trim($this->param['message']) == '') {
  35. return $this->showmessage('post_sm_isnull');
  36. }
  37. list($this->param['modnewthreads'], $this->param['modnewreplies']) = threadmodstatus($this->param['subject']."\t".$this->param['message'].$this->param['extramessage']);
  38. if(($post_invalid = checkpost($this->param['subject'], $this->param['message'], ($this->param['special'] || $this->param['sortid'])))) {
  39. return $this->showmessage($post_invalid, '', array('minpostsize' => $this->setting['minpostsize'], 'maxpostsize' => $this->setting['maxpostsize']));
  40. }
  41. if(checkflood()) {
  42. return $this->showmessage('post_flood_ctrl', '', array('floodctrl' => $this->setting['floodctrl']));
  43. } elseif(checkmaxperhour('tid')) {
  44. return $this->showmessage('thread_flood_ctrl_threads_per_hour', '', array('threads_per_hour' => $this->group['maxthreadsperhour']));
  45. }
  46. $this->param['save'] = $this->member['uid'] ? $this->param['save'] : 0;
  47. $this->param['typeid'] = isset($this->param['typeid']) && isset($this->forum['threadtypes']['types'][$this->param['typeid']]) && (!$this->forum['threadtypes']['moderators'][$this->param['typeid']] || $this->forum['ismoderator']) ? $this->param['typeid'] : 0;
  48. $this->param['displayorder'] = $this->param['modnewthreads'] ? -2 : (($this->forum['ismoderator'] && $this->group['allowstickthread'] && !empty($this->param['sticktopic'])) ? 1 : (empty($this->param['save']) ? 0 : -4));
  49. if($this->param['displayorder'] == -2) {
  50. C::t('forum_forum')->update($this->forum['fid'], array('modworks' => '1'));
  51. }
  52. $this->param['digest'] = $this->forum['ismoderator'] && $this->group['allowdigestthread'] && !empty($this->param['digest']) ? 1 : 0;
  53. $this->param['readperm'] = $this->group['allowsetreadperm'] ? $this->param['readperm'] : 0;
  54. $this->param['isanonymous'] = $this->group['allowanonymous'] && $this->param['isanonymous'] ? 1 : 0;
  55. $this->param['price'] = intval($this->param['price']);
  56. if(!$this->param['special']) {
  57. $this->param['price'] = $this->group['maxprice'] ? ($this->param['price'] <= $this->group['maxprice'] ? $this->param['price'] : $this->group['maxprice']) : 0;
  58. }
  59. if(!$this->param['typeid'] && $this->forum['threadtypes']['required'] && !$this->param['special']) {
  60. return $this->showmessage('post_type_isnull');
  61. }
  62. if(!$this->param['sortid'] && $this->forum['threadsorts']['required'] && !$this->param['special']) {
  63. return $this->showmessage('post_sort_isnull');
  64. }
  65. if(!$this->param['special'] && $this->param['price'] > 0 && floor($this->param['price'] * (1 - $this->setting['creditstax'])) == 0) {
  66. return $this->showmessage('post_net_price_iszero');
  67. }
  68. $this->param['sortid'] = $this->param['special'] && $this->forum['threadsorts']['types'][$this->param['sortid']] ? 0 : $this->param['sortid'];
  69. $this->param['typeexpiration'] = intval($this->param['typeexpiration']);
  70. if($this->forum['threadsorts']['expiration'][$this->param['typeid']] && !$this->param['typeexpiration']) {
  71. return $this->showmessage('threadtype_expiration_invalid');
  72. }
  73. $author = !$this->param['isanonymous'] ? $this->member['username'] : '';
  74. $this->param['moderated'] = $this->param['digest'] || $this->param['displayorder'] > 0 ? 1 : 0;
  75. $this->param['ordertype'] && $this->param['tstatus'] = setstatus(4, 1, $this->param['tstatus']);
  76. $this->param['imgcontent'] && $this->param['tstatus'] = setstatus(15, $this->param['imgcontent'], $this->param['tstatus']);
  77. $this->param['hiddenreplies'] && $this->param['tstatus'] = setstatus(2, 1, $this->param['tstatus']);
  78. $this->param['allownoticeauthor'] && $this->param['tstatus'] = setstatus(6, 1, $this->param['tstatus']);
  79. $this->param['isgroup'] = $this->forum['status'] == 3 ? 1 : 0;
  80. $this->param['publishdate'] = !$this->param['modnewthreads'] ? $this->param['publishdate'] : TIMESTAMP;
  81. $newthread = array(
  82. 'fid' => $this->forum['fid'],
  83. 'posttableid' => 0,
  84. 'readperm' => $this->param['readperm'],
  85. 'price' => $this->param['price'],
  86. 'typeid' => $this->param['typeid'],
  87. 'sortid' => $this->param['sortid'],
  88. 'author' => $author,
  89. 'authorid' => $this->member['uid'],
  90. 'subject' => $this->param['subject'],
  91. 'dateline' => $this->param['publishdate'],
  92. 'lastpost' => $this->param['publishdate'],
  93. 'lastposter' => $author,
  94. 'displayorder' => $this->param['displayorder'],
  95. 'digest' => $this->param['digest'],
  96. 'special' => $this->param['special'],
  97. 'attachment' => 0,
  98. 'moderated' => $this->param['moderated'],
  99. 'status' => $this->param['tstatus'],
  100. 'isgroup' => $this->param['isgroup'],
  101. 'replycredit' => $this->param['replycredit'],
  102. 'closed' => $this->param['closed'] ? 1 : 0
  103. );
  104. $this->tid = C::t('forum_thread')->insert($newthread, true);
  105. C::t('forum_newthread')->insert(array(
  106. 'tid' => $this->tid,
  107. 'fid' => $this->forum['fid'],
  108. 'dateline' => $this->param['publishdate'],
  109. ));
  110. useractionlog($this->member['uid'], 'tid');
  111. if(!getuserprofile('threads') && $this->setting['newbie']) {
  112. C::t('forum_thread')->update($this->tid, array('icon' => $this->setting['newbie']));
  113. }
  114. if ($this->param['publishdate'] != TIMESTAMP) {
  115. $cron_publish_ids = dunserialize($this->cache('cronpublish'));
  116. $cron_publish_ids[$this->tid] = $this->tid;
  117. $cron_publish_ids = serialize($cron_publish_ids);
  118. savecache('cronpublish', $cron_publish_ids);
  119. }
  120. if(!$this->param['isanonymous']) {
  121. C::t('common_member_field_home')->update($this->member['uid'], array('recentnote'=>$this->param['subject']));
  122. }
  123. if($this->param['moderated']) {
  124. updatemodlog($this->tid, ($this->param['displayorder'] > 0 ? 'STK' : 'DIG'));
  125. updatemodworks(($this->param['displayorder'] > 0 ? 'STK' : 'DIG'), 1);
  126. }
  127. $this->param['bbcodeoff'] = checkbbcodes($this->param['message'], !empty($this->param['bbcodeoff']));
  128. $this->param['smileyoff'] = checksmilies($this->param['message'], !empty($this->param['smileyoff']));
  129. $this->param['parseurloff'] = !empty($this->param['parseurloff']);
  130. $this->param['htmlon'] = $this->group['allowhtml'] && !empty($this->param['htmlon']) ? 1 : 0;
  131. $this->param['usesig'] = !empty($this->param['usesig']) && $this->group['maxsigsize'] ? 1 : 0;
  132. $class_tag = new tag();
  133. $this->param['tagstr'] = $class_tag->add_tag($this->param['tags'], $this->tid, 'tid');
  134. $this->param['pinvisible'] = $this->param['modnewthreads'] ? -2 : (empty($this->param['save']) ? 0 : -3);
  135. $this->param['message'] = preg_replace('/\[attachimg\](\d+)\[\/attachimg\]/is', '[attach]\1[/attach]', $this->param['message']);
  136. $this->param['pstatus'] = intval($this->param['pstatus']);
  137. defined('IN_MOBILE') && $this->param['pstatus'] = setstatus(4, 1, $this->param['pstatus']);
  138. if($this->param['imgcontent']) {
  139. stringtopic($this->param['message'], $this->tid, true, $this->param['imgcontentwidth']);
  140. }
  141. $this->pid = insertpost(array(
  142. 'fid' => $this->forum['fid'],
  143. 'tid' => $this->tid,
  144. 'first' => '1',
  145. 'author' => $this->member['username'],
  146. 'authorid' => $this->member['uid'],
  147. 'subject' => $this->param['subject'],
  148. 'dateline' => $this->param['publishdate'],
  149. 'message' => $this->param['message'],
  150. 'useip' => $this->param['clientip'] ? $this->param['clientip'] : getglobal('clientip'),
  151. 'port' => $this->param['remoteport'] ? $this->param['remoteport'] : getglobal('remoteport'),
  152. 'invisible' => $this->param['pinvisible'],
  153. 'anonymous' => $this->param['isanonymous'],
  154. 'usesig' => $this->param['usesig'],
  155. 'htmlon' => $this->param['htmlon'],
  156. 'bbcodeoff' => $this->param['bbcodeoff'],
  157. 'smileyoff' => $this->param['smileyoff'],
  158. 'parseurloff' => $this->param['parseurloff'],
  159. 'attachment' => '0',
  160. 'tags' => $this->param['tagstr'],
  161. 'replycredit' => 0,
  162. 'status' => $this->param['pstatus']
  163. ));
  164. $statarr = array(0 => 'thread', 1 => 'poll', 2 => 'trade', 3 => 'reward', 4 => 'activity', 5 => 'debate', 127 => 'thread');
  165. include_once libfile('function/stat');
  166. updatestat($this->param['isgroup'] ? 'groupthread' : $statarr[$this->param['special']]);
  167. if($this->param['geoloc'] && IN_MOBILE == 2) {
  168. list($mapx, $mapy, $location) = explode('|', $this->param['geoloc']);
  169. if($mapx && $mapy && $location) {
  170. C::t('forum_post_location')->insert(array(
  171. 'pid' => $this->pid,
  172. 'tid' => $this->tid,
  173. 'uid' => $this->member['uid'],
  174. 'mapx' => $mapx,
  175. 'mapy' => $mapy,
  176. 'location' => $location,
  177. ));
  178. }
  179. }
  180. if($this->param['modnewthreads']) {
  181. updatemoderate('tid', $this->tid);
  182. C::t('forum_forum')->update_forum_counter($this->forum['fid'], 0, 0, 1);
  183. manage_addnotify('verifythread');
  184. return 'post_newthread_mod_succeed';
  185. } else {
  186. if($this->param['displayorder'] != -4) {
  187. if($this->param['digest']) {
  188. updatepostcredits('+', $this->member['uid'], 'digest', $this->forum['fid']);
  189. }
  190. updatepostcredits('+', $this->member['uid'], 'post', $this->forum['fid']);
  191. if($this->param['isgroup']) {
  192. C::t('forum_groupuser')->update_counter_for_user($this->member['uid'], $this->forum['fid'], 1);
  193. }
  194. $subject = str_replace("\t", ' ', $this->param['subject']);
  195. $lastpost = "$this->tid\t".$subject."\t".TIMESTAMP."\t$author";
  196. C::t('forum_forum')->update($this->forum['fid'], array('lastpost' => $lastpost));
  197. C::t('forum_forum')->update_forum_counter($this->forum['fid'], 1, 1, 1);
  198. if($this->forum['type'] == 'sub') {
  199. C::t('forum_forum')->update($this->forum['fup'], array('lastpost' => $lastpost));
  200. }
  201. }
  202. if($this->param['isgroup']) {
  203. C::t('forum_forumfield')->update($this->forum['fid'], array('lastupdate' => TIMESTAMP));
  204. require_once libfile('function/grouplog');
  205. updategroupcreditlog($this->forum['fid'], $this->member['uid']);
  206. }
  207. C::t('forum_sofa')->insert(array('tid' => $this->tid,'fid' => $this->forum['fid']));
  208. return 'post_newthread_succeed';
  209. }
  210. }
  211. public function feed() {
  212. if($this->forum('allowfeed') && !$this->param['isanonymous']) {
  213. if(empty($this->feed)) {
  214. $this->feed = array(
  215. 'icon' => '',
  216. 'title_template' => '',
  217. 'title_data' => array(),
  218. 'body_template' => '',
  219. 'body_data' => array(),
  220. 'title_data' => array(),
  221. 'images' => array()
  222. );
  223. $message = !$this->param['price'] && !$this->param['readperm'] ? $this->param['message'] : '';
  224. $message = messagesafeclear($message);
  225. $this->feed['icon'] = 'thread';
  226. $this->feed['title_template'] = 'feed_thread_title';
  227. $this->feed['body_template'] = 'feed_thread_message';
  228. $this->feed['body_data'] = array(
  229. 'subject' => "<a href=\"forum.php?mod=viewthread&tid={$this->tid}\">{$this->param['subject']}</a>",
  230. 'message' => messagecutstr($message, 150)
  231. );
  232. if(getglobal('forum_attachexist')) {// $firstaid = DB::result_first("SELECT aid FROM ".DB::table(getattachtablebytid($tid))." WHERE pid='$pid' AND dateline>'0' AND isimage='1' ORDER BY dateline LIMIT 1");
  233. $imgattach = C::t('forum_attachment_n')->fetch_max_image('tid:'.$this->tid, 'pid', $this->pid);
  234. $firstaid = $imgattach['aid'];
  235. unset($imgattach);
  236. if($firstaid) {
  237. $this->feed['images'] = array(getforumimg($firstaid));
  238. $this->feed['image_links'] = array("forum.php?mod=viewthread&do=tradeinfo&tid={$this->tid}&pid={$this->pid}");
  239. }
  240. }
  241. }
  242. $this->feed['title_data']['hash_data'] = 'tid'.$this->tid;
  243. $this->feed['id'] = $this->tid;
  244. $this->feed['idtype'] = 'tid';
  245. if($this->feed['icon']) {
  246. postfeed($this->feed);
  247. }
  248. }
  249. }
  250. protected function _init_parameters($parameters){
  251. $varname = array(
  252. 'member', 'group', 'forum', 'extramessage',
  253. 'subject', 'sticktopic', 'save', 'ordertype', 'hiddenreplies',
  254. 'allownoticeauthor', 'readperm', 'price', 'typeid', 'sortid',
  255. 'publishdate', 'digest', 'moderated', 'tstatus', 'isgroup', 'imgcontent', 'imgcontentwidth',
  256. 'replycredit', 'closed', 'special', 'tags',
  257. 'message','clientip', 'invisible', 'isanonymous', 'usesig',
  258. 'htmlon', 'bbcodeoff', 'smileyoff', 'parseurloff', 'pstatus', 'geoloc',
  259. );
  260. foreach($varname as $name) {
  261. if(!isset($this->param[$name]) && isset($parameters[$name])) {
  262. $this->param[$name] = $parameters[$name];
  263. }
  264. }
  265. }
  266. public function forum($name = null, $val = null) {
  267. if(isset($val)) {
  268. return $this->setvar($this->forum, $name, $val);
  269. } else {
  270. return $this->getvar($this->forum, $name);
  271. }
  272. }
  273. }
  274. ?>