-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.php
More file actions
213 lines (171 loc) · 5.12 KB
/
common.php
File metadata and controls
213 lines (171 loc) · 5.12 KB
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
<?php
// via
// http://planetozh.com/blog/2008/07/what-plugin-coders-must-know-about-wordpress-26/
$root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($root.'/wp-load.php')) {
// WP 2.6
require_once($root.'/wp-load.php');
} else {
// Before 2.6
require_once($root.'/wp-config.php');
}
require_once($root . '/wp-includes/registration.php');
if (!class_exists('Facebook')) {
// prevent fatal due to multiple inclusions.
require_once('facebook-platform/php/facebook.php');
}
function _fbc_make_client() {
return new Facebook(get_option(FBC_APP_KEY_OPTION),
get_option(FBC_APP_SECRET_OPTION),
false,
'connect.facebook.com');
}
/*
* Get the facebook client object for easy access.
*/
function fbc_facebook_client() {
static $facebook = null;
if ($facebook === null) {
$facebook = _fbc_make_client();
}
return $facebook;
}
function fbc_api_client() {
return fbc_facebook_client()->api_client;
}
/**
provides an api client without a user session.
*/
function fbc_anon_api_client() {
$client = _fbc_make_client();
$client->user = 0;
$client->api_client->session_key = null;
return $client->api_client;
}
function fbc_get_displayname($userinfo) {
if (empty($userinfo['name'])) {
// i18n-able
return _(FBC_ANONYMOUS_DISPLAYNAME);
} else {
return $userinfo['name'];
}
}
function fbc_make_public_url($userinfo) {
if (empty($userinfo['name'])) {
// This user is hidden from search, so they dont get a url either
return null;
}
$fbuid = $userinfo['uid'];
$name = $userinfo['name'];
$under_name = str_replace(" ", "-", $name);
$clean_name = preg_replace('/[^A-Za-z0-9_\-]+/', '', $under_name);
$url = 'http://www.facebook.com/people/' . $clean_name . '/' . $fbuid;
return $url;
}
function render_fb_profile_pic($user) {
return <<<EOF
<div class="avatar avatar-50">
<fb:profile-pic uid="$user" facebook-logo="true" size="square"></fb:profile-pic>
</div>
EOF;
}
function render_fbconnect_button($onlogin=null) {
if ($onlogin !== null) {
$onlogin_str = ' onlogin="'. $onlogin .'" ';
} else {
$onlogin_str = '';
}
return <<<EOF
<div class="dark">
<fb:login-button size="large" background="white" length="short" $onlogin_str>
</fb:login-button>
</div>
EOF;
}
function get_wpuid_by_fbuid($fbuid) {
global $wpdb;
$sql = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'fbuid' AND meta_value = %s";
$res = $wpdb->get_results($wpdb->prepare($sql, $fbuid), ARRAY_A);
if ($res) {
return $res['user_id'];
} else {
return 0;
}
}
define('FBC_ERROR_NO_FB_SESSION', -2);
define('FBC_ERROR_USERNAME_EXISTS', -1);
function fbc_login_if_necessary($allow_link=false) {
$fbuid = fbc_facebook_client()->get_loggedin_user();
if ($fbuid) {
$wpuid = fbc_fbuser_to_wpuser($fbuid);
if (!$wpuid) {
// There is no wp user associated w/ this fbuid
$user = wp_get_current_user();
$wpuid = $user->ID;
if ($wpuid && $allow_link) {
// User already has a wordpress account, link to this facebook account
update_usermeta($wpuid, 'fbuid', "$fbuid");
} else {
// Create a new wordpress account
$wpuid = fbc_insert_user($fbuid);
if ($wpuid === FBC_ERROR_USERNAME_EXISTS) {
return FBC_ERROR_USERNAME_EXISTS;
}
}
} else {
// Already have a linked wordpress account, fall through and set
// login cookie
}
wp_set_auth_cookie($wpuid, true, false);
return $fbuid;
} else {
return FBC_ERROR_NO_FB_SESSION;
}
}
function get_user_by_meta($meta_key, $meta_value) {
global $wpdb;
$sql = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '%s' AND meta_value = '%s'";
return $wpdb->get_var($wpdb->prepare($sql, $meta_key, $meta_value));
}
function fbc_fbuser_to_wpuser($fbuid) {
return get_user_by_meta('fbuid', $fbuid);
}
function fbc_userinfo_to_wp_user($userinfo) {
return array(
'display_name' => fbc_get_displayname($userinfo),
'user_url' => fbc_make_public_url($userinfo),
// 'user_email' => $userinfo['proxied_email'],
'first_name' => $userinfo['first_name'],
'last_name' => $userinfo['last_name'],
);
}
function fbc_userinfo_keys() {
return array('name',
'first_name',
'last_name',
'proxied_email',
'profile_url');
}
function fbc_insert_user($fbuid) {
$userinfo = fbc_anon_api_client()->users_getInfo(array($fbuid),
fbc_userinfo_keys());
if ($userinfo === null) {
error_log('wp-fbconnect: empty query result for user ' . $fbuid);
}
$userinfo = $userinfo[0];
$fbusername = 'fb' . $fbuid;
if (username_exists($fbusername)) {
return FBC_ERROR_USERNAME_EXISTS;
}
$userdata = fbc_userinfo_to_wp_user($userinfo);
$userdata['user_pass'] = wp_generate_password();
$userdata['user_login'] = $fbusername;
$wpuid = wp_insert_user($userdata);
if($wpuid) {
wp_update_user(array('ID' => $wpuid,
'role' => 'subscriber'));
update_usermeta($wpuid, 'fbuid', "$fbuid");
}
return $wpuid;
}
?>