-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_facebook.php
129 lines (110 loc) · 2.43 KB
/
ml_facebook.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
<?php
include_once dirname( __FILE__ ) . '/libs/fb/facebook.php';
function ml_facebook()
{
global $ml_fb_app_id, $ml_fb_secret_key;
if($ml_fb_app_id == NULL || $ml_fb_secret_key == NULL) return NULL;
try
{
$config = array();
$config['appId'] = $ml_fb_app_id;
$config['secret'] = $ml_fb_secret_key;
$config['fileUpload'] = false; // optional
$config['cookie'] = false;
$facebook = new Facebook($config);
}
catch(Exception $e)
{
return NULL;
}
return $facebook;
}
function ml_facebook_get_app_info()
{
global $ml_fb_app_id, $ml_fb_app_info;
$facebook = ml_facebook();
if($facebook == NULL) return NULL;
if($ml_fb_app_info == NULL)
{
try
{
$ml_fb_app_info = $facebook->api("/$ml_fb_app_id");
}catch(Exception $e)
{
return NULL;
}
}
if($ml_fb_app_id == NULL)
{
return NULL;
}
return $ml_fb_app_info;
}
function ml_facebook_register_user_with_token($token=NULL)
{
global $wpdb;
$table_name = $wpdb->prefix . "mobiloud_fb_users";
$facebook = ml_facebook();
if($facebook == NULL) return NULL;
$facebook->setAccessToken($token);
$fb_id = $facebook->getUser();
$num = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE fb_id = $fb_id" ));
$email = NULL;
$name = NULL;
$user_profile = $facebook->api("/me");
if($user_profile)
{
$email = $user_profile['email'];
$name = $user_profile['name'];
}
if($email)
{
//check if is already registered
if($num == 1)
{
//already registered
//updating email
$wpdb->update($table_name,
array(
'email' => $email,
'name' => $name
),
array('fb_id' => $fb_id)
);
}
else if($fb_id != NULL && $fb_id != 0 && $fb_id != "0")
{
//not registered
$wpdb->insert(
$table_name,
array(
'fb_id' => "$fb_id",
'email' => $email,
'name' => $name
)
);
}
else return array();
return array('fb_id' => $fb_id, 'email' => $email);
}
else return array();
}
function ml_facebook_get_user_from_email($email)
{
global $wpdb;
$table_name = $wpdb->prefix . "mobiloud_fb_users";
$local_users = $wpdb->get_results("SELECT * FROM $table_name WHERE email = '$email'");
if(count($local_users) > 0) return $local_users[0];
else return NULL;
}
function ml_facebook_get_picture_url($email,$size="square")
{
$user = ml_facebook_get_user_from_email($email);
if($user)
{
$fb_id = $user->fb_id;
return "https://graph.facebook.com/$fb_id/picture?type=$size";
}
return NULL;
}
?>