Don't add a trailing slash in viewsvn::paths::out() - method 2
[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 * Returns the name of the repository from a upath
126 *
127 * @access public
128 *
129 * @param string Universal path
130 *
131 * @return string Repository name
132 */
133 function fetch_repos($path)
134 {
135 $temp = preg_split('#/#', $path, 0, PREG_SPLIT_NO_EMPTY);
136 return $temp[0];
137 }
138
139 /**
140 * Returns the path without the repository from a upath
141 *
142 * @access public
143 *
144 * @param string Universal path
145 *
146 * @return string Relative path
147 */
148 function fetch_path($path)
149 {
150 $temp = preg_split('#/#', $path, 0, PREG_SPLIT_NO_EMPTY);
151 unset($temp[0]);
152 return '/' . implode('/', $temp);
153 }
154
155 /**
156 * Fetches any URL parameters a link has
157 *
158 * @access public
159 *
160 * @param string Original URL
161 *
162 * @return array Two-element array: base path (no trailing '?'), arguments
163 */
164 function fetch_arguments($url)
165 {
166 $return = array();
167
168 if (($pos = strpos($url, '?')) !== false)
169 {
170 $return[0] = substr($url, 0, strlen($url) - $pos + 1);
171 $return[1] = substr($url, $pos + 1);
172 }
173 else
174 {
175 $return[0] = $url;
176 $return[1] = '';
177 }
178
179 return $return;
180 }
181
182 /**
183 * Sanitizes a path for passing
184 *
185 * @access private
186 *
187 * @param string Path
188 *
189 * @return string Cleaned string
190 */
191 function sanitize($path)
192 {
193 return preg_replace('#[^a-z0-9\./\-]*#i', '', $path);
194 }
195 }
196
197 /*=====================================================================*\
198 || ###################################################################
199 || # $HeadURL$
200 || # $Id$
201 || ###################################################################
202 \*=====================================================================*/
203 ?>