File:  [LON-CAPA] / loncom / interface / lonextresedit.pm
Revision 1.8.2.4: download - view: text, annotated - select for diffs
Tue Jul 30 19:53:22 2019 UTC (4 years, 9 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3
- For 2.11
  Backport 1.28, 1.29

    1: # The LearningOnline Network
    2: # Documents
    3: #
    4: # $Id: lonextresedit.pm,v 1.8.2.4 2019/07/30 19:53:22 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonextresedit;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common :http);
   33: use HTML::Entities;
   34: use Apache::lonlocal;
   35: use Apache::lonnet;
   36: use Apache::loncommon;
   37: use Apache::lonhtmlcommon;
   38: use Apache::lonuserstate;
   39: use LONCAPA::map();
   40: use LONCAPA qw(:DEFAULT :match);
   41: 
   42: sub handler {
   43:     my $r=shift;
   44:     &Apache::loncommon::content_type($r,'text/html');
   45:     $r->send_http_header;
   46: 
   47:     return OK if $r->header_only;
   48: 
   49:     # Check for access
   50:     if (! &Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
   51:         $env{'user.error.msg'}=
   52:             $r->uri.":mdc:0:0:Cannot modify course content.";
   53:             return HTTP_NOT_ACCEPTABLE;
   54:     }
   55: 
   56:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   57:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   58:     my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
   59:     my ($supplementalflag,$updated,$output,$errormsg,$residx,$url,$title,$symb);
   60:     if (($env{'form.folderpath'} =~ /^supplemental/) && ($env{'form.suppurl'})) {
   61:         $supplementalflag = 1;
   62:     }
   63:     if (($supplementalflag) || ($env{'form.symb'} =~ /^uploaded/)) {
   64:         ($updated,$output,$errormsg,$residx,$url,$title,$symb) =
   65:             &process_changes($supplementalflag,$cdom,$cnum,$chome);
   66:         if ($supplementalflag) {
   67:             if ($url ne $env{'form.suppurl'}) {
   68:                  $env{'form.suppurl'} = $url;
   69:             }
   70:             if ($title ne $env{'form.title'}) {
   71:                 $env{'form.title'} = $title;
   72:             }
   73:             $env{'form.idx'} = $residx;
   74:         } else {
   75:             if ($symb ne $env{'form.symb'}) {
   76:                 $env{'form.symb'} = $symb;
   77:             }
   78:         }
   79:     } else {
   80:         $errormsg = &mt('Information about external resource to edit is missing.');
   81:     }
   82:     if ($updated) {
   83:         $output = &Apache::lonhtmlcommon::confirm_success(&mt('External Resource updated'));
   84:     }
   85:     if ($errormsg) {
   86:         $errormsg = '<p class="LC_error">'.$errormsg.'</p>';
   87:     }
   88:     my $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
   89:     my $pathitem = '<input type="hidden" name="folderpath" value="'.
   90:                    &HTML::Entities::encode($env{'form.folderpath'},'<>&"').'" />';
   91:     $r->print(&Apache::loncommon::start_page('External Resource Editor',$js).
   92:               '<div class="LC_left_float">'.
   93:               $output.
   94:               $errormsg.
   95:               &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,
   96:                            'direct',$env{'form.symb'}).
   97:               '</div>'.&Apache::loncommon::end_page());
   98:     return OK;
   99: }
  100: 
  101: sub process_changes {
  102:     my ($supplementalflag,$cdom,$cnum,$chome) = @_;
  103:     my ($folder,$container,$output,$errormsg,$updated,$symb,$oldidx,$oldurl,
  104:         $oldtitle,$newidx,$newurl,$newtitle,$residx,$url,$title);
  105:     if ($env{'form.symb'}) {
  106:         $symb = $env{'form.symb'};
  107:         (my $map,$oldidx,$oldurl)=&Apache::lonnet::decode_symb($symb);
  108:         if ($map =~ m{^uploaded/$cdom/$cnum/(default(_\d+|))\.(sequence|page)$}) {
  109:             $folder = $1;
  110:             $container = $3;
  111:         }
  112:         $oldtitle = &Apache::lonnet::gettitle($env{'form.symb'});
  113:     } elsif ($env{'form.folderpath'}) {
  114:         $folder = &unescape( (split('&',$env{'form.folderpath'}))[-2] );
  115:         $oldurl = &unescape($env{'form.suppurl'});
  116:         $oldtitle = &unescape($env{'form.title'});
  117:         $container = 'sequence';
  118:         $supplementalflag = 1;
  119:     }
  120:     if ($oldurl =~ m{^ext/(.+)$}) {
  121:         my $external = $1; 
  122:         if ($external =~ m{^https://}) {
  123:             $oldurl = $external;
  124:         } else {
  125:             $oldurl = 'http://'.$oldurl;
  126:         }
  127:     }
  128:     $url = $oldurl;
  129:     $title = $oldtitle;
  130:     if ($env{'form.importdetail'}) {
  131:         ($newtitle,$newurl,$newidx) =
  132:             map {&unescape($_)} split(/\=/,$env{'form.importdetail'});
  133:     }
  134:     if ($supplementalflag) {
  135:         $residx = $newidx;
  136:     } else {
  137:         $residx = $oldidx;
  138:     }
  139:     if ($folder && $container) {
  140:         if ($env{'form.importdetail'}) {
  141:             my ($errtext,$fatal,$mismatchedid,@imports);
  142:             if (!$supplementalflag) {
  143:                 if (($oldidx) && ($oldidx != $newidx)) {
  144:                     $mismatchedid = 1;
  145:                 }
  146:             }
  147:             if ($mismatchedid) {
  148:                 $errormsg = 'Wrong item identifier';
  149:             } elsif (($newtitle eq $oldtitle) && ($newurl eq $oldurl)) {
  150:                 $output = &mt('No change');
  151:             } else {
  152:                 my $map = "/uploaded/$cdom/$cnum/$folder.$container";
  153:                 my ($errtext,$fatal) = &LONCAPA::map::mapread($map);
  154:                 if ($fatal) {
  155:                     $errormsg = &mt('Update failed: [_1].',$errtext);
  156:                 } else {
  157:                     my $saveurl = &LONCAPA::map::qtunescape($newurl);
  158:                     my $savetitle = &LONCAPA::map::qtunescape($newtitle);
  159:                     $LONCAPA::map::resources[$residx] =
  160:                         join(':', ($savetitle,$saveurl,'true','normal','res'));
  161:                     my ($outtext,$errtext) = &LONCAPA::map::storemap($map,1);
  162:                     if ($errtext) {
  163:                         $errormsg = &mt('Update failed: [_1].',$errtext);
  164:                     } else {
  165:                         $updated = 1;
  166:                         $title = $newtitle;
  167:                         if ($newurl ne $oldurl) {
  168:                             $url = $newurl;
  169:                             $newurl =~ s{^http://}{};
  170:                             $newurl = "ext/$newurl";
  171:                         }
  172:                         if (!$supplementalflag) {
  173:                             if ($newurl ne $oldurl) {
  174:                                 $symb = &Apache::lonnet::encode_symb($map,$residx,$newurl);
  175:                             } else {
  176:                                 $symb = $env{'form.symb'};
  177:                                 if ($symb) {
  178:                                     &Apache::lonnet::devalidate_title_cache($symb);
  179:                                 }
  180:                             }
  181:                         }
  182:                         my ($furl,$ferr) = 
  183:                             &Apache::lonuserstate::readmap("$cdom/$cnum");
  184:                         if ($ferr) {
  185:                             $errormsg = &mt('Reload failed: [_1].',$ferr);
  186:                         } else {
  187:                             unless ($supplementalflag) {
  188:                                 &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,$cdom.'_'.$cnum);
  189:                             }
  190:                         }
  191:                     }
  192:                 }
  193:             }
  194:         } else {
  195:             $output = &mt('No change');
  196:         }
  197:     } else {
  198:         $errormsg = &mt('Information about current external resource is incomplete.');
  199:     }
  200:     return ($updated,$output,$errormsg,$residx,$url,$title,$symb);
  201: }
  202: 
  203: sub extedit_form {
  204:     my ($supplementalflag,$residx,$orig_url,$orig_title,$pathitem,$helpitem,$caller,$symb,$disabled) = @_;
  205:     my %lt = &Apache::lonlocal::texthash(
  206:         ex => 'External Resource',
  207:         ed => 'Edit',
  208:         ee => 'External Resource Editor',
  209:         pr => 'Preview',
  210:         sv => 'Save',
  211:         ul => 'URL',
  212:         ti => 'Title',
  213:         al => 'Add Link',
  214:     );
  215:     my $formname = 'newext';
  216:     my $tabid = 'aa';
  217:     my $toggle = 'ext';
  218:     my $fieldsetid = 'uploadextform';
  219:     my $urlid = 'exturl';
  220:     my $size = 60;
  221:     if ($supplementalflag) {
  222:         $formname = 'newsuppext';
  223:         $tabid = 'ee';
  224:         $toggle = 'suppext';
  225:         $fieldsetid = 'uploadsuppextform';
  226:         $urlid = 'suppexturl';
  227:     }
  228:     my ($link,$legend,$active,$srcclass,$extsrc,$preview,$title,$save,
  229:         $fieldsetstyle,$action,$hiddenelem,$form);
  230:     $fieldsetstyle = 'display: none;';
  231:     $action = '/adm/coursedocs';
  232:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https':'http');
  233:     if ($residx) {
  234:         if ($caller eq 'direct') {
  235:             $fieldsetstyle = 'display: block;';
  236:             $action = '/adm/extresedit';
  237:             $legend = "<legend>$lt{'ee'}</legend>";
  238:             if ($symb) {
  239:                 $hiddenelem = '<input type="hidden" name="symb" value="'.$symb.'" />';
  240:             } elsif ($supplementalflag) {
  241:                 $hiddenelem = '<input type="hidden" name="suppurl" value="'.
  242:                               &HTML::Entities::encode(&escape($orig_url),'<>&"').'" />'."\n".
  243:                               '<input type="hidden" name="title" value="'.
  244:                               &HTML::Entities::encode(&escape($orig_title),'<>&"').'" />';
  245:             }
  246:         } else {        
  247:             $link = '<a class="LC_docs_ext_edit" href="javascript:editext('."'$residx'".');">'.$lt{'ed'}.'</a>&nbsp;'."\n";
  248:             $size = 40;
  249:             $active = '<input type="hidden" name="active" value="'.$tabid.'" />';
  250:         }
  251:         $formname = "editext_$residx";
  252:         $fieldsetid = "uploadext$residx";
  253:         $urlid = "exturl_$residx";
  254:         $srcclass = ' class="LC_nobreak"';
  255:         $extsrc = '<span class="LC_docs_ext_edit">'.$lt{'ul'}.'&nbsp;</span>';
  256:         $preview = '&nbsp;<a class="LC_docs_ext_edit" href="javascript:extUrlPreview('."'$urlid','$protocol'".');">'.$lt{'pr'}.'</a>';
  257:         $title = '<span class="LC_docs_ext_edit">'.$lt{'ti'}.'&nbsp;</span>';
  258:         $save = $lt{'sv'};
  259:     } else {
  260:         $link = '<a class="LC_menubuttons_link" href="javascript:toggleUpload('."'$toggle'".');">'.$lt{'ex'}.'</a>'.$helpitem;
  261:         $legend = "<legend>$lt{'ex'}</legend>";
  262:         $extsrc = $lt{'ul'}.':<br />';
  263:         $title = $lt{'ti'}.':<br />';
  264:         $residx = 0;
  265:         $orig_url = 'http://';
  266:         $orig_title = $lt{'ex'};
  267:         $preview = '<input type="button" name="view" value="'.$lt{'pr'}.'" onclick="javascript:extUrlPreview('."'$urlid','$protocol'".');"'.$disabled.' />';
  268:         $save = $lt{'al'};
  269:         $pathitem .= '<br />';
  270:     }
  271:     $form = <<ENDFORM;
  272: <form action="$action" method="post" name="$formname">
  273: <fieldset id="$fieldsetid" style="$fieldsetstyle">
  274: $legend
  275: $active
  276: <span$srcclass>
  277: $extsrc
  278: <input type="text" size="$size" name="exturl" id="$urlid" value="$orig_url" $disabled />
  279: $preview
  280: </span> 
  281: <br />
  282: <span$srcclass>
  283: $title
  284: <input type="text" size="$size" name="exttitle" value="$orig_title" $disabled />
  285: <input type="hidden" name="importdetail" value="" />
  286: $pathitem
  287: $hiddenelem
  288: <input type="button" value="$save" onclick="javascript:setExternal(this.form,'$residx');" $disabled />
  289: </span>
  290: </fieldset>
  291: </form>
  292: ENDFORM
  293:     if (wantarray) {
  294:         return ($link,$form);
  295:     } else {
  296:         return $link.$form;
  297:     }
  298: }
  299: 
  300: sub display_editor {
  301:     my ($url,$folderpath,$symb,$idx,$type,$cdom,$cnum,$hostname) = @_;
  302:     my ($residx,$supplementalflag,$title,$pathitem,$output,$js,$navmap);
  303:     if ($folderpath =~ /^supplemental/) {
  304:         $supplementalflag = 1;
  305:         $residx = $idx;
  306:         $title = &unescape($env{'form.title'});
  307:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($folderpath,'<>&"').'" />';
  308:     } elsif ($symb =~ /^uploaded/) {
  309:         (my $map,$residx,my $res) =
  310:             &Apache::lonnet::decode_symb($symb);
  311:         $title = &Apache::lonnet::gettitle($symb);
  312:         my $path = &Apache::loncommon::symb_to_docspath($symb,\$navmap);
  313:         $pathitem = '<input type="hidden" name="folderpath" value="'.&HTML::Entities::encode($path,'<>&"').'" />';
  314:     }
  315:     $js = &Apache::lonhtmlcommon::scripttag(&extedit_javascript());
  316:     my $args = { 'force_register' => $env{'form.register'} };
  317:     if ($hostname) {
  318:         $args->{'hostname'} = $hostname;
  319:     }
  320:     return &Apache::loncommon::start_page('External Resource Editor',$js,$args).
  321:            '<div class="LC_left_float">'.
  322:            &extedit_form($supplementalflag,$residx,$url,$title,$pathitem,undef,'direct',$symb).
  323:            '</div>'.
  324:            &Apache::loncommon::end_page();
  325: }
  326: 
  327: sub extedit_javascript {
  328:     my %js_lt = &Apache::lonlocal::texthash(
  329:         invurl  => 'Invalid URL',
  330:         titbl   => 'Title is blank',
  331:         mixfra  => 'Show preview in pop-up? (http in https page + no framing)',
  332:         mixonly => 'Show preview in pop-up? (http in https page)',
  333:         fraonly => 'Show preview in pop-up? (framing disallowed)',
  334:         nopopup => 'Pop-up blocked',
  335:         nopriv  => 'Insufficient privileges to use preview',
  336:         badurl  => 'URL is not: http://hostname/path or https://hostname/path',
  337:     );
  338:     &js_escape(\%js_lt);
  339: 
  340:     my $urlregexp = <<'ENDREGEXP';
  341: /^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z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F\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
  342: ENDREGEXP
  343: 
  344:     return <<ENDJS;
  345: 
  346: var regexp = $urlregexp;
  347: 
  348: function setExternal(extform,residx) {
  349:     var title=extform.exttitle.value;
  350:     if (!String.trim) {
  351:         String.prototype.trim = function() {return this.replace(\/^\\s+|\\s+$\/g, "");};    }
  352:     var url=extform.exturl.value;
  353:     if (title == null || title.trim()=="") {
  354:         alert("$js_lt{'titbl'}");
  355:         extform.exttitle.focus();
  356:         return;
  357:     }
  358:     if (regexp.test(url)) {
  359:         url = escape(url);
  360:         title = escape(title);
  361:         if (residx > 0) {
  362:             eval("extform.importdetail.value=title+'='+url+'='+residx;extform.submit();");
  363:         } else {
  364:             eval("extform.importdetail.value=title+'='+url;extform.submit();");
  365:         }
  366:     } else {
  367:         alert("$js_lt{'invurl'}");
  368:         extform.exturl.focus();
  369:         return;
  370:     }
  371: }
  372: 
  373: function editext(residx) {
  374:     if (document.getElementById('uploadext'+residx)) {
  375:         var curr = document.getElementById('uploadext'+residx).style.display;
  376:         if (curr == 'none') {
  377:             disp = 'block';
  378:         } else {
  379:             disp = 'none';
  380:         }
  381:         document.getElementById('uploadext'+residx).style.display=disp;
  382:     }
  383:     resize_scrollbox('contentscroll','1','1');
  384:     return;
  385: }
  386: 
  387: function extUrlPreview(caller,protocol) {
  388:     if (document.getElementById(caller)) {
  389:         var url = document.getElementById(caller).value;
  390:         if (regexp.test(url)) {
  391:             var http_regex = /^http\:\/\//gi;
  392:             var mixed = 0;
  393:             var noiframe = 0;
  394:             var nopriv = 0;
  395:             var badurl = 0;
  396:             var name = "externalpreview";
  397:             if ((protocol == 'https') && (http_regex.test(url))) {
  398:                 mixed = 1;
  399:             }
  400:             var http = new XMLHttpRequest();
  401:             var lcurl = "/adm/exturlcheck";
  402:             var params = "exturl="+url;
  403:             http.open("POST",lcurl, true);
  404:             http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  405:             http.onreadystatechange = function() {
  406:                 if (http.readyState == 4) {
  407:                     if (http.status == 200) {
  408:                         if (http.responseText.length > 0) {
  409:                             if (http.responseText == 1) {
  410:                                 noiframe = 1;
  411:                             } else if (http.responseText == -1) {
  412:                                 nopriv = 1;
  413:                             } else if (http.responseText == 0) {
  414:                                 badurl = 1;
  415:                             }
  416:                         }
  417:                         openPreviewWindow(url,name,noiframe,mixed,nopriv,badurl);
  418:                     }
  419:                 }
  420:             }
  421:             http.send(params);
  422:         } else {
  423:             alert("$js_lt{'invurl'}");
  424:         }
  425:     }
  426: }
  427: 
  428: var previewLCWindow = null;
  429: function openPreviewWindow(url,name,noiframe,mixed,nopriv,badurl) {
  430:     if (previewLCWindow !=null) {
  431:         previewLCWindow.close();
  432:     }
  433:     if (badurl) {
  434:         alert("$js_lt{'badurl'}");
  435:     } else if (nopriv) {
  436:         alert("$js_lt{'nopriv'}");
  437:     } else if ((noiframe == 1) || (mixed == 1)) {
  438:         var encurl = encodeURI(url);
  439:         var msg;
  440:         if (mixed == 1) {
  441:             if (noiframe == 1) {
  442:                 msg = "$js_lt{'mixfra'}";
  443:             } else {
  444:                 msg = "$js_lt{'mixonly'}";
  445:             }
  446:         } else {
  447:             msg = "$js_lt{'fraonly'}";
  448:         }
  449:         if (confirm(msg)) {
  450:             previewLCWindow = window.open(url,name,"height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1");
  451:             if (previewLCWindow != null) {
  452:                 previewLCWindow.focus();
  453:             } else {
  454:                 alert("$js_lt{'nopopup'}");
  455:             }
  456:         }
  457:     } else {
  458:         openMyModal(url,500,400,'yes');
  459:     }
  460: }
  461: 
  462: ENDJS
  463: 
  464: }
  465: 
  466: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>