source: branches/1.2/content/plugins/messaging/libs/MessagingFuncs.php @ 1354

Revision 1354, 7.9 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.2] Custom iconv function, plus more SMTP email fixes

Line 
1<?php
2
3class MessagingFuncs
4{
5    protected $to           = '';
6    protected $from         = '';
7    protected $subject      = '';
8    protected $body         = '';
9    protected $errors       = array();
10   
11    /**
12     * Access modifier to set protected properties
13     */
14    public function __set($var, $val)
15    {
16        $this->$var = $val;
17    }
18   
19   
20    /**
21     * Access modifier to get protected properties
22     * The & is necessary (http://bugs.php.net/bug.php?id=39449)
23     */
24    public function &__get($var)
25    {
26        return $this->$var;
27    }
28   
29    /**
30     * Find User - not being used yet!
31     */
32     public function findUser($h, $search = '')
33     {
34        if (strlen($search_term) < 3) {
35            array_push($this->errors, 'too_short');
36            return false;
37        }
38       
39        $h->vars['search_term'] = $search_term; // used to refill the search box after a search
40        $where_clause = " WHERE user_username LIKE %s OR user_email LIKE %s";
41        $sort_clause = ' ORDER BY user_date DESC'; // ordered by newest user first
42        $search_term = '%' . $search_term . '%';
43        $count_sql = "SELECT count(*) AS number FROM " . TABLE_USERS . $where_clause . $sort_clause;
44        $count = $h->db->get_var($h->db->prepare($count_sql, $search_term, $search_term));
45        $sql = "SELECT * FROM " . TABLE_USERS . $where_clause . $sort_clause;
46        $query = $h->db->prepare($sql, $search_term, $search_term);
47        $results = $h->db->get_results($query);
48        if ($results) {
49            return $results;
50        } else {
51            return false;
52        }
53     }
54     
55
56    /**
57     * Get Box Count
58     */
59     public function getBoxCount($h, $box = 'inbox')
60     {
61        if ($box == 'inbox') { $type = "to"; } else { $type = "from"; }
62       
63        $sql = "SELECT count(*) AS number FROM " . DB_PREFIX . "messaging WHERE message_archived = %s AND message_" . $type . " = %d AND message_" . $box . " = %d";
64        $count = $h->db->get_var($h->db->prepare($sql, 'N', $h->currentUser->id, 1));
65       
66        return $count;
67     }
68     
69     
70    /**
71     * Get Box Query
72     */
73     public function getBoxQuery($h, $box = 'inbox')
74     {
75        if ($box == 'inbox') { $type = "to"; } else { $type = "from"; }
76       
77        $sql = "SELECT message_id, message_from, message_to, message_date, message_subject, message_read FROM " . DB_PREFIX . "messaging WHERE message_archived = %s AND message_" . $type . " = %d AND message_" . $box . " = %d ORDER BY message_date DESC";
78        $query = $h->db->prepare($sql, 'N', $h->currentUser->id, 1);
79       
80        return $query;
81     }
82     
83     
84    /**
85     * Mark message as read
86     */
87     public function markRead($h, $message_id = 0)
88     {
89        if (!$message_id) { return false; }
90       
91        $sql = "UPDATE " . DB_PREFIX . "messaging SET message_read = %d WHERE message_id = %d";
92        $h->db->query($h->db->prepare($sql, 1, $message_id));
93     }
94     
95     
96    /**
97     * Get Message
98     */
99     public function getMessage($h, $message_id = 0)
100     {
101        if (!$message_id) { return false; }
102       
103        $sql = "SELECT * FROM " . DB_PREFIX . "messaging WHERE message_id = %d";
104        $message = $h->db->get_row($h->db->prepare($sql, $message_id));
105       
106        if ($message) { return $message; } else { return false; }
107     }
108     
109     
110    /**
111     * Send Message
112     */
113     public function sendMessage($h)
114     {
115        // check for errors
116        if (!$this->to) {       array_push($this->errors, 'no_to');         return false; }
117        if (!$this->subject) {  array_push($this->errors, 'no_subject');    return false; }
118        if (!$this->body) {     array_push($this->errors, 'no_body');       return false; }
119        if ($h->userExists(0, $this->to) == "no") {
120                                array_push($this->errors, 'no_user');       return false; }
121       
122        // if no From field, assume current user
123        if (!$this->from) {
124            $this->from = $h->currentUser->name;
125        }
126       
127        // save to database
128        $this->saveMessage($h);
129       
130        // code here to call sendEmailNotification IF PERMITTED
131        $recipient = new UserAuth();
132        $recipient_id = $h->getUserIdFromName($this->to);
133        $recipient->getUserBasic($h, $recipient_id);
134        $recipient_settings = $recipient->getProfileSettingsData($h, 'user_settings');
135        if ($recipient_settings['pm_notify']) {
136            $this->sendEmailNotification($h);
137        }
138       
139        return true;
140     }
141     
142     
143    /**
144     * Delete Message
145     */
146     public function deleteMessage($h, $message_id = 0, $box = 'inbox')
147     {
148        if (!$message_id) { return false; }
149       
150        $sql = "UPDATE " . DB_PREFIX . "messaging SET message_" . $box . " = %d WHERE message_id = %d";
151        $h->db->query($h->db->prepare($sql, 0, $message_id));
152     }
153     
154     
155    /**
156     * Save to database
157     */
158     private function saveMessage($h)
159     {
160        // we did checks in sendMessage so we know the data is okay,
161        // and this function private in case anyone tries to use it directly
162       
163        // get ids
164        $from_id = $h->getUserIdFromName($this->from);  // get the ID of the sender
165        $to_id = $h->getUserIdFromName($this->to);  // get the ID of the recipient
166       
167        // SQL
168        $sql = "INSERT INTO " . DB_PREFIX . "messaging ";
169        $sql .= "(message_from, message_to, message_date, message_subject, message_content, message_updateby) ";
170        $sql .= "VALUES(%d, %d, CURRENT_TIMESTAMP, %s, %s, %d)";
171       
172        // prepare the query
173        $query = $h->db->prepare($sql, $from_id, $to_id, urlencode($this->subject), urlencode($this->body), $h->currentUser->id);
174       
175        // save to database
176        $h->db->query($query);
177       
178        // get last insert id (appended to link in email notification)
179        $this->id = $h->db->get_var($h->db->prepare("SELECT LAST_INSERT_ID()"));
180     }
181     
182     
183    /**
184     * Send Email Notification of new message
185     */
186     public function sendEmailNotification($h)
187     {
188        $next_line = "\r\n";
189        $skip_line = "\r\n\r\n";
190       
191        $to_id = $h->getUserIdFromName($this->to);  // get the ID of the recipient
192        $to_email = $h->getEmailFromId($to_id);     // get the email address of the recipient
193       
194        $email_subject = $h->lang['messaging_email_subject'];   // email subject (New message from SITE_NAME)
195       
196        // Hi username...
197        $email_message = $h->lang['messaging_email_greeting'] . $this->to . "," . $skip_line;
198       
199        // You've been sent a private message from...
200        $email_message .= $h->lang['messaging_email_message'] . $this->from . $skip_line;
201       
202        // The full content of the message sent
203        $email_message .= '-------' . $next_line;
204        $email_message .= $this->subject . $skip_line;
205        $email_message .= $this->body . $next_line;
206        $email_message .= '-------' . $skip_line;
207       
208        // *** PLEASE DON'T REPLY TO THIS EMAIL ***
209        $email_message .= $h->lang['messaging_email_no_reply'] . $skip_line;
210       
211        // You can reply to the message on " . SITE_NAME . " here:
212        $email_message .= $h->lang['messaging_email_reply_here'] . BASEURL . "index.php?page=compose&reply=" . $this->id . $skip_line;
213       
214        // Thank you,
215        $email_message .= $h->lang['messaging_email_thank_you'] . $next_line;
216       
217        // SITE_NAME Admin
218        $email_message .= $h->lang['messaging_email_site_admin'] . $next_line;
219        $email_message .= BASEURL . $next_line;
220
221        // SEND EMAIL       
222        $h->email($to_email, $email_subject, $email_message);
223     }
224}
225?>
Note: See TracBrowser for help on using the repository browser.