Blame view

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