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