engelsystem/includes/model/Message_model.php

68 lines
1.5 KiB
PHP
Raw Normal View History

2014-01-07 15:50:16 +01:00
<?php
use Engelsystem\Database\DB;
/**
* Returns Message id array
2017-01-03 03:22:48 +01:00
*
* @return array
2014-01-07 15:50:16 +01:00
*/
2017-01-02 03:57:23 +01:00
function Message_ids()
{
return DB::select('SELECT `id` FROM `Messages`');
2014-01-07 15:50:16 +01:00
}
/**
* Returns message by id.
*
2017-01-03 03:22:48 +01:00
* @param int $message_id message ID
2018-01-14 23:07:34 +01:00
* @return array|null
2014-01-07 15:50:16 +01:00
*/
2017-01-02 03:57:23 +01:00
function Message($message_id)
{
2018-01-14 23:07:34 +01:00
$message = DB::selectOne('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]);
return empty($message) ? null : $message;
2014-01-07 15:50:16 +01:00
}
/**
* TODO: use validation functions, return new message id
* send message
*
2017-12-25 21:29:00 +01:00
* @param int $receiver_user_id User ID of Receiver
2017-01-03 03:22:48 +01:00
* @param string $text Text of Message
* @return bool
2014-01-07 15:50:16 +01:00
*/
2017-01-02 03:57:23 +01:00
function Message_send($receiver_user_id, $text)
{
2018-10-08 21:15:56 +02:00
$user = Auth()->user();
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($text));
2017-01-21 19:47:44 +01:00
$receiver_user_id = preg_replace('/([^\d]{1,})/ui', '', strip_tags($receiver_user_id));
2017-01-02 15:43:36 +01:00
if (
2017-01-03 14:12:17 +01:00
($text != '' && is_numeric($receiver_user_id))
&& count(DB::select('
2018-10-14 18:24:42 +02:00
SELECT `id`
FROM `users`
WHERE `id` = ?
AND NOT `id` = ?
2017-01-02 15:43:36 +01:00
LIMIT 1
2018-10-08 21:15:56 +02:00
', [$receiver_user_id, $user->id])) > 0
2017-01-02 15:43:36 +01:00
) {
return DB::insert('
INSERT INTO `Messages` (`Datum`, `SUID`, `RUID`, `Text`)
VALUES(?, ?, ?, ?)
',
[
time(),
2018-10-08 21:15:56 +02:00
$user->id,
$receiver_user_id,
$text
]
);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return false;
2014-01-07 15:50:16 +01:00
}