Odyssey
hcuSecureMail.data
1 <?php
2 /*
3  * File: hcuSecureMail.data
4  * Purpose: Handle the CRUD portion of the Secure Messaging feature. Return any requested data
5  * in a JSON format, for the client to display accordingly.
6  *
7  * Call this script with the following parameters
8  * action - what the client side is requesting.
9  *
10  *
11  * Returns JSON OBJECT.
12  */
13 try {
14  header('Content-Type: application/json');
15 
16  // ** SET HOMECU FLAGS
17  $serviceShowInfo = false;
18  $serviceLoadMenu = false;
19  $serviceShowMenu = false;
20  $serviceAllowReadonly = true;
21 
22  // ** INCLUDE MAIN GLOBAL SCRIPT -- Handles security / global variable values
23  // hcuService will be returning a status object: e.g. ["homecuErrors":{[{"message":"message1"}...{"message":"messageN"}}]
24  require_once(dirname(__FILE__) . '/../library/hcuService.i');
25  require_once(dirname(__FILE__) . '/../library/msgECO.i');
26 
27  $string= array("filter" => FILTER_DEFAULT);
28 
29  // ** IMPORT FORM VALUES
30  $dms_ok=array( "action"=>$string, "show"=>$string, "what"=>$string, "order"=>$string, "parentid"=>$string, "subject"=>$string, "message"=>$string);
31 
32  HCU_ImportVars($HB_ENV, "HCUPOST", $dms_ok);
33 
34  $Cu = $HB_ENV["Cu"];
35  $Cn = $HB_ENV["Cn"];
36  $Uid = $HB_ENV["Uid"];
37 
38  //get the database connection
39  // $dbh is set up
40  // ** First check the refer script -- make sure coming from right place
41  $parseRefer = parse_url($_SERVER['HTTP_REFERER']);
42  $referScript = basename($parseRefer['path']);
43  if (!in_array($referScript, array('hcuSecureMail.prg'))) {
44  // ** Wrong script calling this data routine
45  $aryErrors[] = $MC->msg('Feature Unavailable', HCU_DISPLAY_AS_RAW);
46  throw new Exception (HCU_JsonEncode($aryErrors));
47  }
48 
49  /*
50  * ** CHECK USER FEATURE PERMISSIONS **
51  * NOTE: DO NOT AUTO-REDIR. Handle perm error here
52  */
53  if (!PermCheckFeatureScreen($dbh, $HB_ENV, $MC, FEATURE_SECURE_MSG, '', false)) {
54  throw new Exception (HCU_JsonEncode(Array($MC->msg('Rights not set', HCU_DISPLAY_AS_HTML))));
55  }
56 
57 
58  if (!$dbh) {
59  // The connection was not made to the database
60  // unresolved: return an error??
61  }
62 
63  // initialize the error and result objects
64  $aryResult = array();
65  $aryErrors = array();
66  $aryInfo = array();
67  $aryReply = array();
68 
69  // do the requested operation
70  switch ( $HB_ENV["HCUPOST"]["action"] ) {
71  case "read_messages":
72  $aryReply = msgReadMessages($HB_ENV["dbh"], $HB_ENV);
73  santitizeSubject($aryReply);
74  break;
75  case "read_thread":
76  $aryReply = msgReadMessageThread($HB_ENV["dbh"], $HB_ENV);
77  santitizeBody($aryReply);
78  break;
79  case "send_message":
80  $aryReply = msgSendMessage($HB_ENV["dbh"], $HB_ENV, $HB_ENV["MC"]);
81  santitizeSubject($aryReply);
82  break;
83  case "delete_message":
84  $aryReply = msgDeleteMessageThread($HB_ENV["dbh"], $HB_ENV);
85  santitizeSubject($aryReply);
86  break;
87  default:
88  $aryErrors[] = array( "message" => "Unexpected action: {$HB_ENV["HCUPOST"]["action"]}" );
89  throw new Exception (HCU_JsonEncode($aryErrors));
90  }
91 }
92 catch(Exception $ex)
93 {
94  //Return error message
95  $aryReply["homecuErrors"] = HCU_JsonDecode( $ex->getMessage() );
96 }
97 
98 print HCU_JsonEncode($aryReply);
99 
100 function santitizeSubject(&$aryReply)
101 {
102  // nl2br here so that it isn't picked up by the apps.
103  for($i= 0, $count= count($aryReply["homecuData"]); $i != $count; $i++)
104  {
105  $subject= $aryReply["homecuData"][$i]["subject"];
106  $aryReply["homecuData"][$i]["subject"]= nl2br(htmlentities($subject, ENT_QUOTES, "UTF-8", false));
107  }
108 }
109 
110 function santitizeBody(&$aryReply)
111 {
112  // nl2br here so that it isn't picked up by the apps.
113  for($i= 0, $count= count($aryReply["homecuData"]); $i != $count; $i++)
114  {
115  $message= $aryReply["homecuData"][$i]["message"];
116  $aryReply["homecuData"][$i]["message"]= nl2br(htmlentities($message, ENT_QUOTES, "UTF-8", false));
117  }
118 }
119