AJAX_Locking
[ class tree: AJAX_Locking ] [ index: AJAX_Locking ] [ all elements ]

Source for file File.php

Documentation is available at File.php

  1. <?php
  2. require_once('Driver.php');
  3. require_once('System/SharedMemory.php');
  4.  
  5. /**
  6. * A driver that use plain filesystem as data store
  7. */
  8. class AJAX_Locking_Driver_File extends AJAX_Locking_Driver
  9. {
  10. var $dir;
  11.  
  12. /**
  13. * Constructor
  14. *
  15. * @return AJAX_Locking_Driver
  16. */
  17. function AJAX_Locking_Driver_File($dir = '/tmp', $timeout = false)
  18. {
  19. parent::AJAX_Locking_Driver($timeout);
  20.  
  21. $this->dir = realpath($dir);
  22. }
  23.  
  24. /**
  25. * Locks an object
  26. *
  27. * @param mixed $user id of the user who wants to lock
  28. * @param string $type type/classname of the object
  29. * @param mixed $id id of the object
  30. * @return boolean true if the unlock was successfull, false otherwise
  31. */
  32. function lock($user, $type, $id)
  33. {
  34. $key = $this->_getKey($type, $id);
  35.  
  36. $value = $this->_get($key);
  37. if (empty($value)) {
  38. // object is unlocked: lock it
  39. $value = $this->_getValue($user, $type, $id);
  40. $this->_set($key, $value);
  41. return true;
  42. } else {
  43. // object is locked: check if lock expired
  44. list($owner, $type, $id, $time) = $this->_parseValue($value);
  45. if (time() - $time > $this->timeout) {
  46. // lock expired: remove it and lock
  47. $this->_rm($key);
  48. $this->_set($key, $value);
  49. return true;
  50. } else {
  51. // object properly locked by (another?) user
  52. return ($owner == $user);
  53. }
  54. }
  55.  
  56. }
  57.  
  58. /**
  59. * Unlocks an object
  60. *
  61. * @param mixed $user id of the user who wants to unlock
  62. * @param string $type type/classname of the object
  63. * @param mixed $id id of the object
  64. * @return boolean true if the unlock was successfull, false otherwise
  65. */
  66. function unlock($user, $type, $id)
  67. {
  68. $key = $this->_getKey($type, $id);
  69. $value = $this->_get($key);
  70. if (empty($value)) {
  71. // object was not locked
  72. return false;
  73. } else {
  74. // check if user is owner of the lock
  75. list($owner, $type, $id, $time) = $this->_parseValue($value);
  76. if ($user == $owner) {
  77. // unlock it
  78. $this->_rm($key);
  79. return true;
  80. } else {
  81. // user cannot unlock it
  82. return false;
  83. }
  84. }
  85. }
  86.  
  87. /**
  88. * Returns the status of the object (lock or unlocked)
  89. *
  90. * @param mixed $user id of the user who wants to know the object's status
  91. * @param string $type type/classname of the object
  92. * @param mixed $id id of the object
  93. * @return string the status and the owner of the object
  94. */
  95. function status($user, $type, $id)
  96. {
  97. $key = $this->_getKey($type, $id);
  98. $value = $this->_get($key);
  99. if (empty($value)) {
  100. // object is unlocked
  101. return AJAX_LOCKING_UNLOCKED . '~' . 'noboby';
  102. } else {
  103. // object is locked:
  104. // 1. check if current user is owner of the lock
  105. // 2. check if lock expired
  106. list($owner, $type, $id, $time) = $this->_parseValue($value);
  107. $timeout = (time() - $time > $this->timeout);
  108. if ($owner == $user) {
  109. if ($timeout) {
  110. // lock expired: remove it and inform it's ower
  111. $this->_rm($key);
  112. return AJAX_LOCKING_TIMEOUT . '~' . $owner;
  113. } else {
  114. return AJAX_LOCKING_OWNED . '~' . $owner;
  115. }
  116. } else {
  117. if ($timeout) {
  118. // lock expired: remove it
  119. $this->_rm($key);
  120. return AJAX_LOCKING_UNLOCKED . '~' . 'nobody';
  121. } else {
  122. return AJAX_LOCKING_LOCKED . '~' . $owner;
  123. }
  124. }
  125. }
  126. }
  127.  
  128. /**
  129. * Returns the list of all active locks for administration purpose
  130. *
  131. * @return array of locks (owner, type, id), false if not implemented
  132. */
  133. function getLocks()
  134. {
  135. $files = array();
  136. $dh = opendir($this->dir);
  137. while (false !== ($filename = readdir($dh))) {
  138. if (strstr($filename, AJAX_LOCKING_PREFIX) == $filename) {
  139. $files[] = $filename;
  140. }
  141. }
  142.  
  143. $locks = array();
  144. foreach ($files as $f) {
  145. $value = file_get_contents($this->dir . DIRECTORY_SEPARATOR . $f);
  146. if ($value) {
  147. list($owner, $type, $id, $time) = $this->_parseValue($value);
  148. $timeout = (time() - $time > $this->timeout);
  149. if (!$timeout) {
  150. $locks[] = array(
  151. 'owner' => $owner,
  152. 'type' => $type,
  153. 'id' => $id
  154. );
  155. }
  156. }
  157. }
  158.  
  159. return $locks;
  160. }
  161.  
  162. /**
  163. * Administrately delete lock
  164. *
  165. * @param string $type
  166. * @param mixed $id
  167. *
  168. * @return true if the lock is deleted successfully, false otherwise
  169. */
  170. function delete($type, $id)
  171. {
  172. $key = $this->_getKey($type, $id);
  173. $this->_rm($key);
  174. }
  175.  
  176.  
  177. /**
  178. * Returns the values stored in file $key
  179. *
  180. * @param string $key
  181. * @return string
  182. */
  183. function _get($key)
  184. {
  185. $value = '';
  186. $filename = $this->dir . DIRECTORY_SEPARATOR . $key;
  187. if (file_exists($filename)) {
  188. $value = file_get_contents($filename);
  189. }
  190. return $value;
  191. }
  192.  
  193. /**
  194. * Puts the value $value into file $key
  195. *
  196. * @param string $key
  197. * @param string $value
  198. */
  199. function _set($key, $value)
  200. {
  201. $filename = $this->dir . DIRECTORY_SEPARATOR . $key;
  202. $fp = fopen($filename, 'w');
  203. fwrite($fp, $value);
  204. fclose($fp);
  205. }
  206.  
  207. /**
  208. * Remove the file $key
  209. *
  210. * @param string $key
  211. */
  212. function _rm($key)
  213. {
  214. $filename = $this->dir . DIRECTORY_SEPARATOR . $key;
  215. return @unlink($filename);
  216. }
  217.  
  218. }
  219. ?>

Documentation generated on Tue, 13 Feb 2007 21:42:44 +0100 by phpDocumentor 1.3.0RC3