Skip to content

Commit 56c0394

Browse files
xh3n1michield
authored andcommitted
Hashing workaround for unique indexes (#389)
* hashing workaround on structure page Signed-off-by: Xheni Myrtaj <[email protected]> * Create hash columns and unique indexes on upgrade Signed-off-by: Xheni Myrtaj <[email protected]> * fix syntax Signed-off-by: Xheni Myrtaj <[email protected]> * calculate hash values using md5 Signed-off-by: Xheni Myrtaj <[email protected]> * Change binary size to 16 Signed-off-by: Xheni Myrtaj <[email protected]> * remove linktrack Signed-off-by: Xheni Myrtaj <[email protected]> * apply md5 to regex new entries Signed-off-by: Xheni Myrtaj <[email protected]> * update queries for url new entries Signed-off-by: Xheni Myrtaj <[email protected]> * apply changes to sendmaillib Signed-off-by: Xheni Myrtaj <[email protected]> * change datatype and add where clause Signed-off-by: Xheni Myrtaj <[email protected]> * apply changes to structure Signed-off-by: Xheni Myrtaj <[email protected]> * fix typos Signed-off-by: Xheni Myrtaj <[email protected]>
1 parent d1d6ce3 commit 56c0394

File tree

6 files changed

+39
-29
lines changed

6 files changed

+39
-29
lines changed

public_html/lists/admin/bouncerule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
}
3131

3232
if (isset($_POST['save']) && $_POST['save']) {
33-
Sql_Query(sprintf('update %s set regex = "%s",action="%s", comment="%s",status = "%s" where id= %d',
34-
$GLOBALS['tables']['bounceregex'], trim($_POST['regex']), sql_escape($_POST['action']),
33+
Sql_Query(sprintf('update %s set regex = "%s", regexhash = "%s", action="%s", comment="%s",status = "%s" where id= %d',
34+
$GLOBALS['tables']['bounceregex'], trim($_POST['regex']), md5(trim($_POST['regex'])), sql_escape($_POST['action']),
3535
sql_escape($_POST['comment']), sql_escape($_POST['status']), $_GET['id']), 1);
3636
$num = Sql_Affected_Rows();
3737
if ($num < 0) {

public_html/lists/admin/bouncerules.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
}
4747

4848
if (isset($_POST['newrule']) && $_POST['newrule']) {
49-
Sql_Query(sprintf('insert into %s (regex,action,comment,admin,status) values("%s","%s","%s",%d,"active")',
50-
$GLOBALS['tables']['bounceregex'], sql_escape($_POST['newrule']), sql_escape($_POST['action']),
49+
Sql_Query(sprintf('insert into %s (regex, regexhash, action,comment,admin,status) values("%s","%s","%s","%s",%d,"active")',
50+
$GLOBALS['tables']['bounceregex'], sql_escape($_POST['newrule']), md5(sql_escape($_POST['newrule'])), sql_escape($_POST['action']),
5151
sql_escape($_POST['comment']), $_SESSION['logindetails']['id']), 1);
5252
$num = Sql_Affected_Rows();
5353
if ($num < 0) {

public_html/lists/admin/convertstats.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ function flushbuffer()
8383
break;
8484
}
8585

86-
$exists = Sql_Fetch_Row_Query(sprintf('select id from %s where url = "%s"', $GLOBALS['tables']['linktrack_forward'],
87-
$row['url']));
86+
$exists = Sql_Fetch_Row_Query(sprintf('select id from %s where urlhash = "%s"', $GLOBALS['tables']['linktrack_forward'],
87+
md5($row['url'])));
8888
if (!$exists[0]) {
8989
$personalise = preg_match('/uid=/', $row['forward']);
90-
Sql_Query(sprintf('insert into %s (url,personalise) values("%s",%d)', $GLOBALS['tables']['linktrack_forward'],
91-
$row['url'], $personalise));
90+
Sql_Query(sprintf('insert into %s (url, urlhash, personalise) values("%s","%s", %d)', $GLOBALS['tables']['linktrack_forward'],
91+
$row['url'], md5($row['url']), $personalise));
9292
$fwdid = Sql_Insert_id();
9393
} else {
9494
$fwdid = $exists[0];

public_html/lists/admin/sendemaillib.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,13 +1263,13 @@ function clickTrackLinkId($messageid, $userid, $url, $link)
12631263
* alter table phplist_linktrack_forward add index (url);
12641264
* */
12651265

1266-
$exists = Sql_Fetch_Row_Query(sprintf('select id,uuid from %s where url = "%s"',
1267-
$GLOBALS['tables']['linktrack_forward'], sql_escape(substr($url, 0, 255))));
1266+
$exists = Sql_Fetch_Row_Query(sprintf('select id,uuid from %s where urlhash = "%s"',
1267+
$GLOBALS['tables']['linktrack_forward'], md5(sql_escape($url))));
12681268
if (empty($exists[0])) {
12691269
$personalise = preg_match('/uid=/', $link);
12701270
$uuid = (string)Uuid::generate(4);
1271-
Sql_Query(sprintf('insert into %s set url = "%s", personalise = %d, uuid = "%s"',
1272-
$GLOBALS['tables']['linktrack_forward'], sql_escape($url), $personalise, $uuid));
1271+
Sql_Query(sprintf('insert into %s set url = "%s", urlhash = "%s", personalise = %d, uuid = "%s"',
1272+
$GLOBALS['tables']['linktrack_forward'], sql_escape($url), md5(sql_escape($url)), $personalise, $uuid));
12731273
$fwdid = Sql_Insert_id();
12741274
$fwduuid = $uuid;
12751275
} elseif (empty($exists[1])) {

public_html/lists/admin/structure.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@
359359
),
360360
'urlcache' => array(
361361
'id' => array('integer not null primary key auto_increment', 'ID'),
362-
'url' => array('text not null', ''),
362+
'url' => array('varchar(2083) not null', ''),
363363
'lastmodified' => array('integer', ''),
364364
'added' => array('datetime', ''),
365365
'content' => array('longblob', ''),
@@ -376,21 +376,22 @@
376376

377377
//# keep it in for now, but could be dropped at some point
378378
//# once all dependencies have been removed
379+
// tables that have unique indexes on hash values : linktrack_forward, bounceregex
379380
'linktrack' => array(
380381
'linkid' => array('integer not null primary key auto_increment', 'Link ID'),
381382
'messageid' => array('integer not null', 'Message ID'),
382383
'userid' => array('integer not null', 'subscriber ID'),
383-
'url' => array('text', 'URL to log'),
384-
'forward' => array('text', 'URL to forward to'),
384+
'url' => array('varchar(255)', 'URL to log'),
385+
'forward' => array('varchar(255)', 'URL to forward to'),
385386
'firstclick' => array('datetime', 'When first clicked'),
386387
'latestclick' => array('timestamp', 'When last clicked'),
387388
'clicked' => array('integer default 0', 'Number of clicks'),
388389
'index_1' => array('midindex (messageid)', ''),
389390
'index_2' => array('uidindex (userid)', ''),
390-
'index_3' => array('urlindex (url(255))', ''),
391+
'index_3' => array('urlindex (url)', ''),
391392
'index_4' => array('miduidindex (messageid,userid)', ''),
392393
// "index_5" => array("miduidurlindex (messageid,userid,url)",""),
393-
'unique_1' => array('miduidurlindex (messageid,userid,url(255))', ''),
394+
'unique_1' => array('miduidurlindex (messageid,userid,url)', ''),
394395
),
395396
'linktrack_ml' => array( // ml -> message link
396397
'messageid' => array('integer not null', 'Message ID'),
@@ -425,14 +426,15 @@
425426
),
426427
'linktrack_forward' => array(
427428
'id' => array('integer not null primary key auto_increment', 'forward ID'),
428-
'url' => array('text', 'URL to log'),
429+
'url' => array('varchar(2083)', 'URL to log'),
430+
'urlhash' => array('char(32)', 'hash value of URL'),
429431
'uuid' => array('varchar(36) default ""', 'UUID'),
430432
// "forward" => array("text","URL to forward to"),
431433
'personalise' => array('tinyint default 0', 'Forward adding the UID?'),
432434
'index_1' => array('urlindex (url(255))', ''),
433435
// "index_2" => array("urlforwardindex (url,forward(255))",""),
434436
// 'unique_1' => array('fwdunique (forward (500))','Forward should be unique'),
435-
'unique_1' => array('urlunique (url(255))', 'URL should be unique'),
437+
'unique_1' => array('urlunique (urlhash)', 'URL should be unique'),
436438
'index_2' => array('uuididx (uuid)', 'sys:index'),
437439
),
438440
'linktrack_userclick' => array(
@@ -468,14 +470,15 @@
468470
// ),
469471
'bounceregex' => array(
470472
'id' => array('integer not null primary key auto_increment', 'ID'),
471-
'regex' => array('text', 'Regex'),
473+
'regex' => array('varchar(2083)', 'Regex'),
474+
'regexhash' => array('char(32)', 'hash value of regex'),
472475
'action' => array('varchar(255)', 'Action on rule'),
473476
'listorder' => array('integer default 0', ''),
474477
'admin' => array('integer', ''),
475478
'comment' => array('text', ''),
476479
'status' => array('varchar(255)', ''),
477480
'count' => array('integer default 0', 'Count of matching bounces on this rule'),
478-
'unique_1' => array('regex (regex(255))', ''),
481+
'unique_1' => array('regex (regexhash)', ''),
479482
),
480483
'bounceregex_bounce' => array(
481484
'regex' => array('integer not null', 'Related regex'),

public_html/lists/admin/upgrade.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,33 @@ function output($message)
118118
'urlcache' => array(
119119
'urlindex' => array('value' => 'url(255)', 'unique' => false),
120120
),
121-
'linktrack' => array(
122-
'urlindex' => array('value' => 'url(255)', 'unique' => false),
123-
'miduidurlindex' => array('value' => 'messageid,userid,url(255)', 'unique' => true),
124-
),
125121
'linktrack_forward' => array(
126122
'urlindex' => array('value' => 'url(255)', 'unique' => false),
127-
'urlunique' => array('value' => 'url(255)', 'unique' => true),
123+
'urlunique' => array('value' => 'urlhash', 'unique' => true),
128124
),
129125
'bounceregex' => array(
130-
'regex' => array('value' => 'regex(255)', 'unique'=> true),
126+
'regex' => array('value' => 'regexhash', 'unique'=> true),
131127
),
132128
);
133129

134130
$tablesToAlter = array(
135131
'urlcache' => array('url'),
136-
'linktrack' => array('url', 'forward'),
137132
'linktrack_forward' => array('url'),
138133
'bounceregex' => array('regex'),
139134
);
140135

136+
//add columns for hash values
137+
138+
Sql_Query("alter table {$GLOBALS['tables']['linktrack_forward']} add urlhash char(32) ");
139+
Sql_Query("alter table {$GLOBALS['tables']['bounceregex']} add regexhash char(32) ");
140+
141+
// add hash values
142+
143+
Sql_Query("update {$GLOBALS['tables']['linktrack_forward']} set urlhash = md5(url) where urlhash is NULL ");
144+
Sql_Query("update {$GLOBALS['tables']['bounceregex']} set regexhash = md5(regex) where regexhash is NULL ");
145+
146+
147+
141148
foreach($indexesToRecreate as $table => $indexes) {
142149

143150

@@ -151,7 +158,7 @@ function output($message)
151158

152159
$alteringOperations = $tablesToAlter[$table];
153160
foreach($alteringOperations as $operation) {
154-
Sql_Query("alter table {$GLOBALS['tables'][$table]} modify $operation text ");
161+
Sql_Query("alter table {$GLOBALS['tables'][$table]} modify $operation varchar(2083) ");
155162
}
156163

157164
foreach($indexes as $indexName => $settings) {

0 commit comments

Comments
 (0)