<?php
/**
 * 模塊上傳
 *
 * @version        $Id: module_upload.php 1 14:43 2010年7月20日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/config.php");
CheckPurview('sys_module');
require_once(dirname(__FILE__)."/../include/dedemodule.class.php");
require_once(dirname(__FILE__)."/../include/oxwindow.class.php");
if(empty($action)) $action = '';
$mdir = DEDEDATA.'/module';

if($action=='upload')
{
    if( !is_uploaded_file($upfile) )
    {
        ShowMsg("貌似你什麼都沒有上傳哦！", "javascript:;");
        exit();
    }
    else
    {
        include_once(dirname(__FILE__)."/../include/zip.class.php");
        $tmpfilename = $mdir.'/'.ExecTime().mt_rand(10000,50000).'.tmp';
        move_uploaded_file($upfile, $tmpfilename) or die("把上傳的文件移動到{$tmpfilename}時失敗，請檢查{$mdir}目錄是否有寫入權限！");

        //ZIP格式的文件
        if($filetype==1)
        {
            $z = new zip();
            $files = $z->get_List($tmpfilename);
            $dedefileindex = -1;
            //為了節省資源，系統僅以.xml作為擴展名識別ZIP包裡了dede模塊格式文件
            if(is_array($files))
            {
                for($i=0; $i<count($files); $i++)
                {
                    if( preg_match( "#\.xml#i", $files[$i]['filename']) )
                    {
                        $dedefile = $files[$i]['filename'];
                        $dedefileindex = $i;
                        break;
                    }
                }
            }
            if($dedefileindex==-1)
            {
                unlink($tmpfilename);
                ShowMsg("對不起，你上傳的壓縮包中不存在dede模塊文件！<br /><br /><a href='javascript:history.go(-1);'>&gt;&gt;返回重新上傳&gt;&gt;</a>","javascript:;");
                exit();
            }
            $ziptmp = $mdir.'/ziptmp';
            $z->Extract($tmpfilename, $ziptmp, $dedefileindex);
            unlink($tmpfilename);
            $tmpfilename = $mdir."/ziptmp/".$dedefile;
        }

        $dm = new DedeModule($mdir);
        $infos = $dm->GetModuleInfo($tmpfilename,'file');
        if(empty($infos['hash']))
        {
            unlink($tmpfilename);
            $dm->Clear();
            ShowMsg("對不起，你上傳的文件可能不是織夢模塊的標準格式文件！<br /><br /><a href='javascript:history.go(-1);'>&gt;&gt;返回重新上傳&gt;&gt;</a>","javascript:;");
            exit();
        }
        $okfile = $mdir.'/'.$infos['hash'].'.xml';
        if($dm->HasModule($infos['hash']) && empty($delhas))
        {
            unlink($tmpfilename);
            $dm->Clear();
            ShowMsg("對不起，你上傳的模塊已經存在，<br />如果要覆蓋請先刪除原來版本或選擇強制刪除的選項！<br /><br /><a href='javascript:history.go(-1);'>&gt;&gt;返回重新上傳&gt;&gt;</a>","javascript:;");
            exit();
        }
        @unlink($okfile);
        copy($tmpfilename,$okfile);
        @unlink($tmpfilename);
        $dm->Clear();
        ShowMsg("成功上傳一個新的模塊！","module_main.php?action=view&hash={$infos['hash']}");
        exit();
    }
}
else
{
    $win = new OxWindow();
    $win->Init("module_upload.php", "js/blank.js", "POST' enctype='multipart/form-data");
    $win->mainTitle = "模塊管理";
    $wecome_info = "<a href='module_main.php'>模塊管理</a> &gt;&gt; 上傳模塊";
    $win->AddTitle('請選擇要上傳的文件:');
    $win->AddHidden("action", 'upload');
    $msg = "
    <table width='600' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td height='30'>文件格式：</td>
    <td>
    <input name='filetype' type='radio' value='0' checked='checked' />
      正常的模塊包
      <input type='radio' name='filetype' value='1' />
    經過 zip 壓縮的模塊包 </td>
  </tr>
  <tr>
    <td height='30'>已有模塊：</td>
    <td>
      <input name='delhas' type='checkbox' id='delhas' value='1' /> 強制刪除同名模塊(這可能導致已經安裝的模塊無法卸載)
     </td>
  </tr>
  <tr>
    <td width='96' height='60'>請選擇文件：</td>
    <td width='504'>
    <input name='upfile' type='file' id='upfile' style='width:380px' />    </td>
  </tr>
 </table>
    ";
    $win->AddMsgItem("<div style='padding-left:20px;line-height:150%;'>$msg</div>");
    $winform = $win->GetWindow('ok', '');
    $win->Display();
    exit();
}
//ClearAllLink();
