Skip to content

Commit

Permalink
autolink and html support
Browse files Browse the repository at this point in the history
  • Loading branch information
synox committed Jan 9, 2018
1 parent 9e36b93 commit 015b424
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"require": {
"php-imap/php-imap": "~2.0",
"gnugat/PronounceableWord": "*"
"gnugat/PronounceableWord": "*",
"ezyang/htmlpurifier": "^4.9"
},
"config": {
"vendor-dir": "src/backend-libs"
Expand Down
51 changes: 49 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/backend-libs/composer/autoload_namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
return array(
'PronounceableWord_' => array($vendorDir . '/gnugat/PronounceableWord/src'),
'PhpImap' => array($vendorDir . '/php-imap/php-imap/src'),
'HTMLPurifier' => array($vendorDir . '/ezyang/htmlpurifier/library'),
);
18 changes: 18 additions & 0 deletions src/backend-libs/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public static function getLoader()

$loader->register(true);

if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit125dddd280a32cf75b181166154246ec::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire125dddd280a32cf75b181166154246ec($fileIdentifier, $file);
}

return $loader;
}
}

function composerRequire125dddd280a32cf75b181166154246ec($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
}
}
11 changes: 11 additions & 0 deletions src/backend-libs/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class ComposerStaticInit125dddd280a32cf75b181166154246ec
{
public static $files = array (
'2cffec82183ee1cea088009cef9a6fc3' => __DIR__ . '/..' . '/ezyang/htmlpurifier/library/HTMLPurifier.composer.php',
);

public static $prefixesPsr0 = array (
'P' =>
array (
Expand All @@ -18,6 +22,13 @@ class ComposerStaticInit125dddd280a32cf75b181166154246ec
0 => __DIR__ . '/..' . '/php-imap/php-imap/src',
),
),
'H' =>
array (
'HTMLPurifier' =>
array (
0 => __DIR__ . '/..' . '/ezyang/htmlpurifier/library',
),
),
);

public static function getInitializer(ClassLoader $loader)
Expand Down
49 changes: 49 additions & 0 deletions src/backend-libs/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,54 @@
"word"
],
"abandoned": true
},
{
"name": "ezyang/htmlpurifier",
"version": "v4.9.3",
"version_normalized": "4.9.3.0",
"source": {
"type": "git",
"url": "https://github.com/ezyang/htmlpurifier.git",
"reference": "95e1bae3182efc0f3422896a3236e991049dac69"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ezyang/htmlpurifier/zipball/95e1bae3182efc0f3422896a3236e991049dac69",
"reference": "95e1bae3182efc0f3422896a3236e991049dac69",
"shasum": ""
},
"require": {
"php": ">=5.2"
},
"require-dev": {
"simpletest/simpletest": "^1.1"
},
"time": "2017-06-03 02:28:16",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-0": {
"HTMLPurifier": "library/"
},
"files": [
"library/HTMLPurifier.composer.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL"
],
"authors": [
{
"name": "Edward Z. Yang",
"email": "[email protected]",
"homepage": "http://ezyang.com"
}
],
"description": "Standards compliant HTML filter written in PHP",
"homepage": "http://htmlpurifier.org/",
"keywords": [
"html"
]
}
]
69 changes: 69 additions & 0 deletions src/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,74 @@ function delete_old_messages() {
}


class AutoLinkExtension {
static public function auto_link_text($string) {

$string = preg_replace_callback("/
((?<![\"']) # don't look inside quotes
(\b
( # protocol or www.
[a-z]{3,}:\/\/
|
www\.
)
(?: # domain
[a-zA-Z0-9_\-]+
(?:\.[a-zA-Z0-9_\-]+)*
|
localhost
)
(?: # port
\:[0-9]+
)?
(?: # path
\/[a-z0-9:%_|~.-]*
(?:\/[a-z0-9:%_|~.-]*)*
)?
(?: # attributes
\?[a-z0-9:%_|~.=&#;-]*
)?
(?: # anchor
\#[a-z0-9:%_|~.=&#;-]*
)?
)
(?![\"']))
/ix", function ($match) {
$url = $match[0];
$href = $url;

if (false === strpos($href, 'http')) {
$href = 'http://' . $href;
}
return '<a href="' . $href . '" rel="noreferrer">' . $url . '</a>';
}
, $string);


$string = AutoLinkExtension::unescape($string);

return $string;
} # filter()

/**
* unescape()
*
* @param string $text
* @return string $text
**/
static function unescape($text) {
global $escape_autolink_uri;

if (!$escape_autolink_uri)
return $text;

$unescape = array_reverse($escape_autolink_uri);

return str_replace(array_keys($unescape), array_values($unescape), $text);
} # unescape()

}


// run on every request
delete_old_messages();
66 changes: 50 additions & 16 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
require_once('backend.php');

$purifier_config = HTMLPurifier_Config::createDefault();
$purifier_config->set('HTML.Nofollow', true);
$purifier_config->set('HTML.ForbiddenElements', array("img"));
$purifier = new HTMLPurifier($purifier_config);

// simple router:
if (isset($_GET['username']) && isset($_GET['domain'])) {
$username = filter_input(INPUT_GET, 'username', FILTER_SANITIZE_EMAIL);
Expand Down Expand Up @@ -30,15 +35,14 @@
exit();
}
$emails = get_emails($address);

?>

<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $address ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="icon" type="image/x-icon" href="favicon.gif">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
Expand Down Expand Up @@ -77,7 +81,24 @@ function reload() {

setInterval(function () {
reloadWithTurbolinks();
}, 15000)
}, 15000);


function showHtml(id) {
document.getElementById('email-' + id + '-html').style.display = 'block';
document.getElementById('email-' + id + '-plain').style.display = 'none';
document.getElementById('show-html-button-' + id).style.display = 'none';
document.getElementById('show-plain-button-' + id).style.display = 'block';
return false;
}

function showPlain(id) {
document.getElementById('email-' + id + '-html').style.display = 'none';
document.getElementById('email-' + id + '-plain').style.display = 'block';
document.getElementById('show-html-button-' + id).style.display = 'block';
document.getElementById('show-plain-button-' + id).style.display = 'none';
return false;
}

</script>
</head>
Expand All @@ -95,11 +116,11 @@ function reload() {
<div class="form-group row">

<div class="col-sm-4">
<input id="username" class="form-control form-control-lg" name="username"
<input id="username" class="form-control form-control-lg" name="username" title="username"
value="<?php echo $username ?>">
</div>
<div class="col-sm-3">
<select id="domain" class="form-control form-control-lg" name="domain">
<select id="domain" class="form-control form-control-lg" name="domain" title="domain">
<?php
foreach ($config['domains'] as $domain) {
$selected = $domain === $userDomain ? ' selected ' : '';
Expand Down Expand Up @@ -166,9 +187,18 @@ function reload() {
<div class="col-sm-4 text-right">
<form class="form-inline float-xs-right">

<!-- TODO: switch between html and plaintext -->
<!-- <button class="btn btn-outline-info btn-sm">show html-->
<!-- </button>-->
<button type="button" class="btn btn-outline-info btn-sm"
style="display: block"
id="show-html-button-<?php echo filter_var($email->id, FILTER_VALIDATE_INT); ?>"
onclick="showHtml(<?php echo filter_var($email->id, FILTER_VALIDATE_INT); ?>)">
show html
</button>
<button type="button" class="btn btn-outline-info btn-sm"
style="display: none"
id="show-plain-button-<?php echo filter_var($email->id, FILTER_VALIDATE_INT); ?>"
onclick="showPlain(<?php echo filter_var($email->id, FILTER_VALIDATE_INT); ?>)">
show text
</button>

<a class="btn btn-sm btn-outline-primary " download="true"
role="button"
Expand Down Expand Up @@ -216,17 +246,21 @@ function reload() {


<div class="mt-2 card-text">
<!-- TODO: switch between html and plaintext -->
<div>
<!-- TODO: applyAutolink
TODO: applyNewlines -->
<!-- show plaintext or html -->
<div id="email-<?php echo filter_var($email->id, FILTER_VALIDATE_INT); ?>-plain"
style="display: block;">
<?php $text = filter_var($email->textPlain, FILTER_SANITIZE_SPECIAL_CHARS);
echo str_replace('&#10;', '<br />', $text);
// Keep newlines
$text = str_replace('&#10;', '<br />', $text);
echo \AutoLinkExtension::auto_link_text($text)
?>

</div>
<div *ngIf="htmlTabActive">
<!-- TODO: stripHtml(mail.textHtml) -->
<div id="email-<?php echo filter_var($email->id, FILTER_VALIDATE_INT); ?>-html"
style="display: none;">
<?php
$clean_html = $purifier->purify($email->textHtml);
echo $clean_html;
?>
</div>
</div>

Expand Down

0 comments on commit 015b424

Please sign in to comment.