The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.3KB

  1. <?php
  2. require 'info.php';
  3. function recentposts_build($action, $settings, $board) {
  4. // Possible values for $action:
  5. // - all (rebuild everything, initialization)
  6. // - news (news has been updated)
  7. // - boards (board list changed)
  8. // - post (a post has been made)
  9. // - post-thread (a thread has been made)
  10. $b = new RecentPosts();
  11. $b->build($action, $settings);
  12. }
  13. // Wrap functions in a class so they don't interfere with normal Tinyboard operations
  14. class RecentPosts {
  15. public function build($action, $settings) {
  16. global $config, $_theme;
  17. if ($action == 'all') {
  18. copy('templates/themes/recent/' . $settings['basecss'], $config['dir']['home'] . $settings['css']);
  19. }
  20. $this->excluded = explode(' ', $settings['exclude']);
  21. if ($action == 'all' || $action == 'post' || $action == 'post-thread' || $action == 'post-delete') {
  22. $action = generation_strategy('sb_recent', array());
  23. if ($action == 'delete') {
  24. file_unlink($config['dir']['home'] . $settings['html']);
  25. }
  26. elseif ($action == 'rebuild') {
  27. file_write($config['dir']['home'] . $settings['html'], $this->homepage($settings));
  28. }
  29. }
  30. }
  31. // Build news page
  32. public function homepage($settings) {
  33. global $config, $board;
  34. $recent_images = Array();
  35. $recent_posts = Array();
  36. $stats = Array();
  37. $boards = listBoards();
  38. $query = '';
  39. foreach ($boards as &$_board) {
  40. if (in_array($_board['uri'], $this->excluded))
  41. continue;
  42. $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE `files` IS NOT NULL UNION ALL ", $_board['uri'], $_board['uri']);
  43. }
  44. $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_images'], $query);
  45. if ($query == '') {
  46. error(_("Can't build the RecentPosts theme, because there are no boards to be fetched."));
  47. }
  48. $query = query($query) or error(db_error());
  49. while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  50. openBoard($post['board']);
  51. if (isset($post['files']))
  52. $files = json_decode($post['files']);
  53. if ($files[0]->file == 'deleted') continue;
  54. // board settings won't be available in the template file, so generate links now
  55. $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res']
  56. . link_for($post) . '#' . $post['id'];
  57. if ($files) {
  58. if ($files[0]->thumb == 'spoiler') {
  59. $tn_size = @getimagesize($config['spoiler_image']);
  60. $post['src'] = $config['spoiler_image'];
  61. $post['thumbwidth'] = $tn_size[0];
  62. $post['thumbheight'] = $tn_size[1];
  63. }
  64. else {
  65. $post['src'] = $config['uri_thumb'] . $files[0]->thumb;
  66. }
  67. }
  68. $recent_images[] = $post;
  69. }
  70. $query = '';
  71. foreach ($boards as &$_board) {
  72. if (in_array($_board['uri'], $this->excluded))
  73. continue;
  74. $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` UNION ALL ", $_board['uri'], $_board['uri']);
  75. }
  76. $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_posts'], $query);
  77. $query = query($query) or error(db_error());
  78. while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
  79. openBoard($post['board']);
  80. $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post) . '#' . $post['id'];
  81. if ($post['body'] != "")
  82. $post['snippet'] = pm_snippet($post['body'], 30);
  83. else
  84. $post['snippet'] = "<em>" . _("(no comment)") . "</em>";
  85. $post['board_name'] = $board['name'];
  86. $recent_posts[] = $post;
  87. }
  88. // Total posts
  89. $query = 'SELECT SUM(`top`) FROM (';
  90. foreach ($boards as &$_board) {
  91. if (in_array($_board['uri'], $this->excluded))
  92. continue;
  93. $query .= sprintf("SELECT MAX(`id`) AS `top` FROM ``posts_%s`` UNION ALL ", $_board['uri']);
  94. }
  95. $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query);
  96. $query = query($query) or error(db_error());
  97. $stats['total_posts'] = number_format($query->fetchColumn());
  98. // Unique IPs
  99. $query = 'SELECT COUNT(DISTINCT(`ip`)) FROM (';
  100. foreach ($boards as &$_board) {
  101. if (in_array($_board['uri'], $this->excluded))
  102. continue;
  103. $query .= sprintf("SELECT `ip` FROM ``posts_%s`` UNION ALL ", $_board['uri']);
  104. }
  105. $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query);
  106. $query = query($query) or error(db_error());
  107. $stats['unique_posters'] = number_format($query->fetchColumn());
  108. // Active content
  109. $query = 'SELECT DISTINCT(`files`) FROM (';
  110. foreach ($boards as &$_board) {
  111. if (in_array($_board['uri'], $this->excluded))
  112. continue;
  113. $query .= sprintf("SELECT `files` FROM ``posts_%s`` UNION ALL ", $_board['uri']);
  114. }
  115. $query = preg_replace('/UNION ALL $/', ' WHERE `num_files` > 0) AS `posts_all`', $query);
  116. $query = query($query) or error(db_error());
  117. $files = $query->fetchAll();
  118. $stats['active_content'] = 0;
  119. foreach ($files as &$file) {
  120. preg_match_all('/"size":([0-9]*)/', $file[0], $matches);
  121. $stats['active_content'] += array_sum($matches[1]);
  122. }
  123. return Element('themes/recent/recent.html', Array(
  124. 'settings' => $settings,
  125. 'config' => $config,
  126. 'boardlist' => createBoardlist(),
  127. 'recent_images' => $recent_images,
  128. 'recent_posts' => $recent_posts,
  129. 'stats' => $stats
  130. ));
  131. }
  132. };
  133. ?>