From 88171df674a8e7535dc413667e00545363e1f592 Mon Sep 17 00:00:00 2001 From: Dev-iL Date: Sun, 22 Jul 2018 15:04:57 +0300 Subject: [PATCH 1/4] Better handling of uifigure-independent webwindows in getHTML() - getHTML now accepts either a webwindow or a UIFigure handle. - getHTML tries to determine if the webwindow has an associated UIFigure or not, and uses the appropriate technique to get the HTML. --- mlapptools.m | 77 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/mlapptools.m b/mlapptools.m index ef83993..0f8eaee 100644 --- a/mlapptools.m +++ b/mlapptools.m @@ -102,30 +102,59 @@ function fontWeight(uiElement, weight) win.executeJS('W = undefined'); end % getChildNodeIDs - function [fullHTML] = getHTML(hUIFig) + function [fullHTML] = getHTML(hFigOrWin) % A method for dumping the HTML code of a uifigure. - % Intended for R2017b (and onward?) where the CEF url cannot be simply opened in a browser. + % Intended for R2017b (and onward?) where the CEF url cannot be simply opened in + % an external browser. + %% Obtain webwindow handle: + if isa(hFigOrWin,'matlab.ui.Figure') + win = mlapptools.getWebWindow(hFigOrWin); + indepWW = false; + else + win = hFigOrWin; % rename the handle + %% Attempt to determine if this is an "independent" webwindow: + hF = mlapptools.figFromWebwindow(win); + if isempty(hF) + indepWW = true; + else + indepWW = false; + end + end + %% Get HTML according to webwindow type: + if indepWW + [~,hWB] = web(win.URL, '-new'); + fullHTML = hWB.getHtmlText(); + close(hWB); + %% TODO: Try to fix css paths: + % See: https://stackoverflow.com/q/50944935/ + %{ + % Get all elements: + win.executeJS('dojo.query("link")') + % Convert relative paths to absolute: + + % Replace paths in HTML: - win = mlapptools.getWebWindow(hUIFig); - % Get the outer html: - fullHTML = win.executeJS('document.documentElement.outerHTML'); - % Replace some strings for conversion to work well: - fullHTML = strrep(fullHTML,'%','%%'); - fullHTML = strrep(fullHTML,'><','>\n<'); - % Append the DOCTYPE header and remove quotes: - fullHTML = sprintf(['\n' fullHTML(2:end-1)]); - - %% Optional things to do with the output: - % Display as web page: - %{ - web(['text://' fullHTML]); - %} - % Save as file: - %{ - fid = fopen('uifig_raw.html','w'); - fprintf(fid,'%s',fullHTML); - fclose(fid); - %} + %} + else + % Get the outer html: + fullHTML = win.executeJS('document.documentElement.outerHTML'); + % Replace some strings for conversion to work well: + fullHTML = strrep(fullHTML,'%','%%'); + fullHTML = strrep(fullHTML,'><','>\n<'); + % Append the DOCTYPE header and remove quotes: + fullHTML = sprintf(['\n' fullHTML(2:end-1)]); + %% Optional things to do with the output: + % Display as web page: + %{ + web(['text://' fullHTML]); + %} + % Save as file: + %{ + fid = fopen('uifig_raw.html','w'); + fprintf(fid,'%s',fullHTML); + fclose(fid); + %} + end end % getHTML function [parentID] = getParentNodeID(win,ID_obj) @@ -438,6 +467,10 @@ function textAlign(uiElement, alignment) hFigs = findall(groot, 'Type', 'figure'); warnState = mlapptools.toggleWarnings('off'); hUIFigs = hFigs(arrayfun(@(x)isstruct(struct(x).ControllerInfo), hFigs)); + if isempty(hUIFigs) + hFig = gobjects(0); + return + end hUIFigs = hUIFigs(strcmp({hUIFigs.Visible},'on')); % Hidden figures are ignored ww = arrayfun(@mlapptools.getWebWindow, hUIFigs); warning(warnState); % Restore warning state From d021c627de8d3a1dcba5e63b31ad9fefdc4118d4 Mon Sep 17 00:00:00 2001 From: Dev-iL Date: Sun, 22 Jul 2018 15:07:55 +0300 Subject: [PATCH 2/4] Added unlockUIFig(), for bringing back access via external browsers! See example in the updated README.md. --- README.md | 30 ++++++++++++++++++++++++++++++ mlapptools.m | 13 +++++++++++++ 2 files changed, 43 insertions(+) diff --git a/README.md b/README.md index e0eff96..8c2fa53 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ published Wednesday, September 7th, 2016. [`setStyle`](#setStyle) - Modify a specified style property. [`setTimeout`](#setTimeout) - Override the default timeout for dojo commands, for a specific `uifigure`. [`textAlign`](#textAlign) - Modify text alignment. +[`unlockUIFig`](#unlockUIFig) - Modify this UIFigure to allow viewing it in an external browser, on release R2017b and newer. [`waitForFigureReady`](#waitForFigureReady) - A blocking method that only returns after the uifigure is fully loaded. ----------------- @@ -335,6 +336,35 @@ myGUI = DOMdemoGUI; mlapptools.textAlign(myGUI.TextArea, 'right'); ``` +----------------- + + +#### *mlapptools*.**unlockUIFig**(*hUIFig*) + +##### Description + +This method modifies the properties of the UIFigure controller such that opening the UIFigure in a browser (using the webwindow URL) becomes possible. This method has no effect on MATLAB releases before R2017b (where no such restriction exists in the first place). + +##### Examples + +Using the demo GUI generated by `./Demo/DOMdemoGUI.m` + +```MATLAB +% Create a uifigure: +myGUI = DOMdemoGUI; + +% Attempt to open it using an external browser: +hWW = mlapptools.getWebWindow(myGUI.UIFigure); +web(hWW.URL,'-browser'); pause(3); + +% "Unlock" the uifigure: +mlapptools.unlockUIFig(myGUI.UIFigure); + +% Attempt to open again: +web(hWW.URL,'-browser'); +``` +The 1st browser window should be empty, whereas the 2nd should correctly show the contents of the UIFigure. + ----------------- diff --git a/mlapptools.m b/mlapptools.m index 0f8eaee..a4c91e9 100644 --- a/mlapptools.m +++ b/mlapptools.m @@ -16,6 +16,7 @@ % setStyle - Modify a specified style property. % setTimeout - Override the default timeout for dojo commands, for a specific uifigure. % textAlign - Modify text alignment. + % unlockUIFig - Allow the uifigure to be opened using an external browser. % waitForFigureReady - A blocking method that only returns after the uifigure is fully loaded. % % See README.md for detailed documentation and examples. @@ -106,6 +107,8 @@ function fontWeight(uiElement, weight) % A method for dumping the HTML code of a uifigure. % Intended for R2017b (and onward?) where the CEF url cannot be simply opened in % an external browser. + % Mostly irrelevant for UIFigures as of the introduction of mlapptools.unlockUIFig(...), + % but can be useful for other non-uifigure webwindows. %% Obtain webwindow handle: if isa(hFigOrWin,'matlab.ui.Figure') win = mlapptools.getWebWindow(hFigOrWin); @@ -341,6 +344,16 @@ function textAlign(uiElement, alignment) mlapptools.setStyle(win, 'textAlign', alignment, ID_struct); end % textAlign + + function unlockUIFig(hUIFig) + % This method allows the uifigure to be opened in an external browser, + % as was possible before R2017b. + if verLessThan('matlab','9.3') + % Do nothing, since this is not required pre-R2017b. + else + struct(hUIFig).Controller.ProxyView.PeerNode.setProperty('hostType','""'); + end + end % unlockUIFig function win = waitForFigureReady(hUIFig) % This blocking method waits until a UIFigure and its widgets have fully loaded. From b8f70c5c6a8d0e2216823359bce2ea98dcc77b78 Mon Sep 17 00:00:00 2001 From: Dev-iL Date: Tue, 24 Jul 2018 17:38:07 +0300 Subject: [PATCH 3/4] Added automatic certificate testing and importing for Windows Unix/Linux and Mac support is "TODO". --- mlapptools.m | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/mlapptools.m b/mlapptools.m index a4c91e9..eebcd29 100644 --- a/mlapptools.m +++ b/mlapptools.m @@ -348,11 +348,64 @@ function textAlign(uiElement, alignment) function unlockUIFig(hUIFig) % This method allows the uifigure to be opened in an external browser, % as was possible before R2017b. + assert(checkCert(), 'Certificate not imported; cannot proceed.'); if verLessThan('matlab','9.3') % Do nothing, since this is not required pre-R2017b. else struct(hUIFig).Controller.ProxyView.PeerNode.setProperty('hostType','""'); end + + function tf = checkCert() + SUCCESS_CODE = 0; + switch true + case ispc + %% Test if certificate is already accepted: + [s,c] = system('certutil -verifystore -user "Root" localhost'); + if s == SUCCESS_CODE + tf = true; + else + reply = questdlg('Certificate not found. Would you like to import it?',... + 'Import "localhost" certificate','Yes','No','Yes'); + if strcmp(reply,'Yes') + %% Import the certificate + [s,c] = system(['certutil -addstore -user "Root" ' ... + connector.getCertificateLocation()]); + tf = s == SUCCESS_CODE; + if tf + disp(['Certificate import successful! You should now be '... + 'able to navigate to the webwindow URL in your browser.']);... + disp(['If the figure is still blank, recreate it and navigate '... + 'to the new URL.']); + else + disp(c); + end + else + disp(c); + tf = false; + end + end + case isunix + warning('checkCert:unsupportedOS:unix',... + 'OS not supported for automatic testing, assuming the certificate is in order.'); + tf = true; + % TODO + % See: https://askubuntu.com/a/648629, https://superuser.com/a/437377 + %{ + system(['sudo cp ' connector.getCertificateLocation() ... + ' /usr/local/share/ca-certificates/localhost-matlab.crt && '... + 'sudo dpkg-reconfigure ca-certificates && sudo update-ca-certificates']) + %} + case ismac + warning('checkCert:unsupportedOS:mac',... + 'OS not supported for automatic testing, assuming the certificate is in order.'); + tf = true; + % TODO + %{ + system(['sudo security add-trusted-cert -d -r trustRoot -k '... + '"$HOME/Library/Keychains/login.keychain"' connector.getCertificateLocation()]); + %} + end + end % checkCert end % unlockUIFig function win = waitForFigureReady(hUIFig) From 6e12b32f98a3426a0c7d3488311a54bee889893d Mon Sep 17 00:00:00 2001 From: Dev-iL Date: Sat, 28 Jul 2018 19:38:57 +0300 Subject: [PATCH 4/4] Refactored and expanded checkCert() to work on Unix and macOS Note: it was only tested with an actual MATLAB installation on Windows! --- mlapptools.m | 116 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 46 deletions(-) diff --git a/mlapptools.m b/mlapptools.m index eebcd29..d548fe7 100644 --- a/mlapptools.m +++ b/mlapptools.m @@ -356,54 +356,78 @@ function unlockUIFig(hUIFig) end function tf = checkCert() - SUCCESS_CODE = 0; + % This tools works on the OS-level, and was tested on Win 7 & 10. + % + % With certain browsers it might not be required/helpful as noted in + % https://askubuntu.com/questions/73287/#comment1533817_94861 : + % Note that Chromium and Firefox do not use the system CA certificates, + % so require separate instructions. + % - In Chromium, visit chrome://settings/certificates, click Authorities, + % then click import, and select your .pem. + % - In Firefox, visit about:preferences#privacy, click Certificates, + % View Certificates, Authorities, then click Import and select your .pem. + SUCCESS_CODE = 0; + CL = connector.getCertificateLocation(); % certificate location; + if isempty(CL), CL = fullfile(prefdir, 'thisMatlab.pem'); end + %% Test if certificate is already accepted: switch true - case ispc - %% Test if certificate is already accepted: - [s,c] = system('certutil -verifystore -user "Root" localhost'); - if s == SUCCESS_CODE - tf = true; - else - reply = questdlg('Certificate not found. Would you like to import it?',... - 'Import "localhost" certificate','Yes','No','Yes'); - if strcmp(reply,'Yes') - %% Import the certificate - [s,c] = system(['certutil -addstore -user "Root" ' ... - connector.getCertificateLocation()]); - tf = s == SUCCESS_CODE; - if tf - disp(['Certificate import successful! You should now be '... - 'able to navigate to the webwindow URL in your browser.']);... - disp(['If the figure is still blank, recreate it and navigate '... - 'to the new URL.']); - else - disp(c); - end - else - disp(c); - tf = false; - end - end + case ispc + [s,c] = system('certutil -verifystore -user "Root" localhost'); case isunix - warning('checkCert:unsupportedOS:unix',... - 'OS not supported for automatic testing, assuming the certificate is in order.'); - tf = true; - % TODO - % See: https://askubuntu.com/a/648629, https://superuser.com/a/437377 - %{ - system(['sudo cp ' connector.getCertificateLocation() ... - ' /usr/local/share/ca-certificates/localhost-matlab.crt && '... - 'sudo dpkg-reconfigure ca-certificates && sudo update-ca-certificates']) - %} - case ismac - warning('checkCert:unsupportedOS:mac',... - 'OS not supported for automatic testing, assuming the certificate is in order.'); - tf = true; - % TODO - %{ - system(['sudo security add-trusted-cert -d -r trustRoot -k '... - '"$HOME/Library/Keychains/login.keychain"' connector.getCertificateLocation()]); - %} + [s,c] = system(['openssl crl2pkcs7 -nocrl -certfile '... + '/etc/ssl/certs/ca-certificates.crt '... + '| openssl pkcs7 -print_certs -noout '... + '| grep ''^issuer=/C=US/O=company/CN=localhost/OU=engineering''']); + case ismac + [s,c] = system('security find-certificate -c "localhost"'); + end + isAccepted = s == SUCCESS_CODE; + + %% Try to import certificate: + if ~isAccepted + reply = questdlg('Certificate not found. Would you like to import it?',... + 'Import "localhost" certificate','Yes','No','Yes'); + if strcmp(reply,'Yes'), switch true %#ok + case ispc + [s,c] = system(['certutil -addstore -user "Root" ' CL]); + % %APPDATA%\MathWorks\MATLAB\R20##x\thisMatlab.pem + case isunix + [s,c] = system(['sudo cp ' CL ... + ' /usr/local/share/ca-certificates/localhost-matlab.crt && ',... + 'sudo update-ca-certificates']); + % ~/.matlab/thisMatlab.pem + case ismac % https://apple.stackexchange.com/a/80625 + [s,c] = system(['security add-trusted-cert -d -r trustRoot -p ssl -k ' ... + '"$HOME/Library/Keychains/login.keychain" ' CL]); + % ~/Library/Application\ Support/MathWorks/MATLAB/R20##x/thisMatlab.pem + end % switch + wasImported = s == SUCCESS_CODE; + else + warning('Certificate import cancelled by user!'); + wasImported = false; + end + end + %% Report result + tf = isAccepted || wasImported; + if wasImported + fprintf(1, '\n%s\n%s\n%s\n',... + ['Certificate import successful! You should now be '... + 'able to navigate to the webwindow URL in your browser.'],... + ['If the figure is still blank, recreate it and navigate '... + 'to the new URL.'],... + ['Also, if you have a script blocking addon (e.g. NoScript), '... + 'be sure to whitelist "localhost".']); + elseif ~isAccepted % && ~wasImported (implicitly) + disp(c); + fprintf(1, '\n%s\n%s\n\t%s\n\t%s\n%s\n',... + 'Either certificate presence cannot be determined, or the import failed.',... + 'If you''re using Chromium or Firefox you can follow these instructions:',... + ['- In Chromium, visit chrome://settings > (Show advanced) > '... + 'Manage HTTP/SSL certificates > Trusted Root Certification Authorities Tab'... + ' > Import, and select your .pem.'],... + ['- In Firefox, visit about:preferences#privacy, click Certificates > ',... + 'View Certificates > Authorities > Import, and select your .pem.'],... + ['The certificate is found here: ' CL ]); end end % checkCert end % unlockUIFig