PHP Written Resource Download Anti theft Chain Class Sharing

  • 2021-06-28 11:37:48
  • OfStack

Writing an PHP anti-theft chain external resource download processing function these days, just finished last night, and encountered some problems, which are not detailed here;
The following is a simple self-written PHP anti-theft chain processing class (reorganized into class files for later improvement);


<?php
/**
 *
 *  Anti-theft Chain External Resource Download Processing Class 
 * 
 * @link   http://ofstack.com
 * 
 */
class BurglarDow{
 /**
     *  Initial License Download Status 
     * @var    allow
     * @access private
     */
 private $allow      =  false;
 /**
     *  Initial download address 
     * @var    dowUrl
     * @access private
     */
 private $dowUrl     =  null;
 /**
     *  Initial incoming domain name 
     * @var    RemoteUrl
     * @access private
     */
 private $RemoteUrl  =  null;
 /**
     *  List of initially licensed resource access domain names 
     * @var    allowUrl
     * @access private
     */
 private $allowUrl   =  array();
 /**
     *  Initial jump address 
     * @var    Location
     * @access private
     */
 private $Location   =  null;
 public function __construct($dowUrl,$Location,array $allowUrl){
  //  Initial download address 
  $this->dowUrl   = $dowUrl;
  //  List of initially licensed resource access domain names 
  $this->allowUrl = $allowUrl;
  //  Initial jump address 
  $this->Location = $Location;
  $this->RemoteUrl = @parse_url($_SERVER['HTTP_REFERER']);                                                      //  Get incoming domain name 
  if(!is_array($this->RemoteUrl))
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: ".$this->Location);
  if(isset($this->RemoteUrl['host'])){
   if(in_array($this->RemoteUrl['host'],$this->allowUrl)){                                                   //  Determine whether to come to a licensed domain name 
    $this->allow  = true;                                                                                 //  Download license status is:True 
   }
  }
  unset($this->allowUrl,$this->RemoteUrl);                                                                      //  Release memory variables 
 }
 /**
  *  Anti-theft Chain Resource Download 
  * @access public
  * @return mixed
  */
 public function dow(){
  $FileInfo = get_headers($this->dowUrl,1);                                                                     //  Get remote file header information 
  if(true === $this->allow){                                                                                    //  Determine whether to license downloaded resources 
   // Determine whether the profile exists 
   if(is_file('Config.ini')){
    $FileCon = parse_ini_file('Config.ini');
   }else{
    $FileName   =  basename($FileInfo['Content-Location']);
    $FileConStr = "FileName  = {$FileName}\r\nFileUrl   = {$FileInfo['Content-Location']}\r\nFileSize   = {$FileInfo['Content-Length']}";
    $handle = fopen ('Config.ini', "wb");                                                                 // Config.ini Create a file if it does not exist 
    if (fwrite ($handle, $FileConStr) == FALSE) {                                                         //  Data Write to File  
     echo "File creation failed ..."; 
    }
    fclose ($handle);                                                                                     //  Close 1 Open file pointers 
    $FileCon = parse_ini_file('Config.ini');
   }
   if(!empty($$this->dowUrl)){
    $fp = @fopen($$this->dowUrl, "rb");                                                                   // 2 Binary mode read file 
    if (!$fp)
      exit("Download a mistake.\n\n");
    //  Output remote resources 
    header("Content-type:text/html;charset=utf-8");
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$FileCon['FileName']);
    header("Accept-Ranges: bytes");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Pragma: public');
    header('Content-Length: '.$FileCon['FileSize']);
    while (!feof($fp)){
     set_time_limit(0);                                                                                 //  Set maximum file execution time 
     echo fread($fp, 1024);                                                                             //  output file 
     flush();                                                                                           //  Output Buffer 
     ob_flush();                                                                                        //  Contents in output buffer 
    }
    fclose($fp);
   }else{
    header("HTTP/1.1 404 Not Found");
   }
  }else{
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: ".$this->Location);
  }
 }
}
//  Remote Resource Address 
$dowUrl = 'http://dldir1.qq.com/qqfile/qq/QQ5.1/10055/QQ5.1.exe';
//  Jump Address 
$Location = 'http://ofstack.com';
//  List of licensed incoming domain names 
$allowUrl = array(
 'ofstack.com',
);
$BurglarDow = new BurglarDow($dowUrl,$Location,$allowUrl);
$BurglarDow -> dow();


Related articles: