Odyssey
cu_func.i
1 <?php
2 
3 require_once dirname(__FILE__). '/hcuCommon.i';
4 // require_once dirname(__FILE__). '/hcuEnv.i';
5 
6 $chk_key="SayChee3e";
7 // Constants for URL related encryption
8 define("URL_ENCDEC_CIPHER_MODE", "aes-256-ecb");
9 // allowed values: sha1, sha256
10 define("URL_ENCDEC_AUTH_HASH_ALGO", "sha256");
11 
12 /**
13  * check if mcrypt library exists
14  */
15 function check_mcrypt_exists_cufunc() {
16  if (!function_exists("mdecrypt_generic") && !function_exists("mcrypt_generic"))
17  throw new exception("MCRYPT library does not exist.");
18 }
19 
20 function hcu_encrypturl_mcrypt($str, $key) {
21  check_mcrypt_exists_cufunc();
22  $key_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
23  // ** IF the key does not fit the proper length be sure to fill in the remaining spaces with null bytes
24  $key = substr($key . str_repeat(chr(0), $key_size), 0, $key_size);
25  $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
26 
27  # implements PKCS7 padding
28  $len = strlen($str);
29  $pad = $blocksize - ($len % $blocksize);
30  $str .= str_repeat(chr($pad), $pad);
31 
32  return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB));
33 }
34 
35 function hcu_encrypturl($str, $key, $cipher_method=URL_ENCDEC_CIPHER_MODE) {
36  try {
37  $enc_resp_url = hcuOpenSSLEncrypt($str,
38  $key,
39  $method=$cipher_method,
40  $auth_hash_algo=URL_ENCDEC_AUTH_HASH_ALGO);
41  $ciphertext = $enc_resp_url["message"];
42  $hash_hmac = $enc_resp_url["hash"];
43  return bin2hex($hash_hmac.$ciphertext);
44 
45  // return bin2hex($enc_resp_url["message"]);
46  } catch (Exception $ex) {
47  throw $ex;
48  }
49 
50 }
51 
52 function hcu_decrypturl_mcrypt($str, $key) {
53  check_mcrypt_exists_cufunc();
54  $key_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
55  $key = substr($key . str_repeat(chr(0), $key_size), 0, $key_size);
56 
57  // ** ctype_xdigit - Checks the string to look for Hex Characters only -- pack throws error if non-hex is found
58  if (ctype_xdigit($str)) {
59  $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, pack("H*",$str), MCRYPT_MODE_ECB);
60  } else {
61  // ** Return empty string, the remaining code fails if an empty string
62  $str = '';
63  }
64 
65  if ($str !== '') {
66  # strip PKCS7 padding
67  $slast = ord(substr($str, -1));
68  // ** As explained in RFC 5652 6.3 Content-Encryption Process
69  // ** All input is padded, so the ordinal value of the last character will always
70  // ** represent the number of characters to strip.
71  $str = substr($str, 0, -$slast);
72  }
73  return $str;
74 }
75 
76 function hcu_decrypturl_openssl($str, $key, $cipher_method) {
77  try {
78  if (ctype_xdigit($str)) {
79 
80  $cipher_all = hex2bin($str);
81 
82  if(URL_ENCDEC_AUTH_HASH_ALGO == "sha256") {
83  $auth_hash_len = 32; // bytes
84  }
85  // PARMENCDEC_AUTH_HASH_ALGO == "sha1"
86  else {
87  $auth_hash_len = 16; //bytes
88  }
89 
90  $encrypted_hash = substr($cipher_all, 0, $auth_hash_len);
91  $ciphertext = substr($cipher_all, $auth_hash_len);
92 
93  return hcuOpenSSLDecrypt($ciphertext,
94  $encrypted_hash,
95  $key,
96  $method=$cipher_method,
97  $auth_hash_algo=URL_ENCDEC_AUTH_HASH_ALGO);
98  } else {
99  return '';
100  }
101  } catch (Exception $ex) {
102  return False;
103  }
104 }
105 
106 function hcu_decrypturl($str, $key, $cipher_method=URL_ENCDEC_CIPHER_MODE) {
107  try {
108  $openssl_result = hcu_decrypturl_openssl($str, $key, $cipher_method);
109  if ($openssl_result == False) {
110  $mcrypt_result = hcu_decrypturl_mcrypt($str, $key);
111  return $mcrypt_result;
112  } else {
113  return $openssl_result;
114  }
115  } catch (Exception $ex) {
116  throw $ex;
117  }
118 }
119 
120 function hcu_checkOffline($dbh,$HB_ENV) {
121 # returns boolean - does status allow current script to continue?
122 $Cu=$HB_ENV['Cu'];
123 $live=$HB_ENV['live'];
124 $offline=$HB_ENV['offline'];
125 $allowReadonly=$HB_ENV['allowReadonly'];
126 
127 switch ("$offline") {
128  case "Y": # yes, offline
129  case "U": # offline but "up-able"
130  $CheckStatus=false;
131  break;
132  case "N": # no, not offline
133  $CheckStatus=true;
134  break;
135  case "R": # readonly
136  if ($allowReadonly && !($live)) {
137  $CheckStatus=true;
138  } else {
139  $CheckStatus=false;
140  }
141  break;
142  default:
143  $CheckStatus=true;
144  }
145 
146 return $CheckStatus;
147 }
148 
149 /**
150  * hcu_checkService
151  *
152  * Requirements: cutrusted.i, must ALREADY be included for this function to work
153  *
154  * This function will look up a HOMECU service.
155  * The HOMECU service status are stored in the parms field of cutrusteddetail
156  * WHERE cu = HOMECU and trustedid = HOMECUSERVICE
157  *
158  * At this time, I will hard cord these value in this function, possibly later
159  * we may want to make this a global option
160  *
161  *
162  *
163  * @param integer $p_dbh - Current database handle
164  * @param string $pRequestedService - this is the HOMECU SERVICE (ie IPAY, ENSENTA)
165  *
166  * that is being checked for online status
167  *
168  * @return boolean This function will return {true/false}
169  * true - The Status for the requested Service is ACTIVE/ENABLED
170  * false - The Status of the requested Service is OFFLINE --
171  * The calling script should try and avoid using the
172  * service if false is returned
173  *
174  *
175  * RULES *
176  * Assume TRUE - ONLY SET TO FALSE for explicit value in the field,
177  * I am thinking this because we have a big system and don't want
178  * failure UNLESS expressed
179  *
180  */
181 function hcu_checkService ($p_dbh, $pRequestedService) {
182  $bolRet = true;
183 
184  // * SETUP THE cutrusted information to use to retrieve the service statuses
185  $homecuTrustCU = 'HOMECU';
186  $homecuTrustID = 'HOMECUSERVICE';
187 
188  $trustParms = Array("Cu" => $homecuTrustCU, "trustedid" => $homecuTrustID);
189  $trustItems = cutd_read($p_dbh, $trustParms);
190 
191  if ($trustItems['status']['Response'] == 'true') {
192  if ($trustItems['data']["{$homecuTrustCU}|{$homecuTrustID}"]) {
193  if (is_array($trustItems['data']["{$homecuTrustCU}|{$homecuTrustID}"])) {
194  $homecuServices = $trustItems['data']["{$homecuTrustCU}|{$homecuTrustID}"];
195  // ** Now have an ARRAY of services
196  foreach ($homecuServices as $itemService => $itemValue) {
197  // * MATCH the requested service
198  if (strtoupper($pRequestedService) == strtoupper($itemService)) {
199  // IF FOUND --
200  // return - true if value is N
201  // return - false if value is {Y/U}
202  $bolRet = ($itemValue == 'N' ? true : false);
203  break;
204  }
205  }
206  }
207  }
208  }
209 
210  return $bolRet;
211 }
212 function hcu_checkServiceMsg ($p_dbh, $pRequestedService) {
213 
214  // * SETUP THE cutrusted information to use to retrieve the service statuses
215  $homecuTrustCU = 'HOMECU';
216  $homecuTrustID = 'HOMECUSERVICE';
217 
218  $trustParms = Array("Cu" => $homecuTrustCU, "trustedid" => $homecuTrustID);
219  $trustItems = cutd_read($p_dbh, $trustParms);
220  if ($trustItems['status']['Response'] == 'true') {
221  if ($trustItems['data']["{$homecuTrustCU}|{$homecuTrustID}"]) {
222  if (is_array($trustItems['data']["{$homecuTrustCU}|{$homecuTrustID}"])) {
223  $homecuServices = $trustItems['data']["{$homecuTrustCU}|{$homecuTrustID}"];
224  $pRequestedService=strtoupper($pRequestedService);
225  $strMsg = $homecuServices["{$pRequestedService}_MSG"];
226  }
227  }
228  }
229  $strMsg = (trim($strMsg) == '' ? "Service temporarily unavailable. Please try again later" : $strMsg);
230  return $strMsg;
231 }
232 function getcurl($cmd){
233  $response='';
234  $fd=popen("$cmd", "r");
235  if ($fd) {
236  do {
237  $data = @fread($fd, 8192);
238  if (strlen($data) == 0) {
239  break;
240  }
241  $response .= $data;
242  } while(true);
243 
244  pclose($fd);
245  }
246  return $response;
247 }
248 
249  // This function will examine a string for a 2-byte UTF-8 value and convert it to an HTML entity.
250  // NOTE: UTF-8 2-byte format: 110xxxxx 10xxxxxx where x's make up the encoded number
251  // (so C4 84 -> 11000100 10000100 -> 001 00000100 -> 0x0104 -> 260 -> &#260)
252  function ConvertFromUTF8( $stringIn ) {
253  $stringOut = "";
254 
255  $len = strlen( $stringIn );
256  for( $i = 0; $i < $len; $i++ ) {
257  $c = ord( $stringIn[$i] );
258 
259  if ( $c >= 223 ) return $stringIn;
260  elseif ( $c >= 192 ) {
261  $bytes = 2;
262  if ( ( $i + $bytes ) > $len ) return $stringIn;
263 
264 
265  $firstByte = $c;
266  $i++;
267  $secondByte = ord( $stringIn[$i] );
268 
269  // mask off the first byte
270  $firstByteMasked = $firstByte & 0x1F;
271 
272  // mask off the 2nd byte
273  $secondByteMasked = $secondByte & 0x3F;
274 
275  // put low two bits from 1st byte into high two bits of 2nd byte
276  $lowTwo = $firstByteMasked & 0x03;
277  $lowTwo = $lowTwo << 6;
278  $newSecond = $secondByteMasked | $lowTwo;
279 
280  // shift off low two bits of 1st byte
281  $newFirst = $firstByteMasked >> 2;
282 
283  // now take the first nibble * 16^2 + 2nd nibble * 16 + 3rd nibble
284  $final = $newFirst * 256 + ($newSecond >> 4) * 16 + ($newSecond & 0x0F);
285  $encodedBytes = sprintf( "&#%d;", $final );
286 
287  $stringOut .= $encodedBytes;
288  } else {
289  // add to the output
290  $stringOut .= chr( $c );
291  }
292  }
293  return $stringOut;
294  } // end ConvertFromUTF8
295 
296 /**
297  * This will return the FIRST directory from the parameter string
298  *
299  * ie
300  * /hcubin7/m/script
301  * returns hcubin7
302  * /hcubin7/script
303  * returns hcubin7
304  *
305  * @param type $pUrl
306  * @return type
307  */
308 function returnBaseDirectory($pUrl){
309  //get public directory structure eg "/top/second/third"
310  $public_directory = dirname($pUrl);
311  //place each directory into array
312  $directory_array = explode('/', $public_directory);
313  //get highest or top level in array of directory strings
314 
315  // * Return the first non-blank value..
316  // * If a / is the first charactre of the directory then the first element is blank.
317  $public_base = ($directory_array[0] != '' ? $directory_array[0] : $directory_array[1]);
318 
319  return $public_base;
320 }
321 
322 /**
323  * Convert Microsoft "smart quotes" and other characters to be a kind that doesn't make JSON or htmlentities barf.
324  */
325 function convertMicrosoftCharacters( $inputStr ) {
326  $search = array(chr(145),
327  chr(146),
328  chr(147),
329  chr(148),
330  chr(151),
331  chr(150),
332  chr(133),
333  "\342\200\234",
334  "\342\200\235"
335  );
336 
337  $replace = array("'",
338  "'",
339  '"',
340  '"',
341  '-',
342  '-',
343  '&#8230;',
344  '"',
345  '"'
346  );
347 
348  return str_replace($search, $replace, $inputStr);
349 } // end convertMicrosoftChars