Skip to content

Commit

Permalink
Fix cloudflare emails in wiki (fix #1362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Aug 9, 2018
1 parent e02675d commit 5695f6e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lib/src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,31 @@ bool isTestModeEnabled()
}


QString fixCloudflareEmail(const QString &a)
{
QString s;
int r = a.midRef(0, 2).toInt(nullptr, 16);
for (int j = 2; a.length() - j; j += 2)
{
int c = a.midRef(j, 2).toInt(nullptr, 16) ^ r;
s += QString(QChar(c));
}
return s;
}
QString fixCloudflareEmails(QString html)
{
static QRegularExpression rx("<span class=\"__cf_email__\" data-cfemail=\"([^\"]+)\">\\[[^<]+\\]<\\/span>");
auto matches = rx.globalMatch(html);
while (matches.hasNext())
{
auto match = matches.next();
const QString email = fixCloudflareEmail(match.captured(1));
html.replace(match.captured(0), email);
}
return html;
}


QString parseMarkdown(QString str)
{
// Windows EOL
Expand Down
3 changes: 3 additions & 0 deletions lib/src/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ QString getExtensionFromHeader(const QByteArray &data12);
void setTestModeEnabled(bool testMode);
bool isTestModeEnabled();

QString fixCloudflareEmail(const QString &a);
QString fixCloudflareEmails(QString html);

QString parseMarkdown(QString str);
QString decodeHtmlEntities(const QString &html);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/page-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void PageApi::parseActual()
if (page.urlPrevPage.isValid())
{ m_urlPrevPage = page.urlPrevPage; }
if (!page.wiki.isEmpty())
{ m_wiki = page.wiki; }
{ m_wiki = fixCloudflareEmails(page.wiki); }

// Complete image count information from tag count information
if (m_imagesCount < 1 || !m_imagesCountSafe)
Expand Down
11 changes: 11 additions & 0 deletions tests/src/functions-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,17 @@ void FunctionsTest::testGetExternalLogFilesSuffixes()
settings->remove("LogFiles/0/content");
}

void FunctionsTest::testFixCloudflareEmail()
{
QCOMPARE(fixCloudflareEmail("145d505b58595447405146"), QString("IDOLM@STER"));
QCOMPARE(fixCloudflareEmail("cc9cbea3a6a9afb8e1a5818c9f"), QString("Project-iM@S"));
}
void FunctionsTest::testFixCloudflareEmails()
{
QCOMPARE(fixCloudflareEmails(R"(<a class="dtext-link dtext-wiki-link" href="/wiki_pages/show_or_new?title=idolm%40ster_cinderella_girls"><span class="__cf_email__" data-cfemail="145d505b58595447405146">[email&#160;protected]</span> Cinderella Girls</a>)"), QString(R"(<a class="dtext-link dtext-wiki-link" href="/wiki_pages/show_or_new?title=idolm%40ster_cinderella_girls">IDOLM@STER Cinderella Girls</a>)"));
QCOMPARE(fixCloudflareEmails(R"(Koshimizu Sachiko on <span class="__cf_email__" data-cfemail="cc9cbea3a6a9afb8e1a5818c9f">[email&#160;protected]</span>)"), QString("Koshimizu Sachiko on Project-iM@S"));
}


void FunctionsTest::assertFixFilename(int platform, const QString &filename, const QString &path, const QString &expected)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/src/functions-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class FunctionsTest : public TestSuite
void testSetFileCreationDate();
void testSetFileCreationDateUtf8();
void testGetExternalLogFilesSuffixes();
void testFixCloudflareEmail();
void testFixCloudflareEmails();

protected:
void assertFixFilename(int platform, const QString &filename, const QString &path, const QString &expected);
Expand Down

0 comments on commit 5695f6e

Please sign in to comment.