2013-09-27 01:40:27 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
|
|
|
|
gpg-mailgate
|
|
|
|
|
|
|
|
This file is part of the gpg-mailgate source code.
|
|
|
|
|
|
|
|
gpg-mailgate is free software: you can redistribute it and/or modify
|
2013-10-02 20:27:28 +02:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2013-09-27 01:40:27 +02:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
gpg-mailgate source code is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2013-10-02 20:27:28 +02:00
|
|
|
GNU General Public License for more details.
|
2013-09-27 01:40:27 +02:00
|
|
|
|
2013-10-02 20:27:28 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
2013-09-27 01:40:27 +02:00
|
|
|
along with gpg-mailgate source code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
//returns true on success or error message on failure
|
|
|
|
function requestPGP($email, $key) {
|
|
|
|
require_once(includePath() . "/lock.php");
|
2013-09-28 23:38:18 +02:00
|
|
|
global $config, $lang;
|
2013-09-27 01:40:27 +02:00
|
|
|
|
|
|
|
if(!checkLock('requestpgp')) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return $lang['submit_error_trylater'];
|
2013-09-27 01:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return $lang['submit_error_bademail'];
|
2013-09-27 01:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(strlen($email) > 256 || strlen($key) > 1024 * 32) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return $lang['submit_error_toolong'];
|
2013-09-27 01:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!isAscii($key)) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return $lang['submit_error_nonascii'];
|
2013-09-27 01:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//housekeeping
|
|
|
|
databaseQuery("DELETE FROM gpgmw_keys WHERE time < DATE_SUB(NOW(), INTERVAL 48 HOUR) AND confirm != '' AND status = 0");
|
|
|
|
|
|
|
|
//if we already have an unaccepted key for this user, only replace if one day has elapsed since the last request
|
|
|
|
// this may prevent spam
|
|
|
|
$result = databaseQuery("SELECT HOUR(TIMEDIFF(time, NOW())), id FROM gpgmw_keys WHERE email = ? AND status = 0", array($email));
|
|
|
|
|
|
|
|
if($row = $result->fetch()) {
|
|
|
|
if($row[0] < 24) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return $lang['submit_error_alreadyqueue'];
|
2013-09-27 01:40:27 +02:00
|
|
|
} else {
|
|
|
|
databaseQuery('DELETE FROM gpgmw_keys WHERE id = ?', array($row[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 03:57:07 +02:00
|
|
|
//if PGP key verification is enabled, do it
|
|
|
|
if($config['pgpverify_enable']) {
|
|
|
|
require_once(includePath() . "/gpg.php");
|
|
|
|
|
|
|
|
if(!verifyPGPKey($key, $email)) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return $lang['submit_error_badkey'];
|
2013-09-28 03:57:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 01:40:27 +02:00
|
|
|
//well, it looks good, let's submit it
|
|
|
|
lockAction('requestpgp');
|
|
|
|
$confirm = uid(32);
|
2013-09-28 23:38:18 +02:00
|
|
|
$confirm_link = "{$config['site_url']}/confirm.php?email=" . urlencode($email) . "&confirm=$confirm";
|
|
|
|
$result = gpgmw_mail($config['email_subject_requestpgp'], sprintf($lang['mail_confirm'], $confirm_link), $email);
|
2013-09-27 01:40:27 +02:00
|
|
|
|
|
|
|
if(!$result) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return $lang['submit_error_emailfail'];
|
2013-09-27 01:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
databaseQuery("INSERT INTO gpgmw_keys (email, publickey, confirm) VALUES (?, ?, ?)", array($email, $key, $confirm));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns false on failure or true on success
|
|
|
|
function confirmPGP($email, $confirm) {
|
2013-09-28 02:59:44 +02:00
|
|
|
require_once(includePath() . "/lock.php");
|
|
|
|
|
2013-09-27 01:40:27 +02:00
|
|
|
if(!lockAction('confirmpgp')) {
|
2013-09-28 23:38:18 +02:00
|
|
|
return false;
|
2013-09-27 01:40:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = databaseQuery("SELECT id FROM gpgmw_keys WHERE confirm = ? AND email = ?", array($confirm, $email));
|
|
|
|
|
|
|
|
if($row = $result->fetch()) {
|
|
|
|
databaseQuery("UPDATE gpgmw_keys SET confirm = '' WHERE id = ?", array($row[0]));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|