Allow underscores in paths
[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 *
131 * @return string Breadcrumb HTML
132 */
133 function construct_breadcrumb($path, $doslash)
134 {
135 $html = '/ ';
136 $itembit = '/';
137
138 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
139 //krsort($temp);
140
141 foreach ($temp AS $item)
142 {
143 $itembit .= '/' . $item . '/';
144 $html .= '<a href="/viewsvn/' . $this->out('browse.php', $itembit) . '">' . $item . '</a> / ';
145 }
146
147 return $html;
148 }
149
150 /**
151 * Returns the name of the repository from a upath
152 *
153 * @access public
154 *
155 * @param string Universal path
156 *
157 * @return string Repository name
158 */
159 function fetch_repos($path)
160 {
161 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
162 return $temp[0];
163 }
164
165 /**
166 * Returns the path without the repository from a upath
167 *
168 * @access public
169 *
170 * @param string Universal path
171 * @param bool Preceding slash
172 *
173 * @return string Relative path
174 */
175 function fetch_path($path, $doslash = false)
176 {
177 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
178 unset($temp[0]);
179 return ($doslash ? '/' : '') . implode('/', $temp);
180 }
181
182 /**
183 * Fetches any URL parameters a link has
184 *
185 * @access public
186 *
187 * @param string Original URL
188 *
189 * @return array Two-element array: base path (no trailing '?'), arguments
190 */
191 function fetch_arguments($url)
192 {
193 $return = array();
194
195 if (($pos = strpos($url, '?')) !== false)
196 {
197 $return[0] = substr($url, 0, strlen($url) - $pos + 1);
198 $return[1] = substr($url, $pos + 1);
199 }
200 else
201 {
202 $return[0] = $url;
203 $return[1] = '';
204 }
205
206 return $return;
207 }
208
209 /**
210 * Determines if the root path has been reached
211 *
212 * @access public
213 *
214 * @param string Universal path
215 *
216 * @return bool Root of path?
217 */
218 function is_root_path($path)
219 {
220 $path = $this->fetch_path($path);
221 $temp = preg_split('#/#', $path, -1, PREG_SPLIT_NO_EMPTY);
222 if (count($temp) > 0)
223 {
224 return false;
225 }
226 else
227 {
228 return true;
229 }
230 }
231
232 /**
233 * Sanitizes a path for passing
234 *
235 * @access private
236 *
237 * @param string Path
238 *
239 * @return string Cleaned string
240 */
241 function sanitize($path)
242 {
243 return preg_replace('#[^a-z0-9\./\-_]*#i', '', $path);
244 }
245 }
246
247 /*=====================================================================*\
248 || ###################################################################
249 || # $HeadURL$
250 || # $Id$
251 || ###################################################################
252 \*=====================================================================*/
253 ?>