connect_db -> pdo_connect

This commit is contained in:
mischa 2022-09-04 18:50:21 +00:00
parent a628e105b0
commit 759223e7d7
24 changed files with 62 additions and 61 deletions

View File

@ -58,7 +58,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($domain_exist && empty($message)) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO alias (address,goto,domain,created,modified) VALUES (?,?,?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $goto, PDO::PARAM_STR);

View File

@ -64,7 +64,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
$maildir = $from . "/";
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO alias (address,goto,domain,created,modified) VALUES (?,'vmail',?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -75,7 +75,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
}
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO mailbox (username,password,name,maildir,domain,created,modified) VALUES (?,?,?,?,?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $hashed, PDO::PARAM_STR);

View File

@ -57,7 +57,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($domain_exist && empty($message)) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO alias (address,goto,domain,created,modified) VALUES (?,?,?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $goto, PDO::PARAM_STR);

View File

@ -63,7 +63,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
$maildir = $from . "/";
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO alias (address,goto,domain,created,modified) VALUES (?,'vmail',?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -74,7 +74,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
}
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO mailbox (username,password,name,maildir,domain,created,modified) VALUES (?,?,?,?,?,NOW(),NOW())");
$sth->bindParam(1, $from, PDO::PARAM_STR);
$sth->bindParam(2, $hashed, PDO::PARAM_STR);

View File

@ -62,7 +62,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($message)) {
$hashed = bcrypt($password1);
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO admin (username,password,created,modified) VALUES (?,?,NOW(),NOW())");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->bindParam(2, $hashed, PDO::PARAM_STR);
@ -87,7 +87,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($message) && !empty($password1)) {
$hashed = bcrypt($password1);
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE admin SET password=?,modified=NOW() WHERE username=?");
$sth->bindParam(1, $hashed, PDO::PARAM_STR);
$sth->bindParam(2, $username, PDO::PARAM_STR);
@ -102,7 +102,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
}
if (empty($message)) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT COUNT(*) FROM domain_admins WHERE username=?");
$sth->execute(array($username));
$count_domain_admins = $sth->fetchColumn();

View File

@ -36,7 +36,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
include '../templates/footer.tpl';
} else {
fwrite($fh, $header);
$dbh = connect_db();
$dbh = pdo_connect();
foreach ($tables as $table) {
$sth = $dbh->query("SHOW CREATE TABLE $table");
$row = $sth->fetch(PDO::FETCH_ASSOC);

View File

@ -35,7 +35,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($domain_exist && $table == "domain") {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$dbh->beginTransaction();
$sth = $dbh->prepare("SELECT COUNT(*) FROM log WHERE domain=?");
@ -110,7 +110,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($table == "admin") {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$dbh->beginTransaction();
$sth = $dbh->prepare("SELECT COUNT(*) FROM admin WHERE username=?");
@ -146,7 +146,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($domain_exist && ($table == 'alias' || $table == 'mailbox')) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("DELETE FROM alias WHERE address=? AND domain=?");
$sth->bindParam(1, $delete, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -164,7 +164,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
}
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("DELETE FROM mailbox WHERE username=? AND domain=?");
$sth->bindParam(1, $delete, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);

View File

@ -50,7 +50,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (!in_array($domain, array_column($list_domains, 'domain'))) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO domain (domain,description,aliases,mailboxes,created,modified) VALUES (?,?,?,?,NOW(),NOW())");
$sth->bindParam(1, $domain, PDO::PARAM_STR);
$sth->bindParam(2, $description, PDO::PARAM_STR);
@ -67,7 +67,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (in_array($domain, array_column($list_domains, 'domain')) && $action == 'edit') {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE domain SET description=?,aliases=?,mailboxes=?,modified=NOW() WHERE domain=?");
$sth->bindParam(1, $description, PDO::PARAM_STR);
$sth->bindParam(2, $aliases, PDO::PARAM_INT);

View File

@ -34,7 +34,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($domain_exist) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT goto FROM alias WHERE address=? AND domain=?");
$sth->bindParam(1, $address, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -73,7 +73,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($domain_exist && empty($message)) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE alias SET goto=?,modified=NOW() WHERE address=? AND domain=?");
$sth->bindParam(1, $goto, PDO::PARAM_STR);
$sth->bindParam(2, $address, PDO::PARAM_STR);

View File

@ -36,7 +36,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($domain_exist) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT * FROM mailbox WHERE username=? AND domain=?");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -65,7 +65,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($message) && isset($domain_key) && !empty($password1)) {
$hashed = bcrypt($password1);
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE mailbox SET password=?,name=?,modified=NOW() WHERE username=? AND domain=?");
$sth->bindParam(1, $hashed, PDO::PARAM_STR);
$sth->bindParam(2, $name, PDO::PARAM_STR);
@ -79,7 +79,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($domain_exist && empty($message)) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE mailbox SET name=?,modified=NOW() WHERE username=? AND domain=?");
$sth->bindParam(1, $name, PDO::PARAM_STR);
$sth->bindParam(2, $username, PDO::PARAM_STR);

View File

@ -26,7 +26,7 @@ $list_domains = list_domains();
$search = filter_input(INPUT_POST, 'search', FILTER_DEFAULT);
if (isset($search)) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT alias.address,alias.goto,alias.modified,alias.domain FROM alias LEFT JOIN mailbox ON alias.address=mailbox.username WHERE alias.address LIKE ? AND mailbox.maildir IS NULL ORDER BY alias.address");
$sth->bindValue(1, '%'.$search.'%', PDO::PARAM_STR);
$sth->execute();

View File

@ -28,7 +28,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
$domain_exist = in_array($domain, array_column($list_domains, 'domain'));
if ($domain_exist) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT * FROM log WHERE domain=? ORDER BY timestamp DESC LIMIT 10");
$sth->bindParam(1, $domain, PDO::PARAM_STR);
$sth->execute();

View File

@ -36,7 +36,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($domain_exist && ($table == 'alias' || $table == 'mailbox')) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("DELETE FROM alias WHERE address=? AND domain=?");
$sth->bindParam(1, $delete, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -54,7 +54,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
}
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("DELETE FROM mailbox WHERE username=? AND domain=?");
$sth->bindParam(1, $delete, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);

View File

@ -35,7 +35,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($domain_exist) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT goto FROM alias WHERE address=? AND domain=?");
$sth->bindParam(1, $address, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -74,7 +74,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($domain_exist && empty($message)) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE alias SET goto=?,modified=NOW() WHERE address=? AND domain=?");
$sth->bindParam(1, $goto, PDO::PARAM_STR);
$sth->bindParam(2, $address, PDO::PARAM_STR);

View File

@ -37,7 +37,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($domain_exist) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT * FROM mailbox WHERE username=? AND domain=?");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);
@ -66,7 +66,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($message) && isset($domain_key) && !empty($password1)) {
$hashed = bcrypt($password1);
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE mailbox SET password=?,name=?,modified=NOW() WHERE username=? AND domain=?");
$sth->bindParam(1, $hashed, PDO::PARAM_STR);
$sth->bindParam(2, $name, PDO::PARAM_STR);
@ -80,7 +80,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($domain_exist && empty($message)) {
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE mailbox SET name=?,modified=NOW() WHERE username=? AND domain=?");
$sth->bindParam(1, $name, PDO::PARAM_STR);
$sth->bindParam(2, $username, PDO::PARAM_STR);

View File

@ -55,11 +55,23 @@ function check_language() {
}
//
// connect_db
// Action: make db connection
// Call: connect_db()
// bcrypt
// Action: Hashs the password with bcrypt
// Call: bcrypt(string cleartextpassword)
//
function connect_db() {
function bcrypt($password) {
$options = ['cost' => 8];
$hashed = password_hash($password, PASSWORD_BCRYPT, $options);
$hashed = preg_replace('/\$2y\$/', '\$2b\$', $hashed);
return $hashed;
}
//
// pdo_connect
// Action: make db connection
// Call: pdo_connect()
//
function pdo_connect() {
try {
$dbh = new PDO(DB_TYPE . ':host='. DB_HOST . ';dbname='. DB_NAME , DB_USER, DB_PASS, array(PDO::ATTR_PERSISTENT => true));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@ -76,7 +88,7 @@ function connect_db() {
// Call: list_domains(string admin (optional))
//
function list_domains($username = null) {
$dbh = connect_db();
$dbh = pdo_connect();
if (isset($username)) {
$sth = $dbh->prepare("SELECT * FROM domain INNER JOIN domain_admins ON domain.domain=domain_admins.domain WHERE domain_admins.username=? ORDER BY domain_admins.domain");
$sth->bindParam(1, $username, PDO::PARAM_STR);
@ -106,7 +118,7 @@ function list_domains($username = null) {
// Call: list_aliases(string domain, int offset)
//
function list_aliases($domain, $offset, $limit) {
$dbh = connect_db();
$dbh = pdo_connect();
if (ALIAS_CONTROL == 'NO') {
$sth = $dbh->prepare("SELECT alias.address,alias.goto,alias.modified FROM alias LEFT JOIN mailbox ON alias.address=mailbox.username WHERE alias.domain=? AND mailbox.maildir IS NULL ORDER BY alias.address LIMIT ?, ?");
} else {
@ -126,7 +138,7 @@ function list_aliases($domain, $offset, $limit) {
// Call: list_mailboxes(string domaini, int offset)
//
function list_mailboxes($domain, $offset, $limit) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT * FROM mailbox WHERE domain=? ORDER BY username LIMIT ?, ?");
$sth->bindParam(1, $domain, PDO::PARAM_STR);
$sth->bindParam(2, $offset, PDO::PARAM_INT);
@ -156,18 +168,6 @@ function list_admins() {
return $list;
}
//
// bcrypt
// Action: Hashs the password with bcrypt
// Call: bcrypt(string cleartextpassword)
//
function bcrypt($password) {
$options = ['cost' => 8];
$hashed = password_hash($password, PASSWORD_BCRYPT, $options);
$hashed = preg_replace('/\$2y\$/', '\$2b\$', $hashed);
return $hashed;
}
// logging
// Action: Logs actions from admin
// Call: logging(string username, string domain, string action, string data)
@ -176,7 +176,7 @@ function logging($username, $domain, $action, $data) {
$remote_addr = $_SERVER['HTTP_X_CLIENTIP'] ?? $_SERVER['REMOTE_ADDR'];
$username = $username . ' (' . $remote_addr . ')';
if (LOGGING == 'YES') {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO log (timestamp,username,domain,action,data) VALUES (NOW(),?,?,?,?)");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->bindParam(2, $domain, PDO::PARAM_STR);

View File

@ -27,7 +27,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
$password = filter_input(INPUT_POST, 'password', FILTER_DEFAULT);
if (!empty($username) && !empty($password)) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT password FROM admin WHERE username=?");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->execute();

View File

@ -35,7 +35,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
}
if (empty($message) && !empty($password_current)) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT password FROM admin WHERE username=?");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->execute();
@ -48,7 +48,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($message) && !empty($password1)) {
$hashed = bcrypt($password1);
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE admin SET password=?,modified=NOW() WHERE username=?");
$sth->bindParam(1, $hashed, PDO::PARAM_STR);
$sth->bindParam(2, $username, PDO::PARAM_STR);

View File

@ -27,7 +27,7 @@ $list_domains = list_domains($SESSID_USERNAME);
$search = filter_input(INPUT_POST, 'search', FILTER_DEFAULT);
if (isset($search)) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT alias.address,alias.goto,alias.modified,alias.domain FROM alias LEFT JOIN mailbox ON alias.address=mailbox.username WHERE alias.address LIKE ? AND mailbox.maildir IS NULL ORDER BY alias.address");
$sth->bindValue(1, '%'.$search.'%', PDO::PARAM_STR);
$sth->execute();

View File

@ -45,6 +45,7 @@ if (count($list_mailbox) > 0) {
echo " <tr class=\"hilightoff\" onMouseOver=\"className='hilighton';\" onMouseOut=\"className='hilightoff';\">\n";
echo " <td>" . $row['username'] . "</td>\n";
echo " <td>" . $row['name'] . "</td>\n";
echo " <td>" . $row['modified'] . "</td>\n";
echo " <td><a href=\"edit-mailbox.php?action=edit&username=" . $row['username'] . "&domain=" . $row['domain'] . "\">" . $LANG['edit'] . "</a></td>\n";
echo " <td><a href=\"delete.php?table=mailbox&delete=" . $row['username'] . "&domain=" . $row['domain'] . "\"onclick=\"return confirm ('" . $LANG['confirm'] . $LANG['Overview_get_mailboxes'] . ": ". $row['username'] . "')\">" . $LANG['del'] . "</a></td>\n";
echo " </tr>\n";

View File

@ -27,7 +27,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
$password = filter_input(INPUT_POST, 'password', FILTER_DEFAULT);
if (!empty($username) && !empty($password)) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT password FROM mailbox WHERE username=?");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->execute();

View File

@ -36,7 +36,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
}
if (empty($message) && !empty($password_current)) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT password FROM mailbox WHERE username=?");
$sth->bindParam(1, $username, PDO::PARAM_STR);
$sth->execute();
@ -49,7 +49,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($message) && !empty($password1)) {
$hashed = bcrypt($password1);
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("UPDATE mailbox SET password=?,modified=NOW() WHERE username=?");
$sth->bindParam(1, $hashed, PDO::PARAM_STR);
$sth->bindParam(2, $username, PDO::PARAM_STR);

View File

@ -27,7 +27,7 @@ $SESSID_USERNAME = check_session('userid');
$USERID_DOMAIN = substr(strrchr($SESSID_USERNAME, "@"), 1);
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT COUNT(*) FROM vacation WHERE email=?");
$sth->execute(array($SESSID_USERNAME));
@ -45,7 +45,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (!empty($_POST['back'])) {
$action = 'back';
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("DELETE FROM vacation WHERE email=?");
$sth->bindParam(1, $SESSID_USERNAME, PDO::PARAM_STR);
$sth->execute();
@ -60,7 +60,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (!empty($_POST['away'])) {
$action = 'away';
try {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("INSERT INTO vacation (email,subject,body,cache,domain,created) VALUES (?,?,?,'',?,NOW())");
$sth->bindParam(1, $SESSID_USERNAME, PDO::PARAM_STR);
$sth->bindParam(2, $subject, PDO::PARAM_STR);

View File

@ -29,7 +29,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") {
$domain_exist = in_array($domain, array_column($list_domains, 'domain'));
if ($domain_exist) {
$dbh = connect_db();
$dbh = pdo_connect();
$sth = $dbh->prepare("SELECT * FROM log WHERE domain=? ORDER BY timestamp DESC LIMIT 10");
$sth->bindParam(1, $domain, PDO::PARAM_STR);
$sth->execute();