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

/blog/core/xpdo/transport/xpdofilevehicle.class.php

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 235 lines | 170 code | 7 blank | 58 comment | 60 complexity | 8d29f9b35da91b05fb44624386757c70 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2010-2013 by MODX, LLC.
  4. *
  5. * This file is part of xPDO.
  6. *
  7. * xPDO is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * xPDO is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * xPDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * Defines a class that represents a fileset within a transportable package.
  22. *
  23. * @package xpdo
  24. * @subpackage transport
  25. */
  26. /**
  27. * Represents an individual file set within an {@link xPDOTransport} package.
  28. *
  29. * @package xpdo
  30. * @subpackage transport
  31. */
  32. class xPDOFileVehicle extends xPDOVehicle {
  33. public $class = 'xPDOFileVehicle';
  34. /**
  35. * Install a file set represented by and stored in this vehicle.
  36. */
  37. public function install(& $transport, $options) {
  38. $installed = $this->_installFiles($transport, $options);
  39. return $installed;
  40. }
  41. /**
  42. * Uninstall a file set represented by and stored in this vehicle.
  43. */
  44. public function uninstall(& $transport, $options) {
  45. $uninstalled = $this->_uninstallFiles($transport, $options);
  46. return $uninstalled;
  47. }
  48. /**
  49. * Install files or folders represented by and stored in this vehicle.
  50. *
  51. * @access protected
  52. * @param xPDOTransport &$transport A reference the transport this vehicle is stored in.
  53. * @param array $options Optional attributes that can be applied to vehicle install process.
  54. * @return boolean True if the files are installed successfully.
  55. */
  56. protected function _installFiles(& $transport, $options) {
  57. $installed = false;
  58. $copied = false;
  59. $vOptions = $this->get($transport, $options);
  60. if (isset ($vOptions['object']) && isset ($vOptions['object']['source']) && isset ($vOptions['object']['target'])) {
  61. $object = $vOptions['object'];
  62. $fileName = $object['name'];
  63. $fileSource = $transport->path . $object['source'];
  64. $fileTarget = eval ($object['target']);
  65. $fileTargetPath = $fileTarget . $fileName;
  66. $preExistingMode = xPDOTransport::PRESERVE_PREEXISTING;
  67. if (isset ($vOptions[xPDOTransport::PREEXISTING_MODE])) {
  68. $preExistingMode = (integer) $vOptions[xPDOTransport::PREEXISTING_MODE];
  69. }
  70. $cacheManager = $transport->xpdo->getCacheManager();
  71. if ($this->validate($transport, $object, $vOptions)) {
  72. if (isset ($vOptions[xPDOTransport::INSTALL_FILES]) && !$vOptions[xPDOTransport::INSTALL_FILES]) {
  73. $transport->xpdo->log(xPDO::LOG_LEVEL_INFO, "Skipping installion of files from {$fileSource} to {$fileTargetPath}");
  74. $installed = true;
  75. } elseif ($cacheManager && file_exists($fileSource) && !empty ($fileTarget)) {
  76. $transport->xpdo->log(xPDO::LOG_LEVEL_INFO, "Installing files from {$fileSource} to {$fileTargetPath}");
  77. $copied = array();
  78. if ($preExistingMode === xPDOTransport::PRESERVE_PREEXISTING && file_exists($fileTargetPath)) {
  79. $preservedArchive = $transport->path . $transport->signature . '/' . $this->payload['class'] . '/' . $this->payload['signature'] . '.preserved.zip';
  80. $transport->xpdo->log(xPDO::LOG_LEVEL_INFO, "Attempting to preserve files at {$fileTargetPath} into archive {$preservedArchive}");
  81. $preserved = xPDOTransport::_pack($transport->xpdo, $preservedArchive, $fileTarget, $fileName);
  82. }
  83. if (is_dir($fileSource)) {
  84. $copied = $cacheManager->copyTree($fileSource, $fileTarget, array_merge($vOptions, array('copy_return_file_stat' => true)));
  85. } elseif (is_file($fileSource)) {
  86. $copied = $cacheManager->copyFile($fileSource, $fileTarget, array_merge($vOptions, array('copy_return_file_stat' => true)));
  87. }
  88. if (empty($copied)) {
  89. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error copying files from {$fileSource} to {$fileTargetPath}");
  90. } else {
  91. if ($preExistingMode === xPDOTransport::PRESERVE_PREEXISTING && is_array($copied)) {
  92. foreach ($copied as $copiedFile => $stat) {
  93. if (isset($stat['overwritten'])) $transport->_preserved[$vOptions['guid']]['files'][$copiedFile]= $stat;
  94. }
  95. }
  96. $installed = true;
  97. }
  98. } else {
  99. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not install files from {$fileSource} to {$fileTarget}");
  100. }
  101. if (!$this->resolve($transport, $object, $vOptions)) {
  102. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not resolve vehicle for object: ' . print_r($object, true));
  103. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not resolve vehicle: ' . print_r($vOptions, true));
  104. }
  105. } else {
  106. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not validate vehicle for object: ' . print_r($object, true));
  107. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not validate vehicle: ' . print_r($vOptions, true));
  108. }
  109. }
  110. return $installed;
  111. }
  112. /**
  113. * Uninstall files or folders represented by and stored in this vehicle.
  114. *
  115. * @access protected
  116. * @param xPDOTransport &$transport A reference the transport this vehicle is stored in.
  117. * @param array $options Optional attributes that can be applied to vehicle uninstall process.
  118. * @return boolean True if the files are uninstalled successfully.
  119. */
  120. protected function _uninstallFiles(& $transport, $options) {
  121. $uninstalled = false;
  122. $vOptions = $this->get($transport, $options);
  123. if (isset ($vOptions['object']) && isset ($vOptions['object']['source']) && isset ($vOptions['object']['target'])) {
  124. $object = $vOptions['object'];
  125. $fileName = $object['name'];
  126. $fileSource = $transport->path . $object['source'];
  127. $fileTarget = eval ($object['target']);
  128. $preExistingMode = xPDOTransport::PRESERVE_PREEXISTING;
  129. if (isset ($vOptions[xPDOTransport::PREEXISTING_MODE])) {
  130. $preExistingMode = (integer) $vOptions[xPDOTransport::PREEXISTING_MODE];
  131. }
  132. $cacheManager = $transport->xpdo->getCacheManager();
  133. $path = $fileTarget . $fileName;
  134. $transport->xpdo->log(xPDO::LOG_LEVEL_INFO, 'Uninstalling files from xPDOFileVehicle: ' . $path);
  135. if ($this->validate($transport, $object, $vOptions)) {
  136. if (!isset ($vOptions[xPDOTransport::UNINSTALL_FILES]) || $vOptions[xPDOTransport::UNINSTALL_FILES] == true) {
  137. $transport->xpdo->log(xPDO::LOG_LEVEL_INFO,'Removing files from xPDOFileVehicle: '.$path);
  138. if ($cacheManager && file_exists($path)) {
  139. if (is_dir($path) && $cacheManager->deleteTree($path, array_merge(array('deleteTop' => true, 'skipDirs' => false, 'extensions' => array()), $vOptions))) {
  140. $uninstalled = true;
  141. } elseif (is_file($path) && unlink($path)) {
  142. $uninstalled = true;
  143. } else {
  144. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR,'Could not remove files from path: '.$path);
  145. }
  146. } else {
  147. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR,'Could not find files to remove at path: '.$path);
  148. }
  149. } else {
  150. $transport->xpdo->log(xPDO::LOG_LEVEL_INFO,'Skipping removal of files according to vehicle attributes.');
  151. $uninstalled = true;
  152. }
  153. $preservedArchive = $transport->path . $transport->signature . '/' . $this->payload['class'] . '/' . $this->payload['signature'] . '.preserved.zip';
  154. if ($preExistingMode === xPDOTransport::RESTORE_PREEXISTING && file_exists($preservedArchive)) {
  155. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Attempting to restore files to {$fileTarget} from archive {$preservedArchive}");
  156. $unpackedResult = xPDOTransport::_unpack($transport->xpdo, $preservedArchive, $fileTarget);
  157. if ($unpackedResult > 0) {
  158. $uninstalled = true;
  159. } else {
  160. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Error unpacking preserved files from archive {$preservedArchive}");
  161. }
  162. }
  163. if (!$this->resolve($transport, $object, $vOptions)) {
  164. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not resolve vehicle for object: ' . print_r($object, true));
  165. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not resolve vehicle: ' . print_r($vOptions, true));
  166. }
  167. } else {
  168. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Could not validate vehicle for object: ' . print_r($object, true));
  169. if ($transport->xpdo->getDebug() === true) $transport->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Could not validate vehicle: ' . print_r($vOptions, true));
  170. }
  171. }
  172. return $uninstalled;
  173. }
  174. /**
  175. * Adds the file definition object to the payload.
  176. */
  177. public function put(& $transport, & $object, $attributes = array ()) {
  178. if (!isset ($this->payload['class'])) {
  179. $this->payload['class'] = 'xPDOFileVehicle';
  180. }
  181. if (is_array($object) && isset ($object['source']) && isset ($object['target'])) {
  182. if (!isset($object['name'])) $object['name'] = basename($object['source']);
  183. $this->payload['object'] = $object;
  184. }
  185. parent :: put($transport, $object, $attributes);
  186. }
  187. /**
  188. * Copies the files into the vehicle and transforms the payload for storage.
  189. */
  190. protected function _compilePayload(& $transport) {
  191. parent :: _compilePayload($transport);
  192. $body = array ();
  193. $cacheManager = $transport->xpdo->getCacheManager();
  194. if ($cacheManager) {
  195. if (isset($this->payload['object'])) {
  196. $object = $this->payload['object'];
  197. $fileSource = $object['source'];
  198. $body['source'] = $transport->signature . '/' . $this->payload['class'] . '/' . $this->payload['signature'] . '/';
  199. $fileTarget = $transport->path . $body['source'];
  200. $body['target'] = $object['target'];
  201. $fileName = isset ($object['name']) ? $object['name'] : basename($fileSource);
  202. $body['name'] = $fileName;
  203. if (!is_writable($fileTarget)) {
  204. $cacheManager->writeTree($fileTarget);
  205. }
  206. if (file_exists($fileSource) && is_writable($fileTarget)) {
  207. $copied = false;
  208. if (is_dir($fileSource)) {
  209. $copied = $cacheManager->copyTree($fileSource, $fileTarget . $fileName);
  210. }
  211. elseif (is_file($fileSource)) {
  212. $copied = $cacheManager->copyFile($fileSource, $fileTarget . $fileName);
  213. }
  214. if (!$copied) {
  215. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not copy file from {$fileSource} to {$fileTarget}{$fileName}");
  216. $body = null;
  217. }
  218. } else {
  219. $transport->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Source file {$fileSource} is missing or {$fileTarget} is not writable");
  220. $body = null;
  221. }
  222. }
  223. }
  224. if (!empty($body)) {
  225. $this->payload['object'] = $body;
  226. }
  227. }
  228. }