[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/ -> Zip.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  require_once ('vtlib/thirdparty/dZip.inc.php');
  11  
  12  /**
  13   * Wrapper class over dZip.
  14   * @package vtlib
  15   */
  16  class Vtiger_Zip extends dZip {
  17      /**
  18       * Push out the file content for download.
  19       */
  20  	function forceDownload($zipfileName) {
  21          header("Pragma: public");
  22          header("Expires: 0");
  23          header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  24          header("Cache-Control: private",false);
  25          header("Content-Type: application/zip");
  26          header("Content-Disposition: attachment; filename=".basename($zipfileName).";" );
  27          //header("Content-Transfer-Encoding: binary");
  28  
  29          // For details on this workaround check here the ticket
  30          // http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/5298
  31          $disk_file_size = filesize($zipfileName);
  32          $zipfilesize = $disk_file_size + ($disk_file_size % 1024);
  33          header("Content-Length: ".$zipfilesize);
  34          $fileContent = fread(fopen($zipfileName, "rb"), $zipfilesize);
  35          echo $fileContent;    
  36      }
  37  
  38      /**
  39       * Get relative path (w.r.t base)
  40       */
  41  	function __getRelativePath($basepath, $srcpath) {
  42          $base_realpath = $this->__normalizePath(realpath($basepath));
  43          $src_realpath  = $this->__normalizePath(realpath($srcpath));
  44          $search_index  = strpos($src_realpath, $base_realpath);
  45          if($search_index === 0) {
  46              $startindex = strlen($base_realpath)+1;
  47              // On windows $base_realpath ends with / and On Linux it will not have / at end!
  48              if(strrpos($base_realpath, '/') == strlen($base_realpath)-1) $startindex -= 1;
  49              $relpath = substr($src_realpath, $startindex);
  50          }
  51          return $relpath;
  52      }
  53  
  54      /**
  55       * Check and add '/' directory separator
  56       */
  57  	function __fixDirSeparator($path) {
  58          if($path != '' && (strripos($path, '/') != strlen($path)-1)) $path .= '/';
  59          return $path;
  60      }
  61  
  62      /**
  63       * Normalize the directory path separators.
  64       */
  65  	function __normalizePath($path) {
  66          if($path && strpos($path, '\\')!== false) $path = preg_replace("/\\\\/", "/", $path);
  67          return $path;
  68      }
  69  
  70      /**
  71       * Copy the directory on the disk into zip file.
  72       */
  73  	function copyDirectoryFromDisk($dirname, $zipdirname=null, $excludeList=null, $basedirname=null) {
  74          $dir = opendir($dirname);
  75          if(strripos($dirname, '/') != strlen($dirname)-1)
  76              $dirname .= '/';
  77  
  78          if($basedirname == null) $basedirname = realpath($dirname);
  79  
  80          while(false !== ($file = readdir($dir))) {
  81              if($file != '.' && $file != '..' && 
  82                  $file != '.svn' && $file != 'CVS') {
  83                      // Exclude the file/directory 
  84                      if(!empty($excludeList) && in_array("$dirname$file", $excludeList))
  85                          continue;
  86  
  87                      if(is_dir("$dirname$file")) {
  88                          $this->copyDirectoryFromDisk("$dirname$file", $zipdirname, $excludeList, $basedirname);
  89                      } else {
  90                          $zippath = $dirname;
  91                          if($zipdirname != null && $zipdirname != '') {
  92                              $zipdirname = $this->__fixDirSeparator($zipdirname);
  93                              $zippath = $zipdirname.$this->__getRelativePath($basedirname, $dirname);
  94                          }
  95                          $this->copyFileFromDisk($dirname, $zippath, $file);
  96                      }
  97                  }
  98          }
  99          closedir($dir);
 100      }
 101  
 102      /**
 103       * Copy the disk file into the zip.
 104       */
 105  	function copyFileFromDisk($path, $zippath, $file) {
 106          $path = $this->__fixDirSeparator($path);
 107          $zippath = $this->__fixDirSeparator($zippath);
 108          $this->addFile("$path$file", "$zippath$file");
 109      }
 110  }
 111  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1