Changes because this is now a static class
[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 if ($highlow)
179 {
180 if (isset($viewsvn->in['high']) AND is_null($high))
181 {
182 $high = SVNCommon::rev($viewsvn->in['high']);
183 }
184 else if (is_null($high))
185 {
186 $high = 'HEAD';
187 }
188
189 if (isset($viewsvn->in['low']) AND is_null($low))
190 {
191 $low = SVNCommon::rev($viewsvn->in['low']);
192 }
193 else if (is_null($low))
194 {
195 $low = 0;
196 }
197
198 if ($low == 'HEAD')
199 {
200 $low = 0;
201 }
202
203 if (is_int($high) AND is_int($low) AND $low > $high)
204 {
205 $temp = $high;
206 $high = $low;
207 $low = $temp;
208 }
209
210 return array('high' => $high, 'low' => $low);
211 }
212 else
213 {
214 if (isset($viewsvn->in['rev']) AND is_null($high))
215 {
216 $rev = SVNCommon::rev($viewsvn->in['rev']);
217 }
218 else if (is_null($high))
219 {
220 $rev = 'HEAD';
221 }
222 else
223 {
224 $rev = $high;
225 }
226
227 return $rev;
228 }
229 }
230
231 /**
232 * Returns a GET string with sanitized revision data
233 *
234 * @access public
235 *
236 * @param bool High-low or not
237 * @param mixed High revision (or regular)
238 * @param mixed Low revision
239 *
240 * @return string Revision GET data
241 */
242 function fetch_rev_str($highlow = false, $high = null, $low = null)
243 {
244 $rev = Paths::fetch_rev_num($highlow, $high, $low);
245
246 if ($highlow)
247 {
248 return '?low=' . $rev['low'] . '&amp;high=' . $rev['high'];
249 }
250 else
251 {
252 return '?rev=' . $rev;
253 }
254 }
255
256 /**
257 * Sanitizes a path for passing
258 *
259 * @access private
260 *
261 * @param string Path
262 *
263 * @return string Cleaned string
264 */
265 function sanitize($path)
266 {
267 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
268 }
269 }
270
271 /*=====================================================================*\
272 || ###################################################################
273 || # $HeadURL$
274 || # $Id$
275 || ###################################################################
276 \*=====================================================================*/
277 ?>