Skip to content

Commit

Permalink
Merge 15021c5 into ff68043
Browse files Browse the repository at this point in the history
  • Loading branch information
Haocen committed Sep 5, 2019
2 parents ff68043 + 15021c5 commit c659207
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 8 deletions.
Binary file added img/icon_email.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
184 changes: 180 additions & 4 deletions js/privatebin.js
Expand Up @@ -411,6 +411,45 @@ jQuery.PrivateBin = (function($, RawDeflate) {
baseUri = null;
};

/**
* calculate expiration date given initial date and expiration period
*
* @name Helper.calculateExpirationDate
* @function
* @param {Date} initialDate - may not be empty
* @param {string|number} expirationDisplayStringOrSecondsToExpire - may not be empty
* @return {Date}
*/
me.calculateExpirationDate = function(initialDate, expirationDisplayStringOrSecondsToExpire) {
let expirationDate = new Date(initialDate);

const expirationDisplayStringToSecondsDict = {
'5min': 300,
'10min': 600,
'1hour': 3500,
'1day': 86400,
'1week': 604800,
'1month': 2592000,
'1year': 31536000,
'never': 0
};

let secondsToExpiration = expirationDisplayStringOrSecondsToExpire;
if (typeof expirationDisplayStringOrSecondsToExpire === 'string') {
secondsToExpiration = expirationDisplayStringToSecondsDict[expirationDisplayStringOrSecondsToExpire];
}

if (typeof secondsToExpiration !== 'number') {
throw new Error('Cannot calculate expiration date.');
}
if (secondsToExpiration === 0) {
return null;
}

expirationDate = expirationDate.setUTCSeconds(expirationDate.getUTCSeconds() + secondsToExpiration);
return expirationDate;
}

return me;
})();

Expand Down Expand Up @@ -1780,7 +1819,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
`${$shortenButton.data('shortener')}${encodeURIComponent($pasteUrl.attr('href'))}`,
'_blank',
'noopener, noreferrer'
)
);
});
}

Expand Down Expand Up @@ -3292,9 +3331,10 @@ jQuery.PrivateBin = (function($, RawDeflate) {
$passwordInput,
$rawTextButton,
$qrCodeLink,
$emailLink,
$sendButton,
$retryButton,
pasteExpiration = '1week',
pasteExpiration = null,
retryButtonCallback;

/**
Expand Down Expand Up @@ -3502,6 +3542,68 @@ jQuery.PrivateBin = (function($, RawDeflate) {
$('#qrcode-display').html(qrCanvas);
}

/**
* Send Email with current paste (URL).
*
* @name TopNav.sendEmail
* @private
* @function
* @param {Date|null} expirationDate date of expiration
* @param {bool} isBurnafterreading whether it is burn after reading
*/
function sendEmail(expirationDate, isBurnafterreading)
{
const EOL = '\n';
const BULLET = ' - ';
let emailBody = '';
const expirationDateRoundedToSecond = new Date(expirationDate);

// round down at least 30 seconds to make up for the delay of request
expirationDateRoundedToSecond.setUTCSeconds(
expirationDateRoundedToSecond.getUTCSeconds() - 30
);
expirationDateRoundedToSecond.setUTCSeconds(0);

const expirationDateString = window.confirm(
I18n._('Recipient may become aware of your timezone, convert time to UTC?')
) ? expirationDateRoundedToSecond.toLocaleString(
undefined,
// we don't use Date.prototype.toUTCString() because we would like to avoid GMT
{ timeZone: 'UTC', dateStyle: 'long', timeStyle: 'long' }
) : expirationDateRoundedToSecond.toLocaleString();
if (expirationDate !== null || isBurnafterreading) {
emailBody += I18n._('Notice:');
emailBody += EOL;

if (expirationDate !== null) {
emailBody += EOL;
emailBody += BULLET;
emailBody += I18n._(
'This link will expire after %s.',
expirationDateString
);
}
if (isBurnafterreading) {
emailBody += EOL;
emailBody += BULLET;
emailBody += I18n._(
'This link can only be accessed once, do not use back or refresh button in your browser.'
);
}

emailBody += EOL;
emailBody += EOL;
}
emailBody += I18n._('Link:');
emailBody += EOL;
emailBody += `${window.location.href}`;
window.open(
`mailto:?body=${encodeURIComponent(emailBody)}`,
'_self',
'noopener, noreferrer'
);
}

/**
* Shows all navigation elements for viewing an existing paste
*
Expand Down Expand Up @@ -3538,6 +3640,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
$newButton.addClass('hidden');
$rawTextButton.addClass('hidden');
$qrCodeLink.addClass('hidden');
me.hideEmailbutton();

viewButtonsDisplayed = false;
};
Expand Down Expand Up @@ -3635,6 +3738,47 @@ jQuery.PrivateBin = (function($, RawDeflate) {
$retryButton.addClass('hidden');
}

/**
* show the "email" button
*
* @name TopNav.showEmailbutton
* @function
* @param {function} optionalRemainingTimeInSeconds
*/
me.showEmailButton = function(optionalRemainingTimeInSeconds)
{
try {
// we cache expiration date in closure to avoid inaccurate expiration datetime
const expirationDate = Helper.calculateExpirationDate(new Date(), optionalRemainingTimeInSeconds || TopNav.getExpiration());
const isBurnafterreading = TopNav.getBurnAfterReading();

$emailLink.removeClass('hidden');
$emailLink.off('click.sendEmail');
$emailLink.on('click.sendEmail', function(expirationDate, isBurnafterreading) {
return function() {
sendEmail(expirationDate, isBurnafterreading);
};
} (expirationDate, isBurnafterreading));
} catch (error) {
console.error(error);
Alert.showError(
I18n._('Cannot calculate expiration date.')
);
}
}

/**
* hide the "email" button
*
* @name TopNav.hideEmailbutton
* @function
*/
me.hideEmailbutton = function()
{
$emailLink.addClass('hidden');
$emailLink.off('click.sendEmail');
}

/**
* only hides the clone button
*
Expand All @@ -3657,6 +3801,30 @@ jQuery.PrivateBin = (function($, RawDeflate) {
$rawTextButton.addClass('hidden');
};

/**
* only hides the qr code button
*
* @name TopNav.hideQrCodeButton
* @function
*/
me.hideQrCodeButton = function()
{
$qrCodeLink.addClass('hidden');
}

/**
* hide all irrelevant buttons when viewing burn after reading paste
*
* @name TopNav.hideBurnAfterReadingButtons
* @function
*/
me.hideBurnAfterReadingButtons = function()
{
me.hideCloneButton();
me.hideQrCodeButton();
me.hideEmailbutton();
}

/**
* hides the file selector in attachment
*
Expand Down Expand Up @@ -3744,7 +3912,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
/**
* returns the state of the burn after reading checkbox
*
* @name TopNav.getExpiration
* @name TopNav.getBurnAfterReading
* @function
* @return {bool}
*/
Expand Down Expand Up @@ -3873,6 +4041,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
$retryButton = $('#retrybutton');
$sendButton = $('#sendbutton');
$qrCodeLink = $('#qrcodelink');
$emailLink = $('#emaillink');

// bootstrap template drop down
$('#language ul.dropdown-menu li a').click(setLanguage);
Expand Down Expand Up @@ -4219,6 +4388,10 @@ jQuery.PrivateBin = (function($, RawDeflate) {
history.pushState({type: 'newpaste'}, document.title, url);

TopNav.showViewButtons();

// this cannot be grouped with showViewButtons due to remaining time calculation
TopNav.showEmailButton();

TopNav.hideRawButton();
Editor.hide();

Expand Down Expand Up @@ -4684,7 +4857,10 @@ jQuery.PrivateBin = (function($, RawDeflate) {

// discourage cloning (it cannot really be prevented)
if (paste.isBurnAfterReadingEnabled()) {
TopNav.hideCloneButton();
TopNav.hideBurnAfterReadingButtons();
} else {
// we have to pass in remaining_time here
TopNav.showEmailButton(paste.getTimeToLive());
}
})
.catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion js/test/TopNav.js
Expand Up @@ -326,7 +326,7 @@ describe('TopNav', function () {
'returns the currently selected expiration date',
function () {
$.PrivateBin.TopNav.init();
assert.ok($.PrivateBin.TopNav.getExpiration() === '1week');
assert.ok($.PrivateBin.TopNav.getExpiration() === null);
}
);
});
Expand Down
7 changes: 5 additions & 2 deletions tpl/bootstrap.php
Expand Up @@ -71,7 +71,7 @@
endif;
?>
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.11.js" integrity="sha512-p7UyJuyBkhMcMgE4mDsgK0Lz70OvetLefua1oXs1OujWv9gOxh4xy8InFux7bZ4/DAZsTmO4rgVwZW9BHKaTaw==" crossorigin="anonymous"></script>
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-86VTqw2HsaCQ0DAunK2MH68P+8RLbbaK7HZP8nwDtwNoF44usxDCptmD8TC+zwQc7HM46AkrvVFb3ZkIb6VhMQ==" crossorigin="anonymous"></script>
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-BH2vIKNQVc8LGYcqgvCoxIpzG0DFXqxdBCa9ptYzekQXhRZJ6dmr0z1Jlx9XaniFMX1tJQMAIcvVWrFEgskCNA==" crossorigin="anonymous"></script>
<!--[if IE]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;}</style>
<![endif]-->
Expand Down Expand Up @@ -173,6 +173,9 @@
<button id="rawtextbutton" type="button" class="hidden btn btn-<?php echo $isDark ? 'warning' : 'default'; ?> navbar-btn">
<span class="glyphicon glyphicon-text-background" aria-hidden="true"></span> <?php echo I18n::_('Raw text'), PHP_EOL; ?>
</button>
<button id="emaillink" type="button" class="hidden btn btn-<?php echo $isDark ? 'warning' : 'default'; ?> navbar-btn">
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> <?php echo I18n::_('Email'), PHP_EOL; ?>
</button>
<?php
if ($QRCODE):
?>
Expand Down Expand Up @@ -561,4 +564,4 @@
endif;
?>
</body>
</html>
</html>
3 changes: 2 additions & 1 deletion tpl/page.php
Expand Up @@ -49,7 +49,7 @@
endif;
?>
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.11.js" integrity="sha512-p7UyJuyBkhMcMgE4mDsgK0Lz70OvetLefua1oXs1OujWv9gOxh4xy8InFux7bZ4/DAZsTmO4rgVwZW9BHKaTaw==" crossorigin="anonymous"></script>
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-86VTqw2HsaCQ0DAunK2MH68P+8RLbbaK7HZP8nwDtwNoF44usxDCptmD8TC+zwQc7HM46AkrvVFb3ZkIb6VhMQ==" crossorigin="anonymous"></script>
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-BH2vIKNQVc8LGYcqgvCoxIpzG0DFXqxdBCa9ptYzekQXhRZJ6dmr0z1Jlx9XaniFMX1tJQMAIcvVWrFEgskCNA==" crossorigin="anonymous"></script>
<!--[if IE]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;}</style>
<![endif]-->
Expand Down Expand Up @@ -105,6 +105,7 @@
<button id="sendbutton" class="hidden"><img src="img/icon_send.png" width="18" height="15" alt="" /><?php echo I18n::_('Send'); ?></button>
<button id="clonebutton" class="hidden"><img src="img/icon_clone.png" width="15" height="17" alt="" /><?php echo I18n::_('Clone'); ?></button>
<button id="rawtextbutton" class="hidden"><img src="img/icon_raw.png" width="15" height="15" alt="" /><?php echo I18n::_('Raw text'); ?></button>
<button id="emaillink" class="hidden"><img src="img/icon_email.png" width="15" height="15" alt="" /><?php echo I18n::_('Email'); ?></button>
<?php
if ($QRCODE):
?>
Expand Down

0 comments on commit c659207

Please sign in to comment.