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