Rev 171 Rev 172
1 <?php 1 <?php
2 # vim:et:ts=3:sts=3:sw=3:fdm=marker: 2 # vim:et:ts=3:sts=3:sw=3:fdm=marker:
3   3  
4 // WebSVN - Subversion repository viewing via the web using PHP 4 // WebSVN - Subversion repository viewing via the web using PHP
5 // Copyright © 2004-2006 Tim Armes, Matt Sicker 5 // Copyright © 2004-2006 Tim Armes, Matt Sicker
6 // 6 //
7 // This program is free software; you can redistribute it and/or modify 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 8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or 9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version. 10 // (at your option) any later version.
11 // 11 //
12 // This program is distributed in the hope that it will be useful, 12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details. 15 // GNU General Public License for more details.
16 // 16 //
17 // You should have received a copy of the GNU General Public License 17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software 18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // 20 //
21 // -- 21 // --
22 // 22 //
23 // utils.inc 23 // utils.inc
24 // 24 //
25 // General utility commands 25 // General utility commands
26   26  
27 #require_once 'php5compat.inc'; 27 #require_once 'php5compat.inc';
28   28  
29 // {{{ createDirLinks 29 // {{{ createDirLinks
30 // 30 //
31 // Create a list of links to the current path that'll be available from the template 31 // Create a list of links to the current path that'll be available from the template
32   32  
33 function createDirLinks($rep, $path, $rev, $showchanged) 33 function createDirLinks($rep, $path, $rev, $showchanged)
34 { 34 {
35 global $vars, $config; 35 global $vars, $config;
36 36
37 $subs = explode('/', htmlentities($path)); 37 $subs = explode('/', htmlentities($path));
38 $sofar = ""; 38 $sofar = "";
39 $count = count($subs); 39 $count = count($subs);
40 $vars["curdirlinks"] = ""; 40 $vars["curdirlinks"] = "";
41 41
42 // The number of links depends on the last item. It's empty if 42 // The number of links depends on the last item. It's empty if
43 // we're looing at a directory, and full if it's a file 43 // we're looing at a directory, and full if it's a file
44 if (empty($subs[$count - 1])) 44 if (empty($subs[$count - 1]))
45 { 45 {
46 $limit = $count - 2; 46 $limit = $count - 2;
47 $dir = true; 47 $dir = true;
48 } 48 }
49 else 49 else
50 { 50 {
51 $limit = $count - 1; 51 $limit = $count - 1;
52 $dir = false; 52 $dir = false;
53 } 53 }
54 54
55 for ($n = 0; $n < $limit; $n++) 55 for ($n = 0; $n < $limit; $n++)
56 { 56 {
57 $sofar .= html_entity_decode($subs[$n])."/"; 57 $sofar .= html_entity_decode($subs[$n])."/";
58 $sofarurl = $config->getURL($rep, $sofar, "dir"); 58 $sofarurl = $config->getURL($rep, $sofar, "dir");
59 $vars["curdirlinks"] .= "[<a href=\"${sofarurl}rev=$rev&amp;sc=$showchanged\">".$subs[$n]."/]</a> "; 59 $vars["curdirlinks"] .= "[<a href=\"${sofarurl}rev=$rev&amp;sc=$showchanged\">".$subs[$n]."/]</a> ";
60 } 60 }
61 61
62 if ($dir) 62 if ($dir)
63 { 63 {
64 $vars["curdirlinks"] .= "[<b>".html_entity_decode($subs[$n])."</b>/]"; 64 $vars["curdirlinks"] .= "[<b>".html_entity_decode($subs[$n])."</b>/]";
65 } 65 }
66 else 66 else
67 { 67 {
68 $vars["curdirlinks"] .= "[<b>".html_entity_decode($subs[$n])."</b>]"; 68 $vars["curdirlinks"] .= "[<b>".html_entity_decode($subs[$n])."</b>]";
69 } 69 }
70 } 70 }
71   71  
72 // }}} 72 // }}}
73   73  
74 // {{{ create_anchors 74 // {{{ create_anchors
75 // 75 //
76 // Create links out of http:// and mailto: tags 76 // Create links out of http:// and mailto: tags
77   77  
78 # TODO: the target="_blank" nonsense should be optional (or specified by the template) 78 # TODO: the target="_blank" nonsense should be optional (or specified by the template)
79 function create_anchors($text) 79 function create_anchors($text)
80 { 80 {
81 $ret = $text; 81 $ret = $text;
82   82  
83 // Match correctly formed URLs that aren't already links 83 // Match correctly formed URLs that aren't already links
84 $ret = preg_replace("#\b(?<!href=\")([a-z]+?)://(\S*)([\w/]+)#i", 84 $ret = preg_replace("#\b(?<!href=\")([a-z]+?)://(\S*)([\w/]+)#i",
85 "<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>", 85 "<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>",
86 $ret); 86 $ret);
87 87
88 // Now match anything beginning with www, as long as it's not //www since they were matched above 88 // Now match anything beginning with www, as long as it's not //www since they were matched above
89 $ret = preg_replace("#\b(?<!//)www\.(\S*)([\w/]+)#i", 89 $ret = preg_replace("#\b(?<!//)www\.(\S*)([\w/]+)#i",
90 "<a href=\"http://www.\\1\\2\" target=\"_blank\">www.\\1\\2</a>", 90 "<a href=\"http://www.\\1\\2\" target=\"_blank\">www.\\1\\2</a>",
91 $ret); 91 $ret);
92   92  
93 // Match email addresses 93 // Match email addresses
94 $ret = preg_replace("#\b([\w\-_.]+)@([\w\-.]+)\b#i", 94 $ret = preg_replace("#\b([\w\-_.]+)@([\w\-.]+)\b#i",
95 "<a href=\"mailto:\\1@\\2\">\\1@\\2</a>", 95 "<a href=\"mailto:\\1@\\2\">\\1@\\2</a>",
96 $ret); 96 $ret);
97 97
98 return ($ret); 98 return ($ret);
99 } 99 }
100   100  
101 // }}} 101 // }}}
102   102  
103 // {{{ getFullURL 103 // {{{ getFullURL
104   104  
105 function getFullURL($loc) 105 function getFullURL($loc)
106 { 106 {
107 $protocol = 'http'; 107 $protocol = 'http';
108 108
109 if (isset($_SERVER["HTTPS"]) && (strtolower($_SERVER["HTTPS"]) != "off")) 109 if (isset($_SERVER["HTTPS"]) && (strtolower($_SERVER["HTTPS"]) != "off"))
110 { 110 {
111 $protocol = "https"; 111 $protocol = "https";
112 } 112 }
113 113
114 $port = ":".$_SERVER["SERVER_PORT"]; 114 $port = ":".$_SERVER["SERVER_PORT"];
115 if ((":80" == $port && "http" == $protocol) || 115 if ((":80" == $port && "http" == $protocol) ||
116 (":443" == $port && "https" == $protocol)) 116 (":443" == $port && "https" == $protocol))
117 { 117 {
118 $port = ""; 118 $port = "";
119 } 119 }
120 120
121 if (isset($_SERVER["HTTP_HOST"])) 121 if (isset($_SERVER["HTTP_HOST"]))
122 { 122 {
123 $host = $_SERVER["HTTP_HOST"]; 123 $host = $_SERVER["HTTP_HOST"];
124 } 124 }
125 else if (isset($_SERVER["SERVER_NAME"])) 125 else if (isset($_SERVER["SERVER_NAME"]))
126 { 126 {
127 $host = $_SERVER["SERVER_NAME"].$port; 127 $host = $_SERVER["SERVER_NAME"].$port;
128 } 128 }
129 else if (isset($_SERVER["SERVER_ADDR"])) 129 else if (isset($_SERVER["SERVER_ADDR"]))
130 { 130 {
131 $host = $_SERVER["SERVER_ADDR"].$port; 131 $host = $_SERVER["SERVER_ADDR"].$port;
132 } 132 }
133 else 133 else
134 { 134 {
135 print "Unable to redirect"; 135 print "Unable to redirect";
136 exit; 136 exit;
137 } 137 }
138 138
139 $url = $protocol . "://" . $host . $loc; 139 $url = $protocol . "://" . $host . $loc;
140   140  
141 return $url; 141 return $url;
142 } 142 }
143   143  
144 // }}} 144 // }}}
145   145  
146 // {{{ hardspace 146 // {{{ hardspace
147 // 147 //
148 // Replace the spaces at the front of a line with hard spaces 148 // Replace the spaces at the front of a line with hard spaces
149   149  
150 # XXX: this is an unnecessary function; you can prevent whitespace from being 150 # XXX: this is an unnecessary function; you can prevent whitespace from being
151 # trimmed via CSS (use the "white-space: pre;" properties). ~J 151 # trimmed via CSS (use the "white-space: pre;" properties). ~J
152 # in the meantime, here's an improved function (does nothing) 152 # in the meantime, here's an improved function (does nothing)
153   153  
154 function hardspace($s) 154 function hardspace($s)
155 { 155 {
156 return '<code>'.$s.'</code>'; 156 return '<code>'.$s.'</code>';
157 } 157 }
158   158  
159 // }}} 159 // }}}
160   160  
161 // {{{ datetimeFormatDuration 161 // {{{ datetimeFormatDuration
162 // 162 //
163 // Formats a duration of seconds for display. 163 // Formats a duration of seconds for display.
164 // 164 //
165 // $seconds the number of seconds until something 165 // $seconds the number of seconds until something
166 // $nbsp true if spaces should be replaced by nbsp 166 // $nbsp true if spaces should be replaced by nbsp
167 // $skipSeconds true if seconds should be omitted 167 // $skipSeconds true if seconds should be omitted
168 // 168 //
169 // return the formatted duration (e.g. @c "8h 6m 1s") 169 // return the formatted duration (e.g. @c "8h 6m 1s")
170   170  
171 function datetimeFormatDuration($seconds, $nbsp = false, $skipSeconds = false) 171 function datetimeFormatDuration($seconds, $nbsp = false, $skipSeconds = false)
172 { 172 {
173 global $lang; 173 global $lang;
174 174
175 if ($seconds < 0) 175 if ($seconds < 0)
176 { 176 {
177 $seconds = -$seconds; 177 $seconds = -$seconds;
178 $neg = true; 178 $neg = true;
179 } 179 }
180 else 180 else
181 $neg = false; 181 $neg = false;
182   182  
183 $qty = array(); 183 $qty = array();
184   184  
185 $qty[] = (int)($seconds / (60 * 60 * 24)); 185 $qty[] = (int)($seconds / (60 * 60 * 24));
186 $seconds %= 60 * 60 * 24; 186 $seconds %= 60 * 60 * 24;
187   187  
188 $qty[] = (int)($seconds / (60 * 60)); 188 $qty[] = (int)($seconds / (60 * 60));
189 $seconds %= 60 * 60; 189 $seconds %= 60 * 60;
190   190  
191 $qty[] = (int)($seconds / 60); 191 $qty[] = (int)($seconds / 60);
192 $qty[] = (int)($seconds % 60); 192 $qty[] = (int)($seconds % 60);
193   193  
194 $text = ""; 194 $text = "";
195 $names = $lang["DAYLETTER"].$lang["HOURLETTER"].$lang["MINUTELETTER"]; 195 $names = $lang["DAYLETTER"].$lang["HOURLETTER"].$lang["MINUTELETTER"];
196 if (!$skipSeconds) $names .= $lang["SECONDLETTER"]; 196 if (!$skipSeconds) $names .= $lang["SECONDLETTER"];
197   197  
198 $any = false; 198 $any = false;
199 for ($i = 0; $i < strlen($names); $i++) 199 for ($i = 0; $i < strlen($names); $i++)
200 // If a "higher valued" time slot had a value or this time slot 200 // If a "higher valued" time slot had a value or this time slot
201 // has a value or this is the very last entry (i.e. all values 201 // has a value or this is the very last entry (i.e. all values
202 // are 0 and we still want to print seconds) 202 // are 0 and we still want to print seconds)
203 if ($any || $qty[$i] || $i == sizeof($qty) - 1) 203 if ($any || $qty[$i] || $i == sizeof($qty) - 1)
204 { 204 {
205 $any = true; 205 $any = true;
206   206  
207 if ($i) 207 if ($i)
208 $text .= sprintf("%2d", $qty[$i]); 208 $text .= sprintf("%2d", $qty[$i]);
209 else 209 else
210 $text .= $qty[$i]; 210 $text .= $qty[$i];
211   211  
212 $text .= "{$names{$i}} "; 212 $text .= "{$names{$i}} ";
213 } 213 }
214   214  
215 $text = trim($text); 215 $text = trim($text);
216 if ($neg) 216 if ($neg)
217 $text = "-$text"; 217 $text = "-$text";
218   218  
219 return $nbsp ? str_replace(" ", "&nbsp;", $text) : $text; 219 return $nbsp ? str_replace(" ", "&nbsp;", $text) : $text;
220 } 220 }
221   221  
222 // }}} 222 // }}}
223   223  
224 // {{{ getParameterisedSelfUrl 224 // {{{ getParameterisedSelfUrl
225 // 225 //
226 // Get the relative URL (PHP_SELF) with GET and POST data 226 // Get the relative URL (PHP_SELF) with GET and POST data
227   227  
228 function getParameterisedSelfUrl($params = true) 228 function getParameterisedSelfUrl($params = true)
229 { 229 {
230 $url = null; 230 $url = null;
231   231  
232 if ($config->multiViews) 232 if ($config->multiViews)
233 { 233 {
234 // Get rid of the file's name 234 // Get rid of the file's name
235 $url = preg_replace('/\.php/', '', $_SERVER['PHP_SELF'], 1); 235 $url = preg_replace('/\.php/', '', $_SERVER['PHP_SELF'], 1);
236 } 236 }
237 else 237 else
238 { 238 {
239 $url = basename($_SERVER['PHP_SELF']); 239 $url = basename($_SERVER['PHP_SELF']);
240   240  
241 // Sometimes the .php isn't on the end. Damn strange... 241 // Sometimes the .php isn't on the end. Damn strange...
242 if (strchr($url, '.') === false) 242 if (strchr($url, '.') === false)
243 $url .= '.php'; 243 $url .= '.php';
244 } 244 }
245   245  
246 if ($params) 246 if ($params)
247 { 247 {
248 $arr = $_GET + $_POST; 248 $arr = $_GET + $_POST;
249 # XXX: the point of HTTP POST is that URIs have a set size limit, so POST 249 # XXX: the point of HTTP POST is that URIs have a set size limit, so POST
250 # data is typically too large to bother with; why include it? 250 # data is typically too large to bother with; why include it?
251 $url .= '?'.http_build_query($arr); 251 $url .= '?'.http_build_query($arr);
252 } 252 }
253   253  
254 return $url; 254 return $url;
255 } 255 }
256   256  
257 // }}} 257 // }}}
258   258  
259 ?> 259 ?>