PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/files/mapper.php

https://github.com/sezuan/core
PHP | 239 lines | 150 code | 41 blank | 48 comment | 19 complexity | 2b14bd8c9c478f32f03286b6345cef12 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. namespace OC\Files;
  3. /**
  4. * class Mapper is responsible to translate logical paths to physical paths and reverse
  5. */
  6. class Mapper
  7. {
  8. private $unchangedPhysicalRoot;
  9. public function __construct($rootDir) {
  10. $this->unchangedPhysicalRoot = $rootDir;
  11. }
  12. /**
  13. * @param string $logicPath
  14. * @param bool $create indicates if the generated physical name shall be stored in the database or not
  15. * @return string the physical path
  16. */
  17. public function logicToPhysical($logicPath, $create) {
  18. $physicalPath = $this->resolveLogicPath($logicPath);
  19. if ($physicalPath !== null) {
  20. return $physicalPath;
  21. }
  22. return $this->create($logicPath, $create);
  23. }
  24. /**
  25. * @param string $physicalPath
  26. * @return string
  27. */
  28. public function physicalToLogic($physicalPath) {
  29. $logicPath = $this->resolvePhysicalPath($physicalPath);
  30. if ($logicPath !== null) {
  31. return $logicPath;
  32. }
  33. $this->insert($physicalPath, $physicalPath);
  34. return $physicalPath;
  35. }
  36. /**
  37. * @param string $path
  38. * @param bool $isLogicPath indicates if $path is logical or physical
  39. * @param $recursive
  40. * @return void
  41. */
  42. public function removePath($path, $isLogicPath, $recursive) {
  43. if ($recursive) {
  44. $path=$path.'%';
  45. }
  46. if ($isLogicPath) {
  47. \OC_DB::executeAudited('DELETE FROM `*PREFIX*file_map` WHERE `logic_path` LIKE ?', array($path));
  48. } else {
  49. \OC_DB::executeAudited('DELETE FROM `*PREFIX*file_map` WHERE `physic_path` LIKE ?', array($path));
  50. }
  51. }
  52. /**
  53. * @param $path1
  54. * @param $path2
  55. * @throws \Exception
  56. */
  57. public function copy($path1, $path2)
  58. {
  59. $path1 = $this->stripLast($path1);
  60. $path2 = $this->stripLast($path2);
  61. $physicPath1 = $this->logicToPhysical($path1, true);
  62. $physicPath2 = $this->logicToPhysical($path2, true);
  63. $sql = 'SELECT * FROM `*PREFIX*file_map` WHERE `logic_path` LIKE ?';
  64. $result = \OC_DB::executeAudited($sql, array($path1.'%'));
  65. $updateQuery = \OC_DB::prepare('UPDATE `*PREFIX*file_map`'
  66. .' SET `logic_path` = ?'
  67. .' , `logic_path_hash` = ?'
  68. .' , `physic_path` = ?'
  69. .' , `physic_path_hash` = ?'
  70. .' WHERE `logic_path` = ?');
  71. while( $row = $result->fetchRow()) {
  72. $currentLogic = $row['logic_path'];
  73. $currentPhysic = $row['physic_path'];
  74. $newLogic = $path2.$this->stripRootFolder($currentLogic, $path1);
  75. $newPhysic = $physicPath2.$this->stripRootFolder($currentPhysic, $physicPath1);
  76. if ($path1 !== $currentLogic) {
  77. try {
  78. \OC_DB::executeAudited($updateQuery, array($newLogic, md5($newLogic), $newPhysic, md5($newPhysic),
  79. $currentLogic));
  80. } catch (\Exception $e) {
  81. error_log('Mapper::Copy failed '.$currentLogic.' -> '.$newLogic.'\n'.$e);
  82. throw $e;
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * @param $path
  89. * @param $root
  90. * @return bool|string
  91. */
  92. public function stripRootFolder($path, $root) {
  93. if (strpos($path, $root) !== 0) {
  94. // throw exception ???
  95. return false;
  96. }
  97. if (strlen($path) > strlen($root)) {
  98. return substr($path, strlen($root));
  99. }
  100. return '';
  101. }
  102. private function stripLast($path) {
  103. if (substr($path, -1) == '/') {
  104. $path = substr_replace($path, '', -1);
  105. }
  106. return $path;
  107. }
  108. private function resolveLogicPath($logicPath) {
  109. $logicPath = $this->stripLast($logicPath);
  110. $sql = 'SELECT * FROM `*PREFIX*file_map` WHERE `logic_path_hash` = ?';
  111. $result = \OC_DB::executeAudited($sql, array(md5($logicPath)));
  112. $result = $result->fetchRow();
  113. if ($result === false) {
  114. return null;
  115. }
  116. return $result['physic_path'];
  117. }
  118. private function resolvePhysicalPath($physicalPath) {
  119. $physicalPath = $this->stripLast($physicalPath);
  120. $sql = \OC_DB::prepare('SELECT * FROM `*PREFIX*file_map` WHERE `physic_path_hash` = ?');
  121. $result = \OC_DB::executeAudited($sql, array(md5($physicalPath)));
  122. $result = $result->fetchRow();
  123. return $result['logic_path'];
  124. }
  125. private function create($logicPath, $store) {
  126. $logicPath = $this->stripLast($logicPath);
  127. $index = 0;
  128. // create the slugified path
  129. $physicalPath = $this->slugifyPath($logicPath);
  130. // detect duplicates
  131. while ($this->resolvePhysicalPath($physicalPath) !== null) {
  132. $physicalPath = $this->slugifyPath($logicPath, $index++);
  133. }
  134. // insert the new path mapping if requested
  135. if ($store) {
  136. $this->insert($logicPath, $physicalPath);
  137. }
  138. return $physicalPath;
  139. }
  140. private function insert($logicPath, $physicalPath) {
  141. $sql = 'INSERT INTO `*PREFIX*file_map` (`logic_path`, `physic_path`, `logic_path_hash`, `physic_path_hash`)
  142. VALUES (?, ?, ?, ?)';
  143. \OC_DB::executeAudited($sql, array($logicPath, $physicalPath, md5($logicPath), md5($physicalPath)));
  144. }
  145. public function slugifyPath($path, $index=null) {
  146. $path = $this->stripRootFolder($path, $this->unchangedPhysicalRoot);
  147. $pathElements = explode('/', $path);
  148. $sluggedElements = array();
  149. $last= end($pathElements);
  150. foreach ($pathElements as $pathElement) {
  151. // remove empty elements
  152. if (empty($pathElement)) {
  153. continue;
  154. }
  155. $sluggedElements[] = self::slugify($pathElement);
  156. }
  157. // apply index to file name
  158. if ($index !== null) {
  159. $last= array_pop($sluggedElements);
  160. // if filename contains periods - add index number before last period
  161. if (preg_match('~\.[^\.]+$~i',$last,$extension)){
  162. array_push($sluggedElements, substr($last,0,-(strlen($extension[0]))).'-'.$index.$extension[0]);
  163. } else {
  164. // if filename doesn't contain periods add index ofter the last char
  165. array_push($sluggedElements, $last.'-'.$index);
  166. }
  167. }
  168. $sluggedPath = $this->unchangedPhysicalRoot.implode('/', $sluggedElements);
  169. return $this->stripLast($sluggedPath);
  170. }
  171. /**
  172. * Modifies a string to remove all non ASCII characters and spaces.
  173. *
  174. * @param string $text
  175. * @return string
  176. */
  177. private function slugify($text)
  178. {
  179. // replace non letter or digits or dots by -
  180. $text = preg_replace('~[^\\pL\d\.]+~u', '-', $text);
  181. // trim
  182. $text = trim($text, '-');
  183. // transliterate
  184. if (function_exists('iconv')) {
  185. $text = iconv('utf-8', 'us-ascii//TRANSLIT//IGNORE', $text);
  186. }
  187. // lowercase
  188. $text = strtolower($text);
  189. // remove unwanted characters
  190. $text = preg_replace('~[^-\w\.]+~', '', $text);
  191. // trim ending dots (for security reasons and win compatibility)
  192. $text = preg_replace('~\.+$~', '', $text);
  193. if (empty($text)) {
  194. return uniqid();
  195. }
  196. return $text;
  197. }
  198. }