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