-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaiquiri.php
151 lines (118 loc) · 4.06 KB
/
daiquiri.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
<?php
/*
Plugin Name: Daiquiri framework integration
Description: Daiquiri framework integration
Author: Jochen Klar <[email protected]>
Version: 0.1
Text Domain: Daiquiri framework integration
*/
/*
* Helper function to log debug messages
*/
if (!function_exists('daiquiri_log')) {
function daiquiri_log($message)
{
if (DAIQUIRI_DEBUG) {
error_log($message);
}
}
}
/*
* Include daiquiri shortcodes
*/
require_once('daiquiri_navigation.php');
/*
* disable admin bar
*/
$show_admin_bar = false;
/*
* Automatiacally login the user which is logged in into daiquiri right now.
*/
add_action('init', 'daiquiri_auto_login');
function daiquiri_auto_login()
{
if (isset($_COOKIE['sessionid']) && !is_user_logged_in()) {
// check if WordPress is below Daiquiri
if (strpos(get_option('siteurl'), DAIQUIRI_URL) === false) {
echo '<h1>Error with daiquiri plugin</h1><p>Wordpress URL is not below Daiquiri URL. Please set the correct DAIQUIRI_URL in wp-config.php.</p>';
die(0);
}
// check which user is logged in into daiquiri right now
$url = rtrim(DAIQUIRI_URL, '/') . '/accounts/profile.json/';
require_once('HTTP/Request2.php');
$req = new HTTP_Request2($url);
$req->setMethod('GET');
$req->addCookie("sessionid", $_COOKIE["sessionid"]);
$req->setConfig(array(
'ssl_verify_peer' => false, // we trust the certificate here
'connect_timeout' => 2,
'timeout' => 3
));
try {
$response = $req->send();
$status = $response->getStatus();
$body = $response->getBody();
daiquiri_log($url . ' returned ' . $status);
} catch (HTTP_Request2_Exception $e) {
echo '<h1>Error with daiquiri plugin</h1><p>Error with HTTP request.</p>';
throw $e;
}
if ($status == 200) {
// decode the json to get the daiquiri user
$daiquiri_user = json_decode($body);
// get the wordpress user with the same username and log him/her in (or not)
$wordpress_user = get_user_by('login', $daiquiri_user->username);
if ($wordpress_user) {
// log him/her in
wp_set_current_user($wordpress_user->ID, $wordpress_user->user_login);
wp_set_auth_cookie($wordpress_user->ID);
do_action('wp_login', $wordpress_user->user_login);
daiquiri_log('"' . $wordpress_user->user_login . '" logged in');
} else {
echo '<h1>Error with daiquiri plugin</h1><p>User not found.</p>';
error_log('Daiquiri user "' . $daiquiri_user->username . '" does not exist in WordPress');
die(0);
}
}
}
}
/*
* Override the build in authentification of wordpress
*/
add_action('wp_authenticate', 'daiquiri_authenticate', 1, 2);
function daiquiri_authenticate($username, $password) {
require_once('./wp-includes/registration.php');
// get the login url
$url = rtrim(DAIQUIRI_URL, '/') . '/accounts/login/';
// append the redirect to query parameter
if ($_GET["redirect_to"]) {
$url .= '?next=' . $_GET["redirect_to"];
}
// just do the redirect
wp_redirect($url);
exit();
}
/*
* Hide the personal profile options.
*/
add_action('profile_personal_options', 'daiquiri_hide_start');
function daiquiri_hide_start() {
echo '<div style="display: none;"><!-- the following fields are hidden since a change to these values would be overwritten at the next login. -->';
}
add_action('show_user_profile', 'daiquiri_hide_end');
function daiquiri_hide_end() {
echo '</div><!-- hidden -->';
}
/*
* Log out of daiquiri when logging out of wordpress.
*/
add_action('wp_logout', 'daiquiri_logout');
function daiquiri_logout() {
$url = rtrim(DAIQUIRI_URL, '/') . '/accounts/logout/';
wp_redirect($url);
exit();
}
/*
* Disable emails send on email address change
*/
add_filter( 'send_password_change_email', '__return_false');