2
0

Printpage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 2012 Simple Machines
  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, $settings;
  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. 'bar' => '<span style="white-space: nowrap;"><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'right' : 'left') . '.png" alt="" /><img src="' . $settings['images_url'] . '/poll_middle.png" width="' . $barWide . '" height="12" alt="-" /><img src="' . $settings['images_url'] . '/poll_' . ($context['right_to_left'] ? 'left' : 'right') . '.png" alt="" /></span>',
  200. // Note: IE < 8 requires us to set a width on the container, too.
  201. 'bar_ndt' => $bar > 0 ? '<div class="bar" style="width: ' . ($bar * 3.5 + 4) . 'px;"><div style="width: ' . $bar * 3.5 . 'px;"></div></div>' : '',
  202. 'bar_width' => $barWide,
  203. 'option' => parse_bbc($option['label']),
  204. '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') . '" />'
  205. );
  206. }
  207. }
  208. // Lets "output" all that info.
  209. loadTemplate('Printpage');
  210. $context['template_layers'] = array('print');
  211. $context['board_name'] = $board_info['name'];
  212. $context['category_name'] = $board_info['cat']['name'];
  213. $context['poster_name'] = $row['poster_name'];
  214. $context['post_time'] = timeformat($row['poster_time'], false);
  215. $context['parent_boards'] = array();
  216. foreach ($board_info['parent_boards'] as $parent)
  217. $context['parent_boards'][] = $parent['name'];
  218. // Split the topics up so we can print them.
  219. $request = $smcFunc['db_query']('', '
  220. SELECT subject, poster_time, body, IFNULL(mem.real_name, poster_name) AS poster_name, id_msg
  221. FROM {db_prefix}messages AS m
  222. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  223. WHERE m.id_topic = {int:current_topic}' . ($modSettings['postmod_active'] && !allowedTo('approve_posts') ? '
  224. AND (m.approved = {int:is_approved}' . ($user_info['is_guest'] ? '' : ' OR m.id_member = {int:current_member}') . ')' : '') . '
  225. ORDER BY m.id_msg',
  226. array(
  227. 'current_topic' => $topic,
  228. 'is_approved' => 1,
  229. 'current_member' => $user_info['id'],
  230. )
  231. );
  232. $context['posts'] = array();
  233. while ($row = $smcFunc['db_fetch_assoc']($request))
  234. {
  235. // Censor the subject and message.
  236. censorText($row['subject']);
  237. censorText($row['body']);
  238. $context['posts'][] = array(
  239. 'subject' => $row['subject'],
  240. 'member' => $row['poster_name'],
  241. 'time' => timeformat($row['poster_time'], false),
  242. 'timestamp' => forum_time(true, $row['poster_time']),
  243. 'body' => parse_bbc($row['body'], 'print'),
  244. 'id_msg' => $row['id_msg'],
  245. );
  246. if (!isset($context['topic_subject']))
  247. $context['topic_subject'] = $row['subject'];
  248. }
  249. $smcFunc['db_free_result']($request);
  250. // Fetch attachments so we can print them if asked, enabled and allowed
  251. if (isset($_REQUEST['images']) && !empty($modSettings['attachmentEnable']) && allowedTo('view_attachments'))
  252. {
  253. $messages = array();
  254. foreach ($context['posts'] as $temp)
  255. $messages[] = $temp['id_msg'];
  256. // build the request
  257. $request = $smcFunc['db_query']('', '
  258. SELECT
  259. a.id_attach, a.id_msg, a.approved, a.width, a.height, a.file_hash, a.filename, a.id_folder, a.mime_type
  260. FROM {db_prefix}attachments AS a
  261. WHERE a.id_msg IN ({array_int:message_list})
  262. AND a.attachment_type = {int:attachment_type}',
  263. array(
  264. 'message_list' => $messages,
  265. 'attachment_type' => 0,
  266. 'is_approved' => 1,
  267. )
  268. );
  269. $temp = array();
  270. while ($row = $smcFunc['db_fetch_assoc']($request))
  271. {
  272. $temp[$row['id_attach']] = $row;
  273. if (!isset($context['printattach'][$row['id_msg']]))
  274. $context['printattach'][$row['id_msg']] = array();
  275. }
  276. $smcFunc['db_free_result']($request);
  277. ksort($temp);
  278. // load them into $context so the template can use them
  279. foreach ($temp as $row)
  280. {
  281. if (!empty($row['width']) && !empty($row['height']))
  282. {
  283. if (!empty($modSettings['max_image_width']) && (empty($modSettings['max_image_height']) || $row['height'] * ($modSettings['max_image_width'] / $row['width']) <= $modSettings['max_image_height']))
  284. {
  285. if ($row['width'] > $modSettings['max_image_width'])
  286. {
  287. $row['height'] = floor($row['height'] * ($modSettings['max_image_width'] / $row['width']));
  288. $row['width'] = $modSettings['max_image_width'];
  289. }
  290. }
  291. elseif (!empty($modSettings['max_image_width']))
  292. {
  293. if ($row['height'] > $modSettings['max_image_height'])
  294. {
  295. $row['width'] = floor($row['width'] * $modSettings['max_image_height'] / $row['height']);
  296. $row['height'] = $modSettings['max_image_height'];
  297. }
  298. }
  299. $row['filename'] = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  300. // save for the template
  301. $context['printattach'][$row['id_msg']][] = $row;
  302. }
  303. }
  304. }
  305. // Set a canonical URL for this page.
  306. $context['canonical_url'] = $scripturl . '?topic=' . $topic . '.0';
  307. }
  308. ?>