-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathae_drupal.module
239 lines (210 loc) · 6.68 KB
/
ae_drupal.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
<?php
/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
require('activationengine.php');
define('AEURL', 'http://aengine.net/api');
function ae_drupal_help($path, $arg) {
switch ($path) {
case "admin/help#ae_drupal":
return '<p>' . t("Provides integration with Activation Engine. <br /> Activation Engine is a game engine geared towards real life multiplayer gaming. It provides a multi channel game experience with heavy emphasis on mobile devices. Activation Engine can be extended by creating custom game actions and fully tailored game user experience. It is suitable for both casual games and education.") . '</p>';
break;
}
}
/**
* Implements hook_block_info().
*/
function ae_drupal_block_info() {
$blocks['ae_drupal'] = array(
// The name that will appear in the block list.
'info' => t('Activation Engine'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_menu().
*/
function ae_drupal_menu() {
$items = array();
$items['admin/config/content/ae_drupal'] = array(
'title' => 'AE Drupal',
'description' => 'Configuration for AE Drupal module',
'page callback' => 'drupal_get_form',
'page arguments' => array('ae_drupal_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['aedrupal/register_url'] = array(
'title' => 'AE Current Page Recording',
'type' => MENU_CALLBACK,
'page callback' => 'ae_drupal_register_url',
'access arguments' => array('access content'),
);
$items['aedrupal/get_dashboard'] = array(
'title' => 'AE Get Dashboard',
'type' => MENU_CALLBACK,
'page callback' => 'ae_drupal_get_dashboard',
'access arguments' => array('access content'),
);
return $items;
}
/**
* Page callback: AE Drupal settings
*
* @see ae_drupal_menu()
*/
function ae_drupal_form($form, &$form_state) {
$form['ae_drupal_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Activation Engine API key'),
'#default_value' => variable_get('ae_drupal_api_key', '****************'),
'#size' => 16,
'#maxlength' => 16,
'#description' => t('The API key used to connect to Activation Engine.'),
'#required' => TRUE,
);
$form['ae_drupal_api_secret_key'] = array(
'#type' => 'textfield',
'#title' => t('Activation Engine API key secret'),
'#default_value' => variable_get('ae_drupal_api_secret_key', '****************'),
'#size' => 16,
'#maxlength' => 16,
'#description' => t('The API key secret used to connect to Activation Engine.'),
'#required' => TRUE,
);
$form['ae_drupal_api_url'] = array(
'#type' => 'textfield',
'#title' => t('Activation Engine API Endpoint Url'),
'#default_value' => variable_get('ae_drupal_api_url', 'http://aengine.net/api'),
'#size' => 20,
'#maxlength' => 25,
'#description' => t('The endpoint address used to connect to Activation Engine API.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* Page callback: AE Drupal page url save
*
* @see ae_drupal_menu()
*/
function ae_drupal_register_url() {
// Only do this for ajax requests
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
if (isset($_SESSION['aetoken'])) {
$callUrl = sprintf('%s/%s/publictoken/visitpage?page=%s',
variable_get('ae_drupal_api_url', AEURL),
$_SESSION['aetoken'],
isset($_POST['current_page']) ? $_POST['current_page'] : $_SERVER['PHP_SELF']
);
$result = drupal_http_request($callUrl, array(
'method' => 'GET',
'timeout' => 15,
));
if (isset($result->data)) {
$return = json_decode($result->data);
// If we have a message token return it
if (isset($return->msgtoken)) {
drupal_json_output($return->msgtoken);
}
// Else, if we have message carry on
if (isset($return->msg) && $return->msg == 'ok') {
drupal_exit();
}
}
}
}
}
/**
* Page callback: AE Drupal dashboard
*
* @see ae_drupal_menu()
*/
function ae_drupal_get_dashboard() {
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$contents = ae_drupal_get_contents();
drupal_json_output($contents);
}
}
/**
* Utility function for registering
* an user with Activation Engine
* The data is stored in session variables
*/
function ae_drupal_create_token($params) {
$activationengine = new ActivationEngine($params);
$userinfo = $activationengine->createUser(array('temp' => true));
if (isset($userinfo->token)) {
$_SESSION['aetoken'] = $userinfo->token;
$_SESSION['username'] = $userinfo->username;
return $userinfo->token;
} else {
return false;
}
}
/**
* This gets the AE content via an http request
* and returns the decoded result
*/
function ae_drupal_get_contents() {
$result = '';
$params = array(
'api_key' => variable_get('ae_drupal_api_key', 'eeccad7839f22abd'),
'api_secret_key' => variable_get('ae_drupal_api_secret_key', 'e4496cf12b40062a'),
'api_url' => variable_get('ae_drupal_api_url', AEURL),
);
if (!isset($_SESSION['aetoken'])) {
session_start();
$token = ae_drupal_create_token($params);
} else {
$activationengine = new ActivationEngine($params);
$test = $activationengine->testAccessToken($_SESSION['aetoken']);
$token = ($test != true) ? ae_drupal_create_token($params) : $_SESSION['aetoken'];
}
$callUrl = sprintf('%s/%s/publictoken/getpage?frompage=timotest', variable_get('ae_drupal_api_url', AEURL), $_SESSION['aetoken']);
$result = drupal_http_request($callUrl, array(
'method' => 'GET',
'timeout' => 15,
));
if (isset($result->data)) {
return json_decode($result->data);
}
return false;
}
/**
* This renders the html into a block
*/
function ae_drupal_block_view($delta = '') {
switch ($delta) {
case 'ae_drupal':
$block['subject'] = t('Activation Engine');
if (user_access('access content')) {
// Use our custom function to retrieve data.
// $result = ae_drupal_get_contents();
// Assign content to the block
// $block['content'] = $result ? stripslashes($result->dashboard) : t('Nothing to display at the moment.');
$content = ob_start();
?>
<div id="ae-dashboard" class="dropdown is-not-empty">
<div class="dropdown-toggle">
<div class="dropdown-toggle-title" id="ae-title">0 Bonus Points</div>
</div>
<div class="dropdown-menu left-hand" id="ae-content">
<div id="some_id">Loading...</div>
</div>
</div>
<?php
$block['content'] = ob_get_clean();
}
return $block;
}
}