<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board v1.2
|   ========================================
|   by Matthew Mecham
|   (c) 2001 - 2003 Invision Power Services
|   http://www.invisionpower.com
|   ========================================
|   Web: http://www.invisionboard.com
|   Email: matt@invisionpower.com
|   Licence Info: http://www.invisionboard.com/?license
+---------------------------------------------------------------------------
|
|   > Attachment Handler module
|   > Module written by Matt Mecham
|   > Date started: 10th March 2002
|
|	> Module Version Number: 1.0.0
+--------------------------------------------------------------------------
*/


$idx = new attach;

class attach {


    function attach()
    {
        global $ibforums, $DB, $std, $print, $skin_universal;

        $ibforums->input['id'] = preg_replace( "/^(\d+)$/", "\\1", $ibforums->input['id'] );

        if ($ibforums->input['id'] == "")
        {
        	$std->Error( array( 'LEVEL' => 1, 'MSG' => 'missing_files' ) );
        }

        if ($ibforums->input['type'] == 'post')
        {
        	// Handle post attachments.

        	$DB->query("SELECT pid, attach_id, attach_type, attach_file, author_id, attach_price FROM ibf_posts WHERE pid='".$ibforums->input['id']."'"); /*买卖hack*/

        	if ( !$DB->get_num_rows() )
        	{
        		$std->Error( array( 'LEVEL' => 1, 'MSG' => 'missing_files' ) );
        	}

        	$post = $DB->fetch_row();

        	if ( $post['attach_id'] == "" )
        	{
        		$std->Error( array( 'LEVEL' => 1, 'MSG' => 'missing_files' ) );
        	}
        	//买卖hack开始
			$pass = 0;
			if($post['attach_price']>0)
			{
				$DB->query("SELECT customer FROM ibf_sale WHERE mode='a' AND pid=".$post['pid']);
				$customer_data = $DB->fetch_row();
				$DB->free_result();
				
				if(!preg_match("#\|".$ibforums->member['uid']."\|#",$customer_data['customer']))
				{
					if($ibforums->member['money']>=$post['attach_price'])
					{
						$DB->query("UPDATE ibf_members SET money=money-".$post['attach_price']." WHERE uid='".$ibforums->member['uid']."'");
						$DB->query("UPDATE ibf_members SET money=money+".$post['attach_price']." WHERE uid='".$post['author_id']."'");
						
						$sale['customer'] = $customer_data['customer'].$ibforums->member['uid']."|";
						
						$DB->query("UPDATE ibf_sale SET customer='".$sale['customer']."' WHERE mode='a' AND pid=".$post['pid']);
		
						$pass = 1;
					}
					else
					{
						$pass = 0;
					}
				}
				else
				{
					$pass = 1;
				}
			}
			else
			{
				$pass = 1;
			}

			if(!$pass)
			{
        		$std->Error( array( 'LEVEL' => 1, 'MSG' => 'sale_not_enough_money' ) );
			}
                //买卖hack结束

        	$file = $ibforums->vars['upload_dir']."/".$post['attach_id'];

        	if ( file_exists( $file ) and ( $post['attach_type'] != "" ) )
        	{
        		// Update the "hits"..

        		$DB->query("UPDATE ibf_posts SET attach_hits=attach_hits+1 WHERE pid='".$post['pid']."'");

        		// Set up the headers..

        		@header( "Content-Type: ".$post['attach_type']."\nContent-Disposition: inline; filename=\"".$post['attach_file']."\"\nContent-Length: ".(string)(filesize( $file ) ) );

        		// Open and display the file..

        		$fh = fopen( $file, 'rb' );  // Set binary for Win even if it's an ascii file, it won't hurt.
        		fpassthru( $fh );
        		@fclose( $fh );
        		exit();
        	}
        	else
        	{
        		// File does not exist..
        		$std->Error( array( 'LEVEL' => 1, 'MSG' => 'missing_files' ) );
        	}
        }

    }


}

?>
