-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2d_passport.module
408 lines (352 loc) · 12.3 KB
/
d2d_passport.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
* @file
*/
define('D2D_PASSPORT_OFFICIAL_SERVER', 'http://d2d.inn.ac');
define('D2D_PASSPORT_OFFICIAL_SERVER_XMLRPC', D2D_PASSPORT_OFFICIAL_SERVER . '/xmlrpc.php');
/**
* Implements hook_d2d_secure_rpc().
*/
function d2d_passport_d2d_secure_rpc() {
$methods = array();
$methods['d2d_passport_get_informations'] = array(
'arguments' => array('infotypes' => 'is_string'),
'callback' => '_d2d_passport_get_informations',
'description' => 'Returns detailed information about this instance (d2d_passport).',
);
return $methods;
}
/**
*/
function _d2d_passport_get_informations($arguments, $rpc_info) {
$error_string = NULL;
$users_number = NULL;
$infotypes = $arguments['infotypes'];
$infotypes = d2d_explode($infotypes);
$return = array();
if (isset($infotypes['users_number']) && $infotypes['users_number'] === TRUE) {
$query = db_select('users', 'u')
->fields('u', array('uid'))
->condition('status', 1, '=')
->execute();
$return['users_number'] = $query->rowCount();
}
if (isset($infotypes['site_info']) && $infotypes['site_info'] === TRUE) {
$name = variable_get('site_name', '');
$slogan = variable_get('site_slogan', '');
$return['site_info'] = d2d_implode(array('name' => $name, 'slogan' => $slogan));
}
if (isset($infotypes['nodes_number']) && $infotypes['nodes_number'] === TRUE) {
$query = db_select('node', 'n')
->fields('n', array('nid'))
->condition('status', 1, '=')
->execute();
$return['nodes_number'] = $query->rowCount();
}
if (isset($infotypes['tags']) && $infotypes['tags'] === TRUE) {
$return['tags'] = variable_get('d2d_passport_instance_tags', '');
}
if (isset($infotypes['friendship']) && $infotypes['friendship'] === TRUE) {
$friends = d2d_api_friend_get();
$fs = array();
foreach ($friends as $key => $value) {
$fs[] = d2d_implode(array(
'd2did' => $value['d2d_id'],
'url' => $value['url'],
));
}
$return['friendship'] = d2d_implode($fs);
}
if (isset($infotypes['d2d_description']) && $infotypes['d2d_description'] === TRUE) {
$di = d2d_api_own_instance_get();
$return['d2d_description'] = $di['description'];
}
return d2d_implode(array('return' => d2d_implode($return), 'error' => $error_string));
}
/**
*/
function _d2d_passport_get_users_number($arguments, $rpc_info) {
$error_string = NULL;
$users_number = NULL;
$query = db_select('users', 'u')
->fields('u', array('uid'))
->condition('status', 1, '=')
->execute();
$users_number = $query->rowCount();
return d2d_implode(array('return' => $users_number, 'error' => $error_string));
}
/**
*/
function _d2d_passport_get_site_information($arguments, $rpc_info) {
$error_string = NULL;
$return = array();
$return['name'] = variable_get('site_name', '');
$return['slogan'] = variable_get('site_slogan', '');
$return = d2d_implode($return);
return d2d_implode(array('return' => $return, 'error' => $error_string));
}
/**
*/
function _d2d_passport_get_nodes_number($arguments, $rpc_info) {
$error_string = NULL;
$nodes_number = NULL;
$query = db_select('node', 'n')
->fields('n', array('nid'))
->condition('status', 1, '=')
->execute();
$nodes_number = $query->rowCount();
return d2d_implode(array('return' => $nodes_number, 'error' => $error_string));
}
/**
*/
function _d2d_passport_get_tags($arguments, $rpc_info) {
$error_string = NULL;
$return = variable_get('d2d_passport_instance_tags', '');
return d2d_implode(array('return' => $return, 'error' => $error_string));
}
/**
* d2d_passport_configure_form
*/
function d2d_passport_configure_form($form, &$form_state) {
$form = array();
$form['tags'] = array(
'#type' => 'fieldset',
'#title' => 'D2D Tags',
'#description' => t('Insert here a few keywords that properly identify your site. These keywords will be used by the D2D server to index your instance.'),
);
$form['tags']['text'] = array(
'#type' => 'textfield',
// '#title' => 'tags',
'#description' => 'Separate multiple tags with a comma.',
'#default_value' => variable_get('d2d_passport_instance_tags', ""),
'#size' => 50,
);
$form['tags']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Tags'),
'#submit' => array('d2d_passport_configure_form_tags_submit'),
);
$form['notify'] = array(
'#type' => 'fieldset',
'#title' => 'D2D Passport Notify',
'#description' => t('If you set this option, you will ping the D2D server at every request. No private information will be sent out. '),
);
$form['notify']['checkb'] = array(
'#type' => 'checkbox',
'#title' => 'Notify D2D Server',
'#default_value' => variable_get('D2D_PASSPORT_NOTIFY', FALSE)?1:0,
);
$form['notify']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('d2d_passport_configure_form_notify_submit'),
);
return $form;
}
/**
*/
function d2d_passport_configure_form_tags_submit($form, &$form_state) {
if (empty($form_state['values']['text'])) {
drupal_set_message(t("Please enter some words."), "error");
return;
}
variable_set('d2d_passport_instance_tags', $form_state['values']['text']);
drupal_set_message(t("D2D tags successfully saved. D2D Server will fetch them later."));
}
/**
*/
function d2d_passport_configure_form_notify_submit($form, &$form_state) {
// Cannot use variable_get inside empty, otherwise an error is raised.
$server_url = variable_get('D2D_PASSPORT_SERVER_URL', FALSE);
if ($form_state['values']['checkb'] === 1 && !empty($server_url)) {
variable_set('D2D_PASSPORT_NOTIFY', TRUE);
variable_set('D2D_PASSPORT_SHOULD_NOTIFY', TRUE);
drupal_set_message(t("D2D Notify option correctly set."));
}
elseif ($form_state['values']['checkb'] === 1) {
drupal_set_message(t("D2D Notify option cannot be enabled. Select a D2D Server first."), "warning");
variable_set('D2D_PASSPORT_NOTIFY', FALSE);
}
else {
variable_set('D2D_PASSPORT_SHOULD_NOTIFY', FALSE);
variable_set('D2D_PASSPORT_NOTIFY', FALSE);
}
}
/**
* save the d2d_sever
*/
function d2d_passport_define_server_form($form, &$form_state) {
$server = variable_get('D2D_PASSPORT_SERVER_URL', FALSE);
$serverid = variable_get('D2D_PASSPORT_SERVER_ID', FALSE);
$serverd2did = variable_get('D2D_PASSPORT_SERVER_D2DID', FALSE);
$form = array();
$form['define_d2d_server'] = array(
'#type' => 'fieldset',
'#title' => 'Define D2D Server',
);
$friends = d2d_api_friend_get();
if ($server !== FALSE) {
$form['define_d2d_server']['selectedserver'] = array(
'#prefix' => "<strong>Selected Server: " . l($serverd2did, "admin/d2d/instances/$serverid/details") . "<br></strong>",
'#markup' => t('Click') . ' ' . l(t('here'), 'admin/d2d/passport/resetserver'),
'#suffix' => ' ' . t('to reset it.'),
);
return $form;
}
if ($friends === FALSE) {
$form['define_d2d_server']['noavaild2d'] = array(
'#prefix' => "No friend instances for now.<br>Check if you can add ",
'#markup' => l(t('some friends'), 'admin/d2d'),
'#suffix' => '.',
);
return $form;
}
//dsm($friends);
$header = array(
'd2did' => t('D2D ID'),
'url' => t('URL'),
'title' => t('TITLE'),
'description' => t('DESCRIPTION'),
'time_inserted' => t('FRIENDSHIP CREATED'),
);
$options = array();
foreach ($friends as $key => $friend) {
$createdtime = date('j-m-Y H:i:s (P)', $friend['time_inserted']);
$options[$key]= array(
'd2did' => $key,
'url' => $friend['url'],
'title' => $friend['name'],
'description' => $friend['description'],
'time_inserted' => $createdtime,
);
}
$form['define_d2d_server']['tablesl'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE,
'#js_select' => FALSE,
);
$form['define_d2d_server']['submit'] = array(
'#type' => 'submit',
'#value' => t('Select The D2D Server'),
);
return $form;
}
/**
*/
function d2d_passport_define_server_form_submit(&$form, &$form_state) {
if (empty($form_state['values']['tablesl'])) {
drupal_set_message(t("Please choose at least one."), 'error');
return;
}
else {
// Reset the old value of 'D2D_PASSPORT_NOTIFY, if it was TRUE the
// last time a server was set or no server was set (fresh install).
if (variable_get('D2D_PASSPORT_SHOULD_NOTIFY', FALSE)) {
drupal_set_message(t('D2D Server notification re-enabled.'), 'warning');
variable_set('D2D_PASSPORT_NOTIFY', TRUE);
}
$instance = d2d_api_instance_get($form_state['values']['tablesl']);
variable_set('D2D_PASSPORT_SERVER_URL', $instance['url']);
variable_set('D2D_PASSPORT_SERVER_ID', $instance['id']);
variable_set('D2D_PASSPORT_SERVER_D2DID', $instance['d2d_id']);
drupal_set_message(t("D2D Server correctly updated."));
}
}
/**
* Implements hook_menu().
*/
function d2d_passport_menu() {
$items['admin/d2d/passport'] = array(
'title' => 'D2D Passport',
'page callback' => 'd2d_passport_configure_page',
'access arguments' => array('administer d2d'),
'type' => MENU_LOCAL_TASK,
'weight' => 81,
);
$items['admin/d2d/passport/resetserver'] = array(
'title' => 'reset d2d server',
'page callback' => 'd2d_passport_reset_d2d_server',
'access arguments' => array('administer d2d'),
);
return $items;
}
/**
*/
function d2d_passport_reset_d2d_server() {
$notify = variable_get('D2D_PASSPORT_NOTIFY', FALSE);
if ($notify) {
drupal_set_message(t('D2D Server notification temporarily disabled. It will automatically be re-enabled when a new server will be selected.'), 'warning');
variable_set('D2D_PASSPORT_NOTIFY', FALSE);
}
variable_del('D2D_PASSPORT_SERVER_URL');
variable_del('D2D_PASSPORT_SERVER_D2DID');
variable_del('D2D_PASSPORT_SERVER_ID');
drupal_goto('admin/d2d/passport');
}
/**
*/
function d2d_passport_configure_page() {
$form = drupal_get_form('d2d_passport_define_server_form');
$page = drupal_render($form);
$hks = module_invoke('d2d_passport', 'd2d_secure_rpc');
$header = array(
'method_name' => t('D2D Passport Method Name'),
'method_description' => t('method_description'),
);
$rows = array();
foreach ($hks as $key => $value) {
$rows[$key][] = $key;
$rows[$key][] = isset($value['description']) ? $value['description'] : '';
}
$check = l(t('Make sure that the permissions are correctly set.'), 'admin/d2d/g_and_p/permissions');
//$rows[] = array('<div id="d2d-passport-check-permission">' . $check . "</div>", NULL);
$rows[] = array('<strong>' . $check . "</strong>", NULL);
$page .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('patterns-server-list-table'))));
$form = drupal_get_form('d2d_passport_configure_form');
$page .= drupal_render($form);
return $page;
}
/**
*/
function d2d_passport_notify($my_instance, $instance, $type, $len) {
$allow = variable_get('D2D_PASSPORT_NOTIFY', FALSE);
if ($allow === FALSE) {
return;
}
$url = variable_get('D2D_PASSPORT_SERVER_URL', FALSE);
if (empty($url)) {
watchdog('d2d_passport', "The d2d server hasn't been set");
return;
}
$friend = d2d_api_friend_get_by_url($url);
$information = array();
$information['from'] = $my_instance['d2d_id'];
$information['from_name'] = $my_instance['name'];
$information['from_url'] = $my_instance['url'];
$information['to'] = $instance['d2d_id'];
$information['to_name'] = $instance['name'];
$information['to_url'] = $instance['url'];
$information['type'] = $type;
$information['len'] = $len;
$imploded_infor = d2d_implode($information);
if ($imploded_infor === FALSE) {
watchdog('d2d_passport', 'Internal error while compacting the patterns to be sent.');
return FALSE;
}
$error_string = '';
// Last parameter is FALSE, otherwise it creates an infinite loop.
$res = d2d_call_secure_rpc($friend, 'd2d_server_save_client_actions', array(
'info' => $imploded_infor,
), $error_string, FALSE);
if ($res === FALSE) {
watchdog('d2d_passport', check_plain($url) . ' - ' . check_plain($error_string));
}
else {
$result = d2d_explode($res);
if (isset($result['return']) && $result['return'] === FALSE) {
watchdog('d2d_passport', check_plain($url) . ' - ' . $result['error']);
}
}
}