Skip to content

Commit

Permalink
Merge branch 'staged' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed May 17, 2022
2 parents 99d5093 + e06f865 commit 4bdcbf6
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 37 deletions.
18 changes: 6 additions & 12 deletions articleview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,25 +318,19 @@ ArticleView::ArticleView( QWidget * parent, ArticleNetworkAccessManager & nm, Au
QWebEngineSettings * settings = ui.definition->settings();
settings->setUnknownUrlSchemePolicy(QWebEngineSettings::UnknownUrlSchemePolicy::DisallowUnknownUrlSchemes);
#if( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::WebAttribute::ErrorPageEnabled, false );
settings->defaultSettings()->setAttribute( QWebEngineSettings::LocalContentCanAccessRemoteUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::LocalContentCanAccessFileUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::ErrorPageEnabled, false );
settings->defaultSettings()->setAttribute( QWebEngineSettings::PluginsEnabled, cfg.preferences.enableWebPlugins );
settings->defaultSettings()->setAttribute( QWebEngineSettings::PlaybackRequiresUserGesture, false );
settings->defaultSettings()->setAttribute( QWebEngineSettings::JavascriptCanAccessClipboard, true );

if( !cfg.preferences.webFontFamily.isEmpty() )
settings->defaultSettings()->setFontFamily( QWebEngineSettings::StandardFont, cfg.preferences.webFontFamily );
#else
settings->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true );
settings->setAttribute( QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls, true );
settings->setAttribute( QWebEngineSettings::WebAttribute::ErrorPageEnabled, false );
settings->setAttribute( QWebEngineSettings::LocalContentCanAccessRemoteUrls, true );
settings->setAttribute( QWebEngineSettings::LocalContentCanAccessFileUrls, true );
settings->setAttribute( QWebEngineSettings::ErrorPageEnabled, false );
settings->setAttribute( QWebEngineSettings::PluginsEnabled, cfg.preferences.enableWebPlugins );
settings->setAttribute( QWebEngineSettings::PlaybackRequiresUserGesture, false );
settings->setAttribute( QWebEngineSettings::JavascriptCanAccessClipboard, true );

if( !cfg.preferences.webFontFamily.isEmpty() )
settings->setFontFamily( QWebEngineSettings::StandardFont, cfg.preferences.webFontFamily );
#endif

expandOptionalParts = cfg.preferences.alwaysExpandOptionalParts;
Expand Down
10 changes: 3 additions & 7 deletions epwing_book.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#include "wstring_qt.hh"
#include "folding.hh"
#include "epwing_charmap.hh"
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#include <QtCore5Compat>
#endif

#if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC )
#define _FILE_OFFSET_BITS 64
Expand Down Expand Up @@ -1207,8 +1204,8 @@ void EpwingBook::finalizeText( QString & text )
// Replace references

int pos = 0;
QRegularExpression reg1( "<R[^<]*>" );
QRegularExpression reg2( "</R[^<]*>" );
QRegularExpression reg1( "<R[^<]*>", QRegularExpression::CaseInsensitiveOption );
QRegularExpression reg2( "</R[^<]*>", QRegularExpression::CaseInsensitiveOption );

EContainer cont( this, true );

Expand Down Expand Up @@ -1751,8 +1748,7 @@ QByteArray EpwingBook::handleReference( EB_Hook_Code code, const unsigned int *
refPages.append( argv[ 1 ] );
refOffsets.append( argv[ 2 ] );

QString str;
str.asprintf( "</R%i>", refCloseCount );
QString str = QString( "</R%1>" ).arg( refCloseCount );
refCloseCount += 1;

return str.toUtf8();
Expand Down
7 changes: 6 additions & 1 deletion iframeschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
QBuffer * buffer = new QBuffer(requestJob);
buffer->setData(codec->fromUnicode(articleString));

requestJob->reply( "text/html;charset=UTF-8", buffer );
QByteArray contentType="text/html;charset=UTF-8";
auto contentTypeHeader=reply->header(QNetworkRequest::ContentTypeHeader);
if(contentTypeHeader.isValid())
contentType= contentTypeHeader.toByteArray();

requestJob->reply(contentType , buffer );
};
connect( reply, &QNetworkReply::finished, requestJob, finishAction );

Expand Down
27 changes: 22 additions & 5 deletions preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <QMessageBox>
#include "broken_xrecord.hh"
#include "mainwindow.hh"
#include <QWebEngineSettings>
#include <QWebEngineProfile>

Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
QDialog( parent ), prevInterfaceLanguage( 0 )
Expand Down Expand Up @@ -105,7 +107,9 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
ui.fontFamilies->addItem( family );
}
prevWebFontFamily = p.webFontFamily;
ui.fontFamilies->setCurrentText( p.webFontFamily );

if(!p.webFontFamily.isEmpty())
ui.fontFamilies->setCurrentText( p.webFontFamily );
// Fill help languages combobox

ui.helpLanguage->addItem( tr( "Default" ), QString() );
Expand Down Expand Up @@ -373,7 +377,9 @@ Config::Preferences Preferences::getPreferences()
ui.interfaceLanguage->itemData(
ui.interfaceLanguage->currentIndex() ).toString();

p.webFontFamily = ui.fontFamilies->currentText();
//bypass the first default
if(ui.fontFamilies->currentIndex()>0)
p.webFontFamily = ui.fontFamilies->currentText();

p.helpLanguage =
ui.helpLanguage->itemData(
Expand Down Expand Up @@ -645,9 +651,20 @@ void Preferences::on_buttonBox_accepted()
QMessageBox::information( this, tr( "Changing Language" ),
tr( "Restart the program to apply the language change." ) );

if ( prevWebFontFamily != ui.fontFamilies->currentText() )
QMessageBox::information( this, tr( "Changing Dictionary Font Family" ),
tr( "Restart the program to apply the dictionary font family change." ) );
auto currentFontFamily = ui.fontFamilies->currentText();
if( prevWebFontFamily != currentFontFamily )
{
//reset to default font .
if( currentFontFamily.isEmpty() )
{
QWebEngineProfile::defaultProfile()->settings()->resetFontFamily( QWebEngineSettings::StandardFont );
}
else
{
QWebEngineProfile::defaultProfile()->settings()->setFontFamily( QWebEngineSettings::StandardFont,
currentFontFamily );
}
}
}

void Preferences::on_useExternalPlayer_toggled( bool enabled )
Expand Down
7 changes: 3 additions & 4 deletions utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ inline QString unescapeHtml(const QString &str) {


inline bool isExternalLink(QUrl const &url) {
return url.scheme() == "http" || url.scheme() == "https" ||
url.scheme() == "ftp" || url.scheme() == "mailto" ||
url.scheme() == "file";
return url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp" || url.scheme() == "mailto" ||
url.scheme() == "file" || url.toString().startsWith( "//" );
}

inline bool isCssFontImage(QUrl const &url) {
auto fileName = url.fileName();
auto ext=fileName.mid(fileName.lastIndexOf("."));
QStringList extensions{".css",".woff",".woff2",".bmp" ,".jpg", ".png", ".tif",".wav", ".ogg", ".oga", ".mp3", ".mp4", ".aac", ".flac",".mid", ".wv ",".ape"} ;
QStringList extensions{".css",".woff",".woff2","ttf",".bmp" ,".jpg", ".png", ".tif",".wav", ".ogg", ".oga", ".mp3", ".mp4", ".aac", ".flac",".mid", ".wv ",".ape"} ;
return extensions.indexOf(ext)>-1;
}

Expand Down
28 changes: 23 additions & 5 deletions website.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WebSiteDictionary: public Dictionary::Class
{
string name;
QByteArray urlTemplate;
bool experimentalIframe;
QString iconFilename;
bool inside_iframe;
QNetworkAccessManager & netMgr;
Expand All @@ -36,12 +37,20 @@ class WebSiteDictionary: public Dictionary::Class
QNetworkAccessManager & netMgr_ ):
Dictionary::Class( id, vector< string >() ),
name( name_ ),
urlTemplate( QUrl( urlTemplate_ ).toEncoded() ),
iconFilename( iconFilename_ ),
inside_iframe( inside_iframe_ ),
netMgr( netMgr_ )
netMgr( netMgr_ ),
experimentalIframe(false)
{
dictionaryDescription = urlTemplate_;
QString temp=urlTemplate_;
if(temp.endsWith("##")){
experimentalIframe=true;
temp.chop(2);
}

urlTemplate = QUrl( temp ).toEncoded() ;

dictionaryDescription = temp;
}

virtual string getName() throw()
Expand Down Expand Up @@ -372,12 +381,21 @@ sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str,

string result = "<div class=\"website_padding\"></div>";

//permissive add url to global whitelist.
//heuristic add url to global whitelist.
QUrl url(urlString);
GlobalBroadcaster::instance()->addWhitelist(url.host());

QString encodeUrl;
if(experimentalIframe){
encodeUrl="ifr://localhost?url="+QUrl::toPercentEncoding( urlString);
}
else{
encodeUrl = urlString;
}


result += string( "<iframe id=\"gdexpandframe-" ) + getId() +
"\" src=\"ifr://localhost?url=" +QUrl::toPercentEncoding( urlString).data() +
"\" src=\""+encodeUrl.toStdString() +
"\" onmouseover=\"processIframeMouseOver('gdexpandframe-" + getId() + "');\" "
"onmouseout=\"processIframeMouseOut();\" "
"scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" "
Expand Down
16 changes: 13 additions & 3 deletions weburlrequestinterceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)

}
void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) {
if( Utils::isExternalLink( info.requestUrl() ) )
if( GlobalBroadcaster::instance()->getPreference()->disallowContentFromOtherSites && Utils::isExternalLink( info.requestUrl() ) )
{
if(GlobalBroadcaster::instance()-> existedInWhitelist(info.requestUrl().host()))
//file:// link ,pass
if(info.requestUrl().scheme()=="file"){
return;
}
auto hostBase = getHostBase( info.requestUrl().host() );
if( GlobalBroadcaster::instance()->existedInWhitelist( hostBase ) )
{
//whitelist url does not block
return;
Expand All @@ -20,7 +25,12 @@ void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info)
//let throuth the resources file.
return;
}
info.block(true);

// block external links
{
info.block( true );
return;
}
}

if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
Expand Down

0 comments on commit 4bdcbf6

Please sign in to comment.