Added path management
[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] . '/' . $viewsvn->fetch_sourcepath($addpath) . ($url[1] ? '?' . $url[1] : '');
78 }
79 }
80
81 /**
82 * Fetches any URL parameters a link has
83 *
84 * @access public
85 *
86 * @param string Original URL
87 *
88 * @return array Two-element array: base path (no trailing '?'), arguments
89 */
90 function fetch_arguments($url)
91 {
92 $return = array();
93
94 if (($pos = strpos($url, '?')) !== false)
95 {
96 $return[0] = substr($url, 0, strlen($url) - $pos);
97 $return[1] = substr($url, $pos + 1);
98 }
99 else
100 {
101 $return[0] = $url;
102 $return[1] = '';
103 }
104
105 return $return;
106 }
107
108 /**
109 * Sanitizes a path for passing
110 *
111 * @access private
112 *
113 * @param string Path
114 *
115 * @return string Cleaned string
116 */
117 function sanitize($path)
118 {
119 return preg_replace('#[^a-z0-9\.]*#i', '', $path);
120 }
121 }
122
123 /*=====================================================================*\
124 || ###################################################################
125 || # $HeadURL$
126 || # $Id$
127 || ###################################################################
128 \*=====================================================================*/
129 ?>