-
Notifications
You must be signed in to change notification settings - Fork 0
/
instart_logic.module
187 lines (172 loc) · 4.96 KB
/
instart_logic.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
<?php
/**
* @file
* Instart Logic module.
*/
/**
* Implements hook_permission().
*
* Return permissions for the Instart Logic module.
*/
function instart_logic_permission() {
$perms = array(
'administer instart logic' => array(
'title' => t('Administer Instart Logic'),
'description' => t('Allows users to administer Instart Logic.'),
'restrict access' => TRUE,
),
);
return $perms;
}
/**
* Implements hook_menu().
*/
function instart_logic_menu() {
$items = array();
$items['admin/config/services/instart_logic'] = array(
'title' => 'Instart Logic Configuration',
'description' => 'Instart Logic Configuration',
'page callback' => 'instart_logic_redirect',
'page arguments' => array('instart_logic_api_access'),
'access arguments' => array('administer instart logic'),
'file' => 'instart_logic.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/services/instart_logic/api'] = array(
'title' => 'API Access',
'description' => 'Instart Logic API Access',
'page callback' => 'drupal_get_form',
'page arguments' => array('instart_logic_api_access'),
'access arguments' => array('administer instart logic'),
'file' => 'instart_logic.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/services/instart_logic/purge'] = array(
'title' => 'Purge',
'description' => 'Purge URLs',
'page callback' => 'drupal_get_form',
'page arguments' => array('instart_logic_purge_form'),
'access arguments' => array('administer instart logic'),
'file' => 'instart_logic.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Utility to get a session. Creates session if needed.
*
* @param object $api
* API Instance.
*
* @return bool
* The session id or FALSE.
*/
function _instart_logic_get_session($api) {
$session_id = variable_get('instart_logic_session_id', '');
if (!$session_id || !$api->checkSession($session_id)) {
$session_id = _instart_logic_create_session($api);
}
return $session_id;
}
/**
* Utility to create a new session.
*
* @param object $api
* API instance.
*
* @return bool
* The session id or FALSE.
*/
function _instart_logic_create_session($api) {
$result = $api->createSession();
$data = $result->data;
$id = json_decode($data)->id;
variable_set('instart_logic_session_id', $id);
return $id;
}
/**
* Match the form customer name against the account customer name.
*
* @param string $response
* The json account object returned by the Account Management API.
* @param string $customername
* The customer name from the credentials form.
*
* @return bool
* TRUE if it's a match, FALSE otherwise.
*/
function _instart_logic_customername_match($response, $customername) {
if ($response && $response->code == '200') {
$account = json_decode($response->data);
return $account->customer_name == $customername;
}
return FALSE;
}
/**
* Implements hook_expire_cache().
*
* Provides integration with the Cache Expiration (expire) module.
*
* @see expire.api.inc
*/
function instart_logic_expire_cache($urls, $wildcards, $object_type, $object) {
$username = variable_get('instart_logic_username', '');
$password = variable_get('instart_logic_password', '');
$customername = variable_get('instart_logic_customername', '');
$external_domain = _instart_logic_get_external_domain();
$schemes = _instart_logic_get_schemes();
$api = new InstartLogic($username, $password, $customername);
try {
$session_id = _instart_logic_get_session($api);
}
catch (InstartLogicException $e) {
drupal_set_message($e->getMessage(), 'error');
return;
}
foreach ($urls as $url) {
foreach ($schemes as $scheme) {
$path = trim(parse_url($url, PHP_URL_PATH), '/');
$full_url = $scheme . '://' . $external_domain . '/' . $path;
$purge_urls = array(
$full_url => 'EXACT_LITERAL',
);
drupal_set_message(t('Purging @url', array('@url' => $full_url)));
try {
$api->purgeUrls($purge_urls, $session_id);
}
catch (InstartLogicPurgeException $e) {
drupal_set_message($e->getMessage(), 'error');
}
}
}
}
/**
* Return the external domain name.
*
* @return string
* The external domain name.
*/
function _instart_logic_get_external_domain() {
global $base_root;
$domain_name = parse_url($base_root, PHP_URL_HOST);
$external_domain_name = variable_get('instart_logic_external_domain_name', $domain_name);
return $external_domain_name;
}
/**
* Return the selected schemes to use in purging.
*
* @return array
* An array of schemes to purge.
*/
function _instart_logic_get_schemes() {
$schemes = array();
$external_base_http = variable_get('instart_logic_external_base_http', 1);
$external_base_https = variable_get('instart_logic_external_base_https', '');
if ($external_base_http) {
$schemes[] = 'http';
}
if ($external_base_https) {
$schemes[] = 'https';
}
return $schemes;
}