|
@@ -1,63 +1,211 @@
|
|
-<?php
|
|
|
|
- if(isset($_POST['savemode'])){
|
|
|
|
- $savemode = $_POST['savemode'];
|
|
|
|
-
|
|
|
|
- if($savemode === "edit"){
|
|
|
|
-
|
|
|
|
- if(!isset($_POST['id'])){
|
|
|
|
- // Redirect id not set, redirect to overview
|
|
|
|
- redirect("admin/listredirects/");
|
|
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+$id = null;
|
|
|
|
+$redirect = null;
|
|
|
|
+
|
|
|
|
+if(isset($_GET['id'])){
|
|
|
|
+ $id = $db->escape_string($_GET['id']);
|
|
|
|
+
|
|
|
|
+ if(defined('DBC_ALIASES_MULTI_SOURCE')){
|
|
|
|
+ $sql = "SELECT r.* FROM (
|
|
|
|
+ SELECT
|
|
|
|
+ group_concat(g.`".DBC_ALIASES_ID."` ORDER BY g.`".DBC_ALIASES_ID."` SEPARATOR ',') AS `".DBC_ALIASES_ID."`,
|
|
|
|
+ group_concat(g.`".DBC_ALIASES_SOURCE."` SEPARATOR ',') AS `".DBC_ALIASES_SOURCE."`,
|
|
|
|
+ g.`".DBC_ALIASES_DESTINATION."`,
|
|
|
|
+ g.`".DBC_ALIASES_MULTI_SOURCE."`
|
|
|
|
+ FROM `".DBT_ALIASES."` AS g
|
|
|
|
+ WHERE g.`".DBC_ALIASES_MULTI_SOURCE."` IS NOT NULL
|
|
|
|
+ GROUP BY g.`".DBC_ALIASES_MULTI_SOURCE."`
|
|
|
|
+ UNION
|
|
|
|
+ SELECT
|
|
|
|
+ s.`".DBC_ALIASES_ID."`,
|
|
|
|
+ s.`".DBC_ALIASES_SOURCE."`,
|
|
|
|
+ s.`".DBC_ALIASES_DESTINATION."`,
|
|
|
|
+ s.`".DBC_ALIASES_MULTI_SOURCE."`
|
|
|
|
+ FROM `".DBT_ALIASES."` AS s
|
|
|
|
+ WHERE s.`".DBC_ALIASES_MULTI_SOURCE."` IS NULL
|
|
|
|
+ ) AS r
|
|
|
|
+ WHERE `".DBC_ALIASES_ID."` = '$id' LIMIT 1;";
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ $sql = "SELECT `".DBC_ALIASES_ID."`, `".DBC_ALIASES_SOURCE."`, `".DBC_ALIASES_DESTINATION."` FROM `".DBT_ALIASES."` WHERE `".DBC_ALIASES_ID."` = '$id' LIMIT 1;";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(!$result = $db->query($sql)){
|
|
|
|
+ dbError($db->error);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if($result->num_rows !== 1){
|
|
|
|
+ // Redirect does not exist, redirect to overview
|
|
|
|
+ redirect("admin/listredirects/");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $redirect = $result->fetch_assoc();
|
|
|
|
+
|
|
|
|
+ $sources = stringToEmails($redirect[DBC_ALIASES_SOURCE]);
|
|
|
|
+ $destinations = stringToEmails($redirect[DBC_ALIASES_DESTINATION]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+if(isset($_POST['savemode'])){
|
|
|
|
+ $savemode = $_POST['savemode'];
|
|
|
|
+
|
|
|
|
+ $sources = stringToEmails($_POST['source']);
|
|
|
|
+ $destinations = stringToEmails($_POST['destination']);
|
|
|
|
+
|
|
|
|
+ // validate emails
|
|
|
|
+ $emailErrors = array();
|
|
|
|
+
|
|
|
|
+ // basic email validation is not working 100% correct though
|
|
|
|
+ foreach(array_merge($sources, $destinations) as $email){
|
|
|
|
+ if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
|
|
|
|
+ $emailErrors[$email] = "Address \"$email\" is not a valid email address.";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(defined('VALIDATE_ALIASES_SOURCE_ENABLED')){
|
|
|
|
+ $sql = "SELECT GROUP_CONCAT(`".DBC_DOMAINS_DOMAIN."` SEPARATOR ',') as `".DBC_DOMAINS_DOMAIN."` FROM `".DBT_DOMAINS."`";
|
|
|
|
+ if(!$resultDomains = $db->query($sql)){
|
|
|
|
+ dbError($db->error);
|
|
|
|
+ }
|
|
|
|
+ $domainRow = $resultDomains->fetch_assoc();
|
|
|
|
+ $domains = explode(',', $domainRow[DBC_DOMAINS_DOMAIN]);
|
|
|
|
+
|
|
|
|
+ // validate source emails are on domains
|
|
|
|
+ foreach($sources as $email){
|
|
|
|
+ if(isset($emailErrors[$email])){
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
|
|
+ $splited = explode('@', $email);
|
|
|
|
+ if(count($splited) !== 2 || !in_array($splited[1], $domains)){
|
|
|
|
+ $emailErrors[$email] = "Domain of source address \"$email\" not in domains.";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(count($emailErrors) > 0){
|
|
|
|
+ add_message("fail", implode("<br>", $emailErrors));
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ if(count($emailErrors) === 0 && $savemode === "edit" && !is_null($redirect)){
|
|
|
|
+
|
|
|
|
+ if(count($sources) > 0 && count($destinations) > 0){
|
|
|
|
+ $destination = $db->escape_string(emailsToString($destinations));
|
|
|
|
+ $source = $db->escape_string(emailsToString($sources));
|
|
|
|
+
|
|
|
|
+ $key = DBC_ALIASES_ID;
|
|
|
|
+ if(defined('DBC_ALIASES_MULTI_SOURCE') && !empty($redirect[DBC_ALIASES_MULTI_SOURCE])){
|
|
|
|
+ $key = DBC_ALIASES_MULTI_SOURCE;
|
|
|
|
+ }
|
|
|
|
+ $value = $redirect[$key];
|
|
|
|
|
|
- $id = $db->escape_string($_POST['id']);
|
|
|
|
-
|
|
|
|
- $source = $db->escape_string($_POST['source']);
|
|
|
|
- $source = strtolower($source);
|
|
|
|
- $destination = $db->escape_string($_POST['destination']);
|
|
|
|
- $destination = strtolower($destination);
|
|
|
|
-
|
|
|
|
- if($source !== "" && $destination !== ""){
|
|
|
|
-
|
|
|
|
- $sql = "SELECT `".DBC_ALIASES_ID."` FROM `".DBT_ALIASES."` WHERE `".DBC_ALIASES_ID."` = '$id' LIMIT 1;";
|
|
|
|
- if(!$resultExists = $db->query($sql)){
|
|
|
|
|
|
+ $sql = "SELECT `".DBC_ALIASES_ID."`, `".DBC_ALIASES_SOURCE."` FROM `".DBT_ALIASES."` WHERE `$key` = '$value'";
|
|
|
|
+ if(!$resultExisting = $db->query($sql)){
|
|
dbError($db->error);
|
|
dbError($db->error);
|
|
}
|
|
}
|
|
|
|
|
|
- if($resultExists->num_rows !== 1){
|
|
|
|
- // Redirect does not exist, redirect to overview
|
|
|
|
- redirect("admin/listredirects/");
|
|
|
|
|
|
+ $sourceIdMap = array();
|
|
|
|
+ while($existingRedirect = $resultExisting->fetch_assoc()){
|
|
|
|
+ $sourceIdMap[$existingRedirect[DBC_ALIASES_SOURCE]] = $existingRedirect[DBC_ALIASES_ID];
|
|
}
|
|
}
|
|
|
|
|
|
- $sql = "UPDATE `".DBT_ALIASES."` SET `".DBC_ALIASES_SOURCE."` = '$source', `".DBC_ALIASES_DESTINATION."` = '$destination' WHERE `".DBC_ALIASES_ID."` = '$id'";
|
|
|
|
-
|
|
|
|
- if(!$result = $db->query($sql)){
|
|
|
|
- dbError($db->error);
|
|
|
|
|
|
+ // multi source handling
|
|
|
|
+ $hash = (count($sources) === 1) ? "NULL" : "'".md5($source)."'";
|
|
|
|
+
|
|
|
|
+ foreach($sources as $sourceAddress){
|
|
|
|
+ $sourceAddress = $db->escape_string(formatEmail($sourceAddress));
|
|
|
|
+
|
|
|
|
+ if(isset($sourceIdMap[$sourceAddress])){
|
|
|
|
+ // edit existing source
|
|
|
|
+ $id = $sourceIdMap[$sourceAddress];
|
|
|
|
+
|
|
|
|
+ $additionalSql = defined('DBC_ALIASES_MULTI_SOURCE') ? ", `".DBC_ALIASES_MULTI_SOURCE."` = $hash " : "";
|
|
|
|
+ $sql = "UPDATE `".DBT_ALIASES."` SET `".DBC_ALIASES_SOURCE."` = '$sourceAddress', `".DBC_ALIASES_DESTINATION."` = '$destination' $additionalSql WHERE `".DBC_ALIASES_ID."` = '$id';";
|
|
|
|
+
|
|
|
|
+ if(!$result = $db->query($sql)){
|
|
|
|
+ dbError($db->error);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ unset($sourceIdMap[$sourceAddress]); // mark updated
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ // add new source
|
|
|
|
+ $additionalSql = defined('DBC_ALIASES_MULTI_SOURCE') ? ", `".DBC_ALIASES_MULTI_SOURCE."`" : "";
|
|
|
|
+ $additionalSqlValue = defined('DBC_ALIASES_MULTI_SOURCE') ? ", $hash" : "";
|
|
|
|
+ $sql = "INSERT INTO `".DBT_ALIASES."` (`".DBC_ALIASES_SOURCE."`, `".DBC_ALIASES_DESTINATION."` $additionalSql) VALUES ('$sourceAddress', '$destination' $additionalSqlValue);";
|
|
|
|
+
|
|
|
|
+ if(!$result = $db->query($sql)){
|
|
|
|
+ dbError($db->error);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- else{
|
|
|
|
- // Edit successfull, redirect to overview
|
|
|
|
- redirect("admin/listredirects/?edited=1");
|
|
|
|
|
|
+
|
|
|
|
+ // delete none updated redirect
|
|
|
|
+ foreach($sourceIdMap as $source => $id){
|
|
|
|
+ $sql = "DELETE FROM `".DBT_ALIASES."` WHERE `".DBC_ALIASES_ID."` = '$id';";
|
|
|
|
+
|
|
|
|
+ if(!$result = $db->query($sql)){
|
|
|
|
+ dbError($db->error);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // Edit successfull, redirect to overview
|
|
|
|
+ redirect("admin/listredirects/?edited=1");
|
|
}
|
|
}
|
|
else{
|
|
else{
|
|
add_message("fail", "Redirect could not be edited. Fill out all fields.");
|
|
add_message("fail", "Redirect could not be edited. Fill out all fields.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- else if($savemode === "create"){
|
|
|
|
- $source = $db->escape_string($_POST['source']);
|
|
|
|
- $source = strtolower($source);
|
|
|
|
- $destination = $db->escape_string($_POST['destination']);
|
|
|
|
- $destination = strtolower($destination);
|
|
|
|
-
|
|
|
|
- if($source !== "" && $destination !== ""){
|
|
|
|
- $sql = "INSERT INTO `".DBT_ALIASES."` (`".DBC_ALIASES_SOURCE."`, `".DBC_ALIASES_DESTINATION."`) VALUES ('$source', '$destination')";
|
|
|
|
-
|
|
|
|
- if(!$result = $db->query($sql)){
|
|
|
|
|
|
+
|
|
|
|
+ else if(count($emailErrors) === 0 && $savemode === "create"){
|
|
|
|
+ if(count($sources) > 0 && count($destinations) > 0){
|
|
|
|
+
|
|
|
|
+ $values = array();
|
|
|
|
+ foreach($sources as $source){
|
|
|
|
+ $values[] = "'$source'";
|
|
|
|
+ }
|
|
|
|
+ $sql = "SELECT `".DBC_ALIASES_SOURCE."` FROM `".DBT_ALIASES."` WHERE `".DBC_ALIASES_SOURCE."` IN (".implode(',', $values).");";
|
|
|
|
+ if(!$resultExisting = $db->query($sql)){
|
|
dbError($db->error);
|
|
dbError($db->error);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ $errorExisting = array();
|
|
|
|
+ while($existingRedirect = $resultExisting->fetch_assoc()){
|
|
|
|
+ $email = $existingRedirect[DBC_ALIASES_SOURCE];
|
|
|
|
+ $errorExisting[] = "Source address \"$email\" is already redirected to some destination.";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(count($errorExisting) > 0){
|
|
|
|
+ add_message("fail", implode("<br>", $errorExisting));
|
|
|
|
+ }
|
|
else{
|
|
else{
|
|
- // Redirect created, redirect to overview
|
|
|
|
- redirect("admin/listredirects/?created=1");
|
|
|
|
|
|
+ $destination = $db->escape_string(emailsToString($destinations));
|
|
|
|
+ $source = $db->escape_string(emailsToString($sources));
|
|
|
|
+
|
|
|
|
+ $values = array();
|
|
|
|
+ if(count($sources) === 1){
|
|
|
|
+ $values[] = "('$source', '$destination', NULL)";
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ // multi source handling
|
|
|
|
+ $hash = md5($source);
|
|
|
|
+
|
|
|
|
+ foreach($sources as $sourceAddress){
|
|
|
|
+ $sourceAddress = $db->escape_string(formatEmail($sourceAddress));
|
|
|
|
+ $additionalSqlValue = defined('DBC_ALIASES_MULTI_SOURCE') ? ", '$hash'" : "";
|
|
|
|
+ $values[] = "('$sourceAddress', '$destination' $additionalSqlValue)";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $additionalSql = defined('DBC_ALIASES_MULTI_SOURCE') ? ", `".DBC_ALIASES_MULTI_SOURCE."`" : "";
|
|
|
|
+ $sql = "INSERT INTO `".DBT_ALIASES."` (`".DBC_ALIASES_SOURCE."`, `".DBC_ALIASES_DESTINATION."` $additionalSql) VALUES ".implode(',', $values).";";
|
|
|
|
+
|
|
|
|
+ if(!$result = $db->query($sql)){
|
|
|
|
+ dbError($db->error);
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ // Redirect created, redirect to overview
|
|
|
|
+ redirect("admin/listredirects/?created=1");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
else{
|
|
@@ -65,66 +213,50 @@
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- // Select mode
|
|
|
|
- $mode = "create";
|
|
|
|
- if(isset($_GET['id'])){
|
|
|
|
- $mode = "edit";
|
|
|
|
- $id = $db->escape_string($_GET['id']);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if($mode === "edit"){
|
|
|
|
- //Load user data from DB
|
|
|
|
- $sql = "SELECT `".DBC_ALIASES_SOURCE."`, `".DBC_ALIASES_DESTINATION."` FROM `".DBT_ALIASES."` WHERE `".DBC_ALIASES_ID."` = '$id' LIMIT 1;";
|
|
|
|
-
|
|
|
|
- if(!$result = $db->query($sql)){
|
|
|
|
- dbError($db->error);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if($result->num_rows !== 1){
|
|
|
|
- // Redirect does not exist, redirect to overview
|
|
|
|
- redirect("admin/listredirects/");
|
|
|
|
- }
|
|
|
|
|
|
+}
|
|
|
|
|
|
- $row = $result->fetch_assoc();
|
|
|
|
|
|
|
|
- $source = $row[DBC_ALIASES_SOURCE];
|
|
|
|
- $destination = $row[DBC_ALIASES_DESTINATION];
|
|
|
|
- }
|
|
|
|
|
|
+// Select mode
|
|
|
|
+$mode = "create";
|
|
|
|
+if(isset($_GET['id'])){
|
|
|
|
+ $mode = "edit";
|
|
|
|
+}
|
|
?>
|
|
?>
|
|
|
|
|
|
-<h1><?php if($mode === "create") { ?> Create <?php } else {?>Edit <?php } ?>Redirect</h1>
|
|
|
|
|
|
+<h1><?php echo ($mode === "create") ? 'Create' : 'Edit'; ?> Redirect</h1>
|
|
|
|
|
|
<?php output_messages(); ?>
|
|
<?php output_messages(); ?>
|
|
|
|
|
|
-<p>
|
|
|
|
-Here you can edit a redirect.
|
|
|
|
-</p>
|
|
|
|
-
|
|
|
|
<p>
|
|
<p>
|
|
<a class="button button-small" href="<?php echo FRONTEND_BASE_PATH; ?>admin/listredirects/">❬ Back to redirects list</a>
|
|
<a class="button button-small" href="<?php echo FRONTEND_BASE_PATH; ?>admin/listredirects/">❬ Back to redirects list</a>
|
|
</p>
|
|
</p>
|
|
|
|
|
|
<form action="" method="post">
|
|
<form action="" method="post">
|
|
|
|
+ <input name="savemode" type="hidden" value="<?php echo isset($mode) ? $mode : ''; ?>"/>
|
|
|
|
+
|
|
|
|
+ <p>
|
|
|
|
+ Enter single or multiple addresses separated by comma, semicolon or newline.
|
|
|
|
+ </p>
|
|
|
|
+
|
|
<table>
|
|
<table>
|
|
- <tr> <th>Source</th> <th>Destination</th> </tr>
|
|
|
|
-
|
|
|
|
- <tr>
|
|
|
|
- <td>
|
|
|
|
- <input type="text" name="source" class="textinput" placeholder="Source (single address)" required="required" value="<?php if(isset($source)){echo strip_tags($source);}?>" autofocus/>
|
|
|
|
- </td>
|
|
|
|
-
|
|
|
|
- <td>
|
|
|
|
- <textarea name="destination" class="textinput" placeholder="Destination (multiple addresses separated by comma possible)" required="required"><?php if(isset($destination)){echo strip_tags($destination);} ?></textarea>
|
|
|
|
- </td>
|
|
|
|
- </tr>
|
|
|
|
-
|
|
|
|
|
|
+ <tr>
|
|
|
|
+ <th>Source</th>
|
|
|
|
+ <th>Destination</th>
|
|
|
|
+ </tr>
|
|
|
|
+ <tr>
|
|
|
|
+ <td>
|
|
|
|
+ <?php if(defined('DBC_ALIASES_MULTI_SOURCE')): ?>
|
|
|
|
+ <textarea name="source" class="textinput" placeholder="Source" required="required" autofocus><?php echo isset($sources) ? strip_tags(emailsToString($sources, FRONTEND_EMAIL_SEPARATOR_FORM)) : ''; ?></textarea>
|
|
|
|
+ <?php else: ?>
|
|
|
|
+ <input type="text" name="source" class="textinput" placeholder="Source (single address)" required="required" autofocus value="<?php echo isset($sources) ? strip_tags(emailsToString($sources, FRONTEND_EMAIL_SEPARATOR_FORM)) : ''; ?>"/>
|
|
|
|
+ <?php endif; ?>
|
|
|
|
+ </td>
|
|
|
|
+ <td>
|
|
|
|
+ <textarea name="destination" class="textinput" placeholder="Destination" required="required"><?php echo isset($destinations) ? strip_tags(emailsToString($destinations, FRONTEND_EMAIL_SEPARATOR_FORM)) : ''; ?></textarea>
|
|
|
|
+ </td>
|
|
|
|
+ </tr>
|
|
</table>
|
|
</table>
|
|
-
|
|
|
|
- <input name="savemode" type="hidden" value="<?php if(isset($mode)){echo $mode;} ?>"/>
|
|
|
|
- <input name="id" type="hidden" value="<?php if(isset($id)){echo $id;} ?>"/>
|
|
|
|
-
|
|
|
|
|
|
+
|
|
<p>
|
|
<p>
|
|
<input type="submit" class="button button-small" value="Save settings">
|
|
<input type="submit" class="button button-small" value="Save settings">
|
|
</p>
|
|
</p>
|