Use parse_url() in fetch_arguments()
[viewsvn.git] / includes / paths.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # ViewSVN [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Handles the various methods that are used to navigate
15 * browser paths
16 *
17 * @package ViewSVN
18 */
19
20 /**
21 * Path managing class that constructs and parses input
22 * and output for paths
23 *
24 * @package ViewSVN
25 * @version $Id$
26 */
27 class Paths
28 {
29 /**
30 * Path manager type
31 * @var integer
32 */
33 var $type;
34
35 /**
36 * Constructor: determine the type of linking to use
37 *
38 * @param integer Path management type
39 */
40 function Paths($type)
41 {
42 global $viewsvn;
43
44 if ($type < 1 OR $type > 2)
45 {
46 $viewsvn->trigger->error('invalid path management type');
47 }
48
49 $this->type = (int)$type;
50 }
51
52 /**
53 * Constructs a repository browser link
54 *
55 * @access public
56 *
57 * @param string Base path
58 * @param string Browser path, separated using '/'
59 *
60 * @return string Link path
61 */
62 function out($base, $addpath)
63 {
64 global $viewsvn;
65
66 $url = $this->fetch_arguments($base);
67 $addpath = $this->sanitize($addpath);
68
69 // standard URL type
70 if ($this->type == 1)
71 {
72 return $url[0] . '?path=' . $addpath . ($url[1] ? '&amp;' . $url[1] : '');
73 }
74 // advanced path system
75 else if ($this->type == 2)
76 {
77 return $url[0] . ($addpath{0} != '/' ? '/' : '') . $addpath . ($url[1] ? '?' . $url[1] : '');
78 }
79 }
80
81 /**
82 * Parses an incoming path with the various methods
83 * and returns a universal form
84 *
85 * @access public
86 *
87 * @return string Universal path, separated using '/'
88 */
89 function parse()
90 {
91 global $viewsvn;
92
93 // standard URL type
94 if ($this->type == 1)
95 {
96 $path = $viewsvn->in['path'];
97 }
98 // advanced path system
99 else if ($this->type == 2)
100 {
101 if (@$_SERVER['PATH_INFO'])
102 {
103 $path = $viewsvn->sanitize($_SERVER['PATH_INFO']);
104 }
105 else
106 {
107 $viewsvn->trigger->error('server does not support type 2 management');
108 }
109 }
110
111 if (!$path)
112 {
113 $viewsvn->trigger->error('invalid path sent');
114 }
115
116 if (!$viewsvn->repos->verify($this->fetch_repos($path), $this->fetch_path($path)))
117 {
118 $viewsvn->trigger->error('invalid path');
119 }
120
121 return $path;
122 }
123
124 /**
125 * Create path breadcrumb
126 *
127 * @access public
128 *
129 * @param string Universal path
130 * @param bool Add trailing slash
131 *
132 * @return string Breadcrumb HTML
133 */
134 function construct_breadcrumb($path, $doslash = true)
135 {
136 $html = '/ ';
137 $itembit = '/';
138
139 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
140 $count = count($temp) - 1;
141
142 foreach ($temp AS $val => $item)
143 {
144 $itembit .= $item . (($count != $val) ? '/' : ($doslash ? '/' : ''));
145 $html .= ($count == $val ? '<strong>' : '<a href="/viewsvn/' . $this->out('browse.php', $itembit) . '">') . $item . ($count == $val ? '</strong>' : '</a>') . ($count != $val ? ' / ' : '');
146 }
147
148 return $html;
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 * Sanitizes a path for passing
237 *
238 * @access private
239 *
240 * @param string Path
241 *
242 * @return string Cleaned string
243 */
244 function sanitize($path)
245 {
246 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
247 }
248 }
249
250 /*=====================================================================*\
251 || ###################################################################
252 || # $HeadURL$
253 || # $Id$
254 || ###################################################################
255 \*=====================================================================*/
256 ?>