mapper.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 boolean $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 string $path1
  54. * @param string $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 string $path
  89. * @param string $root
  90. * @return false|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. /**
  109. * @param string $logicPath
  110. */
  111. private function resolveLogicPath($logicPath) {
  112. $logicPath = $this->stripLast($logicPath);
  113. $sql = 'SELECT * FROM `*PREFIX*file_map` WHERE `logic_path_hash` = ?';
  114. $result = \OC_DB::executeAudited($sql, array(md5($logicPath)));
  115. $result = $result->fetchRow();
  116. if ($result === false) {
  117. return null;
  118. }
  119. return $result['physic_path'];
  120. }
  121. private function resolvePhysicalPath($physicalPath) {
  122. $physicalPath = $this->stripLast($physicalPath);
  123. $sql = \OC_DB::prepare('SELECT * FROM `*PREFIX*file_map` WHERE `physic_path_hash` = ?');
  124. $result = \OC_DB::executeAudited($sql, array(md5($physicalPath)));
  125. $result = $result->fetchRow();
  126. return $result['logic_path'];
  127. }
  128. /**
  129. * @param string $logicPath
  130. * @param boolean $store
  131. */
  132. private function create($logicPath, $store) {
  133. $logicPath = $this->stripLast($logicPath);
  134. $index = 0;
  135. // create the slugified path
  136. $physicalPath = $this->slugifyPath($logicPath);
  137. // detect duplicates
  138. while ($this->resolvePhysicalPath($physicalPath) !== null) {
  139. $physicalPath = $this->slugifyPath($logicPath, $index++);
  140. }
  141. // insert the new path mapping if requested
  142. if ($store) {
  143. $this->insert($logicPath, $physicalPath);
  144. }
  145. return $physicalPath;
  146. }
  147. private function insert($logicPath, $physicalPath) {
  148. $sql = 'INSERT INTO `*PREFIX*file_map` (`logic_path`, `physic_path`, `logic_path_hash`, `physic_path_hash`)
  149. VALUES (?, ?, ?, ?)';
  150. \OC_DB::executeAudited($sql, array($logicPath, $physicalPath, md5($logicPath), md5($physicalPath)));
  151. }
  152. /**
  153. * @param integer $index
  154. */
  155. public function slugifyPath($path, $index=null) {
  156. $path = $this->stripRootFolder($path, $this->unchangedPhysicalRoot);
  157. $pathElements = explode('/', $path);
  158. $sluggedElements = array();
  159. $last= end($pathElements);
  160. foreach ($pathElements as $pathElement) {
  161. // remove empty elements
  162. if (empty($pathElement)) {
  163. continue;
  164. }
  165. $sluggedElements[] = self::slugify($pathElement);
  166. }
  167. // apply index to file name
  168. if ($index !== null) {
  169. $last= array_pop($sluggedElements);
  170. // if filename contains periods - add index number before last period
  171. if (preg_match('~\.[^\.]+$~i',$last,$extension)){
  172. array_push($sluggedElements, substr($last,0,-(strlen($extension[0]))).'-'.$index.$extension[0]);
  173. } else {
  174. // if filename doesn't contain periods add index ofter the last char
  175. array_push($sluggedElements, $last.'-'.$index);
  176. }
  177. }
  178. $sluggedPath = $this->unchangedPhysicalRoot.implode('/', $sluggedElements);
  179. return $this->stripLast($sluggedPath);
  180. }
  181. /**
  182. * Modifies a string to remove all non ASCII characters and spaces.
  183. *
  184. * @param string $text
  185. * @return string
  186. */
  187. private function slugify($text)
  188. {
  189. // replace non letter or digits or dots by -
  190. $text = preg_replace('~[^\\pL\d\.]+~u', '-', $text);
  191. // trim
  192. $text = trim($text, '-');
  193. // transliterate
  194. if (function_exists('iconv')) {
  195. $text = iconv('utf-8', 'us-ascii//TRANSLIT//IGNORE', $text);
  196. }
  197. // lowercase
  198. $text = strtolower($text);
  199. // remove unwanted characters
  200. $text = preg_replace('~[^-\w\.]+~', '', $text);
  201. // trim ending dots (for security reasons and win compatibility)
  202. $text = preg_replace('~\.+$~', '', $text);
  203. if (empty($text)) {
  204. return uniqid();
  205. }
  206. return $text;
  207. }
  208. }