-
Notifications
You must be signed in to change notification settings - Fork 12
/
zmt_core.module
161 lines (148 loc) · 4.58 KB
/
zmt_core.module
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Implements hook_permission().
*/
function zmt_core_permission() {
return array(
'administer zmt' => array(
'title' => t('Administer zimbra multi tenancy'),
'description' => t('Allows users to perform any action on zimbra multi tenancy.'),
'restrict access' => TRUE,
),
);
}
/**
* Return permission names for a given entity type.
*/
function zmt_access_permissions($entity_type, $entity_name) {
$permissions = array();
$permissions['administer any ' . $entity_type] = array(
'title' => t('Administer any @entity_name', array('@entity_name' => $entity_name)),
'description' => t('Allows users to perform action on any @entity_name.', array('@entity_name' => $entity_name)),
'restrict access' => TRUE,
);
$permissions['administer own ' . $entity_type] = array(
'title' => t('Administer own @entity_name', array('@entity_name' => $entity_name)),
'description' => t('Allows users to perform action on own @entity_name.', array('@entity_name' => $entity_name)),
'restrict access' => TRUE,
);
return $permissions;
}
/**
* Check request is ajax.
*/
function zmt_is_ajax_request() {
$requested_with = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : '';
$is_ajax_request = strtolower($requested_with) === 'xmlhttprequest';
if (!$is_ajax_request) {
$current_path = current_path();
$is_ajax_request = strtolower($current_path) === 'system/ajax';
}
return $is_ajax_request;
}
/**
* Check subject is matches with patterns.
*/
function zmt_is_matches(array $patterns = array(), $subject = ''){
$is_match = FALSE;
if (!empty($patterns)) {
$pattern = implode('|', $patterns);
if (!empty($pattern)) {
$is_match = preg_match('/'.$pattern.'/msS', $subject);
}
}
return $is_match;
}
/**
* Force a client-side redirect.
*/
function zmt_ajax_command_redirect($url, $delay = 0, $options = array()) {
$path = drupal_get_path('module', 'zmt_core');
drupal_add_js($path . '/js/zmt.js');
return array(
'command' => 'zmt_ajax_redirect',
'url' => url($url, $options),
'delay' => (int) $delay,
);
}
/**
* Force reload of the current page.
*/
function zmt_ajax_command_reload() {
$path = drupal_get_path('module', 'zmt_core');
drupal_add_js($path . '/js/zmt.js');
return array(
'command' => 'zmt_ajax_reload',
);
}
/**
* Encrypt a plain string.
*/
function zmt_password_encrypt($text) {
$key = md5(drupal_get_hash_salt());
if (function_exists('mcrypt_encrypt')) {
$ivsize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv)));
}
if (function_exists('openssl_encrypt')) {
return _zmt_openssl_encrypt($text, $key);
}
else {
module_load_include('inc', 'zmt_core', 'includes/zmt.aes');
return AesCtr::encrypt($text, $key, 256);
}
}
/**
* Decrypt an encrypted string.
*/
function zmt_password_decrypt($data) {
$key = md5(drupal_get_hash_salt());
if (function_exists('mcrypt_decrypt')) {
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($data), MCRYPT_MODE_ECB, $iv));
}
elseif (function_exists('openssl_decrypt')) {
return _zmt_openssl_decrypt($data, $key);
}
else {
module_load_include('inc', 'zmt_core', 'includes/zmt.aes');
return AesCtr::decrypt($data, $key, 256);
}
}
/**
* Encrypt a string by openssl.
*/
function _zmt_openssl_encrypt($text, $password){
$salt = openssl_random_pseudo_bytes(8);
$salted = '';
$dx = '';
while (strlen($salted) < 48) {
$dx = md5($dx . $password . $salt, TRUE);
$salted .= $dx;
}
$key = substr($salted, 0, 32);
$iv = substr($salted, 32,16);
$data = openssl_encrypt($text, 'aes-256-cbc', $key, TRUE, $iv);
return trim(base64_encode('Salted__' . $salt . $data));
}
/**
* Decrypt an encrypted string by openssl.
*/
function _zmt_openssl_decrypt($edata, $password) {
$data = base64_decode($edata);
$salt = substr($data, 8, 8);
$ct = substr($data, 16);
$rounds = 3;
$data00 = $password . $salt;
$md5_hash = array();
$md5_hash[0] = md5($data00, TRUE);
$result = $md5_hash[0];
for ($i = 1; $i < $rounds; $i++) {
$md5_hash[$i] = md5($md5_hash[$i - 1].$data00, TRUE);
$result .= $md5_hash[$i];
}
$key = substr($result, 0, 32);
$iv = substr($result, 32,16);
return openssl_decrypt($ct, 'aes-256-cbc', $key, TRUE, $iv);
}