-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstapage.module
343 lines (302 loc) · 11 KB
/
instapage.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
<?php
define('INSTAPAGE_ENDPOINT', 'http://app.myinstapage.com');
define('INSTAPAGE_VERSION', '1.6.6');
/**
* Implements hook_permission().
*/
function instapage_permission() {
return array(
'administer instapage settings' => array(
'title' => t('Administer Instapage settings'),
'description' => t('Allow users to administer Instapage settings.'),
),
'administer instapage landing pages' => array(
'title' => t('Administer Instapage'),
'description' => t('Allow users to administer Instapage landing pages.'),
),
'access instapage landing pages' => array(
'title' => t('Access Instapage landing pages'),
'description' => t('Allow users to access Instapage landing pages.'),
),
);
}
/**
* Implements hook_menu().
*/
function instapage_menu() {
$items = array();
$items['admin/config/services/instapage'] = array(
'title' => 'Instapage',
'description' => 'Administer Instapage settings.',
'access arguments' => array('administer instapage settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('instapage_admin_settings_form'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/structure/instapage'] = array(
'title' => 'Instapage landing pages',
'description' => 'Manage Instapage landing pages.',
'access arguments' => array('administer instapage landing pages'),
'page callback' => 'drupal_get_form',
'page arguments' => array('instapage_pages_form'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implement hook_menu_alter().
*/
function instapage_menu_alter(&$callbacks) {
$pages = variable_get('instapage_pages', NULL);
if (!empty($pages)) {
foreach ($pages as $instapage_id => $path) {
$callbacks[$path] = array(
'title' => 'Title',
'access arguments' => array('access instapage landing pages'),
'page callback' => 'instapage_page_callback',
'page arguments' => array((string)$instapage_id),
'type' => MENU_CALLBACK,
);
}
}
}
/**
* Page callback.
*/
function instapage_page_callback($instapage_page_id) {
$url = INSTAPAGE_ENDPOINT .'/server/view-by-id/'. $instapage_page_id;
$result = drupal_http_request($url);
print $result->data;
}
/**
* Form to edit Instapage pages.
*/
function instapage_admin_settings_form($form_state) {
$form = array();
$instapage_user_id = variable_get('instapage_user_id', false);
$plugin_hash = variable_get('instapage_plugin_hash', false);
if ($instapage_user_id && $plugin_hash) {
$data = array(
'id' => $instapage_user_id,
'plugin_hash' => $plugin_hash,
);
$result = instapage_api_call('get-user', $data, 'GET');
if ( isset( $result->error ) && $result->error == 1 ) {
form_set_error('form', t('Error from Instapage API: @instapage_msg', array('@instapage_msg' => $result->error_message)));
$form['instapage_user_email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#required' => TRUE,
);
$form['instapage_user_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#required' => TRUE,
);
$form['#validate'][] = 'instapage_admin_settings_form_login_validate';
$form['#submit'][] = 'instapage_admin_settings_form_login_submit';
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
return $form;
}
$form['info']['#markup'] = t('You are logged in as @user.', array('@user' => $result->user));
$form['info']['#markup'] .= '<p>' . t('Administer your pages <a href="@link">here</a>.', array('@link' => url('admin/structure/instapage'))) . '</p>';
$form['#submit'][] = 'instapage_admin_settings_form_disconnect_submit';
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Disconnect'));
}
else {
$form['instapage_user_email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#required' => TRUE,
);
$form['instapage_user_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#required' => TRUE,
);
$form['#validate'][] = 'instapage_admin_settings_form_login_validate';
$form['#submit'][] = 'instapage_admin_settings_form_login_submit';
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
}
return $form;
}
/**
* Validate Instapage username and password.
*/
function instapage_admin_settings_form_login_validate($form, &$form_state) {
$data = array(
'email' => base64_encode(trim($form_state['values']['instapage_user_email'])),
'password' => base64_encode(trim($form_state['values']['instapage_user_password'])),
);
$result = instapage_api_call('user-login', $data);
if ($result) {
if (isset($result->success)) {
$validate_results = array();
$validate_results['user_id'] = $result->data->user_id;
$validate_results['plugin_hash'] = $result->data->plugin_hash;
$form_state['values']['validate_results'] = $validate_results;
}
else {
if (isset($result->error) && $result->error == true) {
form_set_error('form', t('Error from Instapage API: @instapage_msg', array('@instapage_msg' => $result->error_message)));
}
else {
form_set_error('form', $result->error_message);
}
}
}
}
/**
* Disconnect Instapage user account.
*/
function instapage_admin_settings_form_disconnect_submit($form, &$form_state) {
variable_set('instapage_user_id', false);
variable_set('instapage_plugin_hash', false);
}
/**
* Save the User ID and Plugin hash
*/
function instapage_admin_settings_form_login_submit($form, &$form_state) {
if (isset($form_state['values']['validate_results'])) {
variable_set('instapage_user_id', $form_state['values']['validate_results']['user_id']);
variable_set('instapage_plugin_hash', $form_state['values']['validate_results']['plugin_hash']);
}
}
/**
* Form for setting up Instapage account.
*/
function instapage_pages_form($form_state) {
global $base_url;
$form = array();
$instapage_user_id = variable_get('instapage_user_id', false);
$plugin_hash = variable_get('instapage_plugin_hash', false);
if (!$instapage_user_id && !$plugin_hash) {
$form['notice']['#markup'] = '<p>' . t('You don\'t have the Instapage account setup yet.') . '</p>';
$form['notice']['#markup'] .= '<p>' . t('Please connect your account <a href="@link">here</a>.', array('@link' => url('admin/config/services/instapage'))) . '</p>';
return $form;
}
$data = array(
'user_id' => $instapage_user_id,
'plugin_hash' => $plugin_hash,
);
$result = instapage_api_call('my-pages/drupal', $data);
if (isset($result->success)) {
if (empty($result->data->pages)){
$form['#prefix'] = '<p>' . t('The list of pages is empty. Add a page on Instapage first.') . '</p>';
}
else {
$form['#prefix'] = '<p>' . t('This is the list of all pages on Instapage. Type the path on which you want to show Instapage page.') . '</p>';
$form['#prefix'] .= '<p>' . t('If you want to show Instapage as a front page, choose a path (for example: front) and then change default front page path <a href="@link">here</a>.', array('@link' => url('admin/config/system/site-information'))) . '</p>';
$pages = variable_get('instapage_pages', array());
$form['instapage_pages'] = array(
'#tree' => TRUE
);
foreach ($result->data->pages as $page) {
$page_url = explode( '/', $page->url );
$page_url = isset( $page_url[1] ) ? $page_url[1] : '';
$form['instapage_pages'][$page->id] = array(
'#type' => 'textfield',
'#title' => $page->title,
'#field_prefix' => $base_url . '/',
'#default_value' => isset($pages[$page->id]) ? $pages[$page->id] : $page_url,
);
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
}
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
return $form;
}
/**
* Checks if URLs are avaliable.
*/
function instapage_pages_form_validate($form, &$form_state) {
if (isset($form_state['values']['instapage_pages'])) {
$paths = array();
$pages = variable_get('instapage_pages', NULL);
foreach ($form_state['values']['instapage_pages'] as $instapage_id => $path) {
$error_field_name = 'instapage_pages][' . $instapage_id;
$menu_item = menu_get_item($path);
if ($menu_item && !in_array($path, $pages)) {
form_set_error($error_field_name, t('The path @path is already used.', array('@path' => $path)));
}
if (in_array($path, $paths)) {
if ($path != ''){
form_set_error($error_field_name, t('Path @path is not unique on the list.', array('@path' => $path)));
}
}
$paths[] = $path;
}
}
else {
form_set_error('form', t('Values are missing.'));
}
}
/**
* Saves Instapage entries to a variable.
*/
function instapage_pages_form_submit($form, &$form_state) {
$pages = array();
if (isset($form_state['values']['instapage_pages'])) {
foreach ($form_state['values']['instapage_pages'] as $instapage_id => $path) {
if ($path != '') {
$pages[$instapage_id] = $path;
// Let Instapage know about the path.
$instapage_user_id = variable_get('instapage_user_id', false);
$plugin_hash = variable_get('instapage_plugin_hash', false);
global $base_url;
$url = $base_url . '/' . $path;
$data = array(
'user_id' => $instapage_user_id,
'plugin_hash' => $plugin_hash,
'page_id' => $instapage_id,
'url' => str_replace('http://', '', $url)
);
instapage_api_call('update-page', $data);
}
}
}
variable_set('instapage_pages', $pages);
menu_rebuild();
drupal_set_message(t('All pages have been saved.'));
}
/**
* Instapage API call.
*/
function instapage_api_call($service, $data, $method = 'POST') {
$url = INSTAPAGE_ENDPOINT .'/ajax/services/' . $service . '/';
if ($method == 'GET' && !empty( $data )) {
foreach($data as $param_key => $param_value) {
$params[] = $param_key . '=' . $param_value;
}
if (!empty( $params )) {
$url .= '?' . implode( '&', $params );
}
}
$request_body = array(
'service-type' => 'Drupal',
'service' => $_SERVER['SERVER_NAME'],
'version' => INSTAPAGE_VERSION,
'user_id' => variable_get('instapage_user_id', false),
'data' => $data,
);
$data_query = http_build_query($request_body, null, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
if ($server_output) {
return json_decode($server_output);
}
else {
return false;
}
}