<?php
/**
* Downloads Section Plugin for MyBB
* Copyright © 2006 MyBB Mods
*
* By: Musicalmidget
* Website: http://mods.mybboard.com/
* Version: 2.0.1
*/
define('IN_MYBB', 1);
$templatelist = 'downloads,downloads_catlist,downloads_newdownload,downloads_no_categories,downloads_no_downloads,downloads_submit,downloads_view_category,downloads_view_category_download,downloads_view_download,downloads_view_download_options';
require_once './global.php';
require_once MYBB_ROOT.'inc/functions_post.php';
require_once MYBB_ROOT.'inc/class_parser.php';
$parser = new postParser;
$parser_options = array(
'allow_html' => 'no',
'allow_mycode' => 'yes',
'allow_smilies' => 'yes',
'allow_imgcode' => 'yes'
);
// Load language phrases
$lang->load('downloads');
add_breadcrumb($lang->download_section, 'downloads.php');
if($mybb->input['action'] == 'do_submit')
{
if(!trim($mybb->input['name']))
{
error($lang->error_no_name);
}
if(!trim($mybb->input['description']))
{
error($lang->error_no_desc);
}
if(!$_FILES['attachment']['size'])
{
error($lang->error_no_file);
}
if($_FILES['attachment']['size'] > 0)
{
$attachedfile = downloads_upload_attachment($_FILES['attachment']);
}
if($attachedfile['error'])
{
error($attachedfile['error']);
}
$cid = intval($mybb->input['cid']);
$uid = intval($mybb->user['uid']);
if($mybb->user['usergroup'] == 3 || $mybb->user['usergroup'] == 4)
{
$validated = 'yes';
}
else
{
$validated = 'no';
}
$new_download = array(
'cid' => $cid,
'uid' => $uid,
'name' => $db->escape_string($mybb->input['name']),
'description' => $db->escape_string($mybb->input['description']),
'filename' => $db->escape_string($attachedfile['filename']),
'validated' => $validated
);
$query = $db->insert_query(TABLE_PREFIX.'downloads', $new_download);
if($validated == 'yes')
{
$db->query("UPDATE ".TABLE_PREFIX."downloadcategories SET downloads=downloads+1 WHERE cid='$cid'");
redirect('downloads.php?action=view&cid='.$cid, $lang->redirect_added);
}
else
{
redirect('downloads.php?action=view&cid='.$cid, $lang->redirect_added_waiting);
}
}
elseif($mybb->input['action'] == 'submit')
{
if(intval($mybb->input['cid']))
{
$cid = intval($mybb->input['cid']);
$query = $db->simple_select(TABLE_PREFIX.'downloadcategories', '*', "cid='$cid'");
if($category = $db->fetch_array($query))
{
$visible_groups = explode(',', $category['visiblegroups']);
$submission_groups = explode(',', $category['submissiongroups']);
if(!in_array($mybb->user['usergroup'], $visible_groups) && !in_array('all', $visible_groups))
{
error($lang->error_invalid_category);
}
if(!in_array($mybb->user['usergroup'], $submission_groups) && !in_array('all', $submission_groups))
{
error($lang->error_invalid_category);
}
add_breadcrumb($category['name'], 'downloads.php?action=view&cid='.$category['cid']);
add_breadcrumb($lang->submit_download);
mt_srand((double) microtime() * 1000000);
$posthash = md5($mybb->user['uid'].mt_rand());
eval("\$submit_download = \"".$templates->get('downloads_submit')."\";");
output_page($submit_download);
}
else
{
error($lang->error_invalid_category);
}
}
else
{
error($lang->error_invalid_category);
}
}
elseif($mybb->input['action'] == 'download')
{
if(intval($mybb->input['did']))
{
$did = intval($mybb->input['did']);
$query = $db->simple_select(TABLE_PREFIX.'downloads', '*', "did='$did'");
if($download = $db->fetch_array($query))
{
$cid = $download['cid'];
$query = $db->simple_select(TABLE_PREFIX.'downloadcategories', '*', "cid='$cid'");
if($category = $db->fetch_array($query))
{
$visible_groups = explode(',', $category['visiblegroups']);
if(in_array($mybb->user['usergroup'], $visible_groups) || in_array('all', $visible_groups))
{
if($mybb->user['usergroup'] == 1)
{
error_no_permission();
}
if($download['validated'] != 'yes')
{
if($mybb->user['usergroup'] != 3 && $mybb->user['usergroup'] != 4)
{
error($lang->error_invalid_download);
}
}
$db->query("UPDATE ".TABLE_PREFIX."downloads SET downloads=downloads+1 WHERE did='$did'");
header('Location: '.$mybb->settings['uploadspath'].'/downloads/'.$download['filename']);
}
else
{
error($lang->error_invalid_download);
}
}
else
{
error($lang->error_invalid_category);
}
}
else
{
error($lang->error_invalid_download);
}
}
else
{
error($lang->error_invalid_action);
}
}
elseif($mybb->input['action'] == 'view')
{
if(intval($mybb->input['did']))
{
$did = intval($mybb->input['did']);
$query = $db->simple_select(TABLE_PREFIX.'downloads', '*', "did='$did'");
if($download = $db->fetch_array($query))
{
$cid = $download['cid'];
$query = $db->simple_select(TABLE_PREFIX.'downloadcategories', '*', "cid='$cid'");
if($category = $db->fetch_array($query))
{
$visible_groups = explode(',', $category['visiblegroups']);
if(in_array($mybb->user['usergroup'], $visible_groups) || in_array('all', $visible_groups))
{
if($download['validated'] != 'yes')
{
if($mybb->user['usergroup'] == 4)
{
$bgcolor = 'trow_shaded';
}
else
{
error($lang->error_invalid_download);
}
}
else
{
$bgcolor = alt_trow();
}
$db->query("UPDATE ".TABLE_PREFIX."downloads SET views=views+1 WHERE did='$did'");
add_breadcrumb($category['name'], 'downloads.php?action=view&cid='.$category['cid']);
add_breadcrumb($download['name']);
$mod_options = '';
if($mybb->user['usergroup'] == 4)
{
eval("\$mod_options = \"".$templates->get('downloads_view_download_options')."\";");
}
$download['description'] = $parser->parse_message($download['description']);
$download['size'] = get_friendly_size(filesize(MYBB_ROOT.$mybb->settings['uploadspath'].'/downloads/'.basename($download['filename'])));
eval("\$download = \"".$templates->get('downloads_view_download')."\";");
output_page($download);
}
else
{
error($lang->error_invalid_category);
}
}
else
{
error($lang->error_invalid_category);
}
}
else
{
error($lang->error_invalid_download);
}
}
elseif(intval($mybb->input['cid']))
{
$cid = intval($mybb->input['cid']);
$query = $db->simple_select(TABLE_PREFIX.'downloadcategories', '*', "cid='$cid'");
if($category = $db->fetch_array($query))
{
$visible_groups = explode(',', $category['visiblegroups']);
if(in_array($mybb->user['usergroup'], $visible_groups) || in_array('all', $visible_groups))
{
$category['name'] = stripslashes($category['name']);
$category['description'] = stripslashes($category['description']);
add_breadcrumb($category['name'], 'downloads.php?action=view&cid='.$category['cid']);
$new_download = '';
$multipage = '';
$submission_groups = explode(',', $category['submissiongroups']);
if(in_array($mybb->user['usergroup'], $submission_groups) || in_array('all', $submission_groups))
{
eval("\$new_download = \"".$templates->get('downloads_newdownload')."\";");
}
$where_sql = '';
$where_sql_2 = '';
if($mybb->user['usergroup'] != 4)
{
$where_sql = "AND validated='yes'";
$where_sql_2 = "AND d.validated='yes'";
}
$query = $db->simple_select(TABLE_PREFIX.'downloads', 'COUNT(*) AS downloads', "cid='$cid' $where_sql");
$count = $db->fetch_field($query, 'downloads');
if($count > 0)
{
$perpage = $mybb->settings['threadsperpage'];
if(intval($mybb->input['page']) > 0)
{
$page = intval($mybb->input['page']);
$start = ($page - 1) * $perpage;
$pages = $count / $perpage;
$pages = ceil($pages);
if($page > $pages)
{
$start = 0;
$page = 1;
}
}
else
{
$start = 0;
$page = 1;
}
$end = $start + $perpage;
$lower = $start + 1;
$upper = $end;
if($upper > $count)
{
$upper = $count;
}
$multipage = multipage($count, $perpage, $page, 'downloads.php?action=view&cid='.$cid);
$query = $db->query("
SELECT d.*, u.username
FROM ".TABLE_PREFIX."downloads d
LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=d.uid)
WHERE d.cid='$cid' $where_sql_2
ORDER BY d.name ASC
LIMIT $start, $perpage
");
while($download = $db->fetch_array($query))
{
if($download['validated'] != 'yes')
{
$bgcolor = 'trow_shaded';
}
else
{
$bgcolor = alt_trow();
}
$download['name'] = $parser->parse_badwords($download['name']);
$download['name'] = htmlspecialchars_uni($download['name']);
$download['profile_link'] = build_profile_link($download['username'], $download['uid']);
$download['views'] = intval($download['views']);
$download['downloads'] = intval($download['downloads']);
eval("\$downloads_list .= \"".$templates->get('downloads_view_category_download')."\";");
}
}
else
{
$bgcolor = alt_trow();
eval("\$downloads_list = \"".$templates->get('downloads_no_downloads')."\";");
}
$bgcolor = alt_trow();
eval("\$category = \"".$templates->get('downloads_view_category')."\";");
output_page($category);
}
else
{
error($lang->error_invalid_category);
}
}
else
{
error($lang->error_invalid_category);
}
}
else
{
error($lang->error_invalid_action);
}
}
else
{
$cat_list = '';
$groups = '';
$query = $db->query("SELECT * FROM `".TABLE_PREFIX."downloadcategories` ORDER BY disporder ASC");
$count = $db->num_rows($query);
if(!$count)
{
eval("\$cat_list = \"".$templates->get('downloads_no_categories')."\";");
}
else
{
while($category = $db->fetch_array($query))
{
$groups = explode(',', $category['visiblegroups']);
foreach($groups as $gid)
{
if($mybb->user['usergroup'] == $gid || $gid == 'all')
{
if($bgcolor == 'trow1')
{
$bgcolor = 'trow2';
}
else
{
$bgcolor = 'trow1';
}
$category['name'] = stripslashes($category['name']);
$category['description'] = stripslashes($category['description']);
eval("\$cat_list .= \"".$templates->get('downloads_catlist')."\";");
}
}
}
}
eval("\$downloads_page = \"".$templates->get('downloads')."\";");
output_page($downloads_page);
}
/**
* Unfortunately, it was not possible to use the standard MyBB upload_attachment
* function for this plugin, hence a similar replacement function has been written
* (below) to use instead. Subsequently, some portions of the below function are
* taken from the MyBB function upload_attachment in the inc/functions_upload.php
* MyBB file.
*/
function downloads_upload_attachment($attachment)
{
global $db, $mybb, $lang;
if(isset($attachment['error']) && $attachment['error'] != 0)
{
$ret['error'] = $lang->error_uploadfailed.$lang->error_uploadfailed_detail;
switch($attachment['error'])
{
case 1: // UPLOAD_ERR_INI_SIZE
$ret['error'] .= $lang->error_uploadfailed_php1;
break;
case 2: // UPLOAD_ERR_FORM_SIZE
$ret['error'] .= $lang->error_uploadfailed_php2;
break;
case 3: // UPLOAD_ERR_PARTIAL
$ret['error'] .= $lang->error_uploadfailed_php3;
break;
case 4: // UPLOAD_ERR_NO_FILE
$ret['error'] .= $lang->error_uploadfailed_php4;
break;
case 6: // UPLOAD_ERR_NO_TMP_DIR
$ret['error'] .= $lang->error_uploadfailed_php6;
break;
case 7: // UPLOAD_ERR_CANT_WRITE
$ret['error'] .= $lang->error_uploadfailed_php7;
break;
default:
$ret['error'] .= sprintf($lang->error_uploadfailed_phpx, $attachment['error']);
break;
}
return $ret;
}
if(!is_uploaded_file($attachment['tmp_name']) || empty($attachment['tmp_name']))
{
$ret['error'] = $lang->error_uploadfailed.$lang->error_uploadfailed_php4;
return $ret;
}
$ext = get_extension($attachment['name']);
// Check if we have a valid extension
$query = $db->simple_select(TABLE_PREFIX."attachtypes", "*", "extension='$ext'");
$attachtype = $db->fetch_array($query);
if(!$attachtype['atid'])
{
$ret['error'] = $lang->error_attachtype;
return $ret;
}
// Check the size
if($attachment['size'] > $attachtype['maxsize']*1024 && $attachtype['maxsize'] != "")
{
$ret['error'] = sprintf($lang->error_attachsize, $attachtype['maxsize']);
return $ret;
}
// All seems to be good, lets move the attachment!
require_once MYBB_ROOT.'inc/functions_upload.php';
$filename = "download_".$mybb->user['uid']."_".time().".".$ext;
$file = upload_file($attachment, $mybb->settings['uploadspath'].'/downloads/', $filename);
if($file['error'])
{
$ret['error'] = $lang->error_uploadfailed.$lang->error_uploadfailed_detail;
switch($file['error'])
{
case 1:
$ret['error'] .= $lang->error_uploadfailed_nothingtomove;
break;
case 2:
$ret['error'] .= $lang->error_uploadfailed_movefailed;
break;
}
return $ret;
}
// Lets just double check that it exists
if(!file_exists($mybb->settings['uploadspath']."/downloads/".$filename))
{
$ret['error'] = $lang->error_uploadfailed.$lang->error_uploadfailed_detail.$lang->error_uploadfailed_lost;
return $ret;
}
return array('filename' => $filename);
}
?>