2013-02-07 02:36:38 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OC\Files;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class Mapper is responsible to translate logical paths to physical paths and reverse
|
|
|
|
*/
|
|
|
|
class Mapper
|
|
|
|
{
|
2013-02-15 20:42:17 +04:00
|
|
|
private $unchangedPhysicalRoot;
|
|
|
|
|
|
|
|
public function __construct($rootDir) {
|
|
|
|
$this->unchangedPhysicalRoot = $rootDir;
|
|
|
|
}
|
|
|
|
|
2013-02-07 02:36:38 +04:00
|
|
|
/**
|
|
|
|
* @param string $logicPath
|
|
|
|
* @param bool $create indicates if the generated physical name shall be stored in the database or not
|
|
|
|
* @return string the physical path
|
|
|
|
*/
|
|
|
|
public function logicToPhysical($logicPath, $create) {
|
|
|
|
$physicalPath = $this->resolveLogicPath($logicPath);
|
|
|
|
if ($physicalPath !== null) {
|
|
|
|
return $physicalPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->create($logicPath, $create);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $physicalPath
|
2013-02-15 20:42:17 +04:00
|
|
|
* @return string
|
2013-02-07 02:36:38 +04:00
|
|
|
*/
|
|
|
|
public function physicalToLogic($physicalPath) {
|
|
|
|
$logicPath = $this->resolvePhysicalPath($physicalPath);
|
|
|
|
if ($logicPath !== null) {
|
|
|
|
return $logicPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->insert($physicalPath, $physicalPath);
|
|
|
|
return $physicalPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param bool $isLogicPath indicates if $path is logical or physical
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param boolean $recursive
|
2013-02-15 20:42:17 +04:00
|
|
|
* @return void
|
2013-02-07 02:36:38 +04:00
|
|
|
*/
|
|
|
|
public function removePath($path, $isLogicPath, $recursive) {
|
|
|
|
if ($recursive) {
|
|
|
|
$path=$path.'%';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isLogicPath) {
|
2013-06-07 16:11:05 +04:00
|
|
|
\OC_DB::executeAudited('DELETE FROM `*PREFIX*file_map` WHERE `logic_path` LIKE ?', array($path));
|
2013-02-07 02:36:38 +04:00
|
|
|
} else {
|
2013-06-07 16:11:05 +04:00
|
|
|
\OC_DB::executeAudited('DELETE FROM `*PREFIX*file_map` WHERE `physic_path` LIKE ?', array($path));
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string $path1
|
|
|
|
* @param string $path2
|
2013-02-07 02:36:38 +04:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function copy($path1, $path2)
|
|
|
|
{
|
2014-09-17 13:36:08 +04:00
|
|
|
$path1 = $this->resolveRelativePath($path1);
|
|
|
|
$path2 = $this->resolveRelativePath($path2);
|
2013-02-07 02:36:38 +04:00
|
|
|
$physicPath1 = $this->logicToPhysical($path1, true);
|
|
|
|
$physicPath2 = $this->logicToPhysical($path2, true);
|
|
|
|
|
2013-06-07 16:11:05 +04:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*file_map` WHERE `logic_path` LIKE ?';
|
|
|
|
$result = \OC_DB::executeAudited($sql, array($path1.'%'));
|
2013-02-07 02:36:38 +04:00
|
|
|
$updateQuery = \OC_DB::prepare('UPDATE `*PREFIX*file_map`'
|
|
|
|
.' SET `logic_path` = ?'
|
2013-03-12 18:30:10 +04:00
|
|
|
.' , `logic_path_hash` = ?'
|
|
|
|
.' , `physic_path` = ?'
|
|
|
|
.' , `physic_path_hash` = ?'
|
2013-02-07 02:36:38 +04:00
|
|
|
.' WHERE `logic_path` = ?');
|
|
|
|
while( $row = $result->fetchRow()) {
|
|
|
|
$currentLogic = $row['logic_path'];
|
|
|
|
$currentPhysic = $row['physic_path'];
|
|
|
|
$newLogic = $path2.$this->stripRootFolder($currentLogic, $path1);
|
|
|
|
$newPhysic = $physicPath2.$this->stripRootFolder($currentPhysic, $physicPath1);
|
|
|
|
if ($path1 !== $currentLogic) {
|
|
|
|
try {
|
2013-06-07 16:11:05 +04:00
|
|
|
\OC_DB::executeAudited($updateQuery, array($newLogic, md5($newLogic), $newPhysic, md5($newPhysic),
|
|
|
|
$currentLogic));
|
2013-02-07 02:36:38 +04:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
error_log('Mapper::Copy failed '.$currentLogic.' -> '.$newLogic.'\n'.$e);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $path
|
|
|
|
* @param string $root
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return false|string
|
2013-02-07 02:36:38 +04:00
|
|
|
*/
|
|
|
|
public function stripRootFolder($path, $root) {
|
|
|
|
if (strpos($path, $root) !== 0) {
|
|
|
|
// throw exception ???
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (strlen($path) > strlen($root)) {
|
|
|
|
return substr($path, strlen($root));
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $logicPath
|
2015-02-06 17:03:29 +03:00
|
|
|
* @return null
|
|
|
|
* @throws \OC\DatabaseException
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2013-02-07 02:36:38 +04:00
|
|
|
private function resolveLogicPath($logicPath) {
|
2014-09-17 13:36:08 +04:00
|
|
|
$logicPath = $this->resolveRelativePath($logicPath);
|
2013-06-07 16:11:05 +04:00
|
|
|
$sql = 'SELECT * FROM `*PREFIX*file_map` WHERE `logic_path_hash` = ?';
|
|
|
|
$result = \OC_DB::executeAudited($sql, array(md5($logicPath)));
|
2013-02-07 02:36:38 +04:00
|
|
|
$result = $result->fetchRow();
|
2013-02-11 16:53:10 +04:00
|
|
|
if ($result === false) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-07 02:36:38 +04:00
|
|
|
|
|
|
|
return $result['physic_path'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolvePhysicalPath($physicalPath) {
|
2014-09-17 13:36:08 +04:00
|
|
|
$physicalPath = $this->resolveRelativePath($physicalPath);
|
2013-06-07 16:11:05 +04:00
|
|
|
$sql = \OC_DB::prepare('SELECT * FROM `*PREFIX*file_map` WHERE `physic_path_hash` = ?');
|
|
|
|
$result = \OC_DB::executeAudited($sql, array(md5($physicalPath)));
|
2013-02-07 02:36:38 +04:00
|
|
|
$result = $result->fetchRow();
|
|
|
|
|
|
|
|
return $result['logic_path'];
|
|
|
|
}
|
|
|
|
|
2014-09-17 13:36:08 +04:00
|
|
|
private function resolveRelativePath($path) {
|
|
|
|
$explodedPath = explode('/', $path);
|
|
|
|
$pathArray = array();
|
|
|
|
foreach ($explodedPath as $pathElement) {
|
|
|
|
if (empty($pathElement) || ($pathElement == '.')) {
|
|
|
|
continue;
|
|
|
|
} elseif ($pathElement == '..') {
|
|
|
|
if (count($pathArray) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
array_pop($pathArray);
|
|
|
|
} else {
|
|
|
|
array_push($pathArray, $pathElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (substr($path, 0, 1) == '/') {
|
|
|
|
$path = '/';
|
|
|
|
} else {
|
|
|
|
$path = '';
|
|
|
|
}
|
|
|
|
return $path.implode('/', $pathArray);
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $logicPath
|
2015-02-06 17:03:29 +03:00
|
|
|
* @param bool $store
|
|
|
|
* @return string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2013-02-07 02:36:38 +04:00
|
|
|
private function create($logicPath, $store) {
|
2014-09-17 13:36:08 +04:00
|
|
|
$logicPath = $this->resolveRelativePath($logicPath);
|
2013-02-07 02:36:38 +04:00
|
|
|
$index = 0;
|
|
|
|
|
|
|
|
// create the slugified path
|
|
|
|
$physicalPath = $this->slugifyPath($logicPath);
|
|
|
|
|
|
|
|
// detect duplicates
|
|
|
|
while ($this->resolvePhysicalPath($physicalPath) !== null) {
|
2013-03-12 12:15:53 +04:00
|
|
|
$physicalPath = $this->slugifyPath($logicPath, $index++);
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert the new path mapping if requested
|
|
|
|
if ($store) {
|
|
|
|
$this->insert($logicPath, $physicalPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $physicalPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function insert($logicPath, $physicalPath) {
|
2013-06-07 16:11:05 +04:00
|
|
|
$sql = 'INSERT INTO `*PREFIX*file_map` (`logic_path`, `physic_path`, `logic_path_hash`, `physic_path_hash`)
|
|
|
|
VALUES (?, ?, ?, ?)';
|
|
|
|
\OC_DB::executeAudited($sql, array($logicPath, $physicalPath, md5($logicPath), md5($physicalPath)));
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2015-02-06 17:03:29 +03:00
|
|
|
* @param string $path
|
|
|
|
* @param int $index
|
|
|
|
* @return string
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2014-08-25 14:48:26 +04:00
|
|
|
public function slugifyPath($path, $index = null) {
|
2013-02-15 20:42:17 +04:00
|
|
|
$path = $this->stripRootFolder($path, $this->unchangedPhysicalRoot);
|
|
|
|
|
2013-02-07 02:36:38 +04:00
|
|
|
$pathElements = explode('/', $path);
|
|
|
|
$sluggedElements = array();
|
2014-08-25 14:48:26 +04:00
|
|
|
|
2013-02-07 02:36:38 +04:00
|
|
|
foreach ($pathElements as $pathElement) {
|
2013-02-11 16:53:10 +04:00
|
|
|
// remove empty elements
|
|
|
|
if (empty($pathElement)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-06 17:03:29 +03:00
|
|
|
$sluggedElements[] = $this->slugify($pathElement);
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
|
|
|
|
2013-03-19 19:53:37 +04:00
|
|
|
// apply index to file name
|
2013-02-07 02:36:38 +04:00
|
|
|
if ($index !== null) {
|
2014-08-25 14:48:26 +04:00
|
|
|
$last = array_pop($sluggedElements);
|
2013-06-08 19:39:25 +04:00
|
|
|
|
|
|
|
// if filename contains periods - add index number before last period
|
2014-08-25 14:48:26 +04:00
|
|
|
if (preg_match('~\.[^\.]+$~i', $last, $extension)) {
|
|
|
|
array_push($sluggedElements, substr($last, 0, -(strlen($extension[0]))) . '-' . $index . $extension[0]);
|
2013-06-08 19:39:25 +04:00
|
|
|
} else {
|
|
|
|
// if filename doesn't contain periods add index ofter the last char
|
2014-08-25 14:48:26 +04:00
|
|
|
array_push($sluggedElements, $last . '-' . $index);
|
|
|
|
}
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
2013-02-11 16:53:10 +04:00
|
|
|
|
2014-09-17 13:36:08 +04:00
|
|
|
$sluggedPath = $this->unchangedPhysicalRoot.implode('/', $sluggedElements);
|
|
|
|
return $this->resolveRelativePath($sluggedPath);
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies a string to remove all non ASCII characters and spaces.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-08-25 14:48:26 +04:00
|
|
|
private function slugify($text) {
|
2014-08-25 16:29:07 +04:00
|
|
|
$originalText = $text;
|
2013-05-12 03:47:48 +04:00
|
|
|
// replace non letter or digits or dots by -
|
|
|
|
$text = preg_replace('~[^\\pL\d\.]+~u', '-', $text);
|
2013-02-07 02:36:38 +04:00
|
|
|
|
|
|
|
// trim
|
|
|
|
$text = trim($text, '-');
|
|
|
|
|
|
|
|
// transliterate
|
|
|
|
if (function_exists('iconv')) {
|
2013-03-12 12:26:21 +04:00
|
|
|
$text = iconv('utf-8', 'us-ascii//TRANSLIT//IGNORE', $text);
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// lowercase
|
|
|
|
$text = strtolower($text);
|
|
|
|
|
|
|
|
// remove unwanted characters
|
2013-05-12 03:47:48 +04:00
|
|
|
$text = preg_replace('~[^-\w\.]+~', '', $text);
|
|
|
|
|
|
|
|
// trim ending dots (for security reasons and win compatibility)
|
|
|
|
$text = preg_replace('~\.+$~', '', $text);
|
2013-02-07 02:36:38 +04:00
|
|
|
|
2015-02-06 17:03:29 +03:00
|
|
|
if (empty($text) || \OC\Files\Filesystem::isFileBlacklisted($text)) {
|
2014-08-25 16:29:07 +04:00
|
|
|
/**
|
|
|
|
* Item slug would be empty. Previously we used uniqid() here.
|
|
|
|
* However this means that the behaviour is not reproducible, so
|
|
|
|
* when uploading files into a "empty" folder, the folders name is
|
|
|
|
* different.
|
|
|
|
*
|
2015-02-06 17:03:29 +03:00
|
|
|
* The other case is, that the slugified name would be a blacklisted
|
|
|
|
* filename. In this case we just use the same workaround by
|
|
|
|
* returning the secure md5 hash of the original name.
|
|
|
|
*
|
|
|
|
*
|
2014-08-25 16:29:07 +04:00
|
|
|
* If there would be a md5() hash collision, the deduplicate check
|
|
|
|
* will spot this and append an index later, so this should not be
|
|
|
|
* a problem.
|
|
|
|
*/
|
|
|
|
return md5($originalText);
|
2013-02-07 02:36:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
}
|