Adopting the GPL
[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 $html = '/ ';
146 $itembit = '/';
147
148 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
149 $count = count($temp) - 1;
150
151 foreach ($temp AS $val => $item)
152 {
153 $itembit .= $item . (($count != $val) ? '/' : ($doslash ? '/' : ''));
154 $html .= ($count == $val ? '<strong>' : '<a href="/viewsvn/' . $this->out('browse.php' . $this->fetch_rev_str(), $itembit) . '">') . $item . ($count == $val ? '</strong>' : '</a>') . ($count != $val ? ' / ' : '');
155 }
156
157 return $html;
158 }
159
160 /**
161 * Returns the name of the repository from a upath
162 *
163 * @access public
164 *
165 * @param string Universal path
166 *
167 * @return string Repository name
168 */
169 function fetch_repos($path)
170 {
171 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
172 return $temp[0];
173 }
174
175 /**
176 * Returns the path without the repository from a upath
177 *
178 * @access public
179 *
180 * @param string Universal path
181 * @param bool Preceding slash
182 *
183 * @return string Relative path
184 */
185 function fetch_path($path, $doslash = false)
186 {
187 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
188 unset($temp[0]);
189 return ($doslash ? '/' : '') . implode('/', $temp);
190 }
191
192 /**
193 * Fetches any URL parameters a link has
194 *
195 * @access public
196 *
197 * @param string Original URL
198 *
199 * @return array Two-element array: base path (no trailing '?'), arguments
200 */
201 function fetch_arguments($url)
202 {
203 $return = array();
204
205 $bits = parse_url($url);
206
207 if (isset($bits['query']))
208 {
209 $return[0] = $bits['path'];
210 $return[1] = $bits['query'];
211 }
212 else
213 {
214 $return[0] = $bits['path'];
215 $return[1] = '';
216 }
217
218 return $return;
219 }
220
221 /**
222 * Determines if the root path has been reached
223 *
224 * @access public
225 *
226 * @param string Universal path
227 *
228 * @return bool Root of path?
229 */
230 function is_root_path($path)
231 {
232 $path = $this->fetch_path($path);
233 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
234 if (count($temp) > 0)
235 {
236 return false;
237 }
238 else
239 {
240 return true;
241 }
242 }
243
244 /**
245 * Returns the current sanitized revision
246 *
247 * @access public
248 *
249 * @param bool High-low or not
250 * @param mixed High revision (or regular)
251 * @param mixed Low revision
252 *
253 * @return mixed Revision number or HEAD
254 */
255 function fetch_rev_num($highlow = false, $high = null, $low = null)
256 {
257 global $viewsvn;
258
259 if ($highlow)
260 {
261 if (isset($viewsvn->in['high']) AND is_null($high))
262 {
263 $high = $viewsvn->svn->rev($viewsvn->in['high']);
264 }
265 else if (is_null($high))
266 {
267 $high = 'HEAD';
268 }
269
270 if (isset($viewsvn->in['low']))
271 {
272 $low = $viewsvn->svn->rev($viewsvn->in['low']);
273 }
274 else if (is_null($low))
275 {
276 $low = 0;
277 }
278
279 if ($low == 'HEAD')
280 {
281 $low = 0;
282 }
283
284 if (is_int($high) AND is_int($low) AND $low > $high)
285 {
286 $temp = $high;
287 $high = $low;
288 $low = $temp;
289 }
290
291 return array('high' => $high, 'low' => $low);
292 }
293 else
294 {
295 if (isset($viewsvn->in['rev']) AND is_null($high))
296 {
297 $rev = $viewsvn->svn->rev($viewsvn->in['rev']);
298 }
299 else if (is_null($high))
300 {
301 $rev = 'HEAD';
302 }
303 else
304 {
305 $rev = $high;
306 }
307
308 return $rev;
309 }
310 }
311
312 /**
313 * Returns a GET string with sanitized revision data
314 *
315 * @access public
316 *
317 * @param bool High-low or not
318 * @param mixed High revision (or regular)
319 * @param mixed Low revision
320 *
321 * @return string Revision GET data
322 */
323 function fetch_rev_str($highlow = false, $high = null, $low = null)
324 {
325 $rev = $this->fetch_rev_num($highlow, $high, $low);
326
327 if ($highlow)
328 {
329 return '?low=' . $rev['low'] . '&amp;high=' . $rev['high'];
330 }
331 else
332 {
333 return '?rev=' . $rev;
334 }
335 }
336
337 /**
338 * Sanitizes a path for passing
339 *
340 * @access private
341 *
342 * @param string Path
343 *
344 * @return string Cleaned string
345 */
346 function sanitize($path)
347 {
348 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
349 }
350 }
351
352 /*=====================================================================*\
353 || ###################################################################
354 || # $HeadURL$
355 || # $Id$
356 || ###################################################################
357 \*=====================================================================*/
358 ?>