/
glome.php
229 lines (200 loc) · 5.5 KB
/
glome.php
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
<?php
/**
* @package Glome Plugin for Wordpress
* @version 1.10
*/
/*
Plugin Name: Glome
Plugin URI: http://wordpress.org/plugins/glome/
Description: Glome Plugin for Wordpress
Version: 1.10
Author: Glome Oy - http://glome.me
*/
include plugin_dir_path(__FILE__) . 'includes/wp_bindings.php';
include plugin_dir_path(__FILE__) . 'includes/glome_api.php';
/**
* Language files
*/
function load_locales()
{
$domain = 'glome_plugin';
$locale = apply_filters('plugin_locale', get_locale(), $domain);
load_plugin_textdomain($domain, FALSE, plugin_basename(dirname(__FILE__)) . '/languages/');
}
add_action('init', 'load_locales');
/**
* Hook this to the activation phase
*/
function glome_plugin_activate()
{
add_option('glome_plugin_do_activation_redirect', true);
if (! function_exists ('register_post_status'))
{
deactivate_plugins(basename(dirname(__FILE__)) . '/' . basename(__FILE__));
exit;
}
update_option('glome_plugin_activation_message', 0);
// check and set the Glome API domain
// the function will sanitize the value; see glome_api.php
$domain = get_api_domain();
}
register_activation_hook(__FILE__, 'glome_plugin_activate');
/**
* Redirect to the Settings page upon succesful activation
*/
function glome_plugin_redirect()
{
if (get_option('glome_plugin_do_activation_redirect', false))
{
delete_option('glome_plugin_do_activation_redirect');
if( ! isset($_GET['activate-multi']))
{
wp_redirect('/wp-admin/options-general.php?page=glome_settings');
}
}
}
add_action('admin_init', 'glome_plugin_redirect');
/**
* Links to the Settings menu
*/
function glome_plugin_add_setup_link($links, $file)
{
static $glome_plugin = null;
if (is_null ($glome_plugin))
{
$glome_plugin = plugin_basename(__FILE__);
}
if ($file == $glome_plugin)
{
$settings_link = '<a href="users.php?page=glome-settings">' . _e('Setup', 'glome_plugin') . '</a>';
array_unshift ($links, $settings_link);
}
return $links;
}
add_filter('plugin_action_links', 'glome_plugin_add_setup_link', 10, 2);
include __DIR__ . '/includes/glome_plugin_admin_page.php';
include __DIR__ . '/includes/glome_plugin_profile_page.php';
/**
* Where it all begins; hooked to the init phase
*/
function glome_start()
{
$token = $glomeid = $current_user = false;
global $post;
if (session_status() != PHP_SESSION_ACTIVE)
{
$_SESSION['glome'] = array();
session_start();
}
// we don't save this into DB or file, no need to sanitze further
if (isset($_POST['one_time_access']))
{
$_SESSION['glome'] = glome_create_user();
}
if (array_key_exists('glome', $_SESSION) and
array_key_exists('token', $_SESSION['glome']) and
array_key_exists('glomeid', $_SESSION['glome']))
{
$token = $_SESSION['glome']['token'];
$glomeid = $_SESSION['glome']['glomeid'];
}
if (array_key_exists('magic', $_COOKIE) and strlen($_COOKIE['magic']) > 12)
{
// this is set after a succesful identification with Glome key
if (array_key_exists('key', $_SESSION['glome']))
{
$key = substr($_COOKIE['magic'], 0, 12);
if ($_SESSION['glome']['key']['code'] == $key)
{
$token = substr($_COOKIE['magic'], 12, 32);
$glomeid = substr($_COOKIE['magic'], 44);
}
}
}
if ($token and $glomeid)
{
if (mywp_user_exists($token) === false)
{
mywp_create_user($token, $glomeid);
}
mywp_login_user($token, $glomeid);
setcookie('magic', '', time() - 3600); /* delete */
redirect_if_needed();
}
// check Glome session
$ret = glome_get_user_profile();
if ($ret)
{
// is the Glome user locked?
$_SESSION['glome'] = $ret;
if (isset($ret['code']))
{
switch ($ret['code'])
{
case 403:
case 2301:
$ret = null;
break;
}
}
}
if (is_user_logged_in())
{
$current_user = wp_get_current_user();
}
if ($current_user && $ret == null)
{
if (! is_super_admin($current_user->ID))
{
//logout from Wordpress
wp_logout();
header('Location: /exit');
exit;
}
}
if (get_option('glome_activity_tracking'))
{
if ($current_user && $current_user->has_prop('glomeid') && $current_user->get('allow_tracking_me') == 1)
{
glome_track_activity($_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
redirect_if_needed();
}
}
return;
}
add_action('init', 'glome_start');
/**
* Internal method for auto reloading pages in certain cases
*/
function redirect_if_needed()
{
// check if redirect is requested
$url = '';
if (isset($_COOKIE['redirect']))
{
$url = $_COOKIE['redirect'];
setcookie('redirect', '', time() - 3600); /* delete */
}
else
{
if (isset($_GET['redirect_to']))
{
$url = $_GET['redirect_to'];
}
}
if (strlen($url) > 1)
{
wp_safe_redirect(admin_url($url), 301);
exit;
}
}
// this could be used to check if the WP account has a corresponding Glome ID
// add_action('set_current_user', 'glome_get_user_profile');
include plugin_dir_path(__FILE__) . 'includes/ui.php';
include plugin_dir_path(__FILE__) . 'includes/pairing_common.php';
include plugin_dir_path(__FILE__) . 'includes/one_time_login_widget.php';
include plugin_dir_path(__FILE__) . 'includes/scanner_widget.php';
include plugin_dir_path(__FILE__) . 'includes/show_qr_widget.php';
include plugin_dir_path(__FILE__) . 'includes/gnb_widget.php';
include plugin_dir_path(__FILE__) . 'includes/show_key_widget.php';
include plugin_dir_path(__FILE__) . 'includes/enter_key_widget.php';