--- loncom/xml/londefdef.pm	2013/06/04 22:20:20	1.444
+++ loncom/xml/londefdef.pm	2013/09/26 22:03:36	1.447
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Tags Default Definition Module 
 #
-# $Id: londefdef.pm,v 1.444 2013/06/04 22:20:20 raeburn Exp $
+# $Id: londefdef.pm,v 1.447 2013/09/26 22:03:36 raeburn Exp $
 # 
 #
 # Copyright Michigan State University Board of Trustees
@@ -599,12 +599,16 @@ sub start_body {
             $header=&Apache::lonmenu::constspaceform();
         }
         if ($env{'request.state'} ne 'published') {
-            $header.=&edit_controls();
+            unless ($env{'form.inhibitmenu'} eq 'yes') {
+                $header.=&edit_controls();
+            }
         }
         if ($env{'request.state'} eq 'construct') {
-            $currentstring.=&Apache::loncommon::head_subbox(
-                                &Apache::loncommon::CSTR_pageheader()
-                               .$header);
+            unless ($env{'form.inhibitmenu'} eq 'yes') {
+                $currentstring.=&Apache::loncommon::head_subbox(
+                                    &Apache::loncommon::CSTR_pageheader()
+                                   .$header);
+            }
         } elsif ($env{'request.state'} eq 'edit') {
             $currentstring.=&Apache::loncommon::head_subbox($header);
         }
@@ -1724,6 +1728,15 @@ sub start_a {
                     if (!&Apache::lonnet::allowed('bre',$linkurl)) {
                         if (&Apache::lonnet::is_on_map($url)) {
                             &Apache::lonxml::extlink($linkurl);
+                        } elsif ($env{'request.course.id'}) {
+                            my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+                            my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+                            if ($linkurl =~ m{^([^/]|/uploaded/$cdom/$cnum/docs/)}) {
+                                my $cleanhref = &Apache::londefdef::clean_docs_httpref($linkurl,$url,$cdom,$cnum);
+                                if ($cleanhref) {
+                                    &Apache::lonxml::extlink($cleanhref);
+                                }
+                            }
                         }
                     }
                 }
@@ -3570,8 +3583,64 @@ sub start_iframe {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+        my ($src,$url,$query);
+        if ($token->[2]->{'src'}) {
+            $src = $token->[2]->{'src'};
+        } elsif ($token->[2]->{'SRC'}) {
+            $src = $token->[2]->{'SRC'};
+        }
+        if ($src) {
+            ($url,$query) = ($src =~ /^([^?]+)\??([^?]*)$/);
+            if ($query =~ /inhibitmenu=yes/) {
+                $currentstring = $token->[4];
+            } else {
+                my $inhibit;
+                if ($url =~ m{^[^/.].*\.x?html?$}) {
+                    $inhibit = 1;
+                } elsif ($url =~ m{^/(uploaded|res)/.*\.x?html?$}) {
+                    $inhibit = 1;
+                }
+                if ($inhibit) {
+                    $currentstring = '<iframe ';
+                    foreach my $attrib (@{$token->[3]}) {
+                        if (lc($attrib) eq 'src') {
+                            if ($query) {
+                                $query.='&amp;inhibitmenu=yes';
+                            } else {
+                                $query = 'inhibitmenu=yes';
+                            } 
+                            $currentstring .= 'src="'.$url.'?'.$query.'" ';
+                        } else {
+                            $currentstring .= lc($attrib).'="'.$token->[2]->{$attrib}.'" ';
+                        }
+                    }
+                    $currentstring =~ s/\s+$//;
+                    $currentstring .= '>';
+                } else {
+                    $currentstring = $token->[4];
+                }
+                if (($url !~ m{^https?://}) && ($env{'request.course.id'})) {
+                    my $docuri = &Apache::lonnet::hreflocation('',$env{'request.filename'});
+                    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+                    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+                    if ($url =~ m{^([^/]|/uploaded/)}) {
+                        my $cleanhref = &Apache::londefdef::clean_docs_httpref($url,$docuri,$cdom,$cnum);
+                        if ($cleanhref) {
+                            &Apache::lonxml::extlink($cleanhref);
+                        }
+                    } elsif (($url =~ m{/res/$LONCAPA::domain_re/}) && ($docuri =~ m{^\Q/uploaded/$cdom/$cnum/docs/\E})) {
+                        if (!&Apache::lonnet::allowed('bre',$url)) {
+                            if (&Apache::lonnet::is_on_map($url)) {
+                                &Apache::lonxml::extlink($url);
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            $currentstring = $token->[4];
+        }
+    }
     return $currentstring;
 }
 
@@ -4519,6 +4588,54 @@ sub latex_header {
 
 }
 
+sub clean_docs_httpref {
+    my ($href,$docuri,$cdom,$cnum) = @_;
+    if ($docuri eq '') {
+        &Apache::lonnet::hreflocation('',$env{'request.filename'});
+    }
+    if ($cdom eq '') {
+        $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    }
+    if ($cnum eq '') {
+        $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    }
+    my $cleanhref;
+    if ($docuri =~ m{^(\Q/uploaded/$cdom/$cnum/docs/\E)(.+/)[^/]+$}) {
+        my $prefix = $1;
+        my $relpath = $2;
+        my ($hrefpath,$fname);
+        if ($href =~ m{^/}) {
+            if ($href =~ m{^\Q$prefix\E(.+/)([^/]+)$}) {
+                $hrefpath = $1;
+                $fname = $2;
+            } else {
+                return $cleanhref;
+            }
+        } else {
+            (my $path,$fname) = ($href =~ m{^(.+)/([^/]*)$});
+            $hrefpath = $relpath.$path;
+        }
+        if ($fname ne '') {
+            my $cleanrelpath;
+            foreach my $dir (split(/\//,$hrefpath)) {
+                next if ($dir eq '.');
+                if ($dir eq '..') {
+                    $cleanrelpath =~ s{([^/]+/)$}{};
+                } else {
+                    $cleanrelpath .= $dir.'/';
+                }
+            }
+            $cleanrelpath =~ s{/$}{};
+            if ($cleanrelpath ne '') {
+                $cleanhref = $prefix.'/'.$cleanrelpath.'/'.$fname;
+            } else {
+                $cleanhref = $prefix.'/'.$fname;
+            }
+        }
+    }
+    return $cleanhref;
+}
+
 =pod
 
 =head1 NAME
@@ -4613,7 +4730,22 @@ described at http://www.lon-capa.org.
      	   I'm in a table....
      	}
 
-
+=item clean_docs_httpref($href,$docuri,$cdom,$cnum)
+        HTML pages uploaded to a course which contain dependencies either from iframes,
+        javascript files or objects (FlashPlayerSwf, MediaSrc, XMPSrc, ConfigurationSrc,
+        and PosterImageSrc) for which dependency is another file uploaded to the same
+        course.
+
+        Required input: 
+        href - dependency (either a relative URL, or an absolute URL)
+        Optional inputs:
+        docuri - URL of HTML page containing the dependency
+        cdom - Course domain
+        cnum - CourseID
+
+        Output:
+        returns an absolute URL constructed from the href provided, and the calling context.
+        (this will be null, if the URL does not begin: /uploaded/$cdom/$cnum/docs/).
 
 =back