Topic.php 16 KB

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