/
common.inc.php
284 lines (244 loc) · 13.2 KB
/
common.inc.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
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
<?php # $Id$
// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
include $probelang;
}
include_once dirname(__FILE__) . '/lang_en.inc.php';
function escape($message) {
return htmlspecialchars($message, ENT_QUOTES);
}
class serendipity_common_openid {
function redir_openidserver($openid_url, $store_path, $wfFlag=1) {
global $serendipity;
$path_extra = dirname(__FILE__).DIRECTORY_SEPARATOR.'PHP-openid/';
$path = ini_get('include_path');
$path = $path_extra . PATH_SEPARATOR . $path;
ini_set('include_path', $path);
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
$store = new Auth_OpenID_FileStore($store_path);
$consumer = new Auth_OpenID_Consumer($store);
$trust_root = $serendipity['baseURL'];
switch ($wfFlag) {
case 1:
$process_url = $trust_root . 'serendipity_admin.php?serendipity[openidflag]=1';
break;
case 3:
$process_url = $trust_root . 'serendipity_admin.php?serendipity[openidflag]=3'.
'&serendipity[adminModule]=event_display&serendipity[adminAction]=profiles';
break;
default:
$process_url = $trust_root . $serendipity['indexFile'] . '?serendipity[subpage]=addopenid&serendipity[openidflag]=2';
}
$auth_request = $consumer->begin($openid_url);
if (!$auth_request) {
return FALSE;
}
$auth_request->addExtensionArg('sreg', 'required', 'fullname');
$auth_request->addExtensionArg('sreg', 'required', 'email');
$redirect_url = $auth_request->redirectURL($trust_root,
$process_url);
header('Status: 302 Found');
header("Location: ".$redirect_url);
exit;
}
static function reauth_openid() {
global $serendipity;
if (isset($_SESSION['serendipityOpenID']) && $_SESSION['serendipityOpenID']) {
$serendipity['serendipityRealname'] = $_SESSION['serendipityRealname'];
$serendipity['serendipityUser'] = $_SESSION['serendipityUser'];
$serendipity['serendipityPassword'] = $_SESSION['serendipityPassword'];
$serendipity['serendipityEmail'] = $_SESSION['serendipityEmail'];
$serendipity['authorid'] = $_SESSION['serendipityAuthorid'];
$serendipity['serendipityUserlevel'] = $_SESSION['serendipityUserlevel'];
$serendipity['serendipityAuthedUser'] = $_SESSION['serendipityAuthedUser'];
$serendipity['serendipityRightPublish'] = $_SESSION['serendipityRightPublish'];
serendipity_load_configuration($serendipity['authorid']);
return true;
}
return false;
}
function authenticate_openid($getData, $store_path, $returnData = false) {
global $serendipity;
$trust_root = $serendipity['baseURL'] . 'serendipity_admin.php';
$path_extra = dirname(__FILE__).DIRECTORY_SEPARATOR.'PHP-openid';
$path = ini_get('include_path');
$path = $path_extra . PATH_SEPARATOR . $path;
ini_set('include_path', $path);
require_once("Auth/OpenID/Consumer.php");
require_once("Auth/OpenID/FileStore.php");
require_once("Auth/OpenID/SReg.php");
require_once("Auth/OpenID/PAPE.php");
$store = new Auth_OpenID_FileStore($store_path);
$consumer = new Auth_OpenID_Consumer($store);
$response = $consumer->complete($trust_root); //, $getData);
if ($response->status == Auth_OpenID_CANCEL) {
$success = 'Verification cancelled.';
} else if ($response->status == Auth_OpenID_FAILURE) {
$success = "OpenID authentication failed: " . $response->message;
} else if ($response->status == Auth_OpenID_SUCCESS) {
// This means the authentication succeeded; extract the
// identity URL and Simple Registration data (if it was
// returned).
$openid = $response->getDisplayIdentifier();
$esc_identity = escape($openid);
$success = sprintf('You have successfully verified ' .
'<a href="%s">%s</a> as your identity.',
$esc_identity, $esc_identity);
if ($response->endpoint->canonicalID) {
$escaped_canonicalID = escape($response->endpoint->canonicalID);
$success .= ' (XRI CanonicalID: '.$escaped_canonicalID.') ';
}
$sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
$sreg = $sreg_resp->contents();
if (@$sreg['email']) {
escape($sreg['email']);
$success .= " You also returned '".escape($sreg['email']).
"' as your email.";
}
if (@$sreg['nickname']) {
$success .= " Your nickname is '".escape($sreg['nickname']).
"'.";
}
if (@$sreg['fullname']) {
$success .= " Your fullname is '".escape($sreg['fullname']).
"'.";
}
}
if (! empty($openid)) {
if ($returnData) {
return array('realname'=>$realname, 'email'=>$email, 'openID'=>$openid);
}
$password = md5($openid);
$query = "SELECT DISTINCT a.email, a.authorid, a.userlevel, a.right_publish, a.realname
FROM
{$serendipity['dbPrefix']}authors AS a, {$serendipity['dbPrefix']}openid_authors AS oa
WHERE
oa.openid_url = '".serendipity_db_escape_string($openid)."' and
oa.authorid = a.authorid";
$row = serendipity_db_query($query, true, 'assoc');
if (is_array($row)) {
serendipity_setCookie('old_session', session_id());
serendipity_setAuthorToken();
$_SESSION['serendipityUser'] = $serendipity['serendipityUser'] = $row['realname'];
$_SESSION['serendipityPassword'] = $serendipity['serendipityPassword'] = $password;
$_SESSION['serendipityEmail'] = $serendipity['serendipityEmail'] = $email;
$_SESSION['serendipityAuthorid'] = $serendipity['authorid'] = $row['authorid'];
$_SESSION['serendipityUserlevel'] = $serendipity['serendipityUserlevel'] = $row['userlevel'];
$_SESSION['serendipityAuthedUser'] = $serendipity['serendipityAuthedUser'] = true;
$_SESSION['serendipityRightPublish']= $serendipity['serendipityRightPublish'] = $row['right_publish'];
$_SESSION['serendipityRealname'] = $serendipity['serendipityRealname'] = $row['realname'];
$_SESSION['serendipityOpenID'] = true;
serendipity_load_configuration($serendipity['authorid']);
return true;
} else {
$_SESSION['serendipityAuthedUser'] = false;
@session_destroy();
}
}
return false;
}
function getOpenID($userID, $checkExist=false) {
global $serendipity;
$q = "SELECT openid_url, authorid FROM {$serendipity['dbPrefix']}openid_authors WHERE authorid = " . (int)$userID;
$author = serendipity_db_query($q, true);
if (is_array($author)) {
if ($checkExist) {
return $author['authorid'];
} elseif (! empty($author['openid_url'])) {
return $author['openid_url'];
}
}
return '';
}
function updateOpenID($openid_url, $authorID) {
global $serendipity;
if (!is_array(serendipity_db_query("SELECT username FROM {$serendipity['dbPrefix']}openid_authors LIMIT 1", true, 'both', false, false, false, true))) {
serendipity_db_schema_import("CREATE TABLE {$serendipity['dbPrefix']}openid_authors (
openid_url varchar(255) default null,
hash varchar(32) default null,
authorid int(11) default '0'
);");
}
$hash = md5($openid_url);
if (serendipity_common_openid::getOpenID($authorID, true)) {
$retVal = serendipity_db_update('openid_authors',
array('authorid'=>$authorID),
array('openid_url'=> $openid_url,
'hash'=> $hash));
} else {
$retVal = serendipity_db_insert('openid_authors',
array('openid_url'=> $openid_url,
'hash'=> $hash,
'authorid'=>$authorID));
}
return ($retVal===true)?true:false;
}
function load_account_selectbox() {
global $serendipity;
$query = "SELECT DISTINCT a.realname, a.username, oa.openid_url
FROM
{$serendipity['dbPrefix']}authors AS a, {$serendipity['dbPrefix']}openid_authors AS oa
WHERE
oa.authorid = a.authorid";
$rows = serendipity_db_query($query);
// Singnal no existing OpenID URL.
if (!is_array($rows) || count($rows)==0) return false;
$result = '<select name="serendipity[openid_url]">';
foreach ($rows as $row) {
$result .= '<option value="' . $row['openid_url'] . '">';
if (!empty($row['realname'])) {
$result .= $row['realname'];
}
else {
$result .= $row['username'];
}
$result .= '</option>';
}
$result .= '</select> ';
return $result;
}
function loginform($url, $hidden = array(), $useAutorSelector = true) {
global $serendipity;
$imgopenid = $serendipity['baseURL'] . 'index.php?/plugin/openid.png';
// Check, if we have any user with OpenID configured
$select = serendipity_common_openid::load_account_selectbox();
if ($select===false) { // No we don't. Say so
$result = '<div class="no_openid_user">';
$result .= '<img src="' . $imgopenid . '" alt="OpenID">';
$result .= '<p>' . PLUGIN_OPENID_LOGIN_NOOPENID . '</p></div>';
return $result;
}
$imggoogle = $serendipity['baseURL'] . 'index.php?/plugin/oid_google.png';
$imgyahoo = $serendipity['baseURL'] . 'index.php?/plugin/oid_yahoo.png';
$imgaol = $serendipity['baseURL'] . 'index.php?/plugin/oid_aol.png';
$form = '';
// We need two forms in order to allow ENTER in the input line
$form .= '<form name="openid" id="openid" method="post" action="' . $url . '">';
$form .='<input type="hidden" name="serendipity[openidflag]" value="1" />';
foreach($hidden AS $key => $val) {
$form .= '<input type="hidden" name="serendipity[' . $key . ']" value="' . htmlspecialchars($val) . '" />';
}
$form .= '<img src="' . $imgopenid . '" alt="OpenID"> ';
if ($useAutorSelector) {
$form .= $select;
$form .= '<input type="submit" name="openIDLogin" value="Login with OpenID" />';
}
if (!$useAutorSelector) {
$form .= '<input type="text" size="40" name="serendipity[openid_url]" value="" placeholder="' . PLUGIN_OPENID_LOGIN_INPUT . '"/>';
$form .= '<input type="submit" name="openIDLogin" value="Login" />';
$form .= '</form>';
$form .= '<form name="openid" id="openid" method="post" action="' . $url . '">';
$form .='<input type="hidden" name="serendipity[openidflag]" value="1" />';
foreach($hidden AS $key => $val) {
$form .= '<input type="hidden" name="serendipity[' . $key . ']" value="' . htmlspecialchars($val) . '" />';
}
$form .= '<input name="openIDLoginGoogle" type="image" src="' . $imggoogle . '" alt="' . PLUGIN_OPENID_LOGIN_WITH_GOOGLE . '" title="' . PLUGIN_OPENID_LOGIN_WITH_GOOGLE .'"/> ';
$form .= '<input name="openIDLoginYahoo" type="image" src="' . $imgyahoo . '" alt="' . PLUGIN_OPENID_LOGIN_WITH_YAHOO . '" title="' . PLUGIN_OPENID_LOGIN_WITH_YAHOO .'"/> ';
$form .= '<input name="openIDLoginAol" type="image" src="' . $imgaol . '" alt="' . PLUGIN_OPENID_LOGIN_WITH_AOL . '" title="' . PLUGIN_OPENID_LOGIN_WITH_AOL .'"/> ';
}
$form .= '</form>';
return $form;
}
}