Subversion Repositories oidplus

Rev

Rev 635 | Rev 672 | Go to most recent revision | View as "text/javascript" | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * OIDplus 2.0
  3.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. var OIDplusPagePublicObjects = {
  19.  
  20.         oid: "1.3.6.1.4.1.37476.2.5.2.4.1.0",
  21.  
  22.         generateRandomUUID: function() {
  23.                 $.ajax({
  24.                         url:"ajax.php",
  25.                         method:"POST",
  26.                         beforeSend: function(jqXHR, settings) {
  27.                                 $.xhrPool.abortAll();
  28.                                 $.xhrPool.add(jqXHR);
  29.                         },
  30.                         complete: function(jqXHR, text) {
  31.                                 $.xhrPool.remove(jqXHR);
  32.                         },
  33.                         data: {
  34.                                 csrf_token:csrf_token,
  35.                                 plugin:OIDplusPagePublicObjects.oid,
  36.                                 action:"generate_uuid"
  37.                         },
  38.                         error:function(jqXHR, textStatus, errorThrown) {
  39.                                 if (errorThrown == "abort") return;
  40.                                 alert(_L("Error: %1",errorThrown));
  41.                         },
  42.                         success:function(data) {
  43.                                 if ("error" in data) {
  44.                                         alert(_L("Error: %1",data.error));
  45.                                 } else if (data.status >= 0) {
  46.                                         if (data.status == 0/*OK*/) {
  47.                                                 $("#id").val(data.intval);
  48.                                                 alert(_L("OK! Generated UUID %1 which resolves to OID %2", data.uuid, "2.25."+data.intval));
  49.                                         } else {
  50.                                                 alert(_L("Error: %1",data.status));
  51.                                         }
  52.                                 } else {
  53.                                         alert(_L("Error: %1",data));
  54.                                 }
  55.                         }
  56.                 });
  57.         },
  58.  
  59.         cbRemoveTinyMCE: function(selector) {
  60.                 if ((typeof tinymce == "undefined") || (tinymce == null)) {
  61.                         // This should not happen
  62.                         console.error("OIDplusPagePublicObjects.cbRemoveTinyMCE(): TinyMCE is missing?!");
  63.                         return;
  64.                 }
  65.                 tinymce.remove(selector); // here, we need a "#" (selector contains "#")
  66.         },
  67.  
  68.         cbQueryTinyMCE: function(selector) {
  69.                 // tinymce.get() does NOT allow a "#" prefix (but selector contains "#"). So we remove it
  70.                 selector = selector.replace('#', '');
  71.  
  72.                 if ((typeof tinymce == "undefined") || (tinymce == null) || (tinymce.get(selector) == null)) {
  73.                         // This should not happen
  74.                         console.error("OIDplusPagePublicObjects.cbQueryTinyMCE(): TinyMCE is missing?!");
  75.                         return true;
  76.                 }
  77.                 if (tinymce.get(selector).isDirty()) {
  78.                         return confirm(_L("Attention: Do you want to continue without saving?"));
  79.                 } else {
  80.                         return true;
  81.                 }
  82.         },
  83.  
  84.         checkMissingOrDoubleASN1: function(oid) {
  85.                 var suffix = (oid == '') ? '' : '_'+oid;
  86.  
  87.                 //var curinput = $('#asn1ids'+suffix').value;
  88.                 var curinput = $('input[id="asn1ids'+suffix+'"]')[0];
  89.  
  90.                 if (typeof curinput == "undefined") return true;
  91.  
  92.                 if (curinput.value == '') {
  93.                         // TODO: maybe we should only warn if ASN.1, IRI and Comment are all null, not just ASN.1?
  94.                         if (!confirm(_L("Attention: You did not enter an ASN.1 identifier. Are you sure that you want to continue?"))) return false;
  95.                 }
  96.  
  97.                 var ary = curinput.value.split(',');
  98.  
  99.                 for (var i=0; i<ary.length; i++) {
  100.                         var toCheck = ary[i];
  101.                         var bry = $('input[id^="asn1ids_"]');
  102.                         for (var j=0; j<bry.length; j++) {
  103.                                 if (bry[j].id != 'asn1ids'+suffix) {
  104.                                         var cry = bry[j].value.split(',');
  105.                                         for (var k=0; k<cry.length; k++) {
  106.                                                 var candidate = cry[k];
  107.                                                 if ((toCheck != "") && (candidate != "") && (toCheck == candidate)) {
  108.                                                         if (!confirm(_L("Warning! ASN.1 ID %1 is already used in another OID. Continue?", candidate))) return false;
  109.                                                 }
  110.                                         }
  111.                                 }
  112.                         }
  113.                 }
  114.  
  115.                 return true;
  116.         },
  117.  
  118.         crudActionInsert: function(parent) {
  119.                 if (parent.startsWith('oid:') && !OIDplusPagePublicObjects.checkMissingOrDoubleASN1('')) return;
  120.  
  121.                 $.ajax({
  122.                         url:"ajax.php",
  123.                         method:"POST",
  124.                         beforeSend: function(jqXHR, settings) {
  125.                                 $.xhrPool.abortAll();
  126.                                 $.xhrPool.add(jqXHR);
  127.                         },
  128.                         complete: function(jqXHR, text) {
  129.                                 $.xhrPool.remove(jqXHR);
  130.                         },
  131.                         data: {
  132.                                 csrf_token:csrf_token,
  133.                                 plugin:OIDplusPagePublicObjects.oid,
  134.                                 action:"Insert",
  135.                                 id:$("#id")[0].value,
  136.                                 ra_email:$("#ra_email")[0].value,
  137.                                 comment:$("#comment")[0].value,
  138.                                 asn1ids:($("#asn1ids")[0] ? $("#asn1ids")[0].value : null),
  139.                                 iris:($("#iris")[0] ? $("#iris")[0].value : null),
  140.                                 confidential:($("#hide")[0] ? $("#hide")[0].checked : null),
  141.                                 weid:($("#weid")[0] ? $("#weid")[0].checked : null),
  142.                                 parent:parent
  143.                         },
  144.                         error:function(jqXHR, textStatus, errorThrown) {
  145.                                 if (errorThrown == "abort") return;
  146.                                 alert(_L("Error: %1",errorThrown));
  147.                         },
  148.                         success:function(data) {
  149.                                 if ("error" in data) {
  150.                                         alert(_L("Error: %1",data.error));
  151.                                 } else if (data.status >= 0) {
  152.                                         if (data.status == 0/*OK*/) {
  153.                                                 if (confirm(_L("Insert OK.")+"\n\n"+_L("Do you want to open the newly created object now?"))) {
  154.                                                         openAndSelectNode(data.inserted_id, parent);
  155.                                                         return;
  156.                                                 }
  157.                                         }
  158.  
  159.                                         if ((data.status & 1) == 1/*RaNotExisting*/) {
  160.                                                 if (confirm(_L("Insert OK. However, the email address you have entered (%1) is not in our system. Do you want to send an invitation, so that the RA can register an account to manage their OIDs?",$("#ra_email")[0].value))) {
  161.                                                         OIDplusPagePublicObjects.crudActionSendInvitation(parent, $("#ra_email_"+id)[0].value);
  162.                                                         return;
  163.                                                 } else {
  164.                                                         if (confirm(_L("Do you want to open the newly created object now?"))) {
  165.                                                                 openAndSelectNode(data.inserted_id, parent);
  166.                                                                 return;
  167.                                                         }
  168.                                                 }
  169.                                         }
  170.  
  171.                                         if ((data.status & 2) == 2/*RaNotExistingNoInvitation*/) {
  172.                                                 if (confirm(_L("Insert OK.")+"\n\n"+_L("Do you want to open the newly created object now?"))) {
  173.                                                         openAndSelectNode(data.inserted_id, parent);
  174.                                                         return;
  175.                                                 }
  176.                                         }
  177.  
  178.                                         if ((data.status & 4) == 4/*IsWellKnownOID*/) {
  179.                                                 if (confirm(_L("Insert OK. However, the RA and the ASN.1 and IRI identifiers were overwritten, because this OID is a well-known OID.")+"\n\n"+_L("Do you want to open the newly created object now?"))) {
  180.                                                         openAndSelectNode(data.inserted_id, parent);
  181.                                                         return;
  182.                                                 }
  183.                                         }
  184.  
  185.                                         // TODO: Don't use reloadContent(); instead add a node at the tree at the left add at the right add a new row to the table
  186.                                         reloadContent();
  187.                                 } else {
  188.                                         alert(_L("Error: %1",data));
  189.                                 }
  190.                         }
  191.                 });
  192.         },
  193.  
  194.         crudActionUpdate: function(id, parent) {
  195.                 if (id.startsWith('oid:') && !OIDplusPagePublicObjects.checkMissingOrDoubleASN1(id)) return;
  196.  
  197.                 $.ajax({
  198.                         url:"ajax.php",
  199.                         method:"POST",
  200.                         beforeSend: function(jqXHR, settings) {
  201.                                 $.xhrPool.abortAll();
  202.                                 $.xhrPool.add(jqXHR);
  203.                         },
  204.                         complete: function(jqXHR, text) {
  205.                                 $.xhrPool.remove(jqXHR);
  206.                         },
  207.                         data: {
  208.                                 csrf_token:csrf_token,
  209.                                 plugin:OIDplusPagePublicObjects.oid,
  210.                                 action:"Update",
  211.                                 id:id,
  212.                                 ra_email:$("#ra_email_"+id)[0].value,
  213.                                 comment:$("#comment_"+id)[0].value,
  214.                                 asn1ids:($("#asn1ids_"+id)[0] ? $("#asn1ids_"+id)[0].value : null),
  215.                                 iris:($("#iris_"+id)[0] ? $("#iris_"+id)[0].value : null),
  216.                                 confidential:($("#hide_"+id)[0] ? $("#hide_"+id)[0].checked : null),
  217.                                 parent:parent
  218.                         },
  219.                         error:function(jqXHR, textStatus, errorThrown) {
  220.                                 if (errorThrown == "abort") return;
  221.                                 alert(_L("Error: %1",errorThrown));
  222.                         },
  223.                         success:function(data) {
  224.                                 if ("error" in data) {
  225.                                         alert(_L("Error: %1",data.error));
  226.                                 } else if (data.status >= 0) {
  227.                                         if (data.status == 0/*OK*/) {
  228.                                                 alert(_L("Update OK"));
  229.                                         }
  230.  
  231.                                         if ((data.status & 1) == 1/*RaNotExisting*/) {
  232.                                                 if (confirm(_L("Update OK. However, the email address you have entered (%1) is not in our system. Do you want to send an invitation, so that the RA can register an account to manage their OIDs?",$("#ra_email_"+id)[0].value))) {
  233.                                                         OIDplusPagePublicObjects.crudActionSendInvitation(parent, $("#ra_email_"+id)[0].value);
  234.                                                         return;
  235.                                                 }
  236.                                         }
  237.  
  238.                                         if ((data.status & 2) == 2/*RaNotExistingNoInvitation*/) {
  239.                                                 alert(_L("Update OK"));
  240.                                         }
  241.  
  242.                                         if ((data.status & 4) == 4/*IsWellKnownOID*/) {
  243.                                                 alert(_L("Update OK. However, the RA and the ASN.1 and IRI identifiers were overwritten, because this OID is a well-known OID."));
  244.                                         }
  245.  
  246.                                         // reloadContent();
  247.                                         $('#oidtree').jstree("refresh");
  248.                                 } else {
  249.                                         alert(_L("Error: %1",data));
  250.                                 }
  251.                         }
  252.                 });
  253.         },
  254.  
  255.         crudActionDelete: function(id, parent) {
  256.                 if(!window.confirm(_L("Are you sure that you want to delete %1?",id))) return false;
  257.  
  258.                 $.ajax({
  259.                         url:"ajax.php",
  260.                         method:"POST",
  261.                         beforeSend: function(jqXHR, settings) {
  262.                                 $.xhrPool.abortAll();
  263.                                 $.xhrPool.add(jqXHR);
  264.                         },
  265.                         complete: function(jqXHR, text) {
  266.                                 $.xhrPool.remove(jqXHR);
  267.                         },
  268.                         data: {
  269.                                 csrf_token:csrf_token,
  270.                                 plugin:OIDplusPagePublicObjects.oid,
  271.                                 action:"Delete",
  272.                                 id:id,
  273.                                 parent:parent
  274.                         },
  275.                         error:function(jqXHR, textStatus, errorThrown) {
  276.                                 if (errorThrown == "abort") return;
  277.                                 alert(_L("Error: %1",errorThrown));
  278.                         },
  279.                         success:function(data) {
  280.                                 if ("error" in data) {
  281.                                         alert(_L("Error: %1",data.error));
  282.                                 } else if (data.status >= 0) {
  283.                                         reloadContent();
  284.                                         // TODO: Don't use reloadContent(); instead delete node at the left tree and remove the row at the right table
  285.                                 } else {
  286.                                         alert(_L("Error: %1",data.error));
  287.                                 }
  288.                         }
  289.                 });
  290.         },
  291.  
  292.         updateDesc: function() {
  293.                 $.ajax({
  294.                         url:"ajax.php",
  295.                         method:"POST",
  296.                         beforeSend: function(jqXHR, settings) {
  297.                                 $.xhrPool.abortAll();
  298.                                 $.xhrPool.add(jqXHR);
  299.                         },
  300.                         complete: function(jqXHR, text) {
  301.                                 $.xhrPool.remove(jqXHR);
  302.                         },
  303.                         data: {
  304.                                 csrf_token:csrf_token,
  305.                                 plugin:OIDplusPagePublicObjects.oid,
  306.                                 action:"Update2",
  307.                                 id:current_node,
  308.                                 title:($("#titleedit")[0] ? $("#titleedit")[0].value : null),
  309.                                 //description:($("#description")[0] ? $("#description")[0].value : null)
  310.                                 description:tinyMCE.get('description').getContent()
  311.                         },
  312.                         error:function(jqXHR, textStatus, errorThrown) {
  313.                                 if (errorThrown == "abort") return;
  314.                                 alert(_L("Error: %1",errorThrown));
  315.                         },
  316.                         success:function(data) {
  317.                                 if ("error" in data) {
  318.                                         alert(_L("Error: %1",data.error));
  319.                                 } else if (data.status >= 0) {
  320.                                         alert(_L("Update OK"));
  321.                                         //reloadContent();
  322.                                         $('#oidtree').jstree("refresh");
  323.                                         var h1s = $("h1");
  324.                                         for (var i = 0; i < h1s.length; i++) {
  325.                                                 var h1 = h1s[i];
  326.                                                 h1.innerHTML = $("#titleedit")[0].value.htmlentities();
  327.                                         }
  328.                                         document.title = combine_systemtitle_and_pagetitle(getOidPlusSystemTitle(), $("#titleedit")[0].value);
  329.  
  330.                                         var mce = tinymce.get('description');
  331.                                         if (mce != null) mce.setDirty(false);
  332.                                 } else {
  333.                                         alert(_L("Error: %1",data.error));
  334.                                 }
  335.                         }
  336.                 });
  337.         },
  338.  
  339.         crudActionSendInvitation: function(origin, email) {
  340.                 // window.location.href = "?goto=oidplus:invite_ra$"+encodeURIComponent(email)+"$"+encodeURIComponent(origin);
  341.                 openOidInPanel('oidplus:invite_ra$'+email+'$'+origin, false);
  342.         },
  343.  
  344.         frdl_weid_change: function() {
  345.                 var from_base = 36;
  346.                 var from_control = "#weid";
  347.                 var to_base = 10;
  348.                 var to_control = "#id";
  349.  
  350.                 var inp = $(from_control).val().trim();
  351.                 if (inp == "") {
  352.                         $(to_control).val("");
  353.                 } else {
  354.                         var x = BigNumber(inp, from_base);
  355.                         if (isNaN(x)) {
  356.                                 $(to_control).val("");
  357.                         } else {
  358.                                 $(to_control).val(x.toString(to_base));
  359.                         }
  360.                 }
  361.         },
  362.  
  363.         frdl_oidid_change: function() {
  364.                 var from_base = 10;
  365.                 var from_control = "#id";
  366.                 var to_base = 36;
  367.                 var to_control = "#weid";
  368.  
  369.                 var inp = $(from_control).val().trim();
  370.                 if (inp == "") {
  371.                         $(to_control).val("");
  372.                 } else {
  373.                         var x = BigNumber(inp, from_base);
  374.                         if (isNaN(x)) {
  375.                                 $(to_control).val("");
  376.                         } else {
  377.                                 $(to_control).val(x.toString(to_base).toUpperCase());
  378.                         }
  379.                 }
  380.         }
  381.  
  382. };
  383.