ThreemaGateway_Installer_MessagesDb   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 101
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 75 1
A destroy() 0 11 1
1
<?php
2
/**
3
 * Adds/Deletes the message tables.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2015-2016 rugk
8
 * @license MIT
9
 */
10
11
/**
12
 * Methods for creating or deleting the message tables.
13
 */
14
class ThreemaGateway_Installer_MessagesDb
15
{
16
    /**
17
     * @var string database table prefix
18
     */
19
    const DB_TABLE_PREFIX = 'xf_threemagw';
20
21
    /**
22
     * Create a new message tables in the database.
23
     */
24
    public function create()
25
    {
26
        $db = XenForo_Application::get('db');
27
28
        // set charset
29
        $db->query('SET NAMES utf8mb4');
30
31
        // main table
32
        $db->query('CREATE TABLE `' . self::DB_TABLE_PREFIX . '_messages`
33
            (`message_id` CHAR(16),
34
            `message_type_code` INT UNSIGNED COMMENT \'determinates type of message\',
35
            `sender_threema_id` CHAR(8),
36
            `date_send` INT UNSIGNED COMMENT \'the date/time delivered by the Gateway server stored as unix timestamp\',
37
            `date_received` INT UNSIGNED COMMENT \'the date/time when msg was received by this server stored as unix timestamp\',
38
            PRIMARY KEY (`message_id`)
39
            )');
40
        // here "null" is allowed as for deleted messages the message ID is
41
        // still stored, which prevents replay attacks as a message ID cannot
42
        // be reused in this case
43
44
        // files associated with messages
45
        $db->query('CREATE TABLE `' . self::DB_TABLE_PREFIX . '_files`
46
            (`file_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
47
            `message_id` CHAR(16) NOT NULL,
48
            `file_path` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
49
            `file_type` VARCHAR(100) NOT NULL,
50
            `is_saved` BOOLEAN NOT NULL DEFAULT true,
51
            PRIMARY KEY (`file_id`),
52
            FOREIGN KEY (`message_id`) REFERENCES ' . self::DB_TABLE_PREFIX . '_messages(`message_id`)
53
            ) COMMENT=\'Stores files associated with messages.\'');
54
55
        // text messages
56
        $db->query('CREATE TABLE `' . self::DB_TABLE_PREFIX . '_messages_text`
57
            (`message_id` CHAR(16) NOT NULL,
58
            `text` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
59
            PRIMARY KEY (`message_id`),
60
            FOREIGN KEY (`message_id`) REFERENCES ' . self::DB_TABLE_PREFIX . '_messages(`message_id`)
61
            )');
62
63
        // delivery receipt
64
        $db->query('CREATE TABLE `' . self::DB_TABLE_PREFIX . '_messages_delivery_receipt`
65
            (`message_id` CHAR(16) NOT NULL,
66
            `receipt_type` TINYINT UNSIGNED NOT NULL,
67
            PRIMARY KEY (`message_id`),
68
            FOREIGN KEY (`message_id`) REFERENCES ' . self::DB_TABLE_PREFIX . '_messages(`message_id`)
69
            )');
70
71
        // file message
72
        $db->query('CREATE TABLE `' . self::DB_TABLE_PREFIX . '_messages_file`
73
            (`message_id` CHAR(16) NOT NULL,
74
            `file_size` INT UNSIGNED NOT NULL,
75
            `file_name` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
76
            `mime_type` VARCHAR(255) NOT NULL,
77
            PRIMARY KEY (`message_id`),
78
            FOREIGN KEY (`message_id`) REFERENCES ' . self::DB_TABLE_PREFIX . '_messages(`message_id`)
79
            )');
80
81
        // image message
82
        $db->query('CREATE TABLE `' . self::DB_TABLE_PREFIX . '_messages_image`
83
            (`message_id` CHAR(16) NOT NULL,
84
            `file_size` INT UNSIGNED NOT NULL,
85
            PRIMARY KEY (`message_id`),
86
            FOREIGN KEY (`message_id`) REFERENCES ' . self::DB_TABLE_PREFIX . '_messages(`message_id`)
87
            )');
88
89
        // acknowledged messages associated with delivery receipt messages
90
        $db->query('CREATE TABLE `' . self::DB_TABLE_PREFIX . '_ackmsgs`
91
            (`ack_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
92
            `message_id` CHAR(16) NOT NULL COMMENT \'the id of the delivery receipt message, which acknowledges other messages\',
93
            `ack_message_id` CHAR(16) NOT NULL COMMENT \'the id of the message, which has been acknowledged \',
94
            PRIMARY KEY(`ack_id`),
95
            FOREIGN KEY (`message_id`) REFERENCES ' . self::DB_TABLE_PREFIX . '_messages(`message_id`),
96
            INDEX(`ack_message_id`)
97
            ) COMMENT=\'Stores acknowledged message IDs.\'');
98
    }
99
100
    /**
101
     * Deletes all message tables.
102
     */
103
    public function destroy()
104
    {
105
        $db = XenForo_Application::get('db');
106
        $db->query('DROP TABLE `' . self::DB_TABLE_PREFIX . '_ackmsgs`');
107
        $db->query('DROP TABLE `' . self::DB_TABLE_PREFIX . '_messages_file`');
108
        $db->query('DROP TABLE `' . self::DB_TABLE_PREFIX . '_messages_delivery_receipt`');
109
        $db->query('DROP TABLE `' . self::DB_TABLE_PREFIX . '_messages_image`');
110
        $db->query('DROP TABLE `' . self::DB_TABLE_PREFIX . '_messages_text`');
111
        $db->query('DROP TABLE `' . self::DB_TABLE_PREFIX . '_files`');
112
        $db->query('DROP TABLE `' . self::DB_TABLE_PREFIX . '_messages`');
113
    }
114
}
115