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