Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function PromoteTopic()
- {
- global $topic, $board, $smcFunc, $context, $modSettings, $boarddir, $boardurl, $site_dir, $source_dir, $settings;
- if (empty($topic))
- fatal_lang_error('not_a_topic', false);
- if (!allowedTo('np_can_promote'))
- redirectext('topic=' . $topic . '.' . $_REQUEST['start']);
- $context['promote_img_dir'] = $site_dir . '/NewsImages/';
- $context['promote_img_url'] = $site_url . '/NewsImages/';
- // First lets check for any previous data
- loadPromoted($shortest, $short, $smallPic, $pic, $promoted);
- // Maybe we are demoting? If so we keep all data and just flag it as demoted
- if (!empty($promoted) && isset($_POST['demote']))
- {
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}news
- SET promoted = 0
- WHERE id_topic = {int:topic}',
- array(
- 'topic' => $topic,
- )
- );
- logAction('demoted', array('topic' => $topic, 'board' => $board));
- $context['demoted'] = true;
- loadPromoted($shortest, $short, $smallPic, $pic, $promoted);
- }
- // Validate submitted data
- $context['errors'] = array();
- if (isset($_POST['submit']) || isset($_POST['preview']))
- {
- if (empty($_POST['short']))
- $context['errors']['short'] = 'You need to fill out the blurb field';
- if (empty($_POST['shortest']))
- $context['errors']['shortest'] = 'You need to fill out the short blurb field';
- // Attempt to upload the large pic
- if (isset($_FILES['pic']) && !empty($_FILES['pic']['tmp_name']))
- {
- if (isset($_SESSION['pic']))
- {
- unlink($context['promote_img_dir'] . $_SESSION['pic']);
- unset($_SESSION['pic']);
- }
- else if (!empty($pic['filename']))
- unlink($context['promote_img_dir'] . $pic['filename']);
- $pic = uploadPic($_FILES['pic']);
- if (empty($pic['error']))
- $_SESSION['pic'] = $pic['filename'];
- else
- $context['errors']['pic'] = $pic['error'];
- }
- elseif (isset($_SESSION['pic']))
- $pic['filename'] = $_SESSION['pic'];
- if (empty($pic))
- $context['errors']['pic'] = 'You need to upload the larget image!';
- if (isset($_POST['submit']))
- unset($_SESSION['pic']);
- // Now the smaller pic
- if (isset($_FILES['smallpic']) && !empty($_FILES['smallpic']['tmp_name']))
- {
- if (isset($_SESSION['smallpic']))
- {
- unlink($context['promote_img_dir'] . $_SESSION['smallpic']);
- unset($_SESSION['smallpic']);
- }
- else if (!empty($smallPic['filename']))
- unlink($context['promote_img_dir'] . $smallPic['filename']);
- $smallPic = uploadPic($_FILES['smallpic']);
- if (empty($smallPic['error']))
- $_SESSION['smallPic'] = $smallPic['filename'];
- else
- $context['errors']['smallpic'] = $smallPic['error'];
- }
- elseif (isset($_SESSION['pic']))
- $smallPic['filename'] = $_SESSION['pic'];
- if (empty($smallPic))
- $context['errors']['smallpic'] = 'You need to upload the smaller image!';
- if (isset($_POST['submit']))
- unset($_SESSION['smallpic']);
- }
- $context['promote_data'] = array(
- 'short' => isset($_POST['short']) ? $_POST['short'] : (!empty($short) ? $short : ''),
- 'shortest' => isset($_POST['shortest']) ? $_POST['shortest'] : (!empty($shortest) ? $shortest : ''),
- 'pic' => isset($pic['filename']) ? $pic['filename'] : null,
- 'smallpic' => isset($smallPic['filename']) ? $smallPic['filename'] : null,
- 'promoted' => !empty($promoted),
- 'updating' => !empty($shortest),
- );
- // Get the topics subject for the link tree
- $request = $smcFunc['db_query']('', '
- SELECT m.subject
- FROM {db_prefix}messages AS m
- INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
- WHERE t.id_topic = {int:topic}',
- array(
- 'topic' => $topic,
- )
- );
- list ($subject) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $context['page_title'] = 'Promoting Topic';
- $context['linktree'] = array();
- loadTemplate('Promote');
- loadTemplate('Main');
- $context['sub_template'] = 'promote';
- $_GET['news'] = 0;
- $noPost = array(
- 'id' => null,
- 'subject' => null,
- );
- $context['news_posts'] = $context['latest_posts'] = array($noPost, $noPost, $noPost);
- $request = $smcFunc['db_query']('', '
- SELECT
- m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
- t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, m.id_board,
- n.shortest, n.short, n.pic
- FROM {db_prefix}topics AS t
- INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
- LEFT JOIN {db_prefix}news AS n ON (n.id_topic = t.id_topic)
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
- WHERE t.id_topic = {int:topic}',
- array('topic' => $topic)
- );
- $row = $smcFunc['db_fetch_assoc']($request);
- $smcFunc['db_free_result']($request);
- $context['news_posts'][0] = array(
- 'id' => $topic,
- 'short' => '<em>null</em>',
- 'shortest' => '<em>null</em>',
- 'subject' => $row['subject'],
- 'href' => '#',
- 'icon' => '<img class="icon" src="' . $settings['images_url'] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />',
- 'body' => $row['body'],
- 'shortbody' => $row['body'],
- 'poster' => array(
- 'id' => $row['id_member'],
- 'name' => $row['poster_name'],
- 'href' => !empty($row['id_member']) ? '#' : '',
- 'link' => !empty($row['id_member']) ? '<a href="#">' . $row['poster_name'] . '</a>' : $row['poster_name']
- ),
- 'link' => '<a href="#">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? 'Comment' : 'Comments') . '</a>',
- 'new_comment' => !empty($row['locked']) ? '' : '<a href="#">Write Comment</a>',
- 'time' => timeformat($row['poster_time']),
- 'timestamp' => forum_time(true, $row['poster_time']),
- );
- if (isset($_POST['submit']) && empty($context['errors']))
- {
- promote();
- $context['promoted'] = true;
- }
- }
- function loadPromoted(&$shortest, &$short, &$smallPic, &$pic, &$promoted)
- {
- global $smcFunc, $topic;
- // First lets check for any previous data
- $request = $smcFunc['db_query']('', '
- SELECT shortest, short, pic, promoted
- FROM {db_prefix}news
- WHERE id_topic = {int:topic}',
- array(
- 'topic' => $topic,
- )
- );
- list ($shortest, $short, $pic['filename'], $promoted) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- }
- function promote()
- {
- global $topic, $board, $smcFunc, $context;
- // for easier use
- $data = $context['promote_data'];
- $data['topic'] = $topic;
- if ($data['updating'])
- {
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}news
- SET short = {string:short},
- shortest = {string:shortest},
- pic = {string:pic},
- promoted = 1
- WHERE id_topic = {int:topic}',
- $data
- );
- }
- else
- {
- $smcFunc['db_insert']('',
- '{db_prefix}news',
- array('short' => 'string', 'shortest' => 'string', 'pic' => 'string', 'id_topic' => 'int' ),
- array($data['short'], $data['shortest'], $data['pic'], $topic),
- array('id_topic')
- );
- }
- logAction('promoted', array('topic' => $topic, 'board' => $board));
- }
- function uploadPic($file)
- {
- global $context, $topic;
- if (!is_dir($context['promote_img_dir']))
- {
- if (!mkdir($context['promote_img_dir'], 0755))
- return array('error' => 'Failed to create directory. Line: ' . _LINE__);
- if (!chmod($context['promote_img_dir'], 0755))
- return array('error' => 'Failed to chmod directory. Line: ' . _LINE__);
- }
- if (!is_uploaded_file($file['tmp_name']))
- return array('error' => 'File uploading failed. Line: ' . __LINE__);
- $extension = substr(strtolower(strrchr($file['name'], '.')), 1);
- $filename = 'news_' . $topic . '.' . $extension;
- if (!in_array($extension, array('jpg', 'jpeg', 'gif', 'png', 'bmp')))
- return array('error' => 'The image type you are trying to upload is invalid');
- $im = getimagesize($file['tmp_name']);
- if ($im[0] != 346 || $im[1] != 182)
- return array('error' => 'The image does not meet the correct size requirements');
- switch($im['mime'])
- {
- case 'image/jpeg':
- $img = imagecreatefromjpeg($file['tmp_name']);
- if(!imagejpeg($img, $context['promote_img_dir'] . $filename))
- return array('error' => 'Error with saving the image. Line: ' . __LINE__);
- break;
- case 'image/gif':
- $img = imagecreatefromgif($file['tmp_name']);
- if(!imagegif($img, $context['promote_img_dir'] . $filename))
- return array('error' => 'Error with saving the image. Line: ' . __LINE__);
- break;
- case 'image/png':
- $img = imagecreatefrompng($file['tmp_name']);
- if(!imagepng($img, $context['promote_img_dir'] . $filename))
- return array('error' => 'Error with saving the image. Line: ' . __LINE__);
- break;
- case 'image/wbmp': case 'image/x-ms-bmp':
- $img = imagecreatefromwbmp($file['tmp_name']);
- if(!imagewbmp($img, $context['promote_img_dir'] . $filename))
- return array('error' => 'Error with saving the image. Line: ' . __LINE__);
- break;
- default:
- return array('error' => 'Error with saving the image. Line:' . __LINE__);
- break;
- }
- if (!chmod($context['promote_img_dir'] . $filename, 0777))
- return array('error' => 'Error with perms. Line: ' . __LINE__);
- return array('filename' => $filename);
- }
Add Comment
Please, Sign In to add comment