Guest User

Untitled

a guest
Nov 20th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.50 KB | None | 0 0
  1. <?php
  2.  
  3. function PromoteTopic()
  4. {
  5. global $topic, $board, $smcFunc, $context, $modSettings, $boarddir, $boardurl, $site_dir, $source_dir, $settings;
  6.  
  7. if (empty($topic))
  8. fatal_lang_error('not_a_topic', false);
  9.  
  10. if (!allowedTo('np_can_promote'))
  11. redirectext('topic=' . $topic . '.' . $_REQUEST['start']);
  12.  
  13. $context['promote_img_dir'] = $site_dir . '/NewsImages/';
  14. $context['promote_img_url'] = $site_url . '/NewsImages/';
  15.  
  16. // First lets check for any previous data
  17. loadPromoted($shortest, $short, $smallPic, $pic, $promoted);
  18.  
  19. // Maybe we are demoting? If so we keep all data and just flag it as demoted
  20. if (!empty($promoted) && isset($_POST['demote']))
  21. {
  22. $smcFunc['db_query']('', '
  23. UPDATE {db_prefix}news
  24. SET promoted = 0
  25. WHERE id_topic = {int:topic}',
  26. array(
  27. 'topic' => $topic,
  28. )
  29. );
  30. logAction('demoted', array('topic' => $topic, 'board' => $board));
  31. $context['demoted'] = true;
  32. loadPromoted($shortest, $short, $smallPic, $pic, $promoted);
  33. }
  34.  
  35. // Validate submitted data
  36. $context['errors'] = array();
  37. if (isset($_POST['submit']) || isset($_POST['preview']))
  38. {
  39. if (empty($_POST['short']))
  40. $context['errors']['short'] = 'You need to fill out the blurb field';
  41. if (empty($_POST['shortest']))
  42. $context['errors']['shortest'] = 'You need to fill out the short blurb field';
  43.  
  44. // Attempt to upload the large pic
  45. if (isset($_FILES['pic']) && !empty($_FILES['pic']['tmp_name']))
  46. {
  47. if (isset($_SESSION['pic']))
  48. {
  49. unlink($context['promote_img_dir'] . $_SESSION['pic']);
  50. unset($_SESSION['pic']);
  51. }
  52. else if (!empty($pic['filename']))
  53. unlink($context['promote_img_dir'] . $pic['filename']);
  54.  
  55. $pic = uploadPic($_FILES['pic']);
  56.  
  57. if (empty($pic['error']))
  58. $_SESSION['pic'] = $pic['filename'];
  59. else
  60. $context['errors']['pic'] = $pic['error'];
  61. }
  62. elseif (isset($_SESSION['pic']))
  63. $pic['filename'] = $_SESSION['pic'];
  64.  
  65. if (empty($pic))
  66. $context['errors']['pic'] = 'You need to upload the larget image!';
  67.  
  68. if (isset($_POST['submit']))
  69. unset($_SESSION['pic']);
  70.  
  71. // Now the smaller pic
  72. if (isset($_FILES['smallpic']) && !empty($_FILES['smallpic']['tmp_name']))
  73. {
  74. if (isset($_SESSION['smallpic']))
  75. {
  76. unlink($context['promote_img_dir'] . $_SESSION['smallpic']);
  77. unset($_SESSION['smallpic']);
  78. }
  79. else if (!empty($smallPic['filename']))
  80. unlink($context['promote_img_dir'] . $smallPic['filename']);
  81.  
  82. $smallPic = uploadPic($_FILES['smallpic']);
  83.  
  84. if (empty($smallPic['error']))
  85. $_SESSION['smallPic'] = $smallPic['filename'];
  86. else
  87. $context['errors']['smallpic'] = $smallPic['error'];
  88. }
  89. elseif (isset($_SESSION['pic']))
  90. $smallPic['filename'] = $_SESSION['pic'];
  91.  
  92. if (empty($smallPic))
  93. $context['errors']['smallpic'] = 'You need to upload the smaller image!';
  94.  
  95. if (isset($_POST['submit']))
  96. unset($_SESSION['smallpic']);
  97. }
  98.  
  99. $context['promote_data'] = array(
  100. 'short' => isset($_POST['short']) ? $_POST['short'] : (!empty($short) ? $short : ''),
  101. 'shortest' => isset($_POST['shortest']) ? $_POST['shortest'] : (!empty($shortest) ? $shortest : ''),
  102. 'pic' => isset($pic['filename']) ? $pic['filename'] : null,
  103. 'smallpic' => isset($smallPic['filename']) ? $smallPic['filename'] : null,
  104. 'promoted' => !empty($promoted),
  105. 'updating' => !empty($shortest),
  106. );
  107.  
  108. // Get the topics subject for the link tree
  109. $request = $smcFunc['db_query']('', '
  110. SELECT m.subject
  111. FROM {db_prefix}messages AS m
  112. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  113. WHERE t.id_topic = {int:topic}',
  114. array(
  115. 'topic' => $topic,
  116. )
  117. );
  118. list ($subject) = $smcFunc['db_fetch_row']($request);
  119. $smcFunc['db_free_result']($request);
  120.  
  121. $context['page_title'] = 'Promoting Topic';
  122. $context['linktree'] = array();
  123. loadTemplate('Promote');
  124. loadTemplate('Main');
  125. $context['sub_template'] = 'promote';
  126. $_GET['news'] = 0;
  127. $noPost = array(
  128. 'id' => null,
  129. 'subject' => null,
  130. );
  131. $context['news_posts'] = $context['latest_posts'] = array($noPost, $noPost, $noPost);
  132. $request = $smcFunc['db_query']('', '
  133. SELECT
  134. m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
  135. t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, m.id_board,
  136. n.shortest, n.short, n.pic
  137. FROM {db_prefix}topics AS t
  138. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  139. LEFT JOIN {db_prefix}news AS n ON (n.id_topic = t.id_topic)
  140. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  141. WHERE t.id_topic = {int:topic}',
  142. array('topic' => $topic)
  143. );
  144. $row = $smcFunc['db_fetch_assoc']($request);
  145. $smcFunc['db_free_result']($request);
  146. $context['news_posts'][0] = array(
  147. 'id' => $topic,
  148. 'short' => '<em>null</em>',
  149. 'shortest' => '<em>null</em>',
  150. 'subject' => $row['subject'],
  151. 'href' => '#',
  152. 'icon' => '<img class="icon" src="' . $settings['images_url'] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />',
  153. 'body' => $row['body'],
  154. 'shortbody' => $row['body'],
  155. 'poster' => array(
  156. 'id' => $row['id_member'],
  157. 'name' => $row['poster_name'],
  158. 'href' => !empty($row['id_member']) ? '#' : '',
  159. 'link' => !empty($row['id_member']) ? '<a href="#">' . $row['poster_name'] . '</a>' : $row['poster_name']
  160. ),
  161. 'link' => '<a href="#">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? 'Comment' : 'Comments') . '</a>',
  162. 'new_comment' => !empty($row['locked']) ? '' : '<a href="#">Write Comment</a>',
  163. 'time' => timeformat($row['poster_time']),
  164. 'timestamp' => forum_time(true, $row['poster_time']),
  165. );
  166.  
  167. if (isset($_POST['submit']) && empty($context['errors']))
  168. {
  169. promote();
  170. $context['promoted'] = true;
  171. }
  172. }
  173.  
  174. function loadPromoted(&$shortest, &$short, &$smallPic, &$pic, &$promoted)
  175. {
  176. global $smcFunc, $topic;
  177.  
  178. // First lets check for any previous data
  179. $request = $smcFunc['db_query']('', '
  180. SELECT shortest, short, pic, promoted
  181. FROM {db_prefix}news
  182. WHERE id_topic = {int:topic}',
  183. array(
  184. 'topic' => $topic,
  185. )
  186. );
  187. list ($shortest, $short, $pic['filename'], $promoted) = $smcFunc['db_fetch_row']($request);
  188. $smcFunc['db_free_result']($request);
  189. }
  190.  
  191. function promote()
  192. {
  193. global $topic, $board, $smcFunc, $context;
  194.  
  195. // for easier use
  196. $data = $context['promote_data'];
  197. $data['topic'] = $topic;
  198.  
  199. if ($data['updating'])
  200. {
  201. $smcFunc['db_query']('', '
  202. UPDATE {db_prefix}news
  203. SET short = {string:short},
  204. shortest = {string:shortest},
  205. pic = {string:pic},
  206. promoted = 1
  207. WHERE id_topic = {int:topic}',
  208. $data
  209. );
  210. }
  211. else
  212. {
  213. $smcFunc['db_insert']('',
  214. '{db_prefix}news',
  215. array('short' => 'string', 'shortest' => 'string', 'pic' => 'string', 'id_topic' => 'int' ),
  216. array($data['short'], $data['shortest'], $data['pic'], $topic),
  217. array('id_topic')
  218. );
  219. }
  220.  
  221. logAction('promoted', array('topic' => $topic, 'board' => $board));
  222. }
  223.  
  224. function uploadPic($file)
  225. {
  226. global $context, $topic;
  227.  
  228. if (!is_dir($context['promote_img_dir']))
  229. {
  230. if (!mkdir($context['promote_img_dir'], 0755))
  231. return array('error' => 'Failed to create directory. Line: ' . _LINE__);
  232. if (!chmod($context['promote_img_dir'], 0755))
  233. return array('error' => 'Failed to chmod directory. Line: ' . _LINE__);
  234. }
  235.  
  236. if (!is_uploaded_file($file['tmp_name']))
  237. return array('error' => 'File uploading failed. Line: ' . __LINE__);
  238.  
  239.  
  240. $extension = substr(strtolower(strrchr($file['name'], '.')), 1);
  241. $filename = 'news_' . $topic . '.' . $extension;
  242.  
  243. if (!in_array($extension, array('jpg', 'jpeg', 'gif', 'png', 'bmp')))
  244. return array('error' => 'The image type you are trying to upload is invalid');
  245.  
  246. $im = getimagesize($file['tmp_name']);
  247.  
  248. if ($im[0] != 346 || $im[1] != 182)
  249. return array('error' => 'The image does not meet the correct size requirements');
  250.  
  251.  
  252.  
  253. switch($im['mime'])
  254. {
  255. case 'image/jpeg':
  256. $img = imagecreatefromjpeg($file['tmp_name']);
  257. if(!imagejpeg($img, $context['promote_img_dir'] . $filename))
  258. return array('error' => 'Error with saving the image. Line: ' . __LINE__);
  259. break;
  260. case 'image/gif':
  261. $img = imagecreatefromgif($file['tmp_name']);
  262. if(!imagegif($img, $context['promote_img_dir'] . $filename))
  263. return array('error' => 'Error with saving the image. Line: ' . __LINE__);
  264. break;
  265. case 'image/png':
  266. $img = imagecreatefrompng($file['tmp_name']);
  267. if(!imagepng($img, $context['promote_img_dir'] . $filename))
  268. return array('error' => 'Error with saving the image. Line: ' . __LINE__);
  269. break;
  270. case 'image/wbmp': case 'image/x-ms-bmp':
  271. $img = imagecreatefromwbmp($file['tmp_name']);
  272. if(!imagewbmp($img, $context['promote_img_dir'] . $filename))
  273. return array('error' => 'Error with saving the image. Line: ' . __LINE__);
  274. break;
  275. default:
  276. return array('error' => 'Error with saving the image. Line:' . __LINE__);
  277. break;
  278. }
  279.  
  280. if (!chmod($context['promote_img_dir'] . $filename, 0777))
  281. return array('error' => 'Error with perms. Line: ' . __LINE__);
  282.  
  283. return array('filename' => $filename);
  284. }
Add Comment
Please, Sign In to add comment