1 | <? |
---|
2 | |
---|
3 | function action_gfc_auth_dist() { |
---|
4 | //ini_set("error_reporting", E_ALL); |
---|
5 | //ini_set("display_errors", 1); |
---|
6 | |
---|
7 | // Set the default timezone since many servers won't have this configured |
---|
8 | //date_default_timezone_set('America/Los_Angeles'); |
---|
9 | |
---|
10 | //get osapi info |
---|
11 | $display_name = $member_id = false; |
---|
12 | include_spip(_DIR_OSAPI.'osapi'); |
---|
13 | include_spip('inc/texte'); |
---|
14 | include_spip('base/abstract_sql'); |
---|
15 | $provider = new osapiFriendConnectProvider(); |
---|
16 | $auth = new osapiFCAuth($GLOBALS['gfc']['cookie_value']); |
---|
17 | $osapi = new osapi($provider, $auth); |
---|
18 | $strictMode = true; |
---|
19 | if ($osapi) { |
---|
20 | $request = $osapi->people->get(array('userId'=>'@me', 'groupId'=>'@self')); |
---|
21 | $batch = $osapi->newBatch(); |
---|
22 | $batch->add($request, 'me'); |
---|
23 | $result = $batch->execute(); |
---|
24 | $me = $result['me']; |
---|
25 | if ($me instanceof osapiError) { |
---|
26 | $code = $me->getErrorCode(); |
---|
27 | $message = $me->getErrorMessage(); |
---|
28 | //die("$code - $message"); |
---|
29 | } |
---|
30 | else{ |
---|
31 | $display_name = $me->getFieldByName("displayName"); |
---|
32 | $member_id = $me->getFieldByName("id"); |
---|
33 | if(trim($display_name) == '') $display_name = $member_id; |
---|
34 | } |
---|
35 | } |
---|
36 | //END get osapi info |
---|
37 | |
---|
38 | if($member_id){ |
---|
39 | //try to login SPIP if google friend account already binded |
---|
40 | if (login_spip($member_id)){} |
---|
41 | //elseif member already connected in SPIP... |
---|
42 | elseif($GLOBALS['auteur_session']['id_auteur']!=''){ |
---|
43 | //if he already has a gfc_id, we do nothing !!THIS IS BAD, WE NEED TO WORK ON THIS CASE!! |
---|
44 | $res = spip_query("select gfc_uid from spip_auteurs where id_auteur=".sql_quote($GLOBALS['auteur_session']['id_auteur'])." and gfc_uid!='' limit 1"); |
---|
45 | if(sql_count($res)==1){} |
---|
46 | // else we consider this is an attempt to bind a spip account to a google friend account, we automatically bind the 2 account |
---|
47 | else spip_query("update spip_auteurs set gfc_uid=".sql_quote($member_id)." where id_auteur=".sql_quote($GLOBALS['auteur_session']['id_auteur'])); |
---|
48 | } |
---|
49 | //if not connected to SPIP and gfc_id not in our system, we create a new SPIP account |
---|
50 | else{ |
---|
51 | $declaration = array(); |
---|
52 | $declaration['statut'] = 'nouveau'; |
---|
53 | $declaration['bio'] = 'forum'; |
---|
54 | $declaration['nom'] = safehtml($display_name); |
---|
55 | $declaration['login'] = $declaration['url_propre'] = ereg_replace("[^a-zA-Z0-9_]", "_", $display_name);; |
---|
56 | $declaration['email'] = $GLOBALS['gfc']['default_email']; |
---|
57 | $declaration['gfc_uid'] = $member_id; |
---|
58 | $n = sql_insert('spip_auteurs', ('(en_ligne,' .join(',',array_keys($declaration)).')'), ("(NOW()," .join(", ",array_map('sql_quote', $declaration)) .")")); |
---|
59 | $declaration['id_auteur'] = $n; |
---|
60 | |
---|
61 | //then we log user in |
---|
62 | login_spip($member_id); |
---|
63 | } |
---|
64 | } |
---|
65 | if($_SESSION["gfc"]["login_redirect"] != '') $url_retour = $_SESSION["gfc"]["login_redirect"]; |
---|
66 | else $url_retour = "/"; |
---|
67 | header("Location: $url_retour"); |
---|
68 | die(); |
---|
69 | } |
---|
70 | |
---|
71 | function login_spip($gfc_id, $spip_id=''){ |
---|
72 | if($gfc_id != '') $res = sql_select("*","spip_auteurs","gfc_uid=".sql_quote($gfc_id)." limit 1"); |
---|
73 | elseif($spip_id != '') $res = spip_query("select * from spip_auteurs where id_auteur=".sql_quote($spip_id)." limit 1"); |
---|
74 | if ($row = sql_fetch($res)){ |
---|
75 | $auth_source = 'gfc'; |
---|
76 | $row['auth'] = $auth_source; |
---|
77 | // create session |
---|
78 | $session = charger_fonction('session','inc'); |
---|
79 | $spip_session = $session($row); |
---|
80 | // create cookie |
---|
81 | $_COOKIE['spip_session'] = $spip_session; |
---|
82 | preg_match(',^[^/]*//[^/]*(.*)/$,', |
---|
83 | url_de_base(), |
---|
84 | $r); |
---|
85 | include_spip('inc/cookie'); |
---|
86 | spip_setcookie('spip_session', $spip_session, time() + 3600 * 24 * 14, $r[1]); |
---|
87 | // antentification |
---|
88 | $auth = charger_fonction('auth','inc'); |
---|
89 | $auth(); |
---|
90 | return true; |
---|
91 | } |
---|
92 | return false; |
---|
93 | } |
---|
94 | |
---|
95 | ?> |
---|