docs++
[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. This is essentially a controller
32 * of all current information.
33 *
34 * @package ViewSVN
35 * @version $Id$
36 */
37 class Paths
38 {
39 /**
40 * Path manager type
41 * @var integer
42 */
43 var $type;
44
45 /**
46 * The universal path that is currently being browsed
47 * @var string
48 */
49 var $path;
50
51 /**
52 * Current repository that we're in
53 * @var string
54 */
55 var $repos;
56
57 /**
58 * Current internal path inside the repository
59 * @var string
60 */
61 var $relpath;
62
63 /**
64 * Constructor: determine the type of linking to use
65 *
66 * @param integer Path management type
67 */
68 function Paths($type)
69 {
70 global $viewsvn;
71
72 if ($type < 1 OR $type > 2)
73 {
74 $viewsvn->trigger->error($viewsvn->lang->string('Invalid path management type specified in [includes/config.php]'));
75 }
76
77 $this->type = (int)$type;
78
79 $this->path = $this->parse();
80 }
81
82 /**
83 * Constructs a repository browser link
84 *
85 * @access public
86 *
87 * @param string Base path
88 * @param string Browser path, separated using '/'
89 *
90 * @return string Link path
91 */
92 function out($base, $addpath)
93 {
94 global $viewsvn;
95
96 $url = $this->fetch_arguments($base);
97 $addpath = $this->sanitize($addpath);
98
99 // standard URL type
100 if ($this->type == 1)
101 {
102 return $url[0] . '?path=' . $addpath . ($url[1] ? '&amp;' . $url[1] : '');
103 }
104 // advanced path system
105 else if ($this->type == 2)
106 {
107 return $url[0] . ($addpath{0} != '/' ? '/' : '') . $addpath . ($url[1] ? '?' . $url[1] : '');
108 }
109 }
110
111 /**
112 * Parses an incoming path with the various methods
113 * and returns a universal form
114 *
115 * @access public
116 *
117 * @return string Universal path, separated using '/'
118 */
119 function parse()
120 {
121 global $viewsvn;
122
123 if (defined('PATH_OVERRIDE') AND PATH_OVERRIDE == 1)
124 {
125 return;
126 }
127
128 // standard URL type
129 if ($this->type == 1)
130 {
131 $path = $viewsvn->in['path'];
132 }
133 // advanced path system
134 else if ($this->type == 2)
135 {
136 if (@$_SERVER['PATH_INFO'])
137 {
138 $path = $viewsvn->sanitize($_SERVER['PATH_INFO']);
139 }
140 else
141 {
142 $viewsvn->trigger->error($viewsvn->lang->string('Your server does not support type-2 path management'));
143 }
144 }
145
146 if (!$path)
147 {
148 $viewsvn->trigger->error($viewsvn->lang->string('Invalid path sent'));
149 }
150
151 if (!$viewsvn->repos->verify($this->repos = $this->fetch_repos($path), $this->relpath = $this->fetch_path($path)))
152 {
153 $viewsvn->trigger->error($viewsvn->lang->string('The path specified could not be verified'));
154 }
155
156 return $path;
157 }
158
159 /**
160 * Create path breadcrumb
161 *
162 * @access public
163 *
164 * @param string Universal path
165 * @param bool Add trailing slash
166 *
167 * @return string Breadcrumb HTML
168 */
169 function construct_breadcrumb($path, $doslash = true)
170 {
171 global $viewsvn;
172
173 $html = '/ ';
174 $itembit = '/';
175
176 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
177 $count = count($temp) - 1;
178
179 foreach ($temp AS $val => $item)
180 {
181 $itembit .= $item;
182 $itembit .= (($count != $val OR @$viewsvn->svn->common->isdir($itembit)) ? '/' : '');
183 $html .= '<a href="' . $viewsvn->path . '/' . $this->out('browse.php' . $this->fetch_rev_str(), $itembit) . '">' . $item . '</a>'. ($count != $val ? ' / ' : '');
184 }
185
186 return $html;
187 }
188
189 /**
190 * Returns the name of the repository from a upath
191 *
192 * @access public
193 *
194 * @param string Universal path
195 *
196 * @return string Repository name
197 */
198 function fetch_repos($path)
199 {
200 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
201 return $temp[0];
202 }
203
204 /**
205 * Returns the path without the repository from a upath
206 *
207 * @access public
208 *
209 * @param string Universal path
210 * @param bool Preceding slash
211 *
212 * @return string Relative path
213 */
214 function fetch_path($path, $doslash = false)
215 {
216 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
217 unset($temp[0]);
218 return ($doslash ? '/' : '') . implode('/', $temp);
219 }
220
221 /**
222 * Fetches any URL parameters a link has
223 *
224 * @access public
225 *
226 * @param string Original URL
227 *
228 * @return array Two-element array: base path (no trailing '?'), arguments
229 */
230 function fetch_arguments($url)
231 {
232 $return = array();
233
234 $bits = parse_url($url);
235
236 if (isset($bits['query']))
237 {
238 $return[0] = $bits['path'];
239 $return[1] = $bits['query'];
240 }
241 else
242 {
243 $return[0] = $bits['path'];
244 $return[1] = '';
245 }
246
247 return $return;
248 }
249
250 /**
251 * Determines if the root path has been reached
252 *
253 * @access public
254 *
255 * @param string Universal path
256 *
257 * @return bool Root of path?
258 */
259 function is_root_path($path)
260 {
261 $path = $this->fetch_path($path);
262 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
263 if (count($temp) > 0)
264 {
265 return false;
266 }
267 else
268 {
269 return true;
270 }
271 }
272
273 /**
274 * Returns the current sanitized revision
275 *
276 * @access public
277 *
278 * @param bool High-low or not
279 * @param mixed High revision (or regular)
280 * @param mixed Low revision
281 *
282 * @return mixed Revision number or HEAD
283 */
284 function fetch_rev_num($highlow = false, $high = null, $low = null)
285 {
286 global $viewsvn;
287
288 if ($highlow)
289 {
290 if (isset($viewsvn->in['high']) AND is_null($high))
291 {
292 $high = $viewsvn->svn->rev($viewsvn->in['high']);
293 }
294 else if (is_null($high))
295 {
296 $high = 'HEAD';
297 }
298
299 if (isset($viewsvn->in['low']) AND is_null($low))
300 {
301 $low = $viewsvn->svn->rev($viewsvn->in['low']);
302 }
303 else if (is_null($low))
304 {
305 $low = 0;
306 }
307
308 if ($low == 'HEAD')
309 {
310 $low = 0;
311 }
312
313 if (is_int($high) AND is_int($low) AND $low > $high)
314 {
315 $temp = $high;
316 $high = $low;
317 $low = $temp;
318 }
319
320 return array('high' => $high, 'low' => $low);
321 }
322 else
323 {
324 if (isset($viewsvn->in['rev']) AND is_null($high))
325 {
326 $rev = $viewsvn->svn->rev($viewsvn->in['rev']);
327 }
328 else if (is_null($high))
329 {
330 $rev = 'HEAD';
331 }
332 else
333 {
334 $rev = $high;
335 }
336
337 return $rev;
338 }
339 }
340
341 /**
342 * Returns a GET string with sanitized revision data
343 *
344 * @access public
345 *
346 * @param bool High-low or not
347 * @param mixed High revision (or regular)
348 * @param mixed Low revision
349 *
350 * @return string Revision GET data
351 */
352 function fetch_rev_str($highlow = false, $high = null, $low = null)
353 {
354 $rev = $this->fetch_rev_num($highlow, $high, $low);
355
356 if ($highlow)
357 {
358 return '?low=' . $rev['low'] . '&amp;high=' . $rev['high'];
359 }
360 else
361 {
362 return '?rev=' . $rev;
363 }
364 }
365
366 /**
367 * Sanitizes a path for passing
368 *
369 * @access private
370 *
371 * @param string Path
372 *
373 * @return string Cleaned string
374 */
375 function sanitize($path)
376 {
377 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
378 }
379 }
380
381 /*=====================================================================*\
382 || ###################################################################
383 || # $HeadURL$
384 || # $Id$
385 || ###################################################################
386 \*=====================================================================*/
387 ?>