Odyssey
msgECO.i
1 <?php
2 
3 /* Include file for secure messages. This has all the entry points for gathering the necessary data for display,
4  * or to perform any operations.
5  *
6  * Functions will return a structure in this format:
7  * [status][response] = true/false
8  * [code] = "000" or error
9  * [message] = "success" or error message
10  * [data] = array
11  */
12 define( "SECURE_MESSAGES_ERROR_CODE", "5020" );
13 
14 /**
15  * function msgCheckForMessages($pDbh, $pHBEnv)
16  * Check to see if any unread messages exist. Return a boolean reply.
17  * deprecated -- This function is not currently used in the desktop version which adds the unread attribute inside of the read function.
18  * 11/14/2017 MH Not deprecated -- the app still uses it.
19  *
20  * @param $pDbh -- the database connection
21  * @param $pHBEnv -- the environment variables
22  *
23 * Environmental variables needed:


24  * $pHBEnv["Cu"]

25  * $pHBEnv["Uid"]

26  *
27  * @return array
28  * $status -- array of the statuses
29  * $response -- "true" if there is a response, false otherwise.
30  * $code -- "000" if successful, SECURE_MESSAGES_ERROR_CODE otherwise.
31  * $message -- "success" if successful, error message if not.
32  * $data -- empty array if error, array if not.
33  * $message_count -- number of messages.
34  * $has_messages -- if there are messages.
35  */
36 function msgCheckForMessages($pDbh, $pHBEnv) {
37  $Cu = $pHBEnv["Cu"];
38  $Uid = $pHBEnv["Uid"];
39  if ( empty($Cu) || empty($Uid) ) {
40  throw new exception('Missing Parameters', 1);
41  }
42 
43  try {
44  $sql = "select count(*) from cuadmeco where cu = '$Cu'
45  and user_id = '$Uid'
46  and origination = 0 and unread;";
47  $rh = db_query($sql, $pDbh);
48 
49  if ($rh) {
50  list( $msgct ) = db_fetch_array($rh, 0);
51  }
52 
53  $msgct = ($msgct ? $msgct : 0);
54  db_free_result($rh);
55 
56  // always return success
57  $return = array("code" => "000", "homecuData" => array( "message_count" => $msgct, "has_messages" => ($msgct > 0) ? "1" : "0" ));
58 
59  } catch (Exception $e) {
60  $return = array("homecuErrors" => $e->getMessage(), "code" => SECURE_MESSAGES_ERROR_CODE);
61  }
62 
63  return $return;
64 }
65 
66 /**
67  * function msgReadMessages($dbh, $pHBEnv)
68  * Read the messages. Just get the thread topic, the thread id, and flag whether there are unread messages.
69  * Order the threads by most recent tread date, descending.
70  *
71  * @param $dbh -- the database connection
72  * @param $pHBEnv -- the environment variables
73  *
74  * Environmental variables needed:


75  * $pHBEnv["Cu"]

76  * $pHBEnv["Uid"]

77  * $pHBEnv["HCUPOST"]["what"] — The subject filter. This can be empty or undefined.
78  *
79  * @return array
80  * $code -- "000" if successful, SECURE_MESSAGES_ERROR_CODE otherwise.
81  * $homecuInfo -- doesn't exist but could. (If it did, it would show a message.)
82  * $homecuErrors -- Doesn't exist if successful, otherwise is a string with the error message.
83  * $homecuData -- Doesn't exist if not successful, otherwise is an array of results for the grid.
84  */
85 function msgReadMessages($dbh, $pHBEnv) {
86 
87  try
88  {
89  $Cu= $pHBEnv["Cu"];
90  $Uid= $pHBEnv["Uid"];
91  if ( empty($Cu) || empty($Uid) ) {
92  throw new exception('Missing Parameters', 1);
93  }
94  $tz= GetCreditUnionTimezone($dbh, $Cu); // Returns the default timezone of mountain if not found or SQL error. Therefore, this doesn't need to be trapped.
95  $localzone="set time zone 'US/Mountain';";
96  $what= strtolower(HCU_array_key_value('what',$pHBEnv["HCUPOST"]));
97  $value= str_replace("_", "^_", str_replace("%", "^%", str_replace("^", "^^", $what))); // % and _ in the string are intrepreted as literals NOT wildcards.
98  $whereEnd= "like '%$value%' escape '^'"; // Use something else than the default backslash because that might be needed to escaped multiple times.
99 
100  $where= trim($what) == "" ? "" : " and lower(subject) $whereEnd";
101 
102  $sql = "set time zone '$tz'; ";
103  $sql .= "select messageid, parentid, to_char(date,'MM/DD/YYYY HH24:MI') as date, unread, memdeleted, admdeleted, origination, subject
104  from cuadmeco a where cu = '$Cu' and user_id = '$Uid' and not memdeleted $where order by messageid; ";
105 
106  $sth = db_query($sql,$dbh);
107 
108  if (!$sth)
109  throw new exception("Read query failed.", 1);
110 
111  // set timezone back to default
112  $sth1 = db_query($localzone,$dbh);
113 
114  if (!$sth1)
115  throw new exception("Timezone query failed.", 2);
116 
117  $aryMessages = array();
118  for ($i= 0; $msgRow = db_fetch_array($sth, $i); $i++) {
119  // flag for if the meesage is unread and from the CU
120  $new = ($msgRow['unread'] == 't' && $msgRow['origination'] == 0) ? 1 : 0;
121 
122  // parentid is the thread id
123  $p = $msgRow['parentid'];
124 
125  // messageid is used to order the record
126  $m = $msgRow['messageid'];
127 
128  // origination: 0 = admin, 1 = user
129  $o = $msgRow["origination"];
130 
131  // convert any characters that got in from being copied from an editor
132  $subject = convertMicrosoftCharacters( $msgRow["subject"] );
133 
134  // Was On2 but now is closer to On.
135  if (!isset($aryMessages[$p]))
136  $aryMessages[$p] = array( "threadId"=>$p, "order" => $m, "unread"=>$new, "origination"=>$o, "subject"=>$subject, "date"=>$msgRow["date"] );
137  else {
138  // only keep unique topics so just update the existing record
139  $aryMessages[$p]["order"] = $m;
140  // if the parent id == the message id, overwrite the subject since we found the correct initial message
141  if ( $p == $m ) {
142  $aryMessages[$p]["subject"] = $subject;
143  }
144  // only update the unread if it is a new message
145  if ( $new == 1 ) {
146  $aryMessages[$p]["unread"] = $new;
147  }
148  // keep the latest origination
149  $aryMessages[$p]["origination"] = $o;
150 
151  $aryMessages[$p]["date"] = $msgRow["date"];
152  }
153  }
154 
155  // now sort the messages
156  usort( $aryMessages, "SortMessages" );
157 
158  return array("code" => "000", "homecuData" => $aryMessages);
159  } // End try
160  catch (exception $e)
161  {
162  return array("homecuErrors" => $e->getMessage(), "code" => SECURE_MESSAGES_ERROR_CODE);
163  }
164 }
165 
166 /**
167  * function msgReadMessageThread($dbh, $pHBEnv)
168  * Read all the messages in a given topic/thread.
169  *
170  * @param $dbh -- the database connection
171  * @param $pHBEnv -- the environment variables
172  *
173  * Environmental variables needed:


174  * $pHBEnv["Cu"]

175  * $pHBEnv["Uid"]

176  * $pHBEnv["HCUPOST"]["parentid"]
177  *
178  * @return array
179  * $code -- "000" if successful, SECURE_MESSAGES_ERROR_CODE otherwise.
180  * $homecuInfo -- doesn't exist but could. (If it did, it would show a message.)
181  * $homecuErrors -- Doesn't exist if successful, otherwise is a string with the error message.
182  * $homecuData -- Doesn't exist if not successful, otherwise is an array of results for the thread.
183  */
184 function msgReadMessageThread($dbh, $pHBEnv) {
185  try
186  {
187  $parentId = $pHBEnv["HCUPOST"]["parentid"]; // thread id
188  $Cu= $pHBEnv["Cu"];
189  $Uid= $pHBEnv["Uid"];
190  if ( empty($Cu) || empty($Uid) || empty($parentId) ) {
191  throw new exception('Missing Parameters', 1);
192  }
193 
194  $tz= GetCreditUnionTimezone($dbh, $Cu);
195  $localzone="set time zone 'US/Mountain';";
196  $sqlunread = "update cuadmeco set unread=FALSE where cu = '$Cu' and parentid = $parentId and user_id = '$Uid' and origination = 0";
197  $sql = "set time zone '$tz';
198  select messageid, parentid, to_char(date,'MM/DD/YYYY HH24:MI TZ') as date, unread, origination, subject, messagetext from cuadmeco
199  where cu = '$Cu' and parentid = $parentId and user_id = '$Uid' and not memdeleted order by messageid; ";
200  $sth = db_query($sql,$dbh);
201 
202  if (!$sth)
203  throw new exception("Read query failed.", 1);
204 
205  $sth1 = db_query($localzone,$dbh);
206 
207  if (!$sth1)
208  throw new exception("Localzone query failed.", 2);
209 
210  $aryResult= array();
211  for($i= 0; $msgrow = db_fetch_array($sth, $i); $i++ )
212  {
213  // convert any characters that got in from being copied from an editor
214  $messageText = convertMicrosoftCharacters( $msgrow['messagetext'] );
215 
216  $aryResult[] = array( "date"=>$msgrow['date'], "origination"=>$msgrow['origination'], "subject" => $msgrow['subject'], "message"=>$messageText );
217  }
218  $sth = db_query($sqlunread,$dbh);
219 
220  if (!$sth)
221  throw new exception("Update query failed.", 3);
222 
223  return array("code" => "000", "homecuData" => $aryResult);
224  }
225  catch(exception $e)
226  {
227  return array("homecuErrors" => $e->getMessage(), "code" => SECURE_MESSAGES_ERROR_CODE);
228  }
229 }
230 
231 /**
232  * function msgSendMessage($dbh, $pHBEnv, $pMC)
233  * Send the given message. If an admin email exists then send an email to notify the admin user a message is present.
234  *
235  * @param $dbh -- the database connection
236  * @param $pHBEnv -- the environment variables
237  * @param $pMC -- the dictionary
238  *
239  * Environmental variables needed:


240  * $pHBEnv["Cu"]

241  * $pHBEnv["Uid"]

242  * $pHBEnv["HCUPOST"]["subject"]
243  * $pHBEnv["HCUPOST"]["message"]
244  * $pHBEnv["HCUPOST"]["parentid"]
245  * $pHBEnv["HCUPOST"]["what"] — The subject filter. This can be empty or undefined (for msgReadMessages).
246  *
247  * @return array
248  * $code -- "000" if successful, SECURE_MESSAGES_ERROR_CODE otherwise.
249  * $homecuInfo -- doesn't exist but could. (If it did, it would show a message.)
250  * $homecuErrors -- Doesn't exist if successful, otherwise is a string with the error message.
251  * $homecuData -- Doesn't exist if not successful, otherwise is an array of results for the grid.
252  */
253 function msgSendMessage($dbh, $pHBEnv, $pMC) {
254  try
255  {
256  $subject = $pHBEnv["HCUPOST"]["subject"]; // message subject
257  $message = $pHBEnv["HCUPOST"]["message"]; // message itself
258  $parentId = $pHBEnv["HCUPOST"]["parentid"]; // thread id
259 
260  $Cu= $pHBEnv["Cu"];
261  $Uid= $pHBEnv["Uid"];
262  if ( empty($Cu) || empty($Uid) ) {
263  throw new exception('Missing Parameters', 1);
264  }
265 
266  $subject = prep_save(hcu_displayHtml( preg_replace( "/[\`\;]/", "", $subject ), ENT_NOQUOTES ) );
267  if ( trim( $subject ) == '' )
268  throw new exception($pMC->msg('Message subject cannot be blank', HCU_DISPLAY_AS_RAW), 1);
269 
270  $messageText = prep_save(hcu_displayHtml( preg_replace( "/[\`\;]/", "", $message ), ENT_NOQUOTES ) );
271  if ( trim( $messageText ) == '' )
272  throw new exception($pMC->msg('Message body cannot be blank', HCU_DISPLAY_AS_RAW), 2);
273 
274  $sql = "insert into cuadmeco (parentid, cu, user_id, date, unread, memdeleted, admdeleted, origination, subject, messagetext)
275  values ( " . ($parentId ? $parentId : "currval('cuadmeco_messageid_seq')") . ", '$Cu', $Uid, CURRENT_TIMESTAMP, TRUE, FALSE, FALSE, 1, '$subject','$messageText');";
276  $sth = db_query($sql,$dbh);
277 
278  if (!$sth)
279  throw new exception("Insert query failed.", 3);
280 
281  // ** MWS 9/8/2006 -- Check for an email account in the admnotify table -- if there is then I want to send an email to the credit union disclosing that a message was sent
282  $sql = "select email from cuadmnotify where cu = '$Cu' and role = 'securenotify' ";
283  $emRS = db_query($sql, $dbh);
284 
285  if (!$emRS)
286  throw new exception("Email query failed.", 4);
287 
288  if ($emRow = db_fetch_array($emRS)) {
289  $secEmail = trim($emRow['email']);
290  if ($secEmail != '') {
291 
292  // * Send Email Notification
293  $emMsg = "\t $Cu Secure Message\n\nYou have received a secure message from a member.\nPlease log in to your admin to view the message.\n\n";
294  $emSubject = "New Secure Message";
295 
296  $notify = new ErrorMail;
297 
298  $notify->mailto = $secEmail; // Set to transfer email setup in CU Admin
299  $notify->replyto = "noreply@homecu.net";
300  $notify->subject = $emSubject;
301  $notify->msgbody = $emMsg;
302  $notify->callingfunction = __FUNCTION__;
303  $notify->file = __FILE__;
304  $notify->cu = $Cu;
305  $notify->SendMail();
306 
307  }
308  }
309 
310  return msgReadMessages($dbh, $pHBEnv);
311  }
312  catch(exception $e)
313  {
314  return array("homecuErrors" => $e->getMessage(), "code" => SECURE_MESSAGES_ERROR_CODE);
315  }
316 }
317 
318 /**
319  * function msgDeleteMessageThread($dbh, $pHBEnv)
320  * Delete the given message. The user has already confirmed it is okay.
321  *
322  * @param $dbh -- the database connection
323  * @param $pHBEnv -- the environment variables
324  *
325 * Environmental variables needed:


326  * $pHBEnv["Cu"]

327  * $pHBEnv["Uid"]

328  * $pHBEnv["HCUPOST"]["parentid"]
329  * $pHBEnv["HCUPOST"]["what"] — The subject filter. This can be empty or undefined (for msgReadMessages).
330  *
331  * @return array
332  * $code -- "000" if successful, SECURE_MESSAGES_ERROR_CODE otherwise.
333  * $homecuInfo -- doesn't exist but could. (If it did, it would show a message.)
334  * $homecuErrors -- Doesn't exist if successful, otherwise is a string with the error message.
335  * $homecuData -- Doesn't exist if not successful, otherwise is an array of results for the grid.
336  */
337 function msgDeleteMessageThread($dbh, $pHBEnv) {
338  try
339  {
340  $parentId = $pHBEnv["HCUPOST"]["parentid"]; // thread id
341  $Cu= $pHBEnv["Cu"];
342  $Uid= $pHBEnv["Uid"];
343  if ( empty($Cu) || empty($Uid) || empty($parentId) ) {
344  throw new exception('Missing Paramters', 1);
345  }
346 
347  if ( $parentId > 0 ) { // If there is no parentId, I guess that it is "successful" based on there being nothing to delete.
348  $sql = "update cuadmeco set memdeleted = TRUE where cu = '$Cu' and user_id = '$Uid' and parentid = $parentId;
349  delete from cuadmeco where cu = '$Cu' and parentid = $parentId and user_id = '$Uid' and admdeleted and memdeleted;";
350  $sth = db_query($sql,$dbh);
351 
352  if (!$sth)
353  throw new exception("Update/delete query failed.", 1);
354  }
355 
356  return msgReadMessages($dbh, $pHBEnv);
357  }
358  catch(exception $e)
359  {
360  return array("homecuErrors" => $e->getMessage(), "code" => SECURE_MESSAGES_ERROR_CODE);
361  }
362 }
363 
364 /**
365  * function getCUTimeZone($dbh, $Cu)
366  * Gets the timezone from the credit union
367  * @deprecated -- This function is replaced by a global function: GetCreditUnionTimezone($dbh, $Cu). This function is in hcuCommon.i.
368  *
369  * @param $dbh -- the database connection.
370  * @param $Cu -- the credit union
371  *
372  * @return $tz -- the timezone of the credit union.
373  */
374 function getCUTimeZone($dbh, $Cu) {
375  // get CU time zone
376  $sql = "select rtrim(tz) from cuadmin where cu='$Cu'";
377  $sth = db_query($sql,$dbh);
378  if($sth) { list($tz) = db_fetch_array($sth,0); }
379  $tz = ("$tz" == "" ? "Mountain" : $tz);
380  if (strpos("$tz","/") === false) { $tz = "US/$tz"; }
381  return $tz;
382  }
383 /******** functions **********/
384 function SortMessages( $a, $b ) {
385  return ( $a["order"] < $b["order"] ? 1 : -1);
386 } // Sort Messages