萝卜头IT论坛

搜索
查看: 3401|回复: 1
收起左侧

[Discuz!] Discuz X3 自动下载程序

[复制链接]
发表于 2013-6-29 14:05:01 | 显示全部楼层 |阅读模式
将下面的文字保存为php文件,上传到服务器或空间上运行即可。会自动下载gbk版的discuz程序并解压到当前目录下。
  1. <?php
  2. set_time_limit(0);


  3. /**
  4. * 下载远程文件类支持断点续传
  5. */
  6. class HttpDownload {

  7.         private $m_url = "";
  8.          private $m_urlpath = "";
  9.          private $m_scheme = "http";
  10.          private $m_host = "";
  11.          private $m_port = "80";
  12.          private $m_user = "";
  13.          private $m_pass = "";
  14.          private $m_path = "/";
  15.          private $m_query = "";
  16.          private $m_fp = "";
  17.          private $m_error = "";
  18.         private $m_httphead = "" ;
  19.         private $m_html = "";

  20.         /**
  21.          * 初始化
  22.          */
  23.         public function PrivateInit($url){
  24.                 $urls = "";
  25.                 $urls = @parse_url($url);
  26.                 $this->m_url = $url;
  27.                 if(is_array($urls)) {
  28.                         $this->m_host = $urls["host"];
  29.                         if(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
  30.                         if(!empty($urls["user"])) $this->m_user = $urls["user"];
  31.                     if(!empty($urls["pass"])) $this->m_pass = $urls["pass"];
  32.                     if(!empty($urls["port"])) $this->m_port = $urls["port"];
  33.                     if(!empty($urls["path"])) $this->m_path = $urls["path"];
  34.                     $this->m_urlpath = $this->m_path;
  35.                         if(!empty($urls["query"])) {
  36.                              $this->m_query = $urls["query"];
  37.                              $this->m_urlpath .= "?".$this->m_query;
  38.                      }
  39.                   }
  40.         }

  41.         /**
  42.         * 打开指定网址
  43.         */
  44.         function OpenUrl($url) {
  45.                 #重设各参数
  46.                 $this->m_url = "";
  47.                 $this->m_urlpath = "";
  48.                 $this->m_scheme = "http";
  49.                 $this->m_host = "";
  50.                 $this->m_port = "80";
  51.                 $this->m_user = "";
  52.                 $this->m_pass = "";
  53.                 $this->m_path = "/";
  54.                 $this->m_query = "";
  55.                 $this->m_error = "";
  56.                 $this->m_httphead = "" ;
  57.                 $this->m_html = "";
  58.                 $this->Close();
  59.                 #初始化系统
  60.                 $this->PrivateInit($url);
  61.                 $this->PrivateStartSession();
  62.         }

  63.         /**
  64.         * 获得某操作错误的原因
  65.         */
  66.         public function printError() {
  67.                 echo "错误信息:".$this->m_error;
  68.                 echo "具体返回头:<br>";
  69.                 foreach($this->m_httphead as $k=>$v) {
  70.                         echo "$k => $v <br>\r\n";
  71.                 }
  72.         }

  73.         /**
  74.         * 判别用Get方法发送的头的应答结果是否正确
  75.         */
  76.         public function IsGetOK() {
  77.                 if( ereg("^2",$this->GetHead("http-state")) ) {
  78.                         return true;
  79.                 } else {
  80.                         $this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."<br>";
  81.                         return false;
  82.                 }
  83.         }
  84.         
  85.         /**
  86.         * 看看返回的网页是否是text类型
  87.         */
  88.         public function IsText() {
  89.                 if (ereg("^2",$this->GetHead("http-state")) && eregi("^text",$this->GetHead("content-type"))) {
  90.                         return true;
  91.                 } else {
  92.                         $this->m_error .= "内容为非文本类型<br>";
  93.                         return false;
  94.                 }
  95.         }
  96.         /**
  97.         * 判断返回的网页是否是特定的类型
  98.         */
  99.         public function IsContentType($ctype) {
  100.                 if (ereg("^2",$this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {
  101.                         return true;
  102.                 } else {
  103.                         $this->m_error .= "类型不对 ".$this->GetHead("content-type")."<br>";
  104.                         return false;
  105.                 }
  106.         }
  107.         
  108.         /**
  109.         * 用 HTTP 协议下载文件
  110.         */
  111.         public function SaveToBin($savefilename) {
  112.                 if (!$this->IsGetOK()) return false;
  113.                 if (@feof($this->m_fp)) {
  114.                         $this->m_error = "连接已经关闭!";
  115.                         return false;
  116.                 }
  117.                 $fp = fopen($savefilename,"w") or die("写入文件 $savefilename 失败!");
  118.                 while (!feof($this->m_fp)) {
  119.                         @fwrite($fp,fgets($this->m_fp,256));
  120.                 }
  121.                 @fclose($this->m_fp);
  122.                 return true;
  123.         }
  124.         
  125.         /**
  126.         * 保存网页内容为 Text 文件
  127.         */
  128.         public function SaveToText($savefilename) {
  129.                 if ($this->IsText()) {
  130.                         $this->SaveBinFile($savefilename);
  131.                 } else {
  132.                         return "";
  133.                 }
  134.         }
  135.         
  136.         /**
  137.         * 用 HTTP 协议获得一个网页的内容
  138.         */
  139.         public function GetHtml() {
  140.                 if (!$this->IsText()) return "";
  141.                 if ($this->m_html!="") return $this->m_html;
  142.                 if (!$this->m_fp||@feof($this->m_fp)) return "";
  143.                 while(!feof($this->m_fp)) {
  144.                         $this->m_html .= fgets($this->m_fp,256);
  145.                 }
  146.                 @fclose($this->m_fp);
  147.                 return $this->m_html;
  148.         }
  149.         
  150.         /**
  151.         * 开始 HTTP 会话
  152.         */
  153.         public function PrivateStartSession() {
  154.                 if (!$this->PrivateOpenHost()) {
  155.                         $this->m_error .= "打开远程主机出错!";
  156.                         return false;
  157.                 }
  158.                 if ($this->GetHead("http-edition")=="HTTP/1.1") {
  159.                         $httpv = "HTTP/1.1";
  160.                 } else {
  161.                         $httpv = "HTTP/1.0";
  162.                 }
  163.                 fputs($this->m_fp,"GET ".$this->m_urlpath." $httpv\r\n");
  164.                 fputs($this->m_fp,"Host: ".$this->m_host."\r\n");
  165.                 fputs($this->m_fp,"Accept: */*\r\n");
  166.                 fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)\r\n");
  167.                 #HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
  168.                 if ($httpv=="HTTP/1.1") {
  169.                         fputs($this->m_fp,"Connection: Close\r\n\r\n");
  170.                 } else {
  171.                         fputs($this->m_fp,"\r\n");
  172.                 }
  173.                 $httpstas = fgets($this->m_fp,256);
  174.                 $httpstas = split(" ",$httpstas);
  175.                 $this->m_httphead["http-edition"] = trim($httpstas[0]);
  176.                 $this->m_httphead["http-state"] = trim($httpstas[1]);
  177.                 $this->m_httphead["http-describe"] = "";
  178.                 for ($i=2;$i<count($httpstas);$i++) {
  179.                         $this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]);
  180.                 }
  181.                 while (!feof($this->m_fp)) {
  182.                         $line = str_replace(""","",trim(fgets($this->m_fp,256)));
  183.                         if($line == "") break;
  184.                         if (ereg(":",$line)) {
  185.                                 $lines = split(":",$line);
  186.                                 $this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]);
  187.                         }
  188.                 }
  189.         }
  190.         
  191.         /**
  192.         * 获得一个Http头的值
  193.         */
  194.         public function GetHead($headname) {
  195.                 $headname = strtolower($headname);
  196.                 if (isset($this->m_httphead[$headname])) {
  197.                         return $this->m_httphead[$headname];
  198.                 } else {
  199.                         return "";
  200.                 }
  201.         }
  202.         
  203.         /**
  204.         * 打开连接
  205.         */
  206.         public function PrivateOpenHost() {
  207.                 if ($this->m_host=="") return false;
  208.                 $this->m_fp = @fsockopen($this->m_host, $this->m_port, &$errno, &$errstr,10);
  209.                 if (!$this->m_fp){
  210.                         $this->m_error = $errstr;
  211.                         return false;
  212.                 } else {
  213.                         return true;
  214.                 }
  215.         }
  216.         
  217.         /**
  218.         * 关闭连接
  219.         */
  220.         public function Close(){
  221.                 @fclose($this->m_fp);
  222.         }
  223. }
  224. if(file_exists("Discuz_X3.0_SC_GBK.zip"))
  225. {
  226.     echo "文件已存在 ";
  227. }
  228. else
  229. {
  230. $file = new HttpDownload(); # 实例化类
  231. $file->OpenUrl("http://download.comsenz.com/DiscuzX/3.0/Discuz_X3.0_SC_GBK.zip"); # 远程文件地址
  232. $file->SaveToBin("Discuz_X3.0_SC_GBK.zip"); # 保存路径及文件名
  233. $file->Close(); # 释放资源
  234. echo "文件已下载 ";
  235. }

  236. function unzip_file($file, $destination){
  237.   // create object
  238.   $zip = new ZipArchive() ;
  239.   // open archive
  240.   if ($zip->open($file) !== TRUE) {
  241.   die ('Could not open archive');
  242.   }
  243.   // extract contents to destination directory
  244.   $zip->extractTo($destination);
  245.   // close archive
  246.   $zip->close();
  247.   echo 'Archive extracted to directory';
  248. }
  249. function xCopy($source, $destination, $child){
  250.     if (!file_exists($destination))
  251.     {
  252.         if (!mkdir(rtrim($destination, '/'), 0777))
  253.         {
  254.         //$err->add($_LANG['cannt_mk_dir']);
  255.         return false;
  256.         }
  257.         @chmod($destination, 0777);
  258.      }
  259. if(!is_dir($source)){  
  260. return 0;
  261. }
  262. if(!is_dir($destination)){
  263. mkdir($destination,0777);   
  264. }
  265. $handle=dir($source);
  266. while($entry=$handle->read()){
  267. if(($entry!=".")&&($entry!="..")){
  268. if(is_dir($source."/".$entry)){
  269. if($child)
  270. xCopy($source."/".$entry,$destination."/".$entry,$child);
  271. }
  272. else{
  273. copy($source."/".$entry,$destination."/".$entry);
  274. }
  275. }   
  276. }   
  277. return 1;
  278. }
  279. function deldir($dir) {
  280. if (!file_exists($dir)){return true;
  281. }else{@chmod($dir, 0777);}
  282.   $dh=opendir($dir);
  283.   while ($file=readdir($dh)) {
  284.     if($file!="." && $file!="..") {
  285.       $fullpath=$dir."/".$file;
  286.       if(!is_dir($fullpath)) {
  287.           unlink($fullpath);
  288.       } else {
  289.           deldir($fullpath);
  290.       }
  291.     }
  292.   }}

  293. if(!is_dir('temp')){  
  294. unzip_file('./Discuz_X3.0_SC_GBK.zip','temp');}
  295. else {echo '文件已解压 ';}
  296. xCopy("temp/upload","./",1);
  297. deldir("temp");
  298. echo '下载已完成';
复制代码

discuz_download.php

7.62 KB, 下载次数: 559

回复

使用道具 举报

发表于 2014-12-25 06:21:33 | 显示全部楼层
謝謝您了
回复

使用道具 举报

联系我们(Contact)|手机版|萝卜头IT论坛 ( 苏ICP备15050961号-1 )

GMT+8, 2024-4-18 15:33 , Processed in 0.109101 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表