-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_static_functions.php
More file actions
93 lines (83 loc) · 4.48 KB
/
Copy pathcommon_static_functions.php
File metadata and controls
93 lines (83 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
##
# © 2016 Partners HealthCare System, Inc. All Rights Reserved.
##
/**
Supporting functions for enhanced autonotification plugin.
Originally based on Autonotify plugin by Andy Martin.
As of June 2016, it support multiple notification emails in addition to multiple
triggers. The new version offers the ability to upgrade existing autonitfy
configurations on first use.
It must be used in conjunction with a data entry trigger to function in real-time.
The settings for each project are stored as an encoded variable (an) in the query
string of the DET.
**/
/**
* This file contains some functions that were plucked out of the REDCap code from version 5.x.x
* These functions were changed slightly in subsequent REDcap versions and as a result they have been extracted here for longevity
*/
// Function for decrypting
/**
*
* @global type $salt
* @param type $encrypted_data
* @param type $custom_salt
* @return boolean
*/
function decrypt_static($encrypted_data, $custom_salt=null)
{
if (!mcrypt_loaded_static()) return false;
// $salt from db connection file
global $salt;
// If $custom_salt is not provided, then use the installation-specific $salt value
$this_salt = ($custom_salt === null) ? $salt : $custom_salt;
// This loop is for REDCap 5.x support.
$iloops = 0;
while ( strlen ( $this_salt ) < 32 ) {
$this_salt = $this_salt."".$this_salt; // append the stal to itself until it's over 32
if ( $iloops == 10 ) break;
$iloops+=1;
}
// If salt is longer than 32 characters, then truncate it to prevent issues
if (strlen($this_salt) > 32) $this_salt = substr($this_salt, 0, 32);
// Define an encryption/decryption variable beforehand
defined("MCRYPT_IV") or define("MCRYPT_IV", mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
// Decrypt and return
return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this_salt, base64_decode($encrypted_data), MCRYPT_MODE_ECB, MCRYPT_IV),"\0");
}
// Function for checking if mcrypt PHP extension is loaded
function mcrypt_loaded_static($show_error=false) {
if ((extension_loaded('mcrypt') || (function_exists('dl') && dl(((PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '') . (null ? null : 'mcrypt') . '.' . PHP_SHLIB_SUFFIX))) != 1) {
if ($show_error) {
exit('<div class="red"><b>ERROR:</b><br>The "mcrypt" PHP extension is not loaded but is required for encryption/decryption.<br>
Please install the PHP extension "mcrypt" on your server, reboot your server, and then reload this page.</div>');
} else {
return false;
}
} else {
return true;
}
}
// Function for encrypting
function encrypt_static($data, $custom_salt=null)
{
if (!mcrypt_loaded_static()) return false;
// $salt from db connection file
global $salt;
// If $custom_salt is not provided, then use the installation-specific $salt value
$this_salt = ($custom_salt === null) ? $salt : $custom_salt;
// This loop is for REDCap 5.x support.
$iloops = 0;
while ( strlen ( $this_salt ) < 32 ) {
$this_salt = $this_salt."".$this_salt; // append the salt to itself until it's over 32
if ( $iloops == 10 ) break; // don't be silly - have an exit strategy
$iloops+=1;
}
// If salt is longer than 32 characters, then truncate it to prevent issues
if (strlen($this_salt) > 32) $this_salt = substr($this_salt, 0, 32);
// Define an encryption/decryption variable beforehand
defined("MCRYPT_IV") or define("MCRYPT_IV", mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
// Encrypt and return
return rtrim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this_salt, $data, MCRYPT_MODE_ECB, MCRYPT_IV)),"\0");
}
?>