PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backupbuddy/lib/zipbuddy/zbzipcore.php

https://bitbucket.org/betaimages/chakalos
PHP | 384 lines | 268 code | 31 blank | 85 comment | 10 complexity | 605a49a3a94c39d02ca41fb9970af406 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * pluginbuddy_zbzipcore Class
  4. *
  5. * Provides an abstract zip capability core class
  6. *
  7. * Version: 1.0.0
  8. * Author:
  9. * Author URI:
  10. *
  11. * @param $parent object Optional parent object which can provide functions for reporting, etc.
  12. * @return null
  13. *
  14. */
  15. if ( !class_exists( "pluginbuddy_zbzipcore" ) ) {
  16. abstract class pluginbuddy_zbzipcore {
  17. // status method type parameter values - would like a class for this
  18. const STATUS_TYPE_DETAILS = 'details';
  19. const MAX_ERROR_LINES_TO_SHOW = 10;
  20. const NORM_DIRECTORY_SEPARATOR = '/';
  21. const DIRECTORY_SEPARATORS = '/\\';
  22. public $_version = '1.0';
  23. /**
  24. * The plugin path for this plugin
  25. *
  26. * @var $_pluginPath string
  27. */
  28. public $_pluginPath = '';
  29. /**
  30. * The path of this directory node
  31. *
  32. * @var path string
  33. */
  34. protected $_path = "";
  35. /**
  36. * The absolute paths to be excluded, must be / terminated
  37. *
  38. * @var paths_to_exclude array of string
  39. */
  40. protected $_paths_to_exclude = array();
  41. /**
  42. * The details of the method
  43. *
  44. * @var method_details array
  45. */
  46. protected $_method_details = array();
  47. /**
  48. * The set of paths for where to look for zip or other executables
  49. *
  50. * Applies to Linux only - first path is empty so that default environment PATH is used
  51. * first, after that possible paths (must include leading and trailing slash)
  52. *
  53. * @var executable_paths array
  54. */
  55. protected $_executable_paths = array( '', '/usr/bin/', '/usr/local/bin/' );
  56. /**
  57. * Whether or not we can call a status calback
  58. *
  59. * @var have_status_callback bool
  60. */
  61. protected $_have_status_callback = false;
  62. /**
  63. * Object->method array for status function
  64. *
  65. * @var status_callback array
  66. */
  67. protected $_status_callback = array();
  68. /**
  69. * Array of status information
  70. *
  71. * @var status array
  72. */
  73. protected $_status = array();
  74. /**
  75. * __construct()
  76. *
  77. * Default constructor.
  78. *
  79. * @return null
  80. *
  81. */
  82. public function __construct() {
  83. }
  84. /**
  85. * __destruct()
  86. *
  87. * Default destructor.
  88. *
  89. * @return null
  90. *
  91. */
  92. public function __destruct( ) {
  93. }
  94. /**
  95. * set_status_callback()
  96. *
  97. * Sets a reference to the function to call for each status update.
  98. * Argument must at least be a non-empty array with 2 elements
  99. *
  100. * @param array $callback Object->method to call for status updates.
  101. * @return null
  102. *
  103. */
  104. public function set_status_callback( $callback = array() ) {
  105. if ( is_array( $callback ) && !empty( $callback ) && ( 2 == count( $callback ) ) ) {
  106. $this->_status_callback = $callback;
  107. $this->_have_status_callback = true;
  108. }
  109. }
  110. /**
  111. * get_status()
  112. *
  113. * Returns the status array
  114. *
  115. * @return array The status array
  116. *
  117. */
  118. public function get_status() {
  119. return $this->_status;
  120. }
  121. /**
  122. * get_method_tag()
  123. *
  124. * Returns the (static) method tag
  125. *
  126. * @return string The method tag
  127. *
  128. */
  129. abstract public function get_method_tag();
  130. /**
  131. * get_is_compatibility_method()
  132. *
  133. * Returns the (static) is_compatibility_method boolean
  134. *
  135. * @return bool
  136. *
  137. */
  138. abstract public function get_is_compatibility_method();
  139. /**
  140. * get_method_details()
  141. *
  142. * Returns the details array
  143. *
  144. * @return array
  145. *
  146. */
  147. public function get_method_details() {
  148. return $this->_method_details;
  149. }
  150. /**
  151. * set_method_details()
  152. *
  153. * Sets the internal (settable) details
  154. *
  155. * @param array
  156. * @return null
  157. *
  158. */
  159. public function set_method_details( array $details, $merge = true ) {
  160. if ( true === $merge ) {
  161. $this->_method_details[ 'attr' ] = array_merge( $this->_method_details[ 'attr' ], $details[ 'attr' ] );
  162. $this->_method_details[ 'param' ] = array_merge( $this->_method_details[ 'param' ], $details[ 'param' ] );
  163. } else {
  164. $this->_method_details = $details;
  165. }
  166. }
  167. /**
  168. * get_executable_paths()
  169. *
  170. * Returns the executable_paths array
  171. *
  172. * @return array
  173. *
  174. */
  175. public function get_executable_paths() {
  176. return $this->_executable_paths;
  177. }
  178. /**
  179. * set_executable_paths()
  180. *
  181. * Sets the executable_paths array so can be used to augment or override the default
  182. *
  183. * @param array
  184. * @return null
  185. *
  186. */
  187. public function set_executable_paths( array $paths, $merge = true ) {
  188. if ( true === $merge ) {
  189. $this->_executable_paths = array_merge( $this->_executable_paths, $paths );
  190. } else {
  191. $this->_executable_paths = $paths;
  192. }
  193. }
  194. /**
  195. * delete_directory_recursive()
  196. *
  197. * Recursively delete a directory and it's content
  198. *
  199. * @param string $directory Directory to delete
  200. * @return bool True if operation fully successful, otherwise false
  201. *
  202. */
  203. public function delete_directory_recursive( $directory ) {
  204. $directory = preg_replace( '|[/\\\\]+$|', '', $directory );
  205. $files = glob( $directory . DIRECTORY_SEPARATOR . '*', GLOB_MARK );
  206. if ( is_array( $files ) && !empty( $files ) ) {
  207. foreach( $files as $file ) {
  208. if( DIRECTORY_SEPARATOR === substr( $file, -1 ) ) {
  209. $this->delete_directory_recursive( $file );
  210. } else {
  211. unlink( $file );
  212. }
  213. }
  214. }
  215. // It really should be a directory but check in case
  216. if ( is_dir( $directory ) ) {
  217. rmdir( $directory );
  218. }
  219. // Check if we failed to delete it - possibly not all content was able to be deleted
  220. if ( is_dir( $directory ) ) {
  221. return false;
  222. } else {
  223. return true;
  224. }
  225. }
  226. /* _render_exclusions_file()
  227. *
  228. * function description
  229. *
  230. * @param string $file File to write exclusions into.
  231. * @param array $exclusions Array of directories/paths to exclude. One per line.
  232. * @return null
  233. */
  234. public function _render_exclusions_file( $file, $exclusions ) {
  235. pb_backupbuddy::status( 'details', 'Creating backup exclusions file `' . $file . '`.' );
  236. //$exclusions = pb_backupbuddy::$classes['core']->get_directory_exclusions();
  237. // Format.
  238. foreach( $exclusions as &$exclusion ) {
  239. // DIRECTORY.
  240. if ( is_dir( ABSPATH . ltrim( $exclusion, '/' ) ) ) {
  241. $exclusion = rtrim( $exclusion, '/\\' ) . '/*';
  242. pb_backupbuddy::status( 'details', 'Excluding directory `' . $exclusion . '`.' );
  243. // FILE.
  244. } elseif ( is_file( ABSPATH . ltrim( $exclusion, '/' ) ) ) {
  245. pb_backupbuddy::status( 'details', 'Excluding file `' . $exclusion . '`.' );
  246. // SYMBOLIC LINK.
  247. } elseif ( is_link( ABSPATH . ltrim( $exclusion, '/' ) ) ) {
  248. pb_backupbuddy::status( 'details', 'Excluding symbolic link `' . $exclusion . '`.' );
  249. // DOES NOT EXIST.
  250. } else { // File does not exist. Skip using it in exclusion list.
  251. pb_backupbuddy::status( 'details', 'Omitting exclusion as file/direcory does not currently exist: `' . $exclusion . '`.' );
  252. unset( $exclusion ); // Remove.
  253. }
  254. }
  255. $exclusions = implode( "\n", $exclusions );
  256. file_put_contents( $file, $exclusions );
  257. pb_backupbuddy::status( 'details', 'Backup exclusions file created.' );
  258. } // End render_exclusions_file().
  259. /**
  260. * is_available()
  261. *
  262. * A function that tests for the availability of the specific method in the requested mode
  263. *
  264. * @param string $tempdir Temporary directory to use for any test files (must be writeable)
  265. * @param string $mode Method mode to test for
  266. * @param array $status Array for any status messages
  267. * @return bool True if the method/mode combination is available, false otherwise
  268. *
  269. */
  270. abstract public function is_available( $tempdir, $mode, &$status );
  271. /**
  272. * create()
  273. *
  274. * A function that creates an archive file
  275. *
  276. * The $excludes will be a list or relative path excludes if the $listmaker object is NULL otehrwise
  277. * will be absolute path excludes and relative path excludes can be had from the $listmaker object
  278. *
  279. * @param string $zip Full path & filename of ZIP Archive file to create
  280. * @param string $dir Full path of directory to add to ZIP Archive file
  281. * @param bool $compression True to enable compression of files added to ZIP Archive file
  282. * @parame array $excludes List of either absolute path exclusions or relative exclusions
  283. * @param string $tempdir Full path of directory for temporary usage
  284. * @param object $listmaker The object from which we can get an inclusions list
  285. * @return bool True if the creation was successful, false otherwise
  286. *
  287. */
  288. abstract public function create( $zip, $dir, $compression, $excludes, $tempdir, $listmaker = NULL );
  289. } // end pluginbuddy_zbzipcore class.
  290. }
  291. ?>