<?php

/**
 * 下载类
 *
 * @author 杨金球
 */
class DownAction extends Action {

    public function _empty($method) {
        if (is_numeric($method)) {
            $map['id'] = $method;
            $map['status'] = 1;
            $map['jumpurl'] = 0;

            $software = M('software');
            $soft_msg = $software->field('url')->where($map)->find();
            if ($soft_msg) {
                $soft_msg['url'] = auto_charset($soft_msg['url'], 'utf-8', 'gbk');
                $filename = './Public/Upload/soft/' . $soft_msg['url'];
                if (is_file($filename)) {
                    //$showname = auto_charset($soft_msg['url'], 'utf-8', 'gbk'); //下载时显示的文件名
                    $showname = $soft_msg['url'];
                    //如果没有登录，则写入数据库时user_id = 1
                    if (!isset($_SESSION['user_id'])) {
                        $arg['user_id'] = 0;
                        $arg['username'] = '用户未登录';
                    } else {
                        $arg['user_id'] = $_SESSION['user_id'];
                        $arg['username'] = $_SESSION['username'];
                    }
                    $arg['soft_id'] = $method;
                    $arg['time'] = time();
                    //写入数据库
                    $down = M('download');
                    $down->add($arg);

					//统计点击次数
					$num = new Model();
					$num->query("UPDATE ".C('DB_PREFIX')."software SET hits=hits+1 WHERE id='$method'");
                    //开始下载
                    import("ORG.Net.Http");
                    Http::download($filename, $showname);
                } else {
                    $this->error('附件不存在或者已经删除！');
                }
            } else {
                $this->error('附件不存在或者已经删除！');
            }
        } else {
            $this->error(C('ERROR_MESSAGE'));
        }
    }

    public function download() {
        $map['soft_id'] = intval($_GET['id']);
        $map['time'] = time();
        //如果没有登录，则写入数据库时user_id = 1
        if (!isset($_SESSION['user_id'])) {
            $map['user_id'] = 0;
            $map['username'] = '用户未登录';
        } else {
            $map['user_id'] = $_SESSION['user_id'];
            $map['username'] = $_SESSION['username'];
        }

		//统计点击次数
		$num = new Model();
		$num->query("UPDATE ".C('DB_PREFIX')."software SET hits=hits+1 WHERE id='".$map['soft_id']."'");
        //写入数据库下载
        $down = M('download');
        $down->add($map);
        $this->ajaxReturn('', '', 1);
    }

}

?>
