10 use base
qw(Exporter);
11 our @EXPORT = qw(scrypt jailed_file
13 filedb_atomic_append filedb_atomic_edit filedb_grep
14 filedb_atomic_grep valid_email valid_email_multi
15 valid_repo_url valid_web_url url_path
16 projects_html_list parse_rfc2822_date parse_any_date);
22 crypt($pwd||'', join ('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
28 $Girocco::Config
::chroot."/$filename";
37 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
39 my $handle = new IO
::Handle
;
41 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
43 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
44 ($! == EEXIST
) or die "$path open failed: $!";
45 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
49 # XXX: filedb-specific
50 chmod 0664, $path or die "$path g+w failed: $!";
56 my ($path, $noreplace) = @_;
59 rename "$path.lock", $path or die "$path unlock failed: $!";
61 unlink "$path.lock" or die "$path unlock failed: $!";
65 sub filedb_atomic_append
{
66 my ($file, $line) = @_;
69 open my $src, '<', $file or die "$file open for reading failed: $!";
70 my $dst = lock_file
($file);
73 my $aid = (split /:/)[2];
74 $id = $aid + 1 if ($aid >= $id);
76 print $dst $_ or die "$file(l) write failed: $!";
80 print $dst "$line\n" or die "$file(l) write failed: $!";
82 close $dst or die "$file(l) close failed: $!";
90 sub filedb_atomic_edit
{
93 open my $src, '<', $file or die "$file open for reading failed: $!";
94 my $dst = lock_file
($file);
97 print $dst $fn->($_) or die "$file(l) write failed: $!";
100 close $dst or die "$file(l) close failed: $!";
106 sub filedb_atomic_grep
{
107 my ($file, $fn) = @_;
110 open my $src, '<', $file or die "$file open for reading failed: $!";
111 my $dst = lock_file
($file);
114 my $result = $fn->($_);
115 push(@results, $result) if $result;
118 close $dst or die "$file(l) close failed: $!";
121 unlock_file
($file, 1);
126 my ($file, $fn) = @_;
129 open my $src, '<', $file or die "$file open for reading failed: $!";
132 my $result = $fn->($_);
133 push(@results, $result) if $result;
143 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$/;
145 sub valid_email_multi
{
147 # More relaxed, we just want to avoid too dangerous characters.
148 /^[a-zA-Z0-9+._, @-]+$/;
152 /^https?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~:?&=;-]*)?(#[a-zA-Z0-9._-]+)?$/;
156 /^(https?|git|svn(\+http)?|svn(\+https)?|darcs|bzr):\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
159 # If the passed in argument looks like a URL, return only the stuff following
160 # the host:port part otherwise return the entire argument.
162 my $url = shift || '';
164 $url = $1 if $url =~ m
,^(?
: [A
-Za
-z
][A
-Za
-z0
-9+.-]*: ) # scheme
165 (?
: // ) # // separator
166 (?
: [^\@
]+\@
)?
# optional userinfo
167 (?
: [^/?
#]+ ) # host and port
168 ((?
:[/?#].*)?)$,x; # path and optional query string and/or anchor
174 $str =~ s/\&/\&/gs;
175 $str =~ s/\</\</gs;
176 $str =~ s/\>/\>/gs;
177 $str =~ s/\"/\"/gs; #"
181 # create relative time string from passed in age in seconds
186 if ($age > 60*60*24*365*2) {
187 $age_str = (int $age/60/60/24/365);
188 $age_str .= " years ago";
189 } elsif ($age > 60*60*24*(365/12)*2) {
190 $age_str = int $age/60/60/24/(365/12);
191 $age_str .= " months ago";
192 } elsif ($age > 60*60*24*7*2) {
193 $age_str = int $age/60/60/24/7;
194 $age_str .= " weeks ago";
195 } elsif ($age > 60*60*24*2) {
196 $age_str = int $age/60/60/24;
197 $age_str .= " days ago";
198 } elsif ($age > 60*60*2) {
199 $age_str = int $age/60/60;
200 $age_str .= " hours ago";
201 } elsif ($age > 60*2) {
202 $age_str = int $age/60;
203 $age_str .= " mins ago";
206 $age_str .= " secs ago";
207 } elsif ($age >= 0) {
208 $age_str = "right now";
210 $age_str = "future time";
215 # create relative time string from passed in idle in seconds
217 my $idle_str = _rel_age
(shift);
218 $idle_str =~ s/ ago//;
219 $idle_str = "not at all" if $idle_str eq "right now";
224 use POSIX
qw(strftime);
225 my ($fmt, $secs, $zonesecs) = @_;
226 my ($S,$M,$H,$d,$m,$y) = gmtime($secs + $zonesecs);
227 $zonesecs = int($zonesecs / 60);
229 my $ans = strftime
($fmt, $S, $M, $H, $d, $m, $y, -1, -1, -1);
233 $zonesecs = -$zonesecs;
237 $z .= sprintf("%02d%02d", int($zonesecs/60), $zonesecs % 60);
242 # Take a list of project names and produce a nicely formated table that
243 # includes owner links and descriptions. If the list is empty returns ''.
244 # The first argument may be a hash ref that contains options. The following
245 # options are available:
246 # target -- sets the target value of the owner link
247 # emptyok -- if true returns an empty table rather than ''
248 # typecol -- if true include type column with hover info
249 # changed -- if true include a changed and idle column
250 sub projects_html_list
{
252 if (defined($_[0]) && ref($_[0]) eq 'HASH') {
255 return '' unless @_ || (defined($options->{emptyok
}) && $options->{emptyok
});
256 require Girocco
::Project
;
259 $target = " target=\""._escapeHTML
($options->{target
})."\""
260 if defined($options->{target
});
261 my $withtype = defined($options->{typecol
}) && $options->{typecol
};
262 my $withchanged = defined($options->{changed
}) && $options->{changed
};
264 $typehead = '<th>Type</th>' if $withtype;
266 $chghead = substr(<<EOT, 0, -1) if $withchanged;
267 <th><span class="hover">Changed<span><span class="head">Changed</span
268 />The last time a ref change was received by this site.</span></span></th
269 ><th><span class="hover">Idle<span><span class="head">Idle</span
270 />The most recent committer time in <i>refs/heads</i>.</span></span></th
274 <table class='projectlist'><tr><th>Project</th>$typehead$chghead<th class="desc">Description</th></tr>
276 my $trclass = ' class="odd"';
277 foreach (sort({lc($a) cmp lc($b)} @_)) {
278 if (Girocco
::Project
::does_exist
($_)) {
279 my $proj = Girocco
::Project
->load($_);
280 my $projname = $proj->{name
}.".git";
281 my $projdesc = $proj->{desc
}||'';
282 utf8
::decode
($projdesc) if utf8
::valid
($projdesc);
285 if ($proj->{mirror
}) {
286 $typecol = substr(<<EOT, 0, -1);
287 <td class="type"><span class="hover">mirror<span class="nowrap">@{[_escapeHTML($proj->{url})]}</span></span></td>
290 my $users = @
{$proj->{users
}};
292 $users .= 's' unless @
{$proj->{users
}} == 1;
293 my $userlist = join(', ', sort({lc($a) cmp lc($b)} @
{$proj->{users
}}));
294 my $spncls = length($userlist) > 25 ?
'' : ' class="nowrap"';
295 $typecol = $userlist ?
substr(<<EOT, 0, -1) : substr(<<EOT, 0, -1);
296 <td
class="type"><span
class="hover">$users<span
$spncls>$userlist</span></span
></td
>
298 <td
class="type">$users</td
>
305 my $changetime = $proj->{lastchange
};
307 $rel = "<span class=\"hover\">" .
308 _rel_age
(time - parse_rfc2822_date
($changetime)) .
309 "<span class=\"nowrap\">$changetime</span></span>";
313 $changecol = substr(<<EOT, 0, -1);
314 <td class="change">$rel</td>
316 my $idletime = $proj->{lastactivity
};
318 $idlesecs = parse_any_date
($idletime, \
$tz) if $idletime;
320 my $idle2822 = _strftime
("%a, %d %b %Y %T %z", $idlesecs, $tz);
321 $rel = "<span class=\"hover\">" .
322 _rel_idle
(time - $idlesecs) .
323 "<span class=\"nowrap\">$idle2822</span></span>";
327 $changecol .= substr(<<EOT, 0, -1);
328 <td class="idle">$rel</td>
332 <tr$trclass><td><a href="@{[url_path($Girocco::Config::gitweburl)]}/$projname"$target
333 >@{[_escapeHTML($projname)]}</td>$typecol$changecol<td>@{[_escapeHTML($projdesc)]}</td></tr>
335 $trclass = $trclass ?
'' : ' class="odd"';
342 return ($count || (defined($options->{emptyok
}) && $options->{emptyok
})) ?
$html : '';
348 jan
=> 0, feb
=> 1, mar
=> 2, apr
=> 3, may
=> 4, jun
=> 5,
349 jul
=> 6, aug
=> 7, sep
=> 8, oct => 9, nov
=> 10, dec
=> 11
353 # Should be in "date '+%a, %d %b %Y %T %z'" format as saved to lastgc, lastrefresh and lastchange
354 # The leading "%a, " is optional, returns undef if unrecognized date. This is also known as
355 # RFC 2822 date format and git's '%cD', '%aD' and --date=rfc2822 format.
356 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
357 sub parse_rfc2822_date
{
358 my $dstr = shift || '';
359 my $tzoff = shift || '';
360 $dstr = $1 if $dstr =~/^[^\s]+,\s*(.*)$/;
361 return undef unless $dstr =~
362 /^\s*(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+([+-]\d{4})\s*$/;
363 my ($d,$b,$Y,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7);
364 my $m = $_month_names{lc($b)};
365 return undef unless defined($m);
366 my $seconds = timegm
(0+$S, 0+$M, 0+$H, 0+$d, 0+$m, $Y-1900);
367 my $offset = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
368 $offset = -$offset if substr($z,0,1) eq '-';
369 $$tzoff = $offset if ref($tzoff) eq 'SCALAR';
370 return $seconds - $offset;
373 # Will parse any supported date format. Actually there are three formats
374 # currently supported:
375 # 1. RFC 2822 (uses parse_rfc2822_date)
376 # 2. RFC 3339 / ISO 8601 (T may be ' ', 'Z' is optional, ':' optional in TZ)
377 # 3. unix seconds since epoch with optional +/- trailing TZ (may not have a ':')
378 # Returns undef if unsupported date.
379 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
381 my $dstr = shift || '';
382 my $tzoff = shift || '';
383 if ($dstr =~ /^\s*([-+]?\d+)(?:\s+([-+]\d{4}))?\s*$/) {
389 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
390 $off = -$off if substr($z,0,1) eq '-';
392 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
395 if ($dstr =~ /^\s*(\d{4})-(\d{2})-(\d{2})[Tt ](\d{2}):(\d{2}):(\d{2})(?:[ ]([Zz]|(?:[-+]\d{2}:?\d{2})))?\s*$/) {
396 my ($Y,$m,$d,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7||'');
397 my $seconds = timegm
(0+$S, 0+$M, 0+$H, 0+$d, $m-1, $Y-1900);
401 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
402 $off = -$off if substr($z,0,1) eq '-';
404 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
405 return $seconds - $off;
407 return parse_rfc2822_date
($dstr, $tzoff);