Browse code

added CHANGELOG.txt appinfo/info.xml appinfo/signature.json js/showsmstables.js lib/Migration/Version116Date20230508212745.php lib/Migration/Version117Date20230509183517.php lib/Migration/Version118Date20230509194216.php lib/Migration/Version120Date20230520072431.php lib/Migration/Version121Date20230523201743.php lib/Migration/Version123Date20230524182156.php

DoubleBastionAdmin authored on 24/05/2023 14:49:31
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,546 @@
1
+<?php
2
+/**
3
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
+ *
5
+ * @author Double Bastion LLC
6
+ *
7
+ * @license GNU AGPL version 3 or any later version
8
+ *
9
+ * This program is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
+ * License as published by the Free Software Foundation; either
12
+ * version 3 of the License, or any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
+ *
19
+ * You should have received a copy of the GNU Affero General Public
20
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+declare(strict_types=1);
25
+
26
+namespace OCA\SMSRelentless\Migration;
27
+
28
+use Closure;
29
+use OCP\DB\Types;
30
+use OCP\DB\ISchemaWrapper;
31
+use OCP\IDBConnection;
32
+use OCP\Migration\IOutput;
33
+use OCP\Migration\SimpleMigrationStep;
34
+
35
+
36
+class Version123Date20230524182156 extends SimpleMigrationStep {
37
+
38
+        /** @var IDBConnection */
39
+        private $connection;
40
+
41
+        /**
42
+         * @param IDBConnection $connection
43
+         */
44
+        public function __construct(IDBConnection $connection) {
45
+                $this->connection = $connection;
46
+        }
47
+
48
+	/**
49
+	 * @param IOutput $output
50
+	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
51
+	 * @param array $options
52
+	 * @return null|ISchemaWrapper
53
+	 */
54
+	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
55
+		/** @var ISchemaWrapper $schema */
56
+		$schema = $schemaClosure();
57
+
58
+		if (!$schema->hasTable('sms_relent_settings')) {
59
+			$table = $schema->createTable('sms_relent_settings');
60
+			$table->addColumn('id', Types::BIGINT, [
61
+				'autoincrement' => true,
62
+				'notnull' => true,
63
+                                'length' => 11,
64
+                                'unsigned' => true,
65
+			]);
66
+			$table->addColumn('user_id', Types::STRING, [
67
+				'notnull' => true,
68
+				'length' => 128,
69
+			]);
70
+			$table->addColumn('telapi_key', Types::STRING, [
71
+				'notnull' => true,
72
+				'length' => 512,
73
+			]);
74
+			$table->addColumn('tel_pub_key', Types::STRING, [
75
+				'notnull' => true,
76
+				'length' => 512,
77
+			]);
78
+			$table->addColumn('telapi_url_rec', Types::STRING, [
79
+				'notnull' => true,
80
+				'length' => 512,
81
+			]);
82
+			$table->addColumn('telapi_url', Types::STRING, [
83
+				'notnull' => true,
84
+				'length' => 512,
85
+			]);
86
+			$table->addColumn('messaging_profile_id', Types::STRING, [
87
+				'notnull' => true,
88
+				'length' => 512,
89
+			]);
90
+			$table->addColumn('nexapi_key', Types::STRING, [
91
+				'notnull' => true,
92
+				'length' => 512,
93
+			]);
94
+			$table->addColumn('nexapi_secret', Types::STRING, [
95
+				'notnull' => true,
96
+				'length' => 512,
97
+			]);
98
+			$table->addColumn('nexapi_url_rec', Types::STRING, [
99
+				'notnull' => true,
100
+				'length' => 512,
101
+			]);
102
+			$table->addColumn('nexapi_url', Types::STRING, [
103
+				'notnull' => true,
104
+				'length' => 512,
105
+			]);
106
+			$table->addColumn('twilapi_key', Types::STRING, [
107
+				'notnull' => true,
108
+				'length' => 512,
109
+			]);
110
+			$table->addColumn('twilapi_secret', Types::STRING, [
111
+				'notnull' => true,
112
+				'length' => 512,
113
+			]);
114
+			$table->addColumn('twilapi_url_rec', Types::STRING, [
115
+				'notnull' => true,
116
+				'length' => 512,
117
+			]);
118
+			$table->addColumn('twilapi_url', Types::STRING, [
119
+				'notnull' => true,
120
+				'length' => 512,
121
+			]);
122
+			$table->addColumn('flowapi_key', Types::STRING, [
123
+				'notnull' => true,
124
+				'length' => 512,
125
+			]);
126
+			$table->addColumn('flowapi_secret', Types::STRING, [
127
+				'notnull' => true,
128
+				'length' => 512,
129
+			]);
130
+			$table->addColumn('flowapi_url_rec', Types::STRING, [
131
+				'notnull' => true,
132
+				'length' => 512,
133
+			]);
134
+			$table->addColumn('flowapi_url', Types::STRING, [
135
+				'notnull' => true,
136
+				'length' => 512,
137
+			]);
138
+			$table->addColumn('tel_sender_name', Types::STRING, [
139
+				'notnull' => false,
140
+				'length' => 48,
141
+				'default' => '',
142
+			]);
143
+			$table->addColumn('nex_sender_name', Types::STRING, [
144
+				'notnull' => false,
145
+				'length' => 48,
146
+				'default' => '',
147
+			]);
148
+			$table->addColumn('twil_sender_name', Types::STRING, [
149
+				'notnull' => false,
150
+				'length' => 48,
151
+				'default' => '',
152
+			]);
153
+			$table->addColumn('flow_sender_name', Types::STRING, [
154
+				'notnull' => false,
155
+				'length' => 48,
156
+				'default' => '',
157
+			]);
158
+			$table->addColumn('messagesperpage', Types::INTEGER, [
159
+				'notnull' => false,
160
+				'length' => 11,
161
+                                'unsigned' => true,
162
+			]);
163
+			$table->addColumn('get_notify', Types::SMALLINT, [
164
+				'notnull' => false,
165
+				'length' => 1,
166
+			]);
167
+			$table->addColumn('notification_email', Types::STRING, [
168
+				'notnull' => false,
169
+				'length' => 512,
170
+				'default' => '',
171
+			]);
172
+			$table->addColumn('getsmsinemail', Types::SMALLINT, [
173
+				'notnull' => false,
174
+				'length' => 1,
175
+			]);
176
+			$table->addColumn('show_all_messages', Types::SMALLINT, [
177
+				'notnull' => false,
178
+				'length' => 1,
179
+			]);
180
+			$table->addColumn('show_display_names', Types::SMALLINT, [
181
+				'notnull' => false,
182
+				'length' => 1,
183
+			]);
184
+                        $table->setPrimaryKey(['id']);
185
+                        $table->addUniqueIndex(['id']);
186
+
187
+		}
188
+
189
+
190
+		if (!$schema->hasTable('sms_relent_received')) {
191
+			$table = $schema->createTable('sms_relent_received');
192
+			$table->addColumn('id', Types::BIGINT, [
193
+				'autoincrement' => true,
194
+				'notnull' => true,
195
+                                'length' => 11,
196
+                                'unsigned' => true,
197
+			]);
198
+			$table->addColumn('user_id', Types::STRING, [
199
+				'notnull' => true,
200
+				'length' => 64,
201
+			]);
202
+			$table->addColumn('message_id', Types::STRING, [
203
+				'notnull' => true,
204
+				'length' => 512,
205
+			]);
206
+			$table->addColumn('date', Types::DATETIME, [
207
+				'notnull' => true,
208
+			]);
209
+			$table->addColumn('from', Types::STRING, [
210
+				'notnull' => true,
211
+				'length' => 128,
212
+			]);
213
+			$table->addColumn('to', Types::STRING, [
214
+				'notnull' => true,
215
+				'length' => 128,
216
+			]);
217
+			$table->addColumn('message', Types::TEXT, [
218
+				'notnull' => false,
219
+				'default' => '',
220
+			]);
221
+			$table->addColumn('author_displayname', Types::STRING, [
222
+				'notnull' => true,
223
+				'length' => 255,
224
+			]);
225
+			$table->addColumn('internal_sender', Types::SMALLINT, [
226
+				'notnull' => false,
227
+				'length' => 1,
228
+			]);
229
+                        $table->setPrimaryKey(['id']);
230
+                        $table->addUniqueIndex(['id']);
231
+
232
+		}
233
+
234
+
235
+		if (!$schema->hasTable('sms_relent_sent')) {
236
+			$table = $schema->createTable('sms_relent_sent');
237
+			$table->addColumn('id', Types::BIGINT, [
238
+				'autoincrement' => true,
239
+				'notnull' => true,
240
+                                'length' => 11,
241
+                                'unsigned' => true,
242
+			]);
243
+			$table->addColumn('user_id', Types::STRING, [
244
+				'notnull' => true,
245
+				'length' => 64,
246
+			]);
247
+			$table->addColumn('message_id', Types::STRING, [
248
+				'notnull' => true,
249
+				'length' => 512,
250
+			]);
251
+			$table->addColumn('date', Types::DATETIME, [
252
+				'notnull' => true,
253
+			]);
254
+			$table->addColumn('from', Types::STRING, [
255
+				'notnull' => true,
256
+				'length' => 128,
257
+			]);
258
+			$table->addColumn('to', Types::STRING, [
259
+				'notnull' => true,
260
+				'length' => 128,
261
+			]);
262
+			$table->addColumn('network ', Types::STRING, [
263
+				'notnull' => false,
264
+				'length' => 64,
265
+				'default' => '',
266
+			]);
267
+			$table->addColumn('price', Types::STRING, [
268
+				'notnull' => false,
269
+				'length' => 64,
270
+				'default' => '',
271
+			]);
272
+			$table->addColumn('status', Types::STRING, [
273
+				'notnull' => false,
274
+				'length' => 512,
275
+				'default' => '',
276
+			]);
277
+			$table->addColumn('deliveryreceipt', Types::STRING, [
278
+				'notnull' => false,
279
+				'length' => 64,
280
+				'default' => '',
281
+			]);
282
+			$table->addColumn('message', Types::TEXT, [
283
+				'notnull' => false,
284
+                                'default' => '',
285
+			]);
286
+			$table->addColumn('author_displayname', Types::STRING, [
287
+				'notnull' => true,
288
+				'length' => 255,
289
+			]);
290
+                        $table->setPrimaryKey(['id']);
291
+                        $table->addUniqueIndex(['id']);
292
+
293
+		}
294
+
295
+
296
+		if (!$schema->hasTable('sms_relent_autorply')) {
297
+			$table = $schema->createTable('sms_relent_autorply');
298
+			$table->addColumn('id', Types::BIGINT, [
299
+				'autoincrement' => true,
300
+				'notnull' => true,
301
+                                'length' => 11,
302
+                                'unsigned' => true,
303
+			]);
304
+			$table->addColumn('user_id', Types::STRING, [
305
+				'notnull' => true,
306
+				'length' => 128,
307
+			]);
308
+			$table->addColumn('saved_by_dsplname', Types::STRING, [
309
+				'notnull' => true,
310
+				'length' => 255,
311
+			]);
312
+			$table->addColumn('phone_number', Types::STRING, [
313
+				'notnull' => true,
314
+				'length' => 128,
315
+			]);
316
+			$table->addColumn('days_of_week', Types::STRING, [
317
+				'notnull' => false,
318
+				'length' => 64,
319
+                                'default' => '',
320
+			]);
321
+			$table->addColumn('daily_start', Types::STRING, [
322
+				'notnull' => false,
323
+				'length' => 8,
324
+                                'default' => '',
325
+			]);
326
+			$table->addColumn('daily_end', Types::STRING, [
327
+				'notnull' => false,
328
+				'length' => 8,
329
+                                'default' => '',
330
+			]);
331
+			$table->addColumn('vacation_start', Types::DATETIME, [
332
+				'notnull' => false,
333
+			]);
334
+			$table->addColumn('vacation_end', Types::DATETIME, [
335
+				'notnull' => false,
336
+			]);
337
+			$table->addColumn('message_text', Types::TEXT, [
338
+				'notnull' => false,
339
+                                'default' => '',
340
+			]);
341
+                        $table->setPrimaryKey(['id']);
342
+                        $table->addUniqueIndex(['id']);
343
+
344
+		}
345
+
346
+
347
+		if (!$schema->hasTable('sms_relent_restrict')) {
348
+			$table = $schema->createTable('sms_relent_restrict');
349
+			$table->addColumn('id', Types::BIGINT, [
350
+				'autoincrement' => true,
351
+				'notnull' => true,
352
+                                'length' => 11,
353
+                                'unsigned' => true,
354
+			]);
355
+			$table->addColumn('user_id', Types::STRING, [
356
+				'notnull' => true,
357
+				'length' => 128,
358
+			]);
359
+			$table->addColumn('saved_by_dsplname', Types::STRING, [
360
+				'notnull' => true,
361
+				'length' => 255,
362
+			]);
363
+			$table->addColumn('phone_number', Types::STRING, [
364
+				'notnull' => true,
365
+				'length' => 128,
366
+			]);
367
+			$table->addColumn('groups', Types::STRING, [
368
+				'notnull' => true,
369
+				'length' => 2048,
370
+			]);
371
+			$table->addColumn('users', Types::STRING, [
372
+				'notnull' => true,
373
+				'length' => 2048,
374
+			]);
375
+                        $table->setPrimaryKey(['id']);
376
+                        $table->addUniqueIndex(['id']);
377
+
378
+		}
379
+
380
+		if (!$schema->hasTable('sms_relent_subac')) {
381
+			$table = $schema->createTable('sms_relent_subac');
382
+			$table->addColumn('id', Types::BIGINT, [
383
+				'autoincrement' => true,
384
+				'notnull' => true,
385
+                                'length' => 11,
386
+                                'unsigned' => true,
387
+			]);
388
+			$table->addColumn('user_id', Types::STRING, [
389
+				'notnull' => true,
390
+				'length' => 128,
391
+			]);
392
+			$table->addColumn('tnx_groups_allowed', Types::TEXT, [
393
+				'notnull' => false,
394
+                                'default' => '',
395
+			]);
396
+			$table->addColumn('tnx_users_allowed', Types::TEXT, [
397
+				'notnull' => false,
398
+                                'default' => '',
399
+			]);
400
+			$table->addColumn('plv_groups_allowed', Types::TEXT, [
401
+				'notnull' => false,
402
+                                'default' => '',
403
+			]);
404
+			$table->addColumn('plv_users_allowed', Types::TEXT, [
405
+				'notnull' => false,
406
+                                'default' => '',
407
+			]);
408
+			$table->addColumn('twl_groups_allowed', Types::TEXT, [
409
+				'notnull' => false,
410
+                                'default' => '',
411
+			]);
412
+			$table->addColumn('twl_users_allowed', Types::TEXT, [
413
+				'notnull' => false,
414
+                                'default' => '',
415
+			]);
416
+			$table->addColumn('flr_groups_allowed', Types::TEXT, [
417
+				'notnull' => false,
418
+                                'default' => '',
419
+			]);
420
+			$table->addColumn('flr_users_allowed', Types::TEXT, [
421
+				'notnull' => false,
422
+                                'default' => '',
423
+			]);
424
+                        $table->setPrimaryKey(['id']);
425
+                        $table->addUniqueIndex(['id']);
426
+		}
427
+
428
+
429
+		return $schema;
430
+	}
431
+
432
+        public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
433
+
434
+
435
+                // Enter the Nextcloud display name in the 'author_displayname' column of the 'sms_relent_sent' table, for messages that were sent before the upgrade
436
+                $getrecfromnb = $this->connection->prepare('SELECT `id`, `user_id` FROM `*PREFIX*sms_relent_sent`');
437
+	        $getrecfromnbres = $getrecfromnb->execute();
438
+
439
+                $recdatauids = [];
440
+                $idsent = [];
441
+                while ($rowfetched = $getrecfromnbres->fetch()) {
442
+                       $recdatauids[] = $rowfetched['user_id'];
443
+                       $idsent[] = $rowfetched['id'];
444
+                }
445
+                $getrecfromnbres->closeCursor();
446
+
447
+                if ($recdatauids) {
448
+
449
+                    $recuids = array_values(array_unique($recdatauids));
450
+                    $acdatausrdn = [];
451
+
452
+                    foreach ($recuids as $dnkey => $dnvalue) {
453
+		             $getacdatadn = $this->connection->prepare('SELECT `uid`, `name`, `value` FROM `*PREFIX*accounts_data` WHERE `uid` = ? AND `name` = ?');
454
+			     $getacdatadnres = $getacdatadn->execute([$dnvalue, 'displayname']);
455
+
456
+			     while ($acusrdnfetched = $getacdatadnres->fetch()) {
457
+				    $acdatausrdn[$dnvalue] = $acusrdnfetched['value'];
458
+			     }
459
+			     $getacdatadnres->closeCursor();
460
+                    }
461
+
462
+                    foreach ($recdatauids as $pdpkey => $pdpvalue) {
463
+
464
+			     $updaterecms = $this->connection->prepare('UPDATE `*PREFIX*sms_relent_sent` SET `author_displayname` = ? WHERE `user_id` = ? AND `id` = ?');
465
+			     $updaterecmsres = $updaterecms->execute([$acdatausrdn[$pdpvalue], $pdpvalue, $idsent[$pdpkey]]);
466
+			     $updaterecmsres->closeCursor();
467
+                    }
468
+                }
469
+
470
+
471
+                // Enter the Nextcloud display name in the 'author_displayname' column of the 'sms_relent_received' table, for messages that were received before the upgrade
472
+                $getrecfromnb = $this->connection->prepare('SELECT `id`, `date`, `from` FROM `*PREFIX*sms_relent_received`');
473
+	        $getrecfromnbres = $getrecfromnb->execute();
474
+
475
+                $recdatanmbrs = [];
476
+                $idsrc = [];
477
+                $daterec = [];
478
+                while ($rowfetched = $getrecfromnbres->fetch()) {
479
+                       $recdatanmbrs[] = $rowfetched['from'];
480
+                       $idsrc[] = $rowfetched['id'];
481
+                       $daterecarr = explode(":", $rowfetched['date']);
482
+                       $daterec[] = $daterecarr[0];
483
+                }
484
+                $getrecfromnbres->closeCursor();
485
+
486
+                if ($recdatanmbrs) {
487
+
488
+                    foreach ($recdatanmbrs as $rckey => $rcvalue) {
489
+
490
+			     // Search for the associated Display Name in the previous messages sent from the same phone number, in the 'sms_relent_sent' table
491
+			     $recmsgfromprc = '%' . $rcvalue;
492
+                             $recdataprc = $daterec[$rckey] . '%';
493
+			     $getdspnmst = $this->connection->prepare('SELECT `date`, `from`, `author_displayname` FROM `*PREFIX*sms_relent_sent` WHERE `from` LIKE ? AND `date` LIKE ?');
494
+			     $getdspnmstres = $getdspnmst->execute([$recmsgfromprc, $recdataprc]);
495
+
496
+			     $dispnamearr = [];
497
+			     while ($dspnmstfetched = $getdspnmstres->fetch()) {
498
+				    $dispnamearr[] = $dspnmstfetched['author_displayname'];
499
+			     }
500
+			     $getdspnmstres->closeCursor();
501
+
502
+			     if ($dispnamearr) {
503
+				 $descdspnmarr = array_reverse($dispnamearr);
504
+				 $authorDisplayNm = $descdspnmarr[0];
505
+				 $internalSender = 1;
506
+
507
+			     } else {
508
+
509
+                                 $authorDisplayNm = '';
510
+
511
+			         $getfrom = $this->connection->prepare('SELECT `from` FROM `*PREFIX*sms_relent_sent` WHERE `from` LIKE ? LIMIT 1');
512
+			         $getfromres = $getfrom->execute([$recmsgfromprc]);
513
+                                 $fromfetched = $getfromres->fetch();
514
+
515
+                                 if ($fromfetched) { $internalSender = 1; } else { $internalSender = 0; }
516
+
517
+
518
+				 // Search for the associated Display Name in the previous messages coming from the same phone number, in the 'sms_relent_received' table
519
+				 $getdspnm = $this->connection->prepare('SELECT `from`, `author_displayname` FROM `*PREFIX*sms_relent_received` WHERE `from` = ?');
520
+				 $getdspnmres = $getdspnm->execute([$rcvalue]);
521
+
522
+				 $dspnmearr = [];
523
+				 while ($dspnmfetched = $getdspnmres->fetch()) {
524
+				        if ($dspnmfetched['author_displayname']) {
525
+				            $dspnmearr[] = $dspnmfetched['author_displayname'];
526
+				        }
527
+				 }
528
+				 $getdspnmres->closeCursor();
529
+
530
+				 if ($dspnmearr) {
531
+				     $descdisplaynmarr = array_reverse($dspnmearr);
532
+				     $authorDisplayNm = $descdisplaynmarr[0];
533
+				 } else { $authorDisplayNm = ''; }
534
+			     }
535
+
536
+			     $updaterecms = $this->connection->prepare('UPDATE `*PREFIX*sms_relent_received` SET `author_displayname` = ?, `internal_sender` = ?
537
+                                                                        WHERE `from` LIKE ? AND `id` = ?');
538
+			     $updaterecmsres = $updaterecms->execute([$authorDisplayNm, $internalSender, $recmsgfromprc, $idsrc[$rckey]]);
539
+			     $updaterecmsres->closeCursor();
540
+                    }
541
+                }
542
+
543
+                return null;
544
+        }
545
+
546
+}