forked from domingopa/WhatsAPI-Official
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqliteAxolotlStore.php
More file actions
601 lines (484 loc) · 17 KB
/
Copy pathSqliteAxolotlStore.php
File metadata and controls
601 lines (484 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?php
interface axolotlInterface
{
//PreKeys
public function storePreKey($prekeyId, $preKeyRecord);
public function loadPreKey($preKeyId);
public function loadPreKeys();
public function containsPreKey($preKeyId);
public function removePreKey($preKeyId);
public function removeAllPreKeys();
//signedPreKey
public function storeSignedPreKey($signedPreKeyId, $signedPreKeyRecord);
public function loadSignedPreKey($signedPreKeyId);
public function loadSignedPreKeys();
public function removeSignedPreKey($signedPreKeyId);
public function containsSignedPreKey($signedPreKeyId);
//identity
public function storeLocalData($registrationId, $identityKeyPair);
public function getIdentityKeyPair();
public function getLocalRegistrationId();
public function isTrustedIdentity($recipientId, $identityKey);
public function saveIdentity($recipientId, $identityKey);
public function clearRecipient($recipientId);
//session
public function storeSession($recipientId, $deviceId, $sessionRecord);
public function loadSession($recipientId, $deviceId);
public function getSubDeviceSessions($recipientId);
public function containsSession($recipientId, $deviceId);
public function deleteSession($recipientId, $deviceId);
public function deleteAllSessions($recipientId);
//sender_keys
public function storeSenderKey($senderKeyId, $senderKeyRecord);
public function loadSenderKey($senderKeyId);
public function removeSenderKey($senderKeyId);
public function containsSenderKey($senderKeyId);
public function clear();
}
class axolotlSqliteStore implements axolotlInterface
{
const DATA_FOLDER = 'wadata';
private $db;
private $filename;
public function __construct($number, $customPath = null)
{
if ($customPath) {
$this->filename = $customPath.'axolotl-'.$number.'.db';
} else {
$this->filename = __DIR__.DIRECTORY_SEPARATOR.self::DATA_FOLDER.DIRECTORY_SEPARATOR.'axolotl-'.$number.'.db';
}
$this->create();
}
protected function create()
{
$createTable = !file_exists($this->filename);
$this->db = new \PDO('sqlite:'.$this->filename, null, null, [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
if ($createTable) {
//create necesary tables before starting
$this->db->exec('CREATE TABLE IF NOT EXISTS identities
(
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`recipient_id` INTEGER UNIQUE,
`registration_id` INTEGER,
`public_key` BLOB,
`private_key` BLOB,
`next_prekey_id` INTEGER,
`timestamp` INTEGER
);');
$this->db->exec('CREATE TABLE IF NOT EXISTS prekeys
(
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`prekey_id` INTEGER UNIQUE,
`sent_to_server` BOOLEAN,
`record` BLOB
);');
//$this->db->exec('CREATE TABLE IF NOT EXISTS sender_keys
// (`group_id` TEXT, sender_id TEXT, record TEXT)');
$this->db->exec('CREATE TABLE IF NOT EXISTS sessions
(
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`recipient_id` INTEGER UNIQUE,
`device_id` INTEGER,
`record` BLOB,
`timestamp` INTEGER
);');
$this->db->exec('CREATE TABLE IF NOT EXISTS signed_prekeys
(
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`prekey_id` INTEGER UNIQUE,
`timestamp` INTEGER,
`record` BLOB
);');
$this->db->exec('CREATE TABLE IF NOT EXISTS sender_keys
(
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`sender_key_id` TEXT UNIQUE,
`record` BLOB
);');
}
}
//prekeys
public function storePreKey($prekeyId, $record)
{
$sql = 'INSERT INTO prekeys (`prekey_id`, `record`) VALUES (:prekey_id, :record)';
$query = $this->db->prepare($sql);
$query->execute(
[
':prekey_id' => $prekeyId,
':record' => $record->serialize(),
]
);
}
public function storePreKeys($keys)
{
$this->db->beginTransaction();
foreach ($keys as $key) {
$this->storePreKey($key->getId(), $key);
}
$this->db->commit();
}
public function loadPreKey($preKeyId)
{
$sql = 'SELECT `record` FROM prekeys where `prekey_id` = :id';
$query = $this->db->prepare($sql);
$query->execute(
[
':id' => $preKeyId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row == null || $row === false) {
throw new Exception('No such prekey with id: '.$preKeyId);
}
return new PreKeyRecord(null, null, $row['record']);
}
public function loadPreKeys()
{
$sql = 'SELECT `record` FROM prekeys';
$query = $this->db->prepare($sql);
$query->execute();
$prekeys = [];
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
if ($row != null && $row !== false) {
$prekeys[] = $row['record'];
}
}
return $prekeys;
}
public function containsPreKey($preKeyId)
{
$sql = 'SELECT `record` FROM prekeys where `prekey_id` = :id';
$query = $this->db->prepare($sql);
$query->execute(
[
':id' => $preKeyId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row == null || $row === false) {
return false;
}
return true;
}
public function removePreKey($preKeyId)
{
$sql = 'DELETE FROM prekeys WHERE `prekey_id` = :id';
$query = $this->db->prepare($sql);
$query->execute(
[
':id' => $preKeyId,
]
);
}
public function removeAllPreKeys()
{
$sql = 'DELETE FROM prekeys';
$query = $this->db->prepare($sql);
$query->execute();
}
//signedPreKey
public function loadSignedPreKey($signedPreKeyId)
{
$sql = 'SELECT `record` FROM signed_prekeys where `prekey_id` = :id';
$query = $this->db->prepare($sql);
$query->execute(
[
':id' => $signedPreKeyId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row == null || $row === false) {
throw new Exception('No such signedprekey with id: '.$signedPreKeyId);
}
return new SignedPreKeyRecord(null, null, null, null, $row['record']);
}
public function loadSignedPreKeys()
{
$sql = 'SELECT `record` FROM signed_prekeys';
$query = $this->db->prepare($sql);
$query->execute();
$keys = [];
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
if ($row != null && $row !== false) {
$keys[] = new SignedPreKeyRecord(null, null, null, null, $row['record']);
}
}
return $keys;
}
public function storeSignedPreKey($signedPreKeyId, $signedPreKeyRecord)
{
$sql = 'INSERT INTO signed_prekeys (`prekey_id`, `record`) VALUES (:prekey_id, :record)';
$query = $this->db->prepare($sql);
$query->execute(
[
':prekey_id' => $signedPreKeyId,
':record' => $signedPreKeyRecord->serialize(),
]
);
}
public function removeSignedPreKey($signedPreKeyId)
{
$sql = 'DELETE FROM signed_prekeys WHERE `prekey_id` = :id';
$query = $this->db->prepare($sql);
$query->execute(
[
':id' => $signedPreKeyId,
]
);
}
public function containsSignedPreKey($signedPreKeyId)
{
$sql = 'SELECT `record` FROM signed_prekeys where `prekey_id` = :id';
$query = $this->db->prepare($sql);
$query->execute(
[
':id' => $signedPreKeyId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row == null || $row === false) {
return false;
}
return true;
}
//identity
public function getIdentityKeyPair()
{
$sql = 'SELECT `public_key`, `private_key` FROM identities where recipient_id = -1';
$query = $this->db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row != null && $row !== false) {
$keys = new IdentityKeyPair(
new IdentityKey(new DjbECPublicKey(substr($row['public_key'], 1))),
new DjbECPrivateKey($row['private_key'])
);
} else {
//this should not happen
$keys = null;
}
return $keys;
}
public function getLocalRegistrationId()
{
$sql = 'SELECT `registration_id` FROM identities WHERE recipient_id = -1';
$query = $this->db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row != null && $row !== false) {
$localRegistrationId = $row['registration_id'];
} else {
$localRegistrationId = null;
}
return $localRegistrationId;
}
public function storeLocalData($registrationId, $identityKeyPair)
{
$sql = 'INSERT OR REPLACE INTO identities(recipient_id, registration_id, public_key, private_key)
VALUES (:recipient_id, :registration_id, :public_key, :private_key)';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => -1,
':registration_id' => $registrationId,
':public_key' => $identityKeyPair->getPublicKey()->serialize(), //this should be tested, identityKeyPair.getPublicKey().getPublicKey().serialize()
':private_key' => $identityKeyPair->getPrivateKey()->serialize(),
]
);
}
public function clearRecipient($recipientId)
{
$sql = 'DELETE FROM identities where recipient_id = :recipient_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
]
);
$sql = 'DELETE FROM sessions where recipient_id = :recipient_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
]
);
}
public function isTrustedIdentity($recipientId, $identityKey)
{
/*
$sql = 'SELECT public_key from identities WHERE recipient_id = :recipient_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row == null || $row === false) {
return true;
}
return $row['public_key'] == $identityKey->getPublicKey()->serialize();
*/
return true;
}
public function saveIdentity($recipientId, $identityKey)
{
$sql = 'DELETE FROM identities WHERE recipient_id = :recipient_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
]
);
$sql = 'INSERT INTO identities (recipient_id, public_key) VALUES(:recipient_id, :public_key)';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
':public_key' => $identityKey->getPublicKey()->serialize(),
]
);
}
//session
public function loadSession($recipientId, $deviceId)
{
$sql = 'SELECT record FROM sessions WHERE recipient_id = :recipient_id AND device_id = :device_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
':device_id' => $deviceId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row != null && $row !== false) {
$SessionRecord = new SessionRecord(null, $row['record']);
} else {
$SessionRecord = new SessionRecord();
}
return $SessionRecord;
}
public function getSubDeviceSessions($recipientId)
{
$sql = 'SELECT device_id from sessions WHERE recipient_id = :recipient_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
]
);
$deviceIds = [];
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$deviceIds[] = $row['device_id'];
}
return $deviceIds;
}
public function storeSession($recipientId, $deviceId, $sessionRecord)
{
$this->deleteSession($recipientId, 1);
$sql = 'INSERT INTO sessions(recipient_id, device_id, record) VALUES (:recipient_id, :device_id, :record)';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
':device_id' => $deviceId,
':record' => $sessionRecord->serialize(),
]
);
}
public function containsSession($recipientId, $deviceId)
{
$sql = 'SELECT record FROM sessions WHERE recipient_id = :recipient_id AND device_id = :device_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
':device_id' => $deviceId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row == null || $row === false) {
return false;
}
return true;
}
public function deleteSession($recipientId, $deviceId)
{
$sql = 'DELETE FROM sessions WHERE recipient_id = :recipient_id AND device_id = :device_id';
$query = $this->db->prepare($sql);
$query->bindParam(':recipient_id', $recipientId, PDO::PARAM_INT);
$query->bindParam(':device_id', $deviceId, PDO::PARAM_INT);
$query->execute();
}
public function deleteAllSessions($recipientId)
{
$sql = 'DELETE FROM sessions WHERE recipient_id = :recipient_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':recipient_id' => $recipientId,
]
);
}
//sender_keys
public function storeSenderKey($senderKeyId, $senderKeyRecord)
{
$this->removeSenderKey($senderKeyId);
$sql = 'INSERT INTO sender_keys(sender_key_id, record) VALUES (:sender_key_id, :record)';
$query = $this->db->prepare($sql);
$query->execute(
[
':sender_key_id' => $senderKeyId,
':record' => $senderKeyRecord->serialize(),
]
);
}
public function removeSenderKey($senderKeyId)
{
$sql = 'DELETE FROM sender_keys where sender_key_id = :sender_key_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':sender_key_id' => $senderKeyId,
]
);
}
public function loadSenderKey($senderKeyId)
{
$sql = 'SELECT record FROM sender_keys WHERE sender_key_id = :sender_key_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':sender_key_id' => $senderKeyId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
$record = new SenderKeyRecord();
if ($row != null && $row !== false) {
$record = new SenderKeyRecord($row['record']);
}
return $record;
}
public function containsSenderKey($senderKeyId)
{
$sql = 'SELECT record FROM sender_keys WHERE sender_key_id = :sender_key_id';
$query = $this->db->prepare($sql);
$query->execute(
[
':sender_key_id' => $senderKeyId,
]
);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row === null && $row === false) {
return false;
}
return true;
}
public function clear()
{
if (file_exists($this->filename)) {
unlink($this->filename);
}
$this->create();
}
}