PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wysija-newsletters/helpers/zip.php

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