Removing hard-coded /viewsvn/ path
[viewsvn.git] / includes / paths.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Handles the various methods that are used to navigate
24 * browser paths
25 *
26 * @package ViewSVN
27 */
28
29 /**
30 * Path managing class that constructs and parses input
31 * and output for paths
32 *
33 * @package ViewSVN
34 * @version $Id$
35 */
36 class Paths
37 {
38 /**
39 * Path manager type
40 * @var integer
41 */
42 var $type;
43
44 /**
45 * Constructor: determine the type of linking to use
46 *
47 * @param integer Path management type
48 */
49 function Paths($type)
50 {
51 global $viewsvn;
52
53 if ($type < 1 OR $type > 2)
54 {
55 $viewsvn->trigger->error('invalid path management type');
56 }
57
58 $this->type = (int)$type;
59 }
60
61 /**
62 * Constructs a repository browser link
63 *
64 * @access public
65 *
66 * @param string Base path
67 * @param string Browser path, separated using '/'
68 *
69 * @return string Link path
70 */
71 function out($base, $addpath)
72 {
73 global $viewsvn;
74
75 $url = $this->fetch_arguments($base);
76 $addpath = $this->sanitize($addpath);
77
78 // standard URL type
79 if ($this->type == 1)
80 {
81 return $url[0] . '?path=' . $addpath . ($url[1] ? '&amp;' . $url[1] : '');
82 }
83 // advanced path system
84 else if ($this->type == 2)
85 {
86 return $url[0] . ($addpath{0} != '/' ? '/' : '') . $addpath . ($url[1] ? '?' . $url[1] : '');
87 }
88 }
89
90 /**
91 * Parses an incoming path with the various methods
92 * and returns a universal form
93 *
94 * @access public
95 *
96 * @return string Universal path, separated using '/'
97 */
98 function parse()
99 {
100 global $viewsvn;
101
102 // standard URL type
103 if ($this->type == 1)
104 {
105 $path = $viewsvn->in['path'];
106 }
107 // advanced path system
108 else if ($this->type == 2)
109 {
110 if (@$_SERVER['PATH_INFO'])
111 {
112 $path = $viewsvn->sanitize($_SERVER['PATH_INFO']);
113 }
114 else
115 {
116 $viewsvn->trigger->error('server does not support type 2 management');
117 }
118 }
119
120 if (!$path)
121 {
122 $viewsvn->trigger->error('invalid path sent');
123 }
124
125 if (!$viewsvn->repos->verify($this->fetch_repos($path), $this->fetch_path($path)))
126 {
127 $viewsvn->trigger->error('invalid path');
128 }
129
130 return $path;
131 }
132
133 /**
134 * Create path breadcrumb
135 *
136 * @access public
137 *
138 * @param string Universal path
139 * @param bool Add trailing slash
140 *
141 * @return string Breadcrumb HTML
142 */
143 function construct_breadcrumb($path, $doslash = true)
144 {
145 global $viewsvn;
146
147 $html = '/ ';
148 $itembit = '/';
149
150 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
151 $count = count($temp) - 1;
152
153 foreach ($temp AS $val => $item)
154 {
155 $itembit .= $item . (($count != $val) ? '/' : ($doslash ? '/' : ''));
156 $html .= ($count == $val ? '<strong>' : '<a href="' . $viewsvn->path . '/' . $this->out('browse.php' . $this->fetch_rev_str(), $itembit) . '">') . $item . ($count == $val ? '</strong>' : '</a>') . ($count != $val ? ' / ' : '');
157 }
158
159 return $html;
160 }
161
162 /**
163 * Returns the name of the repository from a upath
164 *
165 * @access public
166 *
167 * @param string Universal path
168 *
169 * @return string Repository name
170 */
171 function fetch_repos($path)
172 {
173 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
174 return $temp[0];
175 }
176
177 /**
178 * Returns the path without the repository from a upath
179 *
180 * @access public
181 *
182 * @param string Universal path
183 * @param bool Preceding slash
184 *
185 * @return string Relative path
186 */
187 function fetch_path($path, $doslash = false)
188 {
189 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
190 unset($temp[0]);
191 return ($doslash ? '/' : '') . implode('/', $temp);
192 }
193
194 /**
195 * Fetches any URL parameters a link has
196 *
197 * @access public
198 *
199 * @param string Original URL
200 *
201 * @return array Two-element array: base path (no trailing '?'), arguments
202 */
203 function fetch_arguments($url)
204 {
205 $return = array();
206
207 $bits = parse_url($url);
208
209 if (isset($bits['query']))
210 {
211 $return[0] = $bits['path'];
212 $return[1] = $bits['query'];
213 }
214 else
215 {
216 $return[0] = $bits['path'];
217 $return[1] = '';
218 }
219
220 return $return;
221 }
222
223 /**
224 * Determines if the root path has been reached
225 *
226 * @access public
227 *
228 * @param string Universal path
229 *
230 * @return bool Root of path?
231 */
232 function is_root_path($path)
233 {
234 $path = $this->fetch_path($path);
235 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
236 if (count($temp) > 0)
237 {
238 return false;
239 }
240 else
241 {
242 return true;
243 }
244 }
245
246 /**
247 * Returns the current sanitized revision
248 *
249 * @access public
250 *
251 * @param bool High-low or not
252 * @param mixed High revision (or regular)
253 * @param mixed Low revision
254 *
255 * @return mixed Revision number or HEAD
256 */
257 function fetch_rev_num($highlow = false, $high = null, $low = null)
258 {
259 global $viewsvn;
260
261 if ($highlow)
262 {
263 if (isset($viewsvn->in['high']) AND is_null($high))
264 {
265 $high = $viewsvn->svn->rev($viewsvn->in['high']);
266 }
267 else if (is_null($high))
268 {
269 $high = 'HEAD';
270 }
271
272 if (isset($viewsvn->in['low']))
273 {
274 $low = $viewsvn->svn->rev($viewsvn->in['low']);
275 }
276 else if (is_null($low))
277 {
278 $low = 0;
279 }
280
281 if ($low == 'HEAD')
282 {
283 $low = 0;
284 }
285
286 if (is_int($high) AND is_int($low) AND $low > $high)
287 {
288 $temp = $high;
289 $high = $low;
290 $low = $temp;
291 }
292
293 return array('high' => $high, 'low' => $low);
294 }
295 else
296 {
297 if (isset($viewsvn->in['rev']) AND is_null($high))
298 {
299 $rev = $viewsvn->svn->rev($viewsvn->in['rev']);
300 }
301 else if (is_null($high))
302 {
303 $rev = 'HEAD';
304 }
305 else
306 {
307 $rev = $high;
308 }
309
310 return $rev;
311 }
312 }
313
314 /**
315 * Returns a GET string with sanitized revision data
316 *
317 * @access public
318 *
319 * @param bool High-low or not
320 * @param mixed High revision (or regular)
321 * @param mixed Low revision
322 *
323 * @return string Revision GET data
324 */
325 function fetch_rev_str($highlow = false, $high = null, $low = null)
326 {
327 $rev = $this->fetch_rev_num($highlow, $high, $low);
328
329 if ($highlow)
330 {
331 return '?low=' . $rev['low'] . '&amp;high=' . $rev['high'];
332 }
333 else
334 {
335 return '?rev=' . $rev;
336 }
337 }
338
339 /**
340 * Sanitizes a path for passing
341 *
342 * @access private
343 *
344 * @param string Path
345 *
346 * @return string Cleaned string
347 */
348 function sanitize($path)
349 {
350 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
351 }
352 }
353
354 /*=====================================================================*\
355 || ###################################################################
356 || # $HeadURL$
357 || # $Id$
358 || ###################################################################
359 \*=====================================================================*/
360 ?>