summaryrefslogtreecommitdiff
path: root/includes/model/Message_model.php
blob: a7da63b8fee014b4af30e2197e9890d7936629c6 (plain)
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
<?php

use Engelsystem\Database\DB;

/**
 * Returns Message id array
 *
 * @return array
 */
function Message_ids()
{
    return DB::select('SELECT `id` FROM `Messages`');
}

/**
 * Returns message by id.
 *
 * @param int $message_id message ID
 * @return array|null
 */
function Message($message_id)
{
    $message_source = DB::select('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]);
    if (empty($message_source)) {
        return null;
    }
    return array_shift($message_source);
}

/**
 * TODO: use validation functions, return new message id
 * TODO: global $user con not be used in model!
 * send message
 *
 * @param int    $receiver_user_id User ID of Reciever
 * @param string $text             Text of Message
 * @return bool
 */
function Message_send($receiver_user_id, $text)
{
    global $user;

    $text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($text));
    $receiver_user_id = preg_replace('/([^0-9]{1,})/ui', '', strip_tags($receiver_user_id));

    if (
        ($text != '' && is_numeric($receiver_user_id))
        && count(DB::select('
            SELECT `UID`
            FROM `User`
            WHERE `UID` = ?
            AND NOT `UID` = ?
            LIMIT 1
        ', [$receiver_user_id, $user['UID']])) > 0
    ) {
        return DB::insert('
            INSERT INTO `Messages` (`Datum`, `SUID`, `RUID`, `Text`)
            VALUES(?, ?, ?, ?)
            ',
            [
                time(),
                $user['UID'],
                $receiver_user_id,
                $text
            ]
        );
    }

    return false;
}