12 define(
"SECURE_MESSAGES_ERROR_CODE",
"5020" );
36 function msgCheckForMessages($pDbh, $pHBEnv) {
38 $Uid = $pHBEnv[
"Uid"];
39 if ( empty($Cu) || empty($Uid) ) {
40 throw new exception(
'Missing Parameters', 1);
44 $sql =
"select count(*) from cuadmeco where cu = '$Cu' 46 and origination = 0 and unread;";
47 $rh = db_query($sql, $pDbh);
50 list( $msgct ) = db_fetch_array($rh, 0);
53 $msgct = ($msgct ? $msgct : 0);
57 $return = array(
"code" =>
"000",
"homecuData" => array(
"message_count" => $msgct,
"has_messages" => ($msgct > 0) ?
"1" :
"0" ));
59 }
catch (Exception $e) {
60 $return = array(
"homecuErrors" => $e->getMessage(),
"code" => SECURE_MESSAGES_ERROR_CODE);
85 function msgReadMessages($dbh, $pHBEnv) {
91 if ( empty($Cu) || empty($Uid) ) {
92 throw new exception(
'Missing Parameters', 1);
94 $tz= GetCreditUnionTimezone($dbh, $Cu);
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)));
98 $whereEnd=
"like '%$value%' escape '^'";
100 $where= trim($what) ==
"" ?
"" :
" and lower(subject) $whereEnd";
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; ";
106 $sth = db_query($sql,$dbh);
109 throw new exception(
"Read query failed.", 1);
112 $sth1 = db_query($localzone,$dbh);
115 throw new exception(
"Timezone query failed.", 2);
117 $aryMessages = array();
118 for ($i= 0; $msgRow = db_fetch_array($sth, $i); $i++) {
120 $new = ($msgRow[
'unread'] ==
't' && $msgRow[
'origination'] == 0) ? 1 : 0;
123 $p = $msgRow[
'parentid'];
126 $m = $msgRow[
'messageid'];
129 $o = $msgRow[
"origination"];
132 $subject = convertMicrosoftCharacters( $msgRow[
"subject"] );
135 if (!isset($aryMessages[$p]))
136 $aryMessages[$p] = array(
"threadId"=>$p,
"order" => $m,
"unread"=>$new,
"origination"=>$o,
"subject"=>$subject,
"date"=>$msgRow[
"date"] );
139 $aryMessages[$p][
"order"] = $m;
142 $aryMessages[$p][
"subject"] = $subject;
146 $aryMessages[$p][
"unread"] = $new;
149 $aryMessages[$p][
"origination"] = $o;
151 $aryMessages[$p][
"date"] = $msgRow[
"date"];
156 usort( $aryMessages,
"SortMessages" );
158 return array(
"code" =>
"000",
"homecuData" => $aryMessages);
162 return array(
"homecuErrors" => $e->getMessage(),
"code" => SECURE_MESSAGES_ERROR_CODE);
184 function msgReadMessageThread($dbh, $pHBEnv) {
187 $parentId = $pHBEnv[
"HCUPOST"][
"parentid"];
189 $Uid= $pHBEnv[
"Uid"];
190 if ( empty($Cu) || empty($Uid) || empty($parentId) ) {
191 throw new exception(
'Missing Parameters', 1);
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);
203 throw new exception(
"Read query failed.", 1);
205 $sth1 = db_query($localzone,$dbh);
208 throw new exception(
"Localzone query failed.", 2);
211 for($i= 0; $msgrow = db_fetch_array($sth, $i); $i++ )
214 $messageText = convertMicrosoftCharacters( $msgrow[
'messagetext'] );
216 $aryResult[] = array(
"date"=>$msgrow[
'date'],
"origination"=>$msgrow[
'origination'],
"subject" => $msgrow[
'subject'],
"message"=>$messageText );
218 $sth = db_query($sqlunread,$dbh);
221 throw new exception(
"Update query failed.", 3);
223 return array(
"code" =>
"000",
"homecuData" => $aryResult);
227 return array(
"homecuErrors" => $e->getMessage(),
"code" => SECURE_MESSAGES_ERROR_CODE);
253 function msgSendMessage($dbh, $pHBEnv, $pMC) {
256 $subject = $pHBEnv[
"HCUPOST"][
"subject"];
257 $message = $pHBEnv[
"HCUPOST"][
"message"];
258 $parentId = $pHBEnv[
"HCUPOST"][
"parentid"];
261 $Uid= $pHBEnv[
"Uid"];
262 if ( empty($Cu) || empty($Uid) ) {
263 throw new exception(
'Missing Parameters', 1);
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);
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);
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);
279 throw new exception(
"Insert query failed.", 3);
282 $sql =
"select email from cuadmnotify where cu = '$Cu' and role = 'securenotify' ";
283 $emRS = db_query($sql, $dbh);
286 throw new exception(
"Email query failed.", 4);
288 if ($emRow = db_fetch_array($emRS)) {
289 $secEmail = trim($emRow[
'email']);
290 if ($secEmail !=
'') {
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";
298 $notify->mailto = $secEmail;
299 $notify->replyto =
"noreply@homecu.net";
300 $notify->subject = $emSubject;
301 $notify->msgbody = $emMsg;
302 $notify->callingfunction = __FUNCTION__;
303 $notify->file = __FILE__;
310 return msgReadMessages($dbh, $pHBEnv);
314 return array(
"homecuErrors" => $e->getMessage(),
"code" => SECURE_MESSAGES_ERROR_CODE);
337 function msgDeleteMessageThread($dbh, $pHBEnv) {
340 $parentId = $pHBEnv[
"HCUPOST"][
"parentid"];
342 $Uid= $pHBEnv[
"Uid"];
343 if ( empty($Cu) || empty($Uid) || empty($parentId) ) {
344 throw new exception(
'Missing Paramters', 1);
347 if ( $parentId > 0 ) {
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);
353 throw new exception(
"Update/delete query failed.", 1);
356 return msgReadMessages($dbh, $pHBEnv);
360 return array(
"homecuErrors" => $e->getMessage(),
"code" => SECURE_MESSAGES_ERROR_CODE);
374 function getCUTimeZone($dbh, $Cu) {
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"; }
384 function SortMessages( $a, $b ) {
385 return ( $a[
"order"] < $b[
"order"] ? 1 : -1);