Show ignored items when adding new adlists (#1997)

* Show ignored itens when adding new adlists

Signed-off-by: rdwebdesign <github@rdwebdesign.com.br>

* Show ignored itens when adding new adlists (new messages)

Signed-off-by: rdwebdesign <github@rdwebdesign.com.br>
This commit is contained in:
RD WebDesign
2021-12-17 15:56:49 -03:00
committed by GitHub
parent e76a03bf0f
commit b94884ae17
2 changed files with 50 additions and 7 deletions

View File

@@ -31,6 +31,16 @@ function JSON_success($message = null)
echo json_encode(array('success' => true, 'message' => $message));
}
function JSON_warning($message = null)
{
header('Content-type: application/json');
echo json_encode(array(
'success' => true,
'warning' => true,
'message' => $message,
));
}
function JSON_error($message = null)
{
header('Content-type: application/json');
@@ -958,8 +968,9 @@ if ($_POST['action'] == 'get_groups') {
$addresses = explode(' ', html_entity_decode(trim($_POST['address'])));
$total = count($addresses);
$added = 0;
$ignored = 0;
$stmt = $db->prepare('INSERT OR IGNORE INTO adlist (address,comment) VALUES (:address,:comment)');
$stmt = $db->prepare('INSERT INTO adlist (address,comment) VALUES (:address,:comment)');
if (!$stmt) {
throw new Exception('While preparing statement: ' . $db->lastErrorMsg());
}
@@ -973,6 +984,8 @@ if ($_POST['action'] == 'get_groups') {
throw new Exception('While binding comment: ' . $db->lastErrorMsg());
}
$added_list = "";
$ignored_list = "";
foreach ($addresses as $address) {
// Silently skip this entry when it is empty or not a string (e.g. NULL)
if(!is_string($address) || strlen($address) == 0) {
@@ -980,7 +993,7 @@ if ($_POST['action'] == 'get_groups') {
}
// this will remove first @ that is after schema and before domain
// $1 is optional schema, $2 is userinfo
// $1 is optional schema, $2 is userinfo
$check_address = preg_replace("|([^:/]*://)?([^/]+)@|", "$1$2", $address, 1);
if(preg_match("/[^a-zA-Z0-9:\/?&%=~._()-;]/", $check_address) !== 0) {
@@ -994,10 +1007,20 @@ if ($_POST['action'] == 'get_groups') {
}
if (!$stmt->execute()) {
throw new Exception('While executing: <strong>' . $db->lastErrorMsg() . '</strong><br>'.
'Added ' . $added . " out of ". $total . " adlists");
if ($db->lastErrorCode() == 19) {
// ErrorCode 19 is "Constraint violation", here the unique constraint of `address`
// is violated (https://www.sqlite.org/rescode.html#constraint).
// If the list is already in database, add to ignored list, but don't throw error
$ignored++;
$ignored_list .= "<small>" . $address . "</small><br>";
} else {
throw new Exception('While executing: <strong>' . $db->lastErrorMsg() . '</strong><br>'.
'Added ' . $added . " out of " . $total . " adlists");
}
} else {
$added++;
$added_list .= "<small>" . $address . "</small><br>";
}
$added++;
}
if(!$db->query('COMMIT;')) {
@@ -1005,7 +1028,20 @@ if ($_POST['action'] == 'get_groups') {
}
$reload = true;
JSON_success();
if ($ignored_list != "") {
// Send added and ignored lists
$msg = "<b>Ignored duplicated adlists: " . $ignored . "</b><br>" . $ignored_list;
if ($added_list != "") {
$msg .= "<br><b>Added adlists: " . $added . "</b><br>" . $added_list;
}
$msg .= "<br><b>Total: " . $total . " adlist(s) processed.</b>";
JSON_warning($msg);
} else {
// All adlists added
$msg = $added_list . "<br><b>Total: " . $total . " adlist(s) processed.</b>";
JSON_success($msg);
}
} catch (\Exception $ex) {
JSON_error($ex->getMessage());
}