Remove the extra path part
[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 /**
48 * Parses the incoming path variables and returns a sanitized path that
49 * is used to init the Controller
50 *
51 * @access public
52 *
53 * @return string Universal path, separated using '/'
54 */
55 function init()
56 {
57 global $viewsvn;
58
59 if (defined('PATH_OVERRIDE') AND PATH_OVERRIDE == 1)
60 {
61 return;
62 }
63
64 if (@$_SERVER['PATH_INFO'])
65 {
66 $path = $viewsvn->sanitize($_SERVER['PATH_INFO']);
67 }
68 else
69 {
70 $viewsvn->trigger->error($viewsvn->lang->string('Your server does not support type-2 path management'));
71 }
72
73 if (!$path)
74 {
75 $viewsvn->trigger->error($viewsvn->lang->string('Invalid path sent'));
76 }
77
78 return $path;
79 }
80
81 /**
82 * Returns the name of the repository from a upath
83 *
84 * @access public
85 *
86 * @param string Universal path
87 *
88 * @return string Repository name
89 */
90 function fetch_repos($path)
91 {
92 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
93 return $temp[0];
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 * Fetches any URL parameters a link has
115 *
116 * @access public
117 *
118 * @param string Original URL
119 *
120 * @return array Two-element array: base path (no trailing '?'), arguments
121 */
122 function fetch_arguments($url)
123 {
124 $return = array();
125
126 $bits = parse_url($url);
127
128 if (isset($bits['query']))
129 {
130 $return[0] = $bits['path'];
131 $return[1] = $bits['query'];
132 }
133 else
134 {
135 $return[0] = $bits['path'];
136 $return[1] = '';
137 }
138
139 return $return;
140 }
141
142 /**
143 * Determines if the root path has been reached
144 *
145 * @access public
146 *
147 * @param string Universal path
148 *
149 * @return bool Root of path?
150 */
151 function is_root_path($path)
152 {
153 $path = Paths::fetch_path($path);
154 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
155 if (sizeof($temp) > 0)
156 {
157 return false;
158 }
159 else
160 {
161 return true;
162 }
163 }
164
165 /**
166 * Returns the current sanitized revision
167 *
168 * @access public
169 *
170 * @param bool High-low or not
171 * @param mixed High revision (or regular)
172 * @param mixed Low revision
173 *
174 * @return mixed Revision number or HEAD
175 */
176 function fetch_rev_num($highlow = false, $high = null, $low = null)
177 {
178 global $viewsvn;
179
180 if ($highlow)
181 {
182 if (isset($viewsvn->in['high']) AND is_null($high))
183 {
184 $high = SVNCommon::rev($viewsvn->in['high']);
185 }
186 else if (is_null($high))
187 {
188 $high = 'HEAD';
189 }
190
191 if (isset($viewsvn->in['low']) AND is_null($low))
192 {
193 $low = SVNCommon::rev($viewsvn->in['low']);
194 }
195 else if (is_null($low))
196 {
197 $low = 0;
198 }
199
200 if ($low == 'HEAD')
201 {
202 $low = 0;
203 }
204
205 if (is_int($high) AND is_int($low) AND $low > $high)
206 {
207 $temp = $high;
208 $high = $low;
209 $low = $temp;
210 }
211
212 return array('high' => $high, 'low' => $low);
213 }
214 else
215 {
216 if (isset($viewsvn->in['rev']) AND is_null($high))
217 {
218 $rev = SVNCommon::rev($viewsvn->in['rev']);
219 }
220 else if (is_null($high))
221 {
222 $rev = 'HEAD';
223 }
224 else
225 {
226 $rev = $high;
227 }
228
229 return $rev;
230 }
231 }
232
233 /**
234 * Returns a GET string with sanitized revision data
235 *
236 * @access public
237 *
238 * @param bool High-low or not
239 * @param mixed High revision (or regular)
240 * @param mixed Low revision
241 *
242 * @return string Revision GET data
243 */
244 function fetch_rev_str($highlow = false, $high = null, $low = null)
245 {
246 $rev = Paths::fetch_rev_num($highlow, $high, $low);
247
248 if ($highlow)
249 {
250 return '?low=' . $rev['low'] . '&amp;high=' . $rev['high'];
251 }
252 else
253 {
254 return '?rev=' . $rev;
255 }
256 }
257
258 /**
259 * Sanitizes a path for passing
260 *
261 * @access private
262 *
263 * @param string Path
264 *
265 * @return string Cleaned string
266 */
267 function sanitize($path)
268 {
269 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
270 }
271 }
272
273 /*=====================================================================*\
274 || ###################################################################
275 || # $HeadURL$
276 || # $Id$
277 || ###################################################################
278 \*=====================================================================*/
279 ?>