Printpage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * This file contains just one function that formats a topic to be printer
  4. * friendly.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2014 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. /**
  18. * Format a topic to be printer friendly.
  19. * Must be called with a topic specified.
  20. * Accessed via ?action=printpage.
  21. *
  22. * @uses Printpage template, main sub-template.
  23. * @uses print_above/print_below later without the main layer.
  24. */
  25. function PrintTopic()
  26. {
  27. global $topic, $txt, $scripturl, $context, $user_info;
  28. global $board_info, $smcFunc, $modSettings;
  29. // Redirect to the boardindex if no valid topic id is provided.
  30. if (empty($topic))
  31. redirectexit();
  32. if (!empty($modSettings['disable_print_topic']))
  33. {
  34. unset($_REQUEST['action']);
  35. $context['theme_loaded'] = false;
  36. fatal_lang_error('feature_disabled', false);
  37. }
  38. // Whatever happens don't index this.
  39. $context['robot_no_index'] = true;
  40. // Get the topic starter information.
  41. $request = $smcFunc['db_query']('', '
  42. SELECT mem.id_member, m.poster_time, IFNULL(mem.real_name, m.poster_name) AS poster_name, t.id_poll
  43. FROM {db_prefix}messages AS m
  44. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  45. LEFT JOIN {db_prefix}topics as t ON (t.id_first_msg = m.id_msg)
  46. WHERE m.id_topic = {int:current_topic}
  47. ORDER BY m.id_msg
  48. LIMIT 1',
  49. array(
  50. 'current_topic' => $topic,
  51. )
  52. );
  53. // Redirect to the boardindex if no valid topic id is provided.
  54. if ($smcFunc['db_num_rows']($request) == 0)
  55. redirectexit();
  56. $row = $smcFunc['db_fetch_assoc']($request);
  57. $smcFunc['db_free_result']($request);
  58. if (!empty($row['id_poll']))
  59. {
  60. loadLanguage('Post');
  61. // Get the question and if it's locked.
  62. $request = $smcFunc['db_query']('', '
  63. SELECT
  64. p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.change_vote,
  65. p.guest_vote, p.id_member, IFNULL(mem.real_name, p.poster_name) AS poster_name, p.num_guest_voters, p.reset_poll
  66. FROM {db_prefix}polls AS p
  67. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = p.id_member)
  68. WHERE p.id_poll = {int:id_poll}
  69. LIMIT 1',
  70. array(
  71. 'id_poll' => $row['id_poll'],
  72. )
  73. );
  74. $pollinfo = $smcFunc['db_fetch_assoc']($request);
  75. $smcFunc['db_free_result']($request);
  76. $request = $smcFunc['db_query']('', '
  77. SELECT COUNT(DISTINCT id_member) AS total
  78. FROM {db_prefix}log_polls
  79. WHERE id_poll = {int:id_poll}
  80. AND id_member != {int:not_guest}',
  81. array(
  82. 'id_poll' => $row['id_poll'],
  83. 'not_guest' => 0,
  84. )
  85. );
  86. list ($pollinfo['total']) = $smcFunc['db_fetch_row']($request);
  87. $smcFunc['db_free_result']($request);
  88. // Total voters needs to include guest voters
  89. $pollinfo['total'] += $pollinfo['num_guest_voters'];
  90. // Get all the options, and calculate the total votes.
  91. $request = $smcFunc['db_query']('', '
  92. SELECT pc.id_choice, pc.label, pc.votes, IFNULL(lp.id_choice, -1) AS voted_this
  93. FROM {db_prefix}poll_choices AS pc
  94. LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_choice = pc.id_choice AND lp.id_poll = {int:id_poll} AND lp.id_member = {int:current_member} AND lp.id_member != {int:not_guest})
  95. WHERE pc.id_poll = {int:id_poll}',
  96. array(
  97. 'current_member' => $user_info['id'],
  98. 'id_poll' => $row['id_poll'],
  99. 'not_guest' => 0,
  100. )
  101. );
  102. $pollOptions = array();
  103. $realtotal = 0;
  104. $pollinfo['has_voted'] = false;
  105. while ($row = $smcFunc['db_fetch_assoc']($request))
  106. {
  107. censorText($row['label']);
  108. $pollOptions[$row['id_choice']] = $row;
  109. $realtotal += $row['votes'];
  110. $pollinfo['has_voted'] |= $row['voted_this'] != -1;
  111. }
  112. $smcFunc['db_free_result']($request);
  113. // If this is a guest we need to do our best to work out if they have voted, and what they voted for.
  114. if ($user_info['is_guest'] && $pollinfo['guest_vote'] && allowedTo('poll_vote'))
  115. {
  116. if (!empty($_COOKIE['guest_poll_vote']) && preg_match('~^[0-9,;]+$~', $_COOKIE['guest_poll_vote']) && strpos($_COOKIE['guest_poll_vote'], ';' . $row['id_poll'] . ',') !== false)
  117. {
  118. // ;id,timestamp,[vote,vote...]; etc
  119. $guestinfo = explode(';', $_COOKIE['guest_poll_vote']);
  120. // Find the poll we're after.
  121. foreach ($guestinfo as $i => $guestvoted)
  122. {
  123. $guestvoted = explode(',', $guestvoted);
  124. if ($guestvoted[0] == $row['id_poll'])
  125. break;
  126. }
  127. // Has the poll been reset since guest voted?
  128. if ($pollinfo['reset_poll'] > $guestvoted[1])
  129. {
  130. // Remove the poll info from the cookie to allow guest to vote again
  131. unset($guestinfo[$i]);
  132. if (!empty($guestinfo))
  133. $_COOKIE['guest_poll_vote'] = ';' . implode(';', $guestinfo);
  134. else
  135. unset($_COOKIE['guest_poll_vote']);
  136. }
  137. else
  138. {
  139. // What did they vote for?
  140. unset($guestvoted[0], $guestvoted[1]);
  141. foreach ($pollOptions as $choice => $details)
  142. {
  143. $pollOptions[$choice]['voted_this'] = in_array($choice, $guestvoted) ? 1 : -1;
  144. $pollinfo['has_voted'] |= $pollOptions[$choice]['voted_this'] != -1;
  145. }
  146. unset($choice, $details, $guestvoted);
  147. }
  148. unset($guestinfo, $guestvoted, $i);
  149. }
  150. }
  151. $context['user']['started'] = $user_info['id'] == $row['id_member'] && !$user_info['is_guest'];
  152. // Set up the basic poll information.
  153. $context['poll'] = array(
  154. 'id' => $row['id_poll'],
  155. 'image' => 'normal_' . (empty($pollinfo['voting_locked']) ? 'poll' : 'locked_poll'),
  156. 'question' => parse_bbc($pollinfo['question']),
  157. 'total_votes' => $pollinfo['total'],
  158. 'change_vote' => !empty($pollinfo['change_vote']),
  159. 'is_locked' => !empty($pollinfo['voting_locked']),
  160. 'options' => array(),
  161. 'lock' => allowedTo('poll_lock_any') || ($context['user']['started'] && allowedTo('poll_lock_own')),
  162. 'edit' => allowedTo('poll_edit_any') || ($context['user']['started'] && allowedTo('poll_edit_own')),
  163. 'allowed_warning' => $pollinfo['max_votes'] > 1 ? sprintf($txt['poll_options6'], min(count($pollOptions), $pollinfo['max_votes'])) : '',
  164. 'is_expired' => !empty($pollinfo['expire_time']) && $pollinfo['expire_time'] < time(),
  165. 'expire_time' => !empty($pollinfo['expire_time']) ? timeformat($pollinfo['expire_time']) : 0,
  166. 'has_voted' => !empty($pollinfo['has_voted']),
  167. 'starter' => array(
  168. 'id' => $pollinfo['id_member'],
  169. 'name' => $row['poster_name'],
  170. 'href' => $pollinfo['id_member'] == 0 ? '' : $scripturl . '?action=profile;u=' . $pollinfo['id_member'],
  171. 'link' => $pollinfo['id_member'] == 0 ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $pollinfo['id_member'] . '">' . $row['poster_name'] . '</a>'
  172. )
  173. );
  174. // Make the lock and edit permissions defined above more directly accessible.
  175. $context['allow_lock_poll'] = $context['poll']['lock'];
  176. $context['allow_edit_poll'] = $context['poll']['edit'];
  177. // You're allowed to view the results if:
  178. // 1. you're just a super-nice-guy, or
  179. // 2. anyone can see them (hide_results == 0), or
  180. // 3. you can see them after you voted (hide_results == 1), or
  181. // 4. you've waited long enough for the poll to expire. (whether hide_results is 1 or 2.)
  182. $context['allow_poll_view'] = allowedTo('moderate_board') || $pollinfo['hide_results'] == 0 || ($pollinfo['hide_results'] == 1 && $context['poll']['has_voted']) || $context['poll']['is_expired'];
  183. // Calculate the percentages and bar lengths...
  184. $divisor = $realtotal == 0 ? 1 : $realtotal;
  185. // Determine if a decimal point is needed in order for the options to add to 100%.
  186. $precision = $realtotal == 100 ? 0 : 1;
  187. // Now look through each option, and...
  188. foreach ($pollOptions as $i => $option)
  189. {
  190. // First calculate the percentage, and then the width of the bar...
  191. $bar = round(($option['votes'] * 100) / $divisor, $precision);
  192. $barWide = $bar == 0 ? 1 : floor(($bar * 8) / 3);
  193. // Now add it to the poll's contextual theme data.
  194. $context['poll']['options'][$i] = array(
  195. 'id' => 'options-' . $i,
  196. 'percent' => $bar,
  197. 'votes' => $option['votes'],
  198. 'voted_this' => $option['voted_this'] != -1,
  199. // Note: IE < 8 requires us to set a width on the container, too.
  200. 'bar_ndt' => $bar > 0 ? '<div class="bar" style="width: ' . ($bar * 3.5 + 4) . 'px;"><div style="width: ' . $bar * 3.5 . 'px;"></div></div>' : '',
  201. 'bar_width' => $barWide,
  202. 'option' => parse_bbc($option['label']),
  203. 'vote_button' => '<input type="' . ($pollinfo['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '" class="input_' . ($pollinfo['max_votes'] > 1 ? 'check' : 'radio') . '">'
  204. );
  205. }
  206. }
  207. // Lets "output" all that info.
  208. loadTemplate('Printpage');
  209. $context['template_layers'] = array('print');
  210. $context['board_name'] = $board_info['name'];
  211. $context['category_name'] = $board_info['cat']['name'];
  212. $context['poster_name'] = $row['poster_name'];
  213. $context['post_time'] = timeformat($row['poster_time'], false);
  214. $context['parent_boards'] = array();
  215. foreach ($board_info['parent_boards'] as $parent)
  216. $context['parent_boards'][] = $parent['name'];
  217. // Split the topics up so we can print them.
  218. $request = $smcFunc['db_query']('', '
  219. SELECT subject, poster_time, body, IFNULL(mem.real_name, poster_name) AS poster_name, id_msg
  220. FROM {db_prefix}messages AS m
  221. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  222. WHERE m.id_topic = {int:current_topic}' . ($modSettings['postmod_active'] && !allowedTo('approve_posts') ? '
  223. AND (m.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR m.id_member = {int:current_member}') . ')' : '') . '
  224. ORDER BY m.id_msg',
  225. array(
  226. 'current_topic' => $topic,
  227. 'is_approved' => 1,
  228. 'current_member' => $user_info['id'],
  229. )
  230. );
  231. $context['posts'] = array();
  232. while ($row = $smcFunc['db_fetch_assoc']($request))
  233. {
  234. // Censor the subject and message.
  235. censorText($row['subject']);
  236. censorText($row['body']);
  237. $context['posts'][] = array(
  238. 'subject' => $row['subject'],
  239. 'member' => $row['poster_name'],
  240. 'time' => timeformat($row['poster_time'], false),
  241. 'timestamp' => forum_time(true, $row['poster_time']),
  242. 'body' => parse_bbc($row['body'], 'print'),
  243. 'id_msg' => $row['id_msg'],
  244. );
  245. if (!isset($context['topic_subject']))
  246. $context['topic_subject'] = $row['subject'];
  247. }
  248. $smcFunc['db_free_result']($request);
  249. // Fetch attachments so we can print them if asked, enabled and allowed
  250. if (isset($_REQUEST['images']) && !empty($modSettings['attachmentEnable']) && allowedTo('view_attachments'))
  251. {
  252. $messages = array();
  253. foreach ($context['posts'] as $temp)
  254. $messages[] = $temp['id_msg'];
  255. // build the request
  256. $request = $smcFunc['db_query']('', '
  257. SELECT
  258. a.id_attach, a.id_msg, a.approved, a.width, a.height, a.file_hash, a.filename, a.id_folder, a.mime_type
  259. FROM {db_prefix}attachments AS a
  260. WHERE a.id_msg IN ({array_int:message_list})
  261. AND a.attachment_type = {int:attachment_type}',
  262. array(
  263. 'message_list' => $messages,
  264. 'attachment_type' => 0,
  265. 'is_approved' => 1,
  266. )
  267. );
  268. $temp = array();
  269. while ($row = $smcFunc['db_fetch_assoc']($request))
  270. {
  271. $temp[$row['id_attach']] = $row;
  272. if (!isset($context['printattach'][$row['id_msg']]))
  273. $context['printattach'][$row['id_msg']] = array();
  274. }
  275. $smcFunc['db_free_result']($request);
  276. ksort($temp);
  277. // load them into $context so the template can use them
  278. foreach ($temp as $row)
  279. {
  280. if (!empty($row['width']) && !empty($row['height']))
  281. {
  282. if (!empty($modSettings['max_image_width']) && (empty($modSettings['max_image_height']) || $row['height'] * ($modSettings['max_image_width'] / $row['width']) <= $modSettings['max_image_height']))
  283. {
  284. if ($row['width'] > $modSettings['max_image_width'])
  285. {
  286. $row['height'] = floor($row['height'] * ($modSettings['max_image_width'] / $row['width']));
  287. $row['width'] = $modSettings['max_image_width'];
  288. }
  289. }
  290. elseif (!empty($modSettings['max_image_width']))
  291. {
  292. if ($row['height'] > $modSettings['max_image_height'])
  293. {
  294. $row['width'] = floor($row['width'] * $modSettings['max_image_height'] / $row['height']);
  295. $row['height'] = $modSettings['max_image_height'];
  296. }
  297. }
  298. $row['filename'] = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  299. // save for the template
  300. $context['printattach'][$row['id_msg']][] = $row;
  301. }
  302. }
  303. }
  304. // Set a canonical URL for this page.
  305. $context['canonical_url'] = $scripturl . '?topic=' . $topic . '.0';
  306. }
  307. ?>