-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitecon.module
executable file
·277 lines (254 loc) · 7.62 KB
/
sitecon.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* Implementation of hook_permission().
*/
function sitecon_permission() {
return array(
'site connection' => array(
'title' => t('Multiple site connection'),
'description' => t('When user login/logout on this site, auto login/logout server site.'),
)
);
}
/**
* Implementation of hook_menu().
*/
function sitecon_menu() {
$items['admin/config/sitecon/global'] = array(
'title' => 'Global',
'type' => MENU_DEFAULT_LOCAL_TASK,
'access arguments' => array('administer site configuration'),
'weight' => -10,
);
$items['admin/config/sitecon'] = array(
'title' => 'Sites Connections',
'description' => 'Sites connections many to many sites.',
'page callback' => 'sitecon',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function sitecon() {
return 'Sites connections many to many sites. You can install server or client module for support sites connections module';
}
function sitecon_encode($key, $data) {
$data = serialize($data);
$key = sha1($key);
$str_len = strlen($data);
$key_len = strlen($key);
$j = 0;
for ($i = 0; $i < $str_len; $i++) {
$ord_str = ord(substr($data, $i, 1));
if ($j == $key_len) { $j = 0; }
$ord_key = ord(substr($key, $j, 1));
$j++;
$hash .= strrev(base_convert(dechex($ord_str + $ord_key), 16, 36));
}
return $hash;
}
function sitecon_decode($key, $data) {
$key = sha1($key);
$str_len = strlen($data);
$key_len = strlen($key);
$j = 0;
for ($i = 0; $i < $str_len; $i+=2) {
$ord_str = hexdec(base_convert(strrev(substr($data, $i, 2)), 36, 16));
if ($j == $key_len) { $j = 0; }
$ord_key = ord(substr($key, $j, 1));
$j++;
$hash .= chr($ord_str - $ord_key);
}
$hash = unserialize($hash);
return $hash;
}
function sitecon_autoregister($user) {
if ($user->pass) {
if($account = db_query("SELECT * FROM users WHERE LOWER(name) = LOWER(:name)", array(':name' => $user->name))->fetchObject()) {
return $account;
}
}
// Case email already registered
if ($account = db_query("SELECT * FROM users WHERE LOWER(mail) = LOWER(:mail)", array(':mail' => $user->mail))->fetchObject()) {
return $account;
}
if ($account = db_query("SELECT * FROM users WHERE LOWER(name) = LOWER(:mail)", array(':mail' => $user->mail))->fetchObject()) {
return;
}
$pass = ($user->pass? $user->pass: user_password());
$account = array(
'name' => $user->name? $user->name: $user->mail,
'mail' => $user->mail,
'timezone' => ($user->timezone? $user->timezone: variable_get('date_default_timezone', NULL)),
'pass' => $pass,
'init' => $user->mail,
'roles' => array(),
'status' => 1
);
$account = user_save('', $account);
// Update password directly because user_save convert password to md5 normaly.
db_update('users')
->fields(array('pass' => $pass))
->condition('uid', $account->uid)
->execute();
_user_mail_notify('register_no_approval_required', $account);
return $account;
}
function sitecon_autologin($account) {
global $user;
if (!$account || drupal_is_denied(ip_address()) || $user->uid) { return $account; }
$user = user_load($account->uid);
user_login_finalize();
return $user;
}
function sitecon_autologout() {
global $user;
watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
// Destroy the current session:
session_destroy();
// Only variables can be passed by reference workaround.
$null = NULL;
user_module_invoke('logout', $null, $user);
// Load the anonymous user
$user = drupal_anonymous_user();
}
// Settings form for server and client extends ====================================
function sitecon_list($type) {
$rs = db_select('sitecon_'.$type, 's')
->fields('s')
->extend('PagerDefault')
->extend('TableSort')
->limit(220)
->execute();
$items = array();
foreach ($rs as $item) {
$items[] = array(l($item->domain, 'admin/config/sitecon/'.$type.'/'.$item->id.'/edit'), $item->apikey);
}
return theme('table', array(
'header' => array('Domain', 'API Key'),
'rows' => $items,
)).theme('pager');
}
/**
* Implementation of hook_form().
*/
function sitecon_form($node, $form_state, $id = 0, $type) {
$item = sitecon_get_item($id, $type);
$form['type'] = array(
'#type' => 'hidden',
'#value' => $type,
);
$form['privatekey'] = array(
'#type' => 'password',
'#title' => t('Private Key'),
);
$form['domain'] = array(
'#type' => 'textfield',
'#title' => t('Domain'),
'#default_value' => $item->domain,
);
$form['apikey'] = array(
'#type' => 'textfield',
'#title' => t('API Key'),
'#default_value' => $item->apikey,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t($id? 'update': 'create'),
);
if ($id) {
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('delete'),
);
$form['id'] = array(
'#type' => 'hidden',
'#value' => $id
);
}
return $form;
}
/**
* Implementation of hook_submit().
*/
function sitecon_form_submit($form, &$form_state) {
$type = $form_state['values']['type'];
switch ($form_state['values']['op']) {
case 'create':
db_merge('sitecon_'.$type)
->key(array(
'privatekey' => md5($form_state['values']['privatekey']),
'domain' => $form_state['values']['domain'],
))
->fields(array(
'apikey' => $form_state['values']['apikey'],
))
->execute();
break;
case 'update':
$fields = array(
'domain' => $form_state['values']['domain'],
'apikey' => $form_state['values']['apikey'],
);
if ($form_state['values']['privatekey']) {
$fields['privatekey'] = md5($form_state['values']['privatekey']);
}
db_update('sitecon_'.$type)
->fields($fields)
->condition('id', $form_state['values']['id'])
->execute();
break;
case 'delete':
drupal_goto('admin/config/sitecon/'.$type.'/'.$form_state['values']['id'].'/delete');
break;
}
drupal_set_message(ucfirst($form_state['values']['op']).' completed.');
drupal_goto('admin/config/sitecon/'.$type);
}
/**
* Implementation of hook_form().
*/
function sitecon_delete_form($node, $form_state, $id = 0, $type) {
$form['type'] = array(
'#type' => 'hidden',
'#value' => $type,
);
$form['delete'] = array(
'#type' => 'markup',
'#value' => '<div>Are you sure you want to delete this item?</div>',
);
$form['id'] = array(
'#type' => 'hidden',
'#value' => $id,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Confirm',
);
$form['cancel'] = array(
'#type' => 'submit',
'#value' => 'Cancel',
);
return $form;
}
function sitecon_delete_form_submit($form, &$form_state) {
$type = $form_state['values']['type'];
if ($form_state['values']['op'] == 'Confirm') {
db_delete('sitecon_'.$type)
->condition('id', $form_state['values']['id'])
->execute();
drupal_goto('admin/config/sitecon/'.$type);
}
else {
drupal_goto('admin/config/sitecon/'.$type.'/'.$form_state['values']['id'].'/edit');
}
}
function sitecon_get_item($id = 0, $type) {
return db_query("SELECT * FROM {sitecon_$type} WHERE id = :id", array(':id' => $id))->fetchObject();
}
function sitecon_get_item_from_domain($domain, $type) {
return db_query("SELECT * FROM {sitecon_$type} WHERE domain = :domain", array(':domain' => $domain))->fetchObject();
}
function sitecon_get_item_from_apikey($apikey, $type) {
return db_query("SELECT * FROM {sitecon_$type} WHERE apikey = :apikey", array(':apikey' => $apikey))->fetchObject();
}