<?php
/**
 * MyBB 1.6
 * Copyright 2010 MyBB Group, All Rights Reserved
 *
 * Website: http://mybb.com
 * License: http://mybb.com/about/license
 *
 * $Id: oldstuff.php 5297 2012-02-26 22:01:14Z Fish $
 */

define("IN_MYBB", 1);
//define("IN_oldstuff", 1);
define('THIS_SCRIPT', 'oldstuff.php');
// end editing

$change_dir = "./";

if(!@chdir($forumdir) && !empty($forumdir))
{
	if(@is_dir($forumdir))
	{
		$change_dir = $forumdir;
	}
	else
	{
		die("\$forumdir is invalid!");
	}
}

$templatelist = "oldstuff,oldstuff_attachments_attachment,oldstuff_attachments_none,oldstuff_attachments,multipage_prevpage,multipage_nextpage,multipage_page_current,multipage_page,multipage_start,multipage_end";

require_once $change_dir."/global.php";

require_once MYBB_ROOT."inc/functions_post.php";

require_once MYBB_ROOT."inc/functions_user.php";

require_once MYBB_ROOT."inc/class_parser.php";

$parser = new postParser;

global $theme;

 $theme['imgdir'] = $forumdir.'/'.substr($theme['imgdir'],0);

// Load global language phrases
$lang->load("usercp");

//Make it admin only

if (!in_array($mybb->user['usergroup'] , array(4,8)))error_no_permission();

// Fetch the current URL

{
$oldstuff_url = get_current_location();
	add_breadcrumb("Old Stuff", $mybb->settings['bburl']."/oldstuff.php");
	
	$attachments = '';
	//set some variables
	
	$mybb->settings['oldstuff_datevar'] = 30;
	$datevar = $mybb->settings['oldstuff_datevar'];
	//$mydate = date((TIME_NOW-(60*60*24*30)));
	$mydate = date((TIME_NOW-(60*60*24*$datevar)));
	$mybb->settings['unitsperpage'] = 30;	
	
	// Pagination

	$perpage = $mybb->settings['unitsperpage'];
	$page = intval($mybb->input['page']);

	if(intval($mybb->input['page']) > 0)
	{
		$start = ($page-1) *$perpage;
	}
	else
	{
		$start = 0;
		$page = 1;
	}

	$end = $start + $perpage;
	$lower = $start+1;

	//Get the attachments
			 
	$query = $db->query("
		SELECT a.*, p.subject, p.dateline, t.tid, t.lastpost, t.subject AS threadsubject
		FROM ".TABLE_PREFIX."attachments a
		LEFT JOIN ".TABLE_PREFIX."posts p ON (a.pid=p.pid)
		LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid)
		WHERE t.lastpost <= $mydate AND a.dateuploaded <=$mydate
		ORDER BY p.dateline DESC LIMIT {$start}, {$perpage}
	");

	$bandwidth = $totaldownloads = 0;
	while($attachment = $db->fetch_array($query))
	{
		if($attachment['dateline'] && $attachment['tid'])
		{
			$attachment['subject'] = htmlspecialchars_uni($parser->parse_badwords($attachment['subject']));
			$attachment['postlink'] = get_post_link($attachment['pid'], $attachment['tid']);
			$attachment['threadlink'] = get_thread_link($attachment['tid']);
			$attachment['threadsubject'] = htmlspecialchars_uni($parser->parse_badwords($attachment['threadsubject']));

			$size = get_friendly_size($attachment['filesize']);
			$icon = get_attachment_icon(get_extension($attachment['filename']));
			$attachment['filename'] = htmlspecialchars_uni($attachment['filename']);

			$sizedownloads = $lang->sprintf($lang->attachment_size_downloads, $size, $attachment['downloads']);
			$attachdate = my_date($mybb->settings['dateformat'], $attachment['dateline']);
			$attachtime = my_date($mybb->settings['timeformat'], $attachment['dateline']);
			$lastactivity = my_date($mybb->settings['dateformat'], $attachment['lastpost']);
			$altbg = alt_trow();

			eval("\$attachments .= \"".$templates->get("oldstuff_attachments_attachment")."\";");

			// Add to bandwidth total
			$bandwidth += ($attachment['filesize'] * $attachment['downloads']);
			$totaldownloads += $attachment['downloads'];
		}
		else
		{
			// This little thing deletes attachments without a thread/post
			remove_attachment($attachment['pid'], $attachment['posthash'], $attachment['aid']);
		}
	}
$query = $db->query("
		SELECT a.*, SUM(filesize) AS ausage, COUNT(aid) AS acount, t.tid, t.lastpost, t.subject AS threadsubject
		FROM ".TABLE_PREFIX."attachments a
		LEFT JOIN ".TABLE_PREFIX."posts p ON (a.pid=p.pid)
		LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=p.tid)
		WHERE t.lastpost <= $mydate
	");
	
	//$query = $db->simple_select("attachments", "SUM(filesize) AS ausage, COUNT(aid) AS acount", "uid !='0'");
	$usage = $db->fetch_array($query);
	$totalusage = $usage['ausage'];
	$totalattachments = $usage['acount'];
	$friendlyusage = get_friendly_size($totalusage);
	if($mybb->usergroup['attachquota'])
	{
		$percent = round(($totalusage/($mybb->usergroup['attachquota']*1024))*100)."%";
		$attachquota = get_friendly_size($mybb->usergroup['attachquota']*1024);
		$usagenote = $lang->sprintf($lang->attachments_usage_quota, $friendlyusage, $attachquota, $percent, $totalattachments);
	}
	else
	{
		$percent = $lang->unlimited;
		$attachquota = $lang->unlimited;
		$usagenote = $lang->sprintf($lang->attachments_usage, $friendlyusage, $totalattachments);
	}
    $multipage = multipage($totalattachments, $perpage, $page, "oldstuff.php");
	//$multipage = multipage($totalattachments, $perpage, $page, $_SERVER['PHP_SELF'].'?paged=1'.$brl);
	$bandwidth = get_friendly_size($bandwidth);

	if(!$attachments)
	{
		eval("\$attachments = \"".$templates->get("oldstuff_attachments_none")."\";");
		$usagenote = '';
	}
	
	eval("\$manageattachments = \"".$templates->get("oldstuff_attachments")."\";");
	output_page($manageattachments);
}

if($mybb->input['action'] == "do_attachments" && $mybb->request_method == "post")
{
	// Verify incoming POST request
	verify_post_check($mybb->input['my_post_key']);

	$plugins->run_hooks("usercp_do_attachments_start");
	require_once MYBB_ROOT."inc/functions_upload.php";
	if(!is_array($mybb->input['attachments']))
	{
		error($lang->no_attachments_selected);
	}
	$aids = implode(',', array_map('intval', $mybb->input['attachments']));
	$query = $db->simple_select("attachments", "*", "aid IN ($aids)");
	while($attachment = $db->fetch_array($query))
	{
		remove_attachment($attachment['pid'], '', $attachment['aid']);
	}
	//$plugins->run_hooks("usercp_do_attachments_end");
	redirect("oldstuff.php?action=attachments", $lang->attachments_deleted);
}
//eval("\$oldstuff = \"".$templates->get("oldstuff")."\";");
output_page($oldstuff);
