WYSIJA_help_zip

The MailPoet Newsletters WYSIJA help zip class.

Defined (1)

The class is defined in the following location(s).

/helpers/zip.php  
  1. class WYSIJA_help_zip extends WYSIJA_object{ 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. /** 
  7. * reeusing wordpress method 
  8. * @param type $temp_file_addr 
  9. * @param type $to 
  10. * @return type  
  11. */ 
  12. function unzip($temp_file_addr, $to) { 
  13. $filesystem = WP_Filesystem(); 
  14. $dounzip = unzip_file($temp_file_addr, $to); 
  15.  
  16. if ( is_wp_error($dounzip) ) { 
  17.  
  18. //DEBUG 
  19. $error = $dounzip->get_error_code(); 
  20. $data = $dounzip->get_error_data($error); 
  21. $this->error($dounzip->get_error_message()); 
  22.  
  23. return false; 
  24.  
  25. return true; 
  26. /** 
  27. * adapted from wp 
  28. */ 
  29. function unzip_wp($file, $to) { 
  30. $filesystem = WP_Filesystem(); 
  31. // Unzip can use a lot of memory, but not this much hopefully 
  32. @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); 
  33.  
  34. $to = str_replace("/", DS, $to); 
  35.  
  36. if (class_exists('ZipArchive')) return $this->_unzip_file_ziparchive($file, $to); 
  37. // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. 
  38. return $this->_unzip_file_pclzip($file, $to); 
  39.  
  40.  
  41. /** 
  42. * adapted from wp 
  43. */ 
  44. function _unzip_file_ziparchive($file, $to) { 
  45. /**careful WordPress global*/ 
  46. global $wp_filesystem; 
  47.  
  48. $z = new ZipArchive(); 
  49.  
  50. // PHP4-compat - php4 classes can't contain constants 
  51. $zopen = $z->open($file, 4); // -- ZIPARCHIVE::CHECKCONS = 4 
  52.  
  53. if ($zopen !== true) { 
  54. $this->error("Archive is not of a correct format!"); 
  55. return false; 
  56.  
  57. $z->extractTo($to);  
  58.  
  59. $z->close(); 
  60.  
  61. return true; 
  62.  
  63. /** 
  64. * adapted from wp 
  65. */ 
  66. function _unzip_file_pclzip($file, $to) { 
  67. global $wp_filesystem; 
  68.  
  69. // See #15789 - PclZip uses string functions on binary data, If it's overloaded with Multibyte safe functions the results are incorrect. 
  70. if ( ini_get('mbstring.func_overload') && function_exists('mb_internal_encoding') ) { 
  71. $previous_encoding = mb_internal_encoding(); 
  72. mb_internal_encoding('ISO-8859-1'); 
  73.  
  74. if(file_exists(ABSPATH . 'wp-admin/includes/class-pclzip.php')) require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php'); 
  75.  
  76. $archive = new PclZip($file); 
  77.  
  78. $archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING); 
  79.  
  80. if ( isset($previous_encoding) ) 
  81. mb_internal_encoding($previous_encoding); 
  82.  
  83. // Is the archive valid? 
  84. if ( !is_array($archive_files) ) { 
  85. $this->error("Archive is not of a correct format!"); 
  86. return false; 
  87.  
  88.  
  89. if ( 0 == count($archive_files) ) { 
  90. $this->error("Archive is empty!"); 
  91. return false; 
  92.  
  93. // Extract the files from the zip 
  94. foreach ( $archive_files as $file ) { 
  95. $filedest=str_replace("/", DS, $to . $file['filename']); 
  96. if ( $file['folder']) { 
  97. $to=str_replace("/", DS, $to); 
  98. if(file_exists($to)) chmod($to, 0777); 
  99.  
  100. //$folderTest=str_replace(array("/"), array(DS), $to . $file['filename']); 
  101. if(is_dir($to) ) { 
  102. //$this->error($to.' is dir with chmod '.substr(sprintf('%o', fileperms($to)), -4)); 
  103. if(!mkdir($filedest, 0777)) { 
  104. $this->error('cannot created folder : '.$filedest); 
  105. $to=dirname($to).DS; 
  106. $filedest=str_replace("/", DS, $to . $file['filename']); 
  107. if(!mkdir($filedest, 0777)) { 
  108. $this->error('Still cannot created folder : '.$filedest); 
  109. return false; 
  110.  
  111. if(file_exists($filedest)) chmod($filedest, 0777); 
  112. continue; 
  113.  
  114. if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files 
  115. continue; 
  116.  
  117. if ( ! $wp_filesystem->put_contents( $filedest, $file['content'], 0644) ) { 
  118. //try another method 
  119. if ( ! ($fp = @fopen($filedest, 'w')) ) 
  120. return false; 
  121. @fwrite($fp, $file['content']); 
  122. @fclose($fp); 
  123.  
  124. if(!file_exists($filedest)) { 
  125. $this->error('Could not copy file : '. $filedest); 
  126. return false; 
  127. }  
  128.  
  129. return true; 
  130.