PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/duplicator/classes/package.archive.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 306 lines | 215 code | 40 blank | 51 comment | 37 complexity | 2ae089f62abfa20e7a5f375dad196618 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'DUPLICATOR_VERSION' ) ) exit; // Exit if accessed directly
  3. require_once (DUPLICATOR_PLUGIN_PATH . 'classes/package.archive.zip.php');
  4. require_once (DUPLICATOR_PLUGIN_PATH . 'lib/forceutf8/Encoding.php');
  5. /**
  6. * The base class for all filter types Directories/Files/Extentions
  7. */
  8. class DUP_Archive_Filter_Scope_Base
  9. {
  10. //All internal storage items that duplicator decides to filter
  11. public $Core = array();
  12. //Items when creating a package or template that a user decides to filter
  13. public $Instance = array();
  14. }
  15. /**
  16. * The filter types that belong to directories
  17. */
  18. class DUP_Archive_Filter_Scope_Directory extends DUP_Archive_Filter_Scope_Base
  19. {
  20. //Items that are not readable
  21. public $Warning = array();
  22. //Items that are not readable
  23. public $Unreadable = array();
  24. }
  25. /**
  26. * The filter types that belong to files
  27. */
  28. class DUP_Archive_Filter_Scope_File extends DUP_Archive_Filter_Scope_Directory
  29. {
  30. //Items that are too large
  31. public $Size = array();
  32. }
  33. /**
  34. * The filter information object which store all information about the filtered
  35. * data that is gathered to the execution of a scan process
  36. */
  37. class DUP_Archive_Filter_Info
  38. {
  39. //Contains all folder filter info
  40. public $Dirs = array();
  41. //Contains all file filter info
  42. public $Files = array();
  43. //Contains all extensions filter info
  44. public $Exts = array();
  45. public $UDirCount = 0;
  46. public $UFileCount = 0;
  47. public $UExtCount = 0;
  48. public function __construct()
  49. {
  50. $this->Dirs = new DUP_Archive_Filter_Scope_Directory();
  51. $this->Files = new DUP_Archive_Filter_Scope_File();
  52. $this->Exts = new DUP_Archive_Filter_Scope_Base();
  53. }
  54. }
  55. class DUP_Archive
  56. {
  57. //PUBLIC
  58. public $FilterDirs;
  59. public $FilterExts;
  60. public $FilterDirsAll = array();
  61. public $FilterExtsAll = array();
  62. public $FilterOn;
  63. public $File;
  64. public $Format;
  65. public $PackDir;
  66. public $Size = 0;
  67. public $Dirs = array();
  68. public $Files = array();
  69. public $FilterInfo;
  70. //PROTECTED
  71. protected $Package;
  72. public function __construct($package)
  73. {
  74. $this->Package = $package;
  75. $this->FilterOn = false;
  76. $this->FilterInfo = new DUP_Archive_Filter_Info();
  77. }
  78. public function Build($package)
  79. {
  80. try
  81. {
  82. $this->Package = $package;
  83. if (!isset($this->PackDir) && ! is_dir($this->PackDir)) throw new Exception("The 'PackDir' property must be a valid diretory.");
  84. if (!isset($this->File)) throw new Exception("A 'File' property must be set.");
  85. $this->Package->SetStatus(DUP_PackageStatus::ARCSTART);
  86. switch ($this->Format)
  87. {
  88. case 'TAR': break;
  89. case 'TAR-GZIP': break;
  90. default:
  91. if (class_exists(ZipArchive))
  92. {
  93. $this->Format = 'ZIP';
  94. DUP_Zip::Create($this);
  95. }
  96. break;
  97. }
  98. $storePath = "{$this->Package->StorePath}/{$this->File}";
  99. $this->Size = @filesize($storePath);
  100. $this->Package->SetStatus(DUP_PackageStatus::ARCDONE);
  101. }
  102. catch (Exception $e)
  103. {
  104. echo 'Caught exception: ', $e->getMessage(), "\n";
  105. }
  106. }
  107. public function GetFilterDirAsArray()
  108. {
  109. return array_map('DUP_Util::SafePath', explode(";", $this->FilterDirs, -1));
  110. }
  111. public function GetFilterExtsAsArray()
  112. {
  113. return explode(";", $this->FilterExts, -1);
  114. }
  115. /**
  116. * Get the directory size recursively, but don't calc the snapshot directory, exclusion diretories
  117. * @link http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx Windows filename restrictions
  118. */
  119. public function Stats()
  120. {
  121. $this->createFilterInfo();
  122. $this->getDirs();
  123. $this->getFiles();
  124. return $this;
  125. }
  126. //Build Filter Data
  127. private function createFilterInfo()
  128. {
  129. //FILTER: INSTANCE ITEMS
  130. //Add the items generated at create time
  131. if ($this->FilterOn)
  132. {
  133. $this->FilterInfo->Dirs->Instance = array_map('DUP_Util::SafePath', explode(";", $this->FilterDirs, -1));
  134. $this->FilterInfo->Exts->Instance = explode(";", $this->FilterExts, -1);
  135. }
  136. //FILTER: CORE ITMES
  137. //Filters Duplicator free packages & All pro local directories
  138. $this->FilterInfo->Dirs->Core[] = DUPLICATOR_SSDIR_PATH;
  139. $this->FilterDirsAll = array_merge($this->FilterInfo->Dirs->Instance,
  140. $this->FilterInfo->Dirs->Core);
  141. $this->FilterExtsAll = array_merge($this->FilterInfo->Exts->Instance,
  142. $this->FilterInfo->Exts->Core);
  143. }
  144. //Get All Directories then filter
  145. private function getDirs()
  146. {
  147. $rootPath = DUP_Util::SafePath(rtrim(DUPLICATOR_WPROOTPATH, '//' ));
  148. $this->Dirs = array();
  149. //If the root directory is a filter then we will only need the root files
  150. if (in_array($this->PackDir, $this->FilterDirsAll))
  151. {
  152. $this->Dirs[] = $this->PackDir;
  153. }
  154. else
  155. {
  156. $this->Dirs = $this->dirsToArray($rootPath);
  157. $this->Dirs[] = $this->PackDir;
  158. }
  159. //Filter Directories
  160. //Invalid test contains checks for: characters over 250, invlaid characters,
  161. //empty string and directories ending with period (Windows incompatable)
  162. foreach ($this->Dirs as $key => $val)
  163. {
  164. //INSTANCE: Remove path filter directories
  165. foreach ($this->FilterDirsAll as $item)
  166. {
  167. $trimmed_item = rtrim($item, '/');
  168. if ($val == $trimmed_item || strstr($val, $trimmed_item . '/'))
  169. {
  170. unset($this->Dirs[$key]);
  171. continue 2;
  172. }
  173. }
  174. //WARNING: Find OS items that may have issues
  175. $name = basename($val);
  176. $warn_test = strlen($val) > 250
  177. || preg_match('/(\/|\*|\?|\>|\<|\:|\\|\|)/', $name)
  178. || trim($name) == ""
  179. || (strrpos($name, '.') == strlen($name) - 1 && substr($name, -1) == '.')
  180. || preg_match('/[^\x20-\x7f]/', $name);
  181. if ($warn_test)
  182. {
  183. $this->FilterInfo->Dirs->Warning[] = DUP_Encoding::toUTF8($val);
  184. }
  185. //UNREADABLE: Directory is unreadable flag it
  186. if (! is_readable($this->Dirs[$key]))
  187. {
  188. unset($this->Dirs[$key]);
  189. $this->FilterInfo->Dirs->Unreadable[] = $val;
  190. $this->FilterDirsAll[] = $val;
  191. }
  192. }
  193. }
  194. //Get all files and filter out error prone subsets
  195. private function getFiles()
  196. {
  197. foreach ($this->Dirs as $key => $val)
  198. {
  199. $files = DUP_Util::ListFiles($val);
  200. foreach ($files as $filePath)
  201. {
  202. $fileName = basename($filePath);
  203. if (!is_dir($filePath))
  204. {
  205. if (!in_array(@pathinfo($filePath, PATHINFO_EXTENSION), $this->FilterExtsAll))
  206. {
  207. //Unreadable
  208. if (!is_readable($filePath))
  209. {
  210. $this->FilterInfo->Files->Unreadable[] = $filePath;
  211. continue;
  212. }
  213. $fileSize = @filesize($filePath);
  214. $fileSize = empty($fileSize) ? 0 : $fileSize;
  215. $invalid_test = strlen($filePath) > 250 ||
  216. preg_match('/(\/|\*|\?|\>|\<|\:|\\|\|)/', $fileName) ||
  217. trim($fileName) == "";
  218. if ($invalid_test || preg_match('/[^\x20-\x7f]/', $fileName))
  219. {
  220. $filePath = DUP_Encoding::toUTF8($filePath);
  221. $this->FilterInfo->Files->Warning[] = $filePath;
  222. }
  223. $this->Size += $fileSize;
  224. $this->Files[] = $filePath;
  225. if ($fileSize > DUPLICATOR_SCAN_WARNFILESIZE)
  226. {
  227. $this->FilterInfo->Files->Size[] = $filePath . ' [' . DUP_Util::ByteSize($fileSize) . ']';
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. //Recursive function to get all Directories in a wp install
  235. //Older PHP logic which is more stable on older version of PHP
  236. //NOTE RecursiveIteratorIterator is problematic on some systems issues include:
  237. // - error 'too many files open' for recursion
  238. // - $file->getExtension() is not reliable as it silently fails at least in php 5.2.9
  239. // - issues with when a file has a permission such as 705 and trying to get info (had to fallback to pathinfo)
  240. // - basic conclusion wait on the SPL libs untill after php 5.4 is a requiremnt
  241. // - since we are in a tight recursive loop lets remove the utiltiy call DUP_Util::SafePath("{$path}/{$file}") and
  242. // squeeze out as much performance as we possible can
  243. private function dirsToArray($path)
  244. {
  245. $items = array();
  246. $handle = @opendir($path);
  247. if ($handle)
  248. {
  249. while (($file = readdir($handle)) !== false)
  250. {
  251. if ($file != '.' && $file != '..')
  252. {
  253. $fullPath = str_replace("\\", '/', "{$path}/{$file}");
  254. if (is_dir($fullPath))
  255. {
  256. $items = array_merge($items, $this->dirsToArray($fullPath));
  257. $items[] = $fullPath;
  258. }
  259. }
  260. }
  261. closedir($handle);
  262. }
  263. return $items;
  264. }
  265. }
  266. ?>