Topic.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace ForkBB\Models\Pages;
  3. use ForkBB\Models\Page;
  4. class Topic extends Page
  5. {
  6. use CrumbTrait;
  7. /**
  8. * Переход к первому новому сообщению темы (или в конец)
  9. *
  10. * @param array $args
  11. *
  12. * @return Page
  13. */
  14. public function viewNew(array $args)
  15. {
  16. return $this->view('new', $args);
  17. }
  18. /**
  19. * Переход к первому непрочитанному сообщению (или в конец)
  20. *
  21. * @param array $args
  22. *
  23. * @return Page
  24. */
  25. public function viewUnread(array $args)
  26. {
  27. return $this->view('unread', $args);
  28. }
  29. /**
  30. * Переход к последнему сообщению темы
  31. *
  32. * @param array $args
  33. *
  34. * @return Page
  35. */
  36. public function viewLast(array $args)
  37. {
  38. return $this->view('last', $args);
  39. }
  40. /**
  41. * Просмотр темы по номеру сообщения
  42. *
  43. * @param array $args
  44. *
  45. * @return Page
  46. */
  47. public function viewPost(array $args)
  48. {
  49. return $this->view('post', $args);
  50. }
  51. /**
  52. * Просмотр темы по ее номеру
  53. *
  54. * @param array $args
  55. *
  56. * @return Page
  57. */
  58. public function viewTopic(array $args)
  59. {
  60. return $this->view('topic', $args);
  61. }
  62. /**
  63. * @param string $type
  64. * @param Models\Topic $topic
  65. *
  66. * @param Page
  67. */
  68. protected function go($type, $topic)
  69. {
  70. switch ($type) {
  71. case 'new':
  72. $pid = $topic->firstNew;
  73. break;
  74. case 'unread':
  75. $pid = $topic->firstUnread;
  76. break;
  77. case 'last':
  78. $pid = $topic->last_post_id;
  79. break;
  80. default:
  81. return $this->c->Message->message('Bad request');
  82. }
  83. return $this->c->Redirect->page('ViewPost', ['id' => $pid ?: $topic->last_post_id]);
  84. }
  85. /**
  86. * Подготовка данных для шаблона
  87. *
  88. * @param string $type
  89. * @param array $args
  90. *
  91. * @return Page
  92. */
  93. protected function view($type, array $args)
  94. {
  95. $topic = $this->c->ModelTopic->load((int) $args['id'], $type === 'post');
  96. if (! $topic->id || ! $topic->last_post_id) {
  97. return $this->c->Message->message('Bad request');
  98. }
  99. if ($topic->moved_to) {
  100. return $this->c->Redirect->page('Topic', ['id' => $topic->moved_to]);
  101. }
  102. switch ($type) {
  103. case 'topic':
  104. $topic->page = isset($args['page']) ? (int) $args['page'] : 1;
  105. break;
  106. case 'post':
  107. $topic->calcPage((int) $args['id']);
  108. break;
  109. default:
  110. return $this->go($type, $topic);
  111. }
  112. if (! $topic->hasPage()) {
  113. return $this->c->Message->message('Bad request');
  114. }
  115. if (! $posts = $topic->posts()) {
  116. return $this->go('last', $topic);
  117. }
  118. $this->c->Lang->load('topic');
  119. $user = $this->c->user;
  120. /*
  121. list($fTree, $fDesc, $fAsc) = $this->c->forums;
  122. $moders = empty($topic['moderators']) ? [] : array_flip(unserialize($topic['moderators']));
  123. $parent = isset($fDesc[$topic['forum_id']][0]) ? $fDesc[$topic['forum_id']][0] : 0;
  124. $perm = $fTree[$parent][$topic['forum_id']];
  125. if($user->isBot) {
  126. $perm['post_replies'] = 0;
  127. }
  128. $newOn = null;
  129. if ($user->isAdmin) {
  130. $newOn = true;
  131. } elseif ($topic['closed'] == '1') {
  132. $newOn = false;
  133. } elseif ($perm['post_replies'] === 1
  134. || (null === $perm['post_replies'] && $user->g_post_replies == '1')
  135. || ($user->isAdmMod && isset($moders[$user->id]))
  136. ) {
  137. $newOn = true;
  138. }
  139. // парсер и его настройка для сообщений
  140. $bbcodes = include $this->c->DIR_CONFIG . '/defaultBBCode.php';
  141. $smilies = $this->c->smilies->list; //????
  142. foreach ($smilies as &$cur) {
  143. $cur = $this->c->PUBLIC_URL . '/img/sm/' . $cur;
  144. }
  145. unset($cur);
  146. $bbInfo = $this->c->BBCODE_INFO;
  147. $bbWList = $this->c->config->p_message_bbcode == '1' ? null : [];
  148. $bbBList = $this->c->config->p_message_img_tag == '1' ? [] : ['img'];
  149. $parser = $this->c->Parser;
  150. $parser->setBBCodes($bbcodes)
  151. ->setAttr('isSign', false)
  152. ->setWhiteList($bbWList)
  153. ->setBlackList($bbBList);
  154. if ($user->show_smilies == '1') {
  155. $parser->setSmilies($smilies)
  156. ->setSmTpl($bbInfo['smTpl'], $bbInfo['smTplTag'], $bbInfo['smTplBl']);
  157. }
  158. $genders = [1 => ' f-user-male', 2 => ' f-user-female'];
  159. $postCount = 0;
  160. $posts = [];
  161. $signs = [];
  162. $posters = [];
  163. $timeMax = 0;
  164. while ($cur = $stmt->fetch()) {
  165. // данные по автору сообшения
  166. if (isset($posters[$cur['poster_id']])) {
  167. $post = $posters[$cur['poster_id']];
  168. } else {
  169. $post = [
  170. 'poster' => $cur['username'],
  171. 'poster_id' => $cur['poster_id'],
  172. 'poster_title' => $this->c->censorship->censor($this->userGetTitle($cur)),
  173. 'poster_avatar' => null,
  174. 'poster_registered' => null,
  175. 'poster_location' => null,
  176. 'poster_info_add' => false,
  177. 'poster_link' => null,
  178. 'poster_posts' => null,
  179. 'poster_gender' => '',
  180. 'poster_online' => '',
  181. ];
  182. if ($cur['poster_id'] > 1) {
  183. if ($user->g_view_users == '1') {
  184. $post['poster_link'] = $this->c->Router->link('User', ['id' => $cur['poster_id'], 'name' => $cur['username']]);
  185. }
  186. if ($this->c->config->o_avatars == '1' && $user->show_avatars == '1') {
  187. $post['poster_avatar'] = $this->userGetAvatarLink($cur['poster_id']);
  188. }
  189. if ($this->c->config->o_show_user_info == '1') {
  190. $post['poster_info_add'] = true;
  191. $post['poster_registered'] = $this->time($cur['registered'], true);
  192. $post['poster_posts'] = $this->number($cur['num_posts']);
  193. $post['poster_num_posts'] = $cur['num_posts'];
  194. if ($cur['location'] != '') {
  195. $post['poster_location'] = $this->c->censorship->censor($cur['location']);
  196. }
  197. if (isset($genders[$cur['gender']])) {
  198. $post['poster_gender'] = $genders[$cur['gender']];
  199. }
  200. }
  201. $post['poster_online'] = ' f-user-online'; //????
  202. $posters[$cur['poster_id']] = $post;
  203. if ($this->c->config->o_signatures == '1'
  204. && $cur['signature'] != ''
  205. && $user->show_sig == '1'
  206. && ! isset($signs[$cur['poster_id']])
  207. ) {
  208. $signs[$cur['poster_id']] = $cur['signature'];
  209. }
  210. }
  211. }
  212. // данные по сообщению
  213. $post['id'] = $cur['id'];
  214. $post['link'] = $this->c->Router->link('ViewPost', ['id' => $cur['id']]);
  215. $post['posted'] = $this->time($cur['posted']);
  216. $post['posted_utc'] = gmdate('Y-m-d\TH:i:s\Z', $cur['posted']);
  217. $timeMax = max($timeMax, $cur['posted']);
  218. $parser->parse($this->c->censorship->censor($cur['message']));
  219. if ($this->c->config->o_smilies == '1' && $user->show_smilies == '1' && $cur['hide_smilies'] == '0') {
  220. $parser->detectSmilies();
  221. }
  222. $post['message'] = $parser->getHtml();
  223. // номер сообшения в теме
  224. if ($stickFP && $offset > 0 && $cur['id'] == $topic['first_post_id']) {
  225. $post['post_number'] = 1;
  226. } else {
  227. ++$postCount;
  228. $post['post_number'] = $offset + $postCount;
  229. }
  230. // данные по элементам управления
  231. $controls = [];
  232. $vars = ['id' => $cur['id']];
  233. if (! $user->isAdmin && ! $user->isGuest) {
  234. $controls['report'] = [$this->c->Router->link('ReportPost', $vars), 'Report'];
  235. }
  236. if ($user->isAdmin
  237. || ($user->isAdmMod && isset($moders[$user->id]) && ! in_array($cur['poster_id'], $this->c->admins->list)) //????
  238. ) {
  239. $controls['delete'] = [$this->c->Router->link('DeletePost', $vars), 'Delete'];
  240. $controls['edit'] = [$this->c->Router->link('EditPost', $vars), 'Edit'];
  241. } elseif ($topic['closed'] != '1'
  242. && $cur['poster_id'] == $user->id
  243. && ($user->g_deledit_interval == '0' || $cur['edit_post'] == '1' || time() - $cur['posted'] < $user->g_deledit_interval)
  244. ) {
  245. if (($cur['id'] == $topic['first_post_id'] && $user->g_delete_topics == '1') || ($cur['id'] != $topic['first_post_id'] && $user->g_delete_posts == '1')) {
  246. $controls['delete'] = [$this->c->Router->link('DeletePost', $vars), 'Delete'];
  247. }
  248. if ($user->g_edit_posts == '1') {
  249. $controls['edit'] = [$this->c->Router->link('EditPost', $vars), 'Edit'];
  250. }
  251. }
  252. if ($newOn) {
  253. $controls['quote'] = [$this->c->Router->link('NewReply', ['id' => $topic['id'], 'quote' => $cur['id']]), 'Reply'];
  254. }
  255. $post['controls'] = $controls;
  256. $posts[] = $post;
  257. }
  258. if ($signs) {
  259. // настройка парсера для подписей
  260. $bbWList = $this->c->config->p_sig_bbcode == '1' ? $bbInfo['forSign'] : [];
  261. $bbBList = $this->c->config->p_sig_img_tag == '1' ? [] : ['img'];
  262. $parser->setAttr('isSign', true)
  263. ->setWhiteList($bbWList)
  264. ->setBlackList($bbBList);
  265. foreach ($signs as &$cur) {
  266. $parser->parse($this->c->censorship->censor($cur));
  267. if ($this->c->config->o_smilies_sig == '1' && $user->show_smilies == '1') {
  268. $parser->detectSmilies();
  269. }
  270. $cur = $parser->getHtml();
  271. }
  272. unset($cur);
  273. }
  274. $topic['subject'] = $this->c->censorship->censor($topic['subject']);
  275. */
  276. // данные для формы быстрого ответа
  277. $form = null;
  278. if ($topic->canReply && $this->c->config->o_quickpost == '1') {
  279. $form = [
  280. 'action' => $this->c->Router->link('NewReply', ['id' => $topic->id]),
  281. 'hidden' => [
  282. 'token' => $this->c->Csrf->create('NewReply', ['id' => $topic->id]),
  283. ],
  284. 'sets' => [],
  285. 'btns' => [
  286. 'submit' => ['submit', __('Submit'), 's'],
  287. 'preview' => ['submit', __('Preview'), 'p'],
  288. ],
  289. ];
  290. $fieldset = [];
  291. if ($user->isGuest) {
  292. $fieldset['username'] = [
  293. 'dl' => 't1',
  294. 'type' => 'text',
  295. 'maxlength' => 25,
  296. 'title' => __('Username'),
  297. 'required' => true,
  298. 'pattern' => '^.{2,25}$',
  299. ];
  300. $fieldset['email'] = [
  301. 'dl' => 't2',
  302. 'type' => 'text',
  303. 'maxlength' => 80,
  304. 'title' => __('Email'),
  305. 'required' => $this->c->config->p_force_guest_email == '1',
  306. 'pattern' => '.+@.+',
  307. ];
  308. }
  309. $fieldset['message'] = [
  310. 'type' => 'textarea',
  311. 'title' => __('Message'),
  312. 'required' => true,
  313. 'bb' => [
  314. ['link', __('BBCode'), __($this->c->config->p_message_bbcode == '1' ? 'on' : 'off')],
  315. ['link', __('url tag'), __($this->c->config->p_message_bbcode == '1' && $user->g_post_links == '1' ? 'on' : 'off')],
  316. ['link', __('img tag'), __($this->c->config->p_message_bbcode == '1' && $this->c->config->p_message_img_tag == '1' ? 'on' : 'off')],
  317. ['link', __('Smilies'), __($this->c->config->o_smilies == '1' ? 'on' : 'off')],
  318. ],
  319. ];
  320. $form['sets'][] = [
  321. 'fields' => $fieldset,
  322. ];
  323. $fieldset = [];
  324. if ($user->isAdmin || ($user->isAdmMod && isset($moders[$user->id]))) {
  325. $fieldset['merge'] = [
  326. 'type' => 'checkbox',
  327. 'label' => __('Merge posts'),
  328. 'value' => '1',
  329. 'checked' => true,
  330. ];
  331. }
  332. if ($fieldset) {
  333. $form['sets'][] = [
  334. 'legend' => __('Options'),
  335. 'fields' => $fieldset,
  336. ];
  337. }
  338. }
  339. $this->nameTpl = 'topic';
  340. $this->onlinePos = 'topic-' . $topic->id;
  341. $this->onlineDetail = true;
  342. $this->canonical = $this->c->Router->link('Topic', ['id' => $topic->id, 'name' => $topic->cens()->subject, 'page' => $topic->page]);
  343. $this->topic = $topic;
  344. $this->posts = $posts;
  345. $this->crumbs = $this->crumbs($topic);
  346. $this->pagination = $topic->pagination;
  347. $this->online = $this->c->Online->calc($this)->info();
  348. $this->stats = null;
  349. $this->form = $form;
  350. if ($topic->showViews) {
  351. $topic->incViews();
  352. }
  353. /*
  354. if (! $user->isGuest) {
  355. $vars = [
  356. ':uid' => $user->id,
  357. ':tid' => $topic->id,
  358. ':read' => $topic->mt_last_read,
  359. ':visit' => $topic->mt_last_visit,
  360. ];
  361. $flag = false;
  362. $lower = max((int) $user->u_mark_all_read, (int) $topic->mf_mark_all_read, (int) $topic->mt_last_read); //????
  363. if ($timeMax > $lower) {
  364. $vars[':read'] = $timeMax;
  365. $flag = true;
  366. }
  367. $upper = max($lower, (int) $topic->mt_last_visit, (int) $user->last_visit); //????
  368. if ($topic->last_post > $upper) {
  369. $vars[':visit'] = $topic->last_post;
  370. $flag = true;
  371. }
  372. if ($flag) {
  373. if (empty($topic->mt_last_read) && empty($topic->mt_last_visit)) {
  374. $this->c->DB->exec('INSERT INTO ::mark_of_topic (uid, tid, mt_last_visit, mt_last_read)
  375. SELECT ?i:uid, ?i:tid, ?i:visit, ?i:read
  376. FROM ::groups
  377. WHERE NOT EXISTS (SELECT 1
  378. FROM ::mark_of_topic
  379. WHERE uid=?i:uid AND tid=?i:tid)
  380. LIMIT 1', $vars);
  381. } else {
  382. $this->c->DB->exec('UPDATE ::mark_of_topic
  383. SET mt_last_visit=?i:visit, mt_last_read=?i:read
  384. WHERE uid=?i:uid AND tid=?i:tid', $vars);
  385. }
  386. }
  387. }
  388. */
  389. return $this;
  390. }
  391. }