mourne_rcpp_fw/Mourne-CI/application/models/mail_model.php

237 lines
5.9 KiB
PHP
Raw Normal View History

2021-10-30 19:02:07 +02:00
<?php
class Mail_model extends MO_Model
{
public function __construct()
{
parent::__construct();
}
public function save_draft($data, $userid)
{
//values id, userid, friend, name, X, Y, time, subject, body
$sql = "INSERT INTO mail_drafts
2021-10-30 19:02:07 +02:00
VALUES(default, '$userid', ?, ?, ?, ?, '" . time() . "', ?, ?)";
//friend, name, X, Y, subject, body
$this->db->query($sql, array(0, $data['name'], 0, 0, $data['subject'], $data['message']));
}
2021-10-30 19:02:07 +02:00
public function get_drafts($userid)
{
$sql = "SELECT * FROM mail_drafts WHERE userid='$userid'";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return false;
}
2021-10-30 19:02:07 +02:00
return $q->result_array();
}
2021-10-30 19:02:07 +02:00
public function get_draft($id, $userid)
{
$sql = "SELECT * FROM mail_drafts WHERE id = ?";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql, array($id));
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return false;
}
2021-10-30 19:02:07 +02:00
$res = $q->row_array();
2021-10-30 19:02:07 +02:00
if ($res['userid'] != $userid) {
return false;
}
2021-10-30 19:02:07 +02:00
return $res;
}
2021-10-30 19:02:07 +02:00
public function delete_draft($id, $userid)
{
$sql = "DELETE FROM mail_drafts WHERE id = ? AND userid = ?";
2021-10-30 19:02:07 +02:00
$this->db->query($sql, array($id, $userid));
}
2021-10-30 19:02:07 +02:00
public function get_inbox($userid, $new)
{
$sql = "SELECT mails.*,users.username FROM mails
2021-10-30 19:02:07 +02:00
LEFT JOIN users on mails.sender=users.id
WHERE owner='$userid'
ORDER BY time DESC";
$q = $this->db->query($sql);
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return false;
}
2021-10-30 19:02:07 +02:00
$res = $q->result_array();
2021-10-30 19:02:07 +02:00
if (!$new) {
return $res;
}
2021-10-30 19:02:07 +02:00
$found = false;
foreach ($res as $row) {
if ($row['new']) {
$found = true;
break;
}
}
if ($found) {
return $res;
}
$sql = "UPDATE users SET new_mail='0' WHERE id='$userid'";
$this->db->query($sql);
return $res;
2021-10-30 19:02:07 +02:00
}
public function send_message($data, $userid)
{
$sql = "SELECT * FROM users WHERE username = ?";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql, array($data['name']));
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return;
}
2021-10-30 19:02:07 +02:00
$res = $q->row_array();
2021-10-30 19:02:07 +02:00
$data['subject'] = htmlspecialchars($data['subject'], ENT_HTML5, 'UTF-8');
2021-10-30 19:02:07 +02:00
if (strlen($data['subject']) >= 45) {
$data['subject'] = (substr($data['subject'], 0, 45) . '...');
}
2021-10-30 19:02:07 +02:00
//determining line endings
$w = substr_count($data['message'], "\r\n");
2021-10-30 19:02:07 +02:00
if ($w) {
$exp = "\r\n";
} else {
$exp = "\n";
}
2021-10-30 19:02:07 +02:00
$message = explode($exp, $data['message']);
2021-10-30 19:02:07 +02:00
if ($message) {
$d = "";
foreach ($message as $row) {
if (strlen($row) > 70) {
//split into multiple lines
for ($i = 0; $i <= (floor(strlen($row) / 70)); $i++) {
$sub = substr($row, (0 + ($i * 70)), 70);
2021-10-30 19:02:07 +02:00
$d .= $sub;
2021-10-30 19:02:07 +02:00
if (strlen($sub) == 70) {
$d .= "«";
}
2021-10-30 19:02:07 +02:00
$d .= "\r\n";
}
} else {
$d .= $row . "\r\n";
}
}
2021-10-30 19:02:07 +02:00
$data['message'] = $d;
}
2021-10-30 19:02:07 +02:00
$data['message'] = htmlspecialchars($data['message'], ENT_HTML5, 'UTF-8');
2021-10-30 19:02:07 +02:00
$breaks = array("\r\n", "\n");
$text = str_ireplace($breaks, "<br />", $data['message']);
2021-10-30 19:02:07 +02:00
$sql = "INSERT INTO mails VALUES(default,
2021-10-30 19:02:07 +02:00
'" . $res['id'] . "', '$userid', '" . time() . "', ?, ?, '1')";
$this->db->query($sql, array($data['subject'], $text));
2021-10-30 19:02:07 +02:00
$sql = "UPDATE users SET new_mail='1' WHERE id='" . $res['id'] . "'";
2021-10-30 19:02:07 +02:00
$this->db->query($sql);
2021-10-30 19:02:07 +02:00
//saving mail to sent
//id, userid, to_id, to, time, subject, body
$sql = "INSERT INTO mail_sent VALUES(default, ?, ?, ?, " . time(). ", ?, ?)";
2021-10-30 19:02:07 +02:00
$sent = array($userid, $res['id'], $res['username'], $data['subject'], $data['message']);
2021-10-30 19:02:07 +02:00
$this->db->query($sql, $sent);
}
2021-10-30 19:02:07 +02:00
public function get_mail($id, $userid, $edit = false)
{
//querying userid here, so if the user types a random id into the browser bar, it won't return anything
$sql = "SELECT mails.*,users.username FROM mails
2021-10-30 19:02:07 +02:00
LEFT JOIN users ON mails.sender=users.id
WHERE mails.id = ? AND mails.owner = ?";
$q = $this->db->query($sql, array($id, $userid));
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return false;
}
2021-10-30 19:02:07 +02:00
$res = $q->row_array();
2021-10-30 19:02:07 +02:00
if ($res['new']) {
//userid is correct we can query with just the id
$sql = "UPDATE mails SET new='0' WHERE id = ?";
2021-10-30 19:02:07 +02:00
$this->db->query($sql, array($id));
}
2021-10-30 19:02:07 +02:00
if ($edit) {
//just in case
$breaks = array("<br />","<br>","<br/>");
2021-10-30 19:02:07 +02:00
$data['body'] = str_ireplace($breaks, "\r\n", $data['body']);
2021-10-30 19:02:07 +02:00
$data['body'] = htmlspecialchars_decode($data['body'], ENT_HTML5, 'UTF-8');
$data['subject'] = htmlspecialchars_decode($data['subject'], ENT_HTML5, 'UTF-8');
}
2021-10-30 19:02:07 +02:00
return $res;
}
2021-10-30 19:02:07 +02:00
public function get_sent($id, $userid)
{
//querying userid here, so if the user types a random id into the browser bar, it won't return anything
$sql = "SELECT * FROM mail_sent WHERE id = ? AND userid = ?";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql, array($id, $userid));
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return false;
}
2021-10-30 19:02:07 +02:00
return $q->row_array();
}
2021-10-30 19:02:07 +02:00
public function get_all_sent($userid)
{
$sql = "SELECT * FROM mail_sent WHERE userid = ?";
2021-10-30 19:02:07 +02:00
$q = $this->db->query($sql, array($userid));
2021-10-30 19:02:07 +02:00
if (!$q->num_rows()) {
return false;
}
2021-10-30 19:02:07 +02:00
return $q->result_array();
}
2021-10-30 19:02:07 +02:00
}
//nowhitesp