PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezc/Webdav/src/plugins/lock/main.php

https://bitbucket.org/crevillo/enetcall
PHP | 445 lines | 193 code | 31 blank | 221 comment | 12 complexity | 1242b8dad493b99da94ed251568a0890 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the ezcWebdavLockPlugin class.
  4. *
  5. * @package Webdav
  6. * @version //autogentag//
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. *
  10. * @access private
  11. */
  12. /**
  13. * Main class of the lock plugin.
  14. *
  15. * This class is responsible to dispatch all actions of the lock plugin and to
  16. * instantiate all necessary objects.
  17. *
  18. * @package Webdav
  19. * @version //autogen//
  20. *
  21. * @property ezcWebdavLockTransport $transport
  22. * The transport class to parse the LOCK and UNLOCK requests and to
  23. * process the corresponding responses.
  24. * @property ezcWebdavLockPropertyHandler $propertyHandler
  25. * Property handler to handle parsing and serializing of lock related
  26. * properties.
  27. * @property ezcWebdavLockHeaderHandler $headerHandler
  28. * Header handler to parse lock related headers.
  29. *
  30. * @access private
  31. */
  32. class ezcWebdavLockPlugin
  33. {
  34. /**
  35. * Namespace of the LOCK plugin.
  36. */
  37. const PLUGIN_NAMESPACE = 'ezcWebdavLockPlugin';
  38. /**
  39. * XML namespace for properties.
  40. */
  41. const XML_NAMESPACE = 'http://ezcomponents.org/s/Webdav#lock';
  42. /**
  43. * Properties.
  44. *
  45. * @var array(string=>mixed)
  46. */
  47. protected $properties = array(
  48. 'transport' => null,
  49. 'propertyHandler' => null,
  50. 'headerHandler' => null,
  51. );
  52. /**
  53. * Maps request classes to handling methods.
  54. *
  55. * @var array(string=>string)
  56. */
  57. protected static $requestHandlingMap = array(
  58. 'ezcWebdavLockRequest' => 'ezcWebdavLockLockRequestResponseHandler',
  59. 'ezcWebdavUnlockRequest' => 'ezcWebdavLockUnlockRequestResponseHandler',
  60. 'ezcWebdavCopyRequest' => 'ezcWebdavLockCopyRequestResponseHandler',
  61. 'ezcWebdavDeleteRequest' => 'ezcWebdavLockDeleteRequestResponseHandler',
  62. 'ezcWebdavMoveRequest' => 'ezcWebdavLockMoveRequestResponseHandler',
  63. 'ezcWebdavMakeCollectionRequest' => 'ezcWebdavLockMakeCollectionRequestResponseHandler',
  64. 'ezcWebdavOptionsRequest' => 'ezcWebdavLockOptionsRequestResponseHandler',
  65. 'ezcWebdavPropFindRequest' => 'ezcWebdavLockPropFindRequestResponseHandler',
  66. 'ezcWebdavPropPatchRequest' => 'ezcWebdavLockPropPatchRequestResponseHandler',
  67. 'ezcWebdavPutRequest' => 'ezcWebdavLockPutRequestResponseHandler',
  68. );
  69. /**
  70. * Lock plugin options.
  71. *
  72. * @var ezcWebdavLockPluginOptions
  73. */
  74. protected $options;
  75. /**
  76. * Lock transport.
  77. *
  78. * @var ezcWebdavLockTransport
  79. */
  80. protected $transport;
  81. /**
  82. * Lock property handler.
  83. *
  84. * @var ezcWebdavLockPropertyHandler
  85. */
  86. protected $propertyHandler;
  87. /**
  88. * Lock header handler.
  89. *
  90. * @var ezcWebdavLockHeaderHandler
  91. */
  92. protected $headerHandler;
  93. /**
  94. * Request / response handler.
  95. *
  96. * @var ezcWebdavLockRequestResponseHandler
  97. */
  98. protected $handler;
  99. /**
  100. * Creates the objects needed for dispatching the hooks.
  101. *
  102. * Can optionally receive $options to influence the behavior of the lock
  103. * plugin.
  104. *
  105. * @param ezcWebdavLockPluginOptions $options
  106. */
  107. public function __construct( ezcWebdavLockPluginOptions $options )
  108. {
  109. $this->options = $options;
  110. $this->headerHandler = new ezcWebdavLockHeaderHandler();
  111. $this->propertyHandler = new ezcWebdavLockPropertyHandler();
  112. $this->transport = new ezcWebdavLockTransport(
  113. $this->headerHandler,
  114. $this->propertyHandler
  115. );
  116. }
  117. /**
  118. * Callback for the hook ezcWebdavTransport::parseUnknownRequest().
  119. *
  120. * This method is attached to the specified hook through {@link
  121. * ezcWebdavLockPluginConfiguration}.
  122. *
  123. * Parameters are:
  124. * - string path
  125. * - string body
  126. * - string requestUri
  127. *
  128. * Reacts on the LOCK and UNLOCK request methods.
  129. *
  130. * @param ezcWebdavPluginParameters $params
  131. * @return ezcWebdavRequest|null
  132. */
  133. public function parseUnknownRequest( ezcWebdavPluginParameters $params )
  134. {
  135. return $this->transport->parseRequest(
  136. $params['requestMethod'],
  137. $params['path'],
  138. $params['body']
  139. );
  140. }
  141. /**
  142. * Callback for the hook ezcWebdavTransport::handleUnknownResponse().
  143. *
  144. * Parameters are:
  145. * - ezcWebdavResponse response
  146. *
  147. * @param ezcWebdavPluginParameters $params
  148. * @return ezcWebdavDisplayInformation
  149. */
  150. public function processUnknownResponse( ezcWebdavPluginParameters $params )
  151. {
  152. return $this->transport->processResponse( $params['response'] );
  153. }
  154. /**
  155. * Callback for the hook ezcWebdavPropertyHandler::extractUnknownLiveProperty().
  156. *
  157. * Parameters are:
  158. * - DOMElement domElement
  159. * - ezcWebdavXmlTool xmlTool
  160. *
  161. * @param ezcWebdavPluginParameters $params
  162. * @return void
  163. */
  164. public function extractUnknownLiveProperty( ezcWebdavPluginParameters $params )
  165. {
  166. return $this->propertyHandler->extractLiveProperty(
  167. $params['domElement'],
  168. $params['xmlTool']
  169. );
  170. }
  171. /**
  172. * Callback for the hook ezcWebdavPropertyHandler::serializeUnknownLiveProperty().;
  173. *
  174. * Parameters are:
  175. * - ezcWebdavLiveProperty property
  176. * - ezcWebdavTransport xmlTool
  177. * - DOMElement parentElement
  178. *
  179. * @param ezcWebdavPluginParameters $params
  180. * @return void
  181. */
  182. public function serializeUnknownLiveProperty( ezcWebdavPluginParameters $params )
  183. {
  184. return $this->propertyHandler->serializeLiveProperty(
  185. $params['property'],
  186. $params['parentElement'],
  187. $params['xmlTool']
  188. );
  189. }
  190. /**
  191. * Callback for the hook ezcWebdavPropertyHandler::extractDeadProperty().
  192. *
  193. * Parameters are:
  194. * - DOMElement domElement
  195. * - ezcWebdavXmlTool xmlTool
  196. *
  197. * @param ezcWebdavPluginParameters $params
  198. * @return ezcWebdavDeadProperty|null
  199. */
  200. public function extractDeadProperty( ezcWebdavPluginParameters $params )
  201. {
  202. // Check namespace before bothering property handler
  203. if ( $params['domElement']->namespaceURI !== ezcWebdavLockPlugin::XML_NAMESPACE )
  204. {
  205. return;
  206. }
  207. return $this->propertyHandler->extractDeadProperty(
  208. $params['domElement'],
  209. $params['xmlTool']
  210. );
  211. }
  212. /**
  213. * Callback for the hook ezcWebdavPropertyHandler::serializeDeadProperty().
  214. *
  215. * Parameters are:
  216. * - ezcWebdavDeadProperty property
  217. * - ezcWebdavXmlTool xmlTool
  218. *
  219. * @param ezcWebdavPluginParameters $params
  220. * @return DOMElement|null
  221. */
  222. public function serializeDeadProperty( ezcWebdavPluginParameters $params )
  223. {
  224. return $this->propertyHandler->serializeDeadProperty(
  225. $params['property'],
  226. $params['xmlTool']
  227. );
  228. }
  229. /**
  230. * Callback for the hook ezcWebdavServer::receivedRequest().
  231. *
  232. * Parameters are:
  233. * - ezcWebdavRequest request
  234. *
  235. * Needs to react directly on:
  236. * - ezcWebdavLockRequest
  237. * - ezcWebdavUnlockRequest
  238. *
  239. * Needs to check if lock violations occur on:
  240. * - ezcWebdavCopyRequest
  241. * - ezcWebdavMoveRequest
  242. * - ezcWebdavMakeCollectionRequest
  243. * - ezcWebdavPropPatchRequest
  244. * - ezcWebdavPutRequest
  245. *
  246. * @param ezcWebdavPluginParameters $params
  247. * @return ezcWebdavResponse|null
  248. */
  249. public function receivedRequest( ezcWebdavPluginParameters $params )
  250. {
  251. $request = $params['request'];
  252. $requestClass = get_class( $request );
  253. if ( isset( ezcWebdavLockPlugin::$requestHandlingMap[$requestClass] ) )
  254. {
  255. // Set headers parsed by the lock plugin only.
  256. $request->setHeader(
  257. 'If',
  258. $this->headerHandler->parseIfHeader( $request )
  259. );
  260. $request->setHeader(
  261. 'Timeout',
  262. $this->headerHandler->parseTimeoutHeader( $request )
  263. );
  264. $request->setHeader(
  265. 'Lock-Token',
  266. $this->headerHandler->parseLockTokenHeader( $request )
  267. );
  268. $request->validateHeaders();
  269. $handlerClass = ezcWebdavLockPlugin::$requestHandlingMap[$requestClass];
  270. $this->handler = new $handlerClass(
  271. new ezcWebdavLockTools( $this->options )
  272. );
  273. if ( $this->handler->needsBackendLock )
  274. {
  275. ezcWebdavServer::getInstance()->backend->lock(
  276. $this->options->backendLockWaitTime,
  277. $this->options->backendLockTimeout
  278. );
  279. }
  280. $res = null;
  281. try
  282. {
  283. $res = $this->handler->receivedRequest( $request );
  284. }
  285. catch ( Exception $e )
  286. {
  287. if ( $this->handler->needsBackendLock )
  288. {
  289. ezcWebdavServer::getInstance()->backend->unlock();
  290. }
  291. throw $e;
  292. }
  293. return $res;
  294. }
  295. // return null
  296. }
  297. /**
  298. * Handles responses generated by the backend.
  299. *
  300. * @param ezcWebdavPluginParameters $params
  301. * @return ezcWebdavResponse|null
  302. */
  303. public function generatedResponse( ezcWebdavPluginParameters $params )
  304. {
  305. if ( isset( $this->handler ) )
  306. {
  307. $res = null;
  308. try
  309. {
  310. $res = $this->handler->generatedResponse( $params['response'] );
  311. }
  312. catch ( Exception $e )
  313. {
  314. if ( $this->handler->needsBackendLock )
  315. {
  316. ezcWebdavServer::getInstance()->backend->unlock();
  317. }
  318. throw $e;
  319. }
  320. if ( $this->handler->needsBackendLock )
  321. {
  322. ezcWebdavServer::getInstance()->backend->unlock();
  323. }
  324. return $res;
  325. }
  326. }
  327. //
  328. //
  329. // Property access
  330. //
  331. //
  332. /**
  333. * Sets a property.
  334. *
  335. * This method is called when an property is to be set.
  336. *
  337. * @param string $propertyName The name of the property to set.
  338. * @param mixed $propertyValue The property value.
  339. * @return void
  340. * @ignore
  341. *
  342. * @throws ezcBasePropertyNotFoundException
  343. * if the given property does not exist.
  344. * @throws ezcBaseValueException
  345. * if the value to be assigned to a property is invalid.
  346. * @throws ezcBasePropertyPermissionException
  347. * if the property to be set is a read-only property.
  348. */
  349. public function __set( $propertyName, $propertyValue )
  350. {
  351. switch ( $propertyName )
  352. {
  353. case 'transport':
  354. if ( !( $propertyValue instanceof ezcWebdavLockTransport ) )
  355. {
  356. throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcWebdavLockTransport' );
  357. }
  358. break;
  359. case 'propertyHandler':
  360. if ( !( $propertyValue instanceof ezcWebdavLockPropertyHandler ) )
  361. {
  362. throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcWebdavLockPropertyHandler' );
  363. }
  364. break;
  365. case 'headerHandler':
  366. if ( !( $propertyValue instanceof ezcWebdavLockHeaderHandler ) )
  367. {
  368. throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcWebdavLockPropertyHandler' );
  369. }
  370. break;
  371. default:
  372. throw new ezcBasePropertyNotFoundException( $propertyName );
  373. }
  374. $this->properties[$propertyName] = $propertyValue;
  375. }
  376. /**
  377. * Property get access.
  378. *
  379. * Simply returns a given property.
  380. *
  381. * @param string $propertyName The name of the property to get.
  382. * @return mixed The property value.
  383. *
  384. * @ignore
  385. *
  386. * @throws ezcBasePropertyNotFoundException
  387. * if the given property does not exist.
  388. * @throws ezcBasePropertyPermissionException
  389. * if the property to be set is a write-only property.
  390. */
  391. public function __get( $propertyName )
  392. {
  393. if ( $this->__isset( $propertyName ) )
  394. {
  395. return $this->properties[$propertyName];
  396. }
  397. throw new ezcBasePropertyNotFoundException( $propertyName );
  398. }
  399. /**
  400. * Returns if a property exists.
  401. *
  402. * Returns true if the property exists in the {@link $properties} array
  403. * (even if it is null) and false otherwise.
  404. *
  405. * @param string $propertyName Option name to check for.
  406. * @return void
  407. * @ignore
  408. */
  409. public function __isset( $propertyName )
  410. {
  411. return array_key_exists( $propertyName, $this->properties );
  412. }
  413. }
  414. ?>