#!/usr/bin/perl -wT # F*EX CGI for document exchange # # Author: Ulli Horlacher # BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 } use strict qw(vars subs); use File::Basename; use Digest::MD5 qw(md5_hex); use Cwd qw(abs_path getcwd); use POSIX qw(strftime locale_h); use Fcntl qw(:flock :seek :mode); # POSIX time format needed for HTTP header setlocale(LC_TIME,'POSIX'); # add fex lib (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/; die "$0: no $FEXLIB\n" unless -d $FEXLIB; our $error = 'F*EX document ERROR'; # import from fex.pp and fex.ph our ($spooldir,$docdir,$tmpdir,$akeydir,$akey,$charset); our ($admin,$hostname,$sender_from,$sendmail,$bcc,@remote_domains,@durl); our ($vp,$mrx,$bs,$timeout,$mdomain,$document_exchange,$style); our ($FEXHOME,$MB,$RA); our (@extra_header,@download_hosts); # load common code, local config : $HOME/lib/fex.ph require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n"; our $head = "$ENV{SERVER_NAME} F*EX DOcument eXchange"; my $log = 'dox.log'; my $QS = $ENV{QUERY_STRING} || ''; my $uri = $ENV{PATH_INFO} || ''; my $owner = ''; my $user = 'anonymous'; my $file = ''; my $folder = ''; my $http_auth = ''; my $realm = ''; my $submit = ''; my $action = ''; my $mirror = ''; my $directory = ''; my $parentdir = ''; my $uploaddir = ''; my $newdir = ''; my $newfolder = ''; my $destination = ''; my $allowpublish = ''; my $userupload = ''; my $filedata = ''; my $utypes = 'gif|jpg|png|avi|mp\d|flv|m4v|ogg|tar|tgz|zip|7z|gz|bz2|rar|iso'; my $back = 'back'; my $scroll = ''; my $delete = 'delete'; my $deleteall = 'DELETE'; my $stdn = 'style="text-decoration:none"'; my $al = 'align="left"'; my $ar = 'align="right"'; my @files = (); my @renamefrom = (); my @renameto = (); my %exclude = (); my %PARAM = (); my $dox = $ENV{SCRIPT_NAME}; my $hosturl = "$ENV{PROTO}://$ENV{HTTP_HOST}"; $ENV{PATH} = $ENV{FEXHOME}.'/bin:'.$ENV{PATH}; # $QS =~ s/%([\dA-F]{2})/unpack("a",pack("H2",$1))/ge; $QS = decode_utf8(urldecode($QS)); if (@download_hosts and not ipin($RA,@download_hosts)) { http_die( "Downloads from your host ($RA) are not allowed.", "Contact $ENV{SERVER_ADMIN} for details." ); } if (($ENV{HTTP_AUTHORIZATION}||'') =~ /Basic\s+(\S+)/) { $http_auth = decode_b64($1); } $uri =~ s:%3F:/?/:g; # escape '?' for URL-decoding $uri =~ s/%([\dA-F]{2})/unpack("a",pack("H2",$1))/ge; $uri =~ s:/\?/:%3F:g; # deescape '?' $uri =~ s://+:/:; $uri =~ s:^/::; $akey = $ENV{AKEY}||''; # look for CGI parameters &parse_request; $akey = '' unless -f "$akeydir/$akey/@"; if ($uri =~ m:(.+?)/(.+):) { $owner = $1; $file = $2; $file =~ s:/+$::; $folder = $file; $folder =~ s:/.*::; } else { $owner = $uri; $owner =~ s:/+$::; } $owner =~ s:/::g; if ($file =~ m:(^|/)\.\.(/|$):) { http_die('illegal file name'); } if ($akey and readlink_("$akeydir/$akey") =~ m:.*/(.+):) { my $a = $1; if ($owner) { if ($a ne $owner) { redirect("/$dox"); } } else { $owner = $a; } } my $doxo = "$dox/$owner"; my $doxurl = "$hosturl/$doxo/"; unless ($owner) { redirect("/fup?forward=/$dox"); } unless (-f "$owner/@") { http_die("unknown user $owner"); } # user client ip allowed? if (length($folder) and not $akey) { my $access = "$owner/DOX/$folder/.fexdox/access"; if (-s $access and open $access,$access) { my $noaccess = 1; while (<$access>) { /^#/ and next; s/\s//g; if (ipin($RA,$_)) { $noaccess = 0; last; } } close $access; if ($noaccess) { errorlog("access denied to $owner/DOX/$folder"); http_error(403); } } } # zip archive can be downloaded without authentification if ($file =~ /^[^\/]+\.zip$/) { chdir "$owner/DOX" or http_die("$owner : no documents available"); if (-s $file) { http_output($file) and unlink $file; } else { http_die("$owner : $file not available"); } exit; } my $useraccess = ''; if (length($folder) and (not $akey or readlink_("$akeydir/$akey") !~ m:\Q/$owner\E$:)) { if ((slurp("$owner/DOX/$folder/.fexdox/users")||'') =~ /^[^\s#]+:\S/m) { $useraccess = 'yes'; } if (open my $fdc,"$owner/DOX/$folder/.fexdox/config") { while (<$fdc>) { s/#.*//; $useraccess = lc $1 if /^\s*useraccess\s*=\s*(\w+)/; } close $fdc; } if ($useraccess ne 'yes' and $useraccess ne 'anonymous') { http_header('200 OK'); print html_header($head); pq(qq( '

Access to folder /$folder is restricted to' '$owner

' )); exit; } } &check_auth; if ($akey) { my $doxua = lc readlink_("$owner/\@DOCUMENT_EXCHANGE"); if ($doxua eq 'yes' or not $doxua and $document_exchange) { mkdirp("$owner/DOX"); } } chdir "$owner/DOX" or http_die("DOX not available for $owner"); if ($file) { if (not -d $file and -d "$file!backup") { $file = "$file!backup"; } elsif ("/$file/" =~ m:\Q/$folder/UPLOAD/:) { if ($akey) { $file =~ s:/UPLOAD.*:/.upload:; redirect("/$doxo/$file/"); } elsif (-d "$folder/.upload/$user") { $file =~ s:\Q$folder\E/UPLOAD:$folder/.upload/$user:; $userupload = $file; mkdirp($userupload); } } mkdir $file if $file =~ m:^\Q$folder/.upload\E$:; if (not -e $file) { $file =~ s:(_$vp)/:$1!backup/:; unless (-e $file) { $file =~ s:!backup::; http_die("$owner : /$file does not exist"); } } } if (length($folder)) { mkfexdox($folder); foreach ('.*','#*','*~') { $exclude{$_} = shp2prx($_); } if (open my $fdx,"$folder/.fexdox/private") { while (<$fdx>) { next if /^#/; s/[\r\n]//g; s://+:/:; s:^/\Q$folder:/:; s:/$::; $exclude{$_} = shp2prx($_) if length; } close $fdx; } if (open my $fdc,"$folder/.fexdox/config") { while (<$fdc>) { s/#.*//; $charset = $1 if /^\s*charset\s*=\s*(\S+)/; } close $fdc; } } if ($action eq 'okey' and -d $file) { okey($file); exit; } if ($action eq 'listit' and -d $file) { list($file); exit; } if ($action eq 'userlist' and -d $file) { userlist($file); exit; } if ($action eq 'zipit' and -d $file) { zipdir($file); exit; } if ($action eq 'syncit' and -d $file) { syncdir($file); exit; } if ($action eq 'streamit' and -d $file) { streamdir($file); exit; } if ($action eq 'fexit' and -d $file) { fexdir($file,$user); exit; } if ($submit eq 'fex it' and -d $file and @files) { fexfiles(@files,$user); exit; } if ($submit eq 'zip it' and -d $file and @files) { zipfiles(@files); exit; } if ($submit eq 'delete' and -d $file and @files) { delfiles(@files); doxdu($owner,$folder); my $dir = $file; unless ($akey) { $dir =~ s:\Q/.upload/$user:/UPLOAD:; } # $dir = urlencode($dir); redirect("$doxurl$dir/"); } if ($submit eq 'publish' and @files) { publish(@files); my $dir = $file; $dir =~ s:/\.upload/[^/]+::; redirect("$doxurl$dir/"); } if ($submit eq 'copy' and -d $file and -d $destination and @files) { copyfiles(@files); redirect("$doxurl$destination/"); } if ($submit eq 'move' and -d $file and -d $destination and @files) { movefiles(@files); redirect($doxurl.urlencode($file).'/'); } if ($submit eq 'rename' and -d $file and @files) { queryrename(@files); exit; } if (@renamefrom and @renameto) { renamefiles(@files); redirect($doxurl.urlencode($file).'/'); } if ($submit eq 'save') { $filedata .= "\n" if length($filedata) and $filedata !~ /\n$/; savefile($file); redirect($doxurl.urlencode(dirname($file)).'/'); } if (-d $file and $user and $action eq 'sendfile') { sendfile($file,$filedata); &reexec; } if ($user and $action eq 'notify') { notify_owner(); exit; } if ($akey and $mirror) { mirror(); exit; } if ($parentdir and $newdir) { if ($akey) { my $dir = "$parentdir/$newdir"; mkdir $dir or http_die("cannot mkdir $dir - $!"); update_folder($1) if $dir =~ m:([^/]+):; redirect("/$doxo/$parentdir/"); } else { if ($parentdir !~ m:^\Q$folder/.upload/$user:) { http_die("illegal parentdir $parentdir"); } my $dir = "$parentdir/$newdir"; mkdir $dir or http_die("cannot mkdir $dir - $!"); $dir = $parentdir; $dir =~ s:\Q/.upload/$user:/UPLOAD:; redirect("/$doxo/$dir/"); } } if ($submit and $uploaddir) { redirect("/$dox") unless $akey; my $dir = "$folder/.upload/$uploaddir"; mkdirp($dir); redirect("/$doxo/$folder/.upload/"); } if ($allowpublish) { redirect("/$dox") unless $akey; my $user = $allowpublish; my $dir = "$folder/.upload/$user"; unless (-d $dir) { http_die("$dir does not exist"); } my $publish = "$folder/.upload/$user!publish"; open $publish,'>',$publish or http_die("$publish - $!"); print {$publish} "# User $user can publish his uploads (move to /$folder/)\n"; close $publish; redirect("/$doxo/$folder/.upload/"); } if ($newfolder) { redirect("/$dox") unless $akey; if (-d $newfolder) { http_die("$newfolder does already exist"); } my @d = localtime time; my $nv = sprintf( '%s_%d%02d%02d_%02d%02d%02d', $newfolder,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0] ); mkdir $nv or http_die("cannot mkdir $nv - $!"); symlink $nv,$newfolder or http_die("cannot symlink $nv - $!"); redirect("/$dox"); } if ($QS =~ /ACTION=logout/) { unlink "$akeydir/$akey" if $akey; redirect("/$dox"); } if ($akey and $QS =~ /ACTION=edit/) { edit($file); exit; } if ($akey and $QS =~ /ACTION=logview/) { logview($file); exit; } if ($file and $QS eq 'ACTION=publishall') { publish_all($file); redirect("/$doxo/$folder/"); exit; } if ($file and $QS eq 'ACTION=backup') { redirect("/$dox") unless $akey; $folder = basename($file); if (-d $folder and my $ov = untaint(readlink_($folder))) { my @d = localtime time; my $nv = sprintf( '%s_%d%02d%02d_%02d%02d%02d', $folder,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0] ); my $backup = "$ov!backup"; unless (-d $backup) { $_ = `cp -al $ov $backup 2>&1`; http_die($_) if $?; } backup_cleanup($folder); redirect("/$dox"); } else { http_die("unknown folder /$folder"); } } elsif ($file and $QS eq 'ACTION=restore') { redirect("/$dox") unless $akey; $file =~ s:/::; if (-d $file and $file =~ /(.+)_$vp(!backup)?$/ and -l $1) { $folder = $1; if ($file =~ s/!backup$//) { } else { $_ = `cp -al $file $file!backup 2>&1`; http_die($_) if $?; } unlink $folder; symlink $file,$folder; backup_cleanup($folder); redirect("/$dox"); } else { http_die("unknown backup /$file"); } } elsif ($file and $QS eq 'ACTION=delete') { redirect("/$dox") unless $akey; if ($file !~ m:/:) { if (-l $file) { # delete all versions, backup and stream if (my @v = grep /^\Q$file\E_$vp(!\S+)?$/,glob "${file}_*") { foreach my $v (@v) { rmrf(untaint($v)); } } } elsif (-d $file and $file =~ /(.+)_$vp$/) { $folder = $1; if (-l $folder and my @v = grep /^\Q$folder\E_$vp$/,glob "${folder}_*") { if (scalar(@v) == 1) { unlink $folder; } elsif (readlink_($folder) eq $file) { unlink $folder; if ($v[-1] ne $file) { symlink untaint($v[-1]),$folder; } else { symlink untaint($v[-2]),$folder; } } } } } rmrf($file); doxdu($owner,$folder); redirect("/$dox/".dirname($uri)); } elsif ($file =~ m:([^/]+): and -d $1) { if ($akey and $file eq "$folder/.fexdox" and not -d $file) { mkfexdox($folder); } if (-d $file) { if ($uri !~ m:/$:) { redirect("/$dox/$uri/"); } else { showindex($file); } } elsif (-f $file) { sleep(2); http_output($file); } else { http_die("$owner : $file is not available"); } } elsif ($akey) { showindex('/'); } else { http_die("$owner : $file is not available"); } &reexec; exit; sub check_auth { my $id = readline1("$spooldir/$owner/@")||''; my ($users); my (%pw,%access); if ($akey) { if (-f "$akeydir/$akey/@") { $user = $owner; return; } else { $akey = ''; } } if ($http_auth =~ /^(fexmaster.*):/) { my $fm = $1; $fm .= '@'.$mdomain if $mdomain and $fm !~ /@/; $id = readline1("$spooldir/$fm/@")||''; if ($http_auth =~ /:$id$/) { $akey = 'fexmaster'; if ($folder) { $realm = "/$owner/$folder"; } else { $realm = "/$owner"; } return; } } if ($folder) { $realm = "/$owner/$folder"; } else { if ($QS =~ /^realm=([^\s\'\"]+)/) { http_header( '401 Authorization Required', "WWW-Authenticate: Basic realm=\"$1\"", 'Content-Length: 0', ); &reexec; } else { redirect("/fup?from=$owner?forward=/$dox/$owner"); } } if ($http_auth eq "$owner:$id") { $user = $owner; return; } return if $useraccess eq 'anonymous'; if (open $users,"$spooldir/$owner/DOX/$folder/.fexdox/users") { while (<$users>) { /^#/ and next; s/\s//g; my ($user,$pw) = split ':'; $pw{lc($user)} = $pw if $pw; } close $users; } if ($http_auth =~ /(.+):(.+)/) { my $u = lc $1; my $p = $2; if ($pw{$u} and $pw{$u} eq $p) { $user = $u; return; } } http_header( '401 Authorization Required', "WWW-Authenticate: Basic realm=\"$realm\"", 'Content-Length: 0', ); &reexec; } sub http_output { my $file = shift; my ($filename,$size,$total_size,$range); my ($data,$type); my ($var,$env,$con); my @files; my @s; my $s = 0; my $b = 0; my $seek = 0; my $stop = 0; local $_; if (-l $file or not -f $file or $file =~ m:/\.: and not $akey) { http_error(404); } # reget? if ($range = $ENV{HTTP_RANGE}) { $seek = $1 if $range =~ /^bytes=(\d+)-/i; $stop = $1 if $range =~ /^bytes=\d*-(\d+)/i; } if (not $akey and excluded($file)) { http_die("no access for /$file"); } if ($file =~ m:^/: or "/$file/" =~ m:\Q/../:) { http_die("illegal file name $file"); } open $file,'<',$file or http_error(400); $type = 'application/octet-stream'; if ($file =~ /\.html?$/i) { $type = 'text/html' } elsif ($file =~ /\.css$/) { $type = 'text/css' } elsif ($file =~ /\.js$/) { $type = 'text/javascript' } elsif ($file =~ /\.txt$/i) { $type = 'text/plain' } elsif ($file =~ /\.ps$/) { $type = 'application/postscript' } elsif ($file =~ /\.pdf$/i) { $type = 'application/pdf' } elsif ($file =~ /\.mp3$/i) { $type = 'audio/mpeg' } elsif ($file =~ /\.mp4$/i) { $type = 'video/mp4' } elsif ($file =~ /\.avi$/i) { $type = 'video/x-msvideo' } elsif ($file =~ /\.jpg$/i) { $type = 'image/jpeg' } elsif ($file =~ /\.png$/i) { $type = 'image/png' } elsif ($file =~ /\.gif$/i) { $type = 'image/gif' } elsif ($file =~ /\.bmp$/i) { $type = 'image/x-ms-bmp' } elsif ($file =~ /\.ico$/i) { $type = 'image/vnd.microsoft.icon' } elsif ($file !~ /\.(tar|tgz|zip|jar|rar|arj|7z|bz2?|gz)$/i) { my $qfile = shellquote(untaint($file)); $_ = `file $qfile 2>/dev/null`; if (/:.*HTML/) { $type = 'text/html'; } elsif (/:.*text/i and not -x $file) { $type = 'text/plain'; if (/\sASCII\s/) { $type .= "; charset=us-ascii" } elsif (/(ISO-[\w-]+)/) { $type .= "; charset=".lc($1) } else { $type .= "; charset=utf-8" } } } $type = 'text/plain' if $type eq 'text/html'; $total_size = -s $file || 0; $size = $total_size - $seek - ($stop ? $total_size-$stop-1 : 0); if ($size < 0) { http_header('416 Requested Range Not Satisfiable'); exit; } alarm($timeout*10); if ($seek or $stop) { my $range; if ($stop) { $range = sprintf("bytes %s-%s/%s",$seek,$stop,$total_size); } else { $range = sprintf("bytes %s-%s/%s",$seek,$total_size-1,$total_size); } nvt_print( 'HTTP/1.1 206 Partial Content', 'Server: fexsrv', "Content-Length: $size", "Content-Range: $range", "Content-Type: $type", ); } else { my $date = http_date($file); nvt_print( 'HTTP/1.1 200 OK', 'Server: fexsrv', "Last-Modified: $date", "Expires: 0", "Content-Length: $size", "Content-Type: $type", ); } nvt_print($_) foreach(@extra_header); nvt_print(''); # binary data # can be stream! seek $file,$seek,0 if $seek; while ($bs and $b = read($file,$data,$bs)) { if ($stop and $s+$b > $size) { $b = $size-$s; $data = substr($data,0,$b); $bs = 0; } $s += $b; alarm($timeout*10); print $data or last; } doxlog(sprintf "%s %s/%s",hexencode($file),$s,$size) if $s; alarm(0); close $file; return($s+$seek == $total_size); } # show directory index sub showindex { my $dir = shift; my ($htmldoc,$size,$dirhe,$path); my $pathhref = ''; my @links = (); my @dirs = (); my %versions = (); my %folder = (); my @files = (); my @users = (); my $allowed; my $uid = randstring(8); my $js = slurp("$docdir/DOX/dox.js"); my $showdel; my $v; local $_; $dir =~ s://+:/:; $dir =~ s:^/::; $dir =~ s:/$::; $dir ||= '.' if $akey; my $udir = urlencode($dir); $js =~ s/\$RANDOM\$/$uid/g; $htmldoc = html_header($head); $htmldoc .= qqq(qq( '' )); if ($akey and $dir eq '.') { # toplevel folders view opendir $dir,$dir or http_die("$dir - $!"); while (defined($_ = readdir $dir)) { next if /^[.#]/; if ($v = readlink and $v =~ /$vp$/) { $folder{$v} = $_; } if (-d and /((.+_$vp)!backup)$/ and not -d $2) { rename $1,$2 and s/!backup$//; } if (-d and /(.+)_($vp(!backup)?)$/) { push @{$versions{$1}},$2; } } closedir $dir; $style = qqq(qq( 'table {' ' border-collapse: collapse;' '}' 'th,td {' ' border: 1px solid black;' ' padding: 10px;' '}' )); $htmldoc .= qq'\n'; $htmldoc .= '

'; if ($akey eq 'fexmaster') { $htmldoc .= "DOX of $owner ($akey access)\n"; } else { $htmldoc .= "DOX of $owner\n"; } $htmldoc .= '

'; my $logout = button("/$dox?ACTION=logout",'logout'); $htmldoc .= qqq(qq( '' '$logout' ' ' '[documentation]' '

' '' '' )); foreach my $d (asort(keys %versions)) { my $kb = readlink(".$d.du")||0; my ($mb,$date); $mb = int($kb/1024) || '<1'; $showdel = 'a'; if (open my $fdc,"$d/.fexdox/config") { while (<$fdc>) { s/#.*//; $showdel = '' if /^\s*showdelete\s*=\s*no/; $showdel = 'a' if /^\s*showdelete\s*=\s*all/; $showdel = 'b' if /^\s*showdelete\s*=\s*backup/; } close $fdc; } if (readlink_($d) =~ /_(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)$/) { $date = "$1-$2-$3 $4:$5:$6"; } else { $date = "????-??-?? ??:??:??"; } $htmldoc .= qq'\n"; } $htmldoc .= "
foldersizelast modificationbackup versionsfolder actions
/$d'; $htmldoc .= qq'$mb MB$date'; @{$versions{$d}} = sort @{$versions{$d}}; foreach (reverse @{$versions{$d}}) { my $vd = my $v = $_; local $_; my $dv = $d.'_'.$v; $vd = "$1-$2-$3 $4:$5:$6" if $v =~ /(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)/; next if $folder{$dv}; if ($v =~ s/!backup//g) { next if readlink_($d) !~ /$v$/; my $href = "javascript:alert('folder $d version is already $v')"; my $sv = $d.'_'.$v; $htmldoc .= qq'$v '. button($href,'restore'); } else { my $sv = $d.'_'.$v; $htmldoc .= qq'$v '. button("/$doxo/$dv?ACTION=restore",'restore'); } if ($showdel =~ /^[ab]/) { $htmldoc .= ' '.button("/$doxo/$dv?ACTION=delete",$delete); } $htmldoc .= '
'; } $htmldoc .= qq'
'; if (-d readlink_($d).'!backup') { my $href = "javascript:alert('backup of folder $d is up to date')"; $htmldoc .= button($href,'backup'); } else { $htmldoc .= button("/$doxo/$d?ACTION=backup",'backup'); } if ($showdel eq 'a') { $htmldoc .= ' ' . button("/$doxo/$d?ACTION=delete",$deleteall); } $htmldoc .= "
\n

\n"; $htmldoc .= qqq(qq( '

' '

' )); } elsif ($akey and $dir eq "$folder/.upload") { $htmldoc .= "

"; $htmldoc .= qq'DOX of $owner '; $htmldoc .= $akey eq 'fexmaster' ? "($akey access)": "(logged in as owner)"; $htmldoc .= "

\n"; opendir $dir,$dir or http_die("$dir - $!"); while (defined($_ = readdir $dir)) { next if /^\.\.?$/; next if /\n/; next if -l "$dir/$_"; if (lstat "$dir/$_") { if (-d _) { push @dirs,$_ } elsif (-f _) { push @files,$_ } } } closedir $dir; my $action = "/$doxo/".urlencode($dir)."?ACTION"; my $pathhref = qq'$folder/.upload'; if (@dirs or @files) { $htmldoc .= button("$action=listit",'list it'); $htmldoc .= '   ' . button("$action=zipit",'zip it'); $htmldoc .= '   ' . button("$action=fexit",'fex it'); $htmldoc .= '   ' . button("$action=syncit",'sync it') if $akey; $htmldoc .= '   ' . button("$action=streamit",'stream it'); } $htmldoc .= '   ' . button("/$doxo/$file",'reload'); $htmldoc .= '   ' . button("/$doxo$realm",'folders overview'); $htmldoc .= '   ' . button("/$dox?ACTION=logout",'logout'); $htmldoc .= '   '; $htmldoc .= qq'[documentation]\n'; $htmldoc .= qqq(qq( '

/$pathhref/

' '


' )); $htmldoc .= "

"; my @users = (); if (open my $users,"$folder/.fexdox/users") { while (<$users>) { s/#.*//; s/\s//g; push @users,$1 if /^([^\/]+?@[\w.-]+):./; } close $users; } my $selection = '"; if ($selection =~ /