- Making it so the last component in ConstructNavbar() doesn't work as a link
[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 browser paths
24 *
25 * @package ViewSVN
26 */
27
28 /*
29
30 the majority of this stuff should go into the controller.
31 the string creators may need to go into a static class or something. or not. we'll see.
32
33 */
34
35 /**
36 * This is a utility class that should be accessed in a static fashion. It
37 * provides tools for parsing incoming data into variables.
38 *
39 * @package ViewSVN
40 * @version $Id$
41 */
42 class Paths
43 {
44 // ###################################################################
45 /**
46 * Parses the incoming path variables and returns a sanitized path that
47 * is used to init the Controller
48 *
49 * @access public
50 *
51 * @return string Universal path, separated using '/'
52 */
53 function init()
54 {
55 global $viewsvn;
56
57 if (defined('PATH_OVERRIDE') AND PATH_OVERRIDE == 1)
58 {
59 return;
60 }
61
62 if (@$_SERVER['PATH_INFO'])
63 {
64 $path = $viewsvn->sanitize($_SERVER['PATH_INFO']);
65 }
66 else
67 {
68 $viewsvn->trigger->error($viewsvn->lang->string('Your server does not support type-2 path management'));
69 }
70
71 if (!$path)
72 {
73 $viewsvn->trigger->error($viewsvn->lang->string('Invalid path sent'));
74 }
75
76 return $path;
77 }
78
79 // ###################################################################
80 /**
81 * Returns the name of the repository from a upath
82 *
83 * @access public
84 *
85 * @param string Universal path
86 *
87 * @return string Repository name
88 */
89 function fetch_repos($path)
90 {
91 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
92 return $temp[0];
93 }
94
95 // ###################################################################
96 /**
97 * Returns the path without the repository from a upath
98 *
99 * @access public
100 *
101 * @param string Universal path
102 * @param bool Preceding slash
103 *
104 * @return string Relative path
105 */
106 function fetch_path($path, $doslash = false)
107 {
108 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
109 unset($temp[0]);
110 return ($doslash ? '/' : '') . implode('/', $temp);
111 }
112
113 // ###################################################################
114 /**
115 * Fetches any URL parameters a link has
116 *
117 * @access public
118 *
119 * @param string Original URL
120 *
121 * @return array Two-element array: base path (no trailing '?'), arguments
122 */
123 function fetch_arguments($url)
124 {
125 $return = array();
126
127 $bits = parse_url($url);
128
129 if (isset($bits['query']))
130 {
131 $return[0] = $bits['path'];
132 $return[1] = $bits['query'];
133 }
134 else
135 {
136 $return[0] = $bits['path'];
137 $return[1] = '';
138 }
139
140 return $return;
141 }
142
143 // ###################################################################
144 /**
145 * Determines if the root path has been reached
146 *
147 * @access public
148 *
149 * @param string Universal path
150 *
151 * @return bool Root of path?
152 */
153 function is_root_path($path)
154 {
155 $path = Paths::fetch_path($path);
156 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
157 if (sizeof($temp) > 0)
158 {
159 return false;
160 }
161 else
162 {
163 return true;
164 }
165 }
166
167 // ###################################################################
168 /**
169 * Returns the current sanitized revision
170 *
171 * @access public
172 *
173 * @param bool High-low or not
174 * @param mixed High revision (or regular)
175 * @param mixed Low revision
176 *
177 * @return mixed Revision number or HEAD
178 */
179 function fetch_rev_num($highlow = false, $high = null, $low = null)
180 {
181 global $viewsvn;
182
183 if ($highlow)
184 {
185 if (isset($viewsvn->in['high']) AND is_null($high))
186 {
187 $high = SVNCommon::rev($viewsvn->in['high']);
188 }
189 else if (is_null($high))
190 {
191 $high = 'HEAD';
192 }
193
194 if (isset($viewsvn->in['low']) AND is_null($low))
195 {
196 $low = SVNCommon::rev($viewsvn->in['low']);
197 }
198 else if (is_null($low))
199 {
200 $low = 0;
201 }
202
203 if ($low == 'HEAD')
204 {
205 $low = 0;
206 }
207
208 if (is_int($high) AND is_int($low) AND $low > $high)
209 {
210 $temp = $high;
211 $high = $low;
212 $low = $temp;
213 }
214
215 return array('high' => $high, 'low' => $low);
216 }
217 else
218 {
219 if (isset($viewsvn->in['rev']) AND is_null($high))
220 {
221 $rev = SVNCommon::rev($viewsvn->in['rev']);
222 }
223 else if (is_null($high))
224 {
225 $rev = 'HEAD';
226 }
227 else
228 {
229 $rev = $high;
230 }
231
232 return $rev;
233 }
234 }
235
236 // ###################################################################
237 /**
238 * Returns a GET string with sanitized revision data
239 *
240 * @access public
241 *
242 * @param bool High-low or not
243 * @param mixed High revision (or regular)
244 * @param mixed Low revision
245 *
246 * @return string Revision GET data
247 */
248 function fetch_rev_str($highlow = false, $high = null, $low = null)
249 {
250 $rev = Paths::fetch_rev_num($highlow, $high, $low);
251
252 if ($highlow)
253 {
254 return '?low=' . $rev['low'] . '&amp;high=' . $rev['high'];
255 }
256 else
257 {
258 return '?rev=' . $rev;
259 }
260 }
261
262 // ###################################################################
263 /**
264 * Sanitizes a path for passing
265 *
266 * @access private
267 *
268 * @param string Path
269 *
270 * @return string Cleaned string
271 */
272 function sanitize($path)
273 {
274 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
275 }
276 }
277
278 /*=====================================================================*\
279 || ###################################################################
280 || # $HeadURL$
281 || # $Id$
282 || ###################################################################
283 \*=====================================================================*/
284 ?>