Skip to content

Instantly share code, notes, and snippets.

@Roy-Orbison
Created September 13, 2017 08:03
Show Gist options
  • Save Roy-Orbison/14cbf7b80981be25ba69aa6e5152887c to your computer and use it in GitHub Desktop.
Save Roy-Orbison/14cbf7b80981be25ba69aa6e5152887c to your computer and use it in GitHub Desktop.
Sendy bulk delete (tested with v3.0.3 and PHP v5.3.29)
<?php
#ini_set('error_reporting', E_ALL & ~E_NOTICE);
#ini_set('display_errors', 1);
if (isset($_POST['del'])) {
include('includes/functions.php');
include('includes/login/auth.php');
$emails_del_all = array();
switch ($_POST['del']) {
case 'csv':
$csvfile = $_FILES['csv_file']['tmp_name'];
if (!file_exists($csvfile)) {
header('Location: delete-from-all-lists?e=3');
exit;
}
ini_set('auto_detect_line_endings', true);
$file = fopen($csvfile,"r");
if(!$file) {
echo _('Error opening data file.'), "\n";
exit;
}
while ($row = fgetcsv($file)) {
$row = array_filter($row, 'strlen');
switch (count($row)) {
case 1:
$emails_del_all[] = reset($row);
case 0:
continue 2;
default:
header('Location: delete-from-all-lists?e=1');
exit;
}
}
break;
case 'input':
$emails_del_all = preg_split('/\\s*?[\\r\\n]++\\s*/', $_POST['line'], null, PREG_SPLIT_NO_EMPTY);
break;
}
if (!$emails_del_all) {
echo _('Nothing provided to delete.'), "\n";
exit;
}
$emails_del_count_last = 0;
while ($emails_del_all) {
$emails_del = array_splice($emails_del_all, 0, 20);# delete in batches of 20 at most
$emails_del_count = count($emails_del);
if ($emails_del_count != $emails_del_count_last) {
$mysqli_stmt = $mysqli->prepare(
'DELETE FROM subscribers
WHERE email IN (' . implode(', ', array_fill(0, $emails_del_count, '?')) . ')'
);
$bind_param_types = str_repeat('s', $emails_del_count);
$emails_del_count_last = $emails_del_count;
}
$bind_param_args = array(
$bind_param_types,
);
foreach ($emails_del as $i => $email_del) {
$bind_param_args[] = &$emails_del[$i];
}
call_user_func_array(array(&$mysqli_stmt, 'bind_param'), $bind_param_args);
if (!$mysqli_stmt->execute()) {
echo $mysqli_stmt->error, "\n";
exit;
}
}
header('Location: delete-from-all-lists', true, 303);
exit;
}
include('includes/header.php');
include('includes/login/auth.php');
include('includes/subscribers/main.php');
if(isset($_GET['e'])) $err = $_GET['e'];
else $err = '';
?>
<!-- Validation -->
<script type="text/javascript" src="js/validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#import-delete-form").validate({
rules: {
csv_file: {
required: true
}
},
messages: {
csv_file: "<?php echo addslashes(_('Please upload a CSV file'));?>"
}
});
$("#line-import-form").validate({
rules: {
line: {
required: true
}
},
messages: {
line: "<?php echo addslashes(_('Please enter at least one combination of name & email'));?>"
}
});
});
</script>
<h2><?php echo _('Mass delete via CSV file');?></h2><br/>
<form action="" method="POST" accept-charset="utf-8" class="form-vertical" enctype="multipart/form-data" id="import-delete-form">
<input type="hidden" name="del" value="csv">
<?php if($_GET['e']==1):?>
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><?php echo _('There should only be 1 column in your CSV containing emails.');?></strong>
</div>
<?php elseif($_GET['e']==3):?>
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><?php echo _('Please upload a CSV file.');?></strong>
</div>
<?php endif;?>
<label class="control-label" for="csv_file"><em><?php echo _('CSV format example');?>:</em></label>
<table class="table table-bordered table-striped table-condensed" style="width: 300px;">
<tbody>
<tr>
<td>[email protected]</td>
</tr>
<tr>
<td>[email protected]</td>
</tr>
</tbody>
</table>
<div class="control-group">
<div class="controls">
<input type="file" class="input-xlarge" id="csv_file" name="csv_file">
</div>
</div>
<br/>
<button type="submit" class="btn btn-inverse"><?php echo _('Import');?></button>
</form>
<br/>
<h2><?php echo _('Delete email per line');?></h2><br/>
<form action="" method="POST" accept-charset="utf-8" class="form-vertical" enctype="multipart/form-data" id="line-import-form">
<input type="hidden" name="del" value="input">
<?php if($_GET['e']==2):?>
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><?php echo _('Sorry, we didn\'t receive any input.');?></strong>
</div>
<?php endif;?>
<label class="control-label" for="line"><?php echo _('Email to delete');?></label>
<div class="control-group">
<div class="controls">
<textarea class="input-xlarge" id="line" name="line" rows="10" placeholder="Eg. [email protected]"></textarea>
</div>
</div>
<br/>
<button type="submit" class="btn btn-inverse"><?php echo _('Delete');?></button>
</form>
<?php include('includes/footer.php');?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment